blob: f3d6cd52f7eb24797e3adfb263e5fd742d971276 [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/**
479 * nand_block_markbad_lowlevel - mark a block bad
480 * @mtd: MTD device structure
481 * @ofs: offset from device start
482 *
483 * This function performs the generic NAND bad block marking steps (i.e., bad
484 * block table(s) and/or marker(s)). We only allow the hardware driver to
485 * specify how to write bad block markers to OOB (chip->block_markbad).
486 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700487 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300488 *
Brian Norrise2414f42012-02-06 13:44:00 -0800489 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700490 * (2) write bad block marker to OOB area of affected block (unless flag
491 * NAND_BBT_NO_OOB_BBM is present)
492 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300493 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700494 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800495 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700497static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100499 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700500 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000501
Brian Norrisb32843b2013-07-30 17:52:59 -0700502 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800503 struct erase_info einfo;
504
505 /* Attempt erase before marking OOB */
506 memset(&einfo, 0, sizeof(einfo));
Brian Norris00918422012-01-13 18:11:47 -0800507 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300508 einfo.len = 1ULL << chip->phys_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200509 nand_erase_nand(chip, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800510
Brian Norrisb32843b2013-07-30 17:52:59 -0700511 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800512 nand_get_device(mtd, FL_WRITING);
Boris Brezillonc17556f2018-09-06 14:05:25 +0200513 ret = chip->block_markbad(chip, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300514 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200515 }
Brian Norrise2414f42012-02-06 13:44:00 -0800516
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 /* Mark block bad in BBT */
518 if (chip->bbt) {
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200519 res = nand_markbad_bbt(chip, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800520 if (!ret)
521 ret = res;
522 }
523
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200524 if (!ret)
525 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300526
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000530/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700532 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700534 * Check, if the device is write protected. The function expects, that the
535 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100537static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100539 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100540 u8 status;
541 int ret;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200542
Brian Norris8b6e50c2011-05-25 14:59:01 -0700543 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200544 if (chip->options & NAND_BROKEN_XD)
545 return 0;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Check the WP bit */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100548 ret = nand_status_op(chip, &status);
549 if (ret)
550 return ret;
551
552 return status & NAND_STATUS_WP ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800556 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700557 * @mtd: MTD device structure
558 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300559 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800560 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300561 */
562static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
563{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100564 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300565
566 if (!chip->bbt)
567 return 0;
568 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200569 return nand_isreserved_bbt(chip, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300570}
571
572/**
573 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
574 * @mtd: MTD device structure
575 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700576 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
578 * Check, if the block is bad. Either by reading the bad block table or
579 * calling of the scan function.
580 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530581static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100583 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200585 if (!chip->bbt)
Boris Brezillonc17556f2018-09-06 14:05:25 +0200586 return chip->block_bad(chip, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* Return info from the table */
Boris Brezillon5740d4c2018-09-06 14:05:34 +0200589 return nand_isbad_bbt(chip, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200592/**
593 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700594 * @mtd: MTD device structure
595 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200596 *
597 * Helper function for nand_wait_ready used when needing to wait in interrupt
598 * context.
599 */
600static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
601{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100602 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200603 int i;
604
605 /* Wait for the device to get ready */
606 for (i = 0; i < timeo; i++) {
Boris Brezillon50a487e2018-09-06 14:05:27 +0200607 if (chip->dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200608 break;
609 touch_softlockup_watchdog();
610 mdelay(1);
611 }
612}
613
Alex Smithb70af9b2015-10-06 14:52:07 +0100614/**
615 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200616 * @chip: NAND chip object
Alex Smithb70af9b2015-10-06 14:52:07 +0100617 *
618 * Wait for the ready pin after a command, and warn if a timeout occurs.
619 */
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200620void nand_wait_ready(struct nand_chip *chip)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000621{
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200622 struct mtd_info *mtd = nand_to_mtd(chip);
Alex Smithb70af9b2015-10-06 14:52:07 +0100623 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000624
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200625 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100626 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627
Brian Norris7854d3f2011-06-23 14:12:08 -0700628 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100629 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000630 do {
Boris Brezillon50a487e2018-09-06 14:05:27 +0200631 if (chip->dev_ready(chip))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300632 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100633 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000634 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100635
Boris Brezillon50a487e2018-09-06 14:05:27 +0200636 if (!chip->dev_ready(chip))
Brian Norris9ebfdf52016-03-04 17:19:23 -0800637 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000638}
David Woodhouse4b648b02006-09-25 17:05:24 +0100639EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200642 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
643 * @mtd: MTD device structure
644 * @timeo: Timeout in ms
645 *
646 * Wait for status ready (i.e. command done) or timeout.
647 */
648static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
649{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100650 register struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100651 int ret;
Roger Quadros60c70d62015-02-23 17:26:39 +0200652
653 timeo = jiffies + msecs_to_jiffies(timeo);
654 do {
Boris Brezillon97d90da2017-11-30 18:01:29 +0100655 u8 status;
656
657 ret = nand_read_data_op(chip, &status, sizeof(status), true);
658 if (ret)
659 return;
660
661 if (status & NAND_STATUS_READY)
Roger Quadros60c70d62015-02-23 17:26:39 +0200662 break;
663 touch_softlockup_watchdog();
664 } while (time_before(jiffies, timeo));
665};
666
667/**
Miquel Raynal8878b122017-11-09 14:16:45 +0100668 * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1
669 * @chip: NAND chip structure
670 * @timeout_ms: Timeout in ms
671 *
672 * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1.
673 * If that does not happen whitin the specified timeout, -ETIMEDOUT is
674 * returned.
675 *
676 * This helper is intended to be used when the controller does not have access
677 * to the NAND R/B pin.
678 *
679 * Be aware that calling this helper from an ->exec_op() implementation means
680 * ->exec_op() must be re-entrant.
681 *
682 * Return 0 if the NAND chip is ready, a negative error otherwise.
683 */
684int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms)
685{
Boris Brezillon3057fce2018-05-04 21:24:31 +0200686 const struct nand_sdr_timings *timings;
Miquel Raynal8878b122017-11-09 14:16:45 +0100687 u8 status = 0;
688 int ret;
689
690 if (!chip->exec_op)
691 return -ENOTSUPP;
692
Boris Brezillon3057fce2018-05-04 21:24:31 +0200693 /* Wait tWB before polling the STATUS reg. */
694 timings = nand_get_sdr_timings(&chip->data_interface);
695 ndelay(PSEC_TO_NSEC(timings->tWB_max));
696
Miquel Raynal8878b122017-11-09 14:16:45 +0100697 ret = nand_status_op(chip, NULL);
698 if (ret)
699 return ret;
700
701 timeout_ms = jiffies + msecs_to_jiffies(timeout_ms);
702 do {
703 ret = nand_read_data_op(chip, &status, sizeof(status), true);
704 if (ret)
705 break;
706
707 if (status & NAND_STATUS_READY)
708 break;
709
710 /*
711 * Typical lowest execution time for a tR on most NANDs is 10us,
712 * use this as polling delay before doing something smarter (ie.
713 * deriving a delay from the timeout value, timeout_ms/ratio).
714 */
715 udelay(10);
716 } while (time_before(jiffies, timeout_ms));
717
718 /*
719 * We have to exit READ_STATUS mode in order to read real data on the
720 * bus in case the WAITRDY instruction is preceding a DATA_IN
721 * instruction.
722 */
723 nand_exit_status_op(chip);
724
725 if (ret)
726 return ret;
727
728 return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT;
729};
730EXPORT_SYMBOL_GPL(nand_soft_waitrdy);
731
732/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * nand_command - [DEFAULT] Send command to NAND device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200734 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700735 * @command: the command to be sent
736 * @column: the column address for this command, -1 if none
737 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700739 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200740 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200742static void nand_command(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200743 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200745 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200746 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (command == NAND_CMD_SEQIN) {
750 int readcmd;
751
Joern Engel28318772006-05-22 23:18:05 +0200752 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200754 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 readcmd = NAND_CMD_READOOB;
756 } else if (column < 256) {
757 /* First 256 bytes --> READ0 */
758 readcmd = NAND_CMD_READ0;
759 } else {
760 column -= 256;
761 readcmd = NAND_CMD_READ1;
762 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200763 chip->legacy.cmd_ctrl(chip, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200764 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Miquel Raynaldf467892017-11-08 17:00:27 +0100766 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200767 chip->legacy.cmd_ctrl(chip, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Brian Norris8b6e50c2011-05-25 14:59:01 -0700769 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200770 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
771 /* Serially input address */
772 if (column != -1) {
773 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800774 if (chip->options & NAND_BUSWIDTH_16 &&
775 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200776 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200777 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200778 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200780 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200781 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200782 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200783 chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900784 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200785 chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200786 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200787 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
788 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000789
790 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 * Program and erase have their own busy handlers status and sequential
792 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000795
Miquel Raynaldf467892017-11-08 17:00:27 +0100796 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case NAND_CMD_PAGEPROG:
798 case NAND_CMD_ERASE1:
799 case NAND_CMD_ERASE2:
800 case NAND_CMD_SEQIN:
801 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900802 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900803 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return;
805
806 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200807 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 udelay(chip->chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200810 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
811 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
812 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
813 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200814 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
815 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return;
817
David Woodhousee0c7d762006-05-13 18:07:53 +0100818 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200819 case NAND_CMD_READ0:
820 /*
821 * READ0 is sometimes used to exit GET STATUS mode. When this
822 * is the case no address cycles are requested, and we can use
823 * this information to detect that we should not wait for the
824 * device to be ready.
825 */
826 if (column == -1 && page_addr == -1)
827 return;
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000830 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * If we don't have access to the busy pin, we apply the given
832 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100833 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200834 if (!chip->dev_ready) {
835 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700839 /*
840 * Apply this short delay always to ensure that we do wait tWB in
841 * any case on any machine.
842 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000844
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200845 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200848static void nand_ccs_delay(struct nand_chip *chip)
849{
850 /*
851 * The controller already takes care of waiting for tCCS when the RNDIN
852 * or RNDOUT command is sent, return directly.
853 */
854 if (!(chip->options & NAND_WAIT_TCCS))
855 return;
856
857 /*
858 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
859 * (which should be safe for all NANDs).
860 */
Miquel Raynal17fa8042017-11-30 18:01:31 +0100861 if (chip->setup_data_interface)
862 ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200863 else
864 ndelay(500);
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/**
868 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Boris Brezillon5295cf22018-09-06 14:05:28 +0200869 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700870 * @command: the command to be sent
871 * @column: the column address for this command, -1 if none
872 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200874 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700875 * devices. We don't have the separate regions as we have in the small page
876 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Boris Brezillon5295cf22018-09-06 14:05:28 +0200878static void nand_command_lp(struct nand_chip *chip, unsigned int command,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200879 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200881 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 /* Emulate NAND_CMD_READOOB */
884 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200885 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 command = NAND_CMD_READ0;
887 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000888
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200889 /* Command latch cycle */
Miquel Raynaldf467892017-11-08 17:00:27 +0100890 if (command != NAND_CMD_NONE)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200891 chip->legacy.cmd_ctrl(chip, command,
892 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200895 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* Serially input address */
898 if (column != -1) {
899 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800900 if (chip->options & NAND_BUSWIDTH_16 &&
901 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 column >>= 1;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200903 chip->legacy.cmd_ctrl(chip, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200904 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200905
Brian Norrisf5b88de2016-10-03 09:49:35 -0700906 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200907 if (!nand_opcode_8bits(command))
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200908 chip->legacy.cmd_ctrl(chip, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (page_addr != -1) {
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200911 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
912 chip->legacy.cmd_ctrl(chip, page_addr >> 8,
913 NAND_NCE | NAND_ALE);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900914 if (chip->options & NAND_ROW_ADDR_3)
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200915 chip->legacy.cmd_ctrl(chip, page_addr >> 16,
916 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200919 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
920 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000921
922 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700923 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100924 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927
Miquel Raynaldf467892017-11-08 17:00:27 +0100928 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 case NAND_CMD_CACHEDPROG:
930 case NAND_CMD_PAGEPROG:
931 case NAND_CMD_ERASE1:
932 case NAND_CMD_ERASE2:
933 case NAND_CMD_SEQIN:
934 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900935 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900936 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000937 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200939 case NAND_CMD_RNDIN:
940 nand_ccs_delay(chip);
941 return;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200944 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200946 udelay(chip->chip_delay);
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200947 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
948 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
949 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
950 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200951 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
952 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return;
954
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200955 case NAND_CMD_RNDOUT:
956 /* No ready / busy check necessary */
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200957 chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
958 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
959 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
960 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200961
962 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200963 return;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200966 /*
967 * READ0 is sometimes used to exit GET STATUS mode. When this
968 * is the case no address cycles are requested, and we can use
969 * this information to detect that READSTART should not be
970 * issued.
971 */
972 if (column == -1 && page_addr == -1)
973 return;
974
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200975 chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART,
976 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
977 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
978 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000979
David Woodhousee0c7d762006-05-13 18:07:53 +0100980 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000982 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700984 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100985 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200986 if (!chip->dev_ready) {
987 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000991
Brian Norris8b6e50c2011-05-25 14:59:01 -0700992 /*
993 * Apply this short delay always to ensure that we do wait tWB in
994 * any case on any machine.
995 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100996 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000997
Boris Brezillon2b356ab2018-09-06 14:05:16 +0200998 nand_wait_ready(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001002 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001003 * @chip: the nand chip descriptor
1004 * @mtd: MTD device structure
1005 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001006 *
1007 * Used when in panic, no locks are taken.
1008 */
1009static void panic_nand_get_device(struct nand_chip *chip,
1010 struct mtd_info *mtd, int new_state)
1011{
Brian Norris7854d3f2011-06-23 14:12:08 -07001012 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001013 chip->controller->active = chip;
1014 chip->state = new_state;
1015}
1016
1017/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -07001019 * @mtd: MTD device structure
1020 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 *
1022 * Get the device and lock it for exclusive access
1023 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +02001024static int
Huang Shijie6a8214a2012-11-19 14:43:30 +08001025nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001027 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001028 spinlock_t *lock = &chip->controller->lock;
1029 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +01001030 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001031retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001032 spin_lock(lock);
1033
vimal singhb8b3ee92009-07-09 20:41:22 +05301034 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001035 if (!chip->controller->active)
1036 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +02001037
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001038 if (chip->controller->active == chip && chip->state == FL_READY) {
1039 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001040 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +01001041 return 0;
1042 }
1043 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -08001044 if (chip->controller->active->state == FL_PM_SUSPENDED) {
1045 chip->state = FL_PM_SUSPENDED;
1046 spin_unlock(lock);
1047 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -08001048 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +01001049 }
1050 set_current_state(TASK_UNINTERRUPTIBLE);
1051 add_wait_queue(wq, &wait);
1052 spin_unlock(lock);
1053 schedule();
1054 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto retry;
1056}
1057
1058/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001059 * panic_nand_wait - [GENERIC] wait until the command is done
1060 * @mtd: MTD device structure
1061 * @chip: NAND chip structure
1062 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001063 *
1064 * Wait for command done. This is a helper function for nand_wait used when
1065 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001066 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001067 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001068static void panic_nand_wait(struct nand_chip *chip, unsigned long timeo)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001069{
1070 int i;
1071 for (i = 0; i < timeo; i++) {
1072 if (chip->dev_ready) {
Boris Brezillon50a487e2018-09-06 14:05:27 +02001073 if (chip->dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001074 break;
1075 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001076 int ret;
1077 u8 status;
1078
1079 ret = nand_read_data_op(chip, &status, sizeof(status),
1080 true);
1081 if (ret)
1082 return;
1083
1084 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001085 break;
1086 }
1087 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001088 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001089}
1090
1091/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001092 * nand_wait - [DEFAULT] wait until the command is done
1093 * @mtd: MTD device structure
1094 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001096 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001097 */
Boris Brezillonf1d46942018-09-06 14:05:29 +02001098static int nand_wait(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100
Alex Smithb70af9b2015-10-06 14:52:07 +01001101 unsigned long timeo = 400;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001102 u8 status;
1103 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Brian Norris8b6e50c2011-05-25 14:59:01 -07001105 /*
1106 * Apply this short delay always to ensure that we do wait tWB in any
1107 * case on any machine.
1108 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001109 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Boris Brezillon97d90da2017-11-30 18:01:29 +01001111 ret = nand_status_op(chip, NULL);
1112 if (ret)
1113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001115 if (in_interrupt() || oops_in_progress)
Boris Brezillonf1d46942018-09-06 14:05:29 +02001116 panic_nand_wait(chip, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001117 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001118 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001119 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001120 if (chip->dev_ready) {
Boris Brezillon50a487e2018-09-06 14:05:27 +02001121 if (chip->dev_ready(chip))
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001122 break;
1123 } else {
Boris Brezillon97d90da2017-11-30 18:01:29 +01001124 ret = nand_read_data_op(chip, &status,
1125 sizeof(status), true);
1126 if (ret)
1127 return ret;
1128
1129 if (status & NAND_STATUS_READY)
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001130 break;
1131 }
1132 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001133 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001135
Boris Brezillon97d90da2017-11-30 18:01:29 +01001136 ret = nand_read_data_op(chip, &status, sizeof(status), true);
1137 if (ret)
1138 return ret;
1139
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001140 /* This can happen if in case of timeout or buggy dev_ready */
1141 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return status;
1143}
1144
Miquel Raynal789157e2018-03-19 14:47:28 +01001145static bool nand_supports_get_features(struct nand_chip *chip, int addr)
Miquel Raynal97baea12018-03-19 14:47:20 +01001146{
Miquel Raynal789157e2018-03-19 14:47:28 +01001147 return (chip->parameters.supports_set_get_features &&
1148 test_bit(addr, chip->parameters.get_feature_list));
1149}
1150
1151static bool nand_supports_set_features(struct nand_chip *chip, int addr)
1152{
1153 return (chip->parameters.supports_set_get_features &&
1154 test_bit(addr, chip->parameters.set_feature_list));
Miquel Raynal97baea12018-03-19 14:47:20 +01001155}
1156
1157/**
1158 * nand_get_features - wrapper to perform a GET_FEATURE
1159 * @chip: NAND chip info structure
1160 * @addr: feature address
1161 * @subfeature_param: the subfeature parameters, a four bytes array
1162 *
1163 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1164 * operation cannot be handled.
1165 */
1166int nand_get_features(struct nand_chip *chip, int addr,
1167 u8 *subfeature_param)
1168{
Miquel Raynal789157e2018-03-19 14:47:28 +01001169 if (!nand_supports_get_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001170 return -ENOTSUPP;
1171
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001172 return chip->get_features(chip, addr, subfeature_param);
Miquel Raynal97baea12018-03-19 14:47:20 +01001173}
1174EXPORT_SYMBOL_GPL(nand_get_features);
1175
1176/**
1177 * nand_set_features - wrapper to perform a SET_FEATURE
1178 * @chip: NAND chip info structure
1179 * @addr: feature address
1180 * @subfeature_param: the subfeature parameters, a four bytes array
1181 *
1182 * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the
1183 * operation cannot be handled.
1184 */
1185int nand_set_features(struct nand_chip *chip, int addr,
1186 u8 *subfeature_param)
1187{
Miquel Raynal789157e2018-03-19 14:47:28 +01001188 if (!nand_supports_set_features(chip, addr))
Miquel Raynal97baea12018-03-19 14:47:20 +01001189 return -ENOTSUPP;
1190
Boris Brezillonaa36ff22018-09-06 14:05:31 +02001191 return chip->set_features(chip, addr, subfeature_param);
Miquel Raynal97baea12018-03-19 14:47:20 +01001192}
1193EXPORT_SYMBOL_GPL(nand_set_features);
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001196 * nand_reset_data_interface - Reset data interface and timings
1197 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001198 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001199 *
1200 * Reset the Data interface and timings to ONFI mode 0.
1201 *
1202 * Returns 0 for success or negative error code otherwise.
1203 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001204static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001205{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001206 int ret;
1207
1208 if (!chip->setup_data_interface)
1209 return 0;
1210
1211 /*
1212 * The ONFI specification says:
1213 * "
1214 * To transition from NV-DDR or NV-DDR2 to the SDR data
1215 * interface, the host shall use the Reset (FFh) command
1216 * using SDR timing mode 0. A device in any timing mode is
1217 * required to recognize Reset (FFh) command issued in SDR
1218 * timing mode 0.
1219 * "
1220 *
1221 * Configure the data interface in SDR mode and set the
1222 * timings to timing mode 0.
1223 */
1224
Miquel Raynal17fa8042017-11-30 18:01:31 +01001225 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
Boris Brezillon858838b2018-09-06 14:05:33 +02001226 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001227 if (ret)
1228 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1229
1230 return ret;
1231}
1232
1233/**
1234 * nand_setup_data_interface - Setup the best data interface and timings
1235 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001236 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001237 *
1238 * Find and configure the best data interface and NAND timings supported by
1239 * the chip and the driver.
1240 * First tries to retrieve supported timing modes from ONFI information,
1241 * and if the NAND chip does not support ONFI, relies on the
1242 * ->onfi_timing_mode_default specified in the nand_ids table.
1243 *
1244 * Returns 0 for success or negative error code otherwise.
1245 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001246static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001247{
Miquel Raynal97baea12018-03-19 14:47:20 +01001248 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1249 chip->onfi_timing_mode_default,
1250 };
Boris Brezillond8e725d2016-09-15 10:32:50 +02001251 int ret;
1252
Miquel Raynal17fa8042017-11-30 18:01:31 +01001253 if (!chip->setup_data_interface)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001254 return 0;
1255
Miquel Raynal993447b2018-03-19 14:47:21 +01001256 /* Change the mode on the chip side (if supported by the NAND chip) */
Miquel Raynal789157e2018-03-19 14:47:28 +01001257 if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02001258 chip->select_chip(chip, chipnr);
Miquel Raynal993447b2018-03-19 14:47:21 +01001259 ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1260 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001261 chip->select_chip(chip, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001262 if (ret)
Miquel Raynal993447b2018-03-19 14:47:21 +01001263 return ret;
Boris Brezillond8e725d2016-09-15 10:32:50 +02001264 }
1265
Miquel Raynal97baea12018-03-19 14:47:20 +01001266 /* Change the mode on the controller side */
Boris Brezillon858838b2018-09-06 14:05:33 +02001267 ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface);
Miquel Raynal415ae782018-03-19 14:47:24 +01001268 if (ret)
1269 return ret;
1270
1271 /* Check the mode has been accepted by the chip, if supported */
Miquel Raynal789157e2018-03-19 14:47:28 +01001272 if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE))
Miquel Raynal415ae782018-03-19 14:47:24 +01001273 return 0;
1274
1275 memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001276 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001277 ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE,
1278 tmode_param);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001279 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001280 if (ret)
1281 goto err_reset_chip;
1282
1283 if (tmode_param[0] != chip->onfi_timing_mode_default) {
1284 pr_warn("timing mode %d not acknowledged by the NAND chip\n",
1285 chip->onfi_timing_mode_default);
1286 goto err_reset_chip;
1287 }
1288
1289 return 0;
1290
1291err_reset_chip:
1292 /*
1293 * Fallback to mode 0 if the chip explicitly did not ack the chosen
1294 * timing mode.
1295 */
1296 nand_reset_data_interface(chip, chipnr);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001297 chip->select_chip(chip, chipnr);
Miquel Raynal415ae782018-03-19 14:47:24 +01001298 nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02001299 chip->select_chip(chip, -1);
Miquel Raynal415ae782018-03-19 14:47:24 +01001300
Boris Brezillond8e725d2016-09-15 10:32:50 +02001301 return ret;
1302}
1303
1304/**
1305 * nand_init_data_interface - find the best data interface and timings
1306 * @chip: The NAND chip
1307 *
1308 * Find the best data interface and NAND timings supported by the chip
1309 * and the driver.
1310 * First tries to retrieve supported timing modes from ONFI information,
1311 * and if the NAND chip does not support ONFI, relies on the
1312 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1313 * function nand_chip->data_interface is initialized with the best timing mode
1314 * available.
1315 *
1316 * Returns 0 for success or negative error code otherwise.
1317 */
1318static int nand_init_data_interface(struct nand_chip *chip)
1319{
Boris Brezillond8e725d2016-09-15 10:32:50 +02001320 int modes, mode, ret;
1321
1322 if (!chip->setup_data_interface)
1323 return 0;
1324
1325 /*
1326 * First try to identify the best timings from ONFI parameters and
1327 * if the NAND does not support ONFI, fallback to the default ONFI
1328 * timing mode.
1329 */
1330 modes = onfi_get_async_timing_mode(chip);
1331 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1332 if (!chip->onfi_timing_mode_default)
1333 return 0;
1334
1335 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1336 }
1337
Boris Brezillond8e725d2016-09-15 10:32:50 +02001338
1339 for (mode = fls(modes) - 1; mode >= 0; mode--) {
Miquel Raynal17fa8042017-11-30 18:01:31 +01001340 ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001341 if (ret)
1342 continue;
1343
Miquel Raynald787b8b2017-12-22 18:12:41 +01001344 /*
1345 * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the
1346 * controller supports the requested timings.
1347 */
Boris Brezillon858838b2018-09-06 14:05:33 +02001348 ret = chip->setup_data_interface(chip,
Boris Brezillon104e4422017-03-16 09:35:58 +01001349 NAND_DATA_IFACE_CHECK_ONLY,
Miquel Raynal17fa8042017-11-30 18:01:31 +01001350 &chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001351 if (!ret) {
1352 chip->onfi_timing_mode_default = mode;
1353 break;
1354 }
1355 }
1356
1357 return 0;
1358}
1359
Boris Brezillond8e725d2016-09-15 10:32:50 +02001360/**
Miquel Raynal8878b122017-11-09 14:16:45 +01001361 * nand_fill_column_cycles - fill the column cycles of an address
1362 * @chip: The NAND chip
1363 * @addrs: Array of address cycles to fill
1364 * @offset_in_page: The offset in the page
1365 *
1366 * Fills the first or the first two bytes of the @addrs field depending
1367 * on the NAND bus width and the page size.
1368 *
1369 * Returns the number of cycles needed to encode the column, or a negative
1370 * error code in case one of the arguments is invalid.
1371 */
1372static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs,
1373 unsigned int offset_in_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Miquel Raynal8878b122017-11-09 14:16:45 +01001375 struct mtd_info *mtd = nand_to_mtd(chip);
1376
1377 /* Make sure the offset is less than the actual page size. */
1378 if (offset_in_page > mtd->writesize + mtd->oobsize)
1379 return -EINVAL;
1380
1381 /*
1382 * On small page NANDs, there's a dedicated command to access the OOB
1383 * area, and the column address is relative to the start of the OOB
1384 * area, not the start of the page. Asjust the address accordingly.
1385 */
1386 if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize)
1387 offset_in_page -= mtd->writesize;
1388
1389 /*
1390 * The offset in page is expressed in bytes, if the NAND bus is 16-bit
1391 * wide, then it must be divided by 2.
1392 */
1393 if (chip->options & NAND_BUSWIDTH_16) {
1394 if (WARN_ON(offset_in_page % 2))
1395 return -EINVAL;
1396
1397 offset_in_page /= 2;
1398 }
1399
1400 addrs[0] = offset_in_page;
1401
1402 /*
1403 * Small page NANDs use 1 cycle for the columns, while large page NANDs
1404 * need 2
1405 */
1406 if (mtd->writesize <= 512)
1407 return 1;
1408
1409 addrs[1] = offset_in_page >> 8;
1410
1411 return 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Miquel Raynal8878b122017-11-09 14:16:45 +01001414static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1415 unsigned int offset_in_page, void *buf,
1416 unsigned int len)
1417{
1418 struct mtd_info *mtd = nand_to_mtd(chip);
1419 const struct nand_sdr_timings *sdr =
1420 nand_get_sdr_timings(&chip->data_interface);
1421 u8 addrs[4];
1422 struct nand_op_instr instrs[] = {
1423 NAND_OP_CMD(NAND_CMD_READ0, 0),
1424 NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)),
1425 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1426 PSEC_TO_NSEC(sdr->tRR_min)),
1427 NAND_OP_DATA_IN(len, buf, 0),
1428 };
1429 struct nand_operation op = NAND_OPERATION(instrs);
1430 int ret;
1431
1432 /* Drop the DATA_IN instruction if len is set to 0. */
1433 if (!len)
1434 op.ninstrs--;
1435
1436 if (offset_in_page >= mtd->writesize)
1437 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1438 else if (offset_in_page >= 256 &&
1439 !(chip->options & NAND_BUSWIDTH_16))
1440 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1441
1442 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1443 if (ret < 0)
1444 return ret;
1445
1446 addrs[1] = page;
1447 addrs[2] = page >> 8;
1448
1449 if (chip->options & NAND_ROW_ADDR_3) {
1450 addrs[3] = page >> 16;
1451 instrs[1].ctx.addr.naddrs++;
1452 }
1453
1454 return nand_exec_op(chip, &op);
1455}
1456
1457static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page,
1458 unsigned int offset_in_page, void *buf,
1459 unsigned int len)
1460{
1461 const struct nand_sdr_timings *sdr =
1462 nand_get_sdr_timings(&chip->data_interface);
1463 u8 addrs[5];
1464 struct nand_op_instr instrs[] = {
1465 NAND_OP_CMD(NAND_CMD_READ0, 0),
1466 NAND_OP_ADDR(4, addrs, 0),
1467 NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)),
1468 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1469 PSEC_TO_NSEC(sdr->tRR_min)),
1470 NAND_OP_DATA_IN(len, buf, 0),
1471 };
1472 struct nand_operation op = NAND_OPERATION(instrs);
1473 int ret;
1474
1475 /* Drop the DATA_IN instruction if len is set to 0. */
1476 if (!len)
1477 op.ninstrs--;
1478
1479 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1480 if (ret < 0)
1481 return ret;
1482
1483 addrs[2] = page;
1484 addrs[3] = page >> 8;
1485
1486 if (chip->options & NAND_ROW_ADDR_3) {
1487 addrs[4] = page >> 16;
1488 instrs[1].ctx.addr.naddrs++;
1489 }
1490
1491 return nand_exec_op(chip, &op);
1492}
1493
1494/**
Boris Brezillon97d90da2017-11-30 18:01:29 +01001495 * nand_read_page_op - Do a READ PAGE operation
1496 * @chip: The NAND chip
1497 * @page: page to read
1498 * @offset_in_page: offset within the page
1499 * @buf: buffer used to store the data
1500 * @len: length of the buffer
1501 *
1502 * This function issues a READ PAGE operation.
1503 * This function does not select/unselect the CS line.
1504 *
1505 * Returns 0 on success, a negative error code otherwise.
1506 */
1507int nand_read_page_op(struct nand_chip *chip, unsigned int page,
1508 unsigned int offset_in_page, void *buf, unsigned int len)
1509{
1510 struct mtd_info *mtd = nand_to_mtd(chip);
1511
1512 if (len && !buf)
1513 return -EINVAL;
1514
1515 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1516 return -EINVAL;
1517
Miquel Raynal8878b122017-11-09 14:16:45 +01001518 if (chip->exec_op) {
1519 if (mtd->writesize > 512)
1520 return nand_lp_exec_read_page_op(chip, page,
1521 offset_in_page, buf,
1522 len);
1523
1524 return nand_sp_exec_read_page_op(chip, page, offset_in_page,
1525 buf, len);
1526 }
1527
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001528 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001529 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001530 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001531
1532 return 0;
1533}
1534EXPORT_SYMBOL_GPL(nand_read_page_op);
1535
1536/**
1537 * nand_read_param_page_op - Do a READ PARAMETER PAGE operation
1538 * @chip: The NAND chip
1539 * @page: parameter page to read
1540 * @buf: buffer used to store the data
1541 * @len: length of the buffer
1542 *
1543 * This function issues a READ PARAMETER PAGE operation.
1544 * This function does not select/unselect the CS line.
1545 *
1546 * Returns 0 on success, a negative error code otherwise.
1547 */
1548static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
1549 unsigned int len)
1550{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001551 unsigned int i;
1552 u8 *p = buf;
1553
1554 if (len && !buf)
1555 return -EINVAL;
1556
Miquel Raynal8878b122017-11-09 14:16:45 +01001557 if (chip->exec_op) {
1558 const struct nand_sdr_timings *sdr =
1559 nand_get_sdr_timings(&chip->data_interface);
1560 struct nand_op_instr instrs[] = {
1561 NAND_OP_CMD(NAND_CMD_PARAM, 0),
1562 NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)),
1563 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max),
1564 PSEC_TO_NSEC(sdr->tRR_min)),
1565 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1566 };
1567 struct nand_operation op = NAND_OPERATION(instrs);
1568
1569 /* Drop the DATA_IN instruction if len is set to 0. */
1570 if (!len)
1571 op.ninstrs--;
1572
1573 return nand_exec_op(chip, &op);
1574 }
1575
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001576 chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001577 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001578 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001579
1580 return 0;
1581}
1582
1583/**
1584 * nand_change_read_column_op - Do a CHANGE READ COLUMN operation
1585 * @chip: The NAND chip
1586 * @offset_in_page: offset within the page
1587 * @buf: buffer used to store the data
1588 * @len: length of the buffer
1589 * @force_8bit: force 8-bit bus access
1590 *
1591 * This function issues a CHANGE READ COLUMN operation.
1592 * This function does not select/unselect the CS line.
1593 *
1594 * Returns 0 on success, a negative error code otherwise.
1595 */
1596int nand_change_read_column_op(struct nand_chip *chip,
1597 unsigned int offset_in_page, void *buf,
1598 unsigned int len, bool force_8bit)
1599{
1600 struct mtd_info *mtd = nand_to_mtd(chip);
1601
1602 if (len && !buf)
1603 return -EINVAL;
1604
1605 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1606 return -EINVAL;
1607
Miquel Raynal8878b122017-11-09 14:16:45 +01001608 /* Small page NANDs do not support column change. */
1609 if (mtd->writesize <= 512)
1610 return -ENOTSUPP;
1611
1612 if (chip->exec_op) {
1613 const struct nand_sdr_timings *sdr =
1614 nand_get_sdr_timings(&chip->data_interface);
1615 u8 addrs[2] = {};
1616 struct nand_op_instr instrs[] = {
1617 NAND_OP_CMD(NAND_CMD_RNDOUT, 0),
1618 NAND_OP_ADDR(2, addrs, 0),
1619 NAND_OP_CMD(NAND_CMD_RNDOUTSTART,
1620 PSEC_TO_NSEC(sdr->tCCS_min)),
1621 NAND_OP_DATA_IN(len, buf, 0),
1622 };
1623 struct nand_operation op = NAND_OPERATION(instrs);
1624 int ret;
1625
1626 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1627 if (ret < 0)
1628 return ret;
1629
1630 /* Drop the DATA_IN instruction if len is set to 0. */
1631 if (!len)
1632 op.ninstrs--;
1633
1634 instrs[3].ctx.data.force_8bit = force_8bit;
1635
1636 return nand_exec_op(chip, &op);
1637 }
1638
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001639 chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001640 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001641 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001642
1643 return 0;
1644}
1645EXPORT_SYMBOL_GPL(nand_change_read_column_op);
1646
1647/**
1648 * nand_read_oob_op - Do a READ OOB operation
1649 * @chip: The NAND chip
1650 * @page: page to read
1651 * @offset_in_oob: offset within the OOB area
1652 * @buf: buffer used to store the data
1653 * @len: length of the buffer
1654 *
1655 * This function issues a READ OOB operation.
1656 * This function does not select/unselect the CS line.
1657 *
1658 * Returns 0 on success, a negative error code otherwise.
1659 */
1660int nand_read_oob_op(struct nand_chip *chip, unsigned int page,
1661 unsigned int offset_in_oob, void *buf, unsigned int len)
1662{
1663 struct mtd_info *mtd = nand_to_mtd(chip);
1664
1665 if (len && !buf)
1666 return -EINVAL;
1667
1668 if (offset_in_oob + len > mtd->oobsize)
1669 return -EINVAL;
1670
Miquel Raynal8878b122017-11-09 14:16:45 +01001671 if (chip->exec_op)
1672 return nand_read_page_op(chip, page,
1673 mtd->writesize + offset_in_oob,
1674 buf, len);
1675
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001676 chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001677 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001678 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001679
1680 return 0;
1681}
1682EXPORT_SYMBOL_GPL(nand_read_oob_op);
1683
Miquel Raynal8878b122017-11-09 14:16:45 +01001684static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page,
1685 unsigned int offset_in_page, const void *buf,
1686 unsigned int len, bool prog)
1687{
1688 struct mtd_info *mtd = nand_to_mtd(chip);
1689 const struct nand_sdr_timings *sdr =
1690 nand_get_sdr_timings(&chip->data_interface);
1691 u8 addrs[5] = {};
1692 struct nand_op_instr instrs[] = {
1693 /*
1694 * The first instruction will be dropped if we're dealing
1695 * with a large page NAND and adjusted if we're dealing
1696 * with a small page NAND and the page offset is > 255.
1697 */
1698 NAND_OP_CMD(NAND_CMD_READ0, 0),
1699 NAND_OP_CMD(NAND_CMD_SEQIN, 0),
1700 NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)),
1701 NAND_OP_DATA_OUT(len, buf, 0),
1702 NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)),
1703 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1704 };
1705 struct nand_operation op = NAND_OPERATION(instrs);
1706 int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page);
1707 int ret;
1708 u8 status;
1709
1710 if (naddrs < 0)
1711 return naddrs;
1712
1713 addrs[naddrs++] = page;
1714 addrs[naddrs++] = page >> 8;
1715 if (chip->options & NAND_ROW_ADDR_3)
1716 addrs[naddrs++] = page >> 16;
1717
1718 instrs[2].ctx.addr.naddrs = naddrs;
1719
1720 /* Drop the last two instructions if we're not programming the page. */
1721 if (!prog) {
1722 op.ninstrs -= 2;
1723 /* Also drop the DATA_OUT instruction if empty. */
1724 if (!len)
1725 op.ninstrs--;
1726 }
1727
1728 if (mtd->writesize <= 512) {
1729 /*
1730 * Small pages need some more tweaking: we have to adjust the
1731 * first instruction depending on the page offset we're trying
1732 * to access.
1733 */
1734 if (offset_in_page >= mtd->writesize)
1735 instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB;
1736 else if (offset_in_page >= 256 &&
1737 !(chip->options & NAND_BUSWIDTH_16))
1738 instrs[0].ctx.cmd.opcode = NAND_CMD_READ1;
1739 } else {
1740 /*
1741 * Drop the first command if we're dealing with a large page
1742 * NAND.
1743 */
1744 op.instrs++;
1745 op.ninstrs--;
1746 }
1747
1748 ret = nand_exec_op(chip, &op);
1749 if (!prog || ret)
1750 return ret;
1751
1752 ret = nand_status_op(chip, &status);
1753 if (ret)
1754 return ret;
1755
1756 return status;
1757}
1758
Boris Brezillon97d90da2017-11-30 18:01:29 +01001759/**
1760 * nand_prog_page_begin_op - starts a PROG PAGE operation
1761 * @chip: The NAND chip
1762 * @page: page to write
1763 * @offset_in_page: offset within the page
1764 * @buf: buffer containing the data to write to the page
1765 * @len: length of the buffer
1766 *
1767 * This function issues the first half of a PROG PAGE operation.
1768 * This function does not select/unselect the CS line.
1769 *
1770 * Returns 0 on success, a negative error code otherwise.
1771 */
1772int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page,
1773 unsigned int offset_in_page, const void *buf,
1774 unsigned int len)
1775{
1776 struct mtd_info *mtd = nand_to_mtd(chip);
1777
1778 if (len && !buf)
1779 return -EINVAL;
1780
1781 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1782 return -EINVAL;
1783
Miquel Raynal8878b122017-11-09 14:16:45 +01001784 if (chip->exec_op)
1785 return nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1786 len, false);
1787
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001788 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001789
1790 if (buf)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001791 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001792
1793 return 0;
1794}
1795EXPORT_SYMBOL_GPL(nand_prog_page_begin_op);
1796
1797/**
1798 * nand_prog_page_end_op - ends a PROG PAGE operation
1799 * @chip: The NAND chip
1800 *
1801 * This function issues the second half of a PROG PAGE operation.
1802 * This function does not select/unselect the CS line.
1803 *
1804 * Returns 0 on success, a negative error code otherwise.
1805 */
1806int nand_prog_page_end_op(struct nand_chip *chip)
1807{
Miquel Raynal8878b122017-11-09 14:16:45 +01001808 int ret;
1809 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01001810
Miquel Raynal8878b122017-11-09 14:16:45 +01001811 if (chip->exec_op) {
1812 const struct nand_sdr_timings *sdr =
1813 nand_get_sdr_timings(&chip->data_interface);
1814 struct nand_op_instr instrs[] = {
1815 NAND_OP_CMD(NAND_CMD_PAGEPROG,
1816 PSEC_TO_NSEC(sdr->tWB_max)),
1817 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0),
1818 };
1819 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001820
Miquel Raynal8878b122017-11-09 14:16:45 +01001821 ret = nand_exec_op(chip, &op);
1822 if (ret)
1823 return ret;
1824
1825 ret = nand_status_op(chip, &status);
1826 if (ret)
1827 return ret;
1828 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001829 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillonf1d46942018-09-06 14:05:29 +02001830 ret = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001831 if (ret < 0)
1832 return ret;
1833
1834 status = ret;
1835 }
1836
Boris Brezillon97d90da2017-11-30 18:01:29 +01001837 if (status & NAND_STATUS_FAIL)
1838 return -EIO;
1839
1840 return 0;
1841}
1842EXPORT_SYMBOL_GPL(nand_prog_page_end_op);
1843
1844/**
1845 * nand_prog_page_op - Do a full PROG PAGE operation
1846 * @chip: The NAND chip
1847 * @page: page to write
1848 * @offset_in_page: offset within the page
1849 * @buf: buffer containing the data to write to the page
1850 * @len: length of the buffer
1851 *
1852 * This function issues a full PROG PAGE operation.
1853 * This function does not select/unselect the CS line.
1854 *
1855 * Returns 0 on success, a negative error code otherwise.
1856 */
1857int nand_prog_page_op(struct nand_chip *chip, unsigned int page,
1858 unsigned int offset_in_page, const void *buf,
1859 unsigned int len)
1860{
1861 struct mtd_info *mtd = nand_to_mtd(chip);
1862 int status;
1863
1864 if (!len || !buf)
1865 return -EINVAL;
1866
1867 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1868 return -EINVAL;
1869
Miquel Raynal8878b122017-11-09 14:16:45 +01001870 if (chip->exec_op) {
1871 status = nand_exec_prog_page_op(chip, page, offset_in_page, buf,
1872 len, true);
1873 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001874 chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page,
1875 page);
Boris Brezillon716bbba2018-09-07 00:38:35 +02001876 chip->legacy.write_buf(chip, buf, len);
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001877 chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillonf1d46942018-09-06 14:05:29 +02001878 status = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01001879 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01001880
Boris Brezillon97d90da2017-11-30 18:01:29 +01001881 if (status & NAND_STATUS_FAIL)
1882 return -EIO;
1883
1884 return 0;
1885}
1886EXPORT_SYMBOL_GPL(nand_prog_page_op);
1887
1888/**
1889 * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation
1890 * @chip: The NAND chip
1891 * @offset_in_page: offset within the page
1892 * @buf: buffer containing the data to send to the NAND
1893 * @len: length of the buffer
1894 * @force_8bit: force 8-bit bus access
1895 *
1896 * This function issues a CHANGE WRITE COLUMN operation.
1897 * This function does not select/unselect the CS line.
1898 *
1899 * Returns 0 on success, a negative error code otherwise.
1900 */
1901int nand_change_write_column_op(struct nand_chip *chip,
1902 unsigned int offset_in_page,
1903 const void *buf, unsigned int len,
1904 bool force_8bit)
1905{
1906 struct mtd_info *mtd = nand_to_mtd(chip);
1907
1908 if (len && !buf)
1909 return -EINVAL;
1910
1911 if (offset_in_page + len > mtd->writesize + mtd->oobsize)
1912 return -EINVAL;
1913
Miquel Raynal8878b122017-11-09 14:16:45 +01001914 /* Small page NANDs do not support column change. */
1915 if (mtd->writesize <= 512)
1916 return -ENOTSUPP;
1917
1918 if (chip->exec_op) {
1919 const struct nand_sdr_timings *sdr =
1920 nand_get_sdr_timings(&chip->data_interface);
1921 u8 addrs[2];
1922 struct nand_op_instr instrs[] = {
1923 NAND_OP_CMD(NAND_CMD_RNDIN, 0),
1924 NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)),
1925 NAND_OP_DATA_OUT(len, buf, 0),
1926 };
1927 struct nand_operation op = NAND_OPERATION(instrs);
1928 int ret;
1929
1930 ret = nand_fill_column_cycles(chip, addrs, offset_in_page);
1931 if (ret < 0)
1932 return ret;
1933
1934 instrs[2].ctx.data.force_8bit = force_8bit;
1935
1936 /* Drop the DATA_OUT instruction if len is set to 0. */
1937 if (!len)
1938 op.ninstrs--;
1939
1940 return nand_exec_op(chip, &op);
1941 }
1942
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001943 chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001944 if (len)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001945 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001946
1947 return 0;
1948}
1949EXPORT_SYMBOL_GPL(nand_change_write_column_op);
1950
1951/**
1952 * nand_readid_op - Do a READID operation
1953 * @chip: The NAND chip
1954 * @addr: address cycle to pass after the READID command
1955 * @buf: buffer used to store the ID
1956 * @len: length of the buffer
1957 *
1958 * This function sends a READID command and reads back the ID returned by the
1959 * NAND.
1960 * This function does not select/unselect the CS line.
1961 *
1962 * Returns 0 on success, a negative error code otherwise.
1963 */
1964int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf,
1965 unsigned int len)
1966{
Boris Brezillon97d90da2017-11-30 18:01:29 +01001967 unsigned int i;
1968 u8 *id = buf;
1969
1970 if (len && !buf)
1971 return -EINVAL;
1972
Miquel Raynal8878b122017-11-09 14:16:45 +01001973 if (chip->exec_op) {
1974 const struct nand_sdr_timings *sdr =
1975 nand_get_sdr_timings(&chip->data_interface);
1976 struct nand_op_instr instrs[] = {
1977 NAND_OP_CMD(NAND_CMD_READID, 0),
1978 NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)),
1979 NAND_OP_8BIT_DATA_IN(len, buf, 0),
1980 };
1981 struct nand_operation op = NAND_OPERATION(instrs);
1982
1983 /* Drop the DATA_IN instruction if len is set to 0. */
1984 if (!len)
1985 op.ninstrs--;
1986
1987 return nand_exec_op(chip, &op);
1988 }
1989
Boris Brezillonbf6065c2018-09-07 00:38:36 +02001990 chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001991
1992 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02001993 id[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01001994
1995 return 0;
1996}
1997EXPORT_SYMBOL_GPL(nand_readid_op);
1998
1999/**
2000 * nand_status_op - Do a STATUS operation
2001 * @chip: The NAND chip
2002 * @status: out variable to store the NAND status
2003 *
2004 * This function sends a STATUS command and reads back the status returned by
2005 * the NAND.
2006 * This function does not select/unselect the CS line.
2007 *
2008 * Returns 0 on success, a negative error code otherwise.
2009 */
2010int nand_status_op(struct nand_chip *chip, u8 *status)
2011{
Miquel Raynal8878b122017-11-09 14:16:45 +01002012 if (chip->exec_op) {
2013 const struct nand_sdr_timings *sdr =
2014 nand_get_sdr_timings(&chip->data_interface);
2015 struct nand_op_instr instrs[] = {
2016 NAND_OP_CMD(NAND_CMD_STATUS,
2017 PSEC_TO_NSEC(sdr->tADL_min)),
2018 NAND_OP_8BIT_DATA_IN(1, status, 0),
2019 };
2020 struct nand_operation op = NAND_OPERATION(instrs);
2021
2022 if (!status)
2023 op.ninstrs--;
2024
2025 return nand_exec_op(chip, &op);
2026 }
2027
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002028 chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002029 if (status)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002030 *status = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002031
2032 return 0;
2033}
2034EXPORT_SYMBOL_GPL(nand_status_op);
2035
2036/**
2037 * nand_exit_status_op - Exit a STATUS operation
2038 * @chip: The NAND chip
2039 *
2040 * This function sends a READ0 command to cancel the effect of the STATUS
2041 * command to avoid reading only the status until a new read command is sent.
2042 *
2043 * This function does not select/unselect the CS line.
2044 *
2045 * Returns 0 on success, a negative error code otherwise.
2046 */
2047int nand_exit_status_op(struct nand_chip *chip)
2048{
Miquel Raynal8878b122017-11-09 14:16:45 +01002049 if (chip->exec_op) {
2050 struct nand_op_instr instrs[] = {
2051 NAND_OP_CMD(NAND_CMD_READ0, 0),
2052 };
2053 struct nand_operation op = NAND_OPERATION(instrs);
2054
2055 return nand_exec_op(chip, &op);
2056 }
2057
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002058 chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002059
2060 return 0;
2061}
2062EXPORT_SYMBOL_GPL(nand_exit_status_op);
2063
2064/**
2065 * nand_erase_op - Do an erase operation
2066 * @chip: The NAND chip
2067 * @eraseblock: block to erase
2068 *
2069 * This function sends an ERASE command and waits for the NAND to be ready
2070 * before returning.
2071 * This function does not select/unselect the CS line.
2072 *
2073 * Returns 0 on success, a negative error code otherwise.
2074 */
2075int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock)
2076{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002077 unsigned int page = eraseblock <<
2078 (chip->phys_erase_shift - chip->page_shift);
Miquel Raynal8878b122017-11-09 14:16:45 +01002079 int ret;
2080 u8 status;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002081
Miquel Raynal8878b122017-11-09 14:16:45 +01002082 if (chip->exec_op) {
2083 const struct nand_sdr_timings *sdr =
2084 nand_get_sdr_timings(&chip->data_interface);
2085 u8 addrs[3] = { page, page >> 8, page >> 16 };
2086 struct nand_op_instr instrs[] = {
2087 NAND_OP_CMD(NAND_CMD_ERASE1, 0),
2088 NAND_OP_ADDR(2, addrs, 0),
2089 NAND_OP_CMD(NAND_CMD_ERASE2,
2090 PSEC_TO_MSEC(sdr->tWB_max)),
2091 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0),
2092 };
2093 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002094
Miquel Raynal8878b122017-11-09 14:16:45 +01002095 if (chip->options & NAND_ROW_ADDR_3)
2096 instrs[1].ctx.addr.naddrs++;
2097
2098 ret = nand_exec_op(chip, &op);
2099 if (ret)
2100 return ret;
2101
2102 ret = nand_status_op(chip, &status);
2103 if (ret)
2104 return ret;
2105 } else {
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002106 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page);
2107 chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1);
Miquel Raynal8878b122017-11-09 14:16:45 +01002108
Boris Brezillonf1d46942018-09-06 14:05:29 +02002109 ret = chip->waitfunc(chip);
Miquel Raynal8878b122017-11-09 14:16:45 +01002110 if (ret < 0)
2111 return ret;
2112
2113 status = ret;
2114 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01002115
2116 if (status & NAND_STATUS_FAIL)
2117 return -EIO;
2118
2119 return 0;
2120}
2121EXPORT_SYMBOL_GPL(nand_erase_op);
2122
2123/**
2124 * nand_set_features_op - Do a SET FEATURES operation
2125 * @chip: The NAND chip
2126 * @feature: feature id
2127 * @data: 4 bytes of data
2128 *
2129 * This function sends a SET FEATURES command and waits for the NAND to be
2130 * ready before returning.
2131 * This function does not select/unselect the CS line.
2132 *
2133 * Returns 0 on success, a negative error code otherwise.
2134 */
2135static int nand_set_features_op(struct nand_chip *chip, u8 feature,
2136 const void *data)
2137{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002138 const u8 *params = data;
Miquel Raynal8878b122017-11-09 14:16:45 +01002139 int i, ret;
Boris Brezillon97d90da2017-11-30 18:01:29 +01002140
Miquel Raynal8878b122017-11-09 14:16:45 +01002141 if (chip->exec_op) {
2142 const struct nand_sdr_timings *sdr =
2143 nand_get_sdr_timings(&chip->data_interface);
2144 struct nand_op_instr instrs[] = {
2145 NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0),
2146 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)),
2147 NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data,
2148 PSEC_TO_NSEC(sdr->tWB_max)),
2149 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0),
2150 };
2151 struct nand_operation op = NAND_OPERATION(instrs);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002152
Boris Brezillon782d1962018-05-11 14:44:07 +02002153 return nand_exec_op(chip, &op);
Miquel Raynal8878b122017-11-09 14:16:45 +01002154 }
2155
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002156 chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1);
Boris Brezillon782d1962018-05-11 14:44:07 +02002157 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002158 chip->legacy.write_byte(chip, params[i]);
Boris Brezillon782d1962018-05-11 14:44:07 +02002159
Boris Brezillonf1d46942018-09-06 14:05:29 +02002160 ret = chip->waitfunc(chip);
Boris Brezillon782d1962018-05-11 14:44:07 +02002161 if (ret < 0)
2162 return ret;
2163
2164 if (ret & NAND_STATUS_FAIL)
Boris Brezillon97d90da2017-11-30 18:01:29 +01002165 return -EIO;
2166
2167 return 0;
2168}
2169
2170/**
2171 * nand_get_features_op - Do a GET FEATURES operation
2172 * @chip: The NAND chip
2173 * @feature: feature id
2174 * @data: 4 bytes of data
2175 *
2176 * This function sends a GET FEATURES command and waits for the NAND to be
2177 * ready before returning.
2178 * This function does not select/unselect the CS line.
2179 *
2180 * Returns 0 on success, a negative error code otherwise.
2181 */
2182static int nand_get_features_op(struct nand_chip *chip, u8 feature,
2183 void *data)
2184{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002185 u8 *params = data;
2186 int i;
2187
Miquel Raynal8878b122017-11-09 14:16:45 +01002188 if (chip->exec_op) {
2189 const struct nand_sdr_timings *sdr =
2190 nand_get_sdr_timings(&chip->data_interface);
2191 struct nand_op_instr instrs[] = {
2192 NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0),
2193 NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)),
2194 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max),
2195 PSEC_TO_NSEC(sdr->tRR_min)),
2196 NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN,
2197 data, 0),
2198 };
2199 struct nand_operation op = NAND_OPERATION(instrs);
2200
2201 return nand_exec_op(chip, &op);
2202 }
2203
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002204 chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002205 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002206 params[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002207
2208 return 0;
2209}
2210
Boris Brezillon52f05b62018-07-27 09:44:18 +02002211static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms,
2212 unsigned int delay_ns)
2213{
2214 if (chip->exec_op) {
2215 struct nand_op_instr instrs[] = {
2216 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms),
2217 PSEC_TO_NSEC(delay_ns)),
2218 };
2219 struct nand_operation op = NAND_OPERATION(instrs);
2220
2221 return nand_exec_op(chip, &op);
2222 }
2223
2224 /* Apply delay or wait for ready/busy pin */
2225 if (!chip->dev_ready)
2226 udelay(chip->chip_delay);
2227 else
Boris Brezillon2b356ab2018-09-06 14:05:16 +02002228 nand_wait_ready(chip);
Boris Brezillon52f05b62018-07-27 09:44:18 +02002229
2230 return 0;
2231}
2232
Boris Brezillon97d90da2017-11-30 18:01:29 +01002233/**
2234 * nand_reset_op - Do a reset operation
2235 * @chip: The NAND chip
2236 *
2237 * This function sends a RESET command and waits for the NAND to be ready
2238 * before returning.
2239 * This function does not select/unselect the CS line.
2240 *
2241 * Returns 0 on success, a negative error code otherwise.
2242 */
2243int nand_reset_op(struct nand_chip *chip)
2244{
Miquel Raynal8878b122017-11-09 14:16:45 +01002245 if (chip->exec_op) {
2246 const struct nand_sdr_timings *sdr =
2247 nand_get_sdr_timings(&chip->data_interface);
2248 struct nand_op_instr instrs[] = {
2249 NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)),
2250 NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0),
2251 };
2252 struct nand_operation op = NAND_OPERATION(instrs);
2253
2254 return nand_exec_op(chip, &op);
2255 }
2256
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002257 chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002258
2259 return 0;
2260}
2261EXPORT_SYMBOL_GPL(nand_reset_op);
2262
2263/**
2264 * nand_read_data_op - Read data from the NAND
2265 * @chip: The NAND chip
2266 * @buf: buffer used to store the data
2267 * @len: length of the buffer
2268 * @force_8bit: force 8-bit bus access
2269 *
2270 * This function does a raw data read on the bus. Usually used after launching
2271 * another NAND operation like nand_read_page_op().
2272 * This function does not select/unselect the CS line.
2273 *
2274 * Returns 0 on success, a negative error code otherwise.
2275 */
2276int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len,
2277 bool force_8bit)
2278{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002279 if (!len || !buf)
2280 return -EINVAL;
2281
Miquel Raynal8878b122017-11-09 14:16:45 +01002282 if (chip->exec_op) {
2283 struct nand_op_instr instrs[] = {
2284 NAND_OP_DATA_IN(len, buf, 0),
2285 };
2286 struct nand_operation op = NAND_OPERATION(instrs);
2287
2288 instrs[0].ctx.data.force_8bit = force_8bit;
2289
2290 return nand_exec_op(chip, &op);
2291 }
2292
Boris Brezillon97d90da2017-11-30 18:01:29 +01002293 if (force_8bit) {
2294 u8 *p = buf;
2295 unsigned int i;
2296
2297 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002298 p[i] = chip->legacy.read_byte(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002299 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002300 chip->legacy.read_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002301 }
2302
2303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(nand_read_data_op);
2306
2307/**
2308 * nand_write_data_op - Write data from the NAND
2309 * @chip: The NAND chip
2310 * @buf: buffer containing the data to send on the bus
2311 * @len: length of the buffer
2312 * @force_8bit: force 8-bit bus access
2313 *
2314 * This function does a raw data write on the bus. Usually used after launching
2315 * another NAND operation like nand_write_page_begin_op().
2316 * This function does not select/unselect the CS line.
2317 *
2318 * Returns 0 on success, a negative error code otherwise.
2319 */
2320int nand_write_data_op(struct nand_chip *chip, const void *buf,
2321 unsigned int len, bool force_8bit)
2322{
Boris Brezillon97d90da2017-11-30 18:01:29 +01002323 if (!len || !buf)
2324 return -EINVAL;
2325
Miquel Raynal8878b122017-11-09 14:16:45 +01002326 if (chip->exec_op) {
2327 struct nand_op_instr instrs[] = {
2328 NAND_OP_DATA_OUT(len, buf, 0),
2329 };
2330 struct nand_operation op = NAND_OPERATION(instrs);
2331
2332 instrs[0].ctx.data.force_8bit = force_8bit;
2333
2334 return nand_exec_op(chip, &op);
2335 }
2336
Boris Brezillon97d90da2017-11-30 18:01:29 +01002337 if (force_8bit) {
2338 const u8 *p = buf;
2339 unsigned int i;
2340
2341 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002342 chip->legacy.write_byte(chip, p[i]);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002343 } else {
Boris Brezillon716bbba2018-09-07 00:38:35 +02002344 chip->legacy.write_buf(chip, buf, len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002345 }
2346
2347 return 0;
2348}
2349EXPORT_SYMBOL_GPL(nand_write_data_op);
2350
2351/**
Miquel Raynal8878b122017-11-09 14:16:45 +01002352 * struct nand_op_parser_ctx - Context used by the parser
2353 * @instrs: array of all the instructions that must be addressed
2354 * @ninstrs: length of the @instrs array
2355 * @subop: Sub-operation to be passed to the NAND controller
2356 *
2357 * This structure is used by the core to split NAND operations into
2358 * sub-operations that can be handled by the NAND controller.
2359 */
2360struct nand_op_parser_ctx {
2361 const struct nand_op_instr *instrs;
2362 unsigned int ninstrs;
2363 struct nand_subop subop;
2364};
2365
2366/**
2367 * nand_op_parser_must_split_instr - Checks if an instruction must be split
2368 * @pat: the parser pattern element that matches @instr
2369 * @instr: pointer to the instruction to check
2370 * @start_offset: this is an in/out parameter. If @instr has already been
2371 * split, then @start_offset is the offset from which to start
2372 * (either an address cycle or an offset in the data buffer).
2373 * Conversely, if the function returns true (ie. instr must be
2374 * split), this parameter is updated to point to the first
2375 * data/address cycle that has not been taken care of.
2376 *
2377 * Some NAND controllers are limited and cannot send X address cycles with a
2378 * unique operation, or cannot read/write more than Y bytes at the same time.
2379 * In this case, split the instruction that does not fit in a single
2380 * controller-operation into two or more chunks.
2381 *
2382 * Returns true if the instruction must be split, false otherwise.
2383 * The @start_offset parameter is also updated to the offset at which the next
2384 * bundle of instruction must start (if an address or a data instruction).
2385 */
2386static bool
2387nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat,
2388 const struct nand_op_instr *instr,
2389 unsigned int *start_offset)
2390{
2391 switch (pat->type) {
2392 case NAND_OP_ADDR_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002393 if (!pat->ctx.addr.maxcycles)
Miquel Raynal8878b122017-11-09 14:16:45 +01002394 break;
2395
2396 if (instr->ctx.addr.naddrs - *start_offset >
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002397 pat->ctx.addr.maxcycles) {
2398 *start_offset += pat->ctx.addr.maxcycles;
Miquel Raynal8878b122017-11-09 14:16:45 +01002399 return true;
2400 }
2401 break;
2402
2403 case NAND_OP_DATA_IN_INSTR:
2404 case NAND_OP_DATA_OUT_INSTR:
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002405 if (!pat->ctx.data.maxlen)
Miquel Raynal8878b122017-11-09 14:16:45 +01002406 break;
2407
Miquel Raynalc1a72e22018-01-19 19:11:27 +01002408 if (instr->ctx.data.len - *start_offset >
2409 pat->ctx.data.maxlen) {
2410 *start_offset += pat->ctx.data.maxlen;
Miquel Raynal8878b122017-11-09 14:16:45 +01002411 return true;
2412 }
2413 break;
2414
2415 default:
2416 break;
2417 }
2418
2419 return false;
2420}
2421
2422/**
2423 * nand_op_parser_match_pat - Checks if a pattern matches the instructions
2424 * remaining in the parser context
2425 * @pat: the pattern to test
2426 * @ctx: the parser context structure to match with the pattern @pat
2427 *
2428 * Check if @pat matches the set or a sub-set of instructions remaining in @ctx.
2429 * Returns true if this is the case, false ortherwise. When true is returned,
2430 * @ctx->subop is updated with the set of instructions to be passed to the
2431 * controller driver.
2432 */
2433static bool
2434nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat,
2435 struct nand_op_parser_ctx *ctx)
2436{
2437 unsigned int instr_offset = ctx->subop.first_instr_start_off;
2438 const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs;
2439 const struct nand_op_instr *instr = ctx->subop.instrs;
2440 unsigned int i, ninstrs;
2441
2442 for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) {
2443 /*
2444 * The pattern instruction does not match the operation
2445 * instruction. If the instruction is marked optional in the
2446 * pattern definition, we skip the pattern element and continue
2447 * to the next one. If the element is mandatory, there's no
2448 * match and we can return false directly.
2449 */
2450 if (instr->type != pat->elems[i].type) {
2451 if (!pat->elems[i].optional)
2452 return false;
2453
2454 continue;
2455 }
2456
2457 /*
2458 * Now check the pattern element constraints. If the pattern is
2459 * not able to handle the whole instruction in a single step,
2460 * we have to split it.
2461 * The last_instr_end_off value comes back updated to point to
2462 * the position where we have to split the instruction (the
2463 * start of the next subop chunk).
2464 */
2465 if (nand_op_parser_must_split_instr(&pat->elems[i], instr,
2466 &instr_offset)) {
2467 ninstrs++;
2468 i++;
2469 break;
2470 }
2471
2472 instr++;
2473 ninstrs++;
2474 instr_offset = 0;
2475 }
2476
2477 /*
2478 * This can happen if all instructions of a pattern are optional.
2479 * Still, if there's not at least one instruction handled by this
2480 * pattern, this is not a match, and we should try the next one (if
2481 * any).
2482 */
2483 if (!ninstrs)
2484 return false;
2485
2486 /*
2487 * We had a match on the pattern head, but the pattern may be longer
2488 * than the instructions we're asked to execute. We need to make sure
2489 * there's no mandatory elements in the pattern tail.
2490 */
2491 for (; i < pat->nelems; i++) {
2492 if (!pat->elems[i].optional)
2493 return false;
2494 }
2495
2496 /*
2497 * We have a match: update the subop structure accordingly and return
2498 * true.
2499 */
2500 ctx->subop.ninstrs = ninstrs;
2501 ctx->subop.last_instr_end_off = instr_offset;
2502
2503 return true;
2504}
2505
2506#if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
2507static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2508{
2509 const struct nand_op_instr *instr;
2510 char *prefix = " ";
2511 unsigned int i;
2512
2513 pr_debug("executing subop:\n");
2514
2515 for (i = 0; i < ctx->ninstrs; i++) {
2516 instr = &ctx->instrs[i];
2517
2518 if (instr == &ctx->subop.instrs[0])
2519 prefix = " ->";
2520
2521 switch (instr->type) {
2522 case NAND_OP_CMD_INSTR:
2523 pr_debug("%sCMD [0x%02x]\n", prefix,
2524 instr->ctx.cmd.opcode);
2525 break;
2526 case NAND_OP_ADDR_INSTR:
2527 pr_debug("%sADDR [%d cyc: %*ph]\n", prefix,
2528 instr->ctx.addr.naddrs,
2529 instr->ctx.addr.naddrs < 64 ?
2530 instr->ctx.addr.naddrs : 64,
2531 instr->ctx.addr.addrs);
2532 break;
2533 case NAND_OP_DATA_IN_INSTR:
2534 pr_debug("%sDATA_IN [%d B%s]\n", prefix,
2535 instr->ctx.data.len,
2536 instr->ctx.data.force_8bit ?
2537 ", force 8-bit" : "");
2538 break;
2539 case NAND_OP_DATA_OUT_INSTR:
2540 pr_debug("%sDATA_OUT [%d B%s]\n", prefix,
2541 instr->ctx.data.len,
2542 instr->ctx.data.force_8bit ?
2543 ", force 8-bit" : "");
2544 break;
2545 case NAND_OP_WAITRDY_INSTR:
2546 pr_debug("%sWAITRDY [max %d ms]\n", prefix,
2547 instr->ctx.waitrdy.timeout_ms);
2548 break;
2549 }
2550
2551 if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1])
2552 prefix = " ";
2553 }
2554}
2555#else
2556static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx)
2557{
2558 /* NOP */
2559}
2560#endif
2561
2562/**
2563 * nand_op_parser_exec_op - exec_op parser
2564 * @chip: the NAND chip
2565 * @parser: patterns description provided by the controller driver
2566 * @op: the NAND operation to address
2567 * @check_only: when true, the function only checks if @op can be handled but
2568 * does not execute the operation
2569 *
2570 * Helper function designed to ease integration of NAND controller drivers that
2571 * only support a limited set of instruction sequences. The supported sequences
2572 * are described in @parser, and the framework takes care of splitting @op into
2573 * multiple sub-operations (if required) and pass them back to the ->exec()
2574 * callback of the matching pattern if @check_only is set to false.
2575 *
2576 * NAND controller drivers should call this function from their own ->exec_op()
2577 * implementation.
2578 *
2579 * Returns 0 on success, a negative error code otherwise. A failure can be
2580 * caused by an unsupported operation (none of the supported patterns is able
2581 * to handle the requested operation), or an error returned by one of the
2582 * matching pattern->exec() hook.
2583 */
2584int nand_op_parser_exec_op(struct nand_chip *chip,
2585 const struct nand_op_parser *parser,
2586 const struct nand_operation *op, bool check_only)
2587{
2588 struct nand_op_parser_ctx ctx = {
2589 .subop.instrs = op->instrs,
2590 .instrs = op->instrs,
2591 .ninstrs = op->ninstrs,
2592 };
2593 unsigned int i;
2594
2595 while (ctx.subop.instrs < op->instrs + op->ninstrs) {
2596 int ret;
2597
2598 for (i = 0; i < parser->npatterns; i++) {
2599 const struct nand_op_parser_pattern *pattern;
2600
2601 pattern = &parser->patterns[i];
2602 if (!nand_op_parser_match_pat(pattern, &ctx))
2603 continue;
2604
2605 nand_op_parser_trace(&ctx);
2606
2607 if (check_only)
2608 break;
2609
2610 ret = pattern->exec(chip, &ctx.subop);
2611 if (ret)
2612 return ret;
2613
2614 break;
2615 }
2616
2617 if (i == parser->npatterns) {
2618 pr_debug("->exec_op() parser: pattern not found!\n");
2619 return -ENOTSUPP;
2620 }
2621
2622 /*
2623 * Update the context structure by pointing to the start of the
2624 * next subop.
2625 */
2626 ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs;
2627 if (ctx.subop.last_instr_end_off)
2628 ctx.subop.instrs -= 1;
2629
2630 ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off;
2631 }
2632
2633 return 0;
2634}
2635EXPORT_SYMBOL_GPL(nand_op_parser_exec_op);
2636
2637static bool nand_instr_is_data(const struct nand_op_instr *instr)
2638{
2639 return instr && (instr->type == NAND_OP_DATA_IN_INSTR ||
2640 instr->type == NAND_OP_DATA_OUT_INSTR);
2641}
2642
2643static bool nand_subop_instr_is_valid(const struct nand_subop *subop,
2644 unsigned int instr_idx)
2645{
2646 return subop && instr_idx < subop->ninstrs;
2647}
2648
Miquel Raynal760c4352018-07-19 00:09:12 +02002649static unsigned int nand_subop_get_start_off(const struct nand_subop *subop,
2650 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002651{
2652 if (instr_idx)
2653 return 0;
2654
2655 return subop->first_instr_start_off;
2656}
2657
2658/**
2659 * nand_subop_get_addr_start_off - Get the start offset in an address array
2660 * @subop: The entire sub-operation
2661 * @instr_idx: Index of the instruction inside the sub-operation
2662 *
2663 * During driver development, one could be tempted to directly use the
2664 * ->addr.addrs field of address instructions. This is wrong as address
2665 * instructions might be split.
2666 *
2667 * Given an address instruction, returns the offset of the first cycle to issue.
2668 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002669unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop,
2670 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002671{
Miquel Raynal760c4352018-07-19 00:09:12 +02002672 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2673 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2674 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002675
2676 return nand_subop_get_start_off(subop, instr_idx);
2677}
2678EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off);
2679
2680/**
2681 * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert
2682 * @subop: The entire sub-operation
2683 * @instr_idx: Index of the instruction inside the sub-operation
2684 *
2685 * During driver development, one could be tempted to directly use the
2686 * ->addr->naddrs field of a data instruction. This is wrong as instructions
2687 * might be split.
2688 *
2689 * Given an address instruction, returns the number of address cycle to issue.
2690 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002691unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop,
2692 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002693{
2694 int start_off, end_off;
2695
Miquel Raynal760c4352018-07-19 00:09:12 +02002696 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2697 subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR))
2698 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002699
2700 start_off = nand_subop_get_addr_start_off(subop, instr_idx);
2701
2702 if (instr_idx == subop->ninstrs - 1 &&
2703 subop->last_instr_end_off)
2704 end_off = subop->last_instr_end_off;
2705 else
2706 end_off = subop->instrs[instr_idx].ctx.addr.naddrs;
2707
2708 return end_off - start_off;
2709}
2710EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc);
2711
2712/**
2713 * nand_subop_get_data_start_off - Get the start offset in a data array
2714 * @subop: The entire sub-operation
2715 * @instr_idx: Index of the instruction inside the sub-operation
2716 *
2717 * During driver development, one could be tempted to directly use the
2718 * ->data->buf.{in,out} field of data instructions. This is wrong as data
2719 * instructions might be split.
2720 *
2721 * Given a data instruction, returns the offset to start from.
2722 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002723unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop,
2724 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002725{
Miquel Raynal760c4352018-07-19 00:09:12 +02002726 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2727 !nand_instr_is_data(&subop->instrs[instr_idx])))
2728 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002729
2730 return nand_subop_get_start_off(subop, instr_idx);
2731}
2732EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off);
2733
2734/**
2735 * nand_subop_get_data_len - Get the number of bytes to retrieve
2736 * @subop: The entire sub-operation
2737 * @instr_idx: Index of the instruction inside the sub-operation
2738 *
2739 * During driver development, one could be tempted to directly use the
2740 * ->data->len field of a data instruction. This is wrong as data instructions
2741 * might be split.
2742 *
2743 * Returns the length of the chunk of data to send/receive.
2744 */
Miquel Raynal760c4352018-07-19 00:09:12 +02002745unsigned int nand_subop_get_data_len(const struct nand_subop *subop,
2746 unsigned int instr_idx)
Miquel Raynal8878b122017-11-09 14:16:45 +01002747{
2748 int start_off = 0, end_off;
2749
Miquel Raynal760c4352018-07-19 00:09:12 +02002750 if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) ||
2751 !nand_instr_is_data(&subop->instrs[instr_idx])))
2752 return 0;
Miquel Raynal8878b122017-11-09 14:16:45 +01002753
2754 start_off = nand_subop_get_data_start_off(subop, instr_idx);
2755
2756 if (instr_idx == subop->ninstrs - 1 &&
2757 subop->last_instr_end_off)
2758 end_off = subop->last_instr_end_off;
2759 else
2760 end_off = subop->instrs[instr_idx].ctx.data.len;
2761
2762 return end_off - start_off;
2763}
2764EXPORT_SYMBOL_GPL(nand_subop_get_data_len);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002767 * nand_reset - Reset and initialize a NAND device
2768 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02002769 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002770 *
Miquel Raynal17fa8042017-11-30 18:01:31 +01002771 * Save the timings data structure, then apply SDR timings mode 0 (see
2772 * nand_reset_data_interface for details), do the reset operation, and
2773 * apply back the previous timings.
2774 *
2775 * Returns 0 on success, a negative error code otherwise.
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002776 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002777int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002778{
Miquel Raynal17fa8042017-11-30 18:01:31 +01002779 struct nand_data_interface saved_data_intf = chip->data_interface;
Boris Brezillond8e725d2016-09-15 10:32:50 +02002780 int ret;
2781
Boris Brezillon104e4422017-03-16 09:35:58 +01002782 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002783 if (ret)
2784 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002785
Boris Brezillon73f907f2016-10-24 16:46:20 +02002786 /*
2787 * The CS line has to be released before we can apply the new NAND
2788 * interface settings, hence this weird ->select_chip() dance.
2789 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02002790 chip->select_chip(chip, chipnr);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002791 ret = nand_reset_op(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02002792 chip->select_chip(chip, -1);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002793 if (ret)
2794 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002795
Miquel Raynal107b7d62018-03-19 14:47:25 +01002796 /*
2797 * A nand_reset_data_interface() put both the NAND chip and the NAND
2798 * controller in timings mode 0. If the default mode for this chip is
2799 * also 0, no need to proceed to the change again. Plus, at probe time,
2800 * nand_setup_data_interface() uses ->set/get_features() which would
2801 * fail anyway as the parameter page is not available yet.
2802 */
2803 if (!chip->onfi_timing_mode_default)
2804 return 0;
2805
Miquel Raynal17fa8042017-11-30 18:01:31 +01002806 chip->data_interface = saved_data_intf;
Boris Brezillon104e4422017-03-16 09:35:58 +01002807 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02002808 if (ret)
2809 return ret;
2810
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002811 return 0;
2812}
Boris Brezillonb9bb9842017-10-05 18:53:19 +02002813EXPORT_SYMBOL_GPL(nand_reset);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02002814
2815/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002816 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
2817 * @buf: buffer to test
2818 * @len: buffer length
2819 * @bitflips_threshold: maximum number of bitflips
2820 *
2821 * Check if a buffer contains only 0xff, which means the underlying region
2822 * has been erased and is ready to be programmed.
2823 * The bitflips_threshold specify the maximum number of bitflips before
2824 * considering the region is not erased.
2825 * Note: The logic of this function has been extracted from the memweight
2826 * implementation, except that nand_check_erased_buf function exit before
2827 * testing the whole buffer if the number of bitflips exceed the
2828 * bitflips_threshold value.
2829 *
2830 * Returns a positive number of bitflips less than or equal to
2831 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2832 * threshold.
2833 */
2834static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
2835{
2836 const unsigned char *bitmap = buf;
2837 int bitflips = 0;
2838 int weight;
2839
2840 for (; len && ((uintptr_t)bitmap) % sizeof(long);
2841 len--, bitmap++) {
2842 weight = hweight8(*bitmap);
2843 bitflips += BITS_PER_BYTE - weight;
2844 if (unlikely(bitflips > bitflips_threshold))
2845 return -EBADMSG;
2846 }
2847
2848 for (; len >= sizeof(long);
2849 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02002850 unsigned long d = *((unsigned long *)bitmap);
2851 if (d == ~0UL)
2852 continue;
2853 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02002854 bitflips += BITS_PER_LONG - weight;
2855 if (unlikely(bitflips > bitflips_threshold))
2856 return -EBADMSG;
2857 }
2858
2859 for (; len > 0; len--, bitmap++) {
2860 weight = hweight8(*bitmap);
2861 bitflips += BITS_PER_BYTE - weight;
2862 if (unlikely(bitflips > bitflips_threshold))
2863 return -EBADMSG;
2864 }
2865
2866 return bitflips;
2867}
2868
2869/**
2870 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
2871 * 0xff data
2872 * @data: data buffer to test
2873 * @datalen: data length
2874 * @ecc: ECC buffer
2875 * @ecclen: ECC length
2876 * @extraoob: extra OOB buffer
2877 * @extraooblen: extra OOB length
2878 * @bitflips_threshold: maximum number of bitflips
2879 *
2880 * Check if a data buffer and its associated ECC and OOB data contains only
2881 * 0xff pattern, which means the underlying region has been erased and is
2882 * ready to be programmed.
2883 * The bitflips_threshold specify the maximum number of bitflips before
2884 * considering the region as not erased.
2885 *
2886 * Note:
2887 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
2888 * different from the NAND page size. When fixing bitflips, ECC engines will
2889 * report the number of errors per chunk, and the NAND core infrastructure
2890 * expect you to return the maximum number of bitflips for the whole page.
2891 * This is why you should always use this function on a single chunk and
2892 * not on the whole page. After checking each chunk you should update your
2893 * max_bitflips value accordingly.
2894 * 2/ When checking for bitflips in erased pages you should not only check
2895 * the payload data but also their associated ECC data, because a user might
2896 * have programmed almost all bits to 1 but a few. In this case, we
2897 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
2898 * this case.
2899 * 3/ The extraoob argument is optional, and should be used if some of your OOB
2900 * data are protected by the ECC engine.
2901 * It could also be used if you support subpages and want to attach some
2902 * extra OOB data to an ECC chunk.
2903 *
2904 * Returns a positive number of bitflips less than or equal to
2905 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
2906 * threshold. In case of success, the passed buffers are filled with 0xff.
2907 */
2908int nand_check_erased_ecc_chunk(void *data, int datalen,
2909 void *ecc, int ecclen,
2910 void *extraoob, int extraooblen,
2911 int bitflips_threshold)
2912{
2913 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
2914
2915 data_bitflips = nand_check_erased_buf(data, datalen,
2916 bitflips_threshold);
2917 if (data_bitflips < 0)
2918 return data_bitflips;
2919
2920 bitflips_threshold -= data_bitflips;
2921
2922 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
2923 if (ecc_bitflips < 0)
2924 return ecc_bitflips;
2925
2926 bitflips_threshold -= ecc_bitflips;
2927
2928 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
2929 bitflips_threshold);
2930 if (extraoob_bitflips < 0)
2931 return extraoob_bitflips;
2932
2933 if (data_bitflips)
2934 memset(data, 0xff, datalen);
2935
2936 if (ecc_bitflips)
2937 memset(ecc, 0xff, ecclen);
2938
2939 if (extraoob_bitflips)
2940 memset(extraoob, 0xff, extraooblen);
2941
2942 return data_bitflips + ecc_bitflips + extraoob_bitflips;
2943}
2944EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
2945
2946/**
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002947 * nand_read_page_raw_notsupp - dummy read raw page function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002948 * @chip: nand chip info structure
2949 * @buf: buffer to store read data
2950 * @oob_required: caller requires OOB data read to chip->oob_poi
2951 * @page: page number to read
2952 *
2953 * Returns -ENOTSUPP unconditionally.
2954 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002955int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
2956 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02002957{
2958 return -ENOTSUPP;
2959}
2960EXPORT_SYMBOL(nand_read_page_raw_notsupp);
2961
2962/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002963 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002964 * @chip: nand chip info structure
2965 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002966 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002967 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002968 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002969 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002970 */
Boris Brezillonb9761682018-09-06 14:05:20 +02002971int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required,
2972 int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002973{
Boris Brezillonb9761682018-09-06 14:05:20 +02002974 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002975 int ret;
2976
Boris Brezillon25f815f2017-11-30 18:01:30 +01002977 ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01002978 if (ret)
2979 return ret;
2980
2981 if (oob_required) {
2982 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize,
2983 false);
2984 if (ret)
2985 return ret;
2986 }
2987
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002988 return 0;
2989}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002990EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002991
2992/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002993 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002994 * @chip: nand chip info structure
2995 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07002996 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08002998 *
2999 * We need a special oob layout and handling even when OOB isn't used.
3000 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003001static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf,
Brian Norris1fbb9382012-05-02 10:14:55 -07003002 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08003003{
Boris Brezillonb9761682018-09-06 14:05:20 +02003004 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08003005 int eccsize = chip->ecc.size;
3006 int eccbytes = chip->ecc.bytes;
3007 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003008 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003009
Boris Brezillon25f815f2017-11-30 18:01:30 +01003010 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3011 if (ret)
3012 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08003013
3014 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003015 ret = nand_read_data_op(chip, buf, eccsize, false);
3016 if (ret)
3017 return ret;
3018
David Brownell52ff49d2009-03-04 12:01:36 -08003019 buf += eccsize;
3020
3021 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003022 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3023 false);
3024 if (ret)
3025 return ret;
3026
David Brownell52ff49d2009-03-04 12:01:36 -08003027 oob += chip->ecc.prepad;
3028 }
3029
Boris Brezillon97d90da2017-11-30 18:01:29 +01003030 ret = nand_read_data_op(chip, oob, eccbytes, false);
3031 if (ret)
3032 return ret;
3033
David Brownell52ff49d2009-03-04 12:01:36 -08003034 oob += eccbytes;
3035
3036 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003037 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3038 false);
3039 if (ret)
3040 return ret;
3041
David Brownell52ff49d2009-03-04 12:01:36 -08003042 oob += chip->ecc.postpad;
3043 }
3044 }
3045
3046 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003047 if (size) {
3048 ret = nand_read_data_op(chip, oob, size, false);
3049 if (ret)
3050 return ret;
3051 }
David Brownell52ff49d2009-03-04 12:01:36 -08003052
3053 return 0;
3054}
3055
3056/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003057 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003058 * @chip: nand chip info structure
3059 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003060 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003061 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00003062 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003063static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf,
3064 int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Boris Brezillonb9761682018-09-06 14:05:20 +02003066 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003067 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003068 int eccbytes = chip->ecc.bytes;
3069 int eccsteps = chip->ecc.steps;
3070 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003071 uint8_t *ecc_calc = chip->ecc.calc_buf;
3072 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003073 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003074
Boris Brezillonb9761682018-09-06 14:05:20 +02003075 chip->ecc.read_page_raw(chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003076
3077 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003078 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003079
Boris Brezillon846031d2016-02-03 20:11:00 +01003080 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3081 chip->ecc.total);
3082 if (ret)
3083 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003084
3085 eccsteps = chip->ecc.steps;
3086 p = buf;
3087
3088 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3089 int stat;
3090
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003091 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07003092 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003093 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003094 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003095 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003096 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3097 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003098 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003099 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01003100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303103 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003104 * @chip: nand chip info structure
3105 * @data_offs: offset of requested data within the page
3106 * @readlen: data length
3107 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08003108 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01003109 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003110static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs,
3111 uint32_t readlen, uint8_t *bufpoi, int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01003112{
Boris Brezillonb9761682018-09-06 14:05:20 +02003113 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003114 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003115 uint8_t *p;
3116 int data_col_addr, i, gaps = 0;
3117 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
3118 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01003119 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07003120 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01003121 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01003122
Brian Norris7854d3f2011-06-23 14:12:08 -07003123 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01003124 start_step = data_offs / chip->ecc.size;
3125 end_step = (data_offs + readlen - 1) / chip->ecc.size;
3126 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10303127 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01003128
Brian Norris8b6e50c2011-05-25 14:59:01 -07003129 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01003130 datafrag_len = num_steps * chip->ecc.size;
3131 eccfrag_len = num_steps * chip->ecc.bytes;
3132
3133 data_col_addr = start_step * chip->ecc.size;
3134 /* If we read not a page aligned data */
Alexey Korolev3d459552008-05-15 17:23:18 +01003135 p = bufpoi + data_col_addr;
Boris Brezillon25f815f2017-11-30 18:01:30 +01003136 ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003137 if (ret)
3138 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003139
Brian Norris8b6e50c2011-05-25 14:59:01 -07003140 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01003141 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003142 chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]);
Alexey Korolev3d459552008-05-15 17:23:18 +01003143
Brian Norris8b6e50c2011-05-25 14:59:01 -07003144 /*
3145 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07003146 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07003147 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003148 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
3149 if (ret)
3150 return ret;
3151
3152 if (oobregion.length < eccfrag_len)
3153 gaps = 1;
3154
Alexey Korolev3d459552008-05-15 17:23:18 +01003155 if (gaps) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003156 ret = nand_change_read_column_op(chip, mtd->writesize,
3157 chip->oob_poi, mtd->oobsize,
3158 false);
3159 if (ret)
3160 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003161 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003162 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07003163 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07003164 * about buswidth alignment in read_buf.
3165 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003166 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01003167 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01003168 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003169 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01003170 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
3171 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01003172 aligned_len++;
3173
Boris Brezillon97d90da2017-11-30 18:01:29 +01003174 ret = nand_change_read_column_op(chip,
3175 mtd->writesize + aligned_pos,
3176 &chip->oob_poi[aligned_pos],
3177 aligned_len, false);
3178 if (ret)
3179 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003180 }
3181
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003182 ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf,
Boris Brezillon846031d2016-02-03 20:11:00 +01003183 chip->oob_poi, index, eccfrag_len);
3184 if (ret)
3185 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01003186
3187 p = bufpoi + data_col_addr;
3188 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
3189 int stat;
3190
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003191 stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i],
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003192 &chip->ecc.calc_buf[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003193 if (stat == -EBADMSG &&
3194 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3195 /* check for empty pages with bitflips */
3196 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003197 &chip->ecc.code_buf[i],
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003198 chip->ecc.bytes,
3199 NULL, 0,
3200 chip->ecc.strength);
3201 }
3202
Mike Dunn3f91e942012-04-25 12:06:09 -07003203 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003204 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003205 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01003206 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003207 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3208 }
Alexey Korolev3d459552008-05-15 17:23:18 +01003209 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003210 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01003211}
3212
3213/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003214 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003215 * @chip: nand chip info structure
3216 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003217 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003218 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003219 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003220 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003221 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003222static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
3223 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003224{
Boris Brezillonb9761682018-09-06 14:05:20 +02003225 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003226 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003227 int eccbytes = chip->ecc.bytes;
3228 int eccsteps = chip->ecc.steps;
3229 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003230 uint8_t *ecc_calc = chip->ecc.calc_buf;
3231 uint8_t *ecc_code = chip->ecc.code_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003232 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003233
Boris Brezillon25f815f2017-11-30 18:01:30 +01003234 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3235 if (ret)
3236 return ret;
3237
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003238 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02003239 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003240
3241 ret = nand_read_data_op(chip, p, eccsize, false);
3242 if (ret)
3243 return ret;
3244
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003245 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003246 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003247
3248 ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false);
3249 if (ret)
3250 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003251
Boris Brezillon846031d2016-02-03 20:11:00 +01003252 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3253 chip->ecc.total);
3254 if (ret)
3255 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003256
3257 eccsteps = chip->ecc.steps;
3258 p = buf;
3259
3260 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3261 int stat;
3262
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003263 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003264 if (stat == -EBADMSG &&
3265 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3266 /* check for empty pages with bitflips */
3267 stat = nand_check_erased_ecc_chunk(p, eccsize,
3268 &ecc_code[i], eccbytes,
3269 NULL, 0,
3270 chip->ecc.strength);
3271 }
3272
Mike Dunn3f91e942012-04-25 12:06:09 -07003273 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003274 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003275 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003276 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003277 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3278 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003279 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003280 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003281}
3282
3283/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003284 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07003285 * @chip: nand chip info structure
3286 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003287 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003288 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003289 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003290 * Hardware ECC for large page chips, require OOB to be read first. For this
3291 * ECC mode, the write_page method is re-used from ECC_HW. These methods
3292 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
3293 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
3294 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003295 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003296static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf,
3297 int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003298{
Boris Brezillonb9761682018-09-06 14:05:20 +02003299 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01003300 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003301 int eccbytes = chip->ecc.bytes;
3302 int eccsteps = chip->ecc.steps;
3303 uint8_t *p = buf;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003304 uint8_t *ecc_code = chip->ecc.code_buf;
3305 uint8_t *ecc_calc = chip->ecc.calc_buf;
Mike Dunn3f91e942012-04-25 12:06:09 -07003306 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003307
3308 /* Read the OOB area first */
Boris Brezillon97d90da2017-11-30 18:01:29 +01003309 ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
3310 if (ret)
3311 return ret;
3312
3313 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3314 if (ret)
3315 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003316
Boris Brezillon846031d2016-02-03 20:11:00 +01003317 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
3318 chip->ecc.total);
3319 if (ret)
3320 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003321
3322 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3323 int stat;
3324
Boris Brezillonec476362018-09-06 14:05:17 +02003325 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003326
3327 ret = nand_read_data_op(chip, p, eccsize, false);
3328 if (ret)
3329 return ret;
3330
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02003331 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003332
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003333 stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003334 if (stat == -EBADMSG &&
3335 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3336 /* check for empty pages with bitflips */
3337 stat = nand_check_erased_ecc_chunk(p, eccsize,
3338 &ecc_code[i], eccbytes,
3339 NULL, 0,
3340 chip->ecc.strength);
3341 }
3342
Mike Dunn3f91e942012-04-25 12:06:09 -07003343 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003344 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07003345 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003346 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07003347 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3348 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003349 }
Mike Dunn3f91e942012-04-25 12:06:09 -07003350 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003351}
3352
3353/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003354 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07003355 * @chip: nand chip info structure
3356 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07003357 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07003358 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003360 * The hw generator calculates the error syndrome automatically. Therefore we
3361 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003362 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003363static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf,
3364 int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003365{
Boris Brezillonb9761682018-09-06 14:05:20 +02003366 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003367 int ret, i, eccsize = chip->ecc.size;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003368 int eccbytes = chip->ecc.bytes;
3369 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003370 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003371 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003372 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07003373 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003374
Boris Brezillon25f815f2017-11-30 18:01:30 +01003375 ret = nand_read_page_op(chip, page, 0, NULL, 0);
3376 if (ret)
3377 return ret;
3378
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003379 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
3380 int stat;
3381
Boris Brezillonec476362018-09-06 14:05:17 +02003382 chip->ecc.hwctl(chip, NAND_ECC_READ);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003383
3384 ret = nand_read_data_op(chip, p, eccsize, false);
3385 if (ret)
3386 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003387
3388 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003389 ret = nand_read_data_op(chip, oob, chip->ecc.prepad,
3390 false);
3391 if (ret)
3392 return ret;
3393
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003394 oob += chip->ecc.prepad;
3395 }
3396
Boris Brezillonec476362018-09-06 14:05:17 +02003397 chip->ecc.hwctl(chip, NAND_ECC_READSYN);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003398
3399 ret = nand_read_data_op(chip, oob, eccbytes, false);
3400 if (ret)
3401 return ret;
3402
Boris Brezillon00da2ea2018-09-06 14:05:19 +02003403 stat = chip->ecc.correct(chip, p, oob, NULL);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003405 oob += eccbytes;
3406
3407 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003408 ret = nand_read_data_op(chip, oob, chip->ecc.postpad,
3409 false);
3410 if (ret)
3411 return ret;
3412
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003413 oob += chip->ecc.postpad;
3414 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01003415
3416 if (stat == -EBADMSG &&
3417 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
3418 /* check for empty pages with bitflips */
3419 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
3420 oob - eccpadbytes,
3421 eccpadbytes,
3422 NULL, 0,
3423 chip->ecc.strength);
3424 }
3425
3426 if (stat < 0) {
3427 mtd->ecc_stats.failed++;
3428 } else {
3429 mtd->ecc_stats.corrected += stat;
3430 max_bitflips = max_t(unsigned int, max_bitflips, stat);
3431 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003432 }
3433
3434 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04003435 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003436 if (i) {
3437 ret = nand_read_data_op(chip, oob, i, false);
3438 if (ret)
3439 return ret;
3440 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003441
Mike Dunn3f91e942012-04-25 12:06:09 -07003442 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003443}
3444
3445/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003446 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01003447 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07003448 * @oob: oob destination address
3449 * @ops: oob ops structure
3450 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003451 */
Boris Brezillon846031d2016-02-03 20:11:00 +01003452static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03003453 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003454{
Boris Brezillon846031d2016-02-03 20:11:00 +01003455 struct nand_chip *chip = mtd_to_nand(mtd);
3456 int ret;
3457
Florian Fainellif8ac0412010-09-07 13:23:43 +02003458 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003459
Brian Norris0612b9d2011-08-30 18:45:40 -07003460 case MTD_OPS_PLACE_OOB:
3461 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003462 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
3463 return oob + len;
3464
Boris Brezillon846031d2016-02-03 20:11:00 +01003465 case MTD_OPS_AUTO_OOB:
3466 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
3467 ops->ooboffs, len);
3468 BUG_ON(ret);
3469 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003470
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003471 default:
3472 BUG();
3473 }
3474 return NULL;
3475}
3476
3477/**
Brian Norrisba84fb52014-01-03 15:13:33 -08003478 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003479 * @chip: NAND chip object
Brian Norrisba84fb52014-01-03 15:13:33 -08003480 * @retry_mode: the retry mode to use
3481 *
3482 * Some vendors supply a special command to shift the Vt threshold, to be used
3483 * when there are too many bitflips in a page (i.e., ECC error). After setting
3484 * a new threshold, the host should retry reading the page.
3485 */
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003486static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
Brian Norrisba84fb52014-01-03 15:13:33 -08003487{
Brian Norrisba84fb52014-01-03 15:13:33 -08003488 pr_debug("setting READ RETRY mode %d\n", retry_mode);
3489
3490 if (retry_mode >= chip->read_retries)
3491 return -EINVAL;
3492
3493 if (!chip->setup_read_retry)
3494 return -EOPNOTSUPP;
3495
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003496 return chip->setup_read_retry(chip, retry_mode);
Brian Norrisba84fb52014-01-03 15:13:33 -08003497}
3498
Boris Brezillon85e08e52018-07-27 09:44:17 +02003499static void nand_wait_readrdy(struct nand_chip *chip)
3500{
Boris Brezillon52f05b62018-07-27 09:44:18 +02003501 const struct nand_sdr_timings *sdr;
3502
Boris Brezillon85e08e52018-07-27 09:44:17 +02003503 if (!(chip->options & NAND_NEED_READRDY))
3504 return;
3505
Boris Brezillon52f05b62018-07-27 09:44:18 +02003506 sdr = nand_get_sdr_timings(&chip->data_interface);
3507 WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0));
Boris Brezillon85e08e52018-07-27 09:44:17 +02003508}
3509
Brian Norrisba84fb52014-01-03 15:13:33 -08003510/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003511 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003512 * @mtd: MTD device structure
3513 * @from: offset to read from
3514 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00003515 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003516 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00003517 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003518static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
3519 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00003520{
Brian Norrise47f3db2012-05-02 10:14:56 -07003521 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003522 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003523 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003524 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03003525 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01003526 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02003527
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003528 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003529 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07003530 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08003531 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08003532 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003534 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003535 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003537 realpage = (int)(from >> chip->page_shift);
3538 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003540 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003542 buf = ops->datbuf;
3543 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07003544 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003545
Florian Fainellif8ac0412010-09-07 13:23:43 +02003546 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08003547 unsigned int ecc_failures = mtd->ecc_stats.failed;
3548
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003549 bytes = min(mtd->writesize - col, readlen);
3550 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003551
Kamal Dasu66507c72014-05-01 20:51:19 -04003552 if (!aligned)
3553 use_bufpoi = 1;
3554 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09003555 use_bufpoi = !virt_addr_valid(buf) ||
3556 !IS_ALIGNED((unsigned long)buf,
3557 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04003558 else
3559 use_bufpoi = 0;
3560
Brian Norris8b6e50c2011-05-25 14:59:01 -07003561 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003562 if (realpage != chip->pagebuf || oob) {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003563 bufpoi = use_bufpoi ? chip->data_buf : buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04003564
3565 if (use_bufpoi && aligned)
3566 pr_debug("%s: using read bounce buffer for buf@%p\n",
3567 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Brian Norrisba84fb52014-01-03 15:13:33 -08003569read_retry:
Mike Dunnedbc45402012-04-25 12:06:11 -07003570 /*
3571 * Now read the page into the buffer. Absent an error,
3572 * the read methods return max bitflips per ecc step.
3573 */
Brian Norris0612b9d2011-08-30 18:45:40 -07003574 if (unlikely(ops->mode == MTD_OPS_RAW))
Boris Brezillonb9761682018-09-06 14:05:20 +02003575 ret = chip->ecc.read_page_raw(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003576 oob_required,
3577 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003578 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
3579 !oob)
Boris Brezillonb9761682018-09-06 14:05:20 +02003580 ret = chip->ecc.read_subpage(chip, col, bytes,
3581 bufpoi, page);
David Woodhouse956e9442006-09-25 17:12:39 +01003582 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003583 ret = chip->ecc.read_page(chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07003584 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07003585 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04003586 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07003587 /* Invalidate page cache */
3588 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01003589 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07003590 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003591
3592 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04003593 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003594 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08003595 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07003596 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01003597 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07003598 chip->pagebuf_bitflips = ret;
3599 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07003600 /* Invalidate page cache */
3601 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07003602 }
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003603 memcpy(buf, chip->data_buf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003605
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003606 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003607 int toread = min(oobreadlen, max_oobsize);
3608
3609 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01003610 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02003611 oob, ops, toread);
3612 oobreadlen -= toread;
3613 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003614 }
Brian Norris5bc7c332013-03-13 09:51:31 -07003615
Boris Brezillon85e08e52018-07-27 09:44:17 +02003616 nand_wait_readrdy(chip);
Brian Norrisb72f3df2013-12-03 11:04:14 -08003617
Brian Norrisba84fb52014-01-03 15:13:33 -08003618 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08003619 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08003620 retry_mode++;
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003621 ret = nand_setup_read_retry(chip,
Brian Norrisba84fb52014-01-03 15:13:33 -08003622 retry_mode);
3623 if (ret < 0)
3624 break;
3625
3626 /* Reset failures; retry */
3627 mtd->ecc_stats.failed = ecc_failures;
3628 goto read_retry;
3629 } else {
3630 /* No more retry modes; real failure */
3631 ecc_fail = true;
3632 }
3633 }
3634
3635 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09003636 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003637 } else {
Masahiro Yamadac0313b92017-12-05 17:47:16 +09003638 memcpy(buf, chip->data_buf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003639 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07003640 max_bitflips = max_t(unsigned int, max_bitflips,
3641 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003644 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003645
Brian Norrisba84fb52014-01-03 15:13:33 -08003646 /* Reset to retry mode 0 */
3647 if (retry_mode) {
Boris Brezillon2e7f1ce2018-09-06 14:05:32 +02003648 ret = nand_setup_read_retry(chip, 0);
Brian Norrisba84fb52014-01-03 15:13:33 -08003649 if (ret < 0)
3650 break;
3651 retry_mode = 0;
3652 }
3653
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003654 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Brian Norris8b6e50c2011-05-25 14:59:01 -07003657 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 col = 0;
3659 /* Increment page address */
3660 realpage++;
3661
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003662 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 /* Check, if we cross a chip boundary */
3664 if (!page) {
3665 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003666 chip->select_chip(chip, -1);
3667 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003670 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003672 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03003673 if (oob)
3674 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Mike Dunn3f91e942012-04-25 12:06:09 -07003676 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003677 return ret;
3678
Brian Norrisb72f3df2013-12-03 11:04:14 -08003679 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02003680 return -EBADMSG;
3681
Mike Dunnedbc45402012-04-25 12:06:11 -07003682 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003683}
3684
3685/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003686 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003687 * @chip: nand chip info structure
3688 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003689 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003690int nand_read_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003691{
Boris Brezillonb9761682018-09-06 14:05:20 +02003692 struct mtd_info *mtd = nand_to_mtd(chip);
3693
Boris Brezillon97d90da2017-11-30 18:01:29 +01003694 return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003695}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003696EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003697
3698/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003699 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003700 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07003701 * @chip: nand chip info structure
3702 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003703 */
Boris Brezillonb9761682018-09-06 14:05:20 +02003704int nand_read_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003705{
Boris Brezillonb9761682018-09-06 14:05:20 +02003706 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003707 int length = mtd->oobsize;
3708 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3709 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02003710 uint8_t *bufpoi = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003711 int i, toread, sndrnd = 0, pos, ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003712
Boris Brezillon97d90da2017-11-30 18:01:29 +01003713 ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0);
3714 if (ret)
3715 return ret;
3716
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003717 for (i = 0; i < chip->ecc.steps; i++) {
3718 if (sndrnd) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01003719 int ret;
3720
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003721 pos = eccsize + i * (eccsize + chunk);
3722 if (mtd->writesize > 512)
Boris Brezillon97d90da2017-11-30 18:01:29 +01003723 ret = nand_change_read_column_op(chip, pos,
3724 NULL, 0,
3725 false);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003726 else
Boris Brezillon97d90da2017-11-30 18:01:29 +01003727 ret = nand_read_page_op(chip, page, pos, NULL,
3728 0);
3729
3730 if (ret)
3731 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003732 } else
3733 sndrnd = 1;
3734 toread = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003735
3736 ret = nand_read_data_op(chip, bufpoi, toread, false);
3737 if (ret)
3738 return ret;
3739
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003740 bufpoi += toread;
3741 length -= toread;
3742 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003743 if (length > 0) {
3744 ret = nand_read_data_op(chip, bufpoi, length, false);
3745 if (ret)
3746 return ret;
3747 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003748
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03003749 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003750}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003751EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003752
3753/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003754 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003755 * @chip: nand chip info structure
3756 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003757 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003758int nand_write_oob_std(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003759{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003760 struct mtd_info *mtd = nand_to_mtd(chip);
3761
Boris Brezillon97d90da2017-11-30 18:01:29 +01003762 return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi,
3763 mtd->oobsize);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003764}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003765EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003766
3767/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003768 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003769 * with syndrome - only for large page flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003770 * @chip: nand chip info structure
3771 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003772 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003773int nand_write_oob_syndrome(struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003774{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003775 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003776 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
3777 int eccsize = chip->ecc.size, length = mtd->oobsize;
Boris Brezillon97d90da2017-11-30 18:01:29 +01003778 int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003779 const uint8_t *bufpoi = chip->oob_poi;
3780
3781 /*
3782 * data-ecc-data-ecc ... ecc-oob
3783 * or
3784 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
3785 */
3786 if (!chip->ecc.prepad && !chip->ecc.postpad) {
3787 pos = steps * (eccsize + chunk);
3788 steps = 0;
3789 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02003790 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003791
Boris Brezillon97d90da2017-11-30 18:01:29 +01003792 ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0);
3793 if (ret)
3794 return ret;
3795
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003796 for (i = 0; i < steps; i++) {
3797 if (sndcmd) {
3798 if (mtd->writesize <= 512) {
3799 uint32_t fill = 0xFFFFFFFF;
3800
3801 len = eccsize;
3802 while (len > 0) {
3803 int num = min_t(int, len, 4);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003804
3805 ret = nand_write_data_op(chip, &fill,
3806 num, false);
3807 if (ret)
3808 return ret;
3809
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003810 len -= num;
3811 }
3812 } else {
3813 pos = eccsize + i * (eccsize + chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003814 ret = nand_change_write_column_op(chip, pos,
3815 NULL, 0,
3816 false);
3817 if (ret)
3818 return ret;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003819 }
3820 } else
3821 sndcmd = 1;
3822 len = min_t(int, length, chunk);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003823
3824 ret = nand_write_data_op(chip, bufpoi, len, false);
3825 if (ret)
3826 return ret;
3827
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003828 bufpoi += len;
3829 length -= len;
3830 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01003831 if (length > 0) {
3832 ret = nand_write_data_op(chip, bufpoi, length, false);
3833 if (ret)
3834 return ret;
3835 }
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003836
Boris Brezillon97d90da2017-11-30 18:01:29 +01003837 return nand_prog_page_end_op(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003838}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02003839EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003840
3841/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003842 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003843 * @mtd: MTD device structure
3844 * @from: offset to read from
3845 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003847 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003849static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
3850 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003852 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07003853 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003854 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07003855 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03003856 int readlen = ops->ooblen;
3857 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003858 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003859 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Brian Norris289c0522011-07-19 10:06:09 -07003861 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303862 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Brian Norris041e4572011-06-23 16:45:24 -07003864 stats = mtd->ecc_stats;
3865
Boris BREZILLON29f10582016-03-07 10:46:52 +01003866 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003867
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003868 chipnr = (int)(from >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02003869 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003871 /* Shift to get page */
3872 realpage = (int)(from >> chip->page_shift);
3873 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Florian Fainellif8ac0412010-09-07 13:23:43 +02003875 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003876 if (ops->mode == MTD_OPS_RAW)
Boris Brezillonb9761682018-09-06 14:05:20 +02003877 ret = chip->ecc.read_oob_raw(chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07003878 else
Boris Brezillonb9761682018-09-06 14:05:20 +02003879 ret = chip->ecc.read_oob(chip, page);
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003880
3881 if (ret < 0)
3882 break;
Vitaly Wool70145682006-11-03 18:20:38 +03003883
3884 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01003885 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003886
Boris Brezillon85e08e52018-07-27 09:44:17 +02003887 nand_wait_readrdy(chip);
Brian Norris5bc7c332013-03-13 09:51:31 -07003888
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003889 max_bitflips = max_t(unsigned int, max_bitflips, ret);
3890
Vitaly Wool70145682006-11-03 18:20:38 +03003891 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02003892 if (!readlen)
3893 break;
3894
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003895 /* Increment page address */
3896 realpage++;
3897
3898 page = realpage & chip->pagemask;
3899 /* Check, if we cross a chip boundary */
3900 if (!page) {
3901 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02003902 chip->select_chip(chip, -1);
3903 chip->select_chip(chip, chipnr);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02003906 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03003908 ops->oobretlen = ops->ooblen - readlen;
3909
3910 if (ret < 0)
3911 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07003912
3913 if (mtd->ecc_stats.failed - stats.failed)
3914 return -EBADMSG;
3915
Miquel Raynal87e89ce2018-01-12 10:13:36 +01003916 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
3919/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003920 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003921 * @mtd: MTD device structure
3922 * @from: offset to read from
3923 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003925 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003927static int nand_read_oob(struct mtd_info *mtd, loff_t from,
3928 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003930 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003931
3932 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07003934 if (ops->mode != MTD_OPS_PLACE_OOB &&
3935 ops->mode != MTD_OPS_AUTO_OOB &&
3936 ops->mode != MTD_OPS_RAW)
3937 return -ENOTSUPP;
3938
Huang Shijie6a8214a2012-11-19 14:43:30 +08003939 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003941 if (!ops->datbuf)
3942 ret = nand_do_read_oob(mtd, from, ops);
3943 else
3944 ret = nand_do_read_ops(mtd, from, ops);
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003947 return ret;
3948}
3949
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003950/**
3951 * nand_write_page_raw_notsupp - dummy raw page write function
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003952 * @chip: nand chip info structure
3953 * @buf: data buffer
3954 * @oob_required: must write chip->oob_poi to OOB
3955 * @page: page number to write
3956 *
3957 * Returns -ENOTSUPP unconditionally.
3958 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003959int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
3960 int oob_required, int page)
Boris Brezillon0d6030a2018-07-18 10:42:17 +02003961{
3962 return -ENOTSUPP;
3963}
3964EXPORT_SYMBOL(nand_write_page_raw_notsupp);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003965
3966/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003967 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003968 * @chip: nand chip info structure
3969 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07003970 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02003971 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08003972 *
Brian Norris7854d3f2011-06-23 14:12:08 -07003973 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003974 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003975int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
3976 int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003977{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02003978 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003979 int ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08003980
Boris Brezillon25f815f2017-11-30 18:01:30 +01003981 ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
Boris Brezillon97d90da2017-11-30 18:01:29 +01003982 if (ret)
3983 return ret;
3984
3985 if (oob_required) {
3986 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
3987 false);
3988 if (ret)
3989 return ret;
3990 }
Josh Wufdbad98d2012-06-25 18:07:45 +08003991
Boris Brezillon25f815f2017-11-30 18:01:30 +01003992 return nand_prog_page_end_op(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02003994EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003996/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003997 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003998 * @chip: nand chip info structure
3999 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004000 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004001 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08004002 *
4003 * We need a special oob layout and handling even when ECC isn't checked.
4004 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004005static int nand_write_page_raw_syndrome(struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004006 const uint8_t *buf, int oob_required,
4007 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08004008{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004009 struct mtd_info *mtd = nand_to_mtd(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004010 int eccsize = chip->ecc.size;
4011 int eccbytes = chip->ecc.bytes;
4012 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004013 int steps, size, ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004014
Boris Brezillon25f815f2017-11-30 18:01:30 +01004015 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4016 if (ret)
4017 return ret;
David Brownell52ff49d2009-03-04 12:01:36 -08004018
4019 for (steps = chip->ecc.steps; steps > 0; steps--) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004020 ret = nand_write_data_op(chip, buf, eccsize, false);
4021 if (ret)
4022 return ret;
4023
David Brownell52ff49d2009-03-04 12:01:36 -08004024 buf += eccsize;
4025
4026 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004027 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4028 false);
4029 if (ret)
4030 return ret;
4031
David Brownell52ff49d2009-03-04 12:01:36 -08004032 oob += chip->ecc.prepad;
4033 }
4034
Boris Brezillon97d90da2017-11-30 18:01:29 +01004035 ret = nand_write_data_op(chip, oob, eccbytes, false);
4036 if (ret)
4037 return ret;
4038
David Brownell52ff49d2009-03-04 12:01:36 -08004039 oob += eccbytes;
4040
4041 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004042 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4043 false);
4044 if (ret)
4045 return ret;
4046
David Brownell52ff49d2009-03-04 12:01:36 -08004047 oob += chip->ecc.postpad;
4048 }
4049 }
4050
4051 size = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004052 if (size) {
4053 ret = nand_write_data_op(chip, oob, size, false);
4054 if (ret)
4055 return ret;
4056 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004057
Boris Brezillon25f815f2017-11-30 18:01:30 +01004058 return nand_prog_page_end_op(chip);
David Brownell52ff49d2009-03-04 12:01:36 -08004059}
4060/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004061 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004062 * @chip: nand chip info structure
4063 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004064 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004065 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004066 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004067static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf,
4068 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004069{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004070 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004071 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004072 int eccbytes = chip->ecc.bytes;
4073 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004074 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004075 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004076
Brian Norris7854d3f2011-06-23 14:12:08 -07004077 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004078 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004079 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004080
Boris Brezillon846031d2016-02-03 20:11:00 +01004081 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4082 chip->ecc.total);
4083 if (ret)
4084 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004085
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004086 return chip->ecc.write_page_raw(chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004087}
4088
4089/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004090 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07004091 * @chip: nand chip info structure
4092 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004093 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004094 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004095 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004096static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf,
4097 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004098{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004099 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon846031d2016-02-03 20:11:00 +01004100 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004101 int eccbytes = chip->ecc.bytes;
4102 int eccsteps = chip->ecc.steps;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004103 uint8_t *ecc_calc = chip->ecc.calc_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004104 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004105
Boris Brezillon25f815f2017-11-30 18:01:30 +01004106 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4107 if (ret)
4108 return ret;
4109
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004110 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004111 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004112
4113 ret = nand_write_data_op(chip, p, eccsize, false);
4114 if (ret)
4115 return ret;
4116
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004117 chip->ecc.calculate(chip, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004118 }
4119
Boris Brezillon846031d2016-02-03 20:11:00 +01004120 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4121 chip->ecc.total);
4122 if (ret)
4123 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004124
Boris Brezillon97d90da2017-11-30 18:01:29 +01004125 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4126 if (ret)
4127 return ret;
Josh Wufdbad98d2012-06-25 18:07:45 +08004128
Boris Brezillon25f815f2017-11-30 18:01:30 +01004129 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004130}
4131
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304132
4133/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08004134 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304135 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07004136 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304137 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07004138 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304139 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004140 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304141 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004142static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset,
4143 uint32_t data_len, const uint8_t *buf,
4144 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304145{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004146 struct mtd_info *mtd = nand_to_mtd(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304147 uint8_t *oob_buf = chip->oob_poi;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004148 uint8_t *ecc_calc = chip->ecc.calc_buf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304149 int ecc_size = chip->ecc.size;
4150 int ecc_bytes = chip->ecc.bytes;
4151 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304152 uint32_t start_step = offset / ecc_size;
4153 uint32_t end_step = (offset + data_len - 1) / ecc_size;
4154 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01004155 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304156
Boris Brezillon25f815f2017-11-30 18:01:30 +01004157 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4158 if (ret)
4159 return ret;
4160
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304161 for (step = 0; step < ecc_steps; step++) {
4162 /* configure controller for WRITE access */
Boris Brezillonec476362018-09-06 14:05:17 +02004163 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304164
4165 /* write data (untouched subpages already masked by 0xFF) */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004166 ret = nand_write_data_op(chip, buf, ecc_size, false);
4167 if (ret)
4168 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304169
4170 /* mask ECC of un-touched subpages by padding 0xFF */
4171 if ((step < start_step) || (step > end_step))
4172 memset(ecc_calc, 0xff, ecc_bytes);
4173 else
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004174 chip->ecc.calculate(chip, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304175
4176 /* mask OOB of un-touched subpages by padding 0xFF */
4177 /* if oob_required, preserve OOB metadata of written subpage */
4178 if (!oob_required || (step < start_step) || (step > end_step))
4179 memset(oob_buf, 0xff, oob_bytes);
4180
Brian Norrisd6a950802013-08-08 17:16:36 -07004181 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304182 ecc_calc += ecc_bytes;
4183 oob_buf += oob_bytes;
4184 }
4185
4186 /* copy calculated ECC for whole page to chip->buffer->oob */
4187 /* this include masked-value(0xFF) for unwritten subpages */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004188 ecc_calc = chip->ecc.calc_buf;
Boris Brezillon846031d2016-02-03 20:11:00 +01004189 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
4190 chip->ecc.total);
4191 if (ret)
4192 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304193
4194 /* write OOB buffer to NAND device */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004195 ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false);
4196 if (ret)
4197 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304198
Boris Brezillon25f815f2017-11-30 18:01:30 +01004199 return nand_prog_page_end_op(chip);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304200}
4201
4202
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004203/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004204 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004205 * @chip: nand chip info structure
4206 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07004207 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02004208 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004209 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004210 * The hw generator calculates the error syndrome automatically. Therefore we
4211 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004212 */
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004213static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf,
4214 int oob_required, int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004215{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004216 struct mtd_info *mtd = nand_to_mtd(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004217 int i, eccsize = chip->ecc.size;
4218 int eccbytes = chip->ecc.bytes;
4219 int eccsteps = chip->ecc.steps;
4220 const uint8_t *p = buf;
4221 uint8_t *oob = chip->oob_poi;
Boris Brezillon97d90da2017-11-30 18:01:29 +01004222 int ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004223
Boris Brezillon25f815f2017-11-30 18:01:30 +01004224 ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0);
4225 if (ret)
4226 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004227
4228 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Boris Brezillonec476362018-09-06 14:05:17 +02004229 chip->ecc.hwctl(chip, NAND_ECC_WRITE);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004230
4231 ret = nand_write_data_op(chip, p, eccsize, false);
4232 if (ret)
4233 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004234
4235 if (chip->ecc.prepad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004236 ret = nand_write_data_op(chip, oob, chip->ecc.prepad,
4237 false);
4238 if (ret)
4239 return ret;
4240
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004241 oob += chip->ecc.prepad;
4242 }
4243
Boris Brezillonaf37d2c2018-09-06 14:05:18 +02004244 chip->ecc.calculate(chip, p, oob);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004245
4246 ret = nand_write_data_op(chip, oob, eccbytes, false);
4247 if (ret)
4248 return ret;
4249
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004250 oob += eccbytes;
4251
4252 if (chip->ecc.postpad) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01004253 ret = nand_write_data_op(chip, oob, chip->ecc.postpad,
4254 false);
4255 if (ret)
4256 return ret;
4257
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004258 oob += chip->ecc.postpad;
4259 }
4260 }
4261
4262 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04004263 i = mtd->oobsize - (oob - chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +01004264 if (i) {
4265 ret = nand_write_data_op(chip, oob, i, false);
4266 if (ret)
4267 return ret;
4268 }
Josh Wufdbad98d2012-06-25 18:07:45 +08004269
Boris Brezillon25f815f2017-11-30 18:01:30 +01004270 return nand_prog_page_end_op(chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004271}
4272
4273/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004274 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07004275 * @mtd: MTD device structure
4276 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304277 * @offset: address offset within the page
4278 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07004279 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07004280 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07004281 * @page: page number to write
Brian Norris8b6e50c2011-05-25 14:59:01 -07004282 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004283 */
4284static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304285 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004286 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004287{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304288 int status, subpage;
4289
4290 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
4291 chip->ecc.write_subpage)
4292 subpage = offset || (data_len < mtd->writesize);
4293 else
4294 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004295
David Woodhouse956e9442006-09-25 17:12:39 +01004296 if (unlikely(raw))
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004297 status = chip->ecc.write_page_raw(chip, buf, oob_required,
4298 page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304299 else if (subpage)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004300 status = chip->ecc.write_subpage(chip, offset, data_len, buf,
4301 oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01004302 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004303 status = chip->ecc.write_page(chip, buf, oob_required, page);
Josh Wufdbad98d2012-06-25 18:07:45 +08004304
4305 if (status < 0)
4306 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004307
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004308 return 0;
4309}
4310
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004311/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004312 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004313 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07004314 * @oob: oob data buffer
4315 * @len: oob data write length
4316 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004317 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004318static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
4319 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004320{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004321 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01004322 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004323
4324 /*
4325 * Initialise to all 0xFF, to avoid the possibility of left over OOB
4326 * data from a previous OOB read.
4327 */
4328 memset(chip->oob_poi, 0xff, mtd->oobsize);
4329
Florian Fainellif8ac0412010-09-07 13:23:43 +02004330 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004331
Brian Norris0612b9d2011-08-30 18:45:40 -07004332 case MTD_OPS_PLACE_OOB:
4333 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004334 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
4335 return oob + len;
4336
Boris Brezillon846031d2016-02-03 20:11:00 +01004337 case MTD_OPS_AUTO_OOB:
4338 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
4339 ops->ooboffs, len);
4340 BUG_ON(ret);
4341 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004342
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004343 default:
4344 BUG();
4345 }
4346 return NULL;
4347}
4348
Florian Fainellif8ac0412010-09-07 13:23:43 +02004349#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004350
4351/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004352 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004353 * @mtd: MTD device structure
4354 * @to: offset to write to
4355 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004356 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004357 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004358 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004359static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
4360 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004361{
Corentin Labbe73600b62017-09-02 10:49:38 +02004362 int chipnr, realpage, page, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004363 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004364 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02004365
4366 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01004367 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004368
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004369 uint8_t *oob = ops->oobbuf;
4370 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05304371 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07004372 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004373
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004374 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004375 if (!writelen)
4376 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004377
Brian Norris8b6e50c2011-05-25 14:59:01 -07004378 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004379 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004380 pr_notice("%s: attempt to write non page aligned data\n",
4381 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004382 return -EINVAL;
4383 }
4384
Thomas Gleixner29072b92006-09-28 15:38:36 +02004385 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004386
Thomas Gleixner6a930962006-06-28 00:11:45 +02004387 chipnr = (int)(to >> chip->chip_shift);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004388 chip->select_chip(chip, chipnr);
Thomas Gleixner6a930962006-06-28 00:11:45 +02004389
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004390 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004391 if (nand_check_wp(mtd)) {
4392 ret = -EIO;
4393 goto err_out;
4394 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004395
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004396 realpage = (int)(to >> chip->page_shift);
4397 page = realpage & chip->pagemask;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004398
4399 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07004400 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
4401 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004402 chip->pagebuf = -1;
4403
Maxim Levitsky782ce792010-02-22 20:39:36 +02004404 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004405 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
4406 ret = -EINVAL;
4407 goto err_out;
4408 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02004409
Florian Fainellif8ac0412010-09-07 13:23:43 +02004410 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004411 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004412 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04004413 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02004414 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004415
Kamal Dasu66507c72014-05-01 20:51:19 -04004416 if (part_pagewr)
4417 use_bufpoi = 1;
4418 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09004419 use_bufpoi = !virt_addr_valid(buf) ||
4420 !IS_ALIGNED((unsigned long)buf,
4421 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04004422 else
4423 use_bufpoi = 0;
4424
4425 /* Partial page write?, or need to use bounce buffer */
4426 if (use_bufpoi) {
4427 pr_debug("%s: using write bounce buffer for buf@%p\n",
4428 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04004429 if (part_pagewr)
4430 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02004431 chip->pagebuf = -1;
Masahiro Yamadac0313b92017-12-05 17:47:16 +09004432 memset(chip->data_buf, 0xff, mtd->writesize);
4433 memcpy(&chip->data_buf[column], buf, bytes);
4434 wbuf = chip->data_buf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02004435 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004436
Maxim Levitsky782ce792010-02-22 20:39:36 +02004437 if (unlikely(oob)) {
4438 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004439 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004440 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004441 } else {
4442 /* We still need to erase leftover OOB data */
4443 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02004444 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004445
4446 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02004447 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01004448 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004449 if (ret)
4450 break;
4451
4452 writelen -= bytes;
4453 if (!writelen)
4454 break;
4455
Thomas Gleixner29072b92006-09-28 15:38:36 +02004456 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004457 buf += bytes;
4458 realpage++;
4459
4460 page = realpage & chip->pagemask;
4461 /* Check, if we cross a chip boundary */
4462 if (!page) {
4463 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004464 chip->select_chip(chip, -1);
4465 chip->select_chip(chip, chipnr);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004466 }
4467 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004468
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004469 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03004470 if (unlikely(oob))
4471 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004472
4473err_out:
Boris Brezillon758b56f2018-09-06 14:05:24 +02004474 chip->select_chip(chip, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004475 return ret;
4476}
4477
4478/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004479 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004480 * @mtd: MTD device structure
4481 * @to: offset to write to
4482 * @len: number of bytes to write
4483 * @retlen: pointer to variable to store the number of written bytes
4484 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004485 *
4486 * NAND write with ECC. Used when performing writes in interrupt context, this
4487 * may for example be called by mtdoops when writing an oops while in panic.
4488 */
4489static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
4490 size_t *retlen, const uint8_t *buf)
4491{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004492 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor30863e382017-10-30 22:32:45 -05004493 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07004494 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004495 int ret;
4496
Brian Norris8b6e50c2011-05-25 14:59:01 -07004497 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004498 panic_nand_get_device(chip, mtd, FL_WRITING);
4499
Boris Brezillon758b56f2018-09-06 14:05:24 +02004500 chip->select_chip(chip, chipnr);
Brent Taylor30863e382017-10-30 22:32:45 -05004501
4502 /* Wait for the device to get ready */
Boris Brezillonf1d46942018-09-06 14:05:29 +02004503 panic_nand_wait(chip, 400);
Brent Taylor30863e382017-10-30 22:32:45 -05004504
Brian Norris0ec56dc2015-02-28 02:02:30 -08004505 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07004506 ops.len = len;
4507 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08004508 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004509
Brian Norris4a89ff82011-08-30 18:45:45 -07004510 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004511
Brian Norris4a89ff82011-08-30 18:45:45 -07004512 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02004513 return ret;
4514}
4515
4516/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004517 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004518 * @mtd: MTD device structure
4519 * @to: offset to write to
4520 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004522 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004523 */
4524static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
4525 struct mtd_oob_ops *ops)
4526{
Adrian Hunter03736152007-01-31 17:58:29 +02004527 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004528 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Brian Norris289c0522011-07-19 10:06:09 -07004530 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05304531 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Boris BREZILLON29f10582016-03-07 10:46:52 +01004533 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02004534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02004536 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07004537 pr_debug("%s: attempt to write past end of page\n",
4538 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return -EINVAL;
4540 }
4541
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004542 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02004543
4544 /*
4545 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
4546 * of my DiskOnChip 2000 test units) will clear the whole data page too
4547 * if we don't do this. I have no clue why, but I seem to have 'fixed'
4548 * it in the doc2000 driver in August 1999. dwmw2.
4549 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004550 nand_reset(chip, chipnr);
4551
Boris Brezillon758b56f2018-09-06 14:05:24 +02004552 chip->select_chip(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004553
4554 /* Shift to get page */
4555 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
4557 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08004558 if (nand_check_wp(mtd)) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02004559 chip->select_chip(chip, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004560 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08004561 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004564 if (page == chip->pagebuf)
4565 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02004567 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07004568
Brian Norris0612b9d2011-08-30 18:45:40 -07004569 if (ops->mode == MTD_OPS_RAW)
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004570 status = chip->ecc.write_oob_raw(chip, page & chip->pagemask);
Brian Norris9ce244b2011-08-30 18:45:37 -07004571 else
Boris Brezillon767eb6f2018-09-06 14:05:21 +02004572 status = chip->ecc.write_oob(chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004573
Boris Brezillon758b56f2018-09-06 14:05:24 +02004574 chip->select_chip(chip, -1);
Huang Shijieb0bb6902012-11-19 14:43:29 +08004575
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004576 if (status)
4577 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Vitaly Wool70145682006-11-03 18:20:38 +03004579 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Thomas Gleixner7bc33122006-06-20 20:05:05 +02004581 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004584/**
4585 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07004586 * @mtd: MTD device structure
4587 * @to: offset to write to
4588 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004589 */
4590static int nand_write_oob(struct mtd_info *mtd, loff_t to,
4591 struct mtd_oob_ops *ops)
4592{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004593 int ret = -ENOTSUPP;
4594
4595 ops->retlen = 0;
4596
Huang Shijie6a8214a2012-11-19 14:43:30 +08004597 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004598
Florian Fainellif8ac0412010-09-07 13:23:43 +02004599 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07004600 case MTD_OPS_PLACE_OOB:
4601 case MTD_OPS_AUTO_OOB:
4602 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004603 break;
4604
4605 default:
4606 goto out;
4607 }
4608
4609 if (!ops->datbuf)
4610 ret = nand_do_write_oob(mtd, to, ops);
4611 else
4612 ret = nand_do_write_ops(mtd, to, ops);
4613
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004614out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02004615 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 return ret;
4617}
4618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619/**
Brian Norris49c50b92014-05-06 16:02:19 -07004620 * single_erase - [GENERIC] NAND standard block erase command function
Boris Brezillona2098a92018-09-06 14:05:30 +02004621 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004622 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 *
Brian Norris49c50b92014-05-06 16:02:19 -07004624 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 */
Boris Brezillona2098a92018-09-06 14:05:30 +02004626static int single_erase(struct nand_chip *chip, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004628 unsigned int eraseblock;
Brian Norris49c50b92014-05-06 16:02:19 -07004629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 /* Send commands to erase a block */
Boris Brezillon97d90da2017-11-30 18:01:29 +01004631 eraseblock = page >> (chip->phys_erase_shift - chip->page_shift);
Brian Norris49c50b92014-05-06 16:02:19 -07004632
Boris Brezillon97d90da2017-11-30 18:01:29 +01004633 return nand_erase_op(chip, eraseblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634}
4635
4636/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004638 * @mtd: MTD device structure
4639 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004641 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004643static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004645 return nand_erase_nand(mtd_to_nand(mtd), instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/**
Brian Norris7854d3f2011-06-23 14:12:08 -07004649 * nand_erase_nand - [INTERN] erase block(s)
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004650 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004651 * @instr: erase instruction
4652 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004654 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 */
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004656int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004657 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
Boris Brezillone4cdf9c2018-09-06 14:05:35 +02004659 struct mtd_info *mtd = nand_to_mtd(chip);
Adrian Hunter69423d92008-12-10 13:37:21 +00004660 int page, status, pages_per_block, ret, chipnr;
Adrian Hunter69423d92008-12-10 13:37:21 +00004661 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Brian Norris289c0522011-07-19 10:06:09 -07004663 pr_debug("%s: start = 0x%012llx, len = %llu\n",
4664 __func__, (unsigned long long)instr->addr,
4665 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05304667 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004671 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004674 page = (int)(instr->addr >> chip->page_shift);
4675 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004678 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
4680 /* Select the NAND device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004681 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 /* Check, if it is write protected */
4684 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07004685 pr_debug("%s: device is write protected!\n",
4686 __func__);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004687 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 goto erase_exit;
4689 }
4690
4691 /* Loop through the pages */
4692 len = instr->len;
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01004695 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004696 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05304697 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07004698 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
4699 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004700 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 goto erase_exit;
4702 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004703
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004704 /*
4705 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07004706 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004707 */
4708 if (page <= chip->pagebuf && chip->pagebuf <
4709 (page + pages_per_block))
4710 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Boris Brezillona2098a92018-09-06 14:05:30 +02004712 status = chip->erase(chip, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714 /* See if block erase succeeded */
Miquel Raynaleb945552017-11-30 18:01:28 +01004715 if (status) {
Brian Norris289c0522011-07-19 10:06:09 -07004716 pr_debug("%s: failed erase, page 0x%08x\n",
4717 __func__, page);
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004718 ret = -EIO;
Adrian Hunter69423d92008-12-10 13:37:21 +00004719 instr->fail_addr =
4720 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 goto erase_exit;
4722 }
David A. Marlin30f464b2005-01-17 18:35:25 +00004723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03004725 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 page += pages_per_block;
4727
4728 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004729 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 chipnr++;
Boris Brezillon758b56f2018-09-06 14:05:24 +02004731 chip->select_chip(chip, -1);
4732 chip->select_chip(chip, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
4734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Boris Brezillone7bfb3f2018-02-12 22:03:11 +01004736 ret = 0;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004737erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 /* Deselect and wake up anyone waiting on the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02004740 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 nand_release_device(mtd);
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 /* Return more or less happy */
4744 return ret;
4745}
4746
4747/**
4748 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07004749 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004751 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004753static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Brian Norris289c0522011-07-19 10:06:09 -07004755 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
4757 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08004758 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01004760 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761}
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004764 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004765 * @mtd: MTD device structure
4766 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004768static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Archit Taneja9f3e0422016-02-03 14:29:49 +05304770 struct nand_chip *chip = mtd_to_nand(mtd);
4771 int chipnr = (int)(offs >> chip->chip_shift);
4772 int ret;
4773
4774 /* Select the NAND device */
4775 nand_get_device(mtd, FL_READING);
Boris Brezillon758b56f2018-09-06 14:05:24 +02004776 chip->select_chip(chip, chipnr);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304777
4778 ret = nand_block_checkbad(mtd, offs, 0);
4779
Boris Brezillon758b56f2018-09-06 14:05:24 +02004780 chip->select_chip(chip, -1);
Archit Taneja9f3e0422016-02-03 14:29:49 +05304781 nand_release_device(mtd);
4782
4783 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
4786/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004787 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07004788 * @mtd: MTD device structure
4789 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004791static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 int ret;
4794
Florian Fainellif8ac0412010-09-07 13:23:43 +02004795 ret = nand_block_isbad(mtd, ofs);
4796 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07004797 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 if (ret > 0)
4799 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01004800 return ret;
4801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Brian Norris5a0edb22013-07-30 17:52:58 -07004803 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804}
4805
4806/**
Zach Brown56718422017-01-10 13:30:20 -06004807 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
4808 * @mtd: MTD device structure
4809 * @ofs: offset relative to mtd start
4810 * @len: length of mtd
4811 */
4812static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
4813{
4814 struct nand_chip *chip = mtd_to_nand(mtd);
4815 u32 part_start_block;
4816 u32 part_end_block;
4817 u32 part_start_die;
4818 u32 part_end_die;
4819
4820 /*
4821 * max_bb_per_die and blocks_per_die used to determine
4822 * the maximum bad block count.
4823 */
4824 if (!chip->max_bb_per_die || !chip->blocks_per_die)
4825 return -ENOTSUPP;
4826
4827 /* Get the start and end of the partition in erase blocks. */
4828 part_start_block = mtd_div_by_eb(ofs, mtd);
4829 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
4830
4831 /* Get the start and end LUNs of the partition. */
4832 part_start_die = part_start_block / chip->blocks_per_die;
4833 part_end_die = part_end_block / chip->blocks_per_die;
4834
4835 /*
4836 * Look up the bad blocks per unit and multiply by the number of units
4837 * that the partition spans.
4838 */
4839 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
4840}
4841
4842/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004843 * nand_default_set_features- [REPLACEABLE] set NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004844 * @chip: nand chip info structure
4845 * @addr: feature address.
4846 * @subfeature_param: the subfeature parameters, a four bytes array.
4847 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004848static int nand_default_set_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004849 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004850{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004851 return nand_set_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004852}
4853
4854/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004855 * nand_default_get_features- [REPLACEABLE] get NAND chip features
Huang Shijie7db03ec2012-09-13 14:57:52 +08004856 * @chip: nand chip info structure
4857 * @addr: feature address.
4858 * @subfeature_param: the subfeature parameters, a four bytes array.
4859 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004860static int nand_default_get_features(struct nand_chip *chip, int addr,
Miquel Raynalb9587582018-03-19 14:47:19 +01004861 uint8_t *subfeature_param)
Huang Shijie7db03ec2012-09-13 14:57:52 +08004862{
Boris Brezillon97d90da2017-11-30 18:01:29 +01004863 return nand_get_features_op(chip, addr, subfeature_param);
Huang Shijie7db03ec2012-09-13 14:57:52 +08004864}
4865
4866/**
Miquel Raynalb9587582018-03-19 14:47:19 +01004867 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004868 * @chip: nand chip info structure
4869 * @addr: feature address.
4870 * @subfeature_param: the subfeature parameters, a four bytes array.
4871 *
4872 * Should be used by NAND controller drivers that do not support the SET/GET
4873 * FEATURES operations.
4874 */
Boris Brezillonaa36ff22018-09-06 14:05:31 +02004875int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
4876 u8 *subfeature_param)
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004877{
4878 return -ENOTSUPP;
4879}
Miquel Raynalb9587582018-03-19 14:47:19 +01004880EXPORT_SYMBOL(nand_get_set_features_notsupp);
Boris Brezillon4a78cc62017-05-26 17:10:15 +02004881
4882/**
Vitaly Wool962034f2005-09-15 14:58:53 +01004883 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004884 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004885 */
4886static int nand_suspend(struct mtd_info *mtd)
4887{
Huang Shijie6a8214a2012-11-19 14:43:30 +08004888 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01004889}
4890
4891/**
4892 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07004893 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01004894 */
4895static void nand_resume(struct mtd_info *mtd)
4896{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004897 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01004898
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004899 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01004900 nand_release_device(mtd);
4901 else
Brian Norrisd0370212011-07-19 10:06:08 -07004902 pr_err("%s called for a chip which is not in suspended state\n",
4903 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01004904}
4905
Scott Branden72ea4032014-11-20 11:18:05 -08004906/**
4907 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
4908 * prevent further operations
4909 * @mtd: MTD device structure
4910 */
4911static void nand_shutdown(struct mtd_info *mtd)
4912{
Brian Norris9ca641b2015-11-09 16:37:28 -08004913 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08004914}
4915
Brian Norris8b6e50c2011-05-25 14:59:01 -07004916/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004917static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004918{
Boris Brezillon29a198a2016-05-24 20:17:48 +02004919 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004922 if (!chip->chip_delay)
4923 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 /* check, if a user supplied command function given */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02004926 if (!chip->legacy.cmdfunc && !chip->exec_op)
4927 chip->legacy.cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
4929 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004930 if (chip->waitfunc == NULL)
4931 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004933 if (!chip->select_chip)
4934 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07004935
Huang Shijie4204ccc2013-08-16 10:10:07 +08004936 /* set for ONFI nand */
Miquel Raynalb9587582018-03-19 14:47:19 +01004937 if (!chip->set_features)
4938 chip->set_features = nand_default_set_features;
4939 if (!chip->get_features)
4940 chip->get_features = nand_default_get_features;
Huang Shijie4204ccc2013-08-16 10:10:07 +08004941
Brian Norris68e80782013-07-18 01:17:02 -07004942 /* If called twice, pointers that depend on busw may need to be reset */
Boris Brezillon716bbba2018-09-07 00:38:35 +02004943 if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
4944 chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004945 if (!chip->block_bad)
4946 chip->block_bad = nand_block_bad;
4947 if (!chip->block_markbad)
4948 chip->block_markbad = nand_default_block_markbad;
Boris Brezillon716bbba2018-09-07 00:38:35 +02004949 if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
4950 chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
4951 if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
4952 chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
4953 if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
4954 chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004955
4956 if (!chip->controller) {
Miquel Raynal7da45132018-07-17 09:08:02 +02004957 chip->controller = &chip->dummy_controller;
4958 nand_controller_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02004959 }
4960
Masahiro Yamada477544c2017-03-30 17:15:05 +09004961 if (!chip->buf_align)
4962 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004963}
4964
Brian Norris8b6e50c2011-05-25 14:59:01 -07004965/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004966static void sanitize_string(uint8_t *s, size_t len)
4967{
4968 ssize_t i;
4969
Brian Norris8b6e50c2011-05-25 14:59:01 -07004970 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004971 s[len - 1] = 0;
4972
Brian Norris8b6e50c2011-05-25 14:59:01 -07004973 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004974 for (i = 0; i < len - 1; i++) {
4975 if (s[i] < ' ' || s[i] > 127)
4976 s[i] = '?';
4977 }
4978
Brian Norris8b6e50c2011-05-25 14:59:01 -07004979 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004980 strim(s);
4981}
4982
4983static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
4984{
4985 int i;
4986 while (len--) {
4987 crc ^= *p++ << 8;
4988 for (i = 0; i < 8; i++)
4989 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
4990 }
4991
4992 return crc;
4993}
4994
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004995/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004996static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
4997 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08004998{
4999 struct onfi_ext_param_page *ep;
5000 struct onfi_ext_section *s;
5001 struct onfi_ext_ecc_info *ecc;
5002 uint8_t *cursor;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005003 int ret;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005004 int len;
5005 int i;
5006
5007 len = le16_to_cpu(p->ext_param_page_length) * 16;
5008 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07005009 if (!ep)
5010 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005011
5012 /* Send our own NAND_CMD_PARAM. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005013 ret = nand_read_param_page_op(chip, 0, NULL, 0);
5014 if (ret)
5015 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005016
5017 /* Use the Change Read Column command to skip the ONFI param pages. */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005018 ret = nand_change_read_column_op(chip,
5019 sizeof(*p) * p->num_of_param_pages,
5020 ep, len, true);
5021 if (ret)
5022 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005023
Boris Brezillon97d90da2017-11-30 18:01:29 +01005024 ret = -EINVAL;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005025 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
5026 != le16_to_cpu(ep->crc))) {
5027 pr_debug("fail in the CRC.\n");
5028 goto ext_out;
5029 }
5030
5031 /*
5032 * Check the signature.
5033 * Do not strictly follow the ONFI spec, maybe changed in future.
5034 */
5035 if (strncmp(ep->sig, "EPPS", 4)) {
5036 pr_debug("The signature is invalid.\n");
5037 goto ext_out;
5038 }
5039
5040 /* find the ECC section. */
5041 cursor = (uint8_t *)(ep + 1);
5042 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
5043 s = ep->sections + i;
5044 if (s->type == ONFI_SECTION_TYPE_2)
5045 break;
5046 cursor += s->length * 16;
5047 }
5048 if (i == ONFI_EXT_SECTION_MAX) {
5049 pr_debug("We can not find the ECC section.\n");
5050 goto ext_out;
5051 }
5052
5053 /* get the info we want. */
5054 ecc = (struct onfi_ext_ecc_info *)cursor;
5055
Brian Norris4ae7d222013-09-16 18:20:21 -07005056 if (!ecc->codeword_size) {
5057 pr_debug("Invalid codeword size\n");
5058 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005059 }
5060
Brian Norris4ae7d222013-09-16 18:20:21 -07005061 chip->ecc_strength_ds = ecc->ecc_bits;
5062 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07005063 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005064
5065ext_out:
5066 kfree(ep);
5067 return ret;
5068}
5069
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005070/*
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005071 * Recover data with bit-wise majority
5072 */
5073static void nand_bit_wise_majority(const void **srcbufs,
5074 unsigned int nsrcbufs,
5075 void *dstbuf,
5076 unsigned int bufsize)
5077{
5078 int i, j, k;
5079
5080 for (i = 0; i < bufsize; i++) {
5081 u8 val = 0;
5082
5083 for (j = 0; j < 8; j++) {
5084 unsigned int cnt = 0;
5085
5086 for (k = 0; k < nsrcbufs; k++) {
5087 const u8 *srcbuf = srcbufs[k];
5088
5089 if (srcbuf[i] & BIT(j))
5090 cnt++;
5091 }
5092
5093 if (cnt > nsrcbufs / 2)
5094 val |= BIT(j);
5095 }
5096
5097 ((u8 *)dstbuf)[i] = val;
5098 }
5099}
5100
5101/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005102 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005103 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005104static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005105{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005106 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005107 struct nand_onfi_params *p;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005108 struct onfi_params *onfi;
5109 int onfi_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005110 char id[4];
5111 int i, ret, val;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005112
Brian Norris7854d3f2011-06-23 14:12:08 -07005113 /* Try ONFI for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005114 ret = nand_readid_op(chip, 0x20, id, sizeof(id));
5115 if (ret || strncmp(id, "ONFI", 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005116 return 0;
5117
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005118 /* ONFI chip: allocate a buffer to hold its parameter page */
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005119 p = kzalloc((sizeof(*p) * 3), GFP_KERNEL);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005120 if (!p)
5121 return -ENOMEM;
5122
Boris Brezillon97d90da2017-11-30 18:01:29 +01005123 ret = nand_read_param_page_op(chip, 0, NULL, 0);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005124 if (ret) {
5125 ret = 0;
5126 goto free_onfi_param_page;
5127 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005128
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005129 for (i = 0; i < 3; i++) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005130 ret = nand_read_data_op(chip, &p[i], sizeof(*p), true);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005131 if (ret) {
5132 ret = 0;
5133 goto free_onfi_param_page;
5134 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005135
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005136 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) ==
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005137 le16_to_cpu(p->crc)) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005138 if (i)
5139 memcpy(p, &p[i], sizeof(*p));
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005140 break;
5141 }
5142 }
5143
Brian Norrisc7f23a72013-08-13 10:51:55 -07005144 if (i == 3) {
Wan, Jane (Nokia - US/Sunnyvale)39138c12018-05-13 04:30:02 +00005145 const void *srcbufs[3] = {p, p + 1, p + 2};
5146
5147 pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n");
5148 nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p,
5149 sizeof(*p));
5150
5151 if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) !=
5152 le16_to_cpu(p->crc)) {
5153 pr_err("ONFI parameter recovery failed, aborting\n");
5154 goto free_onfi_param_page;
5155 }
Brian Norrisc7f23a72013-08-13 10:51:55 -07005156 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005157
Chris Packham00ce4e02018-06-25 10:44:44 +12005158 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5159 chip->manufacturer.desc->ops->fixup_onfi_param_page)
5160 chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p);
5161
Brian Norris8b6e50c2011-05-25 14:59:01 -07005162 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005163 val = le16_to_cpu(p->revision);
Chris Packham872b71f2018-06-25 10:44:45 +12005164 if (val & ONFI_VERSION_2_3)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005165 onfi_version = 23;
Chris Packham872b71f2018-06-25 10:44:45 +12005166 else if (val & ONFI_VERSION_2_2)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005167 onfi_version = 22;
Chris Packham872b71f2018-06-25 10:44:45 +12005168 else if (val & ONFI_VERSION_2_1)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005169 onfi_version = 21;
Chris Packham872b71f2018-06-25 10:44:45 +12005170 else if (val & ONFI_VERSION_2_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005171 onfi_version = 20;
Chris Packham872b71f2018-06-25 10:44:45 +12005172 else if (val & ONFI_VERSION_1_0)
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005173 onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005174
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005175 if (!onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005176 pr_info("unsupported ONFI version: %d\n", val);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005177 goto free_onfi_param_page;
Brian Norrisb7b1a292010-12-12 00:23:33 -08005178 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005179
5180 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5181 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005182 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5183 if (!chip->parameters.model) {
5184 ret = -ENOMEM;
5185 goto free_onfi_param_page;
5186 }
Brian Norris4355b702013-08-27 18:45:10 -07005187
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005188 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005189
5190 /*
5191 * pages_per_block and blocks_per_lun may not be a power-of-2 size
5192 * (don't ask me who thought of this...). MTD assumes that these
5193 * dimensions will be power-of-2, so just truncate the remaining area.
5194 */
5195 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5196 mtd->erasesize *= mtd->writesize;
5197
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005198 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07005199
5200 /* See erasesize comment */
5201 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01005202 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08005203 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08005204
Zach Brown34da5f52017-01-10 13:30:21 -06005205 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
5206 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
5207
Miquel Raynala97421c2018-03-19 14:47:27 +01005208 if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005209 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005210
Huang Shijie10c86ba2013-05-17 11:17:26 +08005211 if (p->ecc_bits != 0xff) {
5212 chip->ecc_strength_ds = p->ecc_bits;
5213 chip->ecc_step_ds = 512;
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005214 } else if (onfi_version >= 21 &&
Miquel Raynala97421c2018-03-19 14:47:27 +01005215 (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005216
5217 /*
5218 * The nand_flash_detect_ext_param_page() uses the
5219 * Change Read Column command which maybe not supported
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005220 * by the chip->legacy.cmdfunc. So try to update the
5221 * chip->legacy.cmdfunc now. We do not replace user supplied
5222 * command function.
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005223 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005224 if (mtd->writesize > 512 &&
5225 chip->legacy.cmdfunc == nand_command)
5226 chip->legacy.cmdfunc = nand_command_lp;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08005227
5228 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005229 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07005230 pr_warn("Failed to detect ONFI extended param page\n");
5231 } else {
5232 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08005233 }
5234
Miquel Raynalf4531b22018-03-19 14:47:26 +01005235 /* Save some parameters from the parameter page for future use */
Miquel Raynal789157e2018-03-19 14:47:28 +01005236 if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) {
Miquel Raynalf4531b22018-03-19 14:47:26 +01005237 chip->parameters.supports_set_get_features = true;
Miquel Raynal789157e2018-03-19 14:47:28 +01005238 bitmap_set(chip->parameters.get_feature_list,
5239 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5240 bitmap_set(chip->parameters.set_feature_list,
5241 ONFI_FEATURE_ADDR_TIMING_MODE, 1);
5242 }
Miquel Raynalf4531b22018-03-19 14:47:26 +01005243
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005244 onfi = kzalloc(sizeof(*onfi), GFP_KERNEL);
5245 if (!onfi) {
5246 ret = -ENOMEM;
5247 goto free_model;
5248 }
5249
5250 onfi->version = onfi_version;
5251 onfi->tPROG = le16_to_cpu(p->t_prog);
5252 onfi->tBERS = le16_to_cpu(p->t_bers);
5253 onfi->tR = le16_to_cpu(p->t_r);
5254 onfi->tCCS = le16_to_cpu(p->t_ccs);
5255 onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode);
5256 onfi->vendor_revision = le16_to_cpu(p->vendor_revision);
5257 memcpy(onfi->vendor, p->vendor, sizeof(p->vendor));
5258 chip->parameters.onfi = onfi;
5259
5260 /* Identification done, free the full ONFI parameter page and exit */
5261 kfree(p);
5262
5263 return 1;
5264
5265free_model:
5266 kfree(chip->parameters.model);
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005267free_onfi_param_page:
5268 kfree(p);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02005269
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005270 return ret;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005271}
5272
5273/*
Huang Shijie91361812014-02-21 13:39:40 +08005274 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
5275 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005276static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08005277{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005278 struct mtd_info *mtd = nand_to_mtd(chip);
Miquel Raynal480139d2018-03-19 14:47:30 +01005279 struct nand_jedec_params *p;
Huang Shijie91361812014-02-21 13:39:40 +08005280 struct jedec_ecc_info *ecc;
Miquel Raynal480139d2018-03-19 14:47:30 +01005281 int jedec_version = 0;
Boris Brezillon97d90da2017-11-30 18:01:29 +01005282 char id[5];
5283 int i, val, ret;
Huang Shijie91361812014-02-21 13:39:40 +08005284
5285 /* Try JEDEC for unknown chip or LP */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005286 ret = nand_readid_op(chip, 0x40, id, sizeof(id));
5287 if (ret || strncmp(id, "JEDEC", sizeof(id)))
Huang Shijie91361812014-02-21 13:39:40 +08005288 return 0;
5289
Miquel Raynal480139d2018-03-19 14:47:30 +01005290 /* JEDEC chip: allocate a buffer to hold its parameter page */
5291 p = kzalloc(sizeof(*p), GFP_KERNEL);
5292 if (!p)
5293 return -ENOMEM;
5294
Boris Brezillon97d90da2017-11-30 18:01:29 +01005295 ret = nand_read_param_page_op(chip, 0x40, NULL, 0);
Miquel Raynal480139d2018-03-19 14:47:30 +01005296 if (ret) {
5297 ret = 0;
5298 goto free_jedec_param_page;
5299 }
Boris Brezillon97d90da2017-11-30 18:01:29 +01005300
Huang Shijie91361812014-02-21 13:39:40 +08005301 for (i = 0; i < 3; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005302 ret = nand_read_data_op(chip, p, sizeof(*p), true);
Miquel Raynal480139d2018-03-19 14:47:30 +01005303 if (ret) {
5304 ret = 0;
5305 goto free_jedec_param_page;
5306 }
Huang Shijie91361812014-02-21 13:39:40 +08005307
5308 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
5309 le16_to_cpu(p->crc))
5310 break;
5311 }
5312
5313 if (i == 3) {
5314 pr_err("Could not find valid JEDEC parameter page; aborting\n");
Miquel Raynal480139d2018-03-19 14:47:30 +01005315 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005316 }
5317
5318 /* Check version */
5319 val = le16_to_cpu(p->revision);
5320 if (val & (1 << 2))
Miquel Raynal480139d2018-03-19 14:47:30 +01005321 jedec_version = 10;
Huang Shijie91361812014-02-21 13:39:40 +08005322 else if (val & (1 << 1))
Miquel Raynal480139d2018-03-19 14:47:30 +01005323 jedec_version = 1; /* vendor specific version */
Huang Shijie91361812014-02-21 13:39:40 +08005324
Miquel Raynal480139d2018-03-19 14:47:30 +01005325 if (!jedec_version) {
Huang Shijie91361812014-02-21 13:39:40 +08005326 pr_info("unsupported JEDEC version: %d\n", val);
Miquel Raynal480139d2018-03-19 14:47:30 +01005327 goto free_jedec_param_page;
Huang Shijie91361812014-02-21 13:39:40 +08005328 }
5329
5330 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
5331 sanitize_string(p->model, sizeof(p->model));
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005332 chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5333 if (!chip->parameters.model) {
5334 ret = -ENOMEM;
5335 goto free_jedec_param_page;
5336 }
Huang Shijie91361812014-02-21 13:39:40 +08005337
5338 mtd->writesize = le32_to_cpu(p->byte_per_page);
5339
5340 /* Please reference to the comment for nand_flash_detect_onfi. */
5341 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
5342 mtd->erasesize *= mtd->writesize;
5343
5344 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
5345
5346 /* Please reference to the comment for nand_flash_detect_onfi. */
5347 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
5348 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
5349 chip->bits_per_cell = p->bits_per_cell;
5350
Miquel Raynal480139d2018-03-19 14:47:30 +01005351 if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02005352 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08005353
5354 /* ECC info */
5355 ecc = &p->ecc_info[0];
5356
5357 if (ecc->codeword_size >= 9) {
5358 chip->ecc_strength_ds = ecc->ecc_bits;
5359 chip->ecc_step_ds = 1 << ecc->codeword_size;
5360 } else {
5361 pr_warn("Invalid codeword size\n");
5362 }
5363
Miquel Raynal480139d2018-03-19 14:47:30 +01005364free_jedec_param_page:
5365 kfree(p);
5366 return ret;
Huang Shijie91361812014-02-21 13:39:40 +08005367}
5368
5369/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07005370 * nand_id_has_period - Check if an ID string has a given wraparound period
5371 * @id_data: the ID string
5372 * @arrlen: the length of the @id_data array
5373 * @period: the period of repitition
5374 *
5375 * Check if an ID string is repeated within a given sequence of bytes at
5376 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08005377 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07005378 * if the repetition has a period of @period; otherwise, returns zero.
5379 */
5380static int nand_id_has_period(u8 *id_data, int arrlen, int period)
5381{
5382 int i, j;
5383 for (i = 0; i < period; i++)
5384 for (j = i + period; j < arrlen; j += period)
5385 if (id_data[i] != id_data[j])
5386 return 0;
5387 return 1;
5388}
5389
5390/*
5391 * nand_id_len - Get the length of an ID string returned by CMD_READID
5392 * @id_data: the ID string
5393 * @arrlen: the length of the @id_data array
5394
5395 * Returns the length of the ID string, according to known wraparound/trailing
5396 * zero patterns. If no pattern exists, returns the length of the array.
5397 */
5398static int nand_id_len(u8 *id_data, int arrlen)
5399{
5400 int last_nonzero, period;
5401
5402 /* Find last non-zero byte */
5403 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
5404 if (id_data[last_nonzero])
5405 break;
5406
5407 /* All zeros */
5408 if (last_nonzero < 0)
5409 return 0;
5410
5411 /* Calculate wraparound period */
5412 for (period = 1; period < arrlen; period++)
5413 if (nand_id_has_period(id_data, arrlen, period))
5414 break;
5415
5416 /* There's a repeated pattern */
5417 if (period < arrlen)
5418 return period;
5419
5420 /* There are trailing zeros */
5421 if (last_nonzero < arrlen - 1)
5422 return last_nonzero + 1;
5423
5424 /* No pattern detected */
5425 return arrlen;
5426}
5427
Huang Shijie7db906b2013-09-25 14:58:11 +08005428/* Extract the bits of per cell from the 3rd byte of the extended ID */
5429static int nand_get_bits_per_cell(u8 cellinfo)
5430{
5431 int bits;
5432
5433 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
5434 bits >>= NAND_CI_CELLTYPE_SHIFT;
5435 return bits + 1;
5436}
5437
Brian Norrise3b88bd2012-09-24 20:40:52 -07005438/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005439 * Many new NAND share similar device ID codes, which represent the size of the
5440 * chip. The rest of the parameters must be decoded according to generic or
5441 * manufacturer-specific "extended ID" decoding patterns.
5442 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005443void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005444{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005445 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02005446 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005447 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005448 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08005449 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005450 /* The 4th id byte is the important one */
5451 extid = id_data[3];
5452
Boris Brezillon01389b62016-06-08 10:30:18 +02005453 /* Calc pagesize */
5454 mtd->writesize = 1024 << (extid & 0x03);
5455 extid >>= 2;
5456 /* Calc oobsize */
5457 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
5458 extid >>= 2;
5459 /* Calc blocksize. Blocksize is multiples of 64KiB */
5460 mtd->erasesize = (64 * 1024) << (extid & 0x03);
5461 extid >>= 2;
5462 /* Get buswidth information */
5463 if (extid & 0x1)
5464 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005465}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005466EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07005467
5468/*
Brian Norrisf23a4812012-09-24 20:40:51 -07005469 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
5470 * decodes a matching ID table entry and assigns the MTD size parameters for
5471 * the chip.
5472 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005473static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07005474{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005475 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07005476
5477 mtd->erasesize = type->erasesize;
5478 mtd->writesize = type->pagesize;
5479 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07005480
Huang Shijie1c195e92013-09-25 14:58:12 +08005481 /* All legacy ID NAND are small-page, SLC */
5482 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07005483}
5484
5485/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07005486 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
5487 * heuristic patterns using various detected parameters (e.g., manufacturer,
5488 * page size, cell-type information).
5489 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02005490static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07005491{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005492 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005493
5494 /* Set the bad block position */
5495 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
5496 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
5497 else
5498 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07005499}
5500
Huang Shijieec6e87e2013-03-15 11:01:00 +08005501static inline bool is_full_id_nand(struct nand_flash_dev *type)
5502{
5503 return type->id_len;
5504}
5505
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005506static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02005507 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08005508{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005509 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02005510 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005511
Huang Shijieec6e87e2013-03-15 11:01:00 +08005512 if (!strncmp(type->id, id_data, type->id_len)) {
5513 mtd->writesize = type->pagesize;
5514 mtd->erasesize = type->erasesize;
5515 mtd->oobsize = type->oobsize;
5516
Huang Shijie7db906b2013-09-25 14:58:11 +08005517 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08005518 chip->chipsize = (uint64_t)type->chipsize << 20;
5519 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08005520 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
5521 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02005522 chip->onfi_timing_mode_default =
5523 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005524
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005525 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5526 if (!chip->parameters.model)
5527 return false;
Cai Zhiyong092b6a12013-12-25 21:19:21 +08005528
Huang Shijieec6e87e2013-03-15 11:01:00 +08005529 return true;
5530 }
5531 return false;
5532}
5533
Brian Norris7e74c2d2012-09-24 20:40:49 -07005534/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005535 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
5536 * compliant and does not have a full-id or legacy-id entry in the nand_ids
5537 * table.
5538 */
5539static void nand_manufacturer_detect(struct nand_chip *chip)
5540{
5541 /*
5542 * Try manufacturer detection if available and use
5543 * nand_decode_ext_id() otherwise.
5544 */
5545 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005546 chip->manufacturer.desc->ops->detect) {
5547 /* The 3rd id byte holds MLC / multichip data */
5548 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005549 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005550 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005551 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02005552 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005553}
5554
5555/*
5556 * Manufacturer initialization. This function is called for all NANDs including
5557 * ONFI and JEDEC compliant ones.
5558 * Manufacturer drivers should put all their specific initialization code in
5559 * their ->init() hook.
5560 */
5561static int nand_manufacturer_init(struct nand_chip *chip)
5562{
5563 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
5564 !chip->manufacturer.desc->ops->init)
5565 return 0;
5566
5567 return chip->manufacturer.desc->ops->init(chip);
5568}
5569
5570/*
5571 * Manufacturer cleanup. This function is called for all NANDs including
5572 * ONFI and JEDEC compliant ones.
5573 * Manufacturer drivers should put all their specific cleanup code in their
5574 * ->cleanup() hook.
5575 */
5576static void nand_manufacturer_cleanup(struct nand_chip *chip)
5577{
5578 /* Release manufacturer private data */
5579 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
5580 chip->manufacturer.desc->ops->cleanup)
5581 chip->manufacturer.desc->ops->cleanup(chip);
5582}
5583
5584/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07005585 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005586 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005587static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005588{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005589 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02005590 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon97d90da2017-11-30 18:01:29 +01005591 int busw, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005592 u8 *id_data = chip->id.data;
5593 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Karl Beldanef89a882008-09-15 14:37:29 +02005595 /*
5596 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07005597 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02005598 */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005599 ret = nand_reset(chip, 0);
5600 if (ret)
5601 return ret;
Boris Brezillon73f907f2016-10-24 16:46:20 +02005602
5603 /* Select the device */
Boris Brezillon758b56f2018-09-06 14:05:24 +02005604 chip->select_chip(chip, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02005605
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005607 ret = nand_readid_op(chip, 0, id_data, 2);
5608 if (ret)
5609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005612 maf_id = id_data[0];
5613 dev_id = id_data[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Brian Norris8b6e50c2011-05-25 14:59:01 -07005615 /*
5616 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01005617 * interface concerns can cause random data which looks like a
5618 * possibly credible NAND flash to appear. If the two results do
5619 * not match, ignore the device completely.
5620 */
5621
Brian Norris4aef9b72012-09-24 20:40:48 -07005622 /* Read entire ID string */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005623 ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data));
5624 if (ret)
5625 return ret;
Ben Dooksed8165c2008-04-14 14:58:58 +01005626
Boris Brezillon7f501f02016-05-24 19:20:05 +02005627 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03005628 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005629 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005630 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01005631 }
5632
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02005633 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02005634
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005635 /* Try to identify manufacturer */
5636 manufacturer = nand_get_manufacturer(maf_id);
5637 chip->manufacturer.desc = manufacturer;
5638
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005639 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00005640 type = nand_flash_ids;
5641
Boris Brezillon29a198a2016-05-24 20:17:48 +02005642 /*
5643 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
5644 * override it.
5645 * This is required to make sure initial NAND bus width set by the
5646 * NAND controller driver is coherent with the real NAND bus width
5647 * (extracted by auto-detection code).
5648 */
5649 busw = chip->options & NAND_BUSWIDTH_16;
5650
5651 /*
5652 * The flag is only set (never cleared), reset it to its default value
5653 * before starting auto-detection.
5654 */
5655 chip->options &= ~NAND_BUSWIDTH_16;
5656
Huang Shijieec6e87e2013-03-15 11:01:00 +08005657 for (; type->name != NULL; type++) {
5658 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005659 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08005660 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02005661 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07005662 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08005663 }
5664 }
David Woodhouse5e81e882010-02-26 18:32:56 +00005665
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005666 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09005667 /* Check if the chip is ONFI compliant */
Miquel Raynalbd0b6432018-03-19 14:47:31 +01005668 ret = nand_flash_detect_onfi(chip);
5669 if (ret < 0)
5670 return ret;
5671 else if (ret)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02005672 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08005673
5674 /* Check if the chip is JEDEC compliant */
Miquel Raynal480139d2018-03-19 14:47:30 +01005675 ret = nand_flash_detect_jedec(chip);
5676 if (ret < 0)
5677 return ret;
5678 else if (ret)
Huang Shijie91361812014-02-21 13:39:40 +08005679 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005680 }
5681
David Woodhouse5e81e882010-02-26 18:32:56 +00005682 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005683 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005684
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005685 chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5686 if (!chip->parameters.model)
5687 return -ENOMEM;
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02005688
Adrian Hunter69423d92008-12-10 13:37:21 +00005689 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005690
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005691 if (!type->pagesize)
5692 nand_manufacturer_detect(chip);
5693 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02005694 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005695
Brian Norrisbf7a01b2012-07-13 09:28:24 -07005696 /* Get chip options */
5697 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005698
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005699ident_done:
Miquel Raynalf4531b22018-03-19 14:47:26 +01005700 if (!mtd->name)
5701 mtd->name = chip->parameters.model;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02005702
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005703 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02005704 WARN_ON(busw & NAND_BUSWIDTH_16);
5705 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01005706 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
5707 /*
5708 * Check, if buswidth is correct. Hardware drivers should set
5709 * chip correct!
5710 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03005711 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005712 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01005713 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5714 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02005715 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
5716 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005717 ret = -EINVAL;
5718
5719 goto free_detect_allocation;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005720 }
5721
Boris Brezillon7f501f02016-05-24 19:20:05 +02005722 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07005723
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005724 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005725 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07005726 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005727 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005728
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005729 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005730 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00005731 if (chip->chipsize & 0xffffffff)
5732 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005733 else {
5734 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
5735 chip->chip_shift += 32 - 1;
5736 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005737
Masahiro Yamada14157f82017-09-13 11:05:50 +09005738 if (chip->chip_shift - chip->page_shift > 16)
5739 chip->options |= NAND_ROW_ADDR_3;
5740
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03005741 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07005742 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005743
Brian Norris8b6e50c2011-05-25 14:59:01 -07005744 /* Do not replace user supplied command function! */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005745 if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
5746 chip->legacy.cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005747
Ezequiel Garcia20171642013-11-25 08:30:31 -03005748 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02005749 maf_id, dev_id);
Miquel Raynalf4531b22018-03-19 14:47:26 +01005750 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
5751 chip->parameters.model);
Rafał Miłecki3755a992014-10-21 00:01:04 +02005752 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08005753 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02005754 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005755 return 0;
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02005756
5757free_detect_allocation:
5758 kfree(chip->parameters.model);
5759
5760 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005761}
5762
Boris Brezillond48f62b2016-04-01 14:54:32 +02005763static const char * const nand_ecc_modes[] = {
5764 [NAND_ECC_NONE] = "none",
5765 [NAND_ECC_SOFT] = "soft",
5766 [NAND_ECC_HW] = "hw",
5767 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
5768 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02005769 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02005770};
5771
5772static int of_get_nand_ecc_mode(struct device_node *np)
5773{
5774 const char *pm;
5775 int err, i;
5776
5777 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5778 if (err < 0)
5779 return err;
5780
5781 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
5782 if (!strcasecmp(pm, nand_ecc_modes[i]))
5783 return i;
5784
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02005785 /*
5786 * For backward compatibility we support few obsoleted values that don't
5787 * have their mappings into nand_ecc_modes_t anymore (they were merged
5788 * with other enums).
5789 */
5790 if (!strcasecmp(pm, "soft_bch"))
5791 return NAND_ECC_SOFT;
5792
Boris Brezillond48f62b2016-04-01 14:54:32 +02005793 return -ENODEV;
5794}
5795
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005796static const char * const nand_ecc_algos[] = {
5797 [NAND_ECC_HAMMING] = "hamming",
5798 [NAND_ECC_BCH] = "bch",
Stefan Agnerf308d732018-06-24 23:27:22 +02005799 [NAND_ECC_RS] = "rs",
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005800};
5801
Boris Brezillond48f62b2016-04-01 14:54:32 +02005802static int of_get_nand_ecc_algo(struct device_node *np)
5803{
5804 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005805 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02005806
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02005807 err = of_property_read_string(np, "nand-ecc-algo", &pm);
5808 if (!err) {
5809 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
5810 if (!strcasecmp(pm, nand_ecc_algos[i]))
5811 return i;
5812 return -ENODEV;
5813 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02005814
5815 /*
5816 * For backward compatibility we also read "nand-ecc-mode" checking
5817 * for some obsoleted values that were specifying ECC algorithm.
5818 */
5819 err = of_property_read_string(np, "nand-ecc-mode", &pm);
5820 if (err < 0)
5821 return err;
5822
5823 if (!strcasecmp(pm, "soft"))
5824 return NAND_ECC_HAMMING;
5825 else if (!strcasecmp(pm, "soft_bch"))
5826 return NAND_ECC_BCH;
5827
5828 return -ENODEV;
5829}
5830
5831static int of_get_nand_ecc_step_size(struct device_node *np)
5832{
5833 int ret;
5834 u32 val;
5835
5836 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
5837 return ret ? ret : val;
5838}
5839
5840static int of_get_nand_ecc_strength(struct device_node *np)
5841{
5842 int ret;
5843 u32 val;
5844
5845 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
5846 return ret ? ret : val;
5847}
5848
5849static int of_get_nand_bus_width(struct device_node *np)
5850{
5851 u32 val;
5852
5853 if (of_property_read_u32(np, "nand-bus-width", &val))
5854 return 8;
5855
5856 switch (val) {
5857 case 8:
5858 case 16:
5859 return val;
5860 default:
5861 return -EIO;
5862 }
5863}
5864
5865static bool of_get_nand_on_flash_bbt(struct device_node *np)
5866{
5867 return of_property_read_bool(np, "nand-on-flash-bbt");
5868}
5869
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005870static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08005871{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005872 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01005873 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08005874
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005875 if (!dn)
5876 return 0;
5877
Brian Norris5844fee2015-01-23 00:22:27 -08005878 if (of_get_nand_bus_width(dn) == 16)
5879 chip->options |= NAND_BUSWIDTH_16;
5880
Stefan Agnerf922bd72018-06-24 23:27:23 +02005881 if (of_property_read_bool(dn, "nand-is-boot-medium"))
5882 chip->options |= NAND_IS_BOOT_MEDIUM;
5883
Brian Norris5844fee2015-01-23 00:22:27 -08005884 if (of_get_nand_on_flash_bbt(dn))
5885 chip->bbt_options |= NAND_BBT_USE_FLASH;
5886
5887 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01005888 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08005889 ecc_strength = of_get_nand_ecc_strength(dn);
5890 ecc_step = of_get_nand_ecc_step_size(dn);
5891
Brian Norris5844fee2015-01-23 00:22:27 -08005892 if (ecc_mode >= 0)
5893 chip->ecc.mode = ecc_mode;
5894
Rafał Miłecki79082452016-03-23 11:19:02 +01005895 if (ecc_algo >= 0)
5896 chip->ecc.algo = ecc_algo;
5897
Brian Norris5844fee2015-01-23 00:22:27 -08005898 if (ecc_strength >= 0)
5899 chip->ecc.strength = ecc_strength;
5900
5901 if (ecc_step > 0)
5902 chip->ecc.size = ecc_step;
5903
Boris Brezillonba78ee02016-06-08 17:04:22 +02005904 if (of_property_read_bool(dn, "nand-ecc-maximize"))
5905 chip->ecc.options |= NAND_ECC_MAXIMIZE;
5906
Brian Norris5844fee2015-01-23 00:22:27 -08005907 return 0;
5908}
5909
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005910/**
Miquel Raynal98732da2018-07-25 15:31:50 +02005911 * nand_scan_ident - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02005912 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005913 * @maxchips: number of chips to scan for
5914 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005915 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005916 * This is the first phase of the normal nand_scan() function. It reads the
5917 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005918 *
Miquel Raynal98732da2018-07-25 15:31:50 +02005919 * This helper used to be called directly from controller drivers that needed
5920 * to tweak some ECC-related parameters before nand_scan_tail(). This separation
5921 * prevented dynamic allocations during this phase which was unconvenient and
5922 * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005923 */
Boris Brezillon871a4072018-08-04 22:59:22 +02005924static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal98732da2018-07-25 15:31:50 +02005925 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005926{
Boris Brezillon00ad3782018-09-06 14:05:14 +02005927 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon871a4072018-08-04 22:59:22 +02005928 int nand_maf_id, nand_dev_id;
5929 unsigned int i;
Brian Norris5844fee2015-01-23 00:22:27 -08005930 int ret;
5931
Miquel Raynal17fa8042017-11-30 18:01:31 +01005932 /* Enforce the right timings for reset/detection */
5933 onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0);
5934
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01005935 ret = nand_dt_init(chip);
5936 if (ret)
5937 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005938
Brian Norrisf7a8e382016-01-05 10:39:45 -08005939 if (!mtd->name && mtd->dev.parent)
5940 mtd->name = dev_name(mtd->dev.parent);
5941
Miquel Raynal8878b122017-11-09 14:16:45 +01005942 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005943 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
5944 * not populated.
Miquel Raynal8878b122017-11-09 14:16:45 +01005945 */
5946 if (!chip->exec_op) {
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005947 /*
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005948 * Default functions assigned for ->legacy.cmdfunc() and
5949 * ->select_chip() both expect ->legacy.cmd_ctrl() to be
5950 * populated.
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005951 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02005952 if ((!chip->legacy.cmdfunc || !chip->select_chip) &&
5953 !chip->legacy.cmd_ctrl) {
5954 pr_err("->legacy.cmd_ctrl() should be provided\n");
Miquel Raynal8878b122017-11-09 14:16:45 +01005955 return -EINVAL;
5956 }
Andrey Smirnov76fe3342016-07-21 14:59:20 -07005957 }
Miquel Raynal8878b122017-11-09 14:16:45 +01005958
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005959 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02005960 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005961
5962 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02005963 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005964 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00005965 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07005966 pr_warn("No NAND device found\n");
Boris Brezillon758b56f2018-09-06 14:05:24 +02005967 chip->select_chip(chip, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09005968 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 }
5970
Boris Brezillon7f501f02016-05-24 19:20:05 +02005971 nand_maf_id = chip->id.data[0];
5972 nand_dev_id = chip->id.data[1];
5973
Boris Brezillon758b56f2018-09-06 14:05:24 +02005974 chip->select_chip(chip, -1);
Huang Shijie07300162012-11-09 16:23:45 +08005975
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005976 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01005977 for (i = 1; i < maxchips; i++) {
Boris Brezillon97d90da2017-11-30 18:01:29 +01005978 u8 id[2];
5979
Karl Beldanef89a882008-09-15 14:37:29 +02005980 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02005981 nand_reset(chip, i);
5982
Boris Brezillon758b56f2018-09-06 14:05:24 +02005983 chip->select_chip(chip, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 /* Send the command for reading device ID */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005985 nand_readid_op(chip, 0, id, sizeof(id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 /* Read manufacturer and device IDs */
Boris Brezillon97d90da2017-11-30 18:01:29 +01005987 if (nand_maf_id != id[0] || nand_dev_id != id[1]) {
Boris Brezillon758b56f2018-09-06 14:05:24 +02005988 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 break;
Huang Shijie07300162012-11-09 16:23:45 +08005990 }
Boris Brezillon758b56f2018-09-06 14:05:24 +02005991 chip->select_chip(chip, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 }
5993 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03005994 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005997 chip->numchips = i;
5998 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
David Woodhouse3b85c322006-09-25 17:06:53 +01006000 return 0;
6001}
6002
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006003static void nand_scan_ident_cleanup(struct nand_chip *chip)
6004{
6005 kfree(chip->parameters.model);
Miquel Raynal3d3fe3c2018-07-25 15:31:52 +02006006 kfree(chip->parameters.onfi);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006007}
6008
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006009static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
6010{
6011 struct nand_chip *chip = mtd_to_nand(mtd);
6012 struct nand_ecc_ctrl *ecc = &chip->ecc;
6013
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006014 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006015 return -EINVAL;
6016
6017 switch (ecc->algo) {
6018 case NAND_ECC_HAMMING:
6019 ecc->calculate = nand_calculate_ecc;
6020 ecc->correct = nand_correct_data;
6021 ecc->read_page = nand_read_page_swecc;
6022 ecc->read_subpage = nand_read_subpage;
6023 ecc->write_page = nand_write_page_swecc;
6024 ecc->read_page_raw = nand_read_page_raw;
6025 ecc->write_page_raw = nand_write_page_raw;
6026 ecc->read_oob = nand_read_oob_std;
6027 ecc->write_oob = nand_write_oob_std;
6028 if (!ecc->size)
6029 ecc->size = 256;
6030 ecc->bytes = 3;
6031 ecc->strength = 1;
6032 return 0;
6033 case NAND_ECC_BCH:
6034 if (!mtd_nand_has_bch()) {
6035 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
6036 return -EINVAL;
6037 }
6038 ecc->calculate = nand_bch_calculate_ecc;
6039 ecc->correct = nand_bch_correct_data;
6040 ecc->read_page = nand_read_page_swecc;
6041 ecc->read_subpage = nand_read_subpage;
6042 ecc->write_page = nand_write_page_swecc;
6043 ecc->read_page_raw = nand_read_page_raw;
6044 ecc->write_page_raw = nand_write_page_raw;
6045 ecc->read_oob = nand_read_oob_std;
6046 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02006047
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006048 /*
6049 * Board driver should supply ecc.size and ecc.strength
6050 * values to select how many bits are correctable.
6051 * Otherwise, default to 4 bits for large page devices.
6052 */
6053 if (!ecc->size && (mtd->oobsize >= 64)) {
6054 ecc->size = 512;
6055 ecc->strength = 4;
6056 }
6057
6058 /*
6059 * if no ecc placement scheme was provided pickup the default
6060 * large page one.
6061 */
6062 if (!mtd->ooblayout) {
6063 /* handle large page devices only */
6064 if (mtd->oobsize < 64) {
6065 WARN(1, "OOB layout is required when using software BCH on small pages\n");
6066 return -EINVAL;
6067 }
6068
6069 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02006070
6071 }
6072
6073 /*
6074 * We can only maximize ECC config when the default layout is
6075 * used, otherwise we don't know how many bytes can really be
6076 * used.
6077 */
6078 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
6079 ecc->options & NAND_ECC_MAXIMIZE) {
6080 int steps, bytes;
6081
6082 /* Always prefer 1k blocks over 512bytes ones */
6083 ecc->size = 1024;
6084 steps = mtd->writesize / ecc->size;
6085
6086 /* Reserve 2 bytes for the BBM */
6087 bytes = (mtd->oobsize - 2) / steps;
6088 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006089 }
6090
6091 /* See nand_bch_init() for details. */
6092 ecc->bytes = 0;
6093 ecc->priv = nand_bch_init(mtd);
6094 if (!ecc->priv) {
6095 WARN(1, "BCH ECC initialization failed!\n");
6096 return -EINVAL;
6097 }
6098 return 0;
6099 default:
6100 WARN(1, "Unsupported ECC algorithm!\n");
6101 return -EINVAL;
6102 }
6103}
6104
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006105/**
6106 * nand_check_ecc_caps - check the sanity of preset ECC settings
6107 * @chip: nand chip info structure
6108 * @caps: ECC caps info structure
6109 * @oobavail: OOB size that the ECC engine can use
6110 *
6111 * When ECC step size and strength are already set, check if they are supported
6112 * by the controller and the calculated ECC bytes fit within the chip's OOB.
6113 * On success, the calculated ECC bytes is set.
6114 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306115static int
6116nand_check_ecc_caps(struct nand_chip *chip,
6117 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006118{
6119 struct mtd_info *mtd = nand_to_mtd(chip);
6120 const struct nand_ecc_step_info *stepinfo;
6121 int preset_step = chip->ecc.size;
6122 int preset_strength = chip->ecc.strength;
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306123 int ecc_bytes, nsteps = mtd->writesize / preset_step;
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006124 int i, j;
6125
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006126 for (i = 0; i < caps->nstepinfos; i++) {
6127 stepinfo = &caps->stepinfos[i];
6128
6129 if (stepinfo->stepsize != preset_step)
6130 continue;
6131
6132 for (j = 0; j < stepinfo->nstrengths; j++) {
6133 if (stepinfo->strengths[j] != preset_strength)
6134 continue;
6135
6136 ecc_bytes = caps->calc_ecc_bytes(preset_step,
6137 preset_strength);
6138 if (WARN_ON_ONCE(ecc_bytes < 0))
6139 return ecc_bytes;
6140
6141 if (ecc_bytes * nsteps > oobavail) {
6142 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
6143 preset_step, preset_strength);
6144 return -ENOSPC;
6145 }
6146
6147 chip->ecc.bytes = ecc_bytes;
6148
6149 return 0;
6150 }
6151 }
6152
6153 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
6154 preset_step, preset_strength);
6155
6156 return -ENOTSUPP;
6157}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006158
6159/**
6160 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
6161 * @chip: nand chip info structure
6162 * @caps: ECC engine caps info structure
6163 * @oobavail: OOB size that the ECC engine can use
6164 *
6165 * If a chip's ECC requirement is provided, try to meet it with the least
6166 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
6167 * On success, the chosen ECC settings are set.
6168 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306169static int
6170nand_match_ecc_req(struct nand_chip *chip,
6171 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006172{
6173 struct mtd_info *mtd = nand_to_mtd(chip);
6174 const struct nand_ecc_step_info *stepinfo;
6175 int req_step = chip->ecc_step_ds;
6176 int req_strength = chip->ecc_strength_ds;
6177 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
6178 int best_step, best_strength, best_ecc_bytes;
6179 int best_ecc_bytes_total = INT_MAX;
6180 int i, j;
6181
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006182 /* No information provided by the NAND chip */
6183 if (!req_step || !req_strength)
6184 return -ENOTSUPP;
6185
6186 /* number of correctable bits the chip requires in a page */
6187 req_corr = mtd->writesize / req_step * req_strength;
6188
6189 for (i = 0; i < caps->nstepinfos; i++) {
6190 stepinfo = &caps->stepinfos[i];
6191 step_size = stepinfo->stepsize;
6192
6193 for (j = 0; j < stepinfo->nstrengths; j++) {
6194 strength = stepinfo->strengths[j];
6195
6196 /*
6197 * If both step size and strength are smaller than the
6198 * chip's requirement, it is not easy to compare the
6199 * resulted reliability.
6200 */
6201 if (step_size < req_step && strength < req_strength)
6202 continue;
6203
6204 if (mtd->writesize % step_size)
6205 continue;
6206
6207 nsteps = mtd->writesize / step_size;
6208
6209 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6210 if (WARN_ON_ONCE(ecc_bytes < 0))
6211 continue;
6212 ecc_bytes_total = ecc_bytes * nsteps;
6213
6214 if (ecc_bytes_total > oobavail ||
6215 strength * nsteps < req_corr)
6216 continue;
6217
6218 /*
6219 * We assume the best is to meet the chip's requrement
6220 * with the least number of ECC bytes.
6221 */
6222 if (ecc_bytes_total < best_ecc_bytes_total) {
6223 best_ecc_bytes_total = ecc_bytes_total;
6224 best_step = step_size;
6225 best_strength = strength;
6226 best_ecc_bytes = ecc_bytes;
6227 }
6228 }
6229 }
6230
6231 if (best_ecc_bytes_total == INT_MAX)
6232 return -ENOTSUPP;
6233
6234 chip->ecc.size = best_step;
6235 chip->ecc.strength = best_strength;
6236 chip->ecc.bytes = best_ecc_bytes;
6237
6238 return 0;
6239}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006240
6241/**
6242 * nand_maximize_ecc - choose the max ECC strength available
6243 * @chip: nand chip info structure
6244 * @caps: ECC engine caps info structure
6245 * @oobavail: OOB size that the ECC engine can use
6246 *
6247 * Choose the max ECC strength that is supported on the controller, and can fit
6248 * within the chip's OOB. On success, the chosen ECC settings are set.
6249 */
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306250static int
6251nand_maximize_ecc(struct nand_chip *chip,
6252 const struct nand_ecc_caps *caps, int oobavail)
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006253{
6254 struct mtd_info *mtd = nand_to_mtd(chip);
6255 const struct nand_ecc_step_info *stepinfo;
6256 int step_size, strength, nsteps, ecc_bytes, corr;
6257 int best_corr = 0;
6258 int best_step = 0;
6259 int best_strength, best_ecc_bytes;
6260 int i, j;
6261
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006262 for (i = 0; i < caps->nstepinfos; i++) {
6263 stepinfo = &caps->stepinfos[i];
6264 step_size = stepinfo->stepsize;
6265
6266 /* If chip->ecc.size is already set, respect it */
6267 if (chip->ecc.size && step_size != chip->ecc.size)
6268 continue;
6269
6270 for (j = 0; j < stepinfo->nstrengths; j++) {
6271 strength = stepinfo->strengths[j];
6272
6273 if (mtd->writesize % step_size)
6274 continue;
6275
6276 nsteps = mtd->writesize / step_size;
6277
6278 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
6279 if (WARN_ON_ONCE(ecc_bytes < 0))
6280 continue;
6281
6282 if (ecc_bytes * nsteps > oobavail)
6283 continue;
6284
6285 corr = strength * nsteps;
6286
6287 /*
6288 * If the number of correctable bits is the same,
6289 * bigger step_size has more reliability.
6290 */
6291 if (corr > best_corr ||
6292 (corr == best_corr && step_size > best_step)) {
6293 best_corr = corr;
6294 best_step = step_size;
6295 best_strength = strength;
6296 best_ecc_bytes = ecc_bytes;
6297 }
6298 }
6299 }
6300
6301 if (!best_corr)
6302 return -ENOTSUPP;
6303
6304 chip->ecc.size = best_step;
6305 chip->ecc.strength = best_strength;
6306 chip->ecc.bytes = best_ecc_bytes;
6307
6308 return 0;
6309}
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09006310
Abhishek Sahu181ace92018-06-20 12:57:28 +05306311/**
6312 * nand_ecc_choose_conf - Set the ECC strength and ECC step size
6313 * @chip: nand chip info structure
6314 * @caps: ECC engine caps info structure
6315 * @oobavail: OOB size that the ECC engine can use
6316 *
6317 * Choose the ECC configuration according to following logic
6318 *
6319 * 1. If both ECC step size and ECC strength are already set (usually by DT)
6320 * then check if it is supported by this controller.
6321 * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength.
6322 * 3. Otherwise, try to match the ECC step size and ECC strength closest
6323 * to the chip's requirement. If available OOB size can't fit the chip
6324 * requirement then fallback to the maximum ECC step size and ECC strength.
6325 *
6326 * On success, the chosen ECC settings are set.
6327 */
6328int nand_ecc_choose_conf(struct nand_chip *chip,
6329 const struct nand_ecc_caps *caps, int oobavail)
6330{
Abhishek Sahu0cf5c7d2018-06-20 12:57:42 +05306331 struct mtd_info *mtd = nand_to_mtd(chip);
6332
6333 if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize))
6334 return -EINVAL;
6335
Abhishek Sahu181ace92018-06-20 12:57:28 +05306336 if (chip->ecc.size && chip->ecc.strength)
6337 return nand_check_ecc_caps(chip, caps, oobavail);
6338
6339 if (chip->ecc.options & NAND_ECC_MAXIMIZE)
6340 return nand_maximize_ecc(chip, caps, oobavail);
6341
6342 if (!nand_match_ecc_req(chip, caps, oobavail))
6343 return 0;
6344
6345 return nand_maximize_ecc(chip, caps, oobavail);
6346}
6347EXPORT_SYMBOL_GPL(nand_ecc_choose_conf);
6348
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006349/*
6350 * Check if the chip configuration meet the datasheet requirements.
6351
6352 * If our configuration corrects A bits per B bytes and the minimum
6353 * required correction level is X bits per Y bytes, then we must ensure
6354 * both of the following are true:
6355 *
6356 * (1) A / B >= X / Y
6357 * (2) A >= X
6358 *
6359 * Requirement (1) ensures we can correct for the required bitflip density.
6360 * Requirement (2) ensures we can correct even when all bitflips are clumped
6361 * in the same sector.
6362 */
6363static bool nand_ecc_strength_good(struct mtd_info *mtd)
6364{
Boris BREZILLON862eba52015-12-01 12:03:03 +01006365 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006366 struct nand_ecc_ctrl *ecc = &chip->ecc;
6367 int corr, ds_corr;
6368
6369 if (ecc->size == 0 || chip->ecc_step_ds == 0)
6370 /* Not enough information */
6371 return true;
6372
6373 /*
6374 * We get the number of corrected bits per page to compare
6375 * the correction density.
6376 */
6377 corr = (mtd->writesize * ecc->strength) / ecc->size;
6378 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
6379
6380 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
6381}
David Woodhouse3b85c322006-09-25 17:06:53 +01006382
6383/**
Miquel Raynal98732da2018-07-25 15:31:50 +02006384 * nand_scan_tail - Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006385 * @chip: NAND chip object
David Woodhouse3b85c322006-09-25 17:06:53 +01006386 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006387 * This is the second phase of the normal nand_scan() function. It fills out
6388 * all the uninitialized function pointers with the defaults and scans for a
6389 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01006390 */
Boris Brezillon00ad3782018-09-06 14:05:14 +02006391static int nand_scan_tail(struct nand_chip *chip)
David Woodhouse3b85c322006-09-25 17:06:53 +01006392{
Boris Brezillon00ad3782018-09-06 14:05:14 +02006393 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie97de79e02013-10-18 14:20:53 +08006394 struct nand_ecc_ctrl *ecc = &chip->ecc;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006395 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01006396
Brian Norrise2414f42012-02-06 13:44:00 -08006397 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006398 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07006399 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006400 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07006401 }
Brian Norrise2414f42012-02-06 13:44:00 -08006402
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006403 chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006404 if (!chip->data_buf)
Boris Brezillonf84674b2017-06-02 12:18:24 +02006405 return -ENOMEM;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01006406
Boris Brezillonf84674b2017-06-02 12:18:24 +02006407 /*
6408 * FIXME: some NAND manufacturer drivers expect the first die to be
6409 * selected when manufacturer->init() is called. They should be fixed
6410 * to explictly select the relevant die when interacting with the NAND
6411 * chip.
6412 */
Boris Brezillon758b56f2018-09-06 14:05:24 +02006413 chip->select_chip(chip, 0);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006414 ret = nand_manufacturer_init(chip);
Boris Brezillon758b56f2018-09-06 14:05:24 +02006415 chip->select_chip(chip, -1);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006416 if (ret)
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006417 goto err_free_buf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006418
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01006419 /* Set the internal oob buffer location, just after the page data */
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006420 chip->oob_poi = chip->data_buf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006421
6422 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006423 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006424 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006425 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006426 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006427 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01006430 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 break;
6432 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006433 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02006434 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 default:
Miquel Raynal882fd152017-08-26 17:19:15 +02006437 /*
6438 * Expose the whole OOB area to users if ECC_NONE
6439 * is passed. We could do that for all kind of
6440 * ->oobsize, but we must keep the old large/small
6441 * page with ECC layout when ->oobsize <= 128 for
6442 * compatibility reasons.
6443 */
6444 if (ecc->mode == NAND_ECC_NONE) {
6445 mtd_set_ooblayout(mtd,
6446 &nand_ooblayout_lp_ops);
6447 break;
6448 }
6449
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006450 WARN(1, "No oob scheme defined for oobsize %d\n",
6451 mtd->oobsize);
6452 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006453 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 }
6455 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006456
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006457 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07006458 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006459 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01006460 */
David Woodhouse956e9442006-09-25 17:12:39 +01006461
Huang Shijie97de79e02013-10-18 14:20:53 +08006462 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006463 case NAND_ECC_HW_OOB_FIRST:
6464 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08006465 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006466 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6467 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006468 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006469 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006470 if (!ecc->read_page)
6471 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07006472
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006473 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07006474 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006475 if (!ecc->read_page)
6476 ecc->read_page = nand_read_page_hwecc;
6477 if (!ecc->write_page)
6478 ecc->write_page = nand_write_page_hwecc;
6479 if (!ecc->read_page_raw)
6480 ecc->read_page_raw = nand_read_page_raw;
6481 if (!ecc->write_page_raw)
6482 ecc->write_page_raw = nand_write_page_raw;
6483 if (!ecc->read_oob)
6484 ecc->read_oob = nand_read_oob_std;
6485 if (!ecc->write_oob)
6486 ecc->write_oob = nand_write_oob_std;
6487 if (!ecc->read_subpage)
6488 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02006489 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08006490 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006491
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006492 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08006493 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
6494 (!ecc->read_page ||
6495 ecc->read_page == nand_read_page_hwecc ||
6496 !ecc->write_page ||
6497 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006498 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
6499 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006500 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006501 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07006502 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08006503 if (!ecc->read_page)
6504 ecc->read_page = nand_read_page_syndrome;
6505 if (!ecc->write_page)
6506 ecc->write_page = nand_write_page_syndrome;
6507 if (!ecc->read_page_raw)
6508 ecc->read_page_raw = nand_read_page_raw_syndrome;
6509 if (!ecc->write_page_raw)
6510 ecc->write_page_raw = nand_write_page_raw_syndrome;
6511 if (!ecc->read_oob)
6512 ecc->read_oob = nand_read_oob_syndrome;
6513 if (!ecc->write_oob)
6514 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02006515
Huang Shijie97de79e02013-10-18 14:20:53 +08006516 if (mtd->writesize >= ecc->size) {
6517 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006518 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
6519 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006520 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07006521 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006522 break;
Mike Dunne2788c92012-04-25 12:06:10 -07006523 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006524 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
6525 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08006526 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02006527 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02006529 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006530 ret = nand_set_ecc_soft_ops(mtd);
6531 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006532 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006533 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01006534 }
6535 break;
6536
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006537 case NAND_ECC_ON_DIE:
6538 if (!ecc->read_page || !ecc->write_page) {
6539 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
6540 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006541 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02006542 }
6543 if (!ecc->read_oob)
6544 ecc->read_oob = nand_read_oob_std;
6545 if (!ecc->write_oob)
6546 ecc->write_oob = nand_write_oob_std;
6547 break;
6548
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006549 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02006550 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08006551 ecc->read_page = nand_read_page_raw;
6552 ecc->write_page = nand_write_page_raw;
6553 ecc->read_oob = nand_read_oob_std;
6554 ecc->read_page_raw = nand_read_page_raw;
6555 ecc->write_page_raw = nand_write_page_raw;
6556 ecc->write_oob = nand_write_oob_std;
6557 ecc->size = mtd->writesize;
6558 ecc->bytes = 0;
6559 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 break;
David Woodhouse956e9442006-09-25 17:12:39 +01006561
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006563 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
6564 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006565 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
Boris Brezillonaeb93af2017-12-05 12:09:29 +01006568 if (ecc->correct || ecc->calculate) {
6569 ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6570 ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL);
6571 if (!ecc->calc_buf || !ecc->code_buf) {
6572 ret = -ENOMEM;
6573 goto err_nand_manuf_cleanup;
6574 }
6575 }
6576
Brian Norris9ce244b2011-08-30 18:45:37 -07006577 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08006578 if (!ecc->read_oob_raw)
6579 ecc->read_oob_raw = ecc->read_oob;
6580 if (!ecc->write_oob_raw)
6581 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07006582
Boris Brezillon846031d2016-02-03 20:11:00 +01006583 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01006584 mtd->ecc_strength = ecc->strength;
6585 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03006586
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02006587 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006588 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07006589 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02006590 */
Huang Shijie97de79e02013-10-18 14:20:53 +08006591 ecc->steps = mtd->writesize / ecc->size;
6592 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006593 WARN(1, "Invalid ECC parameters\n");
6594 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006595 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 }
Huang Shijie97de79e02013-10-18 14:20:53 +08006597 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006598 if (ecc->total > mtd->oobsize) {
6599 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
6600 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006601 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09006602 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006603
Boris Brezillon846031d2016-02-03 20:11:00 +01006604 /*
6605 * The number of bytes available for a client to place data into
6606 * the out of band area.
6607 */
6608 ret = mtd_ooblayout_count_freebytes(mtd);
6609 if (ret < 0)
6610 ret = 0;
6611
6612 mtd->oobavail = ret;
6613
6614 /* ECC sanity check: warn if it's too weak */
6615 if (!nand_ecc_strength_good(mtd))
6616 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
6617 mtd->name);
6618
Brian Norris8b6e50c2011-05-25 14:59:01 -07006619 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08006620 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08006621 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02006622 case 2:
6623 mtd->subpage_sft = 1;
6624 break;
6625 case 4:
6626 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01006627 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02006628 mtd->subpage_sft = 2;
6629 break;
6630 }
6631 }
6632 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
6633
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02006634 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006635 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006638 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006640 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09306641 switch (ecc->mode) {
6642 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09306643 if (chip->page_shift > 9)
6644 chip->options |= NAND_SUBPAGE_READ;
6645 break;
6646
6647 default:
6648 break;
6649 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05006650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08006652 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02006653 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
6654 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006655 mtd->_erase = nand_erase;
6656 mtd->_point = NULL;
6657 mtd->_unpoint = NULL;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006658 mtd->_panic_write = panic_nand_write;
6659 mtd->_read_oob = nand_read_oob;
6660 mtd->_write_oob = nand_write_oob;
6661 mtd->_sync = nand_sync;
6662 mtd->_lock = NULL;
6663 mtd->_unlock = NULL;
6664 mtd->_suspend = nand_suspend;
6665 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08006666 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03006667 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02006668 mtd->_block_isbad = nand_block_isbad;
6669 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06006670 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01006671 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03006673 /*
6674 * Initialize bitflip_threshold to its default prior scan_bbt() call.
6675 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
6676 * properly set.
6677 */
6678 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08006679 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
Boris Brezillonf84674b2017-06-02 12:18:24 +02006681 /* Initialize the ->data_interface field. */
6682 ret = nand_init_data_interface(chip);
6683 if (ret)
6684 goto err_nand_manuf_cleanup;
6685
6686 /* Enter fastest possible mode on all dies. */
6687 for (i = 0; i < chip->numchips; i++) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02006688 ret = nand_setup_data_interface(chip, i);
Boris Brezillonf84674b2017-06-02 12:18:24 +02006689 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006690 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006691 }
6692
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006693 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006694 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00006695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
6697 /* Build bad block table */
Boris Brezillone80eba72018-07-05 12:27:31 +02006698 ret = nand_create_bbt(chip);
Brian Norris44d41822017-05-01 17:04:50 -07006699 if (ret)
Miquel Raynal17fa8042017-11-30 18:01:31 +01006700 goto err_nand_manuf_cleanup;
Boris Brezillonf84674b2017-06-02 12:18:24 +02006701
Brian Norris44d41822017-05-01 17:04:50 -07006702 return 0;
6703
Boris Brezillonf84674b2017-06-02 12:18:24 +02006704
6705err_nand_manuf_cleanup:
6706 nand_manufacturer_cleanup(chip);
6707
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006708err_free_buf:
6709 kfree(chip->data_buf);
6710 kfree(ecc->code_buf);
6711 kfree(ecc->calc_buf);
Brian Norris78771042017-05-01 17:04:53 -07006712
Ezequiel García11eaf6d2016-04-01 18:29:24 -03006713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714}
6715
Miquel Raynal05b54c72018-07-19 01:05:46 +02006716static int nand_attach(struct nand_chip *chip)
6717{
6718 if (chip->controller->ops && chip->controller->ops->attach_chip)
6719 return chip->controller->ops->attach_chip(chip);
6720
6721 return 0;
6722}
6723
6724static void nand_detach(struct nand_chip *chip)
6725{
6726 if (chip->controller->ops && chip->controller->ops->detach_chip)
6727 chip->controller->ops->detach_chip(chip);
6728}
6729
David Woodhouse3b85c322006-09-25 17:06:53 +01006730/**
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006731 * nand_scan_with_ids - [NAND Interface] Scan for the NAND device
Boris Brezillon00ad3782018-09-06 14:05:14 +02006732 * @chip: NAND chip object
Boris Brezillon800342d2018-08-04 22:59:23 +02006733 * @maxchips: number of chips to scan for.
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006734 * @ids: optional flash IDs table
David Woodhouse3b85c322006-09-25 17:06:53 +01006735 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07006736 * This fills out all the uninitialized function pointers with the defaults.
6737 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03006738 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01006739 */
Boris Brezillon871a4072018-08-04 22:59:22 +02006740int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips,
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006741 struct nand_flash_dev *ids)
David Woodhouse3b85c322006-09-25 17:06:53 +01006742{
6743 int ret;
6744
Boris Brezillon800342d2018-08-04 22:59:23 +02006745 if (!maxchips)
6746 return -EINVAL;
6747
6748 ret = nand_scan_ident(chip, maxchips, ids);
6749 if (ret)
6750 return ret;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006751
6752 ret = nand_attach(chip);
6753 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006754 goto cleanup_ident;
Miquel Raynal05b54c72018-07-19 01:05:46 +02006755
Boris Brezillon00ad3782018-09-06 14:05:14 +02006756 ret = nand_scan_tail(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006757 if (ret)
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006758 goto detach_chip;
6759
6760 return 0;
6761
6762detach_chip:
6763 nand_detach(chip);
6764cleanup_ident:
6765 nand_scan_ident_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006766
David Woodhouse3b85c322006-09-25 17:06:53 +01006767 return ret;
6768}
Miquel Raynal256c4fc2018-04-22 18:02:30 +02006769EXPORT_SYMBOL(nand_scan_with_ids);
David Woodhouse3b85c322006-09-25 17:06:53 +01006770
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006772 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
6773 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07006774 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006775void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02006777 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02006778 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01006779 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
6780
Jesper Juhlfa671642005-11-07 01:01:27 -08006781 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02006782 kfree(chip->bbt);
Masahiro Yamadac0313b92017-12-05 17:47:16 +09006783 kfree(chip->data_buf);
6784 kfree(chip->ecc.code_buf);
6785 kfree(chip->ecc.calc_buf);
Brian Norris58373ff2010-07-15 12:15:44 -07006786
6787 /* Free bad block descriptor memory */
6788 if (chip->badblock_pattern && chip->badblock_pattern->options
6789 & NAND_BBT_DYNAMICSTRUCT)
6790 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02006791
6792 /* Free manufacturer priv data. */
6793 nand_manufacturer_cleanup(chip);
Miquel Raynal05b54c72018-07-19 01:05:46 +02006794
6795 /* Free controller specific allocations after chip identification */
6796 nand_detach(chip);
Miquel Raynal2023f1fa2018-07-25 15:31:51 +02006797
6798 /* Free identification phase allocations */
6799 nand_scan_ident_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800}
Miquel Raynal05b54c72018-07-19 01:05:46 +02006801
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006802EXPORT_SYMBOL_GPL(nand_cleanup);
6803
6804/**
6805 * nand_release - [NAND Interface] Unregister the MTD device and free resources
6806 * held by the NAND device
Boris Brezillon59ac2762018-09-06 14:05:15 +02006807 * @chip: NAND chip object
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006808 */
Boris Brezillon59ac2762018-09-06 14:05:15 +02006809void nand_release(struct nand_chip *chip)
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006810{
Boris Brezillon59ac2762018-09-06 14:05:15 +02006811 mtd_device_unregister(nand_to_mtd(chip));
6812 nand_cleanup(chip);
Richard Weinbergerd44154f2016-09-21 11:44:41 +02006813}
David Woodhousee0c7d762006-05-13 18:07:53 +01006814EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08006815
David Woodhousee0c7d762006-05-13 18:07:53 +01006816MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02006817MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
6818MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01006819MODULE_DESCRIPTION("Generic NAND flash driver code");