blob: 2f8bbc3bca7a880e04c8ca99361c831f10c395e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
Miquel Raynalf7f8c172017-07-05 08:51:09 +020068 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010072 } else {
Miquel Raynalf7f8c172017-07-05 08:51:09 +020073 if (mtd->oobsize == 8)
74 return -ERANGE;
75
Boris Brezillon41b207a2016-02-03 19:06:15 +010076 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
Miquel Raynal882fd152017-08-26 17:19:15 +0200118 if (section || !ecc->total)
Boris Brezillon41b207a2016-02-03 19:06:15 +0100119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200147
Alexander Couzens6a623e02017-05-02 12:19:00 +0200148/*
149 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
150 * are placed at a fixed offset.
151 */
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100219 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530220 int ret = 0;
221
222 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
228 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700230 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530231 ret = -EINVAL;
232 }
233
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530234 return ret;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/**
238 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800241 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100243static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100245 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200247 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * nand_read_byte - [DEFAULT] read one byte from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200257 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700259 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200261static uint8_t nand_read_byte(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200263 return readb(chip->legacy.IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Masanari Iida064a7692012-11-09 23:20:58 +0900267 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Boris Brezillon7e534322018-09-06 14:05:22 +0200268 * @chip: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700270 * Default read function for 16bit buswidth with endianness conversion.
271 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200273static uint8_t nand_read_byte16(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200275 return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * nand_select_chip - [DEFAULT] control CE line
Boris Brezillon758b56f2018-09-06 14:05:24 +0200280 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700281 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
283 * Default select function for 1 chip devices.
284 */
Boris Brezillon758b56f2018-09-06 14:05:24 +0200285static void nand_select_chip(struct nand_chip *chip, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200287 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case -1:
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200289 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
290 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294
295 default:
296 BUG();
297 }
298}
299
300/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100301 * nand_write_byte - [DEFAULT] write single byte to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200302 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100303 * @byte: value to write
304 *
305 * Default function to write a byte to I/O[7:0]
306 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200307static void nand_write_byte(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100308{
Boris Brezillon716bbba2018-09-07 00:38:35 +0200309 chip->legacy.write_buf(chip, &byte, 1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100310}
311
312/**
313 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
Boris Brezillonc0739d82018-09-06 14:05:23 +0200314 * @chip: NAND chip object
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100315 * @byte: value to write
316 *
317 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
318 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200319static void nand_write_byte16(struct nand_chip *chip, uint8_t byte)
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100320{
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100321 uint16_t word = byte;
322
323 /*
324 * It's not entirely clear what should happen to I/O[15:8] when writing
325 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
326 *
327 * When the host supports a 16-bit bus width, only data is
328 * transferred at the 16-bit width. All address and command line
329 * transfers shall use only the lower 8-bits of the data bus. During
330 * command transfers, the host may place any value on the upper
331 * 8-bits of the data bus. During address transfers, the host shall
332 * set the upper 8-bits of the data bus to 00h.
333 *
Miquel Raynalb9587582018-03-19 14:47:19 +0100334 * One user of the write_byte callback is nand_set_features. The
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100335 * four parameters are specified to be written to I/O[7:0], but this is
336 * neither an address nor a command transfer. Let's assume a 0 on the
337 * upper I/O lines is OK.
338 */
Boris Brezillon716bbba2018-09-07 00:38:35 +0200339 chip->legacy.write_buf(chip, (uint8_t *)&word, 2);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100340}
341
342/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * nand_write_buf - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200344 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700345 * @buf: data buffer
346 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700348 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200350static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200352 iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000356 * nand_read_buf - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200357 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700358 * @buf: buffer to store date
359 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700361 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200363static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Boris Brezillon82fc5092018-09-07 00:38:34 +0200365 ioread8_rep(chip->legacy.IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * nand_write_buf16 - [DEFAULT] write buffer to chip
Boris Brezillonc0739d82018-09-06 14:05:23 +0200370 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700371 * @buf: data buffer
372 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700374 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 */
Boris Brezillonc0739d82018-09-06 14:05:23 +0200376static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf,
377 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380
Boris Brezillon82fc5092018-09-07 00:38:34 +0200381 iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Boris Brezillon7e534322018-09-06 14:05:22 +0200386 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700387 * @buf: buffer to store date
388 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700390 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200392static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Boris Brezillon82fc5092018-09-07 00:38:34 +0200396 ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Boris Brezillonc17556f2018-09-06 14:05:25 +0200401 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700402 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000404 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200406static int nand_block_bad(struct nand_chip *chip, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200408 struct mtd_info *mtd = nand_to_mtd(chip);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900409 int page, page_end, res;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900410 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Brian Norris5fb15492011-05-31 16:31:21 -0700412 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700413 ofs += mtd->erasesize - mtd->writesize;
414
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100415 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900416 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100417
Masahiro Yamadac120e752017-03-23 05:07:01 +0900418 for (; page < page_end; page++) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200419 res = chip->ecc.read_oob(chip, page);
Abhishek Sahue9893e62018-06-13 14:32:36 +0530420 if (res < 0)
Masahiro Yamadac120e752017-03-23 05:07:01 +0900421 return res;
422
423 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000424
Brian Norriscdbec052012-01-13 18:11:48 -0800425 if (likely(chip->badblockbits == 8))
426 res = bad != 0xFF;
427 else
428 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900429 if (res)
430 return res;
431 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200432
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700437 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Boris Brezillonc17556f2018-09-06 14:05:25 +0200438 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700439 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700441 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700442 * specific driver. It provides the details for writing a bad block marker to a
443 * block.
444 */
Boris Brezillonc17556f2018-09-06 14:05:25 +0200445static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs)
Brian Norris5a0edb22013-07-30 17:52:58 -0700446{
Boris Brezillonc17556f2018-09-06 14:05:25 +0200447 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris5a0edb22013-07-30 17:52:58 -0700448 struct mtd_oob_ops ops;
449 uint8_t buf[2] = { 0, 0 };
450 int ret = 0, res, i = 0;
451
Brian Norris0ec56dc2015-02-28 02:02:30 -0800452 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700453 ops.oobbuf = buf;
454 ops.ooboffs = chip->badblockpos;
455 if (chip->options & NAND_BUSWIDTH_16) {
456 ops.ooboffs &= ~0x01;
457 ops.len = ops.ooblen = 2;
458 } else {
459 ops.len = ops.ooblen = 1;
460 }
461 ops.mode = MTD_OPS_PLACE_OOB;
462
463 /* Write to first/last page(s) if necessary */
464 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
465 ofs += mtd->erasesize - mtd->writesize;
466 do {
467 res = nand_do_write_oob(mtd, ofs, &ops);
468 if (!ret)
469 ret = res;
470
471 i++;
472 ofs += mtd->writesize;
473 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
474
475 return ret;
476}
477
478/**
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200479 * nand_markbad_bbm - mark a block by updating the BBM
480 * @chip: NAND chip object
481 * @ofs: offset of the block to mark bad
482 */
483int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs)
484{
485 if (chip->legacy.block_markbad)
486 return chip->legacy.block_markbad(chip, ofs);
487
488 return nand_default_block_markbad(chip, ofs);
489}
490
491static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
492{
493 if (chip->legacy.block_bad)
494 return chip->legacy.block_bad(chip, ofs);
495
496 return nand_block_bad(chip, ofs);
497}
498
499/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700500 * nand_block_markbad_lowlevel - mark a block bad
501 * @mtd: MTD device structure
502 * @ofs: offset from device start
503 *
504 * This function performs the generic NAND bad block marking steps (i.e., bad
505 * block table(s) and/or marker(s)). We only allow the hardware driver to
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200506 * specify how to write bad block markers to OOB (chip->legacy.block_markbad).
Brian Norris5a0edb22013-07-30 17:52:58 -0700507 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700508 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300509 *
Brian Norrise2414f42012-02-06 13:44:00 -0800510 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700511 * (2) write bad block marker to OOB area of affected block (unless flag
512 * NAND_BBT_NO_OOB_BBM is present)
513 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300514 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700515 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800516 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700518static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100520 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700521 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000522
Brian Norrisb32843b2013-07-30 17:52:59 -0700523 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800524 struct erase_info einfo;
525
526 /* Attempt erase before marking OOB */
527 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800528 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300529 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200530 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800531
Brian Norrisb32843b2013-07-30 17:52:59 -0700532 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800533 nand_get_device(mtd, FL_WRITING);
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200534 ret = nand_markbad_bbm(chip, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300535 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200536 }
Brian Norrise2414f42012-02-06 13:44:00 -0800537
Brian Norrisb32843b2013-07-30 17:52:59 -0700538 /* Mark block bad in BBT */
539 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200540 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800541 if (!ret)
542 ret = res;
543 }
544
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200545 if (!ret)
546 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300547
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000551/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700553 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700555 * Check, if the device is write protected. The function expects, that the
556 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100558static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100560 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100561 u8 status;
562 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200563
Brian Norris8b6e50c2011-05-25 14:59:01 -0700564 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200565 if (chip->options & NAND_BROKEN_XD)
566 return 0;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100569 ret = nand_status_op(chip, &status);
570 if (ret)
571 return ret;
572
573 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800577 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700578 * @mtd: MTD device structure
579 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300580 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800581 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300582 */
583static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
584{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100585 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300586
587 if (!chip->bbt)
588 return 0;
589 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200590 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300591}
592
593/**
594 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
595 * @mtd: MTD device structure
596 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700597 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
599 * Check, if the block is bad. Either by reading the bad block table or
600 * calling of the scan function.
601 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530602static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100604 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* Return info from the table */
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200607 if (chip->bbt)
608 return nand_isbad_bbt(chip, ofs, allowbbt);
609
610 return nand_isbad_bbm(chip, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200613/**
614 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700615 * @mtd: MTD device structure
616 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200617 *
618 * Helper function for nand_wait_ready used when needing to wait in interrupt
619 * context.
620 */
621static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
622{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100623 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200624 int i;
625
626 /* Wait for the device to get ready */
627 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +0200628 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200629 break;
630 touch_softlockup_watchdog();
631 mdelay(1);
632 }
633}
634
Alex Smithb70af9b2015-10-06 14:52:07 +0100635/**
636 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200637 * @chip: NAND chip object
Alex Smithb70af9b2015-10-06 14:52:07 +0100638 *
639 * Wait for the ready pin after a command, and warn if a timeout occurs.
640 */
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200641void nand_wait_ready(struct nand_chip *chip)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000642{
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200643 struct mtd_info *mtd = nand_to_mtd(chip);
Alex Smithb70af9b2015-10-06 14:52:07 +0100644 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200646 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200648
Brian Norris7854d3f2011-06-23 14:12:08 -0700649 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000651 do {
Boris Brezillon8395b752018-09-07 00:38:37 +0200652 if (chip->legacy.dev_ready(chip))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300653 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100654 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000655 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100656
Boris Brezillon8395b752018-09-07 00:38:37 +0200657 if (!chip->legacy.dev_ready(chip))
Brian Norris9ebfdf52016-03-04 17:19:23 -0800658 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000659}
David Woodhouse4b648b02006-09-25 17:05:24 +0100660EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200663 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
664 * @mtd: MTD device structure
665 * @timeo: Timeout in ms
666 *
667 * Wait for status ready (i.e. command done) or timeout.
668 */
669static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
670{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100671 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100672 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200673
674 timeo = jiffies + msecs_to_jiffies(timeo);
675 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100676 u8 status;
677
678 ret = nand_read_data_op(chip, &status, sizeof(status), true);
679 if (ret)
680 return;
681
682 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200683 break;
684 touch_softlockup_watchdog();
685 } while (time_before(jiffies, timeo));
686};
687
688/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100689 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
690 * @chip: NAND chip structure
691 * @timeout_ms: Timeout in ms
692 *
693 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
694 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
695 * returned.
696 *
697 * This helper is intended to be used when the controller does not have access
698 * to the NAND R/B pin.
699 *
700 * Be aware that calling this helper from an ->exec_op() implementation means
701 * ->exec_op() must be re-entrant.
702 *
703 * Return 0 if the NAND chip is ready, a negative error otherwise.
704 */
705int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
706{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200707 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100708 u8 status = 0;
709 int ret;
710
711 if (!chip->exec_op)
712 return -ENOTSUPP;
713
Boris Brezillon3057fce2018-05-04 21:24:31 +0200714 /* Wait tWB before polling the STATUS reg. */
715 timings = nand_get_sdr_timings(&chip->data_interface);
716 ndelay(PSEC_TO_NSEC(timings->tWB_max));
717
Miquel Raynal8878b122017-11-09 14:16:45 +0100718 ret = nand_status_op(chip, NULL);
719 if (ret)
720 return ret;
721
722 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
723 do {
724 ret = nand_read_data_op(chip, &status, sizeof(status), true);
725 if (ret)
726 break;
727
728 if (status & NAND_STATUS_READY)
729 break;
730
731 /*
732 * Typical lowest execution time for a tR on most NANDs is 10us,
733 * use this as polling delay before doing something smarter (ie.
734 * deriving a delay from the timeout value, timeout_ms/ratio).
735 */
736 udelay(10);
737 } while (time_before(jiffies, timeout_ms));
738
739 /*
740 * We have to exit READ_STATUS mode in order to read real data on the
741 * bus in case the WAITRDY instruction is preceding a DATA_IN
742 * instruction.
743 */
744 nand_exit_status_op(chip);
745
746 if (ret)
747 return ret;
748
749 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
750};
751EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
752
753/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * nand_command - [DEFAULT] Send command to NAND device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200755 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700756 * @command: the command to be sent
757 * @column: the column address for this command, -1 if none
758 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700760 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200761 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200763static void nand_command(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200764 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200766 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200767 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Brian Norris8b6e50c2011-05-25 14:59:01 -0700769 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (command == NAND_CMD_SEQIN) {
771 int readcmd;
772
Joern Engel28318772006-05-22 23:18:05 +0200773 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200775 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 readcmd = NAND_CMD_READOOB;
777 } else if (column < 256) {
778 /* First 256 bytes --> READ0 */
779 readcmd = NAND_CMD_READ0;
780 } else {
781 column -= 256;
782 readcmd = NAND_CMD_READ1;
783 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200784 chip->legacy.cmd_ctrl(chip, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200785 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100787 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200788 chip->legacy.cmd_ctrl(chip, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Brian Norris8b6e50c2011-05-25 14:59:01 -0700790 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200791 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
792 /* Serially input address */
793 if (column != -1) {
794 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800795 if (chip->options & NAND_BUSWIDTH_16 &&
796 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200797 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200798 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200802 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200803 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200804 chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900805 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200806 chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200807 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200808 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
809 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000810
811 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700812 * Program and erase have their own busy handlers status and sequential
813 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000816
Miquel Raynaldf467892017-11-08 17:00:27 +0100817 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case NAND_CMD_PAGEPROG:
819 case NAND_CMD_ERASE1:
820 case NAND_CMD_ERASE2:
821 case NAND_CMD_SEQIN:
822 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900823 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900824 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return;
826
827 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200828 if (chip->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200830 udelay(chip->legacy.chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200831 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
832 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
833 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
834 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200835 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
836 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
838
David Woodhousee0c7d762006-05-13 18:07:53 +0100839 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200840 case NAND_CMD_READ0:
841 /*
842 * READ0 is sometimes used to exit GET STATUS mode. When this
843 * is the case no address cycles are requested, and we can use
844 * this information to detect that we should not wait for the
845 * device to be ready.
846 */
847 if (column == -1 && page_addr == -1)
848 return;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * If we don't have access to the busy pin, we apply the given
853 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100854 */
Boris Brezillon8395b752018-09-07 00:38:37 +0200855 if (!chip->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200856 udelay(chip->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 /*
861 * Apply this short delay always to ensure that we do wait tWB in
862 * any case on any machine.
863 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100864 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000865
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200866 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200869static void nand_ccs_delay(struct nand_chip *chip)
870{
871 /*
872 * The controller already takes care of waiting for tCCS when the RNDIN
873 * or RNDOUT command is sent, return directly.
874 */
875 if (!(chip->options & NAND_WAIT_TCCS))
876 return;
877
878 /*
879 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
880 * (which should be safe for all NANDs).
881 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100882 if (chip->setup_data_interface)
883 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200884 else
885 ndelay(500);
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/**
889 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200890 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700891 * @command: the command to be sent
892 * @column: the column address for this command, -1 if none
893 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200895 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700896 * devices. We don't have the separate regions as we have in the small page
897 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200899static void nand_command_lp(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200900 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200902 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* Emulate NAND_CMD_READOOB */
905 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200906 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 command = NAND_CMD_READ0;
908 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000909
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200910 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100911 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200912 chip->legacy.cmd_ctrl(chip, command,
913 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200916 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* Serially input address */
919 if (column != -1) {
920 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800921 if (chip->options & NAND_BUSWIDTH_16 &&
922 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200924 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200925 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200926
Brian Norrisf5b88de2016-10-03 09:49:35 -0700927 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200928 if (!nand_opcode_8bits(command))
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200929 chip->legacy.cmd_ctrl(chip, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200932 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
933 chip->legacy.cmd_ctrl(chip, page_addr >> 8,
934 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900935 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200936 chip->legacy.cmd_ctrl(chip, page_addr >> 16,
937 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200940 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
941 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000942
943 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700944 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100945 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000948
Miquel Raynaldf467892017-11-08 17:00:27 +0100949 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case NAND_CMD_CACHEDPROG:
951 case NAND_CMD_PAGEPROG:
952 case NAND_CMD_ERASE1:
953 case NAND_CMD_ERASE2:
954 case NAND_CMD_SEQIN:
955 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900956 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900957 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200960 case NAND_CMD_RNDIN:
961 nand_ccs_delay(chip);
962 return;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case NAND_CMD_RESET:
Boris Brezillon8395b752018-09-07 00:38:37 +0200965 if (chip->legacy.dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 break;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200967 udelay(chip->legacy.chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200968 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
969 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
970 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
971 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200972 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
973 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
975
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200976 case NAND_CMD_RNDOUT:
977 /* No ready / busy check necessary */
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200978 chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
979 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
980 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
981 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200982
983 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200984 return;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200987 /*
988 * READ0 is sometimes used to exit GET STATUS mode. When this
989 * is the case no address cycles are requested, and we can use
990 * this information to detect that READSTART should not be
991 * issued.
992 */
993 if (column == -1 && page_addr == -1)
994 return;
995
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200996 chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART,
997 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
998 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
999 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001000
David Woodhousee0c7d762006-05-13 18:07:53 +01001001 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001003 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -07001005 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +01001006 */
Boris Brezillon8395b752018-09-07 00:38:37 +02001007 if (!chip->legacy.dev_ready) {
Boris Brezillon3cece3a2018-09-07 00:38:41 +02001008 udelay(chip->legacy.chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Thomas Gleixner3b887752005-02-22 21:56:49 +00001012
Brian Norris8b6e50c2011-05-25 14:59:01 -07001013 /*
1014 * Apply this short delay always to ensure that we do wait tWB in
1015 * any case on any machine.
1016 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001017 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +00001018
Boris Brezillon2b356ab2018-09-06 14:05:16 +02001019 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001023 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001024 * @chip: the nand chip descriptor
1025 * @mtd: MTD device structure
1026 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001027 *
1028 * Used when in panic, no locks are taken.
1029 */
1030static void panic_nand_get_device(struct nand_chip *chip,
1031 struct mtd_info *mtd, int new_state)
1032{
Brian Norris7854d3f2011-06-23 14:12:08 -07001033 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001034 chip->controller->active = chip;
1035 chip->state = new_state;
1036}
1037
1038/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001040 * @mtd: MTD device structure
1041 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *
1043 * Get the device and lock it for exclusive access
1044 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001045static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001046nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001048 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001049 spinlock_t *lock = &chip->controller->lock;
1050 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001051 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001052retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001053 spin_lock(lock);
1054
vimal singhb8b3ee92009-07-09 20:41:22 +05301055 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001056 if (!chip->controller->active)
1057 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001058
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001059 if (chip->controller->active == chip && chip->state == FL_READY) {
1060 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001061 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001062 return 0;
1063 }
1064 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001065 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1066 chip->state = FL_PM_SUSPENDED;
1067 spin_unlock(lock);
1068 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001069 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001070 }
1071 set_current_state(TASK_UNINTERRUPTIBLE);
1072 add_wait_queue(wq, &wait);
1073 spin_unlock(lock);
1074 schedule();
1075 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 goto retry;
1077}
1078
1079/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001080 * panic_nand_wait - [GENERIC] wait until the command is done
1081 * @mtd: MTD device structure
1082 * @chip: NAND chip structure
1083 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001084 *
1085 * Wait for command done. This is a helper function for nand_wait used when
1086 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001087 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001088 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001089static void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001090{
1091 int i;
1092 for (i = 0; i < timeo; i++) {
Boris Brezillon8395b752018-09-07 00:38:37 +02001093 if (chip->legacy.dev_ready) {
1094 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001095 break;
1096 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001097 int ret;
1098 u8 status;
1099
1100 ret = nand_read_data_op(chip, &status, sizeof(status),
1101 true);
1102 if (ret)
1103 return;
1104
1105 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001106 break;
1107 }
1108 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001109 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001110}
1111
1112/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001113 * nand_wait - [DEFAULT] wait until the command is done
1114 * @mtd: MTD device structure
1115 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001117 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001118 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001119static int nand_wait(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121
Alex Smithb70af9b2015-10-06 14:52:07 +01001122 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001123 u8 status;
1124 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Brian Norris8b6e50c2011-05-25 14:59:01 -07001126 /*
1127 * Apply this short delay always to ensure that we do wait tWB in any
1128 * case on any machine.
1129 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001130 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Boris Brezillon97d90da2017-11-30 18:01:29 +01001132 ret = nand_status_op(chip, NULL);
1133 if (ret)
1134 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001136 if (in_interrupt() || oops_in_progress)
Boris Brezillonf1d46942018-09-06 14:05:29 +02001137 panic_nand_wait(chip, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001138 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001139 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001140 do {
Boris Brezillon8395b752018-09-07 00:38:37 +02001141 if (chip->legacy.dev_ready) {
1142 if (chip->legacy.dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001143 break;
1144 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001145 ret = nand_read_data_op(chip, &status,
1146 sizeof(status), true);
1147 if (ret)
1148 return ret;
1149
1150 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001151 break;
1152 }
1153 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001154 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001156
Boris Brezillon97d90da2017-11-30 18:01:29 +01001157 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1158 if (ret)
1159 return ret;
1160
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001161 /* This can happen if in case of timeout or buggy dev_ready */
1162 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return status;
1164}
1165
Miquel Raynal789157e2018-03-19 14:47:28 +01001166static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001167{
Miquel Raynal789157e2018-03-19 14:47:28 +01001168 return (chip->parameters.supports_set_get_features &&
1169 test_bit(addr, chip->parameters.get_feature_list));
1170}
1171
1172static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1173{
1174 return (chip->parameters.supports_set_get_features &&
1175 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001176}
1177
1178/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001179 * nand_reset_data_interface - Reset data interface and timings
1180 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001181 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001182 *
1183 * Reset the Data interface and timings to ONFI mode 0.
1184 *
1185 * Returns 0 for success or negative error code otherwise.
1186 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001187static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001188{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001189 int ret;
1190
1191 if (!chip->setup_data_interface)
1192 return 0;
1193
1194 /*
1195 * The ONFI specification says:
1196 * "
1197 * To transition from NV-DDR or NV-DDR2 to the SDR data
1198 * interface, the host shall use the Reset (FFh) command
1199 * using SDR timing mode 0. A device in any timing mode is
1200 * required to recognize Reset (FFh) command issued in SDR
1201 * timing mode 0.
1202 * "
1203 *
1204 * Configure the data interface in SDR mode and set the
1205 * timings to timing mode 0.
1206 */
1207
Miquel Raynal17fa8042017-11-30 18:01:31 +01001208 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +02001209 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001210 if (ret)
1211 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1212
1213 return ret;
1214}
1215
1216/**
1217 * nand_setup_data_interface - Setup the best data interface and timings
1218 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001219 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001220 *
1221 * Find and configure the best data interface and NAND timings supported by
1222 * the chip and the driver.
1223 * First tries to retrieve supported timing modes from ONFI information,
1224 * and if the NAND chip does not support ONFI, relies on the
1225 * ->onfi_timing_mode_default specified in the nand_ids table.
1226 *
1227 * Returns 0 for success or negative error code otherwise.
1228 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001229static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001230{
Miquel Raynal97baea12018-03-19 14:47:20 +01001231 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1232 chip->onfi_timing_mode_default,
1233 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001234 int ret;
1235
Miquel Raynal17fa8042017-11-30 18:01:31 +01001236 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001237 return 0;
1238
Miquel Raynal993447b2018-03-19 14:47:21 +01001239 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001240 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001241 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001242 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1243 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001244 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001245 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001246 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001247 }
1248
Miquel Raynal97baea12018-03-19 14:47:20 +01001249 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +02001250 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001251 if (ret)
1252 return ret;
1253
1254 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001255 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001256 return 0;
1257
1258 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001259 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001260 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1261 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001262 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001263 if (ret)
1264 goto err_reset_chip;
1265
1266 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1267 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1268 chip->onfi_timing_mode_default);
1269 goto err_reset_chip;
1270 }
1271
1272 return 0;
1273
1274err_reset_chip:
1275 /*
1276 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1277 * timing mode.
1278 */
1279 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001280 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001281 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001282 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001283
Boris Brezillond8e725d2016-09-15 10:32:50 +02001284 return ret;
1285}
1286
1287/**
1288 * nand_init_data_interface - find the best data interface and timings
1289 * @chip: The NAND chip
1290 *
1291 * Find the best data interface and NAND timings supported by the chip
1292 * and the driver.
1293 * First tries to retrieve supported timing modes from ONFI information,
1294 * and if the NAND chip does not support ONFI, relies on the
1295 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1296 * function nand_chip->data_interface is initialized with the best timing mode
1297 * available.
1298 *
1299 * Returns 0 for success or negative error code otherwise.
1300 */
1301static int nand_init_data_interface(struct nand_chip *chip)
1302{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001303 int modes, mode, ret;
1304
1305 if (!chip->setup_data_interface)
1306 return 0;
1307
1308 /*
1309 * First try to identify the best timings from ONFI parameters and
1310 * if the NAND does not support ONFI, fallback to the default ONFI
1311 * timing mode.
1312 */
1313 modes = onfi_get_async_timing_mode(chip);
1314 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1315 if (!chip->onfi_timing_mode_default)
1316 return 0;
1317
1318 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1319 }
1320
Boris Brezillond8e725d2016-09-15 10:32:50 +02001321
1322 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001323 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001324 if (ret)
1325 continue;
1326
Miquel Raynald787b8b2017-12-22 18:12:41 +01001327 /*
1328 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1329 * controller supports the requested timings.
1330 */
Boris Brezillon858838b2018-09-06 14:05:33 +02001331 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +01001332 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001333 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001334 if (!ret) {
1335 chip->onfi_timing_mode_default = mode;
1336 break;
1337 }
1338 }
1339
1340 return 0;
1341}
1342
Boris Brezillond8e725d2016-09-15 10:32:50 +02001343/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001344 * nand_fill_column_cycles - fill the column cycles of an address
1345 * @chip: The NAND chip
1346 * @addrs: Array of address cycles to fill
1347 * @offset_in_page: The offset in the page
1348 *
1349 * Fills the first or the first two bytes of the @addrs field depending
1350 * on the NAND bus width and the page size.
1351 *
1352 * Returns the number of cycles needed to encode the column, or a negative
1353 * error code in case one of the arguments is invalid.
1354 */
1355static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1356 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Miquel Raynal8878b122017-11-09 14:16:45 +01001358 struct mtd_info *mtd = nand_to_mtd(chip);
1359
1360 /* Make sure the offset is less than the actual page size. */
1361 if (offset_in_page > mtd->writesize + mtd->oobsize)
1362 return -EINVAL;
1363
1364 /*
1365 * On small page NANDs, there's a dedicated command to access the OOB
1366 * area, and the column address is relative to the start of the OOB
1367 * area, not the start of the page. Asjust the address accordingly.
1368 */
1369 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1370 offset_in_page -= mtd->writesize;
1371
1372 /*
1373 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1374 * wide, then it must be divided by 2.
1375 */
1376 if (chip->options & NAND_BUSWIDTH_16) {
1377 if (WARN_ON(offset_in_page % 2))
1378 return -EINVAL;
1379
1380 offset_in_page /= 2;
1381 }
1382
1383 addrs[0] = offset_in_page;
1384
1385 /*
1386 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1387 * need 2
1388 */
1389 if (mtd->writesize <= 512)
1390 return 1;
1391
1392 addrs[1] = offset_in_page >> 8;
1393
1394 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Miquel Raynal8878b122017-11-09 14:16:45 +01001397static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1398 unsigned int offset_in_page, void *buf,
1399 unsigned int len)
1400{
1401 struct mtd_info *mtd = nand_to_mtd(chip);
1402 const struct nand_sdr_timings *sdr =
1403 nand_get_sdr_timings(&chip->data_interface);
1404 u8 addrs[4];
1405 struct nand_op_instr instrs[] = {
1406 NAND_OP_CMD(NAND_CMD_READ0, 0),
1407 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1408 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1409 PSEC_TO_NSEC(sdr->tRR_min)),
1410 NAND_OP_DATA_IN(len, buf, 0),
1411 };
1412 struct nand_operation op = NAND_OPERATION(instrs);
1413 int ret;
1414
1415 /* Drop the DATA_IN instruction if len is set to 0. */
1416 if (!len)
1417 op.ninstrs--;
1418
1419 if (offset_in_page >= mtd->writesize)
1420 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1421 else if (offset_in_page >= 256 &&
1422 !(chip->options & NAND_BUSWIDTH_16))
1423 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1424
1425 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1426 if (ret < 0)
1427 return ret;
1428
1429 addrs[1] = page;
1430 addrs[2] = page >> 8;
1431
1432 if (chip->options & NAND_ROW_ADDR_3) {
1433 addrs[3] = page >> 16;
1434 instrs[1].ctx.addr.naddrs++;
1435 }
1436
1437 return nand_exec_op(chip, &op);
1438}
1439
1440static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1441 unsigned int offset_in_page, void *buf,
1442 unsigned int len)
1443{
1444 const struct nand_sdr_timings *sdr =
1445 nand_get_sdr_timings(&chip->data_interface);
1446 u8 addrs[5];
1447 struct nand_op_instr instrs[] = {
1448 NAND_OP_CMD(NAND_CMD_READ0, 0),
1449 NAND_OP_ADDR(4, addrs, 0),
1450 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1451 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1452 PSEC_TO_NSEC(sdr->tRR_min)),
1453 NAND_OP_DATA_IN(len, buf, 0),
1454 };
1455 struct nand_operation op = NAND_OPERATION(instrs);
1456 int ret;
1457
1458 /* Drop the DATA_IN instruction if len is set to 0. */
1459 if (!len)
1460 op.ninstrs--;
1461
1462 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1463 if (ret < 0)
1464 return ret;
1465
1466 addrs[2] = page;
1467 addrs[3] = page >> 8;
1468
1469 if (chip->options & NAND_ROW_ADDR_3) {
1470 addrs[4] = page >> 16;
1471 instrs[1].ctx.addr.naddrs++;
1472 }
1473
1474 return nand_exec_op(chip, &op);
1475}
1476
1477/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001478 * nand_read_page_op - Do a READ PAGE operation
1479 * @chip: The NAND chip
1480 * @page: page to read
1481 * @offset_in_page: offset within the page
1482 * @buf: buffer used to store the data
1483 * @len: length of the buffer
1484 *
1485 * This function issues a READ PAGE operation.
1486 * This function does not select/unselect the CS line.
1487 *
1488 * Returns 0 on success, a negative error code otherwise.
1489 */
1490int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1491 unsigned int offset_in_page, void *buf, unsigned int len)
1492{
1493 struct mtd_info *mtd = nand_to_mtd(chip);
1494
1495 if (len && !buf)
1496 return -EINVAL;
1497
1498 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1499 return -EINVAL;
1500
Miquel Raynal8878b122017-11-09 14:16:45 +01001501 if (chip->exec_op) {
1502 if (mtd->writesize > 512)
1503 return nand_lp_exec_read_page_op(chip, page,
1504 offset_in_page, buf,
1505 len);
1506
1507 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1508 buf, len);
1509 }
1510
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001511 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001512 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001513 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001514
1515 return 0;
1516}
1517EXPORT_SYMBOL_GPL(nand_read_page_op);
1518
1519/**
1520 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1521 * @chip: The NAND chip
1522 * @page: parameter page to read
1523 * @buf: buffer used to store the data
1524 * @len: length of the buffer
1525 *
1526 * This function issues a READ PARAMETER PAGE operation.
1527 * This function does not select/unselect the CS line.
1528 *
1529 * Returns 0 on success, a negative error code otherwise.
1530 */
1531static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1532 unsigned int len)
1533{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001534 unsigned int i;
1535 u8 *p = buf;
1536
1537 if (len && !buf)
1538 return -EINVAL;
1539
Miquel Raynal8878b122017-11-09 14:16:45 +01001540 if (chip->exec_op) {
1541 const struct nand_sdr_timings *sdr =
1542 nand_get_sdr_timings(&chip->data_interface);
1543 struct nand_op_instr instrs[] = {
1544 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1545 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1546 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1547 PSEC_TO_NSEC(sdr->tRR_min)),
1548 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1549 };
1550 struct nand_operation op = NAND_OPERATION(instrs);
1551
1552 /* Drop the DATA_IN instruction if len is set to 0. */
1553 if (!len)
1554 op.ninstrs--;
1555
1556 return nand_exec_op(chip, &op);
1557 }
1558
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001559 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001560 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001561 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001562
1563 return 0;
1564}
1565
1566/**
1567 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1568 * @chip: The NAND chip
1569 * @offset_in_page: offset within the page
1570 * @buf: buffer used to store the data
1571 * @len: length of the buffer
1572 * @force_8bit: force 8-bit bus access
1573 *
1574 * This function issues a CHANGE READ COLUMN operation.
1575 * This function does not select/unselect the CS line.
1576 *
1577 * Returns 0 on success, a negative error code otherwise.
1578 */
1579int nand_change_read_column_op(struct nand_chip *chip,
1580 unsigned int offset_in_page, void *buf,
1581 unsigned int len, bool force_8bit)
1582{
1583 struct mtd_info *mtd = nand_to_mtd(chip);
1584
1585 if (len && !buf)
1586 return -EINVAL;
1587
1588 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1589 return -EINVAL;
1590
Miquel Raynal8878b122017-11-09 14:16:45 +01001591 /* Small page NANDs do not support column change. */
1592 if (mtd->writesize <= 512)
1593 return -ENOTSUPP;
1594
1595 if (chip->exec_op) {
1596 const struct nand_sdr_timings *sdr =
1597 nand_get_sdr_timings(&chip->data_interface);
1598 u8 addrs[2] = {};
1599 struct nand_op_instr instrs[] = {
1600 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1601 NAND_OP_ADDR(2, addrs, 0),
1602 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1603 PSEC_TO_NSEC(sdr->tCCS_min)),
1604 NAND_OP_DATA_IN(len, buf, 0),
1605 };
1606 struct nand_operation op = NAND_OPERATION(instrs);
1607 int ret;
1608
1609 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1610 if (ret < 0)
1611 return ret;
1612
1613 /* Drop the DATA_IN instruction if len is set to 0. */
1614 if (!len)
1615 op.ninstrs--;
1616
1617 instrs[3].ctx.data.force_8bit = force_8bit;
1618
1619 return nand_exec_op(chip, &op);
1620 }
1621
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001622 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001623 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001624 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001625
1626 return 0;
1627}
1628EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1629
1630/**
1631 * nand_read_oob_op - Do a READ OOB operation
1632 * @chip: The NAND chip
1633 * @page: page to read
1634 * @offset_in_oob: offset within the OOB area
1635 * @buf: buffer used to store the data
1636 * @len: length of the buffer
1637 *
1638 * This function issues a READ OOB operation.
1639 * This function does not select/unselect the CS line.
1640 *
1641 * Returns 0 on success, a negative error code otherwise.
1642 */
1643int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1644 unsigned int offset_in_oob, void *buf, unsigned int len)
1645{
1646 struct mtd_info *mtd = nand_to_mtd(chip);
1647
1648 if (len && !buf)
1649 return -EINVAL;
1650
1651 if (offset_in_oob + len > mtd->oobsize)
1652 return -EINVAL;
1653
Miquel Raynal8878b122017-11-09 14:16:45 +01001654 if (chip->exec_op)
1655 return nand_read_page_op(chip, page,
1656 mtd->writesize + offset_in_oob,
1657 buf, len);
1658
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001659 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001660 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001661 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001662
1663 return 0;
1664}
1665EXPORT_SYMBOL_GPL(nand_read_oob_op);
1666
Miquel Raynal8878b122017-11-09 14:16:45 +01001667static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1668 unsigned int offset_in_page, const void *buf,
1669 unsigned int len, bool prog)
1670{
1671 struct mtd_info *mtd = nand_to_mtd(chip);
1672 const struct nand_sdr_timings *sdr =
1673 nand_get_sdr_timings(&chip->data_interface);
1674 u8 addrs[5] = {};
1675 struct nand_op_instr instrs[] = {
1676 /*
1677 * The first instruction will be dropped if we're dealing
1678 * with a large page NAND and adjusted if we're dealing
1679 * with a small page NAND and the page offset is > 255.
1680 */
1681 NAND_OP_CMD(NAND_CMD_READ0, 0),
1682 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1683 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1684 NAND_OP_DATA_OUT(len, buf, 0),
1685 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1686 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1687 };
1688 struct nand_operation op = NAND_OPERATION(instrs);
1689 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1690 int ret;
1691 u8 status;
1692
1693 if (naddrs < 0)
1694 return naddrs;
1695
1696 addrs[naddrs++] = page;
1697 addrs[naddrs++] = page >> 8;
1698 if (chip->options & NAND_ROW_ADDR_3)
1699 addrs[naddrs++] = page >> 16;
1700
1701 instrs[2].ctx.addr.naddrs = naddrs;
1702
1703 /* Drop the last two instructions if we're not programming the page. */
1704 if (!prog) {
1705 op.ninstrs -= 2;
1706 /* Also drop the DATA_OUT instruction if empty. */
1707 if (!len)
1708 op.ninstrs--;
1709 }
1710
1711 if (mtd->writesize <= 512) {
1712 /*
1713 * Small pages need some more tweaking: we have to adjust the
1714 * first instruction depending on the page offset we're trying
1715 * to access.
1716 */
1717 if (offset_in_page >= mtd->writesize)
1718 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1719 else if (offset_in_page >= 256 &&
1720 !(chip->options & NAND_BUSWIDTH_16))
1721 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1722 } else {
1723 /*
1724 * Drop the first command if we're dealing with a large page
1725 * NAND.
1726 */
1727 op.instrs++;
1728 op.ninstrs--;
1729 }
1730
1731 ret = nand_exec_op(chip, &op);
1732 if (!prog || ret)
1733 return ret;
1734
1735 ret = nand_status_op(chip, &status);
1736 if (ret)
1737 return ret;
1738
1739 return status;
1740}
1741
Boris Brezillon97d90da2017-11-30 18:01:29 +01001742/**
1743 * nand_prog_page_begin_op - starts a PROG PAGE operation
1744 * @chip: The NAND chip
1745 * @page: page to write
1746 * @offset_in_page: offset within the page
1747 * @buf: buffer containing the data to write to the page
1748 * @len: length of the buffer
1749 *
1750 * This function issues the first half of a PROG PAGE operation.
1751 * This function does not select/unselect the CS line.
1752 *
1753 * Returns 0 on success, a negative error code otherwise.
1754 */
1755int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1756 unsigned int offset_in_page, const void *buf,
1757 unsigned int len)
1758{
1759 struct mtd_info *mtd = nand_to_mtd(chip);
1760
1761 if (len && !buf)
1762 return -EINVAL;
1763
1764 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1765 return -EINVAL;
1766
Miquel Raynal8878b122017-11-09 14:16:45 +01001767 if (chip->exec_op)
1768 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1769 len, false);
1770
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001771 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001772
1773 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001774 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001775
1776 return 0;
1777}
1778EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1779
1780/**
1781 * nand_prog_page_end_op - ends a PROG PAGE operation
1782 * @chip: The NAND chip
1783 *
1784 * This function issues the second half of a PROG PAGE operation.
1785 * This function does not select/unselect the CS line.
1786 *
1787 * Returns 0 on success, a negative error code otherwise.
1788 */
1789int nand_prog_page_end_op(struct nand_chip *chip)
1790{
Miquel Raynal8878b122017-11-09 14:16:45 +01001791 int ret;
1792 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001793
Miquel Raynal8878b122017-11-09 14:16:45 +01001794 if (chip->exec_op) {
1795 const struct nand_sdr_timings *sdr =
1796 nand_get_sdr_timings(&chip->data_interface);
1797 struct nand_op_instr instrs[] = {
1798 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1799 PSEC_TO_NSEC(sdr->tWB_max)),
1800 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1801 };
1802 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001803
Miquel Raynal8878b122017-11-09 14:16:45 +01001804 ret = nand_exec_op(chip, &op);
1805 if (ret)
1806 return ret;
1807
1808 ret = nand_status_op(chip, &status);
1809 if (ret)
1810 return ret;
1811 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001812 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001813 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001814 if (ret < 0)
1815 return ret;
1816
1817 status = ret;
1818 }
1819
Boris Brezillon97d90da2017-11-30 18:01:29 +01001820 if (status & NAND_STATUS_FAIL)
1821 return -EIO;
1822
1823 return 0;
1824}
1825EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1826
1827/**
1828 * nand_prog_page_op - Do a full PROG PAGE operation
1829 * @chip: The NAND chip
1830 * @page: page to write
1831 * @offset_in_page: offset within the page
1832 * @buf: buffer containing the data to write to the page
1833 * @len: length of the buffer
1834 *
1835 * This function issues a full PROG PAGE operation.
1836 * This function does not select/unselect the CS line.
1837 *
1838 * Returns 0 on success, a negative error code otherwise.
1839 */
1840int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1841 unsigned int offset_in_page, const void *buf,
1842 unsigned int len)
1843{
1844 struct mtd_info *mtd = nand_to_mtd(chip);
1845 int status;
1846
1847 if (!len || !buf)
1848 return -EINVAL;
1849
1850 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1851 return -EINVAL;
1852
Miquel Raynal8878b122017-11-09 14:16:45 +01001853 if (chip->exec_op) {
1854 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1855 len, true);
1856 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001857 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1858 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001859 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001860 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon8395b752018-09-07 00:38:37 +02001861 status = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001862 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001863
Boris Brezillon97d90da2017-11-30 18:01:29 +01001864 if (status & NAND_STATUS_FAIL)
1865 return -EIO;
1866
1867 return 0;
1868}
1869EXPORT_SYMBOL_GPL(nand_prog_page_op);
1870
1871/**
1872 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1873 * @chip: The NAND chip
1874 * @offset_in_page: offset within the page
1875 * @buf: buffer containing the data to send to the NAND
1876 * @len: length of the buffer
1877 * @force_8bit: force 8-bit bus access
1878 *
1879 * This function issues a CHANGE WRITE COLUMN operation.
1880 * This function does not select/unselect the CS line.
1881 *
1882 * Returns 0 on success, a negative error code otherwise.
1883 */
1884int nand_change_write_column_op(struct nand_chip *chip,
1885 unsigned int offset_in_page,
1886 const void *buf, unsigned int len,
1887 bool force_8bit)
1888{
1889 struct mtd_info *mtd = nand_to_mtd(chip);
1890
1891 if (len && !buf)
1892 return -EINVAL;
1893
1894 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1895 return -EINVAL;
1896
Miquel Raynal8878b122017-11-09 14:16:45 +01001897 /* Small page NANDs do not support column change. */
1898 if (mtd->writesize <= 512)
1899 return -ENOTSUPP;
1900
1901 if (chip->exec_op) {
1902 const struct nand_sdr_timings *sdr =
1903 nand_get_sdr_timings(&chip->data_interface);
1904 u8 addrs[2];
1905 struct nand_op_instr instrs[] = {
1906 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1907 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1908 NAND_OP_DATA_OUT(len, buf, 0),
1909 };
1910 struct nand_operation op = NAND_OPERATION(instrs);
1911 int ret;
1912
1913 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1914 if (ret < 0)
1915 return ret;
1916
1917 instrs[2].ctx.data.force_8bit = force_8bit;
1918
1919 /* Drop the DATA_OUT instruction if len is set to 0. */
1920 if (!len)
1921 op.ninstrs--;
1922
1923 return nand_exec_op(chip, &op);
1924 }
1925
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001926 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001927 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001928 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001929
1930 return 0;
1931}
1932EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1933
1934/**
1935 * nand_readid_op - Do a READID operation
1936 * @chip: The NAND chip
1937 * @addr: address cycle to pass after the READID command
1938 * @buf: buffer used to store the ID
1939 * @len: length of the buffer
1940 *
1941 * This function sends a READID command and reads back the ID returned by the
1942 * NAND.
1943 * This function does not select/unselect the CS line.
1944 *
1945 * Returns 0 on success, a negative error code otherwise.
1946 */
1947int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1948 unsigned int len)
1949{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001950 unsigned int i;
1951 u8 *id = buf;
1952
1953 if (len && !buf)
1954 return -EINVAL;
1955
Miquel Raynal8878b122017-11-09 14:16:45 +01001956 if (chip->exec_op) {
1957 const struct nand_sdr_timings *sdr =
1958 nand_get_sdr_timings(&chip->data_interface);
1959 struct nand_op_instr instrs[] = {
1960 NAND_OP_CMD(NAND_CMD_READID, 0),
1961 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1962 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1963 };
1964 struct nand_operation op = NAND_OPERATION(instrs);
1965
1966 /* Drop the DATA_IN instruction if len is set to 0. */
1967 if (!len)
1968 op.ninstrs--;
1969
1970 return nand_exec_op(chip, &op);
1971 }
1972
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001973 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001974
1975 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001976 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001977
1978 return 0;
1979}
1980EXPORT_SYMBOL_GPL(nand_readid_op);
1981
1982/**
1983 * nand_status_op - Do a STATUS operation
1984 * @chip: The NAND chip
1985 * @status: out variable to store the NAND status
1986 *
1987 * This function sends a STATUS command and reads back the status returned by
1988 * the NAND.
1989 * This function does not select/unselect the CS line.
1990 *
1991 * Returns 0 on success, a negative error code otherwise.
1992 */
1993int nand_status_op(struct nand_chip *chip, u8 *status)
1994{
Miquel Raynal8878b122017-11-09 14:16:45 +01001995 if (chip->exec_op) {
1996 const struct nand_sdr_timings *sdr =
1997 nand_get_sdr_timings(&chip->data_interface);
1998 struct nand_op_instr instrs[] = {
1999 NAND_OP_CMD(NAND_CMD_STATUS,
2000 PSEC_TO_NSEC(sdr->tADL_min)),
2001 NAND_OP_8BIT_DATA_IN(1, status, 0),
2002 };
2003 struct nand_operation op = NAND_OPERATION(instrs);
2004
2005 if (!status)
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_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002012 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002013 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002014
2015 return 0;
2016}
2017EXPORT_SYMBOL_GPL(nand_status_op);
2018
2019/**
2020 * nand_exit_status_op - Exit a STATUS operation
2021 * @chip: The NAND chip
2022 *
2023 * This function sends a READ0 command to cancel the effect of the STATUS
2024 * command to avoid reading only the status until a new read command is sent.
2025 *
2026 * This function does not select/unselect the CS line.
2027 *
2028 * Returns 0 on success, a negative error code otherwise.
2029 */
2030int nand_exit_status_op(struct nand_chip *chip)
2031{
Miquel Raynal8878b122017-11-09 14:16:45 +01002032 if (chip->exec_op) {
2033 struct nand_op_instr instrs[] = {
2034 NAND_OP_CMD(NAND_CMD_READ0, 0),
2035 };
2036 struct nand_operation op = NAND_OPERATION(instrs);
2037
2038 return nand_exec_op(chip, &op);
2039 }
2040
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002041 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002042
2043 return 0;
2044}
2045EXPORT_SYMBOL_GPL(nand_exit_status_op);
2046
2047/**
2048 * nand_erase_op - Do an erase operation
2049 * @chip: The NAND chip
2050 * @eraseblock: block to erase
2051 *
2052 * This function sends an ERASE command and waits for the NAND to be ready
2053 * before returning.
2054 * This function does not select/unselect the CS line.
2055 *
2056 * Returns 0 on success, a negative error code otherwise.
2057 */
2058int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2059{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002060 unsigned int page = eraseblock <<
2061 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002062 int ret;
2063 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002064
Miquel Raynal8878b122017-11-09 14:16:45 +01002065 if (chip->exec_op) {
2066 const struct nand_sdr_timings *sdr =
2067 nand_get_sdr_timings(&chip->data_interface);
2068 u8 addrs[3] = { page, page >> 8, page >> 16 };
2069 struct nand_op_instr instrs[] = {
2070 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2071 NAND_OP_ADDR(2, addrs, 0),
2072 NAND_OP_CMD(NAND_CMD_ERASE2,
2073 PSEC_TO_MSEC(sdr->tWB_max)),
2074 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2075 };
2076 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002077
Miquel Raynal8878b122017-11-09 14:16:45 +01002078 if (chip->options & NAND_ROW_ADDR_3)
2079 instrs[1].ctx.addr.naddrs++;
2080
2081 ret = nand_exec_op(chip, &op);
2082 if (ret)
2083 return ret;
2084
2085 ret = nand_status_op(chip, &status);
2086 if (ret)
2087 return ret;
2088 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002089 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2090 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002091
Boris Brezillon8395b752018-09-07 00:38:37 +02002092 ret = chip->legacy.waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002093 if (ret < 0)
2094 return ret;
2095
2096 status = ret;
2097 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002098
2099 if (status & NAND_STATUS_FAIL)
2100 return -EIO;
2101
2102 return 0;
2103}
2104EXPORT_SYMBOL_GPL(nand_erase_op);
2105
2106/**
2107 * nand_set_features_op - Do a SET FEATURES operation
2108 * @chip: The NAND chip
2109 * @feature: feature id
2110 * @data: 4 bytes of data
2111 *
2112 * This function sends a SET FEATURES command and waits for the NAND to be
2113 * ready before returning.
2114 * This function does not select/unselect the CS line.
2115 *
2116 * Returns 0 on success, a negative error code otherwise.
2117 */
2118static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2119 const void *data)
2120{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002121 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002122 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002123
Miquel Raynal8878b122017-11-09 14:16:45 +01002124 if (chip->exec_op) {
2125 const struct nand_sdr_timings *sdr =
2126 nand_get_sdr_timings(&chip->data_interface);
2127 struct nand_op_instr instrs[] = {
2128 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2129 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2130 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2131 PSEC_TO_NSEC(sdr->tWB_max)),
2132 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2133 };
2134 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002135
Boris Brezillon782d1962018-05-11 14:44:07 +02002136 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002137 }
2138
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002139 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002140 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002141 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002142
Boris Brezillon8395b752018-09-07 00:38:37 +02002143 ret = chip->legacy.waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002144 if (ret < 0)
2145 return ret;
2146
2147 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002148 return -EIO;
2149
2150 return 0;
2151}
2152
2153/**
2154 * nand_get_features_op - Do a GET FEATURES operation
2155 * @chip: The NAND chip
2156 * @feature: feature id
2157 * @data: 4 bytes of data
2158 *
2159 * This function sends a GET FEATURES command and waits for the NAND to be
2160 * ready before returning.
2161 * This function does not select/unselect the CS line.
2162 *
2163 * Returns 0 on success, a negative error code otherwise.
2164 */
2165static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2166 void *data)
2167{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002168 u8 *params = data;
2169 int i;
2170
Miquel Raynal8878b122017-11-09 14:16:45 +01002171 if (chip->exec_op) {
2172 const struct nand_sdr_timings *sdr =
2173 nand_get_sdr_timings(&chip->data_interface);
2174 struct nand_op_instr instrs[] = {
2175 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2176 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2177 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2178 PSEC_TO_NSEC(sdr->tRR_min)),
2179 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2180 data, 0),
2181 };
2182 struct nand_operation op = NAND_OPERATION(instrs);
2183
2184 return nand_exec_op(chip, &op);
2185 }
2186
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002187 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002188 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002189 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002190
2191 return 0;
2192}
2193
Boris Brezillon52f05b62018-07-27 09:44:18 +02002194static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2195 unsigned int delay_ns)
2196{
2197 if (chip->exec_op) {
2198 struct nand_op_instr instrs[] = {
2199 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2200 PSEC_TO_NSEC(delay_ns)),
2201 };
2202 struct nand_operation op = NAND_OPERATION(instrs);
2203
2204 return nand_exec_op(chip, &op);
2205 }
2206
2207 /* Apply delay or wait for ready/busy pin */
Boris Brezillon8395b752018-09-07 00:38:37 +02002208 if (!chip->legacy.dev_ready)
Boris Brezillon3cece3a2018-09-07 00:38:41 +02002209 udelay(chip->legacy.chip_delay);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002210 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002211 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002212
2213 return 0;
2214}
2215
Boris Brezillon97d90da2017-11-30 18:01:29 +01002216/**
2217 * nand_reset_op - Do a reset operation
2218 * @chip: The NAND chip
2219 *
2220 * This function sends a RESET command and waits for the NAND to be ready
2221 * before returning.
2222 * This function does not select/unselect the CS line.
2223 *
2224 * Returns 0 on success, a negative error code otherwise.
2225 */
2226int nand_reset_op(struct nand_chip *chip)
2227{
Miquel Raynal8878b122017-11-09 14:16:45 +01002228 if (chip->exec_op) {
2229 const struct nand_sdr_timings *sdr =
2230 nand_get_sdr_timings(&chip->data_interface);
2231 struct nand_op_instr instrs[] = {
2232 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2233 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2234 };
2235 struct nand_operation op = NAND_OPERATION(instrs);
2236
2237 return nand_exec_op(chip, &op);
2238 }
2239
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002240 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002241
2242 return 0;
2243}
2244EXPORT_SYMBOL_GPL(nand_reset_op);
2245
2246/**
2247 * nand_read_data_op - Read data from the NAND
2248 * @chip: The NAND chip
2249 * @buf: buffer used to store the data
2250 * @len: length of the buffer
2251 * @force_8bit: force 8-bit bus access
2252 *
2253 * This function does a raw data read on the bus. Usually used after launching
2254 * another NAND operation like nand_read_page_op().
2255 * This function does not select/unselect the CS line.
2256 *
2257 * Returns 0 on success, a negative error code otherwise.
2258 */
2259int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2260 bool force_8bit)
2261{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002262 if (!len || !buf)
2263 return -EINVAL;
2264
Miquel Raynal8878b122017-11-09 14:16:45 +01002265 if (chip->exec_op) {
2266 struct nand_op_instr instrs[] = {
2267 NAND_OP_DATA_IN(len, buf, 0),
2268 };
2269 struct nand_operation op = NAND_OPERATION(instrs);
2270
2271 instrs[0].ctx.data.force_8bit = force_8bit;
2272
2273 return nand_exec_op(chip, &op);
2274 }
2275
Boris Brezillon97d90da2017-11-30 18:01:29 +01002276 if (force_8bit) {
2277 u8 *p = buf;
2278 unsigned int i;
2279
2280 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002281 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002282 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002283 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002284 }
2285
2286 return 0;
2287}
2288EXPORT_SYMBOL_GPL(nand_read_data_op);
2289
2290/**
2291 * nand_write_data_op - Write data from the NAND
2292 * @chip: The NAND chip
2293 * @buf: buffer containing the data to send on the bus
2294 * @len: length of the buffer
2295 * @force_8bit: force 8-bit bus access
2296 *
2297 * This function does a raw data write on the bus. Usually used after launching
2298 * another NAND operation like nand_write_page_begin_op().
2299 * This function does not select/unselect the CS line.
2300 *
2301 * Returns 0 on success, a negative error code otherwise.
2302 */
2303int nand_write_data_op(struct nand_chip *chip, const void *buf,
2304 unsigned int len, bool force_8bit)
2305{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002306 if (!len || !buf)
2307 return -EINVAL;
2308
Miquel Raynal8878b122017-11-09 14:16:45 +01002309 if (chip->exec_op) {
2310 struct nand_op_instr instrs[] = {
2311 NAND_OP_DATA_OUT(len, buf, 0),
2312 };
2313 struct nand_operation op = NAND_OPERATION(instrs);
2314
2315 instrs[0].ctx.data.force_8bit = force_8bit;
2316
2317 return nand_exec_op(chip, &op);
2318 }
2319
Boris Brezillon97d90da2017-11-30 18:01:29 +01002320 if (force_8bit) {
2321 const u8 *p = buf;
2322 unsigned int i;
2323
2324 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002325 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002326 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002327 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002328 }
2329
2330 return 0;
2331}
2332EXPORT_SYMBOL_GPL(nand_write_data_op);
2333
2334/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002335 * struct nand_op_parser_ctx - Context used by the parser
2336 * @instrs: array of all the instructions that must be addressed
2337 * @ninstrs: length of the @instrs array
2338 * @subop: Sub-operation to be passed to the NAND controller
2339 *
2340 * This structure is used by the core to split NAND operations into
2341 * sub-operations that can be handled by the NAND controller.
2342 */
2343struct nand_op_parser_ctx {
2344 const struct nand_op_instr *instrs;
2345 unsigned int ninstrs;
2346 struct nand_subop subop;
2347};
2348
2349/**
2350 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2351 * @pat: the parser pattern element that matches @instr
2352 * @instr: pointer to the instruction to check
2353 * @start_offset: this is an in/out parameter. If @instr has already been
2354 * split, then @start_offset is the offset from which to start
2355 * (either an address cycle or an offset in the data buffer).
2356 * Conversely, if the function returns true (ie. instr must be
2357 * split), this parameter is updated to point to the first
2358 * data/address cycle that has not been taken care of.
2359 *
2360 * Some NAND controllers are limited and cannot send X address cycles with a
2361 * unique operation, or cannot read/write more than Y bytes at the same time.
2362 * In this case, split the instruction that does not fit in a single
2363 * controller-operation into two or more chunks.
2364 *
2365 * Returns true if the instruction must be split, false otherwise.
2366 * The @start_offset parameter is also updated to the offset at which the next
2367 * bundle of instruction must start (if an address or a data instruction).
2368 */
2369static bool
2370nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2371 const struct nand_op_instr *instr,
2372 unsigned int *start_offset)
2373{
2374 switch (pat->type) {
2375 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002376 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002377 break;
2378
2379 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002380 pat->ctx.addr.maxcycles) {
2381 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002382 return true;
2383 }
2384 break;
2385
2386 case NAND_OP_DATA_IN_INSTR:
2387 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002388 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002389 break;
2390
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002391 if (instr->ctx.data.len - *start_offset >
2392 pat->ctx.data.maxlen) {
2393 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002394 return true;
2395 }
2396 break;
2397
2398 default:
2399 break;
2400 }
2401
2402 return false;
2403}
2404
2405/**
2406 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2407 * remaining in the parser context
2408 * @pat: the pattern to test
2409 * @ctx: the parser context structure to match with the pattern @pat
2410 *
2411 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2412 * Returns true if this is the case, false ortherwise. When true is returned,
2413 * @ctx->subop is updated with the set of instructions to be passed to the
2414 * controller driver.
2415 */
2416static bool
2417nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2418 struct nand_op_parser_ctx *ctx)
2419{
2420 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2421 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2422 const struct nand_op_instr *instr = ctx->subop.instrs;
2423 unsigned int i, ninstrs;
2424
2425 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2426 /*
2427 * The pattern instruction does not match the operation
2428 * instruction. If the instruction is marked optional in the
2429 * pattern definition, we skip the pattern element and continue
2430 * to the next one. If the element is mandatory, there's no
2431 * match and we can return false directly.
2432 */
2433 if (instr->type != pat->elems[i].type) {
2434 if (!pat->elems[i].optional)
2435 return false;
2436
2437 continue;
2438 }
2439
2440 /*
2441 * Now check the pattern element constraints. If the pattern is
2442 * not able to handle the whole instruction in a single step,
2443 * we have to split it.
2444 * The last_instr_end_off value comes back updated to point to
2445 * the position where we have to split the instruction (the
2446 * start of the next subop chunk).
2447 */
2448 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2449 &instr_offset)) {
2450 ninstrs++;
2451 i++;
2452 break;
2453 }
2454
2455 instr++;
2456 ninstrs++;
2457 instr_offset = 0;
2458 }
2459
2460 /*
2461 * This can happen if all instructions of a pattern are optional.
2462 * Still, if there's not at least one instruction handled by this
2463 * pattern, this is not a match, and we should try the next one (if
2464 * any).
2465 */
2466 if (!ninstrs)
2467 return false;
2468
2469 /*
2470 * We had a match on the pattern head, but the pattern may be longer
2471 * than the instructions we're asked to execute. We need to make sure
2472 * there's no mandatory elements in the pattern tail.
2473 */
2474 for (; i < pat->nelems; i++) {
2475 if (!pat->elems[i].optional)
2476 return false;
2477 }
2478
2479 /*
2480 * We have a match: update the subop structure accordingly and return
2481 * true.
2482 */
2483 ctx->subop.ninstrs = ninstrs;
2484 ctx->subop.last_instr_end_off = instr_offset;
2485
2486 return true;
2487}
2488
2489#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2490static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2491{
2492 const struct nand_op_instr *instr;
2493 char *prefix = " ";
2494 unsigned int i;
2495
2496 pr_debug("executing subop:\n");
2497
2498 for (i = 0; i < ctx->ninstrs; i++) {
2499 instr = &ctx->instrs[i];
2500
2501 if (instr == &ctx->subop.instrs[0])
2502 prefix = " ->";
2503
2504 switch (instr->type) {
2505 case NAND_OP_CMD_INSTR:
2506 pr_debug("%sCMD [0x%02x]\n", prefix,
2507 instr->ctx.cmd.opcode);
2508 break;
2509 case NAND_OP_ADDR_INSTR:
2510 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2511 instr->ctx.addr.naddrs,
2512 instr->ctx.addr.naddrs < 64 ?
2513 instr->ctx.addr.naddrs : 64,
2514 instr->ctx.addr.addrs);
2515 break;
2516 case NAND_OP_DATA_IN_INSTR:
2517 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2518 instr->ctx.data.len,
2519 instr->ctx.data.force_8bit ?
2520 ", force 8-bit" : "");
2521 break;
2522 case NAND_OP_DATA_OUT_INSTR:
2523 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2524 instr->ctx.data.len,
2525 instr->ctx.data.force_8bit ?
2526 ", force 8-bit" : "");
2527 break;
2528 case NAND_OP_WAITRDY_INSTR:
2529 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2530 instr->ctx.waitrdy.timeout_ms);
2531 break;
2532 }
2533
2534 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2535 prefix = " ";
2536 }
2537}
2538#else
2539static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2540{
2541 /* NOP */
2542}
2543#endif
2544
2545/**
2546 * nand_op_parser_exec_op - exec_op parser
2547 * @chip: the NAND chip
2548 * @parser: patterns description provided by the controller driver
2549 * @op: the NAND operation to address
2550 * @check_only: when true, the function only checks if @op can be handled but
2551 * does not execute the operation
2552 *
2553 * Helper function designed to ease integration of NAND controller drivers that
2554 * only support a limited set of instruction sequences. The supported sequences
2555 * are described in @parser, and the framework takes care of splitting @op into
2556 * multiple sub-operations (if required) and pass them back to the ->exec()
2557 * callback of the matching pattern if @check_only is set to false.
2558 *
2559 * NAND controller drivers should call this function from their own ->exec_op()
2560 * implementation.
2561 *
2562 * Returns 0 on success, a negative error code otherwise. A failure can be
2563 * caused by an unsupported operation (none of the supported patterns is able
2564 * to handle the requested operation), or an error returned by one of the
2565 * matching pattern->exec() hook.
2566 */
2567int nand_op_parser_exec_op(struct nand_chip *chip,
2568 const struct nand_op_parser *parser,
2569 const struct nand_operation *op, bool check_only)
2570{
2571 struct nand_op_parser_ctx ctx = {
2572 .subop.instrs = op->instrs,
2573 .instrs = op->instrs,
2574 .ninstrs = op->ninstrs,
2575 };
2576 unsigned int i;
2577
2578 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2579 int ret;
2580
2581 for (i = 0; i < parser->npatterns; i++) {
2582 const struct nand_op_parser_pattern *pattern;
2583
2584 pattern = &parser->patterns[i];
2585 if (!nand_op_parser_match_pat(pattern, &ctx))
2586 continue;
2587
2588 nand_op_parser_trace(&ctx);
2589
2590 if (check_only)
2591 break;
2592
2593 ret = pattern->exec(chip, &ctx.subop);
2594 if (ret)
2595 return ret;
2596
2597 break;
2598 }
2599
2600 if (i == parser->npatterns) {
2601 pr_debug("->exec_op() parser: pattern not found!\n");
2602 return -ENOTSUPP;
2603 }
2604
2605 /*
2606 * Update the context structure by pointing to the start of the
2607 * next subop.
2608 */
2609 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2610 if (ctx.subop.last_instr_end_off)
2611 ctx.subop.instrs -= 1;
2612
2613 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2614 }
2615
2616 return 0;
2617}
2618EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2619
2620static bool nand_instr_is_data(const struct nand_op_instr *instr)
2621{
2622 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2623 instr->type == NAND_OP_DATA_OUT_INSTR);
2624}
2625
2626static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2627 unsigned int instr_idx)
2628{
2629 return subop && instr_idx < subop->ninstrs;
2630}
2631
Miquel Raynal760c4352018-07-19 00:09:12 +02002632static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2633 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002634{
2635 if (instr_idx)
2636 return 0;
2637
2638 return subop->first_instr_start_off;
2639}
2640
2641/**
2642 * nand_subop_get_addr_start_off - Get the start offset in an address array
2643 * @subop: The entire sub-operation
2644 * @instr_idx: Index of the instruction inside the sub-operation
2645 *
2646 * During driver development, one could be tempted to directly use the
2647 * ->addr.addrs field of address instructions. This is wrong as address
2648 * instructions might be split.
2649 *
2650 * Given an address instruction, returns the offset of the first cycle to issue.
2651 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002652unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2653 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002654{
Miquel Raynal760c4352018-07-19 00:09:12 +02002655 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2656 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2657 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002658
2659 return nand_subop_get_start_off(subop, instr_idx);
2660}
2661EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2662
2663/**
2664 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2665 * @subop: The entire sub-operation
2666 * @instr_idx: Index of the instruction inside the sub-operation
2667 *
2668 * During driver development, one could be tempted to directly use the
2669 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2670 * might be split.
2671 *
2672 * Given an address instruction, returns the number of address cycle to issue.
2673 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002674unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2675 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002676{
2677 int start_off, end_off;
2678
Miquel Raynal760c4352018-07-19 00:09:12 +02002679 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2680 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2681 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002682
2683 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2684
2685 if (instr_idx == subop->ninstrs - 1 &&
2686 subop->last_instr_end_off)
2687 end_off = subop->last_instr_end_off;
2688 else
2689 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2690
2691 return end_off - start_off;
2692}
2693EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2694
2695/**
2696 * nand_subop_get_data_start_off - Get the start offset in a data array
2697 * @subop: The entire sub-operation
2698 * @instr_idx: Index of the instruction inside the sub-operation
2699 *
2700 * During driver development, one could be tempted to directly use the
2701 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2702 * instructions might be split.
2703 *
2704 * Given a data instruction, returns the offset to start from.
2705 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002706unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2707 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002708{
Miquel Raynal760c4352018-07-19 00:09:12 +02002709 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2710 !nand_instr_is_data(&subop->instrs[instr_idx])))
2711 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002712
2713 return nand_subop_get_start_off(subop, instr_idx);
2714}
2715EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2716
2717/**
2718 * nand_subop_get_data_len - Get the number of bytes to retrieve
2719 * @subop: The entire sub-operation
2720 * @instr_idx: Index of the instruction inside the sub-operation
2721 *
2722 * During driver development, one could be tempted to directly use the
2723 * ->data->len field of a data instruction. This is wrong as data instructions
2724 * might be split.
2725 *
2726 * Returns the length of the chunk of data to send/receive.
2727 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002728unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2729 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002730{
2731 int start_off = 0, end_off;
2732
Miquel Raynal760c4352018-07-19 00:09:12 +02002733 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2734 !nand_instr_is_data(&subop->instrs[instr_idx])))
2735 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002736
2737 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2738
2739 if (instr_idx == subop->ninstrs - 1 &&
2740 subop->last_instr_end_off)
2741 end_off = subop->last_instr_end_off;
2742 else
2743 end_off = subop->instrs[instr_idx].ctx.data.len;
2744
2745 return end_off - start_off;
2746}
2747EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002750 * nand_reset - Reset and initialize a NAND device
2751 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002752 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002753 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002754 * Save the timings data structure, then apply SDR timings mode 0 (see
2755 * nand_reset_data_interface for details), do the reset operation, and
2756 * apply back the previous timings.
2757 *
2758 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002759 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002760int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002761{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002762 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002763 int ret;
2764
Boris Brezillon104e4422017-03-16 09:35:58 +01002765 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002766 if (ret)
2767 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002768
Boris Brezillon73f907f2016-10-24 16:46:20 +02002769 /*
2770 * The CS line has to be released before we can apply the new NAND
2771 * interface settings, hence this weird ->select_chip() dance.
2772 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002773 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002774 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002775 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002776 if (ret)
2777 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002778
Miquel Raynal107b7d62018-03-19 14:47:25 +01002779 /*
2780 * A nand_reset_data_interface() put both the NAND chip and the NAND
2781 * controller in timings mode 0. If the default mode for this chip is
2782 * also 0, no need to proceed to the change again. Plus, at probe time,
2783 * nand_setup_data_interface() uses ->set/get_features() which would
2784 * fail anyway as the parameter page is not available yet.
2785 */
2786 if (!chip->onfi_timing_mode_default)
2787 return 0;
2788
Miquel Raynal17fa8042017-11-30 18:01:31 +01002789 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002790 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002791 if (ret)
2792 return ret;
2793
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002794 return 0;
2795}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002796EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002797
2798/**
Boris Brezillon45240362018-09-07 00:38:40 +02002799 * nand_get_features - wrapper to perform a GET_FEATURE
2800 * @chip: NAND chip info structure
2801 * @addr: feature address
2802 * @subfeature_param: the subfeature parameters, a four bytes array
2803 *
2804 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2805 * operation cannot be handled.
2806 */
2807int nand_get_features(struct nand_chip *chip, int addr,
2808 u8 *subfeature_param)
2809{
2810 if (!nand_supports_get_features(chip, addr))
2811 return -ENOTSUPP;
2812
2813 if (chip->legacy.get_features)
2814 return chip->legacy.get_features(chip, addr, subfeature_param);
2815
2816 return nand_get_features_op(chip, addr, subfeature_param);
2817}
2818EXPORT_SYMBOL_GPL(nand_get_features);
2819
2820/**
2821 * nand_set_features - wrapper to perform a SET_FEATURE
2822 * @chip: NAND chip info structure
2823 * @addr: feature address
2824 * @subfeature_param: the subfeature parameters, a four bytes array
2825 *
2826 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
2827 * operation cannot be handled.
2828 */
2829int nand_set_features(struct nand_chip *chip, int addr,
2830 u8 *subfeature_param)
2831{
2832 if (!nand_supports_set_features(chip, addr))
2833 return -ENOTSUPP;
2834
2835 if (chip->legacy.set_features)
2836 return chip->legacy.set_features(chip, addr, subfeature_param);
2837
2838 return nand_set_features_op(chip, addr, subfeature_param);
2839}
2840EXPORT_SYMBOL_GPL(nand_set_features);
2841
2842/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002843 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2844 * @buf: buffer to test
2845 * @len: buffer length
2846 * @bitflips_threshold: maximum number of bitflips
2847 *
2848 * Check if a buffer contains only 0xff, which means the underlying region
2849 * has been erased and is ready to be programmed.
2850 * The bitflips_threshold specify the maximum number of bitflips before
2851 * considering the region is not erased.
2852 * Note: The logic of this function has been extracted from the memweight
2853 * implementation, except that nand_check_erased_buf function exit before
2854 * testing the whole buffer if the number of bitflips exceed the
2855 * bitflips_threshold value.
2856 *
2857 * Returns a positive number of bitflips less than or equal to
2858 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2859 * threshold.
2860 */
2861static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2862{
2863 const unsigned char *bitmap = buf;
2864 int bitflips = 0;
2865 int weight;
2866
2867 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2868 len--, bitmap++) {
2869 weight = hweight8(*bitmap);
2870 bitflips += BITS_PER_BYTE - weight;
2871 if (unlikely(bitflips > bitflips_threshold))
2872 return -EBADMSG;
2873 }
2874
2875 for (; len >= sizeof(long);
2876 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002877 unsigned long d = *((unsigned long *)bitmap);
2878 if (d == ~0UL)
2879 continue;
2880 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002881 bitflips += BITS_PER_LONG - weight;
2882 if (unlikely(bitflips > bitflips_threshold))
2883 return -EBADMSG;
2884 }
2885
2886 for (; len > 0; len--, bitmap++) {
2887 weight = hweight8(*bitmap);
2888 bitflips += BITS_PER_BYTE - weight;
2889 if (unlikely(bitflips > bitflips_threshold))
2890 return -EBADMSG;
2891 }
2892
2893 return bitflips;
2894}
2895
2896/**
2897 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2898 * 0xff data
2899 * @data: data buffer to test
2900 * @datalen: data length
2901 * @ecc: ECC buffer
2902 * @ecclen: ECC length
2903 * @extraoob: extra OOB buffer
2904 * @extraooblen: extra OOB length
2905 * @bitflips_threshold: maximum number of bitflips
2906 *
2907 * Check if a data buffer and its associated ECC and OOB data contains only
2908 * 0xff pattern, which means the underlying region has been erased and is
2909 * ready to be programmed.
2910 * The bitflips_threshold specify the maximum number of bitflips before
2911 * considering the region as not erased.
2912 *
2913 * Note:
2914 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2915 * different from the NAND page size. When fixing bitflips, ECC engines will
2916 * report the number of errors per chunk, and the NAND core infrastructure
2917 * expect you to return the maximum number of bitflips for the whole page.
2918 * This is why you should always use this function on a single chunk and
2919 * not on the whole page. After checking each chunk you should update your
2920 * max_bitflips value accordingly.
2921 * 2/ When checking for bitflips in erased pages you should not only check
2922 * the payload data but also their associated ECC data, because a user might
2923 * have programmed almost all bits to 1 but a few. In this case, we
2924 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2925 * this case.
2926 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2927 * data are protected by the ECC engine.
2928 * It could also be used if you support subpages and want to attach some
2929 * extra OOB data to an ECC chunk.
2930 *
2931 * Returns a positive number of bitflips less than or equal to
2932 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2933 * threshold. In case of success, the passed buffers are filled with 0xff.
2934 */
2935int nand_check_erased_ecc_chunk(void *data, int datalen,
2936 void *ecc, int ecclen,
2937 void *extraoob, int extraooblen,
2938 int bitflips_threshold)
2939{
2940 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2941
2942 data_bitflips = nand_check_erased_buf(data, datalen,
2943 bitflips_threshold);
2944 if (data_bitflips < 0)
2945 return data_bitflips;
2946
2947 bitflips_threshold -= data_bitflips;
2948
2949 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2950 if (ecc_bitflips < 0)
2951 return ecc_bitflips;
2952
2953 bitflips_threshold -= ecc_bitflips;
2954
2955 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2956 bitflips_threshold);
2957 if (extraoob_bitflips < 0)
2958 return extraoob_bitflips;
2959
2960 if (data_bitflips)
2961 memset(data, 0xff, datalen);
2962
2963 if (ecc_bitflips)
2964 memset(ecc, 0xff, ecclen);
2965
2966 if (extraoob_bitflips)
2967 memset(extraoob, 0xff, extraooblen);
2968
2969 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2970}
2971EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2972
2973/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002974 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002975 * @chip: nand chip info structure
2976 * @buf: buffer to store read data
2977 * @oob_required: caller requires OOB data read to chip->oob_poi
2978 * @page: page number to read
2979 *
2980 * Returns -ENOTSUPP unconditionally.
2981 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002982int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2983 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002984{
2985 return -ENOTSUPP;
2986}
2987EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2988
2989/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002990 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002991 * @chip: nand chip info structure
2992 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002993 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002994 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002995 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002996 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002998int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2999 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003000{
Boris Brezillonb9761682018-09-06 14:05:20 +02003001 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003002 int ret;
3003
Boris Brezillon25f815f2017-11-30 18:01:30 +01003004 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003005 if (ret)
3006 return ret;
3007
3008 if (oob_required) {
3009 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
3010 false);
3011 if (ret)
3012 return ret;
3013 }
3014
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003015 return 0;
3016}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003017EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003018
3019/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003020 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07003021 * @chip: nand chip info structure
3022 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003023 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003024 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08003025 *
3026 * We need a special oob layout and handling even when OOB isn't used.
3027 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003028static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003029 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003030{
Boris Brezillonb9761682018-09-06 14:05:20 +02003031 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003032 int eccsize = chip->ecc.size;
3033 int eccbytes = chip->ecc.bytes;
3034 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003035 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003036
Boris Brezillon25f815f2017-11-30 18:01:30 +01003037 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3038 if (ret)
3039 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003040
3041 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003042 ret = nand_read_data_op(chip, buf, eccsize, false);
3043 if (ret)
3044 return ret;
3045
David Brownell52ff49d2009-03-04 12:01:36 -08003046 buf += eccsize;
3047
3048 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003049 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3050 false);
3051 if (ret)
3052 return ret;
3053
David Brownell52ff49d2009-03-04 12:01:36 -08003054 oob += chip->ecc.prepad;
3055 }
3056
Boris Brezillon97d90da2017-11-30 18:01:29 +01003057 ret = nand_read_data_op(chip, oob, eccbytes, false);
3058 if (ret)
3059 return ret;
3060
David Brownell52ff49d2009-03-04 12:01:36 -08003061 oob += eccbytes;
3062
3063 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003064 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3065 false);
3066 if (ret)
3067 return ret;
3068
David Brownell52ff49d2009-03-04 12:01:36 -08003069 oob += chip->ecc.postpad;
3070 }
3071 }
3072
3073 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003074 if (size) {
3075 ret = nand_read_data_op(chip, oob, size, false);
3076 if (ret)
3077 return ret;
3078 }
David Brownell52ff49d2009-03-04 12:01:36 -08003079
3080 return 0;
3081}
3082
3083/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003084 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003085 * @chip: nand chip info structure
3086 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003087 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003088 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003089 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003090static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3091 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Boris Brezillonb9761682018-09-06 14:05:20 +02003093 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003094 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003095 int eccbytes = chip->ecc.bytes;
3096 int eccsteps = chip->ecc.steps;
3097 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003098 uint8_t *ecc_calc = chip->ecc.calc_buf;
3099 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003100 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003101
Boris Brezillonb9761682018-09-06 14:05:20 +02003102 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003103
3104 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003105 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003106
Boris Brezillon846031d2016-02-03 20:11:00 +01003107 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3108 chip->ecc.total);
3109 if (ret)
3110 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003111
3112 eccsteps = chip->ecc.steps;
3113 p = buf;
3114
3115 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3116 int stat;
3117
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003118 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003119 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003120 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003121 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003122 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003123 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3124 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003125 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003126 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303130 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003131 * @chip: nand chip info structure
3132 * @data_offs: offset of requested data within the page
3133 * @readlen: data length
3134 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003135 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003136 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003137static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3138 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003139{
Boris Brezillonb9761682018-09-06 14:05:20 +02003140 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003141 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003142 uint8_t *p;
3143 int data_col_addr, i, gaps = 0;
3144 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3145 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003146 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003147 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003148 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003149
Brian Norris7854d3f2011-06-23 14:12:08 -07003150 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003151 start_step = data_offs / chip->ecc.size;
3152 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3153 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303154 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003155
Brian Norris8b6e50c2011-05-25 14:59:01 -07003156 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003157 datafrag_len = num_steps * chip->ecc.size;
3158 eccfrag_len = num_steps * chip->ecc.bytes;
3159
3160 data_col_addr = start_step * chip->ecc.size;
3161 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003162 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003163 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003164 if (ret)
3165 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003166
Brian Norris8b6e50c2011-05-25 14:59:01 -07003167 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003168 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003169 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003170
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 /*
3172 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003173 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003174 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003175 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3176 if (ret)
3177 return ret;
3178
3179 if (oobregion.length < eccfrag_len)
3180 gaps = 1;
3181
Alexey Korolev3d459552008-05-15 17:23:18 +01003182 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003183 ret = nand_change_read_column_op(chip, mtd->writesize,
3184 chip->oob_poi, mtd->oobsize,
3185 false);
3186 if (ret)
3187 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003188 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003189 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003190 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003191 * about buswidth alignment in read_buf.
3192 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003193 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003194 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003195 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003196 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003197 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3198 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003199 aligned_len++;
3200
Boris Brezillon97d90da2017-11-30 18:01:29 +01003201 ret = nand_change_read_column_op(chip,
3202 mtd->writesize + aligned_pos,
3203 &chip->oob_poi[aligned_pos],
3204 aligned_len, false);
3205 if (ret)
3206 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003207 }
3208
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003209 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003210 chip->oob_poi, index, eccfrag_len);
3211 if (ret)
3212 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003213
3214 p = bufpoi + data_col_addr;
3215 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3216 int stat;
3217
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003218 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003219 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003220 if (stat == -EBADMSG &&
3221 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3222 /* check for empty pages with bitflips */
3223 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003224 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003225 chip->ecc.bytes,
3226 NULL, 0,
3227 chip->ecc.strength);
3228 }
3229
Mike Dunn3f91e942012-04-25 12:06:09 -07003230 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003231 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003232 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003233 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003234 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3235 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003236 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003237 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003238}
3239
3240/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003241 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003242 * @chip: nand chip info structure
3243 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003244 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003245 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003246 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003247 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003248 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003249static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3250 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003251{
Boris Brezillonb9761682018-09-06 14:05:20 +02003252 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003253 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003254 int eccbytes = chip->ecc.bytes;
3255 int eccsteps = chip->ecc.steps;
3256 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003257 uint8_t *ecc_calc = chip->ecc.calc_buf;
3258 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003259 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003260
Boris Brezillon25f815f2017-11-30 18:01:30 +01003261 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3262 if (ret)
3263 return ret;
3264
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003265 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003266 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003267
3268 ret = nand_read_data_op(chip, p, eccsize, false);
3269 if (ret)
3270 return ret;
3271
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003272 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003273 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003274
3275 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3276 if (ret)
3277 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003278
Boris Brezillon846031d2016-02-03 20:11:00 +01003279 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3280 chip->ecc.total);
3281 if (ret)
3282 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003283
3284 eccsteps = chip->ecc.steps;
3285 p = buf;
3286
3287 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3288 int stat;
3289
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003290 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003291 if (stat == -EBADMSG &&
3292 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3293 /* check for empty pages with bitflips */
3294 stat = nand_check_erased_ecc_chunk(p, eccsize,
3295 &ecc_code[i], eccbytes,
3296 NULL, 0,
3297 chip->ecc.strength);
3298 }
3299
Mike Dunn3f91e942012-04-25 12:06:09 -07003300 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003301 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003302 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003303 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003304 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3305 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003306 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003307 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003308}
3309
3310/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003311 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003312 * @chip: nand chip info structure
3313 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003314 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003315 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003316 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003317 * Hardware ECC for large page chips, require OOB to be read first. For this
3318 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3319 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3320 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3321 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003322 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003323static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3324 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003325{
Boris Brezillonb9761682018-09-06 14:05:20 +02003326 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003327 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003328 int eccbytes = chip->ecc.bytes;
3329 int eccsteps = chip->ecc.steps;
3330 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003331 uint8_t *ecc_code = chip->ecc.code_buf;
3332 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003333 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003334
3335 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003336 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3337 if (ret)
3338 return ret;
3339
3340 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3341 if (ret)
3342 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003343
Boris Brezillon846031d2016-02-03 20:11:00 +01003344 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3345 chip->ecc.total);
3346 if (ret)
3347 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003348
3349 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3350 int stat;
3351
Boris Brezillonec476362018-09-06 14:05:17 +02003352 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003353
3354 ret = nand_read_data_op(chip, p, eccsize, false);
3355 if (ret)
3356 return ret;
3357
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003358 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003359
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003360 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003361 if (stat == -EBADMSG &&
3362 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3363 /* check for empty pages with bitflips */
3364 stat = nand_check_erased_ecc_chunk(p, eccsize,
3365 &ecc_code[i], eccbytes,
3366 NULL, 0,
3367 chip->ecc.strength);
3368 }
3369
Mike Dunn3f91e942012-04-25 12:06:09 -07003370 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003371 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003372 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003373 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003374 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3375 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003376 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003377 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003378}
3379
3380/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003381 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003382 * @chip: nand chip info structure
3383 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003384 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003385 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003386 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003387 * The hw generator calculates the error syndrome automatically. Therefore we
3388 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003389 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003390static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3391 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003392{
Boris Brezillonb9761682018-09-06 14:05:20 +02003393 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003394 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003395 int eccbytes = chip->ecc.bytes;
3396 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003397 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003398 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003399 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003400 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003401
Boris Brezillon25f815f2017-11-30 18:01:30 +01003402 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3403 if (ret)
3404 return ret;
3405
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003406 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3407 int stat;
3408
Boris Brezillonec476362018-09-06 14:05:17 +02003409 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003410
3411 ret = nand_read_data_op(chip, p, eccsize, false);
3412 if (ret)
3413 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003414
3415 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003416 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3417 false);
3418 if (ret)
3419 return ret;
3420
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003421 oob += chip->ecc.prepad;
3422 }
3423
Boris Brezillonec476362018-09-06 14:05:17 +02003424 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003425
3426 ret = nand_read_data_op(chip, oob, eccbytes, false);
3427 if (ret)
3428 return ret;
3429
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003430 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003431
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003432 oob += eccbytes;
3433
3434 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003435 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3436 false);
3437 if (ret)
3438 return ret;
3439
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003440 oob += chip->ecc.postpad;
3441 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003442
3443 if (stat == -EBADMSG &&
3444 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3445 /* check for empty pages with bitflips */
3446 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3447 oob - eccpadbytes,
3448 eccpadbytes,
3449 NULL, 0,
3450 chip->ecc.strength);
3451 }
3452
3453 if (stat < 0) {
3454 mtd->ecc_stats.failed++;
3455 } else {
3456 mtd->ecc_stats.corrected += stat;
3457 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3458 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003459 }
3460
3461 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003462 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003463 if (i) {
3464 ret = nand_read_data_op(chip, oob, i, false);
3465 if (ret)
3466 return ret;
3467 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003468
Mike Dunn3f91e942012-04-25 12:06:09 -07003469 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003470}
3471
3472/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003473 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003474 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003475 * @oob: oob destination address
3476 * @ops: oob ops structure
3477 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003478 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003479static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003480 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003481{
Boris Brezillon846031d2016-02-03 20:11:00 +01003482 struct nand_chip *chip = mtd_to_nand(mtd);
3483 int ret;
3484
Florian Fainellif8ac0412010-09-07 13:23:43 +02003485 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003486
Brian Norris0612b9d2011-08-30 18:45:40 -07003487 case MTD_OPS_PLACE_OOB:
3488 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003489 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3490 return oob + len;
3491
Boris Brezillon846031d2016-02-03 20:11:00 +01003492 case MTD_OPS_AUTO_OOB:
3493 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3494 ops->ooboffs, len);
3495 BUG_ON(ret);
3496 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003497
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003498 default:
3499 BUG();
3500 }
3501 return NULL;
3502}
3503
3504/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003505 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003506 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003507 * @retry_mode: the retry mode to use
3508 *
3509 * Some vendors supply a special command to shift the Vt threshold, to be used
3510 * when there are too many bitflips in a page (i.e., ECC error). After setting
3511 * a new threshold, the host should retry reading the page.
3512 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003513static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003514{
Brian Norrisba84fb52014-01-03 15:13:33 -08003515 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3516
3517 if (retry_mode >= chip->read_retries)
3518 return -EINVAL;
3519
3520 if (!chip->setup_read_retry)
3521 return -EOPNOTSUPP;
3522
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003523 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003524}
3525
Boris Brezillon85e08e52018-07-27 09:44:17 +02003526static void nand_wait_readrdy(struct nand_chip *chip)
3527{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003528 const struct nand_sdr_timings *sdr;
3529
Boris Brezillon85e08e52018-07-27 09:44:17 +02003530 if (!(chip->options & NAND_NEED_READRDY))
3531 return;
3532
Boris Brezillon52f05b62018-07-27 09:44:18 +02003533 sdr = nand_get_sdr_timings(&chip->data_interface);
3534 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003535}
3536
Brian Norrisba84fb52014-01-03 15:13:33 -08003537/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003538 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003539 * @mtd: MTD device structure
3540 * @from: offset to read from
3541 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003542 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003543 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003544 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003545static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3546 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003547{
Brian Norrise47f3db2012-05-02 10:14:56 -07003548 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003549 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003550 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003551 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003552 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003553 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003554
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003555 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003556 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003557 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003558 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003559 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003561 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003562 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003564 realpage = (int)(from >> chip->page_shift);
3565 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003567 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003569 buf = ops->datbuf;
3570 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003571 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003572
Florian Fainellif8ac0412010-09-07 13:23:43 +02003573 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003574 unsigned int ecc_failures = mtd->ecc_stats.failed;
3575
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003576 bytes = min(mtd->writesize - col, readlen);
3577 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003578
Kamal Dasu66507c72014-05-01 20:51:19 -04003579 if (!aligned)
3580 use_bufpoi = 1;
3581 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003582 use_bufpoi = !virt_addr_valid(buf) ||
3583 !IS_ALIGNED((unsigned long)buf,
3584 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003585 else
3586 use_bufpoi = 0;
3587
Brian Norris8b6e50c2011-05-25 14:59:01 -07003588 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003589 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003590 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003591
3592 if (use_bufpoi && aligned)
3593 pr_debug("%s: using read bounce buffer for buf@%p\n",
3594 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Brian Norrisba84fb52014-01-03 15:13:33 -08003596read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003597 /*
3598 * Now read the page into the buffer. Absent an error,
3599 * the read methods return max bitflips per ecc step.
3600 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003601 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003602 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003603 oob_required,
3604 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003605 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3606 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003607 ret = chip->ecc.read_subpage(chip, col, bytes,
3608 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003609 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003610 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003611 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003612 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003613 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003614 /* Invalidate page cache */
3615 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003616 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003617 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003618
3619 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003620 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003621 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003622 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003623 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003624 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003625 chip->pagebuf_bitflips = ret;
3626 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003627 /* Invalidate page cache */
3628 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003629 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003630 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003632
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003633 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003634 int toread = min(oobreadlen, max_oobsize);
3635
3636 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003637 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003638 oob, ops, toread);
3639 oobreadlen -= toread;
3640 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003641 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003642
Boris Brezillon85e08e52018-07-27 09:44:17 +02003643 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003644
Brian Norrisba84fb52014-01-03 15:13:33 -08003645 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003646 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003647 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003648 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003649 retry_mode);
3650 if (ret < 0)
3651 break;
3652
3653 /* Reset failures; retry */
3654 mtd->ecc_stats.failed = ecc_failures;
3655 goto read_retry;
3656 } else {
3657 /* No more retry modes; real failure */
3658 ecc_fail = true;
3659 }
3660 }
3661
3662 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003663 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003664 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003665 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003666 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003667 max_bitflips = max_t(unsigned int, max_bitflips,
3668 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003671 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003672
Brian Norrisba84fb52014-01-03 15:13:33 -08003673 /* Reset to retry mode 0 */
3674 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003675 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003676 if (ret < 0)
3677 break;
3678 retry_mode = 0;
3679 }
3680
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003681 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Brian Norris8b6e50c2011-05-25 14:59:01 -07003684 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 col = 0;
3686 /* Increment page address */
3687 realpage++;
3688
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003689 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 /* Check, if we cross a chip boundary */
3691 if (!page) {
3692 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003693 chip->select_chip(chip, -1);
3694 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003697 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003699 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003700 if (oob)
3701 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Mike Dunn3f91e942012-04-25 12:06:09 -07003703 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003704 return ret;
3705
Brian Norrisb72f3df2013-12-03 11:04:14 -08003706 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003707 return -EBADMSG;
3708
Mike Dunnedbc45402012-04-25 12:06:11 -07003709 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003710}
3711
3712/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003713 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003714 * @chip: nand chip info structure
3715 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003716 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003717int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003718{
Boris Brezillonb9761682018-09-06 14:05:20 +02003719 struct mtd_info *mtd = nand_to_mtd(chip);
3720
Boris Brezillon97d90da2017-11-30 18:01:29 +01003721 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003722}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003723EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003724
3725/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003726 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003727 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003728 * @chip: nand chip info structure
3729 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003730 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003731int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003732{
Boris Brezillonb9761682018-09-06 14:05:20 +02003733 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003734 int length = mtd->oobsize;
3735 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3736 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003737 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003738 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003739
Boris Brezillon97d90da2017-11-30 18:01:29 +01003740 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3741 if (ret)
3742 return ret;
3743
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003744 for (i = 0; i < chip->ecc.steps; i++) {
3745 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003746 int ret;
3747
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003748 pos = eccsize + i * (eccsize + chunk);
3749 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003750 ret = nand_change_read_column_op(chip, pos,
3751 NULL, 0,
3752 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003753 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003754 ret = nand_read_page_op(chip, page, pos, NULL,
3755 0);
3756
3757 if (ret)
3758 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003759 } else
3760 sndrnd = 1;
3761 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003762
3763 ret = nand_read_data_op(chip, bufpoi, toread, false);
3764 if (ret)
3765 return ret;
3766
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003767 bufpoi += toread;
3768 length -= toread;
3769 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003770 if (length > 0) {
3771 ret = nand_read_data_op(chip, bufpoi, length, false);
3772 if (ret)
3773 return ret;
3774 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003775
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003776 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003777}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003778EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003779
3780/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003781 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003782 * @chip: nand chip info structure
3783 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003784 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003785int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003786{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003787 struct mtd_info *mtd = nand_to_mtd(chip);
3788
Boris Brezillon97d90da2017-11-30 18:01:29 +01003789 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3790 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003791}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003792EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003793
3794/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003795 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003796 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003797 * @chip: nand chip info structure
3798 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003799 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003800int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003801{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003802 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003803 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3804 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003805 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003806 const uint8_t *bufpoi = chip->oob_poi;
3807
3808 /*
3809 * data-ecc-data-ecc ... ecc-oob
3810 * or
3811 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3812 */
3813 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3814 pos = steps * (eccsize + chunk);
3815 steps = 0;
3816 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003817 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003818
Boris Brezillon97d90da2017-11-30 18:01:29 +01003819 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3820 if (ret)
3821 return ret;
3822
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003823 for (i = 0; i < steps; i++) {
3824 if (sndcmd) {
3825 if (mtd->writesize <= 512) {
3826 uint32_t fill = 0xFFFFFFFF;
3827
3828 len = eccsize;
3829 while (len > 0) {
3830 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003831
3832 ret = nand_write_data_op(chip, &fill,
3833 num, false);
3834 if (ret)
3835 return ret;
3836
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003837 len -= num;
3838 }
3839 } else {
3840 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003841 ret = nand_change_write_column_op(chip, pos,
3842 NULL, 0,
3843 false);
3844 if (ret)
3845 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003846 }
3847 } else
3848 sndcmd = 1;
3849 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003850
3851 ret = nand_write_data_op(chip, bufpoi, len, false);
3852 if (ret)
3853 return ret;
3854
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003855 bufpoi += len;
3856 length -= len;
3857 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003858 if (length > 0) {
3859 ret = nand_write_data_op(chip, bufpoi, length, false);
3860 if (ret)
3861 return ret;
3862 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003863
Boris Brezillon97d90da2017-11-30 18:01:29 +01003864 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003865}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003866EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003867
3868/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003869 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003870 * @mtd: MTD device structure
3871 * @from: offset to read from
3872 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003874 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003876static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3877 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003879 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003880 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003881 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003882 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003883 int readlen = ops->ooblen;
3884 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003885 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003886 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Brian Norris289c0522011-07-19 10:06:09 -07003888 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303889 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Brian Norris041e4572011-06-23 16:45:24 -07003891 stats = mtd->ecc_stats;
3892
Boris BREZILLON29f10582016-03-07 10:46:52 +01003893 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003894
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003895 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003896 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003898 /* Shift to get page */
3899 realpage = (int)(from >> chip->page_shift);
3900 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Florian Fainellif8ac0412010-09-07 13:23:43 +02003902 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003903 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003904 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003905 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003906 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003907
3908 if (ret < 0)
3909 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003910
3911 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003912 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003913
Boris Brezillon85e08e52018-07-27 09:44:17 +02003914 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003915
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003916 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3917
Vitaly Wool70145682006-11-03 18:20:38 +03003918 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003919 if (!readlen)
3920 break;
3921
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003922 /* Increment page address */
3923 realpage++;
3924
3925 page = realpage & chip->pagemask;
3926 /* Check, if we cross a chip boundary */
3927 if (!page) {
3928 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003929 chip->select_chip(chip, -1);
3930 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003933 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003935 ops->oobretlen = ops->ooblen - readlen;
3936
3937 if (ret < 0)
3938 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003939
3940 if (mtd->ecc_stats.failed - stats.failed)
3941 return -EBADMSG;
3942
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003943 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
3946/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003947 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003948 * @mtd: MTD device structure
3949 * @from: offset to read from
3950 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003952 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003954static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3955 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003957 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003958
3959 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003961 if (ops->mode != MTD_OPS_PLACE_OOB &&
3962 ops->mode != MTD_OPS_AUTO_OOB &&
3963 ops->mode != MTD_OPS_RAW)
3964 return -ENOTSUPP;
3965
Huang Shijie6a8214a2012-11-19 14:43:30 +08003966 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003968 if (!ops->datbuf)
3969 ret = nand_do_read_oob(mtd, from, ops);
3970 else
3971 ret = nand_do_read_ops(mtd, from, ops);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003974 return ret;
3975}
3976
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003977/**
3978 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003979 * @chip: nand chip info structure
3980 * @buf: data buffer
3981 * @oob_required: must write chip->oob_poi to OOB
3982 * @page: page number to write
3983 *
3984 * Returns -ENOTSUPP unconditionally.
3985 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003986int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3987 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003988{
3989 return -ENOTSUPP;
3990}
3991EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003992
3993/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003994 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003995 * @chip: nand chip info structure
3996 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003997 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003998 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003999 *
Brian Norris7854d3f2011-06-23 14:12:08 -07004000 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004001 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004002int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
4003 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004004{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004005 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004006 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004007
Boris Brezillon25f815f2017-11-30 18:01:30 +01004008 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004009 if (ret)
4010 return ret;
4011
4012 if (oob_required) {
4013 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
4014 false);
4015 if (ret)
4016 return ret;
4017 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004018
Boris Brezillon25f815f2017-11-30 18:01:30 +01004019 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02004021EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004023/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004024 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004025 * @chip: nand chip info structure
4026 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004027 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004028 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004029 *
4030 * We need a special oob layout and handling even when ECC isn't checked.
4031 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004032static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004033 const uint8_t *buf, int oob_required,
4034 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004035{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004036 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004037 int eccsize = chip->ecc.size;
4038 int eccbytes = chip->ecc.bytes;
4039 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004040 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004041
Boris Brezillon25f815f2017-11-30 18:01:30 +01004042 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4043 if (ret)
4044 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004045
4046 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004047 ret = nand_write_data_op(chip, buf, eccsize, false);
4048 if (ret)
4049 return ret;
4050
David Brownell52ff49d2009-03-04 12:01:36 -08004051 buf += eccsize;
4052
4053 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004054 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4055 false);
4056 if (ret)
4057 return ret;
4058
David Brownell52ff49d2009-03-04 12:01:36 -08004059 oob += chip->ecc.prepad;
4060 }
4061
Boris Brezillon97d90da2017-11-30 18:01:29 +01004062 ret = nand_write_data_op(chip, oob, eccbytes, false);
4063 if (ret)
4064 return ret;
4065
David Brownell52ff49d2009-03-04 12:01:36 -08004066 oob += eccbytes;
4067
4068 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004069 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4070 false);
4071 if (ret)
4072 return ret;
4073
David Brownell52ff49d2009-03-04 12:01:36 -08004074 oob += chip->ecc.postpad;
4075 }
4076 }
4077
4078 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004079 if (size) {
4080 ret = nand_write_data_op(chip, oob, size, false);
4081 if (ret)
4082 return ret;
4083 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004084
Boris Brezillon25f815f2017-11-30 18:01:30 +01004085 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004086}
4087/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004088 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004089 * @chip: nand chip info structure
4090 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004091 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004092 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004093 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004094static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4095 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004096{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004097 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004098 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004099 int eccbytes = chip->ecc.bytes;
4100 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004101 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004102 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004103
Brian Norris7854d3f2011-06-23 14:12:08 -07004104 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004105 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004106 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004107
Boris Brezillon846031d2016-02-03 20:11:00 +01004108 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4109 chip->ecc.total);
4110 if (ret)
4111 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004112
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004113 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004114}
4115
4116/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004117 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004118 * @chip: nand chip info structure
4119 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004120 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004121 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004122 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004123static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4124 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004125{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004126 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004127 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004128 int eccbytes = chip->ecc.bytes;
4129 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004130 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004131 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004132
Boris Brezillon25f815f2017-11-30 18:01:30 +01004133 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4134 if (ret)
4135 return ret;
4136
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004137 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004138 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004139
4140 ret = nand_write_data_op(chip, p, eccsize, false);
4141 if (ret)
4142 return ret;
4143
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004144 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004145 }
4146
Boris Brezillon846031d2016-02-03 20:11:00 +01004147 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4148 chip->ecc.total);
4149 if (ret)
4150 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004151
Boris Brezillon97d90da2017-11-30 18:01:29 +01004152 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4153 if (ret)
4154 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004155
Boris Brezillon25f815f2017-11-30 18:01:30 +01004156 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004157}
4158
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304159
4160/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004161 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304162 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004163 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304164 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004165 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304166 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004167 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304168 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004169static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4170 uint32_t data_len, const uint8_t *buf,
4171 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304172{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004173 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304174 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004175 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304176 int ecc_size = chip->ecc.size;
4177 int ecc_bytes = chip->ecc.bytes;
4178 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304179 uint32_t start_step = offset / ecc_size;
4180 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4181 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004182 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304183
Boris Brezillon25f815f2017-11-30 18:01:30 +01004184 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4185 if (ret)
4186 return ret;
4187
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304188 for (step = 0; step < ecc_steps; step++) {
4189 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004190 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304191
4192 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004193 ret = nand_write_data_op(chip, buf, ecc_size, false);
4194 if (ret)
4195 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304196
4197 /* mask ECC of un-touched subpages by padding 0xFF */
4198 if ((step < start_step) || (step > end_step))
4199 memset(ecc_calc, 0xff, ecc_bytes);
4200 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004201 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304202
4203 /* mask OOB of un-touched subpages by padding 0xFF */
4204 /* if oob_required, preserve OOB metadata of written subpage */
4205 if (!oob_required || (step < start_step) || (step > end_step))
4206 memset(oob_buf, 0xff, oob_bytes);
4207
Brian Norrisd6a950802013-08-08 17:16:36 -07004208 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304209 ecc_calc += ecc_bytes;
4210 oob_buf += oob_bytes;
4211 }
4212
4213 /* copy calculated ECC for whole page to chip->buffer->oob */
4214 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004215 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004216 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4217 chip->ecc.total);
4218 if (ret)
4219 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304220
4221 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004222 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4223 if (ret)
4224 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304225
Boris Brezillon25f815f2017-11-30 18:01:30 +01004226 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304227}
4228
4229
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004230/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004231 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004232 * @chip: nand chip info structure
4233 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004234 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004235 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004236 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004237 * The hw generator calculates the error syndrome automatically. Therefore we
4238 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004239 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004240static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4241 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004242{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004243 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004244 int i, eccsize = chip->ecc.size;
4245 int eccbytes = chip->ecc.bytes;
4246 int eccsteps = chip->ecc.steps;
4247 const uint8_t *p = buf;
4248 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004249 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004250
Boris Brezillon25f815f2017-11-30 18:01:30 +01004251 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4252 if (ret)
4253 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004254
4255 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004256 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004257
4258 ret = nand_write_data_op(chip, p, eccsize, false);
4259 if (ret)
4260 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004261
4262 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004263 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4264 false);
4265 if (ret)
4266 return ret;
4267
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004268 oob += chip->ecc.prepad;
4269 }
4270
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004271 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004272
4273 ret = nand_write_data_op(chip, oob, eccbytes, false);
4274 if (ret)
4275 return ret;
4276
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004277 oob += eccbytes;
4278
4279 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004280 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4281 false);
4282 if (ret)
4283 return ret;
4284
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004285 oob += chip->ecc.postpad;
4286 }
4287 }
4288
4289 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004290 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004291 if (i) {
4292 ret = nand_write_data_op(chip, oob, i, false);
4293 if (ret)
4294 return ret;
4295 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004296
Boris Brezillon25f815f2017-11-30 18:01:30 +01004297 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004298}
4299
4300/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004301 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004302 * @mtd: MTD device structure
4303 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304304 * @offset: address offset within the page
4305 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004306 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004307 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004308 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004309 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004310 */
4311static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304312 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004313 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004314{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304315 int status, subpage;
4316
4317 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4318 chip->ecc.write_subpage)
4319 subpage = offset || (data_len < mtd->writesize);
4320 else
4321 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004322
David Woodhouse956e9442006-09-25 17:12:39 +01004323 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004324 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4325 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304326 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004327 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4328 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004329 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004330 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004331
4332 if (status < 0)
4333 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004334
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004335 return 0;
4336}
4337
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004338/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004339 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004340 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004341 * @oob: oob data buffer
4342 * @len: oob data write length
4343 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004344 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004345static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4346 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004347{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004348 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004349 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004350
4351 /*
4352 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4353 * data from a previous OOB read.
4354 */
4355 memset(chip->oob_poi, 0xff, mtd->oobsize);
4356
Florian Fainellif8ac0412010-09-07 13:23:43 +02004357 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004358
Brian Norris0612b9d2011-08-30 18:45:40 -07004359 case MTD_OPS_PLACE_OOB:
4360 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004361 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4362 return oob + len;
4363
Boris Brezillon846031d2016-02-03 20:11:00 +01004364 case MTD_OPS_AUTO_OOB:
4365 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4366 ops->ooboffs, len);
4367 BUG_ON(ret);
4368 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004370 default:
4371 BUG();
4372 }
4373 return NULL;
4374}
4375
Florian Fainellif8ac0412010-09-07 13:23:43 +02004376#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377
4378/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004379 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004380 * @mtd: MTD device structure
4381 * @to: offset to write to
4382 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004383 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004384 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004385 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004386static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4387 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004388{
Corentin Labbe73600b62017-09-02 10:49:38 +02004389 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004390 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004391 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004392
4393 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004394 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004395
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004396 uint8_t *oob = ops->oobbuf;
4397 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304398 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004399 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004400
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004401 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004402 if (!writelen)
4403 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004404
Brian Norris8b6e50c2011-05-25 14:59:01 -07004405 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004406 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004407 pr_notice("%s: attempt to write non page aligned data\n",
4408 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004409 return -EINVAL;
4410 }
4411
Thomas Gleixner29072b92006-09-28 15:38:36 +02004412 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004413
Thomas Gleixner6a930962006-06-28 00:11:45 +02004414 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004415 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004416
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004417 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004418 if (nand_check_wp(mtd)) {
4419 ret = -EIO;
4420 goto err_out;
4421 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004422
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004423 realpage = (int)(to >> chip->page_shift);
4424 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004425
4426 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004427 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4428 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004429 chip->pagebuf = -1;
4430
Maxim Levitsky782ce792010-02-22 20:39:36 +02004431 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004432 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4433 ret = -EINVAL;
4434 goto err_out;
4435 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004436
Florian Fainellif8ac0412010-09-07 13:23:43 +02004437 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004438 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004439 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004440 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004441 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004442
Kamal Dasu66507c72014-05-01 20:51:19 -04004443 if (part_pagewr)
4444 use_bufpoi = 1;
4445 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004446 use_bufpoi = !virt_addr_valid(buf) ||
4447 !IS_ALIGNED((unsigned long)buf,
4448 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004449 else
4450 use_bufpoi = 0;
4451
4452 /* Partial page write?, or need to use bounce buffer */
4453 if (use_bufpoi) {
4454 pr_debug("%s: using write bounce buffer for buf@%p\n",
4455 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004456 if (part_pagewr)
4457 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004458 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004459 memset(chip->data_buf, 0xff, mtd->writesize);
4460 memcpy(&chip->data_buf[column], buf, bytes);
4461 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004462 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004463
Maxim Levitsky782ce792010-02-22 20:39:36 +02004464 if (unlikely(oob)) {
4465 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004466 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004467 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004468 } else {
4469 /* We still need to erase leftover OOB data */
4470 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004471 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004472
4473 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004474 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004475 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004476 if (ret)
4477 break;
4478
4479 writelen -= bytes;
4480 if (!writelen)
4481 break;
4482
Thomas Gleixner29072b92006-09-28 15:38:36 +02004483 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004484 buf += bytes;
4485 realpage++;
4486
4487 page = realpage & chip->pagemask;
4488 /* Check, if we cross a chip boundary */
4489 if (!page) {
4490 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004491 chip->select_chip(chip, -1);
4492 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004493 }
4494 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004496 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004497 if (unlikely(oob))
4498 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004499
4500err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004501 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004502 return ret;
4503}
4504
4505/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004506 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004507 * @mtd: MTD device structure
4508 * @to: offset to write to
4509 * @len: number of bytes to write
4510 * @retlen: pointer to variable to store the number of written bytes
4511 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004512 *
4513 * NAND write with ECC. Used when performing writes in interrupt context, this
4514 * may for example be called by mtdoops when writing an oops while in panic.
4515 */
4516static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4517 size_t *retlen, const uint8_t *buf)
4518{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004519 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004520 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004521 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004522 int ret;
4523
Brian Norris8b6e50c2011-05-25 14:59:01 -07004524 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004525 panic_nand_get_device(chip, mtd, FL_WRITING);
4526
Boris Brezillon758b56f2018-09-06 14:05:24 +02004527 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004528
4529 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004530 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004531
Brian Norris0ec56dc2015-02-28 02:02:30 -08004532 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004533 ops.len = len;
4534 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004535 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004536
Brian Norris4a89ff82011-08-30 18:45:45 -07004537 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004538
Brian Norris4a89ff82011-08-30 18:45:45 -07004539 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004540 return ret;
4541}
4542
4543/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004544 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004545 * @mtd: MTD device structure
4546 * @to: offset to write to
4547 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004548 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004549 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004550 */
4551static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4552 struct mtd_oob_ops *ops)
4553{
Adrian Hunter03736152007-01-31 17:58:29 +02004554 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004555 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Brian Norris289c0522011-07-19 10:06:09 -07004557 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304558 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Boris BREZILLON29f10582016-03-07 10:46:52 +01004560 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004563 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004564 pr_debug("%s: attempt to write past end of page\n",
4565 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return -EINVAL;
4567 }
4568
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004569 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004570
4571 /*
4572 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4573 * of my DiskOnChip 2000 test units) will clear the whole data page too
4574 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4575 * it in the doc2000 driver in August 1999. dwmw2.
4576 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004577 nand_reset(chip, chipnr);
4578
Boris Brezillon758b56f2018-09-06 14:05:24 +02004579 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004580
4581 /* Shift to get page */
4582 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004585 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004586 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004587 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004588 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004591 if (page == chip->pagebuf)
4592 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004594 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004595
Brian Norris0612b9d2011-08-30 18:45:40 -07004596 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004597 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004598 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004599 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004600
Boris Brezillon758b56f2018-09-06 14:05:24 +02004601 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004602
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004603 if (status)
4604 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Vitaly Wool70145682006-11-03 18:20:38 +03004606 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004608 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004611/**
4612 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004613 * @mtd: MTD device structure
4614 * @to: offset to write to
4615 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004616 */
4617static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4618 struct mtd_oob_ops *ops)
4619{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004620 int ret = -ENOTSUPP;
4621
4622 ops->retlen = 0;
4623
Huang Shijie6a8214a2012-11-19 14:43:30 +08004624 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004625
Florian Fainellif8ac0412010-09-07 13:23:43 +02004626 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004627 case MTD_OPS_PLACE_OOB:
4628 case MTD_OPS_AUTO_OOB:
4629 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004630 break;
4631
4632 default:
4633 goto out;
4634 }
4635
4636 if (!ops->datbuf)
4637 ret = nand_do_write_oob(mtd, to, ops);
4638 else
4639 ret = nand_do_write_ops(mtd, to, ops);
4640
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004641out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004642 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 return ret;
4644}
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646/**
Brian Norris49c50b92014-05-06 16:02:19 -07004647 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004648 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004649 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 *
Brian Norris49c50b92014-05-06 16:02:19 -07004651 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004653static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004655 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004658 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004659
Boris Brezillon97d90da2017-11-30 18:01:29 +01004660 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
4663/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004665 * @mtd: MTD device structure
4666 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004668 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004670static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004672 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004676 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004677 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004678 * @instr: erase instruction
4679 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004681 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004683int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004684 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004686 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004687 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004688 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Brian Norris289c0522011-07-19 10:06:09 -07004690 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4691 __func__, (unsigned long long)instr->addr,
4692 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304694 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004698 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
4700 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004701 page = (int)(instr->addr >> chip->page_shift);
4702 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004705 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
4707 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004708 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 /* Check, if it is write protected */
4711 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004712 pr_debug("%s: device is write protected!\n",
4713 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004714 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 goto erase_exit;
4716 }
4717
4718 /* Loop through the pages */
4719 len = instr->len;
4720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004722 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004723 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304724 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004725 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4726 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004727 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 goto erase_exit;
4729 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004730
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004731 /*
4732 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004733 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004734 */
4735 if (page <= chip->pagebuf && chip->pagebuf <
4736 (page + pages_per_block))
4737 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Boris Brezillonf9ebd1b2018-09-07 00:38:39 +02004739 if (chip->legacy.erase)
4740 status = chip->legacy.erase(chip,
4741 page & chip->pagemask);
4742 else
4743 status = single_erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004746 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004747 pr_debug("%s: failed erase, page 0x%08x\n",
4748 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004749 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004750 instr->fail_addr =
4751 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 goto erase_exit;
4753 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004756 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 page += pages_per_block;
4758
4759 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004760 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004762 chip->select_chip(chip, -1);
4763 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
4765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004767 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004768erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004771 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 nand_release_device(mtd);
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 /* Return more or less happy */
4775 return ret;
4776}
4777
4778/**
4779 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004780 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004782 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004784static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
Brian Norris289c0522011-07-19 10:06:09 -07004786 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004789 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004791 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004795 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004796 * @mtd: MTD device structure
4797 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004799static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304801 struct nand_chip *chip = mtd_to_nand(mtd);
4802 int chipnr = (int)(offs >> chip->chip_shift);
4803 int ret;
4804
4805 /* Select the NAND device */
4806 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004807 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304808
4809 ret = nand_block_checkbad(mtd, offs, 0);
4810
Boris Brezillon758b56f2018-09-06 14:05:24 +02004811 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304812 nand_release_device(mtd);
4813
4814 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
4817/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004818 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004819 * @mtd: MTD device structure
4820 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004822static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 int ret;
4825
Florian Fainellif8ac0412010-09-07 13:23:43 +02004826 ret = nand_block_isbad(mtd, ofs);
4827 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004828 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 if (ret > 0)
4830 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004831 return ret;
4832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Brian Norris5a0edb22013-07-30 17:52:58 -07004834 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
4837/**
Zach Brown56718422017-01-10 13:30:20 -06004838 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4839 * @mtd: MTD device structure
4840 * @ofs: offset relative to mtd start
4841 * @len: length of mtd
4842 */
4843static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4844{
4845 struct nand_chip *chip = mtd_to_nand(mtd);
4846 u32 part_start_block;
4847 u32 part_end_block;
4848 u32 part_start_die;
4849 u32 part_end_die;
4850
4851 /*
4852 * max_bb_per_die and blocks_per_die used to determine
4853 * the maximum bad block count.
4854 */
4855 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4856 return -ENOTSUPP;
4857
4858 /* Get the start and end of the partition in erase blocks. */
4859 part_start_block = mtd_div_by_eb(ofs, mtd);
4860 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4861
4862 /* Get the start and end LUNs of the partition. */
4863 part_start_die = part_start_block / chip->blocks_per_die;
4864 part_end_die = part_end_block / chip->blocks_per_die;
4865
4866 /*
4867 * Look up the bad blocks per unit and multiply by the number of units
4868 * that the partition spans.
4869 */
4870 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4871}
4872
4873/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004874 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004875 * @chip: nand chip info structure
4876 * @addr: feature address.
4877 * @subfeature_param: the subfeature parameters, a four bytes array.
4878 *
4879 * Should be used by NAND controller drivers that do not support the SET/GET
4880 * FEATURES operations.
4881 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004882int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
4883 u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004884{
4885 return -ENOTSUPP;
4886}
Miquel Raynalb9587582018-03-19 14:47:19 +01004887EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004888
4889/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004890 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004891 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004892 */
4893static int nand_suspend(struct mtd_info *mtd)
4894{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004895 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004896}
4897
4898/**
4899 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004900 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004901 */
4902static void nand_resume(struct mtd_info *mtd)
4903{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004904 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004905
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004906 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004907 nand_release_device(mtd);
4908 else
Brian Norrisd0370212011-07-19 10:06:08 -07004909 pr_err("%s called for a chip which is not in suspended state\n",
4910 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004911}
4912
Scott Branden72ea4032014-11-20 11:18:05 -08004913/**
4914 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4915 * prevent further operations
4916 * @mtd: MTD device structure
4917 */
4918static void nand_shutdown(struct mtd_info *mtd)
4919{
Brian Norris9ca641b2015-11-09 16:37:28 -08004920 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004921}
4922
Brian Norris8b6e50c2011-05-25 14:59:01 -07004923/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004924static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004925{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004926 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 /* check for proper chip_delay setup, set 20us if not */
Boris Brezillon3cece3a2018-09-07 00:38:41 +02004929 if (!chip->legacy.chip_delay)
4930 chip->legacy.chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932 /* check, if a user supplied command function given */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02004933 if (!chip->legacy.cmdfunc && !chip->exec_op)
4934 chip->legacy.cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936 /* check, if a user supplied wait function given */
Boris Brezillon8395b752018-09-07 00:38:37 +02004937 if (chip->legacy.waitfunc == NULL)
4938 chip->legacy.waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004940 if (!chip->select_chip)
4941 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004942
4943 /* If called twice, pointers that depend on busw may need to be reset */
Boris Brezillon716bbba2018-09-07 00:38:35 +02004944 if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
4945 chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
Boris Brezillon716bbba2018-09-07 00:38:35 +02004946 if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
4947 chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
4948 if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
4949 chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
4950 if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
4951 chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004952
4953 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004954 chip->controller = &chip->dummy_controller;
4955 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004956 }
4957
Masahiro Yamada477544c2017-03-30 17:15:05 +09004958 if (!chip->buf_align)
4959 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004960}
4961
Brian Norris8b6e50c2011-05-25 14:59:01 -07004962/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004963static void sanitize_string(uint8_t *s, size_t len)
4964{
4965 ssize_t i;
4966
Brian Norris8b6e50c2011-05-25 14:59:01 -07004967 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004968 s[len - 1] = 0;
4969
Brian Norris8b6e50c2011-05-25 14:59:01 -07004970 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004971 for (i = 0; i < len - 1; i++) {
4972 if (s[i] < ' ' || s[i] > 127)
4973 s[i] = '?';
4974 }
4975
Brian Norris8b6e50c2011-05-25 14:59:01 -07004976 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004977 strim(s);
4978}
4979
4980static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4981{
4982 int i;
4983 while (len--) {
4984 crc ^= *p++ << 8;
4985 for (i = 0; i < 8; i++)
4986 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4987 }
4988
4989 return crc;
4990}
4991
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004992/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004993static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4994 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004995{
4996 struct onfi_ext_param_page *ep;
4997 struct onfi_ext_section *s;
4998 struct onfi_ext_ecc_info *ecc;
4999 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005000 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005001 int len;
5002 int i;
5003
5004 len = le16_to_cpu(p->ext_param_page_length) * 16;
5005 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005006 if (!ep)
5007 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005008
5009 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005010 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5011 if (ret)
5012 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005013
5014 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005015 ret = nand_change_read_column_op(chip,
5016 sizeof(*p) * p->num_of_param_pages,
5017 ep, len, true);
5018 if (ret)
5019 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005020
Boris Brezillon97d90da2017-11-30 18:01:29 +01005021 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005022 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5023 != le16_to_cpu(ep->crc))) {
5024 pr_debug("fail in the CRC.\n");
5025 goto ext_out;
5026 }
5027
5028 /*
5029 * Check the signature.
5030 * Do not strictly follow the ONFI spec, maybe changed in future.
5031 */
5032 if (strncmp(ep->sig, "EPPS", 4)) {
5033 pr_debug("The signature is invalid.\n");
5034 goto ext_out;
5035 }
5036
5037 /* find the ECC section. */
5038 cursor = (uint8_t *)(ep + 1);
5039 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5040 s = ep->sections + i;
5041 if (s->type == ONFI_SECTION_TYPE_2)
5042 break;
5043 cursor += s->length * 16;
5044 }
5045 if (i == ONFI_EXT_SECTION_MAX) {
5046 pr_debug("We can not find the ECC section.\n");
5047 goto ext_out;
5048 }
5049
5050 /* get the info we want. */
5051 ecc = (struct onfi_ext_ecc_info *)cursor;
5052
Brian Norris4ae7d222013-09-16 18:20:21 -07005053 if (!ecc->codeword_size) {
5054 pr_debug("Invalid codeword size\n");
5055 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005056 }
5057
Brian Norris4ae7d222013-09-16 18:20:21 -07005058 chip->ecc_strength_ds = ecc->ecc_bits;
5059 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005060 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005061
5062ext_out:
5063 kfree(ep);
5064 return ret;
5065}
5066
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005067/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005068 * Recover data with bit-wise majority
5069 */
5070static void nand_bit_wise_majority(const void **srcbufs,
5071 unsigned int nsrcbufs,
5072 void *dstbuf,
5073 unsigned int bufsize)
5074{
5075 int i, j, k;
5076
5077 for (i = 0; i < bufsize; i++) {
5078 u8 val = 0;
5079
5080 for (j = 0; j < 8; j++) {
5081 unsigned int cnt = 0;
5082
5083 for (k = 0; k < nsrcbufs; k++) {
5084 const u8 *srcbuf = srcbufs[k];
5085
5086 if (srcbuf[i] & BIT(j))
5087 cnt++;
5088 }
5089
5090 if (cnt > nsrcbufs / 2)
5091 val |= BIT(j);
5092 }
5093
5094 ((u8 *)dstbuf)[i] = val;
5095 }
5096}
5097
5098/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005099 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005100 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005101static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005102{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005103 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005104 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005105 struct onfi_params *onfi;
5106 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005107 char id[4];
5108 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005109
Brian Norris7854d3f2011-06-23 14:12:08 -07005110 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005111 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5112 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005113 return 0;
5114
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005115 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005116 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005117 if (!p)
5118 return -ENOMEM;
5119
Boris Brezillon97d90da2017-11-30 18:01:29 +01005120 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005121 if (ret) {
5122 ret = 0;
5123 goto free_onfi_param_page;
5124 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005125
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005126 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005127 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005128 if (ret) {
5129 ret = 0;
5130 goto free_onfi_param_page;
5131 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005132
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005133 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005134 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005135 if (i)
5136 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005137 break;
5138 }
5139 }
5140
Brian Norrisc7f23a72013-08-13 10:51:55 -07005141 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005142 const void *srcbufs[3] = {p, p + 1, p + 2};
5143
5144 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5145 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5146 sizeof(*p));
5147
5148 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5149 le16_to_cpu(p->crc)) {
5150 pr_err("ONFI parameter recovery failed, aborting\n");
5151 goto free_onfi_param_page;
5152 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005153 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005154
Chris Packham00ce4e02018-06-25 10:44:44 +12005155 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5156 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5157 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5158
Brian Norris8b6e50c2011-05-25 14:59:01 -07005159 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005160 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005161 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005162 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005163 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005164 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005165 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005166 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005167 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005168 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005169 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005170 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005171
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005172 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005173 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005174 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005175 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005176
5177 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5178 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005179 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5180 if (!chip->parameters.model) {
5181 ret = -ENOMEM;
5182 goto free_onfi_param_page;
5183 }
Brian Norris4355b702013-08-27 18:45:10 -07005184
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005185 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005186
5187 /*
5188 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5189 * (don't ask me who thought of this...). MTD assumes that these
5190 * dimensions will be power-of-2, so just truncate the remaining area.
5191 */
5192 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5193 mtd->erasesize *= mtd->writesize;
5194
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005195 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005196
5197 /* See erasesize comment */
5198 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005199 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005200 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005201
Zach Brown34da5f52017-01-10 13:30:21 -06005202 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5203 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5204
Miquel Raynala97421c2018-03-19 14:47:27 +01005205 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005206 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005207
Huang Shijie10c86ba2013-05-17 11:17:26 +08005208 if (p->ecc_bits != 0xff) {
5209 chip->ecc_strength_ds = p->ecc_bits;
5210 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005211 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005212 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005213
5214 /*
5215 * The nand_flash_detect_ext_param_page() uses the
5216 * Change Read Column command which maybe not supported
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005217 * by the chip->legacy.cmdfunc. So try to update the
5218 * chip->legacy.cmdfunc now. We do not replace user supplied
5219 * command function.
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005220 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005221 if (mtd->writesize > 512 &&
5222 chip->legacy.cmdfunc == nand_command)
5223 chip->legacy.cmdfunc = nand_command_lp;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005224
5225 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005226 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005227 pr_warn("Failed to detect ONFI extended param page\n");
5228 } else {
5229 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005230 }
5231
Miquel Raynalf4531b22018-03-19 14:47:26 +01005232 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005233 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005234 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005235 bitmap_set(chip->parameters.get_feature_list,
5236 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5237 bitmap_set(chip->parameters.set_feature_list,
5238 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5239 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005240
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005241 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5242 if (!onfi) {
5243 ret = -ENOMEM;
5244 goto free_model;
5245 }
5246
5247 onfi->version = onfi_version;
5248 onfi->tPROG = le16_to_cpu(p->t_prog);
5249 onfi->tBERS = le16_to_cpu(p->t_bers);
5250 onfi->tR = le16_to_cpu(p->t_r);
5251 onfi->tCCS = le16_to_cpu(p->t_ccs);
5252 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5253 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5254 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5255 chip->parameters.onfi = onfi;
5256
5257 /* Identification done, free the full ONFI parameter page and exit */
5258 kfree(p);
5259
5260 return 1;
5261
5262free_model:
5263 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005264free_onfi_param_page:
5265 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005266
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005267 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005268}
5269
5270/*
Huang Shijie91361812014-02-21 13:39:40 +08005271 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5272 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005273static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005274{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005275 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005276 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005277 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005278 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005279 char id[5];
5280 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005281
5282 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005283 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5284 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005285 return 0;
5286
Miquel Raynal480139d2018-03-19 14:47:30 +01005287 /* JEDEC chip: allocate a buffer to hold its parameter page */
5288 p = kzalloc(sizeof(*p), GFP_KERNEL);
5289 if (!p)
5290 return -ENOMEM;
5291
Boris Brezillon97d90da2017-11-30 18:01:29 +01005292 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005293 if (ret) {
5294 ret = 0;
5295 goto free_jedec_param_page;
5296 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005297
Huang Shijie91361812014-02-21 13:39:40 +08005298 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005299 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005300 if (ret) {
5301 ret = 0;
5302 goto free_jedec_param_page;
5303 }
Huang Shijie91361812014-02-21 13:39:40 +08005304
5305 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5306 le16_to_cpu(p->crc))
5307 break;
5308 }
5309
5310 if (i == 3) {
5311 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005312 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005313 }
5314
5315 /* Check version */
5316 val = le16_to_cpu(p->revision);
5317 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005318 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005319 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005320 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005321
Miquel Raynal480139d2018-03-19 14:47:30 +01005322 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005323 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005324 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005325 }
5326
5327 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5328 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005329 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5330 if (!chip->parameters.model) {
5331 ret = -ENOMEM;
5332 goto free_jedec_param_page;
5333 }
Huang Shijie91361812014-02-21 13:39:40 +08005334
5335 mtd->writesize = le32_to_cpu(p->byte_per_page);
5336
5337 /* Please reference to the comment for nand_flash_detect_onfi. */
5338 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5339 mtd->erasesize *= mtd->writesize;
5340
5341 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5342
5343 /* Please reference to the comment for nand_flash_detect_onfi. */
5344 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5345 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5346 chip->bits_per_cell = p->bits_per_cell;
5347
Miquel Raynal480139d2018-03-19 14:47:30 +01005348 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005349 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005350
5351 /* ECC info */
5352 ecc = &p->ecc_info[0];
5353
5354 if (ecc->codeword_size >= 9) {
5355 chip->ecc_strength_ds = ecc->ecc_bits;
5356 chip->ecc_step_ds = 1 << ecc->codeword_size;
5357 } else {
5358 pr_warn("Invalid codeword size\n");
5359 }
5360
Miquel Raynal480139d2018-03-19 14:47:30 +01005361free_jedec_param_page:
5362 kfree(p);
5363 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005364}
5365
5366/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005367 * nand_id_has_period - Check if an ID string has a given wraparound period
5368 * @id_data: the ID string
5369 * @arrlen: the length of the @id_data array
5370 * @period: the period of repitition
5371 *
5372 * Check if an ID string is repeated within a given sequence of bytes at
5373 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005374 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005375 * if the repetition has a period of @period; otherwise, returns zero.
5376 */
5377static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5378{
5379 int i, j;
5380 for (i = 0; i < period; i++)
5381 for (j = i + period; j < arrlen; j += period)
5382 if (id_data[i] != id_data[j])
5383 return 0;
5384 return 1;
5385}
5386
5387/*
5388 * nand_id_len - Get the length of an ID string returned by CMD_READID
5389 * @id_data: the ID string
5390 * @arrlen: the length of the @id_data array
5391
5392 * Returns the length of the ID string, according to known wraparound/trailing
5393 * zero patterns. If no pattern exists, returns the length of the array.
5394 */
5395static int nand_id_len(u8 *id_data, int arrlen)
5396{
5397 int last_nonzero, period;
5398
5399 /* Find last non-zero byte */
5400 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5401 if (id_data[last_nonzero])
5402 break;
5403
5404 /* All zeros */
5405 if (last_nonzero < 0)
5406 return 0;
5407
5408 /* Calculate wraparound period */
5409 for (period = 1; period < arrlen; period++)
5410 if (nand_id_has_period(id_data, arrlen, period))
5411 break;
5412
5413 /* There's a repeated pattern */
5414 if (period < arrlen)
5415 return period;
5416
5417 /* There are trailing zeros */
5418 if (last_nonzero < arrlen - 1)
5419 return last_nonzero + 1;
5420
5421 /* No pattern detected */
5422 return arrlen;
5423}
5424
Huang Shijie7db906b2013-09-25 14:58:11 +08005425/* Extract the bits of per cell from the 3rd byte of the extended ID */
5426static int nand_get_bits_per_cell(u8 cellinfo)
5427{
5428 int bits;
5429
5430 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5431 bits >>= NAND_CI_CELLTYPE_SHIFT;
5432 return bits + 1;
5433}
5434
Brian Norrise3b88bd2012-09-24 20:40:52 -07005435/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005436 * Many new NAND share similar device ID codes, which represent the size of the
5437 * chip. The rest of the parameters must be decoded according to generic or
5438 * manufacturer-specific "extended ID" decoding patterns.
5439 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005440void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005441{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005442 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005443 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005444 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005445 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005446 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005447 /* The 4th id byte is the important one */
5448 extid = id_data[3];
5449
Boris Brezillon01389b62016-06-08 10:30:18 +02005450 /* Calc pagesize */
5451 mtd->writesize = 1024 << (extid & 0x03);
5452 extid >>= 2;
5453 /* Calc oobsize */
5454 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5455 extid >>= 2;
5456 /* Calc blocksize. Blocksize is multiples of 64KiB */
5457 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5458 extid >>= 2;
5459 /* Get buswidth information */
5460 if (extid & 0x1)
5461 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005462}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005463EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005464
5465/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005466 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5467 * decodes a matching ID table entry and assigns the MTD size parameters for
5468 * the chip.
5469 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005470static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005471{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005472 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005473
5474 mtd->erasesize = type->erasesize;
5475 mtd->writesize = type->pagesize;
5476 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005477
Huang Shijie1c195e92013-09-25 14:58:12 +08005478 /* All legacy ID NAND are small-page, SLC */
5479 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005480}
5481
5482/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005483 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5484 * heuristic patterns using various detected parameters (e.g., manufacturer,
5485 * page size, cell-type information).
5486 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005487static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005488{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005489 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005490
5491 /* Set the bad block position */
5492 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5493 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5494 else
5495 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005496}
5497
Huang Shijieec6e87e2013-03-15 11:01:00 +08005498static inline bool is_full_id_nand(struct nand_flash_dev *type)
5499{
5500 return type->id_len;
5501}
5502
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005503static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005504 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005505{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005506 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005507 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005508
Huang Shijieec6e87e2013-03-15 11:01:00 +08005509 if (!strncmp(type->id, id_data, type->id_len)) {
5510 mtd->writesize = type->pagesize;
5511 mtd->erasesize = type->erasesize;
5512 mtd->oobsize = type->oobsize;
5513
Huang Shijie7db906b2013-09-25 14:58:11 +08005514 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005515 chip->chipsize = (uint64_t)type->chipsize << 20;
5516 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005517 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5518 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005519 chip->onfi_timing_mode_default =
5520 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005521
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005522 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5523 if (!chip->parameters.model)
5524 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005525
Huang Shijieec6e87e2013-03-15 11:01:00 +08005526 return true;
5527 }
5528 return false;
5529}
5530
Brian Norris7e74c2d2012-09-24 20:40:49 -07005531/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005532 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5533 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5534 * table.
5535 */
5536static void nand_manufacturer_detect(struct nand_chip *chip)
5537{
5538 /*
5539 * Try manufacturer detection if available and use
5540 * nand_decode_ext_id() otherwise.
5541 */
5542 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005543 chip->manufacturer.desc->ops->detect) {
5544 /* The 3rd id byte holds MLC / multichip data */
5545 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005546 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005547 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005548 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005549 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005550}
5551
5552/*
5553 * Manufacturer initialization. This function is called for all NANDs including
5554 * ONFI and JEDEC compliant ones.
5555 * Manufacturer drivers should put all their specific initialization code in
5556 * their ->init() hook.
5557 */
5558static int nand_manufacturer_init(struct nand_chip *chip)
5559{
5560 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5561 !chip->manufacturer.desc->ops->init)
5562 return 0;
5563
5564 return chip->manufacturer.desc->ops->init(chip);
5565}
5566
5567/*
5568 * Manufacturer cleanup. This function is called for all NANDs including
5569 * ONFI and JEDEC compliant ones.
5570 * Manufacturer drivers should put all their specific cleanup code in their
5571 * ->cleanup() hook.
5572 */
5573static void nand_manufacturer_cleanup(struct nand_chip *chip)
5574{
5575 /* Release manufacturer private data */
5576 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5577 chip->manufacturer.desc->ops->cleanup)
5578 chip->manufacturer.desc->ops->cleanup(chip);
5579}
5580
5581/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005582 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005583 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005584static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005585{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005586 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005587 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005588 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005589 u8 *id_data = chip->id.data;
5590 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Karl Beldanef89a882008-09-15 14:37:29 +02005592 /*
5593 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005594 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005595 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005596 ret = nand_reset(chip, 0);
5597 if (ret)
5598 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005599
5600 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005601 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005604 ret = nand_readid_op(chip, 0, id_data, 2);
5605 if (ret)
5606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
5608 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005609 maf_id = id_data[0];
5610 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Brian Norris8b6e50c2011-05-25 14:59:01 -07005612 /*
5613 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005614 * interface concerns can cause random data which looks like a
5615 * possibly credible NAND flash to appear. If the two results do
5616 * not match, ignore the device completely.
5617 */
5618
Brian Norris4aef9b72012-09-24 20:40:48 -07005619 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005620 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5621 if (ret)
5622 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005623
Boris Brezillon7f501f02016-05-24 19:20:05 +02005624 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005625 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005626 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005627 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005628 }
5629
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005630 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005631
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005632 /* Try to identify manufacturer */
5633 manufacturer = nand_get_manufacturer(maf_id);
5634 chip->manufacturer.desc = manufacturer;
5635
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005636 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005637 type = nand_flash_ids;
5638
Boris Brezillon29a198a2016-05-24 20:17:48 +02005639 /*
5640 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5641 * override it.
5642 * This is required to make sure initial NAND bus width set by the
5643 * NAND controller driver is coherent with the real NAND bus width
5644 * (extracted by auto-detection code).
5645 */
5646 busw = chip->options & NAND_BUSWIDTH_16;
5647
5648 /*
5649 * The flag is only set (never cleared), reset it to its default value
5650 * before starting auto-detection.
5651 */
5652 chip->options &= ~NAND_BUSWIDTH_16;
5653
Huang Shijieec6e87e2013-03-15 11:01:00 +08005654 for (; type->name != NULL; type++) {
5655 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005656 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005657 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005658 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005659 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005660 }
5661 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005662
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005663 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005664 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005665 ret = nand_flash_detect_onfi(chip);
5666 if (ret < 0)
5667 return ret;
5668 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005669 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005670
5671 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005672 ret = nand_flash_detect_jedec(chip);
5673 if (ret < 0)
5674 return ret;
5675 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005676 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005677 }
5678
David Woodhouse5e81e882010-02-26 18:32:56 +00005679 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005680 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005681
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005682 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5683 if (!chip->parameters.model)
5684 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005685
Adrian Hunter69423d92008-12-10 13:37:21 +00005686 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005687
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005688 if (!type->pagesize)
5689 nand_manufacturer_detect(chip);
5690 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005691 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005692
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005693 /* Get chip options */
5694 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005695
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005696ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005697 if (!mtd->name)
5698 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005699
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005700 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005701 WARN_ON(busw & NAND_BUSWIDTH_16);
5702 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005703 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5704 /*
5705 * Check, if buswidth is correct. Hardware drivers should set
5706 * chip correct!
5707 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005708 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005709 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005710 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5711 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005712 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5713 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005714 ret = -EINVAL;
5715
5716 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005717 }
5718
Boris Brezillon7f501f02016-05-24 19:20:05 +02005719 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005720
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005721 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005722 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005723 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005724 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005725
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005726 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005727 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005728 if (chip->chipsize & 0xffffffff)
5729 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005730 else {
5731 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5732 chip->chip_shift += 32 - 1;
5733 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005734
Masahiro Yamada14157f82017-09-13 11:05:50 +09005735 if (chip->chip_shift - chip->page_shift > 16)
5736 chip->options |= NAND_ROW_ADDR_3;
5737
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005738 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005739
Brian Norris8b6e50c2011-05-25 14:59:01 -07005740 /* Do not replace user supplied command function! */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005741 if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
5742 chip->legacy.cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005743
Ezequiel Garcia20171642013-11-25 08:30:31 -03005744 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005745 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005746 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5747 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005748 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005749 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005750 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005751 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005752
5753free_detect_allocation:
5754 kfree(chip->parameters.model);
5755
5756 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005757}
5758
Boris Brezillond48f62b2016-04-01 14:54:32 +02005759static const char * const nand_ecc_modes[] = {
5760 [NAND_ECC_NONE] = "none",
5761 [NAND_ECC_SOFT] = "soft",
5762 [NAND_ECC_HW] = "hw",
5763 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5764 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005765 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005766};
5767
5768static int of_get_nand_ecc_mode(struct device_node *np)
5769{
5770 const char *pm;
5771 int err, i;
5772
5773 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5774 if (err < 0)
5775 return err;
5776
5777 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5778 if (!strcasecmp(pm, nand_ecc_modes[i]))
5779 return i;
5780
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005781 /*
5782 * For backward compatibility we support few obsoleted values that don't
5783 * have their mappings into nand_ecc_modes_t anymore (they were merged
5784 * with other enums).
5785 */
5786 if (!strcasecmp(pm, "soft_bch"))
5787 return NAND_ECC_SOFT;
5788
Boris Brezillond48f62b2016-04-01 14:54:32 +02005789 return -ENODEV;
5790}
5791
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005792static const char * const nand_ecc_algos[] = {
5793 [NAND_ECC_HAMMING] = "hamming",
5794 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005795 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005796};
5797
Boris Brezillond48f62b2016-04-01 14:54:32 +02005798static int of_get_nand_ecc_algo(struct device_node *np)
5799{
5800 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005801 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005802
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005803 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5804 if (!err) {
5805 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5806 if (!strcasecmp(pm, nand_ecc_algos[i]))
5807 return i;
5808 return -ENODEV;
5809 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005810
5811 /*
5812 * For backward compatibility we also read "nand-ecc-mode" checking
5813 * for some obsoleted values that were specifying ECC algorithm.
5814 */
5815 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5816 if (err < 0)
5817 return err;
5818
5819 if (!strcasecmp(pm, "soft"))
5820 return NAND_ECC_HAMMING;
5821 else if (!strcasecmp(pm, "soft_bch"))
5822 return NAND_ECC_BCH;
5823
5824 return -ENODEV;
5825}
5826
5827static int of_get_nand_ecc_step_size(struct device_node *np)
5828{
5829 int ret;
5830 u32 val;
5831
5832 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5833 return ret ? ret : val;
5834}
5835
5836static int of_get_nand_ecc_strength(struct device_node *np)
5837{
5838 int ret;
5839 u32 val;
5840
5841 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5842 return ret ? ret : val;
5843}
5844
5845static int of_get_nand_bus_width(struct device_node *np)
5846{
5847 u32 val;
5848
5849 if (of_property_read_u32(np, "nand-bus-width", &val))
5850 return 8;
5851
5852 switch (val) {
5853 case 8:
5854 case 16:
5855 return val;
5856 default:
5857 return -EIO;
5858 }
5859}
5860
5861static bool of_get_nand_on_flash_bbt(struct device_node *np)
5862{
5863 return of_property_read_bool(np, "nand-on-flash-bbt");
5864}
5865
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005866static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005867{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005868 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005869 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005870
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005871 if (!dn)
5872 return 0;
5873
Brian Norris5844fee2015-01-23 00:22:27 -08005874 if (of_get_nand_bus_width(dn) == 16)
5875 chip->options |= NAND_BUSWIDTH_16;
5876
Stefan Agnerf922bd72018-06-24 23:27:23 +02005877 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5878 chip->options |= NAND_IS_BOOT_MEDIUM;
5879
Brian Norris5844fee2015-01-23 00:22:27 -08005880 if (of_get_nand_on_flash_bbt(dn))
5881 chip->bbt_options |= NAND_BBT_USE_FLASH;
5882
5883 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005884 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005885 ecc_strength = of_get_nand_ecc_strength(dn);
5886 ecc_step = of_get_nand_ecc_step_size(dn);
5887
Brian Norris5844fee2015-01-23 00:22:27 -08005888 if (ecc_mode >= 0)
5889 chip->ecc.mode = ecc_mode;
5890
Rafał Miłecki79082452016-03-23 11:19:02 +01005891 if (ecc_algo >= 0)
5892 chip->ecc.algo = ecc_algo;
5893
Brian Norris5844fee2015-01-23 00:22:27 -08005894 if (ecc_strength >= 0)
5895 chip->ecc.strength = ecc_strength;
5896
5897 if (ecc_step > 0)
5898 chip->ecc.size = ecc_step;
5899
Boris Brezillonba78ee02016-06-08 17:04:22 +02005900 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5901 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5902
Brian Norris5844fee2015-01-23 00:22:27 -08005903 return 0;
5904}
5905
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005906/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005907 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005908 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005909 * @maxchips: number of chips to scan for
5910 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005911 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005912 * This is the first phase of the normal nand_scan() function. It reads the
5913 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005914 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005915 * This helper used to be called directly from controller drivers that needed
5916 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5917 * prevented dynamic allocations during this phase which was unconvenient and
5918 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005919 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005920static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005921 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005922{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005923 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005924 int nand_maf_id, nand_dev_id;
5925 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005926 int ret;
5927
Miquel Raynal17fa8042017-11-30 18:01:31 +01005928 /* Enforce the right timings for reset/detection */
5929 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5930
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005931 ret = nand_dt_init(chip);
5932 if (ret)
5933 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005934
Brian Norrisf7a8e382016-01-05 10:39:45 -08005935 if (!mtd->name && mtd->dev.parent)
5936 mtd->name = dev_name(mtd->dev.parent);
5937
Miquel Raynal8878b122017-11-09 14:16:45 +01005938 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005939 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
5940 * not populated.
Miquel Raynal8878b122017-11-09 14:16:45 +01005941 */
5942 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005943 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005944 * Default functions assigned for ->legacy.cmdfunc() and
5945 * ->select_chip() both expect ->legacy.cmd_ctrl() to be
5946 * populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005947 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005948 if ((!chip->legacy.cmdfunc || !chip->select_chip) &&
5949 !chip->legacy.cmd_ctrl) {
5950 pr_err("->legacy.cmd_ctrl() should be provided\n");
Miquel Raynal8878b122017-11-09 14:16:45 +01005951 return -EINVAL;
5952 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005953 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005954
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005955 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005956 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005957
5958 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005959 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005960 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005961 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005962 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005963 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005964 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 }
5966
Boris Brezillon7f501f02016-05-24 19:20:05 +02005967 nand_maf_id = chip->id.data[0];
5968 nand_dev_id = chip->id.data[1];
5969
Boris Brezillon758b56f2018-09-06 14:05:24 +02005970 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005971
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005972 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005973 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005974 u8 id[2];
5975
Karl Beldanef89a882008-09-15 14:37:29 +02005976 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005977 nand_reset(chip, i);
5978
Boris Brezillon758b56f2018-09-06 14:05:24 +02005979 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005981 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005983 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005984 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 break;
Huang Shijie07300162012-11-09 16:23:45 +08005986 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005987 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 }
5989 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005990 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005993 chip->numchips = i;
5994 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
David Woodhouse3b85c322006-09-25 17:06:53 +01005996 return 0;
5997}
5998
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005999static void nand_scan_ident_cleanup(struct nand_chip *chip)
6000{
6001 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006002 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006003}
6004
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006005static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6006{
6007 struct nand_chip *chip = mtd_to_nand(mtd);
6008 struct nand_ecc_ctrl *ecc = &chip->ecc;
6009
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006010 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006011 return -EINVAL;
6012
6013 switch (ecc->algo) {
6014 case NAND_ECC_HAMMING:
6015 ecc->calculate = nand_calculate_ecc;
6016 ecc->correct = nand_correct_data;
6017 ecc->read_page = nand_read_page_swecc;
6018 ecc->read_subpage = nand_read_subpage;
6019 ecc->write_page = nand_write_page_swecc;
6020 ecc->read_page_raw = nand_read_page_raw;
6021 ecc->write_page_raw = nand_write_page_raw;
6022 ecc->read_oob = nand_read_oob_std;
6023 ecc->write_oob = nand_write_oob_std;
6024 if (!ecc->size)
6025 ecc->size = 256;
6026 ecc->bytes = 3;
6027 ecc->strength = 1;
6028 return 0;
6029 case NAND_ECC_BCH:
6030 if (!mtd_nand_has_bch()) {
6031 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6032 return -EINVAL;
6033 }
6034 ecc->calculate = nand_bch_calculate_ecc;
6035 ecc->correct = nand_bch_correct_data;
6036 ecc->read_page = nand_read_page_swecc;
6037 ecc->read_subpage = nand_read_subpage;
6038 ecc->write_page = nand_write_page_swecc;
6039 ecc->read_page_raw = nand_read_page_raw;
6040 ecc->write_page_raw = nand_write_page_raw;
6041 ecc->read_oob = nand_read_oob_std;
6042 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006043
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006044 /*
6045 * Board driver should supply ecc.size and ecc.strength
6046 * values to select how many bits are correctable.
6047 * Otherwise, default to 4 bits for large page devices.
6048 */
6049 if (!ecc->size && (mtd->oobsize >= 64)) {
6050 ecc->size = 512;
6051 ecc->strength = 4;
6052 }
6053
6054 /*
6055 * if no ecc placement scheme was provided pickup the default
6056 * large page one.
6057 */
6058 if (!mtd->ooblayout) {
6059 /* handle large page devices only */
6060 if (mtd->oobsize < 64) {
6061 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6062 return -EINVAL;
6063 }
6064
6065 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006066
6067 }
6068
6069 /*
6070 * We can only maximize ECC config when the default layout is
6071 * used, otherwise we don't know how many bytes can really be
6072 * used.
6073 */
6074 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6075 ecc->options & NAND_ECC_MAXIMIZE) {
6076 int steps, bytes;
6077
6078 /* Always prefer 1k blocks over 512bytes ones */
6079 ecc->size = 1024;
6080 steps = mtd->writesize / ecc->size;
6081
6082 /* Reserve 2 bytes for the BBM */
6083 bytes = (mtd->oobsize - 2) / steps;
6084 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006085 }
6086
6087 /* See nand_bch_init() for details. */
6088 ecc->bytes = 0;
6089 ecc->priv = nand_bch_init(mtd);
6090 if (!ecc->priv) {
6091 WARN(1, "BCH ECC initialization failed!\n");
6092 return -EINVAL;
6093 }
6094 return 0;
6095 default:
6096 WARN(1, "Unsupported ECC algorithm!\n");
6097 return -EINVAL;
6098 }
6099}
6100
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006101/**
6102 * nand_check_ecc_caps - check the sanity of preset ECC settings
6103 * @chip: nand chip info structure
6104 * @caps: ECC caps info structure
6105 * @oobavail: OOB size that the ECC engine can use
6106 *
6107 * When ECC step size and strength are already set, check if they are supported
6108 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6109 * On success, the calculated ECC bytes is set.
6110 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306111static int
6112nand_check_ecc_caps(struct nand_chip *chip,
6113 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006114{
6115 struct mtd_info *mtd = nand_to_mtd(chip);
6116 const struct nand_ecc_step_info *stepinfo;
6117 int preset_step = chip->ecc.size;
6118 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306119 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006120 int i, j;
6121
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006122 for (i = 0; i < caps->nstepinfos; i++) {
6123 stepinfo = &caps->stepinfos[i];
6124
6125 if (stepinfo->stepsize != preset_step)
6126 continue;
6127
6128 for (j = 0; j < stepinfo->nstrengths; j++) {
6129 if (stepinfo->strengths[j] != preset_strength)
6130 continue;
6131
6132 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6133 preset_strength);
6134 if (WARN_ON_ONCE(ecc_bytes < 0))
6135 return ecc_bytes;
6136
6137 if (ecc_bytes * nsteps > oobavail) {
6138 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6139 preset_step, preset_strength);
6140 return -ENOSPC;
6141 }
6142
6143 chip->ecc.bytes = ecc_bytes;
6144
6145 return 0;
6146 }
6147 }
6148
6149 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6150 preset_step, preset_strength);
6151
6152 return -ENOTSUPP;
6153}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006154
6155/**
6156 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6157 * @chip: nand chip info structure
6158 * @caps: ECC engine caps info structure
6159 * @oobavail: OOB size that the ECC engine can use
6160 *
6161 * If a chip's ECC requirement is provided, try to meet it with the least
6162 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6163 * On success, the chosen ECC settings are set.
6164 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306165static int
6166nand_match_ecc_req(struct nand_chip *chip,
6167 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006168{
6169 struct mtd_info *mtd = nand_to_mtd(chip);
6170 const struct nand_ecc_step_info *stepinfo;
6171 int req_step = chip->ecc_step_ds;
6172 int req_strength = chip->ecc_strength_ds;
6173 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6174 int best_step, best_strength, best_ecc_bytes;
6175 int best_ecc_bytes_total = INT_MAX;
6176 int i, j;
6177
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006178 /* No information provided by the NAND chip */
6179 if (!req_step || !req_strength)
6180 return -ENOTSUPP;
6181
6182 /* number of correctable bits the chip requires in a page */
6183 req_corr = mtd->writesize / req_step * req_strength;
6184
6185 for (i = 0; i < caps->nstepinfos; i++) {
6186 stepinfo = &caps->stepinfos[i];
6187 step_size = stepinfo->stepsize;
6188
6189 for (j = 0; j < stepinfo->nstrengths; j++) {
6190 strength = stepinfo->strengths[j];
6191
6192 /*
6193 * If both step size and strength are smaller than the
6194 * chip's requirement, it is not easy to compare the
6195 * resulted reliability.
6196 */
6197 if (step_size < req_step && strength < req_strength)
6198 continue;
6199
6200 if (mtd->writesize % step_size)
6201 continue;
6202
6203 nsteps = mtd->writesize / step_size;
6204
6205 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6206 if (WARN_ON_ONCE(ecc_bytes < 0))
6207 continue;
6208 ecc_bytes_total = ecc_bytes * nsteps;
6209
6210 if (ecc_bytes_total > oobavail ||
6211 strength * nsteps < req_corr)
6212 continue;
6213
6214 /*
6215 * We assume the best is to meet the chip's requrement
6216 * with the least number of ECC bytes.
6217 */
6218 if (ecc_bytes_total < best_ecc_bytes_total) {
6219 best_ecc_bytes_total = ecc_bytes_total;
6220 best_step = step_size;
6221 best_strength = strength;
6222 best_ecc_bytes = ecc_bytes;
6223 }
6224 }
6225 }
6226
6227 if (best_ecc_bytes_total == INT_MAX)
6228 return -ENOTSUPP;
6229
6230 chip->ecc.size = best_step;
6231 chip->ecc.strength = best_strength;
6232 chip->ecc.bytes = best_ecc_bytes;
6233
6234 return 0;
6235}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006236
6237/**
6238 * nand_maximize_ecc - choose the max ECC strength available
6239 * @chip: nand chip info structure
6240 * @caps: ECC engine caps info structure
6241 * @oobavail: OOB size that the ECC engine can use
6242 *
6243 * Choose the max ECC strength that is supported on the controller, and can fit
6244 * within the chip's OOB. On success, the chosen ECC settings are set.
6245 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306246static int
6247nand_maximize_ecc(struct nand_chip *chip,
6248 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006249{
6250 struct mtd_info *mtd = nand_to_mtd(chip);
6251 const struct nand_ecc_step_info *stepinfo;
6252 int step_size, strength, nsteps, ecc_bytes, corr;
6253 int best_corr = 0;
6254 int best_step = 0;
6255 int best_strength, best_ecc_bytes;
6256 int i, j;
6257
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006258 for (i = 0; i < caps->nstepinfos; i++) {
6259 stepinfo = &caps->stepinfos[i];
6260 step_size = stepinfo->stepsize;
6261
6262 /* If chip->ecc.size is already set, respect it */
6263 if (chip->ecc.size && step_size != chip->ecc.size)
6264 continue;
6265
6266 for (j = 0; j < stepinfo->nstrengths; j++) {
6267 strength = stepinfo->strengths[j];
6268
6269 if (mtd->writesize % step_size)
6270 continue;
6271
6272 nsteps = mtd->writesize / step_size;
6273
6274 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6275 if (WARN_ON_ONCE(ecc_bytes < 0))
6276 continue;
6277
6278 if (ecc_bytes * nsteps > oobavail)
6279 continue;
6280
6281 corr = strength * nsteps;
6282
6283 /*
6284 * If the number of correctable bits is the same,
6285 * bigger step_size has more reliability.
6286 */
6287 if (corr > best_corr ||
6288 (corr == best_corr && step_size > best_step)) {
6289 best_corr = corr;
6290 best_step = step_size;
6291 best_strength = strength;
6292 best_ecc_bytes = ecc_bytes;
6293 }
6294 }
6295 }
6296
6297 if (!best_corr)
6298 return -ENOTSUPP;
6299
6300 chip->ecc.size = best_step;
6301 chip->ecc.strength = best_strength;
6302 chip->ecc.bytes = best_ecc_bytes;
6303
6304 return 0;
6305}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006306
Abhishek Sahu181ace92018-06-20 12:57:28 +05306307/**
6308 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6309 * @chip: nand chip info structure
6310 * @caps: ECC engine caps info structure
6311 * @oobavail: OOB size that the ECC engine can use
6312 *
6313 * Choose the ECC configuration according to following logic
6314 *
6315 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6316 * then check if it is supported by this controller.
6317 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6318 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6319 * to the chip's requirement. If available OOB size can't fit the chip
6320 * requirement then fallback to the maximum ECC step size and ECC strength.
6321 *
6322 * On success, the chosen ECC settings are set.
6323 */
6324int nand_ecc_choose_conf(struct nand_chip *chip,
6325 const struct nand_ecc_caps *caps, int oobavail)
6326{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306327 struct mtd_info *mtd = nand_to_mtd(chip);
6328
6329 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6330 return -EINVAL;
6331
Abhishek Sahu181ace92018-06-20 12:57:28 +05306332 if (chip->ecc.size && chip->ecc.strength)
6333 return nand_check_ecc_caps(chip, caps, oobavail);
6334
6335 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6336 return nand_maximize_ecc(chip, caps, oobavail);
6337
6338 if (!nand_match_ecc_req(chip, caps, oobavail))
6339 return 0;
6340
6341 return nand_maximize_ecc(chip, caps, oobavail);
6342}
6343EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6344
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006345/*
6346 * Check if the chip configuration meet the datasheet requirements.
6347
6348 * If our configuration corrects A bits per B bytes and the minimum
6349 * required correction level is X bits per Y bytes, then we must ensure
6350 * both of the following are true:
6351 *
6352 * (1) A / B >= X / Y
6353 * (2) A >= X
6354 *
6355 * Requirement (1) ensures we can correct for the required bitflip density.
6356 * Requirement (2) ensures we can correct even when all bitflips are clumped
6357 * in the same sector.
6358 */
6359static bool nand_ecc_strength_good(struct mtd_info *mtd)
6360{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006361 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006362 struct nand_ecc_ctrl *ecc = &chip->ecc;
6363 int corr, ds_corr;
6364
6365 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6366 /* Not enough information */
6367 return true;
6368
6369 /*
6370 * We get the number of corrected bits per page to compare
6371 * the correction density.
6372 */
6373 corr = (mtd->writesize * ecc->strength) / ecc->size;
6374 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6375
6376 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6377}
David Woodhouse3b85c322006-09-25 17:06:53 +01006378
6379/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006380 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006381 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006382 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006383 * This is the second phase of the normal nand_scan() function. It fills out
6384 * all the uninitialized function pointers with the defaults and scans for a
6385 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006386 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006387static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006388{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006389 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006390 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006391 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006392
Brian Norrise2414f42012-02-06 13:44:00 -08006393 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006394 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006395 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006396 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006397 }
Brian Norrise2414f42012-02-06 13:44:00 -08006398
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006399 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006400 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006401 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006402
Boris Brezillonf84674b2017-06-02 12:18:24 +02006403 /*
6404 * FIXME: some NAND manufacturer drivers expect the first die to be
6405 * selected when manufacturer->init() is called. They should be fixed
6406 * to explictly select the relevant die when interacting with the NAND
6407 * chip.
6408 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02006409 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006410 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02006411 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006412 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006413 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006414
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006415 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006416 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006417
6418 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006419 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006420 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006421 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006422 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006423 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006426 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 break;
6428 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006429 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006430 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006433 /*
6434 * Expose the whole OOB area to users if ECC_NONE
6435 * is passed. We could do that for all kind of
6436 * ->oobsize, but we must keep the old large/small
6437 * page with ECC layout when ->oobsize <= 128 for
6438 * compatibility reasons.
6439 */
6440 if (ecc->mode == NAND_ECC_NONE) {
6441 mtd_set_ooblayout(mtd,
6442 &nand_ooblayout_lp_ops);
6443 break;
6444 }
6445
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006446 WARN(1, "No oob scheme defined for oobsize %d\n",
6447 mtd->oobsize);
6448 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006449 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 }
6451 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006452
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006453 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006454 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006455 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006456 */
David Woodhouse956e9442006-09-25 17:12:39 +01006457
Huang Shijie97de79e02013-10-18 14:20:53 +08006458 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006459 case NAND_ECC_HW_OOB_FIRST:
6460 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006461 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006462 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6463 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006464 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006465 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006466 if (!ecc->read_page)
6467 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006468
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006469 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006470 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006471 if (!ecc->read_page)
6472 ecc->read_page = nand_read_page_hwecc;
6473 if (!ecc->write_page)
6474 ecc->write_page = nand_write_page_hwecc;
6475 if (!ecc->read_page_raw)
6476 ecc->read_page_raw = nand_read_page_raw;
6477 if (!ecc->write_page_raw)
6478 ecc->write_page_raw = nand_write_page_raw;
6479 if (!ecc->read_oob)
6480 ecc->read_oob = nand_read_oob_std;
6481 if (!ecc->write_oob)
6482 ecc->write_oob = nand_write_oob_std;
6483 if (!ecc->read_subpage)
6484 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006485 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006486 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006487
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006488 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006489 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6490 (!ecc->read_page ||
6491 ecc->read_page == nand_read_page_hwecc ||
6492 !ecc->write_page ||
6493 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006494 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6495 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006496 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006497 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006498 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006499 if (!ecc->read_page)
6500 ecc->read_page = nand_read_page_syndrome;
6501 if (!ecc->write_page)
6502 ecc->write_page = nand_write_page_syndrome;
6503 if (!ecc->read_page_raw)
6504 ecc->read_page_raw = nand_read_page_raw_syndrome;
6505 if (!ecc->write_page_raw)
6506 ecc->write_page_raw = nand_write_page_raw_syndrome;
6507 if (!ecc->read_oob)
6508 ecc->read_oob = nand_read_oob_syndrome;
6509 if (!ecc->write_oob)
6510 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006511
Huang Shijie97de79e02013-10-18 14:20:53 +08006512 if (mtd->writesize >= ecc->size) {
6513 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006514 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6515 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006516 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006517 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006518 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006519 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006520 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6521 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006522 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006523 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006525 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006526 ret = nand_set_ecc_soft_ops(mtd);
6527 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006528 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006529 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006530 }
6531 break;
6532
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006533 case NAND_ECC_ON_DIE:
6534 if (!ecc->read_page || !ecc->write_page) {
6535 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6536 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006537 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006538 }
6539 if (!ecc->read_oob)
6540 ecc->read_oob = nand_read_oob_std;
6541 if (!ecc->write_oob)
6542 ecc->write_oob = nand_write_oob_std;
6543 break;
6544
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006545 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006546 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006547 ecc->read_page = nand_read_page_raw;
6548 ecc->write_page = nand_write_page_raw;
6549 ecc->read_oob = nand_read_oob_std;
6550 ecc->read_page_raw = nand_read_page_raw;
6551 ecc->write_page_raw = nand_write_page_raw;
6552 ecc->write_oob = nand_write_oob_std;
6553 ecc->size = mtd->writesize;
6554 ecc->bytes = 0;
6555 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006557
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006559 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6560 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006561 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006564 if (ecc->correct || ecc->calculate) {
6565 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6566 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6567 if (!ecc->calc_buf || !ecc->code_buf) {
6568 ret = -ENOMEM;
6569 goto err_nand_manuf_cleanup;
6570 }
6571 }
6572
Brian Norris9ce244b2011-08-30 18:45:37 -07006573 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006574 if (!ecc->read_oob_raw)
6575 ecc->read_oob_raw = ecc->read_oob;
6576 if (!ecc->write_oob_raw)
6577 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006578
Boris Brezillon846031d2016-02-03 20:11:00 +01006579 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006580 mtd->ecc_strength = ecc->strength;
6581 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006582
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006583 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006584 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006585 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006586 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006587 ecc->steps = mtd->writesize / ecc->size;
6588 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006589 WARN(1, "Invalid ECC parameters\n");
6590 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006591 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006593 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006594 if (ecc->total > mtd->oobsize) {
6595 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6596 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006597 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006598 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006599
Boris Brezillon846031d2016-02-03 20:11:00 +01006600 /*
6601 * The number of bytes available for a client to place data into
6602 * the out of band area.
6603 */
6604 ret = mtd_ooblayout_count_freebytes(mtd);
6605 if (ret < 0)
6606 ret = 0;
6607
6608 mtd->oobavail = ret;
6609
6610 /* ECC sanity check: warn if it's too weak */
6611 if (!nand_ecc_strength_good(mtd))
6612 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6613 mtd->name);
6614
Brian Norris8b6e50c2011-05-25 14:59:01 -07006615 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006616 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006617 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006618 case 2:
6619 mtd->subpage_sft = 1;
6620 break;
6621 case 4:
6622 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006623 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006624 mtd->subpage_sft = 2;
6625 break;
6626 }
6627 }
6628 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6629
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006630 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006631 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006634 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006636 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306637 switch (ecc->mode) {
6638 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306639 if (chip->page_shift > 9)
6640 chip->options |= NAND_SUBPAGE_READ;
6641 break;
6642
6643 default:
6644 break;
6645 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006646
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006648 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006649 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6650 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006651 mtd->_erase = nand_erase;
6652 mtd->_point = NULL;
6653 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006654 mtd->_panic_write = panic_nand_write;
6655 mtd->_read_oob = nand_read_oob;
6656 mtd->_write_oob = nand_write_oob;
6657 mtd->_sync = nand_sync;
6658 mtd->_lock = NULL;
6659 mtd->_unlock = NULL;
6660 mtd->_suspend = nand_suspend;
6661 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006662 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006663 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006664 mtd->_block_isbad = nand_block_isbad;
6665 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006666 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006667 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006669 /*
6670 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6671 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6672 * properly set.
6673 */
6674 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006675 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
Boris Brezillonf84674b2017-06-02 12:18:24 +02006677 /* Initialize the ->data_interface field. */
6678 ret = nand_init_data_interface(chip);
6679 if (ret)
6680 goto err_nand_manuf_cleanup;
6681
6682 /* Enter fastest possible mode on all dies. */
6683 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006684 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006685 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006686 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006687 }
6688
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006689 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006690 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
6693 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006694 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006695 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006696 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006697
Brian Norris44d41822017-05-01 17:04:50 -07006698 return 0;
6699
Boris Brezillonf84674b2017-06-02 12:18:24 +02006700
6701err_nand_manuf_cleanup:
6702 nand_manufacturer_cleanup(chip);
6703
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006704err_free_buf:
6705 kfree(chip->data_buf);
6706 kfree(ecc->code_buf);
6707 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006708
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710}
6711
Miquel Raynal05b54c72018-07-19 01:05:46 +02006712static int nand_attach(struct nand_chip *chip)
6713{
6714 if (chip->controller->ops && chip->controller->ops->attach_chip)
6715 return chip->controller->ops->attach_chip(chip);
6716
6717 return 0;
6718}
6719
6720static void nand_detach(struct nand_chip *chip)
6721{
6722 if (chip->controller->ops && chip->controller->ops->detach_chip)
6723 chip->controller->ops->detach_chip(chip);
6724}
6725
David Woodhouse3b85c322006-09-25 17:06:53 +01006726/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006727 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006728 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02006729 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006730 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006731 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006732 * This fills out all the uninitialized function pointers with the defaults.
6733 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006734 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006735 */
Boris Brezillon871a4072018-08-04 22:59:22 +02006736int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006737 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006738{
6739 int ret;
6740
Boris Brezillon800342d2018-08-04 22:59:23 +02006741 if (!maxchips)
6742 return -EINVAL;
6743
6744 ret = nand_scan_ident(chip, maxchips, ids);
6745 if (ret)
6746 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006747
6748 ret = nand_attach(chip);
6749 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006750 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006751
Boris Brezillon00ad3782018-09-06 14:05:14 +02006752 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006753 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006754 goto detach_chip;
6755
6756 return 0;
6757
6758detach_chip:
6759 nand_detach(chip);
6760cleanup_ident:
6761 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006762
David Woodhouse3b85c322006-09-25 17:06:53 +01006763 return ret;
6764}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006765EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006766
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006768 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6769 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006770 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006771void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006773 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006774 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006775 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6776
Jesper Juhlfa671642005-11-07 01:01:27 -08006777 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006778 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006779 kfree(chip->data_buf);
6780 kfree(chip->ecc.code_buf);
6781 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006782
6783 /* Free bad block descriptor memory */
6784 if (chip->badblock_pattern && chip->badblock_pattern->options
6785 & NAND_BBT_DYNAMICSTRUCT)
6786 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006787
6788 /* Free manufacturer priv data. */
6789 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006790
6791 /* Free controller specific allocations after chip identification */
6792 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006793
6794 /* Free identification phase allocations */
6795 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006797
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006798EXPORT_SYMBOL_GPL(nand_cleanup);
6799
6800/**
6801 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6802 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006803 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006804 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006805void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006806{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006807 mtd_device_unregister(nand_to_mtd(chip));
6808 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006809}
David Woodhousee0c7d762006-05-13 18:07:53 +01006810EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006811
David Woodhousee0c7d762006-05-13 18:07:53 +01006812MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006813MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6814MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006815MODULE_DESCRIPTION("Generic NAND flash driver code");