blob: 79d98c9fa8a7528e8ac815e33dd480151462bd3e [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>
42#include <linux/mtd/nand.h>
43#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
205const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
206 .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:
Brian Norrise2414f42012-02-06 13:44:00 -0800505 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700506 * (2) write bad block marker to OOB area of affected block (unless flag
507 * NAND_BBT_NO_OOB_BBM is present)
508 * (3) update the BBT
509 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800510 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700512static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100514 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700515 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000516
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800518 struct erase_info einfo;
519
520 /* Attempt erase before marking OOB */
521 memset(&einfo, 0, sizeof(einfo));
522 einfo.mtd = mtd;
523 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300524 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800525 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800526
Brian Norrisb32843b2013-07-30 17:52:59 -0700527 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800528 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700529 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300530 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200531 }
Brian Norrise2414f42012-02-06 13:44:00 -0800532
Brian Norrisb32843b2013-07-30 17:52:59 -0700533 /* Mark block bad in BBT */
534 if (chip->bbt) {
535 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800536 if (!ret)
537 ret = res;
538 }
539
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200540 if (!ret)
541 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300542
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000546/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700548 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700550 * Check, if the device is write protected. The function expects, that the
551 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100553static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100555 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200556
Brian Norris8b6e50c2011-05-25 14:59:01 -0700557 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200558 if (chip->options & NAND_BROKEN_XD)
559 return 0;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200562 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
563 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800567 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700568 * @mtd: MTD device structure
569 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300570 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800571 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300572 */
573static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
574{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100575 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300576
577 if (!chip->bbt)
578 return 0;
579 /* Return info from the table */
580 return nand_isreserved_bbt(mtd, ofs);
581}
582
583/**
584 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
585 * @mtd: MTD device structure
586 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700587 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
589 * Check, if the block is bad. Either by reading the bad block table or
590 * calling of the scan function.
591 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530592static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100594 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000595
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530597 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100600 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200603/**
604 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700605 * @mtd: MTD device structure
606 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200607 *
608 * Helper function for nand_wait_ready used when needing to wait in interrupt
609 * context.
610 */
611static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
612{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100613 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200614 int i;
615
616 /* Wait for the device to get ready */
617 for (i = 0; i < timeo; i++) {
618 if (chip->dev_ready(mtd))
619 break;
620 touch_softlockup_watchdog();
621 mdelay(1);
622 }
623}
624
Alex Smithb70af9b2015-10-06 14:52:07 +0100625/**
626 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
627 * @mtd: MTD device structure
628 *
629 * Wait for the ready pin after a command, and warn if a timeout occurs.
630 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100631void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000632{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100633 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100634 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000635
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200636 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100637 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200638
Brian Norris7854d3f2011-06-23 14:12:08 -0700639 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100640 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000641 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300643 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100644 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000645 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100646
Brian Norris9ebfdf52016-03-04 17:19:23 -0800647 if (!chip->dev_ready(mtd))
648 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649}
David Woodhouse4b648b02006-09-25 17:05:24 +0100650EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200653 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
654 * @mtd: MTD device structure
655 * @timeo: Timeout in ms
656 *
657 * Wait for status ready (i.e. command done) or timeout.
658 */
659static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
660{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100661 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200662
663 timeo = jiffies + msecs_to_jiffies(timeo);
664 do {
665 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
666 break;
667 touch_softlockup_watchdog();
668 } while (time_before(jiffies, timeo));
669};
670
671/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700673 * @mtd: MTD device structure
674 * @command: the command to be sent
675 * @column: the column address for this command, -1 if none
676 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700678 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200679 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200681static void nand_command(struct mtd_info *mtd, unsigned int command,
682 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100684 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200685 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Brian Norris8b6e50c2011-05-25 14:59:01 -0700687 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (command == NAND_CMD_SEQIN) {
689 int readcmd;
690
Joern Engel28318772006-05-22 23:18:05 +0200691 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200693 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 readcmd = NAND_CMD_READOOB;
695 } else if (column < 256) {
696 /* First 256 bytes --> READ0 */
697 readcmd = NAND_CMD_READ0;
698 } else {
699 column -= 256;
700 readcmd = NAND_CMD_READ1;
701 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200702 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200703 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200705 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Brian Norris8b6e50c2011-05-25 14:59:01 -0700707 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200708 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
709 /* Serially input address */
710 if (column != -1) {
711 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800712 if (chip->options & NAND_BUSWIDTH_16 &&
713 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200714 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200715 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200718 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200719 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200720 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 if (chip->chipsize > (32 << 20))
724 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200725 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200726 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727
728 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700729 * Program and erase have their own busy handlers status and sequential
730 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 case NAND_CMD_PAGEPROG:
735 case NAND_CMD_ERASE1:
736 case NAND_CMD_ERASE2:
737 case NAND_CMD_SEQIN:
738 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900739 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900740 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return;
742
743 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200744 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 udelay(chip->chip_delay);
747 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200748 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200749 chip->cmd_ctrl(mtd,
750 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200751 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
752 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
754
David Woodhousee0c7d762006-05-13 18:07:53 +0100755 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000757 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * If we don't have access to the busy pin, we apply the given
759 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100760 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200761 if (!chip->dev_ready) {
762 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 /*
767 * Apply this short delay always to ensure that we do wait tWB in
768 * any case on any machine.
769 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100770 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000771
772 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200775static void nand_ccs_delay(struct nand_chip *chip)
776{
777 /*
778 * The controller already takes care of waiting for tCCS when the RNDIN
779 * or RNDOUT command is sent, return directly.
780 */
781 if (!(chip->options & NAND_WAIT_TCCS))
782 return;
783
784 /*
785 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
786 * (which should be safe for all NANDs).
787 */
788 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
789 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
790 else
791 ndelay(500);
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/**
795 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700796 * @mtd: MTD device structure
797 * @command: the command to be sent
798 * @column: the column address for this command, -1 if none
799 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200801 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700802 * devices. We don't have the separate regions as we have in the small page
803 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200805static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
806 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100808 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* Emulate NAND_CMD_READOOB */
811 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200812 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 command = NAND_CMD_READ0;
814 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000815
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200816 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400817 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200820 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Serially input address */
823 if (column != -1) {
824 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800825 if (chip->options & NAND_BUSWIDTH_16 &&
826 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200828 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200829 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200830
Brian Norrisf5b88de2016-10-03 09:49:35 -0700831 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200832 if (!nand_opcode_8bits(command))
833 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200836 chip->cmd_ctrl(mtd, page_addr, ctrl);
837 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200838 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200840 if (chip->chipsize > (128 << 20))
841 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200842 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200845 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846
847 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700848 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100849 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 case NAND_CMD_CACHEDPROG:
854 case NAND_CMD_PAGEPROG:
855 case NAND_CMD_ERASE1:
856 case NAND_CMD_ERASE2:
857 case NAND_CMD_SEQIN:
858 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900859 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900860 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000861 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200863 case NAND_CMD_RNDIN:
864 nand_ccs_delay(chip);
865 return;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200868 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200870 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200871 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
872 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
873 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
874 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200875 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
876 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return;
878
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200879 case NAND_CMD_RNDOUT:
880 /* No ready / busy check necessary */
881 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
882 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
883 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
884 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200885
886 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200887 return;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200890 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
891 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
892 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
893 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000894
David Woodhousee0c7d762006-05-13 18:07:53 +0100895 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000897 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700899 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100900 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200901 if (!chip->dev_ready) {
902 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000906
Brian Norris8b6e50c2011-05-25 14:59:01 -0700907 /*
908 * Apply this short delay always to ensure that we do wait tWB in
909 * any case on any machine.
910 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100911 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000912
913 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200917 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700918 * @chip: the nand chip descriptor
919 * @mtd: MTD device structure
920 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200921 *
922 * Used when in panic, no locks are taken.
923 */
924static void panic_nand_get_device(struct nand_chip *chip,
925 struct mtd_info *mtd, int new_state)
926{
Brian Norris7854d3f2011-06-23 14:12:08 -0700927 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200928 chip->controller->active = chip;
929 chip->state = new_state;
930}
931
932/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700934 * @mtd: MTD device structure
935 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
937 * Get the device and lock it for exclusive access
938 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200939static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800940nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100942 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200943 spinlock_t *lock = &chip->controller->lock;
944 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100945 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200946retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100947 spin_lock(lock);
948
vimal singhb8b3ee92009-07-09 20:41:22 +0530949 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200950 if (!chip->controller->active)
951 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200952
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200953 if (chip->controller->active == chip && chip->state == FL_READY) {
954 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100955 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100956 return 0;
957 }
958 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800959 if (chip->controller->active->state == FL_PM_SUSPENDED) {
960 chip->state = FL_PM_SUSPENDED;
961 spin_unlock(lock);
962 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800963 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100964 }
965 set_current_state(TASK_UNINTERRUPTIBLE);
966 add_wait_queue(wq, &wait);
967 spin_unlock(lock);
968 schedule();
969 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto retry;
971}
972
973/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700974 * panic_nand_wait - [GENERIC] wait until the command is done
975 * @mtd: MTD device structure
976 * @chip: NAND chip structure
977 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200978 *
979 * Wait for command done. This is a helper function for nand_wait used when
980 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400981 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200982 */
983static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
984 unsigned long timeo)
985{
986 int i;
987 for (i = 0; i < timeo; i++) {
988 if (chip->dev_ready) {
989 if (chip->dev_ready(mtd))
990 break;
991 } else {
992 if (chip->read_byte(mtd) & NAND_STATUS_READY)
993 break;
994 }
995 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200996 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200997}
998
999/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * nand_wait - [DEFAULT] wait until the command is done
1001 * @mtd: MTD device structure
1002 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001004 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001005 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001006static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008
Alex Smithb70af9b2015-10-06 14:52:07 +01001009 int status;
1010 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Brian Norris8b6e50c2011-05-25 14:59:01 -07001012 /*
1013 * Apply this short delay always to ensure that we do wait tWB in any
1014 * case on any machine.
1015 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001016 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001018 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001020 if (in_interrupt() || oops_in_progress)
1021 panic_nand_wait(mtd, chip, timeo);
1022 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001023 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001024 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001025 if (chip->dev_ready) {
1026 if (chip->dev_ready(mtd))
1027 break;
1028 } else {
1029 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1030 break;
1031 }
1032 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001033 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001035
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001036 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001037 /* This can happen if in case of timeout or buggy dev_ready */
1038 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return status;
1040}
1041
1042/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001043 * nand_reset_data_interface - Reset data interface and timings
1044 * @chip: The NAND chip
1045 *
1046 * Reset the Data interface and timings to ONFI mode 0.
1047 *
1048 * Returns 0 for success or negative error code otherwise.
1049 */
1050static int nand_reset_data_interface(struct nand_chip *chip)
1051{
1052 struct mtd_info *mtd = nand_to_mtd(chip);
1053 const struct nand_data_interface *conf;
1054 int ret;
1055
1056 if (!chip->setup_data_interface)
1057 return 0;
1058
1059 /*
1060 * The ONFI specification says:
1061 * "
1062 * To transition from NV-DDR or NV-DDR2 to the SDR data
1063 * interface, the host shall use the Reset (FFh) command
1064 * using SDR timing mode 0. A device in any timing mode is
1065 * required to recognize Reset (FFh) command issued in SDR
1066 * timing mode 0.
1067 * "
1068 *
1069 * Configure the data interface in SDR mode and set the
1070 * timings to timing mode 0.
1071 */
1072
1073 conf = nand_get_default_data_interface();
1074 ret = chip->setup_data_interface(mtd, conf, false);
1075 if (ret)
1076 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1077
1078 return ret;
1079}
1080
1081/**
1082 * nand_setup_data_interface - Setup the best data interface and timings
1083 * @chip: The NAND chip
1084 *
1085 * Find and configure the best data interface and NAND timings supported by
1086 * the chip and the driver.
1087 * First tries to retrieve supported timing modes from ONFI information,
1088 * and if the NAND chip does not support ONFI, relies on the
1089 * ->onfi_timing_mode_default specified in the nand_ids table.
1090 *
1091 * Returns 0 for success or negative error code otherwise.
1092 */
1093static int nand_setup_data_interface(struct nand_chip *chip)
1094{
1095 struct mtd_info *mtd = nand_to_mtd(chip);
1096 int ret;
1097
1098 if (!chip->setup_data_interface || !chip->data_interface)
1099 return 0;
1100
1101 /*
1102 * Ensure the timing mode has been changed on the chip side
1103 * before changing timings on the controller side.
1104 */
1105 if (chip->onfi_version) {
1106 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1107 chip->onfi_timing_mode_default,
1108 };
1109
1110 ret = chip->onfi_set_features(mtd, chip,
1111 ONFI_FEATURE_ADDR_TIMING_MODE,
1112 tmode_param);
1113 if (ret)
1114 goto err;
1115 }
1116
1117 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1118err:
1119 return ret;
1120}
1121
1122/**
1123 * nand_init_data_interface - find the best data interface and timings
1124 * @chip: The NAND chip
1125 *
1126 * Find the best data interface and NAND timings supported by the chip
1127 * and the driver.
1128 * First tries to retrieve supported timing modes from ONFI information,
1129 * and if the NAND chip does not support ONFI, relies on the
1130 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1131 * function nand_chip->data_interface is initialized with the best timing mode
1132 * available.
1133 *
1134 * Returns 0 for success or negative error code otherwise.
1135 */
1136static int nand_init_data_interface(struct nand_chip *chip)
1137{
1138 struct mtd_info *mtd = nand_to_mtd(chip);
1139 int modes, mode, ret;
1140
1141 if (!chip->setup_data_interface)
1142 return 0;
1143
1144 /*
1145 * First try to identify the best timings from ONFI parameters and
1146 * if the NAND does not support ONFI, fallback to the default ONFI
1147 * timing mode.
1148 */
1149 modes = onfi_get_async_timing_mode(chip);
1150 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1151 if (!chip->onfi_timing_mode_default)
1152 return 0;
1153
1154 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1155 }
1156
1157 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1158 GFP_KERNEL);
1159 if (!chip->data_interface)
1160 return -ENOMEM;
1161
1162 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1163 ret = onfi_init_data_interface(chip, chip->data_interface,
1164 NAND_SDR_IFACE, mode);
1165 if (ret)
1166 continue;
1167
1168 ret = chip->setup_data_interface(mtd, chip->data_interface,
1169 true);
1170 if (!ret) {
1171 chip->onfi_timing_mode_default = mode;
1172 break;
1173 }
1174 }
1175
1176 return 0;
1177}
1178
1179static void nand_release_data_interface(struct nand_chip *chip)
1180{
1181 kfree(chip->data_interface);
1182}
1183
1184/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001185 * nand_reset - Reset and initialize a NAND device
1186 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001187 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001188 *
1189 * Returns 0 for success or negative error code otherwise
1190 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001191int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001192{
1193 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001194 int ret;
1195
1196 ret = nand_reset_data_interface(chip);
1197 if (ret)
1198 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001199
Boris Brezillon73f907f2016-10-24 16:46:20 +02001200 /*
1201 * The CS line has to be released before we can apply the new NAND
1202 * interface settings, hence this weird ->select_chip() dance.
1203 */
1204 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001205 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001206 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001207
Boris Brezillon73f907f2016-10-24 16:46:20 +02001208 chip->select_chip(mtd, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001209 ret = nand_setup_data_interface(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001210 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001211 if (ret)
1212 return ret;
1213
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001214 return 0;
1215}
1216
1217/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001218 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001219 * @mtd: mtd info
1220 * @ofs: offset to start unlock from
1221 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001222 * @invert: when = 0, unlock the range of blocks within the lower and
1223 * upper boundary address
1224 * when = 1, unlock the range of blocks outside the boundaries
1225 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301226 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001227 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301228 */
1229static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1230 uint64_t len, int invert)
1231{
1232 int ret = 0;
1233 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001234 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301235
1236 /* Submit address of first page to unlock */
1237 page = ofs >> chip->page_shift;
1238 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1239
1240 /* Submit address of last page to unlock */
1241 page = (ofs + len) >> chip->page_shift;
1242 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1243 (page | invert) & chip->pagemask);
1244
1245 /* Call wait ready function */
1246 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301247 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001248 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001249 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301250 __func__, status);
1251 ret = -EIO;
1252 }
1253
1254 return ret;
1255}
1256
1257/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001258 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001259 * @mtd: mtd info
1260 * @ofs: offset to start unlock from
1261 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301262 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001263 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301264 */
1265int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1266{
1267 int ret = 0;
1268 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001269 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301270
Brian Norris289c0522011-07-19 10:06:09 -07001271 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301272 __func__, (unsigned long long)ofs, len);
1273
1274 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001275 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301276
1277 /* Align to last block address if size addresses end of the device */
1278 if (ofs + len == mtd->size)
1279 len -= mtd->erasesize;
1280
Huang Shijie6a8214a2012-11-19 14:43:30 +08001281 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301282
1283 /* Shift to get chip number */
1284 chipnr = ofs >> chip->chip_shift;
1285
White Ding57d3a9a2014-07-24 00:10:45 +08001286 /*
1287 * Reset the chip.
1288 * If we want to check the WP through READ STATUS and check the bit 7
1289 * we must reset the chip
1290 * some operation can also clear the bit 7 of status register
1291 * eg. erase/program a locked block
1292 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001293 nand_reset(chip, chipnr);
1294
1295 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001296
Vimal Singh7d70f332010-02-08 15:50:49 +05301297 /* Check, if it is write protected */
1298 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001299 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301300 __func__);
1301 ret = -EIO;
1302 goto out;
1303 }
1304
1305 ret = __nand_unlock(mtd, ofs, len, 0);
1306
1307out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001308 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301309 nand_release_device(mtd);
1310
1311 return ret;
1312}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001313EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301314
1315/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001316 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001317 * @mtd: mtd info
1318 * @ofs: offset to start unlock from
1319 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301320 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001321 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1322 * have this feature, but it allows only to lock all blocks, not for specified
1323 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1324 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301325 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001326 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301327 */
1328int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1329{
1330 int ret = 0;
1331 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001332 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301333
Brian Norris289c0522011-07-19 10:06:09 -07001334 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301335 __func__, (unsigned long long)ofs, len);
1336
1337 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001338 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301339
Huang Shijie6a8214a2012-11-19 14:43:30 +08001340 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301341
1342 /* Shift to get chip number */
1343 chipnr = ofs >> chip->chip_shift;
1344
White Ding57d3a9a2014-07-24 00:10:45 +08001345 /*
1346 * Reset the chip.
1347 * If we want to check the WP through READ STATUS and check the bit 7
1348 * we must reset the chip
1349 * some operation can also clear the bit 7 of status register
1350 * eg. erase/program a locked block
1351 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001352 nand_reset(chip, chipnr);
1353
1354 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001355
Vimal Singh7d70f332010-02-08 15:50:49 +05301356 /* Check, if it is write protected */
1357 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001358 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301359 __func__);
1360 status = MTD_ERASE_FAILED;
1361 ret = -EIO;
1362 goto out;
1363 }
1364
1365 /* Submit address of first page to lock */
1366 page = ofs >> chip->page_shift;
1367 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1368
1369 /* Call wait ready function */
1370 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301371 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001372 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001373 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301374 __func__, status);
1375 ret = -EIO;
1376 goto out;
1377 }
1378
1379 ret = __nand_unlock(mtd, ofs, len, 0x1);
1380
1381out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001382 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301383 nand_release_device(mtd);
1384
1385 return ret;
1386}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001387EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301388
1389/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001390 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1391 * @buf: buffer to test
1392 * @len: buffer length
1393 * @bitflips_threshold: maximum number of bitflips
1394 *
1395 * Check if a buffer contains only 0xff, which means the underlying region
1396 * has been erased and is ready to be programmed.
1397 * The bitflips_threshold specify the maximum number of bitflips before
1398 * considering the region is not erased.
1399 * Note: The logic of this function has been extracted from the memweight
1400 * implementation, except that nand_check_erased_buf function exit before
1401 * testing the whole buffer if the number of bitflips exceed the
1402 * bitflips_threshold value.
1403 *
1404 * Returns a positive number of bitflips less than or equal to
1405 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1406 * threshold.
1407 */
1408static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1409{
1410 const unsigned char *bitmap = buf;
1411 int bitflips = 0;
1412 int weight;
1413
1414 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1415 len--, bitmap++) {
1416 weight = hweight8(*bitmap);
1417 bitflips += BITS_PER_BYTE - weight;
1418 if (unlikely(bitflips > bitflips_threshold))
1419 return -EBADMSG;
1420 }
1421
1422 for (; len >= sizeof(long);
1423 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001424 unsigned long d = *((unsigned long *)bitmap);
1425 if (d == ~0UL)
1426 continue;
1427 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001428 bitflips += BITS_PER_LONG - weight;
1429 if (unlikely(bitflips > bitflips_threshold))
1430 return -EBADMSG;
1431 }
1432
1433 for (; len > 0; len--, bitmap++) {
1434 weight = hweight8(*bitmap);
1435 bitflips += BITS_PER_BYTE - weight;
1436 if (unlikely(bitflips > bitflips_threshold))
1437 return -EBADMSG;
1438 }
1439
1440 return bitflips;
1441}
1442
1443/**
1444 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1445 * 0xff data
1446 * @data: data buffer to test
1447 * @datalen: data length
1448 * @ecc: ECC buffer
1449 * @ecclen: ECC length
1450 * @extraoob: extra OOB buffer
1451 * @extraooblen: extra OOB length
1452 * @bitflips_threshold: maximum number of bitflips
1453 *
1454 * Check if a data buffer and its associated ECC and OOB data contains only
1455 * 0xff pattern, which means the underlying region has been erased and is
1456 * ready to be programmed.
1457 * The bitflips_threshold specify the maximum number of bitflips before
1458 * considering the region as not erased.
1459 *
1460 * Note:
1461 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1462 * different from the NAND page size. When fixing bitflips, ECC engines will
1463 * report the number of errors per chunk, and the NAND core infrastructure
1464 * expect you to return the maximum number of bitflips for the whole page.
1465 * This is why you should always use this function on a single chunk and
1466 * not on the whole page. After checking each chunk you should update your
1467 * max_bitflips value accordingly.
1468 * 2/ When checking for bitflips in erased pages you should not only check
1469 * the payload data but also their associated ECC data, because a user might
1470 * have programmed almost all bits to 1 but a few. In this case, we
1471 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1472 * this case.
1473 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1474 * data are protected by the ECC engine.
1475 * It could also be used if you support subpages and want to attach some
1476 * extra OOB data to an ECC chunk.
1477 *
1478 * Returns a positive number of bitflips less than or equal to
1479 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1480 * threshold. In case of success, the passed buffers are filled with 0xff.
1481 */
1482int nand_check_erased_ecc_chunk(void *data, int datalen,
1483 void *ecc, int ecclen,
1484 void *extraoob, int extraooblen,
1485 int bitflips_threshold)
1486{
1487 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1488
1489 data_bitflips = nand_check_erased_buf(data, datalen,
1490 bitflips_threshold);
1491 if (data_bitflips < 0)
1492 return data_bitflips;
1493
1494 bitflips_threshold -= data_bitflips;
1495
1496 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1497 if (ecc_bitflips < 0)
1498 return ecc_bitflips;
1499
1500 bitflips_threshold -= ecc_bitflips;
1501
1502 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1503 bitflips_threshold);
1504 if (extraoob_bitflips < 0)
1505 return extraoob_bitflips;
1506
1507 if (data_bitflips)
1508 memset(data, 0xff, datalen);
1509
1510 if (ecc_bitflips)
1511 memset(ecc, 0xff, ecclen);
1512
1513 if (extraoob_bitflips)
1514 memset(extraoob, 0xff, extraooblen);
1515
1516 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1517}
1518EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1519
1520/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001521 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001522 * @mtd: mtd info structure
1523 * @chip: nand chip info structure
1524 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001525 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001526 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001527 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001528 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001529 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001530int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1531 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001532{
1533 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001534 if (oob_required)
1535 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001536 return 0;
1537}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001538EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001539
1540/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001541 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001542 * @mtd: mtd info structure
1543 * @chip: nand chip info structure
1544 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001545 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001546 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001547 *
1548 * We need a special oob layout and handling even when OOB isn't used.
1549 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001550static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001551 struct nand_chip *chip, uint8_t *buf,
1552 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001553{
1554 int eccsize = chip->ecc.size;
1555 int eccbytes = chip->ecc.bytes;
1556 uint8_t *oob = chip->oob_poi;
1557 int steps, size;
1558
1559 for (steps = chip->ecc.steps; steps > 0; steps--) {
1560 chip->read_buf(mtd, buf, eccsize);
1561 buf += eccsize;
1562
1563 if (chip->ecc.prepad) {
1564 chip->read_buf(mtd, oob, chip->ecc.prepad);
1565 oob += chip->ecc.prepad;
1566 }
1567
1568 chip->read_buf(mtd, oob, eccbytes);
1569 oob += eccbytes;
1570
1571 if (chip->ecc.postpad) {
1572 chip->read_buf(mtd, oob, chip->ecc.postpad);
1573 oob += chip->ecc.postpad;
1574 }
1575 }
1576
1577 size = mtd->oobsize - (oob - chip->oob_poi);
1578 if (size)
1579 chip->read_buf(mtd, oob, size);
1580
1581 return 0;
1582}
1583
1584/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001585 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001586 * @mtd: mtd info structure
1587 * @chip: nand chip info structure
1588 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001589 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001590 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001591 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001592static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001593 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Boris Brezillon846031d2016-02-03 20:11:00 +01001595 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001596 int eccbytes = chip->ecc.bytes;
1597 int eccsteps = chip->ecc.steps;
1598 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001599 uint8_t *ecc_calc = chip->buffers->ecccalc;
1600 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001601 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001602
Brian Norris1fbb9382012-05-02 10:14:55 -07001603 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001604
1605 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1606 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1607
Boris Brezillon846031d2016-02-03 20:11:00 +01001608 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1609 chip->ecc.total);
1610 if (ret)
1611 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001612
1613 eccsteps = chip->ecc.steps;
1614 p = buf;
1615
1616 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1617 int stat;
1618
1619 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001620 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001621 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001622 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001623 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001624 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1625 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001626 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001627 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301631 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001632 * @mtd: mtd info structure
1633 * @chip: nand chip info structure
1634 * @data_offs: offset of requested data within the page
1635 * @readlen: data length
1636 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001637 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001638 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001639static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001640 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1641 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001642{
Boris Brezillon846031d2016-02-03 20:11:00 +01001643 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001644 uint8_t *p;
1645 int data_col_addr, i, gaps = 0;
1646 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1647 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001648 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001649 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001650 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001651
Brian Norris7854d3f2011-06-23 14:12:08 -07001652 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001653 start_step = data_offs / chip->ecc.size;
1654 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1655 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301656 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001657
Brian Norris8b6e50c2011-05-25 14:59:01 -07001658 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001659 datafrag_len = num_steps * chip->ecc.size;
1660 eccfrag_len = num_steps * chip->ecc.bytes;
1661
1662 data_col_addr = start_step * chip->ecc.size;
1663 /* If we read not a page aligned data */
1664 if (data_col_addr != 0)
1665 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1666
1667 p = bufpoi + data_col_addr;
1668 chip->read_buf(mtd, p, datafrag_len);
1669
Brian Norris8b6e50c2011-05-25 14:59:01 -07001670 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001671 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1672 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1673
Brian Norris8b6e50c2011-05-25 14:59:01 -07001674 /*
1675 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001676 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001677 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001678 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1679 if (ret)
1680 return ret;
1681
1682 if (oobregion.length < eccfrag_len)
1683 gaps = 1;
1684
Alexey Korolev3d459552008-05-15 17:23:18 +01001685 if (gaps) {
1686 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1687 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1688 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001689 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001690 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001691 * about buswidth alignment in read_buf.
1692 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001693 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001694 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001695 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001696 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001697 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1698 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001699 aligned_len++;
1700
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001701 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001702 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001703 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1704 }
1705
Boris Brezillon846031d2016-02-03 20:11:00 +01001706 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1707 chip->oob_poi, index, eccfrag_len);
1708 if (ret)
1709 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001710
1711 p = bufpoi + data_col_addr;
1712 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1713 int stat;
1714
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001715 stat = chip->ecc.correct(mtd, p,
1716 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001717 if (stat == -EBADMSG &&
1718 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1719 /* check for empty pages with bitflips */
1720 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1721 &chip->buffers->ecccode[i],
1722 chip->ecc.bytes,
1723 NULL, 0,
1724 chip->ecc.strength);
1725 }
1726
Mike Dunn3f91e942012-04-25 12:06:09 -07001727 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001728 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001729 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001730 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001731 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1732 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001733 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001734 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001735}
1736
1737/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001738 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001739 * @mtd: mtd info structure
1740 * @chip: nand chip info structure
1741 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001742 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001743 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001744 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001745 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001746 */
1747static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001748 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001749{
Boris Brezillon846031d2016-02-03 20:11:00 +01001750 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001751 int eccbytes = chip->ecc.bytes;
1752 int eccsteps = chip->ecc.steps;
1753 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001754 uint8_t *ecc_calc = chip->buffers->ecccalc;
1755 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001756 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001757
1758 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1759 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1760 chip->read_buf(mtd, p, eccsize);
1761 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1762 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001763 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001764
Boris Brezillon846031d2016-02-03 20:11:00 +01001765 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1766 chip->ecc.total);
1767 if (ret)
1768 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001769
1770 eccsteps = chip->ecc.steps;
1771 p = buf;
1772
1773 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1774 int stat;
1775
1776 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001777 if (stat == -EBADMSG &&
1778 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1779 /* check for empty pages with bitflips */
1780 stat = nand_check_erased_ecc_chunk(p, eccsize,
1781 &ecc_code[i], eccbytes,
1782 NULL, 0,
1783 chip->ecc.strength);
1784 }
1785
Mike Dunn3f91e942012-04-25 12:06:09 -07001786 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001787 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001788 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001789 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001790 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1791 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001792 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001793 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001794}
1795
1796/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001797 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001798 * @mtd: mtd info structure
1799 * @chip: nand chip info structure
1800 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001801 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001802 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001803 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001804 * Hardware ECC for large page chips, require OOB to be read first. For this
1805 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1806 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1807 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1808 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001809 */
1810static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001811 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001812{
Boris Brezillon846031d2016-02-03 20:11:00 +01001813 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001814 int eccbytes = chip->ecc.bytes;
1815 int eccsteps = chip->ecc.steps;
1816 uint8_t *p = buf;
1817 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001818 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001819 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001820
1821 /* Read the OOB area first */
1822 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1823 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1824 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1825
Boris Brezillon846031d2016-02-03 20:11:00 +01001826 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1827 chip->ecc.total);
1828 if (ret)
1829 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001830
1831 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1832 int stat;
1833
1834 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1835 chip->read_buf(mtd, p, eccsize);
1836 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1837
1838 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001839 if (stat == -EBADMSG &&
1840 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1841 /* check for empty pages with bitflips */
1842 stat = nand_check_erased_ecc_chunk(p, eccsize,
1843 &ecc_code[i], eccbytes,
1844 NULL, 0,
1845 chip->ecc.strength);
1846 }
1847
Mike Dunn3f91e942012-04-25 12:06:09 -07001848 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001849 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001850 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001851 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001852 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1853 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001854 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001855 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001856}
1857
1858/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001859 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001860 * @mtd: mtd info structure
1861 * @chip: nand chip info structure
1862 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001863 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001864 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001865 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001866 * The hw generator calculates the error syndrome automatically. Therefore we
1867 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001868 */
1869static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001870 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001871{
1872 int i, eccsize = chip->ecc.size;
1873 int eccbytes = chip->ecc.bytes;
1874 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001875 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001876 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001877 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001878 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001879
1880 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1881 int stat;
1882
1883 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1884 chip->read_buf(mtd, p, eccsize);
1885
1886 if (chip->ecc.prepad) {
1887 chip->read_buf(mtd, oob, chip->ecc.prepad);
1888 oob += chip->ecc.prepad;
1889 }
1890
1891 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1892 chip->read_buf(mtd, oob, eccbytes);
1893 stat = chip->ecc.correct(mtd, p, oob, NULL);
1894
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001895 oob += eccbytes;
1896
1897 if (chip->ecc.postpad) {
1898 chip->read_buf(mtd, oob, chip->ecc.postpad);
1899 oob += chip->ecc.postpad;
1900 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001901
1902 if (stat == -EBADMSG &&
1903 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1904 /* check for empty pages with bitflips */
1905 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1906 oob - eccpadbytes,
1907 eccpadbytes,
1908 NULL, 0,
1909 chip->ecc.strength);
1910 }
1911
1912 if (stat < 0) {
1913 mtd->ecc_stats.failed++;
1914 } else {
1915 mtd->ecc_stats.corrected += stat;
1916 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1917 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001918 }
1919
1920 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001921 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001922 if (i)
1923 chip->read_buf(mtd, oob, i);
1924
Mike Dunn3f91e942012-04-25 12:06:09 -07001925 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001926}
1927
1928/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001929 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001930 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001931 * @oob: oob destination address
1932 * @ops: oob ops structure
1933 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001934 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001935static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001936 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001937{
Boris Brezillon846031d2016-02-03 20:11:00 +01001938 struct nand_chip *chip = mtd_to_nand(mtd);
1939 int ret;
1940
Florian Fainellif8ac0412010-09-07 13:23:43 +02001941 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001942
Brian Norris0612b9d2011-08-30 18:45:40 -07001943 case MTD_OPS_PLACE_OOB:
1944 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1946 return oob + len;
1947
Boris Brezillon846031d2016-02-03 20:11:00 +01001948 case MTD_OPS_AUTO_OOB:
1949 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1950 ops->ooboffs, len);
1951 BUG_ON(ret);
1952 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001953
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001954 default:
1955 BUG();
1956 }
1957 return NULL;
1958}
1959
1960/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001961 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1962 * @mtd: MTD device structure
1963 * @retry_mode: the retry mode to use
1964 *
1965 * Some vendors supply a special command to shift the Vt threshold, to be used
1966 * when there are too many bitflips in a page (i.e., ECC error). After setting
1967 * a new threshold, the host should retry reading the page.
1968 */
1969static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1970{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001971 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001972
1973 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1974
1975 if (retry_mode >= chip->read_retries)
1976 return -EINVAL;
1977
1978 if (!chip->setup_read_retry)
1979 return -EOPNOTSUPP;
1980
1981 return chip->setup_read_retry(mtd, retry_mode);
1982}
1983
1984/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001985 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001986 * @mtd: MTD device structure
1987 * @from: offset to read from
1988 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001989 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001990 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001991 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001992static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1993 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001994{
Brian Norrise47f3db2012-05-02 10:14:56 -07001995 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001996 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001997 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001998 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001999 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002000 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002001
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002002 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002003 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002004 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002005 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002006 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002008 chipnr = (int)(from >> chip->chip_shift);
2009 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002011 realpage = (int)(from >> chip->page_shift);
2012 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002014 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002016 buf = ops->datbuf;
2017 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002018 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002019
Florian Fainellif8ac0412010-09-07 13:23:43 +02002020 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002021 unsigned int ecc_failures = mtd->ecc_stats.failed;
2022
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002023 bytes = min(mtd->writesize - col, readlen);
2024 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002025
Kamal Dasu66507c72014-05-01 20:51:19 -04002026 if (!aligned)
2027 use_bufpoi = 1;
2028 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002029 use_bufpoi = !virt_addr_valid(buf) ||
2030 !IS_ALIGNED((unsigned long)buf,
2031 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002032 else
2033 use_bufpoi = 0;
2034
Brian Norris8b6e50c2011-05-25 14:59:01 -07002035 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002036 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002037 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2038
2039 if (use_bufpoi && aligned)
2040 pr_debug("%s: using read bounce buffer for buf@%p\n",
2041 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Brian Norrisba84fb52014-01-03 15:13:33 -08002043read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01002044 if (nand_standard_page_accessors(&chip->ecc))
2045 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Mike Dunnedbc45402012-04-25 12:06:11 -07002047 /*
2048 * Now read the page into the buffer. Absent an error,
2049 * the read methods return max bitflips per ecc step.
2050 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002051 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002052 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002053 oob_required,
2054 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002055 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2056 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002057 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002058 col, bytes, bufpoi,
2059 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002060 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002061 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002062 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002063 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002064 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002065 /* Invalidate page cache */
2066 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002067 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002068 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002069
2070 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002071 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002072 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002073 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002074 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002075 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002076 chip->pagebuf_bitflips = ret;
2077 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002078 /* Invalidate page cache */
2079 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002080 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002081 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002083
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002084 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002085 int toread = min(oobreadlen, max_oobsize);
2086
2087 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002088 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002089 oob, ops, toread);
2090 oobreadlen -= toread;
2091 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002092 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002093
2094 if (chip->options & NAND_NEED_READRDY) {
2095 /* Apply delay or wait for ready/busy pin */
2096 if (!chip->dev_ready)
2097 udelay(chip->chip_delay);
2098 else
2099 nand_wait_ready(mtd);
2100 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002101
Brian Norrisba84fb52014-01-03 15:13:33 -08002102 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002103 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002104 retry_mode++;
2105 ret = nand_setup_read_retry(mtd,
2106 retry_mode);
2107 if (ret < 0)
2108 break;
2109
2110 /* Reset failures; retry */
2111 mtd->ecc_stats.failed = ecc_failures;
2112 goto read_retry;
2113 } else {
2114 /* No more retry modes; real failure */
2115 ecc_fail = true;
2116 }
2117 }
2118
2119 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002120 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002121 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002122 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002123 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002124 max_bitflips = max_t(unsigned int, max_bitflips,
2125 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002128 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002129
Brian Norrisba84fb52014-01-03 15:13:33 -08002130 /* Reset to retry mode 0 */
2131 if (retry_mode) {
2132 ret = nand_setup_read_retry(mtd, 0);
2133 if (ret < 0)
2134 break;
2135 retry_mode = 0;
2136 }
2137
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002138 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Brian Norris8b6e50c2011-05-25 14:59:01 -07002141 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 col = 0;
2143 /* Increment page address */
2144 realpage++;
2145
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002146 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /* Check, if we cross a chip boundary */
2148 if (!page) {
2149 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002150 chip->select_chip(mtd, -1);
2151 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002154 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002156 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002157 if (oob)
2158 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Mike Dunn3f91e942012-04-25 12:06:09 -07002160 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002161 return ret;
2162
Brian Norrisb72f3df2013-12-03 11:04:14 -08002163 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002164 return -EBADMSG;
2165
Mike Dunnedbc45402012-04-25 12:06:11 -07002166 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002167}
2168
2169/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002170 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002171 * @mtd: MTD device structure
2172 * @from: offset to read from
2173 * @len: number of bytes to read
2174 * @retlen: pointer to variable to store the number of read bytes
2175 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002176 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002177 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002178 */
2179static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2180 size_t *retlen, uint8_t *buf)
2181{
Brian Norris4a89ff82011-08-30 18:45:45 -07002182 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002183 int ret;
2184
Huang Shijie6a8214a2012-11-19 14:43:30 +08002185 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002186 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002187 ops.len = len;
2188 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002189 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002190 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002191 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002192 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002197 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002198 * @mtd: mtd info structure
2199 * @chip: nand chip info structure
2200 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002201 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002202int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002203{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002204 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002205 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002206 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002207}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002208EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002209
2210/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002211 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002212 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002213 * @mtd: mtd info structure
2214 * @chip: nand chip info structure
2215 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002216 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002217int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2218 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002219{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002220 int length = mtd->oobsize;
2221 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2222 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002223 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002224 int i, toread, sndrnd = 0, pos;
2225
2226 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2227 for (i = 0; i < chip->ecc.steps; i++) {
2228 if (sndrnd) {
2229 pos = eccsize + i * (eccsize + chunk);
2230 if (mtd->writesize > 512)
2231 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2232 else
2233 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2234 } else
2235 sndrnd = 1;
2236 toread = min_t(int, length, chunk);
2237 chip->read_buf(mtd, bufpoi, toread);
2238 bufpoi += toread;
2239 length -= toread;
2240 }
2241 if (length > 0)
2242 chip->read_buf(mtd, bufpoi, length);
2243
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002244 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002245}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002246EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002247
2248/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002249 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002250 * @mtd: mtd info structure
2251 * @chip: nand chip info structure
2252 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002253 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002254int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002255{
2256 int status = 0;
2257 const uint8_t *buf = chip->oob_poi;
2258 int length = mtd->oobsize;
2259
2260 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2261 chip->write_buf(mtd, buf, length);
2262 /* Send command to program the OOB data */
2263 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2264
2265 status = chip->waitfunc(mtd, chip);
2266
Savin Zlobec0d420f92006-06-21 11:51:20 +02002267 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002268}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002269EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002270
2271/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002272 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002273 * with syndrome - only for large page flash
2274 * @mtd: mtd info structure
2275 * @chip: nand chip info structure
2276 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002277 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002278int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2279 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002280{
2281 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2282 int eccsize = chip->ecc.size, length = mtd->oobsize;
2283 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2284 const uint8_t *bufpoi = chip->oob_poi;
2285
2286 /*
2287 * data-ecc-data-ecc ... ecc-oob
2288 * or
2289 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2290 */
2291 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2292 pos = steps * (eccsize + chunk);
2293 steps = 0;
2294 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002295 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002296
2297 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2298 for (i = 0; i < steps; i++) {
2299 if (sndcmd) {
2300 if (mtd->writesize <= 512) {
2301 uint32_t fill = 0xFFFFFFFF;
2302
2303 len = eccsize;
2304 while (len > 0) {
2305 int num = min_t(int, len, 4);
2306 chip->write_buf(mtd, (uint8_t *)&fill,
2307 num);
2308 len -= num;
2309 }
2310 } else {
2311 pos = eccsize + i * (eccsize + chunk);
2312 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2313 }
2314 } else
2315 sndcmd = 1;
2316 len = min_t(int, length, chunk);
2317 chip->write_buf(mtd, bufpoi, len);
2318 bufpoi += len;
2319 length -= len;
2320 }
2321 if (length > 0)
2322 chip->write_buf(mtd, bufpoi, length);
2323
2324 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2325 status = chip->waitfunc(mtd, chip);
2326
2327 return status & NAND_STATUS_FAIL ? -EIO : 0;
2328}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002329EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002330
2331/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002332 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002333 * @mtd: MTD device structure
2334 * @from: offset to read from
2335 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002337 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002339static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2340 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Brian Norrisc00a0992012-05-01 17:12:54 -07002342 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002343 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002344 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002345 int readlen = ops->ooblen;
2346 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002347 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002348 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Brian Norris289c0522011-07-19 10:06:09 -07002350 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302351 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Brian Norris041e4572011-06-23 16:45:24 -07002353 stats = mtd->ecc_stats;
2354
Boris BREZILLON29f10582016-03-07 10:46:52 +01002355 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002356
2357 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002358 pr_debug("%s: attempt to start read outside oob\n",
2359 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002360 return -EINVAL;
2361 }
2362
2363 /* Do not allow reads past end of device */
2364 if (unlikely(from >= mtd->size ||
2365 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2366 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002367 pr_debug("%s: attempt to read beyond end of device\n",
2368 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002369 return -EINVAL;
2370 }
Vitaly Wool70145682006-11-03 18:20:38 +03002371
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002372 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002373 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002375 /* Shift to get page */
2376 realpage = (int)(from >> chip->page_shift);
2377 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Florian Fainellif8ac0412010-09-07 13:23:43 +02002379 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002380 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002381 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002382 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002383 ret = chip->ecc.read_oob(mtd, chip, page);
2384
2385 if (ret < 0)
2386 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002387
2388 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002389 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002390
Brian Norris5bc7c332013-03-13 09:51:31 -07002391 if (chip->options & NAND_NEED_READRDY) {
2392 /* Apply delay or wait for ready/busy pin */
2393 if (!chip->dev_ready)
2394 udelay(chip->chip_delay);
2395 else
2396 nand_wait_ready(mtd);
2397 }
2398
Vitaly Wool70145682006-11-03 18:20:38 +03002399 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002400 if (!readlen)
2401 break;
2402
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002403 /* Increment page address */
2404 realpage++;
2405
2406 page = realpage & chip->pagemask;
2407 /* Check, if we cross a chip boundary */
2408 if (!page) {
2409 chipnr++;
2410 chip->select_chip(mtd, -1);
2411 chip->select_chip(mtd, chipnr);
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002414 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002416 ops->oobretlen = ops->ooblen - readlen;
2417
2418 if (ret < 0)
2419 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002420
2421 if (mtd->ecc_stats.failed - stats.failed)
2422 return -EBADMSG;
2423
2424 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
2427/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002428 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002429 * @mtd: MTD device structure
2430 * @from: offset to read from
2431 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002433 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002435static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2436 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002438 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002439
2440 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002443 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002444 pr_debug("%s: attempt to read beyond end of device\n",
2445 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return -EINVAL;
2447 }
2448
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002449 if (ops->mode != MTD_OPS_PLACE_OOB &&
2450 ops->mode != MTD_OPS_AUTO_OOB &&
2451 ops->mode != MTD_OPS_RAW)
2452 return -ENOTSUPP;
2453
Huang Shijie6a8214a2012-11-19 14:43:30 +08002454 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002456 if (!ops->datbuf)
2457 ret = nand_do_read_oob(mtd, from, ops);
2458 else
2459 ret = nand_do_read_ops(mtd, from, ops);
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462 return ret;
2463}
2464
2465
2466/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002467 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002468 * @mtd: mtd info structure
2469 * @chip: nand chip info structure
2470 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002471 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002472 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002473 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002474 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002475 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002476int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2477 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002478{
2479 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002480 if (oob_required)
2481 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002482
2483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002485EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002487/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002488 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002489 * @mtd: mtd info structure
2490 * @chip: nand chip info structure
2491 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002492 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002493 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002494 *
2495 * We need a special oob layout and handling even when ECC isn't checked.
2496 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002497static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002498 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002499 const uint8_t *buf, int oob_required,
2500 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002501{
2502 int eccsize = chip->ecc.size;
2503 int eccbytes = chip->ecc.bytes;
2504 uint8_t *oob = chip->oob_poi;
2505 int steps, size;
2506
2507 for (steps = chip->ecc.steps; steps > 0; steps--) {
2508 chip->write_buf(mtd, buf, eccsize);
2509 buf += eccsize;
2510
2511 if (chip->ecc.prepad) {
2512 chip->write_buf(mtd, oob, chip->ecc.prepad);
2513 oob += chip->ecc.prepad;
2514 }
2515
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002516 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002517 oob += eccbytes;
2518
2519 if (chip->ecc.postpad) {
2520 chip->write_buf(mtd, oob, chip->ecc.postpad);
2521 oob += chip->ecc.postpad;
2522 }
2523 }
2524
2525 size = mtd->oobsize - (oob - chip->oob_poi);
2526 if (size)
2527 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002528
2529 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002530}
2531/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002532 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002533 * @mtd: mtd info structure
2534 * @chip: nand chip info structure
2535 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002536 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002537 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002538 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002539static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002540 const uint8_t *buf, int oob_required,
2541 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002542{
Boris Brezillon846031d2016-02-03 20:11:00 +01002543 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002544 int eccbytes = chip->ecc.bytes;
2545 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002546 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002547 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002548
Brian Norris7854d3f2011-06-23 14:12:08 -07002549 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002550 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2551 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002552
Boris Brezillon846031d2016-02-03 20:11:00 +01002553 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2554 chip->ecc.total);
2555 if (ret)
2556 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002557
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002558 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002559}
2560
2561/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002562 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002563 * @mtd: mtd info structure
2564 * @chip: nand chip info structure
2565 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002566 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002567 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002569static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002570 const uint8_t *buf, int oob_required,
2571 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002572{
Boris Brezillon846031d2016-02-03 20:11:00 +01002573 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002574 int eccbytes = chip->ecc.bytes;
2575 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002576 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002577 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002578
2579 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2580 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002581 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002582 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2583 }
2584
Boris Brezillon846031d2016-02-03 20:11:00 +01002585 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2586 chip->ecc.total);
2587 if (ret)
2588 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002589
2590 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002591
2592 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002593}
2594
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302595
2596/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002597 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302598 * @mtd: mtd info structure
2599 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002600 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302601 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002602 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302603 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002604 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302605 */
2606static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2607 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002608 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002609 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302610{
2611 uint8_t *oob_buf = chip->oob_poi;
2612 uint8_t *ecc_calc = chip->buffers->ecccalc;
2613 int ecc_size = chip->ecc.size;
2614 int ecc_bytes = chip->ecc.bytes;
2615 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302616 uint32_t start_step = offset / ecc_size;
2617 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2618 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002619 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302620
2621 for (step = 0; step < ecc_steps; step++) {
2622 /* configure controller for WRITE access */
2623 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2624
2625 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002626 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302627
2628 /* mask ECC of un-touched subpages by padding 0xFF */
2629 if ((step < start_step) || (step > end_step))
2630 memset(ecc_calc, 0xff, ecc_bytes);
2631 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002632 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302633
2634 /* mask OOB of un-touched subpages by padding 0xFF */
2635 /* if oob_required, preserve OOB metadata of written subpage */
2636 if (!oob_required || (step < start_step) || (step > end_step))
2637 memset(oob_buf, 0xff, oob_bytes);
2638
Brian Norrisd6a950802013-08-08 17:16:36 -07002639 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302640 ecc_calc += ecc_bytes;
2641 oob_buf += oob_bytes;
2642 }
2643
2644 /* copy calculated ECC for whole page to chip->buffer->oob */
2645 /* this include masked-value(0xFF) for unwritten subpages */
2646 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002647 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2648 chip->ecc.total);
2649 if (ret)
2650 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302651
2652 /* write OOB buffer to NAND device */
2653 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2654
2655 return 0;
2656}
2657
2658
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002659/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002660 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002661 * @mtd: mtd info structure
2662 * @chip: nand chip info structure
2663 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002664 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002665 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002666 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002667 * The hw generator calculates the error syndrome automatically. Therefore we
2668 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002669 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002670static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002671 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002672 const uint8_t *buf, int oob_required,
2673 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002674{
2675 int i, eccsize = chip->ecc.size;
2676 int eccbytes = chip->ecc.bytes;
2677 int eccsteps = chip->ecc.steps;
2678 const uint8_t *p = buf;
2679 uint8_t *oob = chip->oob_poi;
2680
2681 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2682
2683 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2684 chip->write_buf(mtd, p, eccsize);
2685
2686 if (chip->ecc.prepad) {
2687 chip->write_buf(mtd, oob, chip->ecc.prepad);
2688 oob += chip->ecc.prepad;
2689 }
2690
2691 chip->ecc.calculate(mtd, p, oob);
2692 chip->write_buf(mtd, oob, eccbytes);
2693 oob += eccbytes;
2694
2695 if (chip->ecc.postpad) {
2696 chip->write_buf(mtd, oob, chip->ecc.postpad);
2697 oob += chip->ecc.postpad;
2698 }
2699 }
2700
2701 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002702 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002703 if (i)
2704 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002705
2706 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002707}
2708
2709/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002710 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002711 * @mtd: MTD device structure
2712 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302713 * @offset: address offset within the page
2714 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002715 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002716 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002717 * @page: page number to write
2718 * @cached: cached programming
2719 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002720 */
2721static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302722 uint32_t offset, int data_len, const uint8_t *buf,
2723 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002724{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302725 int status, subpage;
2726
2727 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2728 chip->ecc.write_subpage)
2729 subpage = offset || (data_len < mtd->writesize);
2730 else
2731 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002732
Marc Gonzalez3371d662016-11-15 10:56:20 +01002733 if (nand_standard_page_accessors(&chip->ecc))
2734 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002735
David Woodhouse956e9442006-09-25 17:12:39 +01002736 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302737 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002738 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302739 else if (subpage)
2740 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002741 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002742 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002743 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2744 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002745
2746 if (status < 0)
2747 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002748
2749 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002750 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002751 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002752 */
2753 cached = 0;
2754
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002755 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002756
Marc Gonzalez3371d662016-11-15 10:56:20 +01002757 if (nand_standard_page_accessors(&chip->ecc))
2758 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002759 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002760 /*
2761 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002762 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002763 */
2764 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2765 status = chip->errstat(mtd, chip, FL_WRITING, status,
2766 page);
2767
2768 if (status & NAND_STATUS_FAIL)
2769 return -EIO;
2770 } else {
2771 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002772 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002773 }
2774
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002775 return 0;
2776}
2777
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002778/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002779 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002780 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002781 * @oob: oob data buffer
2782 * @len: oob data write length
2783 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002784 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002785static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2786 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002787{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002788 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002789 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002790
2791 /*
2792 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2793 * data from a previous OOB read.
2794 */
2795 memset(chip->oob_poi, 0xff, mtd->oobsize);
2796
Florian Fainellif8ac0412010-09-07 13:23:43 +02002797 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002798
Brian Norris0612b9d2011-08-30 18:45:40 -07002799 case MTD_OPS_PLACE_OOB:
2800 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002801 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2802 return oob + len;
2803
Boris Brezillon846031d2016-02-03 20:11:00 +01002804 case MTD_OPS_AUTO_OOB:
2805 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2806 ops->ooboffs, len);
2807 BUG_ON(ret);
2808 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002809
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002810 default:
2811 BUG();
2812 }
2813 return NULL;
2814}
2815
Florian Fainellif8ac0412010-09-07 13:23:43 +02002816#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002817
2818/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002819 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002820 * @mtd: MTD device structure
2821 * @to: offset to write to
2822 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002823 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002824 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002825 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002826static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2827 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002828{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002829 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002830 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002831 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002832
2833 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002834 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002835
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002836 uint8_t *oob = ops->oobbuf;
2837 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302838 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002839 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002840
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002841 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002842 if (!writelen)
2843 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002844
Brian Norris8b6e50c2011-05-25 14:59:01 -07002845 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002847 pr_notice("%s: attempt to write non page aligned data\n",
2848 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002849 return -EINVAL;
2850 }
2851
Thomas Gleixner29072b92006-09-28 15:38:36 +02002852 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002853
Thomas Gleixner6a930962006-06-28 00:11:45 +02002854 chipnr = (int)(to >> chip->chip_shift);
2855 chip->select_chip(mtd, chipnr);
2856
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002857 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002858 if (nand_check_wp(mtd)) {
2859 ret = -EIO;
2860 goto err_out;
2861 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002862
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002863 realpage = (int)(to >> chip->page_shift);
2864 page = realpage & chip->pagemask;
2865 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2866
2867 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002868 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2869 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002870 chip->pagebuf = -1;
2871
Maxim Levitsky782ce792010-02-22 20:39:36 +02002872 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002873 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2874 ret = -EINVAL;
2875 goto err_out;
2876 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002877
Florian Fainellif8ac0412010-09-07 13:23:43 +02002878 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002879 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002880 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002881 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002882 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002883 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002884
Kamal Dasu66507c72014-05-01 20:51:19 -04002885 if (part_pagewr)
2886 use_bufpoi = 1;
2887 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002888 use_bufpoi = !virt_addr_valid(buf) ||
2889 !IS_ALIGNED((unsigned long)buf,
2890 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002891 else
2892 use_bufpoi = 0;
2893
2894 /* Partial page write?, or need to use bounce buffer */
2895 if (use_bufpoi) {
2896 pr_debug("%s: using write bounce buffer for buf@%p\n",
2897 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002898 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002899 if (part_pagewr)
2900 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002901 chip->pagebuf = -1;
2902 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2903 memcpy(&chip->buffers->databuf[column], buf, bytes);
2904 wbuf = chip->buffers->databuf;
2905 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002906
Maxim Levitsky782ce792010-02-22 20:39:36 +02002907 if (unlikely(oob)) {
2908 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002909 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002910 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002911 } else {
2912 /* We still need to erase leftover OOB data */
2913 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002914 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002915
2916 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
2917 oob_required, page, cached,
2918 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002919 if (ret)
2920 break;
2921
2922 writelen -= bytes;
2923 if (!writelen)
2924 break;
2925
Thomas Gleixner29072b92006-09-28 15:38:36 +02002926 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002927 buf += bytes;
2928 realpage++;
2929
2930 page = realpage & chip->pagemask;
2931 /* Check, if we cross a chip boundary */
2932 if (!page) {
2933 chipnr++;
2934 chip->select_chip(mtd, -1);
2935 chip->select_chip(mtd, chipnr);
2936 }
2937 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002938
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002939 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002940 if (unlikely(oob))
2941 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002942
2943err_out:
2944 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002945 return ret;
2946}
2947
2948/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002949 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002950 * @mtd: MTD device structure
2951 * @to: offset to write to
2952 * @len: number of bytes to write
2953 * @retlen: pointer to variable to store the number of written bytes
2954 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002955 *
2956 * NAND write with ECC. Used when performing writes in interrupt context, this
2957 * may for example be called by mtdoops when writing an oops while in panic.
2958 */
2959static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2960 size_t *retlen, const uint8_t *buf)
2961{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002962 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002963 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002964 int ret;
2965
Brian Norris8b6e50c2011-05-25 14:59:01 -07002966 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002967 panic_nand_wait(mtd, chip, 400);
2968
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002970 panic_nand_get_device(chip, mtd, FL_WRITING);
2971
Brian Norris0ec56dc2015-02-28 02:02:30 -08002972 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002973 ops.len = len;
2974 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002975 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002976
Brian Norris4a89ff82011-08-30 18:45:45 -07002977 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002978
Brian Norris4a89ff82011-08-30 18:45:45 -07002979 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002980 return ret;
2981}
2982
2983/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002984 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002985 * @mtd: MTD device structure
2986 * @to: offset to write to
2987 * @len: number of bytes to write
2988 * @retlen: pointer to variable to store the number of written bytes
2989 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002991 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002993static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002994 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Brian Norris4a89ff82011-08-30 18:45:45 -07002996 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997 int ret;
2998
Huang Shijie6a8214a2012-11-19 14:43:30 +08002999 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003000 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003001 ops.len = len;
3002 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003003 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003004 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003005 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003006 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003007 return ret;
3008}
3009
3010/**
3011 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003012 * @mtd: MTD device structure
3013 * @to: offset to write to
3014 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003015 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003016 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003017 */
3018static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3019 struct mtd_oob_ops *ops)
3020{
Adrian Hunter03736152007-01-31 17:58:29 +02003021 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003022 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Brian Norris289c0522011-07-19 10:06:09 -07003024 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303025 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Boris BREZILLON29f10582016-03-07 10:46:52 +01003027 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003030 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003031 pr_debug("%s: attempt to write past end of page\n",
3032 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return -EINVAL;
3034 }
3035
Adrian Hunter03736152007-01-31 17:58:29 +02003036 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003037 pr_debug("%s: attempt to start write outside oob\n",
3038 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003039 return -EINVAL;
3040 }
3041
Jason Liu775adc3d42011-02-25 13:06:18 +08003042 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003043 if (unlikely(to >= mtd->size ||
3044 ops->ooboffs + ops->ooblen >
3045 ((mtd->size >> chip->page_shift) -
3046 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003047 pr_debug("%s: attempt to write beyond end of device\n",
3048 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003049 return -EINVAL;
3050 }
3051
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003052 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003053
3054 /*
3055 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3056 * of my DiskOnChip 2000 test units) will clear the whole data page too
3057 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3058 * it in the doc2000 driver in August 1999. dwmw2.
3059 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003060 nand_reset(chip, chipnr);
3061
3062 chip->select_chip(mtd, chipnr);
3063
3064 /* Shift to get page */
3065 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003068 if (nand_check_wp(mtd)) {
3069 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003070 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003071 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003074 if (page == chip->pagebuf)
3075 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003077 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003078
Brian Norris0612b9d2011-08-30 18:45:40 -07003079 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003080 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3081 else
3082 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003083
Huang Shijieb0bb6902012-11-19 14:43:29 +08003084 chip->select_chip(mtd, -1);
3085
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003086 if (status)
3087 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Vitaly Wool70145682006-11-03 18:20:38 +03003089 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003091 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003094/**
3095 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003096 * @mtd: MTD device structure
3097 * @to: offset to write to
3098 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003099 */
3100static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3101 struct mtd_oob_ops *ops)
3102{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003103 int ret = -ENOTSUPP;
3104
3105 ops->retlen = 0;
3106
3107 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003108 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003109 pr_debug("%s: attempt to write beyond end of device\n",
3110 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003111 return -EINVAL;
3112 }
3113
Huang Shijie6a8214a2012-11-19 14:43:30 +08003114 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003115
Florian Fainellif8ac0412010-09-07 13:23:43 +02003116 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003117 case MTD_OPS_PLACE_OOB:
3118 case MTD_OPS_AUTO_OOB:
3119 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003120 break;
3121
3122 default:
3123 goto out;
3124 }
3125
3126 if (!ops->datbuf)
3127 ret = nand_do_write_oob(mtd, to, ops);
3128 else
3129 ret = nand_do_write_ops(mtd, to, ops);
3130
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003131out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003132 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return ret;
3134}
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136/**
Brian Norris49c50b92014-05-06 16:02:19 -07003137 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003138 * @mtd: MTD device structure
3139 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 *
Brian Norris49c50b92014-05-06 16:02:19 -07003141 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 */
Brian Norris49c50b92014-05-06 16:02:19 -07003143static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003145 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003147 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3148 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003149
3150 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
3152
3153/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003155 * @mtd: MTD device structure
3156 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003160static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
David Woodhousee0c7d762006-05-13 18:07:53 +01003162 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003166 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003167 * @mtd: MTD device structure
3168 * @instr: erase instruction
3169 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003173int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3174 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Adrian Hunter69423d92008-12-10 13:37:21 +00003176 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003177 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003178 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Brian Norris289c0522011-07-19 10:06:09 -07003180 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3181 __func__, (unsigned long long)instr->addr,
3182 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303184 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003188 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003191 page = (int)(instr->addr >> chip->page_shift);
3192 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003195 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003198 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 /* Check, if it is write protected */
3201 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003202 pr_debug("%s: device is write protected!\n",
3203 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 instr->state = MTD_ERASE_FAILED;
3205 goto erase_exit;
3206 }
3207
3208 /* Loop through the pages */
3209 len = instr->len;
3210
3211 instr->state = MTD_ERASING;
3212
3213 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003214 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003215 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303216 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003217 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3218 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 instr->state = MTD_ERASE_FAILED;
3220 goto erase_exit;
3221 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003222
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003223 /*
3224 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003225 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003226 */
3227 if (page <= chip->pagebuf && chip->pagebuf <
3228 (page + pages_per_block))
3229 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Brian Norris49c50b92014-05-06 16:02:19 -07003231 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003233 /*
3234 * See if operation failed and additional status checks are
3235 * available
3236 */
3237 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3238 status = chip->errstat(mtd, chip, FL_ERASING,
3239 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003242 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003243 pr_debug("%s: failed erase, page 0x%08x\n",
3244 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003246 instr->fail_addr =
3247 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 goto erase_exit;
3249 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003252 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 page += pages_per_block;
3254
3255 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003256 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003258 chip->select_chip(mtd, -1);
3259 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
3261 }
3262 instr->state = MTD_ERASE_DONE;
3263
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003264erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003269 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 nand_release_device(mtd);
3271
David Woodhouse49defc02007-10-06 15:01:59 -04003272 /* Do call back function */
3273 if (!ret)
3274 mtd_erase_callback(instr);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 /* Return more or less happy */
3277 return ret;
3278}
3279
3280/**
3281 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003282 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003286static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Brian Norris289c0522011-07-19 10:06:09 -07003288 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003291 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003293 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003297 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003298 * @mtd: MTD device structure
3299 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003301static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303303 struct nand_chip *chip = mtd_to_nand(mtd);
3304 int chipnr = (int)(offs >> chip->chip_shift);
3305 int ret;
3306
3307 /* Select the NAND device */
3308 nand_get_device(mtd, FL_READING);
3309 chip->select_chip(mtd, chipnr);
3310
3311 ret = nand_block_checkbad(mtd, offs, 0);
3312
3313 chip->select_chip(mtd, -1);
3314 nand_release_device(mtd);
3315
3316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
3319/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003320 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003321 * @mtd: MTD device structure
3322 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003324static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 int ret;
3327
Florian Fainellif8ac0412010-09-07 13:23:43 +02003328 ret = nand_block_isbad(mtd, ofs);
3329 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003330 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 if (ret > 0)
3332 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003333 return ret;
3334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Brian Norris5a0edb22013-07-30 17:52:58 -07003336 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337}
3338
3339/**
Zach Brown56718422017-01-10 13:30:20 -06003340 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3341 * @mtd: MTD device structure
3342 * @ofs: offset relative to mtd start
3343 * @len: length of mtd
3344 */
3345static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3346{
3347 struct nand_chip *chip = mtd_to_nand(mtd);
3348 u32 part_start_block;
3349 u32 part_end_block;
3350 u32 part_start_die;
3351 u32 part_end_die;
3352
3353 /*
3354 * max_bb_per_die and blocks_per_die used to determine
3355 * the maximum bad block count.
3356 */
3357 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3358 return -ENOTSUPP;
3359
3360 /* Get the start and end of the partition in erase blocks. */
3361 part_start_block = mtd_div_by_eb(ofs, mtd);
3362 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3363
3364 /* Get the start and end LUNs of the partition. */
3365 part_start_die = part_start_block / chip->blocks_per_die;
3366 part_end_die = part_end_block / chip->blocks_per_die;
3367
3368 /*
3369 * Look up the bad blocks per unit and multiply by the number of units
3370 * that the partition spans.
3371 */
3372 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3373}
3374
3375/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003376 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3377 * @mtd: MTD device structure
3378 * @chip: nand chip info structure
3379 * @addr: feature address.
3380 * @subfeature_param: the subfeature parameters, a four bytes array.
3381 */
3382static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3383 int addr, uint8_t *subfeature_param)
3384{
3385 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003386 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003387
David Mosbergerd914c932013-05-29 15:30:13 +03003388 if (!chip->onfi_version ||
3389 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3390 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003391 return -EINVAL;
3392
3393 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003394 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3395 chip->write_byte(mtd, subfeature_param[i]);
3396
Huang Shijie7db03ec2012-09-13 14:57:52 +08003397 status = chip->waitfunc(mtd, chip);
3398 if (status & NAND_STATUS_FAIL)
3399 return -EIO;
3400 return 0;
3401}
3402
3403/**
3404 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3405 * @mtd: MTD device structure
3406 * @chip: nand chip info structure
3407 * @addr: feature address.
3408 * @subfeature_param: the subfeature parameters, a four bytes array.
3409 */
3410static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3411 int addr, uint8_t *subfeature_param)
3412{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003413 int i;
3414
David Mosbergerd914c932013-05-29 15:30:13 +03003415 if (!chip->onfi_version ||
3416 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3417 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003418 return -EINVAL;
3419
Huang Shijie7db03ec2012-09-13 14:57:52 +08003420 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003421 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3422 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003423 return 0;
3424}
3425
3426/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003427 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3428 * -ENOTSUPP
3429 * @mtd: MTD device structure
3430 * @chip: nand chip info structure
3431 * @addr: feature address.
3432 * @subfeature_param: the subfeature parameters, a four bytes array.
3433 *
3434 * Should be used by NAND controller drivers that do not support the SET/GET
3435 * FEATURES operations.
3436 */
3437int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3438 struct nand_chip *chip, int addr,
3439 u8 *subfeature_param)
3440{
3441 return -ENOTSUPP;
3442}
3443EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3444
3445/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003446 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003447 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003448 */
3449static int nand_suspend(struct mtd_info *mtd)
3450{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003451 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003452}
3453
3454/**
3455 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003456 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003457 */
3458static void nand_resume(struct mtd_info *mtd)
3459{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003460 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003461
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003462 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003463 nand_release_device(mtd);
3464 else
Brian Norrisd0370212011-07-19 10:06:08 -07003465 pr_err("%s called for a chip which is not in suspended state\n",
3466 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003467}
3468
Scott Branden72ea4032014-11-20 11:18:05 -08003469/**
3470 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3471 * prevent further operations
3472 * @mtd: MTD device structure
3473 */
3474static void nand_shutdown(struct mtd_info *mtd)
3475{
Brian Norris9ca641b2015-11-09 16:37:28 -08003476 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003477}
3478
Brian Norris8b6e50c2011-05-25 14:59:01 -07003479/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003480static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003481{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003482 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003485 if (!chip->chip_delay)
3486 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
3488 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003489 if (chip->cmdfunc == NULL)
3490 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003493 if (chip->waitfunc == NULL)
3494 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003496 if (!chip->select_chip)
3497 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003498
Huang Shijie4204ccc2013-08-16 10:10:07 +08003499 /* set for ONFI nand */
3500 if (!chip->onfi_set_features)
3501 chip->onfi_set_features = nand_onfi_set_features;
3502 if (!chip->onfi_get_features)
3503 chip->onfi_get_features = nand_onfi_get_features;
3504
Brian Norris68e80782013-07-18 01:17:02 -07003505 /* If called twice, pointers that depend on busw may need to be reset */
3506 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003507 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3508 if (!chip->read_word)
3509 chip->read_word = nand_read_word;
3510 if (!chip->block_bad)
3511 chip->block_bad = nand_block_bad;
3512 if (!chip->block_markbad)
3513 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003514 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003515 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003516 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3517 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003518 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003519 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003520 if (!chip->scan_bbt)
3521 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003522
3523 if (!chip->controller) {
3524 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003525 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003526 }
3527
Masahiro Yamada477544c2017-03-30 17:15:05 +09003528 if (!chip->buf_align)
3529 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003530}
3531
Brian Norris8b6e50c2011-05-25 14:59:01 -07003532/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003533static void sanitize_string(uint8_t *s, size_t len)
3534{
3535 ssize_t i;
3536
Brian Norris8b6e50c2011-05-25 14:59:01 -07003537 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003538 s[len - 1] = 0;
3539
Brian Norris8b6e50c2011-05-25 14:59:01 -07003540 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003541 for (i = 0; i < len - 1; i++) {
3542 if (s[i] < ' ' || s[i] > 127)
3543 s[i] = '?';
3544 }
3545
Brian Norris8b6e50c2011-05-25 14:59:01 -07003546 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003547 strim(s);
3548}
3549
3550static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3551{
3552 int i;
3553 while (len--) {
3554 crc ^= *p++ << 8;
3555 for (i = 0; i < 8; i++)
3556 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3557 }
3558
3559 return crc;
3560}
3561
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003562/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003563static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3564 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003565{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003566 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003567 struct onfi_ext_param_page *ep;
3568 struct onfi_ext_section *s;
3569 struct onfi_ext_ecc_info *ecc;
3570 uint8_t *cursor;
3571 int ret = -EINVAL;
3572 int len;
3573 int i;
3574
3575 len = le16_to_cpu(p->ext_param_page_length) * 16;
3576 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003577 if (!ep)
3578 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003579
3580 /* Send our own NAND_CMD_PARAM. */
3581 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3582
3583 /* Use the Change Read Column command to skip the ONFI param pages. */
3584 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3585 sizeof(*p) * p->num_of_param_pages , -1);
3586
3587 /* Read out the Extended Parameter Page. */
3588 chip->read_buf(mtd, (uint8_t *)ep, len);
3589 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3590 != le16_to_cpu(ep->crc))) {
3591 pr_debug("fail in the CRC.\n");
3592 goto ext_out;
3593 }
3594
3595 /*
3596 * Check the signature.
3597 * Do not strictly follow the ONFI spec, maybe changed in future.
3598 */
3599 if (strncmp(ep->sig, "EPPS", 4)) {
3600 pr_debug("The signature is invalid.\n");
3601 goto ext_out;
3602 }
3603
3604 /* find the ECC section. */
3605 cursor = (uint8_t *)(ep + 1);
3606 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3607 s = ep->sections + i;
3608 if (s->type == ONFI_SECTION_TYPE_2)
3609 break;
3610 cursor += s->length * 16;
3611 }
3612 if (i == ONFI_EXT_SECTION_MAX) {
3613 pr_debug("We can not find the ECC section.\n");
3614 goto ext_out;
3615 }
3616
3617 /* get the info we want. */
3618 ecc = (struct onfi_ext_ecc_info *)cursor;
3619
Brian Norris4ae7d222013-09-16 18:20:21 -07003620 if (!ecc->codeword_size) {
3621 pr_debug("Invalid codeword size\n");
3622 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003623 }
3624
Brian Norris4ae7d222013-09-16 18:20:21 -07003625 chip->ecc_strength_ds = ecc->ecc_bits;
3626 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003627 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003628
3629ext_out:
3630 kfree(ep);
3631 return ret;
3632}
3633
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003634/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003635 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003636 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003637static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003638{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003639 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003640 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003641 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003642 int val;
3643
Brian Norris7854d3f2011-06-23 14:12:08 -07003644 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003645 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3646 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3647 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3648 return 0;
3649
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003650 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3651 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003652 for (j = 0; j < sizeof(*p); j++)
3653 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003654 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3655 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003656 break;
3657 }
3658 }
3659
Brian Norrisc7f23a72013-08-13 10:51:55 -07003660 if (i == 3) {
3661 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003662 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003663 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003664
Brian Norris8b6e50c2011-05-25 14:59:01 -07003665 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003666 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003667 if (val & (1 << 5))
3668 chip->onfi_version = 23;
3669 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003670 chip->onfi_version = 22;
3671 else if (val & (1 << 3))
3672 chip->onfi_version = 21;
3673 else if (val & (1 << 2))
3674 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003675 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003676 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003677
3678 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003679 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003680 return 0;
3681 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003682
3683 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3684 sanitize_string(p->model, sizeof(p->model));
3685 if (!mtd->name)
3686 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003687
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003688 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003689
3690 /*
3691 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3692 * (don't ask me who thought of this...). MTD assumes that these
3693 * dimensions will be power-of-2, so just truncate the remaining area.
3694 */
3695 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3696 mtd->erasesize *= mtd->writesize;
3697
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003698 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003699
3700 /* See erasesize comment */
3701 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003702 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003703 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003704
Zach Brown34da5f52017-01-10 13:30:21 -06003705 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3706 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3707
Huang Shijiee2985fc2013-05-17 11:17:30 +08003708 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003709 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003710
Huang Shijie10c86ba2013-05-17 11:17:26 +08003711 if (p->ecc_bits != 0xff) {
3712 chip->ecc_strength_ds = p->ecc_bits;
3713 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003714 } else if (chip->onfi_version >= 21 &&
3715 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3716
3717 /*
3718 * The nand_flash_detect_ext_param_page() uses the
3719 * Change Read Column command which maybe not supported
3720 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3721 * now. We do not replace user supplied command function.
3722 */
3723 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3724 chip->cmdfunc = nand_command_lp;
3725
3726 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003727 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003728 pr_warn("Failed to detect ONFI extended param page\n");
3729 } else {
3730 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003731 }
3732
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003733 return 1;
3734}
3735
3736/*
Huang Shijie91361812014-02-21 13:39:40 +08003737 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3738 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003739static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003740{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003741 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003742 struct nand_jedec_params *p = &chip->jedec_params;
3743 struct jedec_ecc_info *ecc;
3744 int val;
3745 int i, j;
3746
3747 /* Try JEDEC for unknown chip or LP */
3748 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3749 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3750 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3751 chip->read_byte(mtd) != 'C')
3752 return 0;
3753
3754 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3755 for (i = 0; i < 3; i++) {
3756 for (j = 0; j < sizeof(*p); j++)
3757 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3758
3759 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3760 le16_to_cpu(p->crc))
3761 break;
3762 }
3763
3764 if (i == 3) {
3765 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3766 return 0;
3767 }
3768
3769 /* Check version */
3770 val = le16_to_cpu(p->revision);
3771 if (val & (1 << 2))
3772 chip->jedec_version = 10;
3773 else if (val & (1 << 1))
3774 chip->jedec_version = 1; /* vendor specific version */
3775
3776 if (!chip->jedec_version) {
3777 pr_info("unsupported JEDEC version: %d\n", val);
3778 return 0;
3779 }
3780
3781 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3782 sanitize_string(p->model, sizeof(p->model));
3783 if (!mtd->name)
3784 mtd->name = p->model;
3785
3786 mtd->writesize = le32_to_cpu(p->byte_per_page);
3787
3788 /* Please reference to the comment for nand_flash_detect_onfi. */
3789 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3790 mtd->erasesize *= mtd->writesize;
3791
3792 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3793
3794 /* Please reference to the comment for nand_flash_detect_onfi. */
3795 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3796 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3797 chip->bits_per_cell = p->bits_per_cell;
3798
3799 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003800 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003801
3802 /* ECC info */
3803 ecc = &p->ecc_info[0];
3804
3805 if (ecc->codeword_size >= 9) {
3806 chip->ecc_strength_ds = ecc->ecc_bits;
3807 chip->ecc_step_ds = 1 << ecc->codeword_size;
3808 } else {
3809 pr_warn("Invalid codeword size\n");
3810 }
3811
3812 return 1;
3813}
3814
3815/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003816 * nand_id_has_period - Check if an ID string has a given wraparound period
3817 * @id_data: the ID string
3818 * @arrlen: the length of the @id_data array
3819 * @period: the period of repitition
3820 *
3821 * Check if an ID string is repeated within a given sequence of bytes at
3822 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003823 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003824 * if the repetition has a period of @period; otherwise, returns zero.
3825 */
3826static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3827{
3828 int i, j;
3829 for (i = 0; i < period; i++)
3830 for (j = i + period; j < arrlen; j += period)
3831 if (id_data[i] != id_data[j])
3832 return 0;
3833 return 1;
3834}
3835
3836/*
3837 * nand_id_len - Get the length of an ID string returned by CMD_READID
3838 * @id_data: the ID string
3839 * @arrlen: the length of the @id_data array
3840
3841 * Returns the length of the ID string, according to known wraparound/trailing
3842 * zero patterns. If no pattern exists, returns the length of the array.
3843 */
3844static int nand_id_len(u8 *id_data, int arrlen)
3845{
3846 int last_nonzero, period;
3847
3848 /* Find last non-zero byte */
3849 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3850 if (id_data[last_nonzero])
3851 break;
3852
3853 /* All zeros */
3854 if (last_nonzero < 0)
3855 return 0;
3856
3857 /* Calculate wraparound period */
3858 for (period = 1; period < arrlen; period++)
3859 if (nand_id_has_period(id_data, arrlen, period))
3860 break;
3861
3862 /* There's a repeated pattern */
3863 if (period < arrlen)
3864 return period;
3865
3866 /* There are trailing zeros */
3867 if (last_nonzero < arrlen - 1)
3868 return last_nonzero + 1;
3869
3870 /* No pattern detected */
3871 return arrlen;
3872}
3873
Huang Shijie7db906b2013-09-25 14:58:11 +08003874/* Extract the bits of per cell from the 3rd byte of the extended ID */
3875static int nand_get_bits_per_cell(u8 cellinfo)
3876{
3877 int bits;
3878
3879 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3880 bits >>= NAND_CI_CELLTYPE_SHIFT;
3881 return bits + 1;
3882}
3883
Brian Norrise3b88bd2012-09-24 20:40:52 -07003884/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003885 * Many new NAND share similar device ID codes, which represent the size of the
3886 * chip. The rest of the parameters must be decoded according to generic or
3887 * manufacturer-specific "extended ID" decoding patterns.
3888 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003889void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003890{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003891 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003892 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003893 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003894 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003895 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003896 /* The 4th id byte is the important one */
3897 extid = id_data[3];
3898
Boris Brezillon01389b62016-06-08 10:30:18 +02003899 /* Calc pagesize */
3900 mtd->writesize = 1024 << (extid & 0x03);
3901 extid >>= 2;
3902 /* Calc oobsize */
3903 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3904 extid >>= 2;
3905 /* Calc blocksize. Blocksize is multiples of 64KiB */
3906 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3907 extid >>= 2;
3908 /* Get buswidth information */
3909 if (extid & 0x1)
3910 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003911}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003912EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003913
3914/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003915 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3916 * decodes a matching ID table entry and assigns the MTD size parameters for
3917 * the chip.
3918 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003919static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003920{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003921 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003922
3923 mtd->erasesize = type->erasesize;
3924 mtd->writesize = type->pagesize;
3925 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003926
Huang Shijie1c195e92013-09-25 14:58:12 +08003927 /* All legacy ID NAND are small-page, SLC */
3928 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003929}
3930
3931/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003932 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3933 * heuristic patterns using various detected parameters (e.g., manufacturer,
3934 * page size, cell-type information).
3935 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003936static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003937{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003938 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003939
3940 /* Set the bad block position */
3941 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3942 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3943 else
3944 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003945}
3946
Huang Shijieec6e87e2013-03-15 11:01:00 +08003947static inline bool is_full_id_nand(struct nand_flash_dev *type)
3948{
3949 return type->id_len;
3950}
3951
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003952static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003953 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003954{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003955 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003956 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003957
Huang Shijieec6e87e2013-03-15 11:01:00 +08003958 if (!strncmp(type->id, id_data, type->id_len)) {
3959 mtd->writesize = type->pagesize;
3960 mtd->erasesize = type->erasesize;
3961 mtd->oobsize = type->oobsize;
3962
Huang Shijie7db906b2013-09-25 14:58:11 +08003963 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003964 chip->chipsize = (uint64_t)type->chipsize << 20;
3965 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003966 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3967 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003968 chip->onfi_timing_mode_default =
3969 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003970
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003971 if (!mtd->name)
3972 mtd->name = type->name;
3973
Huang Shijieec6e87e2013-03-15 11:01:00 +08003974 return true;
3975 }
3976 return false;
3977}
3978
Brian Norris7e74c2d2012-09-24 20:40:49 -07003979/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003980 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3981 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3982 * table.
3983 */
3984static void nand_manufacturer_detect(struct nand_chip *chip)
3985{
3986 /*
3987 * Try manufacturer detection if available and use
3988 * nand_decode_ext_id() otherwise.
3989 */
3990 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3991 chip->manufacturer.desc->ops->detect)
3992 chip->manufacturer.desc->ops->detect(chip);
3993 else
3994 nand_decode_ext_id(chip);
3995}
3996
3997/*
3998 * Manufacturer initialization. This function is called for all NANDs including
3999 * ONFI and JEDEC compliant ones.
4000 * Manufacturer drivers should put all their specific initialization code in
4001 * their ->init() hook.
4002 */
4003static int nand_manufacturer_init(struct nand_chip *chip)
4004{
4005 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4006 !chip->manufacturer.desc->ops->init)
4007 return 0;
4008
4009 return chip->manufacturer.desc->ops->init(chip);
4010}
4011
4012/*
4013 * Manufacturer cleanup. This function is called for all NANDs including
4014 * ONFI and JEDEC compliant ones.
4015 * Manufacturer drivers should put all their specific cleanup code in their
4016 * ->cleanup() hook.
4017 */
4018static void nand_manufacturer_cleanup(struct nand_chip *chip)
4019{
4020 /* Release manufacturer private data */
4021 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4022 chip->manufacturer.desc->ops->cleanup)
4023 chip->manufacturer.desc->ops->cleanup(chip);
4024}
4025
4026/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004027 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004028 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004029static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004030{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004031 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004032 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08004033 int busw;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004034 int i, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004035 u8 *id_data = chip->id.data;
4036 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Karl Beldanef89a882008-09-15 14:37:29 +02004038 /*
4039 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004040 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004041 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004042 nand_reset(chip, 0);
4043
4044 /* Select the device */
4045 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004048 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
4050 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004051 maf_id = chip->read_byte(mtd);
4052 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Brian Norris8b6e50c2011-05-25 14:59:01 -07004054 /*
4055 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004056 * interface concerns can cause random data which looks like a
4057 * possibly credible NAND flash to appear. If the two results do
4058 * not match, ignore the device completely.
4059 */
4060
4061 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4062
Brian Norris4aef9b72012-09-24 20:40:48 -07004063 /* Read entire ID string */
4064 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004065 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004066
Boris Brezillon7f501f02016-05-24 19:20:05 +02004067 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004068 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004069 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004070 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004071 }
4072
Boris Brezillon7f501f02016-05-24 19:20:05 +02004073 chip->id.len = nand_id_len(id_data, 8);
4074
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004075 /* Try to identify manufacturer */
4076 manufacturer = nand_get_manufacturer(maf_id);
4077 chip->manufacturer.desc = manufacturer;
4078
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004079 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004080 type = nand_flash_ids;
4081
Boris Brezillon29a198a2016-05-24 20:17:48 +02004082 /*
4083 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4084 * override it.
4085 * This is required to make sure initial NAND bus width set by the
4086 * NAND controller driver is coherent with the real NAND bus width
4087 * (extracted by auto-detection code).
4088 */
4089 busw = chip->options & NAND_BUSWIDTH_16;
4090
4091 /*
4092 * The flag is only set (never cleared), reset it to its default value
4093 * before starting auto-detection.
4094 */
4095 chip->options &= ~NAND_BUSWIDTH_16;
4096
Huang Shijieec6e87e2013-03-15 11:01:00 +08004097 for (; type->name != NULL; type++) {
4098 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004099 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004100 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004101 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004102 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004103 }
4104 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004105
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004106 chip->onfi_version = 0;
4107 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004108 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004109 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004110 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004111
4112 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004113 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004114 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004115 }
4116
David Woodhouse5e81e882010-02-26 18:32:56 +00004117 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004118 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004119
Thomas Gleixnerba0251fe2006-05-27 01:02:13 +02004120 if (!mtd->name)
4121 mtd->name = type->name;
4122
Adrian Hunter69423d92008-12-10 13:37:21 +00004123 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004124
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004125 if (!type->pagesize)
4126 nand_manufacturer_detect(chip);
4127 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004128 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004129
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004130 /* Get chip options */
4131 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004132
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004133ident_done:
4134
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004135 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004136 WARN_ON(busw & NAND_BUSWIDTH_16);
4137 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004138 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4139 /*
4140 * Check, if buswidth is correct. Hardware drivers should set
4141 * chip correct!
4142 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004143 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004144 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004145 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4146 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004147 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4148 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004149 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004150 }
4151
Boris Brezillon7f501f02016-05-24 19:20:05 +02004152 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004153
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004154 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004155 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004156 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004157 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004158
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004159 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004160 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004161 if (chip->chipsize & 0xffffffff)
4162 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004163 else {
4164 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4165 chip->chip_shift += 32 - 1;
4166 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004167
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004168 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004169 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004170
Brian Norris8b6e50c2011-05-25 14:59:01 -07004171 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004172 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4173 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004174
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004175 ret = nand_manufacturer_init(chip);
4176 if (ret)
4177 return ret;
4178
Ezequiel Garcia20171642013-11-25 08:30:31 -03004179 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004180 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004181
4182 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004183 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4184 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004185 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004186 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4187 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004188 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004189 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4190 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004191
Rafał Miłecki3755a992014-10-21 00:01:04 +02004192 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004193 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004194 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004195 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004196}
4197
Boris Brezillond48f62b2016-04-01 14:54:32 +02004198static const char * const nand_ecc_modes[] = {
4199 [NAND_ECC_NONE] = "none",
4200 [NAND_ECC_SOFT] = "soft",
4201 [NAND_ECC_HW] = "hw",
4202 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4203 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004204 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004205};
4206
4207static int of_get_nand_ecc_mode(struct device_node *np)
4208{
4209 const char *pm;
4210 int err, i;
4211
4212 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4213 if (err < 0)
4214 return err;
4215
4216 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4217 if (!strcasecmp(pm, nand_ecc_modes[i]))
4218 return i;
4219
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004220 /*
4221 * For backward compatibility we support few obsoleted values that don't
4222 * have their mappings into nand_ecc_modes_t anymore (they were merged
4223 * with other enums).
4224 */
4225 if (!strcasecmp(pm, "soft_bch"))
4226 return NAND_ECC_SOFT;
4227
Boris Brezillond48f62b2016-04-01 14:54:32 +02004228 return -ENODEV;
4229}
4230
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004231static const char * const nand_ecc_algos[] = {
4232 [NAND_ECC_HAMMING] = "hamming",
4233 [NAND_ECC_BCH] = "bch",
4234};
4235
Boris Brezillond48f62b2016-04-01 14:54:32 +02004236static int of_get_nand_ecc_algo(struct device_node *np)
4237{
4238 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004239 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004240
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004241 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4242 if (!err) {
4243 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4244 if (!strcasecmp(pm, nand_ecc_algos[i]))
4245 return i;
4246 return -ENODEV;
4247 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004248
4249 /*
4250 * For backward compatibility we also read "nand-ecc-mode" checking
4251 * for some obsoleted values that were specifying ECC algorithm.
4252 */
4253 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4254 if (err < 0)
4255 return err;
4256
4257 if (!strcasecmp(pm, "soft"))
4258 return NAND_ECC_HAMMING;
4259 else if (!strcasecmp(pm, "soft_bch"))
4260 return NAND_ECC_BCH;
4261
4262 return -ENODEV;
4263}
4264
4265static int of_get_nand_ecc_step_size(struct device_node *np)
4266{
4267 int ret;
4268 u32 val;
4269
4270 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4271 return ret ? ret : val;
4272}
4273
4274static int of_get_nand_ecc_strength(struct device_node *np)
4275{
4276 int ret;
4277 u32 val;
4278
4279 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4280 return ret ? ret : val;
4281}
4282
4283static int of_get_nand_bus_width(struct device_node *np)
4284{
4285 u32 val;
4286
4287 if (of_property_read_u32(np, "nand-bus-width", &val))
4288 return 8;
4289
4290 switch (val) {
4291 case 8:
4292 case 16:
4293 return val;
4294 default:
4295 return -EIO;
4296 }
4297}
4298
4299static bool of_get_nand_on_flash_bbt(struct device_node *np)
4300{
4301 return of_property_read_bool(np, "nand-on-flash-bbt");
4302}
4303
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004304static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004305{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004306 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004307 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004308
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004309 if (!dn)
4310 return 0;
4311
Brian Norris5844fee2015-01-23 00:22:27 -08004312 if (of_get_nand_bus_width(dn) == 16)
4313 chip->options |= NAND_BUSWIDTH_16;
4314
4315 if (of_get_nand_on_flash_bbt(dn))
4316 chip->bbt_options |= NAND_BBT_USE_FLASH;
4317
4318 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004319 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004320 ecc_strength = of_get_nand_ecc_strength(dn);
4321 ecc_step = of_get_nand_ecc_step_size(dn);
4322
Brian Norris5844fee2015-01-23 00:22:27 -08004323 if (ecc_mode >= 0)
4324 chip->ecc.mode = ecc_mode;
4325
Rafał Miłecki79082452016-03-23 11:19:02 +01004326 if (ecc_algo >= 0)
4327 chip->ecc.algo = ecc_algo;
4328
Brian Norris5844fee2015-01-23 00:22:27 -08004329 if (ecc_strength >= 0)
4330 chip->ecc.strength = ecc_strength;
4331
4332 if (ecc_step > 0)
4333 chip->ecc.size = ecc_step;
4334
Boris Brezillonba78ee02016-06-08 17:04:22 +02004335 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4336 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4337
Brian Norris5844fee2015-01-23 00:22:27 -08004338 return 0;
4339}
4340
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004341/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004342 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004343 * @mtd: MTD device structure
4344 * @maxchips: number of chips to scan for
4345 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004346 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004347 * This is the first phase of the normal nand_scan() function. It reads the
4348 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004349 *
4350 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004351int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4352 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004353{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004354 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004355 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004356 int ret;
4357
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004358 ret = nand_dt_init(chip);
4359 if (ret)
4360 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004361
Brian Norrisf7a8e382016-01-05 10:39:45 -08004362 if (!mtd->name && mtd->dev.parent)
4363 mtd->name = dev_name(mtd->dev.parent);
4364
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004365 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4366 /*
4367 * Default functions assigned for chip_select() and
4368 * cmdfunc() both expect cmd_ctrl() to be populated,
4369 * so we need to check that that's the case
4370 */
4371 pr_err("chip.cmd_ctrl() callback is not provided");
4372 return -EINVAL;
4373 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004374 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004375 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004376
4377 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004378 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004379 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004380 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004381 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004382 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 }
4385
Boris Brezillon73f907f2016-10-24 16:46:20 +02004386 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004387 ret = nand_init_data_interface(chip);
4388 if (ret)
4389 return ret;
4390
Boris Brezillon73f907f2016-10-24 16:46:20 +02004391 /*
4392 * Setup the data interface correctly on the chip and controller side.
4393 * This explicit call to nand_setup_data_interface() is only required
4394 * for the first die, because nand_reset() has been called before
4395 * ->data_interface and ->default_onfi_timing_mode were set.
4396 * For the other dies, nand_reset() will automatically switch to the
4397 * best mode for us.
4398 */
4399 ret = nand_setup_data_interface(chip);
4400 if (ret)
4401 return ret;
4402
Boris Brezillon7f501f02016-05-24 19:20:05 +02004403 nand_maf_id = chip->id.data[0];
4404 nand_dev_id = chip->id.data[1];
4405
Huang Shijie07300162012-11-09 16:23:45 +08004406 chip->select_chip(mtd, -1);
4407
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004408 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004409 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004410 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004411 nand_reset(chip, i);
4412
4413 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004415 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004417 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004418 nand_dev_id != chip->read_byte(mtd)) {
4419 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 break;
Huang Shijie07300162012-11-09 16:23:45 +08004421 }
4422 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
4424 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004425 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004428 chip->numchips = i;
4429 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
David Woodhouse3b85c322006-09-25 17:06:53 +01004431 return 0;
4432}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004433EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004434
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004435static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4436{
4437 struct nand_chip *chip = mtd_to_nand(mtd);
4438 struct nand_ecc_ctrl *ecc = &chip->ecc;
4439
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004440 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004441 return -EINVAL;
4442
4443 switch (ecc->algo) {
4444 case NAND_ECC_HAMMING:
4445 ecc->calculate = nand_calculate_ecc;
4446 ecc->correct = nand_correct_data;
4447 ecc->read_page = nand_read_page_swecc;
4448 ecc->read_subpage = nand_read_subpage;
4449 ecc->write_page = nand_write_page_swecc;
4450 ecc->read_page_raw = nand_read_page_raw;
4451 ecc->write_page_raw = nand_write_page_raw;
4452 ecc->read_oob = nand_read_oob_std;
4453 ecc->write_oob = nand_write_oob_std;
4454 if (!ecc->size)
4455 ecc->size = 256;
4456 ecc->bytes = 3;
4457 ecc->strength = 1;
4458 return 0;
4459 case NAND_ECC_BCH:
4460 if (!mtd_nand_has_bch()) {
4461 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4462 return -EINVAL;
4463 }
4464 ecc->calculate = nand_bch_calculate_ecc;
4465 ecc->correct = nand_bch_correct_data;
4466 ecc->read_page = nand_read_page_swecc;
4467 ecc->read_subpage = nand_read_subpage;
4468 ecc->write_page = nand_write_page_swecc;
4469 ecc->read_page_raw = nand_read_page_raw;
4470 ecc->write_page_raw = nand_write_page_raw;
4471 ecc->read_oob = nand_read_oob_std;
4472 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004473
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004474 /*
4475 * Board driver should supply ecc.size and ecc.strength
4476 * values to select how many bits are correctable.
4477 * Otherwise, default to 4 bits for large page devices.
4478 */
4479 if (!ecc->size && (mtd->oobsize >= 64)) {
4480 ecc->size = 512;
4481 ecc->strength = 4;
4482 }
4483
4484 /*
4485 * if no ecc placement scheme was provided pickup the default
4486 * large page one.
4487 */
4488 if (!mtd->ooblayout) {
4489 /* handle large page devices only */
4490 if (mtd->oobsize < 64) {
4491 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4492 return -EINVAL;
4493 }
4494
4495 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004496
4497 }
4498
4499 /*
4500 * We can only maximize ECC config when the default layout is
4501 * used, otherwise we don't know how many bytes can really be
4502 * used.
4503 */
4504 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4505 ecc->options & NAND_ECC_MAXIMIZE) {
4506 int steps, bytes;
4507
4508 /* Always prefer 1k blocks over 512bytes ones */
4509 ecc->size = 1024;
4510 steps = mtd->writesize / ecc->size;
4511
4512 /* Reserve 2 bytes for the BBM */
4513 bytes = (mtd->oobsize - 2) / steps;
4514 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004515 }
4516
4517 /* See nand_bch_init() for details. */
4518 ecc->bytes = 0;
4519 ecc->priv = nand_bch_init(mtd);
4520 if (!ecc->priv) {
4521 WARN(1, "BCH ECC initialization failed!\n");
4522 return -EINVAL;
4523 }
4524 return 0;
4525 default:
4526 WARN(1, "Unsupported ECC algorithm!\n");
4527 return -EINVAL;
4528 }
4529}
4530
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004531/*
4532 * Check if the chip configuration meet the datasheet requirements.
4533
4534 * If our configuration corrects A bits per B bytes and the minimum
4535 * required correction level is X bits per Y bytes, then we must ensure
4536 * both of the following are true:
4537 *
4538 * (1) A / B >= X / Y
4539 * (2) A >= X
4540 *
4541 * Requirement (1) ensures we can correct for the required bitflip density.
4542 * Requirement (2) ensures we can correct even when all bitflips are clumped
4543 * in the same sector.
4544 */
4545static bool nand_ecc_strength_good(struct mtd_info *mtd)
4546{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004547 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004548 struct nand_ecc_ctrl *ecc = &chip->ecc;
4549 int corr, ds_corr;
4550
4551 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4552 /* Not enough information */
4553 return true;
4554
4555 /*
4556 * We get the number of corrected bits per page to compare
4557 * the correction density.
4558 */
4559 corr = (mtd->writesize * ecc->strength) / ecc->size;
4560 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4561
4562 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4563}
David Woodhouse3b85c322006-09-25 17:06:53 +01004564
Marc Gonzalez3371d662016-11-15 10:56:20 +01004565static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4566{
4567 struct nand_ecc_ctrl *ecc = &chip->ecc;
4568
4569 if (nand_standard_page_accessors(ecc))
4570 return false;
4571
4572 /*
4573 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4574 * controller driver implements all the page accessors because
4575 * default helpers are not suitable when the core does not
4576 * send the READ0/PAGEPROG commands.
4577 */
4578 return (!ecc->read_page || !ecc->write_page ||
4579 !ecc->read_page_raw || !ecc->write_page_raw ||
4580 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4581 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4582 ecc->hwctl && ecc->calculate));
4583}
4584
David Woodhouse3b85c322006-09-25 17:06:53 +01004585/**
4586 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004587 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004588 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004589 * This is the second phase of the normal nand_scan() function. It fills out
4590 * all the uninitialized function pointers with the defaults and scans for a
4591 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004592 */
4593int nand_scan_tail(struct mtd_info *mtd)
4594{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004595 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004596 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004597 struct nand_buffers *nbuf = NULL;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004598 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004599
Brian Norrise2414f42012-02-06 13:44:00 -08004600 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004601 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4602 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4603 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004604
Marc Gonzalez3371d662016-11-15 10:56:20 +01004605 if (invalid_ecc_page_accessors(chip)) {
4606 pr_err("Invalid ECC page accessors setup\n");
4607 return -EINVAL;
4608 }
4609
Huang Shijief02ea4e2014-01-13 14:27:12 +08004610 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004611 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Huang Shijief02ea4e2014-01-13 14:27:12 +08004612 if (!nbuf)
4613 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004614
4615 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4616 if (!nbuf->ecccalc) {
4617 ret = -ENOMEM;
4618 goto err_free;
4619 }
4620
4621 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4622 if (!nbuf->ecccode) {
4623 ret = -ENOMEM;
4624 goto err_free;
4625 }
4626
4627 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4628 GFP_KERNEL);
4629 if (!nbuf->databuf) {
4630 ret = -ENOMEM;
4631 goto err_free;
4632 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004633
4634 chip->buffers = nbuf;
4635 } else {
4636 if (!chip->buffers)
4637 return -ENOMEM;
4638 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004639
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004640 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004641 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004642
4643 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004644 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004645 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004646 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004647 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004648 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004651 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 break;
4653 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004654 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004655 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004658 WARN(1, "No oob scheme defined for oobsize %d\n",
4659 mtd->oobsize);
4660 ret = -EINVAL;
4661 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
4663 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004664
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004665 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004666 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004667 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004668 */
David Woodhouse956e9442006-09-25 17:12:39 +01004669
Huang Shijie97de79e02013-10-18 14:20:53 +08004670 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004671 case NAND_ECC_HW_OOB_FIRST:
4672 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004673 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004674 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4675 ret = -EINVAL;
4676 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004677 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004678 if (!ecc->read_page)
4679 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004680
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004681 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004682 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004683 if (!ecc->read_page)
4684 ecc->read_page = nand_read_page_hwecc;
4685 if (!ecc->write_page)
4686 ecc->write_page = nand_write_page_hwecc;
4687 if (!ecc->read_page_raw)
4688 ecc->read_page_raw = nand_read_page_raw;
4689 if (!ecc->write_page_raw)
4690 ecc->write_page_raw = nand_write_page_raw;
4691 if (!ecc->read_oob)
4692 ecc->read_oob = nand_read_oob_std;
4693 if (!ecc->write_oob)
4694 ecc->write_oob = nand_write_oob_std;
4695 if (!ecc->read_subpage)
4696 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004697 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004698 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004699
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004700 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004701 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4702 (!ecc->read_page ||
4703 ecc->read_page == nand_read_page_hwecc ||
4704 !ecc->write_page ||
4705 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004706 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4707 ret = -EINVAL;
4708 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004709 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004710 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004711 if (!ecc->read_page)
4712 ecc->read_page = nand_read_page_syndrome;
4713 if (!ecc->write_page)
4714 ecc->write_page = nand_write_page_syndrome;
4715 if (!ecc->read_page_raw)
4716 ecc->read_page_raw = nand_read_page_raw_syndrome;
4717 if (!ecc->write_page_raw)
4718 ecc->write_page_raw = nand_write_page_raw_syndrome;
4719 if (!ecc->read_oob)
4720 ecc->read_oob = nand_read_oob_syndrome;
4721 if (!ecc->write_oob)
4722 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004723
Huang Shijie97de79e02013-10-18 14:20:53 +08004724 if (mtd->writesize >= ecc->size) {
4725 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004726 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4727 ret = -EINVAL;
4728 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004729 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004730 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004731 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004732 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4733 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004734 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004735 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004737 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004738 ret = nand_set_ecc_soft_ops(mtd);
4739 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004740 ret = -EINVAL;
4741 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004742 }
4743 break;
4744
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004745 case NAND_ECC_ON_DIE:
4746 if (!ecc->read_page || !ecc->write_page) {
4747 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4748 ret = -EINVAL;
4749 goto err_free;
4750 }
4751 if (!ecc->read_oob)
4752 ecc->read_oob = nand_read_oob_std;
4753 if (!ecc->write_oob)
4754 ecc->write_oob = nand_write_oob_std;
4755 break;
4756
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004757 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004758 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004759 ecc->read_page = nand_read_page_raw;
4760 ecc->write_page = nand_write_page_raw;
4761 ecc->read_oob = nand_read_oob_std;
4762 ecc->read_page_raw = nand_read_page_raw;
4763 ecc->write_page_raw = nand_write_page_raw;
4764 ecc->write_oob = nand_write_oob_std;
4765 ecc->size = mtd->writesize;
4766 ecc->bytes = 0;
4767 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004771 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4772 ret = -EINVAL;
4773 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Brian Norris9ce244b2011-08-30 18:45:37 -07004776 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004777 if (!ecc->read_oob_raw)
4778 ecc->read_oob_raw = ecc->read_oob;
4779 if (!ecc->write_oob_raw)
4780 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004781
Boris Brezillon846031d2016-02-03 20:11:00 +01004782 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004783 mtd->ecc_strength = ecc->strength;
4784 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004785
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004786 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004787 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004788 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004789 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004790 ecc->steps = mtd->writesize / ecc->size;
4791 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004792 WARN(1, "Invalid ECC parameters\n");
4793 ret = -EINVAL;
4794 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004796 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004797
Boris Brezillon846031d2016-02-03 20:11:00 +01004798 /*
4799 * The number of bytes available for a client to place data into
4800 * the out of band area.
4801 */
4802 ret = mtd_ooblayout_count_freebytes(mtd);
4803 if (ret < 0)
4804 ret = 0;
4805
4806 mtd->oobavail = ret;
4807
4808 /* ECC sanity check: warn if it's too weak */
4809 if (!nand_ecc_strength_good(mtd))
4810 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4811 mtd->name);
4812
Brian Norris8b6e50c2011-05-25 14:59:01 -07004813 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004814 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004815 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004816 case 2:
4817 mtd->subpage_sft = 1;
4818 break;
4819 case 4:
4820 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004821 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004822 mtd->subpage_sft = 2;
4823 break;
4824 }
4825 }
4826 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4827
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004828 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004829 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004832 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004834 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304835 switch (ecc->mode) {
4836 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304837 if (chip->page_shift > 9)
4838 chip->options |= NAND_SUBPAGE_READ;
4839 break;
4840
4841 default:
4842 break;
4843 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004846 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004847 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4848 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004849 mtd->_erase = nand_erase;
4850 mtd->_point = NULL;
4851 mtd->_unpoint = NULL;
4852 mtd->_read = nand_read;
4853 mtd->_write = nand_write;
4854 mtd->_panic_write = panic_nand_write;
4855 mtd->_read_oob = nand_read_oob;
4856 mtd->_write_oob = nand_write_oob;
4857 mtd->_sync = nand_sync;
4858 mtd->_lock = NULL;
4859 mtd->_unlock = NULL;
4860 mtd->_suspend = nand_suspend;
4861 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004862 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004863 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004864 mtd->_block_isbad = nand_block_isbad;
4865 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004866 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004867 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004869 /*
4870 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4871 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4872 * properly set.
4873 */
4874 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004875 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004877 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004878 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004882 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004883err_free:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004884 if (nbuf) {
4885 kfree(nbuf->databuf);
4886 kfree(nbuf->ecccode);
4887 kfree(nbuf->ecccalc);
4888 kfree(nbuf);
4889 }
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004892EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Brian Norris8b6e50c2011-05-25 14:59:01 -07004894/*
4895 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004896 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004897 * to call us from in-kernel code if the core NAND support is modular.
4898 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004899#ifdef MODULE
4900#define caller_is_module() (1)
4901#else
4902#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004903 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004904#endif
4905
4906/**
4907 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004908 * @mtd: MTD device structure
4909 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004910 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004911 * This fills out all the uninitialized function pointers with the defaults.
4912 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004913 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004914 */
4915int nand_scan(struct mtd_info *mtd, int maxchips)
4916{
4917 int ret;
4918
David Woodhouse5e81e882010-02-26 18:32:56 +00004919 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004920 if (!ret)
4921 ret = nand_scan_tail(mtd);
4922 return ret;
4923}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004924EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004927 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4928 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004929 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004930void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004932 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004933 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004934 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4935
Boris Brezillond8e725d2016-09-15 10:32:50 +02004936 nand_release_data_interface(chip);
4937
Jesper Juhlfa671642005-11-07 01:01:27 -08004938 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004939 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004940 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
4941 kfree(chip->buffers->databuf);
4942 kfree(chip->buffers->ecccode);
4943 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004944 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004945 }
Brian Norris58373ff2010-07-15 12:15:44 -07004946
4947 /* Free bad block descriptor memory */
4948 if (chip->badblock_pattern && chip->badblock_pattern->options
4949 & NAND_BBT_DYNAMICSTRUCT)
4950 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004951
4952 /* Free manufacturer priv data. */
4953 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004955EXPORT_SYMBOL_GPL(nand_cleanup);
4956
4957/**
4958 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4959 * held by the NAND device
4960 * @mtd: MTD device structure
4961 */
4962void nand_release(struct mtd_info *mtd)
4963{
4964 mtd_device_unregister(mtd);
4965 nand_cleanup(mtd_to_nand(mtd));
4966}
David Woodhousee0c7d762006-05-13 18:07:53 +01004967EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004968
David Woodhousee0c7d762006-05-13 18:07:53 +01004969MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004970MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4971MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004972MODULE_DESCRIPTION("Generic NAND flash driver code");