blob: 2404bb046b69aa1dee9bd3e1aa259109723b5ab9 [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 */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200756 case NAND_CMD_READ0:
757 /*
758 * READ0 is sometimes used to exit GET STATUS mode. When this
759 * is the case no address cycles are requested, and we can use
760 * this information to detect that we should not wait for the
761 * device to be ready.
762 */
763 if (column == -1 && page_addr == -1)
764 return;
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000767 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * If we don't have access to the busy pin, we apply the given
769 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100770 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200771 if (!chip->dev_ready) {
772 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700776 /*
777 * Apply this short delay always to ensure that we do wait tWB in
778 * any case on any machine.
779 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100780 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000781
782 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200785static void nand_ccs_delay(struct nand_chip *chip)
786{
787 /*
788 * The controller already takes care of waiting for tCCS when the RNDIN
789 * or RNDOUT command is sent, return directly.
790 */
791 if (!(chip->options & NAND_WAIT_TCCS))
792 return;
793
794 /*
795 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
796 * (which should be safe for all NANDs).
797 */
798 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
799 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
800 else
801 ndelay(500);
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804/**
805 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700806 * @mtd: MTD device structure
807 * @command: the command to be sent
808 * @column: the column address for this command, -1 if none
809 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200811 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700812 * devices. We don't have the separate regions as we have in the small page
813 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200815static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
816 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100818 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* Emulate NAND_CMD_READOOB */
821 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200822 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 command = NAND_CMD_READ0;
824 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000825
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200826 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400827 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200830 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /* Serially input address */
833 if (column != -1) {
834 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800835 if (chip->options & NAND_BUSWIDTH_16 &&
836 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200838 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200839 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200840
Brian Norrisf5b88de2016-10-03 09:49:35 -0700841 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200842 if (!nand_opcode_8bits(command))
843 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200846 chip->cmd_ctrl(mtd, page_addr, ctrl);
847 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200848 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200850 if (chip->chipsize > (128 << 20))
851 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200852 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200855 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000856
857 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700858 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100859 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 case NAND_CMD_CACHEDPROG:
864 case NAND_CMD_PAGEPROG:
865 case NAND_CMD_ERASE1:
866 case NAND_CMD_ERASE2:
867 case NAND_CMD_SEQIN:
868 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900869 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900870 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000871 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200873 case NAND_CMD_RNDIN:
874 nand_ccs_delay(chip);
875 return;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200878 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200880 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200881 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
882 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
883 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
884 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200885 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
886 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return;
888
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200889 case NAND_CMD_RNDOUT:
890 /* No ready / busy check necessary */
891 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
892 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
893 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
894 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200895
896 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200897 return;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200900 /*
901 * READ0 is sometimes used to exit GET STATUS mode. When this
902 * is the case no address cycles are requested, and we can use
903 * this information to detect that READSTART should not be
904 * issued.
905 */
906 if (column == -1 && page_addr == -1)
907 return;
908
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200909 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
910 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
911 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
912 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000913
David Woodhousee0c7d762006-05-13 18:07:53 +0100914 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000916 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700918 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100919 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200920 if (!chip->dev_ready) {
921 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000925
Brian Norris8b6e50c2011-05-25 14:59:01 -0700926 /*
927 * Apply this short delay always to ensure that we do wait tWB in
928 * any case on any machine.
929 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100930 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000931
932 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200936 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700937 * @chip: the nand chip descriptor
938 * @mtd: MTD device structure
939 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200940 *
941 * Used when in panic, no locks are taken.
942 */
943static void panic_nand_get_device(struct nand_chip *chip,
944 struct mtd_info *mtd, int new_state)
945{
Brian Norris7854d3f2011-06-23 14:12:08 -0700946 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200947 chip->controller->active = chip;
948 chip->state = new_state;
949}
950
951/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700953 * @mtd: MTD device structure
954 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *
956 * Get the device and lock it for exclusive access
957 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200958static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800959nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100961 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200962 spinlock_t *lock = &chip->controller->lock;
963 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100964 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200965retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100966 spin_lock(lock);
967
vimal singhb8b3ee92009-07-09 20:41:22 +0530968 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200969 if (!chip->controller->active)
970 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200971
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200972 if (chip->controller->active == chip && chip->state == FL_READY) {
973 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100974 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100975 return 0;
976 }
977 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800978 if (chip->controller->active->state == FL_PM_SUSPENDED) {
979 chip->state = FL_PM_SUSPENDED;
980 spin_unlock(lock);
981 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800982 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100983 }
984 set_current_state(TASK_UNINTERRUPTIBLE);
985 add_wait_queue(wq, &wait);
986 spin_unlock(lock);
987 schedule();
988 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto retry;
990}
991
992/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700993 * panic_nand_wait - [GENERIC] wait until the command is done
994 * @mtd: MTD device structure
995 * @chip: NAND chip structure
996 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200997 *
998 * Wait for command done. This is a helper function for nand_wait used when
999 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001000 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001001 */
1002static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1003 unsigned long timeo)
1004{
1005 int i;
1006 for (i = 0; i < timeo; i++) {
1007 if (chip->dev_ready) {
1008 if (chip->dev_ready(mtd))
1009 break;
1010 } else {
1011 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1012 break;
1013 }
1014 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001015 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001016}
1017
1018/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001019 * nand_wait - [DEFAULT] wait until the command is done
1020 * @mtd: MTD device structure
1021 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001023 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001024 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001025static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027
Alex Smithb70af9b2015-10-06 14:52:07 +01001028 int status;
1029 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Brian Norris8b6e50c2011-05-25 14:59:01 -07001031 /*
1032 * Apply this short delay always to ensure that we do wait tWB in any
1033 * case on any machine.
1034 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001035 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001037 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001039 if (in_interrupt() || oops_in_progress)
1040 panic_nand_wait(mtd, chip, timeo);
1041 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001042 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001043 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001044 if (chip->dev_ready) {
1045 if (chip->dev_ready(mtd))
1046 break;
1047 } else {
1048 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1049 break;
1050 }
1051 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001052 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001054
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001055 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001056 /* This can happen if in case of timeout or buggy dev_ready */
1057 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return status;
1059}
1060
1061/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001062 * nand_reset_data_interface - Reset data interface and timings
1063 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001064 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001065 *
1066 * Reset the Data interface and timings to ONFI mode 0.
1067 *
1068 * Returns 0 for success or negative error code otherwise.
1069 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001070static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001071{
1072 struct mtd_info *mtd = nand_to_mtd(chip);
1073 const struct nand_data_interface *conf;
1074 int ret;
1075
1076 if (!chip->setup_data_interface)
1077 return 0;
1078
1079 /*
1080 * The ONFI specification says:
1081 * "
1082 * To transition from NV-DDR or NV-DDR2 to the SDR data
1083 * interface, the host shall use the Reset (FFh) command
1084 * using SDR timing mode 0. A device in any timing mode is
1085 * required to recognize Reset (FFh) command issued in SDR
1086 * timing mode 0.
1087 * "
1088 *
1089 * Configure the data interface in SDR mode and set the
1090 * timings to timing mode 0.
1091 */
1092
1093 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001094 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001095 if (ret)
1096 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1097
1098 return ret;
1099}
1100
1101/**
1102 * nand_setup_data_interface - Setup the best data interface and timings
1103 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001104 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001105 *
1106 * Find and configure the best data interface and NAND timings supported by
1107 * the chip and the driver.
1108 * First tries to retrieve supported timing modes from ONFI information,
1109 * and if the NAND chip does not support ONFI, relies on the
1110 * ->onfi_timing_mode_default specified in the nand_ids table.
1111 *
1112 * Returns 0 for success or negative error code otherwise.
1113 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001114static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001115{
1116 struct mtd_info *mtd = nand_to_mtd(chip);
1117 int ret;
1118
1119 if (!chip->setup_data_interface || !chip->data_interface)
1120 return 0;
1121
1122 /*
1123 * Ensure the timing mode has been changed on the chip side
1124 * before changing timings on the controller side.
1125 */
1126 if (chip->onfi_version) {
1127 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1128 chip->onfi_timing_mode_default,
1129 };
1130
1131 ret = chip->onfi_set_features(mtd, chip,
1132 ONFI_FEATURE_ADDR_TIMING_MODE,
1133 tmode_param);
1134 if (ret)
1135 goto err;
1136 }
1137
Boris Brezillon104e4422017-03-16 09:35:58 +01001138 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001139err:
1140 return ret;
1141}
1142
1143/**
1144 * nand_init_data_interface - find the best data interface and timings
1145 * @chip: The NAND chip
1146 *
1147 * Find the best data interface and NAND timings supported by the chip
1148 * and the driver.
1149 * First tries to retrieve supported timing modes from ONFI information,
1150 * and if the NAND chip does not support ONFI, relies on the
1151 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1152 * function nand_chip->data_interface is initialized with the best timing mode
1153 * available.
1154 *
1155 * Returns 0 for success or negative error code otherwise.
1156 */
1157static int nand_init_data_interface(struct nand_chip *chip)
1158{
1159 struct mtd_info *mtd = nand_to_mtd(chip);
1160 int modes, mode, ret;
1161
1162 if (!chip->setup_data_interface)
1163 return 0;
1164
1165 /*
1166 * First try to identify the best timings from ONFI parameters and
1167 * if the NAND does not support ONFI, fallback to the default ONFI
1168 * timing mode.
1169 */
1170 modes = onfi_get_async_timing_mode(chip);
1171 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1172 if (!chip->onfi_timing_mode_default)
1173 return 0;
1174
1175 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1176 }
1177
1178 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1179 GFP_KERNEL);
1180 if (!chip->data_interface)
1181 return -ENOMEM;
1182
1183 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1184 ret = onfi_init_data_interface(chip, chip->data_interface,
1185 NAND_SDR_IFACE, mode);
1186 if (ret)
1187 continue;
1188
Boris Brezillon104e4422017-03-16 09:35:58 +01001189 /* Pass -1 to only */
1190 ret = chip->setup_data_interface(mtd,
1191 NAND_DATA_IFACE_CHECK_ONLY,
1192 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001193 if (!ret) {
1194 chip->onfi_timing_mode_default = mode;
1195 break;
1196 }
1197 }
1198
1199 return 0;
1200}
1201
1202static void nand_release_data_interface(struct nand_chip *chip)
1203{
1204 kfree(chip->data_interface);
1205}
1206
1207/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001208 * nand_reset - Reset and initialize a NAND device
1209 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001210 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001211 *
1212 * Returns 0 for success or negative error code otherwise
1213 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001214int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001215{
1216 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001217 int ret;
1218
Boris Brezillon104e4422017-03-16 09:35:58 +01001219 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001220 if (ret)
1221 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001222
Boris Brezillon73f907f2016-10-24 16:46:20 +02001223 /*
1224 * The CS line has to be released before we can apply the new NAND
1225 * interface settings, hence this weird ->select_chip() dance.
1226 */
1227 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001228 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001229 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001230
Boris Brezillon73f907f2016-10-24 16:46:20 +02001231 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001232 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001233 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001234 if (ret)
1235 return ret;
1236
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001237 return 0;
1238}
1239
1240/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001241 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001242 * @mtd: mtd info
1243 * @ofs: offset to start unlock from
1244 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001245 * @invert: when = 0, unlock the range of blocks within the lower and
1246 * upper boundary address
1247 * when = 1, unlock the range of blocks outside the boundaries
1248 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301249 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001250 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301251 */
1252static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1253 uint64_t len, int invert)
1254{
1255 int ret = 0;
1256 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001257 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301258
1259 /* Submit address of first page to unlock */
1260 page = ofs >> chip->page_shift;
1261 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1262
1263 /* Submit address of last page to unlock */
1264 page = (ofs + len) >> chip->page_shift;
1265 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1266 (page | invert) & chip->pagemask);
1267
1268 /* Call wait ready function */
1269 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301270 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001271 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001272 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301273 __func__, status);
1274 ret = -EIO;
1275 }
1276
1277 return ret;
1278}
1279
1280/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001281 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001282 * @mtd: mtd info
1283 * @ofs: offset to start unlock from
1284 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301285 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001286 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301287 */
1288int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1289{
1290 int ret = 0;
1291 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001292 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301293
Brian Norris289c0522011-07-19 10:06:09 -07001294 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301295 __func__, (unsigned long long)ofs, len);
1296
1297 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001298 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301299
1300 /* Align to last block address if size addresses end of the device */
1301 if (ofs + len == mtd->size)
1302 len -= mtd->erasesize;
1303
Huang Shijie6a8214a2012-11-19 14:43:30 +08001304 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301305
1306 /* Shift to get chip number */
1307 chipnr = ofs >> chip->chip_shift;
1308
White Ding57d3a9a2014-07-24 00:10:45 +08001309 /*
1310 * Reset the chip.
1311 * If we want to check the WP through READ STATUS and check the bit 7
1312 * we must reset the chip
1313 * some operation can also clear the bit 7 of status register
1314 * eg. erase/program a locked block
1315 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001316 nand_reset(chip, chipnr);
1317
1318 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001319
Vimal Singh7d70f332010-02-08 15:50:49 +05301320 /* Check, if it is write protected */
1321 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001322 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301323 __func__);
1324 ret = -EIO;
1325 goto out;
1326 }
1327
1328 ret = __nand_unlock(mtd, ofs, len, 0);
1329
1330out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001331 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301332 nand_release_device(mtd);
1333
1334 return ret;
1335}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001336EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301337
1338/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001339 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001340 * @mtd: mtd info
1341 * @ofs: offset to start unlock from
1342 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301343 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001344 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1345 * have this feature, but it allows only to lock all blocks, not for specified
1346 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1347 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301348 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001349 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301350 */
1351int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1352{
1353 int ret = 0;
1354 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001355 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301356
Brian Norris289c0522011-07-19 10:06:09 -07001357 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301358 __func__, (unsigned long long)ofs, len);
1359
1360 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001361 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301362
Huang Shijie6a8214a2012-11-19 14:43:30 +08001363 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301364
1365 /* Shift to get chip number */
1366 chipnr = ofs >> chip->chip_shift;
1367
White Ding57d3a9a2014-07-24 00:10:45 +08001368 /*
1369 * Reset the chip.
1370 * If we want to check the WP through READ STATUS and check the bit 7
1371 * we must reset the chip
1372 * some operation can also clear the bit 7 of status register
1373 * eg. erase/program a locked block
1374 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001375 nand_reset(chip, chipnr);
1376
1377 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001378
Vimal Singh7d70f332010-02-08 15:50:49 +05301379 /* Check, if it is write protected */
1380 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001381 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301382 __func__);
1383 status = MTD_ERASE_FAILED;
1384 ret = -EIO;
1385 goto out;
1386 }
1387
1388 /* Submit address of first page to lock */
1389 page = ofs >> chip->page_shift;
1390 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1391
1392 /* Call wait ready function */
1393 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301394 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001395 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001396 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301397 __func__, status);
1398 ret = -EIO;
1399 goto out;
1400 }
1401
1402 ret = __nand_unlock(mtd, ofs, len, 0x1);
1403
1404out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001405 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301406 nand_release_device(mtd);
1407
1408 return ret;
1409}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001410EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301411
1412/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001413 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1414 * @buf: buffer to test
1415 * @len: buffer length
1416 * @bitflips_threshold: maximum number of bitflips
1417 *
1418 * Check if a buffer contains only 0xff, which means the underlying region
1419 * has been erased and is ready to be programmed.
1420 * The bitflips_threshold specify the maximum number of bitflips before
1421 * considering the region is not erased.
1422 * Note: The logic of this function has been extracted from the memweight
1423 * implementation, except that nand_check_erased_buf function exit before
1424 * testing the whole buffer if the number of bitflips exceed the
1425 * bitflips_threshold value.
1426 *
1427 * Returns a positive number of bitflips less than or equal to
1428 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1429 * threshold.
1430 */
1431static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1432{
1433 const unsigned char *bitmap = buf;
1434 int bitflips = 0;
1435 int weight;
1436
1437 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1438 len--, bitmap++) {
1439 weight = hweight8(*bitmap);
1440 bitflips += BITS_PER_BYTE - weight;
1441 if (unlikely(bitflips > bitflips_threshold))
1442 return -EBADMSG;
1443 }
1444
1445 for (; len >= sizeof(long);
1446 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001447 unsigned long d = *((unsigned long *)bitmap);
1448 if (d == ~0UL)
1449 continue;
1450 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001451 bitflips += BITS_PER_LONG - weight;
1452 if (unlikely(bitflips > bitflips_threshold))
1453 return -EBADMSG;
1454 }
1455
1456 for (; len > 0; len--, bitmap++) {
1457 weight = hweight8(*bitmap);
1458 bitflips += BITS_PER_BYTE - weight;
1459 if (unlikely(bitflips > bitflips_threshold))
1460 return -EBADMSG;
1461 }
1462
1463 return bitflips;
1464}
1465
1466/**
1467 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1468 * 0xff data
1469 * @data: data buffer to test
1470 * @datalen: data length
1471 * @ecc: ECC buffer
1472 * @ecclen: ECC length
1473 * @extraoob: extra OOB buffer
1474 * @extraooblen: extra OOB length
1475 * @bitflips_threshold: maximum number of bitflips
1476 *
1477 * Check if a data buffer and its associated ECC and OOB data contains only
1478 * 0xff pattern, which means the underlying region has been erased and is
1479 * ready to be programmed.
1480 * The bitflips_threshold specify the maximum number of bitflips before
1481 * considering the region as not erased.
1482 *
1483 * Note:
1484 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1485 * different from the NAND page size. When fixing bitflips, ECC engines will
1486 * report the number of errors per chunk, and the NAND core infrastructure
1487 * expect you to return the maximum number of bitflips for the whole page.
1488 * This is why you should always use this function on a single chunk and
1489 * not on the whole page. After checking each chunk you should update your
1490 * max_bitflips value accordingly.
1491 * 2/ When checking for bitflips in erased pages you should not only check
1492 * the payload data but also their associated ECC data, because a user might
1493 * have programmed almost all bits to 1 but a few. In this case, we
1494 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1495 * this case.
1496 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1497 * data are protected by the ECC engine.
1498 * It could also be used if you support subpages and want to attach some
1499 * extra OOB data to an ECC chunk.
1500 *
1501 * Returns a positive number of bitflips less than or equal to
1502 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1503 * threshold. In case of success, the passed buffers are filled with 0xff.
1504 */
1505int nand_check_erased_ecc_chunk(void *data, int datalen,
1506 void *ecc, int ecclen,
1507 void *extraoob, int extraooblen,
1508 int bitflips_threshold)
1509{
1510 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1511
1512 data_bitflips = nand_check_erased_buf(data, datalen,
1513 bitflips_threshold);
1514 if (data_bitflips < 0)
1515 return data_bitflips;
1516
1517 bitflips_threshold -= data_bitflips;
1518
1519 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1520 if (ecc_bitflips < 0)
1521 return ecc_bitflips;
1522
1523 bitflips_threshold -= ecc_bitflips;
1524
1525 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1526 bitflips_threshold);
1527 if (extraoob_bitflips < 0)
1528 return extraoob_bitflips;
1529
1530 if (data_bitflips)
1531 memset(data, 0xff, datalen);
1532
1533 if (ecc_bitflips)
1534 memset(ecc, 0xff, ecclen);
1535
1536 if (extraoob_bitflips)
1537 memset(extraoob, 0xff, extraooblen);
1538
1539 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1540}
1541EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1542
1543/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001544 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001545 * @mtd: mtd info structure
1546 * @chip: nand chip info structure
1547 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001548 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001549 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001550 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001551 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001552 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001553int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1554 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001555{
1556 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001557 if (oob_required)
1558 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001559 return 0;
1560}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001561EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001562
1563/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001564 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001565 * @mtd: mtd info structure
1566 * @chip: nand chip info structure
1567 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001568 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001569 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001570 *
1571 * We need a special oob layout and handling even when OOB isn't used.
1572 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001573static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001574 struct nand_chip *chip, uint8_t *buf,
1575 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001576{
1577 int eccsize = chip->ecc.size;
1578 int eccbytes = chip->ecc.bytes;
1579 uint8_t *oob = chip->oob_poi;
1580 int steps, size;
1581
1582 for (steps = chip->ecc.steps; steps > 0; steps--) {
1583 chip->read_buf(mtd, buf, eccsize);
1584 buf += eccsize;
1585
1586 if (chip->ecc.prepad) {
1587 chip->read_buf(mtd, oob, chip->ecc.prepad);
1588 oob += chip->ecc.prepad;
1589 }
1590
1591 chip->read_buf(mtd, oob, eccbytes);
1592 oob += eccbytes;
1593
1594 if (chip->ecc.postpad) {
1595 chip->read_buf(mtd, oob, chip->ecc.postpad);
1596 oob += chip->ecc.postpad;
1597 }
1598 }
1599
1600 size = mtd->oobsize - (oob - chip->oob_poi);
1601 if (size)
1602 chip->read_buf(mtd, oob, size);
1603
1604 return 0;
1605}
1606
1607/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001608 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001609 * @mtd: mtd info structure
1610 * @chip: nand chip info structure
1611 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001612 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001613 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001614 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001615static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001616 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Boris Brezillon846031d2016-02-03 20:11:00 +01001618 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001619 int eccbytes = chip->ecc.bytes;
1620 int eccsteps = chip->ecc.steps;
1621 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001622 uint8_t *ecc_calc = chip->buffers->ecccalc;
1623 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001624 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001625
Brian Norris1fbb9382012-05-02 10:14:55 -07001626 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001627
1628 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1629 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1630
Boris Brezillon846031d2016-02-03 20:11:00 +01001631 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1632 chip->ecc.total);
1633 if (ret)
1634 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001635
1636 eccsteps = chip->ecc.steps;
1637 p = buf;
1638
1639 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1640 int stat;
1641
1642 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001643 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001644 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001645 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001646 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001647 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1648 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001649 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001650 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301654 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001655 * @mtd: mtd info structure
1656 * @chip: nand chip info structure
1657 * @data_offs: offset of requested data within the page
1658 * @readlen: data length
1659 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001660 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001661 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001662static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001663 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1664 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001665{
Boris Brezillon846031d2016-02-03 20:11:00 +01001666 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001667 uint8_t *p;
1668 int data_col_addr, i, gaps = 0;
1669 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1670 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001671 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001672 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001673 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001674
Brian Norris7854d3f2011-06-23 14:12:08 -07001675 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001676 start_step = data_offs / chip->ecc.size;
1677 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1678 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301679 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001680
Brian Norris8b6e50c2011-05-25 14:59:01 -07001681 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001682 datafrag_len = num_steps * chip->ecc.size;
1683 eccfrag_len = num_steps * chip->ecc.bytes;
1684
1685 data_col_addr = start_step * chip->ecc.size;
1686 /* If we read not a page aligned data */
1687 if (data_col_addr != 0)
1688 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1689
1690 p = bufpoi + data_col_addr;
1691 chip->read_buf(mtd, p, datafrag_len);
1692
Brian Norris8b6e50c2011-05-25 14:59:01 -07001693 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001694 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1695 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1696
Brian Norris8b6e50c2011-05-25 14:59:01 -07001697 /*
1698 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001699 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001700 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001701 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1702 if (ret)
1703 return ret;
1704
1705 if (oobregion.length < eccfrag_len)
1706 gaps = 1;
1707
Alexey Korolev3d459552008-05-15 17:23:18 +01001708 if (gaps) {
1709 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1710 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1711 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001712 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001713 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001714 * about buswidth alignment in read_buf.
1715 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001716 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001717 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001718 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001719 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001720 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1721 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001722 aligned_len++;
1723
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001724 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001725 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001726 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1727 }
1728
Boris Brezillon846031d2016-02-03 20:11:00 +01001729 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1730 chip->oob_poi, index, eccfrag_len);
1731 if (ret)
1732 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001733
1734 p = bufpoi + data_col_addr;
1735 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1736 int stat;
1737
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001738 stat = chip->ecc.correct(mtd, p,
1739 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001740 if (stat == -EBADMSG &&
1741 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1742 /* check for empty pages with bitflips */
1743 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1744 &chip->buffers->ecccode[i],
1745 chip->ecc.bytes,
1746 NULL, 0,
1747 chip->ecc.strength);
1748 }
1749
Mike Dunn3f91e942012-04-25 12:06:09 -07001750 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001751 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001752 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001753 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001754 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1755 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001756 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001757 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001758}
1759
1760/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001761 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001762 * @mtd: mtd info structure
1763 * @chip: nand chip info structure
1764 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001765 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001766 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001767 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001768 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001769 */
1770static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001771 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001772{
Boris Brezillon846031d2016-02-03 20:11:00 +01001773 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001774 int eccbytes = chip->ecc.bytes;
1775 int eccsteps = chip->ecc.steps;
1776 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001777 uint8_t *ecc_calc = chip->buffers->ecccalc;
1778 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001779 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001780
1781 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1782 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1783 chip->read_buf(mtd, p, eccsize);
1784 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1785 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001786 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001787
Boris Brezillon846031d2016-02-03 20:11:00 +01001788 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1789 chip->ecc.total);
1790 if (ret)
1791 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001792
1793 eccsteps = chip->ecc.steps;
1794 p = buf;
1795
1796 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1797 int stat;
1798
1799 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001800 if (stat == -EBADMSG &&
1801 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1802 /* check for empty pages with bitflips */
1803 stat = nand_check_erased_ecc_chunk(p, eccsize,
1804 &ecc_code[i], eccbytes,
1805 NULL, 0,
1806 chip->ecc.strength);
1807 }
1808
Mike Dunn3f91e942012-04-25 12:06:09 -07001809 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001810 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001811 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001812 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001813 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1814 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001815 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001816 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001817}
1818
1819/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001820 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001821 * @mtd: mtd info structure
1822 * @chip: nand chip info structure
1823 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001824 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001825 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001826 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001827 * Hardware ECC for large page chips, require OOB to be read first. For this
1828 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1829 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1830 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1831 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001832 */
1833static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001834 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001835{
Boris Brezillon846031d2016-02-03 20:11:00 +01001836 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001837 int eccbytes = chip->ecc.bytes;
1838 int eccsteps = chip->ecc.steps;
1839 uint8_t *p = buf;
1840 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001841 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001842 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001843
1844 /* Read the OOB area first */
1845 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1846 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1847 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1848
Boris Brezillon846031d2016-02-03 20:11:00 +01001849 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1850 chip->ecc.total);
1851 if (ret)
1852 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001853
1854 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1855 int stat;
1856
1857 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1858 chip->read_buf(mtd, p, eccsize);
1859 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1860
1861 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001862 if (stat == -EBADMSG &&
1863 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1864 /* check for empty pages with bitflips */
1865 stat = nand_check_erased_ecc_chunk(p, eccsize,
1866 &ecc_code[i], eccbytes,
1867 NULL, 0,
1868 chip->ecc.strength);
1869 }
1870
Mike Dunn3f91e942012-04-25 12:06:09 -07001871 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001872 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001873 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001874 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001875 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1876 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001877 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001878 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001879}
1880
1881/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001882 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001883 * @mtd: mtd info structure
1884 * @chip: nand chip info structure
1885 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001886 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001887 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001888 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001889 * The hw generator calculates the error syndrome automatically. Therefore we
1890 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001891 */
1892static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001893 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001894{
1895 int i, eccsize = chip->ecc.size;
1896 int eccbytes = chip->ecc.bytes;
1897 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001898 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001899 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001900 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001901 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001902
1903 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1904 int stat;
1905
1906 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1907 chip->read_buf(mtd, p, eccsize);
1908
1909 if (chip->ecc.prepad) {
1910 chip->read_buf(mtd, oob, chip->ecc.prepad);
1911 oob += chip->ecc.prepad;
1912 }
1913
1914 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1915 chip->read_buf(mtd, oob, eccbytes);
1916 stat = chip->ecc.correct(mtd, p, oob, NULL);
1917
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001918 oob += eccbytes;
1919
1920 if (chip->ecc.postpad) {
1921 chip->read_buf(mtd, oob, chip->ecc.postpad);
1922 oob += chip->ecc.postpad;
1923 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001924
1925 if (stat == -EBADMSG &&
1926 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1927 /* check for empty pages with bitflips */
1928 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1929 oob - eccpadbytes,
1930 eccpadbytes,
1931 NULL, 0,
1932 chip->ecc.strength);
1933 }
1934
1935 if (stat < 0) {
1936 mtd->ecc_stats.failed++;
1937 } else {
1938 mtd->ecc_stats.corrected += stat;
1939 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1940 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001941 }
1942
1943 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001944 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001945 if (i)
1946 chip->read_buf(mtd, oob, i);
1947
Mike Dunn3f91e942012-04-25 12:06:09 -07001948 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001949}
1950
1951/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001952 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001953 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001954 * @oob: oob destination address
1955 * @ops: oob ops structure
1956 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001957 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001958static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001959 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001960{
Boris Brezillon846031d2016-02-03 20:11:00 +01001961 struct nand_chip *chip = mtd_to_nand(mtd);
1962 int ret;
1963
Florian Fainellif8ac0412010-09-07 13:23:43 +02001964 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001965
Brian Norris0612b9d2011-08-30 18:45:40 -07001966 case MTD_OPS_PLACE_OOB:
1967 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1969 return oob + len;
1970
Boris Brezillon846031d2016-02-03 20:11:00 +01001971 case MTD_OPS_AUTO_OOB:
1972 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1973 ops->ooboffs, len);
1974 BUG_ON(ret);
1975 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001976
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001977 default:
1978 BUG();
1979 }
1980 return NULL;
1981}
1982
1983/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001984 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1985 * @mtd: MTD device structure
1986 * @retry_mode: the retry mode to use
1987 *
1988 * Some vendors supply a special command to shift the Vt threshold, to be used
1989 * when there are too many bitflips in a page (i.e., ECC error). After setting
1990 * a new threshold, the host should retry reading the page.
1991 */
1992static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1993{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001994 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001995
1996 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1997
1998 if (retry_mode >= chip->read_retries)
1999 return -EINVAL;
2000
2001 if (!chip->setup_read_retry)
2002 return -EOPNOTSUPP;
2003
2004 return chip->setup_read_retry(mtd, retry_mode);
2005}
2006
2007/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002008 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002009 * @mtd: MTD device structure
2010 * @from: offset to read from
2011 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00002012 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002013 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00002014 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002015static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2016 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00002017{
Brian Norrise47f3db2012-05-02 10:14:56 -07002018 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002019 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002020 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002021 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03002022 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002023 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002024
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002025 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002026 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002027 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002028 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002029 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002031 chipnr = (int)(from >> chip->chip_shift);
2032 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002034 realpage = (int)(from >> chip->page_shift);
2035 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002037 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002039 buf = ops->datbuf;
2040 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002041 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002042
Florian Fainellif8ac0412010-09-07 13:23:43 +02002043 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002044 unsigned int ecc_failures = mtd->ecc_stats.failed;
2045
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002046 bytes = min(mtd->writesize - col, readlen);
2047 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002048
Kamal Dasu66507c72014-05-01 20:51:19 -04002049 if (!aligned)
2050 use_bufpoi = 1;
2051 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002052 use_bufpoi = !virt_addr_valid(buf) ||
2053 !IS_ALIGNED((unsigned long)buf,
2054 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002055 else
2056 use_bufpoi = 0;
2057
Brian Norris8b6e50c2011-05-25 14:59:01 -07002058 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002059 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002060 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2061
2062 if (use_bufpoi && aligned)
2063 pr_debug("%s: using read bounce buffer for buf@%p\n",
2064 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Brian Norrisba84fb52014-01-03 15:13:33 -08002066read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01002067 if (nand_standard_page_accessors(&chip->ecc))
2068 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Mike Dunnedbc45402012-04-25 12:06:11 -07002070 /*
2071 * Now read the page into the buffer. Absent an error,
2072 * the read methods return max bitflips per ecc step.
2073 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002074 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002075 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002076 oob_required,
2077 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002078 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2079 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002080 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002081 col, bytes, bufpoi,
2082 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002083 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002084 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002085 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002086 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002087 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002088 /* Invalidate page cache */
2089 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002090 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002091 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002092
2093 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002094 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002095 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002096 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002097 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002098 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002099 chip->pagebuf_bitflips = ret;
2100 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002101 /* Invalidate page cache */
2102 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002103 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002104 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002107 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002108 int toread = min(oobreadlen, max_oobsize);
2109
2110 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002111 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002112 oob, ops, toread);
2113 oobreadlen -= toread;
2114 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002115 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002116
2117 if (chip->options & NAND_NEED_READRDY) {
2118 /* Apply delay or wait for ready/busy pin */
2119 if (!chip->dev_ready)
2120 udelay(chip->chip_delay);
2121 else
2122 nand_wait_ready(mtd);
2123 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002124
Brian Norrisba84fb52014-01-03 15:13:33 -08002125 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002126 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002127 retry_mode++;
2128 ret = nand_setup_read_retry(mtd,
2129 retry_mode);
2130 if (ret < 0)
2131 break;
2132
2133 /* Reset failures; retry */
2134 mtd->ecc_stats.failed = ecc_failures;
2135 goto read_retry;
2136 } else {
2137 /* No more retry modes; real failure */
2138 ecc_fail = true;
2139 }
2140 }
2141
2142 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002143 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002144 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002145 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002146 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002147 max_bitflips = max_t(unsigned int, max_bitflips,
2148 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002151 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002152
Brian Norrisba84fb52014-01-03 15:13:33 -08002153 /* Reset to retry mode 0 */
2154 if (retry_mode) {
2155 ret = nand_setup_read_retry(mtd, 0);
2156 if (ret < 0)
2157 break;
2158 retry_mode = 0;
2159 }
2160
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002161 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Brian Norris8b6e50c2011-05-25 14:59:01 -07002164 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 col = 0;
2166 /* Increment page address */
2167 realpage++;
2168
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002169 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* Check, if we cross a chip boundary */
2171 if (!page) {
2172 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002173 chip->select_chip(mtd, -1);
2174 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002177 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002179 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002180 if (oob)
2181 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Mike Dunn3f91e942012-04-25 12:06:09 -07002183 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002184 return ret;
2185
Brian Norrisb72f3df2013-12-03 11:04:14 -08002186 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002187 return -EBADMSG;
2188
Mike Dunnedbc45402012-04-25 12:06:11 -07002189 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002190}
2191
2192/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002193 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002194 * @mtd: MTD device structure
2195 * @from: offset to read from
2196 * @len: number of bytes to read
2197 * @retlen: pointer to variable to store the number of read bytes
2198 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002199 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002200 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002201 */
2202static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2203 size_t *retlen, uint8_t *buf)
2204{
Brian Norris4a89ff82011-08-30 18:45:45 -07002205 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002206 int ret;
2207
Huang Shijie6a8214a2012-11-19 14:43:30 +08002208 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002209 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002210 ops.len = len;
2211 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002212 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002213 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002214 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002215 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002216 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002220 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002221 * @mtd: mtd info structure
2222 * @chip: nand chip info structure
2223 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002224 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002225int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002226{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002227 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002228 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002229 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002230}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002231EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002232
2233/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002234 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002235 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002236 * @mtd: mtd info structure
2237 * @chip: nand chip info structure
2238 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002239 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002240int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2241 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002242{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002243 int length = mtd->oobsize;
2244 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2245 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002246 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002247 int i, toread, sndrnd = 0, pos;
2248
2249 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2250 for (i = 0; i < chip->ecc.steps; i++) {
2251 if (sndrnd) {
2252 pos = eccsize + i * (eccsize + chunk);
2253 if (mtd->writesize > 512)
2254 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2255 else
2256 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2257 } else
2258 sndrnd = 1;
2259 toread = min_t(int, length, chunk);
2260 chip->read_buf(mtd, bufpoi, toread);
2261 bufpoi += toread;
2262 length -= toread;
2263 }
2264 if (length > 0)
2265 chip->read_buf(mtd, bufpoi, length);
2266
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002267 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002268}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002269EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002270
2271/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002272 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002273 * @mtd: mtd info structure
2274 * @chip: nand chip info structure
2275 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002276 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002277int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002278{
2279 int status = 0;
2280 const uint8_t *buf = chip->oob_poi;
2281 int length = mtd->oobsize;
2282
2283 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2284 chip->write_buf(mtd, buf, length);
2285 /* Send command to program the OOB data */
2286 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2287
2288 status = chip->waitfunc(mtd, chip);
2289
Savin Zlobec0d420f92006-06-21 11:51:20 +02002290 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002291}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002292EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002293
2294/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002295 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002296 * with syndrome - only for large page flash
2297 * @mtd: mtd info structure
2298 * @chip: nand chip info structure
2299 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002300 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002301int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2302 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002303{
2304 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2305 int eccsize = chip->ecc.size, length = mtd->oobsize;
2306 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2307 const uint8_t *bufpoi = chip->oob_poi;
2308
2309 /*
2310 * data-ecc-data-ecc ... ecc-oob
2311 * or
2312 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2313 */
2314 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2315 pos = steps * (eccsize + chunk);
2316 steps = 0;
2317 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002318 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002319
2320 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2321 for (i = 0; i < steps; i++) {
2322 if (sndcmd) {
2323 if (mtd->writesize <= 512) {
2324 uint32_t fill = 0xFFFFFFFF;
2325
2326 len = eccsize;
2327 while (len > 0) {
2328 int num = min_t(int, len, 4);
2329 chip->write_buf(mtd, (uint8_t *)&fill,
2330 num);
2331 len -= num;
2332 }
2333 } else {
2334 pos = eccsize + i * (eccsize + chunk);
2335 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2336 }
2337 } else
2338 sndcmd = 1;
2339 len = min_t(int, length, chunk);
2340 chip->write_buf(mtd, bufpoi, len);
2341 bufpoi += len;
2342 length -= len;
2343 }
2344 if (length > 0)
2345 chip->write_buf(mtd, bufpoi, length);
2346
2347 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2348 status = chip->waitfunc(mtd, chip);
2349
2350 return status & NAND_STATUS_FAIL ? -EIO : 0;
2351}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002352EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002353
2354/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002355 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002356 * @mtd: MTD device structure
2357 * @from: offset to read from
2358 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002360 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002362static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2363 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Brian Norrisc00a0992012-05-01 17:12:54 -07002365 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002366 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002367 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002368 int readlen = ops->ooblen;
2369 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002370 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002371 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Brian Norris289c0522011-07-19 10:06:09 -07002373 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302374 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Brian Norris041e4572011-06-23 16:45:24 -07002376 stats = mtd->ecc_stats;
2377
Boris BREZILLON29f10582016-03-07 10:46:52 +01002378 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002379
2380 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002381 pr_debug("%s: attempt to start read outside oob\n",
2382 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002383 return -EINVAL;
2384 }
2385
2386 /* Do not allow reads past end of device */
2387 if (unlikely(from >= mtd->size ||
2388 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2389 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002390 pr_debug("%s: attempt to read beyond end of device\n",
2391 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002392 return -EINVAL;
2393 }
Vitaly Wool70145682006-11-03 18:20:38 +03002394
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002395 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002396 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002398 /* Shift to get page */
2399 realpage = (int)(from >> chip->page_shift);
2400 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Florian Fainellif8ac0412010-09-07 13:23:43 +02002402 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002403 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002404 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002405 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002406 ret = chip->ecc.read_oob(mtd, chip, page);
2407
2408 if (ret < 0)
2409 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002410
2411 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002412 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002413
Brian Norris5bc7c332013-03-13 09:51:31 -07002414 if (chip->options & NAND_NEED_READRDY) {
2415 /* Apply delay or wait for ready/busy pin */
2416 if (!chip->dev_ready)
2417 udelay(chip->chip_delay);
2418 else
2419 nand_wait_ready(mtd);
2420 }
2421
Vitaly Wool70145682006-11-03 18:20:38 +03002422 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002423 if (!readlen)
2424 break;
2425
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002426 /* Increment page address */
2427 realpage++;
2428
2429 page = realpage & chip->pagemask;
2430 /* Check, if we cross a chip boundary */
2431 if (!page) {
2432 chipnr++;
2433 chip->select_chip(mtd, -1);
2434 chip->select_chip(mtd, chipnr);
2435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002437 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002439 ops->oobretlen = ops->ooblen - readlen;
2440
2441 if (ret < 0)
2442 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002443
2444 if (mtd->ecc_stats.failed - stats.failed)
2445 return -EBADMSG;
2446
2447 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
2450/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002451 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002452 * @mtd: MTD device structure
2453 * @from: offset to read from
2454 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002456 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002458static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2459 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002461 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462
2463 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002466 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002467 pr_debug("%s: attempt to read beyond end of device\n",
2468 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return -EINVAL;
2470 }
2471
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002472 if (ops->mode != MTD_OPS_PLACE_OOB &&
2473 ops->mode != MTD_OPS_AUTO_OOB &&
2474 ops->mode != MTD_OPS_RAW)
2475 return -ENOTSUPP;
2476
Huang Shijie6a8214a2012-11-19 14:43:30 +08002477 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479 if (!ops->datbuf)
2480 ret = nand_do_read_oob(mtd, from, ops);
2481 else
2482 ret = nand_do_read_ops(mtd, from, ops);
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002485 return ret;
2486}
2487
2488
2489/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002490 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002491 * @mtd: mtd info structure
2492 * @chip: nand chip info structure
2493 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002494 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002495 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002496 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002497 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002498 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002499int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2500 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002501{
2502 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002503 if (oob_required)
2504 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002505
2506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002508EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002510/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002511 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002512 * @mtd: mtd info structure
2513 * @chip: nand chip info structure
2514 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002515 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002516 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002517 *
2518 * We need a special oob layout and handling even when ECC isn't checked.
2519 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002520static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002521 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002522 const uint8_t *buf, int oob_required,
2523 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002524{
2525 int eccsize = chip->ecc.size;
2526 int eccbytes = chip->ecc.bytes;
2527 uint8_t *oob = chip->oob_poi;
2528 int steps, size;
2529
2530 for (steps = chip->ecc.steps; steps > 0; steps--) {
2531 chip->write_buf(mtd, buf, eccsize);
2532 buf += eccsize;
2533
2534 if (chip->ecc.prepad) {
2535 chip->write_buf(mtd, oob, chip->ecc.prepad);
2536 oob += chip->ecc.prepad;
2537 }
2538
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002539 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002540 oob += eccbytes;
2541
2542 if (chip->ecc.postpad) {
2543 chip->write_buf(mtd, oob, chip->ecc.postpad);
2544 oob += chip->ecc.postpad;
2545 }
2546 }
2547
2548 size = mtd->oobsize - (oob - chip->oob_poi);
2549 if (size)
2550 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002551
2552 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002553}
2554/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002555 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002556 * @mtd: mtd info structure
2557 * @chip: nand chip info structure
2558 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002559 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002560 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002561 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002562static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002563 const uint8_t *buf, int oob_required,
2564 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002565{
Boris Brezillon846031d2016-02-03 20:11:00 +01002566 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002567 int eccbytes = chip->ecc.bytes;
2568 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002569 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002570 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002571
Brian Norris7854d3f2011-06-23 14:12:08 -07002572 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002573 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2574 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002575
Boris Brezillon846031d2016-02-03 20:11:00 +01002576 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2577 chip->ecc.total);
2578 if (ret)
2579 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002580
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002581 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002582}
2583
2584/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002585 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002586 * @mtd: mtd info structure
2587 * @chip: nand chip info structure
2588 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002589 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002590 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002591 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002592static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002593 const uint8_t *buf, int oob_required,
2594 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002595{
Boris Brezillon846031d2016-02-03 20:11:00 +01002596 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002597 int eccbytes = chip->ecc.bytes;
2598 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002599 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002600 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002601
2602 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2603 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002604 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002605 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2606 }
2607
Boris Brezillon846031d2016-02-03 20:11:00 +01002608 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2609 chip->ecc.total);
2610 if (ret)
2611 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612
2613 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002614
2615 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002616}
2617
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302618
2619/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002620 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302621 * @mtd: mtd info structure
2622 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002623 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302624 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002625 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302626 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002627 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302628 */
2629static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2630 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002631 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002632 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302633{
2634 uint8_t *oob_buf = chip->oob_poi;
2635 uint8_t *ecc_calc = chip->buffers->ecccalc;
2636 int ecc_size = chip->ecc.size;
2637 int ecc_bytes = chip->ecc.bytes;
2638 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302639 uint32_t start_step = offset / ecc_size;
2640 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2641 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002642 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302643
2644 for (step = 0; step < ecc_steps; step++) {
2645 /* configure controller for WRITE access */
2646 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2647
2648 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002649 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302650
2651 /* mask ECC of un-touched subpages by padding 0xFF */
2652 if ((step < start_step) || (step > end_step))
2653 memset(ecc_calc, 0xff, ecc_bytes);
2654 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002655 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302656
2657 /* mask OOB of un-touched subpages by padding 0xFF */
2658 /* if oob_required, preserve OOB metadata of written subpage */
2659 if (!oob_required || (step < start_step) || (step > end_step))
2660 memset(oob_buf, 0xff, oob_bytes);
2661
Brian Norrisd6a950802013-08-08 17:16:36 -07002662 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302663 ecc_calc += ecc_bytes;
2664 oob_buf += oob_bytes;
2665 }
2666
2667 /* copy calculated ECC for whole page to chip->buffer->oob */
2668 /* this include masked-value(0xFF) for unwritten subpages */
2669 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002670 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2671 chip->ecc.total);
2672 if (ret)
2673 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302674
2675 /* write OOB buffer to NAND device */
2676 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2677
2678 return 0;
2679}
2680
2681
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002682/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002683 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002684 * @mtd: mtd info structure
2685 * @chip: nand chip info structure
2686 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002687 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002688 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002689 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002690 * The hw generator calculates the error syndrome automatically. Therefore we
2691 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002692 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002693static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002694 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002695 const uint8_t *buf, int oob_required,
2696 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002697{
2698 int i, eccsize = chip->ecc.size;
2699 int eccbytes = chip->ecc.bytes;
2700 int eccsteps = chip->ecc.steps;
2701 const uint8_t *p = buf;
2702 uint8_t *oob = chip->oob_poi;
2703
2704 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2705
2706 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2707 chip->write_buf(mtd, p, eccsize);
2708
2709 if (chip->ecc.prepad) {
2710 chip->write_buf(mtd, oob, chip->ecc.prepad);
2711 oob += chip->ecc.prepad;
2712 }
2713
2714 chip->ecc.calculate(mtd, p, oob);
2715 chip->write_buf(mtd, oob, eccbytes);
2716 oob += eccbytes;
2717
2718 if (chip->ecc.postpad) {
2719 chip->write_buf(mtd, oob, chip->ecc.postpad);
2720 oob += chip->ecc.postpad;
2721 }
2722 }
2723
2724 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002725 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002726 if (i)
2727 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002728
2729 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002730}
2731
2732/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002733 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002734 * @mtd: MTD device structure
2735 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302736 * @offset: address offset within the page
2737 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002738 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002739 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002740 * @page: page number to write
2741 * @cached: cached programming
2742 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002743 */
2744static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302745 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002746 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002747{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302748 int status, subpage;
2749
2750 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2751 chip->ecc.write_subpage)
2752 subpage = offset || (data_len < mtd->writesize);
2753 else
2754 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002755
Marc Gonzalez3371d662016-11-15 10:56:20 +01002756 if (nand_standard_page_accessors(&chip->ecc))
2757 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002758
David Woodhouse956e9442006-09-25 17:12:39 +01002759 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302760 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002761 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302762 else if (subpage)
2763 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002764 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002765 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002766 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2767 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002768
2769 if (status < 0)
2770 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002771
Boris Brezillon41145642017-05-16 18:27:49 +02002772 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002773 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Boris Brezillon41145642017-05-16 18:27:49 +02002774
2775 status = chip->waitfunc(mtd, chip);
2776 if (status & NAND_STATUS_FAIL)
2777 return -EIO;
2778 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002779
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002780 return 0;
2781}
2782
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002783/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002784 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002785 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002786 * @oob: oob data buffer
2787 * @len: oob data write length
2788 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002789 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002790static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2791 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002792{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002793 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002794 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002795
2796 /*
2797 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2798 * data from a previous OOB read.
2799 */
2800 memset(chip->oob_poi, 0xff, mtd->oobsize);
2801
Florian Fainellif8ac0412010-09-07 13:23:43 +02002802 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002803
Brian Norris0612b9d2011-08-30 18:45:40 -07002804 case MTD_OPS_PLACE_OOB:
2805 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002806 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2807 return oob + len;
2808
Boris Brezillon846031d2016-02-03 20:11:00 +01002809 case MTD_OPS_AUTO_OOB:
2810 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2811 ops->ooboffs, len);
2812 BUG_ON(ret);
2813 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002814
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002815 default:
2816 BUG();
2817 }
2818 return NULL;
2819}
2820
Florian Fainellif8ac0412010-09-07 13:23:43 +02002821#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002822
2823/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002824 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002825 * @mtd: MTD device structure
2826 * @to: offset to write to
2827 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002828 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002829 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002830 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002831static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2832 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002833{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002834 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002835 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002836 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002837
2838 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002839 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002840
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002841 uint8_t *oob = ops->oobbuf;
2842 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302843 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002844 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002845
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002846 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002847 if (!writelen)
2848 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002849
Brian Norris8b6e50c2011-05-25 14:59:01 -07002850 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002851 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002852 pr_notice("%s: attempt to write non page aligned data\n",
2853 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002854 return -EINVAL;
2855 }
2856
Thomas Gleixner29072b92006-09-28 15:38:36 +02002857 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002858
Thomas Gleixner6a930962006-06-28 00:11:45 +02002859 chipnr = (int)(to >> chip->chip_shift);
2860 chip->select_chip(mtd, chipnr);
2861
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002862 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002863 if (nand_check_wp(mtd)) {
2864 ret = -EIO;
2865 goto err_out;
2866 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002867
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002868 realpage = (int)(to >> chip->page_shift);
2869 page = realpage & chip->pagemask;
2870 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2871
2872 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002873 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2874 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002875 chip->pagebuf = -1;
2876
Maxim Levitsky782ce792010-02-22 20:39:36 +02002877 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002878 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2879 ret = -EINVAL;
2880 goto err_out;
2881 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002882
Florian Fainellif8ac0412010-09-07 13:23:43 +02002883 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002884 int bytes = mtd->writesize;
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);
Kamal Dasu66507c72014-05-01 20:51:19 -04002902 if (part_pagewr)
2903 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002904 chip->pagebuf = -1;
2905 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2906 memcpy(&chip->buffers->databuf[column], buf, bytes);
2907 wbuf = chip->buffers->databuf;
2908 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002909
Maxim Levitsky782ce792010-02-22 20:39:36 +02002910 if (unlikely(oob)) {
2911 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002912 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002913 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002914 } else {
2915 /* We still need to erase leftover OOB data */
2916 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002917 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002918
2919 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002920 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002921 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002922 if (ret)
2923 break;
2924
2925 writelen -= bytes;
2926 if (!writelen)
2927 break;
2928
Thomas Gleixner29072b92006-09-28 15:38:36 +02002929 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002930 buf += bytes;
2931 realpage++;
2932
2933 page = realpage & chip->pagemask;
2934 /* Check, if we cross a chip boundary */
2935 if (!page) {
2936 chipnr++;
2937 chip->select_chip(mtd, -1);
2938 chip->select_chip(mtd, chipnr);
2939 }
2940 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002941
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002942 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002943 if (unlikely(oob))
2944 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002945
2946err_out:
2947 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002948 return ret;
2949}
2950
2951/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002952 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002953 * @mtd: MTD device structure
2954 * @to: offset to write to
2955 * @len: number of bytes to write
2956 * @retlen: pointer to variable to store the number of written bytes
2957 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002958 *
2959 * NAND write with ECC. Used when performing writes in interrupt context, this
2960 * may for example be called by mtdoops when writing an oops while in panic.
2961 */
2962static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2963 size_t *retlen, const uint8_t *buf)
2964{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002965 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002966 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002967 int ret;
2968
Brian Norris8b6e50c2011-05-25 14:59:01 -07002969 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002970 panic_nand_wait(mtd, chip, 400);
2971
Brian Norris8b6e50c2011-05-25 14:59:01 -07002972 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002973 panic_nand_get_device(chip, mtd, FL_WRITING);
2974
Brian Norris0ec56dc2015-02-28 02:02:30 -08002975 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002976 ops.len = len;
2977 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002978 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002979
Brian Norris4a89ff82011-08-30 18:45:45 -07002980 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002981
Brian Norris4a89ff82011-08-30 18:45:45 -07002982 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002983 return ret;
2984}
2985
2986/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002987 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002988 * @mtd: MTD device structure
2989 * @to: offset to write to
2990 * @len: number of bytes to write
2991 * @retlen: pointer to variable to store the number of written bytes
2992 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002994 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002996static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002997 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Brian Norris4a89ff82011-08-30 18:45:45 -07002999 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003000 int ret;
3001
Huang Shijie6a8214a2012-11-19 14:43:30 +08003002 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003003 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003004 ops.len = len;
3005 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003006 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003007 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003008 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003009 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003010 return ret;
3011}
3012
3013/**
3014 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003015 * @mtd: MTD device structure
3016 * @to: offset to write to
3017 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003018 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003019 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003020 */
3021static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3022 struct mtd_oob_ops *ops)
3023{
Adrian Hunter03736152007-01-31 17:58:29 +02003024 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003025 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Brian Norris289c0522011-07-19 10:06:09 -07003027 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303028 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Boris BREZILLON29f10582016-03-07 10:46:52 +01003030 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003033 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003034 pr_debug("%s: attempt to write past end of page\n",
3035 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return -EINVAL;
3037 }
3038
Adrian Hunter03736152007-01-31 17:58:29 +02003039 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003040 pr_debug("%s: attempt to start write outside oob\n",
3041 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003042 return -EINVAL;
3043 }
3044
Jason Liu775adc3d42011-02-25 13:06:18 +08003045 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003046 if (unlikely(to >= mtd->size ||
3047 ops->ooboffs + ops->ooblen >
3048 ((mtd->size >> chip->page_shift) -
3049 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003050 pr_debug("%s: attempt to write beyond end of device\n",
3051 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003052 return -EINVAL;
3053 }
3054
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003055 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003056
3057 /*
3058 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3059 * of my DiskOnChip 2000 test units) will clear the whole data page too
3060 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3061 * it in the doc2000 driver in August 1999. dwmw2.
3062 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003063 nand_reset(chip, chipnr);
3064
3065 chip->select_chip(mtd, chipnr);
3066
3067 /* Shift to get page */
3068 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003071 if (nand_check_wp(mtd)) {
3072 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003073 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003074 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003077 if (page == chip->pagebuf)
3078 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003080 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003081
Brian Norris0612b9d2011-08-30 18:45:40 -07003082 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003083 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3084 else
3085 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003086
Huang Shijieb0bb6902012-11-19 14:43:29 +08003087 chip->select_chip(mtd, -1);
3088
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003089 if (status)
3090 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Vitaly Wool70145682006-11-03 18:20:38 +03003092 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003094 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003097/**
3098 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003099 * @mtd: MTD device structure
3100 * @to: offset to write to
3101 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003102 */
3103static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3104 struct mtd_oob_ops *ops)
3105{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003106 int ret = -ENOTSUPP;
3107
3108 ops->retlen = 0;
3109
3110 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003111 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003112 pr_debug("%s: attempt to write beyond end of device\n",
3113 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003114 return -EINVAL;
3115 }
3116
Huang Shijie6a8214a2012-11-19 14:43:30 +08003117 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003118
Florian Fainellif8ac0412010-09-07 13:23:43 +02003119 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003120 case MTD_OPS_PLACE_OOB:
3121 case MTD_OPS_AUTO_OOB:
3122 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003123 break;
3124
3125 default:
3126 goto out;
3127 }
3128
3129 if (!ops->datbuf)
3130 ret = nand_do_write_oob(mtd, to, ops);
3131 else
3132 ret = nand_do_write_ops(mtd, to, ops);
3133
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003134out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003135 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return ret;
3137}
3138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139/**
Brian Norris49c50b92014-05-06 16:02:19 -07003140 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003141 * @mtd: MTD device structure
3142 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 *
Brian Norris49c50b92014-05-06 16:02:19 -07003144 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 */
Brian Norris49c50b92014-05-06 16:02:19 -07003146static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003148 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003150 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3151 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003152
3153 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
3156/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158 * @mtd: MTD device structure
3159 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003163static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
David Woodhousee0c7d762006-05-13 18:07:53 +01003165 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003169 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003170 * @mtd: MTD device structure
3171 * @instr: erase instruction
3172 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003174 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003176int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3177 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
Adrian Hunter69423d92008-12-10 13:37:21 +00003179 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003180 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003181 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Brian Norris289c0522011-07-19 10:06:09 -07003183 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3184 __func__, (unsigned long long)instr->addr,
3185 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303187 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003191 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003194 page = (int)(instr->addr >> chip->page_shift);
3195 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003198 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003201 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 /* Check, if it is write protected */
3204 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003205 pr_debug("%s: device is write protected!\n",
3206 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 instr->state = MTD_ERASE_FAILED;
3208 goto erase_exit;
3209 }
3210
3211 /* Loop through the pages */
3212 len = instr->len;
3213
3214 instr->state = MTD_ERASING;
3215
3216 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003217 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003218 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303219 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003220 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3221 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 instr->state = MTD_ERASE_FAILED;
3223 goto erase_exit;
3224 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003225
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003226 /*
3227 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003228 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003229 */
3230 if (page <= chip->pagebuf && chip->pagebuf <
3231 (page + pages_per_block))
3232 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Brian Norris49c50b92014-05-06 16:02:19 -07003234 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003237 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003238 pr_debug("%s: failed erase, page 0x%08x\n",
3239 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003241 instr->fail_addr =
3242 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 goto erase_exit;
3244 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003245
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003247 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 page += pages_per_block;
3249
3250 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003251 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003253 chip->select_chip(mtd, -1);
3254 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
3256 }
3257 instr->state = MTD_ERASE_DONE;
3258
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003259erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003264 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 nand_release_device(mtd);
3266
David Woodhouse49defc02007-10-06 15:01:59 -04003267 /* Do call back function */
3268 if (!ret)
3269 mtd_erase_callback(instr);
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /* Return more or less happy */
3272 return ret;
3273}
3274
3275/**
3276 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003277 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003279 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003281static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Brian Norris289c0522011-07-19 10:06:09 -07003283 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003286 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003288 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003292 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003293 * @mtd: MTD device structure
3294 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003296static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303298 struct nand_chip *chip = mtd_to_nand(mtd);
3299 int chipnr = (int)(offs >> chip->chip_shift);
3300 int ret;
3301
3302 /* Select the NAND device */
3303 nand_get_device(mtd, FL_READING);
3304 chip->select_chip(mtd, chipnr);
3305
3306 ret = nand_block_checkbad(mtd, offs, 0);
3307
3308 chip->select_chip(mtd, -1);
3309 nand_release_device(mtd);
3310
3311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003315 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003316 * @mtd: MTD device structure
3317 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003319static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 int ret;
3322
Florian Fainellif8ac0412010-09-07 13:23:43 +02003323 ret = nand_block_isbad(mtd, ofs);
3324 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003325 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 if (ret > 0)
3327 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003328 return ret;
3329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Brian Norris5a0edb22013-07-30 17:52:58 -07003331 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
3334/**
Zach Brown56718422017-01-10 13:30:20 -06003335 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3336 * @mtd: MTD device structure
3337 * @ofs: offset relative to mtd start
3338 * @len: length of mtd
3339 */
3340static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3341{
3342 struct nand_chip *chip = mtd_to_nand(mtd);
3343 u32 part_start_block;
3344 u32 part_end_block;
3345 u32 part_start_die;
3346 u32 part_end_die;
3347
3348 /*
3349 * max_bb_per_die and blocks_per_die used to determine
3350 * the maximum bad block count.
3351 */
3352 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3353 return -ENOTSUPP;
3354
3355 /* Get the start and end of the partition in erase blocks. */
3356 part_start_block = mtd_div_by_eb(ofs, mtd);
3357 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3358
3359 /* Get the start and end LUNs of the partition. */
3360 part_start_die = part_start_block / chip->blocks_per_die;
3361 part_end_die = part_end_block / chip->blocks_per_die;
3362
3363 /*
3364 * Look up the bad blocks per unit and multiply by the number of units
3365 * that the partition spans.
3366 */
3367 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3368}
3369
3370/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003371 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3372 * @mtd: MTD device structure
3373 * @chip: nand chip info structure
3374 * @addr: feature address.
3375 * @subfeature_param: the subfeature parameters, a four bytes array.
3376 */
3377static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3378 int addr, uint8_t *subfeature_param)
3379{
3380 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003381 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003382
David Mosbergerd914c932013-05-29 15:30:13 +03003383 if (!chip->onfi_version ||
3384 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3385 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003386 return -EINVAL;
3387
3388 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003389 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3390 chip->write_byte(mtd, subfeature_param[i]);
3391
Huang Shijie7db03ec2012-09-13 14:57:52 +08003392 status = chip->waitfunc(mtd, chip);
3393 if (status & NAND_STATUS_FAIL)
3394 return -EIO;
3395 return 0;
3396}
3397
3398/**
3399 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3400 * @mtd: MTD device structure
3401 * @chip: nand chip info structure
3402 * @addr: feature address.
3403 * @subfeature_param: the subfeature parameters, a four bytes array.
3404 */
3405static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3406 int addr, uint8_t *subfeature_param)
3407{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003408 int i;
3409
David Mosbergerd914c932013-05-29 15:30:13 +03003410 if (!chip->onfi_version ||
3411 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3412 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003413 return -EINVAL;
3414
Huang Shijie7db03ec2012-09-13 14:57:52 +08003415 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003416 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3417 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003418 return 0;
3419}
3420
3421/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003422 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3423 * -ENOTSUPP
3424 * @mtd: MTD device structure
3425 * @chip: nand chip info structure
3426 * @addr: feature address.
3427 * @subfeature_param: the subfeature parameters, a four bytes array.
3428 *
3429 * Should be used by NAND controller drivers that do not support the SET/GET
3430 * FEATURES operations.
3431 */
3432int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3433 struct nand_chip *chip, int addr,
3434 u8 *subfeature_param)
3435{
3436 return -ENOTSUPP;
3437}
3438EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3439
3440/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003441 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003442 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003443 */
3444static int nand_suspend(struct mtd_info *mtd)
3445{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003446 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003447}
3448
3449/**
3450 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003451 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003452 */
3453static void nand_resume(struct mtd_info *mtd)
3454{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003455 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003456
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003457 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003458 nand_release_device(mtd);
3459 else
Brian Norrisd0370212011-07-19 10:06:08 -07003460 pr_err("%s called for a chip which is not in suspended state\n",
3461 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003462}
3463
Scott Branden72ea4032014-11-20 11:18:05 -08003464/**
3465 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3466 * prevent further operations
3467 * @mtd: MTD device structure
3468 */
3469static void nand_shutdown(struct mtd_info *mtd)
3470{
Brian Norris9ca641b2015-11-09 16:37:28 -08003471 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003472}
3473
Brian Norris8b6e50c2011-05-25 14:59:01 -07003474/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003475static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003476{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003477 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003480 if (!chip->chip_delay)
3481 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003484 if (chip->cmdfunc == NULL)
3485 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003488 if (chip->waitfunc == NULL)
3489 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003491 if (!chip->select_chip)
3492 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003493
Huang Shijie4204ccc2013-08-16 10:10:07 +08003494 /* set for ONFI nand */
3495 if (!chip->onfi_set_features)
3496 chip->onfi_set_features = nand_onfi_set_features;
3497 if (!chip->onfi_get_features)
3498 chip->onfi_get_features = nand_onfi_get_features;
3499
Brian Norris68e80782013-07-18 01:17:02 -07003500 /* If called twice, pointers that depend on busw may need to be reset */
3501 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003502 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3503 if (!chip->read_word)
3504 chip->read_word = nand_read_word;
3505 if (!chip->block_bad)
3506 chip->block_bad = nand_block_bad;
3507 if (!chip->block_markbad)
3508 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003509 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003510 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003511 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3512 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003513 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003514 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003515 if (!chip->scan_bbt)
3516 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003517
3518 if (!chip->controller) {
3519 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003520 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003521 }
3522
Masahiro Yamada477544c2017-03-30 17:15:05 +09003523 if (!chip->buf_align)
3524 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003525}
3526
Brian Norris8b6e50c2011-05-25 14:59:01 -07003527/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003528static void sanitize_string(uint8_t *s, size_t len)
3529{
3530 ssize_t i;
3531
Brian Norris8b6e50c2011-05-25 14:59:01 -07003532 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003533 s[len - 1] = 0;
3534
Brian Norris8b6e50c2011-05-25 14:59:01 -07003535 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003536 for (i = 0; i < len - 1; i++) {
3537 if (s[i] < ' ' || s[i] > 127)
3538 s[i] = '?';
3539 }
3540
Brian Norris8b6e50c2011-05-25 14:59:01 -07003541 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003542 strim(s);
3543}
3544
3545static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3546{
3547 int i;
3548 while (len--) {
3549 crc ^= *p++ << 8;
3550 for (i = 0; i < 8; i++)
3551 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3552 }
3553
3554 return crc;
3555}
3556
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003557/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003558static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3559 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003560{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003561 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003562 struct onfi_ext_param_page *ep;
3563 struct onfi_ext_section *s;
3564 struct onfi_ext_ecc_info *ecc;
3565 uint8_t *cursor;
3566 int ret = -EINVAL;
3567 int len;
3568 int i;
3569
3570 len = le16_to_cpu(p->ext_param_page_length) * 16;
3571 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003572 if (!ep)
3573 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003574
3575 /* Send our own NAND_CMD_PARAM. */
3576 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3577
3578 /* Use the Change Read Column command to skip the ONFI param pages. */
3579 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3580 sizeof(*p) * p->num_of_param_pages , -1);
3581
3582 /* Read out the Extended Parameter Page. */
3583 chip->read_buf(mtd, (uint8_t *)ep, len);
3584 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3585 != le16_to_cpu(ep->crc))) {
3586 pr_debug("fail in the CRC.\n");
3587 goto ext_out;
3588 }
3589
3590 /*
3591 * Check the signature.
3592 * Do not strictly follow the ONFI spec, maybe changed in future.
3593 */
3594 if (strncmp(ep->sig, "EPPS", 4)) {
3595 pr_debug("The signature is invalid.\n");
3596 goto ext_out;
3597 }
3598
3599 /* find the ECC section. */
3600 cursor = (uint8_t *)(ep + 1);
3601 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3602 s = ep->sections + i;
3603 if (s->type == ONFI_SECTION_TYPE_2)
3604 break;
3605 cursor += s->length * 16;
3606 }
3607 if (i == ONFI_EXT_SECTION_MAX) {
3608 pr_debug("We can not find the ECC section.\n");
3609 goto ext_out;
3610 }
3611
3612 /* get the info we want. */
3613 ecc = (struct onfi_ext_ecc_info *)cursor;
3614
Brian Norris4ae7d222013-09-16 18:20:21 -07003615 if (!ecc->codeword_size) {
3616 pr_debug("Invalid codeword size\n");
3617 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003618 }
3619
Brian Norris4ae7d222013-09-16 18:20:21 -07003620 chip->ecc_strength_ds = ecc->ecc_bits;
3621 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003622 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003623
3624ext_out:
3625 kfree(ep);
3626 return ret;
3627}
3628
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003629/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003630 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003631 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003632static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003633{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003634 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003635 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003636 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003637 int val;
3638
Brian Norris7854d3f2011-06-23 14:12:08 -07003639 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003640 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3641 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3642 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3643 return 0;
3644
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003645 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3646 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003647 for (j = 0; j < sizeof(*p); j++)
3648 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003649 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3650 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003651 break;
3652 }
3653 }
3654
Brian Norrisc7f23a72013-08-13 10:51:55 -07003655 if (i == 3) {
3656 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003657 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003658 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003659
Brian Norris8b6e50c2011-05-25 14:59:01 -07003660 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003661 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003662 if (val & (1 << 5))
3663 chip->onfi_version = 23;
3664 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003665 chip->onfi_version = 22;
3666 else if (val & (1 << 3))
3667 chip->onfi_version = 21;
3668 else if (val & (1 << 2))
3669 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003670 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003671 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003672
3673 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003674 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003675 return 0;
3676 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003677
3678 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3679 sanitize_string(p->model, sizeof(p->model));
3680 if (!mtd->name)
3681 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003682
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003683 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003684
3685 /*
3686 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3687 * (don't ask me who thought of this...). MTD assumes that these
3688 * dimensions will be power-of-2, so just truncate the remaining area.
3689 */
3690 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3691 mtd->erasesize *= mtd->writesize;
3692
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003693 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003694
3695 /* See erasesize comment */
3696 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003697 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003698 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003699
Zach Brown34da5f52017-01-10 13:30:21 -06003700 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3701 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3702
Huang Shijiee2985fc2013-05-17 11:17:30 +08003703 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003704 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003705
Huang Shijie10c86ba2013-05-17 11:17:26 +08003706 if (p->ecc_bits != 0xff) {
3707 chip->ecc_strength_ds = p->ecc_bits;
3708 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003709 } else if (chip->onfi_version >= 21 &&
3710 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3711
3712 /*
3713 * The nand_flash_detect_ext_param_page() uses the
3714 * Change Read Column command which maybe not supported
3715 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3716 * now. We do not replace user supplied command function.
3717 */
3718 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3719 chip->cmdfunc = nand_command_lp;
3720
3721 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003722 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003723 pr_warn("Failed to detect ONFI extended param page\n");
3724 } else {
3725 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003726 }
3727
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003728 return 1;
3729}
3730
3731/*
Huang Shijie91361812014-02-21 13:39:40 +08003732 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3733 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003734static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003735{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003736 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003737 struct nand_jedec_params *p = &chip->jedec_params;
3738 struct jedec_ecc_info *ecc;
3739 int val;
3740 int i, j;
3741
3742 /* Try JEDEC for unknown chip or LP */
3743 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3744 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3745 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3746 chip->read_byte(mtd) != 'C')
3747 return 0;
3748
3749 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3750 for (i = 0; i < 3; i++) {
3751 for (j = 0; j < sizeof(*p); j++)
3752 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3753
3754 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3755 le16_to_cpu(p->crc))
3756 break;
3757 }
3758
3759 if (i == 3) {
3760 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3761 return 0;
3762 }
3763
3764 /* Check version */
3765 val = le16_to_cpu(p->revision);
3766 if (val & (1 << 2))
3767 chip->jedec_version = 10;
3768 else if (val & (1 << 1))
3769 chip->jedec_version = 1; /* vendor specific version */
3770
3771 if (!chip->jedec_version) {
3772 pr_info("unsupported JEDEC version: %d\n", val);
3773 return 0;
3774 }
3775
3776 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3777 sanitize_string(p->model, sizeof(p->model));
3778 if (!mtd->name)
3779 mtd->name = p->model;
3780
3781 mtd->writesize = le32_to_cpu(p->byte_per_page);
3782
3783 /* Please reference to the comment for nand_flash_detect_onfi. */
3784 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3785 mtd->erasesize *= mtd->writesize;
3786
3787 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3788
3789 /* Please reference to the comment for nand_flash_detect_onfi. */
3790 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3791 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3792 chip->bits_per_cell = p->bits_per_cell;
3793
3794 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003795 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003796
3797 /* ECC info */
3798 ecc = &p->ecc_info[0];
3799
3800 if (ecc->codeword_size >= 9) {
3801 chip->ecc_strength_ds = ecc->ecc_bits;
3802 chip->ecc_step_ds = 1 << ecc->codeword_size;
3803 } else {
3804 pr_warn("Invalid codeword size\n");
3805 }
3806
3807 return 1;
3808}
3809
3810/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003811 * nand_id_has_period - Check if an ID string has a given wraparound period
3812 * @id_data: the ID string
3813 * @arrlen: the length of the @id_data array
3814 * @period: the period of repitition
3815 *
3816 * Check if an ID string is repeated within a given sequence of bytes at
3817 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003818 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003819 * if the repetition has a period of @period; otherwise, returns zero.
3820 */
3821static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3822{
3823 int i, j;
3824 for (i = 0; i < period; i++)
3825 for (j = i + period; j < arrlen; j += period)
3826 if (id_data[i] != id_data[j])
3827 return 0;
3828 return 1;
3829}
3830
3831/*
3832 * nand_id_len - Get the length of an ID string returned by CMD_READID
3833 * @id_data: the ID string
3834 * @arrlen: the length of the @id_data array
3835
3836 * Returns the length of the ID string, according to known wraparound/trailing
3837 * zero patterns. If no pattern exists, returns the length of the array.
3838 */
3839static int nand_id_len(u8 *id_data, int arrlen)
3840{
3841 int last_nonzero, period;
3842
3843 /* Find last non-zero byte */
3844 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3845 if (id_data[last_nonzero])
3846 break;
3847
3848 /* All zeros */
3849 if (last_nonzero < 0)
3850 return 0;
3851
3852 /* Calculate wraparound period */
3853 for (period = 1; period < arrlen; period++)
3854 if (nand_id_has_period(id_data, arrlen, period))
3855 break;
3856
3857 /* There's a repeated pattern */
3858 if (period < arrlen)
3859 return period;
3860
3861 /* There are trailing zeros */
3862 if (last_nonzero < arrlen - 1)
3863 return last_nonzero + 1;
3864
3865 /* No pattern detected */
3866 return arrlen;
3867}
3868
Huang Shijie7db906b2013-09-25 14:58:11 +08003869/* Extract the bits of per cell from the 3rd byte of the extended ID */
3870static int nand_get_bits_per_cell(u8 cellinfo)
3871{
3872 int bits;
3873
3874 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3875 bits >>= NAND_CI_CELLTYPE_SHIFT;
3876 return bits + 1;
3877}
3878
Brian Norrise3b88bd2012-09-24 20:40:52 -07003879/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003880 * Many new NAND share similar device ID codes, which represent the size of the
3881 * chip. The rest of the parameters must be decoded according to generic or
3882 * manufacturer-specific "extended ID" decoding patterns.
3883 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003884void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003885{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003886 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003887 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003888 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003889 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003890 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003891 /* The 4th id byte is the important one */
3892 extid = id_data[3];
3893
Boris Brezillon01389b62016-06-08 10:30:18 +02003894 /* Calc pagesize */
3895 mtd->writesize = 1024 << (extid & 0x03);
3896 extid >>= 2;
3897 /* Calc oobsize */
3898 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3899 extid >>= 2;
3900 /* Calc blocksize. Blocksize is multiples of 64KiB */
3901 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3902 extid >>= 2;
3903 /* Get buswidth information */
3904 if (extid & 0x1)
3905 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003906}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003907EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003908
3909/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003910 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3911 * decodes a matching ID table entry and assigns the MTD size parameters for
3912 * the chip.
3913 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003914static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003915{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003916 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003917
3918 mtd->erasesize = type->erasesize;
3919 mtd->writesize = type->pagesize;
3920 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003921
Huang Shijie1c195e92013-09-25 14:58:12 +08003922 /* All legacy ID NAND are small-page, SLC */
3923 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003924}
3925
3926/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003927 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3928 * heuristic patterns using various detected parameters (e.g., manufacturer,
3929 * page size, cell-type information).
3930 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003931static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003932{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003933 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003934
3935 /* Set the bad block position */
3936 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3937 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3938 else
3939 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003940}
3941
Huang Shijieec6e87e2013-03-15 11:01:00 +08003942static inline bool is_full_id_nand(struct nand_flash_dev *type)
3943{
3944 return type->id_len;
3945}
3946
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003947static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003948 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003949{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003950 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003951 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003952
Huang Shijieec6e87e2013-03-15 11:01:00 +08003953 if (!strncmp(type->id, id_data, type->id_len)) {
3954 mtd->writesize = type->pagesize;
3955 mtd->erasesize = type->erasesize;
3956 mtd->oobsize = type->oobsize;
3957
Huang Shijie7db906b2013-09-25 14:58:11 +08003958 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003959 chip->chipsize = (uint64_t)type->chipsize << 20;
3960 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003961 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3962 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003963 chip->onfi_timing_mode_default =
3964 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003965
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003966 if (!mtd->name)
3967 mtd->name = type->name;
3968
Huang Shijieec6e87e2013-03-15 11:01:00 +08003969 return true;
3970 }
3971 return false;
3972}
3973
Brian Norris7e74c2d2012-09-24 20:40:49 -07003974/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003975 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3976 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3977 * table.
3978 */
3979static void nand_manufacturer_detect(struct nand_chip *chip)
3980{
3981 /*
3982 * Try manufacturer detection if available and use
3983 * nand_decode_ext_id() otherwise.
3984 */
3985 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3986 chip->manufacturer.desc->ops->detect)
3987 chip->manufacturer.desc->ops->detect(chip);
3988 else
3989 nand_decode_ext_id(chip);
3990}
3991
3992/*
3993 * Manufacturer initialization. This function is called for all NANDs including
3994 * ONFI and JEDEC compliant ones.
3995 * Manufacturer drivers should put all their specific initialization code in
3996 * their ->init() hook.
3997 */
3998static int nand_manufacturer_init(struct nand_chip *chip)
3999{
4000 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4001 !chip->manufacturer.desc->ops->init)
4002 return 0;
4003
4004 return chip->manufacturer.desc->ops->init(chip);
4005}
4006
4007/*
4008 * Manufacturer cleanup. This function is called for all NANDs including
4009 * ONFI and JEDEC compliant ones.
4010 * Manufacturer drivers should put all their specific cleanup code in their
4011 * ->cleanup() hook.
4012 */
4013static void nand_manufacturer_cleanup(struct nand_chip *chip)
4014{
4015 /* Release manufacturer private data */
4016 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4017 chip->manufacturer.desc->ops->cleanup)
4018 chip->manufacturer.desc->ops->cleanup(chip);
4019}
4020
4021/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004022 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004023 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004024static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004025{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004026 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004027 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08004028 int busw;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004029 int i, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004030 u8 *id_data = chip->id.data;
4031 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Karl Beldanef89a882008-09-15 14:37:29 +02004033 /*
4034 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004035 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004036 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004037 nand_reset(chip, 0);
4038
4039 /* Select the device */
4040 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004041
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004043 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004046 maf_id = chip->read_byte(mtd);
4047 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Brian Norris8b6e50c2011-05-25 14:59:01 -07004049 /*
4050 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004051 * interface concerns can cause random data which looks like a
4052 * possibly credible NAND flash to appear. If the two results do
4053 * not match, ignore the device completely.
4054 */
4055
4056 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4057
Brian Norris4aef9b72012-09-24 20:40:48 -07004058 /* Read entire ID string */
4059 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004060 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004061
Boris Brezillon7f501f02016-05-24 19:20:05 +02004062 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004063 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004064 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004065 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004066 }
4067
Boris Brezillon7f501f02016-05-24 19:20:05 +02004068 chip->id.len = nand_id_len(id_data, 8);
4069
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004070 /* Try to identify manufacturer */
4071 manufacturer = nand_get_manufacturer(maf_id);
4072 chip->manufacturer.desc = manufacturer;
4073
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004074 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004075 type = nand_flash_ids;
4076
Boris Brezillon29a198a2016-05-24 20:17:48 +02004077 /*
4078 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4079 * override it.
4080 * This is required to make sure initial NAND bus width set by the
4081 * NAND controller driver is coherent with the real NAND bus width
4082 * (extracted by auto-detection code).
4083 */
4084 busw = chip->options & NAND_BUSWIDTH_16;
4085
4086 /*
4087 * The flag is only set (never cleared), reset it to its default value
4088 * before starting auto-detection.
4089 */
4090 chip->options &= ~NAND_BUSWIDTH_16;
4091
Huang Shijieec6e87e2013-03-15 11:01:00 +08004092 for (; type->name != NULL; type++) {
4093 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004094 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004095 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004096 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004097 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004098 }
4099 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004100
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004101 chip->onfi_version = 0;
4102 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004103 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004104 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004105 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004106
4107 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004108 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004109 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004110 }
4111
David Woodhouse5e81e882010-02-26 18:32:56 +00004112 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004113 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004114
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004115 if (!mtd->name)
4116 mtd->name = type->name;
4117
Adrian Hunter69423d92008-12-10 13:37:21 +00004118 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004119
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004120 if (!type->pagesize)
4121 nand_manufacturer_detect(chip);
4122 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004123 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004124
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004125 /* Get chip options */
4126 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004127
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004128ident_done:
4129
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004130 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004131 WARN_ON(busw & NAND_BUSWIDTH_16);
4132 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004133 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4134 /*
4135 * Check, if buswidth is correct. Hardware drivers should set
4136 * chip correct!
4137 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004138 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004139 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004140 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4141 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004142 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4143 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004144 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004145 }
4146
Boris Brezillon7f501f02016-05-24 19:20:05 +02004147 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004148
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004149 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004150 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004151 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004152 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004153
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004154 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004155 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004156 if (chip->chipsize & 0xffffffff)
4157 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004158 else {
4159 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4160 chip->chip_shift += 32 - 1;
4161 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004162
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004163 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004164 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004165
Brian Norris8b6e50c2011-05-25 14:59:01 -07004166 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004167 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4168 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004169
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004170 ret = nand_manufacturer_init(chip);
4171 if (ret)
4172 return ret;
4173
Ezequiel Garcia20171642013-11-25 08:30:31 -03004174 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004175 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004176
4177 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004178 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4179 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004180 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004181 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4182 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004183 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004184 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4185 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004186
Rafał Miłecki3755a992014-10-21 00:01:04 +02004187 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004188 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004189 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004190 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004191}
4192
Boris Brezillond48f62b2016-04-01 14:54:32 +02004193static const char * const nand_ecc_modes[] = {
4194 [NAND_ECC_NONE] = "none",
4195 [NAND_ECC_SOFT] = "soft",
4196 [NAND_ECC_HW] = "hw",
4197 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4198 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004199 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004200};
4201
4202static int of_get_nand_ecc_mode(struct device_node *np)
4203{
4204 const char *pm;
4205 int err, i;
4206
4207 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4208 if (err < 0)
4209 return err;
4210
4211 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4212 if (!strcasecmp(pm, nand_ecc_modes[i]))
4213 return i;
4214
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004215 /*
4216 * For backward compatibility we support few obsoleted values that don't
4217 * have their mappings into nand_ecc_modes_t anymore (they were merged
4218 * with other enums).
4219 */
4220 if (!strcasecmp(pm, "soft_bch"))
4221 return NAND_ECC_SOFT;
4222
Boris Brezillond48f62b2016-04-01 14:54:32 +02004223 return -ENODEV;
4224}
4225
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004226static const char * const nand_ecc_algos[] = {
4227 [NAND_ECC_HAMMING] = "hamming",
4228 [NAND_ECC_BCH] = "bch",
4229};
4230
Boris Brezillond48f62b2016-04-01 14:54:32 +02004231static int of_get_nand_ecc_algo(struct device_node *np)
4232{
4233 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004234 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004235
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004236 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4237 if (!err) {
4238 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4239 if (!strcasecmp(pm, nand_ecc_algos[i]))
4240 return i;
4241 return -ENODEV;
4242 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004243
4244 /*
4245 * For backward compatibility we also read "nand-ecc-mode" checking
4246 * for some obsoleted values that were specifying ECC algorithm.
4247 */
4248 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4249 if (err < 0)
4250 return err;
4251
4252 if (!strcasecmp(pm, "soft"))
4253 return NAND_ECC_HAMMING;
4254 else if (!strcasecmp(pm, "soft_bch"))
4255 return NAND_ECC_BCH;
4256
4257 return -ENODEV;
4258}
4259
4260static int of_get_nand_ecc_step_size(struct device_node *np)
4261{
4262 int ret;
4263 u32 val;
4264
4265 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4266 return ret ? ret : val;
4267}
4268
4269static int of_get_nand_ecc_strength(struct device_node *np)
4270{
4271 int ret;
4272 u32 val;
4273
4274 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4275 return ret ? ret : val;
4276}
4277
4278static int of_get_nand_bus_width(struct device_node *np)
4279{
4280 u32 val;
4281
4282 if (of_property_read_u32(np, "nand-bus-width", &val))
4283 return 8;
4284
4285 switch (val) {
4286 case 8:
4287 case 16:
4288 return val;
4289 default:
4290 return -EIO;
4291 }
4292}
4293
4294static bool of_get_nand_on_flash_bbt(struct device_node *np)
4295{
4296 return of_property_read_bool(np, "nand-on-flash-bbt");
4297}
4298
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004299static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004300{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004301 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004302 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004303
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004304 if (!dn)
4305 return 0;
4306
Brian Norris5844fee2015-01-23 00:22:27 -08004307 if (of_get_nand_bus_width(dn) == 16)
4308 chip->options |= NAND_BUSWIDTH_16;
4309
4310 if (of_get_nand_on_flash_bbt(dn))
4311 chip->bbt_options |= NAND_BBT_USE_FLASH;
4312
4313 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004314 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004315 ecc_strength = of_get_nand_ecc_strength(dn);
4316 ecc_step = of_get_nand_ecc_step_size(dn);
4317
Brian Norris5844fee2015-01-23 00:22:27 -08004318 if (ecc_mode >= 0)
4319 chip->ecc.mode = ecc_mode;
4320
Rafał Miłecki79082452016-03-23 11:19:02 +01004321 if (ecc_algo >= 0)
4322 chip->ecc.algo = ecc_algo;
4323
Brian Norris5844fee2015-01-23 00:22:27 -08004324 if (ecc_strength >= 0)
4325 chip->ecc.strength = ecc_strength;
4326
4327 if (ecc_step > 0)
4328 chip->ecc.size = ecc_step;
4329
Boris Brezillonba78ee02016-06-08 17:04:22 +02004330 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4331 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4332
Brian Norris5844fee2015-01-23 00:22:27 -08004333 return 0;
4334}
4335
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004336/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004337 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004338 * @mtd: MTD device structure
4339 * @maxchips: number of chips to scan for
4340 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004341 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004342 * This is the first phase of the normal nand_scan() function. It reads the
4343 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004344 *
4345 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004346int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4347 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004348{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004349 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004350 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004351 int ret;
4352
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004353 ret = nand_dt_init(chip);
4354 if (ret)
4355 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004356
Brian Norrisf7a8e382016-01-05 10:39:45 -08004357 if (!mtd->name && mtd->dev.parent)
4358 mtd->name = dev_name(mtd->dev.parent);
4359
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004360 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4361 /*
4362 * Default functions assigned for chip_select() and
4363 * cmdfunc() both expect cmd_ctrl() to be populated,
4364 * so we need to check that that's the case
4365 */
4366 pr_err("chip.cmd_ctrl() callback is not provided");
4367 return -EINVAL;
4368 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004369 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004370 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004371
4372 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004373 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004374 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004375 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004376 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004377 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004378 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 }
4380
Boris Brezillon73f907f2016-10-24 16:46:20 +02004381 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004382 ret = nand_init_data_interface(chip);
4383 if (ret)
4384 return ret;
4385
Boris Brezillon73f907f2016-10-24 16:46:20 +02004386 /*
4387 * Setup the data interface correctly on the chip and controller side.
4388 * This explicit call to nand_setup_data_interface() is only required
4389 * for the first die, because nand_reset() has been called before
4390 * ->data_interface and ->default_onfi_timing_mode were set.
4391 * For the other dies, nand_reset() will automatically switch to the
4392 * best mode for us.
4393 */
Boris Brezillon104e4422017-03-16 09:35:58 +01004394 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004395 if (ret)
4396 return ret;
4397
Boris Brezillon7f501f02016-05-24 19:20:05 +02004398 nand_maf_id = chip->id.data[0];
4399 nand_dev_id = chip->id.data[1];
4400
Huang Shijie07300162012-11-09 16:23:45 +08004401 chip->select_chip(mtd, -1);
4402
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004403 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004404 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004405 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004406 nand_reset(chip, i);
4407
4408 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004410 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004412 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004413 nand_dev_id != chip->read_byte(mtd)) {
4414 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 break;
Huang Shijie07300162012-11-09 16:23:45 +08004416 }
4417 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
4419 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004420 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004423 chip->numchips = i;
4424 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
David Woodhouse3b85c322006-09-25 17:06:53 +01004426 return 0;
4427}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004428EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004429
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004430static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4431{
4432 struct nand_chip *chip = mtd_to_nand(mtd);
4433 struct nand_ecc_ctrl *ecc = &chip->ecc;
4434
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004435 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004436 return -EINVAL;
4437
4438 switch (ecc->algo) {
4439 case NAND_ECC_HAMMING:
4440 ecc->calculate = nand_calculate_ecc;
4441 ecc->correct = nand_correct_data;
4442 ecc->read_page = nand_read_page_swecc;
4443 ecc->read_subpage = nand_read_subpage;
4444 ecc->write_page = nand_write_page_swecc;
4445 ecc->read_page_raw = nand_read_page_raw;
4446 ecc->write_page_raw = nand_write_page_raw;
4447 ecc->read_oob = nand_read_oob_std;
4448 ecc->write_oob = nand_write_oob_std;
4449 if (!ecc->size)
4450 ecc->size = 256;
4451 ecc->bytes = 3;
4452 ecc->strength = 1;
4453 return 0;
4454 case NAND_ECC_BCH:
4455 if (!mtd_nand_has_bch()) {
4456 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4457 return -EINVAL;
4458 }
4459 ecc->calculate = nand_bch_calculate_ecc;
4460 ecc->correct = nand_bch_correct_data;
4461 ecc->read_page = nand_read_page_swecc;
4462 ecc->read_subpage = nand_read_subpage;
4463 ecc->write_page = nand_write_page_swecc;
4464 ecc->read_page_raw = nand_read_page_raw;
4465 ecc->write_page_raw = nand_write_page_raw;
4466 ecc->read_oob = nand_read_oob_std;
4467 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004468
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004469 /*
4470 * Board driver should supply ecc.size and ecc.strength
4471 * values to select how many bits are correctable.
4472 * Otherwise, default to 4 bits for large page devices.
4473 */
4474 if (!ecc->size && (mtd->oobsize >= 64)) {
4475 ecc->size = 512;
4476 ecc->strength = 4;
4477 }
4478
4479 /*
4480 * if no ecc placement scheme was provided pickup the default
4481 * large page one.
4482 */
4483 if (!mtd->ooblayout) {
4484 /* handle large page devices only */
4485 if (mtd->oobsize < 64) {
4486 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4487 return -EINVAL;
4488 }
4489
4490 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004491
4492 }
4493
4494 /*
4495 * We can only maximize ECC config when the default layout is
4496 * used, otherwise we don't know how many bytes can really be
4497 * used.
4498 */
4499 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4500 ecc->options & NAND_ECC_MAXIMIZE) {
4501 int steps, bytes;
4502
4503 /* Always prefer 1k blocks over 512bytes ones */
4504 ecc->size = 1024;
4505 steps = mtd->writesize / ecc->size;
4506
4507 /* Reserve 2 bytes for the BBM */
4508 bytes = (mtd->oobsize - 2) / steps;
4509 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004510 }
4511
4512 /* See nand_bch_init() for details. */
4513 ecc->bytes = 0;
4514 ecc->priv = nand_bch_init(mtd);
4515 if (!ecc->priv) {
4516 WARN(1, "BCH ECC initialization failed!\n");
4517 return -EINVAL;
4518 }
4519 return 0;
4520 default:
4521 WARN(1, "Unsupported ECC algorithm!\n");
4522 return -EINVAL;
4523 }
4524}
4525
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004526/*
4527 * Check if the chip configuration meet the datasheet requirements.
4528
4529 * If our configuration corrects A bits per B bytes and the minimum
4530 * required correction level is X bits per Y bytes, then we must ensure
4531 * both of the following are true:
4532 *
4533 * (1) A / B >= X / Y
4534 * (2) A >= X
4535 *
4536 * Requirement (1) ensures we can correct for the required bitflip density.
4537 * Requirement (2) ensures we can correct even when all bitflips are clumped
4538 * in the same sector.
4539 */
4540static bool nand_ecc_strength_good(struct mtd_info *mtd)
4541{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004542 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004543 struct nand_ecc_ctrl *ecc = &chip->ecc;
4544 int corr, ds_corr;
4545
4546 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4547 /* Not enough information */
4548 return true;
4549
4550 /*
4551 * We get the number of corrected bits per page to compare
4552 * the correction density.
4553 */
4554 corr = (mtd->writesize * ecc->strength) / ecc->size;
4555 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4556
4557 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4558}
David Woodhouse3b85c322006-09-25 17:06:53 +01004559
Marc Gonzalez3371d662016-11-15 10:56:20 +01004560static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4561{
4562 struct nand_ecc_ctrl *ecc = &chip->ecc;
4563
4564 if (nand_standard_page_accessors(ecc))
4565 return false;
4566
4567 /*
4568 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4569 * controller driver implements all the page accessors because
4570 * default helpers are not suitable when the core does not
4571 * send the READ0/PAGEPROG commands.
4572 */
4573 return (!ecc->read_page || !ecc->write_page ||
4574 !ecc->read_page_raw || !ecc->write_page_raw ||
4575 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4576 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4577 ecc->hwctl && ecc->calculate));
4578}
4579
David Woodhouse3b85c322006-09-25 17:06:53 +01004580/**
4581 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004582 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004583 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004584 * This is the second phase of the normal nand_scan() function. It fills out
4585 * all the uninitialized function pointers with the defaults and scans for a
4586 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004587 */
4588int nand_scan_tail(struct mtd_info *mtd)
4589{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004590 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004591 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004592 struct nand_buffers *nbuf = NULL;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004593 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004594
Brian Norrise2414f42012-02-06 13:44:00 -08004595 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004596 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4597 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4598 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004599
Marc Gonzalez3371d662016-11-15 10:56:20 +01004600 if (invalid_ecc_page_accessors(chip)) {
4601 pr_err("Invalid ECC page accessors setup\n");
4602 return -EINVAL;
4603 }
4604
Huang Shijief02ea4e2014-01-13 14:27:12 +08004605 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004606 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Huang Shijief02ea4e2014-01-13 14:27:12 +08004607 if (!nbuf)
4608 return -ENOMEM;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004609
4610 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4611 if (!nbuf->ecccalc) {
4612 ret = -ENOMEM;
4613 goto err_free;
4614 }
4615
4616 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4617 if (!nbuf->ecccode) {
4618 ret = -ENOMEM;
4619 goto err_free;
4620 }
4621
4622 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4623 GFP_KERNEL);
4624 if (!nbuf->databuf) {
4625 ret = -ENOMEM;
4626 goto err_free;
4627 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004628
4629 chip->buffers = nbuf;
4630 } else {
4631 if (!chip->buffers)
4632 return -ENOMEM;
4633 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004634
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004635 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004636 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004637
4638 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004639 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004640 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004641 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004642 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004643 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004646 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 break;
4648 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004649 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004650 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004651 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004653 WARN(1, "No oob scheme defined for oobsize %d\n",
4654 mtd->oobsize);
4655 ret = -EINVAL;
4656 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
4658 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004659
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004660 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004661 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004662 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004663 */
David Woodhouse956e9442006-09-25 17:12:39 +01004664
Huang Shijie97de79e02013-10-18 14:20:53 +08004665 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004666 case NAND_ECC_HW_OOB_FIRST:
4667 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004668 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004669 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4670 ret = -EINVAL;
4671 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004672 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004673 if (!ecc->read_page)
4674 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004675
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004676 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004677 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004678 if (!ecc->read_page)
4679 ecc->read_page = nand_read_page_hwecc;
4680 if (!ecc->write_page)
4681 ecc->write_page = nand_write_page_hwecc;
4682 if (!ecc->read_page_raw)
4683 ecc->read_page_raw = nand_read_page_raw;
4684 if (!ecc->write_page_raw)
4685 ecc->write_page_raw = nand_write_page_raw;
4686 if (!ecc->read_oob)
4687 ecc->read_oob = nand_read_oob_std;
4688 if (!ecc->write_oob)
4689 ecc->write_oob = nand_write_oob_std;
4690 if (!ecc->read_subpage)
4691 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004692 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004693 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004694
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004695 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004696 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4697 (!ecc->read_page ||
4698 ecc->read_page == nand_read_page_hwecc ||
4699 !ecc->write_page ||
4700 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004701 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4702 ret = -EINVAL;
4703 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004704 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004705 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004706 if (!ecc->read_page)
4707 ecc->read_page = nand_read_page_syndrome;
4708 if (!ecc->write_page)
4709 ecc->write_page = nand_write_page_syndrome;
4710 if (!ecc->read_page_raw)
4711 ecc->read_page_raw = nand_read_page_raw_syndrome;
4712 if (!ecc->write_page_raw)
4713 ecc->write_page_raw = nand_write_page_raw_syndrome;
4714 if (!ecc->read_oob)
4715 ecc->read_oob = nand_read_oob_syndrome;
4716 if (!ecc->write_oob)
4717 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004718
Huang Shijie97de79e02013-10-18 14:20:53 +08004719 if (mtd->writesize >= ecc->size) {
4720 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004721 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4722 ret = -EINVAL;
4723 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004724 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004725 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004726 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004727 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4728 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004729 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004730 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004732 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004733 ret = nand_set_ecc_soft_ops(mtd);
4734 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004735 ret = -EINVAL;
4736 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004737 }
4738 break;
4739
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004740 case NAND_ECC_ON_DIE:
4741 if (!ecc->read_page || !ecc->write_page) {
4742 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4743 ret = -EINVAL;
4744 goto err_free;
4745 }
4746 if (!ecc->read_oob)
4747 ecc->read_oob = nand_read_oob_std;
4748 if (!ecc->write_oob)
4749 ecc->write_oob = nand_write_oob_std;
4750 break;
4751
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004752 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004753 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004754 ecc->read_page = nand_read_page_raw;
4755 ecc->write_page = nand_write_page_raw;
4756 ecc->read_oob = nand_read_oob_std;
4757 ecc->read_page_raw = nand_read_page_raw;
4758 ecc->write_page_raw = nand_write_page_raw;
4759 ecc->write_oob = nand_write_oob_std;
4760 ecc->size = mtd->writesize;
4761 ecc->bytes = 0;
4762 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004764
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004766 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4767 ret = -EINVAL;
4768 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Brian Norris9ce244b2011-08-30 18:45:37 -07004771 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004772 if (!ecc->read_oob_raw)
4773 ecc->read_oob_raw = ecc->read_oob;
4774 if (!ecc->write_oob_raw)
4775 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004776
Boris Brezillon846031d2016-02-03 20:11:00 +01004777 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004778 mtd->ecc_strength = ecc->strength;
4779 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004780
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004781 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004782 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004783 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004784 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004785 ecc->steps = mtd->writesize / ecc->size;
4786 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004787 WARN(1, "Invalid ECC parameters\n");
4788 ret = -EINVAL;
4789 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004791 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09004792 if (ecc->total > mtd->oobsize) {
4793 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4794 ret = -EINVAL;
4795 goto err_free;
4796 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004797
Boris Brezillon846031d2016-02-03 20:11:00 +01004798 /*
4799 * The number of bytes available for a client to place data into
4800 * the out of band area.
4801 */
4802 ret = mtd_ooblayout_count_freebytes(mtd);
4803 if (ret < 0)
4804 ret = 0;
4805
4806 mtd->oobavail = ret;
4807
4808 /* ECC sanity check: warn if it's too weak */
4809 if (!nand_ecc_strength_good(mtd))
4810 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4811 mtd->name);
4812
Brian Norris8b6e50c2011-05-25 14:59:01 -07004813 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004814 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004815 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004816 case 2:
4817 mtd->subpage_sft = 1;
4818 break;
4819 case 4:
4820 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004821 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004822 mtd->subpage_sft = 2;
4823 break;
4824 }
4825 }
4826 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4827
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004828 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004829 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004832 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004834 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304835 switch (ecc->mode) {
4836 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304837 if (chip->page_shift > 9)
4838 chip->options |= NAND_SUBPAGE_READ;
4839 break;
4840
4841 default:
4842 break;
4843 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004846 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004847 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4848 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004849 mtd->_erase = nand_erase;
4850 mtd->_point = NULL;
4851 mtd->_unpoint = NULL;
4852 mtd->_read = nand_read;
4853 mtd->_write = nand_write;
4854 mtd->_panic_write = panic_nand_write;
4855 mtd->_read_oob = nand_read_oob;
4856 mtd->_write_oob = nand_write_oob;
4857 mtd->_sync = nand_sync;
4858 mtd->_lock = NULL;
4859 mtd->_unlock = NULL;
4860 mtd->_suspend = nand_suspend;
4861 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004862 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004863 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004864 mtd->_block_isbad = nand_block_isbad;
4865 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06004866 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004867 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004869 /*
4870 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4871 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4872 * properly set.
4873 */
4874 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004875 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004877 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004878 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004882 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004883err_free:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004884 if (nbuf) {
4885 kfree(nbuf->databuf);
4886 kfree(nbuf->ecccode);
4887 kfree(nbuf->ecccalc);
4888 kfree(nbuf);
4889 }
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004892EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Brian Norris8b6e50c2011-05-25 14:59:01 -07004894/*
4895 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004896 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004897 * to call us from in-kernel code if the core NAND support is modular.
4898 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004899#ifdef MODULE
4900#define caller_is_module() (1)
4901#else
4902#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004903 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004904#endif
4905
4906/**
4907 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004908 * @mtd: MTD device structure
4909 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004910 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004911 * This fills out all the uninitialized function pointers with the defaults.
4912 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004913 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004914 */
4915int nand_scan(struct mtd_info *mtd, int maxchips)
4916{
4917 int ret;
4918
David Woodhouse5e81e882010-02-26 18:32:56 +00004919 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004920 if (!ret)
4921 ret = nand_scan_tail(mtd);
4922 return ret;
4923}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004924EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004927 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4928 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004929 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004930void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004932 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004933 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004934 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4935
Boris Brezillond8e725d2016-09-15 10:32:50 +02004936 nand_release_data_interface(chip);
4937
Jesper Juhlfa671642005-11-07 01:01:27 -08004938 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004939 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004940 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
4941 kfree(chip->buffers->databuf);
4942 kfree(chip->buffers->ecccode);
4943 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004944 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004945 }
Brian Norris58373ff2010-07-15 12:15:44 -07004946
4947 /* Free bad block descriptor memory */
4948 if (chip->badblock_pattern && chip->badblock_pattern->options
4949 & NAND_BBT_DYNAMICSTRUCT)
4950 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004951
4952 /* Free manufacturer priv data. */
4953 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004955EXPORT_SYMBOL_GPL(nand_cleanup);
4956
4957/**
4958 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4959 * held by the NAND device
4960 * @mtd: MTD device structure
4961 */
4962void nand_release(struct mtd_info *mtd)
4963{
4964 mtd_device_unregister(mtd);
4965 nand_cleanup(mtd_to_nand(mtd));
4966}
David Woodhousee0c7d762006-05-13 18:07:53 +01004967EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004968
David Woodhousee0c7d762006-05-13 18:07:53 +01004969MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004970MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4971MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004972MODULE_DESCRIPTION("Generic NAND flash driver code");