blob: 5fb45161789ce012e928c8ec05f589465a33ca93 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020048#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010049
Huang Shijie6a8214a2012-11-19 14:43:30 +080050static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020052static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
53 struct mtd_oob_ops *ops);
54
Boris Brezillon41b207a2016-02-03 19:06:15 +010055/* Define default oob placement schemes for large and small page devices */
56static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
57 struct mtd_oob_region *oobregion)
58{
59 struct nand_chip *chip = mtd_to_nand(mtd);
60 struct nand_ecc_ctrl *ecc = &chip->ecc;
61
62 if (section > 1)
63 return -ERANGE;
64
65 if (!section) {
66 oobregion->offset = 0;
Miquel Raynal160c3652017-07-05 08:51:09 +020067 if (mtd->oobsize == 16)
68 oobregion->length = 4;
69 else
70 oobregion->length = 3;
Boris Brezillon41b207a2016-02-03 19:06:15 +010071 } else {
Miquel Raynal160c3652017-07-05 08:51:09 +020072 if (mtd->oobsize == 8)
73 return -ERANGE;
74
Boris Brezillon41b207a2016-02-03 19:06:15 +010075 oobregion->offset = 6;
76 oobregion->length = ecc->total - 4;
77 }
78
79 return 0;
80}
81
82static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
83 struct mtd_oob_region *oobregion)
84{
85 if (section > 1)
86 return -ERANGE;
87
88 if (mtd->oobsize == 16) {
89 if (section)
90 return -ERANGE;
91
92 oobregion->length = 8;
93 oobregion->offset = 8;
94 } else {
95 oobregion->length = 2;
96 if (!section)
97 oobregion->offset = 3;
98 else
99 oobregion->offset = 6;
100 }
101
102 return 0;
103}
104
105const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
106 .ecc = nand_ooblayout_ecc_sp,
107 .free = nand_ooblayout_free_sp,
108};
109EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
110
111static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
112 struct mtd_oob_region *oobregion)
113{
114 struct nand_chip *chip = mtd_to_nand(mtd);
115 struct nand_ecc_ctrl *ecc = &chip->ecc;
116
117 if (section)
118 return -ERANGE;
119
120 oobregion->length = ecc->total;
121 oobregion->offset = mtd->oobsize - oobregion->length;
122
123 return 0;
124}
125
126static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
127 struct mtd_oob_region *oobregion)
128{
129 struct nand_chip *chip = mtd_to_nand(mtd);
130 struct nand_ecc_ctrl *ecc = &chip->ecc;
131
132 if (section)
133 return -ERANGE;
134
135 oobregion->length = mtd->oobsize - ecc->total - 2;
136 oobregion->offset = 2;
137
138 return 0;
139}
140
141const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
142 .ecc = nand_ooblayout_ecc_lp,
143 .free = nand_ooblayout_free_lp,
144};
145EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200146
Alexander Couzens5956b282017-05-02 12:19:00 +0200147/*
148 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
149 * are placed at a fixed offset.
150 */
151static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
152 struct mtd_oob_region *oobregion)
153{
154 struct nand_chip *chip = mtd_to_nand(mtd);
155 struct nand_ecc_ctrl *ecc = &chip->ecc;
156
157 if (section)
158 return -ERANGE;
159
160 switch (mtd->oobsize) {
161 case 64:
162 oobregion->offset = 40;
163 break;
164 case 128:
165 oobregion->offset = 80;
166 break;
167 default:
168 return -EINVAL;
169 }
170
171 oobregion->length = ecc->total;
172 if (oobregion->offset + oobregion->length > mtd->oobsize)
173 return -ERANGE;
174
175 return 0;
176}
177
178static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
179 struct mtd_oob_region *oobregion)
180{
181 struct nand_chip *chip = mtd_to_nand(mtd);
182 struct nand_ecc_ctrl *ecc = &chip->ecc;
183 int ecc_offset = 0;
184
185 if (section < 0 || section > 1)
186 return -ERANGE;
187
188 switch (mtd->oobsize) {
189 case 64:
190 ecc_offset = 40;
191 break;
192 case 128:
193 ecc_offset = 80;
194 break;
195 default:
196 return -EINVAL;
197 }
198
199 if (section == 0) {
200 oobregion->offset = 2;
201 oobregion->length = ecc_offset - 2;
202 } else {
203 oobregion->offset = ecc_offset + ecc->total;
204 oobregion->length = mtd->oobsize - oobregion->offset;
205 }
206
207 return 0;
208}
209
210const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
211 .ecc = nand_ooblayout_ecc_lp_hamming,
212 .free = nand_ooblayout_free_lp_hamming,
213};
214
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530215static int check_offs_len(struct mtd_info *mtd,
216 loff_t ofs, uint64_t len)
217{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100218 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 int ret = 0;
220
221 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300222 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700223 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530224 ret = -EINVAL;
225 }
226
227 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300228 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700229 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530230 ret = -EINVAL;
231 }
232
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530233 return ret;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/**
237 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700238 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000239 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800240 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100242static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100244 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200246 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200247 spin_lock(&chip->controller->lock);
248 chip->controller->active = NULL;
249 chip->state = FL_READY;
250 wake_up(&chip->controller->wq);
251 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/**
255 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700256 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700258 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200260static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100262 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200263 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Masanari Iida064a7692012-11-09 23:20:58 +0900267 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700268 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700270 * Default read function for 16bit buswidth with endianness conversion.
271 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200273static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100275 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200276 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700281 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700283 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285static u16 nand_read_word(struct mtd_info *mtd)
286{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100287 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200288 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700293 * @mtd: MTD device structure
294 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *
296 * Default select function for 1 chip devices.
297 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200298static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100300 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200301
302 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200304 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308
309 default:
310 BUG();
311 }
312}
313
314/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100315 * nand_write_byte - [DEFAULT] write single byte to chip
316 * @mtd: MTD device structure
317 * @byte: value to write
318 *
319 * Default function to write a byte to I/O[7:0]
320 */
321static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
322{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100323 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100324
325 chip->write_buf(mtd, &byte, 1);
326}
327
328/**
329 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
330 * @mtd: MTD device structure
331 * @byte: value to write
332 *
333 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
334 */
335static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
336{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100337 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100338 uint16_t word = byte;
339
340 /*
341 * It's not entirely clear what should happen to I/O[15:8] when writing
342 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
343 *
344 * When the host supports a 16-bit bus width, only data is
345 * transferred at the 16-bit width. All address and command line
346 * transfers shall use only the lower 8-bits of the data bus. During
347 * command transfers, the host may place any value on the upper
348 * 8-bits of the data bus. During address transfers, the host shall
349 * set the upper 8-bits of the data bus to 00h.
350 *
351 * One user of the write_byte callback is nand_onfi_set_features. The
352 * four parameters are specified to be written to I/O[7:0], but this is
353 * neither an address nor a command transfer. Let's assume a 0 on the
354 * upper I/O lines is OK.
355 */
356 chip->write_buf(mtd, (uint8_t *)&word, 2);
357}
358
359/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700361 * @mtd: MTD device structure
362 * @buf: data buffer
363 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700365 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200367static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100369 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Alexander Shiyan76413832013-04-13 09:32:13 +0400371 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000375 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700376 * @mtd: MTD device structure
377 * @buf: buffer to store date
378 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700380 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200382static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100384 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Alexander Shiyan76413832013-04-13 09:32:13 +0400386 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700391 * @mtd: MTD device structure
392 * @buf: data buffer
393 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700395 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200397static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100399 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000401
Alexander Shiyan76413832013-04-13 09:32:13 +0400402 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000406 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700407 * @mtd: MTD device structure
408 * @buf: buffer to store date
409 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700411 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200413static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100415 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alexander Shiyan76413832013-04-13 09:32:13 +0400418 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700423 * @mtd: MTD device structure
424 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000426 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530428static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530430 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100431 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 u16 bad;
433
Brian Norris5fb15492011-05-31 16:31:21 -0700434 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700435 ofs += mtd->erasesize - mtd->writesize;
436
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100437 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
438
Brian Norriscdbec052012-01-13 18:11:48 -0800439 do {
440 if (chip->options & NAND_BUSWIDTH_16) {
441 chip->cmdfunc(mtd, NAND_CMD_READOOB,
442 chip->badblockpos & 0xFE, page);
443 bad = cpu_to_le16(chip->read_word(mtd));
444 if (chip->badblockpos & 0x1)
445 bad >>= 8;
446 else
447 bad &= 0xFF;
448 } else {
449 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
450 page);
451 bad = chip->read_byte(mtd);
452 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000453
Brian Norriscdbec052012-01-13 18:11:48 -0800454 if (likely(chip->badblockbits == 8))
455 res = bad != 0xFF;
456 else
457 res = hweight8(bad) < chip->badblockbits;
458 ofs += mtd->writesize;
459 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
460 i++;
461 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return res;
464}
465
466/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700467 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700468 * @mtd: MTD device structure
469 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700471 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700472 * specific driver. It provides the details for writing a bad block marker to a
473 * block.
474 */
475static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
476{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100477 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700478 struct mtd_oob_ops ops;
479 uint8_t buf[2] = { 0, 0 };
480 int ret = 0, res, i = 0;
481
Brian Norris0ec56dc2015-02-28 02:02:30 -0800482 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700483 ops.oobbuf = buf;
484 ops.ooboffs = chip->badblockpos;
485 if (chip->options & NAND_BUSWIDTH_16) {
486 ops.ooboffs &= ~0x01;
487 ops.len = ops.ooblen = 2;
488 } else {
489 ops.len = ops.ooblen = 1;
490 }
491 ops.mode = MTD_OPS_PLACE_OOB;
492
493 /* Write to first/last page(s) if necessary */
494 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
495 ofs += mtd->erasesize - mtd->writesize;
496 do {
497 res = nand_do_write_oob(mtd, ofs, &ops);
498 if (!ret)
499 ret = res;
500
501 i++;
502 ofs += mtd->writesize;
503 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
504
505 return ret;
506}
507
508/**
509 * nand_block_markbad_lowlevel - mark a block bad
510 * @mtd: MTD device structure
511 * @ofs: offset from device start
512 *
513 * This function performs the generic NAND bad block marking steps (i.e., bad
514 * block table(s) and/or marker(s)). We only allow the hardware driver to
515 * specify how to write bad block markers to OOB (chip->block_markbad).
516 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800518 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700519 * (2) write bad block marker to OOB area of affected block (unless flag
520 * NAND_BBT_NO_OOB_BBM is present)
521 * (3) update the BBT
522 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800523 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700525static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100527 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700528 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000529
Brian Norrisb32843b2013-07-30 17:52:59 -0700530 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800531 struct erase_info einfo;
532
533 /* Attempt erase before marking OOB */
534 memset(&einfo, 0, sizeof(einfo));
535 einfo.mtd = mtd;
536 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300537 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800538 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800539
Brian Norrisb32843b2013-07-30 17:52:59 -0700540 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800541 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700542 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300543 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200544 }
Brian Norrise2414f42012-02-06 13:44:00 -0800545
Brian Norrisb32843b2013-07-30 17:52:59 -0700546 /* Mark block bad in BBT */
547 if (chip->bbt) {
548 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800549 if (!ret)
550 ret = res;
551 }
552
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200553 if (!ret)
554 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300555
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200556 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000559/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700561 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700563 * Check, if the device is write protected. The function expects, that the
564 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100566static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100568 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200569
Brian Norris8b6e50c2011-05-25 14:59:01 -0700570 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200571 if (chip->options & NAND_BROKEN_XD)
572 return 0;
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200575 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
576 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800580 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700581 * @mtd: MTD device structure
582 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300583 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800584 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300585 */
586static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
587{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100588 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300589
590 if (!chip->bbt)
591 return 0;
592 /* Return info from the table */
593 return nand_isreserved_bbt(mtd, ofs);
594}
595
596/**
597 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
598 * @mtd: MTD device structure
599 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700600 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
602 * Check, if the block is bad. Either by reading the bad block table or
603 * calling of the scan function.
604 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530605static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100607 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530610 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100613 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616/**
617 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200620 *
621 * Helper function for nand_wait_ready used when needing to wait in interrupt
622 * context.
623 */
624static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
625{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100626 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200627 int i;
628
629 /* Wait for the device to get ready */
630 for (i = 0; i < timeo; i++) {
631 if (chip->dev_ready(mtd))
632 break;
633 touch_softlockup_watchdog();
634 mdelay(1);
635 }
636}
637
Alex Smithb70af9b2015-10-06 14:52:07 +0100638/**
639 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
640 * @mtd: MTD device structure
641 *
642 * Wait for the ready pin after a command, and warn if a timeout occurs.
643 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100644void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000645{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100646 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100647 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200649 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100650 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200651
Brian Norris7854d3f2011-06-23 14:12:08 -0700652 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100653 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000654 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300656 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100657 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100659
Brian Norris9ebfdf52016-03-04 17:19:23 -0800660 if (!chip->dev_ready(mtd))
661 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000662}
David Woodhouse4b648b02006-09-25 17:05:24 +0100663EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200666 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
667 * @mtd: MTD device structure
668 * @timeo: Timeout in ms
669 *
670 * Wait for status ready (i.e. command done) or timeout.
671 */
672static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
673{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100674 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200675
676 timeo = jiffies + msecs_to_jiffies(timeo);
677 do {
678 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
679 break;
680 touch_softlockup_watchdog();
681 } while (time_before(jiffies, timeo));
682};
683
684/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700686 * @mtd: MTD device structure
687 * @command: the command to be sent
688 * @column: the column address for this command, -1 if none
689 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700691 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200692 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200694static void nand_command(struct mtd_info *mtd, unsigned int command,
695 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100697 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200698 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Brian Norris8b6e50c2011-05-25 14:59:01 -0700700 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (command == NAND_CMD_SEQIN) {
702 int readcmd;
703
Joern Engel28318772006-05-22 23:18:05 +0200704 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200706 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 readcmd = NAND_CMD_READOOB;
708 } else if (column < 256) {
709 /* First 256 bytes --> READ0 */
710 readcmd = NAND_CMD_READ0;
711 } else {
712 column -= 256;
713 readcmd = NAND_CMD_READ1;
714 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200715 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Miquel Raynalda2ad1d2017-11-08 17:00:27 +0100718 if (command != NAND_CMD_NONE)
719 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Brian Norris8b6e50c2011-05-25 14:59:01 -0700721 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
723 /* Serially input address */
724 if (column != -1) {
725 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800726 if (chip->options & NAND_BUSWIDTH_16 &&
727 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200728 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200729 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200730 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200732 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200733 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200734 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200735 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200736 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200737 if (chip->chipsize > (32 << 20))
738 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200739 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200740 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741
742 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700743 * Program and erase have their own busy handlers status and sequential
744 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000747
Miquel Raynalda2ad1d2017-11-08 17:00:27 +0100748 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 case NAND_CMD_PAGEPROG:
750 case NAND_CMD_ERASE1:
751 case NAND_CMD_ERASE2:
752 case NAND_CMD_SEQIN:
753 case NAND_CMD_STATUS:
754 return;
755
756 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200757 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200759 udelay(chip->chip_delay);
760 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200761 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200762 chip->cmd_ctrl(mtd,
763 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200764 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
765 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return;
767
David Woodhousee0c7d762006-05-13 18:07:53 +0100768 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000770 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * If we don't have access to the busy pin, we apply the given
772 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100773 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200774 if (!chip->dev_ready) {
775 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700779 /*
780 * Apply this short delay always to ensure that we do wait tWB in
781 * any case on any machine.
782 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100783 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000784
785 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/**
789 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700790 * @mtd: MTD device structure
791 * @command: the command to be sent
792 * @column: the column address for this command, -1 if none
793 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200795 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700796 * devices. We don't have the separate regions as we have in the small page
797 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200799static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
800 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100802 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* Emulate NAND_CMD_READOOB */
805 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200806 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 command = NAND_CMD_READ0;
808 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000809
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200810 /* Command latch cycle */
Miquel Raynalda2ad1d2017-11-08 17:00:27 +0100811 if (command != NAND_CMD_NONE)
812 chip->cmd_ctrl(mtd, command,
813 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200816 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Serially input address */
819 if (column != -1) {
820 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800821 if (chip->options & NAND_BUSWIDTH_16 &&
822 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200824 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200825 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200826
Brian Norrisf5b88de2016-10-03 09:49:35 -0700827 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200828 if (!nand_opcode_8bits(command))
829 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200832 chip->cmd_ctrl(mtd, page_addr, ctrl);
833 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200834 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200836 if (chip->chipsize > (128 << 20))
837 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200838 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200841 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000842
843 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700844 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100845 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000848
Miquel Raynalda2ad1d2017-11-08 17:00:27 +0100849 case NAND_CMD_NONE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 case NAND_CMD_CACHEDPROG:
851 case NAND_CMD_PAGEPROG:
852 case NAND_CMD_ERASE1:
853 case NAND_CMD_ERASE2:
854 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200855 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000857 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200860 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200862 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200863 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
864 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
865 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
866 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200867 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
868 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return;
870
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200871 case NAND_CMD_RNDOUT:
872 /* No ready / busy check necessary */
873 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
874 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
875 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
876 NAND_NCE | NAND_CTRL_CHANGE);
877 return;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200880 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
881 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
882 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
883 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000884
David Woodhousee0c7d762006-05-13 18:07:53 +0100885 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000887 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700889 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100890 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200891 if (!chip->dev_ready) {
892 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000896
Brian Norris8b6e50c2011-05-25 14:59:01 -0700897 /*
898 * Apply this short delay always to ensure that we do wait tWB in
899 * any case on any machine.
900 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100901 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000902
903 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200907 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700908 * @chip: the nand chip descriptor
909 * @mtd: MTD device structure
910 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200911 *
912 * Used when in panic, no locks are taken.
913 */
914static void panic_nand_get_device(struct nand_chip *chip,
915 struct mtd_info *mtd, int new_state)
916{
Brian Norris7854d3f2011-06-23 14:12:08 -0700917 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200918 chip->controller->active = chip;
919 chip->state = new_state;
920}
921
922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700924 * @mtd: MTD device structure
925 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * Get the device and lock it for exclusive access
928 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200929static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800930nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100932 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200933 spinlock_t *lock = &chip->controller->lock;
934 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100935 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200936retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100937 spin_lock(lock);
938
vimal singhb8b3ee92009-07-09 20:41:22 +0530939 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200940 if (!chip->controller->active)
941 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200942
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200943 if (chip->controller->active == chip && chip->state == FL_READY) {
944 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100945 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100946 return 0;
947 }
948 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800949 if (chip->controller->active->state == FL_PM_SUSPENDED) {
950 chip->state = FL_PM_SUSPENDED;
951 spin_unlock(lock);
952 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800953 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100954 }
955 set_current_state(TASK_UNINTERRUPTIBLE);
956 add_wait_queue(wq, &wait);
957 spin_unlock(lock);
958 schedule();
959 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 goto retry;
961}
962
963/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700964 * panic_nand_wait - [GENERIC] wait until the command is done
965 * @mtd: MTD device structure
966 * @chip: NAND chip structure
967 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200968 *
969 * Wait for command done. This is a helper function for nand_wait used when
970 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400971 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200972 */
973static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
974 unsigned long timeo)
975{
976 int i;
977 for (i = 0; i < timeo; i++) {
978 if (chip->dev_ready) {
979 if (chip->dev_ready(mtd))
980 break;
981 } else {
982 if (chip->read_byte(mtd) & NAND_STATUS_READY)
983 break;
984 }
985 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200986 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200987}
988
989/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700990 * nand_wait - [DEFAULT] wait until the command is done
991 * @mtd: MTD device structure
992 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100994 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700995 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200996static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998
Alex Smithb70af9b2015-10-06 14:52:07 +0100999 int status;
1000 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Brian Norris8b6e50c2011-05-25 14:59:01 -07001002 /*
1003 * Apply this short delay always to ensure that we do wait tWB in any
1004 * case on any machine.
1005 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001006 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001008 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001010 if (in_interrupt() || oops_in_progress)
1011 panic_nand_wait(mtd, chip, timeo);
1012 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001013 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001014 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001015 if (chip->dev_ready) {
1016 if (chip->dev_ready(mtd))
1017 break;
1018 } else {
1019 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1020 break;
1021 }
1022 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001023 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001025
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001026 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001027 /* This can happen if in case of timeout or buggy dev_ready */
1028 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return status;
1030}
1031
1032/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001033 * nand_reset_data_interface - Reset data interface and timings
1034 * @chip: The NAND chip
1035 *
1036 * Reset the Data interface and timings to ONFI mode 0.
1037 *
1038 * Returns 0 for success or negative error code otherwise.
1039 */
1040static int nand_reset_data_interface(struct nand_chip *chip)
1041{
1042 struct mtd_info *mtd = nand_to_mtd(chip);
1043 const struct nand_data_interface *conf;
1044 int ret;
1045
1046 if (!chip->setup_data_interface)
1047 return 0;
1048
1049 /*
1050 * The ONFI specification says:
1051 * "
1052 * To transition from NV-DDR or NV-DDR2 to the SDR data
1053 * interface, the host shall use the Reset (FFh) command
1054 * using SDR timing mode 0. A device in any timing mode is
1055 * required to recognize Reset (FFh) command issued in SDR
1056 * timing mode 0.
1057 * "
1058 *
1059 * Configure the data interface in SDR mode and set the
1060 * timings to timing mode 0.
1061 */
1062
1063 conf = nand_get_default_data_interface();
1064 ret = chip->setup_data_interface(mtd, conf, false);
1065 if (ret)
1066 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1067
1068 return ret;
1069}
1070
1071/**
1072 * nand_setup_data_interface - Setup the best data interface and timings
1073 * @chip: The NAND chip
1074 *
1075 * Find and configure the best data interface and NAND timings supported by
1076 * the chip and the driver.
1077 * First tries to retrieve supported timing modes from ONFI information,
1078 * and if the NAND chip does not support ONFI, relies on the
1079 * ->onfi_timing_mode_default specified in the nand_ids table.
1080 *
1081 * Returns 0 for success or negative error code otherwise.
1082 */
1083static int nand_setup_data_interface(struct nand_chip *chip)
1084{
1085 struct mtd_info *mtd = nand_to_mtd(chip);
1086 int ret;
1087
1088 if (!chip->setup_data_interface || !chip->data_interface)
1089 return 0;
1090
1091 /*
1092 * Ensure the timing mode has been changed on the chip side
1093 * before changing timings on the controller side.
1094 */
Boris Brezillonced271b2017-07-31 10:29:56 +02001095 if (chip->onfi_version &&
1096 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1097 ONFI_OPT_CMD_SET_GET_FEATURES)) {
Boris Brezillond8e725d2016-09-15 10:32:50 +02001098 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1099 chip->onfi_timing_mode_default,
1100 };
1101
1102 ret = chip->onfi_set_features(mtd, chip,
1103 ONFI_FEATURE_ADDR_TIMING_MODE,
1104 tmode_param);
1105 if (ret)
1106 goto err;
1107 }
1108
1109 ret = chip->setup_data_interface(mtd, chip->data_interface, false);
1110err:
1111 return ret;
1112}
1113
1114/**
1115 * nand_init_data_interface - find the best data interface and timings
1116 * @chip: The NAND chip
1117 *
1118 * Find the best data interface and NAND timings supported by the chip
1119 * and the driver.
1120 * First tries to retrieve supported timing modes from ONFI information,
1121 * and if the NAND chip does not support ONFI, relies on the
1122 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1123 * function nand_chip->data_interface is initialized with the best timing mode
1124 * available.
1125 *
1126 * Returns 0 for success or negative error code otherwise.
1127 */
1128static int nand_init_data_interface(struct nand_chip *chip)
1129{
1130 struct mtd_info *mtd = nand_to_mtd(chip);
1131 int modes, mode, ret;
1132
1133 if (!chip->setup_data_interface)
1134 return 0;
1135
1136 /*
1137 * First try to identify the best timings from ONFI parameters and
1138 * if the NAND does not support ONFI, fallback to the default ONFI
1139 * timing mode.
1140 */
1141 modes = onfi_get_async_timing_mode(chip);
1142 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1143 if (!chip->onfi_timing_mode_default)
1144 return 0;
1145
1146 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1147 }
1148
1149 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1150 GFP_KERNEL);
1151 if (!chip->data_interface)
1152 return -ENOMEM;
1153
1154 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1155 ret = onfi_init_data_interface(chip, chip->data_interface,
1156 NAND_SDR_IFACE, mode);
1157 if (ret)
1158 continue;
1159
1160 ret = chip->setup_data_interface(mtd, chip->data_interface,
1161 true);
1162 if (!ret) {
1163 chip->onfi_timing_mode_default = mode;
1164 break;
1165 }
1166 }
1167
1168 return 0;
1169}
1170
1171static void nand_release_data_interface(struct nand_chip *chip)
1172{
1173 kfree(chip->data_interface);
1174}
1175
1176/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001177 * nand_reset - Reset and initialize a NAND device
1178 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001179 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001180 *
1181 * Returns 0 for success or negative error code otherwise
1182 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001183int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001184{
1185 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001186 int ret;
1187
1188 ret = nand_reset_data_interface(chip);
1189 if (ret)
1190 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001191
Boris Brezillon73f907f2016-10-24 16:46:20 +02001192 /*
1193 * The CS line has to be released before we can apply the new NAND
1194 * interface settings, hence this weird ->select_chip() dance.
1195 */
1196 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001197 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001198 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001199
Boris Brezillon73f907f2016-10-24 16:46:20 +02001200 chip->select_chip(mtd, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001201 ret = nand_setup_data_interface(chip);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001202 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001203 if (ret)
1204 return ret;
1205
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001206 return 0;
1207}
1208
1209/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001210 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001211 * @mtd: mtd info
1212 * @ofs: offset to start unlock from
1213 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -07001214 * @invert: when = 0, unlock the range of blocks within the lower and
1215 * upper boundary address
1216 * when = 1, unlock the range of blocks outside the boundaries
1217 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301218 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001219 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301220 */
1221static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1222 uint64_t len, int invert)
1223{
1224 int ret = 0;
1225 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001226 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301227
1228 /* Submit address of first page to unlock */
1229 page = ofs >> chip->page_shift;
1230 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1231
1232 /* Submit address of last page to unlock */
1233 page = (ofs + len) >> chip->page_shift;
1234 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1235 (page | invert) & chip->pagemask);
1236
1237 /* Call wait ready function */
1238 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301239 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001240 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001241 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301242 __func__, status);
1243 ret = -EIO;
1244 }
1245
1246 return ret;
1247}
1248
1249/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001250 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001251 * @mtd: mtd info
1252 * @ofs: offset to start unlock from
1253 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301254 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001255 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301256 */
1257int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1258{
1259 int ret = 0;
1260 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001261 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301262
Brian Norris289c0522011-07-19 10:06:09 -07001263 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301264 __func__, (unsigned long long)ofs, len);
1265
1266 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001267 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301268
1269 /* Align to last block address if size addresses end of the device */
1270 if (ofs + len == mtd->size)
1271 len -= mtd->erasesize;
1272
Huang Shijie6a8214a2012-11-19 14:43:30 +08001273 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301274
1275 /* Shift to get chip number */
1276 chipnr = ofs >> chip->chip_shift;
1277
White Ding57d3a9a2014-07-24 00:10:45 +08001278 /*
1279 * Reset the chip.
1280 * If we want to check the WP through READ STATUS and check the bit 7
1281 * we must reset the chip
1282 * some operation can also clear the bit 7 of status register
1283 * eg. erase/program a locked block
1284 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001285 nand_reset(chip, chipnr);
1286
1287 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001288
Vimal Singh7d70f332010-02-08 15:50:49 +05301289 /* Check, if it is write protected */
1290 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001291 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301292 __func__);
1293 ret = -EIO;
1294 goto out;
1295 }
1296
1297 ret = __nand_unlock(mtd, ofs, len, 0);
1298
1299out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001300 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301301 nand_release_device(mtd);
1302
1303 return ret;
1304}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001305EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301306
1307/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001308 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001309 * @mtd: mtd info
1310 * @ofs: offset to start unlock from
1311 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301312 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001313 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1314 * have this feature, but it allows only to lock all blocks, not for specified
1315 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1316 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301317 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001318 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301319 */
1320int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1321{
1322 int ret = 0;
1323 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001324 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301325
Brian Norris289c0522011-07-19 10:06:09 -07001326 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301327 __func__, (unsigned long long)ofs, len);
1328
1329 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001330 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301331
Huang Shijie6a8214a2012-11-19 14:43:30 +08001332 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301333
1334 /* Shift to get chip number */
1335 chipnr = ofs >> chip->chip_shift;
1336
White Ding57d3a9a2014-07-24 00:10:45 +08001337 /*
1338 * Reset the chip.
1339 * If we want to check the WP through READ STATUS and check the bit 7
1340 * we must reset the chip
1341 * some operation can also clear the bit 7 of status register
1342 * eg. erase/program a locked block
1343 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001344 nand_reset(chip, chipnr);
1345
1346 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001347
Vimal Singh7d70f332010-02-08 15:50:49 +05301348 /* Check, if it is write protected */
1349 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001350 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301351 __func__);
1352 status = MTD_ERASE_FAILED;
1353 ret = -EIO;
1354 goto out;
1355 }
1356
1357 /* Submit address of first page to lock */
1358 page = ofs >> chip->page_shift;
1359 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1360
1361 /* Call wait ready function */
1362 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301363 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001364 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001365 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301366 __func__, status);
1367 ret = -EIO;
1368 goto out;
1369 }
1370
1371 ret = __nand_unlock(mtd, ofs, len, 0x1);
1372
1373out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001374 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301375 nand_release_device(mtd);
1376
1377 return ret;
1378}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001379EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301380
1381/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001382 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1383 * @buf: buffer to test
1384 * @len: buffer length
1385 * @bitflips_threshold: maximum number of bitflips
1386 *
1387 * Check if a buffer contains only 0xff, which means the underlying region
1388 * has been erased and is ready to be programmed.
1389 * The bitflips_threshold specify the maximum number of bitflips before
1390 * considering the region is not erased.
1391 * Note: The logic of this function has been extracted from the memweight
1392 * implementation, except that nand_check_erased_buf function exit before
1393 * testing the whole buffer if the number of bitflips exceed the
1394 * bitflips_threshold value.
1395 *
1396 * Returns a positive number of bitflips less than or equal to
1397 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1398 * threshold.
1399 */
1400static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1401{
1402 const unsigned char *bitmap = buf;
1403 int bitflips = 0;
1404 int weight;
1405
1406 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1407 len--, bitmap++) {
1408 weight = hweight8(*bitmap);
1409 bitflips += BITS_PER_BYTE - weight;
1410 if (unlikely(bitflips > bitflips_threshold))
1411 return -EBADMSG;
1412 }
1413
1414 for (; len >= sizeof(long);
1415 len -= sizeof(long), bitmap += sizeof(long)) {
1416 weight = hweight_long(*((unsigned long *)bitmap));
1417 bitflips += BITS_PER_LONG - weight;
1418 if (unlikely(bitflips > bitflips_threshold))
1419 return -EBADMSG;
1420 }
1421
1422 for (; len > 0; len--, bitmap++) {
1423 weight = hweight8(*bitmap);
1424 bitflips += BITS_PER_BYTE - weight;
1425 if (unlikely(bitflips > bitflips_threshold))
1426 return -EBADMSG;
1427 }
1428
1429 return bitflips;
1430}
1431
1432/**
1433 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1434 * 0xff data
1435 * @data: data buffer to test
1436 * @datalen: data length
1437 * @ecc: ECC buffer
1438 * @ecclen: ECC length
1439 * @extraoob: extra OOB buffer
1440 * @extraooblen: extra OOB length
1441 * @bitflips_threshold: maximum number of bitflips
1442 *
1443 * Check if a data buffer and its associated ECC and OOB data contains only
1444 * 0xff pattern, which means the underlying region has been erased and is
1445 * ready to be programmed.
1446 * The bitflips_threshold specify the maximum number of bitflips before
1447 * considering the region as not erased.
1448 *
1449 * Note:
1450 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1451 * different from the NAND page size. When fixing bitflips, ECC engines will
1452 * report the number of errors per chunk, and the NAND core infrastructure
1453 * expect you to return the maximum number of bitflips for the whole page.
1454 * This is why you should always use this function on a single chunk and
1455 * not on the whole page. After checking each chunk you should update your
1456 * max_bitflips value accordingly.
1457 * 2/ When checking for bitflips in erased pages you should not only check
1458 * the payload data but also their associated ECC data, because a user might
1459 * have programmed almost all bits to 1 but a few. In this case, we
1460 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1461 * this case.
1462 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1463 * data are protected by the ECC engine.
1464 * It could also be used if you support subpages and want to attach some
1465 * extra OOB data to an ECC chunk.
1466 *
1467 * Returns a positive number of bitflips less than or equal to
1468 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1469 * threshold. In case of success, the passed buffers are filled with 0xff.
1470 */
1471int nand_check_erased_ecc_chunk(void *data, int datalen,
1472 void *ecc, int ecclen,
1473 void *extraoob, int extraooblen,
1474 int bitflips_threshold)
1475{
1476 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1477
1478 data_bitflips = nand_check_erased_buf(data, datalen,
1479 bitflips_threshold);
1480 if (data_bitflips < 0)
1481 return data_bitflips;
1482
1483 bitflips_threshold -= data_bitflips;
1484
1485 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1486 if (ecc_bitflips < 0)
1487 return ecc_bitflips;
1488
1489 bitflips_threshold -= ecc_bitflips;
1490
1491 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1492 bitflips_threshold);
1493 if (extraoob_bitflips < 0)
1494 return extraoob_bitflips;
1495
1496 if (data_bitflips)
1497 memset(data, 0xff, datalen);
1498
1499 if (ecc_bitflips)
1500 memset(ecc, 0xff, ecclen);
1501
1502 if (extraoob_bitflips)
1503 memset(extraoob, 0xff, extraooblen);
1504
1505 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1506}
1507EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1508
1509/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001510 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001511 * @mtd: mtd info structure
1512 * @chip: nand chip info structure
1513 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001514 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001515 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001516 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001517 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001518 */
1519static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001520 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001521{
1522 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001523 if (oob_required)
1524 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001525 return 0;
1526}
1527
1528/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001529 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001530 * @mtd: mtd info structure
1531 * @chip: nand chip info structure
1532 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001533 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001534 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001535 *
1536 * We need a special oob layout and handling even when OOB isn't used.
1537 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001538static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001539 struct nand_chip *chip, uint8_t *buf,
1540 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001541{
1542 int eccsize = chip->ecc.size;
1543 int eccbytes = chip->ecc.bytes;
1544 uint8_t *oob = chip->oob_poi;
1545 int steps, size;
1546
1547 for (steps = chip->ecc.steps; steps > 0; steps--) {
1548 chip->read_buf(mtd, buf, eccsize);
1549 buf += eccsize;
1550
1551 if (chip->ecc.prepad) {
1552 chip->read_buf(mtd, oob, chip->ecc.prepad);
1553 oob += chip->ecc.prepad;
1554 }
1555
1556 chip->read_buf(mtd, oob, eccbytes);
1557 oob += eccbytes;
1558
1559 if (chip->ecc.postpad) {
1560 chip->read_buf(mtd, oob, chip->ecc.postpad);
1561 oob += chip->ecc.postpad;
1562 }
1563 }
1564
1565 size = mtd->oobsize - (oob - chip->oob_poi);
1566 if (size)
1567 chip->read_buf(mtd, oob, size);
1568
1569 return 0;
1570}
1571
1572/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001573 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001574 * @mtd: mtd info structure
1575 * @chip: nand chip info structure
1576 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001577 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001578 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001579 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001580static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001581 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Boris Brezillon846031d2016-02-03 20:11:00 +01001583 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001584 int eccbytes = chip->ecc.bytes;
1585 int eccsteps = chip->ecc.steps;
1586 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001587 uint8_t *ecc_calc = chip->buffers->ecccalc;
1588 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001589 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001590
Brian Norris1fbb9382012-05-02 10:14:55 -07001591 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001592
1593 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1594 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1595
Boris Brezillon846031d2016-02-03 20:11:00 +01001596 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1597 chip->ecc.total);
1598 if (ret)
1599 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001600
1601 eccsteps = chip->ecc.steps;
1602 p = buf;
1603
1604 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1605 int stat;
1606
1607 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001608 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001609 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001610 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001611 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001612 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1613 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001614 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001615 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301619 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001620 * @mtd: mtd info structure
1621 * @chip: nand chip info structure
1622 * @data_offs: offset of requested data within the page
1623 * @readlen: data length
1624 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001625 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001626 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001627static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001628 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1629 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001630{
Boris Brezillon846031d2016-02-03 20:11:00 +01001631 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001632 uint8_t *p;
1633 int data_col_addr, i, gaps = 0;
1634 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1635 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001636 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001637 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001638 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001639
Brian Norris7854d3f2011-06-23 14:12:08 -07001640 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001641 start_step = data_offs / chip->ecc.size;
1642 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1643 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301644 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001645
Brian Norris8b6e50c2011-05-25 14:59:01 -07001646 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001647 datafrag_len = num_steps * chip->ecc.size;
1648 eccfrag_len = num_steps * chip->ecc.bytes;
1649
1650 data_col_addr = start_step * chip->ecc.size;
1651 /* If we read not a page aligned data */
1652 if (data_col_addr != 0)
1653 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1654
1655 p = bufpoi + data_col_addr;
1656 chip->read_buf(mtd, p, datafrag_len);
1657
Brian Norris8b6e50c2011-05-25 14:59:01 -07001658 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001659 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1660 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1661
Brian Norris8b6e50c2011-05-25 14:59:01 -07001662 /*
1663 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001664 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001665 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001666 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1667 if (ret)
1668 return ret;
1669
1670 if (oobregion.length < eccfrag_len)
1671 gaps = 1;
1672
Alexey Korolev3d459552008-05-15 17:23:18 +01001673 if (gaps) {
1674 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1675 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1676 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001677 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001678 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001679 * about buswidth alignment in read_buf.
1680 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001681 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001682 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001683 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001684 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001685 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1686 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001687 aligned_len++;
1688
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001689 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001690 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001691 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1692 }
1693
Boris Brezillon846031d2016-02-03 20:11:00 +01001694 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1695 chip->oob_poi, index, eccfrag_len);
1696 if (ret)
1697 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001698
1699 p = bufpoi + data_col_addr;
1700 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1701 int stat;
1702
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001703 stat = chip->ecc.correct(mtd, p,
1704 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001705 if (stat == -EBADMSG &&
1706 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1707 /* check for empty pages with bitflips */
1708 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1709 &chip->buffers->ecccode[i],
1710 chip->ecc.bytes,
1711 NULL, 0,
1712 chip->ecc.strength);
1713 }
1714
Mike Dunn3f91e942012-04-25 12:06:09 -07001715 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001716 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001717 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001718 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001719 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1720 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001721 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001722 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001723}
1724
1725/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001726 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001727 * @mtd: mtd info structure
1728 * @chip: nand chip info structure
1729 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001730 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001731 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001732 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001733 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001734 */
1735static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001736 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001737{
Boris Brezillon846031d2016-02-03 20:11:00 +01001738 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001739 int eccbytes = chip->ecc.bytes;
1740 int eccsteps = chip->ecc.steps;
1741 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001742 uint8_t *ecc_calc = chip->buffers->ecccalc;
1743 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001744 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001745
1746 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1747 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1748 chip->read_buf(mtd, p, eccsize);
1749 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1750 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001751 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001752
Boris Brezillon846031d2016-02-03 20:11:00 +01001753 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1754 chip->ecc.total);
1755 if (ret)
1756 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001757
1758 eccsteps = chip->ecc.steps;
1759 p = buf;
1760
1761 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1762 int stat;
1763
1764 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001765 if (stat == -EBADMSG &&
1766 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1767 /* check for empty pages with bitflips */
1768 stat = nand_check_erased_ecc_chunk(p, eccsize,
1769 &ecc_code[i], eccbytes,
1770 NULL, 0,
1771 chip->ecc.strength);
1772 }
1773
Mike Dunn3f91e942012-04-25 12:06:09 -07001774 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001775 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001776 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001777 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001778 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1779 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001780 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001781 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001782}
1783
1784/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001785 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001786 * @mtd: mtd info structure
1787 * @chip: nand chip info structure
1788 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001789 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001790 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001791 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001792 * Hardware ECC for large page chips, require OOB to be read first. For this
1793 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1794 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1795 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1796 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001797 */
1798static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001799 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001800{
Boris Brezillon846031d2016-02-03 20:11:00 +01001801 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001802 int eccbytes = chip->ecc.bytes;
1803 int eccsteps = chip->ecc.steps;
1804 uint8_t *p = buf;
1805 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001806 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001807 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001808
1809 /* Read the OOB area first */
1810 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1811 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1812 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1813
Boris Brezillon846031d2016-02-03 20:11:00 +01001814 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1815 chip->ecc.total);
1816 if (ret)
1817 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001818
1819 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1820 int stat;
1821
1822 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1823 chip->read_buf(mtd, p, eccsize);
1824 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1825
1826 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001827 if (stat == -EBADMSG &&
1828 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1829 /* check for empty pages with bitflips */
1830 stat = nand_check_erased_ecc_chunk(p, eccsize,
1831 &ecc_code[i], eccbytes,
1832 NULL, 0,
1833 chip->ecc.strength);
1834 }
1835
Mike Dunn3f91e942012-04-25 12:06:09 -07001836 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001837 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001838 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001839 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001840 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1841 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001842 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001843 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001844}
1845
1846/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001847 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001848 * @mtd: mtd info structure
1849 * @chip: nand chip info structure
1850 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001851 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001852 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001853 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001854 * The hw generator calculates the error syndrome automatically. Therefore we
1855 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001856 */
1857static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001858 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001859{
1860 int i, eccsize = chip->ecc.size;
1861 int eccbytes = chip->ecc.bytes;
1862 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001863 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001864 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001865 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001866 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001867
1868 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1869 int stat;
1870
1871 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1872 chip->read_buf(mtd, p, eccsize);
1873
1874 if (chip->ecc.prepad) {
1875 chip->read_buf(mtd, oob, chip->ecc.prepad);
1876 oob += chip->ecc.prepad;
1877 }
1878
1879 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1880 chip->read_buf(mtd, oob, eccbytes);
1881 stat = chip->ecc.correct(mtd, p, oob, NULL);
1882
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001883 oob += eccbytes;
1884
1885 if (chip->ecc.postpad) {
1886 chip->read_buf(mtd, oob, chip->ecc.postpad);
1887 oob += chip->ecc.postpad;
1888 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001889
1890 if (stat == -EBADMSG &&
1891 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1892 /* check for empty pages with bitflips */
1893 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1894 oob - eccpadbytes,
1895 eccpadbytes,
1896 NULL, 0,
1897 chip->ecc.strength);
1898 }
1899
1900 if (stat < 0) {
1901 mtd->ecc_stats.failed++;
1902 } else {
1903 mtd->ecc_stats.corrected += stat;
1904 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1905 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001906 }
1907
1908 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001909 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001910 if (i)
1911 chip->read_buf(mtd, oob, i);
1912
Mike Dunn3f91e942012-04-25 12:06:09 -07001913 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001914}
1915
1916/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001917 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001918 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001919 * @oob: oob destination address
1920 * @ops: oob ops structure
1921 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001922 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001923static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001924 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001925{
Boris Brezillon846031d2016-02-03 20:11:00 +01001926 struct nand_chip *chip = mtd_to_nand(mtd);
1927 int ret;
1928
Florian Fainellif8ac0412010-09-07 13:23:43 +02001929 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930
Brian Norris0612b9d2011-08-30 18:45:40 -07001931 case MTD_OPS_PLACE_OOB:
1932 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001933 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1934 return oob + len;
1935
Boris Brezillon846031d2016-02-03 20:11:00 +01001936 case MTD_OPS_AUTO_OOB:
1937 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1938 ops->ooboffs, len);
1939 BUG_ON(ret);
1940 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001942 default:
1943 BUG();
1944 }
1945 return NULL;
1946}
1947
1948/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001949 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1950 * @mtd: MTD device structure
1951 * @retry_mode: the retry mode to use
1952 *
1953 * Some vendors supply a special command to shift the Vt threshold, to be used
1954 * when there are too many bitflips in a page (i.e., ECC error). After setting
1955 * a new threshold, the host should retry reading the page.
1956 */
1957static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1958{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001959 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001960
1961 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1962
1963 if (retry_mode >= chip->read_retries)
1964 return -EINVAL;
1965
1966 if (!chip->setup_read_retry)
1967 return -EOPNOTSUPP;
1968
1969 return chip->setup_read_retry(mtd, retry_mode);
1970}
1971
1972/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001973 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001974 * @mtd: MTD device structure
1975 * @from: offset to read from
1976 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001977 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001978 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001979 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001980static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1981 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001982{
Brian Norrise47f3db2012-05-02 10:14:56 -07001983 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001984 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001985 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001986 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001987 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001988 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001989
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001990 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001991 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001992 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001993 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001994 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001996 chipnr = (int)(from >> chip->chip_shift);
1997 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001999 realpage = (int)(from >> chip->page_shift);
2000 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002002 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002004 buf = ops->datbuf;
2005 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002006 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002007
Florian Fainellif8ac0412010-09-07 13:23:43 +02002008 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002009 unsigned int ecc_failures = mtd->ecc_stats.failed;
2010
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002011 bytes = min(mtd->writesize - col, readlen);
2012 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002013
Kamal Dasu66507c72014-05-01 20:51:19 -04002014 if (!aligned)
2015 use_bufpoi = 1;
2016 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2017 use_bufpoi = !virt_addr_valid(buf);
2018 else
2019 use_bufpoi = 0;
2020
Brian Norris8b6e50c2011-05-25 14:59:01 -07002021 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002022 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002023 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2024
2025 if (use_bufpoi && aligned)
2026 pr_debug("%s: using read bounce buffer for buf@%p\n",
2027 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Brian Norrisba84fb52014-01-03 15:13:33 -08002029read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07002030 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Mike Dunnedbc45402012-04-25 12:06:11 -07002032 /*
2033 * Now read the page into the buffer. Absent an error,
2034 * the read methods return max bitflips per ecc step.
2035 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002036 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002037 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002038 oob_required,
2039 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002040 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2041 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002042 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002043 col, bytes, bufpoi,
2044 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002045 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002046 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002047 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002048 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002049 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002050 /* Invalidate page cache */
2051 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002052 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002053 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002054
Mike Dunnedbc45402012-04-25 12:06:11 -07002055 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2056
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002057 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002058 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002059 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002060 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002061 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002062 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002063 chip->pagebuf_bitflips = ret;
2064 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002065 /* Invalidate page cache */
2066 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002067 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002068 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002070
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002071 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002072 int toread = min(oobreadlen, max_oobsize);
2073
2074 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002075 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002076 oob, ops, toread);
2077 oobreadlen -= toread;
2078 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002079 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002080
2081 if (chip->options & NAND_NEED_READRDY) {
2082 /* Apply delay or wait for ready/busy pin */
2083 if (!chip->dev_ready)
2084 udelay(chip->chip_delay);
2085 else
2086 nand_wait_ready(mtd);
2087 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002088
Brian Norrisba84fb52014-01-03 15:13:33 -08002089 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002090 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002091 retry_mode++;
2092 ret = nand_setup_read_retry(mtd,
2093 retry_mode);
2094 if (ret < 0)
2095 break;
2096
2097 /* Reset failures; retry */
2098 mtd->ecc_stats.failed = ecc_failures;
2099 goto read_retry;
2100 } else {
2101 /* No more retry modes; real failure */
2102 ecc_fail = true;
2103 }
2104 }
2105
2106 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002107 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002108 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002109 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002110 max_bitflips = max_t(unsigned int, max_bitflips,
2111 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002114 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002115
Brian Norrisba84fb52014-01-03 15:13:33 -08002116 /* Reset to retry mode 0 */
2117 if (retry_mode) {
2118 ret = nand_setup_read_retry(mtd, 0);
2119 if (ret < 0)
2120 break;
2121 retry_mode = 0;
2122 }
2123
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002124 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002125 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Brian Norris8b6e50c2011-05-25 14:59:01 -07002127 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 col = 0;
2129 /* Increment page address */
2130 realpage++;
2131
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002132 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* Check, if we cross a chip boundary */
2134 if (!page) {
2135 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002136 chip->select_chip(mtd, -1);
2137 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002140 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002142 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002143 if (oob)
2144 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Mike Dunn3f91e942012-04-25 12:06:09 -07002146 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002147 return ret;
2148
Brian Norrisb72f3df2013-12-03 11:04:14 -08002149 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002150 return -EBADMSG;
2151
Mike Dunnedbc45402012-04-25 12:06:11 -07002152 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002153}
2154
2155/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002156 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002157 * @mtd: MTD device structure
2158 * @from: offset to read from
2159 * @len: number of bytes to read
2160 * @retlen: pointer to variable to store the number of read bytes
2161 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002162 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002163 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002164 */
2165static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2166 size_t *retlen, uint8_t *buf)
2167{
Brian Norris4a89ff82011-08-30 18:45:45 -07002168 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002169 int ret;
2170
Huang Shijie6a8214a2012-11-19 14:43:30 +08002171 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002172 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002173 ops.len = len;
2174 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002175 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002176 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002177 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002178 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002179 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002183 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002184 * @mtd: mtd info structure
2185 * @chip: nand chip info structure
2186 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002187 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002188int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002189{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002190 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002191 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002192 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002193}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002194EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002195
2196/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002197 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002198 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002199 * @mtd: mtd info structure
2200 * @chip: nand chip info structure
2201 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002202 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002203int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2204 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002205{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002206 int length = mtd->oobsize;
2207 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2208 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002209 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002210 int i, toread, sndrnd = 0, pos;
2211
2212 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2213 for (i = 0; i < chip->ecc.steps; i++) {
2214 if (sndrnd) {
2215 pos = eccsize + i * (eccsize + chunk);
2216 if (mtd->writesize > 512)
2217 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2218 else
2219 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2220 } else
2221 sndrnd = 1;
2222 toread = min_t(int, length, chunk);
2223 chip->read_buf(mtd, bufpoi, toread);
2224 bufpoi += toread;
2225 length -= toread;
2226 }
2227 if (length > 0)
2228 chip->read_buf(mtd, bufpoi, length);
2229
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002230 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002231}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002232EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002233
2234/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002235 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002236 * @mtd: mtd info structure
2237 * @chip: nand chip info structure
2238 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002239 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002240int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002241{
2242 int status = 0;
2243 const uint8_t *buf = chip->oob_poi;
2244 int length = mtd->oobsize;
2245
2246 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2247 chip->write_buf(mtd, buf, length);
2248 /* Send command to program the OOB data */
2249 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2250
2251 status = chip->waitfunc(mtd, chip);
2252
Savin Zlobec0d420f92006-06-21 11:51:20 +02002253 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002254}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002255EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002256
2257/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002258 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002259 * with syndrome - only for large page flash
2260 * @mtd: mtd info structure
2261 * @chip: nand chip info structure
2262 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002263 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002264int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2265 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002266{
2267 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2268 int eccsize = chip->ecc.size, length = mtd->oobsize;
2269 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2270 const uint8_t *bufpoi = chip->oob_poi;
2271
2272 /*
2273 * data-ecc-data-ecc ... ecc-oob
2274 * or
2275 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2276 */
2277 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2278 pos = steps * (eccsize + chunk);
2279 steps = 0;
2280 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002281 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002282
2283 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2284 for (i = 0; i < steps; i++) {
2285 if (sndcmd) {
2286 if (mtd->writesize <= 512) {
2287 uint32_t fill = 0xFFFFFFFF;
2288
2289 len = eccsize;
2290 while (len > 0) {
2291 int num = min_t(int, len, 4);
2292 chip->write_buf(mtd, (uint8_t *)&fill,
2293 num);
2294 len -= num;
2295 }
2296 } else {
2297 pos = eccsize + i * (eccsize + chunk);
2298 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2299 }
2300 } else
2301 sndcmd = 1;
2302 len = min_t(int, length, chunk);
2303 chip->write_buf(mtd, bufpoi, len);
2304 bufpoi += len;
2305 length -= len;
2306 }
2307 if (length > 0)
2308 chip->write_buf(mtd, bufpoi, length);
2309
2310 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2311 status = chip->waitfunc(mtd, chip);
2312
2313 return status & NAND_STATUS_FAIL ? -EIO : 0;
2314}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002315EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002316
2317/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002318 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002319 * @mtd: MTD device structure
2320 * @from: offset to read from
2321 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002323 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002325static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2326 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Miquel Raynald80cd3e2018-01-12 10:13:36 +01002328 unsigned int max_bitflips = 0;
Brian Norrisc00a0992012-05-01 17:12:54 -07002329 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002330 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002331 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002332 int readlen = ops->ooblen;
2333 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002334 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002335 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Brian Norris289c0522011-07-19 10:06:09 -07002337 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302338 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Brian Norris041e4572011-06-23 16:45:24 -07002340 stats = mtd->ecc_stats;
2341
Boris BREZILLON29f10582016-03-07 10:46:52 +01002342 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002343
2344 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002345 pr_debug("%s: attempt to start read outside oob\n",
2346 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002347 return -EINVAL;
2348 }
2349
2350 /* Do not allow reads past end of device */
2351 if (unlikely(from >= mtd->size ||
2352 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2353 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002354 pr_debug("%s: attempt to read beyond end of device\n",
2355 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002356 return -EINVAL;
2357 }
Vitaly Wool70145682006-11-03 18:20:38 +03002358
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002359 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002360 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002362 /* Shift to get page */
2363 realpage = (int)(from >> chip->page_shift);
2364 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Florian Fainellif8ac0412010-09-07 13:23:43 +02002366 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002367 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002368 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002369 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002370 ret = chip->ecc.read_oob(mtd, chip, page);
2371
2372 if (ret < 0)
2373 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002374
2375 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002376 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002377
Brian Norris5bc7c332013-03-13 09:51:31 -07002378 if (chip->options & NAND_NEED_READRDY) {
2379 /* Apply delay or wait for ready/busy pin */
2380 if (!chip->dev_ready)
2381 udelay(chip->chip_delay);
2382 else
2383 nand_wait_ready(mtd);
2384 }
2385
Miquel Raynald80cd3e2018-01-12 10:13:36 +01002386 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2387
Vitaly Wool70145682006-11-03 18:20:38 +03002388 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002389 if (!readlen)
2390 break;
2391
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002392 /* Increment page address */
2393 realpage++;
2394
2395 page = realpage & chip->pagemask;
2396 /* Check, if we cross a chip boundary */
2397 if (!page) {
2398 chipnr++;
2399 chip->select_chip(mtd, -1);
2400 chip->select_chip(mtd, chipnr);
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002403 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002405 ops->oobretlen = ops->ooblen - readlen;
2406
2407 if (ret < 0)
2408 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002409
2410 if (mtd->ecc_stats.failed - stats.failed)
2411 return -EBADMSG;
2412
Miquel Raynald80cd3e2018-01-12 10:13:36 +01002413 return max_bitflips;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415
2416/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002417 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002418 * @mtd: MTD device structure
2419 * @from: offset to read from
2420 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002422 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002424static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2425 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002427 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002428
2429 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002432 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002433 pr_debug("%s: attempt to read beyond end of device\n",
2434 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -EINVAL;
2436 }
2437
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002438 if (ops->mode != MTD_OPS_PLACE_OOB &&
2439 ops->mode != MTD_OPS_AUTO_OOB &&
2440 ops->mode != MTD_OPS_RAW)
2441 return -ENOTSUPP;
2442
Huang Shijie6a8214a2012-11-19 14:43:30 +08002443 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002445 if (!ops->datbuf)
2446 ret = nand_do_read_oob(mtd, from, ops);
2447 else
2448 ret = nand_do_read_ops(mtd, from, ops);
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002451 return ret;
2452}
2453
2454
2455/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002456 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002457 * @mtd: mtd info structure
2458 * @chip: nand chip info structure
2459 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002460 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002461 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002462 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002463 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002464 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002465static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002466 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002467{
2468 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002469 if (oob_required)
2470 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002471
2472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002475/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002476 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002477 * @mtd: mtd info structure
2478 * @chip: nand chip info structure
2479 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002480 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002481 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002482 *
2483 * We need a special oob layout and handling even when ECC isn't checked.
2484 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002485static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002486 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002487 const uint8_t *buf, int oob_required,
2488 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002489{
2490 int eccsize = chip->ecc.size;
2491 int eccbytes = chip->ecc.bytes;
2492 uint8_t *oob = chip->oob_poi;
2493 int steps, size;
2494
2495 for (steps = chip->ecc.steps; steps > 0; steps--) {
2496 chip->write_buf(mtd, buf, eccsize);
2497 buf += eccsize;
2498
2499 if (chip->ecc.prepad) {
2500 chip->write_buf(mtd, oob, chip->ecc.prepad);
2501 oob += chip->ecc.prepad;
2502 }
2503
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002504 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002505 oob += eccbytes;
2506
2507 if (chip->ecc.postpad) {
2508 chip->write_buf(mtd, oob, chip->ecc.postpad);
2509 oob += chip->ecc.postpad;
2510 }
2511 }
2512
2513 size = mtd->oobsize - (oob - chip->oob_poi);
2514 if (size)
2515 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002516
2517 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002518}
2519/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002520 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002521 * @mtd: mtd info structure
2522 * @chip: nand chip info structure
2523 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002524 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002525 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002526 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002527static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002528 const uint8_t *buf, int oob_required,
2529 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002530{
Boris Brezillon846031d2016-02-03 20:11:00 +01002531 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002532 int eccbytes = chip->ecc.bytes;
2533 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002534 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002535 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002536
Brian Norris7854d3f2011-06-23 14:12:08 -07002537 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002538 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2539 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002540
Boris Brezillon846031d2016-02-03 20:11:00 +01002541 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2542 chip->ecc.total);
2543 if (ret)
2544 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002545
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002546 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002547}
2548
2549/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002550 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002551 * @mtd: mtd info structure
2552 * @chip: nand chip info structure
2553 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002554 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002555 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002556 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002557static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002558 const uint8_t *buf, int oob_required,
2559 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002560{
Boris Brezillon846031d2016-02-03 20:11:00 +01002561 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002562 int eccbytes = chip->ecc.bytes;
2563 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002564 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002565 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002566
2567 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2568 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002569 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002570 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2571 }
2572
Boris Brezillon846031d2016-02-03 20:11:00 +01002573 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2574 chip->ecc.total);
2575 if (ret)
2576 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002577
2578 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002579
2580 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002581}
2582
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302583
2584/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002585 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302586 * @mtd: mtd info structure
2587 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002588 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302589 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002590 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302591 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002592 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302593 */
2594static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2595 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002596 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002597 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302598{
2599 uint8_t *oob_buf = chip->oob_poi;
2600 uint8_t *ecc_calc = chip->buffers->ecccalc;
2601 int ecc_size = chip->ecc.size;
2602 int ecc_bytes = chip->ecc.bytes;
2603 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302604 uint32_t start_step = offset / ecc_size;
2605 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2606 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002607 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302608
2609 for (step = 0; step < ecc_steps; step++) {
2610 /* configure controller for WRITE access */
2611 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2612
2613 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002614 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302615
2616 /* mask ECC of un-touched subpages by padding 0xFF */
2617 if ((step < start_step) || (step > end_step))
2618 memset(ecc_calc, 0xff, ecc_bytes);
2619 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002620 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302621
2622 /* mask OOB of un-touched subpages by padding 0xFF */
2623 /* if oob_required, preserve OOB metadata of written subpage */
2624 if (!oob_required || (step < start_step) || (step > end_step))
2625 memset(oob_buf, 0xff, oob_bytes);
2626
Brian Norrisd6a950802013-08-08 17:16:36 -07002627 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302628 ecc_calc += ecc_bytes;
2629 oob_buf += oob_bytes;
2630 }
2631
2632 /* copy calculated ECC for whole page to chip->buffer->oob */
2633 /* this include masked-value(0xFF) for unwritten subpages */
2634 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002635 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2636 chip->ecc.total);
2637 if (ret)
2638 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302639
2640 /* write OOB buffer to NAND device */
2641 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2642
2643 return 0;
2644}
2645
2646
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002647/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002648 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002649 * @mtd: mtd info structure
2650 * @chip: nand chip info structure
2651 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002652 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002653 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002654 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002655 * The hw generator calculates the error syndrome automatically. Therefore we
2656 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002657 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002658static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002659 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002660 const uint8_t *buf, int oob_required,
2661 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002662{
2663 int i, eccsize = chip->ecc.size;
2664 int eccbytes = chip->ecc.bytes;
2665 int eccsteps = chip->ecc.steps;
2666 const uint8_t *p = buf;
2667 uint8_t *oob = chip->oob_poi;
2668
2669 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2670
2671 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2672 chip->write_buf(mtd, p, eccsize);
2673
2674 if (chip->ecc.prepad) {
2675 chip->write_buf(mtd, oob, chip->ecc.prepad);
2676 oob += chip->ecc.prepad;
2677 }
2678
2679 chip->ecc.calculate(mtd, p, oob);
2680 chip->write_buf(mtd, oob, eccbytes);
2681 oob += eccbytes;
2682
2683 if (chip->ecc.postpad) {
2684 chip->write_buf(mtd, oob, chip->ecc.postpad);
2685 oob += chip->ecc.postpad;
2686 }
2687 }
2688
2689 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002690 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002691 if (i)
2692 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002693
2694 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002695}
2696
2697/**
David Woodhouse956e9442006-09-25 17:12:39 +01002698 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002699 * @mtd: MTD device structure
2700 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302701 * @offset: address offset within the page
2702 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002703 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002704 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002705 * @page: page number to write
2706 * @cached: cached programming
2707 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002708 */
2709static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302710 uint32_t offset, int data_len, const uint8_t *buf,
2711 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002712{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302713 int status, subpage;
2714
2715 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2716 chip->ecc.write_subpage)
2717 subpage = offset || (data_len < mtd->writesize);
2718 else
2719 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002720
2721 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2722
David Woodhouse956e9442006-09-25 17:12:39 +01002723 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302724 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002725 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302726 else if (subpage)
2727 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002728 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002729 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002730 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2731 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002732
2733 if (status < 0)
2734 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002735
2736 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002737 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002738 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002739 */
2740 cached = 0;
2741
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002742 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002743
2744 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002745 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002746 /*
2747 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002748 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002749 */
2750 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2751 status = chip->errstat(mtd, chip, FL_WRITING, status,
2752 page);
2753
2754 if (status & NAND_STATUS_FAIL)
2755 return -EIO;
2756 } else {
2757 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002758 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002759 }
2760
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002761 return 0;
2762}
2763
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002764/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002765 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002766 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002767 * @oob: oob data buffer
2768 * @len: oob data write length
2769 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002770 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002771static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2772 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002773{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002774 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002775 int ret;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002776
2777 /*
2778 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2779 * data from a previous OOB read.
2780 */
2781 memset(chip->oob_poi, 0xff, mtd->oobsize);
2782
Florian Fainellif8ac0412010-09-07 13:23:43 +02002783 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002784
Brian Norris0612b9d2011-08-30 18:45:40 -07002785 case MTD_OPS_PLACE_OOB:
2786 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002787 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2788 return oob + len;
2789
Boris Brezillon846031d2016-02-03 20:11:00 +01002790 case MTD_OPS_AUTO_OOB:
2791 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2792 ops->ooboffs, len);
2793 BUG_ON(ret);
2794 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002795
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002796 default:
2797 BUG();
2798 }
2799 return NULL;
2800}
2801
Florian Fainellif8ac0412010-09-07 13:23:43 +02002802#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002803
2804/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002805 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002806 * @mtd: MTD device structure
2807 * @to: offset to write to
2808 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002809 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002810 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002811 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002812static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2813 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002814{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002815 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002816 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002817 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002818
2819 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002820 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002821
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002822 uint8_t *oob = ops->oobbuf;
2823 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302824 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002825 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002826
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002827 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002828 if (!writelen)
2829 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002830
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002832 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002833 pr_notice("%s: attempt to write non page aligned data\n",
2834 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002835 return -EINVAL;
2836 }
2837
Thomas Gleixner29072b92006-09-28 15:38:36 +02002838 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002839
Thomas Gleixner6a930962006-06-28 00:11:45 +02002840 chipnr = (int)(to >> chip->chip_shift);
2841 chip->select_chip(mtd, chipnr);
2842
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002843 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002844 if (nand_check_wp(mtd)) {
2845 ret = -EIO;
2846 goto err_out;
2847 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002848
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002849 realpage = (int)(to >> chip->page_shift);
2850 page = realpage & chip->pagemask;
2851 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2852
2853 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002854 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2855 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002856 chip->pagebuf = -1;
2857
Maxim Levitsky782ce792010-02-22 20:39:36 +02002858 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002859 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2860 ret = -EINVAL;
2861 goto err_out;
2862 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002863
Florian Fainellif8ac0412010-09-07 13:23:43 +02002864 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002865 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002866 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002867 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002868 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002869 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002870
Kamal Dasu66507c72014-05-01 20:51:19 -04002871 if (part_pagewr)
2872 use_bufpoi = 1;
2873 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2874 use_bufpoi = !virt_addr_valid(buf);
2875 else
2876 use_bufpoi = 0;
2877
2878 /* Partial page write?, or need to use bounce buffer */
2879 if (use_bufpoi) {
2880 pr_debug("%s: using write bounce buffer for buf@%p\n",
2881 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002882 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002883 if (part_pagewr)
2884 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002885 chip->pagebuf = -1;
2886 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2887 memcpy(&chip->buffers->databuf[column], buf, bytes);
2888 wbuf = chip->buffers->databuf;
2889 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002890
Maxim Levitsky782ce792010-02-22 20:39:36 +02002891 if (unlikely(oob)) {
2892 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002893 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002894 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002895 } else {
2896 /* We still need to erase leftover OOB data */
2897 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002898 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302899 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2900 oob_required, page, cached,
2901 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002902 if (ret)
2903 break;
2904
2905 writelen -= bytes;
2906 if (!writelen)
2907 break;
2908
Thomas Gleixner29072b92006-09-28 15:38:36 +02002909 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002910 buf += bytes;
2911 realpage++;
2912
2913 page = realpage & chip->pagemask;
2914 /* Check, if we cross a chip boundary */
2915 if (!page) {
2916 chipnr++;
2917 chip->select_chip(mtd, -1);
2918 chip->select_chip(mtd, chipnr);
2919 }
2920 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002921
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002922 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002923 if (unlikely(oob))
2924 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002925
2926err_out:
2927 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002928 return ret;
2929}
2930
2931/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002932 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002933 * @mtd: MTD device structure
2934 * @to: offset to write to
2935 * @len: number of bytes to write
2936 * @retlen: pointer to variable to store the number of written bytes
2937 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002938 *
2939 * NAND write with ECC. Used when performing writes in interrupt context, this
2940 * may for example be called by mtdoops when writing an oops while in panic.
2941 */
2942static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2943 size_t *retlen, const uint8_t *buf)
2944{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002945 struct nand_chip *chip = mtd_to_nand(mtd);
Brent Taylor17074fa2017-10-30 22:32:45 -05002946 int chipnr = (int)(to >> chip->chip_shift);
Brian Norris4a89ff82011-08-30 18:45:45 -07002947 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002948 int ret;
2949
Brian Norris8b6e50c2011-05-25 14:59:01 -07002950 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002951 panic_nand_get_device(chip, mtd, FL_WRITING);
2952
Brent Taylor17074fa2017-10-30 22:32:45 -05002953 chip->select_chip(mtd, chipnr);
2954
2955 /* Wait for the device to get ready */
2956 panic_nand_wait(mtd, chip, 400);
2957
Brian Norris0ec56dc2015-02-28 02:02:30 -08002958 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002959 ops.len = len;
2960 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002961 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002962
Brian Norris4a89ff82011-08-30 18:45:45 -07002963 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002964
Brian Norris4a89ff82011-08-30 18:45:45 -07002965 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002966 return ret;
2967}
2968
2969/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002970 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971 * @mtd: MTD device structure
2972 * @to: offset to write to
2973 * @len: number of bytes to write
2974 * @retlen: pointer to variable to store the number of written bytes
2975 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002977 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002979static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002980 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Brian Norris4a89ff82011-08-30 18:45:45 -07002982 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002983 int ret;
2984
Huang Shijie6a8214a2012-11-19 14:43:30 +08002985 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002986 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002987 ops.len = len;
2988 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002989 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002990 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002991 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002992 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002993 return ret;
2994}
2995
2996/**
2997 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002998 * @mtd: MTD device structure
2999 * @to: offset to write to
3000 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003001 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003002 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003003 */
3004static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3005 struct mtd_oob_ops *ops)
3006{
Adrian Hunter03736152007-01-31 17:58:29 +02003007 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003008 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Brian Norris289c0522011-07-19 10:06:09 -07003010 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303011 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Boris BREZILLON29f10582016-03-07 10:46:52 +01003013 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003016 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003017 pr_debug("%s: attempt to write past end of page\n",
3018 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return -EINVAL;
3020 }
3021
Adrian Hunter03736152007-01-31 17:58:29 +02003022 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003023 pr_debug("%s: attempt to start write outside oob\n",
3024 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003025 return -EINVAL;
3026 }
3027
Jason Liu775adc32011-02-25 13:06:18 +08003028 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003029 if (unlikely(to >= mtd->size ||
3030 ops->ooboffs + ops->ooblen >
3031 ((mtd->size >> chip->page_shift) -
3032 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003033 pr_debug("%s: attempt to write beyond end of device\n",
3034 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003035 return -EINVAL;
3036 }
3037
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003038 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003039
3040 /*
3041 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3042 * of my DiskOnChip 2000 test units) will clear the whole data page too
3043 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3044 * it in the doc2000 driver in August 1999. dwmw2.
3045 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003046 nand_reset(chip, chipnr);
3047
3048 chip->select_chip(mtd, chipnr);
3049
3050 /* Shift to get page */
3051 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003054 if (nand_check_wp(mtd)) {
3055 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003056 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003057 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003060 if (page == chip->pagebuf)
3061 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02003063 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003064
Brian Norris0612b9d2011-08-30 18:45:40 -07003065 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003066 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3067 else
3068 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003069
Huang Shijieb0bb6902012-11-19 14:43:29 +08003070 chip->select_chip(mtd, -1);
3071
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003072 if (status)
3073 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Vitaly Wool70145682006-11-03 18:20:38 +03003075 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003077 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003080/**
3081 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003082 * @mtd: MTD device structure
3083 * @to: offset to write to
3084 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003085 */
3086static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3087 struct mtd_oob_ops *ops)
3088{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003089 int ret = -ENOTSUPP;
3090
3091 ops->retlen = 0;
3092
3093 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003094 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003095 pr_debug("%s: attempt to write beyond end of device\n",
3096 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003097 return -EINVAL;
3098 }
3099
Huang Shijie6a8214a2012-11-19 14:43:30 +08003100 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003101
Florian Fainellif8ac0412010-09-07 13:23:43 +02003102 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003103 case MTD_OPS_PLACE_OOB:
3104 case MTD_OPS_AUTO_OOB:
3105 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003106 break;
3107
3108 default:
3109 goto out;
3110 }
3111
3112 if (!ops->datbuf)
3113 ret = nand_do_write_oob(mtd, to, ops);
3114 else
3115 ret = nand_do_write_ops(mtd, to, ops);
3116
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003117out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003118 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return ret;
3120}
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122/**
Brian Norris49c50b92014-05-06 16:02:19 -07003123 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003124 * @mtd: MTD device structure
3125 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 *
Brian Norris49c50b92014-05-06 16:02:19 -07003127 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 */
Brian Norris49c50b92014-05-06 16:02:19 -07003129static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003131 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003133 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3134 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003135
3136 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
3139/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003141 * @mtd: MTD device structure
3142 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003144 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003146static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
David Woodhousee0c7d762006-05-13 18:07:53 +01003148 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003152 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003153 * @mtd: MTD device structure
3154 * @instr: erase instruction
3155 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003157 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003159int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3160 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Adrian Hunter69423d92008-12-10 13:37:21 +00003162 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003163 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003164 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Brian Norris289c0522011-07-19 10:06:09 -07003166 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3167 __func__, (unsigned long long)instr->addr,
3168 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303170 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003174 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003177 page = (int)(instr->addr >> chip->page_shift);
3178 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003181 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003184 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 /* Check, if it is write protected */
3187 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003188 pr_debug("%s: device is write protected!\n",
3189 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 instr->state = MTD_ERASE_FAILED;
3191 goto erase_exit;
3192 }
3193
3194 /* Loop through the pages */
3195 len = instr->len;
3196
3197 instr->state = MTD_ERASING;
3198
3199 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003200 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003201 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303202 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003203 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3204 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 instr->state = MTD_ERASE_FAILED;
3206 goto erase_exit;
3207 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003208
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003209 /*
3210 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003211 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003212 */
3213 if (page <= chip->pagebuf && chip->pagebuf <
3214 (page + pages_per_block))
3215 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Brian Norris49c50b92014-05-06 16:02:19 -07003217 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003219 /*
3220 * See if operation failed and additional status checks are
3221 * available
3222 */
3223 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
3224 status = chip->errstat(mtd, chip, FL_ERASING,
3225 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00003226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003228 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003229 pr_debug("%s: failed erase, page 0x%08x\n",
3230 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003232 instr->fail_addr =
3233 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 goto erase_exit;
3235 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003238 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 page += pages_per_block;
3240
3241 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003242 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003244 chip->select_chip(mtd, -1);
3245 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247 }
3248 instr->state = MTD_ERASE_DONE;
3249
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003250erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003255 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 nand_release_device(mtd);
3257
David Woodhouse49defc02007-10-06 15:01:59 -04003258 /* Do call back function */
3259 if (!ret)
3260 mtd_erase_callback(instr);
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 /* Return more or less happy */
3263 return ret;
3264}
3265
3266/**
3267 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003268 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003270 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003272static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Brian Norris289c0522011-07-19 10:06:09 -07003274 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003277 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003279 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003283 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * @mtd: MTD device structure
3285 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003287static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303289 struct nand_chip *chip = mtd_to_nand(mtd);
3290 int chipnr = (int)(offs >> chip->chip_shift);
3291 int ret;
3292
3293 /* Select the NAND device */
3294 nand_get_device(mtd, FL_READING);
3295 chip->select_chip(mtd, chipnr);
3296
3297 ret = nand_block_checkbad(mtd, offs, 0);
3298
3299 chip->select_chip(mtd, -1);
3300 nand_release_device(mtd);
3301
3302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003306 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003307 * @mtd: MTD device structure
3308 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003310static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 int ret;
3313
Florian Fainellif8ac0412010-09-07 13:23:43 +02003314 ret = nand_block_isbad(mtd, ofs);
3315 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003316 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 if (ret > 0)
3318 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003319 return ret;
3320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Brian Norris5a0edb22013-07-30 17:52:58 -07003322 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
3325/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003326 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3327 * @mtd: MTD device structure
3328 * @chip: nand chip info structure
3329 * @addr: feature address.
3330 * @subfeature_param: the subfeature parameters, a four bytes array.
3331 */
3332static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3333 int addr, uint8_t *subfeature_param)
3334{
3335 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003336 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003337
David Mosbergerd914c932013-05-29 15:30:13 +03003338 if (!chip->onfi_version ||
3339 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3340 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003341 return -EINVAL;
3342
3343 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003344 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3345 chip->write_byte(mtd, subfeature_param[i]);
3346
Huang Shijie7db03ec2012-09-13 14:57:52 +08003347 status = chip->waitfunc(mtd, chip);
3348 if (status & NAND_STATUS_FAIL)
3349 return -EIO;
3350 return 0;
3351}
3352
3353/**
3354 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3355 * @mtd: MTD device structure
3356 * @chip: nand chip info structure
3357 * @addr: feature address.
3358 * @subfeature_param: the subfeature parameters, a four bytes array.
3359 */
3360static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3361 int addr, uint8_t *subfeature_param)
3362{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003363 int i;
3364
David Mosbergerd914c932013-05-29 15:30:13 +03003365 if (!chip->onfi_version ||
3366 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3367 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003368 return -EINVAL;
3369
Huang Shijie7db03ec2012-09-13 14:57:52 +08003370 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003371 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3372 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003373 return 0;
3374}
3375
3376/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003377 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003378 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003379 */
3380static int nand_suspend(struct mtd_info *mtd)
3381{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003382 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003383}
3384
3385/**
3386 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003387 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003388 */
3389static void nand_resume(struct mtd_info *mtd)
3390{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003391 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003392
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003393 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003394 nand_release_device(mtd);
3395 else
Brian Norrisd0370212011-07-19 10:06:08 -07003396 pr_err("%s called for a chip which is not in suspended state\n",
3397 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003398}
3399
Scott Branden72ea4032014-11-20 11:18:05 -08003400/**
3401 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3402 * prevent further operations
3403 * @mtd: MTD device structure
3404 */
3405static void nand_shutdown(struct mtd_info *mtd)
3406{
Brian Norris9ca641b2015-11-09 16:37:28 -08003407 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003408}
3409
Brian Norris8b6e50c2011-05-25 14:59:01 -07003410/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003411static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003412{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003414 if (!chip->chip_delay)
3415 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003418 if (chip->cmdfunc == NULL)
3419 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
3421 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003422 if (chip->waitfunc == NULL)
3423 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003425 if (!chip->select_chip)
3426 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003427
Huang Shijie4204ccc2013-08-16 10:10:07 +08003428 /* set for ONFI nand */
3429 if (!chip->onfi_set_features)
3430 chip->onfi_set_features = nand_onfi_set_features;
3431 if (!chip->onfi_get_features)
3432 chip->onfi_get_features = nand_onfi_get_features;
3433
Brian Norris68e80782013-07-18 01:17:02 -07003434 /* If called twice, pointers that depend on busw may need to be reset */
3435 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003436 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3437 if (!chip->read_word)
3438 chip->read_word = nand_read_word;
3439 if (!chip->block_bad)
3440 chip->block_bad = nand_block_bad;
3441 if (!chip->block_markbad)
3442 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003443 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003444 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003445 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3446 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003447 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003448 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003449 if (!chip->scan_bbt)
3450 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003451
3452 if (!chip->controller) {
3453 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003454 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003455 }
3456
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003457}
3458
Brian Norris8b6e50c2011-05-25 14:59:01 -07003459/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003460static void sanitize_string(uint8_t *s, size_t len)
3461{
3462 ssize_t i;
3463
Brian Norris8b6e50c2011-05-25 14:59:01 -07003464 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003465 s[len - 1] = 0;
3466
Brian Norris8b6e50c2011-05-25 14:59:01 -07003467 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003468 for (i = 0; i < len - 1; i++) {
3469 if (s[i] < ' ' || s[i] > 127)
3470 s[i] = '?';
3471 }
3472
Brian Norris8b6e50c2011-05-25 14:59:01 -07003473 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003474 strim(s);
3475}
3476
3477static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3478{
3479 int i;
3480 while (len--) {
3481 crc ^= *p++ << 8;
3482 for (i = 0; i < 8; i++)
3483 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3484 }
3485
3486 return crc;
3487}
3488
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003489/* Parse the Extended Parameter Page. */
3490static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3491 struct nand_chip *chip, struct nand_onfi_params *p)
3492{
3493 struct onfi_ext_param_page *ep;
3494 struct onfi_ext_section *s;
3495 struct onfi_ext_ecc_info *ecc;
3496 uint8_t *cursor;
3497 int ret = -EINVAL;
3498 int len;
3499 int i;
3500
3501 len = le16_to_cpu(p->ext_param_page_length) * 16;
3502 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003503 if (!ep)
3504 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003505
3506 /* Send our own NAND_CMD_PARAM. */
3507 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3508
3509 /* Use the Change Read Column command to skip the ONFI param pages. */
3510 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3511 sizeof(*p) * p->num_of_param_pages , -1);
3512
3513 /* Read out the Extended Parameter Page. */
3514 chip->read_buf(mtd, (uint8_t *)ep, len);
3515 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3516 != le16_to_cpu(ep->crc))) {
3517 pr_debug("fail in the CRC.\n");
3518 goto ext_out;
3519 }
3520
3521 /*
3522 * Check the signature.
3523 * Do not strictly follow the ONFI spec, maybe changed in future.
3524 */
3525 if (strncmp(ep->sig, "EPPS", 4)) {
3526 pr_debug("The signature is invalid.\n");
3527 goto ext_out;
3528 }
3529
3530 /* find the ECC section. */
3531 cursor = (uint8_t *)(ep + 1);
3532 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3533 s = ep->sections + i;
3534 if (s->type == ONFI_SECTION_TYPE_2)
3535 break;
3536 cursor += s->length * 16;
3537 }
3538 if (i == ONFI_EXT_SECTION_MAX) {
3539 pr_debug("We can not find the ECC section.\n");
3540 goto ext_out;
3541 }
3542
3543 /* get the info we want. */
3544 ecc = (struct onfi_ext_ecc_info *)cursor;
3545
Brian Norris4ae7d222013-09-16 18:20:21 -07003546 if (!ecc->codeword_size) {
3547 pr_debug("Invalid codeword size\n");
3548 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003549 }
3550
Brian Norris4ae7d222013-09-16 18:20:21 -07003551 chip->ecc_strength_ds = ecc->ecc_bits;
3552 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003553 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003554
3555ext_out:
3556 kfree(ep);
3557 return ret;
3558}
3559
Brian Norris8429bb32013-12-03 15:51:09 -08003560static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3561{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003562 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003563 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3564
3565 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3566 feature);
3567}
3568
3569/*
3570 * Configure chip properties from Micron vendor-specific ONFI table
3571 */
3572static void nand_onfi_detect_micron(struct nand_chip *chip,
3573 struct nand_onfi_params *p)
3574{
3575 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3576
3577 if (le16_to_cpu(p->vendor_revision) < 1)
3578 return;
3579
3580 chip->read_retries = micron->read_retry_options;
3581 chip->setup_read_retry = nand_setup_read_retry_micron;
3582}
3583
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003584/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003585 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003586 */
3587static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003588 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003589{
3590 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003591 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003592 int val;
3593
Brian Norris7854d3f2011-06-23 14:12:08 -07003594 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003595 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3596 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3597 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3598 return 0;
3599
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003600 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3601 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003602 for (j = 0; j < sizeof(*p); j++)
3603 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003604 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3605 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003606 break;
3607 }
3608 }
3609
Brian Norrisc7f23a72013-08-13 10:51:55 -07003610 if (i == 3) {
3611 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003612 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003613 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003614
Brian Norris8b6e50c2011-05-25 14:59:01 -07003615 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003616 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003617 if (val & (1 << 5))
3618 chip->onfi_version = 23;
3619 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003620 chip->onfi_version = 22;
3621 else if (val & (1 << 3))
3622 chip->onfi_version = 21;
3623 else if (val & (1 << 2))
3624 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003625 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003626 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003627
3628 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003629 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003630 return 0;
3631 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003632
3633 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3634 sanitize_string(p->model, sizeof(p->model));
3635 if (!mtd->name)
3636 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003637
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003638 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003639
3640 /*
3641 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3642 * (don't ask me who thought of this...). MTD assumes that these
3643 * dimensions will be power-of-2, so just truncate the remaining area.
3644 */
3645 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3646 mtd->erasesize *= mtd->writesize;
3647
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003648 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003649
3650 /* See erasesize comment */
3651 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003652 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003653 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003654
3655 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003656 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003657 else
3658 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003659
Huang Shijie10c86ba2013-05-17 11:17:26 +08003660 if (p->ecc_bits != 0xff) {
3661 chip->ecc_strength_ds = p->ecc_bits;
3662 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003663 } else if (chip->onfi_version >= 21 &&
3664 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3665
3666 /*
3667 * The nand_flash_detect_ext_param_page() uses the
3668 * Change Read Column command which maybe not supported
3669 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3670 * now. We do not replace user supplied command function.
3671 */
3672 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3673 chip->cmdfunc = nand_command_lp;
3674
3675 /* The Extended Parameter Page is supported since ONFI 2.1. */
3676 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003677 pr_warn("Failed to detect ONFI extended param page\n");
3678 } else {
3679 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003680 }
3681
Brian Norris8429bb32013-12-03 15:51:09 -08003682 if (p->jedec_id == NAND_MFR_MICRON)
3683 nand_onfi_detect_micron(chip, p);
3684
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003685 return 1;
3686}
3687
3688/*
Huang Shijie91361812014-02-21 13:39:40 +08003689 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3690 */
3691static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3692 int *busw)
3693{
3694 struct nand_jedec_params *p = &chip->jedec_params;
3695 struct jedec_ecc_info *ecc;
3696 int val;
3697 int i, j;
3698
3699 /* Try JEDEC for unknown chip or LP */
3700 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3701 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3702 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3703 chip->read_byte(mtd) != 'C')
3704 return 0;
3705
3706 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3707 for (i = 0; i < 3; i++) {
3708 for (j = 0; j < sizeof(*p); j++)
3709 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3710
3711 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3712 le16_to_cpu(p->crc))
3713 break;
3714 }
3715
3716 if (i == 3) {
3717 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3718 return 0;
3719 }
3720
3721 /* Check version */
3722 val = le16_to_cpu(p->revision);
3723 if (val & (1 << 2))
3724 chip->jedec_version = 10;
3725 else if (val & (1 << 1))
3726 chip->jedec_version = 1; /* vendor specific version */
3727
3728 if (!chip->jedec_version) {
3729 pr_info("unsupported JEDEC version: %d\n", val);
3730 return 0;
3731 }
3732
3733 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3734 sanitize_string(p->model, sizeof(p->model));
3735 if (!mtd->name)
3736 mtd->name = p->model;
3737
3738 mtd->writesize = le32_to_cpu(p->byte_per_page);
3739
3740 /* Please reference to the comment for nand_flash_detect_onfi. */
3741 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3742 mtd->erasesize *= mtd->writesize;
3743
3744 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3745
3746 /* Please reference to the comment for nand_flash_detect_onfi. */
3747 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3748 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3749 chip->bits_per_cell = p->bits_per_cell;
3750
3751 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3752 *busw = NAND_BUSWIDTH_16;
3753 else
3754 *busw = 0;
3755
3756 /* ECC info */
3757 ecc = &p->ecc_info[0];
3758
3759 if (ecc->codeword_size >= 9) {
3760 chip->ecc_strength_ds = ecc->ecc_bits;
3761 chip->ecc_step_ds = 1 << ecc->codeword_size;
3762 } else {
3763 pr_warn("Invalid codeword size\n");
3764 }
3765
3766 return 1;
3767}
3768
3769/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003770 * nand_id_has_period - Check if an ID string has a given wraparound period
3771 * @id_data: the ID string
3772 * @arrlen: the length of the @id_data array
3773 * @period: the period of repitition
3774 *
3775 * Check if an ID string is repeated within a given sequence of bytes at
3776 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003777 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003778 * if the repetition has a period of @period; otherwise, returns zero.
3779 */
3780static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3781{
3782 int i, j;
3783 for (i = 0; i < period; i++)
3784 for (j = i + period; j < arrlen; j += period)
3785 if (id_data[i] != id_data[j])
3786 return 0;
3787 return 1;
3788}
3789
3790/*
3791 * nand_id_len - Get the length of an ID string returned by CMD_READID
3792 * @id_data: the ID string
3793 * @arrlen: the length of the @id_data array
3794
3795 * Returns the length of the ID string, according to known wraparound/trailing
3796 * zero patterns. If no pattern exists, returns the length of the array.
3797 */
3798static int nand_id_len(u8 *id_data, int arrlen)
3799{
3800 int last_nonzero, period;
3801
3802 /* Find last non-zero byte */
3803 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3804 if (id_data[last_nonzero])
3805 break;
3806
3807 /* All zeros */
3808 if (last_nonzero < 0)
3809 return 0;
3810
3811 /* Calculate wraparound period */
3812 for (period = 1; period < arrlen; period++)
3813 if (nand_id_has_period(id_data, arrlen, period))
3814 break;
3815
3816 /* There's a repeated pattern */
3817 if (period < arrlen)
3818 return period;
3819
3820 /* There are trailing zeros */
3821 if (last_nonzero < arrlen - 1)
3822 return last_nonzero + 1;
3823
3824 /* No pattern detected */
3825 return arrlen;
3826}
3827
Huang Shijie7db906b2013-09-25 14:58:11 +08003828/* Extract the bits of per cell from the 3rd byte of the extended ID */
3829static int nand_get_bits_per_cell(u8 cellinfo)
3830{
3831 int bits;
3832
3833 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3834 bits >>= NAND_CI_CELLTYPE_SHIFT;
3835 return bits + 1;
3836}
3837
Brian Norrise3b88bd2012-09-24 20:40:52 -07003838/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003839 * Many new NAND share similar device ID codes, which represent the size of the
3840 * chip. The rest of the parameters must be decoded according to generic or
3841 * manufacturer-specific "extended ID" decoding patterns.
3842 */
3843static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3844 u8 id_data[8], int *busw)
3845{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003846 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003847 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003848 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003849 /* The 4th id byte is the important one */
3850 extid = id_data[3];
3851
Brian Norrise3b88bd2012-09-24 20:40:52 -07003852 id_len = nand_id_len(id_data, 8);
3853
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003854 /*
3855 * Field definitions are in the following datasheets:
3856 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003857 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003858 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003859 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003860 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3861 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003862 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003863 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003864 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003865 /* Calc pagesize */
3866 mtd->writesize = 2048 << (extid & 0x03);
3867 extid >>= 2;
3868 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003869 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003870 case 1:
3871 mtd->oobsize = 128;
3872 break;
3873 case 2:
3874 mtd->oobsize = 218;
3875 break;
3876 case 3:
3877 mtd->oobsize = 400;
3878 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003879 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003880 mtd->oobsize = 436;
3881 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003882 case 5:
3883 mtd->oobsize = 512;
3884 break;
3885 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003886 mtd->oobsize = 640;
3887 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003888 case 7:
3889 default: /* Other cases are "reserved" (unknown) */
3890 mtd->oobsize = 1024;
3891 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003892 }
3893 extid >>= 2;
3894 /* Calc blocksize */
3895 mtd->erasesize = (128 * 1024) <<
3896 (((extid >> 1) & 0x04) | (extid & 0x03));
3897 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003898 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003899 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003900 unsigned int tmp;
3901
3902 /* Calc pagesize */
3903 mtd->writesize = 2048 << (extid & 0x03);
3904 extid >>= 2;
3905 /* Calc oobsize */
3906 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3907 case 0:
3908 mtd->oobsize = 128;
3909 break;
3910 case 1:
3911 mtd->oobsize = 224;
3912 break;
3913 case 2:
3914 mtd->oobsize = 448;
3915 break;
3916 case 3:
3917 mtd->oobsize = 64;
3918 break;
3919 case 4:
3920 mtd->oobsize = 32;
3921 break;
3922 case 5:
3923 mtd->oobsize = 16;
3924 break;
3925 default:
3926 mtd->oobsize = 640;
3927 break;
3928 }
3929 extid >>= 2;
3930 /* Calc blocksize */
3931 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3932 if (tmp < 0x03)
3933 mtd->erasesize = (128 * 1024) << tmp;
3934 else if (tmp == 0x03)
3935 mtd->erasesize = 768 * 1024;
3936 else
3937 mtd->erasesize = (64 * 1024) << tmp;
3938 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003939 } else {
3940 /* Calc pagesize */
3941 mtd->writesize = 1024 << (extid & 0x03);
3942 extid >>= 2;
3943 /* Calc oobsize */
3944 mtd->oobsize = (8 << (extid & 0x01)) *
3945 (mtd->writesize >> 9);
3946 extid >>= 2;
3947 /* Calc blocksize. Blocksize is multiples of 64KiB */
3948 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3949 extid >>= 2;
3950 /* Get buswidth information */
3951 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003952
3953 /*
3954 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3955 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3956 * follows:
3957 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3958 * 110b -> 24nm
3959 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3960 */
3961 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003962 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003963 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3964 !(id_data[4] & 0x80) /* !BENAND */) {
3965 mtd->oobsize = 32 * mtd->writesize >> 9;
3966 }
3967
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003968 }
3969}
3970
3971/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003972 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3973 * decodes a matching ID table entry and assigns the MTD size parameters for
3974 * the chip.
3975 */
3976static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3977 struct nand_flash_dev *type, u8 id_data[8],
3978 int *busw)
3979{
3980 int maf_id = id_data[0];
3981
3982 mtd->erasesize = type->erasesize;
3983 mtd->writesize = type->pagesize;
3984 mtd->oobsize = mtd->writesize / 32;
3985 *busw = type->options & NAND_BUSWIDTH_16;
3986
Huang Shijie1c195e92013-09-25 14:58:12 +08003987 /* All legacy ID NAND are small-page, SLC */
3988 chip->bits_per_cell = 1;
3989
Brian Norrisf23a4812012-09-24 20:40:51 -07003990 /*
3991 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3992 * some Spansion chips have erasesize that conflicts with size
3993 * listed in nand_ids table.
3994 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3995 */
3996 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3997 && id_data[6] == 0x00 && id_data[7] == 0x00
3998 && mtd->writesize == 512) {
3999 mtd->erasesize = 128 * 1024;
4000 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
4001 }
4002}
4003
4004/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07004005 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
4006 * heuristic patterns using various detected parameters (e.g., manufacturer,
4007 * page size, cell-type information).
4008 */
4009static void nand_decode_bbm_options(struct mtd_info *mtd,
4010 struct nand_chip *chip, u8 id_data[8])
4011{
4012 int maf_id = id_data[0];
4013
4014 /* Set the bad block position */
4015 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
4016 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
4017 else
4018 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
4019
4020 /*
4021 * Bad block marker is stored in the last page of each block on Samsung
4022 * and Hynix MLC devices; stored in first two pages of each block on
4023 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
4024 * AMD/Spansion, and Macronix. All others scan only the first page.
4025 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004026 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07004027 (maf_id == NAND_MFR_SAMSUNG ||
4028 maf_id == NAND_MFR_HYNIX))
4029 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08004030 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07004031 (maf_id == NAND_MFR_SAMSUNG ||
4032 maf_id == NAND_MFR_HYNIX ||
4033 maf_id == NAND_MFR_TOSHIBA ||
4034 maf_id == NAND_MFR_AMD ||
4035 maf_id == NAND_MFR_MACRONIX)) ||
4036 (mtd->writesize == 2048 &&
4037 maf_id == NAND_MFR_MICRON))
4038 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
4039}
4040
Huang Shijieec6e87e2013-03-15 11:01:00 +08004041static inline bool is_full_id_nand(struct nand_flash_dev *type)
4042{
4043 return type->id_len;
4044}
4045
4046static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
4047 struct nand_flash_dev *type, u8 *id_data, int *busw)
4048{
4049 if (!strncmp(type->id, id_data, type->id_len)) {
4050 mtd->writesize = type->pagesize;
4051 mtd->erasesize = type->erasesize;
4052 mtd->oobsize = type->oobsize;
4053
Huang Shijie7db906b2013-09-25 14:58:11 +08004054 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08004055 chip->chipsize = (uint64_t)type->chipsize << 20;
4056 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08004057 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
4058 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02004059 chip->onfi_timing_mode_default =
4060 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004061
4062 *busw = type->options & NAND_BUSWIDTH_16;
4063
Cai Zhiyong092b6a12013-12-25 21:19:21 +08004064 if (!mtd->name)
4065 mtd->name = type->name;
4066
Huang Shijieec6e87e2013-03-15 11:01:00 +08004067 return true;
4068 }
4069 return false;
4070}
4071
Brian Norris7e74c2d2012-09-24 20:40:49 -07004072/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004073 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004074 */
4075static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004076 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004077 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00004078 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004079{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004080 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004081 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07004082 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Karl Beldanef89a882008-09-15 14:37:29 +02004084 /*
4085 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004086 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004087 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004088 nand_reset(chip, 0);
4089
4090 /* Select the device */
4091 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004094 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004097 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004098 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Brian Norris8b6e50c2011-05-25 14:59:01 -07004100 /*
4101 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004102 * interface concerns can cause random data which looks like a
4103 * possibly credible NAND flash to appear. If the two results do
4104 * not match, ignore the device completely.
4105 */
4106
4107 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4108
Brian Norris4aef9b72012-09-24 20:40:48 -07004109 /* Read entire ID string */
4110 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004111 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004112
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004113 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004114 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004115 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01004116 return ERR_PTR(-ENODEV);
4117 }
4118
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004119 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004120 type = nand_flash_ids;
4121
Huang Shijieec6e87e2013-03-15 11:01:00 +08004122 for (; type->name != NULL; type++) {
4123 if (is_full_id_nand(type)) {
4124 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
4125 goto ident_done;
4126 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004127 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004128 }
4129 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004130
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004131 chip->onfi_version = 0;
4132 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004133 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07004134 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004135 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004136
4137 /* Check if the chip is JEDEC compliant */
4138 if (nand_flash_detect_jedec(mtd, chip, &busw))
4139 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004140 }
4141
David Woodhouse5e81e882010-02-26 18:32:56 +00004142 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004143 return ERR_PTR(-ENODEV);
4144
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004145 if (!mtd->name)
4146 mtd->name = type->name;
4147
Adrian Hunter69423d92008-12-10 13:37:21 +00004148 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004149
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02004150 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07004151 /* Decode parameters from extended ID */
4152 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004153 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07004154 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004155 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004156 /* Get chip options */
4157 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004158
Brian Norris8b6e50c2011-05-25 14:59:01 -07004159 /*
4160 * Check if chip is not a Samsung device. Do not clear the
4161 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004162 */
4163 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
4164 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
4165ident_done:
4166
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004167 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01004168 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004169 if (nand_manuf_ids[maf_idx].id == *maf_id)
4170 break;
4171 }
4172
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004173 if (chip->options & NAND_BUSWIDTH_AUTO) {
4174 WARN_ON(chip->options & NAND_BUSWIDTH_16);
4175 chip->options |= busw;
4176 nand_set_defaults(chip, busw);
4177 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4178 /*
4179 * Check, if buswidth is correct. Hardware drivers should set
4180 * chip correct!
4181 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004182 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4183 *maf_id, *dev_id);
4184 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
4185 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07004186 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
4187 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004188 return ERR_PTR(-EINVAL);
4189 }
4190
Brian Norris7e74c2d2012-09-24 20:40:49 -07004191 nand_decode_bbm_options(mtd, chip, id_data);
4192
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004193 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004194 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004195 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004196 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004197
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004198 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004199 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004200 if (chip->chipsize & 0xffffffff)
4201 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004202 else {
4203 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4204 chip->chip_shift += 32 - 1;
4205 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004206
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004207 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004208 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004209
Brian Norris8b6e50c2011-05-25 14:59:01 -07004210 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004211 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4212 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004213
Ezequiel Garcia20171642013-11-25 08:30:31 -03004214 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4215 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004216
4217 if (chip->onfi_version)
4218 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4219 chip->onfi_params.model);
4220 else if (chip->jedec_version)
4221 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4222 chip->jedec_params.model);
4223 else
4224 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
4225 type->name);
4226
Rafał Miłecki3755a992014-10-21 00:01:04 +02004227 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004228 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004229 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004230 return type;
4231}
4232
Boris Brezillond48f62b2016-04-01 14:54:32 +02004233static const char * const nand_ecc_modes[] = {
4234 [NAND_ECC_NONE] = "none",
4235 [NAND_ECC_SOFT] = "soft",
4236 [NAND_ECC_HW] = "hw",
4237 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4238 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004239};
4240
4241static int of_get_nand_ecc_mode(struct device_node *np)
4242{
4243 const char *pm;
4244 int err, i;
4245
4246 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4247 if (err < 0)
4248 return err;
4249
4250 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4251 if (!strcasecmp(pm, nand_ecc_modes[i]))
4252 return i;
4253
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004254 /*
4255 * For backward compatibility we support few obsoleted values that don't
4256 * have their mappings into nand_ecc_modes_t anymore (they were merged
4257 * with other enums).
4258 */
4259 if (!strcasecmp(pm, "soft_bch"))
4260 return NAND_ECC_SOFT;
4261
Boris Brezillond48f62b2016-04-01 14:54:32 +02004262 return -ENODEV;
4263}
4264
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004265static const char * const nand_ecc_algos[] = {
4266 [NAND_ECC_HAMMING] = "hamming",
4267 [NAND_ECC_BCH] = "bch",
4268};
4269
Boris Brezillond48f62b2016-04-01 14:54:32 +02004270static int of_get_nand_ecc_algo(struct device_node *np)
4271{
4272 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004273 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004274
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004275 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4276 if (!err) {
4277 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4278 if (!strcasecmp(pm, nand_ecc_algos[i]))
4279 return i;
4280 return -ENODEV;
4281 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004282
4283 /*
4284 * For backward compatibility we also read "nand-ecc-mode" checking
4285 * for some obsoleted values that were specifying ECC algorithm.
4286 */
4287 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4288 if (err < 0)
4289 return err;
4290
4291 if (!strcasecmp(pm, "soft"))
4292 return NAND_ECC_HAMMING;
4293 else if (!strcasecmp(pm, "soft_bch"))
4294 return NAND_ECC_BCH;
4295
4296 return -ENODEV;
4297}
4298
4299static int of_get_nand_ecc_step_size(struct device_node *np)
4300{
4301 int ret;
4302 u32 val;
4303
4304 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4305 return ret ? ret : val;
4306}
4307
4308static int of_get_nand_ecc_strength(struct device_node *np)
4309{
4310 int ret;
4311 u32 val;
4312
4313 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4314 return ret ? ret : val;
4315}
4316
4317static int of_get_nand_bus_width(struct device_node *np)
4318{
4319 u32 val;
4320
4321 if (of_property_read_u32(np, "nand-bus-width", &val))
4322 return 8;
4323
4324 switch (val) {
4325 case 8:
4326 case 16:
4327 return val;
4328 default:
4329 return -EIO;
4330 }
4331}
4332
4333static bool of_get_nand_on_flash_bbt(struct device_node *np)
4334{
4335 return of_property_read_bool(np, "nand-on-flash-bbt");
4336}
4337
Boris BREZILLON7194a292015-12-10 09:00:37 +01004338static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004339{
Boris BREZILLON7194a292015-12-10 09:00:37 +01004340 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004341 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004342
Boris BREZILLON7194a292015-12-10 09:00:37 +01004343 if (!dn)
4344 return 0;
4345
Brian Norris5844fee2015-01-23 00:22:27 -08004346 if (of_get_nand_bus_width(dn) == 16)
4347 chip->options |= NAND_BUSWIDTH_16;
4348
4349 if (of_get_nand_on_flash_bbt(dn))
4350 chip->bbt_options |= NAND_BBT_USE_FLASH;
4351
4352 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004353 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004354 ecc_strength = of_get_nand_ecc_strength(dn);
4355 ecc_step = of_get_nand_ecc_step_size(dn);
4356
4357 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
4358 (!(ecc_step >= 0) && ecc_strength >= 0)) {
4359 pr_err("must set both strength and step size in DT\n");
4360 return -EINVAL;
4361 }
4362
4363 if (ecc_mode >= 0)
4364 chip->ecc.mode = ecc_mode;
4365
Rafał Miłecki79082452016-03-23 11:19:02 +01004366 if (ecc_algo >= 0)
4367 chip->ecc.algo = ecc_algo;
4368
Brian Norris5844fee2015-01-23 00:22:27 -08004369 if (ecc_strength >= 0)
4370 chip->ecc.strength = ecc_strength;
4371
4372 if (ecc_step > 0)
4373 chip->ecc.size = ecc_step;
4374
Boris Brezillonba78ee02016-06-08 17:04:22 +02004375 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4376 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4377
Brian Norris5844fee2015-01-23 00:22:27 -08004378 return 0;
4379}
4380
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004381/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004382 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004383 * @mtd: MTD device structure
4384 * @maxchips: number of chips to scan for
4385 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004386 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004387 * This is the first phase of the normal nand_scan() function. It reads the
4388 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004389 *
4390 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004391int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4392 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004393{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004394 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004395 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004396 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08004397 int ret;
4398
Boris BREZILLON7194a292015-12-10 09:00:37 +01004399 ret = nand_dt_init(chip);
4400 if (ret)
4401 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004402
Brian Norrisf7a8e382016-01-05 10:39:45 -08004403 if (!mtd->name && mtd->dev.parent)
4404 mtd->name = dev_name(mtd->dev.parent);
4405
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004406 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4407 /*
4408 * Default functions assigned for chip_select() and
4409 * cmdfunc() both expect cmd_ctrl() to be populated,
4410 * so we need to check that that's the case
4411 */
4412 pr_err("chip.cmd_ctrl() callback is not provided");
4413 return -EINVAL;
4414 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004415 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004416 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004417
4418 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004419 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4420 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004421
4422 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004423 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004424 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004425 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004426 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428
Boris Brezillon73f907f2016-10-24 16:46:20 +02004429 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004430 ret = nand_init_data_interface(chip);
4431 if (ret)
4432 return ret;
4433
Boris Brezillon73f907f2016-10-24 16:46:20 +02004434 /*
4435 * Setup the data interface correctly on the chip and controller side.
4436 * This explicit call to nand_setup_data_interface() is only required
4437 * for the first die, because nand_reset() has been called before
4438 * ->data_interface and ->default_onfi_timing_mode were set.
4439 * For the other dies, nand_reset() will automatically switch to the
4440 * best mode for us.
4441 */
4442 ret = nand_setup_data_interface(chip);
4443 if (ret)
4444 return ret;
4445
Huang Shijie07300162012-11-09 16:23:45 +08004446 chip->select_chip(mtd, -1);
4447
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004448 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004449 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004450 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004451 nand_reset(chip, i);
4452
4453 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004455 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004457 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004458 nand_dev_id != chip->read_byte(mtd)) {
4459 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 break;
Huang Shijie07300162012-11-09 16:23:45 +08004461 }
4462 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
4464 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004465 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004468 chip->numchips = i;
4469 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
David Woodhouse3b85c322006-09-25 17:06:53 +01004471 return 0;
4472}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004473EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004474
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004475static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4476{
4477 struct nand_chip *chip = mtd_to_nand(mtd);
4478 struct nand_ecc_ctrl *ecc = &chip->ecc;
4479
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004480 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004481 return -EINVAL;
4482
4483 switch (ecc->algo) {
4484 case NAND_ECC_HAMMING:
4485 ecc->calculate = nand_calculate_ecc;
4486 ecc->correct = nand_correct_data;
4487 ecc->read_page = nand_read_page_swecc;
4488 ecc->read_subpage = nand_read_subpage;
4489 ecc->write_page = nand_write_page_swecc;
4490 ecc->read_page_raw = nand_read_page_raw;
4491 ecc->write_page_raw = nand_write_page_raw;
4492 ecc->read_oob = nand_read_oob_std;
4493 ecc->write_oob = nand_write_oob_std;
4494 if (!ecc->size)
4495 ecc->size = 256;
4496 ecc->bytes = 3;
4497 ecc->strength = 1;
4498 return 0;
4499 case NAND_ECC_BCH:
4500 if (!mtd_nand_has_bch()) {
4501 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4502 return -EINVAL;
4503 }
4504 ecc->calculate = nand_bch_calculate_ecc;
4505 ecc->correct = nand_bch_correct_data;
4506 ecc->read_page = nand_read_page_swecc;
4507 ecc->read_subpage = nand_read_subpage;
4508 ecc->write_page = nand_write_page_swecc;
4509 ecc->read_page_raw = nand_read_page_raw;
4510 ecc->write_page_raw = nand_write_page_raw;
4511 ecc->read_oob = nand_read_oob_std;
4512 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004513
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004514 /*
4515 * Board driver should supply ecc.size and ecc.strength
4516 * values to select how many bits are correctable.
4517 * Otherwise, default to 4 bits for large page devices.
4518 */
4519 if (!ecc->size && (mtd->oobsize >= 64)) {
4520 ecc->size = 512;
4521 ecc->strength = 4;
4522 }
4523
4524 /*
4525 * if no ecc placement scheme was provided pickup the default
4526 * large page one.
4527 */
4528 if (!mtd->ooblayout) {
4529 /* handle large page devices only */
4530 if (mtd->oobsize < 64) {
4531 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4532 return -EINVAL;
4533 }
4534
4535 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004536
4537 }
4538
4539 /*
4540 * We can only maximize ECC config when the default layout is
4541 * used, otherwise we don't know how many bytes can really be
4542 * used.
4543 */
4544 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4545 ecc->options & NAND_ECC_MAXIMIZE) {
4546 int steps, bytes;
4547
4548 /* Always prefer 1k blocks over 512bytes ones */
4549 ecc->size = 1024;
4550 steps = mtd->writesize / ecc->size;
4551
4552 /* Reserve 2 bytes for the BBM */
4553 bytes = (mtd->oobsize - 2) / steps;
4554 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004555 }
4556
4557 /* See nand_bch_init() for details. */
4558 ecc->bytes = 0;
4559 ecc->priv = nand_bch_init(mtd);
4560 if (!ecc->priv) {
4561 WARN(1, "BCH ECC initialization failed!\n");
4562 return -EINVAL;
4563 }
4564 return 0;
4565 default:
4566 WARN(1, "Unsupported ECC algorithm!\n");
4567 return -EINVAL;
4568 }
4569}
4570
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004571/*
4572 * Check if the chip configuration meet the datasheet requirements.
4573
4574 * If our configuration corrects A bits per B bytes and the minimum
4575 * required correction level is X bits per Y bytes, then we must ensure
4576 * both of the following are true:
4577 *
4578 * (1) A / B >= X / Y
4579 * (2) A >= X
4580 *
4581 * Requirement (1) ensures we can correct for the required bitflip density.
4582 * Requirement (2) ensures we can correct even when all bitflips are clumped
4583 * in the same sector.
4584 */
4585static bool nand_ecc_strength_good(struct mtd_info *mtd)
4586{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004587 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004588 struct nand_ecc_ctrl *ecc = &chip->ecc;
4589 int corr, ds_corr;
4590
4591 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4592 /* Not enough information */
4593 return true;
4594
4595 /*
4596 * We get the number of corrected bits per page to compare
4597 * the correction density.
4598 */
4599 corr = (mtd->writesize * ecc->strength) / ecc->size;
4600 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4601
4602 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4603}
David Woodhouse3b85c322006-09-25 17:06:53 +01004604
4605/**
4606 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004607 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004608 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004609 * This is the second phase of the normal nand_scan() function. It fills out
4610 * all the uninitialized function pointers with the defaults and scans for a
4611 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004612 */
4613int nand_scan_tail(struct mtd_info *mtd)
4614{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004615 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004616 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004617 struct nand_buffers *nbuf;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004618 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004619
Brian Norrise2414f42012-02-06 13:44:00 -08004620 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004621 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4622 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4623 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004624
Huang Shijief02ea4e2014-01-13 14:27:12 +08004625 if (!(chip->options & NAND_OWN_BUFFERS)) {
4626 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4627 + mtd->oobsize * 3, GFP_KERNEL);
4628 if (!nbuf)
4629 return -ENOMEM;
4630 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4631 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4632 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4633
4634 chip->buffers = nbuf;
4635 } else {
4636 if (!chip->buffers)
4637 return -ENOMEM;
4638 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004639
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004640 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004641 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004642
4643 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004644 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004645 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004646 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004647 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004648 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004651 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 break;
4653 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004654 case 128:
Alexander Couzens5956b282017-05-02 12:19:00 +02004655 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004658 WARN(1, "No oob scheme defined for oobsize %d\n",
4659 mtd->oobsize);
4660 ret = -EINVAL;
4661 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
4663 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004664
David Woodhouse956e9442006-09-25 17:12:39 +01004665 if (!chip->write_page)
4666 chip->write_page = nand_write_page;
4667
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004668 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004669 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004670 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004671 */
David Woodhouse956e9442006-09-25 17:12:39 +01004672
Huang Shijie97de79e02013-10-18 14:20:53 +08004673 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004674 case NAND_ECC_HW_OOB_FIRST:
4675 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004676 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004677 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4678 ret = -EINVAL;
4679 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004680 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004681 if (!ecc->read_page)
4682 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004683
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004684 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004685 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004686 if (!ecc->read_page)
4687 ecc->read_page = nand_read_page_hwecc;
4688 if (!ecc->write_page)
4689 ecc->write_page = nand_write_page_hwecc;
4690 if (!ecc->read_page_raw)
4691 ecc->read_page_raw = nand_read_page_raw;
4692 if (!ecc->write_page_raw)
4693 ecc->write_page_raw = nand_write_page_raw;
4694 if (!ecc->read_oob)
4695 ecc->read_oob = nand_read_oob_std;
4696 if (!ecc->write_oob)
4697 ecc->write_oob = nand_write_oob_std;
4698 if (!ecc->read_subpage)
4699 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004700 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004701 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004702
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004703 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004704 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4705 (!ecc->read_page ||
4706 ecc->read_page == nand_read_page_hwecc ||
4707 !ecc->write_page ||
4708 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004709 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4710 ret = -EINVAL;
4711 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004712 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004713 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004714 if (!ecc->read_page)
4715 ecc->read_page = nand_read_page_syndrome;
4716 if (!ecc->write_page)
4717 ecc->write_page = nand_write_page_syndrome;
4718 if (!ecc->read_page_raw)
4719 ecc->read_page_raw = nand_read_page_raw_syndrome;
4720 if (!ecc->write_page_raw)
4721 ecc->write_page_raw = nand_write_page_raw_syndrome;
4722 if (!ecc->read_oob)
4723 ecc->read_oob = nand_read_oob_syndrome;
4724 if (!ecc->write_oob)
4725 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004726
Huang Shijie97de79e02013-10-18 14:20:53 +08004727 if (mtd->writesize >= ecc->size) {
4728 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004729 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4730 ret = -EINVAL;
4731 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004732 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004733 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004734 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004735 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4736 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004737 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004738 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004740 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004741 ret = nand_set_ecc_soft_ops(mtd);
4742 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004743 ret = -EINVAL;
4744 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004745 }
4746 break;
4747
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004748 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004749 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004750 ecc->read_page = nand_read_page_raw;
4751 ecc->write_page = nand_write_page_raw;
4752 ecc->read_oob = nand_read_oob_std;
4753 ecc->read_page_raw = nand_read_page_raw;
4754 ecc->write_page_raw = nand_write_page_raw;
4755 ecc->write_oob = nand_write_oob_std;
4756 ecc->size = mtd->writesize;
4757 ecc->bytes = 0;
4758 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004762 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4763 ret = -EINVAL;
4764 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Brian Norris9ce244b2011-08-30 18:45:37 -07004767 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004768 if (!ecc->read_oob_raw)
4769 ecc->read_oob_raw = ecc->read_oob;
4770 if (!ecc->write_oob_raw)
4771 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004772
Boris Brezillon846031d2016-02-03 20:11:00 +01004773 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01004774 mtd->ecc_strength = ecc->strength;
4775 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004776
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004777 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004778 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004779 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004780 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004781 ecc->steps = mtd->writesize / ecc->size;
4782 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004783 WARN(1, "Invalid ECC parameters\n");
4784 ret = -EINVAL;
4785 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004787 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada2d0eb3f2017-05-25 13:50:20 +09004788 if (ecc->total > mtd->oobsize) {
4789 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
4790 ret = -EINVAL;
4791 goto err_free;
4792 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004793
Boris Brezillon846031d2016-02-03 20:11:00 +01004794 /*
4795 * The number of bytes available for a client to place data into
4796 * the out of band area.
4797 */
4798 ret = mtd_ooblayout_count_freebytes(mtd);
4799 if (ret < 0)
4800 ret = 0;
4801
4802 mtd->oobavail = ret;
4803
4804 /* ECC sanity check: warn if it's too weak */
4805 if (!nand_ecc_strength_good(mtd))
4806 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4807 mtd->name);
4808
Brian Norris8b6e50c2011-05-25 14:59:01 -07004809 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004810 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004811 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004812 case 2:
4813 mtd->subpage_sft = 1;
4814 break;
4815 case 4:
4816 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004817 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004818 mtd->subpage_sft = 2;
4819 break;
4820 }
4821 }
4822 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4823
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004824 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004825 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004828 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004830 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304831 switch (ecc->mode) {
4832 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09304833 if (chip->page_shift > 9)
4834 chip->options |= NAND_SUBPAGE_READ;
4835 break;
4836
4837 default:
4838 break;
4839 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004842 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004843 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4844 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004845 mtd->_erase = nand_erase;
4846 mtd->_point = NULL;
4847 mtd->_unpoint = NULL;
4848 mtd->_read = nand_read;
4849 mtd->_write = nand_write;
4850 mtd->_panic_write = panic_nand_write;
4851 mtd->_read_oob = nand_read_oob;
4852 mtd->_write_oob = nand_write_oob;
4853 mtd->_sync = nand_sync;
4854 mtd->_lock = NULL;
4855 mtd->_unlock = NULL;
4856 mtd->_suspend = nand_suspend;
4857 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004858 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004859 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004860 mtd->_block_isbad = nand_block_isbad;
4861 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004862 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004864 /*
4865 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4866 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4867 * properly set.
4868 */
4869 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004870 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004872 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004873 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004877 return chip->scan_bbt(mtd);
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004878err_free:
4879 if (!(chip->options & NAND_OWN_BUFFERS))
4880 kfree(chip->buffers);
4881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004883EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Brian Norris8b6e50c2011-05-25 14:59:01 -07004885/*
4886 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004887 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004888 * to call us from in-kernel code if the core NAND support is modular.
4889 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004890#ifdef MODULE
4891#define caller_is_module() (1)
4892#else
4893#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004894 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004895#endif
4896
4897/**
4898 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004899 * @mtd: MTD device structure
4900 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004901 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004902 * This fills out all the uninitialized function pointers with the defaults.
4903 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03004904 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01004905 */
4906int nand_scan(struct mtd_info *mtd, int maxchips)
4907{
4908 int ret;
4909
David Woodhouse5e81e882010-02-26 18:32:56 +00004910 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004911 if (!ret)
4912 ret = nand_scan_tail(mtd);
4913 return ret;
4914}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004915EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004918 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
4919 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07004920 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004921void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004923 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004924 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01004925 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4926
Boris Brezillond8e725d2016-09-15 10:32:50 +02004927 nand_release_data_interface(chip);
4928
Jesper Juhlfa671642005-11-07 01:01:27 -08004929 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004930 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004931 if (!(chip->options & NAND_OWN_BUFFERS))
4932 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004933
4934 /* Free bad block descriptor memory */
4935 if (chip->badblock_pattern && chip->badblock_pattern->options
4936 & NAND_BBT_DYNAMICSTRUCT)
4937 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02004939EXPORT_SYMBOL_GPL(nand_cleanup);
4940
4941/**
4942 * nand_release - [NAND Interface] Unregister the MTD device and free resources
4943 * held by the NAND device
4944 * @mtd: MTD device structure
4945 */
4946void nand_release(struct mtd_info *mtd)
4947{
4948 mtd_device_unregister(mtd);
4949 nand_cleanup(mtd_to_nand(mtd));
4950}
David Woodhousee0c7d762006-05-13 18:07:53 +01004951EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004952
David Woodhousee0c7d762006-05-13 18:07:53 +01004953MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004954MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4955MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004956MODULE_DESCRIPTION("Generic NAND flash driver code");