blob: d48c588d1abeafed49ac89398c1606131ff8118e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020048#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Boris Brezillon348d56a2018-09-07 00:38:48 +020050#include "internals.h"
51
Huang Shijie6a8214a2012-11-19 14:43:30 +080052static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020054static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
55 struct mtd_oob_ops *ops);
56
Boris Brezillon41b207a2016-02-03 19:06:15 +010057/* Define default oob placement schemes for large and small page devices */
58static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
59 struct mtd_oob_region *oobregion)
60{
61 struct nand_chip *chip = mtd_to_nand(mtd);
62 struct nand_ecc_ctrl *ecc = &chip->ecc;
63
64 if (section > 1)
65 return -ERANGE;
66
67 if (!section) {
68 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020069 if (mtd->oobsize == 16)
70 oobregion->length = 4;
71 else
72 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010073 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020074 if (mtd->oobsize == 8)
75 return -ERANGE;
76
Boris Brezillon41b207a2016-02-03 19:06:15 +010077 oobregion->offset = 6;
78 oobregion->length = ecc->total - 4;
79 }
80
81 return 0;
82}
83
84static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
85 struct mtd_oob_region *oobregion)
86{
87 if (section > 1)
88 return -ERANGE;
89
90 if (mtd->oobsize == 16) {
91 if (section)
92 return -ERANGE;
93
94 oobregion->length = 8;
95 oobregion->offset = 8;
96 } else {
97 oobregion->length = 2;
98 if (!section)
99 oobregion->offset = 3;
100 else
101 oobregion->offset = 6;
102 }
103
104 return 0;
105}
106
107const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
108 .ecc = nand_ooblayout_ecc_sp,
109 .free = nand_ooblayout_free_sp,
110};
111EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
112
113static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
114 struct mtd_oob_region *oobregion)
115{
116 struct nand_chip *chip = mtd_to_nand(mtd);
117 struct nand_ecc_ctrl *ecc = &chip->ecc;
118
Miquel Raynal882fd152017-08-26 17:19:15 +0200119 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100120 return -ERANGE;
121
122 oobregion->length = ecc->total;
123 oobregion->offset = mtd->oobsize - oobregion->length;
124
125 return 0;
126}
127
128static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
129 struct mtd_oob_region *oobregion)
130{
131 struct nand_chip *chip = mtd_to_nand(mtd);
132 struct nand_ecc_ctrl *ecc = &chip->ecc;
133
134 if (section)
135 return -ERANGE;
136
137 oobregion->length = mtd->oobsize - ecc->total - 2;
138 oobregion->offset = 2;
139
140 return 0;
141}
142
143const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
144 .ecc = nand_ooblayout_ecc_lp,
145 .free = nand_ooblayout_free_lp,
146};
147EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200148
Alexander Couzens6a623e02017-05-02 12:19:00 +0200149/*
150 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
151 * are placed at a fixed offset.
152 */
153static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
154 struct mtd_oob_region *oobregion)
155{
156 struct nand_chip *chip = mtd_to_nand(mtd);
157 struct nand_ecc_ctrl *ecc = &chip->ecc;
158
159 if (section)
160 return -ERANGE;
161
162 switch (mtd->oobsize) {
163 case 64:
164 oobregion->offset = 40;
165 break;
166 case 128:
167 oobregion->offset = 80;
168 break;
169 default:
170 return -EINVAL;
171 }
172
173 oobregion->length = ecc->total;
174 if (oobregion->offset + oobregion->length > mtd->oobsize)
175 return -ERANGE;
176
177 return 0;
178}
179
180static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
181 struct mtd_oob_region *oobregion)
182{
183 struct nand_chip *chip = mtd_to_nand(mtd);
184 struct nand_ecc_ctrl *ecc = &chip->ecc;
185 int ecc_offset = 0;
186
187 if (section < 0 || section > 1)
188 return -ERANGE;
189
190 switch (mtd->oobsize) {
191 case 64:
192 ecc_offset = 40;
193 break;
194 case 128:
195 ecc_offset = 80;
196 break;
197 default:
198 return -EINVAL;
199 }
200
201 if (section == 0) {
202 oobregion->offset = 2;
203 oobregion->length = ecc_offset - 2;
204 } else {
205 oobregion->offset = ecc_offset + ecc->total;
206 oobregion->length = mtd->oobsize - oobregion->offset;
207 }
208
209 return 0;
210}
211
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100212static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200213 .ecc = nand_ooblayout_ecc_lp_hamming,
214 .free = nand_ooblayout_free_lp_hamming,
215};
216
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530217static int check_offs_len(struct mtd_info *mtd,
218 loff_t ofs, uint64_t len)
219{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100220 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530221 int ret = 0;
222
223 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300224 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700225 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530226 ret = -EINVAL;
227 }
228
229 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300230 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700231 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530232 ret = -EINVAL;
233 }
234
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530235 return ret;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/**
239 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700240 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000241 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800242 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100244static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100246 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200248 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200249 spin_lock(&chip->controller->lock);
250 chip->controller->active = NULL;
251 chip->state = FL_READY;
252 wake_up(&chip->controller->wq);
253 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/**
257 * nand_read_byte - [DEFAULT] read one byte from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200258 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700260 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200262static uint8_t nand_read_byte(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200264 return readb(chip->legacy.IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Masanari Iida064a7692012-11-09 23:20:58 +0900268 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200269 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700271 * Default read function for 16bit buswidth with endianness conversion.
272 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200274static uint8_t nand_read_byte16(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200276 return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * nand_select_chip - [DEFAULT] control CE line
Boris Brezillon758b56f2018-09-06 14:05:24 +0200281 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
284 * Default select function for 1 chip devices.
285 */
Boris Brezillon758b56f2018-09-06 14:05:24 +0200286static void nand_select_chip(struct nand_chip *chip, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200288 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case -1:
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200290 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
291 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295
296 default:
297 BUG();
298 }
299}
300
301/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100302 * nand_write_byte - [DEFAULT] write single byte to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200303 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100304 * @byte: value to write
305 *
306 * Default function to write a byte to I/O[7:0]
307 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200308static void nand_write_byte(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100309{
Boris Brezillon716bbba2018-09-07 00:38:35 +0200310 chip->legacy.write_buf(chip, &byte, 1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100311}
312
313/**
314 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
Boris Brezillonc0739d82018-09-06 14:05:23 +0200315 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100316 * @byte: value to write
317 *
318 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
319 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200320static void nand_write_byte16(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100321{
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100322 uint16_t word = byte;
323
324 /*
325 * It's not entirely clear what should happen to I/O[15:8] when writing
326 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
327 *
328 * When the host supports a 16-bit bus width, only data is
329 * transferred at the 16-bit width. All address and command line
330 * transfers shall use only the lower 8-bits of the data bus. During
331 * command transfers, the host may place any value on the upper
332 * 8-bits of the data bus. During address transfers, the host shall
333 * set the upper 8-bits of the data bus to 00h.
334 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100335 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100336 * four parameters are specified to be written to I/O[7:0], but this is
337 * neither an address nor a command transfer. Let's assume a 0 on the
338 * upper I/O lines is OK.
339 */
Boris Brezillon716bbba2018-09-07 00:38:35 +0200340 chip->legacy.write_buf(chip, (uint8_t *)&word, 2);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100341}
342
343/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * nand_write_buf - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200345 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700346 * @buf: data buffer
347 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700349 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200351static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200353 iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000357 * nand_read_buf - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200358 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700359 * @buf: buffer to store date
360 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700362 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200364static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200366 ioread8_rep(chip->legacy.IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * nand_write_buf16 - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200371 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700372 * @buf: data buffer
373 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700375 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200377static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf,
378 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000381
Boris Brezillon82fc5092018-09-07 00:38:34 +0200382 iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000386 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200387 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700388 * @buf: buffer to store date
389 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700391 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200393static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Boris Brezillon82fc5092018-09-07 00:38:34 +0200397 ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200402 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700403 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000405 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200407static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200409 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900410 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900411 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Brian Norris5fb15492011-05-31 16:31:21 -0700413 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700414 ofs += mtd->erasesize - mtd->writesize;
415
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100416 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900417 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100418
Masahiro Yamadac120e752017-03-23 05:07:01 +0900419 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200420 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530421 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900422 return res;
423
424 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000425
Brian Norriscdbec052012-01-13 18:11:48 -0800426 if (likely(chip->badblockbits == 8))
427 res = bad != 0xFF;
428 else
429 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900430 if (res)
431 return res;
432 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200433
Masahiro Yamadac120e752017-03-23 05:07:01 +0900434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700438 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200439 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700440 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700442 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700443 * specific driver. It provides the details for writing a bad block marker to a
444 * block.
445 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200446static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700447{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200448 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700449 struct mtd_oob_ops ops;
450 uint8_t buf[2] = { 0, 0 };
451 int ret = 0, res, i = 0;
452
Brian Norris0ec56dc2015-02-28 02:02:30 -0800453 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700454 ops.oobbuf = buf;
455 ops.ooboffs = chip->badblockpos;
456 if (chip->options & NAND_BUSWIDTH_16) {
457 ops.ooboffs &= ~0x01;
458 ops.len = ops.ooblen = 2;
459 } else {
460 ops.len = ops.ooblen = 1;
461 }
462 ops.mode = MTD_OPS_PLACE_OOB;
463
464 /* Write to first/last page(s) if necessary */
465 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
466 ofs += mtd->erasesize - mtd->writesize;
467 do {
468 res = nand_do_write_oob(mtd, ofs, &ops);
469 if (!ret)
470 ret = res;
471
472 i++;
473 ofs += mtd->writesize;
474 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
475
476 return ret;
477}
478
479/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200480 * nand_markbad_bbm - mark a block by updating the BBM
481 * @chip: NAND chip object
482 * @ofs: offset of the block to mark bad
483 */
484int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
485{
486 if (chip->legacy.block_markbad)
487 return chip->legacy.block_markbad(chip, ofs);
488
489 return nand_default_block_markbad(chip, ofs);
490}
491
492static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
493{
494 if (chip->legacy.block_bad)
495 return chip->legacy.block_bad(chip, ofs);
496
497 return nand_block_bad(chip, ofs);
498}
499
500/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700501 * nand_block_markbad_lowlevel - mark a block bad
502 * @mtd: MTD device structure
503 * @ofs: offset from device start
504 *
505 * This function performs the generic NAND bad block marking steps (i.e., bad
506 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200507 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700508 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700509 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300510 *
Brian Norrise2414f42012-02-06 13:44:00 -0800511 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700512 * (2) write bad block marker to OOB area of affected block (unless flag
513 * NAND_BBT_NO_OOB_BBM is present)
514 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300515 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700516 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800517 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700519static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100521 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700522 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000523
Brian Norrisb32843b2013-07-30 17:52:59 -0700524 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800525 struct erase_info einfo;
526
527 /* Attempt erase before marking OOB */
528 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800529 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300530 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200531 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800532
Brian Norrisb32843b2013-07-30 17:52:59 -0700533 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800534 nand_get_device(mtd, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200535 ret = nand_markbad_bbm(chip, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300536 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200537 }
Brian Norrise2414f42012-02-06 13:44:00 -0800538
Brian Norrisb32843b2013-07-30 17:52:59 -0700539 /* Mark block bad in BBT */
540 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200541 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800542 if (!ret)
543 ret = res;
544 }
545
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200546 if (!ret)
547 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300548
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200549 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000552/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700554 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700556 * Check, if the device is write protected. The function expects, that the
557 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100559static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100561 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100562 u8 status;
563 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200564
Brian Norris8b6e50c2011-05-25 14:59:01 -0700565 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200566 if (chip->options & NAND_BROKEN_XD)
567 return 0;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100570 ret = nand_status_op(chip, &status);
571 if (ret)
572 return ret;
573
574 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800578 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700579 * @mtd: MTD device structure
580 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300581 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800582 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 */
584static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
585{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100586 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300587
588 if (!chip->bbt)
589 return 0;
590 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200591 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300592}
593
594/**
595 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
596 * @mtd: MTD device structure
597 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700598 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
600 * Check, if the block is bad. Either by reading the bad block table or
601 * calling of the scan function.
602 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530603static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100605 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200608 if (chip->bbt)
609 return nand_isbad_bbt(chip, ofs, allowbbt);
610
611 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200614/**
615 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700616 * @mtd: MTD device structure
617 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200618 *
619 * Helper function for nand_wait_ready used when needing to wait in interrupt
620 * context.
621 */
622static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
623{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100624 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200625 int i;
626
627 /* Wait for the device to get ready */
628 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200629 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200630 break;
631 touch_softlockup_watchdog();
632 mdelay(1);
633 }
634}
635
Alex Smithb70af9b2015-10-06 14:52:07 +0100636/**
637 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200638 * @chip: NAND chip object
Alex Smithb70af9b2015-10-06 14:52:07 +0100639 *
640 * Wait for the ready pin after a command, and warn if a timeout occurs.
641 */
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200642void nand_wait_ready(struct nand_chip *chip)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643{
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200644 struct mtd_info *mtd = nand_to_mtd(chip);
Alex Smithb70af9b2015-10-06 14:52:07 +0100645 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000646
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200647 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100648 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649
Brian Norris7854d3f2011-06-23 14:12:08 -0700650 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100651 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000652 do {
Boris Brezillon8395b752018-09-07 00:38:37 +0200653 if (chip->legacy.dev_ready(chip))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300654 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100655 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000656 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100657
Boris Brezillon8395b752018-09-07 00:38:37 +0200658 if (!chip->legacy.dev_ready(chip))
Brian Norris9ebfdf52016-03-04 17:19:23 -0800659 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000660}
David Woodhouse4b648b02006-09-25 17:05:24 +0100661EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200664 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
665 * @mtd: MTD device structure
666 * @timeo: Timeout in ms
667 *
668 * Wait for status ready (i.e. command done) or timeout.
669 */
670static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
671{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100672 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100673 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200674
675 timeo = jiffies + msecs_to_jiffies(timeo);
676 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100677 u8 status;
678
679 ret = nand_read_data_op(chip, &status, sizeof(status), true);
680 if (ret)
681 return;
682
683 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200684 break;
685 touch_softlockup_watchdog();
686 } while (time_before(jiffies, timeo));
687};
688
689/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100690 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
691 * @chip: NAND chip structure
692 * @timeout_ms: Timeout in ms
693 *
694 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
695 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
696 * returned.
697 *
698 * This helper is intended to be used when the controller does not have access
699 * to the NAND R/B pin.
700 *
701 * Be aware that calling this helper from an ->exec_op() implementation means
702 * ->exec_op() must be re-entrant.
703 *
704 * Return 0 if the NAND chip is ready, a negative error otherwise.
705 */
706int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
707{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200708 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100709 u8 status = 0;
710 int ret;
711
712 if (!chip->exec_op)
713 return -ENOTSUPP;
714
Boris Brezillon3057fce2018-05-04 21:24:31 +0200715 /* Wait tWB before polling the STATUS reg. */
716 timings = nand_get_sdr_timings(&chip->data_interface);
717 ndelay(PSEC_TO_NSEC(timings->tWB_max));
718
Miquel Raynal8878b122017-11-09 14:16:45 +0100719 ret = nand_status_op(chip, NULL);
720 if (ret)
721 return ret;
722
723 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
724 do {
725 ret = nand_read_data_op(chip, &status, sizeof(status), true);
726 if (ret)
727 break;
728
729 if (status & NAND_STATUS_READY)
730 break;
731
732 /*
733 * Typical lowest execution time for a tR on most NANDs is 10us,
734 * use this as polling delay before doing something smarter (ie.
735 * deriving a delay from the timeout value, timeout_ms/ratio).
736 */
737 udelay(10);
738 } while (time_before(jiffies, timeout_ms));
739
740 /*
741 * We have to exit READ_STATUS mode in order to read real data on the
742 * bus in case the WAITRDY instruction is preceding a DATA_IN
743 * instruction.
744 */
745 nand_exit_status_op(chip);
746
747 if (ret)
748 return ret;
749
750 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
751};
752EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
753
754/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 * nand_command - [DEFAULT] Send command to NAND device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200756 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700757 * @command: the command to be sent
758 * @column: the column address for this command, -1 if none
759 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700761 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200762 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200764static void nand_command(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200765 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200767 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200768 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Brian Norris8b6e50c2011-05-25 14:59:01 -0700770 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (command == NAND_CMD_SEQIN) {
772 int readcmd;
773
Joern Engel28318772006-05-22 23:18:05 +0200774 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200776 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 readcmd = NAND_CMD_READOOB;
778 } else if (column < 256) {
779 /* First 256 bytes --> READ0 */
780 readcmd = NAND_CMD_READ0;
781 } else {
782 column -= 256;
783 readcmd = NAND_CMD_READ1;
784 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200785 chip->legacy.cmd_ctrl(chip, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200786 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100788 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200789 chip->legacy.cmd_ctrl(chip, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200792 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
793 /* Serially input address */
794 if (column != -1) {
795 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800796 if (chip->options & NAND_BUSWIDTH_16 &&
797 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200798 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200799 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200800 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200802 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200803 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200804 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200805 chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900806 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200807 chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200808 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200809 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
810 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000811
812 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700813 * Program and erase have their own busy handlers status and sequential
814 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000817
Miquel Raynaldf467892017-11-08 17:00:27 +0100818 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 case NAND_CMD_PAGEPROG:
820 case NAND_CMD_ERASE1:
821 case NAND_CMD_ERASE2:
822 case NAND_CMD_SEQIN:
823 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900824 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900825 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return;
827
828 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200829 if (chip->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200831 udelay(chip->legacy.chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200832 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
833 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
834 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
835 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200836 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
837 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return;
839
David Woodhousee0c7d762006-05-13 18:07:53 +0100840 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200841 case NAND_CMD_READ0:
842 /*
843 * READ0 is sometimes used to exit GET STATUS mode. When this
844 * is the case no address cycles are requested, and we can use
845 * this information to detect that we should not wait for the
846 * device to be ready.
847 */
848 if (column == -1 && page_addr == -1)
849 return;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000852 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * If we don't have access to the busy pin, we apply the given
854 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100855 */
Boris Brezillon8395b752018-09-07 00:38:37 +0200856 if (!chip->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200857 udelay(chip->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700861 /*
862 * Apply this short delay always to ensure that we do wait tWB in
863 * any case on any machine.
864 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100865 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000866
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200867 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200870static void nand_ccs_delay(struct nand_chip *chip)
871{
872 /*
873 * The controller already takes care of waiting for tCCS when the RNDIN
874 * or RNDOUT command is sent, return directly.
875 */
876 if (!(chip->options & NAND_WAIT_TCCS))
877 return;
878
879 /*
880 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
881 * (which should be safe for all NANDs).
882 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100883 if (chip->setup_data_interface)
884 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200885 else
886 ndelay(500);
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/**
890 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200891 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700892 * @command: the command to be sent
893 * @column: the column address for this command, -1 if none
894 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200896 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700897 * devices. We don't have the separate regions as we have in the small page
898 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200900static void nand_command_lp(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200901 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200903 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* Emulate NAND_CMD_READOOB */
906 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200907 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 command = NAND_CMD_READ0;
909 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000910
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200911 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100912 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200913 chip->legacy.cmd_ctrl(chip, command,
914 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200917 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* Serially input address */
920 if (column != -1) {
921 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800922 if (chip->options & NAND_BUSWIDTH_16 &&
923 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200925 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200926 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200927
Brian Norrisf5b88de2016-10-03 09:49:35 -0700928 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200929 if (!nand_opcode_8bits(command))
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200930 chip->legacy.cmd_ctrl(chip, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200933 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
934 chip->legacy.cmd_ctrl(chip, page_addr >> 8,
935 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900936 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200937 chip->legacy.cmd_ctrl(chip, page_addr >> 16,
938 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200941 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
942 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000943
944 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700945 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100946 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000949
Miquel Raynaldf467892017-11-08 17:00:27 +0100950 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 case NAND_CMD_CACHEDPROG:
952 case NAND_CMD_PAGEPROG:
953 case NAND_CMD_ERASE1:
954 case NAND_CMD_ERASE2:
955 case NAND_CMD_SEQIN:
956 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900957 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900958 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200961 case NAND_CMD_RNDIN:
962 nand_ccs_delay(chip);
963 return;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200966 if (chip->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200968 udelay(chip->legacy.chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200969 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
970 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
971 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
972 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200973 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
974 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return;
976
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200977 case NAND_CMD_RNDOUT:
978 /* No ready / busy check necessary */
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200979 chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
980 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
981 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
982 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200983
984 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200985 return;
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200988 /*
989 * READ0 is sometimes used to exit GET STATUS mode. When this
990 * is the case no address cycles are requested, and we can use
991 * this information to detect that READSTART should not be
992 * issued.
993 */
994 if (column == -1 && page_addr == -1)
995 return;
996
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200997 chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART,
998 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
999 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
1000 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001001
David Woodhousee0c7d762006-05-13 18:07:53 +01001002 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001004 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001006 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001007 */
Boris Brezillon8395b752018-09-07 00:38:37 +02001008 if (!chip->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001009 udelay(chip->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001013
Brian Norris8b6e50c2011-05-25 14:59:01 -07001014 /*
1015 * Apply this short delay always to ensure that we do wait tWB in
1016 * any case on any machine.
1017 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001018 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001019
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001020 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001024 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001025 * @chip: the nand chip descriptor
1026 * @mtd: MTD device structure
1027 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001028 *
1029 * Used when in panic, no locks are taken.
1030 */
1031static void panic_nand_get_device(struct nand_chip *chip,
1032 struct mtd_info *mtd, int new_state)
1033{
Brian Norris7854d3f2011-06-23 14:12:08 -07001034 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001035 chip->controller->active = chip;
1036 chip->state = new_state;
1037}
1038
1039/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001041 * @mtd: MTD device structure
1042 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
1044 * Get the device and lock it for exclusive access
1045 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001046static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001047nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001049 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001050 spinlock_t *lock = &chip->controller->lock;
1051 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001052 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001053retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001054 spin_lock(lock);
1055
vimal singhb8b3ee92009-07-09 20:41:22 +05301056 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001057 if (!chip->controller->active)
1058 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001059
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001060 if (chip->controller->active == chip && chip->state == FL_READY) {
1061 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001062 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001063 return 0;
1064 }
1065 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001066 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1067 chip->state = FL_PM_SUSPENDED;
1068 spin_unlock(lock);
1069 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001070 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001071 }
1072 set_current_state(TASK_UNINTERRUPTIBLE);
1073 add_wait_queue(wq, &wait);
1074 spin_unlock(lock);
1075 schedule();
1076 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto retry;
1078}
1079
1080/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001081 * panic_nand_wait - [GENERIC] wait until the command is done
1082 * @mtd: MTD device structure
1083 * @chip: NAND chip structure
1084 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001085 *
1086 * Wait for command done. This is a helper function for nand_wait used when
1087 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001088 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001089 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001090static void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001091{
1092 int i;
1093 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +02001094 if (chip->legacy.dev_ready) {
1095 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001096 break;
1097 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001098 int ret;
1099 u8 status;
1100
1101 ret = nand_read_data_op(chip, &status, sizeof(status),
1102 true);
1103 if (ret)
1104 return;
1105
1106 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001107 break;
1108 }
1109 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001110 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001111}
1112
1113/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001114 * nand_wait - [DEFAULT] wait until the command is done
1115 * @mtd: MTD device structure
1116 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001118 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001119 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001120static int nand_wait(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122
Alex Smithb70af9b2015-10-06 14:52:07 +01001123 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001124 u8 status;
1125 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Brian Norris8b6e50c2011-05-25 14:59:01 -07001127 /*
1128 * Apply this short delay always to ensure that we do wait tWB in any
1129 * case on any machine.
1130 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001131 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Boris Brezillon97d90da2017-11-30 18:01:29 +01001133 ret = nand_status_op(chip, NULL);
1134 if (ret)
1135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001137 if (in_interrupt() || oops_in_progress)
Boris Brezillonf1d46942018-09-06 14:05:29 +02001138 panic_nand_wait(chip, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001139 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001140 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001141 do {
Boris Brezillon8395b752018-09-07 00:38:37 +02001142 if (chip->legacy.dev_ready) {
1143 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001144 break;
1145 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001146 ret = nand_read_data_op(chip, &status,
1147 sizeof(status), true);
1148 if (ret)
1149 return ret;
1150
1151 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001152 break;
1153 }
1154 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001155 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001157
Boris Brezillon97d90da2017-11-30 18:01:29 +01001158 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1159 if (ret)
1160 return ret;
1161
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001162 /* This can happen if in case of timeout or buggy dev_ready */
1163 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return status;
1165}
1166
Miquel Raynal789157e2018-03-19 14:47:28 +01001167static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001168{
Miquel Raynal789157e2018-03-19 14:47:28 +01001169 return (chip->parameters.supports_set_get_features &&
1170 test_bit(addr, chip->parameters.get_feature_list));
1171}
1172
1173static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1174{
1175 return (chip->parameters.supports_set_get_features &&
1176 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001177}
1178
1179/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001180 * nand_reset_data_interface - Reset data interface and timings
1181 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001182 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001183 *
1184 * Reset the Data interface and timings to ONFI mode 0.
1185 *
1186 * Returns 0 for success or negative error code otherwise.
1187 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001188static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001189{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001190 int ret;
1191
1192 if (!chip->setup_data_interface)
1193 return 0;
1194
1195 /*
1196 * The ONFI specification says:
1197 * "
1198 * To transition from NV-DDR or NV-DDR2 to the SDR data
1199 * interface, the host shall use the Reset (FFh) command
1200 * using SDR timing mode 0. A device in any timing mode is
1201 * required to recognize Reset (FFh) command issued in SDR
1202 * timing mode 0.
1203 * "
1204 *
1205 * Configure the data interface in SDR mode and set the
1206 * timings to timing mode 0.
1207 */
1208
Miquel Raynal17fa8042017-11-30 18:01:31 +01001209 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +02001210 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001211 if (ret)
1212 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1213
1214 return ret;
1215}
1216
1217/**
1218 * nand_setup_data_interface - Setup the best data interface and timings
1219 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001220 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001221 *
1222 * Find and configure the best data interface and NAND timings supported by
1223 * the chip and the driver.
1224 * First tries to retrieve supported timing modes from ONFI information,
1225 * and if the NAND chip does not support ONFI, relies on the
1226 * ->onfi_timing_mode_default specified in the nand_ids table.
1227 *
1228 * Returns 0 for success or negative error code otherwise.
1229 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001230static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001231{
Miquel Raynal97baea12018-03-19 14:47:20 +01001232 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1233 chip->onfi_timing_mode_default,
1234 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001235 int ret;
1236
Miquel Raynal17fa8042017-11-30 18:01:31 +01001237 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001238 return 0;
1239
Miquel Raynal993447b2018-03-19 14:47:21 +01001240 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001241 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001242 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001243 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1244 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001245 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001246 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001247 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001248 }
1249
Miquel Raynal97baea12018-03-19 14:47:20 +01001250 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +02001251 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001252 if (ret)
1253 return ret;
1254
1255 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001256 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001257 return 0;
1258
1259 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001260 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001261 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1262 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001263 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001264 if (ret)
1265 goto err_reset_chip;
1266
1267 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1268 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1269 chip->onfi_timing_mode_default);
1270 goto err_reset_chip;
1271 }
1272
1273 return 0;
1274
1275err_reset_chip:
1276 /*
1277 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1278 * timing mode.
1279 */
1280 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001281 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001282 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001283 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001284
Boris Brezillond8e725d2016-09-15 10:32:50 +02001285 return ret;
1286}
1287
1288/**
1289 * nand_init_data_interface - find the best data interface and timings
1290 * @chip: The NAND chip
1291 *
1292 * Find the best data interface and NAND timings supported by the chip
1293 * and the driver.
1294 * First tries to retrieve supported timing modes from ONFI information,
1295 * and if the NAND chip does not support ONFI, relies on the
1296 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1297 * function nand_chip->data_interface is initialized with the best timing mode
1298 * available.
1299 *
1300 * Returns 0 for success or negative error code otherwise.
1301 */
1302static int nand_init_data_interface(struct nand_chip *chip)
1303{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001304 int modes, mode, ret;
1305
1306 if (!chip->setup_data_interface)
1307 return 0;
1308
1309 /*
1310 * First try to identify the best timings from ONFI parameters and
1311 * if the NAND does not support ONFI, fallback to the default ONFI
1312 * timing mode.
1313 */
Boris Brezillon462f35d2018-09-07 00:38:47 +02001314 if (chip->parameters.onfi) {
1315 modes = chip->parameters.onfi->async_timing_mode;
1316 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001317 if (!chip->onfi_timing_mode_default)
1318 return 0;
1319
1320 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1321 }
1322
Boris Brezillond8e725d2016-09-15 10:32:50 +02001323 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001324 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001325 if (ret)
1326 continue;
1327
Miquel Raynald787b8b2017-12-22 18:12:41 +01001328 /*
1329 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1330 * controller supports the requested timings.
1331 */
Boris Brezillon858838b2018-09-06 14:05:33 +02001332 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +01001333 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001334 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001335 if (!ret) {
1336 chip->onfi_timing_mode_default = mode;
1337 break;
1338 }
1339 }
1340
1341 return 0;
1342}
1343
Boris Brezillond8e725d2016-09-15 10:32:50 +02001344/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001345 * nand_fill_column_cycles - fill the column cycles of an address
1346 * @chip: The NAND chip
1347 * @addrs: Array of address cycles to fill
1348 * @offset_in_page: The offset in the page
1349 *
1350 * Fills the first or the first two bytes of the @addrs field depending
1351 * on the NAND bus width and the page size.
1352 *
1353 * Returns the number of cycles needed to encode the column, or a negative
1354 * error code in case one of the arguments is invalid.
1355 */
1356static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1357 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Miquel Raynal8878b122017-11-09 14:16:45 +01001359 struct mtd_info *mtd = nand_to_mtd(chip);
1360
1361 /* Make sure the offset is less than the actual page size. */
1362 if (offset_in_page > mtd->writesize + mtd->oobsize)
1363 return -EINVAL;
1364
1365 /*
1366 * On small page NANDs, there's a dedicated command to access the OOB
1367 * area, and the column address is relative to the start of the OOB
1368 * area, not the start of the page. Asjust the address accordingly.
1369 */
1370 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1371 offset_in_page -= mtd->writesize;
1372
1373 /*
1374 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1375 * wide, then it must be divided by 2.
1376 */
1377 if (chip->options & NAND_BUSWIDTH_16) {
1378 if (WARN_ON(offset_in_page % 2))
1379 return -EINVAL;
1380
1381 offset_in_page /= 2;
1382 }
1383
1384 addrs[0] = offset_in_page;
1385
1386 /*
1387 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1388 * need 2
1389 */
1390 if (mtd->writesize <= 512)
1391 return 1;
1392
1393 addrs[1] = offset_in_page >> 8;
1394
1395 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Miquel Raynal8878b122017-11-09 14:16:45 +01001398static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1399 unsigned int offset_in_page, void *buf,
1400 unsigned int len)
1401{
1402 struct mtd_info *mtd = nand_to_mtd(chip);
1403 const struct nand_sdr_timings *sdr =
1404 nand_get_sdr_timings(&chip->data_interface);
1405 u8 addrs[4];
1406 struct nand_op_instr instrs[] = {
1407 NAND_OP_CMD(NAND_CMD_READ0, 0),
1408 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1409 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1410 PSEC_TO_NSEC(sdr->tRR_min)),
1411 NAND_OP_DATA_IN(len, buf, 0),
1412 };
1413 struct nand_operation op = NAND_OPERATION(instrs);
1414 int ret;
1415
1416 /* Drop the DATA_IN instruction if len is set to 0. */
1417 if (!len)
1418 op.ninstrs--;
1419
1420 if (offset_in_page >= mtd->writesize)
1421 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1422 else if (offset_in_page >= 256 &&
1423 !(chip->options & NAND_BUSWIDTH_16))
1424 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1425
1426 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1427 if (ret < 0)
1428 return ret;
1429
1430 addrs[1] = page;
1431 addrs[2] = page >> 8;
1432
1433 if (chip->options & NAND_ROW_ADDR_3) {
1434 addrs[3] = page >> 16;
1435 instrs[1].ctx.addr.naddrs++;
1436 }
1437
1438 return nand_exec_op(chip, &op);
1439}
1440
1441static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1442 unsigned int offset_in_page, void *buf,
1443 unsigned int len)
1444{
1445 const struct nand_sdr_timings *sdr =
1446 nand_get_sdr_timings(&chip->data_interface);
1447 u8 addrs[5];
1448 struct nand_op_instr instrs[] = {
1449 NAND_OP_CMD(NAND_CMD_READ0, 0),
1450 NAND_OP_ADDR(4, addrs, 0),
1451 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1452 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1453 PSEC_TO_NSEC(sdr->tRR_min)),
1454 NAND_OP_DATA_IN(len, buf, 0),
1455 };
1456 struct nand_operation op = NAND_OPERATION(instrs);
1457 int ret;
1458
1459 /* Drop the DATA_IN instruction if len is set to 0. */
1460 if (!len)
1461 op.ninstrs--;
1462
1463 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1464 if (ret < 0)
1465 return ret;
1466
1467 addrs[2] = page;
1468 addrs[3] = page >> 8;
1469
1470 if (chip->options & NAND_ROW_ADDR_3) {
1471 addrs[4] = page >> 16;
1472 instrs[1].ctx.addr.naddrs++;
1473 }
1474
1475 return nand_exec_op(chip, &op);
1476}
1477
1478/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001479 * nand_read_page_op - Do a READ PAGE operation
1480 * @chip: The NAND chip
1481 * @page: page to read
1482 * @offset_in_page: offset within the page
1483 * @buf: buffer used to store the data
1484 * @len: length of the buffer
1485 *
1486 * This function issues a READ PAGE operation.
1487 * This function does not select/unselect the CS line.
1488 *
1489 * Returns 0 on success, a negative error code otherwise.
1490 */
1491int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1492 unsigned int offset_in_page, void *buf, unsigned int len)
1493{
1494 struct mtd_info *mtd = nand_to_mtd(chip);
1495
1496 if (len && !buf)
1497 return -EINVAL;
1498
1499 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1500 return -EINVAL;
1501
Miquel Raynal8878b122017-11-09 14:16:45 +01001502 if (chip->exec_op) {
1503 if (mtd->writesize > 512)
1504 return nand_lp_exec_read_page_op(chip, page,
1505 offset_in_page, buf,
1506 len);
1507
1508 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1509 buf, len);
1510 }
1511
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001512 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001513 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001514 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001515
1516 return 0;
1517}
1518EXPORT_SYMBOL_GPL(nand_read_page_op);
1519
1520/**
1521 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1522 * @chip: The NAND chip
1523 * @page: parameter page to read
1524 * @buf: buffer used to store the data
1525 * @len: length of the buffer
1526 *
1527 * This function issues a READ PARAMETER PAGE operation.
1528 * This function does not select/unselect the CS line.
1529 *
1530 * Returns 0 on success, a negative error code otherwise.
1531 */
1532static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1533 unsigned int len)
1534{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001535 unsigned int i;
1536 u8 *p = buf;
1537
1538 if (len && !buf)
1539 return -EINVAL;
1540
Miquel Raynal8878b122017-11-09 14:16:45 +01001541 if (chip->exec_op) {
1542 const struct nand_sdr_timings *sdr =
1543 nand_get_sdr_timings(&chip->data_interface);
1544 struct nand_op_instr instrs[] = {
1545 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1546 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1547 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1548 PSEC_TO_NSEC(sdr->tRR_min)),
1549 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1550 };
1551 struct nand_operation op = NAND_OPERATION(instrs);
1552
1553 /* Drop the DATA_IN instruction if len is set to 0. */
1554 if (!len)
1555 op.ninstrs--;
1556
1557 return nand_exec_op(chip, &op);
1558 }
1559
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001560 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001561 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001562 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001563
1564 return 0;
1565}
1566
1567/**
1568 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1569 * @chip: The NAND chip
1570 * @offset_in_page: offset within the page
1571 * @buf: buffer used to store the data
1572 * @len: length of the buffer
1573 * @force_8bit: force 8-bit bus access
1574 *
1575 * This function issues a CHANGE READ COLUMN operation.
1576 * This function does not select/unselect the CS line.
1577 *
1578 * Returns 0 on success, a negative error code otherwise.
1579 */
1580int nand_change_read_column_op(struct nand_chip *chip,
1581 unsigned int offset_in_page, void *buf,
1582 unsigned int len, bool force_8bit)
1583{
1584 struct mtd_info *mtd = nand_to_mtd(chip);
1585
1586 if (len && !buf)
1587 return -EINVAL;
1588
1589 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1590 return -EINVAL;
1591
Miquel Raynal8878b122017-11-09 14:16:45 +01001592 /* Small page NANDs do not support column change. */
1593 if (mtd->writesize <= 512)
1594 return -ENOTSUPP;
1595
1596 if (chip->exec_op) {
1597 const struct nand_sdr_timings *sdr =
1598 nand_get_sdr_timings(&chip->data_interface);
1599 u8 addrs[2] = {};
1600 struct nand_op_instr instrs[] = {
1601 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1602 NAND_OP_ADDR(2, addrs, 0),
1603 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1604 PSEC_TO_NSEC(sdr->tCCS_min)),
1605 NAND_OP_DATA_IN(len, buf, 0),
1606 };
1607 struct nand_operation op = NAND_OPERATION(instrs);
1608 int ret;
1609
1610 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1611 if (ret < 0)
1612 return ret;
1613
1614 /* Drop the DATA_IN instruction if len is set to 0. */
1615 if (!len)
1616 op.ninstrs--;
1617
1618 instrs[3].ctx.data.force_8bit = force_8bit;
1619
1620 return nand_exec_op(chip, &op);
1621 }
1622
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001623 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001624 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001625 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001626
1627 return 0;
1628}
1629EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1630
1631/**
1632 * nand_read_oob_op - Do a READ OOB operation
1633 * @chip: The NAND chip
1634 * @page: page to read
1635 * @offset_in_oob: offset within the OOB area
1636 * @buf: buffer used to store the data
1637 * @len: length of the buffer
1638 *
1639 * This function issues a READ OOB operation.
1640 * This function does not select/unselect the CS line.
1641 *
1642 * Returns 0 on success, a negative error code otherwise.
1643 */
1644int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1645 unsigned int offset_in_oob, void *buf, unsigned int len)
1646{
1647 struct mtd_info *mtd = nand_to_mtd(chip);
1648
1649 if (len && !buf)
1650 return -EINVAL;
1651
1652 if (offset_in_oob + len > mtd->oobsize)
1653 return -EINVAL;
1654
Miquel Raynal8878b122017-11-09 14:16:45 +01001655 if (chip->exec_op)
1656 return nand_read_page_op(chip, page,
1657 mtd->writesize + offset_in_oob,
1658 buf, len);
1659
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001660 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001661 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001662 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001663
1664 return 0;
1665}
1666EXPORT_SYMBOL_GPL(nand_read_oob_op);
1667
Miquel Raynal8878b122017-11-09 14:16:45 +01001668static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1669 unsigned int offset_in_page, const void *buf,
1670 unsigned int len, bool prog)
1671{
1672 struct mtd_info *mtd = nand_to_mtd(chip);
1673 const struct nand_sdr_timings *sdr =
1674 nand_get_sdr_timings(&chip->data_interface);
1675 u8 addrs[5] = {};
1676 struct nand_op_instr instrs[] = {
1677 /*
1678 * The first instruction will be dropped if we're dealing
1679 * with a large page NAND and adjusted if we're dealing
1680 * with a small page NAND and the page offset is > 255.
1681 */
1682 NAND_OP_CMD(NAND_CMD_READ0, 0),
1683 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1684 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1685 NAND_OP_DATA_OUT(len, buf, 0),
1686 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1687 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1688 };
1689 struct nand_operation op = NAND_OPERATION(instrs);
1690 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1691 int ret;
1692 u8 status;
1693
1694 if (naddrs < 0)
1695 return naddrs;
1696
1697 addrs[naddrs++] = page;
1698 addrs[naddrs++] = page >> 8;
1699 if (chip->options & NAND_ROW_ADDR_3)
1700 addrs[naddrs++] = page >> 16;
1701
1702 instrs[2].ctx.addr.naddrs = naddrs;
1703
1704 /* Drop the last two instructions if we're not programming the page. */
1705 if (!prog) {
1706 op.ninstrs -= 2;
1707 /* Also drop the DATA_OUT instruction if empty. */
1708 if (!len)
1709 op.ninstrs--;
1710 }
1711
1712 if (mtd->writesize <= 512) {
1713 /*
1714 * Small pages need some more tweaking: we have to adjust the
1715 * first instruction depending on the page offset we're trying
1716 * to access.
1717 */
1718 if (offset_in_page >= mtd->writesize)
1719 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1720 else if (offset_in_page >= 256 &&
1721 !(chip->options & NAND_BUSWIDTH_16))
1722 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1723 } else {
1724 /*
1725 * Drop the first command if we're dealing with a large page
1726 * NAND.
1727 */
1728 op.instrs++;
1729 op.ninstrs--;
1730 }
1731
1732 ret = nand_exec_op(chip, &op);
1733 if (!prog || ret)
1734 return ret;
1735
1736 ret = nand_status_op(chip, &status);
1737 if (ret)
1738 return ret;
1739
1740 return status;
1741}
1742
Boris Brezillon97d90da2017-11-30 18:01:29 +01001743/**
1744 * nand_prog_page_begin_op - starts a PROG PAGE operation
1745 * @chip: The NAND chip
1746 * @page: page to write
1747 * @offset_in_page: offset within the page
1748 * @buf: buffer containing the data to write to the page
1749 * @len: length of the buffer
1750 *
1751 * This function issues the first half of a PROG PAGE operation.
1752 * This function does not select/unselect the CS line.
1753 *
1754 * Returns 0 on success, a negative error code otherwise.
1755 */
1756int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1757 unsigned int offset_in_page, const void *buf,
1758 unsigned int len)
1759{
1760 struct mtd_info *mtd = nand_to_mtd(chip);
1761
1762 if (len && !buf)
1763 return -EINVAL;
1764
1765 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1766 return -EINVAL;
1767
Miquel Raynal8878b122017-11-09 14:16:45 +01001768 if (chip->exec_op)
1769 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1770 len, false);
1771
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001772 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001773
1774 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001775 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001776
1777 return 0;
1778}
1779EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1780
1781/**
1782 * nand_prog_page_end_op - ends a PROG PAGE operation
1783 * @chip: The NAND chip
1784 *
1785 * This function issues the second half of a PROG PAGE operation.
1786 * This function does not select/unselect the CS line.
1787 *
1788 * Returns 0 on success, a negative error code otherwise.
1789 */
1790int nand_prog_page_end_op(struct nand_chip *chip)
1791{
Miquel Raynal8878b122017-11-09 14:16:45 +01001792 int ret;
1793 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001794
Miquel Raynal8878b122017-11-09 14:16:45 +01001795 if (chip->exec_op) {
1796 const struct nand_sdr_timings *sdr =
1797 nand_get_sdr_timings(&chip->data_interface);
1798 struct nand_op_instr instrs[] = {
1799 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1800 PSEC_TO_NSEC(sdr->tWB_max)),
1801 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1802 };
1803 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001804
Miquel Raynal8878b122017-11-09 14:16:45 +01001805 ret = nand_exec_op(chip, &op);
1806 if (ret)
1807 return ret;
1808
1809 ret = nand_status_op(chip, &status);
1810 if (ret)
1811 return ret;
1812 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001813 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001814 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001815 if (ret < 0)
1816 return ret;
1817
1818 status = ret;
1819 }
1820
Boris Brezillon97d90da2017-11-30 18:01:29 +01001821 if (status & NAND_STATUS_FAIL)
1822 return -EIO;
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1827
1828/**
1829 * nand_prog_page_op - Do a full PROG PAGE operation
1830 * @chip: The NAND chip
1831 * @page: page to write
1832 * @offset_in_page: offset within the page
1833 * @buf: buffer containing the data to write to the page
1834 * @len: length of the buffer
1835 *
1836 * This function issues a full PROG PAGE operation.
1837 * This function does not select/unselect the CS line.
1838 *
1839 * Returns 0 on success, a negative error code otherwise.
1840 */
1841int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1842 unsigned int offset_in_page, const void *buf,
1843 unsigned int len)
1844{
1845 struct mtd_info *mtd = nand_to_mtd(chip);
1846 int status;
1847
1848 if (!len || !buf)
1849 return -EINVAL;
1850
1851 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1852 return -EINVAL;
1853
Miquel Raynal8878b122017-11-09 14:16:45 +01001854 if (chip->exec_op) {
1855 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1856 len, true);
1857 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001858 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1859 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001860 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001861 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001862 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001863 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001864
Boris Brezillon97d90da2017-11-30 18:01:29 +01001865 if (status & NAND_STATUS_FAIL)
1866 return -EIO;
1867
1868 return 0;
1869}
1870EXPORT_SYMBOL_GPL(nand_prog_page_op);
1871
1872/**
1873 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1874 * @chip: The NAND chip
1875 * @offset_in_page: offset within the page
1876 * @buf: buffer containing the data to send to the NAND
1877 * @len: length of the buffer
1878 * @force_8bit: force 8-bit bus access
1879 *
1880 * This function issues a CHANGE WRITE COLUMN operation.
1881 * This function does not select/unselect the CS line.
1882 *
1883 * Returns 0 on success, a negative error code otherwise.
1884 */
1885int nand_change_write_column_op(struct nand_chip *chip,
1886 unsigned int offset_in_page,
1887 const void *buf, unsigned int len,
1888 bool force_8bit)
1889{
1890 struct mtd_info *mtd = nand_to_mtd(chip);
1891
1892 if (len && !buf)
1893 return -EINVAL;
1894
1895 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1896 return -EINVAL;
1897
Miquel Raynal8878b122017-11-09 14:16:45 +01001898 /* Small page NANDs do not support column change. */
1899 if (mtd->writesize <= 512)
1900 return -ENOTSUPP;
1901
1902 if (chip->exec_op) {
1903 const struct nand_sdr_timings *sdr =
1904 nand_get_sdr_timings(&chip->data_interface);
1905 u8 addrs[2];
1906 struct nand_op_instr instrs[] = {
1907 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1908 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1909 NAND_OP_DATA_OUT(len, buf, 0),
1910 };
1911 struct nand_operation op = NAND_OPERATION(instrs);
1912 int ret;
1913
1914 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1915 if (ret < 0)
1916 return ret;
1917
1918 instrs[2].ctx.data.force_8bit = force_8bit;
1919
1920 /* Drop the DATA_OUT instruction if len is set to 0. */
1921 if (!len)
1922 op.ninstrs--;
1923
1924 return nand_exec_op(chip, &op);
1925 }
1926
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001927 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001928 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001929 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001930
1931 return 0;
1932}
1933EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1934
1935/**
1936 * nand_readid_op - Do a READID operation
1937 * @chip: The NAND chip
1938 * @addr: address cycle to pass after the READID command
1939 * @buf: buffer used to store the ID
1940 * @len: length of the buffer
1941 *
1942 * This function sends a READID command and reads back the ID returned by the
1943 * NAND.
1944 * This function does not select/unselect the CS line.
1945 *
1946 * Returns 0 on success, a negative error code otherwise.
1947 */
1948int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1949 unsigned int len)
1950{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001951 unsigned int i;
1952 u8 *id = buf;
1953
1954 if (len && !buf)
1955 return -EINVAL;
1956
Miquel Raynal8878b122017-11-09 14:16:45 +01001957 if (chip->exec_op) {
1958 const struct nand_sdr_timings *sdr =
1959 nand_get_sdr_timings(&chip->data_interface);
1960 struct nand_op_instr instrs[] = {
1961 NAND_OP_CMD(NAND_CMD_READID, 0),
1962 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1963 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1964 };
1965 struct nand_operation op = NAND_OPERATION(instrs);
1966
1967 /* Drop the DATA_IN instruction if len is set to 0. */
1968 if (!len)
1969 op.ninstrs--;
1970
1971 return nand_exec_op(chip, &op);
1972 }
1973
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001974 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001975
1976 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001977 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001978
1979 return 0;
1980}
1981EXPORT_SYMBOL_GPL(nand_readid_op);
1982
1983/**
1984 * nand_status_op - Do a STATUS operation
1985 * @chip: The NAND chip
1986 * @status: out variable to store the NAND status
1987 *
1988 * This function sends a STATUS command and reads back the status returned by
1989 * the NAND.
1990 * This function does not select/unselect the CS line.
1991 *
1992 * Returns 0 on success, a negative error code otherwise.
1993 */
1994int nand_status_op(struct nand_chip *chip, u8 *status)
1995{
Miquel Raynal8878b122017-11-09 14:16:45 +01001996 if (chip->exec_op) {
1997 const struct nand_sdr_timings *sdr =
1998 nand_get_sdr_timings(&chip->data_interface);
1999 struct nand_op_instr instrs[] = {
2000 NAND_OP_CMD(NAND_CMD_STATUS,
2001 PSEC_TO_NSEC(sdr->tADL_min)),
2002 NAND_OP_8BIT_DATA_IN(1, status, 0),
2003 };
2004 struct nand_operation op = NAND_OPERATION(instrs);
2005
2006 if (!status)
2007 op.ninstrs--;
2008
2009 return nand_exec_op(chip, &op);
2010 }
2011
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002012 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002013 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002014 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002015
2016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(nand_status_op);
2019
2020/**
2021 * nand_exit_status_op - Exit a STATUS operation
2022 * @chip: The NAND chip
2023 *
2024 * This function sends a READ0 command to cancel the effect of the STATUS
2025 * command to avoid reading only the status until a new read command is sent.
2026 *
2027 * This function does not select/unselect the CS line.
2028 *
2029 * Returns 0 on success, a negative error code otherwise.
2030 */
2031int nand_exit_status_op(struct nand_chip *chip)
2032{
Miquel Raynal8878b122017-11-09 14:16:45 +01002033 if (chip->exec_op) {
2034 struct nand_op_instr instrs[] = {
2035 NAND_OP_CMD(NAND_CMD_READ0, 0),
2036 };
2037 struct nand_operation op = NAND_OPERATION(instrs);
2038
2039 return nand_exec_op(chip, &op);
2040 }
2041
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002042 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002043
2044 return 0;
2045}
Boris Brezillon97d90da2017-11-30 18:01:29 +01002046
2047/**
2048 * nand_erase_op - Do an erase operation
2049 * @chip: The NAND chip
2050 * @eraseblock: block to erase
2051 *
2052 * This function sends an ERASE command and waits for the NAND to be ready
2053 * before returning.
2054 * This function does not select/unselect the CS line.
2055 *
2056 * Returns 0 on success, a negative error code otherwise.
2057 */
2058int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2059{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002060 unsigned int page = eraseblock <<
2061 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002062 int ret;
2063 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002064
Miquel Raynal8878b122017-11-09 14:16:45 +01002065 if (chip->exec_op) {
2066 const struct nand_sdr_timings *sdr =
2067 nand_get_sdr_timings(&chip->data_interface);
2068 u8 addrs[3] = { page, page >> 8, page >> 16 };
2069 struct nand_op_instr instrs[] = {
2070 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2071 NAND_OP_ADDR(2, addrs, 0),
2072 NAND_OP_CMD(NAND_CMD_ERASE2,
2073 PSEC_TO_MSEC(sdr->tWB_max)),
2074 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2075 };
2076 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002077
Miquel Raynal8878b122017-11-09 14:16:45 +01002078 if (chip->options & NAND_ROW_ADDR_3)
2079 instrs[1].ctx.addr.naddrs++;
2080
2081 ret = nand_exec_op(chip, &op);
2082 if (ret)
2083 return ret;
2084
2085 ret = nand_status_op(chip, &status);
2086 if (ret)
2087 return ret;
2088 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002089 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2090 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002091
Boris Brezillon8395b752018-09-07 00:38:37 +02002092 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002093 if (ret < 0)
2094 return ret;
2095
2096 status = ret;
2097 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002098
2099 if (status & NAND_STATUS_FAIL)
2100 return -EIO;
2101
2102 return 0;
2103}
2104EXPORT_SYMBOL_GPL(nand_erase_op);
2105
2106/**
2107 * nand_set_features_op - Do a SET FEATURES operation
2108 * @chip: The NAND chip
2109 * @feature: feature id
2110 * @data: 4 bytes of data
2111 *
2112 * This function sends a SET FEATURES command and waits for the NAND to be
2113 * ready before returning.
2114 * This function does not select/unselect the CS line.
2115 *
2116 * Returns 0 on success, a negative error code otherwise.
2117 */
2118static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2119 const void *data)
2120{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002121 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002122 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002123
Miquel Raynal8878b122017-11-09 14:16:45 +01002124 if (chip->exec_op) {
2125 const struct nand_sdr_timings *sdr =
2126 nand_get_sdr_timings(&chip->data_interface);
2127 struct nand_op_instr instrs[] = {
2128 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2129 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2130 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2131 PSEC_TO_NSEC(sdr->tWB_max)),
2132 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2133 };
2134 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002135
Boris Brezillon782d1962018-05-11 14:44:07 +02002136 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002137 }
2138
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002139 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002140 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002141 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002142
Boris Brezillon8395b752018-09-07 00:38:37 +02002143 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002144 if (ret < 0)
2145 return ret;
2146
2147 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002148 return -EIO;
2149
2150 return 0;
2151}
2152
2153/**
2154 * nand_get_features_op - Do a GET FEATURES operation
2155 * @chip: The NAND chip
2156 * @feature: feature id
2157 * @data: 4 bytes of data
2158 *
2159 * This function sends a GET FEATURES command and waits for the NAND to be
2160 * ready before returning.
2161 * This function does not select/unselect the CS line.
2162 *
2163 * Returns 0 on success, a negative error code otherwise.
2164 */
2165static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2166 void *data)
2167{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002168 u8 *params = data;
2169 int i;
2170
Miquel Raynal8878b122017-11-09 14:16:45 +01002171 if (chip->exec_op) {
2172 const struct nand_sdr_timings *sdr =
2173 nand_get_sdr_timings(&chip->data_interface);
2174 struct nand_op_instr instrs[] = {
2175 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2176 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2177 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2178 PSEC_TO_NSEC(sdr->tRR_min)),
2179 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2180 data, 0),
2181 };
2182 struct nand_operation op = NAND_OPERATION(instrs);
2183
2184 return nand_exec_op(chip, &op);
2185 }
2186
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002187 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002188 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002189 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002190
2191 return 0;
2192}
2193
Boris Brezillon52f05b62018-07-27 09:44:18 +02002194static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2195 unsigned int delay_ns)
2196{
2197 if (chip->exec_op) {
2198 struct nand_op_instr instrs[] = {
2199 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2200 PSEC_TO_NSEC(delay_ns)),
2201 };
2202 struct nand_operation op = NAND_OPERATION(instrs);
2203
2204 return nand_exec_op(chip, &op);
2205 }
2206
2207 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02002208 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02002209 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002210 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002211 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002212
2213 return 0;
2214}
2215
Boris Brezillon97d90da2017-11-30 18:01:29 +01002216/**
2217 * nand_reset_op - Do a reset operation
2218 * @chip: The NAND chip
2219 *
2220 * This function sends a RESET command and waits for the NAND to be ready
2221 * before returning.
2222 * This function does not select/unselect the CS line.
2223 *
2224 * Returns 0 on success, a negative error code otherwise.
2225 */
2226int nand_reset_op(struct nand_chip *chip)
2227{
Miquel Raynal8878b122017-11-09 14:16:45 +01002228 if (chip->exec_op) {
2229 const struct nand_sdr_timings *sdr =
2230 nand_get_sdr_timings(&chip->data_interface);
2231 struct nand_op_instr instrs[] = {
2232 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2233 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2234 };
2235 struct nand_operation op = NAND_OPERATION(instrs);
2236
2237 return nand_exec_op(chip, &op);
2238 }
2239
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002240 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002241
2242 return 0;
2243}
2244EXPORT_SYMBOL_GPL(nand_reset_op);
2245
2246/**
2247 * nand_read_data_op - Read data from the NAND
2248 * @chip: The NAND chip
2249 * @buf: buffer used to store the data
2250 * @len: length of the buffer
2251 * @force_8bit: force 8-bit bus access
2252 *
2253 * This function does a raw data read on the bus. Usually used after launching
2254 * another NAND operation like nand_read_page_op().
2255 * This function does not select/unselect the CS line.
2256 *
2257 * Returns 0 on success, a negative error code otherwise.
2258 */
2259int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2260 bool force_8bit)
2261{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002262 if (!len || !buf)
2263 return -EINVAL;
2264
Miquel Raynal8878b122017-11-09 14:16:45 +01002265 if (chip->exec_op) {
2266 struct nand_op_instr instrs[] = {
2267 NAND_OP_DATA_IN(len, buf, 0),
2268 };
2269 struct nand_operation op = NAND_OPERATION(instrs);
2270
2271 instrs[0].ctx.data.force_8bit = force_8bit;
2272
2273 return nand_exec_op(chip, &op);
2274 }
2275
Boris Brezillon97d90da2017-11-30 18:01:29 +01002276 if (force_8bit) {
2277 u8 *p = buf;
2278 unsigned int i;
2279
2280 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002281 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002282 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002283 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002284 }
2285
2286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(nand_read_data_op);
2289
2290/**
2291 * nand_write_data_op - Write data from the NAND
2292 * @chip: The NAND chip
2293 * @buf: buffer containing the data to send on the bus
2294 * @len: length of the buffer
2295 * @force_8bit: force 8-bit bus access
2296 *
2297 * This function does a raw data write on the bus. Usually used after launching
2298 * another NAND operation like nand_write_page_begin_op().
2299 * This function does not select/unselect the CS line.
2300 *
2301 * Returns 0 on success, a negative error code otherwise.
2302 */
2303int nand_write_data_op(struct nand_chip *chip, const void *buf,
2304 unsigned int len, bool force_8bit)
2305{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002306 if (!len || !buf)
2307 return -EINVAL;
2308
Miquel Raynal8878b122017-11-09 14:16:45 +01002309 if (chip->exec_op) {
2310 struct nand_op_instr instrs[] = {
2311 NAND_OP_DATA_OUT(len, buf, 0),
2312 };
2313 struct nand_operation op = NAND_OPERATION(instrs);
2314
2315 instrs[0].ctx.data.force_8bit = force_8bit;
2316
2317 return nand_exec_op(chip, &op);
2318 }
2319
Boris Brezillon97d90da2017-11-30 18:01:29 +01002320 if (force_8bit) {
2321 const u8 *p = buf;
2322 unsigned int i;
2323
2324 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002325 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002326 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002327 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002328 }
2329
2330 return 0;
2331}
2332EXPORT_SYMBOL_GPL(nand_write_data_op);
2333
2334/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002335 * struct nand_op_parser_ctx - Context used by the parser
2336 * @instrs: array of all the instructions that must be addressed
2337 * @ninstrs: length of the @instrs array
2338 * @subop: Sub-operation to be passed to the NAND controller
2339 *
2340 * This structure is used by the core to split NAND operations into
2341 * sub-operations that can be handled by the NAND controller.
2342 */
2343struct nand_op_parser_ctx {
2344 const struct nand_op_instr *instrs;
2345 unsigned int ninstrs;
2346 struct nand_subop subop;
2347};
2348
2349/**
2350 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2351 * @pat: the parser pattern element that matches @instr
2352 * @instr: pointer to the instruction to check
2353 * @start_offset: this is an in/out parameter. If @instr has already been
2354 * split, then @start_offset is the offset from which to start
2355 * (either an address cycle or an offset in the data buffer).
2356 * Conversely, if the function returns true (ie. instr must be
2357 * split), this parameter is updated to point to the first
2358 * data/address cycle that has not been taken care of.
2359 *
2360 * Some NAND controllers are limited and cannot send X address cycles with a
2361 * unique operation, or cannot read/write more than Y bytes at the same time.
2362 * In this case, split the instruction that does not fit in a single
2363 * controller-operation into two or more chunks.
2364 *
2365 * Returns true if the instruction must be split, false otherwise.
2366 * The @start_offset parameter is also updated to the offset at which the next
2367 * bundle of instruction must start (if an address or a data instruction).
2368 */
2369static bool
2370nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2371 const struct nand_op_instr *instr,
2372 unsigned int *start_offset)
2373{
2374 switch (pat->type) {
2375 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002376 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002377 break;
2378
2379 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002380 pat->ctx.addr.maxcycles) {
2381 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002382 return true;
2383 }
2384 break;
2385
2386 case NAND_OP_DATA_IN_INSTR:
2387 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002388 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002389 break;
2390
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002391 if (instr->ctx.data.len - *start_offset >
2392 pat->ctx.data.maxlen) {
2393 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002394 return true;
2395 }
2396 break;
2397
2398 default:
2399 break;
2400 }
2401
2402 return false;
2403}
2404
2405/**
2406 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2407 * remaining in the parser context
2408 * @pat: the pattern to test
2409 * @ctx: the parser context structure to match with the pattern @pat
2410 *
2411 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2412 * Returns true if this is the case, false ortherwise. When true is returned,
2413 * @ctx->subop is updated with the set of instructions to be passed to the
2414 * controller driver.
2415 */
2416static bool
2417nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2418 struct nand_op_parser_ctx *ctx)
2419{
2420 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2421 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2422 const struct nand_op_instr *instr = ctx->subop.instrs;
2423 unsigned int i, ninstrs;
2424
2425 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2426 /*
2427 * The pattern instruction does not match the operation
2428 * instruction. If the instruction is marked optional in the
2429 * pattern definition, we skip the pattern element and continue
2430 * to the next one. If the element is mandatory, there's no
2431 * match and we can return false directly.
2432 */
2433 if (instr->type != pat->elems[i].type) {
2434 if (!pat->elems[i].optional)
2435 return false;
2436
2437 continue;
2438 }
2439
2440 /*
2441 * Now check the pattern element constraints. If the pattern is
2442 * not able to handle the whole instruction in a single step,
2443 * we have to split it.
2444 * The last_instr_end_off value comes back updated to point to
2445 * the position where we have to split the instruction (the
2446 * start of the next subop chunk).
2447 */
2448 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2449 &instr_offset)) {
2450 ninstrs++;
2451 i++;
2452 break;
2453 }
2454
2455 instr++;
2456 ninstrs++;
2457 instr_offset = 0;
2458 }
2459
2460 /*
2461 * This can happen if all instructions of a pattern are optional.
2462 * Still, if there's not at least one instruction handled by this
2463 * pattern, this is not a match, and we should try the next one (if
2464 * any).
2465 */
2466 if (!ninstrs)
2467 return false;
2468
2469 /*
2470 * We had a match on the pattern head, but the pattern may be longer
2471 * than the instructions we're asked to execute. We need to make sure
2472 * there's no mandatory elements in the pattern tail.
2473 */
2474 for (; i < pat->nelems; i++) {
2475 if (!pat->elems[i].optional)
2476 return false;
2477 }
2478
2479 /*
2480 * We have a match: update the subop structure accordingly and return
2481 * true.
2482 */
2483 ctx->subop.ninstrs = ninstrs;
2484 ctx->subop.last_instr_end_off = instr_offset;
2485
2486 return true;
2487}
2488
2489#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2490static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2491{
2492 const struct nand_op_instr *instr;
2493 char *prefix = " ";
2494 unsigned int i;
2495
2496 pr_debug("executing subop:\n");
2497
2498 for (i = 0; i < ctx->ninstrs; i++) {
2499 instr = &ctx->instrs[i];
2500
2501 if (instr == &ctx->subop.instrs[0])
2502 prefix = " ->";
2503
2504 switch (instr->type) {
2505 case NAND_OP_CMD_INSTR:
2506 pr_debug("%sCMD [0x%02x]\n", prefix,
2507 instr->ctx.cmd.opcode);
2508 break;
2509 case NAND_OP_ADDR_INSTR:
2510 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2511 instr->ctx.addr.naddrs,
2512 instr->ctx.addr.naddrs < 64 ?
2513 instr->ctx.addr.naddrs : 64,
2514 instr->ctx.addr.addrs);
2515 break;
2516 case NAND_OP_DATA_IN_INSTR:
2517 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2518 instr->ctx.data.len,
2519 instr->ctx.data.force_8bit ?
2520 ", force 8-bit" : "");
2521 break;
2522 case NAND_OP_DATA_OUT_INSTR:
2523 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2524 instr->ctx.data.len,
2525 instr->ctx.data.force_8bit ?
2526 ", force 8-bit" : "");
2527 break;
2528 case NAND_OP_WAITRDY_INSTR:
2529 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2530 instr->ctx.waitrdy.timeout_ms);
2531 break;
2532 }
2533
2534 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2535 prefix = " ";
2536 }
2537}
2538#else
2539static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2540{
2541 /* NOP */
2542}
2543#endif
2544
2545/**
2546 * nand_op_parser_exec_op - exec_op parser
2547 * @chip: the NAND chip
2548 * @parser: patterns description provided by the controller driver
2549 * @op: the NAND operation to address
2550 * @check_only: when true, the function only checks if @op can be handled but
2551 * does not execute the operation
2552 *
2553 * Helper function designed to ease integration of NAND controller drivers that
2554 * only support a limited set of instruction sequences. The supported sequences
2555 * are described in @parser, and the framework takes care of splitting @op into
2556 * multiple sub-operations (if required) and pass them back to the ->exec()
2557 * callback of the matching pattern if @check_only is set to false.
2558 *
2559 * NAND controller drivers should call this function from their own ->exec_op()
2560 * implementation.
2561 *
2562 * Returns 0 on success, a negative error code otherwise. A failure can be
2563 * caused by an unsupported operation (none of the supported patterns is able
2564 * to handle the requested operation), or an error returned by one of the
2565 * matching pattern->exec() hook.
2566 */
2567int nand_op_parser_exec_op(struct nand_chip *chip,
2568 const struct nand_op_parser *parser,
2569 const struct nand_operation *op, bool check_only)
2570{
2571 struct nand_op_parser_ctx ctx = {
2572 .subop.instrs = op->instrs,
2573 .instrs = op->instrs,
2574 .ninstrs = op->ninstrs,
2575 };
2576 unsigned int i;
2577
2578 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2579 int ret;
2580
2581 for (i = 0; i < parser->npatterns; i++) {
2582 const struct nand_op_parser_pattern *pattern;
2583
2584 pattern = &parser->patterns[i];
2585 if (!nand_op_parser_match_pat(pattern, &ctx))
2586 continue;
2587
2588 nand_op_parser_trace(&ctx);
2589
2590 if (check_only)
2591 break;
2592
2593 ret = pattern->exec(chip, &ctx.subop);
2594 if (ret)
2595 return ret;
2596
2597 break;
2598 }
2599
2600 if (i == parser->npatterns) {
2601 pr_debug("->exec_op() parser: pattern not found!\n");
2602 return -ENOTSUPP;
2603 }
2604
2605 /*
2606 * Update the context structure by pointing to the start of the
2607 * next subop.
2608 */
2609 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2610 if (ctx.subop.last_instr_end_off)
2611 ctx.subop.instrs -= 1;
2612
2613 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2614 }
2615
2616 return 0;
2617}
2618EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2619
2620static bool nand_instr_is_data(const struct nand_op_instr *instr)
2621{
2622 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2623 instr->type == NAND_OP_DATA_OUT_INSTR);
2624}
2625
2626static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2627 unsigned int instr_idx)
2628{
2629 return subop && instr_idx < subop->ninstrs;
2630}
2631
Miquel Raynal760c4352018-07-19 00:09:12 +02002632static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2633 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002634{
2635 if (instr_idx)
2636 return 0;
2637
2638 return subop->first_instr_start_off;
2639}
2640
2641/**
2642 * nand_subop_get_addr_start_off - Get the start offset in an address array
2643 * @subop: The entire sub-operation
2644 * @instr_idx: Index of the instruction inside the sub-operation
2645 *
2646 * During driver development, one could be tempted to directly use the
2647 * ->addr.addrs field of address instructions. This is wrong as address
2648 * instructions might be split.
2649 *
2650 * Given an address instruction, returns the offset of the first cycle to issue.
2651 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002652unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2653 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002654{
Miquel Raynal760c4352018-07-19 00:09:12 +02002655 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2656 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2657 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002658
2659 return nand_subop_get_start_off(subop, instr_idx);
2660}
2661EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2662
2663/**
2664 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2665 * @subop: The entire sub-operation
2666 * @instr_idx: Index of the instruction inside the sub-operation
2667 *
2668 * During driver development, one could be tempted to directly use the
2669 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2670 * might be split.
2671 *
2672 * Given an address instruction, returns the number of address cycle to issue.
2673 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002674unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2675 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002676{
2677 int start_off, end_off;
2678
Miquel Raynal760c4352018-07-19 00:09:12 +02002679 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2680 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2681 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002682
2683 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2684
2685 if (instr_idx == subop->ninstrs - 1 &&
2686 subop->last_instr_end_off)
2687 end_off = subop->last_instr_end_off;
2688 else
2689 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2690
2691 return end_off - start_off;
2692}
2693EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2694
2695/**
2696 * nand_subop_get_data_start_off - Get the start offset in a data array
2697 * @subop: The entire sub-operation
2698 * @instr_idx: Index of the instruction inside the sub-operation
2699 *
2700 * During driver development, one could be tempted to directly use the
2701 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2702 * instructions might be split.
2703 *
2704 * Given a data instruction, returns the offset to start from.
2705 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002706unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2707 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002708{
Miquel Raynal760c4352018-07-19 00:09:12 +02002709 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2710 !nand_instr_is_data(&subop->instrs[instr_idx])))
2711 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002712
2713 return nand_subop_get_start_off(subop, instr_idx);
2714}
2715EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2716
2717/**
2718 * nand_subop_get_data_len - Get the number of bytes to retrieve
2719 * @subop: The entire sub-operation
2720 * @instr_idx: Index of the instruction inside the sub-operation
2721 *
2722 * During driver development, one could be tempted to directly use the
2723 * ->data->len field of a data instruction. This is wrong as data instructions
2724 * might be split.
2725 *
2726 * Returns the length of the chunk of data to send/receive.
2727 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002728unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2729 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002730{
2731 int start_off = 0, end_off;
2732
Miquel Raynal760c4352018-07-19 00:09:12 +02002733 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2734 !nand_instr_is_data(&subop->instrs[instr_idx])))
2735 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002736
2737 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2738
2739 if (instr_idx == subop->ninstrs - 1 &&
2740 subop->last_instr_end_off)
2741 end_off = subop->last_instr_end_off;
2742 else
2743 end_off = subop->instrs[instr_idx].ctx.data.len;
2744
2745 return end_off - start_off;
2746}
2747EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002750 * nand_reset - Reset and initialize a NAND device
2751 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002752 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002753 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002754 * Save the timings data structure, then apply SDR timings mode 0 (see
2755 * nand_reset_data_interface for details), do the reset operation, and
2756 * apply back the previous timings.
2757 *
2758 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002759 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002760int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002761{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002762 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002763 int ret;
2764
Boris Brezillon104e4422017-03-16 09:35:58 +01002765 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002766 if (ret)
2767 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002768
Boris Brezillon73f907f2016-10-24 16:46:20 +02002769 /*
2770 * The CS line has to be released before we can apply the new NAND
2771 * interface settings, hence this weird ->select_chip() dance.
2772 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002773 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002774 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002775 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002776 if (ret)
2777 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002778
Miquel Raynal107b7d62018-03-19 14:47:25 +01002779 /*
2780 * A nand_reset_data_interface() put both the NAND chip and the NAND
2781 * controller in timings mode 0. If the default mode for this chip is
2782 * also 0, no need to proceed to the change again. Plus, at probe time,
2783 * nand_setup_data_interface() uses ->set/get_features() which would
2784 * fail anyway as the parameter page is not available yet.
2785 */
2786 if (!chip->onfi_timing_mode_default)
2787 return 0;
2788
Miquel Raynal17fa8042017-11-30 18:01:31 +01002789 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002790 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002791 if (ret)
2792 return ret;
2793
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002794 return 0;
2795}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002796EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002797
2798/**
Boris Brezillon45240362018-09-07 00:38:40 +02002799 * nand_get_features - wrapper to perform a GET_FEATURE
2800 * @chip: NAND chip info structure
2801 * @addr: feature address
2802 * @subfeature_param: the subfeature parameters, a four bytes array
2803 *
2804 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2805 * operation cannot be handled.
2806 */
2807int nand_get_features(struct nand_chip *chip, int addr,
2808 u8 *subfeature_param)
2809{
2810 if (!nand_supports_get_features(chip, addr))
2811 return -ENOTSUPP;
2812
2813 if (chip->legacy.get_features)
2814 return chip->legacy.get_features(chip, addr, subfeature_param);
2815
2816 return nand_get_features_op(chip, addr, subfeature_param);
2817}
Boris Brezillon45240362018-09-07 00:38:40 +02002818
2819/**
2820 * nand_set_features - wrapper to perform a SET_FEATURE
2821 * @chip: NAND chip info structure
2822 * @addr: feature address
2823 * @subfeature_param: the subfeature parameters, a four bytes array
2824 *
2825 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2826 * operation cannot be handled.
2827 */
2828int nand_set_features(struct nand_chip *chip, int addr,
2829 u8 *subfeature_param)
2830{
2831 if (!nand_supports_set_features(chip, addr))
2832 return -ENOTSUPP;
2833
2834 if (chip->legacy.set_features)
2835 return chip->legacy.set_features(chip, addr, subfeature_param);
2836
2837 return nand_set_features_op(chip, addr, subfeature_param);
2838}
Boris Brezillon45240362018-09-07 00:38:40 +02002839
2840/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002841 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2842 * @buf: buffer to test
2843 * @len: buffer length
2844 * @bitflips_threshold: maximum number of bitflips
2845 *
2846 * Check if a buffer contains only 0xff, which means the underlying region
2847 * has been erased and is ready to be programmed.
2848 * The bitflips_threshold specify the maximum number of bitflips before
2849 * considering the region is not erased.
2850 * Note: The logic of this function has been extracted from the memweight
2851 * implementation, except that nand_check_erased_buf function exit before
2852 * testing the whole buffer if the number of bitflips exceed the
2853 * bitflips_threshold value.
2854 *
2855 * Returns a positive number of bitflips less than or equal to
2856 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2857 * threshold.
2858 */
2859static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2860{
2861 const unsigned char *bitmap = buf;
2862 int bitflips = 0;
2863 int weight;
2864
2865 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2866 len--, bitmap++) {
2867 weight = hweight8(*bitmap);
2868 bitflips += BITS_PER_BYTE - weight;
2869 if (unlikely(bitflips > bitflips_threshold))
2870 return -EBADMSG;
2871 }
2872
2873 for (; len >= sizeof(long);
2874 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002875 unsigned long d = *((unsigned long *)bitmap);
2876 if (d == ~0UL)
2877 continue;
2878 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002879 bitflips += BITS_PER_LONG - weight;
2880 if (unlikely(bitflips > bitflips_threshold))
2881 return -EBADMSG;
2882 }
2883
2884 for (; len > 0; len--, bitmap++) {
2885 weight = hweight8(*bitmap);
2886 bitflips += BITS_PER_BYTE - weight;
2887 if (unlikely(bitflips > bitflips_threshold))
2888 return -EBADMSG;
2889 }
2890
2891 return bitflips;
2892}
2893
2894/**
2895 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2896 * 0xff data
2897 * @data: data buffer to test
2898 * @datalen: data length
2899 * @ecc: ECC buffer
2900 * @ecclen: ECC length
2901 * @extraoob: extra OOB buffer
2902 * @extraooblen: extra OOB length
2903 * @bitflips_threshold: maximum number of bitflips
2904 *
2905 * Check if a data buffer and its associated ECC and OOB data contains only
2906 * 0xff pattern, which means the underlying region has been erased and is
2907 * ready to be programmed.
2908 * The bitflips_threshold specify the maximum number of bitflips before
2909 * considering the region as not erased.
2910 *
2911 * Note:
2912 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2913 * different from the NAND page size. When fixing bitflips, ECC engines will
2914 * report the number of errors per chunk, and the NAND core infrastructure
2915 * expect you to return the maximum number of bitflips for the whole page.
2916 * This is why you should always use this function on a single chunk and
2917 * not on the whole page. After checking each chunk you should update your
2918 * max_bitflips value accordingly.
2919 * 2/ When checking for bitflips in erased pages you should not only check
2920 * the payload data but also their associated ECC data, because a user might
2921 * have programmed almost all bits to 1 but a few. In this case, we
2922 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2923 * this case.
2924 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2925 * data are protected by the ECC engine.
2926 * It could also be used if you support subpages and want to attach some
2927 * extra OOB data to an ECC chunk.
2928 *
2929 * Returns a positive number of bitflips less than or equal to
2930 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2931 * threshold. In case of success, the passed buffers are filled with 0xff.
2932 */
2933int nand_check_erased_ecc_chunk(void *data, int datalen,
2934 void *ecc, int ecclen,
2935 void *extraoob, int extraooblen,
2936 int bitflips_threshold)
2937{
2938 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2939
2940 data_bitflips = nand_check_erased_buf(data, datalen,
2941 bitflips_threshold);
2942 if (data_bitflips < 0)
2943 return data_bitflips;
2944
2945 bitflips_threshold -= data_bitflips;
2946
2947 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2948 if (ecc_bitflips < 0)
2949 return ecc_bitflips;
2950
2951 bitflips_threshold -= ecc_bitflips;
2952
2953 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2954 bitflips_threshold);
2955 if (extraoob_bitflips < 0)
2956 return extraoob_bitflips;
2957
2958 if (data_bitflips)
2959 memset(data, 0xff, datalen);
2960
2961 if (ecc_bitflips)
2962 memset(ecc, 0xff, ecclen);
2963
2964 if (extraoob_bitflips)
2965 memset(extraoob, 0xff, extraooblen);
2966
2967 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2968}
2969EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2970
2971/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002972 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002973 * @chip: nand chip info structure
2974 * @buf: buffer to store read data
2975 * @oob_required: caller requires OOB data read to chip->oob_poi
2976 * @page: page number to read
2977 *
2978 * Returns -ENOTSUPP unconditionally.
2979 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002980int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2981 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002982{
2983 return -ENOTSUPP;
2984}
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002985
2986/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002987 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002988 * @chip: nand chip info structure
2989 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002990 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002991 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002992 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002993 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002994 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002995int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2996 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997{
Boris Brezillonb9761682018-09-06 14:05:20 +02002998 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002999 int ret;
3000
Boris Brezillon25f815f2017-11-30 18:01:30 +01003001 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003002 if (ret)
3003 return ret;
3004
3005 if (oob_required) {
3006 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3007 false);
3008 if (ret)
3009 return ret;
3010 }
3011
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003012 return 0;
3013}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003014EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003015
3016/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003017 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003018 * @chip: nand chip info structure
3019 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003020 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003021 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003022 *
3023 * We need a special oob layout and handling even when OOB isn't used.
3024 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003025static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003026 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003027{
Boris Brezillonb9761682018-09-06 14:05:20 +02003028 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003029 int eccsize = chip->ecc.size;
3030 int eccbytes = chip->ecc.bytes;
3031 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003032 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003033
Boris Brezillon25f815f2017-11-30 18:01:30 +01003034 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3035 if (ret)
3036 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003037
3038 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003039 ret = nand_read_data_op(chip, buf, eccsize, false);
3040 if (ret)
3041 return ret;
3042
David Brownell52ff49d2009-03-04 12:01:36 -08003043 buf += eccsize;
3044
3045 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003046 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3047 false);
3048 if (ret)
3049 return ret;
3050
David Brownell52ff49d2009-03-04 12:01:36 -08003051 oob += chip->ecc.prepad;
3052 }
3053
Boris Brezillon97d90da2017-11-30 18:01:29 +01003054 ret = nand_read_data_op(chip, oob, eccbytes, false);
3055 if (ret)
3056 return ret;
3057
David Brownell52ff49d2009-03-04 12:01:36 -08003058 oob += eccbytes;
3059
3060 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003061 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3062 false);
3063 if (ret)
3064 return ret;
3065
David Brownell52ff49d2009-03-04 12:01:36 -08003066 oob += chip->ecc.postpad;
3067 }
3068 }
3069
3070 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003071 if (size) {
3072 ret = nand_read_data_op(chip, oob, size, false);
3073 if (ret)
3074 return ret;
3075 }
David Brownell52ff49d2009-03-04 12:01:36 -08003076
3077 return 0;
3078}
3079
3080/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003081 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003082 * @chip: nand chip info structure
3083 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003084 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003085 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003086 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003087static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3088 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Boris Brezillonb9761682018-09-06 14:05:20 +02003090 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003091 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003092 int eccbytes = chip->ecc.bytes;
3093 int eccsteps = chip->ecc.steps;
3094 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003095 uint8_t *ecc_calc = chip->ecc.calc_buf;
3096 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003097 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003098
Boris Brezillonb9761682018-09-06 14:05:20 +02003099 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003100
3101 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003102 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003103
Boris Brezillon846031d2016-02-03 20:11:00 +01003104 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3105 chip->ecc.total);
3106 if (ret)
3107 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003108
3109 eccsteps = chip->ecc.steps;
3110 p = buf;
3111
3112 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3113 int stat;
3114
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003115 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003116 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003117 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003118 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003119 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003120 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3121 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003122 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003123 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303127 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003128 * @chip: nand chip info structure
3129 * @data_offs: offset of requested data within the page
3130 * @readlen: data length
3131 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003132 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003133 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003134static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3135 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003136{
Boris Brezillonb9761682018-09-06 14:05:20 +02003137 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003138 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003139 uint8_t *p;
3140 int data_col_addr, i, gaps = 0;
3141 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3142 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003143 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003144 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003145 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003146
Brian Norris7854d3f2011-06-23 14:12:08 -07003147 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003148 start_step = data_offs / chip->ecc.size;
3149 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3150 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303151 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003152
Brian Norris8b6e50c2011-05-25 14:59:01 -07003153 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003154 datafrag_len = num_steps * chip->ecc.size;
3155 eccfrag_len = num_steps * chip->ecc.bytes;
3156
3157 data_col_addr = start_step * chip->ecc.size;
3158 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003159 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003160 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003161 if (ret)
3162 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003163
Brian Norris8b6e50c2011-05-25 14:59:01 -07003164 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003165 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003166 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003167
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 /*
3169 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003170 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003172 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3173 if (ret)
3174 return ret;
3175
3176 if (oobregion.length < eccfrag_len)
3177 gaps = 1;
3178
Alexey Korolev3d459552008-05-15 17:23:18 +01003179 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003180 ret = nand_change_read_column_op(chip, mtd->writesize,
3181 chip->oob_poi, mtd->oobsize,
3182 false);
3183 if (ret)
3184 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003185 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003186 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003187 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003188 * about buswidth alignment in read_buf.
3189 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003190 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003191 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003192 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003193 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003194 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3195 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003196 aligned_len++;
3197
Boris Brezillon97d90da2017-11-30 18:01:29 +01003198 ret = nand_change_read_column_op(chip,
3199 mtd->writesize + aligned_pos,
3200 &chip->oob_poi[aligned_pos],
3201 aligned_len, false);
3202 if (ret)
3203 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003204 }
3205
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003206 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003207 chip->oob_poi, index, eccfrag_len);
3208 if (ret)
3209 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003210
3211 p = bufpoi + data_col_addr;
3212 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3213 int stat;
3214
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003215 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003216 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003217 if (stat == -EBADMSG &&
3218 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3219 /* check for empty pages with bitflips */
3220 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003221 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003222 chip->ecc.bytes,
3223 NULL, 0,
3224 chip->ecc.strength);
3225 }
3226
Mike Dunn3f91e942012-04-25 12:06:09 -07003227 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003228 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003229 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003230 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003231 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3232 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003233 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003234 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003235}
3236
3237/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003238 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003239 * @chip: nand chip info structure
3240 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003241 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003242 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003243 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003244 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003245 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003246static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3247 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003248{
Boris Brezillonb9761682018-09-06 14:05:20 +02003249 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003250 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003251 int eccbytes = chip->ecc.bytes;
3252 int eccsteps = chip->ecc.steps;
3253 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003254 uint8_t *ecc_calc = chip->ecc.calc_buf;
3255 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003256 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003257
Boris Brezillon25f815f2017-11-30 18:01:30 +01003258 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3259 if (ret)
3260 return ret;
3261
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003262 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003263 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003264
3265 ret = nand_read_data_op(chip, p, eccsize, false);
3266 if (ret)
3267 return ret;
3268
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003269 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003270 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003271
3272 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3273 if (ret)
3274 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003275
Boris Brezillon846031d2016-02-03 20:11:00 +01003276 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3277 chip->ecc.total);
3278 if (ret)
3279 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003280
3281 eccsteps = chip->ecc.steps;
3282 p = buf;
3283
3284 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3285 int stat;
3286
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003287 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003288 if (stat == -EBADMSG &&
3289 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3290 /* check for empty pages with bitflips */
3291 stat = nand_check_erased_ecc_chunk(p, eccsize,
3292 &ecc_code[i], eccbytes,
3293 NULL, 0,
3294 chip->ecc.strength);
3295 }
3296
Mike Dunn3f91e942012-04-25 12:06:09 -07003297 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003298 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003299 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003300 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003301 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3302 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003303 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003304 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003305}
3306
3307/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003308 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003309 * @chip: nand chip info structure
3310 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003311 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003312 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003313 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003314 * Hardware ECC for large page chips, require OOB to be read first. For this
3315 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3316 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3317 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3318 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003319 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003320static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3321 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003322{
Boris Brezillonb9761682018-09-06 14:05:20 +02003323 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003324 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003325 int eccbytes = chip->ecc.bytes;
3326 int eccsteps = chip->ecc.steps;
3327 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003328 uint8_t *ecc_code = chip->ecc.code_buf;
3329 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003330 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003331
3332 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003333 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3334 if (ret)
3335 return ret;
3336
3337 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3338 if (ret)
3339 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003340
Boris Brezillon846031d2016-02-03 20:11:00 +01003341 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3342 chip->ecc.total);
3343 if (ret)
3344 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003345
3346 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3347 int stat;
3348
Boris Brezillonec476362018-09-06 14:05:17 +02003349 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003350
3351 ret = nand_read_data_op(chip, p, eccsize, false);
3352 if (ret)
3353 return ret;
3354
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003355 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003356
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003357 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003358 if (stat == -EBADMSG &&
3359 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3360 /* check for empty pages with bitflips */
3361 stat = nand_check_erased_ecc_chunk(p, eccsize,
3362 &ecc_code[i], eccbytes,
3363 NULL, 0,
3364 chip->ecc.strength);
3365 }
3366
Mike Dunn3f91e942012-04-25 12:06:09 -07003367 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003368 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003369 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003370 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003371 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3372 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003373 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003374 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003375}
3376
3377/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003378 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003379 * @chip: nand chip info structure
3380 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003381 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003382 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003383 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003384 * The hw generator calculates the error syndrome automatically. Therefore we
3385 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003387static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3388 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003389{
Boris Brezillonb9761682018-09-06 14:05:20 +02003390 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003391 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003392 int eccbytes = chip->ecc.bytes;
3393 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003394 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003395 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003396 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003397 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003398
Boris Brezillon25f815f2017-11-30 18:01:30 +01003399 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3400 if (ret)
3401 return ret;
3402
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003403 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3404 int stat;
3405
Boris Brezillonec476362018-09-06 14:05:17 +02003406 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003407
3408 ret = nand_read_data_op(chip, p, eccsize, false);
3409 if (ret)
3410 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003411
3412 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003413 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3414 false);
3415 if (ret)
3416 return ret;
3417
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003418 oob += chip->ecc.prepad;
3419 }
3420
Boris Brezillonec476362018-09-06 14:05:17 +02003421 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003422
3423 ret = nand_read_data_op(chip, oob, eccbytes, false);
3424 if (ret)
3425 return ret;
3426
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003427 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003428
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003429 oob += eccbytes;
3430
3431 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003432 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3433 false);
3434 if (ret)
3435 return ret;
3436
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003437 oob += chip->ecc.postpad;
3438 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003439
3440 if (stat == -EBADMSG &&
3441 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3442 /* check for empty pages with bitflips */
3443 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3444 oob - eccpadbytes,
3445 eccpadbytes,
3446 NULL, 0,
3447 chip->ecc.strength);
3448 }
3449
3450 if (stat < 0) {
3451 mtd->ecc_stats.failed++;
3452 } else {
3453 mtd->ecc_stats.corrected += stat;
3454 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3455 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003456 }
3457
3458 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003459 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003460 if (i) {
3461 ret = nand_read_data_op(chip, oob, i, false);
3462 if (ret)
3463 return ret;
3464 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003465
Mike Dunn3f91e942012-04-25 12:06:09 -07003466 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003467}
3468
3469/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003470 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003471 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003472 * @oob: oob destination address
3473 * @ops: oob ops structure
3474 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003475 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003476static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003477 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003478{
Boris Brezillon846031d2016-02-03 20:11:00 +01003479 struct nand_chip *chip = mtd_to_nand(mtd);
3480 int ret;
3481
Florian Fainellif8ac0412010-09-07 13:23:43 +02003482 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003483
Brian Norris0612b9d2011-08-30 18:45:40 -07003484 case MTD_OPS_PLACE_OOB:
3485 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003486 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3487 return oob + len;
3488
Boris Brezillon846031d2016-02-03 20:11:00 +01003489 case MTD_OPS_AUTO_OOB:
3490 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3491 ops->ooboffs, len);
3492 BUG_ON(ret);
3493 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003494
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003495 default:
3496 BUG();
3497 }
3498 return NULL;
3499}
3500
3501/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003502 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003503 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003504 * @retry_mode: the retry mode to use
3505 *
3506 * Some vendors supply a special command to shift the Vt threshold, to be used
3507 * when there are too many bitflips in a page (i.e., ECC error). After setting
3508 * a new threshold, the host should retry reading the page.
3509 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003510static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003511{
Brian Norrisba84fb52014-01-03 15:13:33 -08003512 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3513
3514 if (retry_mode >= chip->read_retries)
3515 return -EINVAL;
3516
3517 if (!chip->setup_read_retry)
3518 return -EOPNOTSUPP;
3519
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003520 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003521}
3522
Boris Brezillon85e08e52018-07-27 09:44:17 +02003523static void nand_wait_readrdy(struct nand_chip *chip)
3524{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003525 const struct nand_sdr_timings *sdr;
3526
Boris Brezillon85e08e52018-07-27 09:44:17 +02003527 if (!(chip->options & NAND_NEED_READRDY))
3528 return;
3529
Boris Brezillon52f05b62018-07-27 09:44:18 +02003530 sdr = nand_get_sdr_timings(&chip->data_interface);
3531 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003532}
3533
Brian Norrisba84fb52014-01-03 15:13:33 -08003534/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003535 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003536 * @mtd: MTD device structure
3537 * @from: offset to read from
3538 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003539 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003540 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003541 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003542static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3543 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003544{
Brian Norrise47f3db2012-05-02 10:14:56 -07003545 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003546 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003547 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003548 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003549 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003550 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003551
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003552 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003553 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003554 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003555 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003556 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003558 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003559 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003561 realpage = (int)(from >> chip->page_shift);
3562 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003564 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003566 buf = ops->datbuf;
3567 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003568 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003569
Florian Fainellif8ac0412010-09-07 13:23:43 +02003570 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003571 unsigned int ecc_failures = mtd->ecc_stats.failed;
3572
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003573 bytes = min(mtd->writesize - col, readlen);
3574 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003575
Kamal Dasu66507c72014-05-01 20:51:19 -04003576 if (!aligned)
3577 use_bufpoi = 1;
3578 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003579 use_bufpoi = !virt_addr_valid(buf) ||
3580 !IS_ALIGNED((unsigned long)buf,
3581 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003582 else
3583 use_bufpoi = 0;
3584
Brian Norris8b6e50c2011-05-25 14:59:01 -07003585 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003586 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003587 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003588
3589 if (use_bufpoi && aligned)
3590 pr_debug("%s: using read bounce buffer for buf@%p\n",
3591 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Brian Norrisba84fb52014-01-03 15:13:33 -08003593read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003594 /*
3595 * Now read the page into the buffer. Absent an error,
3596 * the read methods return max bitflips per ecc step.
3597 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003598 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003599 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003600 oob_required,
3601 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003602 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3603 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003604 ret = chip->ecc.read_subpage(chip, col, bytes,
3605 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003606 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003607 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003608 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003609 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003610 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003611 /* Invalidate page cache */
3612 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003613 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003614 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003615
3616 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003617 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003618 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003619 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003620 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003621 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003622 chip->pagebuf_bitflips = ret;
3623 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003624 /* Invalidate page cache */
3625 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003626 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003627 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003629
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003630 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003631 int toread = min(oobreadlen, max_oobsize);
3632
3633 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003634 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003635 oob, ops, toread);
3636 oobreadlen -= toread;
3637 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003638 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003639
Boris Brezillon85e08e52018-07-27 09:44:17 +02003640 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003641
Brian Norrisba84fb52014-01-03 15:13:33 -08003642 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003643 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003644 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003645 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003646 retry_mode);
3647 if (ret < 0)
3648 break;
3649
3650 /* Reset failures; retry */
3651 mtd->ecc_stats.failed = ecc_failures;
3652 goto read_retry;
3653 } else {
3654 /* No more retry modes; real failure */
3655 ecc_fail = true;
3656 }
3657 }
3658
3659 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003660 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003661 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003662 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003663 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003664 max_bitflips = max_t(unsigned int, max_bitflips,
3665 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003668 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003669
Brian Norrisba84fb52014-01-03 15:13:33 -08003670 /* Reset to retry mode 0 */
3671 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003672 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003673 if (ret < 0)
3674 break;
3675 retry_mode = 0;
3676 }
3677
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003678 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Brian Norris8b6e50c2011-05-25 14:59:01 -07003681 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 col = 0;
3683 /* Increment page address */
3684 realpage++;
3685
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003686 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /* Check, if we cross a chip boundary */
3688 if (!page) {
3689 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003690 chip->select_chip(chip, -1);
3691 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003694 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003696 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003697 if (oob)
3698 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Mike Dunn3f91e942012-04-25 12:06:09 -07003700 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003701 return ret;
3702
Brian Norrisb72f3df2013-12-03 11:04:14 -08003703 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003704 return -EBADMSG;
3705
Mike Dunnedbc45402012-04-25 12:06:11 -07003706 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003707}
3708
3709/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003710 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003711 * @chip: nand chip info structure
3712 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003713 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003714int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003715{
Boris Brezillonb9761682018-09-06 14:05:20 +02003716 struct mtd_info *mtd = nand_to_mtd(chip);
3717
Boris Brezillon97d90da2017-11-30 18:01:29 +01003718 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003719}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003720EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003721
3722/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003723 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003725 * @chip: nand chip info structure
3726 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003727 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003728static int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003729{
Boris Brezillonb9761682018-09-06 14:05:20 +02003730 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003731 int length = mtd->oobsize;
3732 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3733 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003734 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003735 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003736
Boris Brezillon97d90da2017-11-30 18:01:29 +01003737 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3738 if (ret)
3739 return ret;
3740
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003741 for (i = 0; i < chip->ecc.steps; i++) {
3742 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003743 int ret;
3744
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003745 pos = eccsize + i * (eccsize + chunk);
3746 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003747 ret = nand_change_read_column_op(chip, pos,
3748 NULL, 0,
3749 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003750 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003751 ret = nand_read_page_op(chip, page, pos, NULL,
3752 0);
3753
3754 if (ret)
3755 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003756 } else
3757 sndrnd = 1;
3758 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003759
3760 ret = nand_read_data_op(chip, bufpoi, toread, false);
3761 if (ret)
3762 return ret;
3763
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003764 bufpoi += toread;
3765 length -= toread;
3766 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003767 if (length > 0) {
3768 ret = nand_read_data_op(chip, bufpoi, length, false);
3769 if (ret)
3770 return ret;
3771 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003773 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003774}
3775
3776/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003777 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003778 * @chip: nand chip info structure
3779 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003781int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003782{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003783 struct mtd_info *mtd = nand_to_mtd(chip);
3784
Boris Brezillon97d90da2017-11-30 18:01:29 +01003785 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3786 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003788EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003789
3790/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003791 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003792 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003793 * @chip: nand chip info structure
3794 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003795 */
Boris Brezillon348d56a2018-09-07 00:38:48 +02003796static int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003797{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003798 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003799 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3800 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003801 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003802 const uint8_t *bufpoi = chip->oob_poi;
3803
3804 /*
3805 * data-ecc-data-ecc ... ecc-oob
3806 * or
3807 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3808 */
3809 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3810 pos = steps * (eccsize + chunk);
3811 steps = 0;
3812 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003813 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003814
Boris Brezillon97d90da2017-11-30 18:01:29 +01003815 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3816 if (ret)
3817 return ret;
3818
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003819 for (i = 0; i < steps; i++) {
3820 if (sndcmd) {
3821 if (mtd->writesize <= 512) {
3822 uint32_t fill = 0xFFFFFFFF;
3823
3824 len = eccsize;
3825 while (len > 0) {
3826 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003827
3828 ret = nand_write_data_op(chip, &fill,
3829 num, false);
3830 if (ret)
3831 return ret;
3832
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003833 len -= num;
3834 }
3835 } else {
3836 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003837 ret = nand_change_write_column_op(chip, pos,
3838 NULL, 0,
3839 false);
3840 if (ret)
3841 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003842 }
3843 } else
3844 sndcmd = 1;
3845 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003846
3847 ret = nand_write_data_op(chip, bufpoi, len, false);
3848 if (ret)
3849 return ret;
3850
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003851 bufpoi += len;
3852 length -= len;
3853 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003854 if (length > 0) {
3855 ret = nand_write_data_op(chip, bufpoi, length, false);
3856 if (ret)
3857 return ret;
3858 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003859
Boris Brezillon97d90da2017-11-30 18:01:29 +01003860 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003861}
3862
3863/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003864 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003865 * @mtd: MTD device structure
3866 * @from: offset to read from
3867 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003869 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003871static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3872 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003874 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003875 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003876 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003877 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003878 int readlen = ops->ooblen;
3879 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003880 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003881 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Brian Norris289c0522011-07-19 10:06:09 -07003883 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303884 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Brian Norris041e4572011-06-23 16:45:24 -07003886 stats = mtd->ecc_stats;
3887
Boris BREZILLON29f10582016-03-07 10:46:52 +01003888 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003889
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003890 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003891 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003893 /* Shift to get page */
3894 realpage = (int)(from >> chip->page_shift);
3895 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Florian Fainellif8ac0412010-09-07 13:23:43 +02003897 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003898 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003899 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003900 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003901 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003902
3903 if (ret < 0)
3904 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003905
3906 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003907 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003908
Boris Brezillon85e08e52018-07-27 09:44:17 +02003909 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003910
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003911 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3912
Vitaly Wool70145682006-11-03 18:20:38 +03003913 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003914 if (!readlen)
3915 break;
3916
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003917 /* Increment page address */
3918 realpage++;
3919
3920 page = realpage & chip->pagemask;
3921 /* Check, if we cross a chip boundary */
3922 if (!page) {
3923 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003924 chip->select_chip(chip, -1);
3925 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003928 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003930 ops->oobretlen = ops->ooblen - readlen;
3931
3932 if (ret < 0)
3933 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003934
3935 if (mtd->ecc_stats.failed - stats.failed)
3936 return -EBADMSG;
3937
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003938 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
3941/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003942 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003943 * @mtd: MTD device structure
3944 * @from: offset to read from
3945 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003947 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003949static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3950 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003952 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003953
3954 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003956 if (ops->mode != MTD_OPS_PLACE_OOB &&
3957 ops->mode != MTD_OPS_AUTO_OOB &&
3958 ops->mode != MTD_OPS_RAW)
3959 return -ENOTSUPP;
3960
Huang Shijie6a8214a2012-11-19 14:43:30 +08003961 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003963 if (!ops->datbuf)
3964 ret = nand_do_read_oob(mtd, from, ops);
3965 else
3966 ret = nand_do_read_ops(mtd, from, ops);
3967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003969 return ret;
3970}
3971
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003972/**
3973 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003974 * @chip: nand chip info structure
3975 * @buf: data buffer
3976 * @oob_required: must write chip->oob_poi to OOB
3977 * @page: page number to write
3978 *
3979 * Returns -ENOTSUPP unconditionally.
3980 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003981int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3982 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003983{
3984 return -ENOTSUPP;
3985}
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003986
3987/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003988 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003989 * @chip: nand chip info structure
3990 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003991 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003992 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003993 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003994 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003995 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003996int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3997 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003998{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003999 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004000 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004001
Boris Brezillon25f815f2017-11-30 18:01:30 +01004002 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004003 if (ret)
4004 return ret;
4005
4006 if (oob_required) {
4007 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4008 false);
4009 if (ret)
4010 return ret;
4011 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004012
Boris Brezillon25f815f2017-11-30 18:01:30 +01004013 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004015EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004017/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004018 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004019 * @chip: nand chip info structure
4020 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004021 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004022 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004023 *
4024 * We need a special oob layout and handling even when ECC isn't checked.
4025 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004026static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004027 const uint8_t *buf, int oob_required,
4028 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004029{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004030 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004031 int eccsize = chip->ecc.size;
4032 int eccbytes = chip->ecc.bytes;
4033 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004034 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004035
Boris Brezillon25f815f2017-11-30 18:01:30 +01004036 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4037 if (ret)
4038 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004039
4040 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004041 ret = nand_write_data_op(chip, buf, eccsize, false);
4042 if (ret)
4043 return ret;
4044
David Brownell52ff49d2009-03-04 12:01:36 -08004045 buf += eccsize;
4046
4047 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004048 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4049 false);
4050 if (ret)
4051 return ret;
4052
David Brownell52ff49d2009-03-04 12:01:36 -08004053 oob += chip->ecc.prepad;
4054 }
4055
Boris Brezillon97d90da2017-11-30 18:01:29 +01004056 ret = nand_write_data_op(chip, oob, eccbytes, false);
4057 if (ret)
4058 return ret;
4059
David Brownell52ff49d2009-03-04 12:01:36 -08004060 oob += eccbytes;
4061
4062 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004063 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4064 false);
4065 if (ret)
4066 return ret;
4067
David Brownell52ff49d2009-03-04 12:01:36 -08004068 oob += chip->ecc.postpad;
4069 }
4070 }
4071
4072 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004073 if (size) {
4074 ret = nand_write_data_op(chip, oob, size, false);
4075 if (ret)
4076 return ret;
4077 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004078
Boris Brezillon25f815f2017-11-30 18:01:30 +01004079 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004080}
4081/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004082 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004083 * @chip: nand chip info structure
4084 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004085 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004086 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004087 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004088static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4089 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004090{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004091 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004092 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004093 int eccbytes = chip->ecc.bytes;
4094 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004095 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004096 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004097
Brian Norris7854d3f2011-06-23 14:12:08 -07004098 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004099 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004100 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101
Boris Brezillon846031d2016-02-03 20:11:00 +01004102 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4103 chip->ecc.total);
4104 if (ret)
4105 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004106
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004107 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004108}
4109
4110/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004111 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004112 * @chip: nand chip info structure
4113 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004114 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004115 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004116 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004117static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4118 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004119{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004120 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004121 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004122 int eccbytes = chip->ecc.bytes;
4123 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004124 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004125 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004126
Boris Brezillon25f815f2017-11-30 18:01:30 +01004127 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4128 if (ret)
4129 return ret;
4130
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004131 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004132 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004133
4134 ret = nand_write_data_op(chip, p, eccsize, false);
4135 if (ret)
4136 return ret;
4137
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004138 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004139 }
4140
Boris Brezillon846031d2016-02-03 20:11:00 +01004141 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4142 chip->ecc.total);
4143 if (ret)
4144 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004145
Boris Brezillon97d90da2017-11-30 18:01:29 +01004146 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4147 if (ret)
4148 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004149
Boris Brezillon25f815f2017-11-30 18:01:30 +01004150 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004151}
4152
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304153
4154/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004155 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304156 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004157 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304158 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004159 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304160 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004161 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304162 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004163static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4164 uint32_t data_len, const uint8_t *buf,
4165 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304166{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004167 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304168 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004169 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304170 int ecc_size = chip->ecc.size;
4171 int ecc_bytes = chip->ecc.bytes;
4172 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304173 uint32_t start_step = offset / ecc_size;
4174 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4175 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004176 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304177
Boris Brezillon25f815f2017-11-30 18:01:30 +01004178 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4179 if (ret)
4180 return ret;
4181
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304182 for (step = 0; step < ecc_steps; step++) {
4183 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004184 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304185
4186 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004187 ret = nand_write_data_op(chip, buf, ecc_size, false);
4188 if (ret)
4189 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304190
4191 /* mask ECC of un-touched subpages by padding 0xFF */
4192 if ((step < start_step) || (step > end_step))
4193 memset(ecc_calc, 0xff, ecc_bytes);
4194 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004195 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196
4197 /* mask OOB of un-touched subpages by padding 0xFF */
4198 /* if oob_required, preserve OOB metadata of written subpage */
4199 if (!oob_required || (step < start_step) || (step > end_step))
4200 memset(oob_buf, 0xff, oob_bytes);
4201
Brian Norrisd6a950802013-08-08 17:16:36 -07004202 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304203 ecc_calc += ecc_bytes;
4204 oob_buf += oob_bytes;
4205 }
4206
4207 /* copy calculated ECC for whole page to chip->buffer->oob */
4208 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004209 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004210 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4211 chip->ecc.total);
4212 if (ret)
4213 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304214
4215 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004216 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4217 if (ret)
4218 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304219
Boris Brezillon25f815f2017-11-30 18:01:30 +01004220 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304221}
4222
4223
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004224/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004225 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004226 * @chip: nand chip info structure
4227 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004228 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004229 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004230 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004231 * The hw generator calculates the error syndrome automatically. Therefore we
4232 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004233 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004234static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4235 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004236{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004237 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004238 int i, eccsize = chip->ecc.size;
4239 int eccbytes = chip->ecc.bytes;
4240 int eccsteps = chip->ecc.steps;
4241 const uint8_t *p = buf;
4242 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004243 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004244
Boris Brezillon25f815f2017-11-30 18:01:30 +01004245 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4246 if (ret)
4247 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004248
4249 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004250 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004251
4252 ret = nand_write_data_op(chip, p, eccsize, false);
4253 if (ret)
4254 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004255
4256 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004257 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4258 false);
4259 if (ret)
4260 return ret;
4261
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004262 oob += chip->ecc.prepad;
4263 }
4264
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004265 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004266
4267 ret = nand_write_data_op(chip, oob, eccbytes, false);
4268 if (ret)
4269 return ret;
4270
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004271 oob += eccbytes;
4272
4273 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004274 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4275 false);
4276 if (ret)
4277 return ret;
4278
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004279 oob += chip->ecc.postpad;
4280 }
4281 }
4282
4283 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004284 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004285 if (i) {
4286 ret = nand_write_data_op(chip, oob, i, false);
4287 if (ret)
4288 return ret;
4289 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004290
Boris Brezillon25f815f2017-11-30 18:01:30 +01004291 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004292}
4293
4294/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004295 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004296 * @mtd: MTD device structure
4297 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304298 * @offset: address offset within the page
4299 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004300 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004301 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004303 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004304 */
4305static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304306 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004307 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004308{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304309 int status, subpage;
4310
4311 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4312 chip->ecc.write_subpage)
4313 subpage = offset || (data_len < mtd->writesize);
4314 else
4315 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004316
David Woodhouse956e9442006-09-25 17:12:39 +01004317 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004318 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4319 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304320 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004321 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4322 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004323 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004324 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004325
4326 if (status < 0)
4327 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004328
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004329 return 0;
4330}
4331
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004332/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004333 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004334 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004335 * @oob: oob data buffer
4336 * @len: oob data write length
4337 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004338 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004339static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4340 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004341{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004342 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004343 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004344
4345 /*
4346 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4347 * data from a previous OOB read.
4348 */
4349 memset(chip->oob_poi, 0xff, mtd->oobsize);
4350
Florian Fainellif8ac0412010-09-07 13:23:43 +02004351 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004352
Brian Norris0612b9d2011-08-30 18:45:40 -07004353 case MTD_OPS_PLACE_OOB:
4354 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004355 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4356 return oob + len;
4357
Boris Brezillon846031d2016-02-03 20:11:00 +01004358 case MTD_OPS_AUTO_OOB:
4359 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4360 ops->ooboffs, len);
4361 BUG_ON(ret);
4362 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004363
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364 default:
4365 BUG();
4366 }
4367 return NULL;
4368}
4369
Florian Fainellif8ac0412010-09-07 13:23:43 +02004370#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004371
4372/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004373 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004374 * @mtd: MTD device structure
4375 * @to: offset to write to
4376 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004378 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004379 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004380static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4381 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382{
Corentin Labbe73600b62017-09-02 10:49:38 +02004383 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004384 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004385 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004386
4387 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004388 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004389
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004390 uint8_t *oob = ops->oobbuf;
4391 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304392 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004393 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004394
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004395 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004396 if (!writelen)
4397 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004398
Brian Norris8b6e50c2011-05-25 14:59:01 -07004399 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004400 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004401 pr_notice("%s: attempt to write non page aligned data\n",
4402 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004403 return -EINVAL;
4404 }
4405
Thomas Gleixner29072b92006-09-28 15:38:36 +02004406 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004407
Thomas Gleixner6a930962006-06-28 00:11:45 +02004408 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004409 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004410
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004411 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004412 if (nand_check_wp(mtd)) {
4413 ret = -EIO;
4414 goto err_out;
4415 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004416
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004417 realpage = (int)(to >> chip->page_shift);
4418 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004419
4420 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004421 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4422 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004423 chip->pagebuf = -1;
4424
Maxim Levitsky782ce792010-02-22 20:39:36 +02004425 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004426 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4427 ret = -EINVAL;
4428 goto err_out;
4429 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004430
Florian Fainellif8ac0412010-09-07 13:23:43 +02004431 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004432 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004433 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004434 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004435 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004436
Kamal Dasu66507c72014-05-01 20:51:19 -04004437 if (part_pagewr)
4438 use_bufpoi = 1;
4439 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004440 use_bufpoi = !virt_addr_valid(buf) ||
4441 !IS_ALIGNED((unsigned long)buf,
4442 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004443 else
4444 use_bufpoi = 0;
4445
4446 /* Partial page write?, or need to use bounce buffer */
4447 if (use_bufpoi) {
4448 pr_debug("%s: using write bounce buffer for buf@%p\n",
4449 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004450 if (part_pagewr)
4451 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004452 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004453 memset(chip->data_buf, 0xff, mtd->writesize);
4454 memcpy(&chip->data_buf[column], buf, bytes);
4455 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004456 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004457
Maxim Levitsky782ce792010-02-22 20:39:36 +02004458 if (unlikely(oob)) {
4459 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004460 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004461 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004462 } else {
4463 /* We still need to erase leftover OOB data */
4464 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004465 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004466
4467 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004468 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004469 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004470 if (ret)
4471 break;
4472
4473 writelen -= bytes;
4474 if (!writelen)
4475 break;
4476
Thomas Gleixner29072b92006-09-28 15:38:36 +02004477 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004478 buf += bytes;
4479 realpage++;
4480
4481 page = realpage & chip->pagemask;
4482 /* Check, if we cross a chip boundary */
4483 if (!page) {
4484 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004485 chip->select_chip(chip, -1);
4486 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004487 }
4488 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004489
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004490 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004491 if (unlikely(oob))
4492 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004493
4494err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004495 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004496 return ret;
4497}
4498
4499/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004500 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004501 * @mtd: MTD device structure
4502 * @to: offset to write to
4503 * @len: number of bytes to write
4504 * @retlen: pointer to variable to store the number of written bytes
4505 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004506 *
4507 * NAND write with ECC. Used when performing writes in interrupt context, this
4508 * may for example be called by mtdoops when writing an oops while in panic.
4509 */
4510static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4511 size_t *retlen, const uint8_t *buf)
4512{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004513 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004514 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004515 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004516 int ret;
4517
Brian Norris8b6e50c2011-05-25 14:59:01 -07004518 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004519 panic_nand_get_device(chip, mtd, FL_WRITING);
4520
Boris Brezillon758b56f2018-09-06 14:05:24 +02004521 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004522
4523 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004524 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004525
Brian Norris0ec56dc2015-02-28 02:02:30 -08004526 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004527 ops.len = len;
4528 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004529 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004530
Brian Norris4a89ff82011-08-30 18:45:45 -07004531 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004532
Brian Norris4a89ff82011-08-30 18:45:45 -07004533 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004534 return ret;
4535}
4536
4537/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004538 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004539 * @mtd: MTD device structure
4540 * @to: offset to write to
4541 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004542 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004543 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004544 */
4545static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4546 struct mtd_oob_ops *ops)
4547{
Adrian Hunter03736152007-01-31 17:58:29 +02004548 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004549 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Brian Norris289c0522011-07-19 10:06:09 -07004551 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304552 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Boris BREZILLON29f10582016-03-07 10:46:52 +01004554 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004557 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004558 pr_debug("%s: attempt to write past end of page\n",
4559 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 return -EINVAL;
4561 }
4562
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004563 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004564
4565 /*
4566 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4567 * of my DiskOnChip 2000 test units) will clear the whole data page too
4568 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4569 * it in the doc2000 driver in August 1999. dwmw2.
4570 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004571 nand_reset(chip, chipnr);
4572
Boris Brezillon758b56f2018-09-06 14:05:24 +02004573 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004574
4575 /* Shift to get page */
4576 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
4578 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004579 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004580 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004581 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004582 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004585 if (page == chip->pagebuf)
4586 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004588 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004589
Brian Norris0612b9d2011-08-30 18:45:40 -07004590 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004591 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004592 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004593 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004594
Boris Brezillon758b56f2018-09-06 14:05:24 +02004595 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004596
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004597 if (status)
4598 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Vitaly Wool70145682006-11-03 18:20:38 +03004600 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004602 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004605/**
4606 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004607 * @mtd: MTD device structure
4608 * @to: offset to write to
4609 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004610 */
4611static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4612 struct mtd_oob_ops *ops)
4613{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004614 int ret = -ENOTSUPP;
4615
4616 ops->retlen = 0;
4617
Huang Shijie6a8214a2012-11-19 14:43:30 +08004618 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004619
Florian Fainellif8ac0412010-09-07 13:23:43 +02004620 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004621 case MTD_OPS_PLACE_OOB:
4622 case MTD_OPS_AUTO_OOB:
4623 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004624 break;
4625
4626 default:
4627 goto out;
4628 }
4629
4630 if (!ops->datbuf)
4631 ret = nand_do_write_oob(mtd, to, ops);
4632 else
4633 ret = nand_do_write_ops(mtd, to, ops);
4634
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004635out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004636 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 return ret;
4638}
4639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640/**
Brian Norris49c50b92014-05-06 16:02:19 -07004641 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004642 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004643 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 *
Brian Norris49c50b92014-05-06 16:02:19 -07004645 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004647static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004649 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004652 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004653
Boris Brezillon97d90da2017-11-30 18:01:29 +01004654 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
4657/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004659 * @mtd: MTD device structure
4660 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004662 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004664static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004666 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004670 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004671 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004672 * @instr: erase instruction
4673 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004675 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004677int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004678 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004680 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004681 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004682 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Brian Norris289c0522011-07-19 10:06:09 -07004684 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4685 __func__, (unsigned long long)instr->addr,
4686 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304688 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004692 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004695 page = (int)(instr->addr >> chip->page_shift);
4696 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004699 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004702 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 /* Check, if it is write protected */
4705 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004706 pr_debug("%s: device is write protected!\n",
4707 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004708 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 goto erase_exit;
4710 }
4711
4712 /* Loop through the pages */
4713 len = instr->len;
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004716 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004717 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304718 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004719 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4720 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004721 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 goto erase_exit;
4723 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004724
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004725 /*
4726 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004727 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004728 */
4729 if (page <= chip->pagebuf && chip->pagebuf <
4730 (page + pages_per_block))
4731 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004733 if (chip->legacy.erase)
4734 status = chip->legacy.erase(chip,
4735 page & chip->pagemask);
4736 else
4737 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004740 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004741 pr_debug("%s: failed erase, page 0x%08x\n",
4742 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004743 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004744 instr->fail_addr =
4745 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 goto erase_exit;
4747 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004750 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 page += pages_per_block;
4752
4753 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004754 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004756 chip->select_chip(chip, -1);
4757 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
4759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004761 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004762erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004765 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 nand_release_device(mtd);
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 /* Return more or less happy */
4769 return ret;
4770}
4771
4772/**
4773 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004774 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004776 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004778static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Brian Norris289c0522011-07-19 10:06:09 -07004780 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004783 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004785 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004789 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004790 * @mtd: MTD device structure
4791 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004793static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304795 struct nand_chip *chip = mtd_to_nand(mtd);
4796 int chipnr = (int)(offs >> chip->chip_shift);
4797 int ret;
4798
4799 /* Select the NAND device */
4800 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004801 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304802
4803 ret = nand_block_checkbad(mtd, offs, 0);
4804
Boris Brezillon758b56f2018-09-06 14:05:24 +02004805 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304806 nand_release_device(mtd);
4807
4808 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809}
4810
4811/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004812 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004813 * @mtd: MTD device structure
4814 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004816static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 int ret;
4819
Florian Fainellif8ac0412010-09-07 13:23:43 +02004820 ret = nand_block_isbad(mtd, ofs);
4821 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004822 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (ret > 0)
4824 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004825 return ret;
4826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
Brian Norris5a0edb22013-07-30 17:52:58 -07004828 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
4831/**
Zach Brown56718422017-01-10 13:30:20 -06004832 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4833 * @mtd: MTD device structure
4834 * @ofs: offset relative to mtd start
4835 * @len: length of mtd
4836 */
4837static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4838{
4839 struct nand_chip *chip = mtd_to_nand(mtd);
4840 u32 part_start_block;
4841 u32 part_end_block;
4842 u32 part_start_die;
4843 u32 part_end_die;
4844
4845 /*
4846 * max_bb_per_die and blocks_per_die used to determine
4847 * the maximum bad block count.
4848 */
4849 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4850 return -ENOTSUPP;
4851
4852 /* Get the start and end of the partition in erase blocks. */
4853 part_start_block = mtd_div_by_eb(ofs, mtd);
4854 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4855
4856 /* Get the start and end LUNs of the partition. */
4857 part_start_die = part_start_block / chip->blocks_per_die;
4858 part_end_die = part_end_block / chip->blocks_per_die;
4859
4860 /*
4861 * Look up the bad blocks per unit and multiply by the number of units
4862 * that the partition spans.
4863 */
4864 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4865}
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 */
Boris Brezillon3cece3a2018-09-07 00:38:41 +02004923 if (!chip->legacy.chip_delay)
4924 chip->legacy.chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
4926 /* check, if a user supplied command function given */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02004927 if (!chip->legacy.cmdfunc && !chip->exec_op)
4928 chip->legacy.cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
4930 /* check, if a user supplied wait function given */
Boris Brezillon8395b752018-09-07 00:38:37 +02004931 if (chip->legacy.waitfunc == NULL)
4932 chip->legacy.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
4937 /* If called twice, pointers that depend on busw may need to be reset */
Boris Brezillon716bbba2018-09-07 00:38:35 +02004938 if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
4939 chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
Boris Brezillon716bbba2018-09-07 00:38:35 +02004940 if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
4941 chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
4942 if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
4943 chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
4944 if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
4945 chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004946
4947 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004948 chip->controller = &chip->dummy_controller;
4949 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004950 }
4951
Masahiro Yamada477544c2017-03-30 17:15:05 +09004952 if (!chip->buf_align)
4953 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004954}
4955
Brian Norris8b6e50c2011-05-25 14:59:01 -07004956/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004957static void sanitize_string(uint8_t *s, size_t len)
4958{
4959 ssize_t i;
4960
Brian Norris8b6e50c2011-05-25 14:59:01 -07004961 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004962 s[len - 1] = 0;
4963
Brian Norris8b6e50c2011-05-25 14:59:01 -07004964 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004965 for (i = 0; i < len - 1; i++) {
4966 if (s[i] < ' ' || s[i] > 127)
4967 s[i] = '?';
4968 }
4969
Brian Norris8b6e50c2011-05-25 14:59:01 -07004970 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004971 strim(s);
4972}
4973
4974static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4975{
4976 int i;
4977 while (len--) {
4978 crc ^= *p++ << 8;
4979 for (i = 0; i < 8; i++)
4980 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4981 }
4982
4983 return crc;
4984}
4985
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004986/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004987static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4988 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004989{
4990 struct onfi_ext_param_page *ep;
4991 struct onfi_ext_section *s;
4992 struct onfi_ext_ecc_info *ecc;
4993 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004994 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004995 int len;
4996 int i;
4997
4998 len = le16_to_cpu(p->ext_param_page_length) * 16;
4999 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005000 if (!ep)
5001 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005002
5003 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005004 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5005 if (ret)
5006 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005007
5008 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005009 ret = nand_change_read_column_op(chip,
5010 sizeof(*p) * p->num_of_param_pages,
5011 ep, len, true);
5012 if (ret)
5013 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005014
Boris Brezillon97d90da2017-11-30 18:01:29 +01005015 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005016 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5017 != le16_to_cpu(ep->crc))) {
5018 pr_debug("fail in the CRC.\n");
5019 goto ext_out;
5020 }
5021
5022 /*
5023 * Check the signature.
5024 * Do not strictly follow the ONFI spec, maybe changed in future.
5025 */
5026 if (strncmp(ep->sig, "EPPS", 4)) {
5027 pr_debug("The signature is invalid.\n");
5028 goto ext_out;
5029 }
5030
5031 /* find the ECC section. */
5032 cursor = (uint8_t *)(ep + 1);
5033 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5034 s = ep->sections + i;
5035 if (s->type == ONFI_SECTION_TYPE_2)
5036 break;
5037 cursor += s->length * 16;
5038 }
5039 if (i == ONFI_EXT_SECTION_MAX) {
5040 pr_debug("We can not find the ECC section.\n");
5041 goto ext_out;
5042 }
5043
5044 /* get the info we want. */
5045 ecc = (struct onfi_ext_ecc_info *)cursor;
5046
Brian Norris4ae7d222013-09-16 18:20:21 -07005047 if (!ecc->codeword_size) {
5048 pr_debug("Invalid codeword size\n");
5049 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005050 }
5051
Brian Norris4ae7d222013-09-16 18:20:21 -07005052 chip->ecc_strength_ds = ecc->ecc_bits;
5053 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005054 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005055
5056ext_out:
5057 kfree(ep);
5058 return ret;
5059}
5060
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005061/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005062 * Recover data with bit-wise majority
5063 */
5064static void nand_bit_wise_majority(const void **srcbufs,
5065 unsigned int nsrcbufs,
5066 void *dstbuf,
5067 unsigned int bufsize)
5068{
5069 int i, j, k;
5070
5071 for (i = 0; i < bufsize; i++) {
5072 u8 val = 0;
5073
5074 for (j = 0; j < 8; j++) {
5075 unsigned int cnt = 0;
5076
5077 for (k = 0; k < nsrcbufs; k++) {
5078 const u8 *srcbuf = srcbufs[k];
5079
5080 if (srcbuf[i] & BIT(j))
5081 cnt++;
5082 }
5083
5084 if (cnt > nsrcbufs / 2)
5085 val |= BIT(j);
5086 }
5087
5088 ((u8 *)dstbuf)[i] = val;
5089 }
5090}
5091
5092/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005093 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005094 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005095static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005096{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005097 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005098 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005099 struct onfi_params *onfi;
5100 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005101 char id[4];
5102 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005103
Brian Norris7854d3f2011-06-23 14:12:08 -07005104 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005105 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5106 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005107 return 0;
5108
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005109 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005110 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005111 if (!p)
5112 return -ENOMEM;
5113
Boris Brezillon97d90da2017-11-30 18:01:29 +01005114 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005115 if (ret) {
5116 ret = 0;
5117 goto free_onfi_param_page;
5118 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005119
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005120 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005121 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005122 if (ret) {
5123 ret = 0;
5124 goto free_onfi_param_page;
5125 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005126
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005127 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005128 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005129 if (i)
5130 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005131 break;
5132 }
5133 }
5134
Brian Norrisc7f23a72013-08-13 10:51:55 -07005135 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005136 const void *srcbufs[3] = {p, p + 1, p + 2};
5137
5138 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5139 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5140 sizeof(*p));
5141
5142 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5143 le16_to_cpu(p->crc)) {
5144 pr_err("ONFI parameter recovery failed, aborting\n");
5145 goto free_onfi_param_page;
5146 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005147 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005148
Chris Packham00ce4e02018-06-25 10:44:44 +12005149 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5150 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5151 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5152
Brian Norris8b6e50c2011-05-25 14:59:01 -07005153 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005154 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005155 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005156 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005157 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005158 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005159 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005160 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005161 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005162 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005163 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005164 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005165
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005166 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005167 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005168 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005169 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005170
5171 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5172 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005173 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5174 if (!chip->parameters.model) {
5175 ret = -ENOMEM;
5176 goto free_onfi_param_page;
5177 }
Brian Norris4355b702013-08-27 18:45:10 -07005178
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005179 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005180
5181 /*
5182 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5183 * (don't ask me who thought of this...). MTD assumes that these
5184 * dimensions will be power-of-2, so just truncate the remaining area.
5185 */
5186 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5187 mtd->erasesize *= mtd->writesize;
5188
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005189 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005190
5191 /* See erasesize comment */
5192 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005193 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005194 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005195
Zach Brown34da5f52017-01-10 13:30:21 -06005196 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5197 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5198
Miquel Raynala97421c2018-03-19 14:47:27 +01005199 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005200 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005201
Huang Shijie10c86ba2013-05-17 11:17:26 +08005202 if (p->ecc_bits != 0xff) {
5203 chip->ecc_strength_ds = p->ecc_bits;
5204 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005205 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005206 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005207
5208 /*
5209 * The nand_flash_detect_ext_param_page() uses the
5210 * Change Read Column command which maybe not supported
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005211 * by the chip->legacy.cmdfunc. So try to update the
5212 * chip->legacy.cmdfunc now. We do not replace user supplied
5213 * command function.
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005214 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005215 if (mtd->writesize > 512 &&
5216 chip->legacy.cmdfunc == nand_command)
5217 chip->legacy.cmdfunc = nand_command_lp;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005218
5219 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005220 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005221 pr_warn("Failed to detect ONFI extended param page\n");
5222 } else {
5223 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005224 }
5225
Miquel Raynalf4531b22018-03-19 14:47:26 +01005226 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005227 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005228 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005229 bitmap_set(chip->parameters.get_feature_list,
5230 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5231 bitmap_set(chip->parameters.set_feature_list,
5232 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5233 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005234
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005235 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5236 if (!onfi) {
5237 ret = -ENOMEM;
5238 goto free_model;
5239 }
5240
5241 onfi->version = onfi_version;
5242 onfi->tPROG = le16_to_cpu(p->t_prog);
5243 onfi->tBERS = le16_to_cpu(p->t_bers);
5244 onfi->tR = le16_to_cpu(p->t_r);
5245 onfi->tCCS = le16_to_cpu(p->t_ccs);
5246 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5247 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5248 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5249 chip->parameters.onfi = onfi;
5250
5251 /* Identification done, free the full ONFI parameter page and exit */
5252 kfree(p);
5253
5254 return 1;
5255
5256free_model:
5257 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005258free_onfi_param_page:
5259 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005260
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005261 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005262}
5263
5264/*
Huang Shijie91361812014-02-21 13:39:40 +08005265 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5266 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005267static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005268{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005269 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005270 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005271 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005272 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005273 char id[5];
5274 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005275
5276 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005277 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5278 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005279 return 0;
5280
Miquel Raynal480139d2018-03-19 14:47:30 +01005281 /* JEDEC chip: allocate a buffer to hold its parameter page */
5282 p = kzalloc(sizeof(*p), GFP_KERNEL);
5283 if (!p)
5284 return -ENOMEM;
5285
Boris Brezillon97d90da2017-11-30 18:01:29 +01005286 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005287 if (ret) {
5288 ret = 0;
5289 goto free_jedec_param_page;
5290 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005291
Huang Shijie91361812014-02-21 13:39:40 +08005292 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005293 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005294 if (ret) {
5295 ret = 0;
5296 goto free_jedec_param_page;
5297 }
Huang Shijie91361812014-02-21 13:39:40 +08005298
5299 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5300 le16_to_cpu(p->crc))
5301 break;
5302 }
5303
5304 if (i == 3) {
5305 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005306 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005307 }
5308
5309 /* Check version */
5310 val = le16_to_cpu(p->revision);
5311 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005312 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005313 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005314 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005315
Miquel Raynal480139d2018-03-19 14:47:30 +01005316 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005317 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005318 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005319 }
5320
5321 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5322 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005323 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5324 if (!chip->parameters.model) {
5325 ret = -ENOMEM;
5326 goto free_jedec_param_page;
5327 }
Huang Shijie91361812014-02-21 13:39:40 +08005328
5329 mtd->writesize = le32_to_cpu(p->byte_per_page);
5330
5331 /* Please reference to the comment for nand_flash_detect_onfi. */
5332 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5333 mtd->erasesize *= mtd->writesize;
5334
5335 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5336
5337 /* Please reference to the comment for nand_flash_detect_onfi. */
5338 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5339 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5340 chip->bits_per_cell = p->bits_per_cell;
5341
Miquel Raynal480139d2018-03-19 14:47:30 +01005342 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005343 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005344
5345 /* ECC info */
5346 ecc = &p->ecc_info[0];
5347
5348 if (ecc->codeword_size >= 9) {
5349 chip->ecc_strength_ds = ecc->ecc_bits;
5350 chip->ecc_step_ds = 1 << ecc->codeword_size;
5351 } else {
5352 pr_warn("Invalid codeword size\n");
5353 }
5354
Miquel Raynal480139d2018-03-19 14:47:30 +01005355free_jedec_param_page:
5356 kfree(p);
5357 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005358}
5359
5360/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005361 * nand_id_has_period - Check if an ID string has a given wraparound period
5362 * @id_data: the ID string
5363 * @arrlen: the length of the @id_data array
5364 * @period: the period of repitition
5365 *
5366 * Check if an ID string is repeated within a given sequence of bytes at
5367 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005368 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005369 * if the repetition has a period of @period; otherwise, returns zero.
5370 */
5371static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5372{
5373 int i, j;
5374 for (i = 0; i < period; i++)
5375 for (j = i + period; j < arrlen; j += period)
5376 if (id_data[i] != id_data[j])
5377 return 0;
5378 return 1;
5379}
5380
5381/*
5382 * nand_id_len - Get the length of an ID string returned by CMD_READID
5383 * @id_data: the ID string
5384 * @arrlen: the length of the @id_data array
5385
5386 * Returns the length of the ID string, according to known wraparound/trailing
5387 * zero patterns. If no pattern exists, returns the length of the array.
5388 */
5389static int nand_id_len(u8 *id_data, int arrlen)
5390{
5391 int last_nonzero, period;
5392
5393 /* Find last non-zero byte */
5394 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5395 if (id_data[last_nonzero])
5396 break;
5397
5398 /* All zeros */
5399 if (last_nonzero < 0)
5400 return 0;
5401
5402 /* Calculate wraparound period */
5403 for (period = 1; period < arrlen; period++)
5404 if (nand_id_has_period(id_data, arrlen, period))
5405 break;
5406
5407 /* There's a repeated pattern */
5408 if (period < arrlen)
5409 return period;
5410
5411 /* There are trailing zeros */
5412 if (last_nonzero < arrlen - 1)
5413 return last_nonzero + 1;
5414
5415 /* No pattern detected */
5416 return arrlen;
5417}
5418
Huang Shijie7db906b2013-09-25 14:58:11 +08005419/* Extract the bits of per cell from the 3rd byte of the extended ID */
5420static int nand_get_bits_per_cell(u8 cellinfo)
5421{
5422 int bits;
5423
5424 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5425 bits >>= NAND_CI_CELLTYPE_SHIFT;
5426 return bits + 1;
5427}
5428
Brian Norrise3b88bd2012-09-24 20:40:52 -07005429/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005430 * Many new NAND share similar device ID codes, which represent the size of the
5431 * chip. The rest of the parameters must be decoded according to generic or
5432 * manufacturer-specific "extended ID" decoding patterns.
5433 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005434void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005435{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005436 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005437 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005438 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005439 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005440 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005441 /* The 4th id byte is the important one */
5442 extid = id_data[3];
5443
Boris Brezillon01389b62016-06-08 10:30:18 +02005444 /* Calc pagesize */
5445 mtd->writesize = 1024 << (extid & 0x03);
5446 extid >>= 2;
5447 /* Calc oobsize */
5448 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5449 extid >>= 2;
5450 /* Calc blocksize. Blocksize is multiples of 64KiB */
5451 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5452 extid >>= 2;
5453 /* Get buswidth information */
5454 if (extid & 0x1)
5455 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005456}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005457EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005458
5459/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005460 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5461 * decodes a matching ID table entry and assigns the MTD size parameters for
5462 * the chip.
5463 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005464static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005465{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005466 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005467
5468 mtd->erasesize = type->erasesize;
5469 mtd->writesize = type->pagesize;
5470 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005471
Huang Shijie1c195e92013-09-25 14:58:12 +08005472 /* All legacy ID NAND are small-page, SLC */
5473 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005474}
5475
5476/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005477 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5478 * heuristic patterns using various detected parameters (e.g., manufacturer,
5479 * page size, cell-type information).
5480 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005481static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005482{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005483 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005484
5485 /* Set the bad block position */
5486 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5487 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5488 else
5489 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005490}
5491
Huang Shijieec6e87e2013-03-15 11:01:00 +08005492static inline bool is_full_id_nand(struct nand_flash_dev *type)
5493{
5494 return type->id_len;
5495}
5496
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005497static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005498 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005499{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005500 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005501 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005502
Huang Shijieec6e87e2013-03-15 11:01:00 +08005503 if (!strncmp(type->id, id_data, type->id_len)) {
5504 mtd->writesize = type->pagesize;
5505 mtd->erasesize = type->erasesize;
5506 mtd->oobsize = type->oobsize;
5507
Huang Shijie7db906b2013-09-25 14:58:11 +08005508 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005509 chip->chipsize = (uint64_t)type->chipsize << 20;
5510 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005511 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5512 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005513 chip->onfi_timing_mode_default =
5514 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005515
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005516 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5517 if (!chip->parameters.model)
5518 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005519
Huang Shijieec6e87e2013-03-15 11:01:00 +08005520 return true;
5521 }
5522 return false;
5523}
5524
Brian Norris7e74c2d2012-09-24 20:40:49 -07005525/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005526 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5527 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5528 * table.
5529 */
5530static void nand_manufacturer_detect(struct nand_chip *chip)
5531{
5532 /*
5533 * Try manufacturer detection if available and use
5534 * nand_decode_ext_id() otherwise.
5535 */
5536 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005537 chip->manufacturer.desc->ops->detect) {
5538 /* The 3rd id byte holds MLC / multichip data */
5539 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005540 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005541 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005542 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005543 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005544}
5545
5546/*
5547 * Manufacturer initialization. This function is called for all NANDs including
5548 * ONFI and JEDEC compliant ones.
5549 * Manufacturer drivers should put all their specific initialization code in
5550 * their ->init() hook.
5551 */
5552static int nand_manufacturer_init(struct nand_chip *chip)
5553{
5554 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5555 !chip->manufacturer.desc->ops->init)
5556 return 0;
5557
5558 return chip->manufacturer.desc->ops->init(chip);
5559}
5560
5561/*
5562 * Manufacturer cleanup. This function is called for all NANDs including
5563 * ONFI and JEDEC compliant ones.
5564 * Manufacturer drivers should put all their specific cleanup code in their
5565 * ->cleanup() hook.
5566 */
5567static void nand_manufacturer_cleanup(struct nand_chip *chip)
5568{
5569 /* Release manufacturer private data */
5570 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5571 chip->manufacturer.desc->ops->cleanup)
5572 chip->manufacturer.desc->ops->cleanup(chip);
5573}
5574
Boris Brezillon348d56a2018-09-07 00:38:48 +02005575static const char *
5576nand_manufacturer_name(const struct nand_manufacturer *manufacturer)
5577{
5578 return manufacturer ? manufacturer->name : "Unknown";
5579}
5580
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005581/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005582 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005583 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005584static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005585{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005586 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005587 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005588 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005589 u8 *id_data = chip->id.data;
5590 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Karl Beldanef89a882008-09-15 14:37:29 +02005592 /*
5593 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005594 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005595 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005596 ret = nand_reset(chip, 0);
5597 if (ret)
5598 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005599
5600 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005601 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005604 ret = nand_readid_op(chip, 0, id_data, 2);
5605 if (ret)
5606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
5608 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005609 maf_id = id_data[0];
5610 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Brian Norris8b6e50c2011-05-25 14:59:01 -07005612 /*
5613 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005614 * interface concerns can cause random data which looks like a
5615 * possibly credible NAND flash to appear. If the two results do
5616 * not match, ignore the device completely.
5617 */
5618
Brian Norris4aef9b72012-09-24 20:40:48 -07005619 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005620 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5621 if (ret)
5622 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005623
Boris Brezillon7f501f02016-05-24 19:20:05 +02005624 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005625 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005626 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005627 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005628 }
5629
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005630 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005631
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005632 /* Try to identify manufacturer */
5633 manufacturer = nand_get_manufacturer(maf_id);
5634 chip->manufacturer.desc = manufacturer;
5635
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005636 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005637 type = nand_flash_ids;
5638
Boris Brezillon29a198a2016-05-24 20:17:48 +02005639 /*
5640 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5641 * override it.
5642 * This is required to make sure initial NAND bus width set by the
5643 * NAND controller driver is coherent with the real NAND bus width
5644 * (extracted by auto-detection code).
5645 */
5646 busw = chip->options & NAND_BUSWIDTH_16;
5647
5648 /*
5649 * The flag is only set (never cleared), reset it to its default value
5650 * before starting auto-detection.
5651 */
5652 chip->options &= ~NAND_BUSWIDTH_16;
5653
Huang Shijieec6e87e2013-03-15 11:01:00 +08005654 for (; type->name != NULL; type++) {
5655 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005656 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005657 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005658 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005659 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005660 }
5661 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005662
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005663 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005664 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005665 ret = nand_flash_detect_onfi(chip);
5666 if (ret < 0)
5667 return ret;
5668 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005669 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005670
5671 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005672 ret = nand_flash_detect_jedec(chip);
5673 if (ret < 0)
5674 return ret;
5675 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005676 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005677 }
5678
David Woodhouse5e81e882010-02-26 18:32:56 +00005679 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005680 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005681
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005682 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5683 if (!chip->parameters.model)
5684 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005685
Adrian Hunter69423d92008-12-10 13:37:21 +00005686 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005687
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005688 if (!type->pagesize)
5689 nand_manufacturer_detect(chip);
5690 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005691 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005692
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005693 /* Get chip options */
5694 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005695
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005696ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005697 if (!mtd->name)
5698 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005699
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005700 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005701 WARN_ON(busw & NAND_BUSWIDTH_16);
5702 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005703 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5704 /*
5705 * Check, if buswidth is correct. Hardware drivers should set
5706 * chip correct!
5707 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005708 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005709 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005710 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5711 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005712 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5713 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005714 ret = -EINVAL;
5715
5716 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005717 }
5718
Boris Brezillon7f501f02016-05-24 19:20:05 +02005719 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005720
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005721 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005722 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005723 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005724 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005725
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005726 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005727 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005728 if (chip->chipsize & 0xffffffff)
5729 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005730 else {
5731 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5732 chip->chip_shift += 32 - 1;
5733 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005734
Masahiro Yamada14157f82017-09-13 11:05:50 +09005735 if (chip->chip_shift - chip->page_shift > 16)
5736 chip->options |= NAND_ROW_ADDR_3;
5737
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005738 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005739
Brian Norris8b6e50c2011-05-25 14:59:01 -07005740 /* Do not replace user supplied command function! */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005741 if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
5742 chip->legacy.cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005743
Ezequiel Garcia20171642013-11-25 08:30:31 -03005744 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005745 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005746 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5747 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005748 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005749 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005750 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005751 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005752
5753free_detect_allocation:
5754 kfree(chip->parameters.model);
5755
5756 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005757}
5758
Boris Brezillond48f62b2016-04-01 14:54:32 +02005759static const char * const nand_ecc_modes[] = {
5760 [NAND_ECC_NONE] = "none",
5761 [NAND_ECC_SOFT] = "soft",
5762 [NAND_ECC_HW] = "hw",
5763 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5764 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005765 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005766};
5767
5768static int of_get_nand_ecc_mode(struct device_node *np)
5769{
5770 const char *pm;
5771 int err, i;
5772
5773 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5774 if (err < 0)
5775 return err;
5776
5777 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5778 if (!strcasecmp(pm, nand_ecc_modes[i]))
5779 return i;
5780
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005781 /*
5782 * For backward compatibility we support few obsoleted values that don't
5783 * have their mappings into nand_ecc_modes_t anymore (they were merged
5784 * with other enums).
5785 */
5786 if (!strcasecmp(pm, "soft_bch"))
5787 return NAND_ECC_SOFT;
5788
Boris Brezillond48f62b2016-04-01 14:54:32 +02005789 return -ENODEV;
5790}
5791
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005792static const char * const nand_ecc_algos[] = {
5793 [NAND_ECC_HAMMING] = "hamming",
5794 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005795 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005796};
5797
Boris Brezillond48f62b2016-04-01 14:54:32 +02005798static int of_get_nand_ecc_algo(struct device_node *np)
5799{
5800 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005801 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005802
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005803 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5804 if (!err) {
5805 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5806 if (!strcasecmp(pm, nand_ecc_algos[i]))
5807 return i;
5808 return -ENODEV;
5809 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005810
5811 /*
5812 * For backward compatibility we also read "nand-ecc-mode" checking
5813 * for some obsoleted values that were specifying ECC algorithm.
5814 */
5815 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5816 if (err < 0)
5817 return err;
5818
5819 if (!strcasecmp(pm, "soft"))
5820 return NAND_ECC_HAMMING;
5821 else if (!strcasecmp(pm, "soft_bch"))
5822 return NAND_ECC_BCH;
5823
5824 return -ENODEV;
5825}
5826
5827static int of_get_nand_ecc_step_size(struct device_node *np)
5828{
5829 int ret;
5830 u32 val;
5831
5832 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5833 return ret ? ret : val;
5834}
5835
5836static int of_get_nand_ecc_strength(struct device_node *np)
5837{
5838 int ret;
5839 u32 val;
5840
5841 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5842 return ret ? ret : val;
5843}
5844
5845static int of_get_nand_bus_width(struct device_node *np)
5846{
5847 u32 val;
5848
5849 if (of_property_read_u32(np, "nand-bus-width", &val))
5850 return 8;
5851
5852 switch (val) {
5853 case 8:
5854 case 16:
5855 return val;
5856 default:
5857 return -EIO;
5858 }
5859}
5860
5861static bool of_get_nand_on_flash_bbt(struct device_node *np)
5862{
5863 return of_property_read_bool(np, "nand-on-flash-bbt");
5864}
5865
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005866static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005867{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005868 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005869 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005870
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005871 if (!dn)
5872 return 0;
5873
Brian Norris5844fee2015-01-23 00:22:27 -08005874 if (of_get_nand_bus_width(dn) == 16)
5875 chip->options |= NAND_BUSWIDTH_16;
5876
Stefan Agnerf922bd72018-06-24 23:27:23 +02005877 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5878 chip->options |= NAND_IS_BOOT_MEDIUM;
5879
Brian Norris5844fee2015-01-23 00:22:27 -08005880 if (of_get_nand_on_flash_bbt(dn))
5881 chip->bbt_options |= NAND_BBT_USE_FLASH;
5882
5883 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005884 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005885 ecc_strength = of_get_nand_ecc_strength(dn);
5886 ecc_step = of_get_nand_ecc_step_size(dn);
5887
Brian Norris5844fee2015-01-23 00:22:27 -08005888 if (ecc_mode >= 0)
5889 chip->ecc.mode = ecc_mode;
5890
Rafał Miłecki79082452016-03-23 11:19:02 +01005891 if (ecc_algo >= 0)
5892 chip->ecc.algo = ecc_algo;
5893
Brian Norris5844fee2015-01-23 00:22:27 -08005894 if (ecc_strength >= 0)
5895 chip->ecc.strength = ecc_strength;
5896
5897 if (ecc_step > 0)
5898 chip->ecc.size = ecc_step;
5899
Boris Brezillonba78ee02016-06-08 17:04:22 +02005900 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5901 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5902
Brian Norris5844fee2015-01-23 00:22:27 -08005903 return 0;
5904}
5905
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005906/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005907 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005908 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005909 * @maxchips: number of chips to scan for
5910 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005911 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005912 * This is the first phase of the normal nand_scan() function. It reads the
5913 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005914 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005915 * This helper used to be called directly from controller drivers that needed
5916 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5917 * prevented dynamic allocations during this phase which was unconvenient and
5918 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005919 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005920static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005921 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005922{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005923 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005924 int nand_maf_id, nand_dev_id;
5925 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005926 int ret;
5927
Miquel Raynal17fa8042017-11-30 18:01:31 +01005928 /* Enforce the right timings for reset/detection */
5929 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5930
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005931 ret = nand_dt_init(chip);
5932 if (ret)
5933 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005934
Brian Norrisf7a8e382016-01-05 10:39:45 -08005935 if (!mtd->name && mtd->dev.parent)
5936 mtd->name = dev_name(mtd->dev.parent);
5937
Miquel Raynal8878b122017-11-09 14:16:45 +01005938 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005939 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
5940 * not populated.
Miquel Raynal8878b122017-11-09 14:16:45 +01005941 */
5942 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005943 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005944 * Default functions assigned for ->legacy.cmdfunc() and
5945 * ->select_chip() both expect ->legacy.cmd_ctrl() to be
5946 * populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005947 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005948 if ((!chip->legacy.cmdfunc || !chip->select_chip) &&
5949 !chip->legacy.cmd_ctrl) {
5950 pr_err("->legacy.cmd_ctrl() should be provided\n");
Miquel Raynal8878b122017-11-09 14:16:45 +01005951 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
Boris Brezillon800342d2018-08-04 22:59:23 +02006729 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006730 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006731 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006732 * This fills out all the uninitialized function pointers with the defaults.
6733 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006734 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006735 */
Boris Brezillon871a4072018-08-04 22:59:22 +02006736int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006737 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006738{
6739 int ret;
6740
Boris Brezillon800342d2018-08-04 22:59:23 +02006741 if (!maxchips)
6742 return -EINVAL;
6743
6744 ret = nand_scan_ident(chip, maxchips, ids);
6745 if (ret)
6746 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006747
6748 ret = nand_attach(chip);
6749 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006750 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006751
Boris Brezillon00ad3782018-09-06 14:05:14 +02006752 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006753 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006754 goto detach_chip;
6755
6756 return 0;
6757
6758detach_chip:
6759 nand_detach(chip);
6760cleanup_ident:
6761 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006762
David Woodhouse3b85c322006-09-25 17:06:53 +01006763 return ret;
6764}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006765EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006766
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006768 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6769 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006770 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006771void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006773 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006774 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006775 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6776
Jesper Juhlfa671642005-11-07 01:01:27 -08006777 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006778 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006779 kfree(chip->data_buf);
6780 kfree(chip->ecc.code_buf);
6781 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006782
6783 /* Free bad block descriptor memory */
6784 if (chip->badblock_pattern && chip->badblock_pattern->options
6785 & NAND_BBT_DYNAMICSTRUCT)
6786 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006787
6788 /* Free manufacturer priv data. */
6789 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006790
6791 /* Free controller specific allocations after chip identification */
6792 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006793
6794 /* Free identification phase allocations */
6795 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006797
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006798EXPORT_SYMBOL_GPL(nand_cleanup);
6799
6800/**
6801 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6802 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006803 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006804 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006805void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006806{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006807 mtd_device_unregister(nand_to_mtd(chip));
6808 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006809}
David Woodhousee0c7d762006-05-13 18:07:53 +01006810EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006811
David Woodhousee0c7d762006-05-13 18:07:53 +01006812MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006813MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6814MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006815MODULE_DESCRIPTION("Generic NAND flash driver code");