blob: 0ae597ced5b420dbeaf5fd87c8e20e8daf8785fb [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;
Brian Norris00918422012-01-13 18:11:47 -0800508 nand_erase_nand(mtd, &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) {
518 res = nand_markbad_bbt(mtd, 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 */
568 return nand_isreserved_bbt(mtd, ofs);
569}
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 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100588 return nand_isbad_bbt(mtd, 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{
1203 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001204 int ret;
1205
1206 if (!chip->setup_data_interface)
1207 return 0;
1208
1209 /*
1210 * The ONFI specification says:
1211 * "
1212 * To transition from NV-DDR or NV-DDR2 to the SDR data
1213 * interface, the host shall use the Reset (FFh) command
1214 * using SDR timing mode 0. A device in any timing mode is
1215 * required to recognize Reset (FFh) command issued in SDR
1216 * timing mode 0.
1217 * "
1218 *
1219 * Configure the data interface in SDR mode and set the
1220 * timings to timing mode 0.
1221 */
1222
Miquel Raynal17fa8042017-11-30 18:01:31 +01001223 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
1224 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001225 if (ret)
1226 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1227
1228 return ret;
1229}
1230
1231/**
1232 * nand_setup_data_interface - Setup the best data interface and timings
1233 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001234 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001235 *
1236 * Find and configure the best data interface and NAND timings supported by
1237 * the chip and the driver.
1238 * First tries to retrieve supported timing modes from ONFI information,
1239 * and if the NAND chip does not support ONFI, relies on the
1240 * ->onfi_timing_mode_default specified in the nand_ids table.
1241 *
1242 * Returns 0 for success or negative error code otherwise.
1243 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001244static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001245{
1246 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal97baea12018-03-19 14:47:20 +01001247 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1248 chip->onfi_timing_mode_default,
1249 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001250 int ret;
1251
Miquel Raynal17fa8042017-11-30 18:01:31 +01001252 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001253 return 0;
1254
Miquel Raynal993447b2018-03-19 14:47:21 +01001255 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001256 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001257 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001258 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1259 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001260 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001261 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001262 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001263 }
1264
Miquel Raynal97baea12018-03-19 14:47:20 +01001265 /* Change the mode on the controller side */
Miquel Raynal17fa8042017-11-30 18:01:31 +01001266 ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001267 if (ret)
1268 return ret;
1269
1270 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001271 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001272 return 0;
1273
1274 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001275 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001276 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1277 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001278 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001279 if (ret)
1280 goto err_reset_chip;
1281
1282 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1283 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1284 chip->onfi_timing_mode_default);
1285 goto err_reset_chip;
1286 }
1287
1288 return 0;
1289
1290err_reset_chip:
1291 /*
1292 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1293 * timing mode.
1294 */
1295 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001296 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001297 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001298 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001299
Boris Brezillond8e725d2016-09-15 10:32:50 +02001300 return ret;
1301}
1302
1303/**
1304 * nand_init_data_interface - find the best data interface and timings
1305 * @chip: The NAND chip
1306 *
1307 * Find the best data interface and NAND timings supported by the chip
1308 * and the driver.
1309 * First tries to retrieve supported timing modes from ONFI information,
1310 * and if the NAND chip does not support ONFI, relies on the
1311 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1312 * function nand_chip->data_interface is initialized with the best timing mode
1313 * available.
1314 *
1315 * Returns 0 for success or negative error code otherwise.
1316 */
1317static int nand_init_data_interface(struct nand_chip *chip)
1318{
1319 struct mtd_info *mtd = nand_to_mtd(chip);
1320 int modes, mode, ret;
1321
1322 if (!chip->setup_data_interface)
1323 return 0;
1324
1325 /*
1326 * First try to identify the best timings from ONFI parameters and
1327 * if the NAND does not support ONFI, fallback to the default ONFI
1328 * timing mode.
1329 */
1330 modes = onfi_get_async_timing_mode(chip);
1331 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1332 if (!chip->onfi_timing_mode_default)
1333 return 0;
1334
1335 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1336 }
1337
Boris Brezillond8e725d2016-09-15 10:32:50 +02001338
1339 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001340 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001341 if (ret)
1342 continue;
1343
Miquel Raynald787b8b2017-12-22 18:12:41 +01001344 /*
1345 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1346 * controller supports the requested timings.
1347 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001348 ret = chip->setup_data_interface(mtd,
1349 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001350 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001351 if (!ret) {
1352 chip->onfi_timing_mode_default = mode;
1353 break;
1354 }
1355 }
1356
1357 return 0;
1358}
1359
Boris Brezillond8e725d2016-09-15 10:32:50 +02001360/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001361 * nand_fill_column_cycles - fill the column cycles of an address
1362 * @chip: The NAND chip
1363 * @addrs: Array of address cycles to fill
1364 * @offset_in_page: The offset in the page
1365 *
1366 * Fills the first or the first two bytes of the @addrs field depending
1367 * on the NAND bus width and the page size.
1368 *
1369 * Returns the number of cycles needed to encode the column, or a negative
1370 * error code in case one of the arguments is invalid.
1371 */
1372static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1373 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Miquel Raynal8878b122017-11-09 14:16:45 +01001375 struct mtd_info *mtd = nand_to_mtd(chip);
1376
1377 /* Make sure the offset is less than the actual page size. */
1378 if (offset_in_page > mtd->writesize + mtd->oobsize)
1379 return -EINVAL;
1380
1381 /*
1382 * On small page NANDs, there's a dedicated command to access the OOB
1383 * area, and the column address is relative to the start of the OOB
1384 * area, not the start of the page. Asjust the address accordingly.
1385 */
1386 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1387 offset_in_page -= mtd->writesize;
1388
1389 /*
1390 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1391 * wide, then it must be divided by 2.
1392 */
1393 if (chip->options & NAND_BUSWIDTH_16) {
1394 if (WARN_ON(offset_in_page % 2))
1395 return -EINVAL;
1396
1397 offset_in_page /= 2;
1398 }
1399
1400 addrs[0] = offset_in_page;
1401
1402 /*
1403 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1404 * need 2
1405 */
1406 if (mtd->writesize <= 512)
1407 return 1;
1408
1409 addrs[1] = offset_in_page >> 8;
1410
1411 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Miquel Raynal8878b122017-11-09 14:16:45 +01001414static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1415 unsigned int offset_in_page, void *buf,
1416 unsigned int len)
1417{
1418 struct mtd_info *mtd = nand_to_mtd(chip);
1419 const struct nand_sdr_timings *sdr =
1420 nand_get_sdr_timings(&chip->data_interface);
1421 u8 addrs[4];
1422 struct nand_op_instr instrs[] = {
1423 NAND_OP_CMD(NAND_CMD_READ0, 0),
1424 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1425 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1426 PSEC_TO_NSEC(sdr->tRR_min)),
1427 NAND_OP_DATA_IN(len, buf, 0),
1428 };
1429 struct nand_operation op = NAND_OPERATION(instrs);
1430 int ret;
1431
1432 /* Drop the DATA_IN instruction if len is set to 0. */
1433 if (!len)
1434 op.ninstrs--;
1435
1436 if (offset_in_page >= mtd->writesize)
1437 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1438 else if (offset_in_page >= 256 &&
1439 !(chip->options & NAND_BUSWIDTH_16))
1440 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1441
1442 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1443 if (ret < 0)
1444 return ret;
1445
1446 addrs[1] = page;
1447 addrs[2] = page >> 8;
1448
1449 if (chip->options & NAND_ROW_ADDR_3) {
1450 addrs[3] = page >> 16;
1451 instrs[1].ctx.addr.naddrs++;
1452 }
1453
1454 return nand_exec_op(chip, &op);
1455}
1456
1457static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1458 unsigned int offset_in_page, void *buf,
1459 unsigned int len)
1460{
1461 const struct nand_sdr_timings *sdr =
1462 nand_get_sdr_timings(&chip->data_interface);
1463 u8 addrs[5];
1464 struct nand_op_instr instrs[] = {
1465 NAND_OP_CMD(NAND_CMD_READ0, 0),
1466 NAND_OP_ADDR(4, addrs, 0),
1467 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1468 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1469 PSEC_TO_NSEC(sdr->tRR_min)),
1470 NAND_OP_DATA_IN(len, buf, 0),
1471 };
1472 struct nand_operation op = NAND_OPERATION(instrs);
1473 int ret;
1474
1475 /* Drop the DATA_IN instruction if len is set to 0. */
1476 if (!len)
1477 op.ninstrs--;
1478
1479 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1480 if (ret < 0)
1481 return ret;
1482
1483 addrs[2] = page;
1484 addrs[3] = page >> 8;
1485
1486 if (chip->options & NAND_ROW_ADDR_3) {
1487 addrs[4] = page >> 16;
1488 instrs[1].ctx.addr.naddrs++;
1489 }
1490
1491 return nand_exec_op(chip, &op);
1492}
1493
1494/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001495 * nand_read_page_op - Do a READ PAGE operation
1496 * @chip: The NAND chip
1497 * @page: page to read
1498 * @offset_in_page: offset within the page
1499 * @buf: buffer used to store the data
1500 * @len: length of the buffer
1501 *
1502 * This function issues a READ PAGE operation.
1503 * This function does not select/unselect the CS line.
1504 *
1505 * Returns 0 on success, a negative error code otherwise.
1506 */
1507int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1508 unsigned int offset_in_page, void *buf, unsigned int len)
1509{
1510 struct mtd_info *mtd = nand_to_mtd(chip);
1511
1512 if (len && !buf)
1513 return -EINVAL;
1514
1515 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1516 return -EINVAL;
1517
Miquel Raynal8878b122017-11-09 14:16:45 +01001518 if (chip->exec_op) {
1519 if (mtd->writesize > 512)
1520 return nand_lp_exec_read_page_op(chip, page,
1521 offset_in_page, buf,
1522 len);
1523
1524 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1525 buf, len);
1526 }
1527
Boris Brezillon5295cf22018-09-06 14:05:28 +02001528 chip->cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001529 if (len)
Boris Brezillon7e534322018-09-06 14:05:22 +02001530 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001531
1532 return 0;
1533}
1534EXPORT_SYMBOL_GPL(nand_read_page_op);
1535
1536/**
1537 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1538 * @chip: The NAND chip
1539 * @page: parameter page to read
1540 * @buf: buffer used to store the data
1541 * @len: length of the buffer
1542 *
1543 * This function issues a READ PARAMETER PAGE operation.
1544 * This function does not select/unselect the CS line.
1545 *
1546 * Returns 0 on success, a negative error code otherwise.
1547 */
1548static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1549 unsigned int len)
1550{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001551 unsigned int i;
1552 u8 *p = buf;
1553
1554 if (len && !buf)
1555 return -EINVAL;
1556
Miquel Raynal8878b122017-11-09 14:16:45 +01001557 if (chip->exec_op) {
1558 const struct nand_sdr_timings *sdr =
1559 nand_get_sdr_timings(&chip->data_interface);
1560 struct nand_op_instr instrs[] = {
1561 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1562 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1563 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1564 PSEC_TO_NSEC(sdr->tRR_min)),
1565 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1566 };
1567 struct nand_operation op = NAND_OPERATION(instrs);
1568
1569 /* Drop the DATA_IN instruction if len is set to 0. */
1570 if (!len)
1571 op.ninstrs--;
1572
1573 return nand_exec_op(chip, &op);
1574 }
1575
Boris Brezillon5295cf22018-09-06 14:05:28 +02001576 chip->cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001577 for (i = 0; i < len; i++)
Boris Brezillon7e534322018-09-06 14:05:22 +02001578 p[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001579
1580 return 0;
1581}
1582
1583/**
1584 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1585 * @chip: The NAND chip
1586 * @offset_in_page: offset within the page
1587 * @buf: buffer used to store the data
1588 * @len: length of the buffer
1589 * @force_8bit: force 8-bit bus access
1590 *
1591 * This function issues a CHANGE READ COLUMN operation.
1592 * This function does not select/unselect the CS line.
1593 *
1594 * Returns 0 on success, a negative error code otherwise.
1595 */
1596int nand_change_read_column_op(struct nand_chip *chip,
1597 unsigned int offset_in_page, void *buf,
1598 unsigned int len, bool force_8bit)
1599{
1600 struct mtd_info *mtd = nand_to_mtd(chip);
1601
1602 if (len && !buf)
1603 return -EINVAL;
1604
1605 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1606 return -EINVAL;
1607
Miquel Raynal8878b122017-11-09 14:16:45 +01001608 /* Small page NANDs do not support column change. */
1609 if (mtd->writesize <= 512)
1610 return -ENOTSUPP;
1611
1612 if (chip->exec_op) {
1613 const struct nand_sdr_timings *sdr =
1614 nand_get_sdr_timings(&chip->data_interface);
1615 u8 addrs[2] = {};
1616 struct nand_op_instr instrs[] = {
1617 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1618 NAND_OP_ADDR(2, addrs, 0),
1619 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1620 PSEC_TO_NSEC(sdr->tCCS_min)),
1621 NAND_OP_DATA_IN(len, buf, 0),
1622 };
1623 struct nand_operation op = NAND_OPERATION(instrs);
1624 int ret;
1625
1626 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1627 if (ret < 0)
1628 return ret;
1629
1630 /* Drop the DATA_IN instruction if len is set to 0. */
1631 if (!len)
1632 op.ninstrs--;
1633
1634 instrs[3].ctx.data.force_8bit = force_8bit;
1635
1636 return nand_exec_op(chip, &op);
1637 }
1638
Boris Brezillon5295cf22018-09-06 14:05:28 +02001639 chip->cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001640 if (len)
Boris Brezillon7e534322018-09-06 14:05:22 +02001641 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001642
1643 return 0;
1644}
1645EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1646
1647/**
1648 * nand_read_oob_op - Do a READ OOB operation
1649 * @chip: The NAND chip
1650 * @page: page to read
1651 * @offset_in_oob: offset within the OOB area
1652 * @buf: buffer used to store the data
1653 * @len: length of the buffer
1654 *
1655 * This function issues a READ OOB operation.
1656 * This function does not select/unselect the CS line.
1657 *
1658 * Returns 0 on success, a negative error code otherwise.
1659 */
1660int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1661 unsigned int offset_in_oob, void *buf, unsigned int len)
1662{
1663 struct mtd_info *mtd = nand_to_mtd(chip);
1664
1665 if (len && !buf)
1666 return -EINVAL;
1667
1668 if (offset_in_oob + len > mtd->oobsize)
1669 return -EINVAL;
1670
Miquel Raynal8878b122017-11-09 14:16:45 +01001671 if (chip->exec_op)
1672 return nand_read_page_op(chip, page,
1673 mtd->writesize + offset_in_oob,
1674 buf, len);
1675
Boris Brezillon5295cf22018-09-06 14:05:28 +02001676 chip->cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001677 if (len)
Boris Brezillon7e534322018-09-06 14:05:22 +02001678 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001679
1680 return 0;
1681}
1682EXPORT_SYMBOL_GPL(nand_read_oob_op);
1683
Miquel Raynal8878b122017-11-09 14:16:45 +01001684static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1685 unsigned int offset_in_page, const void *buf,
1686 unsigned int len, bool prog)
1687{
1688 struct mtd_info *mtd = nand_to_mtd(chip);
1689 const struct nand_sdr_timings *sdr =
1690 nand_get_sdr_timings(&chip->data_interface);
1691 u8 addrs[5] = {};
1692 struct nand_op_instr instrs[] = {
1693 /*
1694 * The first instruction will be dropped if we're dealing
1695 * with a large page NAND and adjusted if we're dealing
1696 * with a small page NAND and the page offset is > 255.
1697 */
1698 NAND_OP_CMD(NAND_CMD_READ0, 0),
1699 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1700 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1701 NAND_OP_DATA_OUT(len, buf, 0),
1702 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1703 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1704 };
1705 struct nand_operation op = NAND_OPERATION(instrs);
1706 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1707 int ret;
1708 u8 status;
1709
1710 if (naddrs < 0)
1711 return naddrs;
1712
1713 addrs[naddrs++] = page;
1714 addrs[naddrs++] = page >> 8;
1715 if (chip->options & NAND_ROW_ADDR_3)
1716 addrs[naddrs++] = page >> 16;
1717
1718 instrs[2].ctx.addr.naddrs = naddrs;
1719
1720 /* Drop the last two instructions if we're not programming the page. */
1721 if (!prog) {
1722 op.ninstrs -= 2;
1723 /* Also drop the DATA_OUT instruction if empty. */
1724 if (!len)
1725 op.ninstrs--;
1726 }
1727
1728 if (mtd->writesize <= 512) {
1729 /*
1730 * Small pages need some more tweaking: we have to adjust the
1731 * first instruction depending on the page offset we're trying
1732 * to access.
1733 */
1734 if (offset_in_page >= mtd->writesize)
1735 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1736 else if (offset_in_page >= 256 &&
1737 !(chip->options & NAND_BUSWIDTH_16))
1738 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1739 } else {
1740 /*
1741 * Drop the first command if we're dealing with a large page
1742 * NAND.
1743 */
1744 op.instrs++;
1745 op.ninstrs--;
1746 }
1747
1748 ret = nand_exec_op(chip, &op);
1749 if (!prog || ret)
1750 return ret;
1751
1752 ret = nand_status_op(chip, &status);
1753 if (ret)
1754 return ret;
1755
1756 return status;
1757}
1758
Boris Brezillon97d90da2017-11-30 18:01:29 +01001759/**
1760 * nand_prog_page_begin_op - starts a PROG PAGE operation
1761 * @chip: The NAND chip
1762 * @page: page to write
1763 * @offset_in_page: offset within the page
1764 * @buf: buffer containing the data to write to the page
1765 * @len: length of the buffer
1766 *
1767 * This function issues the first half of a PROG PAGE operation.
1768 * This function does not select/unselect the CS line.
1769 *
1770 * Returns 0 on success, a negative error code otherwise.
1771 */
1772int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1773 unsigned int offset_in_page, const void *buf,
1774 unsigned int len)
1775{
1776 struct mtd_info *mtd = nand_to_mtd(chip);
1777
1778 if (len && !buf)
1779 return -EINVAL;
1780
1781 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1782 return -EINVAL;
1783
Miquel Raynal8878b122017-11-09 14:16:45 +01001784 if (chip->exec_op)
1785 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1786 len, false);
1787
Boris Brezillon5295cf22018-09-06 14:05:28 +02001788 chip->cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001789
1790 if (buf)
Boris Brezillonc0739d82018-09-06 14:05:23 +02001791 chip->write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001792
1793 return 0;
1794}
1795EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1796
1797/**
1798 * nand_prog_page_end_op - ends a PROG PAGE operation
1799 * @chip: The NAND chip
1800 *
1801 * This function issues the second half of a PROG PAGE operation.
1802 * This function does not select/unselect the CS line.
1803 *
1804 * Returns 0 on success, a negative error code otherwise.
1805 */
1806int nand_prog_page_end_op(struct nand_chip *chip)
1807{
Miquel Raynal8878b122017-11-09 14:16:45 +01001808 int ret;
1809 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001810
Miquel Raynal8878b122017-11-09 14:16:45 +01001811 if (chip->exec_op) {
1812 const struct nand_sdr_timings *sdr =
1813 nand_get_sdr_timings(&chip->data_interface);
1814 struct nand_op_instr instrs[] = {
1815 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1816 PSEC_TO_NSEC(sdr->tWB_max)),
1817 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1818 };
1819 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001820
Miquel Raynal8878b122017-11-09 14:16:45 +01001821 ret = nand_exec_op(chip, &op);
1822 if (ret)
1823 return ret;
1824
1825 ret = nand_status_op(chip, &status);
1826 if (ret)
1827 return ret;
1828 } else {
Boris Brezillon5295cf22018-09-06 14:05:28 +02001829 chip->cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillonf1d46942018-09-06 14:05:29 +02001830 ret = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001831 if (ret < 0)
1832 return ret;
1833
1834 status = ret;
1835 }
1836
Boris Brezillon97d90da2017-11-30 18:01:29 +01001837 if (status & NAND_STATUS_FAIL)
1838 return -EIO;
1839
1840 return 0;
1841}
1842EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1843
1844/**
1845 * nand_prog_page_op - Do a full PROG PAGE operation
1846 * @chip: The NAND chip
1847 * @page: page to write
1848 * @offset_in_page: offset within the page
1849 * @buf: buffer containing the data to write to the page
1850 * @len: length of the buffer
1851 *
1852 * This function issues a full PROG PAGE operation.
1853 * This function does not select/unselect the CS line.
1854 *
1855 * Returns 0 on success, a negative error code otherwise.
1856 */
1857int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1858 unsigned int offset_in_page, const void *buf,
1859 unsigned int len)
1860{
1861 struct mtd_info *mtd = nand_to_mtd(chip);
1862 int status;
1863
1864 if (!len || !buf)
1865 return -EINVAL;
1866
1867 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1868 return -EINVAL;
1869
Miquel Raynal8878b122017-11-09 14:16:45 +01001870 if (chip->exec_op) {
1871 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1872 len, true);
1873 } else {
Boris Brezillon5295cf22018-09-06 14:05:28 +02001874 chip->cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillonc0739d82018-09-06 14:05:23 +02001875 chip->write_buf(chip, buf, len);
Boris Brezillon5295cf22018-09-06 14:05:28 +02001876 chip->cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillonf1d46942018-09-06 14:05:29 +02001877 status = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001878 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001879
Boris Brezillon97d90da2017-11-30 18:01:29 +01001880 if (status & NAND_STATUS_FAIL)
1881 return -EIO;
1882
1883 return 0;
1884}
1885EXPORT_SYMBOL_GPL(nand_prog_page_op);
1886
1887/**
1888 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1889 * @chip: The NAND chip
1890 * @offset_in_page: offset within the page
1891 * @buf: buffer containing the data to send to the NAND
1892 * @len: length of the buffer
1893 * @force_8bit: force 8-bit bus access
1894 *
1895 * This function issues a CHANGE WRITE COLUMN operation.
1896 * This function does not select/unselect the CS line.
1897 *
1898 * Returns 0 on success, a negative error code otherwise.
1899 */
1900int nand_change_write_column_op(struct nand_chip *chip,
1901 unsigned int offset_in_page,
1902 const void *buf, unsigned int len,
1903 bool force_8bit)
1904{
1905 struct mtd_info *mtd = nand_to_mtd(chip);
1906
1907 if (len && !buf)
1908 return -EINVAL;
1909
1910 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1911 return -EINVAL;
1912
Miquel Raynal8878b122017-11-09 14:16:45 +01001913 /* Small page NANDs do not support column change. */
1914 if (mtd->writesize <= 512)
1915 return -ENOTSUPP;
1916
1917 if (chip->exec_op) {
1918 const struct nand_sdr_timings *sdr =
1919 nand_get_sdr_timings(&chip->data_interface);
1920 u8 addrs[2];
1921 struct nand_op_instr instrs[] = {
1922 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1923 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1924 NAND_OP_DATA_OUT(len, buf, 0),
1925 };
1926 struct nand_operation op = NAND_OPERATION(instrs);
1927 int ret;
1928
1929 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1930 if (ret < 0)
1931 return ret;
1932
1933 instrs[2].ctx.data.force_8bit = force_8bit;
1934
1935 /* Drop the DATA_OUT instruction if len is set to 0. */
1936 if (!len)
1937 op.ninstrs--;
1938
1939 return nand_exec_op(chip, &op);
1940 }
1941
Boris Brezillon5295cf22018-09-06 14:05:28 +02001942 chip->cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001943 if (len)
Boris Brezillonc0739d82018-09-06 14:05:23 +02001944 chip->write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001945
1946 return 0;
1947}
1948EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1949
1950/**
1951 * nand_readid_op - Do a READID operation
1952 * @chip: The NAND chip
1953 * @addr: address cycle to pass after the READID command
1954 * @buf: buffer used to store the ID
1955 * @len: length of the buffer
1956 *
1957 * This function sends a READID command and reads back the ID returned by the
1958 * NAND.
1959 * This function does not select/unselect the CS line.
1960 *
1961 * Returns 0 on success, a negative error code otherwise.
1962 */
1963int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1964 unsigned int len)
1965{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001966 unsigned int i;
1967 u8 *id = buf;
1968
1969 if (len && !buf)
1970 return -EINVAL;
1971
Miquel Raynal8878b122017-11-09 14:16:45 +01001972 if (chip->exec_op) {
1973 const struct nand_sdr_timings *sdr =
1974 nand_get_sdr_timings(&chip->data_interface);
1975 struct nand_op_instr instrs[] = {
1976 NAND_OP_CMD(NAND_CMD_READID, 0),
1977 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1978 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1979 };
1980 struct nand_operation op = NAND_OPERATION(instrs);
1981
1982 /* Drop the DATA_IN instruction if len is set to 0. */
1983 if (!len)
1984 op.ninstrs--;
1985
1986 return nand_exec_op(chip, &op);
1987 }
1988
Boris Brezillon5295cf22018-09-06 14:05:28 +02001989 chip->cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001990
1991 for (i = 0; i < len; i++)
Boris Brezillon7e534322018-09-06 14:05:22 +02001992 id[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001993
1994 return 0;
1995}
1996EXPORT_SYMBOL_GPL(nand_readid_op);
1997
1998/**
1999 * nand_status_op - Do a STATUS operation
2000 * @chip: The NAND chip
2001 * @status: out variable to store the NAND status
2002 *
2003 * This function sends a STATUS command and reads back the status returned by
2004 * the NAND.
2005 * This function does not select/unselect the CS line.
2006 *
2007 * Returns 0 on success, a negative error code otherwise.
2008 */
2009int nand_status_op(struct nand_chip *chip, u8 *status)
2010{
Miquel Raynal8878b122017-11-09 14:16:45 +01002011 if (chip->exec_op) {
2012 const struct nand_sdr_timings *sdr =
2013 nand_get_sdr_timings(&chip->data_interface);
2014 struct nand_op_instr instrs[] = {
2015 NAND_OP_CMD(NAND_CMD_STATUS,
2016 PSEC_TO_NSEC(sdr->tADL_min)),
2017 NAND_OP_8BIT_DATA_IN(1, status, 0),
2018 };
2019 struct nand_operation op = NAND_OPERATION(instrs);
2020
2021 if (!status)
2022 op.ninstrs--;
2023
2024 return nand_exec_op(chip, &op);
2025 }
2026
Boris Brezillon5295cf22018-09-06 14:05:28 +02002027 chip->cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002028 if (status)
Boris Brezillon7e534322018-09-06 14:05:22 +02002029 *status = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002030
2031 return 0;
2032}
2033EXPORT_SYMBOL_GPL(nand_status_op);
2034
2035/**
2036 * nand_exit_status_op - Exit a STATUS operation
2037 * @chip: The NAND chip
2038 *
2039 * This function sends a READ0 command to cancel the effect of the STATUS
2040 * command to avoid reading only the status until a new read command is sent.
2041 *
2042 * This function does not select/unselect the CS line.
2043 *
2044 * Returns 0 on success, a negative error code otherwise.
2045 */
2046int nand_exit_status_op(struct nand_chip *chip)
2047{
Miquel Raynal8878b122017-11-09 14:16:45 +01002048 if (chip->exec_op) {
2049 struct nand_op_instr instrs[] = {
2050 NAND_OP_CMD(NAND_CMD_READ0, 0),
2051 };
2052 struct nand_operation op = NAND_OPERATION(instrs);
2053
2054 return nand_exec_op(chip, &op);
2055 }
2056
Boris Brezillon5295cf22018-09-06 14:05:28 +02002057 chip->cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002058
2059 return 0;
2060}
2061EXPORT_SYMBOL_GPL(nand_exit_status_op);
2062
2063/**
2064 * nand_erase_op - Do an erase operation
2065 * @chip: The NAND chip
2066 * @eraseblock: block to erase
2067 *
2068 * This function sends an ERASE command and waits for the NAND to be ready
2069 * before returning.
2070 * This function does not select/unselect the CS line.
2071 *
2072 * Returns 0 on success, a negative error code otherwise.
2073 */
2074int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2075{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002076 unsigned int page = eraseblock <<
2077 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002078 int ret;
2079 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002080
Miquel Raynal8878b122017-11-09 14:16:45 +01002081 if (chip->exec_op) {
2082 const struct nand_sdr_timings *sdr =
2083 nand_get_sdr_timings(&chip->data_interface);
2084 u8 addrs[3] = { page, page >> 8, page >> 16 };
2085 struct nand_op_instr instrs[] = {
2086 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2087 NAND_OP_ADDR(2, addrs, 0),
2088 NAND_OP_CMD(NAND_CMD_ERASE2,
2089 PSEC_TO_MSEC(sdr->tWB_max)),
2090 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2091 };
2092 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002093
Miquel Raynal8878b122017-11-09 14:16:45 +01002094 if (chip->options & NAND_ROW_ADDR_3)
2095 instrs[1].ctx.addr.naddrs++;
2096
2097 ret = nand_exec_op(chip, &op);
2098 if (ret)
2099 return ret;
2100
2101 ret = nand_status_op(chip, &status);
2102 if (ret)
2103 return ret;
2104 } else {
Boris Brezillon5295cf22018-09-06 14:05:28 +02002105 chip->cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2106 chip->cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002107
Boris Brezillonf1d46942018-09-06 14:05:29 +02002108 ret = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002109 if (ret < 0)
2110 return ret;
2111
2112 status = ret;
2113 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002114
2115 if (status & NAND_STATUS_FAIL)
2116 return -EIO;
2117
2118 return 0;
2119}
2120EXPORT_SYMBOL_GPL(nand_erase_op);
2121
2122/**
2123 * nand_set_features_op - Do a SET FEATURES operation
2124 * @chip: The NAND chip
2125 * @feature: feature id
2126 * @data: 4 bytes of data
2127 *
2128 * This function sends a SET FEATURES command and waits for the NAND to be
2129 * ready before returning.
2130 * This function does not select/unselect the CS line.
2131 *
2132 * Returns 0 on success, a negative error code otherwise.
2133 */
2134static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2135 const void *data)
2136{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002137 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002138 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002139
Miquel Raynal8878b122017-11-09 14:16:45 +01002140 if (chip->exec_op) {
2141 const struct nand_sdr_timings *sdr =
2142 nand_get_sdr_timings(&chip->data_interface);
2143 struct nand_op_instr instrs[] = {
2144 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2145 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2146 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2147 PSEC_TO_NSEC(sdr->tWB_max)),
2148 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2149 };
2150 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002151
Boris Brezillon782d1962018-05-11 14:44:07 +02002152 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002153 }
2154
Boris Brezillon5295cf22018-09-06 14:05:28 +02002155 chip->cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002156 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillonc0739d82018-09-06 14:05:23 +02002157 chip->write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002158
Boris Brezillonf1d46942018-09-06 14:05:29 +02002159 ret = chip->waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002160 if (ret < 0)
2161 return ret;
2162
2163 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002164 return -EIO;
2165
2166 return 0;
2167}
2168
2169/**
2170 * nand_get_features_op - Do a GET FEATURES operation
2171 * @chip: The NAND chip
2172 * @feature: feature id
2173 * @data: 4 bytes of data
2174 *
2175 * This function sends a GET FEATURES command and waits for the NAND to be
2176 * ready before returning.
2177 * This function does not select/unselect the CS line.
2178 *
2179 * Returns 0 on success, a negative error code otherwise.
2180 */
2181static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2182 void *data)
2183{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002184 u8 *params = data;
2185 int i;
2186
Miquel Raynal8878b122017-11-09 14:16:45 +01002187 if (chip->exec_op) {
2188 const struct nand_sdr_timings *sdr =
2189 nand_get_sdr_timings(&chip->data_interface);
2190 struct nand_op_instr instrs[] = {
2191 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2192 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2193 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2194 PSEC_TO_NSEC(sdr->tRR_min)),
2195 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2196 data, 0),
2197 };
2198 struct nand_operation op = NAND_OPERATION(instrs);
2199
2200 return nand_exec_op(chip, &op);
2201 }
2202
Boris Brezillon5295cf22018-09-06 14:05:28 +02002203 chip->cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002204 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon7e534322018-09-06 14:05:22 +02002205 params[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002206
2207 return 0;
2208}
2209
Boris Brezillon52f05b62018-07-27 09:44:18 +02002210static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2211 unsigned int delay_ns)
2212{
2213 if (chip->exec_op) {
2214 struct nand_op_instr instrs[] = {
2215 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2216 PSEC_TO_NSEC(delay_ns)),
2217 };
2218 struct nand_operation op = NAND_OPERATION(instrs);
2219
2220 return nand_exec_op(chip, &op);
2221 }
2222
2223 /* Apply delay or wait for ready/busy pin */
2224 if (!chip->dev_ready)
2225 udelay(chip->chip_delay);
2226 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002227 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002228
2229 return 0;
2230}
2231
Boris Brezillon97d90da2017-11-30 18:01:29 +01002232/**
2233 * nand_reset_op - Do a reset operation
2234 * @chip: The NAND chip
2235 *
2236 * This function sends a RESET command and waits for the NAND to be ready
2237 * before returning.
2238 * This function does not select/unselect the CS line.
2239 *
2240 * Returns 0 on success, a negative error code otherwise.
2241 */
2242int nand_reset_op(struct nand_chip *chip)
2243{
Miquel Raynal8878b122017-11-09 14:16:45 +01002244 if (chip->exec_op) {
2245 const struct nand_sdr_timings *sdr =
2246 nand_get_sdr_timings(&chip->data_interface);
2247 struct nand_op_instr instrs[] = {
2248 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2249 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2250 };
2251 struct nand_operation op = NAND_OPERATION(instrs);
2252
2253 return nand_exec_op(chip, &op);
2254 }
2255
Boris Brezillon5295cf22018-09-06 14:05:28 +02002256 chip->cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002257
2258 return 0;
2259}
2260EXPORT_SYMBOL_GPL(nand_reset_op);
2261
2262/**
2263 * nand_read_data_op - Read data from the NAND
2264 * @chip: The NAND chip
2265 * @buf: buffer used to store the data
2266 * @len: length of the buffer
2267 * @force_8bit: force 8-bit bus access
2268 *
2269 * This function does a raw data read on the bus. Usually used after launching
2270 * another NAND operation like nand_read_page_op().
2271 * This function does not select/unselect the CS line.
2272 *
2273 * Returns 0 on success, a negative error code otherwise.
2274 */
2275int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2276 bool force_8bit)
2277{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002278 if (!len || !buf)
2279 return -EINVAL;
2280
Miquel Raynal8878b122017-11-09 14:16:45 +01002281 if (chip->exec_op) {
2282 struct nand_op_instr instrs[] = {
2283 NAND_OP_DATA_IN(len, buf, 0),
2284 };
2285 struct nand_operation op = NAND_OPERATION(instrs);
2286
2287 instrs[0].ctx.data.force_8bit = force_8bit;
2288
2289 return nand_exec_op(chip, &op);
2290 }
2291
Boris Brezillon97d90da2017-11-30 18:01:29 +01002292 if (force_8bit) {
2293 u8 *p = buf;
2294 unsigned int i;
2295
2296 for (i = 0; i < len; i++)
Boris Brezillon7e534322018-09-06 14:05:22 +02002297 p[i] = chip->read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002298 } else {
Boris Brezillon7e534322018-09-06 14:05:22 +02002299 chip->read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002300 }
2301
2302 return 0;
2303}
2304EXPORT_SYMBOL_GPL(nand_read_data_op);
2305
2306/**
2307 * nand_write_data_op - Write data from the NAND
2308 * @chip: The NAND chip
2309 * @buf: buffer containing the data to send on the bus
2310 * @len: length of the buffer
2311 * @force_8bit: force 8-bit bus access
2312 *
2313 * This function does a raw data write on the bus. Usually used after launching
2314 * another NAND operation like nand_write_page_begin_op().
2315 * This function does not select/unselect the CS line.
2316 *
2317 * Returns 0 on success, a negative error code otherwise.
2318 */
2319int nand_write_data_op(struct nand_chip *chip, const void *buf,
2320 unsigned int len, bool force_8bit)
2321{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002322 if (!len || !buf)
2323 return -EINVAL;
2324
Miquel Raynal8878b122017-11-09 14:16:45 +01002325 if (chip->exec_op) {
2326 struct nand_op_instr instrs[] = {
2327 NAND_OP_DATA_OUT(len, buf, 0),
2328 };
2329 struct nand_operation op = NAND_OPERATION(instrs);
2330
2331 instrs[0].ctx.data.force_8bit = force_8bit;
2332
2333 return nand_exec_op(chip, &op);
2334 }
2335
Boris Brezillon97d90da2017-11-30 18:01:29 +01002336 if (force_8bit) {
2337 const u8 *p = buf;
2338 unsigned int i;
2339
2340 for (i = 0; i < len; i++)
Boris Brezillonc0739d82018-09-06 14:05:23 +02002341 chip->write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002342 } else {
Boris Brezillonc0739d82018-09-06 14:05:23 +02002343 chip->write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002344 }
2345
2346 return 0;
2347}
2348EXPORT_SYMBOL_GPL(nand_write_data_op);
2349
2350/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002351 * struct nand_op_parser_ctx - Context used by the parser
2352 * @instrs: array of all the instructions that must be addressed
2353 * @ninstrs: length of the @instrs array
2354 * @subop: Sub-operation to be passed to the NAND controller
2355 *
2356 * This structure is used by the core to split NAND operations into
2357 * sub-operations that can be handled by the NAND controller.
2358 */
2359struct nand_op_parser_ctx {
2360 const struct nand_op_instr *instrs;
2361 unsigned int ninstrs;
2362 struct nand_subop subop;
2363};
2364
2365/**
2366 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2367 * @pat: the parser pattern element that matches @instr
2368 * @instr: pointer to the instruction to check
2369 * @start_offset: this is an in/out parameter. If @instr has already been
2370 * split, then @start_offset is the offset from which to start
2371 * (either an address cycle or an offset in the data buffer).
2372 * Conversely, if the function returns true (ie. instr must be
2373 * split), this parameter is updated to point to the first
2374 * data/address cycle that has not been taken care of.
2375 *
2376 * Some NAND controllers are limited and cannot send X address cycles with a
2377 * unique operation, or cannot read/write more than Y bytes at the same time.
2378 * In this case, split the instruction that does not fit in a single
2379 * controller-operation into two or more chunks.
2380 *
2381 * Returns true if the instruction must be split, false otherwise.
2382 * The @start_offset parameter is also updated to the offset at which the next
2383 * bundle of instruction must start (if an address or a data instruction).
2384 */
2385static bool
2386nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2387 const struct nand_op_instr *instr,
2388 unsigned int *start_offset)
2389{
2390 switch (pat->type) {
2391 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002392 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002393 break;
2394
2395 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002396 pat->ctx.addr.maxcycles) {
2397 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002398 return true;
2399 }
2400 break;
2401
2402 case NAND_OP_DATA_IN_INSTR:
2403 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002404 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002405 break;
2406
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002407 if (instr->ctx.data.len - *start_offset >
2408 pat->ctx.data.maxlen) {
2409 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002410 return true;
2411 }
2412 break;
2413
2414 default:
2415 break;
2416 }
2417
2418 return false;
2419}
2420
2421/**
2422 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2423 * remaining in the parser context
2424 * @pat: the pattern to test
2425 * @ctx: the parser context structure to match with the pattern @pat
2426 *
2427 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2428 * Returns true if this is the case, false ortherwise. When true is returned,
2429 * @ctx->subop is updated with the set of instructions to be passed to the
2430 * controller driver.
2431 */
2432static bool
2433nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2434 struct nand_op_parser_ctx *ctx)
2435{
2436 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2437 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2438 const struct nand_op_instr *instr = ctx->subop.instrs;
2439 unsigned int i, ninstrs;
2440
2441 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2442 /*
2443 * The pattern instruction does not match the operation
2444 * instruction. If the instruction is marked optional in the
2445 * pattern definition, we skip the pattern element and continue
2446 * to the next one. If the element is mandatory, there's no
2447 * match and we can return false directly.
2448 */
2449 if (instr->type != pat->elems[i].type) {
2450 if (!pat->elems[i].optional)
2451 return false;
2452
2453 continue;
2454 }
2455
2456 /*
2457 * Now check the pattern element constraints. If the pattern is
2458 * not able to handle the whole instruction in a single step,
2459 * we have to split it.
2460 * The last_instr_end_off value comes back updated to point to
2461 * the position where we have to split the instruction (the
2462 * start of the next subop chunk).
2463 */
2464 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2465 &instr_offset)) {
2466 ninstrs++;
2467 i++;
2468 break;
2469 }
2470
2471 instr++;
2472 ninstrs++;
2473 instr_offset = 0;
2474 }
2475
2476 /*
2477 * This can happen if all instructions of a pattern are optional.
2478 * Still, if there's not at least one instruction handled by this
2479 * pattern, this is not a match, and we should try the next one (if
2480 * any).
2481 */
2482 if (!ninstrs)
2483 return false;
2484
2485 /*
2486 * We had a match on the pattern head, but the pattern may be longer
2487 * than the instructions we're asked to execute. We need to make sure
2488 * there's no mandatory elements in the pattern tail.
2489 */
2490 for (; i < pat->nelems; i++) {
2491 if (!pat->elems[i].optional)
2492 return false;
2493 }
2494
2495 /*
2496 * We have a match: update the subop structure accordingly and return
2497 * true.
2498 */
2499 ctx->subop.ninstrs = ninstrs;
2500 ctx->subop.last_instr_end_off = instr_offset;
2501
2502 return true;
2503}
2504
2505#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2506static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2507{
2508 const struct nand_op_instr *instr;
2509 char *prefix = " ";
2510 unsigned int i;
2511
2512 pr_debug("executing subop:\n");
2513
2514 for (i = 0; i < ctx->ninstrs; i++) {
2515 instr = &ctx->instrs[i];
2516
2517 if (instr == &ctx->subop.instrs[0])
2518 prefix = " ->";
2519
2520 switch (instr->type) {
2521 case NAND_OP_CMD_INSTR:
2522 pr_debug("%sCMD [0x%02x]\n", prefix,
2523 instr->ctx.cmd.opcode);
2524 break;
2525 case NAND_OP_ADDR_INSTR:
2526 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2527 instr->ctx.addr.naddrs,
2528 instr->ctx.addr.naddrs < 64 ?
2529 instr->ctx.addr.naddrs : 64,
2530 instr->ctx.addr.addrs);
2531 break;
2532 case NAND_OP_DATA_IN_INSTR:
2533 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2534 instr->ctx.data.len,
2535 instr->ctx.data.force_8bit ?
2536 ", force 8-bit" : "");
2537 break;
2538 case NAND_OP_DATA_OUT_INSTR:
2539 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2540 instr->ctx.data.len,
2541 instr->ctx.data.force_8bit ?
2542 ", force 8-bit" : "");
2543 break;
2544 case NAND_OP_WAITRDY_INSTR:
2545 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2546 instr->ctx.waitrdy.timeout_ms);
2547 break;
2548 }
2549
2550 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2551 prefix = " ";
2552 }
2553}
2554#else
2555static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2556{
2557 /* NOP */
2558}
2559#endif
2560
2561/**
2562 * nand_op_parser_exec_op - exec_op parser
2563 * @chip: the NAND chip
2564 * @parser: patterns description provided by the controller driver
2565 * @op: the NAND operation to address
2566 * @check_only: when true, the function only checks if @op can be handled but
2567 * does not execute the operation
2568 *
2569 * Helper function designed to ease integration of NAND controller drivers that
2570 * only support a limited set of instruction sequences. The supported sequences
2571 * are described in @parser, and the framework takes care of splitting @op into
2572 * multiple sub-operations (if required) and pass them back to the ->exec()
2573 * callback of the matching pattern if @check_only is set to false.
2574 *
2575 * NAND controller drivers should call this function from their own ->exec_op()
2576 * implementation.
2577 *
2578 * Returns 0 on success, a negative error code otherwise. A failure can be
2579 * caused by an unsupported operation (none of the supported patterns is able
2580 * to handle the requested operation), or an error returned by one of the
2581 * matching pattern->exec() hook.
2582 */
2583int nand_op_parser_exec_op(struct nand_chip *chip,
2584 const struct nand_op_parser *parser,
2585 const struct nand_operation *op, bool check_only)
2586{
2587 struct nand_op_parser_ctx ctx = {
2588 .subop.instrs = op->instrs,
2589 .instrs = op->instrs,
2590 .ninstrs = op->ninstrs,
2591 };
2592 unsigned int i;
2593
2594 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2595 int ret;
2596
2597 for (i = 0; i < parser->npatterns; i++) {
2598 const struct nand_op_parser_pattern *pattern;
2599
2600 pattern = &parser->patterns[i];
2601 if (!nand_op_parser_match_pat(pattern, &ctx))
2602 continue;
2603
2604 nand_op_parser_trace(&ctx);
2605
2606 if (check_only)
2607 break;
2608
2609 ret = pattern->exec(chip, &ctx.subop);
2610 if (ret)
2611 return ret;
2612
2613 break;
2614 }
2615
2616 if (i == parser->npatterns) {
2617 pr_debug("->exec_op() parser: pattern not found!\n");
2618 return -ENOTSUPP;
2619 }
2620
2621 /*
2622 * Update the context structure by pointing to the start of the
2623 * next subop.
2624 */
2625 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2626 if (ctx.subop.last_instr_end_off)
2627 ctx.subop.instrs -= 1;
2628
2629 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2630 }
2631
2632 return 0;
2633}
2634EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2635
2636static bool nand_instr_is_data(const struct nand_op_instr *instr)
2637{
2638 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2639 instr->type == NAND_OP_DATA_OUT_INSTR);
2640}
2641
2642static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2643 unsigned int instr_idx)
2644{
2645 return subop && instr_idx < subop->ninstrs;
2646}
2647
Miquel Raynal760c4352018-07-19 00:09:12 +02002648static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2649 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002650{
2651 if (instr_idx)
2652 return 0;
2653
2654 return subop->first_instr_start_off;
2655}
2656
2657/**
2658 * nand_subop_get_addr_start_off - Get the start offset in an address array
2659 * @subop: The entire sub-operation
2660 * @instr_idx: Index of the instruction inside the sub-operation
2661 *
2662 * During driver development, one could be tempted to directly use the
2663 * ->addr.addrs field of address instructions. This is wrong as address
2664 * instructions might be split.
2665 *
2666 * Given an address instruction, returns the offset of the first cycle to issue.
2667 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002668unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2669 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002670{
Miquel Raynal760c4352018-07-19 00:09:12 +02002671 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2672 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2673 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002674
2675 return nand_subop_get_start_off(subop, instr_idx);
2676}
2677EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2678
2679/**
2680 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2681 * @subop: The entire sub-operation
2682 * @instr_idx: Index of the instruction inside the sub-operation
2683 *
2684 * During driver development, one could be tempted to directly use the
2685 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2686 * might be split.
2687 *
2688 * Given an address instruction, returns the number of address cycle to issue.
2689 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002690unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2691 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002692{
2693 int start_off, end_off;
2694
Miquel Raynal760c4352018-07-19 00:09:12 +02002695 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2696 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2697 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002698
2699 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2700
2701 if (instr_idx == subop->ninstrs - 1 &&
2702 subop->last_instr_end_off)
2703 end_off = subop->last_instr_end_off;
2704 else
2705 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2706
2707 return end_off - start_off;
2708}
2709EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2710
2711/**
2712 * nand_subop_get_data_start_off - Get the start offset in a data array
2713 * @subop: The entire sub-operation
2714 * @instr_idx: Index of the instruction inside the sub-operation
2715 *
2716 * During driver development, one could be tempted to directly use the
2717 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2718 * instructions might be split.
2719 *
2720 * Given a data instruction, returns the offset to start from.
2721 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002722unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2723 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002724{
Miquel Raynal760c4352018-07-19 00:09:12 +02002725 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2726 !nand_instr_is_data(&subop->instrs[instr_idx])))
2727 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002728
2729 return nand_subop_get_start_off(subop, instr_idx);
2730}
2731EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2732
2733/**
2734 * nand_subop_get_data_len - Get the number of bytes to retrieve
2735 * @subop: The entire sub-operation
2736 * @instr_idx: Index of the instruction inside the sub-operation
2737 *
2738 * During driver development, one could be tempted to directly use the
2739 * ->data->len field of a data instruction. This is wrong as data instructions
2740 * might be split.
2741 *
2742 * Returns the length of the chunk of data to send/receive.
2743 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002744unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2745 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002746{
2747 int start_off = 0, end_off;
2748
Miquel Raynal760c4352018-07-19 00:09:12 +02002749 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2750 !nand_instr_is_data(&subop->instrs[instr_idx])))
2751 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002752
2753 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2754
2755 if (instr_idx == subop->ninstrs - 1 &&
2756 subop->last_instr_end_off)
2757 end_off = subop->last_instr_end_off;
2758 else
2759 end_off = subop->instrs[instr_idx].ctx.data.len;
2760
2761 return end_off - start_off;
2762}
2763EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002766 * nand_reset - Reset and initialize a NAND device
2767 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002768 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002769 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002770 * Save the timings data structure, then apply SDR timings mode 0 (see
2771 * nand_reset_data_interface for details), do the reset operation, and
2772 * apply back the previous timings.
2773 *
2774 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002775 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002776int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002777{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002778 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002779 int ret;
2780
Boris Brezillon104e4422017-03-16 09:35:58 +01002781 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002782 if (ret)
2783 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002784
Boris Brezillon73f907f2016-10-24 16:46:20 +02002785 /*
2786 * The CS line has to be released before we can apply the new NAND
2787 * interface settings, hence this weird ->select_chip() dance.
2788 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002789 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002790 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002791 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002792 if (ret)
2793 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002794
Miquel Raynal107b7d62018-03-19 14:47:25 +01002795 /*
2796 * A nand_reset_data_interface() put both the NAND chip and the NAND
2797 * controller in timings mode 0. If the default mode for this chip is
2798 * also 0, no need to proceed to the change again. Plus, at probe time,
2799 * nand_setup_data_interface() uses ->set/get_features() which would
2800 * fail anyway as the parameter page is not available yet.
2801 */
2802 if (!chip->onfi_timing_mode_default)
2803 return 0;
2804
Miquel Raynal17fa8042017-11-30 18:01:31 +01002805 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002806 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002807 if (ret)
2808 return ret;
2809
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002810 return 0;
2811}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002812EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002813
2814/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002815 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2816 * @buf: buffer to test
2817 * @len: buffer length
2818 * @bitflips_threshold: maximum number of bitflips
2819 *
2820 * Check if a buffer contains only 0xff, which means the underlying region
2821 * has been erased and is ready to be programmed.
2822 * The bitflips_threshold specify the maximum number of bitflips before
2823 * considering the region is not erased.
2824 * Note: The logic of this function has been extracted from the memweight
2825 * implementation, except that nand_check_erased_buf function exit before
2826 * testing the whole buffer if the number of bitflips exceed the
2827 * bitflips_threshold value.
2828 *
2829 * Returns a positive number of bitflips less than or equal to
2830 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2831 * threshold.
2832 */
2833static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2834{
2835 const unsigned char *bitmap = buf;
2836 int bitflips = 0;
2837 int weight;
2838
2839 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2840 len--, bitmap++) {
2841 weight = hweight8(*bitmap);
2842 bitflips += BITS_PER_BYTE - weight;
2843 if (unlikely(bitflips > bitflips_threshold))
2844 return -EBADMSG;
2845 }
2846
2847 for (; len >= sizeof(long);
2848 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002849 unsigned long d = *((unsigned long *)bitmap);
2850 if (d == ~0UL)
2851 continue;
2852 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002853 bitflips += BITS_PER_LONG - weight;
2854 if (unlikely(bitflips > bitflips_threshold))
2855 return -EBADMSG;
2856 }
2857
2858 for (; len > 0; len--, bitmap++) {
2859 weight = hweight8(*bitmap);
2860 bitflips += BITS_PER_BYTE - weight;
2861 if (unlikely(bitflips > bitflips_threshold))
2862 return -EBADMSG;
2863 }
2864
2865 return bitflips;
2866}
2867
2868/**
2869 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2870 * 0xff data
2871 * @data: data buffer to test
2872 * @datalen: data length
2873 * @ecc: ECC buffer
2874 * @ecclen: ECC length
2875 * @extraoob: extra OOB buffer
2876 * @extraooblen: extra OOB length
2877 * @bitflips_threshold: maximum number of bitflips
2878 *
2879 * Check if a data buffer and its associated ECC and OOB data contains only
2880 * 0xff pattern, which means the underlying region has been erased and is
2881 * ready to be programmed.
2882 * The bitflips_threshold specify the maximum number of bitflips before
2883 * considering the region as not erased.
2884 *
2885 * Note:
2886 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2887 * different from the NAND page size. When fixing bitflips, ECC engines will
2888 * report the number of errors per chunk, and the NAND core infrastructure
2889 * expect you to return the maximum number of bitflips for the whole page.
2890 * This is why you should always use this function on a single chunk and
2891 * not on the whole page. After checking each chunk you should update your
2892 * max_bitflips value accordingly.
2893 * 2/ When checking for bitflips in erased pages you should not only check
2894 * the payload data but also their associated ECC data, because a user might
2895 * have programmed almost all bits to 1 but a few. In this case, we
2896 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2897 * this case.
2898 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2899 * data are protected by the ECC engine.
2900 * It could also be used if you support subpages and want to attach some
2901 * extra OOB data to an ECC chunk.
2902 *
2903 * Returns a positive number of bitflips less than or equal to
2904 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2905 * threshold. In case of success, the passed buffers are filled with 0xff.
2906 */
2907int nand_check_erased_ecc_chunk(void *data, int datalen,
2908 void *ecc, int ecclen,
2909 void *extraoob, int extraooblen,
2910 int bitflips_threshold)
2911{
2912 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2913
2914 data_bitflips = nand_check_erased_buf(data, datalen,
2915 bitflips_threshold);
2916 if (data_bitflips < 0)
2917 return data_bitflips;
2918
2919 bitflips_threshold -= data_bitflips;
2920
2921 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2922 if (ecc_bitflips < 0)
2923 return ecc_bitflips;
2924
2925 bitflips_threshold -= ecc_bitflips;
2926
2927 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2928 bitflips_threshold);
2929 if (extraoob_bitflips < 0)
2930 return extraoob_bitflips;
2931
2932 if (data_bitflips)
2933 memset(data, 0xff, datalen);
2934
2935 if (ecc_bitflips)
2936 memset(ecc, 0xff, ecclen);
2937
2938 if (extraoob_bitflips)
2939 memset(extraoob, 0xff, extraooblen);
2940
2941 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2942}
2943EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2944
2945/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002946 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002947 * @chip: nand chip info structure
2948 * @buf: buffer to store read data
2949 * @oob_required: caller requires OOB data read to chip->oob_poi
2950 * @page: page number to read
2951 *
2952 * Returns -ENOTSUPP unconditionally.
2953 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002954int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2955 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002956{
2957 return -ENOTSUPP;
2958}
2959EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2960
2961/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002962 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002963 * @chip: nand chip info structure
2964 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002965 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002966 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002967 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002968 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002969 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002970int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2971 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002972{
Boris Brezillonb9761682018-09-06 14:05:20 +02002973 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002974 int ret;
2975
Boris Brezillon25f815f2017-11-30 18:01:30 +01002976 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002977 if (ret)
2978 return ret;
2979
2980 if (oob_required) {
2981 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2982 false);
2983 if (ret)
2984 return ret;
2985 }
2986
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002987 return 0;
2988}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002989EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002990
2991/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002992 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002993 * @chip: nand chip info structure
2994 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002995 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002996 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002997 *
2998 * We need a special oob layout and handling even when OOB isn't used.
2999 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003000static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003001 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003002{
Boris Brezillonb9761682018-09-06 14:05:20 +02003003 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003004 int eccsize = chip->ecc.size;
3005 int eccbytes = chip->ecc.bytes;
3006 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003007 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003008
Boris Brezillon25f815f2017-11-30 18:01:30 +01003009 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3010 if (ret)
3011 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003012
3013 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003014 ret = nand_read_data_op(chip, buf, eccsize, false);
3015 if (ret)
3016 return ret;
3017
David Brownell52ff49d2009-03-04 12:01:36 -08003018 buf += eccsize;
3019
3020 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003021 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3022 false);
3023 if (ret)
3024 return ret;
3025
David Brownell52ff49d2009-03-04 12:01:36 -08003026 oob += chip->ecc.prepad;
3027 }
3028
Boris Brezillon97d90da2017-11-30 18:01:29 +01003029 ret = nand_read_data_op(chip, oob, eccbytes, false);
3030 if (ret)
3031 return ret;
3032
David Brownell52ff49d2009-03-04 12:01:36 -08003033 oob += eccbytes;
3034
3035 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003036 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3037 false);
3038 if (ret)
3039 return ret;
3040
David Brownell52ff49d2009-03-04 12:01:36 -08003041 oob += chip->ecc.postpad;
3042 }
3043 }
3044
3045 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003046 if (size) {
3047 ret = nand_read_data_op(chip, oob, size, false);
3048 if (ret)
3049 return ret;
3050 }
David Brownell52ff49d2009-03-04 12:01:36 -08003051
3052 return 0;
3053}
3054
3055/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003056 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003057 * @chip: nand chip info structure
3058 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003059 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003060 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003061 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003062static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3063 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Boris Brezillonb9761682018-09-06 14:05:20 +02003065 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003066 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003067 int eccbytes = chip->ecc.bytes;
3068 int eccsteps = chip->ecc.steps;
3069 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003070 uint8_t *ecc_calc = chip->ecc.calc_buf;
3071 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003072 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003073
Boris Brezillonb9761682018-09-06 14:05:20 +02003074 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003075
3076 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003077 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003078
Boris Brezillon846031d2016-02-03 20:11:00 +01003079 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3080 chip->ecc.total);
3081 if (ret)
3082 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003083
3084 eccsteps = chip->ecc.steps;
3085 p = buf;
3086
3087 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3088 int stat;
3089
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003090 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003091 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003092 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003093 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003094 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003095 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3096 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003097 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003098 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303102 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003103 * @chip: nand chip info structure
3104 * @data_offs: offset of requested data within the page
3105 * @readlen: data length
3106 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003107 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003108 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003109static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3110 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003111{
Boris Brezillonb9761682018-09-06 14:05:20 +02003112 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003113 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003114 uint8_t *p;
3115 int data_col_addr, i, gaps = 0;
3116 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3117 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003118 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003119 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003120 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003121
Brian Norris7854d3f2011-06-23 14:12:08 -07003122 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003123 start_step = data_offs / chip->ecc.size;
3124 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3125 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303126 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003127
Brian Norris8b6e50c2011-05-25 14:59:01 -07003128 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003129 datafrag_len = num_steps * chip->ecc.size;
3130 eccfrag_len = num_steps * chip->ecc.bytes;
3131
3132 data_col_addr = start_step * chip->ecc.size;
3133 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003134 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003135 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003136 if (ret)
3137 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003138
Brian Norris8b6e50c2011-05-25 14:59:01 -07003139 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003140 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003141 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003142
Brian Norris8b6e50c2011-05-25 14:59:01 -07003143 /*
3144 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003145 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003146 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003147 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3148 if (ret)
3149 return ret;
3150
3151 if (oobregion.length < eccfrag_len)
3152 gaps = 1;
3153
Alexey Korolev3d459552008-05-15 17:23:18 +01003154 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003155 ret = nand_change_read_column_op(chip, mtd->writesize,
3156 chip->oob_poi, mtd->oobsize,
3157 false);
3158 if (ret)
3159 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003160 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003162 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003163 * about buswidth alignment in read_buf.
3164 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003165 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003166 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003167 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003168 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003169 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3170 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003171 aligned_len++;
3172
Boris Brezillon97d90da2017-11-30 18:01:29 +01003173 ret = nand_change_read_column_op(chip,
3174 mtd->writesize + aligned_pos,
3175 &chip->oob_poi[aligned_pos],
3176 aligned_len, false);
3177 if (ret)
3178 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003179 }
3180
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003181 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003182 chip->oob_poi, index, eccfrag_len);
3183 if (ret)
3184 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003185
3186 p = bufpoi + data_col_addr;
3187 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3188 int stat;
3189
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003190 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003191 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003192 if (stat == -EBADMSG &&
3193 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3194 /* check for empty pages with bitflips */
3195 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003196 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003197 chip->ecc.bytes,
3198 NULL, 0,
3199 chip->ecc.strength);
3200 }
3201
Mike Dunn3f91e942012-04-25 12:06:09 -07003202 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003203 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003204 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003205 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003206 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3207 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003208 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003209 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003210}
3211
3212/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003213 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003214 * @chip: nand chip info structure
3215 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003216 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003217 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003218 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003219 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003220 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003221static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3222 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003223{
Boris Brezillonb9761682018-09-06 14:05:20 +02003224 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003225 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003226 int eccbytes = chip->ecc.bytes;
3227 int eccsteps = chip->ecc.steps;
3228 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003229 uint8_t *ecc_calc = chip->ecc.calc_buf;
3230 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003231 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003232
Boris Brezillon25f815f2017-11-30 18:01:30 +01003233 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3234 if (ret)
3235 return ret;
3236
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003237 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003238 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003239
3240 ret = nand_read_data_op(chip, p, eccsize, false);
3241 if (ret)
3242 return ret;
3243
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003244 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003245 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003246
3247 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3248 if (ret)
3249 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003250
Boris Brezillon846031d2016-02-03 20:11:00 +01003251 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3252 chip->ecc.total);
3253 if (ret)
3254 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003255
3256 eccsteps = chip->ecc.steps;
3257 p = buf;
3258
3259 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3260 int stat;
3261
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003262 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003263 if (stat == -EBADMSG &&
3264 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3265 /* check for empty pages with bitflips */
3266 stat = nand_check_erased_ecc_chunk(p, eccsize,
3267 &ecc_code[i], eccbytes,
3268 NULL, 0,
3269 chip->ecc.strength);
3270 }
3271
Mike Dunn3f91e942012-04-25 12:06:09 -07003272 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003273 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003274 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003275 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003276 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3277 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003278 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003279 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003280}
3281
3282/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003283 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * @chip: nand chip info structure
3285 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003286 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003287 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003288 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003289 * Hardware ECC for large page chips, require OOB to be read first. For this
3290 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3291 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3292 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3293 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003294 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003295static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3296 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003297{
Boris Brezillonb9761682018-09-06 14:05:20 +02003298 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003299 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003300 int eccbytes = chip->ecc.bytes;
3301 int eccsteps = chip->ecc.steps;
3302 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003303 uint8_t *ecc_code = chip->ecc.code_buf;
3304 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003305 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003306
3307 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003308 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3309 if (ret)
3310 return ret;
3311
3312 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3313 if (ret)
3314 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003315
Boris Brezillon846031d2016-02-03 20:11:00 +01003316 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3317 chip->ecc.total);
3318 if (ret)
3319 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003320
3321 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3322 int stat;
3323
Boris Brezillonec476362018-09-06 14:05:17 +02003324 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003325
3326 ret = nand_read_data_op(chip, p, eccsize, false);
3327 if (ret)
3328 return ret;
3329
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003330 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003331
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003332 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003333 if (stat == -EBADMSG &&
3334 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3335 /* check for empty pages with bitflips */
3336 stat = nand_check_erased_ecc_chunk(p, eccsize,
3337 &ecc_code[i], eccbytes,
3338 NULL, 0,
3339 chip->ecc.strength);
3340 }
3341
Mike Dunn3f91e942012-04-25 12:06:09 -07003342 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003343 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003344 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003345 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003346 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3347 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003348 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003349 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003350}
3351
3352/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003353 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003354 * @chip: nand chip info structure
3355 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003356 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003357 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003358 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003359 * The hw generator calculates the error syndrome automatically. Therefore we
3360 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003361 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003362static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3363 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003364{
Boris Brezillonb9761682018-09-06 14:05:20 +02003365 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003366 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003367 int eccbytes = chip->ecc.bytes;
3368 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003369 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003370 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003371 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003372 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003373
Boris Brezillon25f815f2017-11-30 18:01:30 +01003374 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3375 if (ret)
3376 return ret;
3377
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003378 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3379 int stat;
3380
Boris Brezillonec476362018-09-06 14:05:17 +02003381 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003382
3383 ret = nand_read_data_op(chip, p, eccsize, false);
3384 if (ret)
3385 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386
3387 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003388 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3389 false);
3390 if (ret)
3391 return ret;
3392
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003393 oob += chip->ecc.prepad;
3394 }
3395
Boris Brezillonec476362018-09-06 14:05:17 +02003396 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003397
3398 ret = nand_read_data_op(chip, oob, eccbytes, false);
3399 if (ret)
3400 return ret;
3401
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003402 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003403
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404 oob += eccbytes;
3405
3406 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003407 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3408 false);
3409 if (ret)
3410 return ret;
3411
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003412 oob += chip->ecc.postpad;
3413 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003414
3415 if (stat == -EBADMSG &&
3416 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3417 /* check for empty pages with bitflips */
3418 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3419 oob - eccpadbytes,
3420 eccpadbytes,
3421 NULL, 0,
3422 chip->ecc.strength);
3423 }
3424
3425 if (stat < 0) {
3426 mtd->ecc_stats.failed++;
3427 } else {
3428 mtd->ecc_stats.corrected += stat;
3429 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3430 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003431 }
3432
3433 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003434 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003435 if (i) {
3436 ret = nand_read_data_op(chip, oob, i, false);
3437 if (ret)
3438 return ret;
3439 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003440
Mike Dunn3f91e942012-04-25 12:06:09 -07003441 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003442}
3443
3444/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003445 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003446 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003447 * @oob: oob destination address
3448 * @ops: oob ops structure
3449 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003450 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003451static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003452 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003453{
Boris Brezillon846031d2016-02-03 20:11:00 +01003454 struct nand_chip *chip = mtd_to_nand(mtd);
3455 int ret;
3456
Florian Fainellif8ac0412010-09-07 13:23:43 +02003457 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003458
Brian Norris0612b9d2011-08-30 18:45:40 -07003459 case MTD_OPS_PLACE_OOB:
3460 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003461 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3462 return oob + len;
3463
Boris Brezillon846031d2016-02-03 20:11:00 +01003464 case MTD_OPS_AUTO_OOB:
3465 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3466 ops->ooboffs, len);
3467 BUG_ON(ret);
3468 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003469
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003470 default:
3471 BUG();
3472 }
3473 return NULL;
3474}
3475
3476/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003477 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
3478 * @mtd: MTD device structure
3479 * @retry_mode: the retry mode to use
3480 *
3481 * Some vendors supply a special command to shift the Vt threshold, to be used
3482 * when there are too many bitflips in a page (i.e., ECC error). After setting
3483 * a new threshold, the host should retry reading the page.
3484 */
3485static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
3486{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003487 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08003488
3489 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3490
3491 if (retry_mode >= chip->read_retries)
3492 return -EINVAL;
3493
3494 if (!chip->setup_read_retry)
3495 return -EOPNOTSUPP;
3496
3497 return chip->setup_read_retry(mtd, retry_mode);
3498}
3499
Boris Brezillon85e08e52018-07-27 09:44:17 +02003500static void nand_wait_readrdy(struct nand_chip *chip)
3501{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003502 const struct nand_sdr_timings *sdr;
3503
Boris Brezillon85e08e52018-07-27 09:44:17 +02003504 if (!(chip->options & NAND_NEED_READRDY))
3505 return;
3506
Boris Brezillon52f05b62018-07-27 09:44:18 +02003507 sdr = nand_get_sdr_timings(&chip->data_interface);
3508 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003509}
3510
Brian Norrisba84fb52014-01-03 15:13:33 -08003511/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003512 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003513 * @mtd: MTD device structure
3514 * @from: offset to read from
3515 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003516 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003517 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003518 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003519static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3520 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003521{
Brian Norrise47f3db2012-05-02 10:14:56 -07003522 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003523 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003524 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003525 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003526 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003527 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003528
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003529 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003530 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003531 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003532 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003533 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003535 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003536 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003538 realpage = (int)(from >> chip->page_shift);
3539 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003541 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003543 buf = ops->datbuf;
3544 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003545 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003546
Florian Fainellif8ac0412010-09-07 13:23:43 +02003547 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003548 unsigned int ecc_failures = mtd->ecc_stats.failed;
3549
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003550 bytes = min(mtd->writesize - col, readlen);
3551 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003552
Kamal Dasu66507c72014-05-01 20:51:19 -04003553 if (!aligned)
3554 use_bufpoi = 1;
3555 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003556 use_bufpoi = !virt_addr_valid(buf) ||
3557 !IS_ALIGNED((unsigned long)buf,
3558 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003559 else
3560 use_bufpoi = 0;
3561
Brian Norris8b6e50c2011-05-25 14:59:01 -07003562 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003563 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003564 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003565
3566 if (use_bufpoi && aligned)
3567 pr_debug("%s: using read bounce buffer for buf@%p\n",
3568 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Brian Norrisba84fb52014-01-03 15:13:33 -08003570read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003571 /*
3572 * Now read the page into the buffer. Absent an error,
3573 * the read methods return max bitflips per ecc step.
3574 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003575 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003576 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003577 oob_required,
3578 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003579 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3580 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003581 ret = chip->ecc.read_subpage(chip, col, bytes,
3582 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003583 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003584 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003585 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003586 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003587 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003588 /* Invalidate page cache */
3589 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003590 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003591 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003592
3593 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003594 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003595 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003596 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003597 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003598 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003599 chip->pagebuf_bitflips = ret;
3600 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003601 /* Invalidate page cache */
3602 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003603 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003604 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003606
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003607 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003608 int toread = min(oobreadlen, max_oobsize);
3609
3610 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003611 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003612 oob, ops, toread);
3613 oobreadlen -= toread;
3614 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003615 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003616
Boris Brezillon85e08e52018-07-27 09:44:17 +02003617 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003618
Brian Norrisba84fb52014-01-03 15:13:33 -08003619 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003620 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003621 retry_mode++;
3622 ret = nand_setup_read_retry(mtd,
3623 retry_mode);
3624 if (ret < 0)
3625 break;
3626
3627 /* Reset failures; retry */
3628 mtd->ecc_stats.failed = ecc_failures;
3629 goto read_retry;
3630 } else {
3631 /* No more retry modes; real failure */
3632 ecc_fail = true;
3633 }
3634 }
3635
3636 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003637 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003638 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003639 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003640 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003641 max_bitflips = max_t(unsigned int, max_bitflips,
3642 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003645 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003646
Brian Norrisba84fb52014-01-03 15:13:33 -08003647 /* Reset to retry mode 0 */
3648 if (retry_mode) {
3649 ret = nand_setup_read_retry(mtd, 0);
3650 if (ret < 0)
3651 break;
3652 retry_mode = 0;
3653 }
3654
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003655 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Brian Norris8b6e50c2011-05-25 14:59:01 -07003658 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 col = 0;
3660 /* Increment page address */
3661 realpage++;
3662
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003663 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 /* Check, if we cross a chip boundary */
3665 if (!page) {
3666 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003667 chip->select_chip(chip, -1);
3668 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003671 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003673 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003674 if (oob)
3675 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Mike Dunn3f91e942012-04-25 12:06:09 -07003677 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003678 return ret;
3679
Brian Norrisb72f3df2013-12-03 11:04:14 -08003680 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003681 return -EBADMSG;
3682
Mike Dunnedbc45402012-04-25 12:06:11 -07003683 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003684}
3685
3686/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003687 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003688 * @chip: nand chip info structure
3689 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003690 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003691int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003692{
Boris Brezillonb9761682018-09-06 14:05:20 +02003693 struct mtd_info *mtd = nand_to_mtd(chip);
3694
Boris Brezillon97d90da2017-11-30 18:01:29 +01003695 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003696}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003697EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003698
3699/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003700 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003701 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003702 * @chip: nand chip info structure
3703 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003704 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003705int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003706{
Boris Brezillonb9761682018-09-06 14:05:20 +02003707 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003708 int length = mtd->oobsize;
3709 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3710 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003711 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003712 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003713
Boris Brezillon97d90da2017-11-30 18:01:29 +01003714 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3715 if (ret)
3716 return ret;
3717
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718 for (i = 0; i < chip->ecc.steps; i++) {
3719 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003720 int ret;
3721
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003722 pos = eccsize + i * (eccsize + chunk);
3723 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003724 ret = nand_change_read_column_op(chip, pos,
3725 NULL, 0,
3726 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003727 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003728 ret = nand_read_page_op(chip, page, pos, NULL,
3729 0);
3730
3731 if (ret)
3732 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733 } else
3734 sndrnd = 1;
3735 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003736
3737 ret = nand_read_data_op(chip, bufpoi, toread, false);
3738 if (ret)
3739 return ret;
3740
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003741 bufpoi += toread;
3742 length -= toread;
3743 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003744 if (length > 0) {
3745 ret = nand_read_data_op(chip, bufpoi, length, false);
3746 if (ret)
3747 return ret;
3748 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003749
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003750 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003751}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003752EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003753
3754/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003755 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003756 * @chip: nand chip info structure
3757 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003758 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003759int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003761 struct mtd_info *mtd = nand_to_mtd(chip);
3762
Boris Brezillon97d90da2017-11-30 18:01:29 +01003763 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3764 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003765}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003766EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003767
3768/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003769 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003770 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003771 * @chip: nand chip info structure
3772 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003773 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003774int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003775{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003776 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003777 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3778 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003779 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780 const uint8_t *bufpoi = chip->oob_poi;
3781
3782 /*
3783 * data-ecc-data-ecc ... ecc-oob
3784 * or
3785 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3786 */
3787 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3788 pos = steps * (eccsize + chunk);
3789 steps = 0;
3790 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003791 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003792
Boris Brezillon97d90da2017-11-30 18:01:29 +01003793 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3794 if (ret)
3795 return ret;
3796
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003797 for (i = 0; i < steps; i++) {
3798 if (sndcmd) {
3799 if (mtd->writesize <= 512) {
3800 uint32_t fill = 0xFFFFFFFF;
3801
3802 len = eccsize;
3803 while (len > 0) {
3804 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003805
3806 ret = nand_write_data_op(chip, &fill,
3807 num, false);
3808 if (ret)
3809 return ret;
3810
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003811 len -= num;
3812 }
3813 } else {
3814 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003815 ret = nand_change_write_column_op(chip, pos,
3816 NULL, 0,
3817 false);
3818 if (ret)
3819 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003820 }
3821 } else
3822 sndcmd = 1;
3823 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003824
3825 ret = nand_write_data_op(chip, bufpoi, len, false);
3826 if (ret)
3827 return ret;
3828
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003829 bufpoi += len;
3830 length -= len;
3831 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003832 if (length > 0) {
3833 ret = nand_write_data_op(chip, bufpoi, length, false);
3834 if (ret)
3835 return ret;
3836 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003837
Boris Brezillon97d90da2017-11-30 18:01:29 +01003838 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003839}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003840EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003841
3842/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003843 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003844 * @mtd: MTD device structure
3845 * @from: offset to read from
3846 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003848 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003850static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3851 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003853 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003854 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003855 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003856 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003857 int readlen = ops->ooblen;
3858 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003859 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003860 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Brian Norris289c0522011-07-19 10:06:09 -07003862 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303863 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Brian Norris041e4572011-06-23 16:45:24 -07003865 stats = mtd->ecc_stats;
3866
Boris BREZILLON29f10582016-03-07 10:46:52 +01003867 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003868
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003869 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003870 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003872 /* Shift to get page */
3873 realpage = (int)(from >> chip->page_shift);
3874 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Florian Fainellif8ac0412010-09-07 13:23:43 +02003876 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003877 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003878 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003879 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003880 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003881
3882 if (ret < 0)
3883 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003884
3885 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003886 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003887
Boris Brezillon85e08e52018-07-27 09:44:17 +02003888 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003889
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003890 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3891
Vitaly Wool70145682006-11-03 18:20:38 +03003892 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003893 if (!readlen)
3894 break;
3895
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003896 /* Increment page address */
3897 realpage++;
3898
3899 page = realpage & chip->pagemask;
3900 /* Check, if we cross a chip boundary */
3901 if (!page) {
3902 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003903 chip->select_chip(chip, -1);
3904 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003907 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003909 ops->oobretlen = ops->ooblen - readlen;
3910
3911 if (ret < 0)
3912 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003913
3914 if (mtd->ecc_stats.failed - stats.failed)
3915 return -EBADMSG;
3916
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003917 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
3920/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003921 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003922 * @mtd: MTD device structure
3923 * @from: offset to read from
3924 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003926 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003928static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3929 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003931 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003932
3933 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003935 if (ops->mode != MTD_OPS_PLACE_OOB &&
3936 ops->mode != MTD_OPS_AUTO_OOB &&
3937 ops->mode != MTD_OPS_RAW)
3938 return -ENOTSUPP;
3939
Huang Shijie6a8214a2012-11-19 14:43:30 +08003940 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003942 if (!ops->datbuf)
3943 ret = nand_do_read_oob(mtd, from, ops);
3944 else
3945 ret = nand_do_read_ops(mtd, from, ops);
3946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003948 return ret;
3949}
3950
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003951/**
3952 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003953 * @chip: nand chip info structure
3954 * @buf: data buffer
3955 * @oob_required: must write chip->oob_poi to OOB
3956 * @page: page number to write
3957 *
3958 * Returns -ENOTSUPP unconditionally.
3959 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003960int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3961 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003962{
3963 return -ENOTSUPP;
3964}
3965EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003966
3967/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003968 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003969 * @chip: nand chip info structure
3970 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003971 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003972 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003973 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003974 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003975 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003976int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3977 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003978{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003979 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003980 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003981
Boris Brezillon25f815f2017-11-30 18:01:30 +01003982 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003983 if (ret)
3984 return ret;
3985
3986 if (oob_required) {
3987 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3988 false);
3989 if (ret)
3990 return ret;
3991 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003992
Boris Brezillon25f815f2017-11-30 18:01:30 +01003993 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003995EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003997/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003998 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003999 * @chip: nand chip info structure
4000 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004001 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004002 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004003 *
4004 * We need a special oob layout and handling even when ECC isn't checked.
4005 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004006static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004007 const uint8_t *buf, int oob_required,
4008 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004009{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004010 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004011 int eccsize = chip->ecc.size;
4012 int eccbytes = chip->ecc.bytes;
4013 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004014 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004015
Boris Brezillon25f815f2017-11-30 18:01:30 +01004016 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4017 if (ret)
4018 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004019
4020 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004021 ret = nand_write_data_op(chip, buf, eccsize, false);
4022 if (ret)
4023 return ret;
4024
David Brownell52ff49d2009-03-04 12:01:36 -08004025 buf += eccsize;
4026
4027 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004028 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4029 false);
4030 if (ret)
4031 return ret;
4032
David Brownell52ff49d2009-03-04 12:01:36 -08004033 oob += chip->ecc.prepad;
4034 }
4035
Boris Brezillon97d90da2017-11-30 18:01:29 +01004036 ret = nand_write_data_op(chip, oob, eccbytes, false);
4037 if (ret)
4038 return ret;
4039
David Brownell52ff49d2009-03-04 12:01:36 -08004040 oob += eccbytes;
4041
4042 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004043 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4044 false);
4045 if (ret)
4046 return ret;
4047
David Brownell52ff49d2009-03-04 12:01:36 -08004048 oob += chip->ecc.postpad;
4049 }
4050 }
4051
4052 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004053 if (size) {
4054 ret = nand_write_data_op(chip, oob, size, false);
4055 if (ret)
4056 return ret;
4057 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004058
Boris Brezillon25f815f2017-11-30 18:01:30 +01004059 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004060}
4061/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004062 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004063 * @chip: nand chip info structure
4064 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004065 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004066 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004067 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004068static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4069 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004070{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004071 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004072 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004073 int eccbytes = chip->ecc.bytes;
4074 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004075 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004076 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004077
Brian Norris7854d3f2011-06-23 14:12:08 -07004078 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004079 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004080 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004081
Boris Brezillon846031d2016-02-03 20:11:00 +01004082 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4083 chip->ecc.total);
4084 if (ret)
4085 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004086
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004087 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004088}
4089
4090/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004091 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004092 * @chip: nand chip info structure
4093 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004094 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004095 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004096 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004097static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4098 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004099{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004100 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004101 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004102 int eccbytes = chip->ecc.bytes;
4103 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004104 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004105 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004106
Boris Brezillon25f815f2017-11-30 18:01:30 +01004107 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4108 if (ret)
4109 return ret;
4110
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004111 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004112 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004113
4114 ret = nand_write_data_op(chip, p, eccsize, false);
4115 if (ret)
4116 return ret;
4117
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004118 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004119 }
4120
Boris Brezillon846031d2016-02-03 20:11:00 +01004121 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4122 chip->ecc.total);
4123 if (ret)
4124 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004125
Boris Brezillon97d90da2017-11-30 18:01:29 +01004126 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4127 if (ret)
4128 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004129
Boris Brezillon25f815f2017-11-30 18:01:30 +01004130 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004131}
4132
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304133
4134/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004135 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304136 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004137 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304138 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004139 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304140 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004141 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304142 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004143static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4144 uint32_t data_len, const uint8_t *buf,
4145 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304146{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004147 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304148 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004149 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304150 int ecc_size = chip->ecc.size;
4151 int ecc_bytes = chip->ecc.bytes;
4152 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304153 uint32_t start_step = offset / ecc_size;
4154 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4155 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004156 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304157
Boris Brezillon25f815f2017-11-30 18:01:30 +01004158 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4159 if (ret)
4160 return ret;
4161
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304162 for (step = 0; step < ecc_steps; step++) {
4163 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004164 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304165
4166 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004167 ret = nand_write_data_op(chip, buf, ecc_size, false);
4168 if (ret)
4169 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304170
4171 /* mask ECC of un-touched subpages by padding 0xFF */
4172 if ((step < start_step) || (step > end_step))
4173 memset(ecc_calc, 0xff, ecc_bytes);
4174 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004175 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304176
4177 /* mask OOB of un-touched subpages by padding 0xFF */
4178 /* if oob_required, preserve OOB metadata of written subpage */
4179 if (!oob_required || (step < start_step) || (step > end_step))
4180 memset(oob_buf, 0xff, oob_bytes);
4181
Brian Norrisd6a950802013-08-08 17:16:36 -07004182 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304183 ecc_calc += ecc_bytes;
4184 oob_buf += oob_bytes;
4185 }
4186
4187 /* copy calculated ECC for whole page to chip->buffer->oob */
4188 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004189 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004190 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4191 chip->ecc.total);
4192 if (ret)
4193 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304194
4195 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004196 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4197 if (ret)
4198 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304199
Boris Brezillon25f815f2017-11-30 18:01:30 +01004200 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304201}
4202
4203
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004204/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004205 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004206 * @chip: nand chip info structure
4207 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004208 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004209 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004210 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004211 * The hw generator calculates the error syndrome automatically. Therefore we
4212 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004213 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004214static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4215 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004216{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004217 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004218 int i, eccsize = chip->ecc.size;
4219 int eccbytes = chip->ecc.bytes;
4220 int eccsteps = chip->ecc.steps;
4221 const uint8_t *p = buf;
4222 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004223 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004224
Boris Brezillon25f815f2017-11-30 18:01:30 +01004225 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4226 if (ret)
4227 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004228
4229 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004230 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004231
4232 ret = nand_write_data_op(chip, p, eccsize, false);
4233 if (ret)
4234 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004235
4236 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004237 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4238 false);
4239 if (ret)
4240 return ret;
4241
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004242 oob += chip->ecc.prepad;
4243 }
4244
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004245 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004246
4247 ret = nand_write_data_op(chip, oob, eccbytes, false);
4248 if (ret)
4249 return ret;
4250
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004251 oob += eccbytes;
4252
4253 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004254 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4255 false);
4256 if (ret)
4257 return ret;
4258
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004259 oob += chip->ecc.postpad;
4260 }
4261 }
4262
4263 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004264 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004265 if (i) {
4266 ret = nand_write_data_op(chip, oob, i, false);
4267 if (ret)
4268 return ret;
4269 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004270
Boris Brezillon25f815f2017-11-30 18:01:30 +01004271 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004272}
4273
4274/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004275 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004276 * @mtd: MTD device structure
4277 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304278 * @offset: address offset within the page
4279 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004280 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004281 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004282 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004283 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004284 */
4285static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304286 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004287 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004288{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304289 int status, subpage;
4290
4291 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4292 chip->ecc.write_subpage)
4293 subpage = offset || (data_len < mtd->writesize);
4294 else
4295 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004296
David Woodhouse956e9442006-09-25 17:12:39 +01004297 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004298 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4299 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304300 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004301 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4302 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004303 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004304 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004305
4306 if (status < 0)
4307 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004308
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004309 return 0;
4310}
4311
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004312/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004313 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004314 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004315 * @oob: oob data buffer
4316 * @len: oob data write length
4317 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004318 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004319static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4320 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004321{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004322 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004323 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004324
4325 /*
4326 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4327 * data from a previous OOB read.
4328 */
4329 memset(chip->oob_poi, 0xff, mtd->oobsize);
4330
Florian Fainellif8ac0412010-09-07 13:23:43 +02004331 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004332
Brian Norris0612b9d2011-08-30 18:45:40 -07004333 case MTD_OPS_PLACE_OOB:
4334 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004335 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4336 return oob + len;
4337
Boris Brezillon846031d2016-02-03 20:11:00 +01004338 case MTD_OPS_AUTO_OOB:
4339 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4340 ops->ooboffs, len);
4341 BUG_ON(ret);
4342 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004343
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004344 default:
4345 BUG();
4346 }
4347 return NULL;
4348}
4349
Florian Fainellif8ac0412010-09-07 13:23:43 +02004350#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004351
4352/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004353 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004354 * @mtd: MTD device structure
4355 * @to: offset to write to
4356 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004357 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004358 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004359 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004360static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4361 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004362{
Corentin Labbe73600b62017-09-02 10:49:38 +02004363 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004364 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004365 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004366
4367 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004368 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004369
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004370 uint8_t *oob = ops->oobbuf;
4371 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304372 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004373 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004374
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004375 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004376 if (!writelen)
4377 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004378
Brian Norris8b6e50c2011-05-25 14:59:01 -07004379 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004380 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004381 pr_notice("%s: attempt to write non page aligned data\n",
4382 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004383 return -EINVAL;
4384 }
4385
Thomas Gleixner29072b92006-09-28 15:38:36 +02004386 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004387
Thomas Gleixner6a930962006-06-28 00:11:45 +02004388 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004389 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004390
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004391 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004392 if (nand_check_wp(mtd)) {
4393 ret = -EIO;
4394 goto err_out;
4395 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004397 realpage = (int)(to >> chip->page_shift);
4398 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004399
4400 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004401 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4402 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004403 chip->pagebuf = -1;
4404
Maxim Levitsky782ce792010-02-22 20:39:36 +02004405 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004406 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4407 ret = -EINVAL;
4408 goto err_out;
4409 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004410
Florian Fainellif8ac0412010-09-07 13:23:43 +02004411 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004412 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004413 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004414 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004415 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004416
Kamal Dasu66507c72014-05-01 20:51:19 -04004417 if (part_pagewr)
4418 use_bufpoi = 1;
4419 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004420 use_bufpoi = !virt_addr_valid(buf) ||
4421 !IS_ALIGNED((unsigned long)buf,
4422 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004423 else
4424 use_bufpoi = 0;
4425
4426 /* Partial page write?, or need to use bounce buffer */
4427 if (use_bufpoi) {
4428 pr_debug("%s: using write bounce buffer for buf@%p\n",
4429 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004430 if (part_pagewr)
4431 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004432 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004433 memset(chip->data_buf, 0xff, mtd->writesize);
4434 memcpy(&chip->data_buf[column], buf, bytes);
4435 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004436 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004437
Maxim Levitsky782ce792010-02-22 20:39:36 +02004438 if (unlikely(oob)) {
4439 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004440 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004441 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004442 } else {
4443 /* We still need to erase leftover OOB data */
4444 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004445 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004446
4447 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004448 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004449 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004450 if (ret)
4451 break;
4452
4453 writelen -= bytes;
4454 if (!writelen)
4455 break;
4456
Thomas Gleixner29072b92006-09-28 15:38:36 +02004457 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004458 buf += bytes;
4459 realpage++;
4460
4461 page = realpage & chip->pagemask;
4462 /* Check, if we cross a chip boundary */
4463 if (!page) {
4464 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004465 chip->select_chip(chip, -1);
4466 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004467 }
4468 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004469
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004470 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004471 if (unlikely(oob))
4472 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004473
4474err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004475 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004476 return ret;
4477}
4478
4479/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004480 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004481 * @mtd: MTD device structure
4482 * @to: offset to write to
4483 * @len: number of bytes to write
4484 * @retlen: pointer to variable to store the number of written bytes
4485 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004486 *
4487 * NAND write with ECC. Used when performing writes in interrupt context, this
4488 * may for example be called by mtdoops when writing an oops while in panic.
4489 */
4490static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4491 size_t *retlen, const uint8_t *buf)
4492{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004493 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004494 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004495 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004496 int ret;
4497
Brian Norris8b6e50c2011-05-25 14:59:01 -07004498 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004499 panic_nand_get_device(chip, mtd, FL_WRITING);
4500
Boris Brezillon758b56f2018-09-06 14:05:24 +02004501 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004502
4503 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004504 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004505
Brian Norris0ec56dc2015-02-28 02:02:30 -08004506 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004507 ops.len = len;
4508 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004509 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004510
Brian Norris4a89ff82011-08-30 18:45:45 -07004511 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004512
Brian Norris4a89ff82011-08-30 18:45:45 -07004513 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004514 return ret;
4515}
4516
4517/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004518 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004519 * @mtd: MTD device structure
4520 * @to: offset to write to
4521 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004522 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004523 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004524 */
4525static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4526 struct mtd_oob_ops *ops)
4527{
Adrian Hunter03736152007-01-31 17:58:29 +02004528 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004529 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Brian Norris289c0522011-07-19 10:06:09 -07004531 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304532 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
Boris BREZILLON29f10582016-03-07 10:46:52 +01004534 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004537 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004538 pr_debug("%s: attempt to write past end of page\n",
4539 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 return -EINVAL;
4541 }
4542
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004543 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004544
4545 /*
4546 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4547 * of my DiskOnChip 2000 test units) will clear the whole data page too
4548 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4549 * it in the doc2000 driver in August 1999. dwmw2.
4550 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004551 nand_reset(chip, chipnr);
4552
Boris Brezillon758b56f2018-09-06 14:05:24 +02004553 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004554
4555 /* Shift to get page */
4556 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
4558 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004559 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004560 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004561 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004562 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004565 if (page == chip->pagebuf)
4566 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004568 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004569
Brian Norris0612b9d2011-08-30 18:45:40 -07004570 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004571 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004572 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004573 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004574
Boris Brezillon758b56f2018-09-06 14:05:24 +02004575 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004576
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004577 if (status)
4578 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Vitaly Wool70145682006-11-03 18:20:38 +03004580 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004582 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004585/**
4586 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004587 * @mtd: MTD device structure
4588 * @to: offset to write to
4589 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004590 */
4591static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4592 struct mtd_oob_ops *ops)
4593{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004594 int ret = -ENOTSUPP;
4595
4596 ops->retlen = 0;
4597
Huang Shijie6a8214a2012-11-19 14:43:30 +08004598 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004599
Florian Fainellif8ac0412010-09-07 13:23:43 +02004600 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004601 case MTD_OPS_PLACE_OOB:
4602 case MTD_OPS_AUTO_OOB:
4603 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004604 break;
4605
4606 default:
4607 goto out;
4608 }
4609
4610 if (!ops->datbuf)
4611 ret = nand_do_write_oob(mtd, to, ops);
4612 else
4613 ret = nand_do_write_ops(mtd, to, ops);
4614
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004615out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004616 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return ret;
4618}
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620/**
Brian Norris49c50b92014-05-06 16:02:19 -07004621 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004622 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004623 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 *
Brian Norris49c50b92014-05-06 16:02:19 -07004625 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004627static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004629 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004632 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004633
Boris Brezillon97d90da2017-11-30 18:01:29 +01004634 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
4637/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004639 * @mtd: MTD device structure
4640 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004642 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004644static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
David Woodhousee0c7d762006-05-13 18:07:53 +01004646 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004650 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004651 * @mtd: MTD device structure
4652 * @instr: erase instruction
4653 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004655 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004657int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
4658 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Adrian Hunter69423d92008-12-10 13:37:21 +00004660 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004661 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00004662 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Brian Norris289c0522011-07-19 10:06:09 -07004664 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4665 __func__, (unsigned long long)instr->addr,
4666 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304668 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004672 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
4674 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004675 page = (int)(instr->addr >> chip->page_shift);
4676 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004679 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004682 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 /* Check, if it is write protected */
4685 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004686 pr_debug("%s: device is write protected!\n",
4687 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004688 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 goto erase_exit;
4690 }
4691
4692 /* Loop through the pages */
4693 len = instr->len;
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004696 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004697 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304698 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004699 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4700 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004701 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 goto erase_exit;
4703 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004704
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004705 /*
4706 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004707 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004708 */
4709 if (page <= chip->pagebuf && chip->pagebuf <
4710 (page + pages_per_block))
4711 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
Boris Brezillona2098a92018-09-06 14:05:30 +02004713 status = chip->erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004716 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004717 pr_debug("%s: failed erase, page 0x%08x\n",
4718 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004719 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004720 instr->fail_addr =
4721 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 goto erase_exit;
4723 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004724
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004726 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 page += pages_per_block;
4728
4729 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004730 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004732 chip->select_chip(chip, -1);
4733 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
4735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004737 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004738erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004741 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 nand_release_device(mtd);
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 /* Return more or less happy */
4745 return ret;
4746}
4747
4748/**
4749 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004750 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004752 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004754static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755{
Brian Norris289c0522011-07-19 10:06:09 -07004756 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004759 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004761 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004765 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004766 * @mtd: MTD device structure
4767 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004769static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304771 struct nand_chip *chip = mtd_to_nand(mtd);
4772 int chipnr = (int)(offs >> chip->chip_shift);
4773 int ret;
4774
4775 /* Select the NAND device */
4776 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004777 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304778
4779 ret = nand_block_checkbad(mtd, offs, 0);
4780
Boris Brezillon758b56f2018-09-06 14:05:24 +02004781 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304782 nand_release_device(mtd);
4783
4784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
4787/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004788 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004789 * @mtd: MTD device structure
4790 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004792static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 int ret;
4795
Florian Fainellif8ac0412010-09-07 13:23:43 +02004796 ret = nand_block_isbad(mtd, ofs);
4797 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004798 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (ret > 0)
4800 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004801 return ret;
4802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Brian Norris5a0edb22013-07-30 17:52:58 -07004804 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
4807/**
Zach Brown56718422017-01-10 13:30:20 -06004808 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4809 * @mtd: MTD device structure
4810 * @ofs: offset relative to mtd start
4811 * @len: length of mtd
4812 */
4813static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4814{
4815 struct nand_chip *chip = mtd_to_nand(mtd);
4816 u32 part_start_block;
4817 u32 part_end_block;
4818 u32 part_start_die;
4819 u32 part_end_die;
4820
4821 /*
4822 * max_bb_per_die and blocks_per_die used to determine
4823 * the maximum bad block count.
4824 */
4825 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4826 return -ENOTSUPP;
4827
4828 /* Get the start and end of the partition in erase blocks. */
4829 part_start_block = mtd_div_by_eb(ofs, mtd);
4830 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4831
4832 /* Get the start and end LUNs of the partition. */
4833 part_start_die = part_start_block / chip->blocks_per_die;
4834 part_end_die = part_end_block / chip->blocks_per_die;
4835
4836 /*
4837 * Look up the bad blocks per unit and multiply by the number of units
4838 * that the partition spans.
4839 */
4840 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4841}
4842
4843/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004844 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004845 * @chip: nand chip info structure
4846 * @addr: feature address.
4847 * @subfeature_param: the subfeature parameters, a four bytes array.
4848 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004849static int nand_default_set_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004850 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004851{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004852 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004853}
4854
4855/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004856 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004857 * @chip: nand chip info structure
4858 * @addr: feature address.
4859 * @subfeature_param: the subfeature parameters, a four bytes array.
4860 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004861static int nand_default_get_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004862 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004863{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004864 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004865}
4866
4867/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004868 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004869 * @chip: nand chip info structure
4870 * @addr: feature address.
4871 * @subfeature_param: the subfeature parameters, a four bytes array.
4872 *
4873 * Should be used by NAND controller drivers that do not support the SET/GET
4874 * FEATURES operations.
4875 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004876int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
4877 u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004878{
4879 return -ENOTSUPP;
4880}
Miquel Raynalb9587582018-03-19 14:47:19 +01004881EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004882
4883/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004884 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004885 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004886 */
4887static int nand_suspend(struct mtd_info *mtd)
4888{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004889 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004890}
4891
4892/**
4893 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004894 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004895 */
4896static void nand_resume(struct mtd_info *mtd)
4897{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004898 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004899
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004900 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004901 nand_release_device(mtd);
4902 else
Brian Norrisd0370212011-07-19 10:06:08 -07004903 pr_err("%s called for a chip which is not in suspended state\n",
4904 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004905}
4906
Scott Branden72ea4032014-11-20 11:18:05 -08004907/**
4908 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4909 * prevent further operations
4910 * @mtd: MTD device structure
4911 */
4912static void nand_shutdown(struct mtd_info *mtd)
4913{
Brian Norris9ca641b2015-11-09 16:37:28 -08004914 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004915}
4916
Brian Norris8b6e50c2011-05-25 14:59:01 -07004917/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004918static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004919{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004920 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004923 if (!chip->chip_delay)
4924 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
4926 /* check, if a user supplied command function given */
Miquel Raynal8878b122017-11-09 14:16:45 +01004927 if (!chip->cmdfunc && !chip->exec_op)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004928 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
4930 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004931 if (chip->waitfunc == NULL)
4932 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004934 if (!chip->select_chip)
4935 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004936
Huang Shijie4204ccc2013-08-16 10:10:07 +08004937 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004938 if (!chip->set_features)
4939 chip->set_features = nand_default_set_features;
4940 if (!chip->get_features)
4941 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004942
Brian Norris68e80782013-07-18 01:17:02 -07004943 /* If called twice, pointers that depend on busw may need to be reset */
4944 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004945 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004946 if (!chip->block_bad)
4947 chip->block_bad = nand_block_bad;
4948 if (!chip->block_markbad)
4949 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07004950 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004951 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01004952 if (!chip->write_byte || chip->write_byte == nand_write_byte)
4953 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07004954 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004955 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004956
4957 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004958 chip->controller = &chip->dummy_controller;
4959 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004960 }
4961
Masahiro Yamada477544c2017-03-30 17:15:05 +09004962 if (!chip->buf_align)
4963 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004964}
4965
Brian Norris8b6e50c2011-05-25 14:59:01 -07004966/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004967static void sanitize_string(uint8_t *s, size_t len)
4968{
4969 ssize_t i;
4970
Brian Norris8b6e50c2011-05-25 14:59:01 -07004971 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004972 s[len - 1] = 0;
4973
Brian Norris8b6e50c2011-05-25 14:59:01 -07004974 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004975 for (i = 0; i < len - 1; i++) {
4976 if (s[i] < ' ' || s[i] > 127)
4977 s[i] = '?';
4978 }
4979
Brian Norris8b6e50c2011-05-25 14:59:01 -07004980 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004981 strim(s);
4982}
4983
4984static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4985{
4986 int i;
4987 while (len--) {
4988 crc ^= *p++ << 8;
4989 for (i = 0; i < 8; i++)
4990 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4991 }
4992
4993 return crc;
4994}
4995
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004996/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004997static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4998 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004999{
5000 struct onfi_ext_param_page *ep;
5001 struct onfi_ext_section *s;
5002 struct onfi_ext_ecc_info *ecc;
5003 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005004 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005005 int len;
5006 int i;
5007
5008 len = le16_to_cpu(p->ext_param_page_length) * 16;
5009 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005010 if (!ep)
5011 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005012
5013 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005014 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5015 if (ret)
5016 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005017
5018 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005019 ret = nand_change_read_column_op(chip,
5020 sizeof(*p) * p->num_of_param_pages,
5021 ep, len, true);
5022 if (ret)
5023 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005024
Boris Brezillon97d90da2017-11-30 18:01:29 +01005025 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005026 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5027 != le16_to_cpu(ep->crc))) {
5028 pr_debug("fail in the CRC.\n");
5029 goto ext_out;
5030 }
5031
5032 /*
5033 * Check the signature.
5034 * Do not strictly follow the ONFI spec, maybe changed in future.
5035 */
5036 if (strncmp(ep->sig, "EPPS", 4)) {
5037 pr_debug("The signature is invalid.\n");
5038 goto ext_out;
5039 }
5040
5041 /* find the ECC section. */
5042 cursor = (uint8_t *)(ep + 1);
5043 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5044 s = ep->sections + i;
5045 if (s->type == ONFI_SECTION_TYPE_2)
5046 break;
5047 cursor += s->length * 16;
5048 }
5049 if (i == ONFI_EXT_SECTION_MAX) {
5050 pr_debug("We can not find the ECC section.\n");
5051 goto ext_out;
5052 }
5053
5054 /* get the info we want. */
5055 ecc = (struct onfi_ext_ecc_info *)cursor;
5056
Brian Norris4ae7d222013-09-16 18:20:21 -07005057 if (!ecc->codeword_size) {
5058 pr_debug("Invalid codeword size\n");
5059 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005060 }
5061
Brian Norris4ae7d222013-09-16 18:20:21 -07005062 chip->ecc_strength_ds = ecc->ecc_bits;
5063 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005064 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005065
5066ext_out:
5067 kfree(ep);
5068 return ret;
5069}
5070
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005071/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005072 * Recover data with bit-wise majority
5073 */
5074static void nand_bit_wise_majority(const void **srcbufs,
5075 unsigned int nsrcbufs,
5076 void *dstbuf,
5077 unsigned int bufsize)
5078{
5079 int i, j, k;
5080
5081 for (i = 0; i < bufsize; i++) {
5082 u8 val = 0;
5083
5084 for (j = 0; j < 8; j++) {
5085 unsigned int cnt = 0;
5086
5087 for (k = 0; k < nsrcbufs; k++) {
5088 const u8 *srcbuf = srcbufs[k];
5089
5090 if (srcbuf[i] & BIT(j))
5091 cnt++;
5092 }
5093
5094 if (cnt > nsrcbufs / 2)
5095 val |= BIT(j);
5096 }
5097
5098 ((u8 *)dstbuf)[i] = val;
5099 }
5100}
5101
5102/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005103 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005104 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005105static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005106{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005107 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005108 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005109 struct onfi_params *onfi;
5110 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005111 char id[4];
5112 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005113
Brian Norris7854d3f2011-06-23 14:12:08 -07005114 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005115 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5116 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005117 return 0;
5118
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005119 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005120 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005121 if (!p)
5122 return -ENOMEM;
5123
Boris Brezillon97d90da2017-11-30 18:01:29 +01005124 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005125 if (ret) {
5126 ret = 0;
5127 goto free_onfi_param_page;
5128 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005129
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005130 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005131 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005132 if (ret) {
5133 ret = 0;
5134 goto free_onfi_param_page;
5135 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005136
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005137 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005138 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005139 if (i)
5140 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005141 break;
5142 }
5143 }
5144
Brian Norrisc7f23a72013-08-13 10:51:55 -07005145 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005146 const void *srcbufs[3] = {p, p + 1, p + 2};
5147
5148 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5149 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5150 sizeof(*p));
5151
5152 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5153 le16_to_cpu(p->crc)) {
5154 pr_err("ONFI parameter recovery failed, aborting\n");
5155 goto free_onfi_param_page;
5156 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005157 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005158
Chris Packham00ce4e02018-06-25 10:44:44 +12005159 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5160 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5161 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5162
Brian Norris8b6e50c2011-05-25 14:59:01 -07005163 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005164 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005165 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005166 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005167 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005168 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005169 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005170 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005171 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005172 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005173 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005174 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005175
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005176 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005177 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005178 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005179 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005180
5181 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5182 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005183 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5184 if (!chip->parameters.model) {
5185 ret = -ENOMEM;
5186 goto free_onfi_param_page;
5187 }
Brian Norris4355b702013-08-27 18:45:10 -07005188
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005189 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005190
5191 /*
5192 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5193 * (don't ask me who thought of this...). MTD assumes that these
5194 * dimensions will be power-of-2, so just truncate the remaining area.
5195 */
5196 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5197 mtd->erasesize *= mtd->writesize;
5198
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005199 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005200
5201 /* See erasesize comment */
5202 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005203 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005204 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005205
Zach Brown34da5f52017-01-10 13:30:21 -06005206 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5207 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5208
Miquel Raynala97421c2018-03-19 14:47:27 +01005209 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005210 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005211
Huang Shijie10c86ba2013-05-17 11:17:26 +08005212 if (p->ecc_bits != 0xff) {
5213 chip->ecc_strength_ds = p->ecc_bits;
5214 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005215 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005216 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005217
5218 /*
5219 * The nand_flash_detect_ext_param_page() uses the
5220 * Change Read Column command which maybe not supported
5221 * by the chip->cmdfunc. So try to update the chip->cmdfunc
5222 * now. We do not replace user supplied command function.
5223 */
5224 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5225 chip->cmdfunc = nand_command_lp;
5226
5227 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005228 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005229 pr_warn("Failed to detect ONFI extended param page\n");
5230 } else {
5231 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005232 }
5233
Miquel Raynalf4531b22018-03-19 14:47:26 +01005234 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005235 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005236 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005237 bitmap_set(chip->parameters.get_feature_list,
5238 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5239 bitmap_set(chip->parameters.set_feature_list,
5240 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5241 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005242
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005243 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5244 if (!onfi) {
5245 ret = -ENOMEM;
5246 goto free_model;
5247 }
5248
5249 onfi->version = onfi_version;
5250 onfi->tPROG = le16_to_cpu(p->t_prog);
5251 onfi->tBERS = le16_to_cpu(p->t_bers);
5252 onfi->tR = le16_to_cpu(p->t_r);
5253 onfi->tCCS = le16_to_cpu(p->t_ccs);
5254 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5255 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5256 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5257 chip->parameters.onfi = onfi;
5258
5259 /* Identification done, free the full ONFI parameter page and exit */
5260 kfree(p);
5261
5262 return 1;
5263
5264free_model:
5265 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005266free_onfi_param_page:
5267 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005268
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005269 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005270}
5271
5272/*
Huang Shijie91361812014-02-21 13:39:40 +08005273 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5274 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005275static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005276{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005277 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005278 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005279 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005280 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005281 char id[5];
5282 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005283
5284 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005285 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5286 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005287 return 0;
5288
Miquel Raynal480139d2018-03-19 14:47:30 +01005289 /* JEDEC chip: allocate a buffer to hold its parameter page */
5290 p = kzalloc(sizeof(*p), GFP_KERNEL);
5291 if (!p)
5292 return -ENOMEM;
5293
Boris Brezillon97d90da2017-11-30 18:01:29 +01005294 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005295 if (ret) {
5296 ret = 0;
5297 goto free_jedec_param_page;
5298 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005299
Huang Shijie91361812014-02-21 13:39:40 +08005300 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005301 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005302 if (ret) {
5303 ret = 0;
5304 goto free_jedec_param_page;
5305 }
Huang Shijie91361812014-02-21 13:39:40 +08005306
5307 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5308 le16_to_cpu(p->crc))
5309 break;
5310 }
5311
5312 if (i == 3) {
5313 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005314 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005315 }
5316
5317 /* Check version */
5318 val = le16_to_cpu(p->revision);
5319 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005320 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005321 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005322 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005323
Miquel Raynal480139d2018-03-19 14:47:30 +01005324 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005325 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005326 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005327 }
5328
5329 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5330 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005331 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5332 if (!chip->parameters.model) {
5333 ret = -ENOMEM;
5334 goto free_jedec_param_page;
5335 }
Huang Shijie91361812014-02-21 13:39:40 +08005336
5337 mtd->writesize = le32_to_cpu(p->byte_per_page);
5338
5339 /* Please reference to the comment for nand_flash_detect_onfi. */
5340 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5341 mtd->erasesize *= mtd->writesize;
5342
5343 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5344
5345 /* Please reference to the comment for nand_flash_detect_onfi. */
5346 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5347 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5348 chip->bits_per_cell = p->bits_per_cell;
5349
Miquel Raynal480139d2018-03-19 14:47:30 +01005350 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005351 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005352
5353 /* ECC info */
5354 ecc = &p->ecc_info[0];
5355
5356 if (ecc->codeword_size >= 9) {
5357 chip->ecc_strength_ds = ecc->ecc_bits;
5358 chip->ecc_step_ds = 1 << ecc->codeword_size;
5359 } else {
5360 pr_warn("Invalid codeword size\n");
5361 }
5362
Miquel Raynal480139d2018-03-19 14:47:30 +01005363free_jedec_param_page:
5364 kfree(p);
5365 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005366}
5367
5368/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005369 * nand_id_has_period - Check if an ID string has a given wraparound period
5370 * @id_data: the ID string
5371 * @arrlen: the length of the @id_data array
5372 * @period: the period of repitition
5373 *
5374 * Check if an ID string is repeated within a given sequence of bytes at
5375 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005376 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005377 * if the repetition has a period of @period; otherwise, returns zero.
5378 */
5379static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5380{
5381 int i, j;
5382 for (i = 0; i < period; i++)
5383 for (j = i + period; j < arrlen; j += period)
5384 if (id_data[i] != id_data[j])
5385 return 0;
5386 return 1;
5387}
5388
5389/*
5390 * nand_id_len - Get the length of an ID string returned by CMD_READID
5391 * @id_data: the ID string
5392 * @arrlen: the length of the @id_data array
5393
5394 * Returns the length of the ID string, according to known wraparound/trailing
5395 * zero patterns. If no pattern exists, returns the length of the array.
5396 */
5397static int nand_id_len(u8 *id_data, int arrlen)
5398{
5399 int last_nonzero, period;
5400
5401 /* Find last non-zero byte */
5402 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5403 if (id_data[last_nonzero])
5404 break;
5405
5406 /* All zeros */
5407 if (last_nonzero < 0)
5408 return 0;
5409
5410 /* Calculate wraparound period */
5411 for (period = 1; period < arrlen; period++)
5412 if (nand_id_has_period(id_data, arrlen, period))
5413 break;
5414
5415 /* There's a repeated pattern */
5416 if (period < arrlen)
5417 return period;
5418
5419 /* There are trailing zeros */
5420 if (last_nonzero < arrlen - 1)
5421 return last_nonzero + 1;
5422
5423 /* No pattern detected */
5424 return arrlen;
5425}
5426
Huang Shijie7db906b2013-09-25 14:58:11 +08005427/* Extract the bits of per cell from the 3rd byte of the extended ID */
5428static int nand_get_bits_per_cell(u8 cellinfo)
5429{
5430 int bits;
5431
5432 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5433 bits >>= NAND_CI_CELLTYPE_SHIFT;
5434 return bits + 1;
5435}
5436
Brian Norrise3b88bd2012-09-24 20:40:52 -07005437/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005438 * Many new NAND share similar device ID codes, which represent the size of the
5439 * chip. The rest of the parameters must be decoded according to generic or
5440 * manufacturer-specific "extended ID" decoding patterns.
5441 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005442void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005443{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005444 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005445 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005446 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005447 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005448 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005449 /* The 4th id byte is the important one */
5450 extid = id_data[3];
5451
Boris Brezillon01389b62016-06-08 10:30:18 +02005452 /* Calc pagesize */
5453 mtd->writesize = 1024 << (extid & 0x03);
5454 extid >>= 2;
5455 /* Calc oobsize */
5456 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5457 extid >>= 2;
5458 /* Calc blocksize. Blocksize is multiples of 64KiB */
5459 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5460 extid >>= 2;
5461 /* Get buswidth information */
5462 if (extid & 0x1)
5463 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005464}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005465EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005466
5467/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005468 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5469 * decodes a matching ID table entry and assigns the MTD size parameters for
5470 * the chip.
5471 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005472static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005473{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005474 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005475
5476 mtd->erasesize = type->erasesize;
5477 mtd->writesize = type->pagesize;
5478 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005479
Huang Shijie1c195e92013-09-25 14:58:12 +08005480 /* All legacy ID NAND are small-page, SLC */
5481 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005482}
5483
5484/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005485 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5486 * heuristic patterns using various detected parameters (e.g., manufacturer,
5487 * page size, cell-type information).
5488 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005489static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005490{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005491 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005492
5493 /* Set the bad block position */
5494 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5495 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5496 else
5497 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005498}
5499
Huang Shijieec6e87e2013-03-15 11:01:00 +08005500static inline bool is_full_id_nand(struct nand_flash_dev *type)
5501{
5502 return type->id_len;
5503}
5504
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005505static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005506 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005507{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005508 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005509 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005510
Huang Shijieec6e87e2013-03-15 11:01:00 +08005511 if (!strncmp(type->id, id_data, type->id_len)) {
5512 mtd->writesize = type->pagesize;
5513 mtd->erasesize = type->erasesize;
5514 mtd->oobsize = type->oobsize;
5515
Huang Shijie7db906b2013-09-25 14:58:11 +08005516 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005517 chip->chipsize = (uint64_t)type->chipsize << 20;
5518 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005519 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5520 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005521 chip->onfi_timing_mode_default =
5522 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005523
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005524 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5525 if (!chip->parameters.model)
5526 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005527
Huang Shijieec6e87e2013-03-15 11:01:00 +08005528 return true;
5529 }
5530 return false;
5531}
5532
Brian Norris7e74c2d2012-09-24 20:40:49 -07005533/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005534 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5535 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5536 * table.
5537 */
5538static void nand_manufacturer_detect(struct nand_chip *chip)
5539{
5540 /*
5541 * Try manufacturer detection if available and use
5542 * nand_decode_ext_id() otherwise.
5543 */
5544 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005545 chip->manufacturer.desc->ops->detect) {
5546 /* The 3rd id byte holds MLC / multichip data */
5547 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005548 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005549 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005550 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005551 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005552}
5553
5554/*
5555 * Manufacturer initialization. This function is called for all NANDs including
5556 * ONFI and JEDEC compliant ones.
5557 * Manufacturer drivers should put all their specific initialization code in
5558 * their ->init() hook.
5559 */
5560static int nand_manufacturer_init(struct nand_chip *chip)
5561{
5562 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5563 !chip->manufacturer.desc->ops->init)
5564 return 0;
5565
5566 return chip->manufacturer.desc->ops->init(chip);
5567}
5568
5569/*
5570 * Manufacturer cleanup. This function is called for all NANDs including
5571 * ONFI and JEDEC compliant ones.
5572 * Manufacturer drivers should put all their specific cleanup code in their
5573 * ->cleanup() hook.
5574 */
5575static void nand_manufacturer_cleanup(struct nand_chip *chip)
5576{
5577 /* Release manufacturer private data */
5578 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5579 chip->manufacturer.desc->ops->cleanup)
5580 chip->manufacturer.desc->ops->cleanup(chip);
5581}
5582
5583/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005584 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005585 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005586static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005587{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005588 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005589 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005590 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005591 u8 *id_data = chip->id.data;
5592 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Karl Beldanef89a882008-09-15 14:37:29 +02005594 /*
5595 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005596 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005597 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005598 ret = nand_reset(chip, 0);
5599 if (ret)
5600 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005601
5602 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005603 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005606 ret = nand_readid_op(chip, 0, id_data, 2);
5607 if (ret)
5608 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005611 maf_id = id_data[0];
5612 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Brian Norris8b6e50c2011-05-25 14:59:01 -07005614 /*
5615 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005616 * interface concerns can cause random data which looks like a
5617 * possibly credible NAND flash to appear. If the two results do
5618 * not match, ignore the device completely.
5619 */
5620
Brian Norris4aef9b72012-09-24 20:40:48 -07005621 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005622 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5623 if (ret)
5624 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005625
Boris Brezillon7f501f02016-05-24 19:20:05 +02005626 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005627 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005628 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005629 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005630 }
5631
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005632 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005633
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005634 /* Try to identify manufacturer */
5635 manufacturer = nand_get_manufacturer(maf_id);
5636 chip->manufacturer.desc = manufacturer;
5637
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005638 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005639 type = nand_flash_ids;
5640
Boris Brezillon29a198a2016-05-24 20:17:48 +02005641 /*
5642 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5643 * override it.
5644 * This is required to make sure initial NAND bus width set by the
5645 * NAND controller driver is coherent with the real NAND bus width
5646 * (extracted by auto-detection code).
5647 */
5648 busw = chip->options & NAND_BUSWIDTH_16;
5649
5650 /*
5651 * The flag is only set (never cleared), reset it to its default value
5652 * before starting auto-detection.
5653 */
5654 chip->options &= ~NAND_BUSWIDTH_16;
5655
Huang Shijieec6e87e2013-03-15 11:01:00 +08005656 for (; type->name != NULL; type++) {
5657 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005658 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005659 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005660 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005661 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005662 }
5663 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005664
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005665 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005666 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005667 ret = nand_flash_detect_onfi(chip);
5668 if (ret < 0)
5669 return ret;
5670 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005671 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005672
5673 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005674 ret = nand_flash_detect_jedec(chip);
5675 if (ret < 0)
5676 return ret;
5677 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005678 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005679 }
5680
David Woodhouse5e81e882010-02-26 18:32:56 +00005681 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005682 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005683
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005684 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5685 if (!chip->parameters.model)
5686 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005687
Adrian Hunter69423d92008-12-10 13:37:21 +00005688 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005689
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005690 if (!type->pagesize)
5691 nand_manufacturer_detect(chip);
5692 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005693 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005694
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005695 /* Get chip options */
5696 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005697
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005698ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005699 if (!mtd->name)
5700 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005701
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005702 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005703 WARN_ON(busw & NAND_BUSWIDTH_16);
5704 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005705 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5706 /*
5707 * Check, if buswidth is correct. Hardware drivers should set
5708 * chip correct!
5709 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005710 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005711 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005712 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5713 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005714 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5715 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005716 ret = -EINVAL;
5717
5718 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005719 }
5720
Boris Brezillon7f501f02016-05-24 19:20:05 +02005721 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005722
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005723 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005724 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005725 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005726 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005727
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005728 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005729 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005730 if (chip->chipsize & 0xffffffff)
5731 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005732 else {
5733 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5734 chip->chip_shift += 32 - 1;
5735 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005736
Masahiro Yamada14157f82017-09-13 11:05:50 +09005737 if (chip->chip_shift - chip->page_shift > 16)
5738 chip->options |= NAND_ROW_ADDR_3;
5739
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005740 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005741 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005742
Brian Norris8b6e50c2011-05-25 14:59:01 -07005743 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005744 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
5745 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005746
Ezequiel Garcia20171642013-11-25 08:30:31 -03005747 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005748 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005749 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5750 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005751 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005752 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005753 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005754 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005755
5756free_detect_allocation:
5757 kfree(chip->parameters.model);
5758
5759 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005760}
5761
Boris Brezillond48f62b2016-04-01 14:54:32 +02005762static const char * const nand_ecc_modes[] = {
5763 [NAND_ECC_NONE] = "none",
5764 [NAND_ECC_SOFT] = "soft",
5765 [NAND_ECC_HW] = "hw",
5766 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5767 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005768 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005769};
5770
5771static int of_get_nand_ecc_mode(struct device_node *np)
5772{
5773 const char *pm;
5774 int err, i;
5775
5776 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5777 if (err < 0)
5778 return err;
5779
5780 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5781 if (!strcasecmp(pm, nand_ecc_modes[i]))
5782 return i;
5783
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005784 /*
5785 * For backward compatibility we support few obsoleted values that don't
5786 * have their mappings into nand_ecc_modes_t anymore (they were merged
5787 * with other enums).
5788 */
5789 if (!strcasecmp(pm, "soft_bch"))
5790 return NAND_ECC_SOFT;
5791
Boris Brezillond48f62b2016-04-01 14:54:32 +02005792 return -ENODEV;
5793}
5794
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005795static const char * const nand_ecc_algos[] = {
5796 [NAND_ECC_HAMMING] = "hamming",
5797 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005798 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005799};
5800
Boris Brezillond48f62b2016-04-01 14:54:32 +02005801static int of_get_nand_ecc_algo(struct device_node *np)
5802{
5803 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005804 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005805
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005806 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5807 if (!err) {
5808 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5809 if (!strcasecmp(pm, nand_ecc_algos[i]))
5810 return i;
5811 return -ENODEV;
5812 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005813
5814 /*
5815 * For backward compatibility we also read "nand-ecc-mode" checking
5816 * for some obsoleted values that were specifying ECC algorithm.
5817 */
5818 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5819 if (err < 0)
5820 return err;
5821
5822 if (!strcasecmp(pm, "soft"))
5823 return NAND_ECC_HAMMING;
5824 else if (!strcasecmp(pm, "soft_bch"))
5825 return NAND_ECC_BCH;
5826
5827 return -ENODEV;
5828}
5829
5830static int of_get_nand_ecc_step_size(struct device_node *np)
5831{
5832 int ret;
5833 u32 val;
5834
5835 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5836 return ret ? ret : val;
5837}
5838
5839static int of_get_nand_ecc_strength(struct device_node *np)
5840{
5841 int ret;
5842 u32 val;
5843
5844 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5845 return ret ? ret : val;
5846}
5847
5848static int of_get_nand_bus_width(struct device_node *np)
5849{
5850 u32 val;
5851
5852 if (of_property_read_u32(np, "nand-bus-width", &val))
5853 return 8;
5854
5855 switch (val) {
5856 case 8:
5857 case 16:
5858 return val;
5859 default:
5860 return -EIO;
5861 }
5862}
5863
5864static bool of_get_nand_on_flash_bbt(struct device_node *np)
5865{
5866 return of_property_read_bool(np, "nand-on-flash-bbt");
5867}
5868
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005869static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005870{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005871 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005872 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005873
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005874 if (!dn)
5875 return 0;
5876
Brian Norris5844fee2015-01-23 00:22:27 -08005877 if (of_get_nand_bus_width(dn) == 16)
5878 chip->options |= NAND_BUSWIDTH_16;
5879
Stefan Agnerf922bd72018-06-24 23:27:23 +02005880 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5881 chip->options |= NAND_IS_BOOT_MEDIUM;
5882
Brian Norris5844fee2015-01-23 00:22:27 -08005883 if (of_get_nand_on_flash_bbt(dn))
5884 chip->bbt_options |= NAND_BBT_USE_FLASH;
5885
5886 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005887 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005888 ecc_strength = of_get_nand_ecc_strength(dn);
5889 ecc_step = of_get_nand_ecc_step_size(dn);
5890
Brian Norris5844fee2015-01-23 00:22:27 -08005891 if (ecc_mode >= 0)
5892 chip->ecc.mode = ecc_mode;
5893
Rafał Miłecki79082452016-03-23 11:19:02 +01005894 if (ecc_algo >= 0)
5895 chip->ecc.algo = ecc_algo;
5896
Brian Norris5844fee2015-01-23 00:22:27 -08005897 if (ecc_strength >= 0)
5898 chip->ecc.strength = ecc_strength;
5899
5900 if (ecc_step > 0)
5901 chip->ecc.size = ecc_step;
5902
Boris Brezillonba78ee02016-06-08 17:04:22 +02005903 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5904 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5905
Brian Norris5844fee2015-01-23 00:22:27 -08005906 return 0;
5907}
5908
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005909/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005910 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005911 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005912 * @maxchips: number of chips to scan for
5913 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005914 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005915 * This is the first phase of the normal nand_scan() function. It reads the
5916 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005917 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005918 * This helper used to be called directly from controller drivers that needed
5919 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5920 * prevented dynamic allocations during this phase which was unconvenient and
5921 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005922 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02005923static int nand_scan_ident(struct nand_chip *chip, int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005924 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005925{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005926 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08005927 int i, nand_maf_id, nand_dev_id;
Brian Norris5844fee2015-01-23 00:22:27 -08005928 int ret;
5929
Miquel Raynal17fa8042017-11-30 18:01:31 +01005930 /* Enforce the right timings for reset/detection */
5931 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5932
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005933 ret = nand_dt_init(chip);
5934 if (ret)
5935 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005936
Brian Norrisf7a8e382016-01-05 10:39:45 -08005937 if (!mtd->name && mtd->dev.parent)
5938 mtd->name = dev_name(mtd->dev.parent);
5939
Miquel Raynal8878b122017-11-09 14:16:45 +01005940 /*
5941 * ->cmdfunc() is legacy and will only be used if ->exec_op() is not
5942 * populated.
5943 */
5944 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005945 /*
Miquel Raynal8878b122017-11-09 14:16:45 +01005946 * Default functions assigned for ->cmdfunc() and
5947 * ->select_chip() both expect ->cmd_ctrl() to be populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005948 */
Miquel Raynal8878b122017-11-09 14:16:45 +01005949 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
5950 pr_err("->cmd_ctrl() should be provided\n");
5951 return -EINVAL;
5952 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005953 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005954
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005955 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005956 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005957
5958 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005959 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005960 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005961 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005962 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005963 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005964 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 }
5966
Boris Brezillon7f501f02016-05-24 19:20:05 +02005967 nand_maf_id = chip->id.data[0];
5968 nand_dev_id = chip->id.data[1];
5969
Boris Brezillon758b56f2018-09-06 14:05:24 +02005970 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005971
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005972 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005973 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005974 u8 id[2];
5975
Karl Beldanef89a882008-09-15 14:37:29 +02005976 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005977 nand_reset(chip, i);
5978
Boris Brezillon758b56f2018-09-06 14:05:24 +02005979 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005981 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005983 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005984 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 break;
Huang Shijie07300162012-11-09 16:23:45 +08005986 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005987 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 }
5989 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005990 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005993 chip->numchips = i;
5994 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
David Woodhouse3b85c322006-09-25 17:06:53 +01005996 return 0;
5997}
5998
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005999static void nand_scan_ident_cleanup(struct nand_chip *chip)
6000{
6001 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006002 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006003}
6004
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006005static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6006{
6007 struct nand_chip *chip = mtd_to_nand(mtd);
6008 struct nand_ecc_ctrl *ecc = &chip->ecc;
6009
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006010 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006011 return -EINVAL;
6012
6013 switch (ecc->algo) {
6014 case NAND_ECC_HAMMING:
6015 ecc->calculate = nand_calculate_ecc;
6016 ecc->correct = nand_correct_data;
6017 ecc->read_page = nand_read_page_swecc;
6018 ecc->read_subpage = nand_read_subpage;
6019 ecc->write_page = nand_write_page_swecc;
6020 ecc->read_page_raw = nand_read_page_raw;
6021 ecc->write_page_raw = nand_write_page_raw;
6022 ecc->read_oob = nand_read_oob_std;
6023 ecc->write_oob = nand_write_oob_std;
6024 if (!ecc->size)
6025 ecc->size = 256;
6026 ecc->bytes = 3;
6027 ecc->strength = 1;
6028 return 0;
6029 case NAND_ECC_BCH:
6030 if (!mtd_nand_has_bch()) {
6031 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6032 return -EINVAL;
6033 }
6034 ecc->calculate = nand_bch_calculate_ecc;
6035 ecc->correct = nand_bch_correct_data;
6036 ecc->read_page = nand_read_page_swecc;
6037 ecc->read_subpage = nand_read_subpage;
6038 ecc->write_page = nand_write_page_swecc;
6039 ecc->read_page_raw = nand_read_page_raw;
6040 ecc->write_page_raw = nand_write_page_raw;
6041 ecc->read_oob = nand_read_oob_std;
6042 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006043
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006044 /*
6045 * Board driver should supply ecc.size and ecc.strength
6046 * values to select how many bits are correctable.
6047 * Otherwise, default to 4 bits for large page devices.
6048 */
6049 if (!ecc->size && (mtd->oobsize >= 64)) {
6050 ecc->size = 512;
6051 ecc->strength = 4;
6052 }
6053
6054 /*
6055 * if no ecc placement scheme was provided pickup the default
6056 * large page one.
6057 */
6058 if (!mtd->ooblayout) {
6059 /* handle large page devices only */
6060 if (mtd->oobsize < 64) {
6061 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6062 return -EINVAL;
6063 }
6064
6065 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006066
6067 }
6068
6069 /*
6070 * We can only maximize ECC config when the default layout is
6071 * used, otherwise we don't know how many bytes can really be
6072 * used.
6073 */
6074 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6075 ecc->options & NAND_ECC_MAXIMIZE) {
6076 int steps, bytes;
6077
6078 /* Always prefer 1k blocks over 512bytes ones */
6079 ecc->size = 1024;
6080 steps = mtd->writesize / ecc->size;
6081
6082 /* Reserve 2 bytes for the BBM */
6083 bytes = (mtd->oobsize - 2) / steps;
6084 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006085 }
6086
6087 /* See nand_bch_init() for details. */
6088 ecc->bytes = 0;
6089 ecc->priv = nand_bch_init(mtd);
6090 if (!ecc->priv) {
6091 WARN(1, "BCH ECC initialization failed!\n");
6092 return -EINVAL;
6093 }
6094 return 0;
6095 default:
6096 WARN(1, "Unsupported ECC algorithm!\n");
6097 return -EINVAL;
6098 }
6099}
6100
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006101/**
6102 * nand_check_ecc_caps - check the sanity of preset ECC settings
6103 * @chip: nand chip info structure
6104 * @caps: ECC caps info structure
6105 * @oobavail: OOB size that the ECC engine can use
6106 *
6107 * When ECC step size and strength are already set, check if they are supported
6108 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6109 * On success, the calculated ECC bytes is set.
6110 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306111static int
6112nand_check_ecc_caps(struct nand_chip *chip,
6113 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006114{
6115 struct mtd_info *mtd = nand_to_mtd(chip);
6116 const struct nand_ecc_step_info *stepinfo;
6117 int preset_step = chip->ecc.size;
6118 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306119 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006120 int i, j;
6121
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006122 for (i = 0; i < caps->nstepinfos; i++) {
6123 stepinfo = &caps->stepinfos[i];
6124
6125 if (stepinfo->stepsize != preset_step)
6126 continue;
6127
6128 for (j = 0; j < stepinfo->nstrengths; j++) {
6129 if (stepinfo->strengths[j] != preset_strength)
6130 continue;
6131
6132 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6133 preset_strength);
6134 if (WARN_ON_ONCE(ecc_bytes < 0))
6135 return ecc_bytes;
6136
6137 if (ecc_bytes * nsteps > oobavail) {
6138 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6139 preset_step, preset_strength);
6140 return -ENOSPC;
6141 }
6142
6143 chip->ecc.bytes = ecc_bytes;
6144
6145 return 0;
6146 }
6147 }
6148
6149 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6150 preset_step, preset_strength);
6151
6152 return -ENOTSUPP;
6153}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006154
6155/**
6156 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6157 * @chip: nand chip info structure
6158 * @caps: ECC engine caps info structure
6159 * @oobavail: OOB size that the ECC engine can use
6160 *
6161 * If a chip's ECC requirement is provided, try to meet it with the least
6162 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6163 * On success, the chosen ECC settings are set.
6164 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306165static int
6166nand_match_ecc_req(struct nand_chip *chip,
6167 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006168{
6169 struct mtd_info *mtd = nand_to_mtd(chip);
6170 const struct nand_ecc_step_info *stepinfo;
6171 int req_step = chip->ecc_step_ds;
6172 int req_strength = chip->ecc_strength_ds;
6173 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6174 int best_step, best_strength, best_ecc_bytes;
6175 int best_ecc_bytes_total = INT_MAX;
6176 int i, j;
6177
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006178 /* No information provided by the NAND chip */
6179 if (!req_step || !req_strength)
6180 return -ENOTSUPP;
6181
6182 /* number of correctable bits the chip requires in a page */
6183 req_corr = mtd->writesize / req_step * req_strength;
6184
6185 for (i = 0; i < caps->nstepinfos; i++) {
6186 stepinfo = &caps->stepinfos[i];
6187 step_size = stepinfo->stepsize;
6188
6189 for (j = 0; j < stepinfo->nstrengths; j++) {
6190 strength = stepinfo->strengths[j];
6191
6192 /*
6193 * If both step size and strength are smaller than the
6194 * chip's requirement, it is not easy to compare the
6195 * resulted reliability.
6196 */
6197 if (step_size < req_step && strength < req_strength)
6198 continue;
6199
6200 if (mtd->writesize % step_size)
6201 continue;
6202
6203 nsteps = mtd->writesize / step_size;
6204
6205 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6206 if (WARN_ON_ONCE(ecc_bytes < 0))
6207 continue;
6208 ecc_bytes_total = ecc_bytes * nsteps;
6209
6210 if (ecc_bytes_total > oobavail ||
6211 strength * nsteps < req_corr)
6212 continue;
6213
6214 /*
6215 * We assume the best is to meet the chip's requrement
6216 * with the least number of ECC bytes.
6217 */
6218 if (ecc_bytes_total < best_ecc_bytes_total) {
6219 best_ecc_bytes_total = ecc_bytes_total;
6220 best_step = step_size;
6221 best_strength = strength;
6222 best_ecc_bytes = ecc_bytes;
6223 }
6224 }
6225 }
6226
6227 if (best_ecc_bytes_total == INT_MAX)
6228 return -ENOTSUPP;
6229
6230 chip->ecc.size = best_step;
6231 chip->ecc.strength = best_strength;
6232 chip->ecc.bytes = best_ecc_bytes;
6233
6234 return 0;
6235}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006236
6237/**
6238 * nand_maximize_ecc - choose the max ECC strength available
6239 * @chip: nand chip info structure
6240 * @caps: ECC engine caps info structure
6241 * @oobavail: OOB size that the ECC engine can use
6242 *
6243 * Choose the max ECC strength that is supported on the controller, and can fit
6244 * within the chip's OOB. On success, the chosen ECC settings are set.
6245 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306246static int
6247nand_maximize_ecc(struct nand_chip *chip,
6248 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006249{
6250 struct mtd_info *mtd = nand_to_mtd(chip);
6251 const struct nand_ecc_step_info *stepinfo;
6252 int step_size, strength, nsteps, ecc_bytes, corr;
6253 int best_corr = 0;
6254 int best_step = 0;
6255 int best_strength, best_ecc_bytes;
6256 int i, j;
6257
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006258 for (i = 0; i < caps->nstepinfos; i++) {
6259 stepinfo = &caps->stepinfos[i];
6260 step_size = stepinfo->stepsize;
6261
6262 /* If chip->ecc.size is already set, respect it */
6263 if (chip->ecc.size && step_size != chip->ecc.size)
6264 continue;
6265
6266 for (j = 0; j < stepinfo->nstrengths; j++) {
6267 strength = stepinfo->strengths[j];
6268
6269 if (mtd->writesize % step_size)
6270 continue;
6271
6272 nsteps = mtd->writesize / step_size;
6273
6274 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6275 if (WARN_ON_ONCE(ecc_bytes < 0))
6276 continue;
6277
6278 if (ecc_bytes * nsteps > oobavail)
6279 continue;
6280
6281 corr = strength * nsteps;
6282
6283 /*
6284 * If the number of correctable bits is the same,
6285 * bigger step_size has more reliability.
6286 */
6287 if (corr > best_corr ||
6288 (corr == best_corr && step_size > best_step)) {
6289 best_corr = corr;
6290 best_step = step_size;
6291 best_strength = strength;
6292 best_ecc_bytes = ecc_bytes;
6293 }
6294 }
6295 }
6296
6297 if (!best_corr)
6298 return -ENOTSUPP;
6299
6300 chip->ecc.size = best_step;
6301 chip->ecc.strength = best_strength;
6302 chip->ecc.bytes = best_ecc_bytes;
6303
6304 return 0;
6305}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006306
Abhishek Sahu181ace92018-06-20 12:57:28 +05306307/**
6308 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6309 * @chip: nand chip info structure
6310 * @caps: ECC engine caps info structure
6311 * @oobavail: OOB size that the ECC engine can use
6312 *
6313 * Choose the ECC configuration according to following logic
6314 *
6315 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6316 * then check if it is supported by this controller.
6317 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6318 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6319 * to the chip's requirement. If available OOB size can't fit the chip
6320 * requirement then fallback to the maximum ECC step size and ECC strength.
6321 *
6322 * On success, the chosen ECC settings are set.
6323 */
6324int nand_ecc_choose_conf(struct nand_chip *chip,
6325 const struct nand_ecc_caps *caps, int oobavail)
6326{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306327 struct mtd_info *mtd = nand_to_mtd(chip);
6328
6329 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6330 return -EINVAL;
6331
Abhishek Sahu181ace92018-06-20 12:57:28 +05306332 if (chip->ecc.size && chip->ecc.strength)
6333 return nand_check_ecc_caps(chip, caps, oobavail);
6334
6335 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6336 return nand_maximize_ecc(chip, caps, oobavail);
6337
6338 if (!nand_match_ecc_req(chip, caps, oobavail))
6339 return 0;
6340
6341 return nand_maximize_ecc(chip, caps, oobavail);
6342}
6343EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6344
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006345/*
6346 * Check if the chip configuration meet the datasheet requirements.
6347
6348 * If our configuration corrects A bits per B bytes and the minimum
6349 * required correction level is X bits per Y bytes, then we must ensure
6350 * both of the following are true:
6351 *
6352 * (1) A / B >= X / Y
6353 * (2) A >= X
6354 *
6355 * Requirement (1) ensures we can correct for the required bitflip density.
6356 * Requirement (2) ensures we can correct even when all bitflips are clumped
6357 * in the same sector.
6358 */
6359static bool nand_ecc_strength_good(struct mtd_info *mtd)
6360{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006361 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006362 struct nand_ecc_ctrl *ecc = &chip->ecc;
6363 int corr, ds_corr;
6364
6365 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6366 /* Not enough information */
6367 return true;
6368
6369 /*
6370 * We get the number of corrected bits per page to compare
6371 * the correction density.
6372 */
6373 corr = (mtd->writesize * ecc->strength) / ecc->size;
6374 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6375
6376 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6377}
David Woodhouse3b85c322006-09-25 17:06:53 +01006378
6379/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006380 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006381 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006382 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006383 * This is the second phase of the normal nand_scan() function. It fills out
6384 * all the uninitialized function pointers with the defaults and scans for a
6385 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006386 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006387static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006388{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006389 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006390 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006391 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006392
Brian Norrise2414f42012-02-06 13:44:00 -08006393 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006394 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006395 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006396 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006397 }
Brian Norrise2414f42012-02-06 13:44:00 -08006398
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006399 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006400 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006401 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006402
Boris Brezillonf84674b2017-06-02 12:18:24 +02006403 /*
6404 * FIXME: some NAND manufacturer drivers expect the first die to be
6405 * selected when manufacturer->init() is called. They should be fixed
6406 * to explictly select the relevant die when interacting with the NAND
6407 * chip.
6408 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02006409 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006410 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02006411 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006412 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006413 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006414
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006415 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006416 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006417
6418 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006419 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006420 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006421 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006422 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006423 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006426 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 break;
6428 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006429 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006430 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006433 /*
6434 * Expose the whole OOB area to users if ECC_NONE
6435 * is passed. We could do that for all kind of
6436 * ->oobsize, but we must keep the old large/small
6437 * page with ECC layout when ->oobsize <= 128 for
6438 * compatibility reasons.
6439 */
6440 if (ecc->mode == NAND_ECC_NONE) {
6441 mtd_set_ooblayout(mtd,
6442 &nand_ooblayout_lp_ops);
6443 break;
6444 }
6445
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006446 WARN(1, "No oob scheme defined for oobsize %d\n",
6447 mtd->oobsize);
6448 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006449 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
6451 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006452
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006453 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006454 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006455 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006456 */
David Woodhouse956e9442006-09-25 17:12:39 +01006457
Huang Shijie97de79e02013-10-18 14:20:53 +08006458 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006459 case NAND_ECC_HW_OOB_FIRST:
6460 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006461 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006462 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6463 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006464 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006465 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006466 if (!ecc->read_page)
6467 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006468
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006469 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006470 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006471 if (!ecc->read_page)
6472 ecc->read_page = nand_read_page_hwecc;
6473 if (!ecc->write_page)
6474 ecc->write_page = nand_write_page_hwecc;
6475 if (!ecc->read_page_raw)
6476 ecc->read_page_raw = nand_read_page_raw;
6477 if (!ecc->write_page_raw)
6478 ecc->write_page_raw = nand_write_page_raw;
6479 if (!ecc->read_oob)
6480 ecc->read_oob = nand_read_oob_std;
6481 if (!ecc->write_oob)
6482 ecc->write_oob = nand_write_oob_std;
6483 if (!ecc->read_subpage)
6484 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006485 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006486 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006487
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006488 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006489 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6490 (!ecc->read_page ||
6491 ecc->read_page == nand_read_page_hwecc ||
6492 !ecc->write_page ||
6493 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006494 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6495 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006496 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006497 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006498 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006499 if (!ecc->read_page)
6500 ecc->read_page = nand_read_page_syndrome;
6501 if (!ecc->write_page)
6502 ecc->write_page = nand_write_page_syndrome;
6503 if (!ecc->read_page_raw)
6504 ecc->read_page_raw = nand_read_page_raw_syndrome;
6505 if (!ecc->write_page_raw)
6506 ecc->write_page_raw = nand_write_page_raw_syndrome;
6507 if (!ecc->read_oob)
6508 ecc->read_oob = nand_read_oob_syndrome;
6509 if (!ecc->write_oob)
6510 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006511
Huang Shijie97de79e02013-10-18 14:20:53 +08006512 if (mtd->writesize >= ecc->size) {
6513 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006514 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6515 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006516 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006517 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006518 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006519 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006520 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6521 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006522 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006523 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006525 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006526 ret = nand_set_ecc_soft_ops(mtd);
6527 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006528 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006529 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006530 }
6531 break;
6532
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006533 case NAND_ECC_ON_DIE:
6534 if (!ecc->read_page || !ecc->write_page) {
6535 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6536 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006537 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006538 }
6539 if (!ecc->read_oob)
6540 ecc->read_oob = nand_read_oob_std;
6541 if (!ecc->write_oob)
6542 ecc->write_oob = nand_write_oob_std;
6543 break;
6544
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006545 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006546 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006547 ecc->read_page = nand_read_page_raw;
6548 ecc->write_page = nand_write_page_raw;
6549 ecc->read_oob = nand_read_oob_std;
6550 ecc->read_page_raw = nand_read_page_raw;
6551 ecc->write_page_raw = nand_write_page_raw;
6552 ecc->write_oob = nand_write_oob_std;
6553 ecc->size = mtd->writesize;
6554 ecc->bytes = 0;
6555 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006557
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006559 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6560 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006561 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006564 if (ecc->correct || ecc->calculate) {
6565 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6566 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6567 if (!ecc->calc_buf || !ecc->code_buf) {
6568 ret = -ENOMEM;
6569 goto err_nand_manuf_cleanup;
6570 }
6571 }
6572
Brian Norris9ce244b2011-08-30 18:45:37 -07006573 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006574 if (!ecc->read_oob_raw)
6575 ecc->read_oob_raw = ecc->read_oob;
6576 if (!ecc->write_oob_raw)
6577 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006578
Boris Brezillon846031d2016-02-03 20:11:00 +01006579 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006580 mtd->ecc_strength = ecc->strength;
6581 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006582
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006583 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006584 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006585 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006586 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006587 ecc->steps = mtd->writesize / ecc->size;
6588 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006589 WARN(1, "Invalid ECC parameters\n");
6590 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006591 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006593 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006594 if (ecc->total > mtd->oobsize) {
6595 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6596 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006597 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006598 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006599
Boris Brezillon846031d2016-02-03 20:11:00 +01006600 /*
6601 * The number of bytes available for a client to place data into
6602 * the out of band area.
6603 */
6604 ret = mtd_ooblayout_count_freebytes(mtd);
6605 if (ret < 0)
6606 ret = 0;
6607
6608 mtd->oobavail = ret;
6609
6610 /* ECC sanity check: warn if it's too weak */
6611 if (!nand_ecc_strength_good(mtd))
6612 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6613 mtd->name);
6614
Brian Norris8b6e50c2011-05-25 14:59:01 -07006615 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006616 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006617 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006618 case 2:
6619 mtd->subpage_sft = 1;
6620 break;
6621 case 4:
6622 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006623 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006624 mtd->subpage_sft = 2;
6625 break;
6626 }
6627 }
6628 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6629
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006630 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006631 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006634 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006636 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306637 switch (ecc->mode) {
6638 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306639 if (chip->page_shift > 9)
6640 chip->options |= NAND_SUBPAGE_READ;
6641 break;
6642
6643 default:
6644 break;
6645 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006646
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006648 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006649 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6650 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006651 mtd->_erase = nand_erase;
6652 mtd->_point = NULL;
6653 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006654 mtd->_panic_write = panic_nand_write;
6655 mtd->_read_oob = nand_read_oob;
6656 mtd->_write_oob = nand_write_oob;
6657 mtd->_sync = nand_sync;
6658 mtd->_lock = NULL;
6659 mtd->_unlock = NULL;
6660 mtd->_suspend = nand_suspend;
6661 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006662 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006663 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006664 mtd->_block_isbad = nand_block_isbad;
6665 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006666 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006667 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006669 /*
6670 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6671 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6672 * properly set.
6673 */
6674 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006675 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Boris Brezillonf84674b2017-06-02 12:18:24 +02006677 /* Initialize the ->data_interface field. */
6678 ret = nand_init_data_interface(chip);
6679 if (ret)
6680 goto err_nand_manuf_cleanup;
6681
6682 /* Enter fastest possible mode on all dies. */
6683 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006684 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006685 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006686 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006687 }
6688
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006689 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006690 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
6693 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006694 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006695 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006696 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006697
Brian Norris44d41822017-05-01 17:04:50 -07006698 return 0;
6699
Boris Brezillonf84674b2017-06-02 12:18:24 +02006700
6701err_nand_manuf_cleanup:
6702 nand_manufacturer_cleanup(chip);
6703
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006704err_free_buf:
6705 kfree(chip->data_buf);
6706 kfree(ecc->code_buf);
6707 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006708
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710}
6711
Miquel Raynal05b54c72018-07-19 01:05:46 +02006712static int nand_attach(struct nand_chip *chip)
6713{
6714 if (chip->controller->ops && chip->controller->ops->attach_chip)
6715 return chip->controller->ops->attach_chip(chip);
6716
6717 return 0;
6718}
6719
6720static void nand_detach(struct nand_chip *chip)
6721{
6722 if (chip->controller->ops && chip->controller->ops->detach_chip)
6723 chip->controller->ops->detach_chip(chip);
6724}
6725
David Woodhouse3b85c322006-09-25 17:06:53 +01006726/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006727 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006728 * @chip: NAND chip object
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006729 * @maxchips: number of chips to scan for. @nand_scan_ident() will not be run if
6730 * this parameter is zero (useful for specific drivers that must
6731 * handle this part of the process themselves, e.g docg4).
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006732 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006733 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006734 * This fills out all the uninitialized function pointers with the defaults.
6735 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006736 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006737 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006738int nand_scan_with_ids(struct nand_chip *chip, int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006739 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006740{
6741 int ret;
6742
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006743 if (maxchips) {
Boris Brezillon00ad3782018-09-06 14:05:14 +02006744 ret = nand_scan_ident(chip, maxchips, ids);
Miquel Raynal49aa76b2018-07-25 15:31:42 +02006745 if (ret)
6746 return ret;
6747 }
Miquel Raynal05b54c72018-07-19 01:05:46 +02006748
6749 ret = nand_attach(chip);
6750 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006751 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006752
Boris Brezillon00ad3782018-09-06 14:05:14 +02006753 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006754 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006755 goto detach_chip;
6756
6757 return 0;
6758
6759detach_chip:
6760 nand_detach(chip);
6761cleanup_ident:
6762 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006763
David Woodhouse3b85c322006-09-25 17:06:53 +01006764 return ret;
6765}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006766EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006769 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6770 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006771 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006772void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006774 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006775 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006776 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6777
Jesper Juhlfa671642005-11-07 01:01:27 -08006778 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006779 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006780 kfree(chip->data_buf);
6781 kfree(chip->ecc.code_buf);
6782 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006783
6784 /* Free bad block descriptor memory */
6785 if (chip->badblock_pattern && chip->badblock_pattern->options
6786 & NAND_BBT_DYNAMICSTRUCT)
6787 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006788
6789 /* Free manufacturer priv data. */
6790 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006791
6792 /* Free controller specific allocations after chip identification */
6793 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006794
6795 /* Free identification phase allocations */
6796 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006798
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006799EXPORT_SYMBOL_GPL(nand_cleanup);
6800
6801/**
6802 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6803 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006804 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006805 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006806void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006807{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006808 mtd_device_unregister(nand_to_mtd(chip));
6809 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006810}
David Woodhousee0c7d762006-05-13 18:07:53 +01006811EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006812
David Woodhousee0c7d762006-05-13 18:07:53 +01006813MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006814MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6815MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006816MODULE_DESCRIPTION("Generic NAND flash driver code");