blob: 6ed5392f3798fecb79b4ea8f608b880a3f9778e6 [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;
68 oobregion->length = 4;
69 } else {
70 oobregion->offset = 6;
71 oobregion->length = ecc->total - 4;
72 }
73
74 return 0;
75}
76
77static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
78 struct mtd_oob_region *oobregion)
79{
80 if (section > 1)
81 return -ERANGE;
82
83 if (mtd->oobsize == 16) {
84 if (section)
85 return -ERANGE;
86
87 oobregion->length = 8;
88 oobregion->offset = 8;
89 } else {
90 oobregion->length = 2;
91 if (!section)
92 oobregion->offset = 3;
93 else
94 oobregion->offset = 6;
95 }
96
97 return 0;
98}
99
100const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
101 .ecc = nand_ooblayout_ecc_sp,
102 .free = nand_ooblayout_free_sp,
103};
104EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
105
106static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
107 struct mtd_oob_region *oobregion)
108{
109 struct nand_chip *chip = mtd_to_nand(mtd);
110 struct nand_ecc_ctrl *ecc = &chip->ecc;
111
112 if (section)
113 return -ERANGE;
114
115 oobregion->length = ecc->total;
116 oobregion->offset = mtd->oobsize - oobregion->length;
117
118 return 0;
119}
120
121static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
122 struct mtd_oob_region *oobregion)
123{
124 struct nand_chip *chip = mtd_to_nand(mtd);
125 struct nand_ecc_ctrl *ecc = &chip->ecc;
126
127 if (section)
128 return -ERANGE;
129
130 oobregion->length = mtd->oobsize - ecc->total - 2;
131 oobregion->offset = 2;
132
133 return 0;
134}
135
136const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
137 .ecc = nand_ooblayout_ecc_lp,
138 .free = nand_ooblayout_free_lp,
139};
140EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200141
Alexander Couzens6a623e02017-05-02 12:19:00 +0200142/*
143 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
144 * are placed at a fixed offset.
145 */
146static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
147 struct mtd_oob_region *oobregion)
148{
149 struct nand_chip *chip = mtd_to_nand(mtd);
150 struct nand_ecc_ctrl *ecc = &chip->ecc;
151
152 if (section)
153 return -ERANGE;
154
155 switch (mtd->oobsize) {
156 case 64:
157 oobregion->offset = 40;
158 break;
159 case 128:
160 oobregion->offset = 80;
161 break;
162 default:
163 return -EINVAL;
164 }
165
166 oobregion->length = ecc->total;
167 if (oobregion->offset + oobregion->length > mtd->oobsize)
168 return -ERANGE;
169
170 return 0;
171}
172
173static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
174 struct mtd_oob_region *oobregion)
175{
176 struct nand_chip *chip = mtd_to_nand(mtd);
177 struct nand_ecc_ctrl *ecc = &chip->ecc;
178 int ecc_offset = 0;
179
180 if (section < 0 || section > 1)
181 return -ERANGE;
182
183 switch (mtd->oobsize) {
184 case 64:
185 ecc_offset = 40;
186 break;
187 case 128:
188 ecc_offset = 80;
189 break;
190 default:
191 return -EINVAL;
192 }
193
194 if (section == 0) {
195 oobregion->offset = 2;
196 oobregion->length = ecc_offset - 2;
197 } else {
198 oobregion->offset = ecc_offset + ecc->total;
199 oobregion->length = mtd->oobsize - oobregion->offset;
200 }
201
202 return 0;
203}
204
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100205static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200206 .ecc = nand_ooblayout_ecc_lp_hamming,
207 .free = nand_ooblayout_free_lp_hamming,
208};
209
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530210static int check_offs_len(struct mtd_info *mtd,
211 loff_t ofs, uint64_t len)
212{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100213 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530214 int ret = 0;
215
216 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300217 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700218 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 ret = -EINVAL;
220 }
221
222 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530228 return ret;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
232 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700233 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000234 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800235 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100237static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100239 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200241 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200242 spin_lock(&chip->controller->lock);
243 chip->controller->active = NULL;
244 chip->state = FL_READY;
245 wake_up(&chip->controller->wq);
246 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/**
250 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700251 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700253 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200255static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100257 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200258 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/**
Masanari Iida064a7692012-11-09 23:20:58 +0900262 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700263 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700265 * Default read function for 16bit buswidth with endianness conversion.
266 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200268static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100270 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200271 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700276 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700278 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280static u16 nand_read_word(struct mtd_info *mtd)
281{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100282 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200283 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700288 * @mtd: MTD device structure
289 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
291 * Default select function for 1 chip devices.
292 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200293static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100295 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200296
297 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303
304 default:
305 BUG();
306 }
307}
308
309/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100310 * nand_write_byte - [DEFAULT] write single byte to chip
311 * @mtd: MTD device structure
312 * @byte: value to write
313 *
314 * Default function to write a byte to I/O[7:0]
315 */
316static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
317{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100318 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100319
320 chip->write_buf(mtd, &byte, 1);
321}
322
323/**
324 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
325 * @mtd: MTD device structure
326 * @byte: value to write
327 *
328 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
329 */
330static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
331{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100332 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100333 uint16_t word = byte;
334
335 /*
336 * It's not entirely clear what should happen to I/O[15:8] when writing
337 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
338 *
339 * When the host supports a 16-bit bus width, only data is
340 * transferred at the 16-bit width. All address and command line
341 * transfers shall use only the lower 8-bits of the data bus. During
342 * command transfers, the host may place any value on the upper
343 * 8-bits of the data bus. During address transfers, the host shall
344 * set the upper 8-bits of the data bus to 00h.
345 *
346 * One user of the write_byte callback is nand_onfi_set_features. The
347 * four parameters are specified to be written to I/O[7:0], but this is
348 * neither an address nor a command transfer. Let's assume a 0 on the
349 * upper I/O lines is OK.
350 */
351 chip->write_buf(mtd, (uint8_t *)&word, 2);
352}
353
354/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700356 * @mtd: MTD device structure
357 * @buf: data buffer
358 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700360 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200362static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100364 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Alexander Shiyan76413832013-04-13 09:32:13 +0400366 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000370 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700371 * @mtd: MTD device structure
372 * @buf: buffer to store date
373 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700375 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200377static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100379 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Alexander Shiyan76413832013-04-13 09:32:13 +0400381 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700386 * @mtd: MTD device structure
387 * @buf: data buffer
388 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700390 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200392static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100394 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000396
Alexander Shiyan76413832013-04-13 09:32:13 +0400397 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000401 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700402 * @mtd: MTD device structure
403 * @buf: buffer to store date
404 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700406 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200408static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100410 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Alexander Shiyan76413832013-04-13 09:32:13 +0400413 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700418 * @mtd: MTD device structure
419 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000421 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530423static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900425 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100426 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900427 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Brian Norris5fb15492011-05-31 16:31:21 -0700429 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700430 ofs += mtd->erasesize - mtd->writesize;
431
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100432 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100434
Masahiro Yamadac120e752017-03-23 05:07:01 +0900435 for (; page < page_end; page++) {
436 res = chip->ecc.read_oob(mtd, chip, page);
437 if (res)
438 return res;
439
440 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000441
Brian Norriscdbec052012-01-13 18:11:48 -0800442 if (likely(chip->badblockbits == 8))
443 res = bad != 0xFF;
444 else
445 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900446 if (res)
447 return res;
448 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200449
Masahiro Yamadac120e752017-03-23 05:07:01 +0900450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700454 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700455 * @mtd: MTD device structure
456 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700458 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700459 * specific driver. It provides the details for writing a bad block marker to a
460 * block.
461 */
462static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
463{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100464 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 struct mtd_oob_ops ops;
466 uint8_t buf[2] = { 0, 0 };
467 int ret = 0, res, i = 0;
468
Brian Norris0ec56dc2015-02-28 02:02:30 -0800469 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700470 ops.oobbuf = buf;
471 ops.ooboffs = chip->badblockpos;
472 if (chip->options & NAND_BUSWIDTH_16) {
473 ops.ooboffs &= ~0x01;
474 ops.len = ops.ooblen = 2;
475 } else {
476 ops.len = ops.ooblen = 1;
477 }
478 ops.mode = MTD_OPS_PLACE_OOB;
479
480 /* Write to first/last page(s) if necessary */
481 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
482 ofs += mtd->erasesize - mtd->writesize;
483 do {
484 res = nand_do_write_oob(mtd, ofs, &ops);
485 if (!ret)
486 ret = res;
487
488 i++;
489 ofs += mtd->writesize;
490 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
491
492 return ret;
493}
494
495/**
496 * nand_block_markbad_lowlevel - mark a block bad
497 * @mtd: MTD device structure
498 * @ofs: offset from device start
499 *
500 * This function performs the generic NAND bad block marking steps (i.e., bad
501 * block table(s) and/or marker(s)). We only allow the hardware driver to
502 * specify how to write bad block markers to OOB (chip->block_markbad).
503 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700504 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300505 *
Brian Norrise2414f42012-02-06 13:44:00 -0800506 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700507 * (2) write bad block marker to OOB area of affected block (unless flag
508 * NAND_BBT_NO_OOB_BBM is present)
509 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300510 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700511 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700514static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100516 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000518
Brian Norrisb32843b2013-07-30 17:52:59 -0700519 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800520 struct erase_info einfo;
521
522 /* Attempt erase before marking OOB */
523 memset(&einfo, 0, sizeof(einfo));
524 einfo.mtd = mtd;
525 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300526 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800527 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800528
Brian Norrisb32843b2013-07-30 17:52:59 -0700529 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800530 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700531 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300532 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200533 }
Brian Norrise2414f42012-02-06 13:44:00 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Mark block bad in BBT */
536 if (chip->bbt) {
537 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800538 if (!ret)
539 ret = res;
540 }
541
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200542 if (!ret)
543 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300544
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000548/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700550 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700552 * Check, if the device is write protected. The function expects, that the
553 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100555static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100557 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200558
Brian Norris8b6e50c2011-05-25 14:59:01 -0700559 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200560 if (chip->options & NAND_BROKEN_XD)
561 return 0;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200564 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
565 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800569 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700570 * @mtd: MTD device structure
571 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300572 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800573 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300574 */
575static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
576{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100577 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300578
579 if (!chip->bbt)
580 return 0;
581 /* Return info from the table */
582 return nand_isreserved_bbt(mtd, ofs);
583}
584
585/**
586 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
587 * @mtd: MTD device structure
588 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700589 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *
591 * Check, if the block is bad. Either by reading the bad block table or
592 * calling of the scan function.
593 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530594static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100596 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000597
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200598 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530599 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100602 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200605/**
606 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700607 * @mtd: MTD device structure
608 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200609 *
610 * Helper function for nand_wait_ready used when needing to wait in interrupt
611 * context.
612 */
613static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
614{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100615 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616 int i;
617
618 /* Wait for the device to get ready */
619 for (i = 0; i < timeo; i++) {
620 if (chip->dev_ready(mtd))
621 break;
622 touch_softlockup_watchdog();
623 mdelay(1);
624 }
625}
626
Alex Smithb70af9b2015-10-06 14:52:07 +0100627/**
628 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
629 * @mtd: MTD device structure
630 *
631 * Wait for the ready pin after a command, and warn if a timeout occurs.
632 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100633void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000634{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100635 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100636 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000637
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200638 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100639 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200640
Brian Norris7854d3f2011-06-23 14:12:08 -0700641 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100642 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300645 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100646 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000647 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100648
Brian Norris9ebfdf52016-03-04 17:19:23 -0800649 if (!chip->dev_ready(mtd))
650 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000651}
David Woodhouse4b648b02006-09-25 17:05:24 +0100652EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200655 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
656 * @mtd: MTD device structure
657 * @timeo: Timeout in ms
658 *
659 * Wait for status ready (i.e. command done) or timeout.
660 */
661static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
662{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100663 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200664
665 timeo = jiffies + msecs_to_jiffies(timeo);
666 do {
667 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
668 break;
669 touch_softlockup_watchdog();
670 } while (time_before(jiffies, timeo));
671};
672
673/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700675 * @mtd: MTD device structure
676 * @command: the command to be sent
677 * @column: the column address for this command, -1 if none
678 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700680 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200681 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200683static void nand_command(struct mtd_info *mtd, unsigned int command,
684 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100686 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200687 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Brian Norris8b6e50c2011-05-25 14:59:01 -0700689 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (command == NAND_CMD_SEQIN) {
691 int readcmd;
692
Joern Engel28318772006-05-22 23:18:05 +0200693 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200695 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 readcmd = NAND_CMD_READOOB;
697 } else if (column < 256) {
698 /* First 256 bytes --> READ0 */
699 readcmd = NAND_CMD_READ0;
700 } else {
701 column -= 256;
702 readcmd = NAND_CMD_READ1;
703 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200704 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200707 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Brian Norris8b6e50c2011-05-25 14:59:01 -0700709 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200710 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
711 /* Serially input address */
712 if (column != -1) {
713 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800714 if (chip->options & NAND_BUSWIDTH_16 &&
715 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200717 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200718 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200720 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725 if (chip->chipsize > (32 << 20))
726 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200727 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000729
730 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700731 * Program and erase have their own busy handlers status and sequential
732 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case NAND_CMD_PAGEPROG:
737 case NAND_CMD_ERASE1:
738 case NAND_CMD_ERASE2:
739 case NAND_CMD_SEQIN:
740 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900741 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900742 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744
745 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 udelay(chip->chip_delay);
749 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200750 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200751 chip->cmd_ctrl(mtd,
752 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200753 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
754 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return;
756
David Woodhousee0c7d762006-05-13 18:07:53 +0100757 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200758 case NAND_CMD_READ0:
759 /*
760 * READ0 is sometimes used to exit GET STATUS mode. When this
761 * is the case no address cycles are requested, and we can use
762 * this information to detect that we should not wait for the
763 * device to be ready.
764 */
765 if (column == -1 && page_addr == -1)
766 return;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000769 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * If we don't have access to the busy pin, we apply the given
771 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100772 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200773 if (!chip->dev_ready) {
774 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700778 /*
779 * Apply this short delay always to ensure that we do wait tWB in
780 * any case on any machine.
781 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100782 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000783
784 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200787static void nand_ccs_delay(struct nand_chip *chip)
788{
789 /*
790 * The controller already takes care of waiting for tCCS when the RNDIN
791 * or RNDOUT command is sent, return directly.
792 */
793 if (!(chip->options & NAND_WAIT_TCCS))
794 return;
795
796 /*
797 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
798 * (which should be safe for all NANDs).
799 */
800 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
801 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
802 else
803 ndelay(500);
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/**
807 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700808 * @mtd: MTD device structure
809 * @command: the command to be sent
810 * @column: the column address for this command, -1 if none
811 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200813 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700814 * devices. We don't have the separate regions as we have in the small page
815 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200817static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
818 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100820 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Emulate NAND_CMD_READOOB */
823 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200824 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 command = NAND_CMD_READ0;
826 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000827
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200828 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400829 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200832 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* Serially input address */
835 if (column != -1) {
836 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800837 if (chip->options & NAND_BUSWIDTH_16 &&
838 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200840 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200841 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200842
Brian Norrisf5b88de2016-10-03 09:49:35 -0700843 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200844 if (!nand_opcode_8bits(command))
845 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200848 chip->cmd_ctrl(mtd, page_addr, ctrl);
849 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200850 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200852 if (chip->chipsize > (128 << 20))
853 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200854 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200857 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000858
859 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100861 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 case NAND_CMD_CACHEDPROG:
866 case NAND_CMD_PAGEPROG:
867 case NAND_CMD_ERASE1:
868 case NAND_CMD_ERASE2:
869 case NAND_CMD_SEQIN:
870 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900871 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900872 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200875 case NAND_CMD_RNDIN:
876 nand_ccs_delay(chip);
877 return;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200880 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200882 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200883 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
884 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
885 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
886 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200887 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
888 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return;
890
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200891 case NAND_CMD_RNDOUT:
892 /* No ready / busy check necessary */
893 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
894 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
895 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
896 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200897
898 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200899 return;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200902 /*
903 * READ0 is sometimes used to exit GET STATUS mode. When this
904 * is the case no address cycles are requested, and we can use
905 * this information to detect that READSTART should not be
906 * issued.
907 */
908 if (column == -1 && page_addr == -1)
909 return;
910
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200911 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
912 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
913 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
914 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000915
David Woodhousee0c7d762006-05-13 18:07:53 +0100916 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000918 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100921 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200922 if (!chip->dev_ready) {
923 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000927
Brian Norris8b6e50c2011-05-25 14:59:01 -0700928 /*
929 * Apply this short delay always to ensure that we do wait tWB in
930 * any case on any machine.
931 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100932 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000933
934 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200938 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700939 * @chip: the nand chip descriptor
940 * @mtd: MTD device structure
941 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200942 *
943 * Used when in panic, no locks are taken.
944 */
945static void panic_nand_get_device(struct nand_chip *chip,
946 struct mtd_info *mtd, int new_state)
947{
Brian Norris7854d3f2011-06-23 14:12:08 -0700948 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200949 chip->controller->active = chip;
950 chip->state = new_state;
951}
952
953/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700955 * @mtd: MTD device structure
956 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
958 * Get the device and lock it for exclusive access
959 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200960static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800961nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100963 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200964 spinlock_t *lock = &chip->controller->lock;
965 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100966 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200967retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100968 spin_lock(lock);
969
vimal singhb8b3ee92009-07-09 20:41:22 +0530970 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200971 if (!chip->controller->active)
972 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200973
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200974 if (chip->controller->active == chip && chip->state == FL_READY) {
975 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100976 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100977 return 0;
978 }
979 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800980 if (chip->controller->active->state == FL_PM_SUSPENDED) {
981 chip->state = FL_PM_SUSPENDED;
982 spin_unlock(lock);
983 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800984 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100985 }
986 set_current_state(TASK_UNINTERRUPTIBLE);
987 add_wait_queue(wq, &wait);
988 spin_unlock(lock);
989 schedule();
990 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto retry;
992}
993
994/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700995 * panic_nand_wait - [GENERIC] wait until the command is done
996 * @mtd: MTD device structure
997 * @chip: NAND chip structure
998 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200999 *
1000 * Wait for command done. This is a helper function for nand_wait used when
1001 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001002 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001003 */
1004static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1005 unsigned long timeo)
1006{
1007 int i;
1008 for (i = 0; i < timeo; i++) {
1009 if (chip->dev_ready) {
1010 if (chip->dev_ready(mtd))
1011 break;
1012 } else {
1013 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1014 break;
1015 }
1016 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001017 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001018}
1019
1020/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001021 * nand_wait - [DEFAULT] wait until the command is done
1022 * @mtd: MTD device structure
1023 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001025 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001026 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001027static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029
Alex Smithb70af9b2015-10-06 14:52:07 +01001030 int status;
1031 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Brian Norris8b6e50c2011-05-25 14:59:01 -07001033 /*
1034 * Apply this short delay always to ensure that we do wait tWB in any
1035 * case on any machine.
1036 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001037 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001039 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001041 if (in_interrupt() || oops_in_progress)
1042 panic_nand_wait(mtd, chip, timeo);
1043 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001044 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001045 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001046 if (chip->dev_ready) {
1047 if (chip->dev_ready(mtd))
1048 break;
1049 } else {
1050 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1051 break;
1052 }
1053 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001054 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001056
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001057 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001058 /* This can happen if in case of timeout or buggy dev_ready */
1059 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return status;
1061}
1062
1063/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001064 * nand_reset_data_interface - Reset data interface and timings
1065 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001066 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001067 *
1068 * Reset the Data interface and timings to ONFI mode 0.
1069 *
1070 * Returns 0 for success or negative error code otherwise.
1071 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001072static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001073{
1074 struct mtd_info *mtd = nand_to_mtd(chip);
1075 const struct nand_data_interface *conf;
1076 int ret;
1077
1078 if (!chip->setup_data_interface)
1079 return 0;
1080
1081 /*
1082 * The ONFI specification says:
1083 * "
1084 * To transition from NV-DDR or NV-DDR2 to the SDR data
1085 * interface, the host shall use the Reset (FFh) command
1086 * using SDR timing mode 0. A device in any timing mode is
1087 * required to recognize Reset (FFh) command issued in SDR
1088 * timing mode 0.
1089 * "
1090 *
1091 * Configure the data interface in SDR mode and set the
1092 * timings to timing mode 0.
1093 */
1094
1095 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001096 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001097 if (ret)
1098 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1099
1100 return ret;
1101}
1102
1103/**
1104 * nand_setup_data_interface - Setup the best data interface and timings
1105 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001106 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001107 *
1108 * Find and configure the best data interface and NAND timings supported by
1109 * the chip and the driver.
1110 * First tries to retrieve supported timing modes from ONFI information,
1111 * and if the NAND chip does not support ONFI, relies on the
1112 * ->onfi_timing_mode_default specified in the nand_ids table.
1113 *
1114 * Returns 0 for success or negative error code otherwise.
1115 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001116static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001117{
1118 struct mtd_info *mtd = nand_to_mtd(chip);
1119 int ret;
1120
1121 if (!chip->setup_data_interface || !chip->data_interface)
1122 return 0;
1123
1124 /*
1125 * Ensure the timing mode has been changed on the chip side
1126 * before changing timings on the controller side.
1127 */
1128 if (chip->onfi_version) {
1129 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1130 chip->onfi_timing_mode_default,
1131 };
1132
1133 ret = chip->onfi_set_features(mtd, chip,
1134 ONFI_FEATURE_ADDR_TIMING_MODE,
1135 tmode_param);
1136 if (ret)
1137 goto err;
1138 }
1139
Boris Brezillon104e4422017-03-16 09:35:58 +01001140 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001141err:
1142 return ret;
1143}
1144
1145/**
1146 * nand_init_data_interface - find the best data interface and timings
1147 * @chip: The NAND chip
1148 *
1149 * Find the best data interface and NAND timings supported by the chip
1150 * and the driver.
1151 * First tries to retrieve supported timing modes from ONFI information,
1152 * and if the NAND chip does not support ONFI, relies on the
1153 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1154 * function nand_chip->data_interface is initialized with the best timing mode
1155 * available.
1156 *
1157 * Returns 0 for success or negative error code otherwise.
1158 */
1159static int nand_init_data_interface(struct nand_chip *chip)
1160{
1161 struct mtd_info *mtd = nand_to_mtd(chip);
1162 int modes, mode, ret;
1163
1164 if (!chip->setup_data_interface)
1165 return 0;
1166
1167 /*
1168 * First try to identify the best timings from ONFI parameters and
1169 * if the NAND does not support ONFI, fallback to the default ONFI
1170 * timing mode.
1171 */
1172 modes = onfi_get_async_timing_mode(chip);
1173 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1174 if (!chip->onfi_timing_mode_default)
1175 return 0;
1176
1177 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1178 }
1179
1180 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1181 GFP_KERNEL);
1182 if (!chip->data_interface)
1183 return -ENOMEM;
1184
1185 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1186 ret = onfi_init_data_interface(chip, chip->data_interface,
1187 NAND_SDR_IFACE, mode);
1188 if (ret)
1189 continue;
1190
Boris Brezillon104e4422017-03-16 09:35:58 +01001191 /* Pass -1 to only */
1192 ret = chip->setup_data_interface(mtd,
1193 NAND_DATA_IFACE_CHECK_ONLY,
1194 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001195 if (!ret) {
1196 chip->onfi_timing_mode_default = mode;
1197 break;
1198 }
1199 }
1200
1201 return 0;
1202}
1203
1204static void nand_release_data_interface(struct nand_chip *chip)
1205{
1206 kfree(chip->data_interface);
1207}
1208
1209/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001210 * nand_reset - Reset and initialize a NAND device
1211 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001212 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001213 *
1214 * Returns 0 for success or negative error code otherwise
1215 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001216int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001217{
1218 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001219 int ret;
1220
Boris Brezillon104e4422017-03-16 09:35:58 +01001221 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001222 if (ret)
1223 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001224
Boris Brezillon73f907f2016-10-24 16:46:20 +02001225 /*
1226 * The CS line has to be released before we can apply the new NAND
1227 * interface settings, hence this weird ->select_chip() dance.
1228 */
1229 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001230 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001231 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001232
Boris Brezillon73f907f2016-10-24 16:46:20 +02001233 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001234 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001235 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001236 if (ret)
1237 return ret;
1238
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001239 return 0;
1240}
1241
1242/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001243 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1244 * @buf: buffer to test
1245 * @len: buffer length
1246 * @bitflips_threshold: maximum number of bitflips
1247 *
1248 * Check if a buffer contains only 0xff, which means the underlying region
1249 * has been erased and is ready to be programmed.
1250 * The bitflips_threshold specify the maximum number of bitflips before
1251 * considering the region is not erased.
1252 * Note: The logic of this function has been extracted from the memweight
1253 * implementation, except that nand_check_erased_buf function exit before
1254 * testing the whole buffer if the number of bitflips exceed the
1255 * bitflips_threshold value.
1256 *
1257 * Returns a positive number of bitflips less than or equal to
1258 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1259 * threshold.
1260 */
1261static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1262{
1263 const unsigned char *bitmap = buf;
1264 int bitflips = 0;
1265 int weight;
1266
1267 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1268 len--, bitmap++) {
1269 weight = hweight8(*bitmap);
1270 bitflips += BITS_PER_BYTE - weight;
1271 if (unlikely(bitflips > bitflips_threshold))
1272 return -EBADMSG;
1273 }
1274
1275 for (; len >= sizeof(long);
1276 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001277 unsigned long d = *((unsigned long *)bitmap);
1278 if (d == ~0UL)
1279 continue;
1280 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001281 bitflips += BITS_PER_LONG - weight;
1282 if (unlikely(bitflips > bitflips_threshold))
1283 return -EBADMSG;
1284 }
1285
1286 for (; len > 0; len--, bitmap++) {
1287 weight = hweight8(*bitmap);
1288 bitflips += BITS_PER_BYTE - weight;
1289 if (unlikely(bitflips > bitflips_threshold))
1290 return -EBADMSG;
1291 }
1292
1293 return bitflips;
1294}
1295
1296/**
1297 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1298 * 0xff data
1299 * @data: data buffer to test
1300 * @datalen: data length
1301 * @ecc: ECC buffer
1302 * @ecclen: ECC length
1303 * @extraoob: extra OOB buffer
1304 * @extraooblen: extra OOB length
1305 * @bitflips_threshold: maximum number of bitflips
1306 *
1307 * Check if a data buffer and its associated ECC and OOB data contains only
1308 * 0xff pattern, which means the underlying region has been erased and is
1309 * ready to be programmed.
1310 * The bitflips_threshold specify the maximum number of bitflips before
1311 * considering the region as not erased.
1312 *
1313 * Note:
1314 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1315 * different from the NAND page size. When fixing bitflips, ECC engines will
1316 * report the number of errors per chunk, and the NAND core infrastructure
1317 * expect you to return the maximum number of bitflips for the whole page.
1318 * This is why you should always use this function on a single chunk and
1319 * not on the whole page. After checking each chunk you should update your
1320 * max_bitflips value accordingly.
1321 * 2/ When checking for bitflips in erased pages you should not only check
1322 * the payload data but also their associated ECC data, because a user might
1323 * have programmed almost all bits to 1 but a few. In this case, we
1324 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1325 * this case.
1326 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1327 * data are protected by the ECC engine.
1328 * It could also be used if you support subpages and want to attach some
1329 * extra OOB data to an ECC chunk.
1330 *
1331 * Returns a positive number of bitflips less than or equal to
1332 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1333 * threshold. In case of success, the passed buffers are filled with 0xff.
1334 */
1335int nand_check_erased_ecc_chunk(void *data, int datalen,
1336 void *ecc, int ecclen,
1337 void *extraoob, int extraooblen,
1338 int bitflips_threshold)
1339{
1340 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1341
1342 data_bitflips = nand_check_erased_buf(data, datalen,
1343 bitflips_threshold);
1344 if (data_bitflips < 0)
1345 return data_bitflips;
1346
1347 bitflips_threshold -= data_bitflips;
1348
1349 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1350 if (ecc_bitflips < 0)
1351 return ecc_bitflips;
1352
1353 bitflips_threshold -= ecc_bitflips;
1354
1355 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1356 bitflips_threshold);
1357 if (extraoob_bitflips < 0)
1358 return extraoob_bitflips;
1359
1360 if (data_bitflips)
1361 memset(data, 0xff, datalen);
1362
1363 if (ecc_bitflips)
1364 memset(ecc, 0xff, ecclen);
1365
1366 if (extraoob_bitflips)
1367 memset(extraoob, 0xff, extraooblen);
1368
1369 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1370}
1371EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1372
1373/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001374 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001375 * @mtd: mtd info structure
1376 * @chip: nand chip info structure
1377 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001378 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001379 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001380 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001381 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001382 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001383int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1384 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001385{
1386 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001387 if (oob_required)
1388 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001389 return 0;
1390}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001391EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001392
1393/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001394 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001395 * @mtd: mtd info structure
1396 * @chip: nand chip info structure
1397 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001398 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001399 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001400 *
1401 * We need a special oob layout and handling even when OOB isn't used.
1402 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001403static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001404 struct nand_chip *chip, uint8_t *buf,
1405 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001406{
1407 int eccsize = chip->ecc.size;
1408 int eccbytes = chip->ecc.bytes;
1409 uint8_t *oob = chip->oob_poi;
1410 int steps, size;
1411
1412 for (steps = chip->ecc.steps; steps > 0; steps--) {
1413 chip->read_buf(mtd, buf, eccsize);
1414 buf += eccsize;
1415
1416 if (chip->ecc.prepad) {
1417 chip->read_buf(mtd, oob, chip->ecc.prepad);
1418 oob += chip->ecc.prepad;
1419 }
1420
1421 chip->read_buf(mtd, oob, eccbytes);
1422 oob += eccbytes;
1423
1424 if (chip->ecc.postpad) {
1425 chip->read_buf(mtd, oob, chip->ecc.postpad);
1426 oob += chip->ecc.postpad;
1427 }
1428 }
1429
1430 size = mtd->oobsize - (oob - chip->oob_poi);
1431 if (size)
1432 chip->read_buf(mtd, oob, size);
1433
1434 return 0;
1435}
1436
1437/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001438 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001439 * @mtd: mtd info structure
1440 * @chip: nand chip info structure
1441 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001442 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001443 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001444 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001445static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001446 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Boris Brezillon846031d2016-02-03 20:11:00 +01001448 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001449 int eccbytes = chip->ecc.bytes;
1450 int eccsteps = chip->ecc.steps;
1451 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001452 uint8_t *ecc_calc = chip->buffers->ecccalc;
1453 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001454 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001455
Brian Norris1fbb9382012-05-02 10:14:55 -07001456 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001457
1458 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1459 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1460
Boris Brezillon846031d2016-02-03 20:11:00 +01001461 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1462 chip->ecc.total);
1463 if (ret)
1464 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001465
1466 eccsteps = chip->ecc.steps;
1467 p = buf;
1468
1469 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1470 int stat;
1471
1472 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001473 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001474 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001475 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001476 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001477 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1478 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001479 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001480 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301484 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001485 * @mtd: mtd info structure
1486 * @chip: nand chip info structure
1487 * @data_offs: offset of requested data within the page
1488 * @readlen: data length
1489 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001490 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001491 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001492static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001493 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1494 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001495{
Boris Brezillon846031d2016-02-03 20:11:00 +01001496 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001497 uint8_t *p;
1498 int data_col_addr, i, gaps = 0;
1499 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1500 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001501 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001502 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001503 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001504
Brian Norris7854d3f2011-06-23 14:12:08 -07001505 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001506 start_step = data_offs / chip->ecc.size;
1507 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1508 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301509 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001510
Brian Norris8b6e50c2011-05-25 14:59:01 -07001511 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001512 datafrag_len = num_steps * chip->ecc.size;
1513 eccfrag_len = num_steps * chip->ecc.bytes;
1514
1515 data_col_addr = start_step * chip->ecc.size;
1516 /* If we read not a page aligned data */
1517 if (data_col_addr != 0)
1518 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1519
1520 p = bufpoi + data_col_addr;
1521 chip->read_buf(mtd, p, datafrag_len);
1522
Brian Norris8b6e50c2011-05-25 14:59:01 -07001523 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001524 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1525 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1526
Brian Norris8b6e50c2011-05-25 14:59:01 -07001527 /*
1528 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001529 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001530 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001531 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1532 if (ret)
1533 return ret;
1534
1535 if (oobregion.length < eccfrag_len)
1536 gaps = 1;
1537
Alexey Korolev3d459552008-05-15 17:23:18 +01001538 if (gaps) {
1539 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1540 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1541 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001542 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001543 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001544 * about buswidth alignment in read_buf.
1545 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001546 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001547 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001548 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001549 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001550 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1551 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001552 aligned_len++;
1553
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001554 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001555 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001556 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1557 }
1558
Boris Brezillon846031d2016-02-03 20:11:00 +01001559 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1560 chip->oob_poi, index, eccfrag_len);
1561 if (ret)
1562 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001563
1564 p = bufpoi + data_col_addr;
1565 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1566 int stat;
1567
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001568 stat = chip->ecc.correct(mtd, p,
1569 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001570 if (stat == -EBADMSG &&
1571 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1572 /* check for empty pages with bitflips */
1573 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1574 &chip->buffers->ecccode[i],
1575 chip->ecc.bytes,
1576 NULL, 0,
1577 chip->ecc.strength);
1578 }
1579
Mike Dunn3f91e942012-04-25 12:06:09 -07001580 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001581 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001582 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001583 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001584 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1585 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001586 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001587 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001588}
1589
1590/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001591 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001592 * @mtd: mtd info structure
1593 * @chip: nand chip info structure
1594 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001595 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001596 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001597 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001598 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001599 */
1600static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001601 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001602{
Boris Brezillon846031d2016-02-03 20:11:00 +01001603 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001604 int eccbytes = chip->ecc.bytes;
1605 int eccsteps = chip->ecc.steps;
1606 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001607 uint8_t *ecc_calc = chip->buffers->ecccalc;
1608 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001609 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001610
1611 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1612 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1613 chip->read_buf(mtd, p, eccsize);
1614 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1615 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001616 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001617
Boris Brezillon846031d2016-02-03 20:11:00 +01001618 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1619 chip->ecc.total);
1620 if (ret)
1621 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001622
1623 eccsteps = chip->ecc.steps;
1624 p = buf;
1625
1626 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1627 int stat;
1628
1629 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001630 if (stat == -EBADMSG &&
1631 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1632 /* check for empty pages with bitflips */
1633 stat = nand_check_erased_ecc_chunk(p, eccsize,
1634 &ecc_code[i], eccbytes,
1635 NULL, 0,
1636 chip->ecc.strength);
1637 }
1638
Mike Dunn3f91e942012-04-25 12:06:09 -07001639 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001640 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001641 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001642 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001643 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1644 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001645 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001646 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001647}
1648
1649/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001650 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001651 * @mtd: mtd info structure
1652 * @chip: nand chip info structure
1653 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001654 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001655 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001656 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001657 * Hardware ECC for large page chips, require OOB to be read first. For this
1658 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1659 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1660 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1661 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001662 */
1663static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001664 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001665{
Boris Brezillon846031d2016-02-03 20:11:00 +01001666 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001667 int eccbytes = chip->ecc.bytes;
1668 int eccsteps = chip->ecc.steps;
1669 uint8_t *p = buf;
1670 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001671 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001672 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001673
1674 /* Read the OOB area first */
1675 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1676 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1677 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1678
Boris Brezillon846031d2016-02-03 20:11:00 +01001679 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1680 chip->ecc.total);
1681 if (ret)
1682 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001683
1684 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1685 int stat;
1686
1687 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1688 chip->read_buf(mtd, p, eccsize);
1689 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1690
1691 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001692 if (stat == -EBADMSG &&
1693 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1694 /* check for empty pages with bitflips */
1695 stat = nand_check_erased_ecc_chunk(p, eccsize,
1696 &ecc_code[i], eccbytes,
1697 NULL, 0,
1698 chip->ecc.strength);
1699 }
1700
Mike Dunn3f91e942012-04-25 12:06:09 -07001701 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001702 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001703 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001704 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001705 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1706 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001707 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001708 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001709}
1710
1711/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001712 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001713 * @mtd: mtd info structure
1714 * @chip: nand chip info structure
1715 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001716 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001717 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001718 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001719 * The hw generator calculates the error syndrome automatically. Therefore we
1720 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001721 */
1722static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001723 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001724{
1725 int i, eccsize = chip->ecc.size;
1726 int eccbytes = chip->ecc.bytes;
1727 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001728 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001729 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001730 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001731 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001732
1733 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1734 int stat;
1735
1736 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1737 chip->read_buf(mtd, p, eccsize);
1738
1739 if (chip->ecc.prepad) {
1740 chip->read_buf(mtd, oob, chip->ecc.prepad);
1741 oob += chip->ecc.prepad;
1742 }
1743
1744 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1745 chip->read_buf(mtd, oob, eccbytes);
1746 stat = chip->ecc.correct(mtd, p, oob, NULL);
1747
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001748 oob += eccbytes;
1749
1750 if (chip->ecc.postpad) {
1751 chip->read_buf(mtd, oob, chip->ecc.postpad);
1752 oob += chip->ecc.postpad;
1753 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001754
1755 if (stat == -EBADMSG &&
1756 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1757 /* check for empty pages with bitflips */
1758 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1759 oob - eccpadbytes,
1760 eccpadbytes,
1761 NULL, 0,
1762 chip->ecc.strength);
1763 }
1764
1765 if (stat < 0) {
1766 mtd->ecc_stats.failed++;
1767 } else {
1768 mtd->ecc_stats.corrected += stat;
1769 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1770 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001771 }
1772
1773 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001774 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001775 if (i)
1776 chip->read_buf(mtd, oob, i);
1777
Mike Dunn3f91e942012-04-25 12:06:09 -07001778 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001779}
1780
1781/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001782 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001783 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001784 * @oob: oob destination address
1785 * @ops: oob ops structure
1786 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001787 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001788static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001789 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001790{
Boris Brezillon846031d2016-02-03 20:11:00 +01001791 struct nand_chip *chip = mtd_to_nand(mtd);
1792 int ret;
1793
Florian Fainellif8ac0412010-09-07 13:23:43 +02001794 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001795
Brian Norris0612b9d2011-08-30 18:45:40 -07001796 case MTD_OPS_PLACE_OOB:
1797 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001798 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1799 return oob + len;
1800
Boris Brezillon846031d2016-02-03 20:11:00 +01001801 case MTD_OPS_AUTO_OOB:
1802 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1803 ops->ooboffs, len);
1804 BUG_ON(ret);
1805 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001806
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001807 default:
1808 BUG();
1809 }
1810 return NULL;
1811}
1812
1813/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001814 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1815 * @mtd: MTD device structure
1816 * @retry_mode: the retry mode to use
1817 *
1818 * Some vendors supply a special command to shift the Vt threshold, to be used
1819 * when there are too many bitflips in a page (i.e., ECC error). After setting
1820 * a new threshold, the host should retry reading the page.
1821 */
1822static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1823{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001824 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001825
1826 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1827
1828 if (retry_mode >= chip->read_retries)
1829 return -EINVAL;
1830
1831 if (!chip->setup_read_retry)
1832 return -EOPNOTSUPP;
1833
1834 return chip->setup_read_retry(mtd, retry_mode);
1835}
1836
1837/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001838 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001839 * @mtd: MTD device structure
1840 * @from: offset to read from
1841 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001842 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001843 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001844 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001845static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1846 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001847{
Brian Norrise47f3db2012-05-02 10:14:56 -07001848 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001849 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001850 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001852 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001853 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001854
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001855 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001856 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001857 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001858 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001859 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001861 chipnr = (int)(from >> chip->chip_shift);
1862 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001864 realpage = (int)(from >> chip->page_shift);
1865 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001867 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001869 buf = ops->datbuf;
1870 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001871 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001872
Florian Fainellif8ac0412010-09-07 13:23:43 +02001873 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001874 unsigned int ecc_failures = mtd->ecc_stats.failed;
1875
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001876 bytes = min(mtd->writesize - col, readlen);
1877 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001878
Kamal Dasu66507c72014-05-01 20:51:19 -04001879 if (!aligned)
1880 use_bufpoi = 1;
1881 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09001882 use_bufpoi = !virt_addr_valid(buf) ||
1883 !IS_ALIGNED((unsigned long)buf,
1884 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04001885 else
1886 use_bufpoi = 0;
1887
Brian Norris8b6e50c2011-05-25 14:59:01 -07001888 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001889 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001890 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1891
1892 if (use_bufpoi && aligned)
1893 pr_debug("%s: using read bounce buffer for buf@%p\n",
1894 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Brian Norrisba84fb52014-01-03 15:13:33 -08001896read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01001897 if (nand_standard_page_accessors(&chip->ecc))
1898 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Mike Dunnedbc45402012-04-25 12:06:11 -07001900 /*
1901 * Now read the page into the buffer. Absent an error,
1902 * the read methods return max bitflips per ecc step.
1903 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001904 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001905 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001906 oob_required,
1907 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001908 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1909 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001910 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001911 col, bytes, bufpoi,
1912 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001913 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001914 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001915 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001916 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001917 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001918 /* Invalidate page cache */
1919 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001920 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001921 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001922
1923 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001924 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001925 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001926 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001927 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001928 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001929 chip->pagebuf_bitflips = ret;
1930 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001931 /* Invalidate page cache */
1932 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001933 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001934 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001936
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001937 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001938 int toread = min(oobreadlen, max_oobsize);
1939
1940 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001941 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001942 oob, ops, toread);
1943 oobreadlen -= toread;
1944 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001946
1947 if (chip->options & NAND_NEED_READRDY) {
1948 /* Apply delay or wait for ready/busy pin */
1949 if (!chip->dev_ready)
1950 udelay(chip->chip_delay);
1951 else
1952 nand_wait_ready(mtd);
1953 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001954
Brian Norrisba84fb52014-01-03 15:13:33 -08001955 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001956 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001957 retry_mode++;
1958 ret = nand_setup_read_retry(mtd,
1959 retry_mode);
1960 if (ret < 0)
1961 break;
1962
1963 /* Reset failures; retry */
1964 mtd->ecc_stats.failed = ecc_failures;
1965 goto read_retry;
1966 } else {
1967 /* No more retry modes; real failure */
1968 ecc_fail = true;
1969 }
1970 }
1971
1972 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09001973 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001974 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001975 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001976 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001977 max_bitflips = max_t(unsigned int, max_bitflips,
1978 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001981 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001982
Brian Norrisba84fb52014-01-03 15:13:33 -08001983 /* Reset to retry mode 0 */
1984 if (retry_mode) {
1985 ret = nand_setup_read_retry(mtd, 0);
1986 if (ret < 0)
1987 break;
1988 retry_mode = 0;
1989 }
1990
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001991 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Brian Norris8b6e50c2011-05-25 14:59:01 -07001994 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 col = 0;
1996 /* Increment page address */
1997 realpage++;
1998
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001999 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* Check, if we cross a chip boundary */
2001 if (!page) {
2002 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002003 chip->select_chip(mtd, -1);
2004 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002007 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002009 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002010 if (oob)
2011 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Mike Dunn3f91e942012-04-25 12:06:09 -07002013 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002014 return ret;
2015
Brian Norrisb72f3df2013-12-03 11:04:14 -08002016 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002017 return -EBADMSG;
2018
Mike Dunnedbc45402012-04-25 12:06:11 -07002019 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002020}
2021
2022/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002023 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002024 * @mtd: MTD device structure
2025 * @from: offset to read from
2026 * @len: number of bytes to read
2027 * @retlen: pointer to variable to store the number of read bytes
2028 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002029 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002030 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002031 */
2032static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2033 size_t *retlen, uint8_t *buf)
2034{
Brian Norris4a89ff82011-08-30 18:45:45 -07002035 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002036 int ret;
2037
Huang Shijie6a8214a2012-11-19 14:43:30 +08002038 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002039 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002040 ops.len = len;
2041 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002042 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002043 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002044 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002045 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002050 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002051 * @mtd: mtd info structure
2052 * @chip: nand chip info structure
2053 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002054 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002055int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002056{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002057 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002058 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002059 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002060}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002061EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002062
2063/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002064 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002065 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002066 * @mtd: mtd info structure
2067 * @chip: nand chip info structure
2068 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002069 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002070int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2071 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002072{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002073 int length = mtd->oobsize;
2074 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2075 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002076 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002077 int i, toread, sndrnd = 0, pos;
2078
2079 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2080 for (i = 0; i < chip->ecc.steps; i++) {
2081 if (sndrnd) {
2082 pos = eccsize + i * (eccsize + chunk);
2083 if (mtd->writesize > 512)
2084 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2085 else
2086 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2087 } else
2088 sndrnd = 1;
2089 toread = min_t(int, length, chunk);
2090 chip->read_buf(mtd, bufpoi, toread);
2091 bufpoi += toread;
2092 length -= toread;
2093 }
2094 if (length > 0)
2095 chip->read_buf(mtd, bufpoi, length);
2096
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002097 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002098}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002099EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002100
2101/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002102 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002103 * @mtd: mtd info structure
2104 * @chip: nand chip info structure
2105 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002106 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002107int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002108{
2109 int status = 0;
2110 const uint8_t *buf = chip->oob_poi;
2111 int length = mtd->oobsize;
2112
2113 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2114 chip->write_buf(mtd, buf, length);
2115 /* Send command to program the OOB data */
2116 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2117
2118 status = chip->waitfunc(mtd, chip);
2119
Savin Zlobec0d420f92006-06-21 11:51:20 +02002120 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002121}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002122EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002123
2124/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002125 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002126 * with syndrome - only for large page flash
2127 * @mtd: mtd info structure
2128 * @chip: nand chip info structure
2129 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002130 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002131int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2132 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002133{
2134 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2135 int eccsize = chip->ecc.size, length = mtd->oobsize;
2136 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2137 const uint8_t *bufpoi = chip->oob_poi;
2138
2139 /*
2140 * data-ecc-data-ecc ... ecc-oob
2141 * or
2142 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2143 */
2144 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2145 pos = steps * (eccsize + chunk);
2146 steps = 0;
2147 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002148 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002149
2150 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2151 for (i = 0; i < steps; i++) {
2152 if (sndcmd) {
2153 if (mtd->writesize <= 512) {
2154 uint32_t fill = 0xFFFFFFFF;
2155
2156 len = eccsize;
2157 while (len > 0) {
2158 int num = min_t(int, len, 4);
2159 chip->write_buf(mtd, (uint8_t *)&fill,
2160 num);
2161 len -= num;
2162 }
2163 } else {
2164 pos = eccsize + i * (eccsize + chunk);
2165 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2166 }
2167 } else
2168 sndcmd = 1;
2169 len = min_t(int, length, chunk);
2170 chip->write_buf(mtd, bufpoi, len);
2171 bufpoi += len;
2172 length -= len;
2173 }
2174 if (length > 0)
2175 chip->write_buf(mtd, bufpoi, length);
2176
2177 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2178 status = chip->waitfunc(mtd, chip);
2179
2180 return status & NAND_STATUS_FAIL ? -EIO : 0;
2181}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002182EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002183
2184/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002185 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002186 * @mtd: MTD device structure
2187 * @from: offset to read from
2188 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002190 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002192static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2193 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Brian Norrisc00a0992012-05-01 17:12:54 -07002195 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002196 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002197 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002198 int readlen = ops->ooblen;
2199 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002200 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002201 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Brian Norris289c0522011-07-19 10:06:09 -07002203 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302204 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Brian Norris041e4572011-06-23 16:45:24 -07002206 stats = mtd->ecc_stats;
2207
Boris BREZILLON29f10582016-03-07 10:46:52 +01002208 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002209
2210 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002211 pr_debug("%s: attempt to start read outside oob\n",
2212 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002213 return -EINVAL;
2214 }
2215
2216 /* Do not allow reads past end of device */
2217 if (unlikely(from >= mtd->size ||
2218 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2219 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002220 pr_debug("%s: attempt to read beyond end of device\n",
2221 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002222 return -EINVAL;
2223 }
Vitaly Wool70145682006-11-03 18:20:38 +03002224
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002225 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002226 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002228 /* Shift to get page */
2229 realpage = (int)(from >> chip->page_shift);
2230 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Florian Fainellif8ac0412010-09-07 13:23:43 +02002232 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002233 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002234 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002235 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002236 ret = chip->ecc.read_oob(mtd, chip, page);
2237
2238 if (ret < 0)
2239 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002240
2241 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002242 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002243
Brian Norris5bc7c332013-03-13 09:51:31 -07002244 if (chip->options & NAND_NEED_READRDY) {
2245 /* Apply delay or wait for ready/busy pin */
2246 if (!chip->dev_ready)
2247 udelay(chip->chip_delay);
2248 else
2249 nand_wait_ready(mtd);
2250 }
2251
Vitaly Wool70145682006-11-03 18:20:38 +03002252 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002253 if (!readlen)
2254 break;
2255
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002256 /* Increment page address */
2257 realpage++;
2258
2259 page = realpage & chip->pagemask;
2260 /* Check, if we cross a chip boundary */
2261 if (!page) {
2262 chipnr++;
2263 chip->select_chip(mtd, -1);
2264 chip->select_chip(mtd, chipnr);
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002267 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002269 ops->oobretlen = ops->ooblen - readlen;
2270
2271 if (ret < 0)
2272 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002273
2274 if (mtd->ecc_stats.failed - stats.failed)
2275 return -EBADMSG;
2276
2277 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002281 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002282 * @mtd: MTD device structure
2283 * @from: offset to read from
2284 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002286 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002288static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2289 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002291 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002292
2293 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002296 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002297 pr_debug("%s: attempt to read beyond end of device\n",
2298 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return -EINVAL;
2300 }
2301
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002302 if (ops->mode != MTD_OPS_PLACE_OOB &&
2303 ops->mode != MTD_OPS_AUTO_OOB &&
2304 ops->mode != MTD_OPS_RAW)
2305 return -ENOTSUPP;
2306
Huang Shijie6a8214a2012-11-19 14:43:30 +08002307 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002309 if (!ops->datbuf)
2310 ret = nand_do_read_oob(mtd, from, ops);
2311 else
2312 ret = nand_do_read_ops(mtd, from, ops);
2313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002315 return ret;
2316}
2317
2318
2319/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002320 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002321 * @mtd: mtd info structure
2322 * @chip: nand chip info structure
2323 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002324 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002325 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002326 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002327 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002329int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2330 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002331{
2332 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002333 if (oob_required)
2334 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002335
2336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002338EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002340/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002341 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002342 * @mtd: mtd info structure
2343 * @chip: nand chip info structure
2344 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002345 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002346 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002347 *
2348 * We need a special oob layout and handling even when ECC isn't checked.
2349 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002350static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002351 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002352 const uint8_t *buf, int oob_required,
2353 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002354{
2355 int eccsize = chip->ecc.size;
2356 int eccbytes = chip->ecc.bytes;
2357 uint8_t *oob = chip->oob_poi;
2358 int steps, size;
2359
2360 for (steps = chip->ecc.steps; steps > 0; steps--) {
2361 chip->write_buf(mtd, buf, eccsize);
2362 buf += eccsize;
2363
2364 if (chip->ecc.prepad) {
2365 chip->write_buf(mtd, oob, chip->ecc.prepad);
2366 oob += chip->ecc.prepad;
2367 }
2368
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002369 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002370 oob += eccbytes;
2371
2372 if (chip->ecc.postpad) {
2373 chip->write_buf(mtd, oob, chip->ecc.postpad);
2374 oob += chip->ecc.postpad;
2375 }
2376 }
2377
2378 size = mtd->oobsize - (oob - chip->oob_poi);
2379 if (size)
2380 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002381
2382 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002383}
2384/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002385 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002386 * @mtd: mtd info structure
2387 * @chip: nand chip info structure
2388 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002389 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002390 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002391 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002392static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002393 const uint8_t *buf, int oob_required,
2394 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002395{
Boris Brezillon846031d2016-02-03 20:11:00 +01002396 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002397 int eccbytes = chip->ecc.bytes;
2398 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002399 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002400 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002401
Brian Norris7854d3f2011-06-23 14:12:08 -07002402 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002403 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2404 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002405
Boris Brezillon846031d2016-02-03 20:11:00 +01002406 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2407 chip->ecc.total);
2408 if (ret)
2409 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002410
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002411 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002412}
2413
2414/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002415 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002416 * @mtd: mtd info structure
2417 * @chip: nand chip info structure
2418 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002419 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002420 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002421 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002422static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002423 const uint8_t *buf, int oob_required,
2424 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002425{
Boris Brezillon846031d2016-02-03 20:11:00 +01002426 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002427 int eccbytes = chip->ecc.bytes;
2428 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002429 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002430 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002431
2432 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2433 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002434 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002435 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2436 }
2437
Boris Brezillon846031d2016-02-03 20:11:00 +01002438 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2439 chip->ecc.total);
2440 if (ret)
2441 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442
2443 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002444
2445 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002446}
2447
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302448
2449/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002450 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302451 * @mtd: mtd info structure
2452 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002453 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302454 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002455 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302456 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002457 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302458 */
2459static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2460 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002461 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002462 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302463{
2464 uint8_t *oob_buf = chip->oob_poi;
2465 uint8_t *ecc_calc = chip->buffers->ecccalc;
2466 int ecc_size = chip->ecc.size;
2467 int ecc_bytes = chip->ecc.bytes;
2468 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302469 uint32_t start_step = offset / ecc_size;
2470 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2471 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002472 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302473
2474 for (step = 0; step < ecc_steps; step++) {
2475 /* configure controller for WRITE access */
2476 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2477
2478 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002479 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302480
2481 /* mask ECC of un-touched subpages by padding 0xFF */
2482 if ((step < start_step) || (step > end_step))
2483 memset(ecc_calc, 0xff, ecc_bytes);
2484 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002485 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302486
2487 /* mask OOB of un-touched subpages by padding 0xFF */
2488 /* if oob_required, preserve OOB metadata of written subpage */
2489 if (!oob_required || (step < start_step) || (step > end_step))
2490 memset(oob_buf, 0xff, oob_bytes);
2491
Brian Norrisd6a950802013-08-08 17:16:36 -07002492 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302493 ecc_calc += ecc_bytes;
2494 oob_buf += oob_bytes;
2495 }
2496
2497 /* copy calculated ECC for whole page to chip->buffer->oob */
2498 /* this include masked-value(0xFF) for unwritten subpages */
2499 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002500 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2501 chip->ecc.total);
2502 if (ret)
2503 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302504
2505 /* write OOB buffer to NAND device */
2506 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2507
2508 return 0;
2509}
2510
2511
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002512/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002513 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002514 * @mtd: mtd info structure
2515 * @chip: nand chip info structure
2516 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002517 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002518 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002519 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002520 * The hw generator calculates the error syndrome automatically. Therefore we
2521 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002522 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002523static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002524 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002525 const uint8_t *buf, int oob_required,
2526 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002527{
2528 int i, eccsize = chip->ecc.size;
2529 int eccbytes = chip->ecc.bytes;
2530 int eccsteps = chip->ecc.steps;
2531 const uint8_t *p = buf;
2532 uint8_t *oob = chip->oob_poi;
2533
2534 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2535
2536 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2537 chip->write_buf(mtd, p, eccsize);
2538
2539 if (chip->ecc.prepad) {
2540 chip->write_buf(mtd, oob, chip->ecc.prepad);
2541 oob += chip->ecc.prepad;
2542 }
2543
2544 chip->ecc.calculate(mtd, p, oob);
2545 chip->write_buf(mtd, oob, eccbytes);
2546 oob += eccbytes;
2547
2548 if (chip->ecc.postpad) {
2549 chip->write_buf(mtd, oob, chip->ecc.postpad);
2550 oob += chip->ecc.postpad;
2551 }
2552 }
2553
2554 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002555 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002556 if (i)
2557 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002558
2559 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002560}
2561
2562/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002563 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002564 * @mtd: MTD device structure
2565 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302566 * @offset: address offset within the page
2567 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002568 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002569 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002570 * @page: page number to write
2571 * @cached: cached programming
2572 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002573 */
2574static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302575 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002576 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002577{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302578 int status, subpage;
2579
2580 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2581 chip->ecc.write_subpage)
2582 subpage = offset || (data_len < mtd->writesize);
2583 else
2584 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002585
Marc Gonzalez3371d662016-11-15 10:56:20 +01002586 if (nand_standard_page_accessors(&chip->ecc))
2587 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002588
David Woodhouse956e9442006-09-25 17:12:39 +01002589 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302590 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002591 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302592 else if (subpage)
2593 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002594 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002595 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002596 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2597 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002598
2599 if (status < 0)
2600 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002601
Boris Brezillon41145642017-05-16 18:27:49 +02002602 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002603 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002604
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002605 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002606 if (status & NAND_STATUS_FAIL)
2607 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002608 }
2609
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002610 return 0;
2611}
2612
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002613/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002614 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002615 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002616 * @oob: oob data buffer
2617 * @len: oob data write length
2618 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002619 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002620static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2621 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002622{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002623 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002624 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002625
2626 /*
2627 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2628 * data from a previous OOB read.
2629 */
2630 memset(chip->oob_poi, 0xff, mtd->oobsize);
2631
Florian Fainellif8ac0412010-09-07 13:23:43 +02002632 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002633
Brian Norris0612b9d2011-08-30 18:45:40 -07002634 case MTD_OPS_PLACE_OOB:
2635 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002636 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2637 return oob + len;
2638
Boris Brezillon846031d2016-02-03 20:11:00 +01002639 case MTD_OPS_AUTO_OOB:
2640 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2641 ops->ooboffs, len);
2642 BUG_ON(ret);
2643 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002644
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002645 default:
2646 BUG();
2647 }
2648 return NULL;
2649}
2650
Florian Fainellif8ac0412010-09-07 13:23:43 +02002651#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002652
2653/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002654 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002655 * @mtd: MTD device structure
2656 * @to: offset to write to
2657 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002658 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002659 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002660 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002661static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2662 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002663{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002664 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002665 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002666 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002667
2668 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002669 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002670
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002671 uint8_t *oob = ops->oobbuf;
2672 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302673 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002674 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002675
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002676 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002677 if (!writelen)
2678 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002679
Brian Norris8b6e50c2011-05-25 14:59:01 -07002680 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002681 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002682 pr_notice("%s: attempt to write non page aligned data\n",
2683 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002684 return -EINVAL;
2685 }
2686
Thomas Gleixner29072b92006-09-28 15:38:36 +02002687 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002688
Thomas Gleixner6a930962006-06-28 00:11:45 +02002689 chipnr = (int)(to >> chip->chip_shift);
2690 chip->select_chip(mtd, chipnr);
2691
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002692 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002693 if (nand_check_wp(mtd)) {
2694 ret = -EIO;
2695 goto err_out;
2696 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002697
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002698 realpage = (int)(to >> chip->page_shift);
2699 page = realpage & chip->pagemask;
2700 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2701
2702 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002703 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2704 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002705 chip->pagebuf = -1;
2706
Maxim Levitsky782ce792010-02-22 20:39:36 +02002707 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002708 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2709 ret = -EINVAL;
2710 goto err_out;
2711 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002712
Florian Fainellif8ac0412010-09-07 13:23:43 +02002713 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002714 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002715 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002716 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002717 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002718
Kamal Dasu66507c72014-05-01 20:51:19 -04002719 if (part_pagewr)
2720 use_bufpoi = 1;
2721 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002722 use_bufpoi = !virt_addr_valid(buf) ||
2723 !IS_ALIGNED((unsigned long)buf,
2724 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002725 else
2726 use_bufpoi = 0;
2727
2728 /* Partial page write?, or need to use bounce buffer */
2729 if (use_bufpoi) {
2730 pr_debug("%s: using write bounce buffer for buf@%p\n",
2731 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002732 if (part_pagewr)
2733 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002734 chip->pagebuf = -1;
2735 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2736 memcpy(&chip->buffers->databuf[column], buf, bytes);
2737 wbuf = chip->buffers->databuf;
2738 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002739
Maxim Levitsky782ce792010-02-22 20:39:36 +02002740 if (unlikely(oob)) {
2741 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002742 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002743 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002744 } else {
2745 /* We still need to erase leftover OOB data */
2746 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002747 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002748
2749 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002750 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002751 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002752 if (ret)
2753 break;
2754
2755 writelen -= bytes;
2756 if (!writelen)
2757 break;
2758
Thomas Gleixner29072b92006-09-28 15:38:36 +02002759 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002760 buf += bytes;
2761 realpage++;
2762
2763 page = realpage & chip->pagemask;
2764 /* Check, if we cross a chip boundary */
2765 if (!page) {
2766 chipnr++;
2767 chip->select_chip(mtd, -1);
2768 chip->select_chip(mtd, chipnr);
2769 }
2770 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002771
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002772 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002773 if (unlikely(oob))
2774 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002775
2776err_out:
2777 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002778 return ret;
2779}
2780
2781/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002782 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002783 * @mtd: MTD device structure
2784 * @to: offset to write to
2785 * @len: number of bytes to write
2786 * @retlen: pointer to variable to store the number of written bytes
2787 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002788 *
2789 * NAND write with ECC. Used when performing writes in interrupt context, this
2790 * may for example be called by mtdoops when writing an oops while in panic.
2791 */
2792static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2793 size_t *retlen, const uint8_t *buf)
2794{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002795 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002796 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002797 int ret;
2798
Brian Norris8b6e50c2011-05-25 14:59:01 -07002799 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002800 panic_nand_wait(mtd, chip, 400);
2801
Brian Norris8b6e50c2011-05-25 14:59:01 -07002802 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002803 panic_nand_get_device(chip, mtd, FL_WRITING);
2804
Brian Norris0ec56dc2015-02-28 02:02:30 -08002805 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002806 ops.len = len;
2807 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002808 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002809
Brian Norris4a89ff82011-08-30 18:45:45 -07002810 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002811
Brian Norris4a89ff82011-08-30 18:45:45 -07002812 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002813 return ret;
2814}
2815
2816/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002817 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002818 * @mtd: MTD device structure
2819 * @to: offset to write to
2820 * @len: number of bytes to write
2821 * @retlen: pointer to variable to store the number of written bytes
2822 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002824 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002826static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002827 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Brian Norris4a89ff82011-08-30 18:45:45 -07002829 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002830 int ret;
2831
Huang Shijie6a8214a2012-11-19 14:43:30 +08002832 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002833 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002834 ops.len = len;
2835 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002836 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002837 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002838 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002839 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002840 return ret;
2841}
2842
2843/**
2844 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002845 * @mtd: MTD device structure
2846 * @to: offset to write to
2847 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002848 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002849 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002850 */
2851static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2852 struct mtd_oob_ops *ops)
2853{
Adrian Hunter03736152007-01-31 17:58:29 +02002854 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002855 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Brian Norris289c0522011-07-19 10:06:09 -07002857 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302858 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Boris BREZILLON29f10582016-03-07 10:46:52 +01002860 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002863 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002864 pr_debug("%s: attempt to write past end of page\n",
2865 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return -EINVAL;
2867 }
2868
Adrian Hunter03736152007-01-31 17:58:29 +02002869 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002870 pr_debug("%s: attempt to start write outside oob\n",
2871 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002872 return -EINVAL;
2873 }
2874
Jason Liu775adc3d42011-02-25 13:06:18 +08002875 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002876 if (unlikely(to >= mtd->size ||
2877 ops->ooboffs + ops->ooblen >
2878 ((mtd->size >> chip->page_shift) -
2879 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002880 pr_debug("%s: attempt to write beyond end of device\n",
2881 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002882 return -EINVAL;
2883 }
2884
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002885 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002886
2887 /*
2888 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2889 * of my DiskOnChip 2000 test units) will clear the whole data page too
2890 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2891 * it in the doc2000 driver in August 1999. dwmw2.
2892 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02002893 nand_reset(chip, chipnr);
2894
2895 chip->select_chip(mtd, chipnr);
2896
2897 /* Shift to get page */
2898 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002901 if (nand_check_wp(mtd)) {
2902 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002903 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002904 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002907 if (page == chip->pagebuf)
2908 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002910 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002911
Brian Norris0612b9d2011-08-30 18:45:40 -07002912 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002913 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2914 else
2915 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002916
Huang Shijieb0bb6902012-11-19 14:43:29 +08002917 chip->select_chip(mtd, -1);
2918
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002919 if (status)
2920 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Vitaly Wool70145682006-11-03 18:20:38 +03002922 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002924 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002927/**
2928 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002929 * @mtd: MTD device structure
2930 * @to: offset to write to
2931 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002932 */
2933static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2934 struct mtd_oob_ops *ops)
2935{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002936 int ret = -ENOTSUPP;
2937
2938 ops->retlen = 0;
2939
2940 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002941 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002942 pr_debug("%s: attempt to write beyond end of device\n",
2943 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002944 return -EINVAL;
2945 }
2946
Huang Shijie6a8214a2012-11-19 14:43:30 +08002947 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002948
Florian Fainellif8ac0412010-09-07 13:23:43 +02002949 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002950 case MTD_OPS_PLACE_OOB:
2951 case MTD_OPS_AUTO_OOB:
2952 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002953 break;
2954
2955 default:
2956 goto out;
2957 }
2958
2959 if (!ops->datbuf)
2960 ret = nand_do_write_oob(mtd, to, ops);
2961 else
2962 ret = nand_do_write_ops(mtd, to, ops);
2963
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002964out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002965 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return ret;
2967}
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969/**
Brian Norris49c50b92014-05-06 16:02:19 -07002970 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * @mtd: MTD device structure
2972 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 *
Brian Norris49c50b92014-05-06 16:02:19 -07002974 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 */
Brian Norris49c50b92014-05-06 16:02:19 -07002976static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002978 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002980 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2981 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002982
2983 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
2986/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002988 * @mtd: MTD device structure
2989 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002991 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002993static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
David Woodhousee0c7d762006-05-13 18:07:53 +01002995 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002999 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003000 * @mtd: MTD device structure
3001 * @instr: erase instruction
3002 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003004 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003006int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3007 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Adrian Hunter69423d92008-12-10 13:37:21 +00003009 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003010 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003011 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Brian Norris289c0522011-07-19 10:06:09 -07003013 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3014 __func__, (unsigned long long)instr->addr,
3015 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303017 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003021 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003024 page = (int)(instr->addr >> chip->page_shift);
3025 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003028 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003031 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /* Check, if it is write protected */
3034 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003035 pr_debug("%s: device is write protected!\n",
3036 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 instr->state = MTD_ERASE_FAILED;
3038 goto erase_exit;
3039 }
3040
3041 /* Loop through the pages */
3042 len = instr->len;
3043
3044 instr->state = MTD_ERASING;
3045
3046 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003047 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003048 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303049 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003050 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3051 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 instr->state = MTD_ERASE_FAILED;
3053 goto erase_exit;
3054 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003055
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003056 /*
3057 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003058 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003059 */
3060 if (page <= chip->pagebuf && chip->pagebuf <
3061 (page + pages_per_block))
3062 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Brian Norris49c50b92014-05-06 16:02:19 -07003064 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003067 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003068 pr_debug("%s: failed erase, page 0x%08x\n",
3069 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003071 instr->fail_addr =
3072 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 goto erase_exit;
3074 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003077 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 page += pages_per_block;
3079
3080 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003081 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003083 chip->select_chip(mtd, -1);
3084 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086 }
3087 instr->state = MTD_ERASE_DONE;
3088
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003089erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003094 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 nand_release_device(mtd);
3096
David Woodhouse49defc02007-10-06 15:01:59 -04003097 /* Do call back function */
3098 if (!ret)
3099 mtd_erase_callback(instr);
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* Return more or less happy */
3102 return ret;
3103}
3104
3105/**
3106 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003107 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003109 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003111static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Brian Norris289c0522011-07-19 10:06:09 -07003113 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003116 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003118 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003122 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003123 * @mtd: MTD device structure
3124 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003126static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303128 struct nand_chip *chip = mtd_to_nand(mtd);
3129 int chipnr = (int)(offs >> chip->chip_shift);
3130 int ret;
3131
3132 /* Select the NAND device */
3133 nand_get_device(mtd, FL_READING);
3134 chip->select_chip(mtd, chipnr);
3135
3136 ret = nand_block_checkbad(mtd, offs, 0);
3137
3138 chip->select_chip(mtd, -1);
3139 nand_release_device(mtd);
3140
3141 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
3144/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003145 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003146 * @mtd: MTD device structure
3147 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003149static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 int ret;
3152
Florian Fainellif8ac0412010-09-07 13:23:43 +02003153 ret = nand_block_isbad(mtd, ofs);
3154 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003155 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (ret > 0)
3157 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003158 return ret;
3159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Brian Norris5a0edb22013-07-30 17:52:58 -07003161 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
3164/**
Zach Brown56718422017-01-10 13:30:20 -06003165 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3166 * @mtd: MTD device structure
3167 * @ofs: offset relative to mtd start
3168 * @len: length of mtd
3169 */
3170static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3171{
3172 struct nand_chip *chip = mtd_to_nand(mtd);
3173 u32 part_start_block;
3174 u32 part_end_block;
3175 u32 part_start_die;
3176 u32 part_end_die;
3177
3178 /*
3179 * max_bb_per_die and blocks_per_die used to determine
3180 * the maximum bad block count.
3181 */
3182 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3183 return -ENOTSUPP;
3184
3185 /* Get the start and end of the partition in erase blocks. */
3186 part_start_block = mtd_div_by_eb(ofs, mtd);
3187 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3188
3189 /* Get the start and end LUNs of the partition. */
3190 part_start_die = part_start_block / chip->blocks_per_die;
3191 part_end_die = part_end_block / chip->blocks_per_die;
3192
3193 /*
3194 * Look up the bad blocks per unit and multiply by the number of units
3195 * that the partition spans.
3196 */
3197 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3198}
3199
3200/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003201 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3202 * @mtd: MTD device structure
3203 * @chip: nand chip info structure
3204 * @addr: feature address.
3205 * @subfeature_param: the subfeature parameters, a four bytes array.
3206 */
3207static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3208 int addr, uint8_t *subfeature_param)
3209{
3210 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003211 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003212
David Mosbergerd914c932013-05-29 15:30:13 +03003213 if (!chip->onfi_version ||
3214 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3215 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003216 return -EINVAL;
3217
3218 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003219 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3220 chip->write_byte(mtd, subfeature_param[i]);
3221
Huang Shijie7db03ec2012-09-13 14:57:52 +08003222 status = chip->waitfunc(mtd, chip);
3223 if (status & NAND_STATUS_FAIL)
3224 return -EIO;
3225 return 0;
3226}
3227
3228/**
3229 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3230 * @mtd: MTD device structure
3231 * @chip: nand chip info structure
3232 * @addr: feature address.
3233 * @subfeature_param: the subfeature parameters, a four bytes array.
3234 */
3235static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3236 int addr, uint8_t *subfeature_param)
3237{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003238 int i;
3239
David Mosbergerd914c932013-05-29 15:30:13 +03003240 if (!chip->onfi_version ||
3241 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3242 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003243 return -EINVAL;
3244
Huang Shijie7db03ec2012-09-13 14:57:52 +08003245 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003246 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3247 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003248 return 0;
3249}
3250
3251/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003252 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3253 * -ENOTSUPP
3254 * @mtd: MTD device structure
3255 * @chip: nand chip info structure
3256 * @addr: feature address.
3257 * @subfeature_param: the subfeature parameters, a four bytes array.
3258 *
3259 * Should be used by NAND controller drivers that do not support the SET/GET
3260 * FEATURES operations.
3261 */
3262int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3263 struct nand_chip *chip, int addr,
3264 u8 *subfeature_param)
3265{
3266 return -ENOTSUPP;
3267}
3268EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3269
3270/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003271 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003272 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003273 */
3274static int nand_suspend(struct mtd_info *mtd)
3275{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003276 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003277}
3278
3279/**
3280 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003281 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003282 */
3283static void nand_resume(struct mtd_info *mtd)
3284{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003285 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003286
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003287 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003288 nand_release_device(mtd);
3289 else
Brian Norrisd0370212011-07-19 10:06:08 -07003290 pr_err("%s called for a chip which is not in suspended state\n",
3291 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003292}
3293
Scott Branden72ea4032014-11-20 11:18:05 -08003294/**
3295 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3296 * prevent further operations
3297 * @mtd: MTD device structure
3298 */
3299static void nand_shutdown(struct mtd_info *mtd)
3300{
Brian Norris9ca641b2015-11-09 16:37:28 -08003301 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003302}
3303
Brian Norris8b6e50c2011-05-25 14:59:01 -07003304/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003305static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003306{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003307 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003310 if (!chip->chip_delay)
3311 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003314 if (chip->cmdfunc == NULL)
3315 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003318 if (chip->waitfunc == NULL)
3319 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003321 if (!chip->select_chip)
3322 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003323
Huang Shijie4204ccc2013-08-16 10:10:07 +08003324 /* set for ONFI nand */
3325 if (!chip->onfi_set_features)
3326 chip->onfi_set_features = nand_onfi_set_features;
3327 if (!chip->onfi_get_features)
3328 chip->onfi_get_features = nand_onfi_get_features;
3329
Brian Norris68e80782013-07-18 01:17:02 -07003330 /* If called twice, pointers that depend on busw may need to be reset */
3331 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003332 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3333 if (!chip->read_word)
3334 chip->read_word = nand_read_word;
3335 if (!chip->block_bad)
3336 chip->block_bad = nand_block_bad;
3337 if (!chip->block_markbad)
3338 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003339 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003340 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003341 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3342 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003343 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003344 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003345 if (!chip->scan_bbt)
3346 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003347
3348 if (!chip->controller) {
3349 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003350 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003351 }
3352
Masahiro Yamada477544c2017-03-30 17:15:05 +09003353 if (!chip->buf_align)
3354 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003355}
3356
Brian Norris8b6e50c2011-05-25 14:59:01 -07003357/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003358static void sanitize_string(uint8_t *s, size_t len)
3359{
3360 ssize_t i;
3361
Brian Norris8b6e50c2011-05-25 14:59:01 -07003362 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003363 s[len - 1] = 0;
3364
Brian Norris8b6e50c2011-05-25 14:59:01 -07003365 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003366 for (i = 0; i < len - 1; i++) {
3367 if (s[i] < ' ' || s[i] > 127)
3368 s[i] = '?';
3369 }
3370
Brian Norris8b6e50c2011-05-25 14:59:01 -07003371 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003372 strim(s);
3373}
3374
3375static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3376{
3377 int i;
3378 while (len--) {
3379 crc ^= *p++ << 8;
3380 for (i = 0; i < 8; i++)
3381 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3382 }
3383
3384 return crc;
3385}
3386
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003387/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003388static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3389 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003390{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003391 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003392 struct onfi_ext_param_page *ep;
3393 struct onfi_ext_section *s;
3394 struct onfi_ext_ecc_info *ecc;
3395 uint8_t *cursor;
3396 int ret = -EINVAL;
3397 int len;
3398 int i;
3399
3400 len = le16_to_cpu(p->ext_param_page_length) * 16;
3401 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003402 if (!ep)
3403 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003404
3405 /* Send our own NAND_CMD_PARAM. */
3406 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3407
3408 /* Use the Change Read Column command to skip the ONFI param pages. */
3409 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3410 sizeof(*p) * p->num_of_param_pages , -1);
3411
3412 /* Read out the Extended Parameter Page. */
3413 chip->read_buf(mtd, (uint8_t *)ep, len);
3414 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3415 != le16_to_cpu(ep->crc))) {
3416 pr_debug("fail in the CRC.\n");
3417 goto ext_out;
3418 }
3419
3420 /*
3421 * Check the signature.
3422 * Do not strictly follow the ONFI spec, maybe changed in future.
3423 */
3424 if (strncmp(ep->sig, "EPPS", 4)) {
3425 pr_debug("The signature is invalid.\n");
3426 goto ext_out;
3427 }
3428
3429 /* find the ECC section. */
3430 cursor = (uint8_t *)(ep + 1);
3431 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3432 s = ep->sections + i;
3433 if (s->type == ONFI_SECTION_TYPE_2)
3434 break;
3435 cursor += s->length * 16;
3436 }
3437 if (i == ONFI_EXT_SECTION_MAX) {
3438 pr_debug("We can not find the ECC section.\n");
3439 goto ext_out;
3440 }
3441
3442 /* get the info we want. */
3443 ecc = (struct onfi_ext_ecc_info *)cursor;
3444
Brian Norris4ae7d222013-09-16 18:20:21 -07003445 if (!ecc->codeword_size) {
3446 pr_debug("Invalid codeword size\n");
3447 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003448 }
3449
Brian Norris4ae7d222013-09-16 18:20:21 -07003450 chip->ecc_strength_ds = ecc->ecc_bits;
3451 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003452 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003453
3454ext_out:
3455 kfree(ep);
3456 return ret;
3457}
3458
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003459/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003460 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003461 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003462static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003463{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003464 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003465 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003466 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003467 int val;
3468
Brian Norris7854d3f2011-06-23 14:12:08 -07003469 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003470 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3471 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3472 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3473 return 0;
3474
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003475 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3476 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003477 for (j = 0; j < sizeof(*p); j++)
3478 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003479 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3480 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003481 break;
3482 }
3483 }
3484
Brian Norrisc7f23a72013-08-13 10:51:55 -07003485 if (i == 3) {
3486 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003487 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003488 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003489
Brian Norris8b6e50c2011-05-25 14:59:01 -07003490 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003491 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003492 if (val & (1 << 5))
3493 chip->onfi_version = 23;
3494 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003495 chip->onfi_version = 22;
3496 else if (val & (1 << 3))
3497 chip->onfi_version = 21;
3498 else if (val & (1 << 2))
3499 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003500 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003501 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003502
3503 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003504 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003505 return 0;
3506 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003507
3508 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3509 sanitize_string(p->model, sizeof(p->model));
3510 if (!mtd->name)
3511 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003512
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003513 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003514
3515 /*
3516 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3517 * (don't ask me who thought of this...). MTD assumes that these
3518 * dimensions will be power-of-2, so just truncate the remaining area.
3519 */
3520 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3521 mtd->erasesize *= mtd->writesize;
3522
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003523 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003524
3525 /* See erasesize comment */
3526 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003527 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003528 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003529
Zach Brown34da5f52017-01-10 13:30:21 -06003530 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3531 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3532
Huang Shijiee2985fc2013-05-17 11:17:30 +08003533 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003534 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003535
Huang Shijie10c86ba2013-05-17 11:17:26 +08003536 if (p->ecc_bits != 0xff) {
3537 chip->ecc_strength_ds = p->ecc_bits;
3538 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003539 } else if (chip->onfi_version >= 21 &&
3540 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3541
3542 /*
3543 * The nand_flash_detect_ext_param_page() uses the
3544 * Change Read Column command which maybe not supported
3545 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3546 * now. We do not replace user supplied command function.
3547 */
3548 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3549 chip->cmdfunc = nand_command_lp;
3550
3551 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003552 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003553 pr_warn("Failed to detect ONFI extended param page\n");
3554 } else {
3555 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003556 }
3557
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003558 return 1;
3559}
3560
3561/*
Huang Shijie91361812014-02-21 13:39:40 +08003562 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3563 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003564static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003565{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003566 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003567 struct nand_jedec_params *p = &chip->jedec_params;
3568 struct jedec_ecc_info *ecc;
3569 int val;
3570 int i, j;
3571
3572 /* Try JEDEC for unknown chip or LP */
3573 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3574 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3575 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3576 chip->read_byte(mtd) != 'C')
3577 return 0;
3578
3579 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3580 for (i = 0; i < 3; i++) {
3581 for (j = 0; j < sizeof(*p); j++)
3582 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3583
3584 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3585 le16_to_cpu(p->crc))
3586 break;
3587 }
3588
3589 if (i == 3) {
3590 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3591 return 0;
3592 }
3593
3594 /* Check version */
3595 val = le16_to_cpu(p->revision);
3596 if (val & (1 << 2))
3597 chip->jedec_version = 10;
3598 else if (val & (1 << 1))
3599 chip->jedec_version = 1; /* vendor specific version */
3600
3601 if (!chip->jedec_version) {
3602 pr_info("unsupported JEDEC version: %d\n", val);
3603 return 0;
3604 }
3605
3606 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3607 sanitize_string(p->model, sizeof(p->model));
3608 if (!mtd->name)
3609 mtd->name = p->model;
3610
3611 mtd->writesize = le32_to_cpu(p->byte_per_page);
3612
3613 /* Please reference to the comment for nand_flash_detect_onfi. */
3614 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3615 mtd->erasesize *= mtd->writesize;
3616
3617 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3618
3619 /* Please reference to the comment for nand_flash_detect_onfi. */
3620 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3621 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3622 chip->bits_per_cell = p->bits_per_cell;
3623
3624 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003625 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003626
3627 /* ECC info */
3628 ecc = &p->ecc_info[0];
3629
3630 if (ecc->codeword_size >= 9) {
3631 chip->ecc_strength_ds = ecc->ecc_bits;
3632 chip->ecc_step_ds = 1 << ecc->codeword_size;
3633 } else {
3634 pr_warn("Invalid codeword size\n");
3635 }
3636
3637 return 1;
3638}
3639
3640/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003641 * nand_id_has_period - Check if an ID string has a given wraparound period
3642 * @id_data: the ID string
3643 * @arrlen: the length of the @id_data array
3644 * @period: the period of repitition
3645 *
3646 * Check if an ID string is repeated within a given sequence of bytes at
3647 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003648 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003649 * if the repetition has a period of @period; otherwise, returns zero.
3650 */
3651static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3652{
3653 int i, j;
3654 for (i = 0; i < period; i++)
3655 for (j = i + period; j < arrlen; j += period)
3656 if (id_data[i] != id_data[j])
3657 return 0;
3658 return 1;
3659}
3660
3661/*
3662 * nand_id_len - Get the length of an ID string returned by CMD_READID
3663 * @id_data: the ID string
3664 * @arrlen: the length of the @id_data array
3665
3666 * Returns the length of the ID string, according to known wraparound/trailing
3667 * zero patterns. If no pattern exists, returns the length of the array.
3668 */
3669static int nand_id_len(u8 *id_data, int arrlen)
3670{
3671 int last_nonzero, period;
3672
3673 /* Find last non-zero byte */
3674 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3675 if (id_data[last_nonzero])
3676 break;
3677
3678 /* All zeros */
3679 if (last_nonzero < 0)
3680 return 0;
3681
3682 /* Calculate wraparound period */
3683 for (period = 1; period < arrlen; period++)
3684 if (nand_id_has_period(id_data, arrlen, period))
3685 break;
3686
3687 /* There's a repeated pattern */
3688 if (period < arrlen)
3689 return period;
3690
3691 /* There are trailing zeros */
3692 if (last_nonzero < arrlen - 1)
3693 return last_nonzero + 1;
3694
3695 /* No pattern detected */
3696 return arrlen;
3697}
3698
Huang Shijie7db906b2013-09-25 14:58:11 +08003699/* Extract the bits of per cell from the 3rd byte of the extended ID */
3700static int nand_get_bits_per_cell(u8 cellinfo)
3701{
3702 int bits;
3703
3704 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3705 bits >>= NAND_CI_CELLTYPE_SHIFT;
3706 return bits + 1;
3707}
3708
Brian Norrise3b88bd2012-09-24 20:40:52 -07003709/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003710 * Many new NAND share similar device ID codes, which represent the size of the
3711 * chip. The rest of the parameters must be decoded according to generic or
3712 * manufacturer-specific "extended ID" decoding patterns.
3713 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003714void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003715{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003716 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003717 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003718 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003719 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003720 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003721 /* The 4th id byte is the important one */
3722 extid = id_data[3];
3723
Boris Brezillon01389b62016-06-08 10:30:18 +02003724 /* Calc pagesize */
3725 mtd->writesize = 1024 << (extid & 0x03);
3726 extid >>= 2;
3727 /* Calc oobsize */
3728 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3729 extid >>= 2;
3730 /* Calc blocksize. Blocksize is multiples of 64KiB */
3731 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3732 extid >>= 2;
3733 /* Get buswidth information */
3734 if (extid & 0x1)
3735 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003736}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003737EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003738
3739/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003740 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3741 * decodes a matching ID table entry and assigns the MTD size parameters for
3742 * the chip.
3743 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003744static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003745{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003746 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003747
3748 mtd->erasesize = type->erasesize;
3749 mtd->writesize = type->pagesize;
3750 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003751
Huang Shijie1c195e92013-09-25 14:58:12 +08003752 /* All legacy ID NAND are small-page, SLC */
3753 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003754}
3755
3756/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003757 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3758 * heuristic patterns using various detected parameters (e.g., manufacturer,
3759 * page size, cell-type information).
3760 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003761static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003762{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003763 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003764
3765 /* Set the bad block position */
3766 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3767 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3768 else
3769 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003770}
3771
Huang Shijieec6e87e2013-03-15 11:01:00 +08003772static inline bool is_full_id_nand(struct nand_flash_dev *type)
3773{
3774 return type->id_len;
3775}
3776
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003777static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003778 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003779{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003780 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003781 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003782
Huang Shijieec6e87e2013-03-15 11:01:00 +08003783 if (!strncmp(type->id, id_data, type->id_len)) {
3784 mtd->writesize = type->pagesize;
3785 mtd->erasesize = type->erasesize;
3786 mtd->oobsize = type->oobsize;
3787
Huang Shijie7db906b2013-09-25 14:58:11 +08003788 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003789 chip->chipsize = (uint64_t)type->chipsize << 20;
3790 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003791 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3792 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003793 chip->onfi_timing_mode_default =
3794 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003795
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003796 if (!mtd->name)
3797 mtd->name = type->name;
3798
Huang Shijieec6e87e2013-03-15 11:01:00 +08003799 return true;
3800 }
3801 return false;
3802}
3803
Brian Norris7e74c2d2012-09-24 20:40:49 -07003804/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003805 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3806 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3807 * table.
3808 */
3809static void nand_manufacturer_detect(struct nand_chip *chip)
3810{
3811 /*
3812 * Try manufacturer detection if available and use
3813 * nand_decode_ext_id() otherwise.
3814 */
3815 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003816 chip->manufacturer.desc->ops->detect) {
3817 /* The 3rd id byte holds MLC / multichip data */
3818 chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003819 chip->manufacturer.desc->ops->detect(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003820 } else {
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003821 nand_decode_ext_id(chip);
Lothar Waßmann69fc0122017-08-29 12:17:12 +02003822 }
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003823}
3824
3825/*
3826 * Manufacturer initialization. This function is called for all NANDs including
3827 * ONFI and JEDEC compliant ones.
3828 * Manufacturer drivers should put all their specific initialization code in
3829 * their ->init() hook.
3830 */
3831static int nand_manufacturer_init(struct nand_chip *chip)
3832{
3833 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
3834 !chip->manufacturer.desc->ops->init)
3835 return 0;
3836
3837 return chip->manufacturer.desc->ops->init(chip);
3838}
3839
3840/*
3841 * Manufacturer cleanup. This function is called for all NANDs including
3842 * ONFI and JEDEC compliant ones.
3843 * Manufacturer drivers should put all their specific cleanup code in their
3844 * ->cleanup() hook.
3845 */
3846static void nand_manufacturer_cleanup(struct nand_chip *chip)
3847{
3848 /* Release manufacturer private data */
3849 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3850 chip->manufacturer.desc->ops->cleanup)
3851 chip->manufacturer.desc->ops->cleanup(chip);
3852}
3853
3854/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003855 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003856 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02003857static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003858{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003859 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003860 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08003861 int busw;
Boris Brezillonf84674b2017-06-02 12:18:24 +02003862 int i;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003863 u8 *id_data = chip->id.data;
3864 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Karl Beldanef89a882008-09-15 14:37:29 +02003866 /*
3867 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003868 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003869 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003870 nand_reset(chip, 0);
3871
3872 /* Select the device */
3873 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02003874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003876 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003879 maf_id = chip->read_byte(mtd);
3880 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Brian Norris8b6e50c2011-05-25 14:59:01 -07003882 /*
3883 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003884 * interface concerns can cause random data which looks like a
3885 * possibly credible NAND flash to appear. If the two results do
3886 * not match, ignore the device completely.
3887 */
3888
3889 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3890
Brian Norris4aef9b72012-09-24 20:40:48 -07003891 /* Read entire ID string */
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003892 for (i = 0; i < ARRAY_SIZE(chip->id.data); i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003893 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003894
Boris Brezillon7f501f02016-05-24 19:20:05 +02003895 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003896 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003897 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003898 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01003899 }
3900
Jean-Louis Thekekara5158bd52017-06-29 19:08:30 +02003901 chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data));
Boris Brezillon7f501f02016-05-24 19:20:05 +02003902
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003903 /* Try to identify manufacturer */
3904 manufacturer = nand_get_manufacturer(maf_id);
3905 chip->manufacturer.desc = manufacturer;
3906
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003907 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003908 type = nand_flash_ids;
3909
Boris Brezillon29a198a2016-05-24 20:17:48 +02003910 /*
3911 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
3912 * override it.
3913 * This is required to make sure initial NAND bus width set by the
3914 * NAND controller driver is coherent with the real NAND bus width
3915 * (extracted by auto-detection code).
3916 */
3917 busw = chip->options & NAND_BUSWIDTH_16;
3918
3919 /*
3920 * The flag is only set (never cleared), reset it to its default value
3921 * before starting auto-detection.
3922 */
3923 chip->options &= ~NAND_BUSWIDTH_16;
3924
Huang Shijieec6e87e2013-03-15 11:01:00 +08003925 for (; type->name != NULL; type++) {
3926 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003927 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08003928 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003929 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003930 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003931 }
3932 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003933
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003934 chip->onfi_version = 0;
3935 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003936 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003937 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003938 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003939
3940 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003941 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08003942 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003943 }
3944
David Woodhouse5e81e882010-02-26 18:32:56 +00003945 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003946 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003947
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02003948 if (!mtd->name)
3949 mtd->name = type->name;
3950
Adrian Hunter69423d92008-12-10 13:37:21 +00003951 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003952
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003953 if (!type->pagesize)
3954 nand_manufacturer_detect(chip);
3955 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02003956 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003957
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003958 /* Get chip options */
3959 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003960
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003961ident_done:
3962
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003963 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02003964 WARN_ON(busw & NAND_BUSWIDTH_16);
3965 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003966 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3967 /*
3968 * Check, if buswidth is correct. Hardware drivers should set
3969 * chip correct!
3970 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003971 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02003972 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01003973 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
3974 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02003975 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
3976 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09003977 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003978 }
3979
Boris Brezillon7f501f02016-05-24 19:20:05 +02003980 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003981
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003982 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003983 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003984 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003985 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003986
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003987 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003988 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003989 if (chip->chipsize & 0xffffffff)
3990 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003991 else {
3992 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3993 chip->chip_shift += 32 - 1;
3994 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003995
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003996 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003997 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003998
Brian Norris8b6e50c2011-05-25 14:59:01 -07003999 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004000 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4001 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004002
Ezequiel Garcia20171642013-11-25 08:30:31 -03004003 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004004 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004005
4006 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004007 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4008 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004009 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004010 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4011 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004012 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004013 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4014 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004015
Rafał Miłecki3755a992014-10-21 00:01:04 +02004016 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004017 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004018 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004019 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004020}
4021
Boris Brezillond48f62b2016-04-01 14:54:32 +02004022static const char * const nand_ecc_modes[] = {
4023 [NAND_ECC_NONE] = "none",
4024 [NAND_ECC_SOFT] = "soft",
4025 [NAND_ECC_HW] = "hw",
4026 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4027 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004028 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004029};
4030
4031static int of_get_nand_ecc_mode(struct device_node *np)
4032{
4033 const char *pm;
4034 int err, i;
4035
4036 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4037 if (err < 0)
4038 return err;
4039
4040 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4041 if (!strcasecmp(pm, nand_ecc_modes[i]))
4042 return i;
4043
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004044 /*
4045 * For backward compatibility we support few obsoleted values that don't
4046 * have their mappings into nand_ecc_modes_t anymore (they were merged
4047 * with other enums).
4048 */
4049 if (!strcasecmp(pm, "soft_bch"))
4050 return NAND_ECC_SOFT;
4051
Boris Brezillond48f62b2016-04-01 14:54:32 +02004052 return -ENODEV;
4053}
4054
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004055static const char * const nand_ecc_algos[] = {
4056 [NAND_ECC_HAMMING] = "hamming",
4057 [NAND_ECC_BCH] = "bch",
4058};
4059
Boris Brezillond48f62b2016-04-01 14:54:32 +02004060static int of_get_nand_ecc_algo(struct device_node *np)
4061{
4062 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004063 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004064
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004065 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4066 if (!err) {
4067 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4068 if (!strcasecmp(pm, nand_ecc_algos[i]))
4069 return i;
4070 return -ENODEV;
4071 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004072
4073 /*
4074 * For backward compatibility we also read "nand-ecc-mode" checking
4075 * for some obsoleted values that were specifying ECC algorithm.
4076 */
4077 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4078 if (err < 0)
4079 return err;
4080
4081 if (!strcasecmp(pm, "soft"))
4082 return NAND_ECC_HAMMING;
4083 else if (!strcasecmp(pm, "soft_bch"))
4084 return NAND_ECC_BCH;
4085
4086 return -ENODEV;
4087}
4088
4089static int of_get_nand_ecc_step_size(struct device_node *np)
4090{
4091 int ret;
4092 u32 val;
4093
4094 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4095 return ret ? ret : val;
4096}
4097
4098static int of_get_nand_ecc_strength(struct device_node *np)
4099{
4100 int ret;
4101 u32 val;
4102
4103 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4104 return ret ? ret : val;
4105}
4106
4107static int of_get_nand_bus_width(struct device_node *np)
4108{
4109 u32 val;
4110
4111 if (of_property_read_u32(np, "nand-bus-width", &val))
4112 return 8;
4113
4114 switch (val) {
4115 case 8:
4116 case 16:
4117 return val;
4118 default:
4119 return -EIO;
4120 }
4121}
4122
4123static bool of_get_nand_on_flash_bbt(struct device_node *np)
4124{
4125 return of_property_read_bool(np, "nand-on-flash-bbt");
4126}
4127
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004128static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004129{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004130 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004131 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004132
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004133 if (!dn)
4134 return 0;
4135
Brian Norris5844fee2015-01-23 00:22:27 -08004136 if (of_get_nand_bus_width(dn) == 16)
4137 chip->options |= NAND_BUSWIDTH_16;
4138
4139 if (of_get_nand_on_flash_bbt(dn))
4140 chip->bbt_options |= NAND_BBT_USE_FLASH;
4141
4142 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004143 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004144 ecc_strength = of_get_nand_ecc_strength(dn);
4145 ecc_step = of_get_nand_ecc_step_size(dn);
4146
Brian Norris5844fee2015-01-23 00:22:27 -08004147 if (ecc_mode >= 0)
4148 chip->ecc.mode = ecc_mode;
4149
Rafał Miłecki79082452016-03-23 11:19:02 +01004150 if (ecc_algo >= 0)
4151 chip->ecc.algo = ecc_algo;
4152
Brian Norris5844fee2015-01-23 00:22:27 -08004153 if (ecc_strength >= 0)
4154 chip->ecc.strength = ecc_strength;
4155
4156 if (ecc_step > 0)
4157 chip->ecc.size = ecc_step;
4158
Boris Brezillonba78ee02016-06-08 17:04:22 +02004159 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4160 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4161
Brian Norris5844fee2015-01-23 00:22:27 -08004162 return 0;
4163}
4164
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004165/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004166 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004167 * @mtd: MTD device structure
4168 * @maxchips: number of chips to scan for
4169 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004170 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004171 * This is the first phase of the normal nand_scan() function. It reads the
4172 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004173 *
4174 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004175int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4176 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004177{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004178 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004179 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004180 int ret;
4181
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004182 ret = nand_dt_init(chip);
4183 if (ret)
4184 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004185
Brian Norrisf7a8e382016-01-05 10:39:45 -08004186 if (!mtd->name && mtd->dev.parent)
4187 mtd->name = dev_name(mtd->dev.parent);
4188
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004189 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4190 /*
4191 * Default functions assigned for chip_select() and
4192 * cmdfunc() both expect cmd_ctrl() to be populated,
4193 * so we need to check that that's the case
4194 */
4195 pr_err("chip.cmd_ctrl() callback is not provided");
4196 return -EINVAL;
4197 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004198 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004199 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004200
4201 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004202 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004203 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004204 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004205 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004206 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
4209
Boris Brezillon7f501f02016-05-24 19:20:05 +02004210 nand_maf_id = chip->id.data[0];
4211 nand_dev_id = chip->id.data[1];
4212
Huang Shijie07300162012-11-09 16:23:45 +08004213 chip->select_chip(mtd, -1);
4214
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004215 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004216 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004217 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004218 nand_reset(chip, i);
4219
4220 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004222 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004224 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004225 nand_dev_id != chip->read_byte(mtd)) {
4226 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 break;
Huang Shijie07300162012-11-09 16:23:45 +08004228 }
4229 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
4231 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004232 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004235 chip->numchips = i;
4236 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
David Woodhouse3b85c322006-09-25 17:06:53 +01004238 return 0;
4239}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004240EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004241
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004242static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4243{
4244 struct nand_chip *chip = mtd_to_nand(mtd);
4245 struct nand_ecc_ctrl *ecc = &chip->ecc;
4246
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004247 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004248 return -EINVAL;
4249
4250 switch (ecc->algo) {
4251 case NAND_ECC_HAMMING:
4252 ecc->calculate = nand_calculate_ecc;
4253 ecc->correct = nand_correct_data;
4254 ecc->read_page = nand_read_page_swecc;
4255 ecc->read_subpage = nand_read_subpage;
4256 ecc->write_page = nand_write_page_swecc;
4257 ecc->read_page_raw = nand_read_page_raw;
4258 ecc->write_page_raw = nand_write_page_raw;
4259 ecc->read_oob = nand_read_oob_std;
4260 ecc->write_oob = nand_write_oob_std;
4261 if (!ecc->size)
4262 ecc->size = 256;
4263 ecc->bytes = 3;
4264 ecc->strength = 1;
4265 return 0;
4266 case NAND_ECC_BCH:
4267 if (!mtd_nand_has_bch()) {
4268 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4269 return -EINVAL;
4270 }
4271 ecc->calculate = nand_bch_calculate_ecc;
4272 ecc->correct = nand_bch_correct_data;
4273 ecc->read_page = nand_read_page_swecc;
4274 ecc->read_subpage = nand_read_subpage;
4275 ecc->write_page = nand_write_page_swecc;
4276 ecc->read_page_raw = nand_read_page_raw;
4277 ecc->write_page_raw = nand_write_page_raw;
4278 ecc->read_oob = nand_read_oob_std;
4279 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004280
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004281 /*
4282 * Board driver should supply ecc.size and ecc.strength
4283 * values to select how many bits are correctable.
4284 * Otherwise, default to 4 bits for large page devices.
4285 */
4286 if (!ecc->size && (mtd->oobsize >= 64)) {
4287 ecc->size = 512;
4288 ecc->strength = 4;
4289 }
4290
4291 /*
4292 * if no ecc placement scheme was provided pickup the default
4293 * large page one.
4294 */
4295 if (!mtd->ooblayout) {
4296 /* handle large page devices only */
4297 if (mtd->oobsize < 64) {
4298 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4299 return -EINVAL;
4300 }
4301
4302 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004303
4304 }
4305
4306 /*
4307 * We can only maximize ECC config when the default layout is
4308 * used, otherwise we don't know how many bytes can really be
4309 * used.
4310 */
4311 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4312 ecc->options & NAND_ECC_MAXIMIZE) {
4313 int steps, bytes;
4314
4315 /* Always prefer 1k blocks over 512bytes ones */
4316 ecc->size = 1024;
4317 steps = mtd->writesize / ecc->size;
4318
4319 /* Reserve 2 bytes for the BBM */
4320 bytes = (mtd->oobsize - 2) / steps;
4321 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004322 }
4323
4324 /* See nand_bch_init() for details. */
4325 ecc->bytes = 0;
4326 ecc->priv = nand_bch_init(mtd);
4327 if (!ecc->priv) {
4328 WARN(1, "BCH ECC initialization failed!\n");
4329 return -EINVAL;
4330 }
4331 return 0;
4332 default:
4333 WARN(1, "Unsupported ECC algorithm!\n");
4334 return -EINVAL;
4335 }
4336}
4337
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004338/**
4339 * nand_check_ecc_caps - check the sanity of preset ECC settings
4340 * @chip: nand chip info structure
4341 * @caps: ECC caps info structure
4342 * @oobavail: OOB size that the ECC engine can use
4343 *
4344 * When ECC step size and strength are already set, check if they are supported
4345 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4346 * On success, the calculated ECC bytes is set.
4347 */
4348int nand_check_ecc_caps(struct nand_chip *chip,
4349 const struct nand_ecc_caps *caps, int oobavail)
4350{
4351 struct mtd_info *mtd = nand_to_mtd(chip);
4352 const struct nand_ecc_step_info *stepinfo;
4353 int preset_step = chip->ecc.size;
4354 int preset_strength = chip->ecc.strength;
4355 int nsteps, ecc_bytes;
4356 int i, j;
4357
4358 if (WARN_ON(oobavail < 0))
4359 return -EINVAL;
4360
4361 if (!preset_step || !preset_strength)
4362 return -ENODATA;
4363
4364 nsteps = mtd->writesize / preset_step;
4365
4366 for (i = 0; i < caps->nstepinfos; i++) {
4367 stepinfo = &caps->stepinfos[i];
4368
4369 if (stepinfo->stepsize != preset_step)
4370 continue;
4371
4372 for (j = 0; j < stepinfo->nstrengths; j++) {
4373 if (stepinfo->strengths[j] != preset_strength)
4374 continue;
4375
4376 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4377 preset_strength);
4378 if (WARN_ON_ONCE(ecc_bytes < 0))
4379 return ecc_bytes;
4380
4381 if (ecc_bytes * nsteps > oobavail) {
4382 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4383 preset_step, preset_strength);
4384 return -ENOSPC;
4385 }
4386
4387 chip->ecc.bytes = ecc_bytes;
4388
4389 return 0;
4390 }
4391 }
4392
4393 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4394 preset_step, preset_strength);
4395
4396 return -ENOTSUPP;
4397}
4398EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4399
4400/**
4401 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4402 * @chip: nand chip info structure
4403 * @caps: ECC engine caps info structure
4404 * @oobavail: OOB size that the ECC engine can use
4405 *
4406 * If a chip's ECC requirement is provided, try to meet it with the least
4407 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4408 * On success, the chosen ECC settings are set.
4409 */
4410int nand_match_ecc_req(struct nand_chip *chip,
4411 const struct nand_ecc_caps *caps, int oobavail)
4412{
4413 struct mtd_info *mtd = nand_to_mtd(chip);
4414 const struct nand_ecc_step_info *stepinfo;
4415 int req_step = chip->ecc_step_ds;
4416 int req_strength = chip->ecc_strength_ds;
4417 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4418 int best_step, best_strength, best_ecc_bytes;
4419 int best_ecc_bytes_total = INT_MAX;
4420 int i, j;
4421
4422 if (WARN_ON(oobavail < 0))
4423 return -EINVAL;
4424
4425 /* No information provided by the NAND chip */
4426 if (!req_step || !req_strength)
4427 return -ENOTSUPP;
4428
4429 /* number of correctable bits the chip requires in a page */
4430 req_corr = mtd->writesize / req_step * req_strength;
4431
4432 for (i = 0; i < caps->nstepinfos; i++) {
4433 stepinfo = &caps->stepinfos[i];
4434 step_size = stepinfo->stepsize;
4435
4436 for (j = 0; j < stepinfo->nstrengths; j++) {
4437 strength = stepinfo->strengths[j];
4438
4439 /*
4440 * If both step size and strength are smaller than the
4441 * chip's requirement, it is not easy to compare the
4442 * resulted reliability.
4443 */
4444 if (step_size < req_step && strength < req_strength)
4445 continue;
4446
4447 if (mtd->writesize % step_size)
4448 continue;
4449
4450 nsteps = mtd->writesize / step_size;
4451
4452 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4453 if (WARN_ON_ONCE(ecc_bytes < 0))
4454 continue;
4455 ecc_bytes_total = ecc_bytes * nsteps;
4456
4457 if (ecc_bytes_total > oobavail ||
4458 strength * nsteps < req_corr)
4459 continue;
4460
4461 /*
4462 * We assume the best is to meet the chip's requrement
4463 * with the least number of ECC bytes.
4464 */
4465 if (ecc_bytes_total < best_ecc_bytes_total) {
4466 best_ecc_bytes_total = ecc_bytes_total;
4467 best_step = step_size;
4468 best_strength = strength;
4469 best_ecc_bytes = ecc_bytes;
4470 }
4471 }
4472 }
4473
4474 if (best_ecc_bytes_total == INT_MAX)
4475 return -ENOTSUPP;
4476
4477 chip->ecc.size = best_step;
4478 chip->ecc.strength = best_strength;
4479 chip->ecc.bytes = best_ecc_bytes;
4480
4481 return 0;
4482}
4483EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4484
4485/**
4486 * nand_maximize_ecc - choose the max ECC strength available
4487 * @chip: nand chip info structure
4488 * @caps: ECC engine caps info structure
4489 * @oobavail: OOB size that the ECC engine can use
4490 *
4491 * Choose the max ECC strength that is supported on the controller, and can fit
4492 * within the chip's OOB. On success, the chosen ECC settings are set.
4493 */
4494int nand_maximize_ecc(struct nand_chip *chip,
4495 const struct nand_ecc_caps *caps, int oobavail)
4496{
4497 struct mtd_info *mtd = nand_to_mtd(chip);
4498 const struct nand_ecc_step_info *stepinfo;
4499 int step_size, strength, nsteps, ecc_bytes, corr;
4500 int best_corr = 0;
4501 int best_step = 0;
4502 int best_strength, best_ecc_bytes;
4503 int i, j;
4504
4505 if (WARN_ON(oobavail < 0))
4506 return -EINVAL;
4507
4508 for (i = 0; i < caps->nstepinfos; i++) {
4509 stepinfo = &caps->stepinfos[i];
4510 step_size = stepinfo->stepsize;
4511
4512 /* If chip->ecc.size is already set, respect it */
4513 if (chip->ecc.size && step_size != chip->ecc.size)
4514 continue;
4515
4516 for (j = 0; j < stepinfo->nstrengths; j++) {
4517 strength = stepinfo->strengths[j];
4518
4519 if (mtd->writesize % step_size)
4520 continue;
4521
4522 nsteps = mtd->writesize / step_size;
4523
4524 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4525 if (WARN_ON_ONCE(ecc_bytes < 0))
4526 continue;
4527
4528 if (ecc_bytes * nsteps > oobavail)
4529 continue;
4530
4531 corr = strength * nsteps;
4532
4533 /*
4534 * If the number of correctable bits is the same,
4535 * bigger step_size has more reliability.
4536 */
4537 if (corr > best_corr ||
4538 (corr == best_corr && step_size > best_step)) {
4539 best_corr = corr;
4540 best_step = step_size;
4541 best_strength = strength;
4542 best_ecc_bytes = ecc_bytes;
4543 }
4544 }
4545 }
4546
4547 if (!best_corr)
4548 return -ENOTSUPP;
4549
4550 chip->ecc.size = best_step;
4551 chip->ecc.strength = best_strength;
4552 chip->ecc.bytes = best_ecc_bytes;
4553
4554 return 0;
4555}
4556EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4557
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004558/*
4559 * Check if the chip configuration meet the datasheet requirements.
4560
4561 * If our configuration corrects A bits per B bytes and the minimum
4562 * required correction level is X bits per Y bytes, then we must ensure
4563 * both of the following are true:
4564 *
4565 * (1) A / B >= X / Y
4566 * (2) A >= X
4567 *
4568 * Requirement (1) ensures we can correct for the required bitflip density.
4569 * Requirement (2) ensures we can correct even when all bitflips are clumped
4570 * in the same sector.
4571 */
4572static bool nand_ecc_strength_good(struct mtd_info *mtd)
4573{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004574 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004575 struct nand_ecc_ctrl *ecc = &chip->ecc;
4576 int corr, ds_corr;
4577
4578 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4579 /* Not enough information */
4580 return true;
4581
4582 /*
4583 * We get the number of corrected bits per page to compare
4584 * the correction density.
4585 */
4586 corr = (mtd->writesize * ecc->strength) / ecc->size;
4587 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4588
4589 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4590}
David Woodhouse3b85c322006-09-25 17:06:53 +01004591
Marc Gonzalez3371d662016-11-15 10:56:20 +01004592static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4593{
4594 struct nand_ecc_ctrl *ecc = &chip->ecc;
4595
4596 if (nand_standard_page_accessors(ecc))
4597 return false;
4598
4599 /*
4600 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4601 * controller driver implements all the page accessors because
4602 * default helpers are not suitable when the core does not
4603 * send the READ0/PAGEPROG commands.
4604 */
4605 return (!ecc->read_page || !ecc->write_page ||
4606 !ecc->read_page_raw || !ecc->write_page_raw ||
4607 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4608 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4609 ecc->hwctl && ecc->calculate));
4610}
4611
David Woodhouse3b85c322006-09-25 17:06:53 +01004612/**
4613 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004614 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004615 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004616 * This is the second phase of the normal nand_scan() function. It fills out
4617 * all the uninitialized function pointers with the defaults and scans for a
4618 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004619 */
4620int nand_scan_tail(struct mtd_info *mtd)
4621{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004622 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004623 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004624 struct nand_buffers *nbuf = NULL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004625 int ret, i;
David Woodhouse3b85c322006-09-25 17:06:53 +01004626
Brian Norrise2414f42012-02-06 13:44:00 -08004627 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004628 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004629 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
Boris Brezillonf84674b2017-06-02 12:18:24 +02004630 return -EINVAL;
Brian Norris78771042017-05-01 17:04:53 -07004631 }
Brian Norrise2414f42012-02-06 13:44:00 -08004632
Marc Gonzalez3371d662016-11-15 10:56:20 +01004633 if (invalid_ecc_page_accessors(chip)) {
4634 pr_err("Invalid ECC page accessors setup\n");
Boris Brezillonf84674b2017-06-02 12:18:24 +02004635 return -EINVAL;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004636 }
4637
Huang Shijief02ea4e2014-01-13 14:27:12 +08004638 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004639 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Boris Brezillonf84674b2017-06-02 12:18:24 +02004640 if (!nbuf)
4641 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004642
4643 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4644 if (!nbuf->ecccalc) {
4645 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004646 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004647 }
4648
4649 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4650 if (!nbuf->ecccode) {
4651 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004652 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004653 }
4654
4655 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4656 GFP_KERNEL);
4657 if (!nbuf->databuf) {
4658 ret = -ENOMEM;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004659 goto err_free_nbuf;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004660 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004661
4662 chip->buffers = nbuf;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004663 } else if (!chip->buffers) {
4664 return -ENOMEM;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004665 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004666
Boris Brezillonf84674b2017-06-02 12:18:24 +02004667 /*
4668 * FIXME: some NAND manufacturer drivers expect the first die to be
4669 * selected when manufacturer->init() is called. They should be fixed
4670 * to explictly select the relevant die when interacting with the NAND
4671 * chip.
4672 */
4673 chip->select_chip(mtd, 0);
4674 ret = nand_manufacturer_init(chip);
4675 chip->select_chip(mtd, -1);
4676 if (ret)
4677 goto err_free_nbuf;
4678
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004679 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004680 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004681
4682 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004683 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004684 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004685 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004686 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004687 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004690 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 break;
4692 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004693 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004694 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004697 WARN(1, "No oob scheme defined for oobsize %d\n",
4698 mtd->oobsize);
4699 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004700 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
4702 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004703
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004704 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004705 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004706 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004707 */
David Woodhouse956e9442006-09-25 17:12:39 +01004708
Huang Shijie97de79e02013-10-18 14:20:53 +08004709 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004710 case NAND_ECC_HW_OOB_FIRST:
4711 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004712 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004713 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4714 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004715 goto err_nand_manuf_cleanup;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004716 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004717 if (!ecc->read_page)
4718 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004719
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004720 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004721 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004722 if (!ecc->read_page)
4723 ecc->read_page = nand_read_page_hwecc;
4724 if (!ecc->write_page)
4725 ecc->write_page = nand_write_page_hwecc;
4726 if (!ecc->read_page_raw)
4727 ecc->read_page_raw = nand_read_page_raw;
4728 if (!ecc->write_page_raw)
4729 ecc->write_page_raw = nand_write_page_raw;
4730 if (!ecc->read_oob)
4731 ecc->read_oob = nand_read_oob_std;
4732 if (!ecc->write_oob)
4733 ecc->write_oob = nand_write_oob_std;
4734 if (!ecc->read_subpage)
4735 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004736 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004737 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004738
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004739 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004740 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4741 (!ecc->read_page ||
4742 ecc->read_page == nand_read_page_hwecc ||
4743 !ecc->write_page ||
4744 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004745 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4746 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004747 goto err_nand_manuf_cleanup;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004748 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004749 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004750 if (!ecc->read_page)
4751 ecc->read_page = nand_read_page_syndrome;
4752 if (!ecc->write_page)
4753 ecc->write_page = nand_write_page_syndrome;
4754 if (!ecc->read_page_raw)
4755 ecc->read_page_raw = nand_read_page_raw_syndrome;
4756 if (!ecc->write_page_raw)
4757 ecc->write_page_raw = nand_write_page_raw_syndrome;
4758 if (!ecc->read_oob)
4759 ecc->read_oob = nand_read_oob_syndrome;
4760 if (!ecc->write_oob)
4761 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004762
Huang Shijie97de79e02013-10-18 14:20:53 +08004763 if (mtd->writesize >= ecc->size) {
4764 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004765 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4766 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004767 goto err_nand_manuf_cleanup;
Mike Dunne2788c92012-04-25 12:06:10 -07004768 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004769 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004770 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004771 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4772 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004773 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004774 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004776 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004777 ret = nand_set_ecc_soft_ops(mtd);
4778 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004779 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004780 goto err_nand_manuf_cleanup;
Ivan Djelic193bd402011-03-11 11:05:33 +01004781 }
4782 break;
4783
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004784 case NAND_ECC_ON_DIE:
4785 if (!ecc->read_page || !ecc->write_page) {
4786 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4787 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004788 goto err_nand_manuf_cleanup;
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004789 }
4790 if (!ecc->read_oob)
4791 ecc->read_oob = nand_read_oob_std;
4792 if (!ecc->write_oob)
4793 ecc->write_oob = nand_write_oob_std;
4794 break;
4795
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004796 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004797 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004798 ecc->read_page = nand_read_page_raw;
4799 ecc->write_page = nand_write_page_raw;
4800 ecc->read_oob = nand_read_oob_std;
4801 ecc->read_page_raw = nand_read_page_raw;
4802 ecc->write_page_raw = nand_write_page_raw;
4803 ecc->write_oob = nand_write_oob_std;
4804 ecc->size = mtd->writesize;
4805 ecc->bytes = 0;
4806 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004810 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4811 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004812 goto err_nand_manuf_cleanup;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Brian Norris9ce244b2011-08-30 18:45:37 -07004815 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004816 if (!ecc->read_oob_raw)
4817 ecc->read_oob_raw = ecc->read_oob;
4818 if (!ecc->write_oob_raw)
4819 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004820
Boris Brezillon846031d2016-02-03 20:11:00 +01004821 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004822 mtd->ecc_strength = ecc->strength;
4823 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004824
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004825 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004826 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004827 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004828 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004829 ecc->steps = mtd->writesize / ecc->size;
4830 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004831 WARN(1, "Invalid ECC parameters\n");
4832 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004833 goto err_nand_manuf_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004835 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004836 if (ecc->total > mtd->oobsize) {
4837 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4838 ret = -EINVAL;
Boris Brezillonf84674b2017-06-02 12:18:24 +02004839 goto err_nand_manuf_cleanup;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004840 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004841
Boris Brezillon846031d2016-02-03 20:11:00 +01004842 /*
4843 * The number of bytes available for a client to place data into
4844 * the out of band area.
4845 */
4846 ret = mtd_ooblayout_count_freebytes(mtd);
4847 if (ret < 0)
4848 ret = 0;
4849
4850 mtd->oobavail = ret;
4851
4852 /* ECC sanity check: warn if it's too weak */
4853 if (!nand_ecc_strength_good(mtd))
4854 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4855 mtd->name);
4856
Brian Norris8b6e50c2011-05-25 14:59:01 -07004857 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004858 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004859 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004860 case 2:
4861 mtd->subpage_sft = 1;
4862 break;
4863 case 4:
4864 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004865 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004866 mtd->subpage_sft = 2;
4867 break;
4868 }
4869 }
4870 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4871
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004872 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004873 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004876 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004878 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304879 switch (ecc->mode) {
4880 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304881 if (chip->page_shift > 9)
4882 chip->options |= NAND_SUBPAGE_READ;
4883 break;
4884
4885 default:
4886 break;
4887 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004890 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004891 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4892 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004893 mtd->_erase = nand_erase;
4894 mtd->_point = NULL;
4895 mtd->_unpoint = NULL;
4896 mtd->_read = nand_read;
4897 mtd->_write = nand_write;
4898 mtd->_panic_write = panic_nand_write;
4899 mtd->_read_oob = nand_read_oob;
4900 mtd->_write_oob = nand_write_oob;
4901 mtd->_sync = nand_sync;
4902 mtd->_lock = NULL;
4903 mtd->_unlock = NULL;
4904 mtd->_suspend = nand_suspend;
4905 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004906 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004907 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004908 mtd->_block_isbad = nand_block_isbad;
4909 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004910 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004911 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004913 /*
4914 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4915 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4916 * properly set.
4917 */
4918 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004919 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Boris Brezillonf84674b2017-06-02 12:18:24 +02004921 /* Initialize the ->data_interface field. */
4922 ret = nand_init_data_interface(chip);
4923 if (ret)
4924 goto err_nand_manuf_cleanup;
4925
4926 /* Enter fastest possible mode on all dies. */
4927 for (i = 0; i < chip->numchips; i++) {
4928 chip->select_chip(mtd, i);
4929 ret = nand_setup_data_interface(chip, i);
4930 chip->select_chip(mtd, -1);
4931
4932 if (ret)
4933 goto err_nand_data_iface_cleanup;
4934 }
4935
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004936 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004937 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07004941 ret = chip->scan_bbt(mtd);
4942 if (ret)
Boris Brezillonf84674b2017-06-02 12:18:24 +02004943 goto err_nand_data_iface_cleanup;
4944
Brian Norris44d41822017-05-01 17:04:50 -07004945 return 0;
4946
Boris Brezillonf84674b2017-06-02 12:18:24 +02004947err_nand_data_iface_cleanup:
4948 nand_release_data_interface(chip);
4949
4950err_nand_manuf_cleanup:
4951 nand_manufacturer_cleanup(chip);
4952
4953err_free_nbuf:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004954 if (nbuf) {
4955 kfree(nbuf->databuf);
4956 kfree(nbuf->ecccode);
4957 kfree(nbuf->ecccalc);
4958 kfree(nbuf);
4959 }
Brian Norris78771042017-05-01 17:04:53 -07004960
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004961 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004963EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
Brian Norris8b6e50c2011-05-25 14:59:01 -07004965/*
4966 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004967 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004968 * to call us from in-kernel code if the core NAND support is modular.
4969 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004970#ifdef MODULE
4971#define caller_is_module() (1)
4972#else
4973#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004974 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004975#endif
4976
4977/**
4978 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004979 * @mtd: MTD device structure
4980 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004981 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004982 * This fills out all the uninitialized function pointers with the defaults.
4983 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004984 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004985 */
4986int nand_scan(struct mtd_info *mtd, int maxchips)
4987{
4988 int ret;
4989
David Woodhouse5e81e882010-02-26 18:32:56 +00004990 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004991 if (!ret)
4992 ret = nand_scan_tail(mtd);
4993 return ret;
4994}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004995EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004998 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4999 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005000 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005001void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005003 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005004 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005005 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5006
Boris Brezillond8e725d2016-09-15 10:32:50 +02005007 nand_release_data_interface(chip);
5008
Jesper Juhlfa671642005-11-07 01:01:27 -08005009 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005010 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005011 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5012 kfree(chip->buffers->databuf);
5013 kfree(chip->buffers->ecccode);
5014 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005015 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005016 }
Brian Norris58373ff2010-07-15 12:15:44 -07005017
5018 /* Free bad block descriptor memory */
5019 if (chip->badblock_pattern && chip->badblock_pattern->options
5020 & NAND_BBT_DYNAMICSTRUCT)
5021 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005022
5023 /* Free manufacturer priv data. */
5024 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005026EXPORT_SYMBOL_GPL(nand_cleanup);
5027
5028/**
5029 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5030 * held by the NAND device
5031 * @mtd: MTD device structure
5032 */
5033void nand_release(struct mtd_info *mtd)
5034{
5035 mtd_device_unregister(mtd);
5036 nand_cleanup(mtd_to_nand(mtd));
5037}
David Woodhousee0c7d762006-05-13 18:07:53 +01005038EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005039
David Woodhousee0c7d762006-05-13 18:07:53 +01005040MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005041MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5042MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005043MODULE_DESCRIPTION("Generic NAND flash driver code");