blob: 3317acf0ce04d5575cbf755e01ffcf0c97d56052 [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
Boris Brezillon104e4422017-03-16 09:35:58 +01001045 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001046 *
1047 * Reset the Data interface and timings to ONFI mode 0.
1048 *
1049 * Returns 0 for success or negative error code otherwise.
1050 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001051static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001052{
1053 struct mtd_info *mtd = nand_to_mtd(chip);
1054 const struct nand_data_interface *conf;
1055 int ret;
1056
1057 if (!chip->setup_data_interface)
1058 return 0;
1059
1060 /*
1061 * The ONFI specification says:
1062 * "
1063 * To transition from NV-DDR or NV-DDR2 to the SDR data
1064 * interface, the host shall use the Reset (FFh) command
1065 * using SDR timing mode 0. A device in any timing mode is
1066 * required to recognize Reset (FFh) command issued in SDR
1067 * timing mode 0.
1068 * "
1069 *
1070 * Configure the data interface in SDR mode and set the
1071 * timings to timing mode 0.
1072 */
1073
1074 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001075 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001076 if (ret)
1077 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1078
1079 return ret;
1080}
1081
1082/**
1083 * nand_setup_data_interface - Setup the best data interface and timings
1084 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001085 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001086 *
1087 * Find and configure the best data interface and NAND timings supported by
1088 * the chip and the driver.
1089 * First tries to retrieve supported timing modes from ONFI information,
1090 * and if the NAND chip does not support ONFI, relies on the
1091 * ->onfi_timing_mode_default specified in the nand_ids table.
1092 *
1093 * Returns 0 for success or negative error code otherwise.
1094 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001095static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001096{
1097 struct mtd_info *mtd = nand_to_mtd(chip);
1098 int ret;
1099
1100 if (!chip->setup_data_interface || !chip->data_interface)
1101 return 0;
1102
1103 /*
1104 * Ensure the timing mode has been changed on the chip side
1105 * before changing timings on the controller side.
1106 */
1107 if (chip->onfi_version) {
1108 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1109 chip->onfi_timing_mode_default,
1110 };
1111
1112 ret = chip->onfi_set_features(mtd, chip,
1113 ONFI_FEATURE_ADDR_TIMING_MODE,
1114 tmode_param);
1115 if (ret)
1116 goto err;
1117 }
1118
Boris Brezillon104e4422017-03-16 09:35:58 +01001119 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001120err:
1121 return ret;
1122}
1123
1124/**
1125 * nand_init_data_interface - find the best data interface and timings
1126 * @chip: The NAND chip
1127 *
1128 * Find the best data interface and NAND timings supported by the chip
1129 * and the driver.
1130 * First tries to retrieve supported timing modes from ONFI information,
1131 * and if the NAND chip does not support ONFI, relies on the
1132 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1133 * function nand_chip->data_interface is initialized with the best timing mode
1134 * available.
1135 *
1136 * Returns 0 for success or negative error code otherwise.
1137 */
1138static int nand_init_data_interface(struct nand_chip *chip)
1139{
1140 struct mtd_info *mtd = nand_to_mtd(chip);
1141 int modes, mode, ret;
1142
1143 if (!chip->setup_data_interface)
1144 return 0;
1145
1146 /*
1147 * First try to identify the best timings from ONFI parameters and
1148 * if the NAND does not support ONFI, fallback to the default ONFI
1149 * timing mode.
1150 */
1151 modes = onfi_get_async_timing_mode(chip);
1152 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1153 if (!chip->onfi_timing_mode_default)
1154 return 0;
1155
1156 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1157 }
1158
1159 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1160 GFP_KERNEL);
1161 if (!chip->data_interface)
1162 return -ENOMEM;
1163
1164 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1165 ret = onfi_init_data_interface(chip, chip->data_interface,
1166 NAND_SDR_IFACE, mode);
1167 if (ret)
1168 continue;
1169
Boris Brezillon104e4422017-03-16 09:35:58 +01001170 /* Pass -1 to only */
1171 ret = chip->setup_data_interface(mtd,
1172 NAND_DATA_IFACE_CHECK_ONLY,
1173 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001174 if (!ret) {
1175 chip->onfi_timing_mode_default = mode;
1176 break;
1177 }
1178 }
1179
1180 return 0;
1181}
1182
1183static void nand_release_data_interface(struct nand_chip *chip)
1184{
1185 kfree(chip->data_interface);
1186}
1187
1188/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001189 * nand_reset - Reset and initialize a NAND device
1190 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001191 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001192 *
1193 * Returns 0 for success or negative error code otherwise
1194 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001195int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001196{
1197 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001198 int ret;
1199
Boris Brezillon104e4422017-03-16 09:35:58 +01001200 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001201 if (ret)
1202 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001203
Boris Brezillon73f907f2016-10-24 16:46:20 +02001204 /*
1205 * The CS line has to be released before we can apply the new NAND
1206 * interface settings, hence this weird ->select_chip() dance.
1207 */
1208 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001209 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001210 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001211
Boris Brezillon73f907f2016-10-24 16:46:20 +02001212 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001213 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001214 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001215 if (ret)
1216 return ret;
1217
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001218 return 0;
1219}
1220
1221/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001222 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001223 * @mtd: mtd info
1224 * @ofs: offset to start unlock from
1225 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001226 * @invert: when = 0, unlock the range of blocks within the lower and
1227 * upper boundary address
1228 * when = 1, unlock the range of blocks outside the boundaries
1229 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301230 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001231 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301232 */
1233static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1234 uint64_t len, int invert)
1235{
1236 int ret = 0;
1237 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001238 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301239
1240 /* Submit address of first page to unlock */
1241 page = ofs >> chip->page_shift;
1242 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1243
1244 /* Submit address of last page to unlock */
1245 page = (ofs + len) >> chip->page_shift;
1246 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1247 (page | invert) & chip->pagemask);
1248
1249 /* Call wait ready function */
1250 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301251 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001252 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001253 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301254 __func__, status);
1255 ret = -EIO;
1256 }
1257
1258 return ret;
1259}
1260
1261/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001262 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001263 * @mtd: mtd info
1264 * @ofs: offset to start unlock from
1265 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301266 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001267 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301268 */
1269int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1270{
1271 int ret = 0;
1272 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001273 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301274
Brian Norris289c0522011-07-19 10:06:09 -07001275 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301276 __func__, (unsigned long long)ofs, len);
1277
1278 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001279 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301280
1281 /* Align to last block address if size addresses end of the device */
1282 if (ofs + len == mtd->size)
1283 len -= mtd->erasesize;
1284
Huang Shijie6a8214a2012-11-19 14:43:30 +08001285 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301286
1287 /* Shift to get chip number */
1288 chipnr = ofs >> chip->chip_shift;
1289
White Ding57d3a9a2014-07-24 00:10:45 +08001290 /*
1291 * Reset the chip.
1292 * If we want to check the WP through READ STATUS and check the bit 7
1293 * we must reset the chip
1294 * some operation can also clear the bit 7 of status register
1295 * eg. erase/program a locked block
1296 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001297 nand_reset(chip, chipnr);
1298
1299 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001300
Vimal Singh7d70f332010-02-08 15:50:49 +05301301 /* Check, if it is write protected */
1302 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001303 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301304 __func__);
1305 ret = -EIO;
1306 goto out;
1307 }
1308
1309 ret = __nand_unlock(mtd, ofs, len, 0);
1310
1311out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001312 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301313 nand_release_device(mtd);
1314
1315 return ret;
1316}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001317EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301318
1319/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001320 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001321 * @mtd: mtd info
1322 * @ofs: offset to start unlock from
1323 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301324 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001325 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1326 * have this feature, but it allows only to lock all blocks, not for specified
1327 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1328 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301329 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001330 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301331 */
1332int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1333{
1334 int ret = 0;
1335 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001336 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301337
Brian Norris289c0522011-07-19 10:06:09 -07001338 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301339 __func__, (unsigned long long)ofs, len);
1340
1341 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001342 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301343
Huang Shijie6a8214a2012-11-19 14:43:30 +08001344 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301345
1346 /* Shift to get chip number */
1347 chipnr = ofs >> chip->chip_shift;
1348
White Ding57d3a9a2014-07-24 00:10:45 +08001349 /*
1350 * Reset the chip.
1351 * If we want to check the WP through READ STATUS and check the bit 7
1352 * we must reset the chip
1353 * some operation can also clear the bit 7 of status register
1354 * eg. erase/program a locked block
1355 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001356 nand_reset(chip, chipnr);
1357
1358 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001359
Vimal Singh7d70f332010-02-08 15:50:49 +05301360 /* Check, if it is write protected */
1361 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001362 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301363 __func__);
1364 status = MTD_ERASE_FAILED;
1365 ret = -EIO;
1366 goto out;
1367 }
1368
1369 /* Submit address of first page to lock */
1370 page = ofs >> chip->page_shift;
1371 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1372
1373 /* Call wait ready function */
1374 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301375 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001376 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001377 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301378 __func__, status);
1379 ret = -EIO;
1380 goto out;
1381 }
1382
1383 ret = __nand_unlock(mtd, ofs, len, 0x1);
1384
1385out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001386 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301387 nand_release_device(mtd);
1388
1389 return ret;
1390}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001391EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301392
1393/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001394 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1395 * @buf: buffer to test
1396 * @len: buffer length
1397 * @bitflips_threshold: maximum number of bitflips
1398 *
1399 * Check if a buffer contains only 0xff, which means the underlying region
1400 * has been erased and is ready to be programmed.
1401 * The bitflips_threshold specify the maximum number of bitflips before
1402 * considering the region is not erased.
1403 * Note: The logic of this function has been extracted from the memweight
1404 * implementation, except that nand_check_erased_buf function exit before
1405 * testing the whole buffer if the number of bitflips exceed the
1406 * bitflips_threshold value.
1407 *
1408 * Returns a positive number of bitflips less than or equal to
1409 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1410 * threshold.
1411 */
1412static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1413{
1414 const unsigned char *bitmap = buf;
1415 int bitflips = 0;
1416 int weight;
1417
1418 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1419 len--, bitmap++) {
1420 weight = hweight8(*bitmap);
1421 bitflips += BITS_PER_BYTE - weight;
1422 if (unlikely(bitflips > bitflips_threshold))
1423 return -EBADMSG;
1424 }
1425
1426 for (; len >= sizeof(long);
1427 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001428 unsigned long d = *((unsigned long *)bitmap);
1429 if (d == ~0UL)
1430 continue;
1431 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001432 bitflips += BITS_PER_LONG - weight;
1433 if (unlikely(bitflips > bitflips_threshold))
1434 return -EBADMSG;
1435 }
1436
1437 for (; len > 0; len--, bitmap++) {
1438 weight = hweight8(*bitmap);
1439 bitflips += BITS_PER_BYTE - weight;
1440 if (unlikely(bitflips > bitflips_threshold))
1441 return -EBADMSG;
1442 }
1443
1444 return bitflips;
1445}
1446
1447/**
1448 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1449 * 0xff data
1450 * @data: data buffer to test
1451 * @datalen: data length
1452 * @ecc: ECC buffer
1453 * @ecclen: ECC length
1454 * @extraoob: extra OOB buffer
1455 * @extraooblen: extra OOB length
1456 * @bitflips_threshold: maximum number of bitflips
1457 *
1458 * Check if a data buffer and its associated ECC and OOB data contains only
1459 * 0xff pattern, which means the underlying region has been erased and is
1460 * ready to be programmed.
1461 * The bitflips_threshold specify the maximum number of bitflips before
1462 * considering the region as not erased.
1463 *
1464 * Note:
1465 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1466 * different from the NAND page size. When fixing bitflips, ECC engines will
1467 * report the number of errors per chunk, and the NAND core infrastructure
1468 * expect you to return the maximum number of bitflips for the whole page.
1469 * This is why you should always use this function on a single chunk and
1470 * not on the whole page. After checking each chunk you should update your
1471 * max_bitflips value accordingly.
1472 * 2/ When checking for bitflips in erased pages you should not only check
1473 * the payload data but also their associated ECC data, because a user might
1474 * have programmed almost all bits to 1 but a few. In this case, we
1475 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1476 * this case.
1477 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1478 * data are protected by the ECC engine.
1479 * It could also be used if you support subpages and want to attach some
1480 * extra OOB data to an ECC chunk.
1481 *
1482 * Returns a positive number of bitflips less than or equal to
1483 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1484 * threshold. In case of success, the passed buffers are filled with 0xff.
1485 */
1486int nand_check_erased_ecc_chunk(void *data, int datalen,
1487 void *ecc, int ecclen,
1488 void *extraoob, int extraooblen,
1489 int bitflips_threshold)
1490{
1491 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1492
1493 data_bitflips = nand_check_erased_buf(data, datalen,
1494 bitflips_threshold);
1495 if (data_bitflips < 0)
1496 return data_bitflips;
1497
1498 bitflips_threshold -= data_bitflips;
1499
1500 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1501 if (ecc_bitflips < 0)
1502 return ecc_bitflips;
1503
1504 bitflips_threshold -= ecc_bitflips;
1505
1506 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1507 bitflips_threshold);
1508 if (extraoob_bitflips < 0)
1509 return extraoob_bitflips;
1510
1511 if (data_bitflips)
1512 memset(data, 0xff, datalen);
1513
1514 if (ecc_bitflips)
1515 memset(ecc, 0xff, ecclen);
1516
1517 if (extraoob_bitflips)
1518 memset(extraoob, 0xff, extraooblen);
1519
1520 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1521}
1522EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1523
1524/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001525 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001526 * @mtd: mtd info structure
1527 * @chip: nand chip info structure
1528 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001529 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001530 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001531 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001532 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001533 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001534int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1535 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001536{
1537 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001538 if (oob_required)
1539 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001540 return 0;
1541}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001542EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001543
1544/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001545 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001546 * @mtd: mtd info structure
1547 * @chip: nand chip info structure
1548 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001549 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001550 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001551 *
1552 * We need a special oob layout and handling even when OOB isn't used.
1553 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001554static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001555 struct nand_chip *chip, uint8_t *buf,
1556 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001557{
1558 int eccsize = chip->ecc.size;
1559 int eccbytes = chip->ecc.bytes;
1560 uint8_t *oob = chip->oob_poi;
1561 int steps, size;
1562
1563 for (steps = chip->ecc.steps; steps > 0; steps--) {
1564 chip->read_buf(mtd, buf, eccsize);
1565 buf += eccsize;
1566
1567 if (chip->ecc.prepad) {
1568 chip->read_buf(mtd, oob, chip->ecc.prepad);
1569 oob += chip->ecc.prepad;
1570 }
1571
1572 chip->read_buf(mtd, oob, eccbytes);
1573 oob += eccbytes;
1574
1575 if (chip->ecc.postpad) {
1576 chip->read_buf(mtd, oob, chip->ecc.postpad);
1577 oob += chip->ecc.postpad;
1578 }
1579 }
1580
1581 size = mtd->oobsize - (oob - chip->oob_poi);
1582 if (size)
1583 chip->read_buf(mtd, oob, size);
1584
1585 return 0;
1586}
1587
1588/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001589 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001590 * @mtd: mtd info structure
1591 * @chip: nand chip info structure
1592 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001593 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001594 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001595 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001596static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001597 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Boris Brezillon846031d2016-02-03 20:11:00 +01001599 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001600 int eccbytes = chip->ecc.bytes;
1601 int eccsteps = chip->ecc.steps;
1602 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001603 uint8_t *ecc_calc = chip->buffers->ecccalc;
1604 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001605 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001606
Brian Norris1fbb9382012-05-02 10:14:55 -07001607 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001608
1609 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1610 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1611
Boris Brezillon846031d2016-02-03 20:11:00 +01001612 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1613 chip->ecc.total);
1614 if (ret)
1615 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001616
1617 eccsteps = chip->ecc.steps;
1618 p = buf;
1619
1620 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1621 int stat;
1622
1623 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001624 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001625 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001626 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001627 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001628 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1629 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001630 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001631 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301635 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001636 * @mtd: mtd info structure
1637 * @chip: nand chip info structure
1638 * @data_offs: offset of requested data within the page
1639 * @readlen: data length
1640 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001641 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001642 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001643static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001644 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1645 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001646{
Boris Brezillon846031d2016-02-03 20:11:00 +01001647 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001648 uint8_t *p;
1649 int data_col_addr, i, gaps = 0;
1650 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1651 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001652 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001653 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001654 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001655
Brian Norris7854d3f2011-06-23 14:12:08 -07001656 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001657 start_step = data_offs / chip->ecc.size;
1658 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1659 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301660 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001661
Brian Norris8b6e50c2011-05-25 14:59:01 -07001662 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001663 datafrag_len = num_steps * chip->ecc.size;
1664 eccfrag_len = num_steps * chip->ecc.bytes;
1665
1666 data_col_addr = start_step * chip->ecc.size;
1667 /* If we read not a page aligned data */
1668 if (data_col_addr != 0)
1669 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1670
1671 p = bufpoi + data_col_addr;
1672 chip->read_buf(mtd, p, datafrag_len);
1673
Brian Norris8b6e50c2011-05-25 14:59:01 -07001674 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001675 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1676 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1677
Brian Norris8b6e50c2011-05-25 14:59:01 -07001678 /*
1679 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001680 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001681 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001682 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1683 if (ret)
1684 return ret;
1685
1686 if (oobregion.length < eccfrag_len)
1687 gaps = 1;
1688
Alexey Korolev3d459552008-05-15 17:23:18 +01001689 if (gaps) {
1690 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1691 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1692 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001693 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001694 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001695 * about buswidth alignment in read_buf.
1696 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001697 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001698 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001699 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001700 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001701 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1702 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001703 aligned_len++;
1704
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001705 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001706 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001707 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1708 }
1709
Boris Brezillon846031d2016-02-03 20:11:00 +01001710 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1711 chip->oob_poi, index, eccfrag_len);
1712 if (ret)
1713 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001714
1715 p = bufpoi + data_col_addr;
1716 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1717 int stat;
1718
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001719 stat = chip->ecc.correct(mtd, p,
1720 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001721 if (stat == -EBADMSG &&
1722 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1723 /* check for empty pages with bitflips */
1724 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1725 &chip->buffers->ecccode[i],
1726 chip->ecc.bytes,
1727 NULL, 0,
1728 chip->ecc.strength);
1729 }
1730
Mike Dunn3f91e942012-04-25 12:06:09 -07001731 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001732 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001733 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001734 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001735 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1736 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001737 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001738 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001739}
1740
1741/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001742 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001743 * @mtd: mtd info structure
1744 * @chip: nand chip info structure
1745 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001746 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001747 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001748 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001749 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001750 */
1751static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001752 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001753{
Boris Brezillon846031d2016-02-03 20:11:00 +01001754 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001755 int eccbytes = chip->ecc.bytes;
1756 int eccsteps = chip->ecc.steps;
1757 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001758 uint8_t *ecc_calc = chip->buffers->ecccalc;
1759 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001760 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001761
1762 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1763 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1764 chip->read_buf(mtd, p, eccsize);
1765 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1766 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001767 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001768
Boris Brezillon846031d2016-02-03 20:11:00 +01001769 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1770 chip->ecc.total);
1771 if (ret)
1772 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001773
1774 eccsteps = chip->ecc.steps;
1775 p = buf;
1776
1777 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1778 int stat;
1779
1780 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001781 if (stat == -EBADMSG &&
1782 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1783 /* check for empty pages with bitflips */
1784 stat = nand_check_erased_ecc_chunk(p, eccsize,
1785 &ecc_code[i], eccbytes,
1786 NULL, 0,
1787 chip->ecc.strength);
1788 }
1789
Mike Dunn3f91e942012-04-25 12:06:09 -07001790 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001791 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001792 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001793 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001794 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1795 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001796 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001797 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001798}
1799
1800/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001801 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001802 * @mtd: mtd info structure
1803 * @chip: nand chip info structure
1804 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001805 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001806 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001807 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001808 * Hardware ECC for large page chips, require OOB to be read first. For this
1809 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1810 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1811 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1812 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001813 */
1814static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001815 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001816{
Boris Brezillon846031d2016-02-03 20:11:00 +01001817 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001818 int eccbytes = chip->ecc.bytes;
1819 int eccsteps = chip->ecc.steps;
1820 uint8_t *p = buf;
1821 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001822 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001823 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001824
1825 /* Read the OOB area first */
1826 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1827 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1828 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1829
Boris Brezillon846031d2016-02-03 20:11:00 +01001830 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1831 chip->ecc.total);
1832 if (ret)
1833 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001834
1835 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1836 int stat;
1837
1838 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1839 chip->read_buf(mtd, p, eccsize);
1840 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1841
1842 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001843 if (stat == -EBADMSG &&
1844 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1845 /* check for empty pages with bitflips */
1846 stat = nand_check_erased_ecc_chunk(p, eccsize,
1847 &ecc_code[i], eccbytes,
1848 NULL, 0,
1849 chip->ecc.strength);
1850 }
1851
Mike Dunn3f91e942012-04-25 12:06:09 -07001852 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001853 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001854 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001855 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001856 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1857 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001858 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001859 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001860}
1861
1862/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001863 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001864 * @mtd: mtd info structure
1865 * @chip: nand chip info structure
1866 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001867 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001868 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001869 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001870 * The hw generator calculates the error syndrome automatically. Therefore we
1871 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001872 */
1873static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001874 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001875{
1876 int i, eccsize = chip->ecc.size;
1877 int eccbytes = chip->ecc.bytes;
1878 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001879 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001880 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001881 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001882 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001883
1884 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1885 int stat;
1886
1887 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1888 chip->read_buf(mtd, p, eccsize);
1889
1890 if (chip->ecc.prepad) {
1891 chip->read_buf(mtd, oob, chip->ecc.prepad);
1892 oob += chip->ecc.prepad;
1893 }
1894
1895 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1896 chip->read_buf(mtd, oob, eccbytes);
1897 stat = chip->ecc.correct(mtd, p, oob, NULL);
1898
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001899 oob += eccbytes;
1900
1901 if (chip->ecc.postpad) {
1902 chip->read_buf(mtd, oob, chip->ecc.postpad);
1903 oob += chip->ecc.postpad;
1904 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001905
1906 if (stat == -EBADMSG &&
1907 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1908 /* check for empty pages with bitflips */
1909 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1910 oob - eccpadbytes,
1911 eccpadbytes,
1912 NULL, 0,
1913 chip->ecc.strength);
1914 }
1915
1916 if (stat < 0) {
1917 mtd->ecc_stats.failed++;
1918 } else {
1919 mtd->ecc_stats.corrected += stat;
1920 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1921 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001922 }
1923
1924 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001925 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001926 if (i)
1927 chip->read_buf(mtd, oob, i);
1928
Mike Dunn3f91e942012-04-25 12:06:09 -07001929 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001930}
1931
1932/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001933 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001934 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001935 * @oob: oob destination address
1936 * @ops: oob ops structure
1937 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001938 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001939static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001940 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941{
Boris Brezillon846031d2016-02-03 20:11:00 +01001942 struct nand_chip *chip = mtd_to_nand(mtd);
1943 int ret;
1944
Florian Fainellif8ac0412010-09-07 13:23:43 +02001945 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946
Brian Norris0612b9d2011-08-30 18:45:40 -07001947 case MTD_OPS_PLACE_OOB:
1948 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001949 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1950 return oob + len;
1951
Boris Brezillon846031d2016-02-03 20:11:00 +01001952 case MTD_OPS_AUTO_OOB:
1953 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1954 ops->ooboffs, len);
1955 BUG_ON(ret);
1956 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001957
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001958 default:
1959 BUG();
1960 }
1961 return NULL;
1962}
1963
1964/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001965 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1966 * @mtd: MTD device structure
1967 * @retry_mode: the retry mode to use
1968 *
1969 * Some vendors supply a special command to shift the Vt threshold, to be used
1970 * when there are too many bitflips in a page (i.e., ECC error). After setting
1971 * a new threshold, the host should retry reading the page.
1972 */
1973static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1974{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001975 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001976
1977 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1978
1979 if (retry_mode >= chip->read_retries)
1980 return -EINVAL;
1981
1982 if (!chip->setup_read_retry)
1983 return -EOPNOTSUPP;
1984
1985 return chip->setup_read_retry(mtd, retry_mode);
1986}
1987
1988/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001989 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001990 * @mtd: MTD device structure
1991 * @from: offset to read from
1992 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001993 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001994 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001995 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001996static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1997 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001998{
Brian Norrise47f3db2012-05-02 10:14:56 -07001999 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002000 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002001 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002002 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03002003 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002004 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002005
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002006 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002007 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002008 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002009 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002010 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002012 chipnr = (int)(from >> chip->chip_shift);
2013 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002015 realpage = (int)(from >> chip->page_shift);
2016 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002018 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002020 buf = ops->datbuf;
2021 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002022 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002023
Florian Fainellif8ac0412010-09-07 13:23:43 +02002024 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002025 unsigned int ecc_failures = mtd->ecc_stats.failed;
2026
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002027 bytes = min(mtd->writesize - col, readlen);
2028 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002029
Kamal Dasu66507c72014-05-01 20:51:19 -04002030 if (!aligned)
2031 use_bufpoi = 1;
2032 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002033 use_bufpoi = !virt_addr_valid(buf) ||
2034 !IS_ALIGNED((unsigned long)buf,
2035 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002036 else
2037 use_bufpoi = 0;
2038
Brian Norris8b6e50c2011-05-25 14:59:01 -07002039 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002040 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002041 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2042
2043 if (use_bufpoi && aligned)
2044 pr_debug("%s: using read bounce buffer for buf@%p\n",
2045 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Brian Norrisba84fb52014-01-03 15:13:33 -08002047read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01002048 if (nand_standard_page_accessors(&chip->ecc))
2049 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Mike Dunnedbc45402012-04-25 12:06:11 -07002051 /*
2052 * Now read the page into the buffer. Absent an error,
2053 * the read methods return max bitflips per ecc step.
2054 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002055 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002056 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002057 oob_required,
2058 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002059 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2060 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002061 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002062 col, bytes, bufpoi,
2063 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002064 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002065 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002066 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002067 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002068 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002069 /* Invalidate page cache */
2070 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002071 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002072 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002073
2074 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002075 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002076 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002077 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002078 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002079 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002080 chip->pagebuf_bitflips = ret;
2081 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002082 /* Invalidate page cache */
2083 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002084 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002085 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002087
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002088 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002089 int toread = min(oobreadlen, max_oobsize);
2090
2091 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002092 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002093 oob, ops, toread);
2094 oobreadlen -= toread;
2095 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002096 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002097
2098 if (chip->options & NAND_NEED_READRDY) {
2099 /* Apply delay or wait for ready/busy pin */
2100 if (!chip->dev_ready)
2101 udelay(chip->chip_delay);
2102 else
2103 nand_wait_ready(mtd);
2104 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002105
Brian Norrisba84fb52014-01-03 15:13:33 -08002106 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002107 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002108 retry_mode++;
2109 ret = nand_setup_read_retry(mtd,
2110 retry_mode);
2111 if (ret < 0)
2112 break;
2113
2114 /* Reset failures; retry */
2115 mtd->ecc_stats.failed = ecc_failures;
2116 goto read_retry;
2117 } else {
2118 /* No more retry modes; real failure */
2119 ecc_fail = true;
2120 }
2121 }
2122
2123 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002124 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002125 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002126 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002127 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002128 max_bitflips = max_t(unsigned int, max_bitflips,
2129 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002132 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002133
Brian Norrisba84fb52014-01-03 15:13:33 -08002134 /* Reset to retry mode 0 */
2135 if (retry_mode) {
2136 ret = nand_setup_read_retry(mtd, 0);
2137 if (ret < 0)
2138 break;
2139 retry_mode = 0;
2140 }
2141
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002142 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Brian Norris8b6e50c2011-05-25 14:59:01 -07002145 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 col = 0;
2147 /* Increment page address */
2148 realpage++;
2149
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002150 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /* Check, if we cross a chip boundary */
2152 if (!page) {
2153 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002154 chip->select_chip(mtd, -1);
2155 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002158 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002160 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002161 if (oob)
2162 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Mike Dunn3f91e942012-04-25 12:06:09 -07002164 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002165 return ret;
2166
Brian Norrisb72f3df2013-12-03 11:04:14 -08002167 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002168 return -EBADMSG;
2169
Mike Dunnedbc45402012-04-25 12:06:11 -07002170 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002171}
2172
2173/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002174 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002175 * @mtd: MTD device structure
2176 * @from: offset to read from
2177 * @len: number of bytes to read
2178 * @retlen: pointer to variable to store the number of read bytes
2179 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002180 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002181 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002182 */
2183static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2184 size_t *retlen, uint8_t *buf)
2185{
Brian Norris4a89ff82011-08-30 18:45:45 -07002186 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002187 int ret;
2188
Huang Shijie6a8214a2012-11-19 14:43:30 +08002189 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002190 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002191 ops.len = len;
2192 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002193 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002194 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002195 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002196 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
2200/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002201 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002202 * @mtd: mtd info structure
2203 * @chip: nand chip info structure
2204 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002205 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002206int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002207{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002208 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002209 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002210 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002211}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002212EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002213
2214/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002215 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002216 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002217 * @mtd: mtd info structure
2218 * @chip: nand chip info structure
2219 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002220 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002221int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2222 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002223{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002224 int length = mtd->oobsize;
2225 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2226 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002227 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002228 int i, toread, sndrnd = 0, pos;
2229
2230 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2231 for (i = 0; i < chip->ecc.steps; i++) {
2232 if (sndrnd) {
2233 pos = eccsize + i * (eccsize + chunk);
2234 if (mtd->writesize > 512)
2235 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2236 else
2237 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2238 } else
2239 sndrnd = 1;
2240 toread = min_t(int, length, chunk);
2241 chip->read_buf(mtd, bufpoi, toread);
2242 bufpoi += toread;
2243 length -= toread;
2244 }
2245 if (length > 0)
2246 chip->read_buf(mtd, bufpoi, length);
2247
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002248 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002249}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002250EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002251
2252/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002253 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002254 * @mtd: mtd info structure
2255 * @chip: nand chip info structure
2256 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002257 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002258int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002259{
2260 int status = 0;
2261 const uint8_t *buf = chip->oob_poi;
2262 int length = mtd->oobsize;
2263
2264 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2265 chip->write_buf(mtd, buf, length);
2266 /* Send command to program the OOB data */
2267 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2268
2269 status = chip->waitfunc(mtd, chip);
2270
Savin Zlobec0d420f92006-06-21 11:51:20 +02002271 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002272}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002273EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002274
2275/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002276 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002277 * with syndrome - only for large page flash
2278 * @mtd: mtd info structure
2279 * @chip: nand chip info structure
2280 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002281 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002282int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2283 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002284{
2285 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2286 int eccsize = chip->ecc.size, length = mtd->oobsize;
2287 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2288 const uint8_t *bufpoi = chip->oob_poi;
2289
2290 /*
2291 * data-ecc-data-ecc ... ecc-oob
2292 * or
2293 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2294 */
2295 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2296 pos = steps * (eccsize + chunk);
2297 steps = 0;
2298 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002299 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002300
2301 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2302 for (i = 0; i < steps; i++) {
2303 if (sndcmd) {
2304 if (mtd->writesize <= 512) {
2305 uint32_t fill = 0xFFFFFFFF;
2306
2307 len = eccsize;
2308 while (len > 0) {
2309 int num = min_t(int, len, 4);
2310 chip->write_buf(mtd, (uint8_t *)&fill,
2311 num);
2312 len -= num;
2313 }
2314 } else {
2315 pos = eccsize + i * (eccsize + chunk);
2316 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2317 }
2318 } else
2319 sndcmd = 1;
2320 len = min_t(int, length, chunk);
2321 chip->write_buf(mtd, bufpoi, len);
2322 bufpoi += len;
2323 length -= len;
2324 }
2325 if (length > 0)
2326 chip->write_buf(mtd, bufpoi, length);
2327
2328 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2329 status = chip->waitfunc(mtd, chip);
2330
2331 return status & NAND_STATUS_FAIL ? -EIO : 0;
2332}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002333EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002334
2335/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002336 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002337 * @mtd: MTD device structure
2338 * @from: offset to read from
2339 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002341 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002343static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2344 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Brian Norrisc00a0992012-05-01 17:12:54 -07002346 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002347 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002348 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002349 int readlen = ops->ooblen;
2350 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002351 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002352 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Brian Norris289c0522011-07-19 10:06:09 -07002354 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302355 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Brian Norris041e4572011-06-23 16:45:24 -07002357 stats = mtd->ecc_stats;
2358
Boris BREZILLON29f10582016-03-07 10:46:52 +01002359 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002360
2361 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002362 pr_debug("%s: attempt to start read outside oob\n",
2363 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002364 return -EINVAL;
2365 }
2366
2367 /* Do not allow reads past end of device */
2368 if (unlikely(from >= mtd->size ||
2369 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2370 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002371 pr_debug("%s: attempt to read beyond end of device\n",
2372 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002373 return -EINVAL;
2374 }
Vitaly Wool70145682006-11-03 18:20:38 +03002375
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002376 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002377 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002379 /* Shift to get page */
2380 realpage = (int)(from >> chip->page_shift);
2381 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Florian Fainellif8ac0412010-09-07 13:23:43 +02002383 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002384 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002385 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002386 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002387 ret = chip->ecc.read_oob(mtd, chip, page);
2388
2389 if (ret < 0)
2390 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002391
2392 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002393 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002394
Brian Norris5bc7c332013-03-13 09:51:31 -07002395 if (chip->options & NAND_NEED_READRDY) {
2396 /* Apply delay or wait for ready/busy pin */
2397 if (!chip->dev_ready)
2398 udelay(chip->chip_delay);
2399 else
2400 nand_wait_ready(mtd);
2401 }
2402
Vitaly Wool70145682006-11-03 18:20:38 +03002403 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002404 if (!readlen)
2405 break;
2406
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002407 /* Increment page address */
2408 realpage++;
2409
2410 page = realpage & chip->pagemask;
2411 /* Check, if we cross a chip boundary */
2412 if (!page) {
2413 chipnr++;
2414 chip->select_chip(mtd, -1);
2415 chip->select_chip(mtd, chipnr);
2416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002418 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002420 ops->oobretlen = ops->ooblen - readlen;
2421
2422 if (ret < 0)
2423 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002424
2425 if (mtd->ecc_stats.failed - stats.failed)
2426 return -EBADMSG;
2427
2428 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002432 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002433 * @mtd: MTD device structure
2434 * @from: offset to read from
2435 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002437 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002439static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2440 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002442 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002443
2444 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002447 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002448 pr_debug("%s: attempt to read beyond end of device\n",
2449 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return -EINVAL;
2451 }
2452
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002453 if (ops->mode != MTD_OPS_PLACE_OOB &&
2454 ops->mode != MTD_OPS_AUTO_OOB &&
2455 ops->mode != MTD_OPS_RAW)
2456 return -ENOTSUPP;
2457
Huang Shijie6a8214a2012-11-19 14:43:30 +08002458 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002460 if (!ops->datbuf)
2461 ret = nand_do_read_oob(mtd, from, ops);
2462 else
2463 ret = nand_do_read_ops(mtd, from, ops);
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002466 return ret;
2467}
2468
2469
2470/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002471 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002472 * @mtd: mtd info structure
2473 * @chip: nand chip info structure
2474 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002475 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002476 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002477 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002478 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002480int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2481 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002482{
2483 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002484 if (oob_required)
2485 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002486
2487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002489EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002491/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002492 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002493 * @mtd: mtd info structure
2494 * @chip: nand chip info structure
2495 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002496 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002497 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002498 *
2499 * We need a special oob layout and handling even when ECC isn't checked.
2500 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002501static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002502 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002503 const uint8_t *buf, int oob_required,
2504 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002505{
2506 int eccsize = chip->ecc.size;
2507 int eccbytes = chip->ecc.bytes;
2508 uint8_t *oob = chip->oob_poi;
2509 int steps, size;
2510
2511 for (steps = chip->ecc.steps; steps > 0; steps--) {
2512 chip->write_buf(mtd, buf, eccsize);
2513 buf += eccsize;
2514
2515 if (chip->ecc.prepad) {
2516 chip->write_buf(mtd, oob, chip->ecc.prepad);
2517 oob += chip->ecc.prepad;
2518 }
2519
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002520 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002521 oob += eccbytes;
2522
2523 if (chip->ecc.postpad) {
2524 chip->write_buf(mtd, oob, chip->ecc.postpad);
2525 oob += chip->ecc.postpad;
2526 }
2527 }
2528
2529 size = mtd->oobsize - (oob - chip->oob_poi);
2530 if (size)
2531 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002532
2533 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002534}
2535/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002536 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002537 * @mtd: mtd info structure
2538 * @chip: nand chip info structure
2539 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002540 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002541 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002542 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002543static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002544 const uint8_t *buf, int oob_required,
2545 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002546{
Boris Brezillon846031d2016-02-03 20:11:00 +01002547 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002548 int eccbytes = chip->ecc.bytes;
2549 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002550 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002551 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002552
Brian Norris7854d3f2011-06-23 14:12:08 -07002553 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002554 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2555 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002556
Boris Brezillon846031d2016-02-03 20:11:00 +01002557 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2558 chip->ecc.total);
2559 if (ret)
2560 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002561
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002562 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002563}
2564
2565/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002566 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002567 * @mtd: mtd info structure
2568 * @chip: nand chip info structure
2569 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002570 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002571 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002572 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002573static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002574 const uint8_t *buf, int oob_required,
2575 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002576{
Boris Brezillon846031d2016-02-03 20:11:00 +01002577 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002578 int eccbytes = chip->ecc.bytes;
2579 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002580 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002581 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002582
2583 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2584 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002585 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002586 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2587 }
2588
Boris Brezillon846031d2016-02-03 20:11:00 +01002589 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2590 chip->ecc.total);
2591 if (ret)
2592 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002593
2594 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002595
2596 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002597}
2598
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302599
2600/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002601 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302602 * @mtd: mtd info structure
2603 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002604 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302605 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002606 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302607 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002608 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302609 */
2610static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2611 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002612 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002613 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302614{
2615 uint8_t *oob_buf = chip->oob_poi;
2616 uint8_t *ecc_calc = chip->buffers->ecccalc;
2617 int ecc_size = chip->ecc.size;
2618 int ecc_bytes = chip->ecc.bytes;
2619 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302620 uint32_t start_step = offset / ecc_size;
2621 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2622 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002623 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302624
2625 for (step = 0; step < ecc_steps; step++) {
2626 /* configure controller for WRITE access */
2627 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2628
2629 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002630 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302631
2632 /* mask ECC of un-touched subpages by padding 0xFF */
2633 if ((step < start_step) || (step > end_step))
2634 memset(ecc_calc, 0xff, ecc_bytes);
2635 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002636 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302637
2638 /* mask OOB of un-touched subpages by padding 0xFF */
2639 /* if oob_required, preserve OOB metadata of written subpage */
2640 if (!oob_required || (step < start_step) || (step > end_step))
2641 memset(oob_buf, 0xff, oob_bytes);
2642
Brian Norrisd6a950802013-08-08 17:16:36 -07002643 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302644 ecc_calc += ecc_bytes;
2645 oob_buf += oob_bytes;
2646 }
2647
2648 /* copy calculated ECC for whole page to chip->buffer->oob */
2649 /* this include masked-value(0xFF) for unwritten subpages */
2650 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002651 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2652 chip->ecc.total);
2653 if (ret)
2654 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302655
2656 /* write OOB buffer to NAND device */
2657 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2658
2659 return 0;
2660}
2661
2662
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002663/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002664 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002665 * @mtd: mtd info structure
2666 * @chip: nand chip info structure
2667 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002668 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002669 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002670 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002671 * The hw generator calculates the error syndrome automatically. Therefore we
2672 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002673 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002674static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002675 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002676 const uint8_t *buf, int oob_required,
2677 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002678{
2679 int i, eccsize = chip->ecc.size;
2680 int eccbytes = chip->ecc.bytes;
2681 int eccsteps = chip->ecc.steps;
2682 const uint8_t *p = buf;
2683 uint8_t *oob = chip->oob_poi;
2684
2685 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2686
2687 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2688 chip->write_buf(mtd, p, eccsize);
2689
2690 if (chip->ecc.prepad) {
2691 chip->write_buf(mtd, oob, chip->ecc.prepad);
2692 oob += chip->ecc.prepad;
2693 }
2694
2695 chip->ecc.calculate(mtd, p, oob);
2696 chip->write_buf(mtd, oob, eccbytes);
2697 oob += eccbytes;
2698
2699 if (chip->ecc.postpad) {
2700 chip->write_buf(mtd, oob, chip->ecc.postpad);
2701 oob += chip->ecc.postpad;
2702 }
2703 }
2704
2705 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002706 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002707 if (i)
2708 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002709
2710 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002711}
2712
2713/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002714 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002715 * @mtd: MTD device structure
2716 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302717 * @offset: address offset within the page
2718 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002719 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002720 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002721 * @page: page number to write
2722 * @cached: cached programming
2723 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002724 */
2725static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302726 uint32_t offset, int data_len, const uint8_t *buf,
2727 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002728{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302729 int status, subpage;
2730
2731 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2732 chip->ecc.write_subpage)
2733 subpage = offset || (data_len < mtd->writesize);
2734 else
2735 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002736
Marc Gonzalez3371d662016-11-15 10:56:20 +01002737 if (nand_standard_page_accessors(&chip->ecc))
2738 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002739
David Woodhouse956e9442006-09-25 17:12:39 +01002740 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302741 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002742 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302743 else if (subpage)
2744 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002745 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002746 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002747 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2748 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002749
2750 if (status < 0)
2751 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002752
2753 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002754 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002755 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002756 */
2757 cached = 0;
2758
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002759 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002760
Marc Gonzalez3371d662016-11-15 10:56:20 +01002761 if (nand_standard_page_accessors(&chip->ecc))
2762 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002763 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002764 /*
2765 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002766 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002767 */
2768 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2769 status = chip->errstat(mtd, chip, FL_WRITING, status,
2770 page);
2771
2772 if (status & NAND_STATUS_FAIL)
2773 return -EIO;
2774 } else {
2775 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002776 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002777 }
2778
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002779 return 0;
2780}
2781
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002782/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002783 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002784 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002785 * @oob: oob data buffer
2786 * @len: oob data write length
2787 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002788 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002789static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2790 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002791{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002792 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002793 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002794
2795 /*
2796 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2797 * data from a previous OOB read.
2798 */
2799 memset(chip->oob_poi, 0xff, mtd->oobsize);
2800
Florian Fainellif8ac0412010-09-07 13:23:43 +02002801 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002802
Brian Norris0612b9d2011-08-30 18:45:40 -07002803 case MTD_OPS_PLACE_OOB:
2804 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002805 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2806 return oob + len;
2807
Boris Brezillon846031d2016-02-03 20:11:00 +01002808 case MTD_OPS_AUTO_OOB:
2809 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2810 ops->ooboffs, len);
2811 BUG_ON(ret);
2812 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002813
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002814 default:
2815 BUG();
2816 }
2817 return NULL;
2818}
2819
Florian Fainellif8ac0412010-09-07 13:23:43 +02002820#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002821
2822/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002823 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002824 * @mtd: MTD device structure
2825 * @to: offset to write to
2826 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002827 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002828 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002829 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002830static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2831 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002832{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002833 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002834 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002835 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002836
2837 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002838 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002839
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002840 uint8_t *oob = ops->oobbuf;
2841 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302842 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002843 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002844
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002845 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002846 if (!writelen)
2847 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002848
Brian Norris8b6e50c2011-05-25 14:59:01 -07002849 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002850 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002851 pr_notice("%s: attempt to write non page aligned data\n",
2852 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002853 return -EINVAL;
2854 }
2855
Thomas Gleixner29072b92006-09-28 15:38:36 +02002856 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002857
Thomas Gleixner6a930962006-06-28 00:11:45 +02002858 chipnr = (int)(to >> chip->chip_shift);
2859 chip->select_chip(mtd, chipnr);
2860
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002861 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002862 if (nand_check_wp(mtd)) {
2863 ret = -EIO;
2864 goto err_out;
2865 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002866
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002867 realpage = (int)(to >> chip->page_shift);
2868 page = realpage & chip->pagemask;
2869 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2870
2871 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002872 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2873 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002874 chip->pagebuf = -1;
2875
Maxim Levitsky782ce792010-02-22 20:39:36 +02002876 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002877 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2878 ret = -EINVAL;
2879 goto err_out;
2880 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002881
Florian Fainellif8ac0412010-09-07 13:23:43 +02002882 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002883 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002884 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002885 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002886 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002887 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002888
Kamal Dasu66507c72014-05-01 20:51:19 -04002889 if (part_pagewr)
2890 use_bufpoi = 1;
2891 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002892 use_bufpoi = !virt_addr_valid(buf) ||
2893 !IS_ALIGNED((unsigned long)buf,
2894 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002895 else
2896 use_bufpoi = 0;
2897
2898 /* Partial page write?, or need to use bounce buffer */
2899 if (use_bufpoi) {
2900 pr_debug("%s: using write bounce buffer for buf@%p\n",
2901 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002902 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002903 if (part_pagewr)
2904 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002905 chip->pagebuf = -1;
2906 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2907 memcpy(&chip->buffers->databuf[column], buf, bytes);
2908 wbuf = chip->buffers->databuf;
2909 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002910
Maxim Levitsky782ce792010-02-22 20:39:36 +02002911 if (unlikely(oob)) {
2912 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002913 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002914 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002915 } else {
2916 /* We still need to erase leftover OOB data */
2917 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002918 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002919
2920 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
2921 oob_required, page, cached,
2922 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002923 if (ret)
2924 break;
2925
2926 writelen -= bytes;
2927 if (!writelen)
2928 break;
2929
Thomas Gleixner29072b92006-09-28 15:38:36 +02002930 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002931 buf += bytes;
2932 realpage++;
2933
2934 page = realpage & chip->pagemask;
2935 /* Check, if we cross a chip boundary */
2936 if (!page) {
2937 chipnr++;
2938 chip->select_chip(mtd, -1);
2939 chip->select_chip(mtd, chipnr);
2940 }
2941 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002942
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002943 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002944 if (unlikely(oob))
2945 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002946
2947err_out:
2948 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002949 return ret;
2950}
2951
2952/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002953 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002954 * @mtd: MTD device structure
2955 * @to: offset to write to
2956 * @len: number of bytes to write
2957 * @retlen: pointer to variable to store the number of written bytes
2958 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002959 *
2960 * NAND write with ECC. Used when performing writes in interrupt context, this
2961 * may for example be called by mtdoops when writing an oops while in panic.
2962 */
2963static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2964 size_t *retlen, const uint8_t *buf)
2965{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002966 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002967 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002968 int ret;
2969
Brian Norris8b6e50c2011-05-25 14:59:01 -07002970 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002971 panic_nand_wait(mtd, chip, 400);
2972
Brian Norris8b6e50c2011-05-25 14:59:01 -07002973 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002974 panic_nand_get_device(chip, mtd, FL_WRITING);
2975
Brian Norris0ec56dc2015-02-28 02:02:30 -08002976 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002977 ops.len = len;
2978 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002979 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002980
Brian Norris4a89ff82011-08-30 18:45:45 -07002981 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002982
Brian Norris4a89ff82011-08-30 18:45:45 -07002983 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002984 return ret;
2985}
2986
2987/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002988 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002989 * @mtd: MTD device structure
2990 * @to: offset to write to
2991 * @len: number of bytes to write
2992 * @retlen: pointer to variable to store the number of written bytes
2993 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002995 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002997static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002998 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Brian Norris4a89ff82011-08-30 18:45:45 -07003000 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003001 int ret;
3002
Huang Shijie6a8214a2012-11-19 14:43:30 +08003003 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003004 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003005 ops.len = len;
3006 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003007 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003008 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003009 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003010 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003011 return ret;
3012}
3013
3014/**
3015 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003016 * @mtd: MTD device structure
3017 * @to: offset to write to
3018 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003019 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003020 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003021 */
3022static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3023 struct mtd_oob_ops *ops)
3024{
Adrian Hunter03736152007-01-31 17:58:29 +02003025 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003026 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Brian Norris289c0522011-07-19 10:06:09 -07003028 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303029 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Boris BREZILLON29f10582016-03-07 10:46:52 +01003031 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003034 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003035 pr_debug("%s: attempt to write past end of page\n",
3036 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return -EINVAL;
3038 }
3039
Adrian Hunter03736152007-01-31 17:58:29 +02003040 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003041 pr_debug("%s: attempt to start write outside oob\n",
3042 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003043 return -EINVAL;
3044 }
3045
Jason Liu775adc3d42011-02-25 13:06:18 +08003046 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003047 if (unlikely(to >= mtd->size ||
3048 ops->ooboffs + ops->ooblen >
3049 ((mtd->size >> chip->page_shift) -
3050 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003051 pr_debug("%s: attempt to write beyond end of device\n",
3052 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003053 return -EINVAL;
3054 }
3055
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003056 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003057
3058 /*
3059 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3060 * of my DiskOnChip 2000 test units) will clear the whole data page too
3061 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3062 * it in the doc2000 driver in August 1999. dwmw2.
3063 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003064 nand_reset(chip, chipnr);
3065
3066 chip->select_chip(mtd, chipnr);
3067
3068 /* Shift to get page */
3069 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003072 if (nand_check_wp(mtd)) {
3073 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003074 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003075 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003078 if (page == chip->pagebuf)
3079 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003081 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003082
Brian Norris0612b9d2011-08-30 18:45:40 -07003083 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003084 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3085 else
3086 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003087
Huang Shijieb0bb6902012-11-19 14:43:29 +08003088 chip->select_chip(mtd, -1);
3089
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003090 if (status)
3091 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Vitaly Wool70145682006-11-03 18:20:38 +03003093 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003095 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003098/**
3099 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003100 * @mtd: MTD device structure
3101 * @to: offset to write to
3102 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003103 */
3104static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3105 struct mtd_oob_ops *ops)
3106{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003107 int ret = -ENOTSUPP;
3108
3109 ops->retlen = 0;
3110
3111 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003112 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003113 pr_debug("%s: attempt to write beyond end of device\n",
3114 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003115 return -EINVAL;
3116 }
3117
Huang Shijie6a8214a2012-11-19 14:43:30 +08003118 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003119
Florian Fainellif8ac0412010-09-07 13:23:43 +02003120 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003121 case MTD_OPS_PLACE_OOB:
3122 case MTD_OPS_AUTO_OOB:
3123 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003124 break;
3125
3126 default:
3127 goto out;
3128 }
3129
3130 if (!ops->datbuf)
3131 ret = nand_do_write_oob(mtd, to, ops);
3132 else
3133 ret = nand_do_write_ops(mtd, to, ops);
3134
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003135out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003136 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return ret;
3138}
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140/**
Brian Norris49c50b92014-05-06 16:02:19 -07003141 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003142 * @mtd: MTD device structure
3143 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 *
Brian Norris49c50b92014-05-06 16:02:19 -07003145 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 */
Brian Norris49c50b92014-05-06 16:02:19 -07003147static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003149 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003151 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3152 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003153
3154 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
3157/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003159 * @mtd: MTD device structure
3160 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003162 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003164static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
David Woodhousee0c7d762006-05-13 18:07:53 +01003166 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003170 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003171 * @mtd: MTD device structure
3172 * @instr: erase instruction
3173 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003175 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003177int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3178 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Adrian Hunter69423d92008-12-10 13:37:21 +00003180 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003181 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003182 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Brian Norris289c0522011-07-19 10:06:09 -07003184 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3185 __func__, (unsigned long long)instr->addr,
3186 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303188 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003192 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003195 page = (int)(instr->addr >> chip->page_shift);
3196 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003199 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003202 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 /* Check, if it is write protected */
3205 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003206 pr_debug("%s: device is write protected!\n",
3207 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 instr->state = MTD_ERASE_FAILED;
3209 goto erase_exit;
3210 }
3211
3212 /* Loop through the pages */
3213 len = instr->len;
3214
3215 instr->state = MTD_ERASING;
3216
3217 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003218 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003219 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303220 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003221 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3222 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 instr->state = MTD_ERASE_FAILED;
3224 goto erase_exit;
3225 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003226
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003227 /*
3228 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003229 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003230 */
3231 if (page <= chip->pagebuf && chip->pagebuf <
3232 (page + pages_per_block))
3233 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Brian Norris49c50b92014-05-06 16:02:19 -07003235 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003237 /*
3238 * See if operation failed and additional status checks are
3239 * available
3240 */
3241 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3242 status = chip->errstat(mtd, chip, FL_ERASING,
3243 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003246 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003247 pr_debug("%s: failed erase, page 0x%08x\n",
3248 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003250 instr->fail_addr =
3251 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 goto erase_exit;
3253 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003256 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 page += pages_per_block;
3258
3259 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003260 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003262 chip->select_chip(mtd, -1);
3263 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
3265 }
3266 instr->state = MTD_ERASE_DONE;
3267
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003268erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003273 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 nand_release_device(mtd);
3275
David Woodhouse49defc02007-10-06 15:01:59 -04003276 /* Do call back function */
3277 if (!ret)
3278 mtd_erase_callback(instr);
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* Return more or less happy */
3281 return ret;
3282}
3283
3284/**
3285 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003286 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003288 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003290static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Brian Norris289c0522011-07-19 10:06:09 -07003292 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003295 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003297 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003301 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003302 * @mtd: MTD device structure
3303 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003305static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303307 struct nand_chip *chip = mtd_to_nand(mtd);
3308 int chipnr = (int)(offs >> chip->chip_shift);
3309 int ret;
3310
3311 /* Select the NAND device */
3312 nand_get_device(mtd, FL_READING);
3313 chip->select_chip(mtd, chipnr);
3314
3315 ret = nand_block_checkbad(mtd, offs, 0);
3316
3317 chip->select_chip(mtd, -1);
3318 nand_release_device(mtd);
3319
3320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
3323/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003324 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003325 * @mtd: MTD device structure
3326 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003328static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 int ret;
3331
Florian Fainellif8ac0412010-09-07 13:23:43 +02003332 ret = nand_block_isbad(mtd, ofs);
3333 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003334 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 if (ret > 0)
3336 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003337 return ret;
3338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Brian Norris5a0edb22013-07-30 17:52:58 -07003340 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
3343/**
Zach Brown56718422017-01-10 13:30:20 -06003344 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3345 * @mtd: MTD device structure
3346 * @ofs: offset relative to mtd start
3347 * @len: length of mtd
3348 */
3349static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3350{
3351 struct nand_chip *chip = mtd_to_nand(mtd);
3352 u32 part_start_block;
3353 u32 part_end_block;
3354 u32 part_start_die;
3355 u32 part_end_die;
3356
3357 /*
3358 * max_bb_per_die and blocks_per_die used to determine
3359 * the maximum bad block count.
3360 */
3361 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3362 return -ENOTSUPP;
3363
3364 /* Get the start and end of the partition in erase blocks. */
3365 part_start_block = mtd_div_by_eb(ofs, mtd);
3366 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3367
3368 /* Get the start and end LUNs of the partition. */
3369 part_start_die = part_start_block / chip->blocks_per_die;
3370 part_end_die = part_end_block / chip->blocks_per_die;
3371
3372 /*
3373 * Look up the bad blocks per unit and multiply by the number of units
3374 * that the partition spans.
3375 */
3376 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3377}
3378
3379/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003380 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3381 * @mtd: MTD device structure
3382 * @chip: nand chip info structure
3383 * @addr: feature address.
3384 * @subfeature_param: the subfeature parameters, a four bytes array.
3385 */
3386static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3387 int addr, uint8_t *subfeature_param)
3388{
3389 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003390 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003391
David Mosbergerd914c932013-05-29 15:30:13 +03003392 if (!chip->onfi_version ||
3393 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3394 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003395 return -EINVAL;
3396
3397 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003398 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3399 chip->write_byte(mtd, subfeature_param[i]);
3400
Huang Shijie7db03ec2012-09-13 14:57:52 +08003401 status = chip->waitfunc(mtd, chip);
3402 if (status & NAND_STATUS_FAIL)
3403 return -EIO;
3404 return 0;
3405}
3406
3407/**
3408 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3409 * @mtd: MTD device structure
3410 * @chip: nand chip info structure
3411 * @addr: feature address.
3412 * @subfeature_param: the subfeature parameters, a four bytes array.
3413 */
3414static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3415 int addr, uint8_t *subfeature_param)
3416{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003417 int i;
3418
David Mosbergerd914c932013-05-29 15:30:13 +03003419 if (!chip->onfi_version ||
3420 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3421 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003422 return -EINVAL;
3423
Huang Shijie7db03ec2012-09-13 14:57:52 +08003424 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003425 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3426 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003427 return 0;
3428}
3429
3430/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003431 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3432 * -ENOTSUPP
3433 * @mtd: MTD device structure
3434 * @chip: nand chip info structure
3435 * @addr: feature address.
3436 * @subfeature_param: the subfeature parameters, a four bytes array.
3437 *
3438 * Should be used by NAND controller drivers that do not support the SET/GET
3439 * FEATURES operations.
3440 */
3441int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3442 struct nand_chip *chip, int addr,
3443 u8 *subfeature_param)
3444{
3445 return -ENOTSUPP;
3446}
3447EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3448
3449/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003450 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003451 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003452 */
3453static int nand_suspend(struct mtd_info *mtd)
3454{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003455 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003456}
3457
3458/**
3459 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003460 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003461 */
3462static void nand_resume(struct mtd_info *mtd)
3463{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003464 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003465
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003466 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003467 nand_release_device(mtd);
3468 else
Brian Norrisd0370212011-07-19 10:06:08 -07003469 pr_err("%s called for a chip which is not in suspended state\n",
3470 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003471}
3472
Scott Branden72ea4032014-11-20 11:18:05 -08003473/**
3474 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3475 * prevent further operations
3476 * @mtd: MTD device structure
3477 */
3478static void nand_shutdown(struct mtd_info *mtd)
3479{
Brian Norris9ca641b2015-11-09 16:37:28 -08003480 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003481}
3482
Brian Norris8b6e50c2011-05-25 14:59:01 -07003483/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003484static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003485{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003486 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003489 if (!chip->chip_delay)
3490 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003493 if (chip->cmdfunc == NULL)
3494 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003497 if (chip->waitfunc == NULL)
3498 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003500 if (!chip->select_chip)
3501 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003502
Huang Shijie4204ccc2013-08-16 10:10:07 +08003503 /* set for ONFI nand */
3504 if (!chip->onfi_set_features)
3505 chip->onfi_set_features = nand_onfi_set_features;
3506 if (!chip->onfi_get_features)
3507 chip->onfi_get_features = nand_onfi_get_features;
3508
Brian Norris68e80782013-07-18 01:17:02 -07003509 /* If called twice, pointers that depend on busw may need to be reset */
3510 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003511 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3512 if (!chip->read_word)
3513 chip->read_word = nand_read_word;
3514 if (!chip->block_bad)
3515 chip->block_bad = nand_block_bad;
3516 if (!chip->block_markbad)
3517 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003518 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003519 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003520 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3521 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003522 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003523 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003524 if (!chip->scan_bbt)
3525 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003526
3527 if (!chip->controller) {
3528 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003529 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003530 }
3531
Masahiro Yamada477544c2017-03-30 17:15:05 +09003532 if (!chip->buf_align)
3533 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003534}
3535
Brian Norris8b6e50c2011-05-25 14:59:01 -07003536/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003537static void sanitize_string(uint8_t *s, size_t len)
3538{
3539 ssize_t i;
3540
Brian Norris8b6e50c2011-05-25 14:59:01 -07003541 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003542 s[len - 1] = 0;
3543
Brian Norris8b6e50c2011-05-25 14:59:01 -07003544 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003545 for (i = 0; i < len - 1; i++) {
3546 if (s[i] < ' ' || s[i] > 127)
3547 s[i] = '?';
3548 }
3549
Brian Norris8b6e50c2011-05-25 14:59:01 -07003550 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003551 strim(s);
3552}
3553
3554static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3555{
3556 int i;
3557 while (len--) {
3558 crc ^= *p++ << 8;
3559 for (i = 0; i < 8; i++)
3560 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3561 }
3562
3563 return crc;
3564}
3565
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003566/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003567static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3568 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003569{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003570 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003571 struct onfi_ext_param_page *ep;
3572 struct onfi_ext_section *s;
3573 struct onfi_ext_ecc_info *ecc;
3574 uint8_t *cursor;
3575 int ret = -EINVAL;
3576 int len;
3577 int i;
3578
3579 len = le16_to_cpu(p->ext_param_page_length) * 16;
3580 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003581 if (!ep)
3582 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003583
3584 /* Send our own NAND_CMD_PARAM. */
3585 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3586
3587 /* Use the Change Read Column command to skip the ONFI param pages. */
3588 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3589 sizeof(*p) * p->num_of_param_pages , -1);
3590
3591 /* Read out the Extended Parameter Page. */
3592 chip->read_buf(mtd, (uint8_t *)ep, len);
3593 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3594 != le16_to_cpu(ep->crc))) {
3595 pr_debug("fail in the CRC.\n");
3596 goto ext_out;
3597 }
3598
3599 /*
3600 * Check the signature.
3601 * Do not strictly follow the ONFI spec, maybe changed in future.
3602 */
3603 if (strncmp(ep->sig, "EPPS", 4)) {
3604 pr_debug("The signature is invalid.\n");
3605 goto ext_out;
3606 }
3607
3608 /* find the ECC section. */
3609 cursor = (uint8_t *)(ep + 1);
3610 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3611 s = ep->sections + i;
3612 if (s->type == ONFI_SECTION_TYPE_2)
3613 break;
3614 cursor += s->length * 16;
3615 }
3616 if (i == ONFI_EXT_SECTION_MAX) {
3617 pr_debug("We can not find the ECC section.\n");
3618 goto ext_out;
3619 }
3620
3621 /* get the info we want. */
3622 ecc = (struct onfi_ext_ecc_info *)cursor;
3623
Brian Norris4ae7d222013-09-16 18:20:21 -07003624 if (!ecc->codeword_size) {
3625 pr_debug("Invalid codeword size\n");
3626 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003627 }
3628
Brian Norris4ae7d222013-09-16 18:20:21 -07003629 chip->ecc_strength_ds = ecc->ecc_bits;
3630 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003631 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003632
3633ext_out:
3634 kfree(ep);
3635 return ret;
3636}
3637
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003638/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003639 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003640 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003641static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003642{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003643 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003644 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003645 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003646 int val;
3647
Brian Norris7854d3f2011-06-23 14:12:08 -07003648 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003649 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3650 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3651 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3652 return 0;
3653
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003654 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3655 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003656 for (j = 0; j < sizeof(*p); j++)
3657 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003658 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3659 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003660 break;
3661 }
3662 }
3663
Brian Norrisc7f23a72013-08-13 10:51:55 -07003664 if (i == 3) {
3665 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003666 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003667 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003668
Brian Norris8b6e50c2011-05-25 14:59:01 -07003669 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003670 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003671 if (val & (1 << 5))
3672 chip->onfi_version = 23;
3673 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003674 chip->onfi_version = 22;
3675 else if (val & (1 << 3))
3676 chip->onfi_version = 21;
3677 else if (val & (1 << 2))
3678 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003679 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003680 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003681
3682 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003683 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003684 return 0;
3685 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003686
3687 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3688 sanitize_string(p->model, sizeof(p->model));
3689 if (!mtd->name)
3690 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003691
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003692 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003693
3694 /*
3695 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3696 * (don't ask me who thought of this...). MTD assumes that these
3697 * dimensions will be power-of-2, so just truncate the remaining area.
3698 */
3699 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3700 mtd->erasesize *= mtd->writesize;
3701
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003702 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003703
3704 /* See erasesize comment */
3705 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003706 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003707 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003708
Zach Brown34da5f52017-01-10 13:30:21 -06003709 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3710 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3711
Huang Shijiee2985fc2013-05-17 11:17:30 +08003712 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003713 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003714
Huang Shijie10c86ba2013-05-17 11:17:26 +08003715 if (p->ecc_bits != 0xff) {
3716 chip->ecc_strength_ds = p->ecc_bits;
3717 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003718 } else if (chip->onfi_version >= 21 &&
3719 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3720
3721 /*
3722 * The nand_flash_detect_ext_param_page() uses the
3723 * Change Read Column command which maybe not supported
3724 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3725 * now. We do not replace user supplied command function.
3726 */
3727 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3728 chip->cmdfunc = nand_command_lp;
3729
3730 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003731 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003732 pr_warn("Failed to detect ONFI extended param page\n");
3733 } else {
3734 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003735 }
3736
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003737 return 1;
3738}
3739
3740/*
Huang Shijie91361812014-02-21 13:39:40 +08003741 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3742 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003743static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003744{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003745 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003746 struct nand_jedec_params *p = &chip->jedec_params;
3747 struct jedec_ecc_info *ecc;
3748 int val;
3749 int i, j;
3750
3751 /* Try JEDEC for unknown chip or LP */
3752 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3753 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3754 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3755 chip->read_byte(mtd) != 'C')
3756 return 0;
3757
3758 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3759 for (i = 0; i < 3; i++) {
3760 for (j = 0; j < sizeof(*p); j++)
3761 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3762
3763 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3764 le16_to_cpu(p->crc))
3765 break;
3766 }
3767
3768 if (i == 3) {
3769 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3770 return 0;
3771 }
3772
3773 /* Check version */
3774 val = le16_to_cpu(p->revision);
3775 if (val & (1 << 2))
3776 chip->jedec_version = 10;
3777 else if (val & (1 << 1))
3778 chip->jedec_version = 1; /* vendor specific version */
3779
3780 if (!chip->jedec_version) {
3781 pr_info("unsupported JEDEC version: %d\n", val);
3782 return 0;
3783 }
3784
3785 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3786 sanitize_string(p->model, sizeof(p->model));
3787 if (!mtd->name)
3788 mtd->name = p->model;
3789
3790 mtd->writesize = le32_to_cpu(p->byte_per_page);
3791
3792 /* Please reference to the comment for nand_flash_detect_onfi. */
3793 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3794 mtd->erasesize *= mtd->writesize;
3795
3796 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3797
3798 /* Please reference to the comment for nand_flash_detect_onfi. */
3799 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3800 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3801 chip->bits_per_cell = p->bits_per_cell;
3802
3803 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003804 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003805
3806 /* ECC info */
3807 ecc = &p->ecc_info[0];
3808
3809 if (ecc->codeword_size >= 9) {
3810 chip->ecc_strength_ds = ecc->ecc_bits;
3811 chip->ecc_step_ds = 1 << ecc->codeword_size;
3812 } else {
3813 pr_warn("Invalid codeword size\n");
3814 }
3815
3816 return 1;
3817}
3818
3819/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003820 * nand_id_has_period - Check if an ID string has a given wraparound period
3821 * @id_data: the ID string
3822 * @arrlen: the length of the @id_data array
3823 * @period: the period of repitition
3824 *
3825 * Check if an ID string is repeated within a given sequence of bytes at
3826 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003827 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003828 * if the repetition has a period of @period; otherwise, returns zero.
3829 */
3830static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3831{
3832 int i, j;
3833 for (i = 0; i < period; i++)
3834 for (j = i + period; j < arrlen; j += period)
3835 if (id_data[i] != id_data[j])
3836 return 0;
3837 return 1;
3838}
3839
3840/*
3841 * nand_id_len - Get the length of an ID string returned by CMD_READID
3842 * @id_data: the ID string
3843 * @arrlen: the length of the @id_data array
3844
3845 * Returns the length of the ID string, according to known wraparound/trailing
3846 * zero patterns. If no pattern exists, returns the length of the array.
3847 */
3848static int nand_id_len(u8 *id_data, int arrlen)
3849{
3850 int last_nonzero, period;
3851
3852 /* Find last non-zero byte */
3853 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3854 if (id_data[last_nonzero])
3855 break;
3856
3857 /* All zeros */
3858 if (last_nonzero < 0)
3859 return 0;
3860
3861 /* Calculate wraparound period */
3862 for (period = 1; period < arrlen; period++)
3863 if (nand_id_has_period(id_data, arrlen, period))
3864 break;
3865
3866 /* There's a repeated pattern */
3867 if (period < arrlen)
3868 return period;
3869
3870 /* There are trailing zeros */
3871 if (last_nonzero < arrlen - 1)
3872 return last_nonzero + 1;
3873
3874 /* No pattern detected */
3875 return arrlen;
3876}
3877
Huang Shijie7db906b2013-09-25 14:58:11 +08003878/* Extract the bits of per cell from the 3rd byte of the extended ID */
3879static int nand_get_bits_per_cell(u8 cellinfo)
3880{
3881 int bits;
3882
3883 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3884 bits >>= NAND_CI_CELLTYPE_SHIFT;
3885 return bits + 1;
3886}
3887
Brian Norrise3b88bd2012-09-24 20:40:52 -07003888/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003889 * Many new NAND share similar device ID codes, which represent the size of the
3890 * chip. The rest of the parameters must be decoded according to generic or
3891 * manufacturer-specific "extended ID" decoding patterns.
3892 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003893void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003894{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003895 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003896 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003897 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003898 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003899 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003900 /* The 4th id byte is the important one */
3901 extid = id_data[3];
3902
Boris Brezillon01389b62016-06-08 10:30:18 +02003903 /* Calc pagesize */
3904 mtd->writesize = 1024 << (extid & 0x03);
3905 extid >>= 2;
3906 /* Calc oobsize */
3907 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3908 extid >>= 2;
3909 /* Calc blocksize. Blocksize is multiples of 64KiB */
3910 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3911 extid >>= 2;
3912 /* Get buswidth information */
3913 if (extid & 0x1)
3914 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003915}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003916EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003917
3918/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003919 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3920 * decodes a matching ID table entry and assigns the MTD size parameters for
3921 * the chip.
3922 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003923static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003924{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003925 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003926
3927 mtd->erasesize = type->erasesize;
3928 mtd->writesize = type->pagesize;
3929 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003930
Huang Shijie1c195e92013-09-25 14:58:12 +08003931 /* All legacy ID NAND are small-page, SLC */
3932 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003933}
3934
3935/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003936 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3937 * heuristic patterns using various detected parameters (e.g., manufacturer,
3938 * page size, cell-type information).
3939 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003940static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003941{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003942 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003943
3944 /* Set the bad block position */
3945 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3946 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3947 else
3948 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003949}
3950
Huang Shijieec6e87e2013-03-15 11:01:00 +08003951static inline bool is_full_id_nand(struct nand_flash_dev *type)
3952{
3953 return type->id_len;
3954}
3955
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003956static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003957 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003958{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003959 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003960 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003961
Huang Shijieec6e87e2013-03-15 11:01:00 +08003962 if (!strncmp(type->id, id_data, type->id_len)) {
3963 mtd->writesize = type->pagesize;
3964 mtd->erasesize = type->erasesize;
3965 mtd->oobsize = type->oobsize;
3966
Huang Shijie7db906b2013-09-25 14:58:11 +08003967 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003968 chip->chipsize = (uint64_t)type->chipsize << 20;
3969 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003970 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3971 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003972 chip->onfi_timing_mode_default =
3973 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003974
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003975 if (!mtd->name)
3976 mtd->name = type->name;
3977
Huang Shijieec6e87e2013-03-15 11:01:00 +08003978 return true;
3979 }
3980 return false;
3981}
3982
Brian Norris7e74c2d2012-09-24 20:40:49 -07003983/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003984 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3985 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3986 * table.
3987 */
3988static void nand_manufacturer_detect(struct nand_chip *chip)
3989{
3990 /*
3991 * Try manufacturer detection if available and use
3992 * nand_decode_ext_id() otherwise.
3993 */
3994 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3995 chip->manufacturer.desc->ops->detect)
3996 chip->manufacturer.desc->ops->detect(chip);
3997 else
3998 nand_decode_ext_id(chip);
3999}
4000
4001/*
4002 * Manufacturer initialization. This function is called for all NANDs including
4003 * ONFI and JEDEC compliant ones.
4004 * Manufacturer drivers should put all their specific initialization code in
4005 * their ->init() hook.
4006 */
4007static int nand_manufacturer_init(struct nand_chip *chip)
4008{
4009 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4010 !chip->manufacturer.desc->ops->init)
4011 return 0;
4012
4013 return chip->manufacturer.desc->ops->init(chip);
4014}
4015
4016/*
4017 * Manufacturer cleanup. This function is called for all NANDs including
4018 * ONFI and JEDEC compliant ones.
4019 * Manufacturer drivers should put all their specific cleanup code in their
4020 * ->cleanup() hook.
4021 */
4022static void nand_manufacturer_cleanup(struct nand_chip *chip)
4023{
4024 /* Release manufacturer private data */
4025 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4026 chip->manufacturer.desc->ops->cleanup)
4027 chip->manufacturer.desc->ops->cleanup(chip);
4028}
4029
4030/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004031 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004032 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004033static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004034{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004035 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004036 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08004037 int busw;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004038 int i, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004039 u8 *id_data = chip->id.data;
4040 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Karl Beldanef89a882008-09-15 14:37:29 +02004042 /*
4043 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004044 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004045 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004046 nand_reset(chip, 0);
4047
4048 /* Select the device */
4049 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004052 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
4054 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004055 maf_id = chip->read_byte(mtd);
4056 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Brian Norris8b6e50c2011-05-25 14:59:01 -07004058 /*
4059 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004060 * interface concerns can cause random data which looks like a
4061 * possibly credible NAND flash to appear. If the two results do
4062 * not match, ignore the device completely.
4063 */
4064
4065 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4066
Brian Norris4aef9b72012-09-24 20:40:48 -07004067 /* Read entire ID string */
4068 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004069 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004070
Boris Brezillon7f501f02016-05-24 19:20:05 +02004071 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004072 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004073 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004074 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004075 }
4076
Boris Brezillon7f501f02016-05-24 19:20:05 +02004077 chip->id.len = nand_id_len(id_data, 8);
4078
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004079 /* Try to identify manufacturer */
4080 manufacturer = nand_get_manufacturer(maf_id);
4081 chip->manufacturer.desc = manufacturer;
4082
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004083 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004084 type = nand_flash_ids;
4085
Boris Brezillon29a198a2016-05-24 20:17:48 +02004086 /*
4087 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4088 * override it.
4089 * This is required to make sure initial NAND bus width set by the
4090 * NAND controller driver is coherent with the real NAND bus width
4091 * (extracted by auto-detection code).
4092 */
4093 busw = chip->options & NAND_BUSWIDTH_16;
4094
4095 /*
4096 * The flag is only set (never cleared), reset it to its default value
4097 * before starting auto-detection.
4098 */
4099 chip->options &= ~NAND_BUSWIDTH_16;
4100
Huang Shijieec6e87e2013-03-15 11:01:00 +08004101 for (; type->name != NULL; type++) {
4102 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004103 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004104 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004105 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004106 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004107 }
4108 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004109
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004110 chip->onfi_version = 0;
4111 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004112 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004113 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004114 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004115
4116 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004117 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004118 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004119 }
4120
David Woodhouse5e81e882010-02-26 18:32:56 +00004121 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004122 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004123
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004124 if (!mtd->name)
4125 mtd->name = type->name;
4126
Adrian Hunter69423d92008-12-10 13:37:21 +00004127 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004128
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004129 if (!type->pagesize)
4130 nand_manufacturer_detect(chip);
4131 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004132 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004133
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004134 /* Get chip options */
4135 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004136
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004137ident_done:
4138
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004139 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004140 WARN_ON(busw & NAND_BUSWIDTH_16);
4141 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004142 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4143 /*
4144 * Check, if buswidth is correct. Hardware drivers should set
4145 * chip correct!
4146 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004147 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004148 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004149 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4150 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004151 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4152 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004153 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004154 }
4155
Boris Brezillon7f501f02016-05-24 19:20:05 +02004156 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004157
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004158 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004159 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004160 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004161 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004162
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004163 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004164 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004165 if (chip->chipsize & 0xffffffff)
4166 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004167 else {
4168 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4169 chip->chip_shift += 32 - 1;
4170 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004171
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004172 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004173 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004174
Brian Norris8b6e50c2011-05-25 14:59:01 -07004175 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004176 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4177 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004178
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004179 ret = nand_manufacturer_init(chip);
4180 if (ret)
4181 return ret;
4182
Ezequiel Garcia20171642013-11-25 08:30:31 -03004183 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004184 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004185
4186 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004187 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4188 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004189 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004190 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4191 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004192 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004193 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4194 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004195
Rafał Miłecki3755a992014-10-21 00:01:04 +02004196 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004197 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004198 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004199 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004200}
4201
Boris Brezillond48f62b2016-04-01 14:54:32 +02004202static const char * const nand_ecc_modes[] = {
4203 [NAND_ECC_NONE] = "none",
4204 [NAND_ECC_SOFT] = "soft",
4205 [NAND_ECC_HW] = "hw",
4206 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4207 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004208 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004209};
4210
4211static int of_get_nand_ecc_mode(struct device_node *np)
4212{
4213 const char *pm;
4214 int err, i;
4215
4216 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4217 if (err < 0)
4218 return err;
4219
4220 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4221 if (!strcasecmp(pm, nand_ecc_modes[i]))
4222 return i;
4223
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004224 /*
4225 * For backward compatibility we support few obsoleted values that don't
4226 * have their mappings into nand_ecc_modes_t anymore (they were merged
4227 * with other enums).
4228 */
4229 if (!strcasecmp(pm, "soft_bch"))
4230 return NAND_ECC_SOFT;
4231
Boris Brezillond48f62b2016-04-01 14:54:32 +02004232 return -ENODEV;
4233}
4234
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004235static const char * const nand_ecc_algos[] = {
4236 [NAND_ECC_HAMMING] = "hamming",
4237 [NAND_ECC_BCH] = "bch",
4238};
4239
Boris Brezillond48f62b2016-04-01 14:54:32 +02004240static int of_get_nand_ecc_algo(struct device_node *np)
4241{
4242 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004243 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004244
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004245 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4246 if (!err) {
4247 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4248 if (!strcasecmp(pm, nand_ecc_algos[i]))
4249 return i;
4250 return -ENODEV;
4251 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004252
4253 /*
4254 * For backward compatibility we also read "nand-ecc-mode" checking
4255 * for some obsoleted values that were specifying ECC algorithm.
4256 */
4257 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4258 if (err < 0)
4259 return err;
4260
4261 if (!strcasecmp(pm, "soft"))
4262 return NAND_ECC_HAMMING;
4263 else if (!strcasecmp(pm, "soft_bch"))
4264 return NAND_ECC_BCH;
4265
4266 return -ENODEV;
4267}
4268
4269static int of_get_nand_ecc_step_size(struct device_node *np)
4270{
4271 int ret;
4272 u32 val;
4273
4274 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4275 return ret ? ret : val;
4276}
4277
4278static int of_get_nand_ecc_strength(struct device_node *np)
4279{
4280 int ret;
4281 u32 val;
4282
4283 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4284 return ret ? ret : val;
4285}
4286
4287static int of_get_nand_bus_width(struct device_node *np)
4288{
4289 u32 val;
4290
4291 if (of_property_read_u32(np, "nand-bus-width", &val))
4292 return 8;
4293
4294 switch (val) {
4295 case 8:
4296 case 16:
4297 return val;
4298 default:
4299 return -EIO;
4300 }
4301}
4302
4303static bool of_get_nand_on_flash_bbt(struct device_node *np)
4304{
4305 return of_property_read_bool(np, "nand-on-flash-bbt");
4306}
4307
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004308static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004309{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004310 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004311 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004312
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004313 if (!dn)
4314 return 0;
4315
Brian Norris5844fee2015-01-23 00:22:27 -08004316 if (of_get_nand_bus_width(dn) == 16)
4317 chip->options |= NAND_BUSWIDTH_16;
4318
4319 if (of_get_nand_on_flash_bbt(dn))
4320 chip->bbt_options |= NAND_BBT_USE_FLASH;
4321
4322 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004323 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004324 ecc_strength = of_get_nand_ecc_strength(dn);
4325 ecc_step = of_get_nand_ecc_step_size(dn);
4326
Brian Norris5844fee2015-01-23 00:22:27 -08004327 if (ecc_mode >= 0)
4328 chip->ecc.mode = ecc_mode;
4329
Rafał Miłecki79082452016-03-23 11:19:02 +01004330 if (ecc_algo >= 0)
4331 chip->ecc.algo = ecc_algo;
4332
Brian Norris5844fee2015-01-23 00:22:27 -08004333 if (ecc_strength >= 0)
4334 chip->ecc.strength = ecc_strength;
4335
4336 if (ecc_step > 0)
4337 chip->ecc.size = ecc_step;
4338
Boris Brezillonba78ee02016-06-08 17:04:22 +02004339 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4340 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4341
Brian Norris5844fee2015-01-23 00:22:27 -08004342 return 0;
4343}
4344
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004345/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004346 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004347 * @mtd: MTD device structure
4348 * @maxchips: number of chips to scan for
4349 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004350 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004351 * This is the first phase of the normal nand_scan() function. It reads the
4352 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004353 *
4354 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004355int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4356 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004357{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004358 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004359 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004360 int ret;
4361
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004362 ret = nand_dt_init(chip);
4363 if (ret)
4364 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004365
Brian Norrisf7a8e382016-01-05 10:39:45 -08004366 if (!mtd->name && mtd->dev.parent)
4367 mtd->name = dev_name(mtd->dev.parent);
4368
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004369 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4370 /*
4371 * Default functions assigned for chip_select() and
4372 * cmdfunc() both expect cmd_ctrl() to be populated,
4373 * so we need to check that that's the case
4374 */
4375 pr_err("chip.cmd_ctrl() callback is not provided");
4376 return -EINVAL;
4377 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004378 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004379 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004380
4381 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004382 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004383 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004384 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004385 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004386 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
4389
Boris Brezillon73f907f2016-10-24 16:46:20 +02004390 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004391 ret = nand_init_data_interface(chip);
4392 if (ret)
4393 return ret;
4394
Boris Brezillon73f907f2016-10-24 16:46:20 +02004395 /*
4396 * Setup the data interface correctly on the chip and controller side.
4397 * This explicit call to nand_setup_data_interface() is only required
4398 * for the first die, because nand_reset() has been called before
4399 * ->data_interface and ->default_onfi_timing_mode were set.
4400 * For the other dies, nand_reset() will automatically switch to the
4401 * best mode for us.
4402 */
Boris Brezillon104e4422017-03-16 09:35:58 +01004403 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004404 if (ret)
4405 return ret;
4406
Boris Brezillon7f501f02016-05-24 19:20:05 +02004407 nand_maf_id = chip->id.data[0];
4408 nand_dev_id = chip->id.data[1];
4409
Huang Shijie07300162012-11-09 16:23:45 +08004410 chip->select_chip(mtd, -1);
4411
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004412 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004413 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004414 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004415 nand_reset(chip, i);
4416
4417 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004419 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004421 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004422 nand_dev_id != chip->read_byte(mtd)) {
4423 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 break;
Huang Shijie07300162012-11-09 16:23:45 +08004425 }
4426 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004429 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004432 chip->numchips = i;
4433 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
David Woodhouse3b85c322006-09-25 17:06:53 +01004435 return 0;
4436}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004437EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004438
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004439static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4440{
4441 struct nand_chip *chip = mtd_to_nand(mtd);
4442 struct nand_ecc_ctrl *ecc = &chip->ecc;
4443
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004444 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004445 return -EINVAL;
4446
4447 switch (ecc->algo) {
4448 case NAND_ECC_HAMMING:
4449 ecc->calculate = nand_calculate_ecc;
4450 ecc->correct = nand_correct_data;
4451 ecc->read_page = nand_read_page_swecc;
4452 ecc->read_subpage = nand_read_subpage;
4453 ecc->write_page = nand_write_page_swecc;
4454 ecc->read_page_raw = nand_read_page_raw;
4455 ecc->write_page_raw = nand_write_page_raw;
4456 ecc->read_oob = nand_read_oob_std;
4457 ecc->write_oob = nand_write_oob_std;
4458 if (!ecc->size)
4459 ecc->size = 256;
4460 ecc->bytes = 3;
4461 ecc->strength = 1;
4462 return 0;
4463 case NAND_ECC_BCH:
4464 if (!mtd_nand_has_bch()) {
4465 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4466 return -EINVAL;
4467 }
4468 ecc->calculate = nand_bch_calculate_ecc;
4469 ecc->correct = nand_bch_correct_data;
4470 ecc->read_page = nand_read_page_swecc;
4471 ecc->read_subpage = nand_read_subpage;
4472 ecc->write_page = nand_write_page_swecc;
4473 ecc->read_page_raw = nand_read_page_raw;
4474 ecc->write_page_raw = nand_write_page_raw;
4475 ecc->read_oob = nand_read_oob_std;
4476 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004477
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004478 /*
4479 * Board driver should supply ecc.size and ecc.strength
4480 * values to select how many bits are correctable.
4481 * Otherwise, default to 4 bits for large page devices.
4482 */
4483 if (!ecc->size && (mtd->oobsize >= 64)) {
4484 ecc->size = 512;
4485 ecc->strength = 4;
4486 }
4487
4488 /*
4489 * if no ecc placement scheme was provided pickup the default
4490 * large page one.
4491 */
4492 if (!mtd->ooblayout) {
4493 /* handle large page devices only */
4494 if (mtd->oobsize < 64) {
4495 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4496 return -EINVAL;
4497 }
4498
4499 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004500
4501 }
4502
4503 /*
4504 * We can only maximize ECC config when the default layout is
4505 * used, otherwise we don't know how many bytes can really be
4506 * used.
4507 */
4508 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4509 ecc->options & NAND_ECC_MAXIMIZE) {
4510 int steps, bytes;
4511
4512 /* Always prefer 1k blocks over 512bytes ones */
4513 ecc->size = 1024;
4514 steps = mtd->writesize / ecc->size;
4515
4516 /* Reserve 2 bytes for the BBM */
4517 bytes = (mtd->oobsize - 2) / steps;
4518 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004519 }
4520
4521 /* See nand_bch_init() for details. */
4522 ecc->bytes = 0;
4523 ecc->priv = nand_bch_init(mtd);
4524 if (!ecc->priv) {
4525 WARN(1, "BCH ECC initialization failed!\n");
4526 return -EINVAL;
4527 }
4528 return 0;
4529 default:
4530 WARN(1, "Unsupported ECC algorithm!\n");
4531 return -EINVAL;
4532 }
4533}
4534
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004535/*
4536 * Check if the chip configuration meet the datasheet requirements.
4537
4538 * If our configuration corrects A bits per B bytes and the minimum
4539 * required correction level is X bits per Y bytes, then we must ensure
4540 * both of the following are true:
4541 *
4542 * (1) A / B >= X / Y
4543 * (2) A >= X
4544 *
4545 * Requirement (1) ensures we can correct for the required bitflip density.
4546 * Requirement (2) ensures we can correct even when all bitflips are clumped
4547 * in the same sector.
4548 */
4549static bool nand_ecc_strength_good(struct mtd_info *mtd)
4550{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004551 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004552 struct nand_ecc_ctrl *ecc = &chip->ecc;
4553 int corr, ds_corr;
4554
4555 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4556 /* Not enough information */
4557 return true;
4558
4559 /*
4560 * We get the number of corrected bits per page to compare
4561 * the correction density.
4562 */
4563 corr = (mtd->writesize * ecc->strength) / ecc->size;
4564 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4565
4566 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4567}
David Woodhouse3b85c322006-09-25 17:06:53 +01004568
Marc Gonzalez3371d662016-11-15 10:56:20 +01004569static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4570{
4571 struct nand_ecc_ctrl *ecc = &chip->ecc;
4572
4573 if (nand_standard_page_accessors(ecc))
4574 return false;
4575
4576 /*
4577 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4578 * controller driver implements all the page accessors because
4579 * default helpers are not suitable when the core does not
4580 * send the READ0/PAGEPROG commands.
4581 */
4582 return (!ecc->read_page || !ecc->write_page ||
4583 !ecc->read_page_raw || !ecc->write_page_raw ||
4584 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4585 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4586 ecc->hwctl && ecc->calculate));
4587}
4588
David Woodhouse3b85c322006-09-25 17:06:53 +01004589/**
4590 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004591 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004592 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004593 * This is the second phase of the normal nand_scan() function. It fills out
4594 * all the uninitialized function pointers with the defaults and scans for a
4595 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004596 */
4597int nand_scan_tail(struct mtd_info *mtd)
4598{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004599 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004600 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004601 struct nand_buffers *nbuf = NULL;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004602 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004603
Brian Norrise2414f42012-02-06 13:44:00 -08004604 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004605 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4606 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4607 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004608
Marc Gonzalez3371d662016-11-15 10:56:20 +01004609 if (invalid_ecc_page_accessors(chip)) {
4610 pr_err("Invalid ECC page accessors setup\n");
4611 return -EINVAL;
4612 }
4613
Huang Shijief02ea4e2014-01-13 14:27:12 +08004614 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004615 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Huang Shijief02ea4e2014-01-13 14:27:12 +08004616 if (!nbuf)
4617 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004618
4619 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4620 if (!nbuf->ecccalc) {
4621 ret = -ENOMEM;
4622 goto err_free;
4623 }
4624
4625 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4626 if (!nbuf->ecccode) {
4627 ret = -ENOMEM;
4628 goto err_free;
4629 }
4630
4631 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4632 GFP_KERNEL);
4633 if (!nbuf->databuf) {
4634 ret = -ENOMEM;
4635 goto err_free;
4636 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004637
4638 chip->buffers = nbuf;
4639 } else {
4640 if (!chip->buffers)
4641 return -ENOMEM;
4642 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004643
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004644 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004645 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004646
4647 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004648 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004649 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004650 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004651 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004652 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004655 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 break;
4657 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004658 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004659 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004662 WARN(1, "No oob scheme defined for oobsize %d\n",
4663 mtd->oobsize);
4664 ret = -EINVAL;
4665 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
4667 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004668
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004669 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004670 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004671 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004672 */
David Woodhouse956e9442006-09-25 17:12:39 +01004673
Huang Shijie97de79e02013-10-18 14:20:53 +08004674 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004675 case NAND_ECC_HW_OOB_FIRST:
4676 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004677 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004678 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4679 ret = -EINVAL;
4680 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004681 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004682 if (!ecc->read_page)
4683 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004684
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004685 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004686 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004687 if (!ecc->read_page)
4688 ecc->read_page = nand_read_page_hwecc;
4689 if (!ecc->write_page)
4690 ecc->write_page = nand_write_page_hwecc;
4691 if (!ecc->read_page_raw)
4692 ecc->read_page_raw = nand_read_page_raw;
4693 if (!ecc->write_page_raw)
4694 ecc->write_page_raw = nand_write_page_raw;
4695 if (!ecc->read_oob)
4696 ecc->read_oob = nand_read_oob_std;
4697 if (!ecc->write_oob)
4698 ecc->write_oob = nand_write_oob_std;
4699 if (!ecc->read_subpage)
4700 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004701 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004702 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004703
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004704 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004705 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4706 (!ecc->read_page ||
4707 ecc->read_page == nand_read_page_hwecc ||
4708 !ecc->write_page ||
4709 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004710 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4711 ret = -EINVAL;
4712 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004713 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004714 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004715 if (!ecc->read_page)
4716 ecc->read_page = nand_read_page_syndrome;
4717 if (!ecc->write_page)
4718 ecc->write_page = nand_write_page_syndrome;
4719 if (!ecc->read_page_raw)
4720 ecc->read_page_raw = nand_read_page_raw_syndrome;
4721 if (!ecc->write_page_raw)
4722 ecc->write_page_raw = nand_write_page_raw_syndrome;
4723 if (!ecc->read_oob)
4724 ecc->read_oob = nand_read_oob_syndrome;
4725 if (!ecc->write_oob)
4726 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004727
Huang Shijie97de79e02013-10-18 14:20:53 +08004728 if (mtd->writesize >= ecc->size) {
4729 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004730 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4731 ret = -EINVAL;
4732 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004733 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004734 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004735 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004736 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4737 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004738 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004739 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004741 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004742 ret = nand_set_ecc_soft_ops(mtd);
4743 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004744 ret = -EINVAL;
4745 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004746 }
4747 break;
4748
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004749 case NAND_ECC_ON_DIE:
4750 if (!ecc->read_page || !ecc->write_page) {
4751 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4752 ret = -EINVAL;
4753 goto err_free;
4754 }
4755 if (!ecc->read_oob)
4756 ecc->read_oob = nand_read_oob_std;
4757 if (!ecc->write_oob)
4758 ecc->write_oob = nand_write_oob_std;
4759 break;
4760
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004761 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004762 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004763 ecc->read_page = nand_read_page_raw;
4764 ecc->write_page = nand_write_page_raw;
4765 ecc->read_oob = nand_read_oob_std;
4766 ecc->read_page_raw = nand_read_page_raw;
4767 ecc->write_page_raw = nand_write_page_raw;
4768 ecc->write_oob = nand_write_oob_std;
4769 ecc->size = mtd->writesize;
4770 ecc->bytes = 0;
4771 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004775 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4776 ret = -EINVAL;
4777 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Brian Norris9ce244b2011-08-30 18:45:37 -07004780 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004781 if (!ecc->read_oob_raw)
4782 ecc->read_oob_raw = ecc->read_oob;
4783 if (!ecc->write_oob_raw)
4784 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004785
Boris Brezillon846031d2016-02-03 20:11:00 +01004786 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004787 mtd->ecc_strength = ecc->strength;
4788 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004789
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004790 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004791 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004792 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004793 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004794 ecc->steps = mtd->writesize / ecc->size;
4795 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004796 WARN(1, "Invalid ECC parameters\n");
4797 ret = -EINVAL;
4798 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004800 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004801
Boris Brezillon846031d2016-02-03 20:11:00 +01004802 /*
4803 * The number of bytes available for a client to place data into
4804 * the out of band area.
4805 */
4806 ret = mtd_ooblayout_count_freebytes(mtd);
4807 if (ret < 0)
4808 ret = 0;
4809
4810 mtd->oobavail = ret;
4811
4812 /* ECC sanity check: warn if it's too weak */
4813 if (!nand_ecc_strength_good(mtd))
4814 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4815 mtd->name);
4816
Brian Norris8b6e50c2011-05-25 14:59:01 -07004817 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004818 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004819 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004820 case 2:
4821 mtd->subpage_sft = 1;
4822 break;
4823 case 4:
4824 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004825 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004826 mtd->subpage_sft = 2;
4827 break;
4828 }
4829 }
4830 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4831
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004832 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004833 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004836 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004838 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304839 switch (ecc->mode) {
4840 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304841 if (chip->page_shift > 9)
4842 chip->options |= NAND_SUBPAGE_READ;
4843 break;
4844
4845 default:
4846 break;
4847 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004850 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004851 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4852 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004853 mtd->_erase = nand_erase;
4854 mtd->_point = NULL;
4855 mtd->_unpoint = NULL;
4856 mtd->_read = nand_read;
4857 mtd->_write = nand_write;
4858 mtd->_panic_write = panic_nand_write;
4859 mtd->_read_oob = nand_read_oob;
4860 mtd->_write_oob = nand_write_oob;
4861 mtd->_sync = nand_sync;
4862 mtd->_lock = NULL;
4863 mtd->_unlock = NULL;
4864 mtd->_suspend = nand_suspend;
4865 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004866 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004867 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004868 mtd->_block_isbad = nand_block_isbad;
4869 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004870 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004871 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004873 /*
4874 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4875 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4876 * properly set.
4877 */
4878 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004879 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004881 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004882 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004886 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004887err_free:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004888 if (nbuf) {
4889 kfree(nbuf->databuf);
4890 kfree(nbuf->ecccode);
4891 kfree(nbuf->ecccalc);
4892 kfree(nbuf);
4893 }
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004894 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004896EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Brian Norris8b6e50c2011-05-25 14:59:01 -07004898/*
4899 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004900 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004901 * to call us from in-kernel code if the core NAND support is modular.
4902 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004903#ifdef MODULE
4904#define caller_is_module() (1)
4905#else
4906#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004907 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004908#endif
4909
4910/**
4911 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004912 * @mtd: MTD device structure
4913 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004914 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004915 * This fills out all the uninitialized function pointers with the defaults.
4916 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004917 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004918 */
4919int nand_scan(struct mtd_info *mtd, int maxchips)
4920{
4921 int ret;
4922
David Woodhouse5e81e882010-02-26 18:32:56 +00004923 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004924 if (!ret)
4925 ret = nand_scan_tail(mtd);
4926 return ret;
4927}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004928EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004931 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4932 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004933 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004934void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004936 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004937 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004938 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4939
Boris Brezillond8e725d2016-09-15 10:32:50 +02004940 nand_release_data_interface(chip);
4941
Jesper Juhlfa671642005-11-07 01:01:27 -08004942 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004943 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004944 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
4945 kfree(chip->buffers->databuf);
4946 kfree(chip->buffers->ecccode);
4947 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004948 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004949 }
Brian Norris58373ff2010-07-15 12:15:44 -07004950
4951 /* Free bad block descriptor memory */
4952 if (chip->badblock_pattern && chip->badblock_pattern->options
4953 & NAND_BBT_DYNAMICSTRUCT)
4954 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004955
4956 /* Free manufacturer priv data. */
4957 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004959EXPORT_SYMBOL_GPL(nand_cleanup);
4960
4961/**
4962 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4963 * held by the NAND device
4964 * @mtd: MTD device structure
4965 */
4966void nand_release(struct mtd_info *mtd)
4967{
4968 mtd_device_unregister(mtd);
4969 nand_cleanup(mtd_to_nand(mtd));
4970}
David Woodhousee0c7d762006-05-13 18:07:53 +01004971EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004972
David Woodhousee0c7d762006-05-13 18:07:53 +01004973MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004974MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4975MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004976MODULE_DESCRIPTION("Generic NAND flash driver code");