blob: 57c89e275a3a1f612b5a979c968a0436dbb0f87a [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;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200830 udelay(chip->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) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200856 udelay(chip->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;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200967 udelay(chip->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) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001008 udelay(chip->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/**
1179 * nand_get_features - wrapper to perform a GET_FEATURE
1180 * @chip: NAND chip info structure
1181 * @addr: feature address
1182 * @subfeature_param: the subfeature parameters, a four bytes array
1183 *
1184 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1185 * operation cannot be handled.
1186 */
1187int nand_get_features(struct nand_chip *chip, int addr,
1188 u8 *subfeature_param)
1189{
Miquel Raynal789157e2018-03-19 14:47:28 +01001190 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001191 return -ENOTSUPP;
1192
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001193 return chip->get_features(chip, addr, subfeature_param);
Miquel Raynal97baea12018-03-19 14:47:20 +01001194}
1195EXPORT_SYMBOL_GPL(nand_get_features);
1196
1197/**
1198 * nand_set_features - wrapper to perform a SET_FEATURE
1199 * @chip: NAND chip info structure
1200 * @addr: feature address
1201 * @subfeature_param: the subfeature parameters, a four bytes array
1202 *
1203 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1204 * operation cannot be handled.
1205 */
1206int nand_set_features(struct nand_chip *chip, int addr,
1207 u8 *subfeature_param)
1208{
Miquel Raynal789157e2018-03-19 14:47:28 +01001209 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001210 return -ENOTSUPP;
1211
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001212 return chip->set_features(chip, addr, subfeature_param);
Miquel Raynal97baea12018-03-19 14:47:20 +01001213}
1214EXPORT_SYMBOL_GPL(nand_set_features);
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001217 * nand_reset_data_interface - Reset 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 * Reset the Data interface and timings to ONFI mode 0.
1222 *
1223 * Returns 0 for success or negative error code otherwise.
1224 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001225static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001226{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001227 int ret;
1228
1229 if (!chip->setup_data_interface)
1230 return 0;
1231
1232 /*
1233 * The ONFI specification says:
1234 * "
1235 * To transition from NV-DDR or NV-DDR2 to the SDR data
1236 * interface, the host shall use the Reset (FFh) command
1237 * using SDR timing mode 0. A device in any timing mode is
1238 * required to recognize Reset (FFh) command issued in SDR
1239 * timing mode 0.
1240 * "
1241 *
1242 * Configure the data interface in SDR mode and set the
1243 * timings to timing mode 0.
1244 */
1245
Miquel Raynal17fa8042017-11-30 18:01:31 +01001246 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +02001247 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001248 if (ret)
1249 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1250
1251 return ret;
1252}
1253
1254/**
1255 * nand_setup_data_interface - Setup the best data interface and timings
1256 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001257 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001258 *
1259 * Find and configure the best data interface and NAND timings supported by
1260 * the chip and the driver.
1261 * First tries to retrieve supported timing modes from ONFI information,
1262 * and if the NAND chip does not support ONFI, relies on the
1263 * ->onfi_timing_mode_default specified in the nand_ids table.
1264 *
1265 * Returns 0 for success or negative error code otherwise.
1266 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001267static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001268{
Miquel Raynal97baea12018-03-19 14:47:20 +01001269 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1270 chip->onfi_timing_mode_default,
1271 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001272 int ret;
1273
Miquel Raynal17fa8042017-11-30 18:01:31 +01001274 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001275 return 0;
1276
Miquel Raynal993447b2018-03-19 14:47:21 +01001277 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001278 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001279 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001280 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1281 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001282 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001283 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001284 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001285 }
1286
Miquel Raynal97baea12018-03-19 14:47:20 +01001287 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +02001288 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001289 if (ret)
1290 return ret;
1291
1292 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001293 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001294 return 0;
1295
1296 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001297 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001298 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1299 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001300 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001301 if (ret)
1302 goto err_reset_chip;
1303
1304 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1305 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1306 chip->onfi_timing_mode_default);
1307 goto err_reset_chip;
1308 }
1309
1310 return 0;
1311
1312err_reset_chip:
1313 /*
1314 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1315 * timing mode.
1316 */
1317 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001318 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001319 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001320 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001321
Boris Brezillond8e725d2016-09-15 10:32:50 +02001322 return ret;
1323}
1324
1325/**
1326 * nand_init_data_interface - find the best data interface and timings
1327 * @chip: The NAND chip
1328 *
1329 * Find the best data interface and NAND timings supported by the chip
1330 * and the driver.
1331 * First tries to retrieve supported timing modes from ONFI information,
1332 * and if the NAND chip does not support ONFI, relies on the
1333 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1334 * function nand_chip->data_interface is initialized with the best timing mode
1335 * available.
1336 *
1337 * Returns 0 for success or negative error code otherwise.
1338 */
1339static int nand_init_data_interface(struct nand_chip *chip)
1340{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001341 int modes, mode, ret;
1342
1343 if (!chip->setup_data_interface)
1344 return 0;
1345
1346 /*
1347 * First try to identify the best timings from ONFI parameters and
1348 * if the NAND does not support ONFI, fallback to the default ONFI
1349 * timing mode.
1350 */
1351 modes = onfi_get_async_timing_mode(chip);
1352 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1353 if (!chip->onfi_timing_mode_default)
1354 return 0;
1355
1356 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1357 }
1358
Boris Brezillond8e725d2016-09-15 10:32:50 +02001359
1360 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001361 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001362 if (ret)
1363 continue;
1364
Miquel Raynald787b8b2017-12-22 18:12:41 +01001365 /*
1366 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1367 * controller supports the requested timings.
1368 */
Boris Brezillon858838b2018-09-06 14:05:33 +02001369 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +01001370 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001371 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001372 if (!ret) {
1373 chip->onfi_timing_mode_default = mode;
1374 break;
1375 }
1376 }
1377
1378 return 0;
1379}
1380
Boris Brezillond8e725d2016-09-15 10:32:50 +02001381/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001382 * nand_fill_column_cycles - fill the column cycles of an address
1383 * @chip: The NAND chip
1384 * @addrs: Array of address cycles to fill
1385 * @offset_in_page: The offset in the page
1386 *
1387 * Fills the first or the first two bytes of the @addrs field depending
1388 * on the NAND bus width and the page size.
1389 *
1390 * Returns the number of cycles needed to encode the column, or a negative
1391 * error code in case one of the arguments is invalid.
1392 */
1393static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1394 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Miquel Raynal8878b122017-11-09 14:16:45 +01001396 struct mtd_info *mtd = nand_to_mtd(chip);
1397
1398 /* Make sure the offset is less than the actual page size. */
1399 if (offset_in_page > mtd->writesize + mtd->oobsize)
1400 return -EINVAL;
1401
1402 /*
1403 * On small page NANDs, there's a dedicated command to access the OOB
1404 * area, and the column address is relative to the start of the OOB
1405 * area, not the start of the page. Asjust the address accordingly.
1406 */
1407 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1408 offset_in_page -= mtd->writesize;
1409
1410 /*
1411 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1412 * wide, then it must be divided by 2.
1413 */
1414 if (chip->options & NAND_BUSWIDTH_16) {
1415 if (WARN_ON(offset_in_page % 2))
1416 return -EINVAL;
1417
1418 offset_in_page /= 2;
1419 }
1420
1421 addrs[0] = offset_in_page;
1422
1423 /*
1424 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1425 * need 2
1426 */
1427 if (mtd->writesize <= 512)
1428 return 1;
1429
1430 addrs[1] = offset_in_page >> 8;
1431
1432 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Miquel Raynal8878b122017-11-09 14:16:45 +01001435static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1436 unsigned int offset_in_page, void *buf,
1437 unsigned int len)
1438{
1439 struct mtd_info *mtd = nand_to_mtd(chip);
1440 const struct nand_sdr_timings *sdr =
1441 nand_get_sdr_timings(&chip->data_interface);
1442 u8 addrs[4];
1443 struct nand_op_instr instrs[] = {
1444 NAND_OP_CMD(NAND_CMD_READ0, 0),
1445 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1446 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1447 PSEC_TO_NSEC(sdr->tRR_min)),
1448 NAND_OP_DATA_IN(len, buf, 0),
1449 };
1450 struct nand_operation op = NAND_OPERATION(instrs);
1451 int ret;
1452
1453 /* Drop the DATA_IN instruction if len is set to 0. */
1454 if (!len)
1455 op.ninstrs--;
1456
1457 if (offset_in_page >= mtd->writesize)
1458 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1459 else if (offset_in_page >= 256 &&
1460 !(chip->options & NAND_BUSWIDTH_16))
1461 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1462
1463 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1464 if (ret < 0)
1465 return ret;
1466
1467 addrs[1] = page;
1468 addrs[2] = page >> 8;
1469
1470 if (chip->options & NAND_ROW_ADDR_3) {
1471 addrs[3] = page >> 16;
1472 instrs[1].ctx.addr.naddrs++;
1473 }
1474
1475 return nand_exec_op(chip, &op);
1476}
1477
1478static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1479 unsigned int offset_in_page, void *buf,
1480 unsigned int len)
1481{
1482 const struct nand_sdr_timings *sdr =
1483 nand_get_sdr_timings(&chip->data_interface);
1484 u8 addrs[5];
1485 struct nand_op_instr instrs[] = {
1486 NAND_OP_CMD(NAND_CMD_READ0, 0),
1487 NAND_OP_ADDR(4, addrs, 0),
1488 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1489 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1490 PSEC_TO_NSEC(sdr->tRR_min)),
1491 NAND_OP_DATA_IN(len, buf, 0),
1492 };
1493 struct nand_operation op = NAND_OPERATION(instrs);
1494 int ret;
1495
1496 /* Drop the DATA_IN instruction if len is set to 0. */
1497 if (!len)
1498 op.ninstrs--;
1499
1500 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1501 if (ret < 0)
1502 return ret;
1503
1504 addrs[2] = page;
1505 addrs[3] = page >> 8;
1506
1507 if (chip->options & NAND_ROW_ADDR_3) {
1508 addrs[4] = page >> 16;
1509 instrs[1].ctx.addr.naddrs++;
1510 }
1511
1512 return nand_exec_op(chip, &op);
1513}
1514
1515/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001516 * nand_read_page_op - Do a READ PAGE operation
1517 * @chip: The NAND chip
1518 * @page: page to read
1519 * @offset_in_page: offset within the page
1520 * @buf: buffer used to store the data
1521 * @len: length of the buffer
1522 *
1523 * This function issues a READ PAGE operation.
1524 * This function does not select/unselect the CS line.
1525 *
1526 * Returns 0 on success, a negative error code otherwise.
1527 */
1528int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1529 unsigned int offset_in_page, void *buf, unsigned int len)
1530{
1531 struct mtd_info *mtd = nand_to_mtd(chip);
1532
1533 if (len && !buf)
1534 return -EINVAL;
1535
1536 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1537 return -EINVAL;
1538
Miquel Raynal8878b122017-11-09 14:16:45 +01001539 if (chip->exec_op) {
1540 if (mtd->writesize > 512)
1541 return nand_lp_exec_read_page_op(chip, page,
1542 offset_in_page, buf,
1543 len);
1544
1545 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1546 buf, len);
1547 }
1548
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001549 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001550 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001551 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001552
1553 return 0;
1554}
1555EXPORT_SYMBOL_GPL(nand_read_page_op);
1556
1557/**
1558 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1559 * @chip: The NAND chip
1560 * @page: parameter page to read
1561 * @buf: buffer used to store the data
1562 * @len: length of the buffer
1563 *
1564 * This function issues a READ PARAMETER PAGE operation.
1565 * This function does not select/unselect the CS line.
1566 *
1567 * Returns 0 on success, a negative error code otherwise.
1568 */
1569static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1570 unsigned int len)
1571{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001572 unsigned int i;
1573 u8 *p = buf;
1574
1575 if (len && !buf)
1576 return -EINVAL;
1577
Miquel Raynal8878b122017-11-09 14:16:45 +01001578 if (chip->exec_op) {
1579 const struct nand_sdr_timings *sdr =
1580 nand_get_sdr_timings(&chip->data_interface);
1581 struct nand_op_instr instrs[] = {
1582 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1583 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1584 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1585 PSEC_TO_NSEC(sdr->tRR_min)),
1586 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1587 };
1588 struct nand_operation op = NAND_OPERATION(instrs);
1589
1590 /* Drop the DATA_IN instruction if len is set to 0. */
1591 if (!len)
1592 op.ninstrs--;
1593
1594 return nand_exec_op(chip, &op);
1595 }
1596
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001597 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001598 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001599 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001600
1601 return 0;
1602}
1603
1604/**
1605 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1606 * @chip: The NAND chip
1607 * @offset_in_page: offset within the page
1608 * @buf: buffer used to store the data
1609 * @len: length of the buffer
1610 * @force_8bit: force 8-bit bus access
1611 *
1612 * This function issues a CHANGE READ COLUMN operation.
1613 * This function does not select/unselect the CS line.
1614 *
1615 * Returns 0 on success, a negative error code otherwise.
1616 */
1617int nand_change_read_column_op(struct nand_chip *chip,
1618 unsigned int offset_in_page, void *buf,
1619 unsigned int len, bool force_8bit)
1620{
1621 struct mtd_info *mtd = nand_to_mtd(chip);
1622
1623 if (len && !buf)
1624 return -EINVAL;
1625
1626 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1627 return -EINVAL;
1628
Miquel Raynal8878b122017-11-09 14:16:45 +01001629 /* Small page NANDs do not support column change. */
1630 if (mtd->writesize <= 512)
1631 return -ENOTSUPP;
1632
1633 if (chip->exec_op) {
1634 const struct nand_sdr_timings *sdr =
1635 nand_get_sdr_timings(&chip->data_interface);
1636 u8 addrs[2] = {};
1637 struct nand_op_instr instrs[] = {
1638 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1639 NAND_OP_ADDR(2, addrs, 0),
1640 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1641 PSEC_TO_NSEC(sdr->tCCS_min)),
1642 NAND_OP_DATA_IN(len, buf, 0),
1643 };
1644 struct nand_operation op = NAND_OPERATION(instrs);
1645 int ret;
1646
1647 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1648 if (ret < 0)
1649 return ret;
1650
1651 /* Drop the DATA_IN instruction if len is set to 0. */
1652 if (!len)
1653 op.ninstrs--;
1654
1655 instrs[3].ctx.data.force_8bit = force_8bit;
1656
1657 return nand_exec_op(chip, &op);
1658 }
1659
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001660 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
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_change_read_column_op);
1667
1668/**
1669 * nand_read_oob_op - Do a READ OOB operation
1670 * @chip: The NAND chip
1671 * @page: page to read
1672 * @offset_in_oob: offset within the OOB area
1673 * @buf: buffer used to store the data
1674 * @len: length of the buffer
1675 *
1676 * This function issues a READ OOB operation.
1677 * This function does not select/unselect the CS line.
1678 *
1679 * Returns 0 on success, a negative error code otherwise.
1680 */
1681int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1682 unsigned int offset_in_oob, void *buf, unsigned int len)
1683{
1684 struct mtd_info *mtd = nand_to_mtd(chip);
1685
1686 if (len && !buf)
1687 return -EINVAL;
1688
1689 if (offset_in_oob + len > mtd->oobsize)
1690 return -EINVAL;
1691
Miquel Raynal8878b122017-11-09 14:16:45 +01001692 if (chip->exec_op)
1693 return nand_read_page_op(chip, page,
1694 mtd->writesize + offset_in_oob,
1695 buf, len);
1696
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001697 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001698 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001699 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001700
1701 return 0;
1702}
1703EXPORT_SYMBOL_GPL(nand_read_oob_op);
1704
Miquel Raynal8878b122017-11-09 14:16:45 +01001705static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1706 unsigned int offset_in_page, const void *buf,
1707 unsigned int len, bool prog)
1708{
1709 struct mtd_info *mtd = nand_to_mtd(chip);
1710 const struct nand_sdr_timings *sdr =
1711 nand_get_sdr_timings(&chip->data_interface);
1712 u8 addrs[5] = {};
1713 struct nand_op_instr instrs[] = {
1714 /*
1715 * The first instruction will be dropped if we're dealing
1716 * with a large page NAND and adjusted if we're dealing
1717 * with a small page NAND and the page offset is > 255.
1718 */
1719 NAND_OP_CMD(NAND_CMD_READ0, 0),
1720 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1721 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1722 NAND_OP_DATA_OUT(len, buf, 0),
1723 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1724 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1725 };
1726 struct nand_operation op = NAND_OPERATION(instrs);
1727 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1728 int ret;
1729 u8 status;
1730
1731 if (naddrs < 0)
1732 return naddrs;
1733
1734 addrs[naddrs++] = page;
1735 addrs[naddrs++] = page >> 8;
1736 if (chip->options & NAND_ROW_ADDR_3)
1737 addrs[naddrs++] = page >> 16;
1738
1739 instrs[2].ctx.addr.naddrs = naddrs;
1740
1741 /* Drop the last two instructions if we're not programming the page. */
1742 if (!prog) {
1743 op.ninstrs -= 2;
1744 /* Also drop the DATA_OUT instruction if empty. */
1745 if (!len)
1746 op.ninstrs--;
1747 }
1748
1749 if (mtd->writesize <= 512) {
1750 /*
1751 * Small pages need some more tweaking: we have to adjust the
1752 * first instruction depending on the page offset we're trying
1753 * to access.
1754 */
1755 if (offset_in_page >= mtd->writesize)
1756 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1757 else if (offset_in_page >= 256 &&
1758 !(chip->options & NAND_BUSWIDTH_16))
1759 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1760 } else {
1761 /*
1762 * Drop the first command if we're dealing with a large page
1763 * NAND.
1764 */
1765 op.instrs++;
1766 op.ninstrs--;
1767 }
1768
1769 ret = nand_exec_op(chip, &op);
1770 if (!prog || ret)
1771 return ret;
1772
1773 ret = nand_status_op(chip, &status);
1774 if (ret)
1775 return ret;
1776
1777 return status;
1778}
1779
Boris Brezillon97d90da2017-11-30 18:01:29 +01001780/**
1781 * nand_prog_page_begin_op - starts a PROG PAGE operation
1782 * @chip: The NAND chip
1783 * @page: page to write
1784 * @offset_in_page: offset within the page
1785 * @buf: buffer containing the data to write to the page
1786 * @len: length of the buffer
1787 *
1788 * This function issues the first half of a PROG PAGE operation.
1789 * This function does not select/unselect the CS line.
1790 *
1791 * Returns 0 on success, a negative error code otherwise.
1792 */
1793int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1794 unsigned int offset_in_page, const void *buf,
1795 unsigned int len)
1796{
1797 struct mtd_info *mtd = nand_to_mtd(chip);
1798
1799 if (len && !buf)
1800 return -EINVAL;
1801
1802 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1803 return -EINVAL;
1804
Miquel Raynal8878b122017-11-09 14:16:45 +01001805 if (chip->exec_op)
1806 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1807 len, false);
1808
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001809 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001810
1811 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001812 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001813
1814 return 0;
1815}
1816EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1817
1818/**
1819 * nand_prog_page_end_op - ends a PROG PAGE operation
1820 * @chip: The NAND chip
1821 *
1822 * This function issues the second half of a PROG PAGE operation.
1823 * This function does not select/unselect the CS line.
1824 *
1825 * Returns 0 on success, a negative error code otherwise.
1826 */
1827int nand_prog_page_end_op(struct nand_chip *chip)
1828{
Miquel Raynal8878b122017-11-09 14:16:45 +01001829 int ret;
1830 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001831
Miquel Raynal8878b122017-11-09 14:16:45 +01001832 if (chip->exec_op) {
1833 const struct nand_sdr_timings *sdr =
1834 nand_get_sdr_timings(&chip->data_interface);
1835 struct nand_op_instr instrs[] = {
1836 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1837 PSEC_TO_NSEC(sdr->tWB_max)),
1838 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1839 };
1840 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001841
Miquel Raynal8878b122017-11-09 14:16:45 +01001842 ret = nand_exec_op(chip, &op);
1843 if (ret)
1844 return ret;
1845
1846 ret = nand_status_op(chip, &status);
1847 if (ret)
1848 return ret;
1849 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001850 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001851 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001852 if (ret < 0)
1853 return ret;
1854
1855 status = ret;
1856 }
1857
Boris Brezillon97d90da2017-11-30 18:01:29 +01001858 if (status & NAND_STATUS_FAIL)
1859 return -EIO;
1860
1861 return 0;
1862}
1863EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1864
1865/**
1866 * nand_prog_page_op - Do a full PROG PAGE operation
1867 * @chip: The NAND chip
1868 * @page: page to write
1869 * @offset_in_page: offset within the page
1870 * @buf: buffer containing the data to write to the page
1871 * @len: length of the buffer
1872 *
1873 * This function issues a full PROG PAGE operation.
1874 * This function does not select/unselect the CS line.
1875 *
1876 * Returns 0 on success, a negative error code otherwise.
1877 */
1878int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1879 unsigned int offset_in_page, const void *buf,
1880 unsigned int len)
1881{
1882 struct mtd_info *mtd = nand_to_mtd(chip);
1883 int status;
1884
1885 if (!len || !buf)
1886 return -EINVAL;
1887
1888 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1889 return -EINVAL;
1890
Miquel Raynal8878b122017-11-09 14:16:45 +01001891 if (chip->exec_op) {
1892 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1893 len, true);
1894 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001895 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1896 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001897 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001898 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001899 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001900 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001901
Boris Brezillon97d90da2017-11-30 18:01:29 +01001902 if (status & NAND_STATUS_FAIL)
1903 return -EIO;
1904
1905 return 0;
1906}
1907EXPORT_SYMBOL_GPL(nand_prog_page_op);
1908
1909/**
1910 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1911 * @chip: The NAND chip
1912 * @offset_in_page: offset within the page
1913 * @buf: buffer containing the data to send to the NAND
1914 * @len: length of the buffer
1915 * @force_8bit: force 8-bit bus access
1916 *
1917 * This function issues a CHANGE WRITE COLUMN operation.
1918 * This function does not select/unselect the CS line.
1919 *
1920 * Returns 0 on success, a negative error code otherwise.
1921 */
1922int nand_change_write_column_op(struct nand_chip *chip,
1923 unsigned int offset_in_page,
1924 const void *buf, unsigned int len,
1925 bool force_8bit)
1926{
1927 struct mtd_info *mtd = nand_to_mtd(chip);
1928
1929 if (len && !buf)
1930 return -EINVAL;
1931
1932 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1933 return -EINVAL;
1934
Miquel Raynal8878b122017-11-09 14:16:45 +01001935 /* Small page NANDs do not support column change. */
1936 if (mtd->writesize <= 512)
1937 return -ENOTSUPP;
1938
1939 if (chip->exec_op) {
1940 const struct nand_sdr_timings *sdr =
1941 nand_get_sdr_timings(&chip->data_interface);
1942 u8 addrs[2];
1943 struct nand_op_instr instrs[] = {
1944 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1945 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1946 NAND_OP_DATA_OUT(len, buf, 0),
1947 };
1948 struct nand_operation op = NAND_OPERATION(instrs);
1949 int ret;
1950
1951 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1952 if (ret < 0)
1953 return ret;
1954
1955 instrs[2].ctx.data.force_8bit = force_8bit;
1956
1957 /* Drop the DATA_OUT instruction if len is set to 0. */
1958 if (!len)
1959 op.ninstrs--;
1960
1961 return nand_exec_op(chip, &op);
1962 }
1963
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001964 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001965 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001966 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001967
1968 return 0;
1969}
1970EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1971
1972/**
1973 * nand_readid_op - Do a READID operation
1974 * @chip: The NAND chip
1975 * @addr: address cycle to pass after the READID command
1976 * @buf: buffer used to store the ID
1977 * @len: length of the buffer
1978 *
1979 * This function sends a READID command and reads back the ID returned by the
1980 * NAND.
1981 * This function does not select/unselect the CS line.
1982 *
1983 * Returns 0 on success, a negative error code otherwise.
1984 */
1985int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1986 unsigned int len)
1987{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001988 unsigned int i;
1989 u8 *id = buf;
1990
1991 if (len && !buf)
1992 return -EINVAL;
1993
Miquel Raynal8878b122017-11-09 14:16:45 +01001994 if (chip->exec_op) {
1995 const struct nand_sdr_timings *sdr =
1996 nand_get_sdr_timings(&chip->data_interface);
1997 struct nand_op_instr instrs[] = {
1998 NAND_OP_CMD(NAND_CMD_READID, 0),
1999 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
2000 NAND_OP_8BIT_DATA_IN(len, buf, 0),
2001 };
2002 struct nand_operation op = NAND_OPERATION(instrs);
2003
2004 /* Drop the DATA_IN instruction if len is set to 0. */
2005 if (!len)
2006 op.ninstrs--;
2007
2008 return nand_exec_op(chip, &op);
2009 }
2010
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002011 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002012
2013 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002014 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002015
2016 return 0;
2017}
2018EXPORT_SYMBOL_GPL(nand_readid_op);
2019
2020/**
2021 * nand_status_op - Do a STATUS operation
2022 * @chip: The NAND chip
2023 * @status: out variable to store the NAND status
2024 *
2025 * This function sends a STATUS command and reads back the status returned by
2026 * the NAND.
2027 * This function does not select/unselect the CS line.
2028 *
2029 * Returns 0 on success, a negative error code otherwise.
2030 */
2031int nand_status_op(struct nand_chip *chip, u8 *status)
2032{
Miquel Raynal8878b122017-11-09 14:16:45 +01002033 if (chip->exec_op) {
2034 const struct nand_sdr_timings *sdr =
2035 nand_get_sdr_timings(&chip->data_interface);
2036 struct nand_op_instr instrs[] = {
2037 NAND_OP_CMD(NAND_CMD_STATUS,
2038 PSEC_TO_NSEC(sdr->tADL_min)),
2039 NAND_OP_8BIT_DATA_IN(1, status, 0),
2040 };
2041 struct nand_operation op = NAND_OPERATION(instrs);
2042
2043 if (!status)
2044 op.ninstrs--;
2045
2046 return nand_exec_op(chip, &op);
2047 }
2048
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002049 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002050 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002051 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002052
2053 return 0;
2054}
2055EXPORT_SYMBOL_GPL(nand_status_op);
2056
2057/**
2058 * nand_exit_status_op - Exit a STATUS operation
2059 * @chip: The NAND chip
2060 *
2061 * This function sends a READ0 command to cancel the effect of the STATUS
2062 * command to avoid reading only the status until a new read command is sent.
2063 *
2064 * This function does not select/unselect the CS line.
2065 *
2066 * Returns 0 on success, a negative error code otherwise.
2067 */
2068int nand_exit_status_op(struct nand_chip *chip)
2069{
Miquel Raynal8878b122017-11-09 14:16:45 +01002070 if (chip->exec_op) {
2071 struct nand_op_instr instrs[] = {
2072 NAND_OP_CMD(NAND_CMD_READ0, 0),
2073 };
2074 struct nand_operation op = NAND_OPERATION(instrs);
2075
2076 return nand_exec_op(chip, &op);
2077 }
2078
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002079 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002080
2081 return 0;
2082}
2083EXPORT_SYMBOL_GPL(nand_exit_status_op);
2084
2085/**
2086 * nand_erase_op - Do an erase operation
2087 * @chip: The NAND chip
2088 * @eraseblock: block to erase
2089 *
2090 * This function sends an ERASE command and waits for the NAND to be ready
2091 * before returning.
2092 * This function does not select/unselect the CS line.
2093 *
2094 * Returns 0 on success, a negative error code otherwise.
2095 */
2096int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2097{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002098 unsigned int page = eraseblock <<
2099 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002100 int ret;
2101 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002102
Miquel Raynal8878b122017-11-09 14:16:45 +01002103 if (chip->exec_op) {
2104 const struct nand_sdr_timings *sdr =
2105 nand_get_sdr_timings(&chip->data_interface);
2106 u8 addrs[3] = { page, page >> 8, page >> 16 };
2107 struct nand_op_instr instrs[] = {
2108 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2109 NAND_OP_ADDR(2, addrs, 0),
2110 NAND_OP_CMD(NAND_CMD_ERASE2,
2111 PSEC_TO_MSEC(sdr->tWB_max)),
2112 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2113 };
2114 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002115
Miquel Raynal8878b122017-11-09 14:16:45 +01002116 if (chip->options & NAND_ROW_ADDR_3)
2117 instrs[1].ctx.addr.naddrs++;
2118
2119 ret = nand_exec_op(chip, &op);
2120 if (ret)
2121 return ret;
2122
2123 ret = nand_status_op(chip, &status);
2124 if (ret)
2125 return ret;
2126 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002127 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2128 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002129
Boris Brezillon8395b752018-09-07 00:38:37 +02002130 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002131 if (ret < 0)
2132 return ret;
2133
2134 status = ret;
2135 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002136
2137 if (status & NAND_STATUS_FAIL)
2138 return -EIO;
2139
2140 return 0;
2141}
2142EXPORT_SYMBOL_GPL(nand_erase_op);
2143
2144/**
2145 * nand_set_features_op - Do a SET FEATURES operation
2146 * @chip: The NAND chip
2147 * @feature: feature id
2148 * @data: 4 bytes of data
2149 *
2150 * This function sends a SET FEATURES command and waits for the NAND to be
2151 * ready before returning.
2152 * This function does not select/unselect the CS line.
2153 *
2154 * Returns 0 on success, a negative error code otherwise.
2155 */
2156static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2157 const void *data)
2158{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002159 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002160 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002161
Miquel Raynal8878b122017-11-09 14:16:45 +01002162 if (chip->exec_op) {
2163 const struct nand_sdr_timings *sdr =
2164 nand_get_sdr_timings(&chip->data_interface);
2165 struct nand_op_instr instrs[] = {
2166 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2167 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2168 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2169 PSEC_TO_NSEC(sdr->tWB_max)),
2170 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2171 };
2172 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002173
Boris Brezillon782d1962018-05-11 14:44:07 +02002174 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002175 }
2176
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002177 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002178 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002179 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002180
Boris Brezillon8395b752018-09-07 00:38:37 +02002181 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002182 if (ret < 0)
2183 return ret;
2184
2185 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002186 return -EIO;
2187
2188 return 0;
2189}
2190
2191/**
2192 * nand_get_features_op - Do a GET FEATURES operation
2193 * @chip: The NAND chip
2194 * @feature: feature id
2195 * @data: 4 bytes of data
2196 *
2197 * This function sends a GET FEATURES command and waits for the NAND to be
2198 * ready before returning.
2199 * This function does not select/unselect the CS line.
2200 *
2201 * Returns 0 on success, a negative error code otherwise.
2202 */
2203static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2204 void *data)
2205{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002206 u8 *params = data;
2207 int i;
2208
Miquel Raynal8878b122017-11-09 14:16:45 +01002209 if (chip->exec_op) {
2210 const struct nand_sdr_timings *sdr =
2211 nand_get_sdr_timings(&chip->data_interface);
2212 struct nand_op_instr instrs[] = {
2213 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2214 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2215 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2216 PSEC_TO_NSEC(sdr->tRR_min)),
2217 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2218 data, 0),
2219 };
2220 struct nand_operation op = NAND_OPERATION(instrs);
2221
2222 return nand_exec_op(chip, &op);
2223 }
2224
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002225 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002226 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002227 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002228
2229 return 0;
2230}
2231
Boris Brezillon52f05b62018-07-27 09:44:18 +02002232static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2233 unsigned int delay_ns)
2234{
2235 if (chip->exec_op) {
2236 struct nand_op_instr instrs[] = {
2237 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2238 PSEC_TO_NSEC(delay_ns)),
2239 };
2240 struct nand_operation op = NAND_OPERATION(instrs);
2241
2242 return nand_exec_op(chip, &op);
2243 }
2244
2245 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02002246 if (!chip->legacy.dev_ready)
Boris Brezillon52f05b62018-07-27 09:44:18 +02002247 udelay(chip->chip_delay);
2248 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002249 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002250
2251 return 0;
2252}
2253
Boris Brezillon97d90da2017-11-30 18:01:29 +01002254/**
2255 * nand_reset_op - Do a reset operation
2256 * @chip: The NAND chip
2257 *
2258 * This function sends a RESET command and waits for the NAND to be ready
2259 * before returning.
2260 * This function does not select/unselect the CS line.
2261 *
2262 * Returns 0 on success, a negative error code otherwise.
2263 */
2264int nand_reset_op(struct nand_chip *chip)
2265{
Miquel Raynal8878b122017-11-09 14:16:45 +01002266 if (chip->exec_op) {
2267 const struct nand_sdr_timings *sdr =
2268 nand_get_sdr_timings(&chip->data_interface);
2269 struct nand_op_instr instrs[] = {
2270 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2271 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2272 };
2273 struct nand_operation op = NAND_OPERATION(instrs);
2274
2275 return nand_exec_op(chip, &op);
2276 }
2277
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002278 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002279
2280 return 0;
2281}
2282EXPORT_SYMBOL_GPL(nand_reset_op);
2283
2284/**
2285 * nand_read_data_op - Read data from the NAND
2286 * @chip: The NAND chip
2287 * @buf: buffer used to store the data
2288 * @len: length of the buffer
2289 * @force_8bit: force 8-bit bus access
2290 *
2291 * This function does a raw data read on the bus. Usually used after launching
2292 * another NAND operation like nand_read_page_op().
2293 * This function does not select/unselect the CS line.
2294 *
2295 * Returns 0 on success, a negative error code otherwise.
2296 */
2297int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2298 bool force_8bit)
2299{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002300 if (!len || !buf)
2301 return -EINVAL;
2302
Miquel Raynal8878b122017-11-09 14:16:45 +01002303 if (chip->exec_op) {
2304 struct nand_op_instr instrs[] = {
2305 NAND_OP_DATA_IN(len, buf, 0),
2306 };
2307 struct nand_operation op = NAND_OPERATION(instrs);
2308
2309 instrs[0].ctx.data.force_8bit = force_8bit;
2310
2311 return nand_exec_op(chip, &op);
2312 }
2313
Boris Brezillon97d90da2017-11-30 18:01:29 +01002314 if (force_8bit) {
2315 u8 *p = buf;
2316 unsigned int i;
2317
2318 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002319 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002320 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002321 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002322 }
2323
2324 return 0;
2325}
2326EXPORT_SYMBOL_GPL(nand_read_data_op);
2327
2328/**
2329 * nand_write_data_op - Write data from the NAND
2330 * @chip: The NAND chip
2331 * @buf: buffer containing the data to send on the bus
2332 * @len: length of the buffer
2333 * @force_8bit: force 8-bit bus access
2334 *
2335 * This function does a raw data write on the bus. Usually used after launching
2336 * another NAND operation like nand_write_page_begin_op().
2337 * This function does not select/unselect the CS line.
2338 *
2339 * Returns 0 on success, a negative error code otherwise.
2340 */
2341int nand_write_data_op(struct nand_chip *chip, const void *buf,
2342 unsigned int len, bool force_8bit)
2343{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002344 if (!len || !buf)
2345 return -EINVAL;
2346
Miquel Raynal8878b122017-11-09 14:16:45 +01002347 if (chip->exec_op) {
2348 struct nand_op_instr instrs[] = {
2349 NAND_OP_DATA_OUT(len, buf, 0),
2350 };
2351 struct nand_operation op = NAND_OPERATION(instrs);
2352
2353 instrs[0].ctx.data.force_8bit = force_8bit;
2354
2355 return nand_exec_op(chip, &op);
2356 }
2357
Boris Brezillon97d90da2017-11-30 18:01:29 +01002358 if (force_8bit) {
2359 const u8 *p = buf;
2360 unsigned int i;
2361
2362 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002363 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002364 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002365 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002366 }
2367
2368 return 0;
2369}
2370EXPORT_SYMBOL_GPL(nand_write_data_op);
2371
2372/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002373 * struct nand_op_parser_ctx - Context used by the parser
2374 * @instrs: array of all the instructions that must be addressed
2375 * @ninstrs: length of the @instrs array
2376 * @subop: Sub-operation to be passed to the NAND controller
2377 *
2378 * This structure is used by the core to split NAND operations into
2379 * sub-operations that can be handled by the NAND controller.
2380 */
2381struct nand_op_parser_ctx {
2382 const struct nand_op_instr *instrs;
2383 unsigned int ninstrs;
2384 struct nand_subop subop;
2385};
2386
2387/**
2388 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2389 * @pat: the parser pattern element that matches @instr
2390 * @instr: pointer to the instruction to check
2391 * @start_offset: this is an in/out parameter. If @instr has already been
2392 * split, then @start_offset is the offset from which to start
2393 * (either an address cycle or an offset in the data buffer).
2394 * Conversely, if the function returns true (ie. instr must be
2395 * split), this parameter is updated to point to the first
2396 * data/address cycle that has not been taken care of.
2397 *
2398 * Some NAND controllers are limited and cannot send X address cycles with a
2399 * unique operation, or cannot read/write more than Y bytes at the same time.
2400 * In this case, split the instruction that does not fit in a single
2401 * controller-operation into two or more chunks.
2402 *
2403 * Returns true if the instruction must be split, false otherwise.
2404 * The @start_offset parameter is also updated to the offset at which the next
2405 * bundle of instruction must start (if an address or a data instruction).
2406 */
2407static bool
2408nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2409 const struct nand_op_instr *instr,
2410 unsigned int *start_offset)
2411{
2412 switch (pat->type) {
2413 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002414 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002415 break;
2416
2417 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002418 pat->ctx.addr.maxcycles) {
2419 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002420 return true;
2421 }
2422 break;
2423
2424 case NAND_OP_DATA_IN_INSTR:
2425 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002426 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002427 break;
2428
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002429 if (instr->ctx.data.len - *start_offset >
2430 pat->ctx.data.maxlen) {
2431 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002432 return true;
2433 }
2434 break;
2435
2436 default:
2437 break;
2438 }
2439
2440 return false;
2441}
2442
2443/**
2444 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2445 * remaining in the parser context
2446 * @pat: the pattern to test
2447 * @ctx: the parser context structure to match with the pattern @pat
2448 *
2449 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2450 * Returns true if this is the case, false ortherwise. When true is returned,
2451 * @ctx->subop is updated with the set of instructions to be passed to the
2452 * controller driver.
2453 */
2454static bool
2455nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2456 struct nand_op_parser_ctx *ctx)
2457{
2458 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2459 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2460 const struct nand_op_instr *instr = ctx->subop.instrs;
2461 unsigned int i, ninstrs;
2462
2463 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2464 /*
2465 * The pattern instruction does not match the operation
2466 * instruction. If the instruction is marked optional in the
2467 * pattern definition, we skip the pattern element and continue
2468 * to the next one. If the element is mandatory, there's no
2469 * match and we can return false directly.
2470 */
2471 if (instr->type != pat->elems[i].type) {
2472 if (!pat->elems[i].optional)
2473 return false;
2474
2475 continue;
2476 }
2477
2478 /*
2479 * Now check the pattern element constraints. If the pattern is
2480 * not able to handle the whole instruction in a single step,
2481 * we have to split it.
2482 * The last_instr_end_off value comes back updated to point to
2483 * the position where we have to split the instruction (the
2484 * start of the next subop chunk).
2485 */
2486 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2487 &instr_offset)) {
2488 ninstrs++;
2489 i++;
2490 break;
2491 }
2492
2493 instr++;
2494 ninstrs++;
2495 instr_offset = 0;
2496 }
2497
2498 /*
2499 * This can happen if all instructions of a pattern are optional.
2500 * Still, if there's not at least one instruction handled by this
2501 * pattern, this is not a match, and we should try the next one (if
2502 * any).
2503 */
2504 if (!ninstrs)
2505 return false;
2506
2507 /*
2508 * We had a match on the pattern head, but the pattern may be longer
2509 * than the instructions we're asked to execute. We need to make sure
2510 * there's no mandatory elements in the pattern tail.
2511 */
2512 for (; i < pat->nelems; i++) {
2513 if (!pat->elems[i].optional)
2514 return false;
2515 }
2516
2517 /*
2518 * We have a match: update the subop structure accordingly and return
2519 * true.
2520 */
2521 ctx->subop.ninstrs = ninstrs;
2522 ctx->subop.last_instr_end_off = instr_offset;
2523
2524 return true;
2525}
2526
2527#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2528static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2529{
2530 const struct nand_op_instr *instr;
2531 char *prefix = " ";
2532 unsigned int i;
2533
2534 pr_debug("executing subop:\n");
2535
2536 for (i = 0; i < ctx->ninstrs; i++) {
2537 instr = &ctx->instrs[i];
2538
2539 if (instr == &ctx->subop.instrs[0])
2540 prefix = " ->";
2541
2542 switch (instr->type) {
2543 case NAND_OP_CMD_INSTR:
2544 pr_debug("%sCMD [0x%02x]\n", prefix,
2545 instr->ctx.cmd.opcode);
2546 break;
2547 case NAND_OP_ADDR_INSTR:
2548 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2549 instr->ctx.addr.naddrs,
2550 instr->ctx.addr.naddrs < 64 ?
2551 instr->ctx.addr.naddrs : 64,
2552 instr->ctx.addr.addrs);
2553 break;
2554 case NAND_OP_DATA_IN_INSTR:
2555 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2556 instr->ctx.data.len,
2557 instr->ctx.data.force_8bit ?
2558 ", force 8-bit" : "");
2559 break;
2560 case NAND_OP_DATA_OUT_INSTR:
2561 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2562 instr->ctx.data.len,
2563 instr->ctx.data.force_8bit ?
2564 ", force 8-bit" : "");
2565 break;
2566 case NAND_OP_WAITRDY_INSTR:
2567 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2568 instr->ctx.waitrdy.timeout_ms);
2569 break;
2570 }
2571
2572 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2573 prefix = " ";
2574 }
2575}
2576#else
2577static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2578{
2579 /* NOP */
2580}
2581#endif
2582
2583/**
2584 * nand_op_parser_exec_op - exec_op parser
2585 * @chip: the NAND chip
2586 * @parser: patterns description provided by the controller driver
2587 * @op: the NAND operation to address
2588 * @check_only: when true, the function only checks if @op can be handled but
2589 * does not execute the operation
2590 *
2591 * Helper function designed to ease integration of NAND controller drivers that
2592 * only support a limited set of instruction sequences. The supported sequences
2593 * are described in @parser, and the framework takes care of splitting @op into
2594 * multiple sub-operations (if required) and pass them back to the ->exec()
2595 * callback of the matching pattern if @check_only is set to false.
2596 *
2597 * NAND controller drivers should call this function from their own ->exec_op()
2598 * implementation.
2599 *
2600 * Returns 0 on success, a negative error code otherwise. A failure can be
2601 * caused by an unsupported operation (none of the supported patterns is able
2602 * to handle the requested operation), or an error returned by one of the
2603 * matching pattern->exec() hook.
2604 */
2605int nand_op_parser_exec_op(struct nand_chip *chip,
2606 const struct nand_op_parser *parser,
2607 const struct nand_operation *op, bool check_only)
2608{
2609 struct nand_op_parser_ctx ctx = {
2610 .subop.instrs = op->instrs,
2611 .instrs = op->instrs,
2612 .ninstrs = op->ninstrs,
2613 };
2614 unsigned int i;
2615
2616 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2617 int ret;
2618
2619 for (i = 0; i < parser->npatterns; i++) {
2620 const struct nand_op_parser_pattern *pattern;
2621
2622 pattern = &parser->patterns[i];
2623 if (!nand_op_parser_match_pat(pattern, &ctx))
2624 continue;
2625
2626 nand_op_parser_trace(&ctx);
2627
2628 if (check_only)
2629 break;
2630
2631 ret = pattern->exec(chip, &ctx.subop);
2632 if (ret)
2633 return ret;
2634
2635 break;
2636 }
2637
2638 if (i == parser->npatterns) {
2639 pr_debug("->exec_op() parser: pattern not found!\n");
2640 return -ENOTSUPP;
2641 }
2642
2643 /*
2644 * Update the context structure by pointing to the start of the
2645 * next subop.
2646 */
2647 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2648 if (ctx.subop.last_instr_end_off)
2649 ctx.subop.instrs -= 1;
2650
2651 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2652 }
2653
2654 return 0;
2655}
2656EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2657
2658static bool nand_instr_is_data(const struct nand_op_instr *instr)
2659{
2660 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2661 instr->type == NAND_OP_DATA_OUT_INSTR);
2662}
2663
2664static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2665 unsigned int instr_idx)
2666{
2667 return subop && instr_idx < subop->ninstrs;
2668}
2669
Miquel Raynal760c4352018-07-19 00:09:12 +02002670static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2671 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002672{
2673 if (instr_idx)
2674 return 0;
2675
2676 return subop->first_instr_start_off;
2677}
2678
2679/**
2680 * nand_subop_get_addr_start_off - Get the start offset in an address array
2681 * @subop: The entire sub-operation
2682 * @instr_idx: Index of the instruction inside the sub-operation
2683 *
2684 * During driver development, one could be tempted to directly use the
2685 * ->addr.addrs field of address instructions. This is wrong as address
2686 * instructions might be split.
2687 *
2688 * Given an address instruction, returns the offset of the first cycle to issue.
2689 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002690unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2691 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002692{
Miquel Raynal760c4352018-07-19 00:09:12 +02002693 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2694 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2695 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002696
2697 return nand_subop_get_start_off(subop, instr_idx);
2698}
2699EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2700
2701/**
2702 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2703 * @subop: The entire sub-operation
2704 * @instr_idx: Index of the instruction inside the sub-operation
2705 *
2706 * During driver development, one could be tempted to directly use the
2707 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2708 * might be split.
2709 *
2710 * Given an address instruction, returns the number of address cycle to issue.
2711 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002712unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2713 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002714{
2715 int start_off, end_off;
2716
Miquel Raynal760c4352018-07-19 00:09:12 +02002717 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2718 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2719 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002720
2721 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2722
2723 if (instr_idx == subop->ninstrs - 1 &&
2724 subop->last_instr_end_off)
2725 end_off = subop->last_instr_end_off;
2726 else
2727 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2728
2729 return end_off - start_off;
2730}
2731EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2732
2733/**
2734 * nand_subop_get_data_start_off - Get the start offset in a data array
2735 * @subop: The entire sub-operation
2736 * @instr_idx: Index of the instruction inside the sub-operation
2737 *
2738 * During driver development, one could be tempted to directly use the
2739 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2740 * instructions might be split.
2741 *
2742 * Given a data instruction, returns the offset to start from.
2743 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002744unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2745 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002746{
Miquel Raynal760c4352018-07-19 00:09:12 +02002747 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2748 !nand_instr_is_data(&subop->instrs[instr_idx])))
2749 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002750
2751 return nand_subop_get_start_off(subop, instr_idx);
2752}
2753EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2754
2755/**
2756 * nand_subop_get_data_len - Get the number of bytes to retrieve
2757 * @subop: The entire sub-operation
2758 * @instr_idx: Index of the instruction inside the sub-operation
2759 *
2760 * During driver development, one could be tempted to directly use the
2761 * ->data->len field of a data instruction. This is wrong as data instructions
2762 * might be split.
2763 *
2764 * Returns the length of the chunk of data to send/receive.
2765 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002766unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2767 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002768{
2769 int start_off = 0, end_off;
2770
Miquel Raynal760c4352018-07-19 00:09:12 +02002771 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2772 !nand_instr_is_data(&subop->instrs[instr_idx])))
2773 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002774
2775 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2776
2777 if (instr_idx == subop->ninstrs - 1 &&
2778 subop->last_instr_end_off)
2779 end_off = subop->last_instr_end_off;
2780 else
2781 end_off = subop->instrs[instr_idx].ctx.data.len;
2782
2783 return end_off - start_off;
2784}
2785EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002788 * nand_reset - Reset and initialize a NAND device
2789 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002790 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002791 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002792 * Save the timings data structure, then apply SDR timings mode 0 (see
2793 * nand_reset_data_interface for details), do the reset operation, and
2794 * apply back the previous timings.
2795 *
2796 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002797 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002798int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002799{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002800 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002801 int ret;
2802
Boris Brezillon104e4422017-03-16 09:35:58 +01002803 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002804 if (ret)
2805 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002806
Boris Brezillon73f907f2016-10-24 16:46:20 +02002807 /*
2808 * The CS line has to be released before we can apply the new NAND
2809 * interface settings, hence this weird ->select_chip() dance.
2810 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002811 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002812 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002813 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002814 if (ret)
2815 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002816
Miquel Raynal107b7d62018-03-19 14:47:25 +01002817 /*
2818 * A nand_reset_data_interface() put both the NAND chip and the NAND
2819 * controller in timings mode 0. If the default mode for this chip is
2820 * also 0, no need to proceed to the change again. Plus, at probe time,
2821 * nand_setup_data_interface() uses ->set/get_features() which would
2822 * fail anyway as the parameter page is not available yet.
2823 */
2824 if (!chip->onfi_timing_mode_default)
2825 return 0;
2826
Miquel Raynal17fa8042017-11-30 18:01:31 +01002827 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002828 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002829 if (ret)
2830 return ret;
2831
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002832 return 0;
2833}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002834EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002835
2836/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002837 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2838 * @buf: buffer to test
2839 * @len: buffer length
2840 * @bitflips_threshold: maximum number of bitflips
2841 *
2842 * Check if a buffer contains only 0xff, which means the underlying region
2843 * has been erased and is ready to be programmed.
2844 * The bitflips_threshold specify the maximum number of bitflips before
2845 * considering the region is not erased.
2846 * Note: The logic of this function has been extracted from the memweight
2847 * implementation, except that nand_check_erased_buf function exit before
2848 * testing the whole buffer if the number of bitflips exceed the
2849 * bitflips_threshold value.
2850 *
2851 * Returns a positive number of bitflips less than or equal to
2852 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2853 * threshold.
2854 */
2855static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2856{
2857 const unsigned char *bitmap = buf;
2858 int bitflips = 0;
2859 int weight;
2860
2861 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2862 len--, bitmap++) {
2863 weight = hweight8(*bitmap);
2864 bitflips += BITS_PER_BYTE - weight;
2865 if (unlikely(bitflips > bitflips_threshold))
2866 return -EBADMSG;
2867 }
2868
2869 for (; len >= sizeof(long);
2870 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002871 unsigned long d = *((unsigned long *)bitmap);
2872 if (d == ~0UL)
2873 continue;
2874 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002875 bitflips += BITS_PER_LONG - weight;
2876 if (unlikely(bitflips > bitflips_threshold))
2877 return -EBADMSG;
2878 }
2879
2880 for (; len > 0; len--, bitmap++) {
2881 weight = hweight8(*bitmap);
2882 bitflips += BITS_PER_BYTE - weight;
2883 if (unlikely(bitflips > bitflips_threshold))
2884 return -EBADMSG;
2885 }
2886
2887 return bitflips;
2888}
2889
2890/**
2891 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2892 * 0xff data
2893 * @data: data buffer to test
2894 * @datalen: data length
2895 * @ecc: ECC buffer
2896 * @ecclen: ECC length
2897 * @extraoob: extra OOB buffer
2898 * @extraooblen: extra OOB length
2899 * @bitflips_threshold: maximum number of bitflips
2900 *
2901 * Check if a data buffer and its associated ECC and OOB data contains only
2902 * 0xff pattern, which means the underlying region has been erased and is
2903 * ready to be programmed.
2904 * The bitflips_threshold specify the maximum number of bitflips before
2905 * considering the region as not erased.
2906 *
2907 * Note:
2908 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2909 * different from the NAND page size. When fixing bitflips, ECC engines will
2910 * report the number of errors per chunk, and the NAND core infrastructure
2911 * expect you to return the maximum number of bitflips for the whole page.
2912 * This is why you should always use this function on a single chunk and
2913 * not on the whole page. After checking each chunk you should update your
2914 * max_bitflips value accordingly.
2915 * 2/ When checking for bitflips in erased pages you should not only check
2916 * the payload data but also their associated ECC data, because a user might
2917 * have programmed almost all bits to 1 but a few. In this case, we
2918 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2919 * this case.
2920 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2921 * data are protected by the ECC engine.
2922 * It could also be used if you support subpages and want to attach some
2923 * extra OOB data to an ECC chunk.
2924 *
2925 * Returns a positive number of bitflips less than or equal to
2926 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2927 * threshold. In case of success, the passed buffers are filled with 0xff.
2928 */
2929int nand_check_erased_ecc_chunk(void *data, int datalen,
2930 void *ecc, int ecclen,
2931 void *extraoob, int extraooblen,
2932 int bitflips_threshold)
2933{
2934 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2935
2936 data_bitflips = nand_check_erased_buf(data, datalen,
2937 bitflips_threshold);
2938 if (data_bitflips < 0)
2939 return data_bitflips;
2940
2941 bitflips_threshold -= data_bitflips;
2942
2943 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2944 if (ecc_bitflips < 0)
2945 return ecc_bitflips;
2946
2947 bitflips_threshold -= ecc_bitflips;
2948
2949 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2950 bitflips_threshold);
2951 if (extraoob_bitflips < 0)
2952 return extraoob_bitflips;
2953
2954 if (data_bitflips)
2955 memset(data, 0xff, datalen);
2956
2957 if (ecc_bitflips)
2958 memset(ecc, 0xff, ecclen);
2959
2960 if (extraoob_bitflips)
2961 memset(extraoob, 0xff, extraooblen);
2962
2963 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2964}
2965EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2966
2967/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002968 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002969 * @chip: nand chip info structure
2970 * @buf: buffer to store read data
2971 * @oob_required: caller requires OOB data read to chip->oob_poi
2972 * @page: page number to read
2973 *
2974 * Returns -ENOTSUPP unconditionally.
2975 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002976int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2977 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002978{
2979 return -ENOTSUPP;
2980}
2981EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2982
2983/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002984 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002985 * @chip: nand chip info structure
2986 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002987 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002988 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002989 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002990 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002991 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002992int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2993 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002994{
Boris Brezillonb9761682018-09-06 14:05:20 +02002995 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002996 int ret;
2997
Boris Brezillon25f815f2017-11-30 18:01:30 +01002998 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002999 if (ret)
3000 return ret;
3001
3002 if (oob_required) {
3003 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3004 false);
3005 if (ret)
3006 return ret;
3007 }
3008
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003009 return 0;
3010}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003011EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003012
3013/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003014 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003015 * @chip: nand chip info structure
3016 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003017 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003018 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003019 *
3020 * We need a special oob layout and handling even when OOB isn't used.
3021 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003022static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003023 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003024{
Boris Brezillonb9761682018-09-06 14:05:20 +02003025 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003026 int eccsize = chip->ecc.size;
3027 int eccbytes = chip->ecc.bytes;
3028 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003029 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003030
Boris Brezillon25f815f2017-11-30 18:01:30 +01003031 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3032 if (ret)
3033 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003034
3035 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003036 ret = nand_read_data_op(chip, buf, eccsize, false);
3037 if (ret)
3038 return ret;
3039
David Brownell52ff49d2009-03-04 12:01:36 -08003040 buf += eccsize;
3041
3042 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003043 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3044 false);
3045 if (ret)
3046 return ret;
3047
David Brownell52ff49d2009-03-04 12:01:36 -08003048 oob += chip->ecc.prepad;
3049 }
3050
Boris Brezillon97d90da2017-11-30 18:01:29 +01003051 ret = nand_read_data_op(chip, oob, eccbytes, false);
3052 if (ret)
3053 return ret;
3054
David Brownell52ff49d2009-03-04 12:01:36 -08003055 oob += eccbytes;
3056
3057 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003058 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3059 false);
3060 if (ret)
3061 return ret;
3062
David Brownell52ff49d2009-03-04 12:01:36 -08003063 oob += chip->ecc.postpad;
3064 }
3065 }
3066
3067 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003068 if (size) {
3069 ret = nand_read_data_op(chip, oob, size, false);
3070 if (ret)
3071 return ret;
3072 }
David Brownell52ff49d2009-03-04 12:01:36 -08003073
3074 return 0;
3075}
3076
3077/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003078 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003079 * @chip: nand chip info structure
3080 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003081 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003082 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003083 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003084static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3085 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Boris Brezillonb9761682018-09-06 14:05:20 +02003087 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003088 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003089 int eccbytes = chip->ecc.bytes;
3090 int eccsteps = chip->ecc.steps;
3091 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003092 uint8_t *ecc_calc = chip->ecc.calc_buf;
3093 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003094 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003095
Boris Brezillonb9761682018-09-06 14:05:20 +02003096 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003097
3098 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003099 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003100
Boris Brezillon846031d2016-02-03 20:11:00 +01003101 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3102 chip->ecc.total);
3103 if (ret)
3104 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003105
3106 eccsteps = chip->ecc.steps;
3107 p = buf;
3108
3109 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3110 int stat;
3111
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003112 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003113 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003114 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003115 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003116 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003117 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3118 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003119 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003120 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303124 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003125 * @chip: nand chip info structure
3126 * @data_offs: offset of requested data within the page
3127 * @readlen: data length
3128 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003129 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003130 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003131static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3132 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003133{
Boris Brezillonb9761682018-09-06 14:05:20 +02003134 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003135 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003136 uint8_t *p;
3137 int data_col_addr, i, gaps = 0;
3138 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3139 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003140 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003141 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003142 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003143
Brian Norris7854d3f2011-06-23 14:12:08 -07003144 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003145 start_step = data_offs / chip->ecc.size;
3146 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3147 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303148 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003149
Brian Norris8b6e50c2011-05-25 14:59:01 -07003150 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003151 datafrag_len = num_steps * chip->ecc.size;
3152 eccfrag_len = num_steps * chip->ecc.bytes;
3153
3154 data_col_addr = start_step * chip->ecc.size;
3155 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003156 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003157 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003158 if (ret)
3159 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003160
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003162 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003163 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003164
Brian Norris8b6e50c2011-05-25 14:59:01 -07003165 /*
3166 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003167 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003168 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003169 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3170 if (ret)
3171 return ret;
3172
3173 if (oobregion.length < eccfrag_len)
3174 gaps = 1;
3175
Alexey Korolev3d459552008-05-15 17:23:18 +01003176 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003177 ret = nand_change_read_column_op(chip, mtd->writesize,
3178 chip->oob_poi, mtd->oobsize,
3179 false);
3180 if (ret)
3181 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003182 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003183 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003184 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003185 * about buswidth alignment in read_buf.
3186 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003187 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003188 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003189 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003190 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003191 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3192 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003193 aligned_len++;
3194
Boris Brezillon97d90da2017-11-30 18:01:29 +01003195 ret = nand_change_read_column_op(chip,
3196 mtd->writesize + aligned_pos,
3197 &chip->oob_poi[aligned_pos],
3198 aligned_len, false);
3199 if (ret)
3200 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003201 }
3202
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003203 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003204 chip->oob_poi, index, eccfrag_len);
3205 if (ret)
3206 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003207
3208 p = bufpoi + data_col_addr;
3209 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3210 int stat;
3211
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003212 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003213 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003214 if (stat == -EBADMSG &&
3215 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3216 /* check for empty pages with bitflips */
3217 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003218 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003219 chip->ecc.bytes,
3220 NULL, 0,
3221 chip->ecc.strength);
3222 }
3223
Mike Dunn3f91e942012-04-25 12:06:09 -07003224 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003225 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003226 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003227 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003228 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3229 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003230 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003231 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003232}
3233
3234/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003235 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003236 * @chip: nand chip info structure
3237 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003238 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003239 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003240 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003241 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003242 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003243static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3244 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003245{
Boris Brezillonb9761682018-09-06 14:05:20 +02003246 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003247 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003248 int eccbytes = chip->ecc.bytes;
3249 int eccsteps = chip->ecc.steps;
3250 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003251 uint8_t *ecc_calc = chip->ecc.calc_buf;
3252 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003253 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003254
Boris Brezillon25f815f2017-11-30 18:01:30 +01003255 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3256 if (ret)
3257 return ret;
3258
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003259 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003260 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003261
3262 ret = nand_read_data_op(chip, p, eccsize, false);
3263 if (ret)
3264 return ret;
3265
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003266 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003267 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003268
3269 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3270 if (ret)
3271 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003272
Boris Brezillon846031d2016-02-03 20:11:00 +01003273 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3274 chip->ecc.total);
3275 if (ret)
3276 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003277
3278 eccsteps = chip->ecc.steps;
3279 p = buf;
3280
3281 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3282 int stat;
3283
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003284 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003285 if (stat == -EBADMSG &&
3286 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3287 /* check for empty pages with bitflips */
3288 stat = nand_check_erased_ecc_chunk(p, eccsize,
3289 &ecc_code[i], eccbytes,
3290 NULL, 0,
3291 chip->ecc.strength);
3292 }
3293
Mike Dunn3f91e942012-04-25 12:06:09 -07003294 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003295 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003296 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003297 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003298 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3299 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003300 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003301 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003302}
3303
3304/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003305 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003306 * @chip: nand chip info structure
3307 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003308 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003309 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003310 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003311 * Hardware ECC for large page chips, require OOB to be read first. For this
3312 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3313 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3314 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3315 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003316 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003317static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3318 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003319{
Boris Brezillonb9761682018-09-06 14:05:20 +02003320 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003321 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003322 int eccbytes = chip->ecc.bytes;
3323 int eccsteps = chip->ecc.steps;
3324 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003325 uint8_t *ecc_code = chip->ecc.code_buf;
3326 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003327 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003328
3329 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003330 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3331 if (ret)
3332 return ret;
3333
3334 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3335 if (ret)
3336 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003337
Boris Brezillon846031d2016-02-03 20:11:00 +01003338 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3339 chip->ecc.total);
3340 if (ret)
3341 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003342
3343 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3344 int stat;
3345
Boris Brezillonec476362018-09-06 14:05:17 +02003346 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003347
3348 ret = nand_read_data_op(chip, p, eccsize, false);
3349 if (ret)
3350 return ret;
3351
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003352 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003353
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003354 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003355 if (stat == -EBADMSG &&
3356 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3357 /* check for empty pages with bitflips */
3358 stat = nand_check_erased_ecc_chunk(p, eccsize,
3359 &ecc_code[i], eccbytes,
3360 NULL, 0,
3361 chip->ecc.strength);
3362 }
3363
Mike Dunn3f91e942012-04-25 12:06:09 -07003364 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003365 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003366 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003367 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003368 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3369 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003370 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003371 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003372}
3373
3374/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003375 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003376 * @chip: nand chip info structure
3377 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003378 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003379 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003380 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003381 * The hw generator calculates the error syndrome automatically. Therefore we
3382 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003383 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003384static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3385 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386{
Boris Brezillonb9761682018-09-06 14:05:20 +02003387 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003388 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003389 int eccbytes = chip->ecc.bytes;
3390 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003391 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003392 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003393 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003394 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003395
Boris Brezillon25f815f2017-11-30 18:01:30 +01003396 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3397 if (ret)
3398 return ret;
3399
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003400 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3401 int stat;
3402
Boris Brezillonec476362018-09-06 14:05:17 +02003403 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003404
3405 ret = nand_read_data_op(chip, p, eccsize, false);
3406 if (ret)
3407 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003408
3409 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003410 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3411 false);
3412 if (ret)
3413 return ret;
3414
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003415 oob += chip->ecc.prepad;
3416 }
3417
Boris Brezillonec476362018-09-06 14:05:17 +02003418 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003419
3420 ret = nand_read_data_op(chip, oob, eccbytes, false);
3421 if (ret)
3422 return ret;
3423
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003424 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003425
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003426 oob += eccbytes;
3427
3428 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003429 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3430 false);
3431 if (ret)
3432 return ret;
3433
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003434 oob += chip->ecc.postpad;
3435 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003436
3437 if (stat == -EBADMSG &&
3438 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3439 /* check for empty pages with bitflips */
3440 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3441 oob - eccpadbytes,
3442 eccpadbytes,
3443 NULL, 0,
3444 chip->ecc.strength);
3445 }
3446
3447 if (stat < 0) {
3448 mtd->ecc_stats.failed++;
3449 } else {
3450 mtd->ecc_stats.corrected += stat;
3451 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3452 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003453 }
3454
3455 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003456 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003457 if (i) {
3458 ret = nand_read_data_op(chip, oob, i, false);
3459 if (ret)
3460 return ret;
3461 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003462
Mike Dunn3f91e942012-04-25 12:06:09 -07003463 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003464}
3465
3466/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003467 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003468 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003469 * @oob: oob destination address
3470 * @ops: oob ops structure
3471 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003472 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003473static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003474 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003475{
Boris Brezillon846031d2016-02-03 20:11:00 +01003476 struct nand_chip *chip = mtd_to_nand(mtd);
3477 int ret;
3478
Florian Fainellif8ac0412010-09-07 13:23:43 +02003479 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003480
Brian Norris0612b9d2011-08-30 18:45:40 -07003481 case MTD_OPS_PLACE_OOB:
3482 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003483 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3484 return oob + len;
3485
Boris Brezillon846031d2016-02-03 20:11:00 +01003486 case MTD_OPS_AUTO_OOB:
3487 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3488 ops->ooboffs, len);
3489 BUG_ON(ret);
3490 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003491
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003492 default:
3493 BUG();
3494 }
3495 return NULL;
3496}
3497
3498/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003499 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003500 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003501 * @retry_mode: the retry mode to use
3502 *
3503 * Some vendors supply a special command to shift the Vt threshold, to be used
3504 * when there are too many bitflips in a page (i.e., ECC error). After setting
3505 * a new threshold, the host should retry reading the page.
3506 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003507static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003508{
Brian Norrisba84fb52014-01-03 15:13:33 -08003509 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3510
3511 if (retry_mode >= chip->read_retries)
3512 return -EINVAL;
3513
3514 if (!chip->setup_read_retry)
3515 return -EOPNOTSUPP;
3516
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003517 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003518}
3519
Boris Brezillon85e08e52018-07-27 09:44:17 +02003520static void nand_wait_readrdy(struct nand_chip *chip)
3521{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003522 const struct nand_sdr_timings *sdr;
3523
Boris Brezillon85e08e52018-07-27 09:44:17 +02003524 if (!(chip->options & NAND_NEED_READRDY))
3525 return;
3526
Boris Brezillon52f05b62018-07-27 09:44:18 +02003527 sdr = nand_get_sdr_timings(&chip->data_interface);
3528 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003529}
3530
Brian Norrisba84fb52014-01-03 15:13:33 -08003531/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003532 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003533 * @mtd: MTD device structure
3534 * @from: offset to read from
3535 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003536 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003537 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003538 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003539static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3540 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003541{
Brian Norrise47f3db2012-05-02 10:14:56 -07003542 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003543 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003544 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003545 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003546 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003547 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003548
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003549 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003550 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003551 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003552 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003553 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003555 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003556 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003558 realpage = (int)(from >> chip->page_shift);
3559 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003561 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003563 buf = ops->datbuf;
3564 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003565 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003566
Florian Fainellif8ac0412010-09-07 13:23:43 +02003567 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003568 unsigned int ecc_failures = mtd->ecc_stats.failed;
3569
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003570 bytes = min(mtd->writesize - col, readlen);
3571 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003572
Kamal Dasu66507c72014-05-01 20:51:19 -04003573 if (!aligned)
3574 use_bufpoi = 1;
3575 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003576 use_bufpoi = !virt_addr_valid(buf) ||
3577 !IS_ALIGNED((unsigned long)buf,
3578 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003579 else
3580 use_bufpoi = 0;
3581
Brian Norris8b6e50c2011-05-25 14:59:01 -07003582 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003583 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003584 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003585
3586 if (use_bufpoi && aligned)
3587 pr_debug("%s: using read bounce buffer for buf@%p\n",
3588 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Brian Norrisba84fb52014-01-03 15:13:33 -08003590read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003591 /*
3592 * Now read the page into the buffer. Absent an error,
3593 * the read methods return max bitflips per ecc step.
3594 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003595 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003596 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003597 oob_required,
3598 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003599 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3600 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003601 ret = chip->ecc.read_subpage(chip, col, bytes,
3602 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003603 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003604 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003605 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003606 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003607 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003608 /* Invalidate page cache */
3609 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003610 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003611 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003612
3613 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003614 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003615 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003616 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003617 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003618 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003619 chip->pagebuf_bitflips = ret;
3620 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003621 /* Invalidate page cache */
3622 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003623 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003624 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003626
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003627 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003628 int toread = min(oobreadlen, max_oobsize);
3629
3630 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003631 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003632 oob, ops, toread);
3633 oobreadlen -= toread;
3634 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003635 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003636
Boris Brezillon85e08e52018-07-27 09:44:17 +02003637 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003638
Brian Norrisba84fb52014-01-03 15:13:33 -08003639 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003640 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003641 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003642 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003643 retry_mode);
3644 if (ret < 0)
3645 break;
3646
3647 /* Reset failures; retry */
3648 mtd->ecc_stats.failed = ecc_failures;
3649 goto read_retry;
3650 } else {
3651 /* No more retry modes; real failure */
3652 ecc_fail = true;
3653 }
3654 }
3655
3656 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003657 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003658 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003659 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003660 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003661 max_bitflips = max_t(unsigned int, max_bitflips,
3662 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003665 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003666
Brian Norrisba84fb52014-01-03 15:13:33 -08003667 /* Reset to retry mode 0 */
3668 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003669 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003670 if (ret < 0)
3671 break;
3672 retry_mode = 0;
3673 }
3674
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003675 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Brian Norris8b6e50c2011-05-25 14:59:01 -07003678 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 col = 0;
3680 /* Increment page address */
3681 realpage++;
3682
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003683 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /* Check, if we cross a chip boundary */
3685 if (!page) {
3686 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003687 chip->select_chip(chip, -1);
3688 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003691 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003693 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003694 if (oob)
3695 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Mike Dunn3f91e942012-04-25 12:06:09 -07003697 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003698 return ret;
3699
Brian Norrisb72f3df2013-12-03 11:04:14 -08003700 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003701 return -EBADMSG;
3702
Mike Dunnedbc45402012-04-25 12:06:11 -07003703 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003704}
3705
3706/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003707 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003708 * @chip: nand chip info structure
3709 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003710 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003711int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003712{
Boris Brezillonb9761682018-09-06 14:05:20 +02003713 struct mtd_info *mtd = nand_to_mtd(chip);
3714
Boris Brezillon97d90da2017-11-30 18:01:29 +01003715 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003716}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003717EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718
3719/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003720 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003721 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003722 * @chip: nand chip info structure
3723 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003725int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003726{
Boris Brezillonb9761682018-09-06 14:05:20 +02003727 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003728 int length = mtd->oobsize;
3729 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3730 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003731 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003732 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003733
Boris Brezillon97d90da2017-11-30 18:01:29 +01003734 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3735 if (ret)
3736 return ret;
3737
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003738 for (i = 0; i < chip->ecc.steps; i++) {
3739 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003740 int ret;
3741
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003742 pos = eccsize + i * (eccsize + chunk);
3743 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003744 ret = nand_change_read_column_op(chip, pos,
3745 NULL, 0,
3746 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003747 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003748 ret = nand_read_page_op(chip, page, pos, NULL,
3749 0);
3750
3751 if (ret)
3752 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003753 } else
3754 sndrnd = 1;
3755 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003756
3757 ret = nand_read_data_op(chip, bufpoi, toread, false);
3758 if (ret)
3759 return ret;
3760
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003761 bufpoi += toread;
3762 length -= toread;
3763 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003764 if (length > 0) {
3765 ret = nand_read_data_op(chip, bufpoi, length, false);
3766 if (ret)
3767 return ret;
3768 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003769
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003770 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003771}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003772EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003773
3774/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003775 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003776 * @chip: nand chip info structure
3777 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003778 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003779int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003780{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003781 struct mtd_info *mtd = nand_to_mtd(chip);
3782
Boris Brezillon97d90da2017-11-30 18:01:29 +01003783 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3784 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003785}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003786EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003787
3788/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003789 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003790 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003791 * @chip: nand chip info structure
3792 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003793 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003794int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003795{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003796 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003797 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3798 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003799 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003800 const uint8_t *bufpoi = chip->oob_poi;
3801
3802 /*
3803 * data-ecc-data-ecc ... ecc-oob
3804 * or
3805 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3806 */
3807 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3808 pos = steps * (eccsize + chunk);
3809 steps = 0;
3810 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003811 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003812
Boris Brezillon97d90da2017-11-30 18:01:29 +01003813 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3814 if (ret)
3815 return ret;
3816
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003817 for (i = 0; i < steps; i++) {
3818 if (sndcmd) {
3819 if (mtd->writesize <= 512) {
3820 uint32_t fill = 0xFFFFFFFF;
3821
3822 len = eccsize;
3823 while (len > 0) {
3824 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003825
3826 ret = nand_write_data_op(chip, &fill,
3827 num, false);
3828 if (ret)
3829 return ret;
3830
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003831 len -= num;
3832 }
3833 } else {
3834 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003835 ret = nand_change_write_column_op(chip, pos,
3836 NULL, 0,
3837 false);
3838 if (ret)
3839 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003840 }
3841 } else
3842 sndcmd = 1;
3843 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003844
3845 ret = nand_write_data_op(chip, bufpoi, len, false);
3846 if (ret)
3847 return ret;
3848
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003849 bufpoi += len;
3850 length -= len;
3851 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003852 if (length > 0) {
3853 ret = nand_write_data_op(chip, bufpoi, length, false);
3854 if (ret)
3855 return ret;
3856 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003857
Boris Brezillon97d90da2017-11-30 18:01:29 +01003858 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003859}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003860EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003861
3862/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003863 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003864 * @mtd: MTD device structure
3865 * @from: offset to read from
3866 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003868 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003870static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3871 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003873 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003874 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003875 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003876 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003877 int readlen = ops->ooblen;
3878 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003879 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003880 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Brian Norris289c0522011-07-19 10:06:09 -07003882 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303883 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Brian Norris041e4572011-06-23 16:45:24 -07003885 stats = mtd->ecc_stats;
3886
Boris BREZILLON29f10582016-03-07 10:46:52 +01003887 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003888
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003889 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003890 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003892 /* Shift to get page */
3893 realpage = (int)(from >> chip->page_shift);
3894 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Florian Fainellif8ac0412010-09-07 13:23:43 +02003896 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003897 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003898 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003899 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003900 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003901
3902 if (ret < 0)
3903 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003904
3905 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003906 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003907
Boris Brezillon85e08e52018-07-27 09:44:17 +02003908 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003909
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003910 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3911
Vitaly Wool70145682006-11-03 18:20:38 +03003912 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003913 if (!readlen)
3914 break;
3915
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003916 /* Increment page address */
3917 realpage++;
3918
3919 page = realpage & chip->pagemask;
3920 /* Check, if we cross a chip boundary */
3921 if (!page) {
3922 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003923 chip->select_chip(chip, -1);
3924 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003927 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003929 ops->oobretlen = ops->ooblen - readlen;
3930
3931 if (ret < 0)
3932 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003933
3934 if (mtd->ecc_stats.failed - stats.failed)
3935 return -EBADMSG;
3936
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003937 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
3940/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003941 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003942 * @mtd: MTD device structure
3943 * @from: offset to read from
3944 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003946 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003948static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3949 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003951 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003952
3953 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003955 if (ops->mode != MTD_OPS_PLACE_OOB &&
3956 ops->mode != MTD_OPS_AUTO_OOB &&
3957 ops->mode != MTD_OPS_RAW)
3958 return -ENOTSUPP;
3959
Huang Shijie6a8214a2012-11-19 14:43:30 +08003960 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003962 if (!ops->datbuf)
3963 ret = nand_do_read_oob(mtd, from, ops);
3964 else
3965 ret = nand_do_read_ops(mtd, from, ops);
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003968 return ret;
3969}
3970
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003971/**
3972 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003973 * @chip: nand chip info structure
3974 * @buf: data buffer
3975 * @oob_required: must write chip->oob_poi to OOB
3976 * @page: page number to write
3977 *
3978 * Returns -ENOTSUPP unconditionally.
3979 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003980int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3981 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003982{
3983 return -ENOTSUPP;
3984}
3985EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003986
3987/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003988 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003989 * @chip: nand chip info structure
3990 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003991 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003992 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003993 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003994 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003995 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003996int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3997 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003998{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003999 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004000 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004001
Boris Brezillon25f815f2017-11-30 18:01:30 +01004002 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004003 if (ret)
4004 return ret;
4005
4006 if (oob_required) {
4007 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4008 false);
4009 if (ret)
4010 return ret;
4011 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004012
Boris Brezillon25f815f2017-11-30 18:01:30 +01004013 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004015EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004017/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004018 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004019 * @chip: nand chip info structure
4020 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004021 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004022 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004023 *
4024 * We need a special oob layout and handling even when ECC isn't checked.
4025 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004026static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004027 const uint8_t *buf, int oob_required,
4028 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004029{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004030 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004031 int eccsize = chip->ecc.size;
4032 int eccbytes = chip->ecc.bytes;
4033 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004034 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004035
Boris Brezillon25f815f2017-11-30 18:01:30 +01004036 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4037 if (ret)
4038 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004039
4040 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004041 ret = nand_write_data_op(chip, buf, eccsize, false);
4042 if (ret)
4043 return ret;
4044
David Brownell52ff49d2009-03-04 12:01:36 -08004045 buf += eccsize;
4046
4047 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004048 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4049 false);
4050 if (ret)
4051 return ret;
4052
David Brownell52ff49d2009-03-04 12:01:36 -08004053 oob += chip->ecc.prepad;
4054 }
4055
Boris Brezillon97d90da2017-11-30 18:01:29 +01004056 ret = nand_write_data_op(chip, oob, eccbytes, false);
4057 if (ret)
4058 return ret;
4059
David Brownell52ff49d2009-03-04 12:01:36 -08004060 oob += eccbytes;
4061
4062 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004063 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4064 false);
4065 if (ret)
4066 return ret;
4067
David Brownell52ff49d2009-03-04 12:01:36 -08004068 oob += chip->ecc.postpad;
4069 }
4070 }
4071
4072 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004073 if (size) {
4074 ret = nand_write_data_op(chip, oob, size, false);
4075 if (ret)
4076 return ret;
4077 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004078
Boris Brezillon25f815f2017-11-30 18:01:30 +01004079 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004080}
4081/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004082 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004083 * @chip: nand chip info structure
4084 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004085 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004086 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004087 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004088static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4089 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004090{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004091 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004092 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004093 int eccbytes = chip->ecc.bytes;
4094 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004095 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004096 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004097
Brian Norris7854d3f2011-06-23 14:12:08 -07004098 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004099 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004100 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101
Boris Brezillon846031d2016-02-03 20:11:00 +01004102 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4103 chip->ecc.total);
4104 if (ret)
4105 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004106
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004107 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004108}
4109
4110/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004111 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004112 * @chip: nand chip info structure
4113 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004114 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004115 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004116 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004117static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4118 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004119{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004120 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004121 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004122 int eccbytes = chip->ecc.bytes;
4123 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004124 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004125 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004126
Boris Brezillon25f815f2017-11-30 18:01:30 +01004127 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4128 if (ret)
4129 return ret;
4130
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004131 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004132 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004133
4134 ret = nand_write_data_op(chip, p, eccsize, false);
4135 if (ret)
4136 return ret;
4137
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004138 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004139 }
4140
Boris Brezillon846031d2016-02-03 20:11:00 +01004141 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4142 chip->ecc.total);
4143 if (ret)
4144 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004145
Boris Brezillon97d90da2017-11-30 18:01:29 +01004146 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4147 if (ret)
4148 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004149
Boris Brezillon25f815f2017-11-30 18:01:30 +01004150 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004151}
4152
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304153
4154/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004155 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304156 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004157 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304158 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004159 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304160 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004161 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304162 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004163static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4164 uint32_t data_len, const uint8_t *buf,
4165 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304166{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004167 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304168 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004169 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304170 int ecc_size = chip->ecc.size;
4171 int ecc_bytes = chip->ecc.bytes;
4172 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304173 uint32_t start_step = offset / ecc_size;
4174 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4175 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004176 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304177
Boris Brezillon25f815f2017-11-30 18:01:30 +01004178 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4179 if (ret)
4180 return ret;
4181
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304182 for (step = 0; step < ecc_steps; step++) {
4183 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004184 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304185
4186 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004187 ret = nand_write_data_op(chip, buf, ecc_size, false);
4188 if (ret)
4189 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304190
4191 /* mask ECC of un-touched subpages by padding 0xFF */
4192 if ((step < start_step) || (step > end_step))
4193 memset(ecc_calc, 0xff, ecc_bytes);
4194 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004195 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196
4197 /* mask OOB of un-touched subpages by padding 0xFF */
4198 /* if oob_required, preserve OOB metadata of written subpage */
4199 if (!oob_required || (step < start_step) || (step > end_step))
4200 memset(oob_buf, 0xff, oob_bytes);
4201
Brian Norrisd6a950802013-08-08 17:16:36 -07004202 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304203 ecc_calc += ecc_bytes;
4204 oob_buf += oob_bytes;
4205 }
4206
4207 /* copy calculated ECC for whole page to chip->buffer->oob */
4208 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004209 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004210 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4211 chip->ecc.total);
4212 if (ret)
4213 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304214
4215 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004216 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4217 if (ret)
4218 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304219
Boris Brezillon25f815f2017-11-30 18:01:30 +01004220 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304221}
4222
4223
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004224/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004225 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004226 * @chip: nand chip info structure
4227 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004228 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004229 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004230 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004231 * The hw generator calculates the error syndrome automatically. Therefore we
4232 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004233 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004234static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4235 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004236{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004237 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004238 int i, eccsize = chip->ecc.size;
4239 int eccbytes = chip->ecc.bytes;
4240 int eccsteps = chip->ecc.steps;
4241 const uint8_t *p = buf;
4242 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004243 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004244
Boris Brezillon25f815f2017-11-30 18:01:30 +01004245 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4246 if (ret)
4247 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004248
4249 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004250 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004251
4252 ret = nand_write_data_op(chip, p, eccsize, false);
4253 if (ret)
4254 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004255
4256 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004257 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4258 false);
4259 if (ret)
4260 return ret;
4261
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004262 oob += chip->ecc.prepad;
4263 }
4264
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004265 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004266
4267 ret = nand_write_data_op(chip, oob, eccbytes, false);
4268 if (ret)
4269 return ret;
4270
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004271 oob += eccbytes;
4272
4273 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004274 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4275 false);
4276 if (ret)
4277 return ret;
4278
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004279 oob += chip->ecc.postpad;
4280 }
4281 }
4282
4283 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004284 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004285 if (i) {
4286 ret = nand_write_data_op(chip, oob, i, false);
4287 if (ret)
4288 return ret;
4289 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004290
Boris Brezillon25f815f2017-11-30 18:01:30 +01004291 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004292}
4293
4294/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004295 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004296 * @mtd: MTD device structure
4297 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304298 * @offset: address offset within the page
4299 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004300 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004301 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004303 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004304 */
4305static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304306 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004307 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004308{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304309 int status, subpage;
4310
4311 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4312 chip->ecc.write_subpage)
4313 subpage = offset || (data_len < mtd->writesize);
4314 else
4315 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004316
David Woodhouse956e9442006-09-25 17:12:39 +01004317 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004318 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4319 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304320 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004321 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4322 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004323 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004324 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004325
4326 if (status < 0)
4327 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004328
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004329 return 0;
4330}
4331
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004332/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004333 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004334 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004335 * @oob: oob data buffer
4336 * @len: oob data write length
4337 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004338 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004339static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4340 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004341{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004342 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004343 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004344
4345 /*
4346 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4347 * data from a previous OOB read.
4348 */
4349 memset(chip->oob_poi, 0xff, mtd->oobsize);
4350
Florian Fainellif8ac0412010-09-07 13:23:43 +02004351 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004352
Brian Norris0612b9d2011-08-30 18:45:40 -07004353 case MTD_OPS_PLACE_OOB:
4354 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004355 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4356 return oob + len;
4357
Boris Brezillon846031d2016-02-03 20:11:00 +01004358 case MTD_OPS_AUTO_OOB:
4359 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4360 ops->ooboffs, len);
4361 BUG_ON(ret);
4362 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004363
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364 default:
4365 BUG();
4366 }
4367 return NULL;
4368}
4369
Florian Fainellif8ac0412010-09-07 13:23:43 +02004370#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004371
4372/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004373 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004374 * @mtd: MTD device structure
4375 * @to: offset to write to
4376 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004378 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004379 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004380static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4381 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382{
Corentin Labbe73600b62017-09-02 10:49:38 +02004383 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004384 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004385 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004386
4387 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004388 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004389
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004390 uint8_t *oob = ops->oobbuf;
4391 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304392 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004393 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004394
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004395 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004396 if (!writelen)
4397 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004398
Brian Norris8b6e50c2011-05-25 14:59:01 -07004399 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004400 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004401 pr_notice("%s: attempt to write non page aligned data\n",
4402 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004403 return -EINVAL;
4404 }
4405
Thomas Gleixner29072b92006-09-28 15:38:36 +02004406 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004407
Thomas Gleixner6a930962006-06-28 00:11:45 +02004408 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004409 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004410
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004411 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004412 if (nand_check_wp(mtd)) {
4413 ret = -EIO;
4414 goto err_out;
4415 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004416
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004417 realpage = (int)(to >> chip->page_shift);
4418 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004419
4420 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004421 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4422 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004423 chip->pagebuf = -1;
4424
Maxim Levitsky782ce792010-02-22 20:39:36 +02004425 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004426 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4427 ret = -EINVAL;
4428 goto err_out;
4429 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004430
Florian Fainellif8ac0412010-09-07 13:23:43 +02004431 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004432 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004433 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004434 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004435 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004436
Kamal Dasu66507c72014-05-01 20:51:19 -04004437 if (part_pagewr)
4438 use_bufpoi = 1;
4439 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004440 use_bufpoi = !virt_addr_valid(buf) ||
4441 !IS_ALIGNED((unsigned long)buf,
4442 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004443 else
4444 use_bufpoi = 0;
4445
4446 /* Partial page write?, or need to use bounce buffer */
4447 if (use_bufpoi) {
4448 pr_debug("%s: using write bounce buffer for buf@%p\n",
4449 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004450 if (part_pagewr)
4451 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004452 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004453 memset(chip->data_buf, 0xff, mtd->writesize);
4454 memcpy(&chip->data_buf[column], buf, bytes);
4455 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004456 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004457
Maxim Levitsky782ce792010-02-22 20:39:36 +02004458 if (unlikely(oob)) {
4459 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004460 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004461 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004462 } else {
4463 /* We still need to erase leftover OOB data */
4464 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004465 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004466
4467 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004468 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004469 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004470 if (ret)
4471 break;
4472
4473 writelen -= bytes;
4474 if (!writelen)
4475 break;
4476
Thomas Gleixner29072b92006-09-28 15:38:36 +02004477 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004478 buf += bytes;
4479 realpage++;
4480
4481 page = realpage & chip->pagemask;
4482 /* Check, if we cross a chip boundary */
4483 if (!page) {
4484 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004485 chip->select_chip(chip, -1);
4486 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004487 }
4488 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004489
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004490 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004491 if (unlikely(oob))
4492 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004493
4494err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004495 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004496 return ret;
4497}
4498
4499/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004500 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004501 * @mtd: MTD device structure
4502 * @to: offset to write to
4503 * @len: number of bytes to write
4504 * @retlen: pointer to variable to store the number of written bytes
4505 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004506 *
4507 * NAND write with ECC. Used when performing writes in interrupt context, this
4508 * may for example be called by mtdoops when writing an oops while in panic.
4509 */
4510static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4511 size_t *retlen, const uint8_t *buf)
4512{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004513 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004514 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004515 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004516 int ret;
4517
Brian Norris8b6e50c2011-05-25 14:59:01 -07004518 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004519 panic_nand_get_device(chip, mtd, FL_WRITING);
4520
Boris Brezillon758b56f2018-09-06 14:05:24 +02004521 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004522
4523 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004524 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004525
Brian Norris0ec56dc2015-02-28 02:02:30 -08004526 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004527 ops.len = len;
4528 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004529 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004530
Brian Norris4a89ff82011-08-30 18:45:45 -07004531 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004532
Brian Norris4a89ff82011-08-30 18:45:45 -07004533 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004534 return ret;
4535}
4536
4537/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004538 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004539 * @mtd: MTD device structure
4540 * @to: offset to write to
4541 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004542 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004543 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004544 */
4545static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4546 struct mtd_oob_ops *ops)
4547{
Adrian Hunter03736152007-01-31 17:58:29 +02004548 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004549 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
Brian Norris289c0522011-07-19 10:06:09 -07004551 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304552 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Boris BREZILLON29f10582016-03-07 10:46:52 +01004554 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004557 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004558 pr_debug("%s: attempt to write past end of page\n",
4559 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 return -EINVAL;
4561 }
4562
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004563 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004564
4565 /*
4566 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4567 * of my DiskOnChip 2000 test units) will clear the whole data page too
4568 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4569 * it in the doc2000 driver in August 1999. dwmw2.
4570 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004571 nand_reset(chip, chipnr);
4572
Boris Brezillon758b56f2018-09-06 14:05:24 +02004573 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004574
4575 /* Shift to get page */
4576 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
4578 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004579 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004580 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004581 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004582 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004585 if (page == chip->pagebuf)
4586 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004588 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004589
Brian Norris0612b9d2011-08-30 18:45:40 -07004590 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004591 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004592 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004593 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004594
Boris Brezillon758b56f2018-09-06 14:05:24 +02004595 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004596
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004597 if (status)
4598 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Vitaly Wool70145682006-11-03 18:20:38 +03004600 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004602 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004605/**
4606 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004607 * @mtd: MTD device structure
4608 * @to: offset to write to
4609 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004610 */
4611static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4612 struct mtd_oob_ops *ops)
4613{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004614 int ret = -ENOTSUPP;
4615
4616 ops->retlen = 0;
4617
Huang Shijie6a8214a2012-11-19 14:43:30 +08004618 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004619
Florian Fainellif8ac0412010-09-07 13:23:43 +02004620 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004621 case MTD_OPS_PLACE_OOB:
4622 case MTD_OPS_AUTO_OOB:
4623 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004624 break;
4625
4626 default:
4627 goto out;
4628 }
4629
4630 if (!ops->datbuf)
4631 ret = nand_do_write_oob(mtd, to, ops);
4632 else
4633 ret = nand_do_write_ops(mtd, to, ops);
4634
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004635out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004636 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 return ret;
4638}
4639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640/**
Brian Norris49c50b92014-05-06 16:02:19 -07004641 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004642 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004643 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 *
Brian Norris49c50b92014-05-06 16:02:19 -07004645 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004647static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004649 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004652 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004653
Boris Brezillon97d90da2017-11-30 18:01:29 +01004654 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
4657/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004659 * @mtd: MTD device structure
4660 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004662 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004664static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004666 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004670 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004671 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004672 * @instr: erase instruction
4673 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004675 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004677int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004678 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004680 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004681 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004682 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Brian Norris289c0522011-07-19 10:06:09 -07004684 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4685 __func__, (unsigned long long)instr->addr,
4686 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304688 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004692 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004695 page = (int)(instr->addr >> chip->page_shift);
4696 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004699 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004702 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 /* Check, if it is write protected */
4705 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004706 pr_debug("%s: device is write protected!\n",
4707 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004708 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 goto erase_exit;
4710 }
4711
4712 /* Loop through the pages */
4713 len = instr->len;
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004716 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004717 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304718 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004719 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4720 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004721 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 goto erase_exit;
4723 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004724
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004725 /*
4726 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004727 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004728 */
4729 if (page <= chip->pagebuf && chip->pagebuf <
4730 (page + pages_per_block))
4731 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004733 if (chip->legacy.erase)
4734 status = chip->legacy.erase(chip,
4735 page & chip->pagemask);
4736 else
4737 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004740 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004741 pr_debug("%s: failed erase, page 0x%08x\n",
4742 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004743 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004744 instr->fail_addr =
4745 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 goto erase_exit;
4747 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004750 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 page += pages_per_block;
4752
4753 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004754 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004756 chip->select_chip(chip, -1);
4757 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
4759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004761 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004762erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004765 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 nand_release_device(mtd);
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 /* Return more or less happy */
4769 return ret;
4770}
4771
4772/**
4773 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004774 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004776 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004778static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Brian Norris289c0522011-07-19 10:06:09 -07004780 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004783 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004785 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004789 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004790 * @mtd: MTD device structure
4791 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004793static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304795 struct nand_chip *chip = mtd_to_nand(mtd);
4796 int chipnr = (int)(offs >> chip->chip_shift);
4797 int ret;
4798
4799 /* Select the NAND device */
4800 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004801 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304802
4803 ret = nand_block_checkbad(mtd, offs, 0);
4804
Boris Brezillon758b56f2018-09-06 14:05:24 +02004805 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304806 nand_release_device(mtd);
4807
4808 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809}
4810
4811/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004812 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004813 * @mtd: MTD device structure
4814 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004816static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 int ret;
4819
Florian Fainellif8ac0412010-09-07 13:23:43 +02004820 ret = nand_block_isbad(mtd, ofs);
4821 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004822 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (ret > 0)
4824 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004825 return ret;
4826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
Brian Norris5a0edb22013-07-30 17:52:58 -07004828 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
4831/**
Zach Brown56718422017-01-10 13:30:20 -06004832 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4833 * @mtd: MTD device structure
4834 * @ofs: offset relative to mtd start
4835 * @len: length of mtd
4836 */
4837static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4838{
4839 struct nand_chip *chip = mtd_to_nand(mtd);
4840 u32 part_start_block;
4841 u32 part_end_block;
4842 u32 part_start_die;
4843 u32 part_end_die;
4844
4845 /*
4846 * max_bb_per_die and blocks_per_die used to determine
4847 * the maximum bad block count.
4848 */
4849 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4850 return -ENOTSUPP;
4851
4852 /* Get the start and end of the partition in erase blocks. */
4853 part_start_block = mtd_div_by_eb(ofs, mtd);
4854 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4855
4856 /* Get the start and end LUNs of the partition. */
4857 part_start_die = part_start_block / chip->blocks_per_die;
4858 part_end_die = part_end_block / chip->blocks_per_die;
4859
4860 /*
4861 * Look up the bad blocks per unit and multiply by the number of units
4862 * that the partition spans.
4863 */
4864 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4865}
4866
4867/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004868 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004869 * @chip: nand chip info structure
4870 * @addr: feature address.
4871 * @subfeature_param: the subfeature parameters, a four bytes array.
4872 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004873static int nand_default_set_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004874 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004875{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004876 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004877}
4878
4879/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004880 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004881 * @chip: nand chip info structure
4882 * @addr: feature address.
4883 * @subfeature_param: the subfeature parameters, a four bytes array.
4884 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004885static int nand_default_get_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004886 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004887{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004888 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004889}
4890
4891/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004892 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004893 * @chip: nand chip info structure
4894 * @addr: feature address.
4895 * @subfeature_param: the subfeature parameters, a four bytes array.
4896 *
4897 * Should be used by NAND controller drivers that do not support the SET/GET
4898 * FEATURES operations.
4899 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004900int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
4901 u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004902{
4903 return -ENOTSUPP;
4904}
Miquel Raynalb9587582018-03-19 14:47:19 +01004905EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004906
4907/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004908 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004909 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004910 */
4911static int nand_suspend(struct mtd_info *mtd)
4912{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004913 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004914}
4915
4916/**
4917 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004918 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004919 */
4920static void nand_resume(struct mtd_info *mtd)
4921{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004922 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004923
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004924 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004925 nand_release_device(mtd);
4926 else
Brian Norrisd0370212011-07-19 10:06:08 -07004927 pr_err("%s called for a chip which is not in suspended state\n",
4928 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004929}
4930
Scott Branden72ea4032014-11-20 11:18:05 -08004931/**
4932 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4933 * prevent further operations
4934 * @mtd: MTD device structure
4935 */
4936static void nand_shutdown(struct mtd_info *mtd)
4937{
Brian Norris9ca641b2015-11-09 16:37:28 -08004938 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004939}
4940
Brian Norris8b6e50c2011-05-25 14:59:01 -07004941/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004942static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004943{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004944 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004947 if (!chip->chip_delay)
4948 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 /* check, if a user supplied command function given */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02004951 if (!chip->legacy.cmdfunc && !chip->exec_op)
4952 chip->legacy.cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953
4954 /* check, if a user supplied wait function given */
Boris Brezillon8395b752018-09-07 00:38:37 +02004955 if (chip->legacy.waitfunc == NULL)
4956 chip->legacy.waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004958 if (!chip->select_chip)
4959 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004960
Huang Shijie4204ccc2013-08-16 10:10:07 +08004961 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004962 if (!chip->set_features)
4963 chip->set_features = nand_default_set_features;
4964 if (!chip->get_features)
4965 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004966
Brian Norris68e80782013-07-18 01:17:02 -07004967 /* If called twice, pointers that depend on busw may need to be reset */
Boris Brezillon716bbba2018-09-07 00:38:35 +02004968 if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
4969 chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
Boris Brezillon716bbba2018-09-07 00:38:35 +02004970 if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
4971 chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
4972 if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
4973 chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
4974 if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
4975 chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004976
4977 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004978 chip->controller = &chip->dummy_controller;
4979 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004980 }
4981
Masahiro Yamada477544c2017-03-30 17:15:05 +09004982 if (!chip->buf_align)
4983 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004984}
4985
Brian Norris8b6e50c2011-05-25 14:59:01 -07004986/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004987static void sanitize_string(uint8_t *s, size_t len)
4988{
4989 ssize_t i;
4990
Brian Norris8b6e50c2011-05-25 14:59:01 -07004991 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004992 s[len - 1] = 0;
4993
Brian Norris8b6e50c2011-05-25 14:59:01 -07004994 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004995 for (i = 0; i < len - 1; i++) {
4996 if (s[i] < ' ' || s[i] > 127)
4997 s[i] = '?';
4998 }
4999
Brian Norris8b6e50c2011-05-25 14:59:01 -07005000 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005001 strim(s);
5002}
5003
5004static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
5005{
5006 int i;
5007 while (len--) {
5008 crc ^= *p++ << 8;
5009 for (i = 0; i < 8; i++)
5010 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
5011 }
5012
5013 return crc;
5014}
5015
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005016/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005017static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
5018 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005019{
5020 struct onfi_ext_param_page *ep;
5021 struct onfi_ext_section *s;
5022 struct onfi_ext_ecc_info *ecc;
5023 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005024 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005025 int len;
5026 int i;
5027
5028 len = le16_to_cpu(p->ext_param_page_length) * 16;
5029 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005030 if (!ep)
5031 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005032
5033 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005034 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5035 if (ret)
5036 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005037
5038 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005039 ret = nand_change_read_column_op(chip,
5040 sizeof(*p) * p->num_of_param_pages,
5041 ep, len, true);
5042 if (ret)
5043 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005044
Boris Brezillon97d90da2017-11-30 18:01:29 +01005045 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005046 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5047 != le16_to_cpu(ep->crc))) {
5048 pr_debug("fail in the CRC.\n");
5049 goto ext_out;
5050 }
5051
5052 /*
5053 * Check the signature.
5054 * Do not strictly follow the ONFI spec, maybe changed in future.
5055 */
5056 if (strncmp(ep->sig, "EPPS", 4)) {
5057 pr_debug("The signature is invalid.\n");
5058 goto ext_out;
5059 }
5060
5061 /* find the ECC section. */
5062 cursor = (uint8_t *)(ep + 1);
5063 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5064 s = ep->sections + i;
5065 if (s->type == ONFI_SECTION_TYPE_2)
5066 break;
5067 cursor += s->length * 16;
5068 }
5069 if (i == ONFI_EXT_SECTION_MAX) {
5070 pr_debug("We can not find the ECC section.\n");
5071 goto ext_out;
5072 }
5073
5074 /* get the info we want. */
5075 ecc = (struct onfi_ext_ecc_info *)cursor;
5076
Brian Norris4ae7d222013-09-16 18:20:21 -07005077 if (!ecc->codeword_size) {
5078 pr_debug("Invalid codeword size\n");
5079 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005080 }
5081
Brian Norris4ae7d222013-09-16 18:20:21 -07005082 chip->ecc_strength_ds = ecc->ecc_bits;
5083 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005084 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005085
5086ext_out:
5087 kfree(ep);
5088 return ret;
5089}
5090
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005091/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005092 * Recover data with bit-wise majority
5093 */
5094static void nand_bit_wise_majority(const void **srcbufs,
5095 unsigned int nsrcbufs,
5096 void *dstbuf,
5097 unsigned int bufsize)
5098{
5099 int i, j, k;
5100
5101 for (i = 0; i < bufsize; i++) {
5102 u8 val = 0;
5103
5104 for (j = 0; j < 8; j++) {
5105 unsigned int cnt = 0;
5106
5107 for (k = 0; k < nsrcbufs; k++) {
5108 const u8 *srcbuf = srcbufs[k];
5109
5110 if (srcbuf[i] & BIT(j))
5111 cnt++;
5112 }
5113
5114 if (cnt > nsrcbufs / 2)
5115 val |= BIT(j);
5116 }
5117
5118 ((u8 *)dstbuf)[i] = val;
5119 }
5120}
5121
5122/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005123 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005124 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005125static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005126{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005127 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005128 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005129 struct onfi_params *onfi;
5130 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005131 char id[4];
5132 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005133
Brian Norris7854d3f2011-06-23 14:12:08 -07005134 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005135 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5136 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005137 return 0;
5138
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005139 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005140 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005141 if (!p)
5142 return -ENOMEM;
5143
Boris Brezillon97d90da2017-11-30 18:01:29 +01005144 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005145 if (ret) {
5146 ret = 0;
5147 goto free_onfi_param_page;
5148 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005149
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005150 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005151 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005152 if (ret) {
5153 ret = 0;
5154 goto free_onfi_param_page;
5155 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005156
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005157 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005158 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005159 if (i)
5160 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005161 break;
5162 }
5163 }
5164
Brian Norrisc7f23a72013-08-13 10:51:55 -07005165 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005166 const void *srcbufs[3] = {p, p + 1, p + 2};
5167
5168 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5169 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5170 sizeof(*p));
5171
5172 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5173 le16_to_cpu(p->crc)) {
5174 pr_err("ONFI parameter recovery failed, aborting\n");
5175 goto free_onfi_param_page;
5176 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005177 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005178
Chris Packham00ce4e02018-06-25 10:44:44 +12005179 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5180 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5181 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5182
Brian Norris8b6e50c2011-05-25 14:59:01 -07005183 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005184 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005185 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005186 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005187 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005188 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005189 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005190 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005191 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005192 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005193 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005194 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005195
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005196 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005197 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005198 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005199 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005200
5201 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5202 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005203 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5204 if (!chip->parameters.model) {
5205 ret = -ENOMEM;
5206 goto free_onfi_param_page;
5207 }
Brian Norris4355b702013-08-27 18:45:10 -07005208
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005209 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005210
5211 /*
5212 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5213 * (don't ask me who thought of this...). MTD assumes that these
5214 * dimensions will be power-of-2, so just truncate the remaining area.
5215 */
5216 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5217 mtd->erasesize *= mtd->writesize;
5218
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005219 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005220
5221 /* See erasesize comment */
5222 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005223 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005224 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005225
Zach Brown34da5f52017-01-10 13:30:21 -06005226 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5227 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5228
Miquel Raynala97421c2018-03-19 14:47:27 +01005229 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005230 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005231
Huang Shijie10c86ba2013-05-17 11:17:26 +08005232 if (p->ecc_bits != 0xff) {
5233 chip->ecc_strength_ds = p->ecc_bits;
5234 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005235 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005236 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005237
5238 /*
5239 * The nand_flash_detect_ext_param_page() uses the
5240 * Change Read Column command which maybe not supported
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005241 * by the chip->legacy.cmdfunc. So try to update the
5242 * chip->legacy.cmdfunc now. We do not replace user supplied
5243 * command function.
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005244 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005245 if (mtd->writesize > 512 &&
5246 chip->legacy.cmdfunc == nand_command)
5247 chip->legacy.cmdfunc = nand_command_lp;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005248
5249 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005250 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005251 pr_warn("Failed to detect ONFI extended param page\n");
5252 } else {
5253 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005254 }
5255
Miquel Raynalf4531b22018-03-19 14:47:26 +01005256 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005257 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005258 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005259 bitmap_set(chip->parameters.get_feature_list,
5260 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5261 bitmap_set(chip->parameters.set_feature_list,
5262 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5263 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005264
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005265 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5266 if (!onfi) {
5267 ret = -ENOMEM;
5268 goto free_model;
5269 }
5270
5271 onfi->version = onfi_version;
5272 onfi->tPROG = le16_to_cpu(p->t_prog);
5273 onfi->tBERS = le16_to_cpu(p->t_bers);
5274 onfi->tR = le16_to_cpu(p->t_r);
5275 onfi->tCCS = le16_to_cpu(p->t_ccs);
5276 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5277 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5278 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5279 chip->parameters.onfi = onfi;
5280
5281 /* Identification done, free the full ONFI parameter page and exit */
5282 kfree(p);
5283
5284 return 1;
5285
5286free_model:
5287 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005288free_onfi_param_page:
5289 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005290
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005291 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005292}
5293
5294/*
Huang Shijie91361812014-02-21 13:39:40 +08005295 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5296 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005297static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005298{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005299 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005300 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005301 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005302 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005303 char id[5];
5304 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005305
5306 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005307 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5308 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005309 return 0;
5310
Miquel Raynal480139d2018-03-19 14:47:30 +01005311 /* JEDEC chip: allocate a buffer to hold its parameter page */
5312 p = kzalloc(sizeof(*p), GFP_KERNEL);
5313 if (!p)
5314 return -ENOMEM;
5315
Boris Brezillon97d90da2017-11-30 18:01:29 +01005316 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005317 if (ret) {
5318 ret = 0;
5319 goto free_jedec_param_page;
5320 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005321
Huang Shijie91361812014-02-21 13:39:40 +08005322 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005323 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005324 if (ret) {
5325 ret = 0;
5326 goto free_jedec_param_page;
5327 }
Huang Shijie91361812014-02-21 13:39:40 +08005328
5329 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5330 le16_to_cpu(p->crc))
5331 break;
5332 }
5333
5334 if (i == 3) {
5335 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005336 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005337 }
5338
5339 /* Check version */
5340 val = le16_to_cpu(p->revision);
5341 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005342 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005343 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005344 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005345
Miquel Raynal480139d2018-03-19 14:47:30 +01005346 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005347 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005348 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005349 }
5350
5351 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5352 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005353 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5354 if (!chip->parameters.model) {
5355 ret = -ENOMEM;
5356 goto free_jedec_param_page;
5357 }
Huang Shijie91361812014-02-21 13:39:40 +08005358
5359 mtd->writesize = le32_to_cpu(p->byte_per_page);
5360
5361 /* Please reference to the comment for nand_flash_detect_onfi. */
5362 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5363 mtd->erasesize *= mtd->writesize;
5364
5365 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5366
5367 /* Please reference to the comment for nand_flash_detect_onfi. */
5368 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5369 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5370 chip->bits_per_cell = p->bits_per_cell;
5371
Miquel Raynal480139d2018-03-19 14:47:30 +01005372 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005373 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005374
5375 /* ECC info */
5376 ecc = &p->ecc_info[0];
5377
5378 if (ecc->codeword_size >= 9) {
5379 chip->ecc_strength_ds = ecc->ecc_bits;
5380 chip->ecc_step_ds = 1 << ecc->codeword_size;
5381 } else {
5382 pr_warn("Invalid codeword size\n");
5383 }
5384
Miquel Raynal480139d2018-03-19 14:47:30 +01005385free_jedec_param_page:
5386 kfree(p);
5387 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005388}
5389
5390/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005391 * nand_id_has_period - Check if an ID string has a given wraparound period
5392 * @id_data: the ID string
5393 * @arrlen: the length of the @id_data array
5394 * @period: the period of repitition
5395 *
5396 * Check if an ID string is repeated within a given sequence of bytes at
5397 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005398 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005399 * if the repetition has a period of @period; otherwise, returns zero.
5400 */
5401static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5402{
5403 int i, j;
5404 for (i = 0; i < period; i++)
5405 for (j = i + period; j < arrlen; j += period)
5406 if (id_data[i] != id_data[j])
5407 return 0;
5408 return 1;
5409}
5410
5411/*
5412 * nand_id_len - Get the length of an ID string returned by CMD_READID
5413 * @id_data: the ID string
5414 * @arrlen: the length of the @id_data array
5415
5416 * Returns the length of the ID string, according to known wraparound/trailing
5417 * zero patterns. If no pattern exists, returns the length of the array.
5418 */
5419static int nand_id_len(u8 *id_data, int arrlen)
5420{
5421 int last_nonzero, period;
5422
5423 /* Find last non-zero byte */
5424 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5425 if (id_data[last_nonzero])
5426 break;
5427
5428 /* All zeros */
5429 if (last_nonzero < 0)
5430 return 0;
5431
5432 /* Calculate wraparound period */
5433 for (period = 1; period < arrlen; period++)
5434 if (nand_id_has_period(id_data, arrlen, period))
5435 break;
5436
5437 /* There's a repeated pattern */
5438 if (period < arrlen)
5439 return period;
5440
5441 /* There are trailing zeros */
5442 if (last_nonzero < arrlen - 1)
5443 return last_nonzero + 1;
5444
5445 /* No pattern detected */
5446 return arrlen;
5447}
5448
Huang Shijie7db906b2013-09-25 14:58:11 +08005449/* Extract the bits of per cell from the 3rd byte of the extended ID */
5450static int nand_get_bits_per_cell(u8 cellinfo)
5451{
5452 int bits;
5453
5454 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5455 bits >>= NAND_CI_CELLTYPE_SHIFT;
5456 return bits + 1;
5457}
5458
Brian Norrise3b88bd2012-09-24 20:40:52 -07005459/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005460 * Many new NAND share similar device ID codes, which represent the size of the
5461 * chip. The rest of the parameters must be decoded according to generic or
5462 * manufacturer-specific "extended ID" decoding patterns.
5463 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005464void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005465{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005466 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005467 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005468 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005469 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005470 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005471 /* The 4th id byte is the important one */
5472 extid = id_data[3];
5473
Boris Brezillon01389b62016-06-08 10:30:18 +02005474 /* Calc pagesize */
5475 mtd->writesize = 1024 << (extid & 0x03);
5476 extid >>= 2;
5477 /* Calc oobsize */
5478 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5479 extid >>= 2;
5480 /* Calc blocksize. Blocksize is multiples of 64KiB */
5481 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5482 extid >>= 2;
5483 /* Get buswidth information */
5484 if (extid & 0x1)
5485 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005486}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005487EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005488
5489/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005490 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5491 * decodes a matching ID table entry and assigns the MTD size parameters for
5492 * the chip.
5493 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005494static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005495{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005496 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005497
5498 mtd->erasesize = type->erasesize;
5499 mtd->writesize = type->pagesize;
5500 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005501
Huang Shijie1c195e92013-09-25 14:58:12 +08005502 /* All legacy ID NAND are small-page, SLC */
5503 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005504}
5505
5506/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005507 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5508 * heuristic patterns using various detected parameters (e.g., manufacturer,
5509 * page size, cell-type information).
5510 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005511static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005512{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005513 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005514
5515 /* Set the bad block position */
5516 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5517 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5518 else
5519 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005520}
5521
Huang Shijieec6e87e2013-03-15 11:01:00 +08005522static inline bool is_full_id_nand(struct nand_flash_dev *type)
5523{
5524 return type->id_len;
5525}
5526
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005527static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005528 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005529{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005530 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005531 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005532
Huang Shijieec6e87e2013-03-15 11:01:00 +08005533 if (!strncmp(type->id, id_data, type->id_len)) {
5534 mtd->writesize = type->pagesize;
5535 mtd->erasesize = type->erasesize;
5536 mtd->oobsize = type->oobsize;
5537
Huang Shijie7db906b2013-09-25 14:58:11 +08005538 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005539 chip->chipsize = (uint64_t)type->chipsize << 20;
5540 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005541 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5542 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005543 chip->onfi_timing_mode_default =
5544 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005545
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005546 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5547 if (!chip->parameters.model)
5548 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005549
Huang Shijieec6e87e2013-03-15 11:01:00 +08005550 return true;
5551 }
5552 return false;
5553}
5554
Brian Norris7e74c2d2012-09-24 20:40:49 -07005555/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005556 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5557 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5558 * table.
5559 */
5560static void nand_manufacturer_detect(struct nand_chip *chip)
5561{
5562 /*
5563 * Try manufacturer detection if available and use
5564 * nand_decode_ext_id() otherwise.
5565 */
5566 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005567 chip->manufacturer.desc->ops->detect) {
5568 /* The 3rd id byte holds MLC / multichip data */
5569 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005570 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005571 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005572 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005573 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005574}
5575
5576/*
5577 * Manufacturer initialization. This function is called for all NANDs including
5578 * ONFI and JEDEC compliant ones.
5579 * Manufacturer drivers should put all their specific initialization code in
5580 * their ->init() hook.
5581 */
5582static int nand_manufacturer_init(struct nand_chip *chip)
5583{
5584 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5585 !chip->manufacturer.desc->ops->init)
5586 return 0;
5587
5588 return chip->manufacturer.desc->ops->init(chip);
5589}
5590
5591/*
5592 * Manufacturer cleanup. This function is called for all NANDs including
5593 * ONFI and JEDEC compliant ones.
5594 * Manufacturer drivers should put all their specific cleanup code in their
5595 * ->cleanup() hook.
5596 */
5597static void nand_manufacturer_cleanup(struct nand_chip *chip)
5598{
5599 /* Release manufacturer private data */
5600 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5601 chip->manufacturer.desc->ops->cleanup)
5602 chip->manufacturer.desc->ops->cleanup(chip);
5603}
5604
5605/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005606 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005607 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005608static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005609{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005610 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005611 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005612 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005613 u8 *id_data = chip->id.data;
5614 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Karl Beldanef89a882008-09-15 14:37:29 +02005616 /*
5617 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005618 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005619 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005620 ret = nand_reset(chip, 0);
5621 if (ret)
5622 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005623
5624 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005625 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005628 ret = nand_readid_op(chip, 0, id_data, 2);
5629 if (ret)
5630 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
5632 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005633 maf_id = id_data[0];
5634 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
Brian Norris8b6e50c2011-05-25 14:59:01 -07005636 /*
5637 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005638 * interface concerns can cause random data which looks like a
5639 * possibly credible NAND flash to appear. If the two results do
5640 * not match, ignore the device completely.
5641 */
5642
Brian Norris4aef9b72012-09-24 20:40:48 -07005643 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005644 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5645 if (ret)
5646 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005647
Boris Brezillon7f501f02016-05-24 19:20:05 +02005648 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005649 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005650 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005651 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005652 }
5653
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005654 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005655
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005656 /* Try to identify manufacturer */
5657 manufacturer = nand_get_manufacturer(maf_id);
5658 chip->manufacturer.desc = manufacturer;
5659
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005660 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005661 type = nand_flash_ids;
5662
Boris Brezillon29a198a2016-05-24 20:17:48 +02005663 /*
5664 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5665 * override it.
5666 * This is required to make sure initial NAND bus width set by the
5667 * NAND controller driver is coherent with the real NAND bus width
5668 * (extracted by auto-detection code).
5669 */
5670 busw = chip->options & NAND_BUSWIDTH_16;
5671
5672 /*
5673 * The flag is only set (never cleared), reset it to its default value
5674 * before starting auto-detection.
5675 */
5676 chip->options &= ~NAND_BUSWIDTH_16;
5677
Huang Shijieec6e87e2013-03-15 11:01:00 +08005678 for (; type->name != NULL; type++) {
5679 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005680 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005681 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005682 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005683 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005684 }
5685 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005686
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005687 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005688 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005689 ret = nand_flash_detect_onfi(chip);
5690 if (ret < 0)
5691 return ret;
5692 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005693 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005694
5695 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005696 ret = nand_flash_detect_jedec(chip);
5697 if (ret < 0)
5698 return ret;
5699 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005700 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005701 }
5702
David Woodhouse5e81e882010-02-26 18:32:56 +00005703 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005704 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005705
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005706 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5707 if (!chip->parameters.model)
5708 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005709
Adrian Hunter69423d92008-12-10 13:37:21 +00005710 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005711
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005712 if (!type->pagesize)
5713 nand_manufacturer_detect(chip);
5714 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005715 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005716
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005717 /* Get chip options */
5718 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005719
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005720ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005721 if (!mtd->name)
5722 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005723
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005724 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005725 WARN_ON(busw & NAND_BUSWIDTH_16);
5726 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005727 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5728 /*
5729 * Check, if buswidth is correct. Hardware drivers should set
5730 * chip correct!
5731 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005732 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005733 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005734 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5735 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005736 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5737 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005738 ret = -EINVAL;
5739
5740 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005741 }
5742
Boris Brezillon7f501f02016-05-24 19:20:05 +02005743 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005744
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005745 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005746 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005747 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005748 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005749
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005750 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005751 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005752 if (chip->chipsize & 0xffffffff)
5753 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005754 else {
5755 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5756 chip->chip_shift += 32 - 1;
5757 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005758
Masahiro Yamada14157f82017-09-13 11:05:50 +09005759 if (chip->chip_shift - chip->page_shift > 16)
5760 chip->options |= NAND_ROW_ADDR_3;
5761
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005762 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005763
Brian Norris8b6e50c2011-05-25 14:59:01 -07005764 /* Do not replace user supplied command function! */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005765 if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
5766 chip->legacy.cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005767
Ezequiel Garcia20171642013-11-25 08:30:31 -03005768 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005769 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005770 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5771 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005772 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005773 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005774 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005775 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005776
5777free_detect_allocation:
5778 kfree(chip->parameters.model);
5779
5780 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005781}
5782
Boris Brezillond48f62b2016-04-01 14:54:32 +02005783static const char * const nand_ecc_modes[] = {
5784 [NAND_ECC_NONE] = "none",
5785 [NAND_ECC_SOFT] = "soft",
5786 [NAND_ECC_HW] = "hw",
5787 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5788 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005789 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005790};
5791
5792static int of_get_nand_ecc_mode(struct device_node *np)
5793{
5794 const char *pm;
5795 int err, i;
5796
5797 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5798 if (err < 0)
5799 return err;
5800
5801 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5802 if (!strcasecmp(pm, nand_ecc_modes[i]))
5803 return i;
5804
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005805 /*
5806 * For backward compatibility we support few obsoleted values that don't
5807 * have their mappings into nand_ecc_modes_t anymore (they were merged
5808 * with other enums).
5809 */
5810 if (!strcasecmp(pm, "soft_bch"))
5811 return NAND_ECC_SOFT;
5812
Boris Brezillond48f62b2016-04-01 14:54:32 +02005813 return -ENODEV;
5814}
5815
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005816static const char * const nand_ecc_algos[] = {
5817 [NAND_ECC_HAMMING] = "hamming",
5818 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005819 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005820};
5821
Boris Brezillond48f62b2016-04-01 14:54:32 +02005822static int of_get_nand_ecc_algo(struct device_node *np)
5823{
5824 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005825 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005826
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005827 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5828 if (!err) {
5829 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5830 if (!strcasecmp(pm, nand_ecc_algos[i]))
5831 return i;
5832 return -ENODEV;
5833 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005834
5835 /*
5836 * For backward compatibility we also read "nand-ecc-mode" checking
5837 * for some obsoleted values that were specifying ECC algorithm.
5838 */
5839 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5840 if (err < 0)
5841 return err;
5842
5843 if (!strcasecmp(pm, "soft"))
5844 return NAND_ECC_HAMMING;
5845 else if (!strcasecmp(pm, "soft_bch"))
5846 return NAND_ECC_BCH;
5847
5848 return -ENODEV;
5849}
5850
5851static int of_get_nand_ecc_step_size(struct device_node *np)
5852{
5853 int ret;
5854 u32 val;
5855
5856 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5857 return ret ? ret : val;
5858}
5859
5860static int of_get_nand_ecc_strength(struct device_node *np)
5861{
5862 int ret;
5863 u32 val;
5864
5865 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5866 return ret ? ret : val;
5867}
5868
5869static int of_get_nand_bus_width(struct device_node *np)
5870{
5871 u32 val;
5872
5873 if (of_property_read_u32(np, "nand-bus-width", &val))
5874 return 8;
5875
5876 switch (val) {
5877 case 8:
5878 case 16:
5879 return val;
5880 default:
5881 return -EIO;
5882 }
5883}
5884
5885static bool of_get_nand_on_flash_bbt(struct device_node *np)
5886{
5887 return of_property_read_bool(np, "nand-on-flash-bbt");
5888}
5889
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005890static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005891{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005892 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005893 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005894
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005895 if (!dn)
5896 return 0;
5897
Brian Norris5844fee2015-01-23 00:22:27 -08005898 if (of_get_nand_bus_width(dn) == 16)
5899 chip->options |= NAND_BUSWIDTH_16;
5900
Stefan Agnerf922bd72018-06-24 23:27:23 +02005901 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5902 chip->options |= NAND_IS_BOOT_MEDIUM;
5903
Brian Norris5844fee2015-01-23 00:22:27 -08005904 if (of_get_nand_on_flash_bbt(dn))
5905 chip->bbt_options |= NAND_BBT_USE_FLASH;
5906
5907 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005908 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005909 ecc_strength = of_get_nand_ecc_strength(dn);
5910 ecc_step = of_get_nand_ecc_step_size(dn);
5911
Brian Norris5844fee2015-01-23 00:22:27 -08005912 if (ecc_mode >= 0)
5913 chip->ecc.mode = ecc_mode;
5914
Rafał Miłecki79082452016-03-23 11:19:02 +01005915 if (ecc_algo >= 0)
5916 chip->ecc.algo = ecc_algo;
5917
Brian Norris5844fee2015-01-23 00:22:27 -08005918 if (ecc_strength >= 0)
5919 chip->ecc.strength = ecc_strength;
5920
5921 if (ecc_step > 0)
5922 chip->ecc.size = ecc_step;
5923
Boris Brezillonba78ee02016-06-08 17:04:22 +02005924 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5925 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5926
Brian Norris5844fee2015-01-23 00:22:27 -08005927 return 0;
5928}
5929
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005930/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005931 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005932 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005933 * @maxchips: number of chips to scan for
5934 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005935 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005936 * This is the first phase of the normal nand_scan() function. It reads the
5937 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005938 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005939 * This helper used to be called directly from controller drivers that needed
5940 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5941 * prevented dynamic allocations during this phase which was unconvenient and
5942 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005943 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005944static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005945 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005946{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005947 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005948 int nand_maf_id, nand_dev_id;
5949 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005950 int ret;
5951
Miquel Raynal17fa8042017-11-30 18:01:31 +01005952 /* Enforce the right timings for reset/detection */
5953 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5954
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005955 ret = nand_dt_init(chip);
5956 if (ret)
5957 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005958
Brian Norrisf7a8e382016-01-05 10:39:45 -08005959 if (!mtd->name && mtd->dev.parent)
5960 mtd->name = dev_name(mtd->dev.parent);
5961
Miquel Raynal8878b122017-11-09 14:16:45 +01005962 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005963 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
5964 * not populated.
Miquel Raynal8878b122017-11-09 14:16:45 +01005965 */
5966 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005967 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005968 * Default functions assigned for ->legacy.cmdfunc() and
5969 * ->select_chip() both expect ->legacy.cmd_ctrl() to be
5970 * populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005971 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005972 if ((!chip->legacy.cmdfunc || !chip->select_chip) &&
5973 !chip->legacy.cmd_ctrl) {
5974 pr_err("->legacy.cmd_ctrl() should be provided\n");
Miquel Raynal8878b122017-11-09 14:16:45 +01005975 return -EINVAL;
5976 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005977 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005978
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005979 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005980 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005981
5982 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005983 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005984 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005985 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005986 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005987 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005988 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 }
5990
Boris Brezillon7f501f02016-05-24 19:20:05 +02005991 nand_maf_id = chip->id.data[0];
5992 nand_dev_id = chip->id.data[1];
5993
Boris Brezillon758b56f2018-09-06 14:05:24 +02005994 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005995
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005996 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005997 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005998 u8 id[2];
5999
Karl Beldanef89a882008-09-15 14:37:29 +02006000 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02006001 nand_reset(chip, i);
6002
Boris Brezillon758b56f2018-09-06 14:05:24 +02006003 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01006005 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01006007 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02006008 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 break;
Huang Shijie07300162012-11-09 16:23:45 +08006010 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02006011 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 }
6013 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03006014 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006017 chip->numchips = i;
6018 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
David Woodhouse3b85c322006-09-25 17:06:53 +01006020 return 0;
6021}
6022
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006023static void nand_scan_ident_cleanup(struct nand_chip *chip)
6024{
6025 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006026 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006027}
6028
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006029static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6030{
6031 struct nand_chip *chip = mtd_to_nand(mtd);
6032 struct nand_ecc_ctrl *ecc = &chip->ecc;
6033
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006034 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006035 return -EINVAL;
6036
6037 switch (ecc->algo) {
6038 case NAND_ECC_HAMMING:
6039 ecc->calculate = nand_calculate_ecc;
6040 ecc->correct = nand_correct_data;
6041 ecc->read_page = nand_read_page_swecc;
6042 ecc->read_subpage = nand_read_subpage;
6043 ecc->write_page = nand_write_page_swecc;
6044 ecc->read_page_raw = nand_read_page_raw;
6045 ecc->write_page_raw = nand_write_page_raw;
6046 ecc->read_oob = nand_read_oob_std;
6047 ecc->write_oob = nand_write_oob_std;
6048 if (!ecc->size)
6049 ecc->size = 256;
6050 ecc->bytes = 3;
6051 ecc->strength = 1;
6052 return 0;
6053 case NAND_ECC_BCH:
6054 if (!mtd_nand_has_bch()) {
6055 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6056 return -EINVAL;
6057 }
6058 ecc->calculate = nand_bch_calculate_ecc;
6059 ecc->correct = nand_bch_correct_data;
6060 ecc->read_page = nand_read_page_swecc;
6061 ecc->read_subpage = nand_read_subpage;
6062 ecc->write_page = nand_write_page_swecc;
6063 ecc->read_page_raw = nand_read_page_raw;
6064 ecc->write_page_raw = nand_write_page_raw;
6065 ecc->read_oob = nand_read_oob_std;
6066 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006067
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006068 /*
6069 * Board driver should supply ecc.size and ecc.strength
6070 * values to select how many bits are correctable.
6071 * Otherwise, default to 4 bits for large page devices.
6072 */
6073 if (!ecc->size && (mtd->oobsize >= 64)) {
6074 ecc->size = 512;
6075 ecc->strength = 4;
6076 }
6077
6078 /*
6079 * if no ecc placement scheme was provided pickup the default
6080 * large page one.
6081 */
6082 if (!mtd->ooblayout) {
6083 /* handle large page devices only */
6084 if (mtd->oobsize < 64) {
6085 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6086 return -EINVAL;
6087 }
6088
6089 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006090
6091 }
6092
6093 /*
6094 * We can only maximize ECC config when the default layout is
6095 * used, otherwise we don't know how many bytes can really be
6096 * used.
6097 */
6098 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6099 ecc->options & NAND_ECC_MAXIMIZE) {
6100 int steps, bytes;
6101
6102 /* Always prefer 1k blocks over 512bytes ones */
6103 ecc->size = 1024;
6104 steps = mtd->writesize / ecc->size;
6105
6106 /* Reserve 2 bytes for the BBM */
6107 bytes = (mtd->oobsize - 2) / steps;
6108 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006109 }
6110
6111 /* See nand_bch_init() for details. */
6112 ecc->bytes = 0;
6113 ecc->priv = nand_bch_init(mtd);
6114 if (!ecc->priv) {
6115 WARN(1, "BCH ECC initialization failed!\n");
6116 return -EINVAL;
6117 }
6118 return 0;
6119 default:
6120 WARN(1, "Unsupported ECC algorithm!\n");
6121 return -EINVAL;
6122 }
6123}
6124
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006125/**
6126 * nand_check_ecc_caps - check the sanity of preset ECC settings
6127 * @chip: nand chip info structure
6128 * @caps: ECC caps info structure
6129 * @oobavail: OOB size that the ECC engine can use
6130 *
6131 * When ECC step size and strength are already set, check if they are supported
6132 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6133 * On success, the calculated ECC bytes is set.
6134 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306135static int
6136nand_check_ecc_caps(struct nand_chip *chip,
6137 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006138{
6139 struct mtd_info *mtd = nand_to_mtd(chip);
6140 const struct nand_ecc_step_info *stepinfo;
6141 int preset_step = chip->ecc.size;
6142 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306143 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006144 int i, j;
6145
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006146 for (i = 0; i < caps->nstepinfos; i++) {
6147 stepinfo = &caps->stepinfos[i];
6148
6149 if (stepinfo->stepsize != preset_step)
6150 continue;
6151
6152 for (j = 0; j < stepinfo->nstrengths; j++) {
6153 if (stepinfo->strengths[j] != preset_strength)
6154 continue;
6155
6156 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6157 preset_strength);
6158 if (WARN_ON_ONCE(ecc_bytes < 0))
6159 return ecc_bytes;
6160
6161 if (ecc_bytes * nsteps > oobavail) {
6162 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6163 preset_step, preset_strength);
6164 return -ENOSPC;
6165 }
6166
6167 chip->ecc.bytes = ecc_bytes;
6168
6169 return 0;
6170 }
6171 }
6172
6173 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6174 preset_step, preset_strength);
6175
6176 return -ENOTSUPP;
6177}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006178
6179/**
6180 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6181 * @chip: nand chip info structure
6182 * @caps: ECC engine caps info structure
6183 * @oobavail: OOB size that the ECC engine can use
6184 *
6185 * If a chip's ECC requirement is provided, try to meet it with the least
6186 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6187 * On success, the chosen ECC settings are set.
6188 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306189static int
6190nand_match_ecc_req(struct nand_chip *chip,
6191 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006192{
6193 struct mtd_info *mtd = nand_to_mtd(chip);
6194 const struct nand_ecc_step_info *stepinfo;
6195 int req_step = chip->ecc_step_ds;
6196 int req_strength = chip->ecc_strength_ds;
6197 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6198 int best_step, best_strength, best_ecc_bytes;
6199 int best_ecc_bytes_total = INT_MAX;
6200 int i, j;
6201
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006202 /* No information provided by the NAND chip */
6203 if (!req_step || !req_strength)
6204 return -ENOTSUPP;
6205
6206 /* number of correctable bits the chip requires in a page */
6207 req_corr = mtd->writesize / req_step * req_strength;
6208
6209 for (i = 0; i < caps->nstepinfos; i++) {
6210 stepinfo = &caps->stepinfos[i];
6211 step_size = stepinfo->stepsize;
6212
6213 for (j = 0; j < stepinfo->nstrengths; j++) {
6214 strength = stepinfo->strengths[j];
6215
6216 /*
6217 * If both step size and strength are smaller than the
6218 * chip's requirement, it is not easy to compare the
6219 * resulted reliability.
6220 */
6221 if (step_size < req_step && strength < req_strength)
6222 continue;
6223
6224 if (mtd->writesize % step_size)
6225 continue;
6226
6227 nsteps = mtd->writesize / step_size;
6228
6229 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6230 if (WARN_ON_ONCE(ecc_bytes < 0))
6231 continue;
6232 ecc_bytes_total = ecc_bytes * nsteps;
6233
6234 if (ecc_bytes_total > oobavail ||
6235 strength * nsteps < req_corr)
6236 continue;
6237
6238 /*
6239 * We assume the best is to meet the chip's requrement
6240 * with the least number of ECC bytes.
6241 */
6242 if (ecc_bytes_total < best_ecc_bytes_total) {
6243 best_ecc_bytes_total = ecc_bytes_total;
6244 best_step = step_size;
6245 best_strength = strength;
6246 best_ecc_bytes = ecc_bytes;
6247 }
6248 }
6249 }
6250
6251 if (best_ecc_bytes_total == INT_MAX)
6252 return -ENOTSUPP;
6253
6254 chip->ecc.size = best_step;
6255 chip->ecc.strength = best_strength;
6256 chip->ecc.bytes = best_ecc_bytes;
6257
6258 return 0;
6259}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006260
6261/**
6262 * nand_maximize_ecc - choose the max ECC strength available
6263 * @chip: nand chip info structure
6264 * @caps: ECC engine caps info structure
6265 * @oobavail: OOB size that the ECC engine can use
6266 *
6267 * Choose the max ECC strength that is supported on the controller, and can fit
6268 * within the chip's OOB. On success, the chosen ECC settings are set.
6269 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306270static int
6271nand_maximize_ecc(struct nand_chip *chip,
6272 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006273{
6274 struct mtd_info *mtd = nand_to_mtd(chip);
6275 const struct nand_ecc_step_info *stepinfo;
6276 int step_size, strength, nsteps, ecc_bytes, corr;
6277 int best_corr = 0;
6278 int best_step = 0;
6279 int best_strength, best_ecc_bytes;
6280 int i, j;
6281
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006282 for (i = 0; i < caps->nstepinfos; i++) {
6283 stepinfo = &caps->stepinfos[i];
6284 step_size = stepinfo->stepsize;
6285
6286 /* If chip->ecc.size is already set, respect it */
6287 if (chip->ecc.size && step_size != chip->ecc.size)
6288 continue;
6289
6290 for (j = 0; j < stepinfo->nstrengths; j++) {
6291 strength = stepinfo->strengths[j];
6292
6293 if (mtd->writesize % step_size)
6294 continue;
6295
6296 nsteps = mtd->writesize / step_size;
6297
6298 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6299 if (WARN_ON_ONCE(ecc_bytes < 0))
6300 continue;
6301
6302 if (ecc_bytes * nsteps > oobavail)
6303 continue;
6304
6305 corr = strength * nsteps;
6306
6307 /*
6308 * If the number of correctable bits is the same,
6309 * bigger step_size has more reliability.
6310 */
6311 if (corr > best_corr ||
6312 (corr == best_corr && step_size > best_step)) {
6313 best_corr = corr;
6314 best_step = step_size;
6315 best_strength = strength;
6316 best_ecc_bytes = ecc_bytes;
6317 }
6318 }
6319 }
6320
6321 if (!best_corr)
6322 return -ENOTSUPP;
6323
6324 chip->ecc.size = best_step;
6325 chip->ecc.strength = best_strength;
6326 chip->ecc.bytes = best_ecc_bytes;
6327
6328 return 0;
6329}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006330
Abhishek Sahu181ace92018-06-20 12:57:28 +05306331/**
6332 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6333 * @chip: nand chip info structure
6334 * @caps: ECC engine caps info structure
6335 * @oobavail: OOB size that the ECC engine can use
6336 *
6337 * Choose the ECC configuration according to following logic
6338 *
6339 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6340 * then check if it is supported by this controller.
6341 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6342 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6343 * to the chip's requirement. If available OOB size can't fit the chip
6344 * requirement then fallback to the maximum ECC step size and ECC strength.
6345 *
6346 * On success, the chosen ECC settings are set.
6347 */
6348int nand_ecc_choose_conf(struct nand_chip *chip,
6349 const struct nand_ecc_caps *caps, int oobavail)
6350{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306351 struct mtd_info *mtd = nand_to_mtd(chip);
6352
6353 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6354 return -EINVAL;
6355
Abhishek Sahu181ace92018-06-20 12:57:28 +05306356 if (chip->ecc.size && chip->ecc.strength)
6357 return nand_check_ecc_caps(chip, caps, oobavail);
6358
6359 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6360 return nand_maximize_ecc(chip, caps, oobavail);
6361
6362 if (!nand_match_ecc_req(chip, caps, oobavail))
6363 return 0;
6364
6365 return nand_maximize_ecc(chip, caps, oobavail);
6366}
6367EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6368
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006369/*
6370 * Check if the chip configuration meet the datasheet requirements.
6371
6372 * If our configuration corrects A bits per B bytes and the minimum
6373 * required correction level is X bits per Y bytes, then we must ensure
6374 * both of the following are true:
6375 *
6376 * (1) A / B >= X / Y
6377 * (2) A >= X
6378 *
6379 * Requirement (1) ensures we can correct for the required bitflip density.
6380 * Requirement (2) ensures we can correct even when all bitflips are clumped
6381 * in the same sector.
6382 */
6383static bool nand_ecc_strength_good(struct mtd_info *mtd)
6384{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006385 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006386 struct nand_ecc_ctrl *ecc = &chip->ecc;
6387 int corr, ds_corr;
6388
6389 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6390 /* Not enough information */
6391 return true;
6392
6393 /*
6394 * We get the number of corrected bits per page to compare
6395 * the correction density.
6396 */
6397 corr = (mtd->writesize * ecc->strength) / ecc->size;
6398 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6399
6400 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6401}
David Woodhouse3b85c322006-09-25 17:06:53 +01006402
6403/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006404 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006405 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006406 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006407 * This is the second phase of the normal nand_scan() function. It fills out
6408 * all the uninitialized function pointers with the defaults and scans for a
6409 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006410 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006411static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006412{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006413 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006414 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006415 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006416
Brian Norrise2414f42012-02-06 13:44:00 -08006417 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006418 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006419 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006420 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006421 }
Brian Norrise2414f42012-02-06 13:44:00 -08006422
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006423 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006424 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006425 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006426
Boris Brezillonf84674b2017-06-02 12:18:24 +02006427 /*
6428 * FIXME: some NAND manufacturer drivers expect the first die to be
6429 * selected when manufacturer->init() is called. They should be fixed
6430 * to explictly select the relevant die when interacting with the NAND
6431 * chip.
6432 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02006433 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006434 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02006435 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006436 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006437 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006438
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006439 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006440 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006441
6442 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006443 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006444 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006445 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006446 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006447 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006450 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 break;
6452 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006453 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006454 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006457 /*
6458 * Expose the whole OOB area to users if ECC_NONE
6459 * is passed. We could do that for all kind of
6460 * ->oobsize, but we must keep the old large/small
6461 * page with ECC layout when ->oobsize <= 128 for
6462 * compatibility reasons.
6463 */
6464 if (ecc->mode == NAND_ECC_NONE) {
6465 mtd_set_ooblayout(mtd,
6466 &nand_ooblayout_lp_ops);
6467 break;
6468 }
6469
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006470 WARN(1, "No oob scheme defined for oobsize %d\n",
6471 mtd->oobsize);
6472 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006473 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
6475 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006476
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006477 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006478 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006479 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006480 */
David Woodhouse956e9442006-09-25 17:12:39 +01006481
Huang Shijie97de79e02013-10-18 14:20:53 +08006482 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006483 case NAND_ECC_HW_OOB_FIRST:
6484 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006485 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006486 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6487 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006488 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006489 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006490 if (!ecc->read_page)
6491 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006492
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006493 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006494 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006495 if (!ecc->read_page)
6496 ecc->read_page = nand_read_page_hwecc;
6497 if (!ecc->write_page)
6498 ecc->write_page = nand_write_page_hwecc;
6499 if (!ecc->read_page_raw)
6500 ecc->read_page_raw = nand_read_page_raw;
6501 if (!ecc->write_page_raw)
6502 ecc->write_page_raw = nand_write_page_raw;
6503 if (!ecc->read_oob)
6504 ecc->read_oob = nand_read_oob_std;
6505 if (!ecc->write_oob)
6506 ecc->write_oob = nand_write_oob_std;
6507 if (!ecc->read_subpage)
6508 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006509 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006510 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006511
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006512 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006513 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6514 (!ecc->read_page ||
6515 ecc->read_page == nand_read_page_hwecc ||
6516 !ecc->write_page ||
6517 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006518 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6519 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006520 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006521 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006522 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006523 if (!ecc->read_page)
6524 ecc->read_page = nand_read_page_syndrome;
6525 if (!ecc->write_page)
6526 ecc->write_page = nand_write_page_syndrome;
6527 if (!ecc->read_page_raw)
6528 ecc->read_page_raw = nand_read_page_raw_syndrome;
6529 if (!ecc->write_page_raw)
6530 ecc->write_page_raw = nand_write_page_raw_syndrome;
6531 if (!ecc->read_oob)
6532 ecc->read_oob = nand_read_oob_syndrome;
6533 if (!ecc->write_oob)
6534 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006535
Huang Shijie97de79e02013-10-18 14:20:53 +08006536 if (mtd->writesize >= ecc->size) {
6537 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006538 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6539 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006540 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006541 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006542 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006543 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006544 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6545 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006546 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006547 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006549 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006550 ret = nand_set_ecc_soft_ops(mtd);
6551 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006552 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006553 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006554 }
6555 break;
6556
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006557 case NAND_ECC_ON_DIE:
6558 if (!ecc->read_page || !ecc->write_page) {
6559 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6560 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006561 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006562 }
6563 if (!ecc->read_oob)
6564 ecc->read_oob = nand_read_oob_std;
6565 if (!ecc->write_oob)
6566 ecc->write_oob = nand_write_oob_std;
6567 break;
6568
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006569 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006570 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006571 ecc->read_page = nand_read_page_raw;
6572 ecc->write_page = nand_write_page_raw;
6573 ecc->read_oob = nand_read_oob_std;
6574 ecc->read_page_raw = nand_read_page_raw;
6575 ecc->write_page_raw = nand_write_page_raw;
6576 ecc->write_oob = nand_write_oob_std;
6577 ecc->size = mtd->writesize;
6578 ecc->bytes = 0;
6579 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006581
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006583 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6584 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006585 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006588 if (ecc->correct || ecc->calculate) {
6589 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6590 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6591 if (!ecc->calc_buf || !ecc->code_buf) {
6592 ret = -ENOMEM;
6593 goto err_nand_manuf_cleanup;
6594 }
6595 }
6596
Brian Norris9ce244b2011-08-30 18:45:37 -07006597 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006598 if (!ecc->read_oob_raw)
6599 ecc->read_oob_raw = ecc->read_oob;
6600 if (!ecc->write_oob_raw)
6601 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006602
Boris Brezillon846031d2016-02-03 20:11:00 +01006603 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006604 mtd->ecc_strength = ecc->strength;
6605 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006606
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006607 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006608 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006609 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006610 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006611 ecc->steps = mtd->writesize / ecc->size;
6612 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006613 WARN(1, "Invalid ECC parameters\n");
6614 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006615 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006617 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006618 if (ecc->total > mtd->oobsize) {
6619 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6620 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006621 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006622 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006623
Boris Brezillon846031d2016-02-03 20:11:00 +01006624 /*
6625 * The number of bytes available for a client to place data into
6626 * the out of band area.
6627 */
6628 ret = mtd_ooblayout_count_freebytes(mtd);
6629 if (ret < 0)
6630 ret = 0;
6631
6632 mtd->oobavail = ret;
6633
6634 /* ECC sanity check: warn if it's too weak */
6635 if (!nand_ecc_strength_good(mtd))
6636 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6637 mtd->name);
6638
Brian Norris8b6e50c2011-05-25 14:59:01 -07006639 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006640 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006641 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006642 case 2:
6643 mtd->subpage_sft = 1;
6644 break;
6645 case 4:
6646 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006647 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006648 mtd->subpage_sft = 2;
6649 break;
6650 }
6651 }
6652 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6653
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006654 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006655 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006658 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006660 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306661 switch (ecc->mode) {
6662 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306663 if (chip->page_shift > 9)
6664 chip->options |= NAND_SUBPAGE_READ;
6665 break;
6666
6667 default:
6668 break;
6669 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006670
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006672 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006673 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6674 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006675 mtd->_erase = nand_erase;
6676 mtd->_point = NULL;
6677 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006678 mtd->_panic_write = panic_nand_write;
6679 mtd->_read_oob = nand_read_oob;
6680 mtd->_write_oob = nand_write_oob;
6681 mtd->_sync = nand_sync;
6682 mtd->_lock = NULL;
6683 mtd->_unlock = NULL;
6684 mtd->_suspend = nand_suspend;
6685 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006686 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006687 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006688 mtd->_block_isbad = nand_block_isbad;
6689 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006690 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006691 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006693 /*
6694 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6695 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6696 * properly set.
6697 */
6698 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006699 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
Boris Brezillonf84674b2017-06-02 12:18:24 +02006701 /* Initialize the ->data_interface field. */
6702 ret = nand_init_data_interface(chip);
6703 if (ret)
6704 goto err_nand_manuf_cleanup;
6705
6706 /* Enter fastest possible mode on all dies. */
6707 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006708 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006709 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006710 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006711 }
6712
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006713 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006714 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
6717 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006718 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006719 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006720 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006721
Brian Norris44d41822017-05-01 17:04:50 -07006722 return 0;
6723
Boris Brezillonf84674b2017-06-02 12:18:24 +02006724
6725err_nand_manuf_cleanup:
6726 nand_manufacturer_cleanup(chip);
6727
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006728err_free_buf:
6729 kfree(chip->data_buf);
6730 kfree(ecc->code_buf);
6731 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006732
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006733 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734}
6735
Miquel Raynal05b54c72018-07-19 01:05:46 +02006736static int nand_attach(struct nand_chip *chip)
6737{
6738 if (chip->controller->ops && chip->controller->ops->attach_chip)
6739 return chip->controller->ops->attach_chip(chip);
6740
6741 return 0;
6742}
6743
6744static void nand_detach(struct nand_chip *chip)
6745{
6746 if (chip->controller->ops && chip->controller->ops->detach_chip)
6747 chip->controller->ops->detach_chip(chip);
6748}
6749
David Woodhouse3b85c322006-09-25 17:06:53 +01006750/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006751 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006752 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02006753 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006754 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006755 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006756 * This fills out all the uninitialized function pointers with the defaults.
6757 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006758 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006759 */
Boris Brezillon871a4072018-08-04 22:59:22 +02006760int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006761 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006762{
6763 int ret;
6764
Boris Brezillon800342d2018-08-04 22:59:23 +02006765 if (!maxchips)
6766 return -EINVAL;
6767
6768 ret = nand_scan_ident(chip, maxchips, ids);
6769 if (ret)
6770 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006771
6772 ret = nand_attach(chip);
6773 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006774 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006775
Boris Brezillon00ad3782018-09-06 14:05:14 +02006776 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006777 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006778 goto detach_chip;
6779
6780 return 0;
6781
6782detach_chip:
6783 nand_detach(chip);
6784cleanup_ident:
6785 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006786
David Woodhouse3b85c322006-09-25 17:06:53 +01006787 return ret;
6788}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006789EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006792 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6793 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006794 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006795void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006797 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006798 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006799 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6800
Jesper Juhlfa671642005-11-07 01:01:27 -08006801 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006802 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006803 kfree(chip->data_buf);
6804 kfree(chip->ecc.code_buf);
6805 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006806
6807 /* Free bad block descriptor memory */
6808 if (chip->badblock_pattern && chip->badblock_pattern->options
6809 & NAND_BBT_DYNAMICSTRUCT)
6810 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006811
6812 /* Free manufacturer priv data. */
6813 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006814
6815 /* Free controller specific allocations after chip identification */
6816 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006817
6818 /* Free identification phase allocations */
6819 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006821
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006822EXPORT_SYMBOL_GPL(nand_cleanup);
6823
6824/**
6825 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6826 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006827 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006828 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006829void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006830{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006831 mtd_device_unregister(nand_to_mtd(chip));
6832 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006833}
David Woodhousee0c7d762006-05-13 18:07:53 +01006834EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006835
David Woodhousee0c7d762006-05-13 18:07:53 +01006836MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006837MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6838MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006839MODULE_DESCRIPTION("Generic NAND flash driver code");