blob: ef4d90ed896d8a7f0af09d387b693c2c723dc4af [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
Boris Brezillon7e534322018-09-06 14:05:22 +0200257 * @chip: NAND chip object
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 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200261static uint8_t nand_read_byte(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200263 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Masanari Iida064a7692012-11-09 23:20:58 +0900267 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200268 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700270 * Default read function for 16bit buswidth with endianness conversion.
271 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200273static uint8_t nand_read_byte16(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200275 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * nand_select_chip - [DEFAULT] control CE line
Boris Brezillon758b56f2018-09-06 14:05:24 +0200280 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700281 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * Default select function for 1 chip devices.
284 */
Boris Brezillon758b56f2018-09-06 14:05:24 +0200285static void nand_select_chip(struct nand_chip *chip, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200287 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case -1:
Boris Brezillon0f808c12018-09-06 14:05:26 +0200289 chip->cmd_ctrl(chip, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293
294 default:
295 BUG();
296 }
297}
298
299/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100300 * nand_write_byte - [DEFAULT] write single byte to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200301 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100302 * @byte: value to write
303 *
304 * Default function to write a byte to I/O[7:0]
305 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200306static void nand_write_byte(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100307{
Boris Brezillonc0739d82018-09-06 14:05:23 +0200308 chip->write_buf(chip, &byte, 1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100309}
310
311/**
312 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
Boris Brezillonc0739d82018-09-06 14:05:23 +0200313 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100314 * @byte: value to write
315 *
316 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
317 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200318static void nand_write_byte16(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100319{
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100320 uint16_t word = byte;
321
322 /*
323 * It's not entirely clear what should happen to I/O[15:8] when writing
324 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
325 *
326 * When the host supports a 16-bit bus width, only data is
327 * transferred at the 16-bit width. All address and command line
328 * transfers shall use only the lower 8-bits of the data bus. During
329 * command transfers, the host may place any value on the upper
330 * 8-bits of the data bus. During address transfers, the host shall
331 * set the upper 8-bits of the data bus to 00h.
332 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100333 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100334 * four parameters are specified to be written to I/O[7:0], but this is
335 * neither an address nor a command transfer. Let's assume a 0 on the
336 * upper I/O lines is OK.
337 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200338 chip->write_buf(chip, (uint8_t *)&word, 2);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100339}
340
341/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * nand_write_buf - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200343 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700344 * @buf: data buffer
345 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700347 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200349static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Alexander Shiyan76413832013-04-13 09:32:13 +0400351 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000355 * nand_read_buf - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200356 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700357 * @buf: buffer to store date
358 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700360 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200362static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Alexander Shiyan76413832013-04-13 09:32:13 +0400364 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * nand_write_buf16 - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200369 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700370 * @buf: data buffer
371 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700373 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200375static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf,
376 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Alexander Shiyan76413832013-04-13 09:32:13 +0400380 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200385 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700386 * @buf: buffer to store date
387 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700389 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200391static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Alexander Shiyan76413832013-04-13 09:32:13 +0400395 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200400 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700401 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000403 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200405static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200407 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900408 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900409 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Brian Norris5fb15492011-05-31 16:31:21 -0700411 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700412 ofs += mtd->erasesize - mtd->writesize;
413
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100414 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900415 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100416
Masahiro Yamadac120e752017-03-23 05:07:01 +0900417 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200418 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530419 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900420 return res;
421
422 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000423
Brian Norriscdbec052012-01-13 18:11:48 -0800424 if (likely(chip->badblockbits == 8))
425 res = bad != 0xFF;
426 else
427 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900428 if (res)
429 return res;
430 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200431
Masahiro Yamadac120e752017-03-23 05:07:01 +0900432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700436 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200437 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700438 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700440 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700441 * specific driver. It provides the details for writing a bad block marker to a
442 * block.
443 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200444static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700445{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200446 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700447 struct mtd_oob_ops ops;
448 uint8_t buf[2] = { 0, 0 };
449 int ret = 0, res, i = 0;
450
Brian Norris0ec56dc2015-02-28 02:02:30 -0800451 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700452 ops.oobbuf = buf;
453 ops.ooboffs = chip->badblockpos;
454 if (chip->options & NAND_BUSWIDTH_16) {
455 ops.ooboffs &= ~0x01;
456 ops.len = ops.ooblen = 2;
457 } else {
458 ops.len = ops.ooblen = 1;
459 }
460 ops.mode = MTD_OPS_PLACE_OOB;
461
462 /* Write to first/last page(s) if necessary */
463 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
464 ofs += mtd->erasesize - mtd->writesize;
465 do {
466 res = nand_do_write_oob(mtd, ofs, &ops);
467 if (!ret)
468 ret = res;
469
470 i++;
471 ofs += mtd->writesize;
472 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
473
474 return ret;
475}
476
477/**
478 * nand_block_markbad_lowlevel - mark a block bad
479 * @mtd: MTD device structure
480 * @ofs: offset from device start
481 *
482 * This function performs the generic NAND bad block marking steps (i.e., bad
483 * block table(s) and/or marker(s)). We only allow the hardware driver to
484 * specify how to write bad block markers to OOB (chip->block_markbad).
485 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700486 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300487 *
Brian Norrise2414f42012-02-06 13:44:00 -0800488 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700489 * (2) write bad block marker to OOB area of affected block (unless flag
490 * NAND_BBT_NO_OOB_BBM is present)
491 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300492 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700493 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800494 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700496static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100498 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700499 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000500
Brian Norrisb32843b2013-07-30 17:52:59 -0700501 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800502 struct erase_info einfo;
503
504 /* Attempt erase before marking OOB */
505 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800506 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300507 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200508 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800509
Brian Norrisb32843b2013-07-30 17:52:59 -0700510 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800511 nand_get_device(mtd, FL_WRITING);
Boris Brezillonc17556f2018-09-06 14:05:25 +0200512 ret = chip->block_markbad(chip, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300513 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200514 }
Brian Norrise2414f42012-02-06 13:44:00 -0800515
Brian Norrisb32843b2013-07-30 17:52:59 -0700516 /* Mark block bad in BBT */
517 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200518 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800519 if (!ret)
520 ret = res;
521 }
522
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200523 if (!ret)
524 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300525
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000529/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700531 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700533 * Check, if the device is write protected. The function expects, that the
534 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100536static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100538 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100539 u8 status;
540 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200541
Brian Norris8b6e50c2011-05-25 14:59:01 -0700542 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200543 if (chip->options & NAND_BROKEN_XD)
544 return 0;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100547 ret = nand_status_op(chip, &status);
548 if (ret)
549 return ret;
550
551 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800555 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * @mtd: MTD device structure
557 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300558 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800559 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300560 */
561static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
562{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100563 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300564
565 if (!chip->bbt)
566 return 0;
567 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200568 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300569}
570
571/**
572 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
573 * @mtd: MTD device structure
574 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700575 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 *
577 * Check, if the block is bad. Either by reading the bad block table or
578 * calling of the scan function.
579 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530580static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100582 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000583
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200584 if (!chip->bbt)
Boris Brezillonc17556f2018-09-06 14:05:25 +0200585 return chip->block_bad(chip, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200588 return nand_isbad_bbt(chip, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200591/**
592 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700593 * @mtd: MTD device structure
594 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200595 *
596 * Helper function for nand_wait_ready used when needing to wait in interrupt
597 * context.
598 */
599static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
600{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100601 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200602 int i;
603
604 /* Wait for the device to get ready */
605 for (i = 0; i < timeo; i++) {
Boris Brezillon50a487e2018-09-06 14:05:27 +0200606 if (chip->dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200607 break;
608 touch_softlockup_watchdog();
609 mdelay(1);
610 }
611}
612
Alex Smithb70af9b2015-10-06 14:52:07 +0100613/**
614 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200615 * @chip: NAND chip object
Alex Smithb70af9b2015-10-06 14:52:07 +0100616 *
617 * Wait for the ready pin after a command, and warn if a timeout occurs.
618 */
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200619void nand_wait_ready(struct nand_chip *chip)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000620{
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200621 struct mtd_info *mtd = nand_to_mtd(chip);
Alex Smithb70af9b2015-10-06 14:52:07 +0100622 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000623
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200624 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100625 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200626
Brian Norris7854d3f2011-06-23 14:12:08 -0700627 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100628 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000629 do {
Boris Brezillon50a487e2018-09-06 14:05:27 +0200630 if (chip->dev_ready(chip))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300631 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100632 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000633 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100634
Boris Brezillon50a487e2018-09-06 14:05:27 +0200635 if (!chip->dev_ready(chip))
Brian Norris9ebfdf52016-03-04 17:19:23 -0800636 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000637}
David Woodhouse4b648b02006-09-25 17:05:24 +0100638EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200641 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
642 * @mtd: MTD device structure
643 * @timeo: Timeout in ms
644 *
645 * Wait for status ready (i.e. command done) or timeout.
646 */
647static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
648{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100649 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100650 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200651
652 timeo = jiffies + msecs_to_jiffies(timeo);
653 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100654 u8 status;
655
656 ret = nand_read_data_op(chip, &status, sizeof(status), true);
657 if (ret)
658 return;
659
660 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200661 break;
662 touch_softlockup_watchdog();
663 } while (time_before(jiffies, timeo));
664};
665
666/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100667 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
668 * @chip: NAND chip structure
669 * @timeout_ms: Timeout in ms
670 *
671 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
672 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
673 * returned.
674 *
675 * This helper is intended to be used when the controller does not have access
676 * to the NAND R/B pin.
677 *
678 * Be aware that calling this helper from an ->exec_op() implementation means
679 * ->exec_op() must be re-entrant.
680 *
681 * Return 0 if the NAND chip is ready, a negative error otherwise.
682 */
683int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
684{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200685 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100686 u8 status = 0;
687 int ret;
688
689 if (!chip->exec_op)
690 return -ENOTSUPP;
691
Boris Brezillon3057fce2018-05-04 21:24:31 +0200692 /* Wait tWB before polling the STATUS reg. */
693 timings = nand_get_sdr_timings(&chip->data_interface);
694 ndelay(PSEC_TO_NSEC(timings->tWB_max));
695
Miquel Raynal8878b122017-11-09 14:16:45 +0100696 ret = nand_status_op(chip, NULL);
697 if (ret)
698 return ret;
699
700 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
701 do {
702 ret = nand_read_data_op(chip, &status, sizeof(status), true);
703 if (ret)
704 break;
705
706 if (status & NAND_STATUS_READY)
707 break;
708
709 /*
710 * Typical lowest execution time for a tR on most NANDs is 10us,
711 * use this as polling delay before doing something smarter (ie.
712 * deriving a delay from the timeout value, timeout_ms/ratio).
713 */
714 udelay(10);
715 } while (time_before(jiffies, timeout_ms));
716
717 /*
718 * We have to exit READ_STATUS mode in order to read real data on the
719 * bus in case the WAITRDY instruction is preceding a DATA_IN
720 * instruction.
721 */
722 nand_exit_status_op(chip);
723
724 if (ret)
725 return ret;
726
727 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
728};
729EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
730
731/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * nand_command - [DEFAULT] Send command to NAND device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200733 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700734 * @command: the command to be sent
735 * @column: the column address for this command, -1 if none
736 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700738 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200739 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200741static void nand_command(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200742 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200744 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200745 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Brian Norris8b6e50c2011-05-25 14:59:01 -0700747 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (command == NAND_CMD_SEQIN) {
749 int readcmd;
750
Joern Engel28318772006-05-22 23:18:05 +0200751 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200753 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 readcmd = NAND_CMD_READOOB;
755 } else if (column < 256) {
756 /* First 256 bytes --> READ0 */
757 readcmd = NAND_CMD_READ0;
758 } else {
759 column -= 256;
760 readcmd = NAND_CMD_READ1;
761 }
Boris Brezillon0f808c12018-09-06 14:05:26 +0200762 chip->cmd_ctrl(chip, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200763 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100765 if (command != NAND_CMD_NONE)
Boris Brezillon0f808c12018-09-06 14:05:26 +0200766 chip->cmd_ctrl(chip, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Brian Norris8b6e50c2011-05-25 14:59:01 -0700768 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200769 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
770 /* Serially input address */
771 if (column != -1) {
772 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800773 if (chip->options & NAND_BUSWIDTH_16 &&
774 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200775 column >>= 1;
Boris Brezillon0f808c12018-09-06 14:05:26 +0200776 chip->cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200777 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200779 if (page_addr != -1) {
Boris Brezillon0f808c12018-09-06 14:05:26 +0200780 chip->cmd_ctrl(chip, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200781 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillon0f808c12018-09-06 14:05:26 +0200782 chip->cmd_ctrl(chip, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900783 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillon0f808c12018-09-06 14:05:26 +0200784 chip->cmd_ctrl(chip, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200785 }
Boris Brezillon0f808c12018-09-06 14:05:26 +0200786 chip->cmd_ctrl(chip, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000787
788 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700789 * Program and erase have their own busy handlers status and sequential
790 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000793
Miquel Raynaldf467892017-11-08 17:00:27 +0100794 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case NAND_CMD_PAGEPROG:
796 case NAND_CMD_ERASE1:
797 case NAND_CMD_ERASE2:
798 case NAND_CMD_SEQIN:
799 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900800 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900801 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return;
803
804 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200805 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200807 udelay(chip->chip_delay);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200808 chip->cmd_ctrl(chip, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200809 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200810 chip->cmd_ctrl(chip,
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200811 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200812 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
813 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815
David Woodhousee0c7d762006-05-13 18:07:53 +0100816 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200817 case NAND_CMD_READ0:
818 /*
819 * READ0 is sometimes used to exit GET STATUS mode. When this
820 * is the case no address cycles are requested, and we can use
821 * this information to detect that we should not wait for the
822 * device to be ready.
823 */
824 if (column == -1 && page_addr == -1)
825 return;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000828 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * If we don't have access to the busy pin, we apply the given
830 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200832 if (!chip->dev_ready) {
833 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700837 /*
838 * Apply this short delay always to ensure that we do wait tWB in
839 * any case on any machine.
840 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100841 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000842
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200843 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200846static void nand_ccs_delay(struct nand_chip *chip)
847{
848 /*
849 * The controller already takes care of waiting for tCCS when the RNDIN
850 * or RNDOUT command is sent, return directly.
851 */
852 if (!(chip->options & NAND_WAIT_TCCS))
853 return;
854
855 /*
856 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
857 * (which should be safe for all NANDs).
858 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100859 if (chip->setup_data_interface)
860 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200861 else
862 ndelay(500);
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/**
866 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200867 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700868 * @command: the command to be sent
869 * @column: the column address for this command, -1 if none
870 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200872 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700873 * devices. We don't have the separate regions as we have in the small page
874 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200876static void nand_command_lp(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200877 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200879 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* Emulate NAND_CMD_READOOB */
882 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200883 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 command = NAND_CMD_READ0;
885 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000886
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200887 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100888 if (command != NAND_CMD_NONE)
Boris Brezillon0f808c12018-09-06 14:05:26 +0200889 chip->cmd_ctrl(chip, command,
Miquel Raynaldf467892017-11-08 17:00:27 +0100890 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200893 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Serially input address */
896 if (column != -1) {
897 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800898 if (chip->options & NAND_BUSWIDTH_16 &&
899 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 column >>= 1;
Boris Brezillon0f808c12018-09-06 14:05:26 +0200901 chip->cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200902 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200903
Brian Norrisf5b88de2016-10-03 09:49:35 -0700904 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200905 if (!nand_opcode_8bits(command))
Boris Brezillon0f808c12018-09-06 14:05:26 +0200906 chip->cmd_ctrl(chip, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (page_addr != -1) {
Boris Brezillon0f808c12018-09-06 14:05:26 +0200909 chip->cmd_ctrl(chip, page_addr, ctrl);
910 chip->cmd_ctrl(chip, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200911 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900912 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillon0f808c12018-09-06 14:05:26 +0200913 chip->cmd_ctrl(chip, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200914 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Boris Brezillon0f808c12018-09-06 14:05:26 +0200917 chip->cmd_ctrl(chip, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000918
919 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100921 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000924
Miquel Raynaldf467892017-11-08 17:00:27 +0100925 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 case NAND_CMD_CACHEDPROG:
927 case NAND_CMD_PAGEPROG:
928 case NAND_CMD_ERASE1:
929 case NAND_CMD_ERASE2:
930 case NAND_CMD_SEQIN:
931 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900932 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900933 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200936 case NAND_CMD_RNDIN:
937 nand_ccs_delay(chip);
938 return;
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200941 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200943 udelay(chip->chip_delay);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200944 chip->cmd_ctrl(chip, NAND_CMD_STATUS,
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200945 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200946 chip->cmd_ctrl(chip, NAND_CMD_NONE,
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200947 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200948 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
949 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return;
951
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200952 case NAND_CMD_RNDOUT:
953 /* No ready / busy check necessary */
Boris Brezillon0f808c12018-09-06 14:05:26 +0200954 chip->cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200955 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200956 chip->cmd_ctrl(chip, NAND_CMD_NONE,
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200957 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200958
959 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200960 return;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200963 /*
964 * READ0 is sometimes used to exit GET STATUS mode. When this
965 * is the case no address cycles are requested, and we can use
966 * this information to detect that READSTART should not be
967 * issued.
968 */
969 if (column == -1 && page_addr == -1)
970 return;
971
Boris Brezillon0f808c12018-09-06 14:05:26 +0200972 chip->cmd_ctrl(chip, NAND_CMD_READSTART,
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200973 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200974 chip->cmd_ctrl(chip, NAND_CMD_NONE,
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200975 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000976
David Woodhousee0c7d762006-05-13 18:07:53 +0100977 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000979 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700981 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100982 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200983 if (!chip->dev_ready) {
984 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000988
Brian Norris8b6e50c2011-05-25 14:59:01 -0700989 /*
990 * Apply this short delay always to ensure that we do wait tWB in
991 * any case on any machine.
992 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100993 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000994
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200995 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200999 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * @chip: the nand chip descriptor
1001 * @mtd: MTD device structure
1002 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001003 *
1004 * Used when in panic, no locks are taken.
1005 */
1006static void panic_nand_get_device(struct nand_chip *chip,
1007 struct mtd_info *mtd, int new_state)
1008{
Brian Norris7854d3f2011-06-23 14:12:08 -07001009 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001010 chip->controller->active = chip;
1011 chip->state = new_state;
1012}
1013
1014/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001016 * @mtd: MTD device structure
1017 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *
1019 * Get the device and lock it for exclusive access
1020 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001021static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001022nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001024 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001025 spinlock_t *lock = &chip->controller->lock;
1026 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001027 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001028retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001029 spin_lock(lock);
1030
vimal singhb8b3ee92009-07-09 20:41:22 +05301031 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001032 if (!chip->controller->active)
1033 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001034
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001035 if (chip->controller->active == chip && chip->state == FL_READY) {
1036 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001037 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001038 return 0;
1039 }
1040 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001041 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1042 chip->state = FL_PM_SUSPENDED;
1043 spin_unlock(lock);
1044 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001045 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001046 }
1047 set_current_state(TASK_UNINTERRUPTIBLE);
1048 add_wait_queue(wq, &wait);
1049 spin_unlock(lock);
1050 schedule();
1051 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto retry;
1053}
1054
1055/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001056 * panic_nand_wait - [GENERIC] wait until the command is done
1057 * @mtd: MTD device structure
1058 * @chip: NAND chip structure
1059 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001060 *
1061 * Wait for command done. This is a helper function for nand_wait used when
1062 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001063 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001064 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001065static void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001066{
1067 int i;
1068 for (i = 0; i < timeo; i++) {
1069 if (chip->dev_ready) {
Boris Brezillon50a487e2018-09-06 14:05:27 +02001070 if (chip->dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001071 break;
1072 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001073 int ret;
1074 u8 status;
1075
1076 ret = nand_read_data_op(chip, &status, sizeof(status),
1077 true);
1078 if (ret)
1079 return;
1080
1081 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001082 break;
1083 }
1084 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001085 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001086}
1087
1088/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001089 * nand_wait - [DEFAULT] wait until the command is done
1090 * @mtd: MTD device structure
1091 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001093 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001094 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001095static int nand_wait(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097
Alex Smithb70af9b2015-10-06 14:52:07 +01001098 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001099 u8 status;
1100 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Brian Norris8b6e50c2011-05-25 14:59:01 -07001102 /*
1103 * Apply this short delay always to ensure that we do wait tWB in any
1104 * case on any machine.
1105 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001106 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Boris Brezillon97d90da2017-11-30 18:01:29 +01001108 ret = nand_status_op(chip, NULL);
1109 if (ret)
1110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001112 if (in_interrupt() || oops_in_progress)
Boris Brezillonf1d46942018-09-06 14:05:29 +02001113 panic_nand_wait(chip, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001114 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001115 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001116 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001117 if (chip->dev_ready) {
Boris Brezillon50a487e2018-09-06 14:05:27 +02001118 if (chip->dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001119 break;
1120 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001121 ret = nand_read_data_op(chip, &status,
1122 sizeof(status), true);
1123 if (ret)
1124 return ret;
1125
1126 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001127 break;
1128 }
1129 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001130 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001132
Boris Brezillon97d90da2017-11-30 18:01:29 +01001133 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1134 if (ret)
1135 return ret;
1136
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001137 /* This can happen if in case of timeout or buggy dev_ready */
1138 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return status;
1140}
1141
Miquel Raynal789157e2018-03-19 14:47:28 +01001142static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001143{
Miquel Raynal789157e2018-03-19 14:47:28 +01001144 return (chip->parameters.supports_set_get_features &&
1145 test_bit(addr, chip->parameters.get_feature_list));
1146}
1147
1148static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1149{
1150 return (chip->parameters.supports_set_get_features &&
1151 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001152}
1153
1154/**
1155 * nand_get_features - wrapper to perform a GET_FEATURE
1156 * @chip: NAND chip info structure
1157 * @addr: feature address
1158 * @subfeature_param: the subfeature parameters, a four bytes array
1159 *
1160 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1161 * operation cannot be handled.
1162 */
1163int nand_get_features(struct nand_chip *chip, int addr,
1164 u8 *subfeature_param)
1165{
Miquel Raynal789157e2018-03-19 14:47:28 +01001166 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001167 return -ENOTSUPP;
1168
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001169 return chip->get_features(chip, addr, subfeature_param);
Miquel Raynal97baea12018-03-19 14:47:20 +01001170}
1171EXPORT_SYMBOL_GPL(nand_get_features);
1172
1173/**
1174 * nand_set_features - wrapper to perform a SET_FEATURE
1175 * @chip: NAND chip info structure
1176 * @addr: feature address
1177 * @subfeature_param: the subfeature parameters, a four bytes array
1178 *
1179 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1180 * operation cannot be handled.
1181 */
1182int nand_set_features(struct nand_chip *chip, int addr,
1183 u8 *subfeature_param)
1184{
Miquel Raynal789157e2018-03-19 14:47:28 +01001185 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001186 return -ENOTSUPP;
1187
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001188 return chip->set_features(chip, addr, subfeature_param);
Miquel Raynal97baea12018-03-19 14:47:20 +01001189}
1190EXPORT_SYMBOL_GPL(nand_set_features);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001193 * nand_reset_data_interface - Reset data interface and timings
1194 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001195 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001196 *
1197 * Reset the Data interface and timings to ONFI mode 0.
1198 *
1199 * Returns 0 for success or negative error code otherwise.
1200 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001201static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001202{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001203 int ret;
1204
1205 if (!chip->setup_data_interface)
1206 return 0;
1207
1208 /*
1209 * The ONFI specification says:
1210 * "
1211 * To transition from NV-DDR or NV-DDR2 to the SDR data
1212 * interface, the host shall use the Reset (FFh) command
1213 * using SDR timing mode 0. A device in any timing mode is
1214 * required to recognize Reset (FFh) command issued in SDR
1215 * timing mode 0.
1216 * "
1217 *
1218 * Configure the data interface in SDR mode and set the
1219 * timings to timing mode 0.
1220 */
1221
Miquel Raynal17fa8042017-11-30 18:01:31 +01001222 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +02001223 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001224 if (ret)
1225 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1226
1227 return ret;
1228}
1229
1230/**
1231 * nand_setup_data_interface - Setup the best data interface and timings
1232 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001233 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001234 *
1235 * Find and configure the best data interface and NAND timings supported by
1236 * the chip and the driver.
1237 * First tries to retrieve supported timing modes from ONFI information,
1238 * and if the NAND chip does not support ONFI, relies on the
1239 * ->onfi_timing_mode_default specified in the nand_ids table.
1240 *
1241 * Returns 0 for success or negative error code otherwise.
1242 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001243static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001244{
Miquel Raynal97baea12018-03-19 14:47:20 +01001245 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1246 chip->onfi_timing_mode_default,
1247 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001248 int ret;
1249
Miquel Raynal17fa8042017-11-30 18:01:31 +01001250 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001251 return 0;
1252
Miquel Raynal993447b2018-03-19 14:47:21 +01001253 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001254 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001255 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001256 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1257 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001258 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001259 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001260 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001261 }
1262
Miquel Raynal97baea12018-03-19 14:47:20 +01001263 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +02001264 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001265 if (ret)
1266 return ret;
1267
1268 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001269 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001270 return 0;
1271
1272 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001273 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001274 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1275 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001276 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001277 if (ret)
1278 goto err_reset_chip;
1279
1280 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1281 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1282 chip->onfi_timing_mode_default);
1283 goto err_reset_chip;
1284 }
1285
1286 return 0;
1287
1288err_reset_chip:
1289 /*
1290 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1291 * timing mode.
1292 */
1293 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001294 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001295 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001296 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001297
Boris Brezillond8e725d2016-09-15 10:32:50 +02001298 return ret;
1299}
1300
1301/**
1302 * nand_init_data_interface - find the best data interface and timings
1303 * @chip: The NAND chip
1304 *
1305 * Find the best data interface and NAND timings supported by the chip
1306 * and the driver.
1307 * First tries to retrieve supported timing modes from ONFI information,
1308 * and if the NAND chip does not support ONFI, relies on the
1309 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1310 * function nand_chip->data_interface is initialized with the best timing mode
1311 * available.
1312 *
1313 * Returns 0 for success or negative error code otherwise.
1314 */
1315static int nand_init_data_interface(struct nand_chip *chip)
1316{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001317 int modes, mode, ret;
1318
1319 if (!chip->setup_data_interface)
1320 return 0;
1321
1322 /*
1323 * First try to identify the best timings from ONFI parameters and
1324 * if the NAND does not support ONFI, fallback to the default ONFI
1325 * timing mode.
1326 */
1327 modes = onfi_get_async_timing_mode(chip);
1328 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1329 if (!chip->onfi_timing_mode_default)
1330 return 0;
1331
1332 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1333 }
1334
Boris Brezillond8e725d2016-09-15 10:32:50 +02001335
1336 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001337 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001338 if (ret)
1339 continue;
1340
Miquel Raynald787b8b2017-12-22 18:12:41 +01001341 /*
1342 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1343 * controller supports the requested timings.
1344 */
Boris Brezillon858838b2018-09-06 14:05:33 +02001345 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +01001346 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001347 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001348 if (!ret) {
1349 chip->onfi_timing_mode_default = mode;
1350 break;
1351 }
1352 }
1353
1354 return 0;
1355}
1356
Boris Brezillond8e725d2016-09-15 10:32:50 +02001357/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001358 * nand_fill_column_cycles - fill the column cycles of an address
1359 * @chip: The NAND chip
1360 * @addrs: Array of address cycles to fill
1361 * @offset_in_page: The offset in the page
1362 *
1363 * Fills the first or the first two bytes of the @addrs field depending
1364 * on the NAND bus width and the page size.
1365 *
1366 * Returns the number of cycles needed to encode the column, or a negative
1367 * error code in case one of the arguments is invalid.
1368 */
1369static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1370 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Miquel Raynal8878b122017-11-09 14:16:45 +01001372 struct mtd_info *mtd = nand_to_mtd(chip);
1373
1374 /* Make sure the offset is less than the actual page size. */
1375 if (offset_in_page > mtd->writesize + mtd->oobsize)
1376 return -EINVAL;
1377
1378 /*
1379 * On small page NANDs, there's a dedicated command to access the OOB
1380 * area, and the column address is relative to the start of the OOB
1381 * area, not the start of the page. Asjust the address accordingly.
1382 */
1383 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1384 offset_in_page -= mtd->writesize;
1385
1386 /*
1387 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1388 * wide, then it must be divided by 2.
1389 */
1390 if (chip->options & NAND_BUSWIDTH_16) {
1391 if (WARN_ON(offset_in_page % 2))
1392 return -EINVAL;
1393
1394 offset_in_page /= 2;
1395 }
1396
1397 addrs[0] = offset_in_page;
1398
1399 /*
1400 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1401 * need 2
1402 */
1403 if (mtd->writesize <= 512)
1404 return 1;
1405
1406 addrs[1] = offset_in_page >> 8;
1407
1408 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Miquel Raynal8878b122017-11-09 14:16:45 +01001411static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1412 unsigned int offset_in_page, void *buf,
1413 unsigned int len)
1414{
1415 struct mtd_info *mtd = nand_to_mtd(chip);
1416 const struct nand_sdr_timings *sdr =
1417 nand_get_sdr_timings(&chip->data_interface);
1418 u8 addrs[4];
1419 struct nand_op_instr instrs[] = {
1420 NAND_OP_CMD(NAND_CMD_READ0, 0),
1421 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1422 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1423 PSEC_TO_NSEC(sdr->tRR_min)),
1424 NAND_OP_DATA_IN(len, buf, 0),
1425 };
1426 struct nand_operation op = NAND_OPERATION(instrs);
1427 int ret;
1428
1429 /* Drop the DATA_IN instruction if len is set to 0. */
1430 if (!len)
1431 op.ninstrs--;
1432
1433 if (offset_in_page >= mtd->writesize)
1434 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1435 else if (offset_in_page >= 256 &&
1436 !(chip->options & NAND_BUSWIDTH_16))
1437 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1438
1439 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1440 if (ret < 0)
1441 return ret;
1442
1443 addrs[1] = page;
1444 addrs[2] = page >> 8;
1445
1446 if (chip->options & NAND_ROW_ADDR_3) {
1447 addrs[3] = page >> 16;
1448 instrs[1].ctx.addr.naddrs++;
1449 }
1450
1451 return nand_exec_op(chip, &op);
1452}
1453
1454static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1455 unsigned int offset_in_page, void *buf,
1456 unsigned int len)
1457{
1458 const struct nand_sdr_timings *sdr =
1459 nand_get_sdr_timings(&chip->data_interface);
1460 u8 addrs[5];
1461 struct nand_op_instr instrs[] = {
1462 NAND_OP_CMD(NAND_CMD_READ0, 0),
1463 NAND_OP_ADDR(4, addrs, 0),
1464 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1465 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1466 PSEC_TO_NSEC(sdr->tRR_min)),
1467 NAND_OP_DATA_IN(len, buf, 0),
1468 };
1469 struct nand_operation op = NAND_OPERATION(instrs);
1470 int ret;
1471
1472 /* Drop the DATA_IN instruction if len is set to 0. */
1473 if (!len)
1474 op.ninstrs--;
1475
1476 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1477 if (ret < 0)
1478 return ret;
1479
1480 addrs[2] = page;
1481 addrs[3] = page >> 8;
1482
1483 if (chip->options & NAND_ROW_ADDR_3) {
1484 addrs[4] = page >> 16;
1485 instrs[1].ctx.addr.naddrs++;
1486 }
1487
1488 return nand_exec_op(chip, &op);
1489}
1490
1491/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001492 * nand_read_page_op - Do a READ PAGE operation
1493 * @chip: The NAND chip
1494 * @page: page to read
1495 * @offset_in_page: offset within the page
1496 * @buf: buffer used to store the data
1497 * @len: length of the buffer
1498 *
1499 * This function issues a READ PAGE operation.
1500 * This function does not select/unselect the CS line.
1501 *
1502 * Returns 0 on success, a negative error code otherwise.
1503 */
1504int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1505 unsigned int offset_in_page, void *buf, unsigned int len)
1506{
1507 struct mtd_info *mtd = nand_to_mtd(chip);
1508
1509 if (len && !buf)
1510 return -EINVAL;
1511
1512 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1513 return -EINVAL;
1514
Miquel Raynal8878b122017-11-09 14:16:45 +01001515 if (chip->exec_op) {
1516 if (mtd->writesize > 512)
1517 return nand_lp_exec_read_page_op(chip, page,
1518 offset_in_page, buf,
1519 len);
1520
1521 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1522 buf, len);
1523 }
1524
Boris Brezillon5295cf22018-09-06 14:05:28 +02001525 chip->cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001526 if (len)
Boris Brezillon7e534322018-09-06 14:05:22 +02001527 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001528
1529 return 0;
1530}
1531EXPORT_SYMBOL_GPL(nand_read_page_op);
1532
1533/**
1534 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1535 * @chip: The NAND chip
1536 * @page: parameter page to read
1537 * @buf: buffer used to store the data
1538 * @len: length of the buffer
1539 *
1540 * This function issues a READ PARAMETER PAGE operation.
1541 * This function does not select/unselect the CS line.
1542 *
1543 * Returns 0 on success, a negative error code otherwise.
1544 */
1545static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1546 unsigned int len)
1547{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001548 unsigned int i;
1549 u8 *p = buf;
1550
1551 if (len && !buf)
1552 return -EINVAL;
1553
Miquel Raynal8878b122017-11-09 14:16:45 +01001554 if (chip->exec_op) {
1555 const struct nand_sdr_timings *sdr =
1556 nand_get_sdr_timings(&chip->data_interface);
1557 struct nand_op_instr instrs[] = {
1558 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1559 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1560 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1561 PSEC_TO_NSEC(sdr->tRR_min)),
1562 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1563 };
1564 struct nand_operation op = NAND_OPERATION(instrs);
1565
1566 /* Drop the DATA_IN instruction if len is set to 0. */
1567 if (!len)
1568 op.ninstrs--;
1569
1570 return nand_exec_op(chip, &op);
1571 }
1572
Boris Brezillon5295cf22018-09-06 14:05:28 +02001573 chip->cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001574 for (i = 0; i < len; i++)
Boris Brezillon7e534322018-09-06 14:05:22 +02001575 p[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001576
1577 return 0;
1578}
1579
1580/**
1581 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1582 * @chip: The NAND chip
1583 * @offset_in_page: offset within the page
1584 * @buf: buffer used to store the data
1585 * @len: length of the buffer
1586 * @force_8bit: force 8-bit bus access
1587 *
1588 * This function issues a CHANGE READ COLUMN operation.
1589 * This function does not select/unselect the CS line.
1590 *
1591 * Returns 0 on success, a negative error code otherwise.
1592 */
1593int nand_change_read_column_op(struct nand_chip *chip,
1594 unsigned int offset_in_page, void *buf,
1595 unsigned int len, bool force_8bit)
1596{
1597 struct mtd_info *mtd = nand_to_mtd(chip);
1598
1599 if (len && !buf)
1600 return -EINVAL;
1601
1602 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1603 return -EINVAL;
1604
Miquel Raynal8878b122017-11-09 14:16:45 +01001605 /* Small page NANDs do not support column change. */
1606 if (mtd->writesize <= 512)
1607 return -ENOTSUPP;
1608
1609 if (chip->exec_op) {
1610 const struct nand_sdr_timings *sdr =
1611 nand_get_sdr_timings(&chip->data_interface);
1612 u8 addrs[2] = {};
1613 struct nand_op_instr instrs[] = {
1614 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1615 NAND_OP_ADDR(2, addrs, 0),
1616 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1617 PSEC_TO_NSEC(sdr->tCCS_min)),
1618 NAND_OP_DATA_IN(len, buf, 0),
1619 };
1620 struct nand_operation op = NAND_OPERATION(instrs);
1621 int ret;
1622
1623 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1624 if (ret < 0)
1625 return ret;
1626
1627 /* Drop the DATA_IN instruction if len is set to 0. */
1628 if (!len)
1629 op.ninstrs--;
1630
1631 instrs[3].ctx.data.force_8bit = force_8bit;
1632
1633 return nand_exec_op(chip, &op);
1634 }
1635
Boris Brezillon5295cf22018-09-06 14:05:28 +02001636 chip->cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001637 if (len)
Boris Brezillon7e534322018-09-06 14:05:22 +02001638 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001639
1640 return 0;
1641}
1642EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1643
1644/**
1645 * nand_read_oob_op - Do a READ OOB operation
1646 * @chip: The NAND chip
1647 * @page: page to read
1648 * @offset_in_oob: offset within the OOB area
1649 * @buf: buffer used to store the data
1650 * @len: length of the buffer
1651 *
1652 * This function issues a READ OOB operation.
1653 * This function does not select/unselect the CS line.
1654 *
1655 * Returns 0 on success, a negative error code otherwise.
1656 */
1657int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1658 unsigned int offset_in_oob, void *buf, unsigned int len)
1659{
1660 struct mtd_info *mtd = nand_to_mtd(chip);
1661
1662 if (len && !buf)
1663 return -EINVAL;
1664
1665 if (offset_in_oob + len > mtd->oobsize)
1666 return -EINVAL;
1667
Miquel Raynal8878b122017-11-09 14:16:45 +01001668 if (chip->exec_op)
1669 return nand_read_page_op(chip, page,
1670 mtd->writesize + offset_in_oob,
1671 buf, len);
1672
Boris Brezillon5295cf22018-09-06 14:05:28 +02001673 chip->cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001674 if (len)
Boris Brezillon7e534322018-09-06 14:05:22 +02001675 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001676
1677 return 0;
1678}
1679EXPORT_SYMBOL_GPL(nand_read_oob_op);
1680
Miquel Raynal8878b122017-11-09 14:16:45 +01001681static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1682 unsigned int offset_in_page, const void *buf,
1683 unsigned int len, bool prog)
1684{
1685 struct mtd_info *mtd = nand_to_mtd(chip);
1686 const struct nand_sdr_timings *sdr =
1687 nand_get_sdr_timings(&chip->data_interface);
1688 u8 addrs[5] = {};
1689 struct nand_op_instr instrs[] = {
1690 /*
1691 * The first instruction will be dropped if we're dealing
1692 * with a large page NAND and adjusted if we're dealing
1693 * with a small page NAND and the page offset is > 255.
1694 */
1695 NAND_OP_CMD(NAND_CMD_READ0, 0),
1696 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1697 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1698 NAND_OP_DATA_OUT(len, buf, 0),
1699 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1700 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1701 };
1702 struct nand_operation op = NAND_OPERATION(instrs);
1703 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1704 int ret;
1705 u8 status;
1706
1707 if (naddrs < 0)
1708 return naddrs;
1709
1710 addrs[naddrs++] = page;
1711 addrs[naddrs++] = page >> 8;
1712 if (chip->options & NAND_ROW_ADDR_3)
1713 addrs[naddrs++] = page >> 16;
1714
1715 instrs[2].ctx.addr.naddrs = naddrs;
1716
1717 /* Drop the last two instructions if we're not programming the page. */
1718 if (!prog) {
1719 op.ninstrs -= 2;
1720 /* Also drop the DATA_OUT instruction if empty. */
1721 if (!len)
1722 op.ninstrs--;
1723 }
1724
1725 if (mtd->writesize <= 512) {
1726 /*
1727 * Small pages need some more tweaking: we have to adjust the
1728 * first instruction depending on the page offset we're trying
1729 * to access.
1730 */
1731 if (offset_in_page >= mtd->writesize)
1732 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1733 else if (offset_in_page >= 256 &&
1734 !(chip->options & NAND_BUSWIDTH_16))
1735 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1736 } else {
1737 /*
1738 * Drop the first command if we're dealing with a large page
1739 * NAND.
1740 */
1741 op.instrs++;
1742 op.ninstrs--;
1743 }
1744
1745 ret = nand_exec_op(chip, &op);
1746 if (!prog || ret)
1747 return ret;
1748
1749 ret = nand_status_op(chip, &status);
1750 if (ret)
1751 return ret;
1752
1753 return status;
1754}
1755
Boris Brezillon97d90da2017-11-30 18:01:29 +01001756/**
1757 * nand_prog_page_begin_op - starts a PROG PAGE operation
1758 * @chip: The NAND chip
1759 * @page: page to write
1760 * @offset_in_page: offset within the page
1761 * @buf: buffer containing the data to write to the page
1762 * @len: length of the buffer
1763 *
1764 * This function issues the first half of a PROG PAGE operation.
1765 * This function does not select/unselect the CS line.
1766 *
1767 * Returns 0 on success, a negative error code otherwise.
1768 */
1769int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1770 unsigned int offset_in_page, const void *buf,
1771 unsigned int len)
1772{
1773 struct mtd_info *mtd = nand_to_mtd(chip);
1774
1775 if (len && !buf)
1776 return -EINVAL;
1777
1778 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1779 return -EINVAL;
1780
Miquel Raynal8878b122017-11-09 14:16:45 +01001781 if (chip->exec_op)
1782 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1783 len, false);
1784
Boris Brezillon5295cf22018-09-06 14:05:28 +02001785 chip->cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001786
1787 if (buf)
Boris Brezillonc0739d82018-09-06 14:05:23 +02001788 chip->write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001789
1790 return 0;
1791}
1792EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1793
1794/**
1795 * nand_prog_page_end_op - ends a PROG PAGE operation
1796 * @chip: The NAND chip
1797 *
1798 * This function issues the second half of a PROG PAGE operation.
1799 * This function does not select/unselect the CS line.
1800 *
1801 * Returns 0 on success, a negative error code otherwise.
1802 */
1803int nand_prog_page_end_op(struct nand_chip *chip)
1804{
Miquel Raynal8878b122017-11-09 14:16:45 +01001805 int ret;
1806 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001807
Miquel Raynal8878b122017-11-09 14:16:45 +01001808 if (chip->exec_op) {
1809 const struct nand_sdr_timings *sdr =
1810 nand_get_sdr_timings(&chip->data_interface);
1811 struct nand_op_instr instrs[] = {
1812 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1813 PSEC_TO_NSEC(sdr->tWB_max)),
1814 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1815 };
1816 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001817
Miquel Raynal8878b122017-11-09 14:16:45 +01001818 ret = nand_exec_op(chip, &op);
1819 if (ret)
1820 return ret;
1821
1822 ret = nand_status_op(chip, &status);
1823 if (ret)
1824 return ret;
1825 } else {
Boris Brezillon5295cf22018-09-06 14:05:28 +02001826 chip->cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillonf1d46942018-09-06 14:05:29 +02001827 ret = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001828 if (ret < 0)
1829 return ret;
1830
1831 status = ret;
1832 }
1833
Boris Brezillon97d90da2017-11-30 18:01:29 +01001834 if (status & NAND_STATUS_FAIL)
1835 return -EIO;
1836
1837 return 0;
1838}
1839EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1840
1841/**
1842 * nand_prog_page_op - Do a full PROG PAGE operation
1843 * @chip: The NAND chip
1844 * @page: page to write
1845 * @offset_in_page: offset within the page
1846 * @buf: buffer containing the data to write to the page
1847 * @len: length of the buffer
1848 *
1849 * This function issues a full PROG PAGE operation.
1850 * This function does not select/unselect the CS line.
1851 *
1852 * Returns 0 on success, a negative error code otherwise.
1853 */
1854int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1855 unsigned int offset_in_page, const void *buf,
1856 unsigned int len)
1857{
1858 struct mtd_info *mtd = nand_to_mtd(chip);
1859 int status;
1860
1861 if (!len || !buf)
1862 return -EINVAL;
1863
1864 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1865 return -EINVAL;
1866
Miquel Raynal8878b122017-11-09 14:16:45 +01001867 if (chip->exec_op) {
1868 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1869 len, true);
1870 } else {
Boris Brezillon5295cf22018-09-06 14:05:28 +02001871 chip->cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillonc0739d82018-09-06 14:05:23 +02001872 chip->write_buf(chip, buf, len);
Boris Brezillon5295cf22018-09-06 14:05:28 +02001873 chip->cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillonf1d46942018-09-06 14:05:29 +02001874 status = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001875 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001876
Boris Brezillon97d90da2017-11-30 18:01:29 +01001877 if (status & NAND_STATUS_FAIL)
1878 return -EIO;
1879
1880 return 0;
1881}
1882EXPORT_SYMBOL_GPL(nand_prog_page_op);
1883
1884/**
1885 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1886 * @chip: The NAND chip
1887 * @offset_in_page: offset within the page
1888 * @buf: buffer containing the data to send to the NAND
1889 * @len: length of the buffer
1890 * @force_8bit: force 8-bit bus access
1891 *
1892 * This function issues a CHANGE WRITE COLUMN operation.
1893 * This function does not select/unselect the CS line.
1894 *
1895 * Returns 0 on success, a negative error code otherwise.
1896 */
1897int nand_change_write_column_op(struct nand_chip *chip,
1898 unsigned int offset_in_page,
1899 const void *buf, unsigned int len,
1900 bool force_8bit)
1901{
1902 struct mtd_info *mtd = nand_to_mtd(chip);
1903
1904 if (len && !buf)
1905 return -EINVAL;
1906
1907 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1908 return -EINVAL;
1909
Miquel Raynal8878b122017-11-09 14:16:45 +01001910 /* Small page NANDs do not support column change. */
1911 if (mtd->writesize <= 512)
1912 return -ENOTSUPP;
1913
1914 if (chip->exec_op) {
1915 const struct nand_sdr_timings *sdr =
1916 nand_get_sdr_timings(&chip->data_interface);
1917 u8 addrs[2];
1918 struct nand_op_instr instrs[] = {
1919 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1920 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1921 NAND_OP_DATA_OUT(len, buf, 0),
1922 };
1923 struct nand_operation op = NAND_OPERATION(instrs);
1924 int ret;
1925
1926 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1927 if (ret < 0)
1928 return ret;
1929
1930 instrs[2].ctx.data.force_8bit = force_8bit;
1931
1932 /* Drop the DATA_OUT instruction if len is set to 0. */
1933 if (!len)
1934 op.ninstrs--;
1935
1936 return nand_exec_op(chip, &op);
1937 }
1938
Boris Brezillon5295cf22018-09-06 14:05:28 +02001939 chip->cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001940 if (len)
Boris Brezillonc0739d82018-09-06 14:05:23 +02001941 chip->write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001942
1943 return 0;
1944}
1945EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1946
1947/**
1948 * nand_readid_op - Do a READID operation
1949 * @chip: The NAND chip
1950 * @addr: address cycle to pass after the READID command
1951 * @buf: buffer used to store the ID
1952 * @len: length of the buffer
1953 *
1954 * This function sends a READID command and reads back the ID returned by the
1955 * NAND.
1956 * This function does not select/unselect the CS line.
1957 *
1958 * Returns 0 on success, a negative error code otherwise.
1959 */
1960int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1961 unsigned int len)
1962{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001963 unsigned int i;
1964 u8 *id = buf;
1965
1966 if (len && !buf)
1967 return -EINVAL;
1968
Miquel Raynal8878b122017-11-09 14:16:45 +01001969 if (chip->exec_op) {
1970 const struct nand_sdr_timings *sdr =
1971 nand_get_sdr_timings(&chip->data_interface);
1972 struct nand_op_instr instrs[] = {
1973 NAND_OP_CMD(NAND_CMD_READID, 0),
1974 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1975 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1976 };
1977 struct nand_operation op = NAND_OPERATION(instrs);
1978
1979 /* Drop the DATA_IN instruction if len is set to 0. */
1980 if (!len)
1981 op.ninstrs--;
1982
1983 return nand_exec_op(chip, &op);
1984 }
1985
Boris Brezillon5295cf22018-09-06 14:05:28 +02001986 chip->cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001987
1988 for (i = 0; i < len; i++)
Boris Brezillon7e534322018-09-06 14:05:22 +02001989 id[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001990
1991 return 0;
1992}
1993EXPORT_SYMBOL_GPL(nand_readid_op);
1994
1995/**
1996 * nand_status_op - Do a STATUS operation
1997 * @chip: The NAND chip
1998 * @status: out variable to store the NAND status
1999 *
2000 * This function sends a STATUS command and reads back the status returned by
2001 * the NAND.
2002 * This function does not select/unselect the CS line.
2003 *
2004 * Returns 0 on success, a negative error code otherwise.
2005 */
2006int nand_status_op(struct nand_chip *chip, u8 *status)
2007{
Miquel Raynal8878b122017-11-09 14:16:45 +01002008 if (chip->exec_op) {
2009 const struct nand_sdr_timings *sdr =
2010 nand_get_sdr_timings(&chip->data_interface);
2011 struct nand_op_instr instrs[] = {
2012 NAND_OP_CMD(NAND_CMD_STATUS,
2013 PSEC_TO_NSEC(sdr->tADL_min)),
2014 NAND_OP_8BIT_DATA_IN(1, status, 0),
2015 };
2016 struct nand_operation op = NAND_OPERATION(instrs);
2017
2018 if (!status)
2019 op.ninstrs--;
2020
2021 return nand_exec_op(chip, &op);
2022 }
2023
Boris Brezillon5295cf22018-09-06 14:05:28 +02002024 chip->cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002025 if (status)
Boris Brezillon7e534322018-09-06 14:05:22 +02002026 *status = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002027
2028 return 0;
2029}
2030EXPORT_SYMBOL_GPL(nand_status_op);
2031
2032/**
2033 * nand_exit_status_op - Exit a STATUS operation
2034 * @chip: The NAND chip
2035 *
2036 * This function sends a READ0 command to cancel the effect of the STATUS
2037 * command to avoid reading only the status until a new read command is sent.
2038 *
2039 * This function does not select/unselect the CS line.
2040 *
2041 * Returns 0 on success, a negative error code otherwise.
2042 */
2043int nand_exit_status_op(struct nand_chip *chip)
2044{
Miquel Raynal8878b122017-11-09 14:16:45 +01002045 if (chip->exec_op) {
2046 struct nand_op_instr instrs[] = {
2047 NAND_OP_CMD(NAND_CMD_READ0, 0),
2048 };
2049 struct nand_operation op = NAND_OPERATION(instrs);
2050
2051 return nand_exec_op(chip, &op);
2052 }
2053
Boris Brezillon5295cf22018-09-06 14:05:28 +02002054 chip->cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002055
2056 return 0;
2057}
2058EXPORT_SYMBOL_GPL(nand_exit_status_op);
2059
2060/**
2061 * nand_erase_op - Do an erase operation
2062 * @chip: The NAND chip
2063 * @eraseblock: block to erase
2064 *
2065 * This function sends an ERASE command and waits for the NAND to be ready
2066 * before returning.
2067 * This function does not select/unselect the CS line.
2068 *
2069 * Returns 0 on success, a negative error code otherwise.
2070 */
2071int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2072{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002073 unsigned int page = eraseblock <<
2074 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002075 int ret;
2076 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002077
Miquel Raynal8878b122017-11-09 14:16:45 +01002078 if (chip->exec_op) {
2079 const struct nand_sdr_timings *sdr =
2080 nand_get_sdr_timings(&chip->data_interface);
2081 u8 addrs[3] = { page, page >> 8, page >> 16 };
2082 struct nand_op_instr instrs[] = {
2083 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2084 NAND_OP_ADDR(2, addrs, 0),
2085 NAND_OP_CMD(NAND_CMD_ERASE2,
2086 PSEC_TO_MSEC(sdr->tWB_max)),
2087 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2088 };
2089 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002090
Miquel Raynal8878b122017-11-09 14:16:45 +01002091 if (chip->options & NAND_ROW_ADDR_3)
2092 instrs[1].ctx.addr.naddrs++;
2093
2094 ret = nand_exec_op(chip, &op);
2095 if (ret)
2096 return ret;
2097
2098 ret = nand_status_op(chip, &status);
2099 if (ret)
2100 return ret;
2101 } else {
Boris Brezillon5295cf22018-09-06 14:05:28 +02002102 chip->cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2103 chip->cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002104
Boris Brezillonf1d46942018-09-06 14:05:29 +02002105 ret = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002106 if (ret < 0)
2107 return ret;
2108
2109 status = ret;
2110 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002111
2112 if (status & NAND_STATUS_FAIL)
2113 return -EIO;
2114
2115 return 0;
2116}
2117EXPORT_SYMBOL_GPL(nand_erase_op);
2118
2119/**
2120 * nand_set_features_op - Do a SET FEATURES operation
2121 * @chip: The NAND chip
2122 * @feature: feature id
2123 * @data: 4 bytes of data
2124 *
2125 * This function sends a SET FEATURES command and waits for the NAND to be
2126 * ready before returning.
2127 * This function does not select/unselect the CS line.
2128 *
2129 * Returns 0 on success, a negative error code otherwise.
2130 */
2131static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2132 const void *data)
2133{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002134 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002135 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002136
Miquel Raynal8878b122017-11-09 14:16:45 +01002137 if (chip->exec_op) {
2138 const struct nand_sdr_timings *sdr =
2139 nand_get_sdr_timings(&chip->data_interface);
2140 struct nand_op_instr instrs[] = {
2141 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2142 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2143 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2144 PSEC_TO_NSEC(sdr->tWB_max)),
2145 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2146 };
2147 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002148
Boris Brezillon782d1962018-05-11 14:44:07 +02002149 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002150 }
2151
Boris Brezillon5295cf22018-09-06 14:05:28 +02002152 chip->cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002153 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillonc0739d82018-09-06 14:05:23 +02002154 chip->write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002155
Boris Brezillonf1d46942018-09-06 14:05:29 +02002156 ret = chip->waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002157 if (ret < 0)
2158 return ret;
2159
2160 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002161 return -EIO;
2162
2163 return 0;
2164}
2165
2166/**
2167 * nand_get_features_op - Do a GET FEATURES operation
2168 * @chip: The NAND chip
2169 * @feature: feature id
2170 * @data: 4 bytes of data
2171 *
2172 * This function sends a GET FEATURES command and waits for the NAND to be
2173 * ready before returning.
2174 * This function does not select/unselect the CS line.
2175 *
2176 * Returns 0 on success, a negative error code otherwise.
2177 */
2178static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2179 void *data)
2180{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002181 u8 *params = data;
2182 int i;
2183
Miquel Raynal8878b122017-11-09 14:16:45 +01002184 if (chip->exec_op) {
2185 const struct nand_sdr_timings *sdr =
2186 nand_get_sdr_timings(&chip->data_interface);
2187 struct nand_op_instr instrs[] = {
2188 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2189 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2190 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2191 PSEC_TO_NSEC(sdr->tRR_min)),
2192 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2193 data, 0),
2194 };
2195 struct nand_operation op = NAND_OPERATION(instrs);
2196
2197 return nand_exec_op(chip, &op);
2198 }
2199
Boris Brezillon5295cf22018-09-06 14:05:28 +02002200 chip->cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002201 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon7e534322018-09-06 14:05:22 +02002202 params[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002203
2204 return 0;
2205}
2206
Boris Brezillon52f05b62018-07-27 09:44:18 +02002207static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2208 unsigned int delay_ns)
2209{
2210 if (chip->exec_op) {
2211 struct nand_op_instr instrs[] = {
2212 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2213 PSEC_TO_NSEC(delay_ns)),
2214 };
2215 struct nand_operation op = NAND_OPERATION(instrs);
2216
2217 return nand_exec_op(chip, &op);
2218 }
2219
2220 /* Apply delay or wait for ready/busy pin */
2221 if (!chip->dev_ready)
2222 udelay(chip->chip_delay);
2223 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002224 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002225
2226 return 0;
2227}
2228
Boris Brezillon97d90da2017-11-30 18:01:29 +01002229/**
2230 * nand_reset_op - Do a reset operation
2231 * @chip: The NAND chip
2232 *
2233 * This function sends a RESET command and waits for the NAND to be ready
2234 * before returning.
2235 * This function does not select/unselect the CS line.
2236 *
2237 * Returns 0 on success, a negative error code otherwise.
2238 */
2239int nand_reset_op(struct nand_chip *chip)
2240{
Miquel Raynal8878b122017-11-09 14:16:45 +01002241 if (chip->exec_op) {
2242 const struct nand_sdr_timings *sdr =
2243 nand_get_sdr_timings(&chip->data_interface);
2244 struct nand_op_instr instrs[] = {
2245 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2246 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2247 };
2248 struct nand_operation op = NAND_OPERATION(instrs);
2249
2250 return nand_exec_op(chip, &op);
2251 }
2252
Boris Brezillon5295cf22018-09-06 14:05:28 +02002253 chip->cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002254
2255 return 0;
2256}
2257EXPORT_SYMBOL_GPL(nand_reset_op);
2258
2259/**
2260 * nand_read_data_op - Read data from the NAND
2261 * @chip: The NAND chip
2262 * @buf: buffer used to store the data
2263 * @len: length of the buffer
2264 * @force_8bit: force 8-bit bus access
2265 *
2266 * This function does a raw data read on the bus. Usually used after launching
2267 * another NAND operation like nand_read_page_op().
2268 * This function does not select/unselect the CS line.
2269 *
2270 * Returns 0 on success, a negative error code otherwise.
2271 */
2272int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2273 bool force_8bit)
2274{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002275 if (!len || !buf)
2276 return -EINVAL;
2277
Miquel Raynal8878b122017-11-09 14:16:45 +01002278 if (chip->exec_op) {
2279 struct nand_op_instr instrs[] = {
2280 NAND_OP_DATA_IN(len, buf, 0),
2281 };
2282 struct nand_operation op = NAND_OPERATION(instrs);
2283
2284 instrs[0].ctx.data.force_8bit = force_8bit;
2285
2286 return nand_exec_op(chip, &op);
2287 }
2288
Boris Brezillon97d90da2017-11-30 18:01:29 +01002289 if (force_8bit) {
2290 u8 *p = buf;
2291 unsigned int i;
2292
2293 for (i = 0; i < len; i++)
Boris Brezillon7e534322018-09-06 14:05:22 +02002294 p[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002295 } else {
Boris Brezillon7e534322018-09-06 14:05:22 +02002296 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002297 }
2298
2299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(nand_read_data_op);
2302
2303/**
2304 * nand_write_data_op - Write data from the NAND
2305 * @chip: The NAND chip
2306 * @buf: buffer containing the data to send on the bus
2307 * @len: length of the buffer
2308 * @force_8bit: force 8-bit bus access
2309 *
2310 * This function does a raw data write on the bus. Usually used after launching
2311 * another NAND operation like nand_write_page_begin_op().
2312 * This function does not select/unselect the CS line.
2313 *
2314 * Returns 0 on success, a negative error code otherwise.
2315 */
2316int nand_write_data_op(struct nand_chip *chip, const void *buf,
2317 unsigned int len, bool force_8bit)
2318{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002319 if (!len || !buf)
2320 return -EINVAL;
2321
Miquel Raynal8878b122017-11-09 14:16:45 +01002322 if (chip->exec_op) {
2323 struct nand_op_instr instrs[] = {
2324 NAND_OP_DATA_OUT(len, buf, 0),
2325 };
2326 struct nand_operation op = NAND_OPERATION(instrs);
2327
2328 instrs[0].ctx.data.force_8bit = force_8bit;
2329
2330 return nand_exec_op(chip, &op);
2331 }
2332
Boris Brezillon97d90da2017-11-30 18:01:29 +01002333 if (force_8bit) {
2334 const u8 *p = buf;
2335 unsigned int i;
2336
2337 for (i = 0; i < len; i++)
Boris Brezillonc0739d82018-09-06 14:05:23 +02002338 chip->write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002339 } else {
Boris Brezillonc0739d82018-09-06 14:05:23 +02002340 chip->write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002341 }
2342
2343 return 0;
2344}
2345EXPORT_SYMBOL_GPL(nand_write_data_op);
2346
2347/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002348 * struct nand_op_parser_ctx - Context used by the parser
2349 * @instrs: array of all the instructions that must be addressed
2350 * @ninstrs: length of the @instrs array
2351 * @subop: Sub-operation to be passed to the NAND controller
2352 *
2353 * This structure is used by the core to split NAND operations into
2354 * sub-operations that can be handled by the NAND controller.
2355 */
2356struct nand_op_parser_ctx {
2357 const struct nand_op_instr *instrs;
2358 unsigned int ninstrs;
2359 struct nand_subop subop;
2360};
2361
2362/**
2363 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2364 * @pat: the parser pattern element that matches @instr
2365 * @instr: pointer to the instruction to check
2366 * @start_offset: this is an in/out parameter. If @instr has already been
2367 * split, then @start_offset is the offset from which to start
2368 * (either an address cycle or an offset in the data buffer).
2369 * Conversely, if the function returns true (ie. instr must be
2370 * split), this parameter is updated to point to the first
2371 * data/address cycle that has not been taken care of.
2372 *
2373 * Some NAND controllers are limited and cannot send X address cycles with a
2374 * unique operation, or cannot read/write more than Y bytes at the same time.
2375 * In this case, split the instruction that does not fit in a single
2376 * controller-operation into two or more chunks.
2377 *
2378 * Returns true if the instruction must be split, false otherwise.
2379 * The @start_offset parameter is also updated to the offset at which the next
2380 * bundle of instruction must start (if an address or a data instruction).
2381 */
2382static bool
2383nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2384 const struct nand_op_instr *instr,
2385 unsigned int *start_offset)
2386{
2387 switch (pat->type) {
2388 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002389 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002390 break;
2391
2392 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002393 pat->ctx.addr.maxcycles) {
2394 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002395 return true;
2396 }
2397 break;
2398
2399 case NAND_OP_DATA_IN_INSTR:
2400 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002401 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002402 break;
2403
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002404 if (instr->ctx.data.len - *start_offset >
2405 pat->ctx.data.maxlen) {
2406 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002407 return true;
2408 }
2409 break;
2410
2411 default:
2412 break;
2413 }
2414
2415 return false;
2416}
2417
2418/**
2419 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2420 * remaining in the parser context
2421 * @pat: the pattern to test
2422 * @ctx: the parser context structure to match with the pattern @pat
2423 *
2424 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2425 * Returns true if this is the case, false ortherwise. When true is returned,
2426 * @ctx->subop is updated with the set of instructions to be passed to the
2427 * controller driver.
2428 */
2429static bool
2430nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2431 struct nand_op_parser_ctx *ctx)
2432{
2433 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2434 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2435 const struct nand_op_instr *instr = ctx->subop.instrs;
2436 unsigned int i, ninstrs;
2437
2438 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2439 /*
2440 * The pattern instruction does not match the operation
2441 * instruction. If the instruction is marked optional in the
2442 * pattern definition, we skip the pattern element and continue
2443 * to the next one. If the element is mandatory, there's no
2444 * match and we can return false directly.
2445 */
2446 if (instr->type != pat->elems[i].type) {
2447 if (!pat->elems[i].optional)
2448 return false;
2449
2450 continue;
2451 }
2452
2453 /*
2454 * Now check the pattern element constraints. If the pattern is
2455 * not able to handle the whole instruction in a single step,
2456 * we have to split it.
2457 * The last_instr_end_off value comes back updated to point to
2458 * the position where we have to split the instruction (the
2459 * start of the next subop chunk).
2460 */
2461 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2462 &instr_offset)) {
2463 ninstrs++;
2464 i++;
2465 break;
2466 }
2467
2468 instr++;
2469 ninstrs++;
2470 instr_offset = 0;
2471 }
2472
2473 /*
2474 * This can happen if all instructions of a pattern are optional.
2475 * Still, if there's not at least one instruction handled by this
2476 * pattern, this is not a match, and we should try the next one (if
2477 * any).
2478 */
2479 if (!ninstrs)
2480 return false;
2481
2482 /*
2483 * We had a match on the pattern head, but the pattern may be longer
2484 * than the instructions we're asked to execute. We need to make sure
2485 * there's no mandatory elements in the pattern tail.
2486 */
2487 for (; i < pat->nelems; i++) {
2488 if (!pat->elems[i].optional)
2489 return false;
2490 }
2491
2492 /*
2493 * We have a match: update the subop structure accordingly and return
2494 * true.
2495 */
2496 ctx->subop.ninstrs = ninstrs;
2497 ctx->subop.last_instr_end_off = instr_offset;
2498
2499 return true;
2500}
2501
2502#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2503static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2504{
2505 const struct nand_op_instr *instr;
2506 char *prefix = " ";
2507 unsigned int i;
2508
2509 pr_debug("executing subop:\n");
2510
2511 for (i = 0; i < ctx->ninstrs; i++) {
2512 instr = &ctx->instrs[i];
2513
2514 if (instr == &ctx->subop.instrs[0])
2515 prefix = " ->";
2516
2517 switch (instr->type) {
2518 case NAND_OP_CMD_INSTR:
2519 pr_debug("%sCMD [0x%02x]\n", prefix,
2520 instr->ctx.cmd.opcode);
2521 break;
2522 case NAND_OP_ADDR_INSTR:
2523 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2524 instr->ctx.addr.naddrs,
2525 instr->ctx.addr.naddrs < 64 ?
2526 instr->ctx.addr.naddrs : 64,
2527 instr->ctx.addr.addrs);
2528 break;
2529 case NAND_OP_DATA_IN_INSTR:
2530 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2531 instr->ctx.data.len,
2532 instr->ctx.data.force_8bit ?
2533 ", force 8-bit" : "");
2534 break;
2535 case NAND_OP_DATA_OUT_INSTR:
2536 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2537 instr->ctx.data.len,
2538 instr->ctx.data.force_8bit ?
2539 ", force 8-bit" : "");
2540 break;
2541 case NAND_OP_WAITRDY_INSTR:
2542 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2543 instr->ctx.waitrdy.timeout_ms);
2544 break;
2545 }
2546
2547 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2548 prefix = " ";
2549 }
2550}
2551#else
2552static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2553{
2554 /* NOP */
2555}
2556#endif
2557
2558/**
2559 * nand_op_parser_exec_op - exec_op parser
2560 * @chip: the NAND chip
2561 * @parser: patterns description provided by the controller driver
2562 * @op: the NAND operation to address
2563 * @check_only: when true, the function only checks if @op can be handled but
2564 * does not execute the operation
2565 *
2566 * Helper function designed to ease integration of NAND controller drivers that
2567 * only support a limited set of instruction sequences. The supported sequences
2568 * are described in @parser, and the framework takes care of splitting @op into
2569 * multiple sub-operations (if required) and pass them back to the ->exec()
2570 * callback of the matching pattern if @check_only is set to false.
2571 *
2572 * NAND controller drivers should call this function from their own ->exec_op()
2573 * implementation.
2574 *
2575 * Returns 0 on success, a negative error code otherwise. A failure can be
2576 * caused by an unsupported operation (none of the supported patterns is able
2577 * to handle the requested operation), or an error returned by one of the
2578 * matching pattern->exec() hook.
2579 */
2580int nand_op_parser_exec_op(struct nand_chip *chip,
2581 const struct nand_op_parser *parser,
2582 const struct nand_operation *op, bool check_only)
2583{
2584 struct nand_op_parser_ctx ctx = {
2585 .subop.instrs = op->instrs,
2586 .instrs = op->instrs,
2587 .ninstrs = op->ninstrs,
2588 };
2589 unsigned int i;
2590
2591 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2592 int ret;
2593
2594 for (i = 0; i < parser->npatterns; i++) {
2595 const struct nand_op_parser_pattern *pattern;
2596
2597 pattern = &parser->patterns[i];
2598 if (!nand_op_parser_match_pat(pattern, &ctx))
2599 continue;
2600
2601 nand_op_parser_trace(&ctx);
2602
2603 if (check_only)
2604 break;
2605
2606 ret = pattern->exec(chip, &ctx.subop);
2607 if (ret)
2608 return ret;
2609
2610 break;
2611 }
2612
2613 if (i == parser->npatterns) {
2614 pr_debug("->exec_op() parser: pattern not found!\n");
2615 return -ENOTSUPP;
2616 }
2617
2618 /*
2619 * Update the context structure by pointing to the start of the
2620 * next subop.
2621 */
2622 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2623 if (ctx.subop.last_instr_end_off)
2624 ctx.subop.instrs -= 1;
2625
2626 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2627 }
2628
2629 return 0;
2630}
2631EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2632
2633static bool nand_instr_is_data(const struct nand_op_instr *instr)
2634{
2635 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2636 instr->type == NAND_OP_DATA_OUT_INSTR);
2637}
2638
2639static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2640 unsigned int instr_idx)
2641{
2642 return subop && instr_idx < subop->ninstrs;
2643}
2644
Miquel Raynal760c4352018-07-19 00:09:12 +02002645static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2646 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002647{
2648 if (instr_idx)
2649 return 0;
2650
2651 return subop->first_instr_start_off;
2652}
2653
2654/**
2655 * nand_subop_get_addr_start_off - Get the start offset in an address array
2656 * @subop: The entire sub-operation
2657 * @instr_idx: Index of the instruction inside the sub-operation
2658 *
2659 * During driver development, one could be tempted to directly use the
2660 * ->addr.addrs field of address instructions. This is wrong as address
2661 * instructions might be split.
2662 *
2663 * Given an address instruction, returns the offset of the first cycle to issue.
2664 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002665unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2666 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002667{
Miquel Raynal760c4352018-07-19 00:09:12 +02002668 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2669 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2670 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002671
2672 return nand_subop_get_start_off(subop, instr_idx);
2673}
2674EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2675
2676/**
2677 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2678 * @subop: The entire sub-operation
2679 * @instr_idx: Index of the instruction inside the sub-operation
2680 *
2681 * During driver development, one could be tempted to directly use the
2682 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2683 * might be split.
2684 *
2685 * Given an address instruction, returns the number of address cycle to issue.
2686 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002687unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2688 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002689{
2690 int start_off, end_off;
2691
Miquel Raynal760c4352018-07-19 00:09:12 +02002692 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2693 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2694 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002695
2696 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2697
2698 if (instr_idx == subop->ninstrs - 1 &&
2699 subop->last_instr_end_off)
2700 end_off = subop->last_instr_end_off;
2701 else
2702 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2703
2704 return end_off - start_off;
2705}
2706EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2707
2708/**
2709 * nand_subop_get_data_start_off - Get the start offset in a data array
2710 * @subop: The entire sub-operation
2711 * @instr_idx: Index of the instruction inside the sub-operation
2712 *
2713 * During driver development, one could be tempted to directly use the
2714 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2715 * instructions might be split.
2716 *
2717 * Given a data instruction, returns the offset to start from.
2718 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002719unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2720 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002721{
Miquel Raynal760c4352018-07-19 00:09:12 +02002722 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2723 !nand_instr_is_data(&subop->instrs[instr_idx])))
2724 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002725
2726 return nand_subop_get_start_off(subop, instr_idx);
2727}
2728EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2729
2730/**
2731 * nand_subop_get_data_len - Get the number of bytes to retrieve
2732 * @subop: The entire sub-operation
2733 * @instr_idx: Index of the instruction inside the sub-operation
2734 *
2735 * During driver development, one could be tempted to directly use the
2736 * ->data->len field of a data instruction. This is wrong as data instructions
2737 * might be split.
2738 *
2739 * Returns the length of the chunk of data to send/receive.
2740 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002741unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2742 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002743{
2744 int start_off = 0, end_off;
2745
Miquel Raynal760c4352018-07-19 00:09:12 +02002746 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2747 !nand_instr_is_data(&subop->instrs[instr_idx])))
2748 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002749
2750 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2751
2752 if (instr_idx == subop->ninstrs - 1 &&
2753 subop->last_instr_end_off)
2754 end_off = subop->last_instr_end_off;
2755 else
2756 end_off = subop->instrs[instr_idx].ctx.data.len;
2757
2758 return end_off - start_off;
2759}
2760EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002763 * nand_reset - Reset and initialize a NAND device
2764 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002765 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002766 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002767 * Save the timings data structure, then apply SDR timings mode 0 (see
2768 * nand_reset_data_interface for details), do the reset operation, and
2769 * apply back the previous timings.
2770 *
2771 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002772 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002773int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002774{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002775 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002776 int ret;
2777
Boris Brezillon104e4422017-03-16 09:35:58 +01002778 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002779 if (ret)
2780 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002781
Boris Brezillon73f907f2016-10-24 16:46:20 +02002782 /*
2783 * The CS line has to be released before we can apply the new NAND
2784 * interface settings, hence this weird ->select_chip() dance.
2785 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002786 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002787 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002788 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002789 if (ret)
2790 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002791
Miquel Raynal107b7d62018-03-19 14:47:25 +01002792 /*
2793 * A nand_reset_data_interface() put both the NAND chip and the NAND
2794 * controller in timings mode 0. If the default mode for this chip is
2795 * also 0, no need to proceed to the change again. Plus, at probe time,
2796 * nand_setup_data_interface() uses ->set/get_features() which would
2797 * fail anyway as the parameter page is not available yet.
2798 */
2799 if (!chip->onfi_timing_mode_default)
2800 return 0;
2801
Miquel Raynal17fa8042017-11-30 18:01:31 +01002802 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002803 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002804 if (ret)
2805 return ret;
2806
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002807 return 0;
2808}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002809EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002810
2811/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002812 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2813 * @buf: buffer to test
2814 * @len: buffer length
2815 * @bitflips_threshold: maximum number of bitflips
2816 *
2817 * Check if a buffer contains only 0xff, which means the underlying region
2818 * has been erased and is ready to be programmed.
2819 * The bitflips_threshold specify the maximum number of bitflips before
2820 * considering the region is not erased.
2821 * Note: The logic of this function has been extracted from the memweight
2822 * implementation, except that nand_check_erased_buf function exit before
2823 * testing the whole buffer if the number of bitflips exceed the
2824 * bitflips_threshold value.
2825 *
2826 * Returns a positive number of bitflips less than or equal to
2827 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2828 * threshold.
2829 */
2830static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2831{
2832 const unsigned char *bitmap = buf;
2833 int bitflips = 0;
2834 int weight;
2835
2836 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2837 len--, bitmap++) {
2838 weight = hweight8(*bitmap);
2839 bitflips += BITS_PER_BYTE - weight;
2840 if (unlikely(bitflips > bitflips_threshold))
2841 return -EBADMSG;
2842 }
2843
2844 for (; len >= sizeof(long);
2845 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002846 unsigned long d = *((unsigned long *)bitmap);
2847 if (d == ~0UL)
2848 continue;
2849 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002850 bitflips += BITS_PER_LONG - weight;
2851 if (unlikely(bitflips > bitflips_threshold))
2852 return -EBADMSG;
2853 }
2854
2855 for (; len > 0; len--, bitmap++) {
2856 weight = hweight8(*bitmap);
2857 bitflips += BITS_PER_BYTE - weight;
2858 if (unlikely(bitflips > bitflips_threshold))
2859 return -EBADMSG;
2860 }
2861
2862 return bitflips;
2863}
2864
2865/**
2866 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2867 * 0xff data
2868 * @data: data buffer to test
2869 * @datalen: data length
2870 * @ecc: ECC buffer
2871 * @ecclen: ECC length
2872 * @extraoob: extra OOB buffer
2873 * @extraooblen: extra OOB length
2874 * @bitflips_threshold: maximum number of bitflips
2875 *
2876 * Check if a data buffer and its associated ECC and OOB data contains only
2877 * 0xff pattern, which means the underlying region has been erased and is
2878 * ready to be programmed.
2879 * The bitflips_threshold specify the maximum number of bitflips before
2880 * considering the region as not erased.
2881 *
2882 * Note:
2883 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2884 * different from the NAND page size. When fixing bitflips, ECC engines will
2885 * report the number of errors per chunk, and the NAND core infrastructure
2886 * expect you to return the maximum number of bitflips for the whole page.
2887 * This is why you should always use this function on a single chunk and
2888 * not on the whole page. After checking each chunk you should update your
2889 * max_bitflips value accordingly.
2890 * 2/ When checking for bitflips in erased pages you should not only check
2891 * the payload data but also their associated ECC data, because a user might
2892 * have programmed almost all bits to 1 but a few. In this case, we
2893 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2894 * this case.
2895 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2896 * data are protected by the ECC engine.
2897 * It could also be used if you support subpages and want to attach some
2898 * extra OOB data to an ECC chunk.
2899 *
2900 * Returns a positive number of bitflips less than or equal to
2901 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2902 * threshold. In case of success, the passed buffers are filled with 0xff.
2903 */
2904int nand_check_erased_ecc_chunk(void *data, int datalen,
2905 void *ecc, int ecclen,
2906 void *extraoob, int extraooblen,
2907 int bitflips_threshold)
2908{
2909 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2910
2911 data_bitflips = nand_check_erased_buf(data, datalen,
2912 bitflips_threshold);
2913 if (data_bitflips < 0)
2914 return data_bitflips;
2915
2916 bitflips_threshold -= data_bitflips;
2917
2918 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2919 if (ecc_bitflips < 0)
2920 return ecc_bitflips;
2921
2922 bitflips_threshold -= ecc_bitflips;
2923
2924 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2925 bitflips_threshold);
2926 if (extraoob_bitflips < 0)
2927 return extraoob_bitflips;
2928
2929 if (data_bitflips)
2930 memset(data, 0xff, datalen);
2931
2932 if (ecc_bitflips)
2933 memset(ecc, 0xff, ecclen);
2934
2935 if (extraoob_bitflips)
2936 memset(extraoob, 0xff, extraooblen);
2937
2938 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2939}
2940EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2941
2942/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002943 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002944 * @chip: nand chip info structure
2945 * @buf: buffer to store read data
2946 * @oob_required: caller requires OOB data read to chip->oob_poi
2947 * @page: page number to read
2948 *
2949 * Returns -ENOTSUPP unconditionally.
2950 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002951int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2952 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002953{
2954 return -ENOTSUPP;
2955}
2956EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2957
2958/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002959 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002960 * @chip: nand chip info structure
2961 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002962 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002963 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002964 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002965 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002966 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002967int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2968 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002969{
Boris Brezillonb9761682018-09-06 14:05:20 +02002970 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002971 int ret;
2972
Boris Brezillon25f815f2017-11-30 18:01:30 +01002973 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002974 if (ret)
2975 return ret;
2976
2977 if (oob_required) {
2978 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2979 false);
2980 if (ret)
2981 return ret;
2982 }
2983
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002984 return 0;
2985}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002986EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002987
2988/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002989 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002990 * @chip: nand chip info structure
2991 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002992 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002993 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002994 *
2995 * We need a special oob layout and handling even when OOB isn't used.
2996 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002997static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07002998 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002999{
Boris Brezillonb9761682018-09-06 14:05:20 +02003000 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003001 int eccsize = chip->ecc.size;
3002 int eccbytes = chip->ecc.bytes;
3003 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003004 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003005
Boris Brezillon25f815f2017-11-30 18:01:30 +01003006 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3007 if (ret)
3008 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003009
3010 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003011 ret = nand_read_data_op(chip, buf, eccsize, false);
3012 if (ret)
3013 return ret;
3014
David Brownell52ff49d2009-03-04 12:01:36 -08003015 buf += eccsize;
3016
3017 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003018 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3019 false);
3020 if (ret)
3021 return ret;
3022
David Brownell52ff49d2009-03-04 12:01:36 -08003023 oob += chip->ecc.prepad;
3024 }
3025
Boris Brezillon97d90da2017-11-30 18:01:29 +01003026 ret = nand_read_data_op(chip, oob, eccbytes, false);
3027 if (ret)
3028 return ret;
3029
David Brownell52ff49d2009-03-04 12:01:36 -08003030 oob += eccbytes;
3031
3032 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003033 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3034 false);
3035 if (ret)
3036 return ret;
3037
David Brownell52ff49d2009-03-04 12:01:36 -08003038 oob += chip->ecc.postpad;
3039 }
3040 }
3041
3042 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003043 if (size) {
3044 ret = nand_read_data_op(chip, oob, size, false);
3045 if (ret)
3046 return ret;
3047 }
David Brownell52ff49d2009-03-04 12:01:36 -08003048
3049 return 0;
3050}
3051
3052/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003053 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003054 * @chip: nand chip info structure
3055 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003056 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003057 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003058 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003059static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3060 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Boris Brezillonb9761682018-09-06 14:05:20 +02003062 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003063 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003064 int eccbytes = chip->ecc.bytes;
3065 int eccsteps = chip->ecc.steps;
3066 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003067 uint8_t *ecc_calc = chip->ecc.calc_buf;
3068 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003069 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003070
Boris Brezillonb9761682018-09-06 14:05:20 +02003071 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003072
3073 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003074 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003075
Boris Brezillon846031d2016-02-03 20:11:00 +01003076 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3077 chip->ecc.total);
3078 if (ret)
3079 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003080
3081 eccsteps = chip->ecc.steps;
3082 p = buf;
3083
3084 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3085 int stat;
3086
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003087 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003088 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003089 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003090 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003091 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003092 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3093 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003094 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003095 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303099 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003100 * @chip: nand chip info structure
3101 * @data_offs: offset of requested data within the page
3102 * @readlen: data length
3103 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003104 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003105 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003106static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3107 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003108{
Boris Brezillonb9761682018-09-06 14:05:20 +02003109 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003110 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003111 uint8_t *p;
3112 int data_col_addr, i, gaps = 0;
3113 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3114 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003115 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003116 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003117 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003118
Brian Norris7854d3f2011-06-23 14:12:08 -07003119 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003120 start_step = data_offs / chip->ecc.size;
3121 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3122 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303123 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003124
Brian Norris8b6e50c2011-05-25 14:59:01 -07003125 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003126 datafrag_len = num_steps * chip->ecc.size;
3127 eccfrag_len = num_steps * chip->ecc.bytes;
3128
3129 data_col_addr = start_step * chip->ecc.size;
3130 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003131 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003132 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003133 if (ret)
3134 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003135
Brian Norris8b6e50c2011-05-25 14:59:01 -07003136 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003137 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003138 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003139
Brian Norris8b6e50c2011-05-25 14:59:01 -07003140 /*
3141 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003142 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003143 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003144 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3145 if (ret)
3146 return ret;
3147
3148 if (oobregion.length < eccfrag_len)
3149 gaps = 1;
3150
Alexey Korolev3d459552008-05-15 17:23:18 +01003151 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003152 ret = nand_change_read_column_op(chip, mtd->writesize,
3153 chip->oob_poi, mtd->oobsize,
3154 false);
3155 if (ret)
3156 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003157 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003159 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003160 * about buswidth alignment in read_buf.
3161 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003162 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003163 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003164 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003165 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003166 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3167 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003168 aligned_len++;
3169
Boris Brezillon97d90da2017-11-30 18:01:29 +01003170 ret = nand_change_read_column_op(chip,
3171 mtd->writesize + aligned_pos,
3172 &chip->oob_poi[aligned_pos],
3173 aligned_len, false);
3174 if (ret)
3175 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003176 }
3177
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003178 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003179 chip->oob_poi, index, eccfrag_len);
3180 if (ret)
3181 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003182
3183 p = bufpoi + data_col_addr;
3184 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3185 int stat;
3186
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003187 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003188 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003189 if (stat == -EBADMSG &&
3190 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3191 /* check for empty pages with bitflips */
3192 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003193 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003194 chip->ecc.bytes,
3195 NULL, 0,
3196 chip->ecc.strength);
3197 }
3198
Mike Dunn3f91e942012-04-25 12:06:09 -07003199 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003200 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003201 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003202 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003203 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3204 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003205 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003206 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003207}
3208
3209/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003210 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003211 * @chip: nand chip info structure
3212 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003213 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003214 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003215 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003216 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003217 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003218static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3219 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003220{
Boris Brezillonb9761682018-09-06 14:05:20 +02003221 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003222 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003223 int eccbytes = chip->ecc.bytes;
3224 int eccsteps = chip->ecc.steps;
3225 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003226 uint8_t *ecc_calc = chip->ecc.calc_buf;
3227 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003228 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003229
Boris Brezillon25f815f2017-11-30 18:01:30 +01003230 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3231 if (ret)
3232 return ret;
3233
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003234 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003235 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003236
3237 ret = nand_read_data_op(chip, p, eccsize, false);
3238 if (ret)
3239 return ret;
3240
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003241 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003242 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003243
3244 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3245 if (ret)
3246 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003247
Boris Brezillon846031d2016-02-03 20:11:00 +01003248 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3249 chip->ecc.total);
3250 if (ret)
3251 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003252
3253 eccsteps = chip->ecc.steps;
3254 p = buf;
3255
3256 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3257 int stat;
3258
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003259 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003260 if (stat == -EBADMSG &&
3261 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3262 /* check for empty pages with bitflips */
3263 stat = nand_check_erased_ecc_chunk(p, eccsize,
3264 &ecc_code[i], eccbytes,
3265 NULL, 0,
3266 chip->ecc.strength);
3267 }
3268
Mike Dunn3f91e942012-04-25 12:06:09 -07003269 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003270 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003271 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003272 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003273 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3274 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003275 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003276 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003277}
3278
3279/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003280 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003281 * @chip: nand chip info structure
3282 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003283 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003285 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003286 * Hardware ECC for large page chips, require OOB to be read first. For this
3287 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3288 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3289 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3290 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003291 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003292static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3293 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003294{
Boris Brezillonb9761682018-09-06 14:05:20 +02003295 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003296 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003297 int eccbytes = chip->ecc.bytes;
3298 int eccsteps = chip->ecc.steps;
3299 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003300 uint8_t *ecc_code = chip->ecc.code_buf;
3301 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003302 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003303
3304 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003305 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3306 if (ret)
3307 return ret;
3308
3309 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3310 if (ret)
3311 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003312
Boris Brezillon846031d2016-02-03 20:11:00 +01003313 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3314 chip->ecc.total);
3315 if (ret)
3316 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003317
3318 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3319 int stat;
3320
Boris Brezillonec476362018-09-06 14:05:17 +02003321 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003322
3323 ret = nand_read_data_op(chip, p, eccsize, false);
3324 if (ret)
3325 return ret;
3326
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003327 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003328
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003329 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003330 if (stat == -EBADMSG &&
3331 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3332 /* check for empty pages with bitflips */
3333 stat = nand_check_erased_ecc_chunk(p, eccsize,
3334 &ecc_code[i], eccbytes,
3335 NULL, 0,
3336 chip->ecc.strength);
3337 }
3338
Mike Dunn3f91e942012-04-25 12:06:09 -07003339 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003340 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003341 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003342 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003343 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3344 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003345 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003346 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003347}
3348
3349/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003350 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003351 * @chip: nand chip info structure
3352 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003353 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003354 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003355 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003356 * The hw generator calculates the error syndrome automatically. Therefore we
3357 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003358 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003359static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3360 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003361{
Boris Brezillonb9761682018-09-06 14:05:20 +02003362 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003363 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003364 int eccbytes = chip->ecc.bytes;
3365 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003366 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003367 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003368 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003369 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003370
Boris Brezillon25f815f2017-11-30 18:01:30 +01003371 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3372 if (ret)
3373 return ret;
3374
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003375 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3376 int stat;
3377
Boris Brezillonec476362018-09-06 14:05:17 +02003378 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003379
3380 ret = nand_read_data_op(chip, p, eccsize, false);
3381 if (ret)
3382 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003383
3384 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003385 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3386 false);
3387 if (ret)
3388 return ret;
3389
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003390 oob += chip->ecc.prepad;
3391 }
3392
Boris Brezillonec476362018-09-06 14:05:17 +02003393 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003394
3395 ret = nand_read_data_op(chip, oob, eccbytes, false);
3396 if (ret)
3397 return ret;
3398
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003399 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003400
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003401 oob += eccbytes;
3402
3403 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003404 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3405 false);
3406 if (ret)
3407 return ret;
3408
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003409 oob += chip->ecc.postpad;
3410 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003411
3412 if (stat == -EBADMSG &&
3413 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3414 /* check for empty pages with bitflips */
3415 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3416 oob - eccpadbytes,
3417 eccpadbytes,
3418 NULL, 0,
3419 chip->ecc.strength);
3420 }
3421
3422 if (stat < 0) {
3423 mtd->ecc_stats.failed++;
3424 } else {
3425 mtd->ecc_stats.corrected += stat;
3426 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3427 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003428 }
3429
3430 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003431 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003432 if (i) {
3433 ret = nand_read_data_op(chip, oob, i, false);
3434 if (ret)
3435 return ret;
3436 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003437
Mike Dunn3f91e942012-04-25 12:06:09 -07003438 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003439}
3440
3441/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003442 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003443 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003444 * @oob: oob destination address
3445 * @ops: oob ops structure
3446 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003447 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003448static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003449 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003450{
Boris Brezillon846031d2016-02-03 20:11:00 +01003451 struct nand_chip *chip = mtd_to_nand(mtd);
3452 int ret;
3453
Florian Fainellif8ac0412010-09-07 13:23:43 +02003454 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003455
Brian Norris0612b9d2011-08-30 18:45:40 -07003456 case MTD_OPS_PLACE_OOB:
3457 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003458 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3459 return oob + len;
3460
Boris Brezillon846031d2016-02-03 20:11:00 +01003461 case MTD_OPS_AUTO_OOB:
3462 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3463 ops->ooboffs, len);
3464 BUG_ON(ret);
3465 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003466
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003467 default:
3468 BUG();
3469 }
3470 return NULL;
3471}
3472
3473/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003474 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003475 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003476 * @retry_mode: the retry mode to use
3477 *
3478 * Some vendors supply a special command to shift the Vt threshold, to be used
3479 * when there are too many bitflips in a page (i.e., ECC error). After setting
3480 * a new threshold, the host should retry reading the page.
3481 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003482static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003483{
Brian Norrisba84fb52014-01-03 15:13:33 -08003484 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3485
3486 if (retry_mode >= chip->read_retries)
3487 return -EINVAL;
3488
3489 if (!chip->setup_read_retry)
3490 return -EOPNOTSUPP;
3491
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003492 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003493}
3494
Boris Brezillon85e08e52018-07-27 09:44:17 +02003495static void nand_wait_readrdy(struct nand_chip *chip)
3496{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003497 const struct nand_sdr_timings *sdr;
3498
Boris Brezillon85e08e52018-07-27 09:44:17 +02003499 if (!(chip->options & NAND_NEED_READRDY))
3500 return;
3501
Boris Brezillon52f05b62018-07-27 09:44:18 +02003502 sdr = nand_get_sdr_timings(&chip->data_interface);
3503 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003504}
3505
Brian Norrisba84fb52014-01-03 15:13:33 -08003506/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003507 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003508 * @mtd: MTD device structure
3509 * @from: offset to read from
3510 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003511 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003512 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003513 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003514static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3515 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003516{
Brian Norrise47f3db2012-05-02 10:14:56 -07003517 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003518 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003519 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003520 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003521 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003522 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003523
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003524 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003525 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003526 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003527 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003528 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003530 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003531 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003533 realpage = (int)(from >> chip->page_shift);
3534 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003536 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003538 buf = ops->datbuf;
3539 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003540 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003541
Florian Fainellif8ac0412010-09-07 13:23:43 +02003542 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003543 unsigned int ecc_failures = mtd->ecc_stats.failed;
3544
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003545 bytes = min(mtd->writesize - col, readlen);
3546 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003547
Kamal Dasu66507c72014-05-01 20:51:19 -04003548 if (!aligned)
3549 use_bufpoi = 1;
3550 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003551 use_bufpoi = !virt_addr_valid(buf) ||
3552 !IS_ALIGNED((unsigned long)buf,
3553 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003554 else
3555 use_bufpoi = 0;
3556
Brian Norris8b6e50c2011-05-25 14:59:01 -07003557 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003558 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003559 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003560
3561 if (use_bufpoi && aligned)
3562 pr_debug("%s: using read bounce buffer for buf@%p\n",
3563 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Brian Norrisba84fb52014-01-03 15:13:33 -08003565read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003566 /*
3567 * Now read the page into the buffer. Absent an error,
3568 * the read methods return max bitflips per ecc step.
3569 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003570 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003571 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003572 oob_required,
3573 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003574 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3575 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003576 ret = chip->ecc.read_subpage(chip, col, bytes,
3577 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003578 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003579 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003580 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003581 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003582 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003583 /* Invalidate page cache */
3584 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003585 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003586 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003587
3588 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003589 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003590 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003591 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003592 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003593 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003594 chip->pagebuf_bitflips = ret;
3595 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003596 /* Invalidate page cache */
3597 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003598 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003599 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003601
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003602 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003603 int toread = min(oobreadlen, max_oobsize);
3604
3605 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003606 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003607 oob, ops, toread);
3608 oobreadlen -= toread;
3609 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003610 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003611
Boris Brezillon85e08e52018-07-27 09:44:17 +02003612 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003613
Brian Norrisba84fb52014-01-03 15:13:33 -08003614 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003615 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003616 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003617 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003618 retry_mode);
3619 if (ret < 0)
3620 break;
3621
3622 /* Reset failures; retry */
3623 mtd->ecc_stats.failed = ecc_failures;
3624 goto read_retry;
3625 } else {
3626 /* No more retry modes; real failure */
3627 ecc_fail = true;
3628 }
3629 }
3630
3631 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003632 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003633 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003634 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003635 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003636 max_bitflips = max_t(unsigned int, max_bitflips,
3637 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003640 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003641
Brian Norrisba84fb52014-01-03 15:13:33 -08003642 /* Reset to retry mode 0 */
3643 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003644 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003645 if (ret < 0)
3646 break;
3647 retry_mode = 0;
3648 }
3649
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003650 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003651 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Brian Norris8b6e50c2011-05-25 14:59:01 -07003653 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 col = 0;
3655 /* Increment page address */
3656 realpage++;
3657
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003658 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /* Check, if we cross a chip boundary */
3660 if (!page) {
3661 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003662 chip->select_chip(chip, -1);
3663 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003666 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003668 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003669 if (oob)
3670 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Mike Dunn3f91e942012-04-25 12:06:09 -07003672 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003673 return ret;
3674
Brian Norrisb72f3df2013-12-03 11:04:14 -08003675 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003676 return -EBADMSG;
3677
Mike Dunnedbc45402012-04-25 12:06:11 -07003678 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003679}
3680
3681/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003682 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003683 * @chip: nand chip info structure
3684 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003685 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003686int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003687{
Boris Brezillonb9761682018-09-06 14:05:20 +02003688 struct mtd_info *mtd = nand_to_mtd(chip);
3689
Boris Brezillon97d90da2017-11-30 18:01:29 +01003690 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003691}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003692EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003693
3694/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003695 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003696 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003697 * @chip: nand chip info structure
3698 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003699 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003700int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701{
Boris Brezillonb9761682018-09-06 14:05:20 +02003702 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003703 int length = mtd->oobsize;
3704 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3705 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003706 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003707 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003708
Boris Brezillon97d90da2017-11-30 18:01:29 +01003709 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3710 if (ret)
3711 return ret;
3712
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003713 for (i = 0; i < chip->ecc.steps; i++) {
3714 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003715 int ret;
3716
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003717 pos = eccsize + i * (eccsize + chunk);
3718 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003719 ret = nand_change_read_column_op(chip, pos,
3720 NULL, 0,
3721 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003722 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003723 ret = nand_read_page_op(chip, page, pos, NULL,
3724 0);
3725
3726 if (ret)
3727 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003728 } else
3729 sndrnd = 1;
3730 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003731
3732 ret = nand_read_data_op(chip, bufpoi, toread, false);
3733 if (ret)
3734 return ret;
3735
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003736 bufpoi += toread;
3737 length -= toread;
3738 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003739 if (length > 0) {
3740 ret = nand_read_data_op(chip, bufpoi, length, false);
3741 if (ret)
3742 return ret;
3743 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003744
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003745 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003746}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003747EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003748
3749/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003750 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003751 * @chip: nand chip info structure
3752 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003753 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003754int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003755{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003756 struct mtd_info *mtd = nand_to_mtd(chip);
3757
Boris Brezillon97d90da2017-11-30 18:01:29 +01003758 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3759 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003761EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003762
3763/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003764 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003765 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003766 * @chip: nand chip info structure
3767 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003768 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003769int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003770{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003771 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3773 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003774 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003775 const uint8_t *bufpoi = chip->oob_poi;
3776
3777 /*
3778 * data-ecc-data-ecc ... ecc-oob
3779 * or
3780 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3781 */
3782 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3783 pos = steps * (eccsize + chunk);
3784 steps = 0;
3785 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003786 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787
Boris Brezillon97d90da2017-11-30 18:01:29 +01003788 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3789 if (ret)
3790 return ret;
3791
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003792 for (i = 0; i < steps; i++) {
3793 if (sndcmd) {
3794 if (mtd->writesize <= 512) {
3795 uint32_t fill = 0xFFFFFFFF;
3796
3797 len = eccsize;
3798 while (len > 0) {
3799 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003800
3801 ret = nand_write_data_op(chip, &fill,
3802 num, false);
3803 if (ret)
3804 return ret;
3805
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003806 len -= num;
3807 }
3808 } else {
3809 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003810 ret = nand_change_write_column_op(chip, pos,
3811 NULL, 0,
3812 false);
3813 if (ret)
3814 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003815 }
3816 } else
3817 sndcmd = 1;
3818 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003819
3820 ret = nand_write_data_op(chip, bufpoi, len, false);
3821 if (ret)
3822 return ret;
3823
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003824 bufpoi += len;
3825 length -= len;
3826 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003827 if (length > 0) {
3828 ret = nand_write_data_op(chip, bufpoi, length, false);
3829 if (ret)
3830 return ret;
3831 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003832
Boris Brezillon97d90da2017-11-30 18:01:29 +01003833 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003834}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003835EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003836
3837/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003838 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003839 * @mtd: MTD device structure
3840 * @from: offset to read from
3841 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003843 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003845static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3846 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003848 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003849 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003850 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003851 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003852 int readlen = ops->ooblen;
3853 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003854 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003855 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Brian Norris289c0522011-07-19 10:06:09 -07003857 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303858 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Brian Norris041e4572011-06-23 16:45:24 -07003860 stats = mtd->ecc_stats;
3861
Boris BREZILLON29f10582016-03-07 10:46:52 +01003862 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003863
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003864 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003865 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003867 /* Shift to get page */
3868 realpage = (int)(from >> chip->page_shift);
3869 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Florian Fainellif8ac0412010-09-07 13:23:43 +02003871 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003872 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003873 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003874 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003875 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003876
3877 if (ret < 0)
3878 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003879
3880 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003881 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003882
Boris Brezillon85e08e52018-07-27 09:44:17 +02003883 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003884
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003885 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3886
Vitaly Wool70145682006-11-03 18:20:38 +03003887 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003888 if (!readlen)
3889 break;
3890
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003891 /* Increment page address */
3892 realpage++;
3893
3894 page = realpage & chip->pagemask;
3895 /* Check, if we cross a chip boundary */
3896 if (!page) {
3897 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003898 chip->select_chip(chip, -1);
3899 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003902 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003904 ops->oobretlen = ops->ooblen - readlen;
3905
3906 if (ret < 0)
3907 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003908
3909 if (mtd->ecc_stats.failed - stats.failed)
3910 return -EBADMSG;
3911
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003912 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
3915/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003916 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003917 * @mtd: MTD device structure
3918 * @from: offset to read from
3919 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003921 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003923static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3924 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003926 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003927
3928 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003930 if (ops->mode != MTD_OPS_PLACE_OOB &&
3931 ops->mode != MTD_OPS_AUTO_OOB &&
3932 ops->mode != MTD_OPS_RAW)
3933 return -ENOTSUPP;
3934
Huang Shijie6a8214a2012-11-19 14:43:30 +08003935 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003937 if (!ops->datbuf)
3938 ret = nand_do_read_oob(mtd, from, ops);
3939 else
3940 ret = nand_do_read_ops(mtd, from, ops);
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003943 return ret;
3944}
3945
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003946/**
3947 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003948 * @chip: nand chip info structure
3949 * @buf: data buffer
3950 * @oob_required: must write chip->oob_poi to OOB
3951 * @page: page number to write
3952 *
3953 * Returns -ENOTSUPP unconditionally.
3954 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003955int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3956 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003957{
3958 return -ENOTSUPP;
3959}
3960EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003961
3962/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003963 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003964 * @chip: nand chip info structure
3965 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003966 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003967 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003968 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003969 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003970 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003971int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3972 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003973{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003974 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003975 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003976
Boris Brezillon25f815f2017-11-30 18:01:30 +01003977 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003978 if (ret)
3979 return ret;
3980
3981 if (oob_required) {
3982 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3983 false);
3984 if (ret)
3985 return ret;
3986 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003987
Boris Brezillon25f815f2017-11-30 18:01:30 +01003988 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003990EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003992/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003993 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003994 * @chip: nand chip info structure
3995 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003996 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003997 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003998 *
3999 * We need a special oob layout and handling even when ECC isn't checked.
4000 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004001static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004002 const uint8_t *buf, int oob_required,
4003 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004004{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004005 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004006 int eccsize = chip->ecc.size;
4007 int eccbytes = chip->ecc.bytes;
4008 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004009 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004010
Boris Brezillon25f815f2017-11-30 18:01:30 +01004011 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4012 if (ret)
4013 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004014
4015 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004016 ret = nand_write_data_op(chip, buf, eccsize, false);
4017 if (ret)
4018 return ret;
4019
David Brownell52ff49d2009-03-04 12:01:36 -08004020 buf += eccsize;
4021
4022 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004023 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4024 false);
4025 if (ret)
4026 return ret;
4027
David Brownell52ff49d2009-03-04 12:01:36 -08004028 oob += chip->ecc.prepad;
4029 }
4030
Boris Brezillon97d90da2017-11-30 18:01:29 +01004031 ret = nand_write_data_op(chip, oob, eccbytes, false);
4032 if (ret)
4033 return ret;
4034
David Brownell52ff49d2009-03-04 12:01:36 -08004035 oob += eccbytes;
4036
4037 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004038 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4039 false);
4040 if (ret)
4041 return ret;
4042
David Brownell52ff49d2009-03-04 12:01:36 -08004043 oob += chip->ecc.postpad;
4044 }
4045 }
4046
4047 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004048 if (size) {
4049 ret = nand_write_data_op(chip, oob, size, false);
4050 if (ret)
4051 return ret;
4052 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004053
Boris Brezillon25f815f2017-11-30 18:01:30 +01004054 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004055}
4056/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004057 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004058 * @chip: nand chip info structure
4059 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004060 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004061 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004062 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004063static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4064 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004065{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004066 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004067 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004068 int eccbytes = chip->ecc.bytes;
4069 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004070 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004071 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004072
Brian Norris7854d3f2011-06-23 14:12:08 -07004073 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004074 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004075 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004076
Boris Brezillon846031d2016-02-03 20:11:00 +01004077 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4078 chip->ecc.total);
4079 if (ret)
4080 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004081
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004082 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004083}
4084
4085/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004086 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004087 * @chip: nand chip info structure
4088 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004089 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004090 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004091 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004092static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4093 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004094{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004095 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004096 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004097 int eccbytes = chip->ecc.bytes;
4098 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004099 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004100 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101
Boris Brezillon25f815f2017-11-30 18:01:30 +01004102 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4103 if (ret)
4104 return ret;
4105
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004106 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004107 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004108
4109 ret = nand_write_data_op(chip, p, eccsize, false);
4110 if (ret)
4111 return ret;
4112
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004113 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004114 }
4115
Boris Brezillon846031d2016-02-03 20:11:00 +01004116 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4117 chip->ecc.total);
4118 if (ret)
4119 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004120
Boris Brezillon97d90da2017-11-30 18:01:29 +01004121 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4122 if (ret)
4123 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004124
Boris Brezillon25f815f2017-11-30 18:01:30 +01004125 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004126}
4127
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304128
4129/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004130 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304131 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004132 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304133 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004134 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304135 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004136 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304137 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004138static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4139 uint32_t data_len, const uint8_t *buf,
4140 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304141{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004142 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304143 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004144 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304145 int ecc_size = chip->ecc.size;
4146 int ecc_bytes = chip->ecc.bytes;
4147 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304148 uint32_t start_step = offset / ecc_size;
4149 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4150 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004151 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304152
Boris Brezillon25f815f2017-11-30 18:01:30 +01004153 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4154 if (ret)
4155 return ret;
4156
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304157 for (step = 0; step < ecc_steps; step++) {
4158 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004159 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304160
4161 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004162 ret = nand_write_data_op(chip, buf, ecc_size, false);
4163 if (ret)
4164 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304165
4166 /* mask ECC of un-touched subpages by padding 0xFF */
4167 if ((step < start_step) || (step > end_step))
4168 memset(ecc_calc, 0xff, ecc_bytes);
4169 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004170 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304171
4172 /* mask OOB of un-touched subpages by padding 0xFF */
4173 /* if oob_required, preserve OOB metadata of written subpage */
4174 if (!oob_required || (step < start_step) || (step > end_step))
4175 memset(oob_buf, 0xff, oob_bytes);
4176
Brian Norrisd6a950802013-08-08 17:16:36 -07004177 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304178 ecc_calc += ecc_bytes;
4179 oob_buf += oob_bytes;
4180 }
4181
4182 /* copy calculated ECC for whole page to chip->buffer->oob */
4183 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004184 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004185 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4186 chip->ecc.total);
4187 if (ret)
4188 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304189
4190 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004191 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4192 if (ret)
4193 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304194
Boris Brezillon25f815f2017-11-30 18:01:30 +01004195 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196}
4197
4198
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004199/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004200 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004201 * @chip: nand chip info structure
4202 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004203 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004204 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004205 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004206 * The hw generator calculates the error syndrome automatically. Therefore we
4207 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004208 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004209static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4210 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004211{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004212 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004213 int i, eccsize = chip->ecc.size;
4214 int eccbytes = chip->ecc.bytes;
4215 int eccsteps = chip->ecc.steps;
4216 const uint8_t *p = buf;
4217 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004218 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004219
Boris Brezillon25f815f2017-11-30 18:01:30 +01004220 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4221 if (ret)
4222 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004223
4224 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004225 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004226
4227 ret = nand_write_data_op(chip, p, eccsize, false);
4228 if (ret)
4229 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004230
4231 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004232 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4233 false);
4234 if (ret)
4235 return ret;
4236
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004237 oob += chip->ecc.prepad;
4238 }
4239
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004240 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004241
4242 ret = nand_write_data_op(chip, oob, eccbytes, false);
4243 if (ret)
4244 return ret;
4245
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004246 oob += eccbytes;
4247
4248 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004249 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4250 false);
4251 if (ret)
4252 return ret;
4253
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004254 oob += chip->ecc.postpad;
4255 }
4256 }
4257
4258 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004259 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004260 if (i) {
4261 ret = nand_write_data_op(chip, oob, i, false);
4262 if (ret)
4263 return ret;
4264 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004265
Boris Brezillon25f815f2017-11-30 18:01:30 +01004266 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004267}
4268
4269/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004270 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004271 * @mtd: MTD device structure
4272 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304273 * @offset: address offset within the page
4274 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004275 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004276 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004277 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004278 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004279 */
4280static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304281 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004282 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004283{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304284 int status, subpage;
4285
4286 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4287 chip->ecc.write_subpage)
4288 subpage = offset || (data_len < mtd->writesize);
4289 else
4290 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004291
David Woodhouse956e9442006-09-25 17:12:39 +01004292 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004293 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4294 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304295 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004296 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4297 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004298 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004299 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004300
4301 if (status < 0)
4302 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004303
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004304 return 0;
4305}
4306
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004307/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004308 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004309 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004310 * @oob: oob data buffer
4311 * @len: oob data write length
4312 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004313 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004314static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4315 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004316{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004317 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004318 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004319
4320 /*
4321 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4322 * data from a previous OOB read.
4323 */
4324 memset(chip->oob_poi, 0xff, mtd->oobsize);
4325
Florian Fainellif8ac0412010-09-07 13:23:43 +02004326 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004327
Brian Norris0612b9d2011-08-30 18:45:40 -07004328 case MTD_OPS_PLACE_OOB:
4329 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004330 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4331 return oob + len;
4332
Boris Brezillon846031d2016-02-03 20:11:00 +01004333 case MTD_OPS_AUTO_OOB:
4334 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4335 ops->ooboffs, len);
4336 BUG_ON(ret);
4337 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004338
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004339 default:
4340 BUG();
4341 }
4342 return NULL;
4343}
4344
Florian Fainellif8ac0412010-09-07 13:23:43 +02004345#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004346
4347/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004348 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004349 * @mtd: MTD device structure
4350 * @to: offset to write to
4351 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004352 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004353 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004354 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004355static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4356 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004357{
Corentin Labbe73600b62017-09-02 10:49:38 +02004358 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004359 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004360 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004361
4362 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004363 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004364
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004365 uint8_t *oob = ops->oobbuf;
4366 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304367 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004368 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004369
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004370 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004371 if (!writelen)
4372 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004373
Brian Norris8b6e50c2011-05-25 14:59:01 -07004374 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004375 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004376 pr_notice("%s: attempt to write non page aligned data\n",
4377 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004378 return -EINVAL;
4379 }
4380
Thomas Gleixner29072b92006-09-28 15:38:36 +02004381 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382
Thomas Gleixner6a930962006-06-28 00:11:45 +02004383 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004384 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004385
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004386 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004387 if (nand_check_wp(mtd)) {
4388 ret = -EIO;
4389 goto err_out;
4390 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004391
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004392 realpage = (int)(to >> chip->page_shift);
4393 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004394
4395 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004396 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4397 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004398 chip->pagebuf = -1;
4399
Maxim Levitsky782ce792010-02-22 20:39:36 +02004400 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004401 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4402 ret = -EINVAL;
4403 goto err_out;
4404 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004405
Florian Fainellif8ac0412010-09-07 13:23:43 +02004406 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004407 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004408 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004409 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004410 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004411
Kamal Dasu66507c72014-05-01 20:51:19 -04004412 if (part_pagewr)
4413 use_bufpoi = 1;
4414 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004415 use_bufpoi = !virt_addr_valid(buf) ||
4416 !IS_ALIGNED((unsigned long)buf,
4417 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004418 else
4419 use_bufpoi = 0;
4420
4421 /* Partial page write?, or need to use bounce buffer */
4422 if (use_bufpoi) {
4423 pr_debug("%s: using write bounce buffer for buf@%p\n",
4424 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004425 if (part_pagewr)
4426 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004427 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004428 memset(chip->data_buf, 0xff, mtd->writesize);
4429 memcpy(&chip->data_buf[column], buf, bytes);
4430 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004431 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004432
Maxim Levitsky782ce792010-02-22 20:39:36 +02004433 if (unlikely(oob)) {
4434 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004435 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004436 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004437 } else {
4438 /* We still need to erase leftover OOB data */
4439 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004440 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004441
4442 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004443 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004444 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004445 if (ret)
4446 break;
4447
4448 writelen -= bytes;
4449 if (!writelen)
4450 break;
4451
Thomas Gleixner29072b92006-09-28 15:38:36 +02004452 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004453 buf += bytes;
4454 realpage++;
4455
4456 page = realpage & chip->pagemask;
4457 /* Check, if we cross a chip boundary */
4458 if (!page) {
4459 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004460 chip->select_chip(chip, -1);
4461 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004462 }
4463 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004464
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004465 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004466 if (unlikely(oob))
4467 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004468
4469err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004470 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004471 return ret;
4472}
4473
4474/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004475 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004476 * @mtd: MTD device structure
4477 * @to: offset to write to
4478 * @len: number of bytes to write
4479 * @retlen: pointer to variable to store the number of written bytes
4480 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004481 *
4482 * NAND write with ECC. Used when performing writes in interrupt context, this
4483 * may for example be called by mtdoops when writing an oops while in panic.
4484 */
4485static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4486 size_t *retlen, const uint8_t *buf)
4487{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004488 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004489 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004490 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004491 int ret;
4492
Brian Norris8b6e50c2011-05-25 14:59:01 -07004493 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004494 panic_nand_get_device(chip, mtd, FL_WRITING);
4495
Boris Brezillon758b56f2018-09-06 14:05:24 +02004496 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004497
4498 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004499 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004500
Brian Norris0ec56dc2015-02-28 02:02:30 -08004501 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004502 ops.len = len;
4503 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004504 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004505
Brian Norris4a89ff82011-08-30 18:45:45 -07004506 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004507
Brian Norris4a89ff82011-08-30 18:45:45 -07004508 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004509 return ret;
4510}
4511
4512/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004513 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004514 * @mtd: MTD device structure
4515 * @to: offset to write to
4516 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004517 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004518 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004519 */
4520static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4521 struct mtd_oob_ops *ops)
4522{
Adrian Hunter03736152007-01-31 17:58:29 +02004523 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004524 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Brian Norris289c0522011-07-19 10:06:09 -07004526 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304527 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Boris BREZILLON29f10582016-03-07 10:46:52 +01004529 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004532 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004533 pr_debug("%s: attempt to write past end of page\n",
4534 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 return -EINVAL;
4536 }
4537
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004538 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004539
4540 /*
4541 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4542 * of my DiskOnChip 2000 test units) will clear the whole data page too
4543 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4544 * it in the doc2000 driver in August 1999. dwmw2.
4545 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004546 nand_reset(chip, chipnr);
4547
Boris Brezillon758b56f2018-09-06 14:05:24 +02004548 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004549
4550 /* Shift to get page */
4551 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
4553 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004554 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004555 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004556 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004557 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004560 if (page == chip->pagebuf)
4561 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004563 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004564
Brian Norris0612b9d2011-08-30 18:45:40 -07004565 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004566 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004567 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004568 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004569
Boris Brezillon758b56f2018-09-06 14:05:24 +02004570 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004571
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004572 if (status)
4573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Vitaly Wool70145682006-11-03 18:20:38 +03004575 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004577 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004580/**
4581 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004582 * @mtd: MTD device structure
4583 * @to: offset to write to
4584 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004585 */
4586static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4587 struct mtd_oob_ops *ops)
4588{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004589 int ret = -ENOTSUPP;
4590
4591 ops->retlen = 0;
4592
Huang Shijie6a8214a2012-11-19 14:43:30 +08004593 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004594
Florian Fainellif8ac0412010-09-07 13:23:43 +02004595 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004596 case MTD_OPS_PLACE_OOB:
4597 case MTD_OPS_AUTO_OOB:
4598 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004599 break;
4600
4601 default:
4602 goto out;
4603 }
4604
4605 if (!ops->datbuf)
4606 ret = nand_do_write_oob(mtd, to, ops);
4607 else
4608 ret = nand_do_write_ops(mtd, to, ops);
4609
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004610out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004611 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 return ret;
4613}
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615/**
Brian Norris49c50b92014-05-06 16:02:19 -07004616 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004617 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004618 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 *
Brian Norris49c50b92014-05-06 16:02:19 -07004620 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004622static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004624 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004627 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004628
Boris Brezillon97d90da2017-11-30 18:01:29 +01004629 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
4632/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004634 * @mtd: MTD device structure
4635 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004637 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004639static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004641 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004645 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004646 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004647 * @instr: erase instruction
4648 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004650 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004652int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004653 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004655 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004656 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004657 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Brian Norris289c0522011-07-19 10:06:09 -07004659 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4660 __func__, (unsigned long long)instr->addr,
4661 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304663 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004667 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004670 page = (int)(instr->addr >> chip->page_shift);
4671 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004674 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
4676 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004677 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 /* Check, if it is write protected */
4680 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004681 pr_debug("%s: device is write protected!\n",
4682 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004683 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 goto erase_exit;
4685 }
4686
4687 /* Loop through the pages */
4688 len = instr->len;
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004691 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004692 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304693 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004694 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4695 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004696 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 goto erase_exit;
4698 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004699
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004700 /*
4701 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004702 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004703 */
4704 if (page <= chip->pagebuf && chip->pagebuf <
4705 (page + pages_per_block))
4706 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
Boris Brezillona2098a92018-09-06 14:05:30 +02004708 status = chip->erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004711 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004712 pr_debug("%s: failed erase, page 0x%08x\n",
4713 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004714 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004715 instr->fail_addr =
4716 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 goto erase_exit;
4718 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004721 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 page += pages_per_block;
4723
4724 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004725 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004727 chip->select_chip(chip, -1);
4728 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
4730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004732 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004733erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004736 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 nand_release_device(mtd);
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 /* Return more or less happy */
4740 return ret;
4741}
4742
4743/**
4744 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004745 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004747 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004749static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
Brian Norris289c0522011-07-19 10:06:09 -07004751 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004754 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004756 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004760 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004761 * @mtd: MTD device structure
4762 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004764static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304766 struct nand_chip *chip = mtd_to_nand(mtd);
4767 int chipnr = (int)(offs >> chip->chip_shift);
4768 int ret;
4769
4770 /* Select the NAND device */
4771 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004772 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304773
4774 ret = nand_block_checkbad(mtd, offs, 0);
4775
Boris Brezillon758b56f2018-09-06 14:05:24 +02004776 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304777 nand_release_device(mtd);
4778
4779 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
4782/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004783 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004784 * @mtd: MTD device structure
4785 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004787static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 int ret;
4790
Florian Fainellif8ac0412010-09-07 13:23:43 +02004791 ret = nand_block_isbad(mtd, ofs);
4792 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004793 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 if (ret > 0)
4795 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004796 return ret;
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Brian Norris5a0edb22013-07-30 17:52:58 -07004799 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801
4802/**
Zach Brown56718422017-01-10 13:30:20 -06004803 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4804 * @mtd: MTD device structure
4805 * @ofs: offset relative to mtd start
4806 * @len: length of mtd
4807 */
4808static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4809{
4810 struct nand_chip *chip = mtd_to_nand(mtd);
4811 u32 part_start_block;
4812 u32 part_end_block;
4813 u32 part_start_die;
4814 u32 part_end_die;
4815
4816 /*
4817 * max_bb_per_die and blocks_per_die used to determine
4818 * the maximum bad block count.
4819 */
4820 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4821 return -ENOTSUPP;
4822
4823 /* Get the start and end of the partition in erase blocks. */
4824 part_start_block = mtd_div_by_eb(ofs, mtd);
4825 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4826
4827 /* Get the start and end LUNs of the partition. */
4828 part_start_die = part_start_block / chip->blocks_per_die;
4829 part_end_die = part_end_block / chip->blocks_per_die;
4830
4831 /*
4832 * Look up the bad blocks per unit and multiply by the number of units
4833 * that the partition spans.
4834 */
4835 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4836}
4837
4838/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004839 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004840 * @chip: nand chip info structure
4841 * @addr: feature address.
4842 * @subfeature_param: the subfeature parameters, a four bytes array.
4843 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004844static int nand_default_set_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004845 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004846{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004847 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004848}
4849
4850/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004851 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004852 * @chip: nand chip info structure
4853 * @addr: feature address.
4854 * @subfeature_param: the subfeature parameters, a four bytes array.
4855 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004856static int nand_default_get_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004857 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004858{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004859 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004860}
4861
4862/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004863 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004864 * @chip: nand chip info structure
4865 * @addr: feature address.
4866 * @subfeature_param: the subfeature parameters, a four bytes array.
4867 *
4868 * Should be used by NAND controller drivers that do not support the SET/GET
4869 * FEATURES operations.
4870 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004871int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
4872 u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004873{
4874 return -ENOTSUPP;
4875}
Miquel Raynalb9587582018-03-19 14:47:19 +01004876EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004877
4878/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004879 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004880 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004881 */
4882static int nand_suspend(struct mtd_info *mtd)
4883{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004884 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004885}
4886
4887/**
4888 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004889 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004890 */
4891static void nand_resume(struct mtd_info *mtd)
4892{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004893 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004894
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004895 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004896 nand_release_device(mtd);
4897 else
Brian Norrisd0370212011-07-19 10:06:08 -07004898 pr_err("%s called for a chip which is not in suspended state\n",
4899 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004900}
4901
Scott Branden72ea4032014-11-20 11:18:05 -08004902/**
4903 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4904 * prevent further operations
4905 * @mtd: MTD device structure
4906 */
4907static void nand_shutdown(struct mtd_info *mtd)
4908{
Brian Norris9ca641b2015-11-09 16:37:28 -08004909 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004910}
4911
Brian Norris8b6e50c2011-05-25 14:59:01 -07004912/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004913static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004914{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004915 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004918 if (!chip->chip_delay)
4919 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
4921 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004922 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004923 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004926 if (chip->waitfunc == NULL)
4927 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004929 if (!chip->select_chip)
4930 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004931
Huang Shijie4204ccc2013-08-16 10:10:07 +08004932 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004933 if (!chip->set_features)
4934 chip->set_features = nand_default_set_features;
4935 if (!chip->get_features)
4936 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004937
Brian Norris68e80782013-07-18 01:17:02 -07004938 /* If called twice, pointers that depend on busw may need to be reset */
4939 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004940 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004941 if (!chip->block_bad)
4942 chip->block_bad = nand_block_bad;
4943 if (!chip->block_markbad)
4944 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004945 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004946 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004947 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4948 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004949 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004950 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004951
4952 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004953 chip->controller = &chip->dummy_controller;
4954 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004955 }
4956
Masahiro Yamada477544c2017-03-30 17:15:05 +09004957 if (!chip->buf_align)
4958 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004959}
4960
Brian Norris8b6e50c2011-05-25 14:59:01 -07004961/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004962static void sanitize_string(uint8_t *s, size_t len)
4963{
4964 ssize_t i;
4965
Brian Norris8b6e50c2011-05-25 14:59:01 -07004966 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004967 s[len - 1] = 0;
4968
Brian Norris8b6e50c2011-05-25 14:59:01 -07004969 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004970 for (i = 0; i < len - 1; i++) {
4971 if (s[i] < ' ' || s[i] > 127)
4972 s[i] = '?';
4973 }
4974
Brian Norris8b6e50c2011-05-25 14:59:01 -07004975 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004976 strim(s);
4977}
4978
4979static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4980{
4981 int i;
4982 while (len--) {
4983 crc ^= *p++ << 8;
4984 for (i = 0; i < 8; i++)
4985 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4986 }
4987
4988 return crc;
4989}
4990
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004991/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004992static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4993 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004994{
4995 struct onfi_ext_param_page *ep;
4996 struct onfi_ext_section *s;
4997 struct onfi_ext_ecc_info *ecc;
4998 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004999 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005000 int len;
5001 int i;
5002
5003 len = le16_to_cpu(p->ext_param_page_length) * 16;
5004 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005005 if (!ep)
5006 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005007
5008 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005009 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5010 if (ret)
5011 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005012
5013 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005014 ret = nand_change_read_column_op(chip,
5015 sizeof(*p) * p->num_of_param_pages,
5016 ep, len, true);
5017 if (ret)
5018 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005019
Boris Brezillon97d90da2017-11-30 18:01:29 +01005020 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005021 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5022 != le16_to_cpu(ep->crc))) {
5023 pr_debug("fail in the CRC.\n");
5024 goto ext_out;
5025 }
5026
5027 /*
5028 * Check the signature.
5029 * Do not strictly follow the ONFI spec, maybe changed in future.
5030 */
5031 if (strncmp(ep->sig, "EPPS", 4)) {
5032 pr_debug("The signature is invalid.\n");
5033 goto ext_out;
5034 }
5035
5036 /* find the ECC section. */
5037 cursor = (uint8_t *)(ep + 1);
5038 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5039 s = ep->sections + i;
5040 if (s->type == ONFI_SECTION_TYPE_2)
5041 break;
5042 cursor += s->length * 16;
5043 }
5044 if (i == ONFI_EXT_SECTION_MAX) {
5045 pr_debug("We can not find the ECC section.\n");
5046 goto ext_out;
5047 }
5048
5049 /* get the info we want. */
5050 ecc = (struct onfi_ext_ecc_info *)cursor;
5051
Brian Norris4ae7d222013-09-16 18:20:21 -07005052 if (!ecc->codeword_size) {
5053 pr_debug("Invalid codeword size\n");
5054 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005055 }
5056
Brian Norris4ae7d222013-09-16 18:20:21 -07005057 chip->ecc_strength_ds = ecc->ecc_bits;
5058 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005059 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005060
5061ext_out:
5062 kfree(ep);
5063 return ret;
5064}
5065
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005066/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005067 * Recover data with bit-wise majority
5068 */
5069static void nand_bit_wise_majority(const void **srcbufs,
5070 unsigned int nsrcbufs,
5071 void *dstbuf,
5072 unsigned int bufsize)
5073{
5074 int i, j, k;
5075
5076 for (i = 0; i < bufsize; i++) {
5077 u8 val = 0;
5078
5079 for (j = 0; j < 8; j++) {
5080 unsigned int cnt = 0;
5081
5082 for (k = 0; k < nsrcbufs; k++) {
5083 const u8 *srcbuf = srcbufs[k];
5084
5085 if (srcbuf[i] & BIT(j))
5086 cnt++;
5087 }
5088
5089 if (cnt > nsrcbufs / 2)
5090 val |= BIT(j);
5091 }
5092
5093 ((u8 *)dstbuf)[i] = val;
5094 }
5095}
5096
5097/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005098 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005099 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005100static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005101{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005102 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005103 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005104 struct onfi_params *onfi;
5105 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005106 char id[4];
5107 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005108
Brian Norris7854d3f2011-06-23 14:12:08 -07005109 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005110 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5111 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005112 return 0;
5113
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005114 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005115 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005116 if (!p)
5117 return -ENOMEM;
5118
Boris Brezillon97d90da2017-11-30 18:01:29 +01005119 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005120 if (ret) {
5121 ret = 0;
5122 goto free_onfi_param_page;
5123 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005124
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005125 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005126 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005127 if (ret) {
5128 ret = 0;
5129 goto free_onfi_param_page;
5130 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005131
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005132 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005133 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005134 if (i)
5135 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005136 break;
5137 }
5138 }
5139
Brian Norrisc7f23a72013-08-13 10:51:55 -07005140 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005141 const void *srcbufs[3] = {p, p + 1, p + 2};
5142
5143 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5144 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5145 sizeof(*p));
5146
5147 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5148 le16_to_cpu(p->crc)) {
5149 pr_err("ONFI parameter recovery failed, aborting\n");
5150 goto free_onfi_param_page;
5151 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005152 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005153
Chris Packham00ce4e02018-06-25 10:44:44 +12005154 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5155 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5156 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5157
Brian Norris8b6e50c2011-05-25 14:59:01 -07005158 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005159 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005160 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005161 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005162 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005163 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005164 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005165 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005166 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005167 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005168 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005169 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005170
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005171 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005172 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005173 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005174 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005175
5176 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5177 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005178 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5179 if (!chip->parameters.model) {
5180 ret = -ENOMEM;
5181 goto free_onfi_param_page;
5182 }
Brian Norris4355b702013-08-27 18:45:10 -07005183
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005184 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005185
5186 /*
5187 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5188 * (don't ask me who thought of this...). MTD assumes that these
5189 * dimensions will be power-of-2, so just truncate the remaining area.
5190 */
5191 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5192 mtd->erasesize *= mtd->writesize;
5193
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005194 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005195
5196 /* See erasesize comment */
5197 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005198 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005199 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005200
Zach Brown34da5f52017-01-10 13:30:21 -06005201 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5202 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5203
Miquel Raynala97421c2018-03-19 14:47:27 +01005204 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005205 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005206
Huang Shijie10c86ba2013-05-17 11:17:26 +08005207 if (p->ecc_bits != 0xff) {
5208 chip->ecc_strength_ds = p->ecc_bits;
5209 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005210 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005211 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005212
5213 /*
5214 * The nand_flash_detect_ext_param_page() uses the
5215 * Change Read Column command which maybe not supported
5216 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5217 * now. We do not replace user supplied command function.
5218 */
5219 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5220 chip->cmdfunc = nand_command_lp;
5221
5222 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005223 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005224 pr_warn("Failed to detect ONFI extended param page\n");
5225 } else {
5226 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005227 }
5228
Miquel Raynalf4531b22018-03-19 14:47:26 +01005229 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005230 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005231 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005232 bitmap_set(chip->parameters.get_feature_list,
5233 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5234 bitmap_set(chip->parameters.set_feature_list,
5235 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5236 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005237
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005238 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5239 if (!onfi) {
5240 ret = -ENOMEM;
5241 goto free_model;
5242 }
5243
5244 onfi->version = onfi_version;
5245 onfi->tPROG = le16_to_cpu(p->t_prog);
5246 onfi->tBERS = le16_to_cpu(p->t_bers);
5247 onfi->tR = le16_to_cpu(p->t_r);
5248 onfi->tCCS = le16_to_cpu(p->t_ccs);
5249 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5250 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5251 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5252 chip->parameters.onfi = onfi;
5253
5254 /* Identification done, free the full ONFI parameter page and exit */
5255 kfree(p);
5256
5257 return 1;
5258
5259free_model:
5260 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005261free_onfi_param_page:
5262 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005263
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005264 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005265}
5266
5267/*
Huang Shijie91361812014-02-21 13:39:40 +08005268 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5269 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005270static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005271{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005272 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005273 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005274 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005275 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005276 char id[5];
5277 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005278
5279 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005280 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5281 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005282 return 0;
5283
Miquel Raynal480139d2018-03-19 14:47:30 +01005284 /* JEDEC chip: allocate a buffer to hold its parameter page */
5285 p = kzalloc(sizeof(*p), GFP_KERNEL);
5286 if (!p)
5287 return -ENOMEM;
5288
Boris Brezillon97d90da2017-11-30 18:01:29 +01005289 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005290 if (ret) {
5291 ret = 0;
5292 goto free_jedec_param_page;
5293 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005294
Huang Shijie91361812014-02-21 13:39:40 +08005295 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005296 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005297 if (ret) {
5298 ret = 0;
5299 goto free_jedec_param_page;
5300 }
Huang Shijie91361812014-02-21 13:39:40 +08005301
5302 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5303 le16_to_cpu(p->crc))
5304 break;
5305 }
5306
5307 if (i == 3) {
5308 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005309 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005310 }
5311
5312 /* Check version */
5313 val = le16_to_cpu(p->revision);
5314 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005315 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005316 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005317 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005318
Miquel Raynal480139d2018-03-19 14:47:30 +01005319 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005320 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005321 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005322 }
5323
5324 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5325 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005326 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5327 if (!chip->parameters.model) {
5328 ret = -ENOMEM;
5329 goto free_jedec_param_page;
5330 }
Huang Shijie91361812014-02-21 13:39:40 +08005331
5332 mtd->writesize = le32_to_cpu(p->byte_per_page);
5333
5334 /* Please reference to the comment for nand_flash_detect_onfi. */
5335 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5336 mtd->erasesize *= mtd->writesize;
5337
5338 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5339
5340 /* Please reference to the comment for nand_flash_detect_onfi. */
5341 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5342 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5343 chip->bits_per_cell = p->bits_per_cell;
5344
Miquel Raynal480139d2018-03-19 14:47:30 +01005345 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005346 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005347
5348 /* ECC info */
5349 ecc = &p->ecc_info[0];
5350
5351 if (ecc->codeword_size >= 9) {
5352 chip->ecc_strength_ds = ecc->ecc_bits;
5353 chip->ecc_step_ds = 1 << ecc->codeword_size;
5354 } else {
5355 pr_warn("Invalid codeword size\n");
5356 }
5357
Miquel Raynal480139d2018-03-19 14:47:30 +01005358free_jedec_param_page:
5359 kfree(p);
5360 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005361}
5362
5363/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005364 * nand_id_has_period - Check if an ID string has a given wraparound period
5365 * @id_data: the ID string
5366 * @arrlen: the length of the @id_data array
5367 * @period: the period of repitition
5368 *
5369 * Check if an ID string is repeated within a given sequence of bytes at
5370 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005371 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005372 * if the repetition has a period of @period; otherwise, returns zero.
5373 */
5374static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5375{
5376 int i, j;
5377 for (i = 0; i < period; i++)
5378 for (j = i + period; j < arrlen; j += period)
5379 if (id_data[i] != id_data[j])
5380 return 0;
5381 return 1;
5382}
5383
5384/*
5385 * nand_id_len - Get the length of an ID string returned by CMD_READID
5386 * @id_data: the ID string
5387 * @arrlen: the length of the @id_data array
5388
5389 * Returns the length of the ID string, according to known wraparound/trailing
5390 * zero patterns. If no pattern exists, returns the length of the array.
5391 */
5392static int nand_id_len(u8 *id_data, int arrlen)
5393{
5394 int last_nonzero, period;
5395
5396 /* Find last non-zero byte */
5397 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5398 if (id_data[last_nonzero])
5399 break;
5400
5401 /* All zeros */
5402 if (last_nonzero < 0)
5403 return 0;
5404
5405 /* Calculate wraparound period */
5406 for (period = 1; period < arrlen; period++)
5407 if (nand_id_has_period(id_data, arrlen, period))
5408 break;
5409
5410 /* There's a repeated pattern */
5411 if (period < arrlen)
5412 return period;
5413
5414 /* There are trailing zeros */
5415 if (last_nonzero < arrlen - 1)
5416 return last_nonzero + 1;
5417
5418 /* No pattern detected */
5419 return arrlen;
5420}
5421
Huang Shijie7db906b2013-09-25 14:58:11 +08005422/* Extract the bits of per cell from the 3rd byte of the extended ID */
5423static int nand_get_bits_per_cell(u8 cellinfo)
5424{
5425 int bits;
5426
5427 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5428 bits >>= NAND_CI_CELLTYPE_SHIFT;
5429 return bits + 1;
5430}
5431
Brian Norrise3b88bd2012-09-24 20:40:52 -07005432/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005433 * Many new NAND share similar device ID codes, which represent the size of the
5434 * chip. The rest of the parameters must be decoded according to generic or
5435 * manufacturer-specific "extended ID" decoding patterns.
5436 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005437void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005438{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005439 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005440 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005441 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005442 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005443 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005444 /* The 4th id byte is the important one */
5445 extid = id_data[3];
5446
Boris Brezillon01389b62016-06-08 10:30:18 +02005447 /* Calc pagesize */
5448 mtd->writesize = 1024 << (extid & 0x03);
5449 extid >>= 2;
5450 /* Calc oobsize */
5451 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5452 extid >>= 2;
5453 /* Calc blocksize. Blocksize is multiples of 64KiB */
5454 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5455 extid >>= 2;
5456 /* Get buswidth information */
5457 if (extid & 0x1)
5458 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005459}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005460EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005461
5462/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005463 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5464 * decodes a matching ID table entry and assigns the MTD size parameters for
5465 * the chip.
5466 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005467static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005468{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005469 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005470
5471 mtd->erasesize = type->erasesize;
5472 mtd->writesize = type->pagesize;
5473 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005474
Huang Shijie1c195e92013-09-25 14:58:12 +08005475 /* All legacy ID NAND are small-page, SLC */
5476 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005477}
5478
5479/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005480 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5481 * heuristic patterns using various detected parameters (e.g., manufacturer,
5482 * page size, cell-type information).
5483 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005484static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005485{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005486 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005487
5488 /* Set the bad block position */
5489 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5490 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5491 else
5492 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005493}
5494
Huang Shijieec6e87e2013-03-15 11:01:00 +08005495static inline bool is_full_id_nand(struct nand_flash_dev *type)
5496{
5497 return type->id_len;
5498}
5499
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005500static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005501 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005502{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005503 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005504 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005505
Huang Shijieec6e87e2013-03-15 11:01:00 +08005506 if (!strncmp(type->id, id_data, type->id_len)) {
5507 mtd->writesize = type->pagesize;
5508 mtd->erasesize = type->erasesize;
5509 mtd->oobsize = type->oobsize;
5510
Huang Shijie7db906b2013-09-25 14:58:11 +08005511 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005512 chip->chipsize = (uint64_t)type->chipsize << 20;
5513 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005514 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5515 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005516 chip->onfi_timing_mode_default =
5517 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005518
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005519 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5520 if (!chip->parameters.model)
5521 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005522
Huang Shijieec6e87e2013-03-15 11:01:00 +08005523 return true;
5524 }
5525 return false;
5526}
5527
Brian Norris7e74c2d2012-09-24 20:40:49 -07005528/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005529 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5530 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5531 * table.
5532 */
5533static void nand_manufacturer_detect(struct nand_chip *chip)
5534{
5535 /*
5536 * Try manufacturer detection if available and use
5537 * nand_decode_ext_id() otherwise.
5538 */
5539 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005540 chip->manufacturer.desc->ops->detect) {
5541 /* The 3rd id byte holds MLC / multichip data */
5542 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005543 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005544 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005545 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005546 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005547}
5548
5549/*
5550 * Manufacturer initialization. This function is called for all NANDs including
5551 * ONFI and JEDEC compliant ones.
5552 * Manufacturer drivers should put all their specific initialization code in
5553 * their ->init() hook.
5554 */
5555static int nand_manufacturer_init(struct nand_chip *chip)
5556{
5557 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5558 !chip->manufacturer.desc->ops->init)
5559 return 0;
5560
5561 return chip->manufacturer.desc->ops->init(chip);
5562}
5563
5564/*
5565 * Manufacturer cleanup. This function is called for all NANDs including
5566 * ONFI and JEDEC compliant ones.
5567 * Manufacturer drivers should put all their specific cleanup code in their
5568 * ->cleanup() hook.
5569 */
5570static void nand_manufacturer_cleanup(struct nand_chip *chip)
5571{
5572 /* Release manufacturer private data */
5573 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5574 chip->manufacturer.desc->ops->cleanup)
5575 chip->manufacturer.desc->ops->cleanup(chip);
5576}
5577
5578/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005579 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005580 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005581static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005582{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005583 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005584 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005585 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005586 u8 *id_data = chip->id.data;
5587 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Karl Beldanef89a882008-09-15 14:37:29 +02005589 /*
5590 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005591 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005592 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005593 ret = nand_reset(chip, 0);
5594 if (ret)
5595 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005596
5597 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005598 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005601 ret = nand_readid_op(chip, 0, id_data, 2);
5602 if (ret)
5603 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005606 maf_id = id_data[0];
5607 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Brian Norris8b6e50c2011-05-25 14:59:01 -07005609 /*
5610 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005611 * interface concerns can cause random data which looks like a
5612 * possibly credible NAND flash to appear. If the two results do
5613 * not match, ignore the device completely.
5614 */
5615
Brian Norris4aef9b72012-09-24 20:40:48 -07005616 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005617 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5618 if (ret)
5619 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005620
Boris Brezillon7f501f02016-05-24 19:20:05 +02005621 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005622 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005623 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005624 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005625 }
5626
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005627 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005628
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005629 /* Try to identify manufacturer */
5630 manufacturer = nand_get_manufacturer(maf_id);
5631 chip->manufacturer.desc = manufacturer;
5632
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005633 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005634 type = nand_flash_ids;
5635
Boris Brezillon29a198a2016-05-24 20:17:48 +02005636 /*
5637 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5638 * override it.
5639 * This is required to make sure initial NAND bus width set by the
5640 * NAND controller driver is coherent with the real NAND bus width
5641 * (extracted by auto-detection code).
5642 */
5643 busw = chip->options & NAND_BUSWIDTH_16;
5644
5645 /*
5646 * The flag is only set (never cleared), reset it to its default value
5647 * before starting auto-detection.
5648 */
5649 chip->options &= ~NAND_BUSWIDTH_16;
5650
Huang Shijieec6e87e2013-03-15 11:01:00 +08005651 for (; type->name != NULL; type++) {
5652 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005653 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005654 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005655 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005656 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005657 }
5658 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005659
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005660 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005661 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005662 ret = nand_flash_detect_onfi(chip);
5663 if (ret < 0)
5664 return ret;
5665 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005666 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005667
5668 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005669 ret = nand_flash_detect_jedec(chip);
5670 if (ret < 0)
5671 return ret;
5672 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005673 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005674 }
5675
David Woodhouse5e81e882010-02-26 18:32:56 +00005676 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005677 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005678
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005679 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5680 if (!chip->parameters.model)
5681 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005682
Adrian Hunter69423d92008-12-10 13:37:21 +00005683 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005684
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005685 if (!type->pagesize)
5686 nand_manufacturer_detect(chip);
5687 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005688 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005689
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005690 /* Get chip options */
5691 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005692
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005693ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005694 if (!mtd->name)
5695 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005696
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005697 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005698 WARN_ON(busw & NAND_BUSWIDTH_16);
5699 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005700 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5701 /*
5702 * Check, if buswidth is correct. Hardware drivers should set
5703 * chip correct!
5704 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005705 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005706 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005707 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5708 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005709 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5710 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005711 ret = -EINVAL;
5712
5713 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005714 }
5715
Boris Brezillon7f501f02016-05-24 19:20:05 +02005716 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005717
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005718 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005719 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005720 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005721 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005722
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005723 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005724 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005725 if (chip->chipsize & 0xffffffff)
5726 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005727 else {
5728 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5729 chip->chip_shift += 32 - 1;
5730 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005731
Masahiro Yamada14157f82017-09-13 11:05:50 +09005732 if (chip->chip_shift - chip->page_shift > 16)
5733 chip->options |= NAND_ROW_ADDR_3;
5734
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005735 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005736 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005737
Brian Norris8b6e50c2011-05-25 14:59:01 -07005738 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005739 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5740 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005741
Ezequiel Garcia20171642013-11-25 08:30:31 -03005742 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005743 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005744 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5745 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005746 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005747 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005748 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005749 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005750
5751free_detect_allocation:
5752 kfree(chip->parameters.model);
5753
5754 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005755}
5756
Boris Brezillond48f62b2016-04-01 14:54:32 +02005757static const char * const nand_ecc_modes[] = {
5758 [NAND_ECC_NONE] = "none",
5759 [NAND_ECC_SOFT] = "soft",
5760 [NAND_ECC_HW] = "hw",
5761 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5762 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005763 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005764};
5765
5766static int of_get_nand_ecc_mode(struct device_node *np)
5767{
5768 const char *pm;
5769 int err, i;
5770
5771 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5772 if (err < 0)
5773 return err;
5774
5775 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5776 if (!strcasecmp(pm, nand_ecc_modes[i]))
5777 return i;
5778
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005779 /*
5780 * For backward compatibility we support few obsoleted values that don't
5781 * have their mappings into nand_ecc_modes_t anymore (they were merged
5782 * with other enums).
5783 */
5784 if (!strcasecmp(pm, "soft_bch"))
5785 return NAND_ECC_SOFT;
5786
Boris Brezillond48f62b2016-04-01 14:54:32 +02005787 return -ENODEV;
5788}
5789
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005790static const char * const nand_ecc_algos[] = {
5791 [NAND_ECC_HAMMING] = "hamming",
5792 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005793 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005794};
5795
Boris Brezillond48f62b2016-04-01 14:54:32 +02005796static int of_get_nand_ecc_algo(struct device_node *np)
5797{
5798 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005799 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005800
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005801 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5802 if (!err) {
5803 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5804 if (!strcasecmp(pm, nand_ecc_algos[i]))
5805 return i;
5806 return -ENODEV;
5807 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005808
5809 /*
5810 * For backward compatibility we also read "nand-ecc-mode" checking
5811 * for some obsoleted values that were specifying ECC algorithm.
5812 */
5813 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5814 if (err < 0)
5815 return err;
5816
5817 if (!strcasecmp(pm, "soft"))
5818 return NAND_ECC_HAMMING;
5819 else if (!strcasecmp(pm, "soft_bch"))
5820 return NAND_ECC_BCH;
5821
5822 return -ENODEV;
5823}
5824
5825static int of_get_nand_ecc_step_size(struct device_node *np)
5826{
5827 int ret;
5828 u32 val;
5829
5830 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5831 return ret ? ret : val;
5832}
5833
5834static int of_get_nand_ecc_strength(struct device_node *np)
5835{
5836 int ret;
5837 u32 val;
5838
5839 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5840 return ret ? ret : val;
5841}
5842
5843static int of_get_nand_bus_width(struct device_node *np)
5844{
5845 u32 val;
5846
5847 if (of_property_read_u32(np, "nand-bus-width", &val))
5848 return 8;
5849
5850 switch (val) {
5851 case 8:
5852 case 16:
5853 return val;
5854 default:
5855 return -EIO;
5856 }
5857}
5858
5859static bool of_get_nand_on_flash_bbt(struct device_node *np)
5860{
5861 return of_property_read_bool(np, "nand-on-flash-bbt");
5862}
5863
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005864static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005865{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005866 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005867 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005868
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005869 if (!dn)
5870 return 0;
5871
Brian Norris5844fee2015-01-23 00:22:27 -08005872 if (of_get_nand_bus_width(dn) == 16)
5873 chip->options |= NAND_BUSWIDTH_16;
5874
Stefan Agnerf922bd72018-06-24 23:27:23 +02005875 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5876 chip->options |= NAND_IS_BOOT_MEDIUM;
5877
Brian Norris5844fee2015-01-23 00:22:27 -08005878 if (of_get_nand_on_flash_bbt(dn))
5879 chip->bbt_options |= NAND_BBT_USE_FLASH;
5880
5881 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005882 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005883 ecc_strength = of_get_nand_ecc_strength(dn);
5884 ecc_step = of_get_nand_ecc_step_size(dn);
5885
Brian Norris5844fee2015-01-23 00:22:27 -08005886 if (ecc_mode >= 0)
5887 chip->ecc.mode = ecc_mode;
5888
Rafał Miłecki79082452016-03-23 11:19:02 +01005889 if (ecc_algo >= 0)
5890 chip->ecc.algo = ecc_algo;
5891
Brian Norris5844fee2015-01-23 00:22:27 -08005892 if (ecc_strength >= 0)
5893 chip->ecc.strength = ecc_strength;
5894
5895 if (ecc_step > 0)
5896 chip->ecc.size = ecc_step;
5897
Boris Brezillonba78ee02016-06-08 17:04:22 +02005898 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5899 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5900
Brian Norris5844fee2015-01-23 00:22:27 -08005901 return 0;
5902}
5903
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005904/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005905 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005906 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005907 * @maxchips: number of chips to scan for
5908 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005909 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005910 * This is the first phase of the normal nand_scan() function. It reads the
5911 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005912 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005913 * This helper used to be called directly from controller drivers that needed
5914 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5915 * prevented dynamic allocations during this phase which was unconvenient and
5916 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005917 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005918static int nand_scan_ident(struct nand_chip *chip, int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005919 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005920{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005921 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08005922 int i, nand_maf_id, nand_dev_id;
Brian Norris5844fee2015-01-23 00:22:27 -08005923 int ret;
5924
Miquel Raynal17fa8042017-11-30 18:01:31 +01005925 /* Enforce the right timings for reset/detection */
5926 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5927
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005928 ret = nand_dt_init(chip);
5929 if (ret)
5930 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005931
Brian Norrisf7a8e382016-01-05 10:39:45 -08005932 if (!mtd->name && mtd->dev.parent)
5933 mtd->name = dev_name(mtd->dev.parent);
5934
Miquel Raynal8878b122017-11-09 14:16:45 +01005935 /*
5936 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5937 * populated.
5938 */
5939 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005940 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005941 * Default functions assigned for ->cmdfunc() and
5942 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005943 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005944 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5945 pr_err("->cmd_ctrl() should be provided\n");
5946 return -EINVAL;
5947 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005948 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005949
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005950 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005951 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005952
5953 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005954 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005955 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005956 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005957 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005958 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005959 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 }
5961
Boris Brezillon7f501f02016-05-24 19:20:05 +02005962 nand_maf_id = chip->id.data[0];
5963 nand_dev_id = chip->id.data[1];
5964
Boris Brezillon758b56f2018-09-06 14:05:24 +02005965 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005966
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005967 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005968 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005969 u8 id[2];
5970
Karl Beldanef89a882008-09-15 14:37:29 +02005971 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005972 nand_reset(chip, i);
5973
Boris Brezillon758b56f2018-09-06 14:05:24 +02005974 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005976 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005978 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005979 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 break;
Huang Shijie07300162012-11-09 16:23:45 +08005981 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005982 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 }
5984 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005985 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005986
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005988 chip->numchips = i;
5989 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
David Woodhouse3b85c322006-09-25 17:06:53 +01005991 return 0;
5992}
5993
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005994static void nand_scan_ident_cleanup(struct nand_chip *chip)
5995{
5996 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005997 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005998}
5999
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006000static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6001{
6002 struct nand_chip *chip = mtd_to_nand(mtd);
6003 struct nand_ecc_ctrl *ecc = &chip->ecc;
6004
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006005 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006006 return -EINVAL;
6007
6008 switch (ecc->algo) {
6009 case NAND_ECC_HAMMING:
6010 ecc->calculate = nand_calculate_ecc;
6011 ecc->correct = nand_correct_data;
6012 ecc->read_page = nand_read_page_swecc;
6013 ecc->read_subpage = nand_read_subpage;
6014 ecc->write_page = nand_write_page_swecc;
6015 ecc->read_page_raw = nand_read_page_raw;
6016 ecc->write_page_raw = nand_write_page_raw;
6017 ecc->read_oob = nand_read_oob_std;
6018 ecc->write_oob = nand_write_oob_std;
6019 if (!ecc->size)
6020 ecc->size = 256;
6021 ecc->bytes = 3;
6022 ecc->strength = 1;
6023 return 0;
6024 case NAND_ECC_BCH:
6025 if (!mtd_nand_has_bch()) {
6026 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6027 return -EINVAL;
6028 }
6029 ecc->calculate = nand_bch_calculate_ecc;
6030 ecc->correct = nand_bch_correct_data;
6031 ecc->read_page = nand_read_page_swecc;
6032 ecc->read_subpage = nand_read_subpage;
6033 ecc->write_page = nand_write_page_swecc;
6034 ecc->read_page_raw = nand_read_page_raw;
6035 ecc->write_page_raw = nand_write_page_raw;
6036 ecc->read_oob = nand_read_oob_std;
6037 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006038
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006039 /*
6040 * Board driver should supply ecc.size and ecc.strength
6041 * values to select how many bits are correctable.
6042 * Otherwise, default to 4 bits for large page devices.
6043 */
6044 if (!ecc->size && (mtd->oobsize >= 64)) {
6045 ecc->size = 512;
6046 ecc->strength = 4;
6047 }
6048
6049 /*
6050 * if no ecc placement scheme was provided pickup the default
6051 * large page one.
6052 */
6053 if (!mtd->ooblayout) {
6054 /* handle large page devices only */
6055 if (mtd->oobsize < 64) {
6056 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6057 return -EINVAL;
6058 }
6059
6060 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006061
6062 }
6063
6064 /*
6065 * We can only maximize ECC config when the default layout is
6066 * used, otherwise we don't know how many bytes can really be
6067 * used.
6068 */
6069 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6070 ecc->options & NAND_ECC_MAXIMIZE) {
6071 int steps, bytes;
6072
6073 /* Always prefer 1k blocks over 512bytes ones */
6074 ecc->size = 1024;
6075 steps = mtd->writesize / ecc->size;
6076
6077 /* Reserve 2 bytes for the BBM */
6078 bytes = (mtd->oobsize - 2) / steps;
6079 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006080 }
6081
6082 /* See nand_bch_init() for details. */
6083 ecc->bytes = 0;
6084 ecc->priv = nand_bch_init(mtd);
6085 if (!ecc->priv) {
6086 WARN(1, "BCH ECC initialization failed!\n");
6087 return -EINVAL;
6088 }
6089 return 0;
6090 default:
6091 WARN(1, "Unsupported ECC algorithm!\n");
6092 return -EINVAL;
6093 }
6094}
6095
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006096/**
6097 * nand_check_ecc_caps - check the sanity of preset ECC settings
6098 * @chip: nand chip info structure
6099 * @caps: ECC caps info structure
6100 * @oobavail: OOB size that the ECC engine can use
6101 *
6102 * When ECC step size and strength are already set, check if they are supported
6103 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6104 * On success, the calculated ECC bytes is set.
6105 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306106static int
6107nand_check_ecc_caps(struct nand_chip *chip,
6108 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006109{
6110 struct mtd_info *mtd = nand_to_mtd(chip);
6111 const struct nand_ecc_step_info *stepinfo;
6112 int preset_step = chip->ecc.size;
6113 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306114 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006115 int i, j;
6116
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006117 for (i = 0; i < caps->nstepinfos; i++) {
6118 stepinfo = &caps->stepinfos[i];
6119
6120 if (stepinfo->stepsize != preset_step)
6121 continue;
6122
6123 for (j = 0; j < stepinfo->nstrengths; j++) {
6124 if (stepinfo->strengths[j] != preset_strength)
6125 continue;
6126
6127 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6128 preset_strength);
6129 if (WARN_ON_ONCE(ecc_bytes < 0))
6130 return ecc_bytes;
6131
6132 if (ecc_bytes * nsteps > oobavail) {
6133 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6134 preset_step, preset_strength);
6135 return -ENOSPC;
6136 }
6137
6138 chip->ecc.bytes = ecc_bytes;
6139
6140 return 0;
6141 }
6142 }
6143
6144 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6145 preset_step, preset_strength);
6146
6147 return -ENOTSUPP;
6148}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006149
6150/**
6151 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6152 * @chip: nand chip info structure
6153 * @caps: ECC engine caps info structure
6154 * @oobavail: OOB size that the ECC engine can use
6155 *
6156 * If a chip's ECC requirement is provided, try to meet it with the least
6157 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6158 * On success, the chosen ECC settings are set.
6159 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306160static int
6161nand_match_ecc_req(struct nand_chip *chip,
6162 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006163{
6164 struct mtd_info *mtd = nand_to_mtd(chip);
6165 const struct nand_ecc_step_info *stepinfo;
6166 int req_step = chip->ecc_step_ds;
6167 int req_strength = chip->ecc_strength_ds;
6168 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6169 int best_step, best_strength, best_ecc_bytes;
6170 int best_ecc_bytes_total = INT_MAX;
6171 int i, j;
6172
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006173 /* No information provided by the NAND chip */
6174 if (!req_step || !req_strength)
6175 return -ENOTSUPP;
6176
6177 /* number of correctable bits the chip requires in a page */
6178 req_corr = mtd->writesize / req_step * req_strength;
6179
6180 for (i = 0; i < caps->nstepinfos; i++) {
6181 stepinfo = &caps->stepinfos[i];
6182 step_size = stepinfo->stepsize;
6183
6184 for (j = 0; j < stepinfo->nstrengths; j++) {
6185 strength = stepinfo->strengths[j];
6186
6187 /*
6188 * If both step size and strength are smaller than the
6189 * chip's requirement, it is not easy to compare the
6190 * resulted reliability.
6191 */
6192 if (step_size < req_step && strength < req_strength)
6193 continue;
6194
6195 if (mtd->writesize % step_size)
6196 continue;
6197
6198 nsteps = mtd->writesize / step_size;
6199
6200 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6201 if (WARN_ON_ONCE(ecc_bytes < 0))
6202 continue;
6203 ecc_bytes_total = ecc_bytes * nsteps;
6204
6205 if (ecc_bytes_total > oobavail ||
6206 strength * nsteps < req_corr)
6207 continue;
6208
6209 /*
6210 * We assume the best is to meet the chip's requrement
6211 * with the least number of ECC bytes.
6212 */
6213 if (ecc_bytes_total < best_ecc_bytes_total) {
6214 best_ecc_bytes_total = ecc_bytes_total;
6215 best_step = step_size;
6216 best_strength = strength;
6217 best_ecc_bytes = ecc_bytes;
6218 }
6219 }
6220 }
6221
6222 if (best_ecc_bytes_total == INT_MAX)
6223 return -ENOTSUPP;
6224
6225 chip->ecc.size = best_step;
6226 chip->ecc.strength = best_strength;
6227 chip->ecc.bytes = best_ecc_bytes;
6228
6229 return 0;
6230}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006231
6232/**
6233 * nand_maximize_ecc - choose the max ECC strength available
6234 * @chip: nand chip info structure
6235 * @caps: ECC engine caps info structure
6236 * @oobavail: OOB size that the ECC engine can use
6237 *
6238 * Choose the max ECC strength that is supported on the controller, and can fit
6239 * within the chip's OOB. On success, the chosen ECC settings are set.
6240 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306241static int
6242nand_maximize_ecc(struct nand_chip *chip,
6243 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006244{
6245 struct mtd_info *mtd = nand_to_mtd(chip);
6246 const struct nand_ecc_step_info *stepinfo;
6247 int step_size, strength, nsteps, ecc_bytes, corr;
6248 int best_corr = 0;
6249 int best_step = 0;
6250 int best_strength, best_ecc_bytes;
6251 int i, j;
6252
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006253 for (i = 0; i < caps->nstepinfos; i++) {
6254 stepinfo = &caps->stepinfos[i];
6255 step_size = stepinfo->stepsize;
6256
6257 /* If chip->ecc.size is already set, respect it */
6258 if (chip->ecc.size && step_size != chip->ecc.size)
6259 continue;
6260
6261 for (j = 0; j < stepinfo->nstrengths; j++) {
6262 strength = stepinfo->strengths[j];
6263
6264 if (mtd->writesize % step_size)
6265 continue;
6266
6267 nsteps = mtd->writesize / step_size;
6268
6269 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6270 if (WARN_ON_ONCE(ecc_bytes < 0))
6271 continue;
6272
6273 if (ecc_bytes * nsteps > oobavail)
6274 continue;
6275
6276 corr = strength * nsteps;
6277
6278 /*
6279 * If the number of correctable bits is the same,
6280 * bigger step_size has more reliability.
6281 */
6282 if (corr > best_corr ||
6283 (corr == best_corr && step_size > best_step)) {
6284 best_corr = corr;
6285 best_step = step_size;
6286 best_strength = strength;
6287 best_ecc_bytes = ecc_bytes;
6288 }
6289 }
6290 }
6291
6292 if (!best_corr)
6293 return -ENOTSUPP;
6294
6295 chip->ecc.size = best_step;
6296 chip->ecc.strength = best_strength;
6297 chip->ecc.bytes = best_ecc_bytes;
6298
6299 return 0;
6300}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006301
Abhishek Sahu181ace92018-06-20 12:57:28 +05306302/**
6303 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6304 * @chip: nand chip info structure
6305 * @caps: ECC engine caps info structure
6306 * @oobavail: OOB size that the ECC engine can use
6307 *
6308 * Choose the ECC configuration according to following logic
6309 *
6310 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6311 * then check if it is supported by this controller.
6312 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6313 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6314 * to the chip's requirement. If available OOB size can't fit the chip
6315 * requirement then fallback to the maximum ECC step size and ECC strength.
6316 *
6317 * On success, the chosen ECC settings are set.
6318 */
6319int nand_ecc_choose_conf(struct nand_chip *chip,
6320 const struct nand_ecc_caps *caps, int oobavail)
6321{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306322 struct mtd_info *mtd = nand_to_mtd(chip);
6323
6324 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6325 return -EINVAL;
6326
Abhishek Sahu181ace92018-06-20 12:57:28 +05306327 if (chip->ecc.size && chip->ecc.strength)
6328 return nand_check_ecc_caps(chip, caps, oobavail);
6329
6330 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6331 return nand_maximize_ecc(chip, caps, oobavail);
6332
6333 if (!nand_match_ecc_req(chip, caps, oobavail))
6334 return 0;
6335
6336 return nand_maximize_ecc(chip, caps, oobavail);
6337}
6338EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6339
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006340/*
6341 * Check if the chip configuration meet the datasheet requirements.
6342
6343 * If our configuration corrects A bits per B bytes and the minimum
6344 * required correction level is X bits per Y bytes, then we must ensure
6345 * both of the following are true:
6346 *
6347 * (1) A / B >= X / Y
6348 * (2) A >= X
6349 *
6350 * Requirement (1) ensures we can correct for the required bitflip density.
6351 * Requirement (2) ensures we can correct even when all bitflips are clumped
6352 * in the same sector.
6353 */
6354static bool nand_ecc_strength_good(struct mtd_info *mtd)
6355{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006356 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006357 struct nand_ecc_ctrl *ecc = &chip->ecc;
6358 int corr, ds_corr;
6359
6360 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6361 /* Not enough information */
6362 return true;
6363
6364 /*
6365 * We get the number of corrected bits per page to compare
6366 * the correction density.
6367 */
6368 corr = (mtd->writesize * ecc->strength) / ecc->size;
6369 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6370
6371 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6372}
David Woodhouse3b85c322006-09-25 17:06:53 +01006373
6374/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006375 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006376 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006377 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006378 * This is the second phase of the normal nand_scan() function. It fills out
6379 * all the uninitialized function pointers with the defaults and scans for a
6380 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006381 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006382static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006383{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006384 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006385 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006386 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006387
Brian Norrise2414f42012-02-06 13:44:00 -08006388 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006389 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006390 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006391 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006392 }
Brian Norrise2414f42012-02-06 13:44:00 -08006393
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006394 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006395 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006396 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006397
Boris Brezillonf84674b2017-06-02 12:18:24 +02006398 /*
6399 * FIXME: some NAND manufacturer drivers expect the first die to be
6400 * selected when manufacturer->init() is called. They should be fixed
6401 * to explictly select the relevant die when interacting with the NAND
6402 * chip.
6403 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02006404 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006405 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02006406 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006407 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006408 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006409
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006410 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006411 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006412
6413 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006414 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006415 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006416 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006417 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006418 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006421 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 break;
6423 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006424 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006425 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006428 /*
6429 * Expose the whole OOB area to users if ECC_NONE
6430 * is passed. We could do that for all kind of
6431 * ->oobsize, but we must keep the old large/small
6432 * page with ECC layout when ->oobsize <= 128 for
6433 * compatibility reasons.
6434 */
6435 if (ecc->mode == NAND_ECC_NONE) {
6436 mtd_set_ooblayout(mtd,
6437 &nand_ooblayout_lp_ops);
6438 break;
6439 }
6440
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006441 WARN(1, "No oob scheme defined for oobsize %d\n",
6442 mtd->oobsize);
6443 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006444 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 }
6446 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006447
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006448 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006449 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006450 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006451 */
David Woodhouse956e9442006-09-25 17:12:39 +01006452
Huang Shijie97de79e02013-10-18 14:20:53 +08006453 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006454 case NAND_ECC_HW_OOB_FIRST:
6455 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006456 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006457 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6458 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006459 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006460 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006461 if (!ecc->read_page)
6462 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006463
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006464 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006465 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006466 if (!ecc->read_page)
6467 ecc->read_page = nand_read_page_hwecc;
6468 if (!ecc->write_page)
6469 ecc->write_page = nand_write_page_hwecc;
6470 if (!ecc->read_page_raw)
6471 ecc->read_page_raw = nand_read_page_raw;
6472 if (!ecc->write_page_raw)
6473 ecc->write_page_raw = nand_write_page_raw;
6474 if (!ecc->read_oob)
6475 ecc->read_oob = nand_read_oob_std;
6476 if (!ecc->write_oob)
6477 ecc->write_oob = nand_write_oob_std;
6478 if (!ecc->read_subpage)
6479 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006480 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006481 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006482
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006483 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006484 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6485 (!ecc->read_page ||
6486 ecc->read_page == nand_read_page_hwecc ||
6487 !ecc->write_page ||
6488 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006489 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6490 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006491 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006492 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006493 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006494 if (!ecc->read_page)
6495 ecc->read_page = nand_read_page_syndrome;
6496 if (!ecc->write_page)
6497 ecc->write_page = nand_write_page_syndrome;
6498 if (!ecc->read_page_raw)
6499 ecc->read_page_raw = nand_read_page_raw_syndrome;
6500 if (!ecc->write_page_raw)
6501 ecc->write_page_raw = nand_write_page_raw_syndrome;
6502 if (!ecc->read_oob)
6503 ecc->read_oob = nand_read_oob_syndrome;
6504 if (!ecc->write_oob)
6505 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006506
Huang Shijie97de79e02013-10-18 14:20:53 +08006507 if (mtd->writesize >= ecc->size) {
6508 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006509 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6510 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006511 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006512 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006513 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006514 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006515 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6516 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006517 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006518 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006520 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006521 ret = nand_set_ecc_soft_ops(mtd);
6522 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006523 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006524 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006525 }
6526 break;
6527
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006528 case NAND_ECC_ON_DIE:
6529 if (!ecc->read_page || !ecc->write_page) {
6530 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6531 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006532 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006533 }
6534 if (!ecc->read_oob)
6535 ecc->read_oob = nand_read_oob_std;
6536 if (!ecc->write_oob)
6537 ecc->write_oob = nand_write_oob_std;
6538 break;
6539
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006540 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006541 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006542 ecc->read_page = nand_read_page_raw;
6543 ecc->write_page = nand_write_page_raw;
6544 ecc->read_oob = nand_read_oob_std;
6545 ecc->read_page_raw = nand_read_page_raw;
6546 ecc->write_page_raw = nand_write_page_raw;
6547 ecc->write_oob = nand_write_oob_std;
6548 ecc->size = mtd->writesize;
6549 ecc->bytes = 0;
6550 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006552
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006554 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6555 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006556 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006559 if (ecc->correct || ecc->calculate) {
6560 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6561 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6562 if (!ecc->calc_buf || !ecc->code_buf) {
6563 ret = -ENOMEM;
6564 goto err_nand_manuf_cleanup;
6565 }
6566 }
6567
Brian Norris9ce244b2011-08-30 18:45:37 -07006568 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006569 if (!ecc->read_oob_raw)
6570 ecc->read_oob_raw = ecc->read_oob;
6571 if (!ecc->write_oob_raw)
6572 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006573
Boris Brezillon846031d2016-02-03 20:11:00 +01006574 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006575 mtd->ecc_strength = ecc->strength;
6576 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006577
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006578 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006579 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006580 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006581 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006582 ecc->steps = mtd->writesize / ecc->size;
6583 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006584 WARN(1, "Invalid ECC parameters\n");
6585 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006586 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006588 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006589 if (ecc->total > mtd->oobsize) {
6590 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6591 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006592 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006593 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006594
Boris Brezillon846031d2016-02-03 20:11:00 +01006595 /*
6596 * The number of bytes available for a client to place data into
6597 * the out of band area.
6598 */
6599 ret = mtd_ooblayout_count_freebytes(mtd);
6600 if (ret < 0)
6601 ret = 0;
6602
6603 mtd->oobavail = ret;
6604
6605 /* ECC sanity check: warn if it's too weak */
6606 if (!nand_ecc_strength_good(mtd))
6607 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6608 mtd->name);
6609
Brian Norris8b6e50c2011-05-25 14:59:01 -07006610 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006611 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006612 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006613 case 2:
6614 mtd->subpage_sft = 1;
6615 break;
6616 case 4:
6617 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006618 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006619 mtd->subpage_sft = 2;
6620 break;
6621 }
6622 }
6623 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6624
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006625 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006626 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006629 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006631 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306632 switch (ecc->mode) {
6633 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306634 if (chip->page_shift > 9)
6635 chip->options |= NAND_SUBPAGE_READ;
6636 break;
6637
6638 default:
6639 break;
6640 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006641
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006643 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006644 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6645 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006646 mtd->_erase = nand_erase;
6647 mtd->_point = NULL;
6648 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006649 mtd->_panic_write = panic_nand_write;
6650 mtd->_read_oob = nand_read_oob;
6651 mtd->_write_oob = nand_write_oob;
6652 mtd->_sync = nand_sync;
6653 mtd->_lock = NULL;
6654 mtd->_unlock = NULL;
6655 mtd->_suspend = nand_suspend;
6656 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006657 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006658 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006659 mtd->_block_isbad = nand_block_isbad;
6660 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006661 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006662 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006664 /*
6665 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6666 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6667 * properly set.
6668 */
6669 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006670 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
Boris Brezillonf84674b2017-06-02 12:18:24 +02006672 /* Initialize the ->data_interface field. */
6673 ret = nand_init_data_interface(chip);
6674 if (ret)
6675 goto err_nand_manuf_cleanup;
6676
6677 /* Enter fastest possible mode on all dies. */
6678 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006679 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006680 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006681 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006682 }
6683
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006684 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006685 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
6688 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006689 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006690 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006691 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006692
Brian Norris44d41822017-05-01 17:04:50 -07006693 return 0;
6694
Boris Brezillonf84674b2017-06-02 12:18:24 +02006695
6696err_nand_manuf_cleanup:
6697 nand_manufacturer_cleanup(chip);
6698
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006699err_free_buf:
6700 kfree(chip->data_buf);
6701 kfree(ecc->code_buf);
6702 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006703
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006704 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705}
6706
Miquel Raynal05b54c72018-07-19 01:05:46 +02006707static int nand_attach(struct nand_chip *chip)
6708{
6709 if (chip->controller->ops && chip->controller->ops->attach_chip)
6710 return chip->controller->ops->attach_chip(chip);
6711
6712 return 0;
6713}
6714
6715static void nand_detach(struct nand_chip *chip)
6716{
6717 if (chip->controller->ops && chip->controller->ops->detach_chip)
6718 chip->controller->ops->detach_chip(chip);
6719}
6720
David Woodhouse3b85c322006-09-25 17:06:53 +01006721/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006722 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006723 * @chip: NAND chip object
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006724 * @maxchips: number of chips to scan for. @nand_scan_ident() will not be run if
6725 * this parameter is zero (useful for specific drivers that must
6726 * handle this part of the process themselves, e.g docg4).
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006727 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006728 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006729 * This fills out all the uninitialized function pointers with the defaults.
6730 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006731 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006732 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006733int nand_scan_with_ids(struct nand_chip *chip, int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006734 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006735{
6736 int ret;
6737
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006738 if (maxchips) {
Boris Brezillon00ad3782018-09-06 14:05:14 +02006739 ret = nand_scan_ident(chip, maxchips, ids);
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006740 if (ret)
6741 return ret;
6742 }
Miquel Raynal05b54c72018-07-19 01:05:46 +02006743
6744 ret = nand_attach(chip);
6745 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006746 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006747
Boris Brezillon00ad3782018-09-06 14:05:14 +02006748 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006749 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006750 goto detach_chip;
6751
6752 return 0;
6753
6754detach_chip:
6755 nand_detach(chip);
6756cleanup_ident:
6757 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006758
David Woodhouse3b85c322006-09-25 17:06:53 +01006759 return ret;
6760}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006761EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006762
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006764 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6765 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006766 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006767void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006769 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006770 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006771 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6772
Jesper Juhlfa671642005-11-07 01:01:27 -08006773 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006774 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006775 kfree(chip->data_buf);
6776 kfree(chip->ecc.code_buf);
6777 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006778
6779 /* Free bad block descriptor memory */
6780 if (chip->badblock_pattern && chip->badblock_pattern->options
6781 & NAND_BBT_DYNAMICSTRUCT)
6782 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006783
6784 /* Free manufacturer priv data. */
6785 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006786
6787 /* Free controller specific allocations after chip identification */
6788 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006789
6790 /* Free identification phase allocations */
6791 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006793
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006794EXPORT_SYMBOL_GPL(nand_cleanup);
6795
6796/**
6797 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6798 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006799 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006800 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006801void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006802{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006803 mtd_device_unregister(nand_to_mtd(chip));
6804 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006805}
David Woodhousee0c7d762006-05-13 18:07:53 +01006806EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006807
David Woodhousee0c7d762006-05-13 18:07:53 +01006808MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006809MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6810MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006811MODULE_DESCRIPTION("Generic NAND flash driver code");