blob: 136ccdc61a06181fa0fa16bfa773d5138b382971 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200257 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200261static uint8_t nand_read_byte(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200263 return readb(chip->legacy.IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Masanari Iida064a7692012-11-09 23:20:58 +0900267 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200268 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700270 * Default read function for 16bit buswidth with endianness conversion.
271 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200273static uint8_t nand_read_byte16(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200275 return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * nand_select_chip - [DEFAULT] control CE line
Boris Brezillon758b56f2018-09-06 14:05:24 +0200280 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700281 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * Default select function for 1 chip devices.
284 */
Boris Brezillon758b56f2018-09-06 14:05:24 +0200285static void nand_select_chip(struct nand_chip *chip, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200287 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case -1:
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200289 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
290 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294
295 default:
296 BUG();
297 }
298}
299
300/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100301 * nand_write_byte - [DEFAULT] write single byte to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200302 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100303 * @byte: value to write
304 *
305 * Default function to write a byte to I/O[7:0]
306 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200307static void nand_write_byte(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100308{
Boris Brezillon716bbba2018-09-07 00:38:35 +0200309 chip->legacy.write_buf(chip, &byte, 1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100310}
311
312/**
313 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
Boris Brezillonc0739d82018-09-06 14:05:23 +0200314 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100315 * @byte: value to write
316 *
317 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
318 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200319static void nand_write_byte16(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100320{
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100321 uint16_t word = byte;
322
323 /*
324 * It's not entirely clear what should happen to I/O[15:8] when writing
325 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
326 *
327 * When the host supports a 16-bit bus width, only data is
328 * transferred at the 16-bit width. All address and command line
329 * transfers shall use only the lower 8-bits of the data bus. During
330 * command transfers, the host may place any value on the upper
331 * 8-bits of the data bus. During address transfers, the host shall
332 * set the upper 8-bits of the data bus to 00h.
333 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100334 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100335 * four parameters are specified to be written to I/O[7:0], but this is
336 * neither an address nor a command transfer. Let's assume a 0 on the
337 * upper I/O lines is OK.
338 */
Boris Brezillon716bbba2018-09-07 00:38:35 +0200339 chip->legacy.write_buf(chip, (uint8_t *)&word, 2);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100340}
341
342/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * nand_write_buf - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200344 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700345 * @buf: data buffer
346 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700348 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200350static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200352 iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000356 * nand_read_buf - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200357 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700358 * @buf: buffer to store date
359 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700361 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200363static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200365 ioread8_rep(chip->legacy.IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * nand_write_buf16 - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200370 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700371 * @buf: data buffer
372 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700374 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200376static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf,
377 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380
Boris Brezillon82fc5092018-09-07 00:38:34 +0200381 iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200386 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700387 * @buf: buffer to store date
388 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700390 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200392static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Boris Brezillon82fc5092018-09-07 00:38:34 +0200396 ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200401 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700402 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000404 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200406static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200408 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900409 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900410 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Brian Norris5fb15492011-05-31 16:31:21 -0700412 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700413 ofs += mtd->erasesize - mtd->writesize;
414
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100415 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900416 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100417
Masahiro Yamadac120e752017-03-23 05:07:01 +0900418 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200419 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530420 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900421 return res;
422
423 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000424
Brian Norriscdbec052012-01-13 18:11:48 -0800425 if (likely(chip->badblockbits == 8))
426 res = bad != 0xFF;
427 else
428 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900429 if (res)
430 return res;
431 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200432
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700437 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200438 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700439 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700441 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700442 * specific driver. It provides the details for writing a bad block marker to a
443 * block.
444 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200445static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700446{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200447 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700448 struct mtd_oob_ops ops;
449 uint8_t buf[2] = { 0, 0 };
450 int ret = 0, res, i = 0;
451
Brian Norris0ec56dc2015-02-28 02:02:30 -0800452 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700453 ops.oobbuf = buf;
454 ops.ooboffs = chip->badblockpos;
455 if (chip->options & NAND_BUSWIDTH_16) {
456 ops.ooboffs &= ~0x01;
457 ops.len = ops.ooblen = 2;
458 } else {
459 ops.len = ops.ooblen = 1;
460 }
461 ops.mode = MTD_OPS_PLACE_OOB;
462
463 /* Write to first/last page(s) if necessary */
464 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
465 ofs += mtd->erasesize - mtd->writesize;
466 do {
467 res = nand_do_write_oob(mtd, ofs, &ops);
468 if (!ret)
469 ret = res;
470
471 i++;
472 ofs += mtd->writesize;
473 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
474
475 return ret;
476}
477
478/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200479 * nand_markbad_bbm - mark a block by updating the BBM
480 * @chip: NAND chip object
481 * @ofs: offset of the block to mark bad
482 */
483int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
484{
485 if (chip->legacy.block_markbad)
486 return chip->legacy.block_markbad(chip, ofs);
487
488 return nand_default_block_markbad(chip, ofs);
489}
490
491static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
492{
493 if (chip->legacy.block_bad)
494 return chip->legacy.block_bad(chip, ofs);
495
496 return nand_block_bad(chip, ofs);
497}
498
499/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700500 * nand_block_markbad_lowlevel - mark a block bad
501 * @mtd: MTD device structure
502 * @ofs: offset from device start
503 *
504 * This function performs the generic NAND bad block marking steps (i.e., bad
505 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200506 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700507 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700508 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300509 *
Brian Norrise2414f42012-02-06 13:44:00 -0800510 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700511 * (2) write bad block marker to OOB area of affected block (unless flag
512 * NAND_BBT_NO_OOB_BBM is present)
513 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300514 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700515 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800516 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700518static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100520 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700521 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000522
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800524 struct erase_info einfo;
525
526 /* Attempt erase before marking OOB */
527 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800528 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300529 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200530 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800531
Brian Norrisb32843b2013-07-30 17:52:59 -0700532 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800533 nand_get_device(mtd, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200534 ret = nand_markbad_bbm(chip, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300535 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200536 }
Brian Norrise2414f42012-02-06 13:44:00 -0800537
Brian Norrisb32843b2013-07-30 17:52:59 -0700538 /* Mark block bad in BBT */
539 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200540 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800541 if (!ret)
542 ret = res;
543 }
544
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200545 if (!ret)
546 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000551/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700553 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700555 * Check, if the device is write protected. The function expects, that the
556 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100558static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100560 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100561 u8 status;
562 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200563
Brian Norris8b6e50c2011-05-25 14:59:01 -0700564 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200565 if (chip->options & NAND_BROKEN_XD)
566 return 0;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100569 ret = nand_status_op(chip, &status);
570 if (ret)
571 return ret;
572
573 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800577 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700578 * @mtd: MTD device structure
579 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300580 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800581 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300582 */
583static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
584{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100585 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300586
587 if (!chip->bbt)
588 return 0;
589 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200590 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300591}
592
593/**
594 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
595 * @mtd: MTD device structure
596 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700597 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
599 * Check, if the block is bad. Either by reading the bad block table or
600 * calling of the scan function.
601 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530602static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100604 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200607 if (chip->bbt)
608 return nand_isbad_bbt(chip, ofs, allowbbt);
609
610 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200613/**
614 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700615 * @mtd: MTD device structure
616 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200617 *
618 * Helper function for nand_wait_ready used when needing to wait in interrupt
619 * context.
620 */
621static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
622{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100623 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200624 int i;
625
626 /* Wait for the device to get ready */
627 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200628 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200629 break;
630 touch_softlockup_watchdog();
631 mdelay(1);
632 }
633}
634
Alex Smithb70af9b2015-10-06 14:52:07 +0100635/**
636 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200637 * @chip: NAND chip object
Alex Smithb70af9b2015-10-06 14:52:07 +0100638 *
639 * Wait for the ready pin after a command, and warn if a timeout occurs.
640 */
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200641void nand_wait_ready(struct nand_chip *chip)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000642{
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200643 struct mtd_info *mtd = nand_to_mtd(chip);
Alex Smithb70af9b2015-10-06 14:52:07 +0100644 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200646 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200648
Brian Norris7854d3f2011-06-23 14:12:08 -0700649 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000651 do {
Boris Brezillon8395b752018-09-07 00:38:37 +0200652 if (chip->legacy.dev_ready(chip))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300653 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100654 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000655 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100656
Boris Brezillon8395b752018-09-07 00:38:37 +0200657 if (!chip->legacy.dev_ready(chip))
Brian Norris9ebfdf52016-03-04 17:19:23 -0800658 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000659}
David Woodhouse4b648b02006-09-25 17:05:24 +0100660EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200663 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
664 * @mtd: MTD device structure
665 * @timeo: Timeout in ms
666 *
667 * Wait for status ready (i.e. command done) or timeout.
668 */
669static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
670{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100671 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100672 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200673
674 timeo = jiffies + msecs_to_jiffies(timeo);
675 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100676 u8 status;
677
678 ret = nand_read_data_op(chip, &status, sizeof(status), true);
679 if (ret)
680 return;
681
682 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200683 break;
684 touch_softlockup_watchdog();
685 } while (time_before(jiffies, timeo));
686};
687
688/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100689 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
690 * @chip: NAND chip structure
691 * @timeout_ms: Timeout in ms
692 *
693 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
694 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
695 * returned.
696 *
697 * This helper is intended to be used when the controller does not have access
698 * to the NAND R/B pin.
699 *
700 * Be aware that calling this helper from an ->exec_op() implementation means
701 * ->exec_op() must be re-entrant.
702 *
703 * Return 0 if the NAND chip is ready, a negative error otherwise.
704 */
705int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
706{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200707 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100708 u8 status = 0;
709 int ret;
710
711 if (!chip->exec_op)
712 return -ENOTSUPP;
713
Boris Brezillon3057fce2018-05-04 21:24:31 +0200714 /* Wait tWB before polling the STATUS reg. */
715 timings = nand_get_sdr_timings(&chip->data_interface);
716 ndelay(PSEC_TO_NSEC(timings->tWB_max));
717
Miquel Raynal8878b122017-11-09 14:16:45 +0100718 ret = nand_status_op(chip, NULL);
719 if (ret)
720 return ret;
721
722 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
723 do {
724 ret = nand_read_data_op(chip, &status, sizeof(status), true);
725 if (ret)
726 break;
727
728 if (status & NAND_STATUS_READY)
729 break;
730
731 /*
732 * Typical lowest execution time for a tR on most NANDs is 10us,
733 * use this as polling delay before doing something smarter (ie.
734 * deriving a delay from the timeout value, timeout_ms/ratio).
735 */
736 udelay(10);
737 } while (time_before(jiffies, timeout_ms));
738
739 /*
740 * We have to exit READ_STATUS mode in order to read real data on the
741 * bus in case the WAITRDY instruction is preceding a DATA_IN
742 * instruction.
743 */
744 nand_exit_status_op(chip);
745
746 if (ret)
747 return ret;
748
749 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
750};
751EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
752
753/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * nand_command - [DEFAULT] Send command to NAND device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200755 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700756 * @command: the command to be sent
757 * @column: the column address for this command, -1 if none
758 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700760 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200761 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200763static void nand_command(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200764 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200766 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200767 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Brian Norris8b6e50c2011-05-25 14:59:01 -0700769 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (command == NAND_CMD_SEQIN) {
771 int readcmd;
772
Joern Engel28318772006-05-22 23:18:05 +0200773 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200775 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 readcmd = NAND_CMD_READOOB;
777 } else if (column < 256) {
778 /* First 256 bytes --> READ0 */
779 readcmd = NAND_CMD_READ0;
780 } else {
781 column -= 256;
782 readcmd = NAND_CMD_READ1;
783 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200784 chip->legacy.cmd_ctrl(chip, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200785 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100787 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200788 chip->legacy.cmd_ctrl(chip, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Brian Norris8b6e50c2011-05-25 14:59:01 -0700790 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200791 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
792 /* Serially input address */
793 if (column != -1) {
794 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800795 if (chip->options & NAND_BUSWIDTH_16 &&
796 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200797 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200798 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200802 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200803 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200804 chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900805 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200806 chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200807 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200808 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
809 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000810
811 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700812 * Program and erase have their own busy handlers status and sequential
813 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000816
Miquel Raynaldf467892017-11-08 17:00:27 +0100817 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case NAND_CMD_PAGEPROG:
819 case NAND_CMD_ERASE1:
820 case NAND_CMD_ERASE2:
821 case NAND_CMD_SEQIN:
822 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900823 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900824 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
826
827 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200828 if (chip->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200830 udelay(chip->legacy.chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200831 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
832 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
833 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
834 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200835 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
836 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838
David Woodhousee0c7d762006-05-13 18:07:53 +0100839 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200840 case NAND_CMD_READ0:
841 /*
842 * READ0 is sometimes used to exit GET STATUS mode. When this
843 * is the case no address cycles are requested, and we can use
844 * this information to detect that we should not wait for the
845 * device to be ready.
846 */
847 if (column == -1 && page_addr == -1)
848 return;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * If we don't have access to the busy pin, we apply the given
853 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100854 */
Boris Brezillon8395b752018-09-07 00:38:37 +0200855 if (!chip->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200856 udelay(chip->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 /*
861 * Apply this short delay always to ensure that we do wait tWB in
862 * any case on any machine.
863 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100864 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000865
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200866 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200869static void nand_ccs_delay(struct nand_chip *chip)
870{
871 /*
872 * The controller already takes care of waiting for tCCS when the RNDIN
873 * or RNDOUT command is sent, return directly.
874 */
875 if (!(chip->options & NAND_WAIT_TCCS))
876 return;
877
878 /*
879 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
880 * (which should be safe for all NANDs).
881 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100882 if (chip->setup_data_interface)
883 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200884 else
885 ndelay(500);
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/**
889 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200890 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700891 * @command: the command to be sent
892 * @column: the column address for this command, -1 if none
893 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200895 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700896 * devices. We don't have the separate regions as we have in the small page
897 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200899static void nand_command_lp(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200900 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200902 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* Emulate NAND_CMD_READOOB */
905 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200906 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 command = NAND_CMD_READ0;
908 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000909
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200910 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100911 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200912 chip->legacy.cmd_ctrl(chip, command,
913 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200916 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* Serially input address */
919 if (column != -1) {
920 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800921 if (chip->options & NAND_BUSWIDTH_16 &&
922 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200924 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200925 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200926
Brian Norrisf5b88de2016-10-03 09:49:35 -0700927 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200928 if (!nand_opcode_8bits(command))
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200929 chip->legacy.cmd_ctrl(chip, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200932 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
933 chip->legacy.cmd_ctrl(chip, page_addr >> 8,
934 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900935 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200936 chip->legacy.cmd_ctrl(chip, page_addr >> 16,
937 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200940 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
941 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000942
943 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700944 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100945 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000948
Miquel Raynaldf467892017-11-08 17:00:27 +0100949 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case NAND_CMD_CACHEDPROG:
951 case NAND_CMD_PAGEPROG:
952 case NAND_CMD_ERASE1:
953 case NAND_CMD_ERASE2:
954 case NAND_CMD_SEQIN:
955 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900956 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900957 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200960 case NAND_CMD_RNDIN:
961 nand_ccs_delay(chip);
962 return;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200965 if (chip->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200967 udelay(chip->legacy.chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200968 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
969 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
970 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
971 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200972 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
973 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
975
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200976 case NAND_CMD_RNDOUT:
977 /* No ready / busy check necessary */
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200978 chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
979 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
980 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
981 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200982
983 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200984 return;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200987 /*
988 * READ0 is sometimes used to exit GET STATUS mode. When this
989 * is the case no address cycles are requested, and we can use
990 * this information to detect that READSTART should not be
991 * issued.
992 */
993 if (column == -1 && page_addr == -1)
994 return;
995
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200996 chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART,
997 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
998 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
999 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001000
David Woodhousee0c7d762006-05-13 18:07:53 +01001001 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001003 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001005 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001006 */
Boris Brezillon8395b752018-09-07 00:38:37 +02001007 if (!chip->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001008 udelay(chip->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001012
Brian Norris8b6e50c2011-05-25 14:59:01 -07001013 /*
1014 * Apply this short delay always to ensure that we do wait tWB in
1015 * any case on any machine.
1016 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001017 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001018
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001019 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001023 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001024 * @chip: the nand chip descriptor
1025 * @mtd: MTD device structure
1026 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001027 *
1028 * Used when in panic, no locks are taken.
1029 */
1030static void panic_nand_get_device(struct nand_chip *chip,
1031 struct mtd_info *mtd, int new_state)
1032{
Brian Norris7854d3f2011-06-23 14:12:08 -07001033 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001034 chip->controller->active = chip;
1035 chip->state = new_state;
1036}
1037
1038/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001040 * @mtd: MTD device structure
1041 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *
1043 * Get the device and lock it for exclusive access
1044 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001045static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001046nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001048 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001049 spinlock_t *lock = &chip->controller->lock;
1050 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001051 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001052retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001053 spin_lock(lock);
1054
vimal singhb8b3ee92009-07-09 20:41:22 +05301055 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001056 if (!chip->controller->active)
1057 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001058
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001059 if (chip->controller->active == chip && chip->state == FL_READY) {
1060 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001061 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001062 return 0;
1063 }
1064 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001065 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1066 chip->state = FL_PM_SUSPENDED;
1067 spin_unlock(lock);
1068 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001069 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001070 }
1071 set_current_state(TASK_UNINTERRUPTIBLE);
1072 add_wait_queue(wq, &wait);
1073 spin_unlock(lock);
1074 schedule();
1075 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto retry;
1077}
1078
1079/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001080 * panic_nand_wait - [GENERIC] wait until the command is done
1081 * @mtd: MTD device structure
1082 * @chip: NAND chip structure
1083 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001084 *
1085 * Wait for command done. This is a helper function for nand_wait used when
1086 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001087 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001088 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001089static void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001090{
1091 int i;
1092 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +02001093 if (chip->legacy.dev_ready) {
1094 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001095 break;
1096 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001097 int ret;
1098 u8 status;
1099
1100 ret = nand_read_data_op(chip, &status, sizeof(status),
1101 true);
1102 if (ret)
1103 return;
1104
1105 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001106 break;
1107 }
1108 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001109 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001110}
1111
1112/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001113 * nand_wait - [DEFAULT] wait until the command is done
1114 * @mtd: MTD device structure
1115 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001117 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001118 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001119static int nand_wait(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121
Alex Smithb70af9b2015-10-06 14:52:07 +01001122 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001123 u8 status;
1124 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Brian Norris8b6e50c2011-05-25 14:59:01 -07001126 /*
1127 * Apply this short delay always to ensure that we do wait tWB in any
1128 * case on any machine.
1129 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001130 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Boris Brezillon97d90da2017-11-30 18:01:29 +01001132 ret = nand_status_op(chip, NULL);
1133 if (ret)
1134 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001136 if (in_interrupt() || oops_in_progress)
Boris Brezillonf1d46942018-09-06 14:05:29 +02001137 panic_nand_wait(chip, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001138 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001139 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001140 do {
Boris Brezillon8395b752018-09-07 00:38:37 +02001141 if (chip->legacy.dev_ready) {
1142 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001143 break;
1144 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001145 ret = nand_read_data_op(chip, &status,
1146 sizeof(status), true);
1147 if (ret)
1148 return ret;
1149
1150 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001151 break;
1152 }
1153 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001154 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001156
Boris Brezillon97d90da2017-11-30 18:01:29 +01001157 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1158 if (ret)
1159 return ret;
1160
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001161 /* This can happen if in case of timeout or buggy dev_ready */
1162 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return status;
1164}
1165
Miquel Raynal789157e2018-03-19 14:47:28 +01001166static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001167{
Miquel Raynal789157e2018-03-19 14:47:28 +01001168 return (chip->parameters.supports_set_get_features &&
1169 test_bit(addr, chip->parameters.get_feature_list));
1170}
1171
1172static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1173{
1174 return (chip->parameters.supports_set_get_features &&
1175 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001176}
1177
1178/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001179 * nand_reset_data_interface - Reset data interface and timings
1180 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001181 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001182 *
1183 * Reset the Data interface and timings to ONFI mode 0.
1184 *
1185 * Returns 0 for success or negative error code otherwise.
1186 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001187static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001188{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001189 int ret;
1190
1191 if (!chip->setup_data_interface)
1192 return 0;
1193
1194 /*
1195 * The ONFI specification says:
1196 * "
1197 * To transition from NV-DDR or NV-DDR2 to the SDR data
1198 * interface, the host shall use the Reset (FFh) command
1199 * using SDR timing mode 0. A device in any timing mode is
1200 * required to recognize Reset (FFh) command issued in SDR
1201 * timing mode 0.
1202 * "
1203 *
1204 * Configure the data interface in SDR mode and set the
1205 * timings to timing mode 0.
1206 */
1207
Miquel Raynal17fa8042017-11-30 18:01:31 +01001208 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +02001209 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001210 if (ret)
1211 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1212
1213 return ret;
1214}
1215
1216/**
1217 * nand_setup_data_interface - Setup the best data interface and timings
1218 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001219 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001220 *
1221 * Find and configure the best data interface and NAND timings supported by
1222 * the chip and the driver.
1223 * First tries to retrieve supported timing modes from ONFI information,
1224 * and if the NAND chip does not support ONFI, relies on the
1225 * ->onfi_timing_mode_default specified in the nand_ids table.
1226 *
1227 * Returns 0 for success or negative error code otherwise.
1228 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001229static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001230{
Miquel Raynal97baea12018-03-19 14:47:20 +01001231 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1232 chip->onfi_timing_mode_default,
1233 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001234 int ret;
1235
Miquel Raynal17fa8042017-11-30 18:01:31 +01001236 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001237 return 0;
1238
Miquel Raynal993447b2018-03-19 14:47:21 +01001239 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001240 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001241 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001242 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1243 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001244 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001245 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001246 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001247 }
1248
Miquel Raynal97baea12018-03-19 14:47:20 +01001249 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +02001250 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001251 if (ret)
1252 return ret;
1253
1254 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001255 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001256 return 0;
1257
1258 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001259 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001260 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1261 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001262 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001263 if (ret)
1264 goto err_reset_chip;
1265
1266 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1267 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1268 chip->onfi_timing_mode_default);
1269 goto err_reset_chip;
1270 }
1271
1272 return 0;
1273
1274err_reset_chip:
1275 /*
1276 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1277 * timing mode.
1278 */
1279 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001280 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001281 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001282 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001283
Boris Brezillond8e725d2016-09-15 10:32:50 +02001284 return ret;
1285}
1286
1287/**
1288 * nand_init_data_interface - find the best data interface and timings
1289 * @chip: The NAND chip
1290 *
1291 * Find the best data interface and NAND timings supported by the chip
1292 * and the driver.
1293 * First tries to retrieve supported timing modes from ONFI information,
1294 * and if the NAND chip does not support ONFI, relies on the
1295 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1296 * function nand_chip->data_interface is initialized with the best timing mode
1297 * available.
1298 *
1299 * Returns 0 for success or negative error code otherwise.
1300 */
1301static int nand_init_data_interface(struct nand_chip *chip)
1302{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001303 int modes, mode, ret;
1304
1305 if (!chip->setup_data_interface)
1306 return 0;
1307
1308 /*
1309 * First try to identify the best timings from ONFI parameters and
1310 * if the NAND does not support ONFI, fallback to the default ONFI
1311 * timing mode.
1312 */
Boris Brezillon462f35d2018-09-07 00:38:47 +02001313 if (chip->parameters.onfi) {
1314 modes = chip->parameters.onfi->async_timing_mode;
1315 } else {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001316 if (!chip->onfi_timing_mode_default)
1317 return 0;
1318
1319 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1320 }
1321
Boris Brezillond8e725d2016-09-15 10:32:50 +02001322
1323 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}
2046EXPORT_SYMBOL_GPL(nand_exit_status_op);
2047
2048/**
2049 * nand_erase_op - Do an erase operation
2050 * @chip: The NAND chip
2051 * @eraseblock: block to erase
2052 *
2053 * This function sends an ERASE command and waits for the NAND to be ready
2054 * before returning.
2055 * This function does not select/unselect the CS line.
2056 *
2057 * Returns 0 on success, a negative error code otherwise.
2058 */
2059int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2060{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002061 unsigned int page = eraseblock <<
2062 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002063 int ret;
2064 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002065
Miquel Raynal8878b122017-11-09 14:16:45 +01002066 if (chip->exec_op) {
2067 const struct nand_sdr_timings *sdr =
2068 nand_get_sdr_timings(&chip->data_interface);
2069 u8 addrs[3] = { page, page >> 8, page >> 16 };
2070 struct nand_op_instr instrs[] = {
2071 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2072 NAND_OP_ADDR(2, addrs, 0),
2073 NAND_OP_CMD(NAND_CMD_ERASE2,
2074 PSEC_TO_MSEC(sdr->tWB_max)),
2075 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2076 };
2077 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002078
Miquel Raynal8878b122017-11-09 14:16:45 +01002079 if (chip->options & NAND_ROW_ADDR_3)
2080 instrs[1].ctx.addr.naddrs++;
2081
2082 ret = nand_exec_op(chip, &op);
2083 if (ret)
2084 return ret;
2085
2086 ret = nand_status_op(chip, &status);
2087 if (ret)
2088 return ret;
2089 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002090 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2091 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002092
Boris Brezillon8395b752018-09-07 00:38:37 +02002093 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002094 if (ret < 0)
2095 return ret;
2096
2097 status = ret;
2098 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002099
2100 if (status & NAND_STATUS_FAIL)
2101 return -EIO;
2102
2103 return 0;
2104}
2105EXPORT_SYMBOL_GPL(nand_erase_op);
2106
2107/**
2108 * nand_set_features_op - Do a SET FEATURES operation
2109 * @chip: The NAND chip
2110 * @feature: feature id
2111 * @data: 4 bytes of data
2112 *
2113 * This function sends a SET FEATURES command and waits for the NAND to be
2114 * ready before returning.
2115 * This function does not select/unselect the CS line.
2116 *
2117 * Returns 0 on success, a negative error code otherwise.
2118 */
2119static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2120 const void *data)
2121{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002122 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002123 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002124
Miquel Raynal8878b122017-11-09 14:16:45 +01002125 if (chip->exec_op) {
2126 const struct nand_sdr_timings *sdr =
2127 nand_get_sdr_timings(&chip->data_interface);
2128 struct nand_op_instr instrs[] = {
2129 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2130 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2131 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2132 PSEC_TO_NSEC(sdr->tWB_max)),
2133 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2134 };
2135 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002136
Boris Brezillon782d1962018-05-11 14:44:07 +02002137 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002138 }
2139
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002140 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002141 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002142 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002143
Boris Brezillon8395b752018-09-07 00:38:37 +02002144 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002145 if (ret < 0)
2146 return ret;
2147
2148 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002149 return -EIO;
2150
2151 return 0;
2152}
2153
2154/**
2155 * nand_get_features_op - Do a GET FEATURES operation
2156 * @chip: The NAND chip
2157 * @feature: feature id
2158 * @data: 4 bytes of data
2159 *
2160 * This function sends a GET FEATURES command and waits for the NAND to be
2161 * ready before returning.
2162 * This function does not select/unselect the CS line.
2163 *
2164 * Returns 0 on success, a negative error code otherwise.
2165 */
2166static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2167 void *data)
2168{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002169 u8 *params = data;
2170 int i;
2171
Miquel Raynal8878b122017-11-09 14:16:45 +01002172 if (chip->exec_op) {
2173 const struct nand_sdr_timings *sdr =
2174 nand_get_sdr_timings(&chip->data_interface);
2175 struct nand_op_instr instrs[] = {
2176 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2177 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2178 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2179 PSEC_TO_NSEC(sdr->tRR_min)),
2180 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2181 data, 0),
2182 };
2183 struct nand_operation op = NAND_OPERATION(instrs);
2184
2185 return nand_exec_op(chip, &op);
2186 }
2187
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002188 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002189 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002190 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002191
2192 return 0;
2193}
2194
Boris Brezillon52f05b62018-07-27 09:44:18 +02002195static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2196 unsigned int delay_ns)
2197{
2198 if (chip->exec_op) {
2199 struct nand_op_instr instrs[] = {
2200 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2201 PSEC_TO_NSEC(delay_ns)),
2202 };
2203 struct nand_operation op = NAND_OPERATION(instrs);
2204
2205 return nand_exec_op(chip, &op);
2206 }
2207
2208 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02002209 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02002210 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002211 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002212 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002213
2214 return 0;
2215}
2216
Boris Brezillon97d90da2017-11-30 18:01:29 +01002217/**
2218 * nand_reset_op - Do a reset operation
2219 * @chip: The NAND chip
2220 *
2221 * This function sends a RESET command and waits for the NAND to be ready
2222 * before returning.
2223 * This function does not select/unselect the CS line.
2224 *
2225 * Returns 0 on success, a negative error code otherwise.
2226 */
2227int nand_reset_op(struct nand_chip *chip)
2228{
Miquel Raynal8878b122017-11-09 14:16:45 +01002229 if (chip->exec_op) {
2230 const struct nand_sdr_timings *sdr =
2231 nand_get_sdr_timings(&chip->data_interface);
2232 struct nand_op_instr instrs[] = {
2233 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2234 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2235 };
2236 struct nand_operation op = NAND_OPERATION(instrs);
2237
2238 return nand_exec_op(chip, &op);
2239 }
2240
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002241 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002242
2243 return 0;
2244}
2245EXPORT_SYMBOL_GPL(nand_reset_op);
2246
2247/**
2248 * nand_read_data_op - Read data from the NAND
2249 * @chip: The NAND chip
2250 * @buf: buffer used to store the data
2251 * @len: length of the buffer
2252 * @force_8bit: force 8-bit bus access
2253 *
2254 * This function does a raw data read on the bus. Usually used after launching
2255 * another NAND operation like nand_read_page_op().
2256 * This function does not select/unselect the CS line.
2257 *
2258 * Returns 0 on success, a negative error code otherwise.
2259 */
2260int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2261 bool force_8bit)
2262{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002263 if (!len || !buf)
2264 return -EINVAL;
2265
Miquel Raynal8878b122017-11-09 14:16:45 +01002266 if (chip->exec_op) {
2267 struct nand_op_instr instrs[] = {
2268 NAND_OP_DATA_IN(len, buf, 0),
2269 };
2270 struct nand_operation op = NAND_OPERATION(instrs);
2271
2272 instrs[0].ctx.data.force_8bit = force_8bit;
2273
2274 return nand_exec_op(chip, &op);
2275 }
2276
Boris Brezillon97d90da2017-11-30 18:01:29 +01002277 if (force_8bit) {
2278 u8 *p = buf;
2279 unsigned int i;
2280
2281 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002282 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002283 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002284 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002285 }
2286
2287 return 0;
2288}
2289EXPORT_SYMBOL_GPL(nand_read_data_op);
2290
2291/**
2292 * nand_write_data_op - Write data from the NAND
2293 * @chip: The NAND chip
2294 * @buf: buffer containing the data to send on the bus
2295 * @len: length of the buffer
2296 * @force_8bit: force 8-bit bus access
2297 *
2298 * This function does a raw data write on the bus. Usually used after launching
2299 * another NAND operation like nand_write_page_begin_op().
2300 * This function does not select/unselect the CS line.
2301 *
2302 * Returns 0 on success, a negative error code otherwise.
2303 */
2304int nand_write_data_op(struct nand_chip *chip, const void *buf,
2305 unsigned int len, bool force_8bit)
2306{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002307 if (!len || !buf)
2308 return -EINVAL;
2309
Miquel Raynal8878b122017-11-09 14:16:45 +01002310 if (chip->exec_op) {
2311 struct nand_op_instr instrs[] = {
2312 NAND_OP_DATA_OUT(len, buf, 0),
2313 };
2314 struct nand_operation op = NAND_OPERATION(instrs);
2315
2316 instrs[0].ctx.data.force_8bit = force_8bit;
2317
2318 return nand_exec_op(chip, &op);
2319 }
2320
Boris Brezillon97d90da2017-11-30 18:01:29 +01002321 if (force_8bit) {
2322 const u8 *p = buf;
2323 unsigned int i;
2324
2325 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002326 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002327 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002328 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002329 }
2330
2331 return 0;
2332}
2333EXPORT_SYMBOL_GPL(nand_write_data_op);
2334
2335/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002336 * struct nand_op_parser_ctx - Context used by the parser
2337 * @instrs: array of all the instructions that must be addressed
2338 * @ninstrs: length of the @instrs array
2339 * @subop: Sub-operation to be passed to the NAND controller
2340 *
2341 * This structure is used by the core to split NAND operations into
2342 * sub-operations that can be handled by the NAND controller.
2343 */
2344struct nand_op_parser_ctx {
2345 const struct nand_op_instr *instrs;
2346 unsigned int ninstrs;
2347 struct nand_subop subop;
2348};
2349
2350/**
2351 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2352 * @pat: the parser pattern element that matches @instr
2353 * @instr: pointer to the instruction to check
2354 * @start_offset: this is an in/out parameter. If @instr has already been
2355 * split, then @start_offset is the offset from which to start
2356 * (either an address cycle or an offset in the data buffer).
2357 * Conversely, if the function returns true (ie. instr must be
2358 * split), this parameter is updated to point to the first
2359 * data/address cycle that has not been taken care of.
2360 *
2361 * Some NAND controllers are limited and cannot send X address cycles with a
2362 * unique operation, or cannot read/write more than Y bytes at the same time.
2363 * In this case, split the instruction that does not fit in a single
2364 * controller-operation into two or more chunks.
2365 *
2366 * Returns true if the instruction must be split, false otherwise.
2367 * The @start_offset parameter is also updated to the offset at which the next
2368 * bundle of instruction must start (if an address or a data instruction).
2369 */
2370static bool
2371nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2372 const struct nand_op_instr *instr,
2373 unsigned int *start_offset)
2374{
2375 switch (pat->type) {
2376 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002377 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002378 break;
2379
2380 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002381 pat->ctx.addr.maxcycles) {
2382 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002383 return true;
2384 }
2385 break;
2386
2387 case NAND_OP_DATA_IN_INSTR:
2388 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002389 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002390 break;
2391
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002392 if (instr->ctx.data.len - *start_offset >
2393 pat->ctx.data.maxlen) {
2394 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002395 return true;
2396 }
2397 break;
2398
2399 default:
2400 break;
2401 }
2402
2403 return false;
2404}
2405
2406/**
2407 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2408 * remaining in the parser context
2409 * @pat: the pattern to test
2410 * @ctx: the parser context structure to match with the pattern @pat
2411 *
2412 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2413 * Returns true if this is the case, false ortherwise. When true is returned,
2414 * @ctx->subop is updated with the set of instructions to be passed to the
2415 * controller driver.
2416 */
2417static bool
2418nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2419 struct nand_op_parser_ctx *ctx)
2420{
2421 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2422 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2423 const struct nand_op_instr *instr = ctx->subop.instrs;
2424 unsigned int i, ninstrs;
2425
2426 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2427 /*
2428 * The pattern instruction does not match the operation
2429 * instruction. If the instruction is marked optional in the
2430 * pattern definition, we skip the pattern element and continue
2431 * to the next one. If the element is mandatory, there's no
2432 * match and we can return false directly.
2433 */
2434 if (instr->type != pat->elems[i].type) {
2435 if (!pat->elems[i].optional)
2436 return false;
2437
2438 continue;
2439 }
2440
2441 /*
2442 * Now check the pattern element constraints. If the pattern is
2443 * not able to handle the whole instruction in a single step,
2444 * we have to split it.
2445 * The last_instr_end_off value comes back updated to point to
2446 * the position where we have to split the instruction (the
2447 * start of the next subop chunk).
2448 */
2449 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2450 &instr_offset)) {
2451 ninstrs++;
2452 i++;
2453 break;
2454 }
2455
2456 instr++;
2457 ninstrs++;
2458 instr_offset = 0;
2459 }
2460
2461 /*
2462 * This can happen if all instructions of a pattern are optional.
2463 * Still, if there's not at least one instruction handled by this
2464 * pattern, this is not a match, and we should try the next one (if
2465 * any).
2466 */
2467 if (!ninstrs)
2468 return false;
2469
2470 /*
2471 * We had a match on the pattern head, but the pattern may be longer
2472 * than the instructions we're asked to execute. We need to make sure
2473 * there's no mandatory elements in the pattern tail.
2474 */
2475 for (; i < pat->nelems; i++) {
2476 if (!pat->elems[i].optional)
2477 return false;
2478 }
2479
2480 /*
2481 * We have a match: update the subop structure accordingly and return
2482 * true.
2483 */
2484 ctx->subop.ninstrs = ninstrs;
2485 ctx->subop.last_instr_end_off = instr_offset;
2486
2487 return true;
2488}
2489
2490#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2491static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2492{
2493 const struct nand_op_instr *instr;
2494 char *prefix = " ";
2495 unsigned int i;
2496
2497 pr_debug("executing subop:\n");
2498
2499 for (i = 0; i < ctx->ninstrs; i++) {
2500 instr = &ctx->instrs[i];
2501
2502 if (instr == &ctx->subop.instrs[0])
2503 prefix = " ->";
2504
2505 switch (instr->type) {
2506 case NAND_OP_CMD_INSTR:
2507 pr_debug("%sCMD [0x%02x]\n", prefix,
2508 instr->ctx.cmd.opcode);
2509 break;
2510 case NAND_OP_ADDR_INSTR:
2511 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2512 instr->ctx.addr.naddrs,
2513 instr->ctx.addr.naddrs < 64 ?
2514 instr->ctx.addr.naddrs : 64,
2515 instr->ctx.addr.addrs);
2516 break;
2517 case NAND_OP_DATA_IN_INSTR:
2518 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2519 instr->ctx.data.len,
2520 instr->ctx.data.force_8bit ?
2521 ", force 8-bit" : "");
2522 break;
2523 case NAND_OP_DATA_OUT_INSTR:
2524 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2525 instr->ctx.data.len,
2526 instr->ctx.data.force_8bit ?
2527 ", force 8-bit" : "");
2528 break;
2529 case NAND_OP_WAITRDY_INSTR:
2530 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2531 instr->ctx.waitrdy.timeout_ms);
2532 break;
2533 }
2534
2535 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2536 prefix = " ";
2537 }
2538}
2539#else
2540static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2541{
2542 /* NOP */
2543}
2544#endif
2545
2546/**
2547 * nand_op_parser_exec_op - exec_op parser
2548 * @chip: the NAND chip
2549 * @parser: patterns description provided by the controller driver
2550 * @op: the NAND operation to address
2551 * @check_only: when true, the function only checks if @op can be handled but
2552 * does not execute the operation
2553 *
2554 * Helper function designed to ease integration of NAND controller drivers that
2555 * only support a limited set of instruction sequences. The supported sequences
2556 * are described in @parser, and the framework takes care of splitting @op into
2557 * multiple sub-operations (if required) and pass them back to the ->exec()
2558 * callback of the matching pattern if @check_only is set to false.
2559 *
2560 * NAND controller drivers should call this function from their own ->exec_op()
2561 * implementation.
2562 *
2563 * Returns 0 on success, a negative error code otherwise. A failure can be
2564 * caused by an unsupported operation (none of the supported patterns is able
2565 * to handle the requested operation), or an error returned by one of the
2566 * matching pattern->exec() hook.
2567 */
2568int nand_op_parser_exec_op(struct nand_chip *chip,
2569 const struct nand_op_parser *parser,
2570 const struct nand_operation *op, bool check_only)
2571{
2572 struct nand_op_parser_ctx ctx = {
2573 .subop.instrs = op->instrs,
2574 .instrs = op->instrs,
2575 .ninstrs = op->ninstrs,
2576 };
2577 unsigned int i;
2578
2579 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2580 int ret;
2581
2582 for (i = 0; i < parser->npatterns; i++) {
2583 const struct nand_op_parser_pattern *pattern;
2584
2585 pattern = &parser->patterns[i];
2586 if (!nand_op_parser_match_pat(pattern, &ctx))
2587 continue;
2588
2589 nand_op_parser_trace(&ctx);
2590
2591 if (check_only)
2592 break;
2593
2594 ret = pattern->exec(chip, &ctx.subop);
2595 if (ret)
2596 return ret;
2597
2598 break;
2599 }
2600
2601 if (i == parser->npatterns) {
2602 pr_debug("->exec_op() parser: pattern not found!\n");
2603 return -ENOTSUPP;
2604 }
2605
2606 /*
2607 * Update the context structure by pointing to the start of the
2608 * next subop.
2609 */
2610 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2611 if (ctx.subop.last_instr_end_off)
2612 ctx.subop.instrs -= 1;
2613
2614 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2615 }
2616
2617 return 0;
2618}
2619EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2620
2621static bool nand_instr_is_data(const struct nand_op_instr *instr)
2622{
2623 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2624 instr->type == NAND_OP_DATA_OUT_INSTR);
2625}
2626
2627static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2628 unsigned int instr_idx)
2629{
2630 return subop && instr_idx < subop->ninstrs;
2631}
2632
Miquel Raynal760c4352018-07-19 00:09:12 +02002633static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2634 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002635{
2636 if (instr_idx)
2637 return 0;
2638
2639 return subop->first_instr_start_off;
2640}
2641
2642/**
2643 * nand_subop_get_addr_start_off - Get the start offset in an address array
2644 * @subop: The entire sub-operation
2645 * @instr_idx: Index of the instruction inside the sub-operation
2646 *
2647 * During driver development, one could be tempted to directly use the
2648 * ->addr.addrs field of address instructions. This is wrong as address
2649 * instructions might be split.
2650 *
2651 * Given an address instruction, returns the offset of the first cycle to issue.
2652 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002653unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2654 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002655{
Miquel Raynal760c4352018-07-19 00:09:12 +02002656 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2657 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2658 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002659
2660 return nand_subop_get_start_off(subop, instr_idx);
2661}
2662EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2663
2664/**
2665 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2666 * @subop: The entire sub-operation
2667 * @instr_idx: Index of the instruction inside the sub-operation
2668 *
2669 * During driver development, one could be tempted to directly use the
2670 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2671 * might be split.
2672 *
2673 * Given an address instruction, returns the number of address cycle to issue.
2674 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002675unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2676 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002677{
2678 int start_off, end_off;
2679
Miquel Raynal760c4352018-07-19 00:09:12 +02002680 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2681 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2682 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002683
2684 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2685
2686 if (instr_idx == subop->ninstrs - 1 &&
2687 subop->last_instr_end_off)
2688 end_off = subop->last_instr_end_off;
2689 else
2690 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2691
2692 return end_off - start_off;
2693}
2694EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2695
2696/**
2697 * nand_subop_get_data_start_off - Get the start offset in a data array
2698 * @subop: The entire sub-operation
2699 * @instr_idx: Index of the instruction inside the sub-operation
2700 *
2701 * During driver development, one could be tempted to directly use the
2702 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2703 * instructions might be split.
2704 *
2705 * Given a data instruction, returns the offset to start from.
2706 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002707unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2708 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002709{
Miquel Raynal760c4352018-07-19 00:09:12 +02002710 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2711 !nand_instr_is_data(&subop->instrs[instr_idx])))
2712 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002713
2714 return nand_subop_get_start_off(subop, instr_idx);
2715}
2716EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2717
2718/**
2719 * nand_subop_get_data_len - Get the number of bytes to retrieve
2720 * @subop: The entire sub-operation
2721 * @instr_idx: Index of the instruction inside the sub-operation
2722 *
2723 * During driver development, one could be tempted to directly use the
2724 * ->data->len field of a data instruction. This is wrong as data instructions
2725 * might be split.
2726 *
2727 * Returns the length of the chunk of data to send/receive.
2728 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002729unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2730 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002731{
2732 int start_off = 0, end_off;
2733
Miquel Raynal760c4352018-07-19 00:09:12 +02002734 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2735 !nand_instr_is_data(&subop->instrs[instr_idx])))
2736 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002737
2738 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2739
2740 if (instr_idx == subop->ninstrs - 1 &&
2741 subop->last_instr_end_off)
2742 end_off = subop->last_instr_end_off;
2743 else
2744 end_off = subop->instrs[instr_idx].ctx.data.len;
2745
2746 return end_off - start_off;
2747}
2748EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002751 * nand_reset - Reset and initialize a NAND device
2752 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002753 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002754 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002755 * Save the timings data structure, then apply SDR timings mode 0 (see
2756 * nand_reset_data_interface for details), do the reset operation, and
2757 * apply back the previous timings.
2758 *
2759 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002760 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002761int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002762{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002763 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002764 int ret;
2765
Boris Brezillon104e4422017-03-16 09:35:58 +01002766 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002767 if (ret)
2768 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002769
Boris Brezillon73f907f2016-10-24 16:46:20 +02002770 /*
2771 * The CS line has to be released before we can apply the new NAND
2772 * interface settings, hence this weird ->select_chip() dance.
2773 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002774 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002775 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002776 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002777 if (ret)
2778 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002779
Miquel Raynal107b7d62018-03-19 14:47:25 +01002780 /*
2781 * A nand_reset_data_interface() put both the NAND chip and the NAND
2782 * controller in timings mode 0. If the default mode for this chip is
2783 * also 0, no need to proceed to the change again. Plus, at probe time,
2784 * nand_setup_data_interface() uses ->set/get_features() which would
2785 * fail anyway as the parameter page is not available yet.
2786 */
2787 if (!chip->onfi_timing_mode_default)
2788 return 0;
2789
Miquel Raynal17fa8042017-11-30 18:01:31 +01002790 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002791 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002792 if (ret)
2793 return ret;
2794
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002795 return 0;
2796}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002797EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002798
2799/**
Boris Brezillon45240362018-09-07 00:38:40 +02002800 * nand_get_features - wrapper to perform a GET_FEATURE
2801 * @chip: NAND chip info structure
2802 * @addr: feature address
2803 * @subfeature_param: the subfeature parameters, a four bytes array
2804 *
2805 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2806 * operation cannot be handled.
2807 */
2808int nand_get_features(struct nand_chip *chip, int addr,
2809 u8 *subfeature_param)
2810{
2811 if (!nand_supports_get_features(chip, addr))
2812 return -ENOTSUPP;
2813
2814 if (chip->legacy.get_features)
2815 return chip->legacy.get_features(chip, addr, subfeature_param);
2816
2817 return nand_get_features_op(chip, addr, subfeature_param);
2818}
2819EXPORT_SYMBOL_GPL(nand_get_features);
2820
2821/**
2822 * nand_set_features - wrapper to perform a SET_FEATURE
2823 * @chip: NAND chip info structure
2824 * @addr: feature address
2825 * @subfeature_param: the subfeature parameters, a four bytes array
2826 *
2827 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2828 * operation cannot be handled.
2829 */
2830int nand_set_features(struct nand_chip *chip, int addr,
2831 u8 *subfeature_param)
2832{
2833 if (!nand_supports_set_features(chip, addr))
2834 return -ENOTSUPP;
2835
2836 if (chip->legacy.set_features)
2837 return chip->legacy.set_features(chip, addr, subfeature_param);
2838
2839 return nand_set_features_op(chip, addr, subfeature_param);
2840}
2841EXPORT_SYMBOL_GPL(nand_set_features);
2842
2843/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002844 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2845 * @buf: buffer to test
2846 * @len: buffer length
2847 * @bitflips_threshold: maximum number of bitflips
2848 *
2849 * Check if a buffer contains only 0xff, which means the underlying region
2850 * has been erased and is ready to be programmed.
2851 * The bitflips_threshold specify the maximum number of bitflips before
2852 * considering the region is not erased.
2853 * Note: The logic of this function has been extracted from the memweight
2854 * implementation, except that nand_check_erased_buf function exit before
2855 * testing the whole buffer if the number of bitflips exceed the
2856 * bitflips_threshold value.
2857 *
2858 * Returns a positive number of bitflips less than or equal to
2859 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2860 * threshold.
2861 */
2862static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2863{
2864 const unsigned char *bitmap = buf;
2865 int bitflips = 0;
2866 int weight;
2867
2868 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2869 len--, bitmap++) {
2870 weight = hweight8(*bitmap);
2871 bitflips += BITS_PER_BYTE - weight;
2872 if (unlikely(bitflips > bitflips_threshold))
2873 return -EBADMSG;
2874 }
2875
2876 for (; len >= sizeof(long);
2877 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002878 unsigned long d = *((unsigned long *)bitmap);
2879 if (d == ~0UL)
2880 continue;
2881 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002882 bitflips += BITS_PER_LONG - weight;
2883 if (unlikely(bitflips > bitflips_threshold))
2884 return -EBADMSG;
2885 }
2886
2887 for (; len > 0; len--, bitmap++) {
2888 weight = hweight8(*bitmap);
2889 bitflips += BITS_PER_BYTE - weight;
2890 if (unlikely(bitflips > bitflips_threshold))
2891 return -EBADMSG;
2892 }
2893
2894 return bitflips;
2895}
2896
2897/**
2898 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2899 * 0xff data
2900 * @data: data buffer to test
2901 * @datalen: data length
2902 * @ecc: ECC buffer
2903 * @ecclen: ECC length
2904 * @extraoob: extra OOB buffer
2905 * @extraooblen: extra OOB length
2906 * @bitflips_threshold: maximum number of bitflips
2907 *
2908 * Check if a data buffer and its associated ECC and OOB data contains only
2909 * 0xff pattern, which means the underlying region has been erased and is
2910 * ready to be programmed.
2911 * The bitflips_threshold specify the maximum number of bitflips before
2912 * considering the region as not erased.
2913 *
2914 * Note:
2915 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2916 * different from the NAND page size. When fixing bitflips, ECC engines will
2917 * report the number of errors per chunk, and the NAND core infrastructure
2918 * expect you to return the maximum number of bitflips for the whole page.
2919 * This is why you should always use this function on a single chunk and
2920 * not on the whole page. After checking each chunk you should update your
2921 * max_bitflips value accordingly.
2922 * 2/ When checking for bitflips in erased pages you should not only check
2923 * the payload data but also their associated ECC data, because a user might
2924 * have programmed almost all bits to 1 but a few. In this case, we
2925 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2926 * this case.
2927 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2928 * data are protected by the ECC engine.
2929 * It could also be used if you support subpages and want to attach some
2930 * extra OOB data to an ECC chunk.
2931 *
2932 * Returns a positive number of bitflips less than or equal to
2933 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2934 * threshold. In case of success, the passed buffers are filled with 0xff.
2935 */
2936int nand_check_erased_ecc_chunk(void *data, int datalen,
2937 void *ecc, int ecclen,
2938 void *extraoob, int extraooblen,
2939 int bitflips_threshold)
2940{
2941 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2942
2943 data_bitflips = nand_check_erased_buf(data, datalen,
2944 bitflips_threshold);
2945 if (data_bitflips < 0)
2946 return data_bitflips;
2947
2948 bitflips_threshold -= data_bitflips;
2949
2950 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2951 if (ecc_bitflips < 0)
2952 return ecc_bitflips;
2953
2954 bitflips_threshold -= ecc_bitflips;
2955
2956 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2957 bitflips_threshold);
2958 if (extraoob_bitflips < 0)
2959 return extraoob_bitflips;
2960
2961 if (data_bitflips)
2962 memset(data, 0xff, datalen);
2963
2964 if (ecc_bitflips)
2965 memset(ecc, 0xff, ecclen);
2966
2967 if (extraoob_bitflips)
2968 memset(extraoob, 0xff, extraooblen);
2969
2970 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2971}
2972EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2973
2974/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002975 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002976 * @chip: nand chip info structure
2977 * @buf: buffer to store read data
2978 * @oob_required: caller requires OOB data read to chip->oob_poi
2979 * @page: page number to read
2980 *
2981 * Returns -ENOTSUPP unconditionally.
2982 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002983int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2984 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002985{
2986 return -ENOTSUPP;
2987}
2988EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2989
2990/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002991 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002992 * @chip: nand chip info structure
2993 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002994 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002996 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002997 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002998 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002999int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
3000 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003001{
Boris Brezillonb9761682018-09-06 14:05:20 +02003002 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003003 int ret;
3004
Boris Brezillon25f815f2017-11-30 18:01:30 +01003005 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003006 if (ret)
3007 return ret;
3008
3009 if (oob_required) {
3010 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3011 false);
3012 if (ret)
3013 return ret;
3014 }
3015
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003016 return 0;
3017}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003018EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003019
3020/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003021 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003022 * @chip: nand chip info structure
3023 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003024 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003025 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003026 *
3027 * We need a special oob layout and handling even when OOB isn't used.
3028 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003029static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003030 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003031{
Boris Brezillonb9761682018-09-06 14:05:20 +02003032 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003033 int eccsize = chip->ecc.size;
3034 int eccbytes = chip->ecc.bytes;
3035 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003036 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003037
Boris Brezillon25f815f2017-11-30 18:01:30 +01003038 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3039 if (ret)
3040 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003041
3042 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003043 ret = nand_read_data_op(chip, buf, eccsize, false);
3044 if (ret)
3045 return ret;
3046
David Brownell52ff49d2009-03-04 12:01:36 -08003047 buf += eccsize;
3048
3049 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003050 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3051 false);
3052 if (ret)
3053 return ret;
3054
David Brownell52ff49d2009-03-04 12:01:36 -08003055 oob += chip->ecc.prepad;
3056 }
3057
Boris Brezillon97d90da2017-11-30 18:01:29 +01003058 ret = nand_read_data_op(chip, oob, eccbytes, false);
3059 if (ret)
3060 return ret;
3061
David Brownell52ff49d2009-03-04 12:01:36 -08003062 oob += eccbytes;
3063
3064 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003065 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3066 false);
3067 if (ret)
3068 return ret;
3069
David Brownell52ff49d2009-03-04 12:01:36 -08003070 oob += chip->ecc.postpad;
3071 }
3072 }
3073
3074 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003075 if (size) {
3076 ret = nand_read_data_op(chip, oob, size, false);
3077 if (ret)
3078 return ret;
3079 }
David Brownell52ff49d2009-03-04 12:01:36 -08003080
3081 return 0;
3082}
3083
3084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003085 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003086 * @chip: nand chip info structure
3087 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003088 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003089 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003090 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003091static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3092 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Boris Brezillonb9761682018-09-06 14:05:20 +02003094 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003095 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003096 int eccbytes = chip->ecc.bytes;
3097 int eccsteps = chip->ecc.steps;
3098 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003099 uint8_t *ecc_calc = chip->ecc.calc_buf;
3100 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003101 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003102
Boris Brezillonb9761682018-09-06 14:05:20 +02003103 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003104
3105 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003106 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003107
Boris Brezillon846031d2016-02-03 20:11:00 +01003108 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3109 chip->ecc.total);
3110 if (ret)
3111 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003112
3113 eccsteps = chip->ecc.steps;
3114 p = buf;
3115
3116 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3117 int stat;
3118
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003119 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003120 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003121 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003122 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003123 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003124 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3125 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003126 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003127 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303131 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003132 * @chip: nand chip info structure
3133 * @data_offs: offset of requested data within the page
3134 * @readlen: data length
3135 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003136 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003137 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003138static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3139 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003140{
Boris Brezillonb9761682018-09-06 14:05:20 +02003141 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003142 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003143 uint8_t *p;
3144 int data_col_addr, i, gaps = 0;
3145 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3146 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003147 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003148 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003149 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003150
Brian Norris7854d3f2011-06-23 14:12:08 -07003151 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003152 start_step = data_offs / chip->ecc.size;
3153 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3154 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303155 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003156
Brian Norris8b6e50c2011-05-25 14:59:01 -07003157 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003158 datafrag_len = num_steps * chip->ecc.size;
3159 eccfrag_len = num_steps * chip->ecc.bytes;
3160
3161 data_col_addr = start_step * chip->ecc.size;
3162 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003163 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003164 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003165 if (ret)
3166 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003167
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003169 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003170 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003171
Brian Norris8b6e50c2011-05-25 14:59:01 -07003172 /*
3173 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003174 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003175 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003176 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3177 if (ret)
3178 return ret;
3179
3180 if (oobregion.length < eccfrag_len)
3181 gaps = 1;
3182
Alexey Korolev3d459552008-05-15 17:23:18 +01003183 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003184 ret = nand_change_read_column_op(chip, mtd->writesize,
3185 chip->oob_poi, mtd->oobsize,
3186 false);
3187 if (ret)
3188 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003189 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003190 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003191 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003192 * about buswidth alignment in read_buf.
3193 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003194 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003195 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003196 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003197 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003198 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3199 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003200 aligned_len++;
3201
Boris Brezillon97d90da2017-11-30 18:01:29 +01003202 ret = nand_change_read_column_op(chip,
3203 mtd->writesize + aligned_pos,
3204 &chip->oob_poi[aligned_pos],
3205 aligned_len, false);
3206 if (ret)
3207 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003208 }
3209
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003210 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003211 chip->oob_poi, index, eccfrag_len);
3212 if (ret)
3213 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003214
3215 p = bufpoi + data_col_addr;
3216 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3217 int stat;
3218
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003219 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003220 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003221 if (stat == -EBADMSG &&
3222 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3223 /* check for empty pages with bitflips */
3224 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003225 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003226 chip->ecc.bytes,
3227 NULL, 0,
3228 chip->ecc.strength);
3229 }
3230
Mike Dunn3f91e942012-04-25 12:06:09 -07003231 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003232 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003233 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003234 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003235 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3236 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003237 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003238 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003239}
3240
3241/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003242 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003243 * @chip: nand chip info structure
3244 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003245 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003246 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003247 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003248 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003249 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003250static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3251 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003252{
Boris Brezillonb9761682018-09-06 14:05:20 +02003253 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003254 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003255 int eccbytes = chip->ecc.bytes;
3256 int eccsteps = chip->ecc.steps;
3257 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003258 uint8_t *ecc_calc = chip->ecc.calc_buf;
3259 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003260 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003261
Boris Brezillon25f815f2017-11-30 18:01:30 +01003262 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3263 if (ret)
3264 return ret;
3265
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003266 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003267 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003268
3269 ret = nand_read_data_op(chip, p, eccsize, false);
3270 if (ret)
3271 return ret;
3272
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003273 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003274 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003275
3276 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3277 if (ret)
3278 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003279
Boris Brezillon846031d2016-02-03 20:11:00 +01003280 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3281 chip->ecc.total);
3282 if (ret)
3283 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003284
3285 eccsteps = chip->ecc.steps;
3286 p = buf;
3287
3288 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3289 int stat;
3290
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003291 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003292 if (stat == -EBADMSG &&
3293 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3294 /* check for empty pages with bitflips */
3295 stat = nand_check_erased_ecc_chunk(p, eccsize,
3296 &ecc_code[i], eccbytes,
3297 NULL, 0,
3298 chip->ecc.strength);
3299 }
3300
Mike Dunn3f91e942012-04-25 12:06:09 -07003301 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003302 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003303 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003304 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003305 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3306 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003307 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003308 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003309}
3310
3311/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003312 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003313 * @chip: nand chip info structure
3314 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003315 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003316 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003317 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003318 * Hardware ECC for large page chips, require OOB to be read first. For this
3319 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3320 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3321 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3322 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003323 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003324static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3325 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003326{
Boris Brezillonb9761682018-09-06 14:05:20 +02003327 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003328 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003329 int eccbytes = chip->ecc.bytes;
3330 int eccsteps = chip->ecc.steps;
3331 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003332 uint8_t *ecc_code = chip->ecc.code_buf;
3333 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003334 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003335
3336 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003337 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3338 if (ret)
3339 return ret;
3340
3341 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3342 if (ret)
3343 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003344
Boris Brezillon846031d2016-02-03 20:11:00 +01003345 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3346 chip->ecc.total);
3347 if (ret)
3348 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003349
3350 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3351 int stat;
3352
Boris Brezillonec476362018-09-06 14:05:17 +02003353 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003354
3355 ret = nand_read_data_op(chip, p, eccsize, false);
3356 if (ret)
3357 return ret;
3358
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003359 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003360
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003361 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003362 if (stat == -EBADMSG &&
3363 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3364 /* check for empty pages with bitflips */
3365 stat = nand_check_erased_ecc_chunk(p, eccsize,
3366 &ecc_code[i], eccbytes,
3367 NULL, 0,
3368 chip->ecc.strength);
3369 }
3370
Mike Dunn3f91e942012-04-25 12:06:09 -07003371 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003372 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003373 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003374 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003375 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3376 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003377 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003378 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003379}
3380
3381/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003382 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003383 * @chip: nand chip info structure
3384 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003385 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003386 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003387 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003388 * The hw generator calculates the error syndrome automatically. Therefore we
3389 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003390 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003391static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3392 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003393{
Boris Brezillonb9761682018-09-06 14:05:20 +02003394 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003395 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003396 int eccbytes = chip->ecc.bytes;
3397 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003398 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003399 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003400 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003401 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003402
Boris Brezillon25f815f2017-11-30 18:01:30 +01003403 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3404 if (ret)
3405 return ret;
3406
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003407 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3408 int stat;
3409
Boris Brezillonec476362018-09-06 14:05:17 +02003410 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003411
3412 ret = nand_read_data_op(chip, p, eccsize, false);
3413 if (ret)
3414 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003415
3416 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003417 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3418 false);
3419 if (ret)
3420 return ret;
3421
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003422 oob += chip->ecc.prepad;
3423 }
3424
Boris Brezillonec476362018-09-06 14:05:17 +02003425 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003426
3427 ret = nand_read_data_op(chip, oob, eccbytes, false);
3428 if (ret)
3429 return ret;
3430
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003431 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003432
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003433 oob += eccbytes;
3434
3435 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003436 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3437 false);
3438 if (ret)
3439 return ret;
3440
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003441 oob += chip->ecc.postpad;
3442 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003443
3444 if (stat == -EBADMSG &&
3445 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3446 /* check for empty pages with bitflips */
3447 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3448 oob - eccpadbytes,
3449 eccpadbytes,
3450 NULL, 0,
3451 chip->ecc.strength);
3452 }
3453
3454 if (stat < 0) {
3455 mtd->ecc_stats.failed++;
3456 } else {
3457 mtd->ecc_stats.corrected += stat;
3458 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3459 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003460 }
3461
3462 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003463 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003464 if (i) {
3465 ret = nand_read_data_op(chip, oob, i, false);
3466 if (ret)
3467 return ret;
3468 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003469
Mike Dunn3f91e942012-04-25 12:06:09 -07003470 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003471}
3472
3473/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003474 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003475 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003476 * @oob: oob destination address
3477 * @ops: oob ops structure
3478 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003479 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003480static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003481 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003482{
Boris Brezillon846031d2016-02-03 20:11:00 +01003483 struct nand_chip *chip = mtd_to_nand(mtd);
3484 int ret;
3485
Florian Fainellif8ac0412010-09-07 13:23:43 +02003486 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003487
Brian Norris0612b9d2011-08-30 18:45:40 -07003488 case MTD_OPS_PLACE_OOB:
3489 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003490 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3491 return oob + len;
3492
Boris Brezillon846031d2016-02-03 20:11:00 +01003493 case MTD_OPS_AUTO_OOB:
3494 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3495 ops->ooboffs, len);
3496 BUG_ON(ret);
3497 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003498
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003499 default:
3500 BUG();
3501 }
3502 return NULL;
3503}
3504
3505/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003506 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003507 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003508 * @retry_mode: the retry mode to use
3509 *
3510 * Some vendors supply a special command to shift the Vt threshold, to be used
3511 * when there are too many bitflips in a page (i.e., ECC error). After setting
3512 * a new threshold, the host should retry reading the page.
3513 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003514static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003515{
Brian Norrisba84fb52014-01-03 15:13:33 -08003516 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3517
3518 if (retry_mode >= chip->read_retries)
3519 return -EINVAL;
3520
3521 if (!chip->setup_read_retry)
3522 return -EOPNOTSUPP;
3523
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003524 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003525}
3526
Boris Brezillon85e08e52018-07-27 09:44:17 +02003527static void nand_wait_readrdy(struct nand_chip *chip)
3528{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003529 const struct nand_sdr_timings *sdr;
3530
Boris Brezillon85e08e52018-07-27 09:44:17 +02003531 if (!(chip->options & NAND_NEED_READRDY))
3532 return;
3533
Boris Brezillon52f05b62018-07-27 09:44:18 +02003534 sdr = nand_get_sdr_timings(&chip->data_interface);
3535 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003536}
3537
Brian Norrisba84fb52014-01-03 15:13:33 -08003538/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003539 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003540 * @mtd: MTD device structure
3541 * @from: offset to read from
3542 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003543 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003544 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003545 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003546static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3547 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003548{
Brian Norrise47f3db2012-05-02 10:14:56 -07003549 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003550 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003551 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003552 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003553 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003554 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003555
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003556 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003557 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003558 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003559 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003560 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003562 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003563 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003565 realpage = (int)(from >> chip->page_shift);
3566 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003568 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003570 buf = ops->datbuf;
3571 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003572 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003573
Florian Fainellif8ac0412010-09-07 13:23:43 +02003574 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003575 unsigned int ecc_failures = mtd->ecc_stats.failed;
3576
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003577 bytes = min(mtd->writesize - col, readlen);
3578 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003579
Kamal Dasu66507c72014-05-01 20:51:19 -04003580 if (!aligned)
3581 use_bufpoi = 1;
3582 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003583 use_bufpoi = !virt_addr_valid(buf) ||
3584 !IS_ALIGNED((unsigned long)buf,
3585 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003586 else
3587 use_bufpoi = 0;
3588
Brian Norris8b6e50c2011-05-25 14:59:01 -07003589 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003590 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003591 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003592
3593 if (use_bufpoi && aligned)
3594 pr_debug("%s: using read bounce buffer for buf@%p\n",
3595 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Brian Norrisba84fb52014-01-03 15:13:33 -08003597read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003598 /*
3599 * Now read the page into the buffer. Absent an error,
3600 * the read methods return max bitflips per ecc step.
3601 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003602 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003603 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003604 oob_required,
3605 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003606 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3607 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003608 ret = chip->ecc.read_subpage(chip, col, bytes,
3609 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003610 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003611 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003612 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003613 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003614 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003615 /* Invalidate page cache */
3616 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003617 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003618 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003619
3620 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003621 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003622 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003623 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003624 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003625 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003626 chip->pagebuf_bitflips = ret;
3627 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003628 /* Invalidate page cache */
3629 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003630 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003631 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003633
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003634 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003635 int toread = min(oobreadlen, max_oobsize);
3636
3637 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003638 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003639 oob, ops, toread);
3640 oobreadlen -= toread;
3641 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003642 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003643
Boris Brezillon85e08e52018-07-27 09:44:17 +02003644 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003645
Brian Norrisba84fb52014-01-03 15:13:33 -08003646 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003647 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003648 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003649 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003650 retry_mode);
3651 if (ret < 0)
3652 break;
3653
3654 /* Reset failures; retry */
3655 mtd->ecc_stats.failed = ecc_failures;
3656 goto read_retry;
3657 } else {
3658 /* No more retry modes; real failure */
3659 ecc_fail = true;
3660 }
3661 }
3662
3663 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003664 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003665 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003666 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003667 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003668 max_bitflips = max_t(unsigned int, max_bitflips,
3669 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003672 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003673
Brian Norrisba84fb52014-01-03 15:13:33 -08003674 /* Reset to retry mode 0 */
3675 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003676 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003677 if (ret < 0)
3678 break;
3679 retry_mode = 0;
3680 }
3681
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003682 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Brian Norris8b6e50c2011-05-25 14:59:01 -07003685 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 col = 0;
3687 /* Increment page address */
3688 realpage++;
3689
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003690 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 /* Check, if we cross a chip boundary */
3692 if (!page) {
3693 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003694 chip->select_chip(chip, -1);
3695 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003698 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003700 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003701 if (oob)
3702 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Mike Dunn3f91e942012-04-25 12:06:09 -07003704 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003705 return ret;
3706
Brian Norrisb72f3df2013-12-03 11:04:14 -08003707 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003708 return -EBADMSG;
3709
Mike Dunnedbc45402012-04-25 12:06:11 -07003710 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003711}
3712
3713/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003714 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003715 * @chip: nand chip info structure
3716 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003717 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003718int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003719{
Boris Brezillonb9761682018-09-06 14:05:20 +02003720 struct mtd_info *mtd = nand_to_mtd(chip);
3721
Boris Brezillon97d90da2017-11-30 18:01:29 +01003722 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003723}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003724EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003725
3726/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003727 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003728 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003729 * @chip: nand chip info structure
3730 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003731 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003732int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733{
Boris Brezillonb9761682018-09-06 14:05:20 +02003734 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003735 int length = mtd->oobsize;
3736 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3737 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003738 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003739 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003740
Boris Brezillon97d90da2017-11-30 18:01:29 +01003741 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3742 if (ret)
3743 return ret;
3744
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003745 for (i = 0; i < chip->ecc.steps; i++) {
3746 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003747 int ret;
3748
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003749 pos = eccsize + i * (eccsize + chunk);
3750 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003751 ret = nand_change_read_column_op(chip, pos,
3752 NULL, 0,
3753 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003754 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003755 ret = nand_read_page_op(chip, page, pos, NULL,
3756 0);
3757
3758 if (ret)
3759 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003760 } else
3761 sndrnd = 1;
3762 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003763
3764 ret = nand_read_data_op(chip, bufpoi, toread, false);
3765 if (ret)
3766 return ret;
3767
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003768 bufpoi += toread;
3769 length -= toread;
3770 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003771 if (length > 0) {
3772 ret = nand_read_data_op(chip, bufpoi, length, false);
3773 if (ret)
3774 return ret;
3775 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003776
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003777 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003778}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003779EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780
3781/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003782 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003783 * @chip: nand chip info structure
3784 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003785 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003786int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003788 struct mtd_info *mtd = nand_to_mtd(chip);
3789
Boris Brezillon97d90da2017-11-30 18:01:29 +01003790 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3791 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003792}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003793EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003794
3795/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003796 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003797 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003798 * @chip: nand chip info structure
3799 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003800 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003801int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003802{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003803 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003804 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3805 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003806 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003807 const uint8_t *bufpoi = chip->oob_poi;
3808
3809 /*
3810 * data-ecc-data-ecc ... ecc-oob
3811 * or
3812 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3813 */
3814 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3815 pos = steps * (eccsize + chunk);
3816 steps = 0;
3817 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003818 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003819
Boris Brezillon97d90da2017-11-30 18:01:29 +01003820 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3821 if (ret)
3822 return ret;
3823
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003824 for (i = 0; i < steps; i++) {
3825 if (sndcmd) {
3826 if (mtd->writesize <= 512) {
3827 uint32_t fill = 0xFFFFFFFF;
3828
3829 len = eccsize;
3830 while (len > 0) {
3831 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003832
3833 ret = nand_write_data_op(chip, &fill,
3834 num, false);
3835 if (ret)
3836 return ret;
3837
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003838 len -= num;
3839 }
3840 } else {
3841 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003842 ret = nand_change_write_column_op(chip, pos,
3843 NULL, 0,
3844 false);
3845 if (ret)
3846 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003847 }
3848 } else
3849 sndcmd = 1;
3850 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003851
3852 ret = nand_write_data_op(chip, bufpoi, len, false);
3853 if (ret)
3854 return ret;
3855
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003856 bufpoi += len;
3857 length -= len;
3858 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003859 if (length > 0) {
3860 ret = nand_write_data_op(chip, bufpoi, length, false);
3861 if (ret)
3862 return ret;
3863 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003864
Boris Brezillon97d90da2017-11-30 18:01:29 +01003865 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003866}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003867EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003868
3869/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003870 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003871 * @mtd: MTD device structure
3872 * @from: offset to read from
3873 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003875 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003877static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3878 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003880 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003881 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003882 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003883 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003884 int readlen = ops->ooblen;
3885 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003886 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003887 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Brian Norris289c0522011-07-19 10:06:09 -07003889 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303890 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Brian Norris041e4572011-06-23 16:45:24 -07003892 stats = mtd->ecc_stats;
3893
Boris BREZILLON29f10582016-03-07 10:46:52 +01003894 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003895
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003896 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003897 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003899 /* Shift to get page */
3900 realpage = (int)(from >> chip->page_shift);
3901 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Florian Fainellif8ac0412010-09-07 13:23:43 +02003903 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003904 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003905 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003906 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003907 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003908
3909 if (ret < 0)
3910 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003911
3912 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003913 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003914
Boris Brezillon85e08e52018-07-27 09:44:17 +02003915 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003916
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003917 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3918
Vitaly Wool70145682006-11-03 18:20:38 +03003919 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003920 if (!readlen)
3921 break;
3922
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003923 /* Increment page address */
3924 realpage++;
3925
3926 page = realpage & chip->pagemask;
3927 /* Check, if we cross a chip boundary */
3928 if (!page) {
3929 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003930 chip->select_chip(chip, -1);
3931 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003934 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003936 ops->oobretlen = ops->ooblen - readlen;
3937
3938 if (ret < 0)
3939 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003940
3941 if (mtd->ecc_stats.failed - stats.failed)
3942 return -EBADMSG;
3943
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003944 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
3946
3947/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003948 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003949 * @mtd: MTD device structure
3950 * @from: offset to read from
3951 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003953 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003955static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3956 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003958 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003959
3960 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003962 if (ops->mode != MTD_OPS_PLACE_OOB &&
3963 ops->mode != MTD_OPS_AUTO_OOB &&
3964 ops->mode != MTD_OPS_RAW)
3965 return -ENOTSUPP;
3966
Huang Shijie6a8214a2012-11-19 14:43:30 +08003967 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003969 if (!ops->datbuf)
3970 ret = nand_do_read_oob(mtd, from, ops);
3971 else
3972 ret = nand_do_read_ops(mtd, from, ops);
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003975 return ret;
3976}
3977
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003978/**
3979 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003980 * @chip: nand chip info structure
3981 * @buf: data buffer
3982 * @oob_required: must write chip->oob_poi to OOB
3983 * @page: page number to write
3984 *
3985 * Returns -ENOTSUPP unconditionally.
3986 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003987int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3988 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003989{
3990 return -ENOTSUPP;
3991}
3992EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003993
3994/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003995 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003996 * @chip: nand chip info structure
3997 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003998 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003999 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004000 *
Brian Norris7854d3f2011-06-23 14:12:08 -07004001 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004002 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004003int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
4004 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004005{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004006 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004007 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004008
Boris Brezillon25f815f2017-11-30 18:01:30 +01004009 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004010 if (ret)
4011 return ret;
4012
4013 if (oob_required) {
4014 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4015 false);
4016 if (ret)
4017 return ret;
4018 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004019
Boris Brezillon25f815f2017-11-30 18:01:30 +01004020 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004022EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004024/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004025 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004026 * @chip: nand chip info structure
4027 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004028 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004029 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004030 *
4031 * We need a special oob layout and handling even when ECC isn't checked.
4032 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004033static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004034 const uint8_t *buf, int oob_required,
4035 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004036{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004037 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004038 int eccsize = chip->ecc.size;
4039 int eccbytes = chip->ecc.bytes;
4040 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004041 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004042
Boris Brezillon25f815f2017-11-30 18:01:30 +01004043 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4044 if (ret)
4045 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004046
4047 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004048 ret = nand_write_data_op(chip, buf, eccsize, false);
4049 if (ret)
4050 return ret;
4051
David Brownell52ff49d2009-03-04 12:01:36 -08004052 buf += eccsize;
4053
4054 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004055 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4056 false);
4057 if (ret)
4058 return ret;
4059
David Brownell52ff49d2009-03-04 12:01:36 -08004060 oob += chip->ecc.prepad;
4061 }
4062
Boris Brezillon97d90da2017-11-30 18:01:29 +01004063 ret = nand_write_data_op(chip, oob, eccbytes, false);
4064 if (ret)
4065 return ret;
4066
David Brownell52ff49d2009-03-04 12:01:36 -08004067 oob += eccbytes;
4068
4069 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004070 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4071 false);
4072 if (ret)
4073 return ret;
4074
David Brownell52ff49d2009-03-04 12:01:36 -08004075 oob += chip->ecc.postpad;
4076 }
4077 }
4078
4079 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004080 if (size) {
4081 ret = nand_write_data_op(chip, oob, size, false);
4082 if (ret)
4083 return ret;
4084 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004085
Boris Brezillon25f815f2017-11-30 18:01:30 +01004086 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004087}
4088/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004089 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004090 * @chip: nand chip info structure
4091 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004092 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004093 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004094 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004095static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4096 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004097{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004098 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004099 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004100 int eccbytes = chip->ecc.bytes;
4101 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004102 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004103 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004104
Brian Norris7854d3f2011-06-23 14:12:08 -07004105 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004106 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004107 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004108
Boris Brezillon846031d2016-02-03 20:11:00 +01004109 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4110 chip->ecc.total);
4111 if (ret)
4112 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004113
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004114 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004115}
4116
4117/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004118 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004119 * @chip: nand chip info structure
4120 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004121 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004122 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004123 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004124static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4125 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004126{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004127 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004128 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004129 int eccbytes = chip->ecc.bytes;
4130 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004131 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004132 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004133
Boris Brezillon25f815f2017-11-30 18:01:30 +01004134 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4135 if (ret)
4136 return ret;
4137
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004138 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004139 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004140
4141 ret = nand_write_data_op(chip, p, eccsize, false);
4142 if (ret)
4143 return ret;
4144
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004145 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004146 }
4147
Boris Brezillon846031d2016-02-03 20:11:00 +01004148 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4149 chip->ecc.total);
4150 if (ret)
4151 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004152
Boris Brezillon97d90da2017-11-30 18:01:29 +01004153 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4154 if (ret)
4155 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004156
Boris Brezillon25f815f2017-11-30 18:01:30 +01004157 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004158}
4159
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304160
4161/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004162 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304163 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004164 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304165 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004166 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304167 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004168 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304169 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004170static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4171 uint32_t data_len, const uint8_t *buf,
4172 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304173{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004174 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304175 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004176 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304177 int ecc_size = chip->ecc.size;
4178 int ecc_bytes = chip->ecc.bytes;
4179 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304180 uint32_t start_step = offset / ecc_size;
4181 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4182 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004183 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304184
Boris Brezillon25f815f2017-11-30 18:01:30 +01004185 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4186 if (ret)
4187 return ret;
4188
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304189 for (step = 0; step < ecc_steps; step++) {
4190 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004191 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304192
4193 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004194 ret = nand_write_data_op(chip, buf, ecc_size, false);
4195 if (ret)
4196 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304197
4198 /* mask ECC of un-touched subpages by padding 0xFF */
4199 if ((step < start_step) || (step > end_step))
4200 memset(ecc_calc, 0xff, ecc_bytes);
4201 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004202 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304203
4204 /* mask OOB of un-touched subpages by padding 0xFF */
4205 /* if oob_required, preserve OOB metadata of written subpage */
4206 if (!oob_required || (step < start_step) || (step > end_step))
4207 memset(oob_buf, 0xff, oob_bytes);
4208
Brian Norrisd6a950802013-08-08 17:16:36 -07004209 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304210 ecc_calc += ecc_bytes;
4211 oob_buf += oob_bytes;
4212 }
4213
4214 /* copy calculated ECC for whole page to chip->buffer->oob */
4215 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004216 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004217 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4218 chip->ecc.total);
4219 if (ret)
4220 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304221
4222 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004223 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4224 if (ret)
4225 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304226
Boris Brezillon25f815f2017-11-30 18:01:30 +01004227 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304228}
4229
4230
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004231/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004232 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004233 * @chip: nand chip info structure
4234 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004235 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004236 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004237 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004238 * The hw generator calculates the error syndrome automatically. Therefore we
4239 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004240 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004241static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4242 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004243{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004244 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004245 int i, eccsize = chip->ecc.size;
4246 int eccbytes = chip->ecc.bytes;
4247 int eccsteps = chip->ecc.steps;
4248 const uint8_t *p = buf;
4249 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004250 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004251
Boris Brezillon25f815f2017-11-30 18:01:30 +01004252 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4253 if (ret)
4254 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004255
4256 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004257 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004258
4259 ret = nand_write_data_op(chip, p, eccsize, false);
4260 if (ret)
4261 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004262
4263 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004264 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4265 false);
4266 if (ret)
4267 return ret;
4268
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004269 oob += chip->ecc.prepad;
4270 }
4271
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004272 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004273
4274 ret = nand_write_data_op(chip, oob, eccbytes, false);
4275 if (ret)
4276 return ret;
4277
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004278 oob += eccbytes;
4279
4280 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004281 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4282 false);
4283 if (ret)
4284 return ret;
4285
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004286 oob += chip->ecc.postpad;
4287 }
4288 }
4289
4290 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004291 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004292 if (i) {
4293 ret = nand_write_data_op(chip, oob, i, false);
4294 if (ret)
4295 return ret;
4296 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004297
Boris Brezillon25f815f2017-11-30 18:01:30 +01004298 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004299}
4300
4301/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004302 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004303 * @mtd: MTD device structure
4304 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304305 * @offset: address offset within the page
4306 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004307 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004308 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004309 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004310 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004311 */
4312static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304313 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004314 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004315{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304316 int status, subpage;
4317
4318 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4319 chip->ecc.write_subpage)
4320 subpage = offset || (data_len < mtd->writesize);
4321 else
4322 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004323
David Woodhouse956e9442006-09-25 17:12:39 +01004324 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004325 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4326 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304327 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004328 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4329 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004330 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004331 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004332
4333 if (status < 0)
4334 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004335
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004336 return 0;
4337}
4338
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004339/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004340 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004341 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004342 * @oob: oob data buffer
4343 * @len: oob data write length
4344 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004345 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004346static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4347 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004348{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004349 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004350 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004351
4352 /*
4353 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4354 * data from a previous OOB read.
4355 */
4356 memset(chip->oob_poi, 0xff, mtd->oobsize);
4357
Florian Fainellif8ac0412010-09-07 13:23:43 +02004358 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004359
Brian Norris0612b9d2011-08-30 18:45:40 -07004360 case MTD_OPS_PLACE_OOB:
4361 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004362 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4363 return oob + len;
4364
Boris Brezillon846031d2016-02-03 20:11:00 +01004365 case MTD_OPS_AUTO_OOB:
4366 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4367 ops->ooboffs, len);
4368 BUG_ON(ret);
4369 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004370
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004371 default:
4372 BUG();
4373 }
4374 return NULL;
4375}
4376
Florian Fainellif8ac0412010-09-07 13:23:43 +02004377#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004378
4379/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004380 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004381 * @mtd: MTD device structure
4382 * @to: offset to write to
4383 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004384 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004385 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004386 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004387static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4388 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004389{
Corentin Labbe73600b62017-09-02 10:49:38 +02004390 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004391 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004392 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004393
4394 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004395 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004396
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004397 uint8_t *oob = ops->oobbuf;
4398 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304399 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004400 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004401
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004402 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004403 if (!writelen)
4404 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004405
Brian Norris8b6e50c2011-05-25 14:59:01 -07004406 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004407 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004408 pr_notice("%s: attempt to write non page aligned data\n",
4409 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004410 return -EINVAL;
4411 }
4412
Thomas Gleixner29072b92006-09-28 15:38:36 +02004413 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004414
Thomas Gleixner6a930962006-06-28 00:11:45 +02004415 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004416 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004417
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004418 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004419 if (nand_check_wp(mtd)) {
4420 ret = -EIO;
4421 goto err_out;
4422 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004423
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004424 realpage = (int)(to >> chip->page_shift);
4425 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004426
4427 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004428 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4429 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004430 chip->pagebuf = -1;
4431
Maxim Levitsky782ce792010-02-22 20:39:36 +02004432 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004433 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4434 ret = -EINVAL;
4435 goto err_out;
4436 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004437
Florian Fainellif8ac0412010-09-07 13:23:43 +02004438 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004439 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004440 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004441 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004442 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004443
Kamal Dasu66507c72014-05-01 20:51:19 -04004444 if (part_pagewr)
4445 use_bufpoi = 1;
4446 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004447 use_bufpoi = !virt_addr_valid(buf) ||
4448 !IS_ALIGNED((unsigned long)buf,
4449 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004450 else
4451 use_bufpoi = 0;
4452
4453 /* Partial page write?, or need to use bounce buffer */
4454 if (use_bufpoi) {
4455 pr_debug("%s: using write bounce buffer for buf@%p\n",
4456 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004457 if (part_pagewr)
4458 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004459 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004460 memset(chip->data_buf, 0xff, mtd->writesize);
4461 memcpy(&chip->data_buf[column], buf, bytes);
4462 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004463 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004464
Maxim Levitsky782ce792010-02-22 20:39:36 +02004465 if (unlikely(oob)) {
4466 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004467 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004468 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004469 } else {
4470 /* We still need to erase leftover OOB data */
4471 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004472 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004473
4474 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004475 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004476 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004477 if (ret)
4478 break;
4479
4480 writelen -= bytes;
4481 if (!writelen)
4482 break;
4483
Thomas Gleixner29072b92006-09-28 15:38:36 +02004484 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004485 buf += bytes;
4486 realpage++;
4487
4488 page = realpage & chip->pagemask;
4489 /* Check, if we cross a chip boundary */
4490 if (!page) {
4491 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004492 chip->select_chip(chip, -1);
4493 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004494 }
4495 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004496
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004497 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004498 if (unlikely(oob))
4499 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004500
4501err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004502 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004503 return ret;
4504}
4505
4506/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004507 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004508 * @mtd: MTD device structure
4509 * @to: offset to write to
4510 * @len: number of bytes to write
4511 * @retlen: pointer to variable to store the number of written bytes
4512 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004513 *
4514 * NAND write with ECC. Used when performing writes in interrupt context, this
4515 * may for example be called by mtdoops when writing an oops while in panic.
4516 */
4517static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4518 size_t *retlen, const uint8_t *buf)
4519{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004520 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004521 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004522 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004523 int ret;
4524
Brian Norris8b6e50c2011-05-25 14:59:01 -07004525 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004526 panic_nand_get_device(chip, mtd, FL_WRITING);
4527
Boris Brezillon758b56f2018-09-06 14:05:24 +02004528 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004529
4530 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004531 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004532
Brian Norris0ec56dc2015-02-28 02:02:30 -08004533 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004534 ops.len = len;
4535 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004536 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004537
Brian Norris4a89ff82011-08-30 18:45:45 -07004538 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004539
Brian Norris4a89ff82011-08-30 18:45:45 -07004540 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004541 return ret;
4542}
4543
4544/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004545 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004546 * @mtd: MTD device structure
4547 * @to: offset to write to
4548 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004549 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004550 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004551 */
4552static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4553 struct mtd_oob_ops *ops)
4554{
Adrian Hunter03736152007-01-31 17:58:29 +02004555 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004556 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Brian Norris289c0522011-07-19 10:06:09 -07004558 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304559 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Boris BREZILLON29f10582016-03-07 10:46:52 +01004561 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004564 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004565 pr_debug("%s: attempt to write past end of page\n",
4566 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 return -EINVAL;
4568 }
4569
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004570 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004571
4572 /*
4573 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4574 * of my DiskOnChip 2000 test units) will clear the whole data page too
4575 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4576 * it in the doc2000 driver in August 1999. dwmw2.
4577 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004578 nand_reset(chip, chipnr);
4579
Boris Brezillon758b56f2018-09-06 14:05:24 +02004580 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004581
4582 /* Shift to get page */
4583 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004586 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004587 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004588 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004589 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004592 if (page == chip->pagebuf)
4593 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004595 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004596
Brian Norris0612b9d2011-08-30 18:45:40 -07004597 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004598 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004599 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004600 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004601
Boris Brezillon758b56f2018-09-06 14:05:24 +02004602 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004603
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004604 if (status)
4605 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Vitaly Wool70145682006-11-03 18:20:38 +03004607 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004609 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004610}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004612/**
4613 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004614 * @mtd: MTD device structure
4615 * @to: offset to write to
4616 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004617 */
4618static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4619 struct mtd_oob_ops *ops)
4620{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004621 int ret = -ENOTSUPP;
4622
4623 ops->retlen = 0;
4624
Huang Shijie6a8214a2012-11-19 14:43:30 +08004625 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004626
Florian Fainellif8ac0412010-09-07 13:23:43 +02004627 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004628 case MTD_OPS_PLACE_OOB:
4629 case MTD_OPS_AUTO_OOB:
4630 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004631 break;
4632
4633 default:
4634 goto out;
4635 }
4636
4637 if (!ops->datbuf)
4638 ret = nand_do_write_oob(mtd, to, ops);
4639 else
4640 ret = nand_do_write_ops(mtd, to, ops);
4641
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004642out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004643 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 return ret;
4645}
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647/**
Brian Norris49c50b92014-05-06 16:02:19 -07004648 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004649 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004650 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 *
Brian Norris49c50b92014-05-06 16:02:19 -07004652 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004654static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004656 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004659 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004660
Boris Brezillon97d90da2017-11-30 18:01:29 +01004661 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
4664/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004666 * @mtd: MTD device structure
4667 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004669 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004671static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004673 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004677 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004678 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004679 * @instr: erase instruction
4680 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004682 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004684int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004685 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004687 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004688 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004689 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Brian Norris289c0522011-07-19 10:06:09 -07004691 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4692 __func__, (unsigned long long)instr->addr,
4693 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304695 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004699 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004702 page = (int)(instr->addr >> chip->page_shift);
4703 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
4705 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004706 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
4708 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004709 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* Check, if it is write protected */
4712 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004713 pr_debug("%s: device is write protected!\n",
4714 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004715 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 goto erase_exit;
4717 }
4718
4719 /* Loop through the pages */
4720 len = instr->len;
4721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004723 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004724 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304725 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004726 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4727 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004728 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 goto erase_exit;
4730 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004731
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004732 /*
4733 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004734 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004735 */
4736 if (page <= chip->pagebuf && chip->pagebuf <
4737 (page + pages_per_block))
4738 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004740 if (chip->legacy.erase)
4741 status = chip->legacy.erase(chip,
4742 page & chip->pagemask);
4743 else
4744 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004747 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004748 pr_debug("%s: failed erase, page 0x%08x\n",
4749 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004750 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004751 instr->fail_addr =
4752 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 goto erase_exit;
4754 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004757 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 page += pages_per_block;
4759
4760 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004761 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004763 chip->select_chip(chip, -1);
4764 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
4766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004768 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004769erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004772 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 nand_release_device(mtd);
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 /* Return more or less happy */
4776 return ret;
4777}
4778
4779/**
4780 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004781 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004783 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004785static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
Brian Norris289c0522011-07-19 10:06:09 -07004787 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004790 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004792 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793}
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004796 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004797 * @mtd: MTD device structure
4798 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004800static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304802 struct nand_chip *chip = mtd_to_nand(mtd);
4803 int chipnr = (int)(offs >> chip->chip_shift);
4804 int ret;
4805
4806 /* Select the NAND device */
4807 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004808 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304809
4810 ret = nand_block_checkbad(mtd, offs, 0);
4811
Boris Brezillon758b56f2018-09-06 14:05:24 +02004812 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304813 nand_release_device(mtd);
4814
4815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
4818/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004819 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004820 * @mtd: MTD device structure
4821 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004823static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 int ret;
4826
Florian Fainellif8ac0412010-09-07 13:23:43 +02004827 ret = nand_block_isbad(mtd, ofs);
4828 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004829 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (ret > 0)
4831 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004832 return ret;
4833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Brian Norris5a0edb22013-07-30 17:52:58 -07004835 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
4838/**
Zach Brown56718422017-01-10 13:30:20 -06004839 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4840 * @mtd: MTD device structure
4841 * @ofs: offset relative to mtd start
4842 * @len: length of mtd
4843 */
4844static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4845{
4846 struct nand_chip *chip = mtd_to_nand(mtd);
4847 u32 part_start_block;
4848 u32 part_end_block;
4849 u32 part_start_die;
4850 u32 part_end_die;
4851
4852 /*
4853 * max_bb_per_die and blocks_per_die used to determine
4854 * the maximum bad block count.
4855 */
4856 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4857 return -ENOTSUPP;
4858
4859 /* Get the start and end of the partition in erase blocks. */
4860 part_start_block = mtd_div_by_eb(ofs, mtd);
4861 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4862
4863 /* Get the start and end LUNs of the partition. */
4864 part_start_die = part_start_block / chip->blocks_per_die;
4865 part_end_die = part_end_block / chip->blocks_per_die;
4866
4867 /*
4868 * Look up the bad blocks per unit and multiply by the number of units
4869 * that the partition spans.
4870 */
4871 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4872}
4873
4874/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004875 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004876 * @chip: nand chip info structure
4877 * @addr: feature address.
4878 * @subfeature_param: the subfeature parameters, a four bytes array.
4879 *
4880 * Should be used by NAND controller drivers that do not support the SET/GET
4881 * FEATURES operations.
4882 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004883int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
4884 u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004885{
4886 return -ENOTSUPP;
4887}
Miquel Raynalb9587582018-03-19 14:47:19 +01004888EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004889
4890/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004891 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004892 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004893 */
4894static int nand_suspend(struct mtd_info *mtd)
4895{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004896 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004897}
4898
4899/**
4900 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004901 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004902 */
4903static void nand_resume(struct mtd_info *mtd)
4904{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004905 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004906
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004907 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004908 nand_release_device(mtd);
4909 else
Brian Norrisd0370212011-07-19 10:06:08 -07004910 pr_err("%s called for a chip which is not in suspended state\n",
4911 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004912}
4913
Scott Branden72ea4032014-11-20 11:18:05 -08004914/**
4915 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4916 * prevent further operations
4917 * @mtd: MTD device structure
4918 */
4919static void nand_shutdown(struct mtd_info *mtd)
4920{
Brian Norris9ca641b2015-11-09 16:37:28 -08004921 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004922}
4923
Brian Norris8b6e50c2011-05-25 14:59:01 -07004924/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004925static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004926{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004927 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 /* check for proper chip_delay setup, set 20us if not */
Boris Brezillon3cece3a2018-09-07 00:38:41 +02004930 if (!chip->legacy.chip_delay)
4931 chip->legacy.chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 /* check, if a user supplied command function given */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02004934 if (!chip->legacy.cmdfunc && !chip->exec_op)
4935 chip->legacy.cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937 /* check, if a user supplied wait function given */
Boris Brezillon8395b752018-09-07 00:38:37 +02004938 if (chip->legacy.waitfunc == NULL)
4939 chip->legacy.waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004941 if (!chip->select_chip)
4942 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004943
4944 /* If called twice, pointers that depend on busw may need to be reset */
Boris Brezillon716bbba2018-09-07 00:38:35 +02004945 if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
4946 chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
Boris Brezillon716bbba2018-09-07 00:38:35 +02004947 if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
4948 chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
4949 if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
4950 chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
4951 if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
4952 chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004953
4954 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004955 chip->controller = &chip->dummy_controller;
4956 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004957 }
4958
Masahiro Yamada477544c2017-03-30 17:15:05 +09004959 if (!chip->buf_align)
4960 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004961}
4962
Brian Norris8b6e50c2011-05-25 14:59:01 -07004963/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004964static void sanitize_string(uint8_t *s, size_t len)
4965{
4966 ssize_t i;
4967
Brian Norris8b6e50c2011-05-25 14:59:01 -07004968 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004969 s[len - 1] = 0;
4970
Brian Norris8b6e50c2011-05-25 14:59:01 -07004971 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004972 for (i = 0; i < len - 1; i++) {
4973 if (s[i] < ' ' || s[i] > 127)
4974 s[i] = '?';
4975 }
4976
Brian Norris8b6e50c2011-05-25 14:59:01 -07004977 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004978 strim(s);
4979}
4980
4981static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4982{
4983 int i;
4984 while (len--) {
4985 crc ^= *p++ << 8;
4986 for (i = 0; i < 8; i++)
4987 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4988 }
4989
4990 return crc;
4991}
4992
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004993/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004994static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4995 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004996{
4997 struct onfi_ext_param_page *ep;
4998 struct onfi_ext_section *s;
4999 struct onfi_ext_ecc_info *ecc;
5000 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005001 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005002 int len;
5003 int i;
5004
5005 len = le16_to_cpu(p->ext_param_page_length) * 16;
5006 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005007 if (!ep)
5008 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005009
5010 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005011 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5012 if (ret)
5013 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005014
5015 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005016 ret = nand_change_read_column_op(chip,
5017 sizeof(*p) * p->num_of_param_pages,
5018 ep, len, true);
5019 if (ret)
5020 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005021
Boris Brezillon97d90da2017-11-30 18:01:29 +01005022 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005023 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5024 != le16_to_cpu(ep->crc))) {
5025 pr_debug("fail in the CRC.\n");
5026 goto ext_out;
5027 }
5028
5029 /*
5030 * Check the signature.
5031 * Do not strictly follow the ONFI spec, maybe changed in future.
5032 */
5033 if (strncmp(ep->sig, "EPPS", 4)) {
5034 pr_debug("The signature is invalid.\n");
5035 goto ext_out;
5036 }
5037
5038 /* find the ECC section. */
5039 cursor = (uint8_t *)(ep + 1);
5040 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5041 s = ep->sections + i;
5042 if (s->type == ONFI_SECTION_TYPE_2)
5043 break;
5044 cursor += s->length * 16;
5045 }
5046 if (i == ONFI_EXT_SECTION_MAX) {
5047 pr_debug("We can not find the ECC section.\n");
5048 goto ext_out;
5049 }
5050
5051 /* get the info we want. */
5052 ecc = (struct onfi_ext_ecc_info *)cursor;
5053
Brian Norris4ae7d222013-09-16 18:20:21 -07005054 if (!ecc->codeword_size) {
5055 pr_debug("Invalid codeword size\n");
5056 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005057 }
5058
Brian Norris4ae7d222013-09-16 18:20:21 -07005059 chip->ecc_strength_ds = ecc->ecc_bits;
5060 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005061 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005062
5063ext_out:
5064 kfree(ep);
5065 return ret;
5066}
5067
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005068/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005069 * Recover data with bit-wise majority
5070 */
5071static void nand_bit_wise_majority(const void **srcbufs,
5072 unsigned int nsrcbufs,
5073 void *dstbuf,
5074 unsigned int bufsize)
5075{
5076 int i, j, k;
5077
5078 for (i = 0; i < bufsize; i++) {
5079 u8 val = 0;
5080
5081 for (j = 0; j < 8; j++) {
5082 unsigned int cnt = 0;
5083
5084 for (k = 0; k < nsrcbufs; k++) {
5085 const u8 *srcbuf = srcbufs[k];
5086
5087 if (srcbuf[i] & BIT(j))
5088 cnt++;
5089 }
5090
5091 if (cnt > nsrcbufs / 2)
5092 val |= BIT(j);
5093 }
5094
5095 ((u8 *)dstbuf)[i] = val;
5096 }
5097}
5098
5099/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005100 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005101 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005102static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005103{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005104 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005105 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005106 struct onfi_params *onfi;
5107 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005108 char id[4];
5109 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005110
Brian Norris7854d3f2011-06-23 14:12:08 -07005111 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005112 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5113 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005114 return 0;
5115
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005116 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005117 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005118 if (!p)
5119 return -ENOMEM;
5120
Boris Brezillon97d90da2017-11-30 18:01:29 +01005121 ret = nand_read_param_page_op(chip, 0, NULL, 0);
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
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005127 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005128 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005129 if (ret) {
5130 ret = 0;
5131 goto free_onfi_param_page;
5132 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005133
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005134 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005135 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005136 if (i)
5137 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005138 break;
5139 }
5140 }
5141
Brian Norrisc7f23a72013-08-13 10:51:55 -07005142 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005143 const void *srcbufs[3] = {p, p + 1, p + 2};
5144
5145 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5146 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5147 sizeof(*p));
5148
5149 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5150 le16_to_cpu(p->crc)) {
5151 pr_err("ONFI parameter recovery failed, aborting\n");
5152 goto free_onfi_param_page;
5153 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005154 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005155
Chris Packham00ce4e02018-06-25 10:44:44 +12005156 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5157 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5158 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5159
Brian Norris8b6e50c2011-05-25 14:59:01 -07005160 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005161 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005162 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005163 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005164 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005165 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005166 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005167 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005168 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005169 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005170 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005171 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005172
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005173 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005174 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005175 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005176 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005177
5178 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5179 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005180 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5181 if (!chip->parameters.model) {
5182 ret = -ENOMEM;
5183 goto free_onfi_param_page;
5184 }
Brian Norris4355b702013-08-27 18:45:10 -07005185
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005186 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005187
5188 /*
5189 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5190 * (don't ask me who thought of this...). MTD assumes that these
5191 * dimensions will be power-of-2, so just truncate the remaining area.
5192 */
5193 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5194 mtd->erasesize *= mtd->writesize;
5195
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005196 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005197
5198 /* See erasesize comment */
5199 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005200 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005201 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005202
Zach Brown34da5f52017-01-10 13:30:21 -06005203 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5204 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5205
Miquel Raynala97421c2018-03-19 14:47:27 +01005206 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005207 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005208
Huang Shijie10c86ba2013-05-17 11:17:26 +08005209 if (p->ecc_bits != 0xff) {
5210 chip->ecc_strength_ds = p->ecc_bits;
5211 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005212 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005213 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005214
5215 /*
5216 * The nand_flash_detect_ext_param_page() uses the
5217 * Change Read Column command which maybe not supported
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005218 * by the chip->legacy.cmdfunc. So try to update the
5219 * chip->legacy.cmdfunc now. We do not replace user supplied
5220 * command function.
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005221 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005222 if (mtd->writesize > 512 &&
5223 chip->legacy.cmdfunc == nand_command)
5224 chip->legacy.cmdfunc = nand_command_lp;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005225
5226 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005227 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005228 pr_warn("Failed to detect ONFI extended param page\n");
5229 } else {
5230 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005231 }
5232
Miquel Raynalf4531b22018-03-19 14:47:26 +01005233 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005234 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005235 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005236 bitmap_set(chip->parameters.get_feature_list,
5237 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5238 bitmap_set(chip->parameters.set_feature_list,
5239 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5240 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005241
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005242 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5243 if (!onfi) {
5244 ret = -ENOMEM;
5245 goto free_model;
5246 }
5247
5248 onfi->version = onfi_version;
5249 onfi->tPROG = le16_to_cpu(p->t_prog);
5250 onfi->tBERS = le16_to_cpu(p->t_bers);
5251 onfi->tR = le16_to_cpu(p->t_r);
5252 onfi->tCCS = le16_to_cpu(p->t_ccs);
5253 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5254 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5255 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5256 chip->parameters.onfi = onfi;
5257
5258 /* Identification done, free the full ONFI parameter page and exit */
5259 kfree(p);
5260
5261 return 1;
5262
5263free_model:
5264 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005265free_onfi_param_page:
5266 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005267
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005268 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005269}
5270
5271/*
Huang Shijie91361812014-02-21 13:39:40 +08005272 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5273 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005274static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005275{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005276 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005277 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005278 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005279 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005280 char id[5];
5281 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005282
5283 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005284 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5285 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005286 return 0;
5287
Miquel Raynal480139d2018-03-19 14:47:30 +01005288 /* JEDEC chip: allocate a buffer to hold its parameter page */
5289 p = kzalloc(sizeof(*p), GFP_KERNEL);
5290 if (!p)
5291 return -ENOMEM;
5292
Boris Brezillon97d90da2017-11-30 18:01:29 +01005293 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005294 if (ret) {
5295 ret = 0;
5296 goto free_jedec_param_page;
5297 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005298
Huang Shijie91361812014-02-21 13:39:40 +08005299 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005300 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005301 if (ret) {
5302 ret = 0;
5303 goto free_jedec_param_page;
5304 }
Huang Shijie91361812014-02-21 13:39:40 +08005305
5306 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5307 le16_to_cpu(p->crc))
5308 break;
5309 }
5310
5311 if (i == 3) {
5312 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005313 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005314 }
5315
5316 /* Check version */
5317 val = le16_to_cpu(p->revision);
5318 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005319 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005320 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005321 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005322
Miquel Raynal480139d2018-03-19 14:47:30 +01005323 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005324 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005325 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005326 }
5327
5328 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5329 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005330 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5331 if (!chip->parameters.model) {
5332 ret = -ENOMEM;
5333 goto free_jedec_param_page;
5334 }
Huang Shijie91361812014-02-21 13:39:40 +08005335
5336 mtd->writesize = le32_to_cpu(p->byte_per_page);
5337
5338 /* Please reference to the comment for nand_flash_detect_onfi. */
5339 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5340 mtd->erasesize *= mtd->writesize;
5341
5342 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5343
5344 /* Please reference to the comment for nand_flash_detect_onfi. */
5345 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5346 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5347 chip->bits_per_cell = p->bits_per_cell;
5348
Miquel Raynal480139d2018-03-19 14:47:30 +01005349 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005350 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005351
5352 /* ECC info */
5353 ecc = &p->ecc_info[0];
5354
5355 if (ecc->codeword_size >= 9) {
5356 chip->ecc_strength_ds = ecc->ecc_bits;
5357 chip->ecc_step_ds = 1 << ecc->codeword_size;
5358 } else {
5359 pr_warn("Invalid codeword size\n");
5360 }
5361
Miquel Raynal480139d2018-03-19 14:47:30 +01005362free_jedec_param_page:
5363 kfree(p);
5364 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005365}
5366
5367/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005368 * nand_id_has_period - Check if an ID string has a given wraparound period
5369 * @id_data: the ID string
5370 * @arrlen: the length of the @id_data array
5371 * @period: the period of repitition
5372 *
5373 * Check if an ID string is repeated within a given sequence of bytes at
5374 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005375 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005376 * if the repetition has a period of @period; otherwise, returns zero.
5377 */
5378static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5379{
5380 int i, j;
5381 for (i = 0; i < period; i++)
5382 for (j = i + period; j < arrlen; j += period)
5383 if (id_data[i] != id_data[j])
5384 return 0;
5385 return 1;
5386}
5387
5388/*
5389 * nand_id_len - Get the length of an ID string returned by CMD_READID
5390 * @id_data: the ID string
5391 * @arrlen: the length of the @id_data array
5392
5393 * Returns the length of the ID string, according to known wraparound/trailing
5394 * zero patterns. If no pattern exists, returns the length of the array.
5395 */
5396static int nand_id_len(u8 *id_data, int arrlen)
5397{
5398 int last_nonzero, period;
5399
5400 /* Find last non-zero byte */
5401 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5402 if (id_data[last_nonzero])
5403 break;
5404
5405 /* All zeros */
5406 if (last_nonzero < 0)
5407 return 0;
5408
5409 /* Calculate wraparound period */
5410 for (period = 1; period < arrlen; period++)
5411 if (nand_id_has_period(id_data, arrlen, period))
5412 break;
5413
5414 /* There's a repeated pattern */
5415 if (period < arrlen)
5416 return period;
5417
5418 /* There are trailing zeros */
5419 if (last_nonzero < arrlen - 1)
5420 return last_nonzero + 1;
5421
5422 /* No pattern detected */
5423 return arrlen;
5424}
5425
Huang Shijie7db906b2013-09-25 14:58:11 +08005426/* Extract the bits of per cell from the 3rd byte of the extended ID */
5427static int nand_get_bits_per_cell(u8 cellinfo)
5428{
5429 int bits;
5430
5431 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5432 bits >>= NAND_CI_CELLTYPE_SHIFT;
5433 return bits + 1;
5434}
5435
Brian Norrise3b88bd2012-09-24 20:40:52 -07005436/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005437 * Many new NAND share similar device ID codes, which represent the size of the
5438 * chip. The rest of the parameters must be decoded according to generic or
5439 * manufacturer-specific "extended ID" decoding patterns.
5440 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005441void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005442{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005443 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005444 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005445 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005446 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005447 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005448 /* The 4th id byte is the important one */
5449 extid = id_data[3];
5450
Boris Brezillon01389b62016-06-08 10:30:18 +02005451 /* Calc pagesize */
5452 mtd->writesize = 1024 << (extid & 0x03);
5453 extid >>= 2;
5454 /* Calc oobsize */
5455 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5456 extid >>= 2;
5457 /* Calc blocksize. Blocksize is multiples of 64KiB */
5458 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5459 extid >>= 2;
5460 /* Get buswidth information */
5461 if (extid & 0x1)
5462 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005463}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005464EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005465
5466/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005467 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5468 * decodes a matching ID table entry and assigns the MTD size parameters for
5469 * the chip.
5470 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005471static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005472{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005473 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005474
5475 mtd->erasesize = type->erasesize;
5476 mtd->writesize = type->pagesize;
5477 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005478
Huang Shijie1c195e92013-09-25 14:58:12 +08005479 /* All legacy ID NAND are small-page, SLC */
5480 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005481}
5482
5483/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005484 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5485 * heuristic patterns using various detected parameters (e.g., manufacturer,
5486 * page size, cell-type information).
5487 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005488static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005489{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005490 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005491
5492 /* Set the bad block position */
5493 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5494 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5495 else
5496 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005497}
5498
Huang Shijieec6e87e2013-03-15 11:01:00 +08005499static inline bool is_full_id_nand(struct nand_flash_dev *type)
5500{
5501 return type->id_len;
5502}
5503
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005504static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005505 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005506{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005507 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005508 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005509
Huang Shijieec6e87e2013-03-15 11:01:00 +08005510 if (!strncmp(type->id, id_data, type->id_len)) {
5511 mtd->writesize = type->pagesize;
5512 mtd->erasesize = type->erasesize;
5513 mtd->oobsize = type->oobsize;
5514
Huang Shijie7db906b2013-09-25 14:58:11 +08005515 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005516 chip->chipsize = (uint64_t)type->chipsize << 20;
5517 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005518 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5519 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005520 chip->onfi_timing_mode_default =
5521 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005522
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005523 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5524 if (!chip->parameters.model)
5525 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005526
Huang Shijieec6e87e2013-03-15 11:01:00 +08005527 return true;
5528 }
5529 return false;
5530}
5531
Brian Norris7e74c2d2012-09-24 20:40:49 -07005532/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005533 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5534 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5535 * table.
5536 */
5537static void nand_manufacturer_detect(struct nand_chip *chip)
5538{
5539 /*
5540 * Try manufacturer detection if available and use
5541 * nand_decode_ext_id() otherwise.
5542 */
5543 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005544 chip->manufacturer.desc->ops->detect) {
5545 /* The 3rd id byte holds MLC / multichip data */
5546 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005547 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005548 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005549 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005550 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005551}
5552
5553/*
5554 * Manufacturer initialization. This function is called for all NANDs including
5555 * ONFI and JEDEC compliant ones.
5556 * Manufacturer drivers should put all their specific initialization code in
5557 * their ->init() hook.
5558 */
5559static int nand_manufacturer_init(struct nand_chip *chip)
5560{
5561 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5562 !chip->manufacturer.desc->ops->init)
5563 return 0;
5564
5565 return chip->manufacturer.desc->ops->init(chip);
5566}
5567
5568/*
5569 * Manufacturer cleanup. This function is called for all NANDs including
5570 * ONFI and JEDEC compliant ones.
5571 * Manufacturer drivers should put all their specific cleanup code in their
5572 * ->cleanup() hook.
5573 */
5574static void nand_manufacturer_cleanup(struct nand_chip *chip)
5575{
5576 /* Release manufacturer private data */
5577 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5578 chip->manufacturer.desc->ops->cleanup)
5579 chip->manufacturer.desc->ops->cleanup(chip);
5580}
5581
5582/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005583 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005584 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005585static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005586{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005587 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005588 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005589 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005590 u8 *id_data = chip->id.data;
5591 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Karl Beldanef89a882008-09-15 14:37:29 +02005593 /*
5594 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005595 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005596 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005597 ret = nand_reset(chip, 0);
5598 if (ret)
5599 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005600
5601 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005602 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005605 ret = nand_readid_op(chip, 0, id_data, 2);
5606 if (ret)
5607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005610 maf_id = id_data[0];
5611 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Brian Norris8b6e50c2011-05-25 14:59:01 -07005613 /*
5614 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005615 * interface concerns can cause random data which looks like a
5616 * possibly credible NAND flash to appear. If the two results do
5617 * not match, ignore the device completely.
5618 */
5619
Brian Norris4aef9b72012-09-24 20:40:48 -07005620 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005621 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5622 if (ret)
5623 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005624
Boris Brezillon7f501f02016-05-24 19:20:05 +02005625 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005626 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005627 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005628 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005629 }
5630
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005631 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005632
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005633 /* Try to identify manufacturer */
5634 manufacturer = nand_get_manufacturer(maf_id);
5635 chip->manufacturer.desc = manufacturer;
5636
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005637 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005638 type = nand_flash_ids;
5639
Boris Brezillon29a198a2016-05-24 20:17:48 +02005640 /*
5641 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5642 * override it.
5643 * This is required to make sure initial NAND bus width set by the
5644 * NAND controller driver is coherent with the real NAND bus width
5645 * (extracted by auto-detection code).
5646 */
5647 busw = chip->options & NAND_BUSWIDTH_16;
5648
5649 /*
5650 * The flag is only set (never cleared), reset it to its default value
5651 * before starting auto-detection.
5652 */
5653 chip->options &= ~NAND_BUSWIDTH_16;
5654
Huang Shijieec6e87e2013-03-15 11:01:00 +08005655 for (; type->name != NULL; type++) {
5656 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005657 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005658 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005659 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005660 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005661 }
5662 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005663
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005664 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005665 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005666 ret = nand_flash_detect_onfi(chip);
5667 if (ret < 0)
5668 return ret;
5669 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005670 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005671
5672 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005673 ret = nand_flash_detect_jedec(chip);
5674 if (ret < 0)
5675 return ret;
5676 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005677 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005678 }
5679
David Woodhouse5e81e882010-02-26 18:32:56 +00005680 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005681 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005682
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005683 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5684 if (!chip->parameters.model)
5685 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005686
Adrian Hunter69423d92008-12-10 13:37:21 +00005687 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005688
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005689 if (!type->pagesize)
5690 nand_manufacturer_detect(chip);
5691 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005692 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005693
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005694 /* Get chip options */
5695 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005696
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005697ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005698 if (!mtd->name)
5699 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005700
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005701 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005702 WARN_ON(busw & NAND_BUSWIDTH_16);
5703 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005704 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5705 /*
5706 * Check, if buswidth is correct. Hardware drivers should set
5707 * chip correct!
5708 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005709 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005710 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005711 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5712 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005713 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5714 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005715 ret = -EINVAL;
5716
5717 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005718 }
5719
Boris Brezillon7f501f02016-05-24 19:20:05 +02005720 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005721
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005722 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005723 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005724 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005725 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005726
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005727 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005728 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005729 if (chip->chipsize & 0xffffffff)
5730 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005731 else {
5732 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5733 chip->chip_shift += 32 - 1;
5734 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005735
Masahiro Yamada14157f82017-09-13 11:05:50 +09005736 if (chip->chip_shift - chip->page_shift > 16)
5737 chip->options |= NAND_ROW_ADDR_3;
5738
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005739 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005740
Brian Norris8b6e50c2011-05-25 14:59:01 -07005741 /* Do not replace user supplied command function! */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005742 if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
5743 chip->legacy.cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005744
Ezequiel Garcia20171642013-11-25 08:30:31 -03005745 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005746 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005747 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5748 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005749 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005750 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005751 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005752 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005753
5754free_detect_allocation:
5755 kfree(chip->parameters.model);
5756
5757 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005758}
5759
Boris Brezillond48f62b2016-04-01 14:54:32 +02005760static const char * const nand_ecc_modes[] = {
5761 [NAND_ECC_NONE] = "none",
5762 [NAND_ECC_SOFT] = "soft",
5763 [NAND_ECC_HW] = "hw",
5764 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5765 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005766 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005767};
5768
5769static int of_get_nand_ecc_mode(struct device_node *np)
5770{
5771 const char *pm;
5772 int err, i;
5773
5774 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5775 if (err < 0)
5776 return err;
5777
5778 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5779 if (!strcasecmp(pm, nand_ecc_modes[i]))
5780 return i;
5781
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005782 /*
5783 * For backward compatibility we support few obsoleted values that don't
5784 * have their mappings into nand_ecc_modes_t anymore (they were merged
5785 * with other enums).
5786 */
5787 if (!strcasecmp(pm, "soft_bch"))
5788 return NAND_ECC_SOFT;
5789
Boris Brezillond48f62b2016-04-01 14:54:32 +02005790 return -ENODEV;
5791}
5792
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005793static const char * const nand_ecc_algos[] = {
5794 [NAND_ECC_HAMMING] = "hamming",
5795 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005796 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005797};
5798
Boris Brezillond48f62b2016-04-01 14:54:32 +02005799static int of_get_nand_ecc_algo(struct device_node *np)
5800{
5801 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005802 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005803
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005804 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5805 if (!err) {
5806 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5807 if (!strcasecmp(pm, nand_ecc_algos[i]))
5808 return i;
5809 return -ENODEV;
5810 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005811
5812 /*
5813 * For backward compatibility we also read "nand-ecc-mode" checking
5814 * for some obsoleted values that were specifying ECC algorithm.
5815 */
5816 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5817 if (err < 0)
5818 return err;
5819
5820 if (!strcasecmp(pm, "soft"))
5821 return NAND_ECC_HAMMING;
5822 else if (!strcasecmp(pm, "soft_bch"))
5823 return NAND_ECC_BCH;
5824
5825 return -ENODEV;
5826}
5827
5828static int of_get_nand_ecc_step_size(struct device_node *np)
5829{
5830 int ret;
5831 u32 val;
5832
5833 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5834 return ret ? ret : val;
5835}
5836
5837static int of_get_nand_ecc_strength(struct device_node *np)
5838{
5839 int ret;
5840 u32 val;
5841
5842 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5843 return ret ? ret : val;
5844}
5845
5846static int of_get_nand_bus_width(struct device_node *np)
5847{
5848 u32 val;
5849
5850 if (of_property_read_u32(np, "nand-bus-width", &val))
5851 return 8;
5852
5853 switch (val) {
5854 case 8:
5855 case 16:
5856 return val;
5857 default:
5858 return -EIO;
5859 }
5860}
5861
5862static bool of_get_nand_on_flash_bbt(struct device_node *np)
5863{
5864 return of_property_read_bool(np, "nand-on-flash-bbt");
5865}
5866
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005867static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005868{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005869 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005870 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005871
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005872 if (!dn)
5873 return 0;
5874
Brian Norris5844fee2015-01-23 00:22:27 -08005875 if (of_get_nand_bus_width(dn) == 16)
5876 chip->options |= NAND_BUSWIDTH_16;
5877
Stefan Agnerf922bd72018-06-24 23:27:23 +02005878 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5879 chip->options |= NAND_IS_BOOT_MEDIUM;
5880
Brian Norris5844fee2015-01-23 00:22:27 -08005881 if (of_get_nand_on_flash_bbt(dn))
5882 chip->bbt_options |= NAND_BBT_USE_FLASH;
5883
5884 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005885 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005886 ecc_strength = of_get_nand_ecc_strength(dn);
5887 ecc_step = of_get_nand_ecc_step_size(dn);
5888
Brian Norris5844fee2015-01-23 00:22:27 -08005889 if (ecc_mode >= 0)
5890 chip->ecc.mode = ecc_mode;
5891
Rafał Miłecki79082452016-03-23 11:19:02 +01005892 if (ecc_algo >= 0)
5893 chip->ecc.algo = ecc_algo;
5894
Brian Norris5844fee2015-01-23 00:22:27 -08005895 if (ecc_strength >= 0)
5896 chip->ecc.strength = ecc_strength;
5897
5898 if (ecc_step > 0)
5899 chip->ecc.size = ecc_step;
5900
Boris Brezillonba78ee02016-06-08 17:04:22 +02005901 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5902 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5903
Brian Norris5844fee2015-01-23 00:22:27 -08005904 return 0;
5905}
5906
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005907/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005908 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005909 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005910 * @maxchips: number of chips to scan for
5911 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005912 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005913 * This is the first phase of the normal nand_scan() function. It reads the
5914 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005915 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005916 * This helper used to be called directly from controller drivers that needed
5917 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5918 * prevented dynamic allocations during this phase which was unconvenient and
5919 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005920 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005921static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005922 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005923{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005924 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005925 int nand_maf_id, nand_dev_id;
5926 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005927 int ret;
5928
Miquel Raynal17fa8042017-11-30 18:01:31 +01005929 /* Enforce the right timings for reset/detection */
5930 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5931
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005932 ret = nand_dt_init(chip);
5933 if (ret)
5934 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005935
Brian Norrisf7a8e382016-01-05 10:39:45 -08005936 if (!mtd->name && mtd->dev.parent)
5937 mtd->name = dev_name(mtd->dev.parent);
5938
Miquel Raynal8878b122017-11-09 14:16:45 +01005939 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005940 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
5941 * not populated.
Miquel Raynal8878b122017-11-09 14:16:45 +01005942 */
5943 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005944 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005945 * Default functions assigned for ->legacy.cmdfunc() and
5946 * ->select_chip() both expect ->legacy.cmd_ctrl() to be
5947 * populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005948 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005949 if ((!chip->legacy.cmdfunc || !chip->select_chip) &&
5950 !chip->legacy.cmd_ctrl) {
5951 pr_err("->legacy.cmd_ctrl() should be provided\n");
Miquel Raynal8878b122017-11-09 14:16:45 +01005952 return -EINVAL;
5953 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005954 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005955
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005956 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005957 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005958
5959 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005960 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005961 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005962 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005963 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005964 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005965 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 }
5967
Boris Brezillon7f501f02016-05-24 19:20:05 +02005968 nand_maf_id = chip->id.data[0];
5969 nand_dev_id = chip->id.data[1];
5970
Boris Brezillon758b56f2018-09-06 14:05:24 +02005971 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005972
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005973 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005974 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005975 u8 id[2];
5976
Karl Beldanef89a882008-09-15 14:37:29 +02005977 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005978 nand_reset(chip, i);
5979
Boris Brezillon758b56f2018-09-06 14:05:24 +02005980 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005982 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005984 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005985 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 break;
Huang Shijie07300162012-11-09 16:23:45 +08005987 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005988 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 }
5990 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005991 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005994 chip->numchips = i;
5995 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
David Woodhouse3b85c322006-09-25 17:06:53 +01005997 return 0;
5998}
5999
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006000static void nand_scan_ident_cleanup(struct nand_chip *chip)
6001{
6002 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006003 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006004}
6005
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006006static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6007{
6008 struct nand_chip *chip = mtd_to_nand(mtd);
6009 struct nand_ecc_ctrl *ecc = &chip->ecc;
6010
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006011 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006012 return -EINVAL;
6013
6014 switch (ecc->algo) {
6015 case NAND_ECC_HAMMING:
6016 ecc->calculate = nand_calculate_ecc;
6017 ecc->correct = nand_correct_data;
6018 ecc->read_page = nand_read_page_swecc;
6019 ecc->read_subpage = nand_read_subpage;
6020 ecc->write_page = nand_write_page_swecc;
6021 ecc->read_page_raw = nand_read_page_raw;
6022 ecc->write_page_raw = nand_write_page_raw;
6023 ecc->read_oob = nand_read_oob_std;
6024 ecc->write_oob = nand_write_oob_std;
6025 if (!ecc->size)
6026 ecc->size = 256;
6027 ecc->bytes = 3;
6028 ecc->strength = 1;
6029 return 0;
6030 case NAND_ECC_BCH:
6031 if (!mtd_nand_has_bch()) {
6032 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6033 return -EINVAL;
6034 }
6035 ecc->calculate = nand_bch_calculate_ecc;
6036 ecc->correct = nand_bch_correct_data;
6037 ecc->read_page = nand_read_page_swecc;
6038 ecc->read_subpage = nand_read_subpage;
6039 ecc->write_page = nand_write_page_swecc;
6040 ecc->read_page_raw = nand_read_page_raw;
6041 ecc->write_page_raw = nand_write_page_raw;
6042 ecc->read_oob = nand_read_oob_std;
6043 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006044
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006045 /*
6046 * Board driver should supply ecc.size and ecc.strength
6047 * values to select how many bits are correctable.
6048 * Otherwise, default to 4 bits for large page devices.
6049 */
6050 if (!ecc->size && (mtd->oobsize >= 64)) {
6051 ecc->size = 512;
6052 ecc->strength = 4;
6053 }
6054
6055 /*
6056 * if no ecc placement scheme was provided pickup the default
6057 * large page one.
6058 */
6059 if (!mtd->ooblayout) {
6060 /* handle large page devices only */
6061 if (mtd->oobsize < 64) {
6062 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6063 return -EINVAL;
6064 }
6065
6066 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006067
6068 }
6069
6070 /*
6071 * We can only maximize ECC config when the default layout is
6072 * used, otherwise we don't know how many bytes can really be
6073 * used.
6074 */
6075 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6076 ecc->options & NAND_ECC_MAXIMIZE) {
6077 int steps, bytes;
6078
6079 /* Always prefer 1k blocks over 512bytes ones */
6080 ecc->size = 1024;
6081 steps = mtd->writesize / ecc->size;
6082
6083 /* Reserve 2 bytes for the BBM */
6084 bytes = (mtd->oobsize - 2) / steps;
6085 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006086 }
6087
6088 /* See nand_bch_init() for details. */
6089 ecc->bytes = 0;
6090 ecc->priv = nand_bch_init(mtd);
6091 if (!ecc->priv) {
6092 WARN(1, "BCH ECC initialization failed!\n");
6093 return -EINVAL;
6094 }
6095 return 0;
6096 default:
6097 WARN(1, "Unsupported ECC algorithm!\n");
6098 return -EINVAL;
6099 }
6100}
6101
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006102/**
6103 * nand_check_ecc_caps - check the sanity of preset ECC settings
6104 * @chip: nand chip info structure
6105 * @caps: ECC caps info structure
6106 * @oobavail: OOB size that the ECC engine can use
6107 *
6108 * When ECC step size and strength are already set, check if they are supported
6109 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6110 * On success, the calculated ECC bytes is set.
6111 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306112static int
6113nand_check_ecc_caps(struct nand_chip *chip,
6114 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006115{
6116 struct mtd_info *mtd = nand_to_mtd(chip);
6117 const struct nand_ecc_step_info *stepinfo;
6118 int preset_step = chip->ecc.size;
6119 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306120 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006121 int i, j;
6122
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006123 for (i = 0; i < caps->nstepinfos; i++) {
6124 stepinfo = &caps->stepinfos[i];
6125
6126 if (stepinfo->stepsize != preset_step)
6127 continue;
6128
6129 for (j = 0; j < stepinfo->nstrengths; j++) {
6130 if (stepinfo->strengths[j] != preset_strength)
6131 continue;
6132
6133 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6134 preset_strength);
6135 if (WARN_ON_ONCE(ecc_bytes < 0))
6136 return ecc_bytes;
6137
6138 if (ecc_bytes * nsteps > oobavail) {
6139 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6140 preset_step, preset_strength);
6141 return -ENOSPC;
6142 }
6143
6144 chip->ecc.bytes = ecc_bytes;
6145
6146 return 0;
6147 }
6148 }
6149
6150 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6151 preset_step, preset_strength);
6152
6153 return -ENOTSUPP;
6154}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006155
6156/**
6157 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6158 * @chip: nand chip info structure
6159 * @caps: ECC engine caps info structure
6160 * @oobavail: OOB size that the ECC engine can use
6161 *
6162 * If a chip's ECC requirement is provided, try to meet it with the least
6163 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6164 * On success, the chosen ECC settings are set.
6165 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306166static int
6167nand_match_ecc_req(struct nand_chip *chip,
6168 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006169{
6170 struct mtd_info *mtd = nand_to_mtd(chip);
6171 const struct nand_ecc_step_info *stepinfo;
6172 int req_step = chip->ecc_step_ds;
6173 int req_strength = chip->ecc_strength_ds;
6174 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6175 int best_step, best_strength, best_ecc_bytes;
6176 int best_ecc_bytes_total = INT_MAX;
6177 int i, j;
6178
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006179 /* No information provided by the NAND chip */
6180 if (!req_step || !req_strength)
6181 return -ENOTSUPP;
6182
6183 /* number of correctable bits the chip requires in a page */
6184 req_corr = mtd->writesize / req_step * req_strength;
6185
6186 for (i = 0; i < caps->nstepinfos; i++) {
6187 stepinfo = &caps->stepinfos[i];
6188 step_size = stepinfo->stepsize;
6189
6190 for (j = 0; j < stepinfo->nstrengths; j++) {
6191 strength = stepinfo->strengths[j];
6192
6193 /*
6194 * If both step size and strength are smaller than the
6195 * chip's requirement, it is not easy to compare the
6196 * resulted reliability.
6197 */
6198 if (step_size < req_step && strength < req_strength)
6199 continue;
6200
6201 if (mtd->writesize % step_size)
6202 continue;
6203
6204 nsteps = mtd->writesize / step_size;
6205
6206 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6207 if (WARN_ON_ONCE(ecc_bytes < 0))
6208 continue;
6209 ecc_bytes_total = ecc_bytes * nsteps;
6210
6211 if (ecc_bytes_total > oobavail ||
6212 strength * nsteps < req_corr)
6213 continue;
6214
6215 /*
6216 * We assume the best is to meet the chip's requrement
6217 * with the least number of ECC bytes.
6218 */
6219 if (ecc_bytes_total < best_ecc_bytes_total) {
6220 best_ecc_bytes_total = ecc_bytes_total;
6221 best_step = step_size;
6222 best_strength = strength;
6223 best_ecc_bytes = ecc_bytes;
6224 }
6225 }
6226 }
6227
6228 if (best_ecc_bytes_total == INT_MAX)
6229 return -ENOTSUPP;
6230
6231 chip->ecc.size = best_step;
6232 chip->ecc.strength = best_strength;
6233 chip->ecc.bytes = best_ecc_bytes;
6234
6235 return 0;
6236}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006237
6238/**
6239 * nand_maximize_ecc - choose the max ECC strength available
6240 * @chip: nand chip info structure
6241 * @caps: ECC engine caps info structure
6242 * @oobavail: OOB size that the ECC engine can use
6243 *
6244 * Choose the max ECC strength that is supported on the controller, and can fit
6245 * within the chip's OOB. On success, the chosen ECC settings are set.
6246 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306247static int
6248nand_maximize_ecc(struct nand_chip *chip,
6249 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006250{
6251 struct mtd_info *mtd = nand_to_mtd(chip);
6252 const struct nand_ecc_step_info *stepinfo;
6253 int step_size, strength, nsteps, ecc_bytes, corr;
6254 int best_corr = 0;
6255 int best_step = 0;
6256 int best_strength, best_ecc_bytes;
6257 int i, j;
6258
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006259 for (i = 0; i < caps->nstepinfos; i++) {
6260 stepinfo = &caps->stepinfos[i];
6261 step_size = stepinfo->stepsize;
6262
6263 /* If chip->ecc.size is already set, respect it */
6264 if (chip->ecc.size && step_size != chip->ecc.size)
6265 continue;
6266
6267 for (j = 0; j < stepinfo->nstrengths; j++) {
6268 strength = stepinfo->strengths[j];
6269
6270 if (mtd->writesize % step_size)
6271 continue;
6272
6273 nsteps = mtd->writesize / step_size;
6274
6275 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6276 if (WARN_ON_ONCE(ecc_bytes < 0))
6277 continue;
6278
6279 if (ecc_bytes * nsteps > oobavail)
6280 continue;
6281
6282 corr = strength * nsteps;
6283
6284 /*
6285 * If the number of correctable bits is the same,
6286 * bigger step_size has more reliability.
6287 */
6288 if (corr > best_corr ||
6289 (corr == best_corr && step_size > best_step)) {
6290 best_corr = corr;
6291 best_step = step_size;
6292 best_strength = strength;
6293 best_ecc_bytes = ecc_bytes;
6294 }
6295 }
6296 }
6297
6298 if (!best_corr)
6299 return -ENOTSUPP;
6300
6301 chip->ecc.size = best_step;
6302 chip->ecc.strength = best_strength;
6303 chip->ecc.bytes = best_ecc_bytes;
6304
6305 return 0;
6306}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006307
Abhishek Sahu181ace92018-06-20 12:57:28 +05306308/**
6309 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6310 * @chip: nand chip info structure
6311 * @caps: ECC engine caps info structure
6312 * @oobavail: OOB size that the ECC engine can use
6313 *
6314 * Choose the ECC configuration according to following logic
6315 *
6316 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6317 * then check if it is supported by this controller.
6318 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6319 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6320 * to the chip's requirement. If available OOB size can't fit the chip
6321 * requirement then fallback to the maximum ECC step size and ECC strength.
6322 *
6323 * On success, the chosen ECC settings are set.
6324 */
6325int nand_ecc_choose_conf(struct nand_chip *chip,
6326 const struct nand_ecc_caps *caps, int oobavail)
6327{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306328 struct mtd_info *mtd = nand_to_mtd(chip);
6329
6330 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6331 return -EINVAL;
6332
Abhishek Sahu181ace92018-06-20 12:57:28 +05306333 if (chip->ecc.size && chip->ecc.strength)
6334 return nand_check_ecc_caps(chip, caps, oobavail);
6335
6336 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6337 return nand_maximize_ecc(chip, caps, oobavail);
6338
6339 if (!nand_match_ecc_req(chip, caps, oobavail))
6340 return 0;
6341
6342 return nand_maximize_ecc(chip, caps, oobavail);
6343}
6344EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6345
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006346/*
6347 * Check if the chip configuration meet the datasheet requirements.
6348
6349 * If our configuration corrects A bits per B bytes and the minimum
6350 * required correction level is X bits per Y bytes, then we must ensure
6351 * both of the following are true:
6352 *
6353 * (1) A / B >= X / Y
6354 * (2) A >= X
6355 *
6356 * Requirement (1) ensures we can correct for the required bitflip density.
6357 * Requirement (2) ensures we can correct even when all bitflips are clumped
6358 * in the same sector.
6359 */
6360static bool nand_ecc_strength_good(struct mtd_info *mtd)
6361{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006362 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006363 struct nand_ecc_ctrl *ecc = &chip->ecc;
6364 int corr, ds_corr;
6365
6366 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6367 /* Not enough information */
6368 return true;
6369
6370 /*
6371 * We get the number of corrected bits per page to compare
6372 * the correction density.
6373 */
6374 corr = (mtd->writesize * ecc->strength) / ecc->size;
6375 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6376
6377 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6378}
David Woodhouse3b85c322006-09-25 17:06:53 +01006379
6380/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006381 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006382 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006383 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006384 * This is the second phase of the normal nand_scan() function. It fills out
6385 * all the uninitialized function pointers with the defaults and scans for a
6386 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006387 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006388static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006389{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006390 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006391 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006392 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006393
Brian Norrise2414f42012-02-06 13:44:00 -08006394 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006395 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006396 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006397 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006398 }
Brian Norrise2414f42012-02-06 13:44:00 -08006399
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006400 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006401 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006402 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006403
Boris Brezillonf84674b2017-06-02 12:18:24 +02006404 /*
6405 * FIXME: some NAND manufacturer drivers expect the first die to be
6406 * selected when manufacturer->init() is called. They should be fixed
6407 * to explictly select the relevant die when interacting with the NAND
6408 * chip.
6409 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02006410 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006411 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02006412 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006413 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006414 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006415
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006416 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006417 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006418
6419 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006420 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006421 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006422 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006423 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006424 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006427 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 break;
6429 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006430 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006431 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006434 /*
6435 * Expose the whole OOB area to users if ECC_NONE
6436 * is passed. We could do that for all kind of
6437 * ->oobsize, but we must keep the old large/small
6438 * page with ECC layout when ->oobsize <= 128 for
6439 * compatibility reasons.
6440 */
6441 if (ecc->mode == NAND_ECC_NONE) {
6442 mtd_set_ooblayout(mtd,
6443 &nand_ooblayout_lp_ops);
6444 break;
6445 }
6446
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006447 WARN(1, "No oob scheme defined for oobsize %d\n",
6448 mtd->oobsize);
6449 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006450 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 }
6452 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006453
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006454 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006455 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006456 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006457 */
David Woodhouse956e9442006-09-25 17:12:39 +01006458
Huang Shijie97de79e02013-10-18 14:20:53 +08006459 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006460 case NAND_ECC_HW_OOB_FIRST:
6461 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006462 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006463 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6464 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006465 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006466 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006467 if (!ecc->read_page)
6468 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006469
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006470 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006471 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006472 if (!ecc->read_page)
6473 ecc->read_page = nand_read_page_hwecc;
6474 if (!ecc->write_page)
6475 ecc->write_page = nand_write_page_hwecc;
6476 if (!ecc->read_page_raw)
6477 ecc->read_page_raw = nand_read_page_raw;
6478 if (!ecc->write_page_raw)
6479 ecc->write_page_raw = nand_write_page_raw;
6480 if (!ecc->read_oob)
6481 ecc->read_oob = nand_read_oob_std;
6482 if (!ecc->write_oob)
6483 ecc->write_oob = nand_write_oob_std;
6484 if (!ecc->read_subpage)
6485 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006486 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006487 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006488
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006489 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006490 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6491 (!ecc->read_page ||
6492 ecc->read_page == nand_read_page_hwecc ||
6493 !ecc->write_page ||
6494 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006495 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6496 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006497 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006498 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006499 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006500 if (!ecc->read_page)
6501 ecc->read_page = nand_read_page_syndrome;
6502 if (!ecc->write_page)
6503 ecc->write_page = nand_write_page_syndrome;
6504 if (!ecc->read_page_raw)
6505 ecc->read_page_raw = nand_read_page_raw_syndrome;
6506 if (!ecc->write_page_raw)
6507 ecc->write_page_raw = nand_write_page_raw_syndrome;
6508 if (!ecc->read_oob)
6509 ecc->read_oob = nand_read_oob_syndrome;
6510 if (!ecc->write_oob)
6511 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006512
Huang Shijie97de79e02013-10-18 14:20:53 +08006513 if (mtd->writesize >= ecc->size) {
6514 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006515 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6516 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006517 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006518 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006519 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006520 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006521 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6522 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006523 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006524 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006526 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006527 ret = nand_set_ecc_soft_ops(mtd);
6528 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006529 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006530 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006531 }
6532 break;
6533
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006534 case NAND_ECC_ON_DIE:
6535 if (!ecc->read_page || !ecc->write_page) {
6536 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6537 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006538 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006539 }
6540 if (!ecc->read_oob)
6541 ecc->read_oob = nand_read_oob_std;
6542 if (!ecc->write_oob)
6543 ecc->write_oob = nand_write_oob_std;
6544 break;
6545
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006546 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006547 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006548 ecc->read_page = nand_read_page_raw;
6549 ecc->write_page = nand_write_page_raw;
6550 ecc->read_oob = nand_read_oob_std;
6551 ecc->read_page_raw = nand_read_page_raw;
6552 ecc->write_page_raw = nand_write_page_raw;
6553 ecc->write_oob = nand_write_oob_std;
6554 ecc->size = mtd->writesize;
6555 ecc->bytes = 0;
6556 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006558
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006560 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6561 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006562 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006565 if (ecc->correct || ecc->calculate) {
6566 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6567 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6568 if (!ecc->calc_buf || !ecc->code_buf) {
6569 ret = -ENOMEM;
6570 goto err_nand_manuf_cleanup;
6571 }
6572 }
6573
Brian Norris9ce244b2011-08-30 18:45:37 -07006574 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006575 if (!ecc->read_oob_raw)
6576 ecc->read_oob_raw = ecc->read_oob;
6577 if (!ecc->write_oob_raw)
6578 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006579
Boris Brezillon846031d2016-02-03 20:11:00 +01006580 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006581 mtd->ecc_strength = ecc->strength;
6582 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006583
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006584 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006585 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006586 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006587 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006588 ecc->steps = mtd->writesize / ecc->size;
6589 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006590 WARN(1, "Invalid ECC parameters\n");
6591 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006592 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006594 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006595 if (ecc->total > mtd->oobsize) {
6596 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6597 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006598 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006599 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006600
Boris Brezillon846031d2016-02-03 20:11:00 +01006601 /*
6602 * The number of bytes available for a client to place data into
6603 * the out of band area.
6604 */
6605 ret = mtd_ooblayout_count_freebytes(mtd);
6606 if (ret < 0)
6607 ret = 0;
6608
6609 mtd->oobavail = ret;
6610
6611 /* ECC sanity check: warn if it's too weak */
6612 if (!nand_ecc_strength_good(mtd))
6613 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6614 mtd->name);
6615
Brian Norris8b6e50c2011-05-25 14:59:01 -07006616 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006617 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006618 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006619 case 2:
6620 mtd->subpage_sft = 1;
6621 break;
6622 case 4:
6623 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006624 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006625 mtd->subpage_sft = 2;
6626 break;
6627 }
6628 }
6629 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6630
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006631 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006632 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006635 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006637 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306638 switch (ecc->mode) {
6639 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306640 if (chip->page_shift > 9)
6641 chip->options |= NAND_SUBPAGE_READ;
6642 break;
6643
6644 default:
6645 break;
6646 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006647
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006649 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006650 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6651 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006652 mtd->_erase = nand_erase;
6653 mtd->_point = NULL;
6654 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006655 mtd->_panic_write = panic_nand_write;
6656 mtd->_read_oob = nand_read_oob;
6657 mtd->_write_oob = nand_write_oob;
6658 mtd->_sync = nand_sync;
6659 mtd->_lock = NULL;
6660 mtd->_unlock = NULL;
6661 mtd->_suspend = nand_suspend;
6662 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006663 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006664 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006665 mtd->_block_isbad = nand_block_isbad;
6666 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006667 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006668 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006670 /*
6671 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6672 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6673 * properly set.
6674 */
6675 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006676 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
Boris Brezillonf84674b2017-06-02 12:18:24 +02006678 /* Initialize the ->data_interface field. */
6679 ret = nand_init_data_interface(chip);
6680 if (ret)
6681 goto err_nand_manuf_cleanup;
6682
6683 /* Enter fastest possible mode on all dies. */
6684 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006685 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006686 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006687 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006688 }
6689
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006690 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006691 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
6694 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006695 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006696 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006697 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006698
Brian Norris44d41822017-05-01 17:04:50 -07006699 return 0;
6700
Boris Brezillonf84674b2017-06-02 12:18:24 +02006701
6702err_nand_manuf_cleanup:
6703 nand_manufacturer_cleanup(chip);
6704
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006705err_free_buf:
6706 kfree(chip->data_buf);
6707 kfree(ecc->code_buf);
6708 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006709
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006710 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711}
6712
Miquel Raynal05b54c72018-07-19 01:05:46 +02006713static int nand_attach(struct nand_chip *chip)
6714{
6715 if (chip->controller->ops && chip->controller->ops->attach_chip)
6716 return chip->controller->ops->attach_chip(chip);
6717
6718 return 0;
6719}
6720
6721static void nand_detach(struct nand_chip *chip)
6722{
6723 if (chip->controller->ops && chip->controller->ops->detach_chip)
6724 chip->controller->ops->detach_chip(chip);
6725}
6726
David Woodhouse3b85c322006-09-25 17:06:53 +01006727/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006728 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006729 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02006730 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006731 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006732 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006733 * This fills out all the uninitialized function pointers with the defaults.
6734 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006735 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006736 */
Boris Brezillon871a4072018-08-04 22:59:22 +02006737int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006738 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006739{
6740 int ret;
6741
Boris Brezillon800342d2018-08-04 22:59:23 +02006742 if (!maxchips)
6743 return -EINVAL;
6744
6745 ret = nand_scan_ident(chip, maxchips, ids);
6746 if (ret)
6747 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006748
6749 ret = nand_attach(chip);
6750 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006751 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006752
Boris Brezillon00ad3782018-09-06 14:05:14 +02006753 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006754 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006755 goto detach_chip;
6756
6757 return 0;
6758
6759detach_chip:
6760 nand_detach(chip);
6761cleanup_ident:
6762 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006763
David Woodhouse3b85c322006-09-25 17:06:53 +01006764 return ret;
6765}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006766EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006767
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006769 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6770 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006771 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006772void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006774 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006775 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006776 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6777
Jesper Juhlfa671642005-11-07 01:01:27 -08006778 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006779 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006780 kfree(chip->data_buf);
6781 kfree(chip->ecc.code_buf);
6782 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006783
6784 /* Free bad block descriptor memory */
6785 if (chip->badblock_pattern && chip->badblock_pattern->options
6786 & NAND_BBT_DYNAMICSTRUCT)
6787 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006788
6789 /* Free manufacturer priv data. */
6790 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006791
6792 /* Free controller specific allocations after chip identification */
6793 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006794
6795 /* Free identification phase allocations */
6796 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006798
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006799EXPORT_SYMBOL_GPL(nand_cleanup);
6800
6801/**
6802 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6803 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006804 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006805 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006806void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006807{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006808 mtd_device_unregister(nand_to_mtd(chip));
6809 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006810}
David Woodhousee0c7d762006-05-13 18:07:53 +01006811EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006812
David Woodhousee0c7d762006-05-13 18:07:53 +01006813MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006814MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6815MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006816MODULE_DESCRIPTION("Generic NAND flash driver code");