blob: 8093df27869286e1dd1ea1bf3aed49781b48712a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Ingo Molnar38b8d202017-02-08 18:51:31 +010039#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/types.h>
41#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020042#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020047#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mtd/partitions.h>
Boris Brezillond48f62b2016-04-01 14:54:32 +020049#include <linux/of.h>
Thomas Gleixner81ec5362007-12-12 17:27:03 +010050
Huang Shijie6a8214a2012-11-19 14:43:30 +080051static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020053static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
Boris Brezillon41b207a2016-02-03 19:06:15 +010056/* Define default oob placement schemes for large and small page devices */
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
68 oobregion->length = 4;
69 } else {
70 oobregion->offset = 6;
71 oobregion->length = ecc->total - 4;
72 }
73
74 return 0;
75}
76
77static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
78 struct mtd_oob_region *oobregion)
79{
80 if (section > 1)
81 return -ERANGE;
82
83 if (mtd->oobsize == 16) {
84 if (section)
85 return -ERANGE;
86
87 oobregion->length = 8;
88 oobregion->offset = 8;
89 } else {
90 oobregion->length = 2;
91 if (!section)
92 oobregion->offset = 3;
93 else
94 oobregion->offset = 6;
95 }
96
97 return 0;
98}
99
100const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
101 .ecc = nand_ooblayout_ecc_sp,
102 .free = nand_ooblayout_free_sp,
103};
104EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
105
106static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
107 struct mtd_oob_region *oobregion)
108{
109 struct nand_chip *chip = mtd_to_nand(mtd);
110 struct nand_ecc_ctrl *ecc = &chip->ecc;
111
112 if (section)
113 return -ERANGE;
114
115 oobregion->length = ecc->total;
116 oobregion->offset = mtd->oobsize - oobregion->length;
117
118 return 0;
119}
120
121static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
122 struct mtd_oob_region *oobregion)
123{
124 struct nand_chip *chip = mtd_to_nand(mtd);
125 struct nand_ecc_ctrl *ecc = &chip->ecc;
126
127 if (section)
128 return -ERANGE;
129
130 oobregion->length = mtd->oobsize - ecc->total - 2;
131 oobregion->offset = 2;
132
133 return 0;
134}
135
136const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
137 .ecc = nand_ooblayout_ecc_lp,
138 .free = nand_ooblayout_free_lp,
139};
140EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200141
Alexander Couzens6a623e02017-05-02 12:19:00 +0200142/*
143 * Support the old "large page" layout used for 1-bit Hamming ECC where ECC
144 * are placed at a fixed offset.
145 */
146static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
147 struct mtd_oob_region *oobregion)
148{
149 struct nand_chip *chip = mtd_to_nand(mtd);
150 struct nand_ecc_ctrl *ecc = &chip->ecc;
151
152 if (section)
153 return -ERANGE;
154
155 switch (mtd->oobsize) {
156 case 64:
157 oobregion->offset = 40;
158 break;
159 case 128:
160 oobregion->offset = 80;
161 break;
162 default:
163 return -EINVAL;
164 }
165
166 oobregion->length = ecc->total;
167 if (oobregion->offset + oobregion->length > mtd->oobsize)
168 return -ERANGE;
169
170 return 0;
171}
172
173static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
174 struct mtd_oob_region *oobregion)
175{
176 struct nand_chip *chip = mtd_to_nand(mtd);
177 struct nand_ecc_ctrl *ecc = &chip->ecc;
178 int ecc_offset = 0;
179
180 if (section < 0 || section > 1)
181 return -ERANGE;
182
183 switch (mtd->oobsize) {
184 case 64:
185 ecc_offset = 40;
186 break;
187 case 128:
188 ecc_offset = 80;
189 break;
190 default:
191 return -EINVAL;
192 }
193
194 if (section == 0) {
195 oobregion->offset = 2;
196 oobregion->length = ecc_offset - 2;
197 } else {
198 oobregion->offset = ecc_offset + ecc->total;
199 oobregion->length = mtd->oobsize - oobregion->offset;
200 }
201
202 return 0;
203}
204
Colin Ian Kingd4ed3b92017-05-04 13:11:00 +0100205static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
Alexander Couzens6a623e02017-05-02 12:19:00 +0200206 .ecc = nand_ooblayout_ecc_lp_hamming,
207 .free = nand_ooblayout_free_lp_hamming,
208};
209
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530210static int check_offs_len(struct mtd_info *mtd,
211 loff_t ofs, uint64_t len)
212{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100213 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530214 int ret = 0;
215
216 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300217 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700218 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530219 ret = -EINVAL;
220 }
221
222 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300223 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700224 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530225 ret = -EINVAL;
226 }
227
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530228 return ret;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
232 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700233 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000234 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800235 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100237static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100239 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200241 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200242 spin_lock(&chip->controller->lock);
243 chip->controller->active = NULL;
244 chip->state = FL_READY;
245 wake_up(&chip->controller->wq);
246 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249/**
250 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700251 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700253 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200255static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100257 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200258 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/**
Masanari Iida064a7692012-11-09 23:20:58 +0900262 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700263 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700265 * Default read function for 16bit buswidth with endianness conversion.
266 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200268static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100270 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200271 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700276 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700278 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280static u16 nand_read_word(struct mtd_info *mtd)
281{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100282 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200283 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700288 * @mtd: MTD device structure
289 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 *
291 * Default select function for 1 chip devices.
292 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200293static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100295 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200296
297 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200299 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303
304 default:
305 BUG();
306 }
307}
308
309/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100310 * nand_write_byte - [DEFAULT] write single byte to chip
311 * @mtd: MTD device structure
312 * @byte: value to write
313 *
314 * Default function to write a byte to I/O[7:0]
315 */
316static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
317{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100318 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100319
320 chip->write_buf(mtd, &byte, 1);
321}
322
323/**
324 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
325 * @mtd: MTD device structure
326 * @byte: value to write
327 *
328 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
329 */
330static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
331{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100332 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100333 uint16_t word = byte;
334
335 /*
336 * It's not entirely clear what should happen to I/O[15:8] when writing
337 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
338 *
339 * When the host supports a 16-bit bus width, only data is
340 * transferred at the 16-bit width. All address and command line
341 * transfers shall use only the lower 8-bits of the data bus. During
342 * command transfers, the host may place any value on the upper
343 * 8-bits of the data bus. During address transfers, the host shall
344 * set the upper 8-bits of the data bus to 00h.
345 *
346 * One user of the write_byte callback is nand_onfi_set_features. The
347 * four parameters are specified to be written to I/O[7:0], but this is
348 * neither an address nor a command transfer. Let's assume a 0 on the
349 * upper I/O lines is OK.
350 */
351 chip->write_buf(mtd, (uint8_t *)&word, 2);
352}
353
354/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700356 * @mtd: MTD device structure
357 * @buf: data buffer
358 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700360 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200362static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100364 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Alexander Shiyan76413832013-04-13 09:32:13 +0400366 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000370 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700371 * @mtd: MTD device structure
372 * @buf: buffer to store date
373 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700375 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200377static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100379 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Alexander Shiyan76413832013-04-13 09:32:13 +0400381 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700386 * @mtd: MTD device structure
387 * @buf: data buffer
388 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700390 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200392static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100394 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000396
Alexander Shiyan76413832013-04-13 09:32:13 +0400397 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000401 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700402 * @mtd: MTD device structure
403 * @buf: buffer to store date
404 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700406 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200408static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100410 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Alexander Shiyan76413832013-04-13 09:32:13 +0400413 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700418 * @mtd: MTD device structure
419 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000421 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530423static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Masahiro Yamadac120e752017-03-23 05:07:01 +0900425 int page, page_end, res;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100426 struct nand_chip *chip = mtd_to_nand(mtd);
Masahiro Yamadac120e752017-03-23 05:07:01 +0900427 u8 bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Brian Norris5fb15492011-05-31 16:31:21 -0700429 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700430 ofs += mtd->erasesize - mtd->writesize;
431
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100432 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900433 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100434
Masahiro Yamadac120e752017-03-23 05:07:01 +0900435 for (; page < page_end; page++) {
436 res = chip->ecc.read_oob(mtd, chip, page);
437 if (res)
438 return res;
439
440 bad = chip->oob_poi[chip->badblockpos];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000441
Brian Norriscdbec052012-01-13 18:11:48 -0800442 if (likely(chip->badblockbits == 8))
443 res = bad != 0xFF;
444 else
445 res = hweight8(bad) < chip->badblockbits;
Masahiro Yamadac120e752017-03-23 05:07:01 +0900446 if (res)
447 return res;
448 }
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200449
Masahiro Yamadac120e752017-03-23 05:07:01 +0900450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700454 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700455 * @mtd: MTD device structure
456 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700458 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700459 * specific driver. It provides the details for writing a bad block marker to a
460 * block.
461 */
462static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
463{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100464 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700465 struct mtd_oob_ops ops;
466 uint8_t buf[2] = { 0, 0 };
467 int ret = 0, res, i = 0;
468
Brian Norris0ec56dc2015-02-28 02:02:30 -0800469 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700470 ops.oobbuf = buf;
471 ops.ooboffs = chip->badblockpos;
472 if (chip->options & NAND_BUSWIDTH_16) {
473 ops.ooboffs &= ~0x01;
474 ops.len = ops.ooblen = 2;
475 } else {
476 ops.len = ops.ooblen = 1;
477 }
478 ops.mode = MTD_OPS_PLACE_OOB;
479
480 /* Write to first/last page(s) if necessary */
481 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
482 ofs += mtd->erasesize - mtd->writesize;
483 do {
484 res = nand_do_write_oob(mtd, ofs, &ops);
485 if (!ret)
486 ret = res;
487
488 i++;
489 ofs += mtd->writesize;
490 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
491
492 return ret;
493}
494
495/**
496 * nand_block_markbad_lowlevel - mark a block bad
497 * @mtd: MTD device structure
498 * @ofs: offset from device start
499 *
500 * This function performs the generic NAND bad block marking steps (i.e., bad
501 * block table(s) and/or marker(s)). We only allow the hardware driver to
502 * specify how to write bad block markers to OOB (chip->block_markbad).
503 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700504 * We try operations in the following order:
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300505 *
Brian Norrise2414f42012-02-06 13:44:00 -0800506 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700507 * (2) write bad block marker to OOB area of affected block (unless flag
508 * NAND_BBT_NO_OOB_BBM is present)
509 * (3) update the BBT
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -0300510 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700511 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800512 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700514static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100516 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700517 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000518
Brian Norrisb32843b2013-07-30 17:52:59 -0700519 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800520 struct erase_info einfo;
521
522 /* Attempt erase before marking OOB */
523 memset(&einfo, 0, sizeof(einfo));
524 einfo.mtd = mtd;
525 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300526 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800527 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800528
Brian Norrisb32843b2013-07-30 17:52:59 -0700529 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800530 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700531 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300532 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200533 }
Brian Norrise2414f42012-02-06 13:44:00 -0800534
Brian Norrisb32843b2013-07-30 17:52:59 -0700535 /* Mark block bad in BBT */
536 if (chip->bbt) {
537 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800538 if (!ret)
539 ret = res;
540 }
541
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200542 if (!ret)
543 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300544
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000548/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700550 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700552 * Check, if the device is write protected. The function expects, that the
553 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100555static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100557 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200558
Brian Norris8b6e50c2011-05-25 14:59:01 -0700559 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200560 if (chip->options & NAND_BROKEN_XD)
561 return 0;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200564 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
565 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800569 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700570 * @mtd: MTD device structure
571 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300572 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800573 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300574 */
575static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
576{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100577 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300578
579 if (!chip->bbt)
580 return 0;
581 /* Return info from the table */
582 return nand_isreserved_bbt(mtd, ofs);
583}
584
585/**
586 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
587 * @mtd: MTD device structure
588 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700589 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *
591 * Check, if the block is bad. Either by reading the bad block table or
592 * calling of the scan function.
593 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530594static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100596 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000597
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200598 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530599 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100602 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200605/**
606 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700607 * @mtd: MTD device structure
608 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200609 *
610 * Helper function for nand_wait_ready used when needing to wait in interrupt
611 * context.
612 */
613static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
614{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100615 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200616 int i;
617
618 /* Wait for the device to get ready */
619 for (i = 0; i < timeo; i++) {
620 if (chip->dev_ready(mtd))
621 break;
622 touch_softlockup_watchdog();
623 mdelay(1);
624 }
625}
626
Alex Smithb70af9b2015-10-06 14:52:07 +0100627/**
628 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
629 * @mtd: MTD device structure
630 *
631 * Wait for the ready pin after a command, and warn if a timeout occurs.
632 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100633void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000634{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100635 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100636 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000637
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200638 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100639 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200640
Brian Norris7854d3f2011-06-23 14:12:08 -0700641 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100642 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000643 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300645 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100646 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000647 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100648
Brian Norris9ebfdf52016-03-04 17:19:23 -0800649 if (!chip->dev_ready(mtd))
650 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000651}
David Woodhouse4b648b02006-09-25 17:05:24 +0100652EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200655 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
656 * @mtd: MTD device structure
657 * @timeo: Timeout in ms
658 *
659 * Wait for status ready (i.e. command done) or timeout.
660 */
661static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
662{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100663 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200664
665 timeo = jiffies + msecs_to_jiffies(timeo);
666 do {
667 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
668 break;
669 touch_softlockup_watchdog();
670 } while (time_before(jiffies, timeo));
671};
672
673/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700675 * @mtd: MTD device structure
676 * @command: the command to be sent
677 * @column: the column address for this command, -1 if none
678 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700680 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200681 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200683static void nand_command(struct mtd_info *mtd, unsigned int command,
684 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100686 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200687 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Brian Norris8b6e50c2011-05-25 14:59:01 -0700689 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (command == NAND_CMD_SEQIN) {
691 int readcmd;
692
Joern Engel28318772006-05-22 23:18:05 +0200693 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200695 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 readcmd = NAND_CMD_READOOB;
697 } else if (column < 256) {
698 /* First 256 bytes --> READ0 */
699 readcmd = NAND_CMD_READ0;
700 } else {
701 column -= 256;
702 readcmd = NAND_CMD_READ1;
703 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200704 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200707 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Brian Norris8b6e50c2011-05-25 14:59:01 -0700709 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200710 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
711 /* Serially input address */
712 if (column != -1) {
713 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800714 if (chip->options & NAND_BUSWIDTH_16 &&
715 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200717 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200718 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200720 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200722 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725 if (chip->chipsize > (32 << 20))
726 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200727 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000729
730 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700731 * Program and erase have their own busy handlers status and sequential
732 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case NAND_CMD_PAGEPROG:
737 case NAND_CMD_ERASE1:
738 case NAND_CMD_ERASE2:
739 case NAND_CMD_SEQIN:
740 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900741 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900742 case NAND_CMD_SET_FEATURES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return;
744
745 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 udelay(chip->chip_delay);
749 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200750 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200751 chip->cmd_ctrl(mtd,
752 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200753 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
754 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return;
756
David Woodhousee0c7d762006-05-13 18:07:53 +0100757 /* This applies to read commands */
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200758 case NAND_CMD_READ0:
759 /*
760 * READ0 is sometimes used to exit GET STATUS mode. When this
761 * is the case no address cycles are requested, and we can use
762 * this information to detect that we should not wait for the
763 * device to be ready.
764 */
765 if (column == -1 && page_addr == -1)
766 return;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000769 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * If we don't have access to the busy pin, we apply the given
771 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100772 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200773 if (!chip->dev_ready) {
774 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700778 /*
779 * Apply this short delay always to ensure that we do wait tWB in
780 * any case on any machine.
781 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100782 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000783
784 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200787static void nand_ccs_delay(struct nand_chip *chip)
788{
789 /*
790 * The controller already takes care of waiting for tCCS when the RNDIN
791 * or RNDOUT command is sent, return directly.
792 */
793 if (!(chip->options & NAND_WAIT_TCCS))
794 return;
795
796 /*
797 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
798 * (which should be safe for all NANDs).
799 */
800 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
801 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
802 else
803 ndelay(500);
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/**
807 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700808 * @mtd: MTD device structure
809 * @command: the command to be sent
810 * @column: the column address for this command, -1 if none
811 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200813 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700814 * devices. We don't have the separate regions as we have in the small page
815 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200817static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
818 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100820 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Emulate NAND_CMD_READOOB */
823 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200824 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 command = NAND_CMD_READ0;
826 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000827
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200828 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400829 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200832 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* Serially input address */
835 if (column != -1) {
836 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800837 if (chip->options & NAND_BUSWIDTH_16 &&
838 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200840 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200841 ctrl &= ~NAND_CTRL_CHANGE;
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200842
Brian Norrisf5b88de2016-10-03 09:49:35 -0700843 /* Only output a single addr cycle for 8bits opcodes. */
Boris Brezillonfde85cf2016-06-15 13:09:51 +0200844 if (!nand_opcode_8bits(command))
845 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200848 chip->cmd_ctrl(mtd, page_addr, ctrl);
849 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200850 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200852 if (chip->chipsize > (128 << 20))
853 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200854 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200857 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000858
859 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100861 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 case NAND_CMD_CACHEDPROG:
866 case NAND_CMD_PAGEPROG:
867 case NAND_CMD_ERASE1:
868 case NAND_CMD_ERASE2:
869 case NAND_CMD_SEQIN:
870 case NAND_CMD_STATUS:
Masahiro Yamada3158fa02017-03-23 09:17:49 +0900871 case NAND_CMD_READID:
Masahiro Yamadac5d664a2017-03-23 09:17:50 +0900872 case NAND_CMD_SET_FEATURES:
David A. Marlin30f464b2005-01-17 18:35:25 +0000873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200875 case NAND_CMD_RNDIN:
876 nand_ccs_delay(chip);
877 return;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200880 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200882 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200883 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
884 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
885 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
886 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200887 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
888 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return;
890
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200891 case NAND_CMD_RNDOUT:
892 /* No ready / busy check necessary */
893 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
894 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
895 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
896 NAND_NCE | NAND_CTRL_CHANGE);
Boris Brezillon6ea40a32016-10-01 10:24:03 +0200897
898 nand_ccs_delay(chip);
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200899 return;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 case NAND_CMD_READ0:
Boris Brezillon2165c4a2017-05-16 18:35:45 +0200902 /*
903 * READ0 is sometimes used to exit GET STATUS mode. When this
904 * is the case no address cycles are requested, and we can use
905 * this information to detect that READSTART should not be
906 * issued.
907 */
908 if (column == -1 && page_addr == -1)
909 return;
910
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200911 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
912 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
913 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
914 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000915
David Woodhousee0c7d762006-05-13 18:07:53 +0100916 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000918 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100921 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200922 if (!chip->dev_ready) {
923 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000927
Brian Norris8b6e50c2011-05-25 14:59:01 -0700928 /*
929 * Apply this short delay always to ensure that we do wait tWB in
930 * any case on any machine.
931 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100932 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000933
934 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200938 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700939 * @chip: the nand chip descriptor
940 * @mtd: MTD device structure
941 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200942 *
943 * Used when in panic, no locks are taken.
944 */
945static void panic_nand_get_device(struct nand_chip *chip,
946 struct mtd_info *mtd, int new_state)
947{
Brian Norris7854d3f2011-06-23 14:12:08 -0700948 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200949 chip->controller->active = chip;
950 chip->state = new_state;
951}
952
953/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700955 * @mtd: MTD device structure
956 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
958 * Get the device and lock it for exclusive access
959 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200960static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800961nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100963 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200964 spinlock_t *lock = &chip->controller->lock;
965 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100966 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200967retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100968 spin_lock(lock);
969
vimal singhb8b3ee92009-07-09 20:41:22 +0530970 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200971 if (!chip->controller->active)
972 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200973
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200974 if (chip->controller->active == chip && chip->state == FL_READY) {
975 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100976 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100977 return 0;
978 }
979 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800980 if (chip->controller->active->state == FL_PM_SUSPENDED) {
981 chip->state = FL_PM_SUSPENDED;
982 spin_unlock(lock);
983 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800984 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100985 }
986 set_current_state(TASK_UNINTERRUPTIBLE);
987 add_wait_queue(wq, &wait);
988 spin_unlock(lock);
989 schedule();
990 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto retry;
992}
993
994/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700995 * panic_nand_wait - [GENERIC] wait until the command is done
996 * @mtd: MTD device structure
997 * @chip: NAND chip structure
998 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200999 *
1000 * Wait for command done. This is a helper function for nand_wait used when
1001 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001002 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001003 */
1004static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1005 unsigned long timeo)
1006{
1007 int i;
1008 for (i = 0; i < timeo; i++) {
1009 if (chip->dev_ready) {
1010 if (chip->dev_ready(mtd))
1011 break;
1012 } else {
1013 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1014 break;
1015 }
1016 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +02001017 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001018}
1019
1020/**
Brian Norris8b6e50c2011-05-25 14:59:01 -07001021 * nand_wait - [DEFAULT] wait until the command is done
1022 * @mtd: MTD device structure
1023 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 *
Alex Smithb70af9b2015-10-06 14:52:07 +01001025 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -07001026 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001027static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029
Alex Smithb70af9b2015-10-06 14:52:07 +01001030 int status;
1031 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Brian Norris8b6e50c2011-05-25 14:59:01 -07001033 /*
1034 * Apply this short delay always to ensure that we do wait tWB in any
1035 * case on any machine.
1036 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001037 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Artem Bityutskiy14c65782013-03-04 14:21:34 +02001039 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001041 if (in_interrupt() || oops_in_progress)
1042 panic_nand_wait(mtd, chip, timeo);
1043 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +08001044 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +01001045 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +02001046 if (chip->dev_ready) {
1047 if (chip->dev_ready(mtd))
1048 break;
1049 } else {
1050 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1051 break;
1052 }
1053 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +01001054 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Richard Purdie8fe833c2006-03-31 02:31:14 -08001056
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001057 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +01001058 /* This can happen if in case of timeout or buggy dev_ready */
1059 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return status;
1061}
1062
1063/**
Boris Brezillond8e725d2016-09-15 10:32:50 +02001064 * nand_reset_data_interface - Reset data interface and timings
1065 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001066 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001067 *
1068 * Reset the Data interface and timings to ONFI mode 0.
1069 *
1070 * Returns 0 for success or negative error code otherwise.
1071 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001072static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001073{
1074 struct mtd_info *mtd = nand_to_mtd(chip);
1075 const struct nand_data_interface *conf;
1076 int ret;
1077
1078 if (!chip->setup_data_interface)
1079 return 0;
1080
1081 /*
1082 * The ONFI specification says:
1083 * "
1084 * To transition from NV-DDR or NV-DDR2 to the SDR data
1085 * interface, the host shall use the Reset (FFh) command
1086 * using SDR timing mode 0. A device in any timing mode is
1087 * required to recognize Reset (FFh) command issued in SDR
1088 * timing mode 0.
1089 * "
1090 *
1091 * Configure the data interface in SDR mode and set the
1092 * timings to timing mode 0.
1093 */
1094
1095 conf = nand_get_default_data_interface();
Boris Brezillon104e4422017-03-16 09:35:58 +01001096 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001097 if (ret)
1098 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1099
1100 return ret;
1101}
1102
1103/**
1104 * nand_setup_data_interface - Setup the best data interface and timings
1105 * @chip: The NAND chip
Boris Brezillon104e4422017-03-16 09:35:58 +01001106 * @chipnr: Internal die id
Boris Brezillond8e725d2016-09-15 10:32:50 +02001107 *
1108 * Find and configure the best data interface and NAND timings supported by
1109 * the chip and the driver.
1110 * First tries to retrieve supported timing modes from ONFI information,
1111 * and if the NAND chip does not support ONFI, relies on the
1112 * ->onfi_timing_mode_default specified in the nand_ids table.
1113 *
1114 * Returns 0 for success or negative error code otherwise.
1115 */
Boris Brezillon104e4422017-03-16 09:35:58 +01001116static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillond8e725d2016-09-15 10:32:50 +02001117{
1118 struct mtd_info *mtd = nand_to_mtd(chip);
1119 int ret;
1120
1121 if (!chip->setup_data_interface || !chip->data_interface)
1122 return 0;
1123
1124 /*
1125 * Ensure the timing mode has been changed on the chip side
1126 * before changing timings on the controller side.
1127 */
1128 if (chip->onfi_version) {
1129 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1130 chip->onfi_timing_mode_default,
1131 };
1132
1133 ret = chip->onfi_set_features(mtd, chip,
1134 ONFI_FEATURE_ADDR_TIMING_MODE,
1135 tmode_param);
1136 if (ret)
1137 goto err;
1138 }
1139
Boris Brezillon104e4422017-03-16 09:35:58 +01001140 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001141err:
1142 return ret;
1143}
1144
1145/**
1146 * nand_init_data_interface - find the best data interface and timings
1147 * @chip: The NAND chip
1148 *
1149 * Find the best data interface and NAND timings supported by the chip
1150 * and the driver.
1151 * First tries to retrieve supported timing modes from ONFI information,
1152 * and if the NAND chip does not support ONFI, relies on the
1153 * ->onfi_timing_mode_default specified in the nand_ids table. After this
1154 * function nand_chip->data_interface is initialized with the best timing mode
1155 * available.
1156 *
1157 * Returns 0 for success or negative error code otherwise.
1158 */
1159static int nand_init_data_interface(struct nand_chip *chip)
1160{
1161 struct mtd_info *mtd = nand_to_mtd(chip);
1162 int modes, mode, ret;
1163
1164 if (!chip->setup_data_interface)
1165 return 0;
1166
1167 /*
1168 * First try to identify the best timings from ONFI parameters and
1169 * if the NAND does not support ONFI, fallback to the default ONFI
1170 * timing mode.
1171 */
1172 modes = onfi_get_async_timing_mode(chip);
1173 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1174 if (!chip->onfi_timing_mode_default)
1175 return 0;
1176
1177 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1178 }
1179
1180 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1181 GFP_KERNEL);
1182 if (!chip->data_interface)
1183 return -ENOMEM;
1184
1185 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1186 ret = onfi_init_data_interface(chip, chip->data_interface,
1187 NAND_SDR_IFACE, mode);
1188 if (ret)
1189 continue;
1190
Boris Brezillon104e4422017-03-16 09:35:58 +01001191 /* Pass -1 to only */
1192 ret = chip->setup_data_interface(mtd,
1193 NAND_DATA_IFACE_CHECK_ONLY,
1194 chip->data_interface);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001195 if (!ret) {
1196 chip->onfi_timing_mode_default = mode;
1197 break;
1198 }
1199 }
1200
1201 return 0;
1202}
1203
1204static void nand_release_data_interface(struct nand_chip *chip)
1205{
1206 kfree(chip->data_interface);
1207}
1208
1209/**
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001210 * nand_reset - Reset and initialize a NAND device
1211 * @chip: The NAND chip
Boris Brezillon73f907f2016-10-24 16:46:20 +02001212 * @chipnr: Internal die id
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001213 *
1214 * Returns 0 for success or negative error code otherwise
1215 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001216int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001217{
1218 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001219 int ret;
1220
Boris Brezillon104e4422017-03-16 09:35:58 +01001221 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001222 if (ret)
1223 return ret;
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001224
Boris Brezillon73f907f2016-10-24 16:46:20 +02001225 /*
1226 * The CS line has to be released before we can apply the new NAND
1227 * interface settings, hence this weird ->select_chip() dance.
1228 */
1229 chip->select_chip(mtd, chipnr);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001230 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001231 chip->select_chip(mtd, -1);
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001232
Boris Brezillon73f907f2016-10-24 16:46:20 +02001233 chip->select_chip(mtd, chipnr);
Boris Brezillon104e4422017-03-16 09:35:58 +01001234 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon73f907f2016-10-24 16:46:20 +02001235 chip->select_chip(mtd, -1);
Boris Brezillond8e725d2016-09-15 10:32:50 +02001236 if (ret)
1237 return ret;
1238
Sascha Hauer2f94abf2016-09-15 10:32:45 +02001239 return 0;
1240}
1241
1242/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001243 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001244 * @mtd: mtd info
1245 * @ofs: offset to start unlock from
1246 * @len: length to unlock
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -03001247 * @invert:
1248 * - when = 0, unlock the range of blocks within the lower and
Brian Norris8b6e50c2011-05-25 14:59:01 -07001249 * upper boundary address
Mauro Carvalho Chehabb6f6c292017-05-13 07:40:36 -03001250 * - when = 1, unlock the range of blocks outside the boundaries
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +05301252 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001253 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301254 */
1255static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1256 uint64_t len, int invert)
1257{
1258 int ret = 0;
1259 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001260 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301261
1262 /* Submit address of first page to unlock */
1263 page = ofs >> chip->page_shift;
1264 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1265
1266 /* Submit address of last page to unlock */
1267 page = (ofs + len) >> chip->page_shift;
1268 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1269 (page | invert) & chip->pagemask);
1270
1271 /* Call wait ready function */
1272 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301273 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001274 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001275 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301276 __func__, status);
1277 ret = -EIO;
1278 }
1279
1280 return ret;
1281}
1282
1283/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001284 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001285 * @mtd: mtd info
1286 * @ofs: offset to start unlock from
1287 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301288 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001289 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301290 */
1291int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1292{
1293 int ret = 0;
1294 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001295 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301296
Brian Norris289c0522011-07-19 10:06:09 -07001297 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301298 __func__, (unsigned long long)ofs, len);
1299
1300 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001301 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301302
1303 /* Align to last block address if size addresses end of the device */
1304 if (ofs + len == mtd->size)
1305 len -= mtd->erasesize;
1306
Huang Shijie6a8214a2012-11-19 14:43:30 +08001307 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301308
1309 /* Shift to get chip number */
1310 chipnr = ofs >> chip->chip_shift;
1311
White Ding57d3a9a2014-07-24 00:10:45 +08001312 /*
1313 * Reset the chip.
1314 * If we want to check the WP through READ STATUS and check the bit 7
1315 * we must reset the chip
1316 * some operation can also clear the bit 7 of status register
1317 * eg. erase/program a locked block
1318 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001319 nand_reset(chip, chipnr);
1320
1321 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001322
Vimal Singh7d70f332010-02-08 15:50:49 +05301323 /* Check, if it is write protected */
1324 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001325 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301326 __func__);
1327 ret = -EIO;
1328 goto out;
1329 }
1330
1331 ret = __nand_unlock(mtd, ofs, len, 0);
1332
1333out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001334 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301335 nand_release_device(mtd);
1336
1337 return ret;
1338}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001339EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301340
1341/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001342 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001343 * @mtd: mtd info
1344 * @ofs: offset to start unlock from
1345 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301346 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001347 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1348 * have this feature, but it allows only to lock all blocks, not for specified
1349 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1350 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301351 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001352 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301353 */
1354int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1355{
1356 int ret = 0;
1357 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001358 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301359
Brian Norris289c0522011-07-19 10:06:09 -07001360 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301361 __func__, (unsigned long long)ofs, len);
1362
1363 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001364 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301365
Huang Shijie6a8214a2012-11-19 14:43:30 +08001366 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301367
1368 /* Shift to get chip number */
1369 chipnr = ofs >> chip->chip_shift;
1370
White Ding57d3a9a2014-07-24 00:10:45 +08001371 /*
1372 * Reset the chip.
1373 * If we want to check the WP through READ STATUS and check the bit 7
1374 * we must reset the chip
1375 * some operation can also clear the bit 7 of status register
1376 * eg. erase/program a locked block
1377 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02001378 nand_reset(chip, chipnr);
1379
1380 chip->select_chip(mtd, chipnr);
White Ding57d3a9a2014-07-24 00:10:45 +08001381
Vimal Singh7d70f332010-02-08 15:50:49 +05301382 /* Check, if it is write protected */
1383 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001384 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301385 __func__);
1386 status = MTD_ERASE_FAILED;
1387 ret = -EIO;
1388 goto out;
1389 }
1390
1391 /* Submit address of first page to lock */
1392 page = ofs >> chip->page_shift;
1393 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1394
1395 /* Call wait ready function */
1396 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301397 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001398 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001399 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301400 __func__, status);
1401 ret = -EIO;
1402 goto out;
1403 }
1404
1405 ret = __nand_unlock(mtd, ofs, len, 0x1);
1406
1407out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001408 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301409 nand_release_device(mtd);
1410
1411 return ret;
1412}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001413EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301414
1415/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001416 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1417 * @buf: buffer to test
1418 * @len: buffer length
1419 * @bitflips_threshold: maximum number of bitflips
1420 *
1421 * Check if a buffer contains only 0xff, which means the underlying region
1422 * has been erased and is ready to be programmed.
1423 * The bitflips_threshold specify the maximum number of bitflips before
1424 * considering the region is not erased.
1425 * Note: The logic of this function has been extracted from the memweight
1426 * implementation, except that nand_check_erased_buf function exit before
1427 * testing the whole buffer if the number of bitflips exceed the
1428 * bitflips_threshold value.
1429 *
1430 * Returns a positive number of bitflips less than or equal to
1431 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1432 * threshold.
1433 */
1434static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1435{
1436 const unsigned char *bitmap = buf;
1437 int bitflips = 0;
1438 int weight;
1439
1440 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1441 len--, bitmap++) {
1442 weight = hweight8(*bitmap);
1443 bitflips += BITS_PER_BYTE - weight;
1444 if (unlikely(bitflips > bitflips_threshold))
1445 return -EBADMSG;
1446 }
1447
1448 for (; len >= sizeof(long);
1449 len -= sizeof(long), bitmap += sizeof(long)) {
Pavel Machek086567f2017-04-21 12:51:07 +02001450 unsigned long d = *((unsigned long *)bitmap);
1451 if (d == ~0UL)
1452 continue;
1453 weight = hweight_long(d);
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001454 bitflips += BITS_PER_LONG - weight;
1455 if (unlikely(bitflips > bitflips_threshold))
1456 return -EBADMSG;
1457 }
1458
1459 for (; len > 0; len--, bitmap++) {
1460 weight = hweight8(*bitmap);
1461 bitflips += BITS_PER_BYTE - weight;
1462 if (unlikely(bitflips > bitflips_threshold))
1463 return -EBADMSG;
1464 }
1465
1466 return bitflips;
1467}
1468
1469/**
1470 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1471 * 0xff data
1472 * @data: data buffer to test
1473 * @datalen: data length
1474 * @ecc: ECC buffer
1475 * @ecclen: ECC length
1476 * @extraoob: extra OOB buffer
1477 * @extraooblen: extra OOB length
1478 * @bitflips_threshold: maximum number of bitflips
1479 *
1480 * Check if a data buffer and its associated ECC and OOB data contains only
1481 * 0xff pattern, which means the underlying region has been erased and is
1482 * ready to be programmed.
1483 * The bitflips_threshold specify the maximum number of bitflips before
1484 * considering the region as not erased.
1485 *
1486 * Note:
1487 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1488 * different from the NAND page size. When fixing bitflips, ECC engines will
1489 * report the number of errors per chunk, and the NAND core infrastructure
1490 * expect you to return the maximum number of bitflips for the whole page.
1491 * This is why you should always use this function on a single chunk and
1492 * not on the whole page. After checking each chunk you should update your
1493 * max_bitflips value accordingly.
1494 * 2/ When checking for bitflips in erased pages you should not only check
1495 * the payload data but also their associated ECC data, because a user might
1496 * have programmed almost all bits to 1 but a few. In this case, we
1497 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1498 * this case.
1499 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1500 * data are protected by the ECC engine.
1501 * It could also be used if you support subpages and want to attach some
1502 * extra OOB data to an ECC chunk.
1503 *
1504 * Returns a positive number of bitflips less than or equal to
1505 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1506 * threshold. In case of success, the passed buffers are filled with 0xff.
1507 */
1508int nand_check_erased_ecc_chunk(void *data, int datalen,
1509 void *ecc, int ecclen,
1510 void *extraoob, int extraooblen,
1511 int bitflips_threshold)
1512{
1513 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1514
1515 data_bitflips = nand_check_erased_buf(data, datalen,
1516 bitflips_threshold);
1517 if (data_bitflips < 0)
1518 return data_bitflips;
1519
1520 bitflips_threshold -= data_bitflips;
1521
1522 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1523 if (ecc_bitflips < 0)
1524 return ecc_bitflips;
1525
1526 bitflips_threshold -= ecc_bitflips;
1527
1528 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1529 bitflips_threshold);
1530 if (extraoob_bitflips < 0)
1531 return extraoob_bitflips;
1532
1533 if (data_bitflips)
1534 memset(data, 0xff, datalen);
1535
1536 if (ecc_bitflips)
1537 memset(ecc, 0xff, ecclen);
1538
1539 if (extraoob_bitflips)
1540 memset(extraoob, 0xff, extraooblen);
1541
1542 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1543}
1544EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1545
1546/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001547 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001548 * @mtd: mtd info structure
1549 * @chip: nand chip info structure
1550 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001551 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001552 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001553 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001554 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001555 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001556int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1557 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001558{
1559 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001560 if (oob_required)
1561 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001562 return 0;
1563}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02001564EXPORT_SYMBOL(nand_read_page_raw);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001565
1566/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001567 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001568 * @mtd: mtd info structure
1569 * @chip: nand chip info structure
1570 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001571 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001572 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001573 *
1574 * We need a special oob layout and handling even when OOB isn't used.
1575 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001576static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001577 struct nand_chip *chip, uint8_t *buf,
1578 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001579{
1580 int eccsize = chip->ecc.size;
1581 int eccbytes = chip->ecc.bytes;
1582 uint8_t *oob = chip->oob_poi;
1583 int steps, size;
1584
1585 for (steps = chip->ecc.steps; steps > 0; steps--) {
1586 chip->read_buf(mtd, buf, eccsize);
1587 buf += eccsize;
1588
1589 if (chip->ecc.prepad) {
1590 chip->read_buf(mtd, oob, chip->ecc.prepad);
1591 oob += chip->ecc.prepad;
1592 }
1593
1594 chip->read_buf(mtd, oob, eccbytes);
1595 oob += eccbytes;
1596
1597 if (chip->ecc.postpad) {
1598 chip->read_buf(mtd, oob, chip->ecc.postpad);
1599 oob += chip->ecc.postpad;
1600 }
1601 }
1602
1603 size = mtd->oobsize - (oob - chip->oob_poi);
1604 if (size)
1605 chip->read_buf(mtd, oob, size);
1606
1607 return 0;
1608}
1609
1610/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001611 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001612 * @mtd: mtd info structure
1613 * @chip: nand chip info structure
1614 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001615 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001616 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001617 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001618static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001619 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Boris Brezillon846031d2016-02-03 20:11:00 +01001621 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001622 int eccbytes = chip->ecc.bytes;
1623 int eccsteps = chip->ecc.steps;
1624 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001625 uint8_t *ecc_calc = chip->buffers->ecccalc;
1626 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001627 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001628
Brian Norris1fbb9382012-05-02 10:14:55 -07001629 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001630
1631 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1632 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1633
Boris Brezillon846031d2016-02-03 20:11:00 +01001634 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1635 chip->ecc.total);
1636 if (ret)
1637 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001638
1639 eccsteps = chip->ecc.steps;
1640 p = buf;
1641
1642 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1643 int stat;
1644
1645 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001646 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001647 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001648 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001649 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001650 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1651 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001652 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001653 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301657 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001658 * @mtd: mtd info structure
1659 * @chip: nand chip info structure
1660 * @data_offs: offset of requested data within the page
1661 * @readlen: data length
1662 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001663 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001664 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001665static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001666 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1667 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001668{
Boris Brezillon846031d2016-02-03 20:11:00 +01001669 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001670 uint8_t *p;
1671 int data_col_addr, i, gaps = 0;
1672 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1673 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001674 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001675 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001676 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001677
Brian Norris7854d3f2011-06-23 14:12:08 -07001678 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001679 start_step = data_offs / chip->ecc.size;
1680 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1681 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301682 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001683
Brian Norris8b6e50c2011-05-25 14:59:01 -07001684 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001685 datafrag_len = num_steps * chip->ecc.size;
1686 eccfrag_len = num_steps * chip->ecc.bytes;
1687
1688 data_col_addr = start_step * chip->ecc.size;
1689 /* If we read not a page aligned data */
1690 if (data_col_addr != 0)
1691 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1692
1693 p = bufpoi + data_col_addr;
1694 chip->read_buf(mtd, p, datafrag_len);
1695
Brian Norris8b6e50c2011-05-25 14:59:01 -07001696 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001697 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1698 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1699
Brian Norris8b6e50c2011-05-25 14:59:01 -07001700 /*
1701 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001702 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001703 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001704 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1705 if (ret)
1706 return ret;
1707
1708 if (oobregion.length < eccfrag_len)
1709 gaps = 1;
1710
Alexey Korolev3d459552008-05-15 17:23:18 +01001711 if (gaps) {
1712 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1713 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1714 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001715 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001716 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001717 * about buswidth alignment in read_buf.
1718 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001719 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001720 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001721 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001722 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001723 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1724 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001725 aligned_len++;
1726
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001727 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001728 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001729 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1730 }
1731
Boris Brezillon846031d2016-02-03 20:11:00 +01001732 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1733 chip->oob_poi, index, eccfrag_len);
1734 if (ret)
1735 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001736
1737 p = bufpoi + data_col_addr;
1738 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1739 int stat;
1740
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001741 stat = chip->ecc.correct(mtd, p,
1742 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001743 if (stat == -EBADMSG &&
1744 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1745 /* check for empty pages with bitflips */
1746 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1747 &chip->buffers->ecccode[i],
1748 chip->ecc.bytes,
1749 NULL, 0,
1750 chip->ecc.strength);
1751 }
1752
Mike Dunn3f91e942012-04-25 12:06:09 -07001753 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001754 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001755 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001756 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001757 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1758 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001759 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001760 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001761}
1762
1763/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001764 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001765 * @mtd: mtd info structure
1766 * @chip: nand chip info structure
1767 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001768 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001769 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001770 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001771 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001772 */
1773static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001774 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001775{
Boris Brezillon846031d2016-02-03 20:11:00 +01001776 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001777 int eccbytes = chip->ecc.bytes;
1778 int eccsteps = chip->ecc.steps;
1779 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001780 uint8_t *ecc_calc = chip->buffers->ecccalc;
1781 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001782 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001783
1784 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1785 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1786 chip->read_buf(mtd, p, eccsize);
1787 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1788 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001789 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001790
Boris Brezillon846031d2016-02-03 20:11:00 +01001791 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1792 chip->ecc.total);
1793 if (ret)
1794 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001795
1796 eccsteps = chip->ecc.steps;
1797 p = buf;
1798
1799 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1800 int stat;
1801
1802 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001803 if (stat == -EBADMSG &&
1804 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1805 /* check for empty pages with bitflips */
1806 stat = nand_check_erased_ecc_chunk(p, eccsize,
1807 &ecc_code[i], eccbytes,
1808 NULL, 0,
1809 chip->ecc.strength);
1810 }
1811
Mike Dunn3f91e942012-04-25 12:06:09 -07001812 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001813 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001814 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001815 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001816 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1817 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001818 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001819 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001820}
1821
1822/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001823 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001824 * @mtd: mtd info structure
1825 * @chip: nand chip info structure
1826 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001827 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001828 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001829 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001830 * Hardware ECC for large page chips, require OOB to be read first. For this
1831 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1832 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1833 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1834 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001835 */
1836static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001837 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001838{
Boris Brezillon846031d2016-02-03 20:11:00 +01001839 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001840 int eccbytes = chip->ecc.bytes;
1841 int eccsteps = chip->ecc.steps;
1842 uint8_t *p = buf;
1843 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001844 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001845 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001846
1847 /* Read the OOB area first */
1848 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1849 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1850 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1851
Boris Brezillon846031d2016-02-03 20:11:00 +01001852 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1853 chip->ecc.total);
1854 if (ret)
1855 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001856
1857 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1858 int stat;
1859
1860 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1861 chip->read_buf(mtd, p, eccsize);
1862 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1863
1864 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001865 if (stat == -EBADMSG &&
1866 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1867 /* check for empty pages with bitflips */
1868 stat = nand_check_erased_ecc_chunk(p, eccsize,
1869 &ecc_code[i], eccbytes,
1870 NULL, 0,
1871 chip->ecc.strength);
1872 }
1873
Mike Dunn3f91e942012-04-25 12:06:09 -07001874 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001875 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001876 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001877 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001878 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1879 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001880 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001881 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001882}
1883
1884/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001885 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001886 * @mtd: mtd info structure
1887 * @chip: nand chip info structure
1888 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001889 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001890 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001891 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001892 * The hw generator calculates the error syndrome automatically. Therefore we
1893 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001894 */
1895static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001896 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001897{
1898 int i, eccsize = chip->ecc.size;
1899 int eccbytes = chip->ecc.bytes;
1900 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001901 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001902 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001903 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001904 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001905
1906 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1907 int stat;
1908
1909 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1910 chip->read_buf(mtd, p, eccsize);
1911
1912 if (chip->ecc.prepad) {
1913 chip->read_buf(mtd, oob, chip->ecc.prepad);
1914 oob += chip->ecc.prepad;
1915 }
1916
1917 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1918 chip->read_buf(mtd, oob, eccbytes);
1919 stat = chip->ecc.correct(mtd, p, oob, NULL);
1920
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001921 oob += eccbytes;
1922
1923 if (chip->ecc.postpad) {
1924 chip->read_buf(mtd, oob, chip->ecc.postpad);
1925 oob += chip->ecc.postpad;
1926 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001927
1928 if (stat == -EBADMSG &&
1929 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1930 /* check for empty pages with bitflips */
1931 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1932 oob - eccpadbytes,
1933 eccpadbytes,
1934 NULL, 0,
1935 chip->ecc.strength);
1936 }
1937
1938 if (stat < 0) {
1939 mtd->ecc_stats.failed++;
1940 } else {
1941 mtd->ecc_stats.corrected += stat;
1942 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1943 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001944 }
1945
1946 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001947 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001948 if (i)
1949 chip->read_buf(mtd, oob, i);
1950
Mike Dunn3f91e942012-04-25 12:06:09 -07001951 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001952}
1953
1954/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001955 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001956 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001957 * @oob: oob destination address
1958 * @ops: oob ops structure
1959 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001960 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001961static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001962 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001963{
Boris Brezillon846031d2016-02-03 20:11:00 +01001964 struct nand_chip *chip = mtd_to_nand(mtd);
1965 int ret;
1966
Florian Fainellif8ac0412010-09-07 13:23:43 +02001967 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968
Brian Norris0612b9d2011-08-30 18:45:40 -07001969 case MTD_OPS_PLACE_OOB:
1970 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001971 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1972 return oob + len;
1973
Boris Brezillon846031d2016-02-03 20:11:00 +01001974 case MTD_OPS_AUTO_OOB:
1975 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1976 ops->ooboffs, len);
1977 BUG_ON(ret);
1978 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001979
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001980 default:
1981 BUG();
1982 }
1983 return NULL;
1984}
1985
1986/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001987 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1988 * @mtd: MTD device structure
1989 * @retry_mode: the retry mode to use
1990 *
1991 * Some vendors supply a special command to shift the Vt threshold, to be used
1992 * when there are too many bitflips in a page (i.e., ECC error). After setting
1993 * a new threshold, the host should retry reading the page.
1994 */
1995static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1996{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001997 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001998
1999 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2000
2001 if (retry_mode >= chip->read_retries)
2002 return -EINVAL;
2003
2004 if (!chip->setup_read_retry)
2005 return -EOPNOTSUPP;
2006
2007 return chip->setup_read_retry(mtd, retry_mode);
2008}
2009
2010/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002011 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002012 * @mtd: MTD device structure
2013 * @from: offset to read from
2014 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00002015 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002016 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00002017 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002018static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2019 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00002020{
Brian Norrise47f3db2012-05-02 10:14:56 -07002021 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002022 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002023 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002024 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03002025 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002026 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02002027
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002028 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002029 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07002030 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08002031 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08002032 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002034 chipnr = (int)(from >> chip->chip_shift);
2035 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002037 realpage = (int)(from >> chip->page_shift);
2038 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002040 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002042 buf = ops->datbuf;
2043 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07002044 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002045
Florian Fainellif8ac0412010-09-07 13:23:43 +02002046 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08002047 unsigned int ecc_failures = mtd->ecc_stats.failed;
2048
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002049 bytes = min(mtd->writesize - col, readlen);
2050 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002051
Kamal Dasu66507c72014-05-01 20:51:19 -04002052 if (!aligned)
2053 use_bufpoi = 1;
2054 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002055 use_bufpoi = !virt_addr_valid(buf) ||
2056 !IS_ALIGNED((unsigned long)buf,
2057 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002058 else
2059 use_bufpoi = 0;
2060
Brian Norris8b6e50c2011-05-25 14:59:01 -07002061 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002062 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002063 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2064
2065 if (use_bufpoi && aligned)
2066 pr_debug("%s: using read bounce buffer for buf@%p\n",
2067 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Brian Norrisba84fb52014-01-03 15:13:33 -08002069read_retry:
Marc Gonzalez3371d662016-11-15 10:56:20 +01002070 if (nand_standard_page_accessors(&chip->ecc))
2071 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Mike Dunnedbc45402012-04-25 12:06:11 -07002073 /*
2074 * Now read the page into the buffer. Absent an error,
2075 * the read methods return max bitflips per ecc step.
2076 */
Brian Norris0612b9d2011-08-30 18:45:40 -07002077 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07002078 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002079 oob_required,
2080 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002081 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2082 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002083 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08002084 col, bytes, bufpoi,
2085 page);
David Woodhouse956e9442006-09-25 17:12:39 +01002086 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07002087 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07002088 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07002089 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04002090 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07002091 /* Invalidate page cache */
2092 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01002093 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07002094 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002095
2096 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04002097 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05002098 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08002099 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07002100 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01002101 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07002102 chip->pagebuf_bitflips = ret;
2103 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07002104 /* Invalidate page cache */
2105 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07002106 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002107 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002109
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002110 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002111 int toread = min(oobreadlen, max_oobsize);
2112
2113 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01002114 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02002115 oob, ops, toread);
2116 oobreadlen -= toread;
2117 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002118 }
Brian Norris5bc7c332013-03-13 09:51:31 -07002119
2120 if (chip->options & NAND_NEED_READRDY) {
2121 /* Apply delay or wait for ready/busy pin */
2122 if (!chip->dev_ready)
2123 udelay(chip->chip_delay);
2124 else
2125 nand_wait_ready(mtd);
2126 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08002127
Brian Norrisba84fb52014-01-03 15:13:33 -08002128 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08002129 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08002130 retry_mode++;
2131 ret = nand_setup_read_retry(mtd,
2132 retry_mode);
2133 if (ret < 0)
2134 break;
2135
2136 /* Reset failures; retry */
2137 mtd->ecc_stats.failed = ecc_failures;
2138 goto read_retry;
2139 } else {
2140 /* No more retry modes; real failure */
2141 ecc_fail = true;
2142 }
2143 }
2144
2145 buf += bytes;
Masahiro Yamada07604682017-03-30 15:45:47 +09002146 max_bitflips = max_t(unsigned int, max_bitflips, ret);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002147 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002148 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002149 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07002150 max_bitflips = max_t(unsigned int, max_bitflips,
2151 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002154 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002155
Brian Norrisba84fb52014-01-03 15:13:33 -08002156 /* Reset to retry mode 0 */
2157 if (retry_mode) {
2158 ret = nand_setup_read_retry(mtd, 0);
2159 if (ret < 0)
2160 break;
2161 retry_mode = 0;
2162 }
2163
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002164 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Brian Norris8b6e50c2011-05-25 14:59:01 -07002167 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 col = 0;
2169 /* Increment page address */
2170 realpage++;
2171
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002172 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* Check, if we cross a chip boundary */
2174 if (!page) {
2175 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002176 chip->select_chip(mtd, -1);
2177 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002180 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002182 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03002183 if (oob)
2184 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Mike Dunn3f91e942012-04-25 12:06:09 -07002186 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002187 return ret;
2188
Brian Norrisb72f3df2013-12-03 11:04:14 -08002189 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02002190 return -EBADMSG;
2191
Mike Dunnedbc45402012-04-25 12:06:11 -07002192 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002193}
2194
2195/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002196 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07002197 * @mtd: MTD device structure
2198 * @from: offset to read from
2199 * @len: number of bytes to read
2200 * @retlen: pointer to variable to store the number of read bytes
2201 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002202 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002203 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002204 */
2205static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2206 size_t *retlen, uint8_t *buf)
2207{
Brian Norris4a89ff82011-08-30 18:45:45 -07002208 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002209 int ret;
2210
Huang Shijie6a8214a2012-11-19 14:43:30 +08002211 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002212 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002213 ops.len = len;
2214 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002215 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002216 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002217 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002218 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02002219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002223 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002224 * @mtd: mtd info structure
2225 * @chip: nand chip info structure
2226 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002227 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002228int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002229{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002230 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002231 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002232 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002233}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002234EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002235
2236/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002237 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002238 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07002239 * @mtd: mtd info structure
2240 * @chip: nand chip info structure
2241 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002242 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002243int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2244 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002245{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002246 int length = mtd->oobsize;
2247 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2248 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02002249 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002250 int i, toread, sndrnd = 0, pos;
2251
2252 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2253 for (i = 0; i < chip->ecc.steps; i++) {
2254 if (sndrnd) {
2255 pos = eccsize + i * (eccsize + chunk);
2256 if (mtd->writesize > 512)
2257 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2258 else
2259 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2260 } else
2261 sndrnd = 1;
2262 toread = min_t(int, length, chunk);
2263 chip->read_buf(mtd, bufpoi, toread);
2264 bufpoi += toread;
2265 length -= toread;
2266 }
2267 if (length > 0)
2268 chip->read_buf(mtd, bufpoi, length);
2269
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03002270 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002271}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002272EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002273
2274/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002275 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002276 * @mtd: mtd info structure
2277 * @chip: nand chip info structure
2278 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002279 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002280int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002281{
2282 int status = 0;
2283 const uint8_t *buf = chip->oob_poi;
2284 int length = mtd->oobsize;
2285
2286 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2287 chip->write_buf(mtd, buf, length);
2288 /* Send command to program the OOB data */
2289 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2290
2291 status = chip->waitfunc(mtd, chip);
2292
Savin Zlobec0d420f92006-06-21 11:51:20 +02002293 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002294}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002295EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002296
2297/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002298 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002299 * with syndrome - only for large page flash
2300 * @mtd: mtd info structure
2301 * @chip: nand chip info structure
2302 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002303 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002304int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2305 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002306{
2307 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2308 int eccsize = chip->ecc.size, length = mtd->oobsize;
2309 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2310 const uint8_t *bufpoi = chip->oob_poi;
2311
2312 /*
2313 * data-ecc-data-ecc ... ecc-oob
2314 * or
2315 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
2316 */
2317 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2318 pos = steps * (eccsize + chunk);
2319 steps = 0;
2320 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002321 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002322
2323 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2324 for (i = 0; i < steps; i++) {
2325 if (sndcmd) {
2326 if (mtd->writesize <= 512) {
2327 uint32_t fill = 0xFFFFFFFF;
2328
2329 len = eccsize;
2330 while (len > 0) {
2331 int num = min_t(int, len, 4);
2332 chip->write_buf(mtd, (uint8_t *)&fill,
2333 num);
2334 len -= num;
2335 }
2336 } else {
2337 pos = eccsize + i * (eccsize + chunk);
2338 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2339 }
2340 } else
2341 sndcmd = 1;
2342 len = min_t(int, length, chunk);
2343 chip->write_buf(mtd, bufpoi, len);
2344 bufpoi += len;
2345 length -= len;
2346 }
2347 if (length > 0)
2348 chip->write_buf(mtd, bufpoi, length);
2349
2350 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2351 status = chip->waitfunc(mtd, chip);
2352
2353 return status & NAND_STATUS_FAIL ? -EIO : 0;
2354}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002355EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002356
2357/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002358 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002359 * @mtd: MTD device structure
2360 * @from: offset to read from
2361 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002363 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002365static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2366 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Brian Norrisc00a0992012-05-01 17:12:54 -07002368 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002369 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002370 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002371 int readlen = ops->ooblen;
2372 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002373 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002374 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Brian Norris289c0522011-07-19 10:06:09 -07002376 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302377 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Brian Norris041e4572011-06-23 16:45:24 -07002379 stats = mtd->ecc_stats;
2380
Boris BREZILLON29f10582016-03-07 10:46:52 +01002381 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002382
2383 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002384 pr_debug("%s: attempt to start read outside oob\n",
2385 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002386 return -EINVAL;
2387 }
2388
2389 /* Do not allow reads past end of device */
2390 if (unlikely(from >= mtd->size ||
2391 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2392 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002393 pr_debug("%s: attempt to read beyond end of device\n",
2394 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002395 return -EINVAL;
2396 }
Vitaly Wool70145682006-11-03 18:20:38 +03002397
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002398 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002399 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002401 /* Shift to get page */
2402 realpage = (int)(from >> chip->page_shift);
2403 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Florian Fainellif8ac0412010-09-07 13:23:43 +02002405 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002406 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002407 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002408 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002409 ret = chip->ecc.read_oob(mtd, chip, page);
2410
2411 if (ret < 0)
2412 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002413
2414 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002415 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002416
Brian Norris5bc7c332013-03-13 09:51:31 -07002417 if (chip->options & NAND_NEED_READRDY) {
2418 /* Apply delay or wait for ready/busy pin */
2419 if (!chip->dev_ready)
2420 udelay(chip->chip_delay);
2421 else
2422 nand_wait_ready(mtd);
2423 }
2424
Vitaly Wool70145682006-11-03 18:20:38 +03002425 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002426 if (!readlen)
2427 break;
2428
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002429 /* Increment page address */
2430 realpage++;
2431
2432 page = realpage & chip->pagemask;
2433 /* Check, if we cross a chip boundary */
2434 if (!page) {
2435 chipnr++;
2436 chip->select_chip(mtd, -1);
2437 chip->select_chip(mtd, chipnr);
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002440 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002442 ops->oobretlen = ops->ooblen - readlen;
2443
2444 if (ret < 0)
2445 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002446
2447 if (mtd->ecc_stats.failed - stats.failed)
2448 return -EBADMSG;
2449
2450 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
2453/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002454 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002455 * @mtd: MTD device structure
2456 * @from: offset to read from
2457 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002459 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002461static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2462 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002464 int ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002465
2466 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002469 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002470 pr_debug("%s: attempt to read beyond end of device\n",
2471 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return -EINVAL;
2473 }
2474
Andrey Smirnovfc6b4d12016-07-21 14:59:21 -07002475 if (ops->mode != MTD_OPS_PLACE_OOB &&
2476 ops->mode != MTD_OPS_AUTO_OOB &&
2477 ops->mode != MTD_OPS_RAW)
2478 return -ENOTSUPP;
2479
Huang Shijie6a8214a2012-11-19 14:43:30 +08002480 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002482 if (!ops->datbuf)
2483 ret = nand_do_read_oob(mtd, from, ops);
2484 else
2485 ret = nand_do_read_ops(mtd, from, ops);
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002488 return ret;
2489}
2490
2491
2492/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002493 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002494 * @mtd: mtd info structure
2495 * @chip: nand chip info structure
2496 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002497 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002498 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002499 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002500 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002501 */
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002502int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2503 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002504{
2505 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002506 if (oob_required)
2507 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002508
2509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
Thomas Petazzonicc0f51e2017-04-29 11:06:44 +02002511EXPORT_SYMBOL(nand_write_page_raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002513/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002514 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002515 * @mtd: mtd info structure
2516 * @chip: nand chip info structure
2517 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002518 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002519 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002520 *
2521 * We need a special oob layout and handling even when ECC isn't checked.
2522 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002523static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002524 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002525 const uint8_t *buf, int oob_required,
2526 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002527{
2528 int eccsize = chip->ecc.size;
2529 int eccbytes = chip->ecc.bytes;
2530 uint8_t *oob = chip->oob_poi;
2531 int steps, size;
2532
2533 for (steps = chip->ecc.steps; steps > 0; steps--) {
2534 chip->write_buf(mtd, buf, eccsize);
2535 buf += eccsize;
2536
2537 if (chip->ecc.prepad) {
2538 chip->write_buf(mtd, oob, chip->ecc.prepad);
2539 oob += chip->ecc.prepad;
2540 }
2541
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002542 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002543 oob += eccbytes;
2544
2545 if (chip->ecc.postpad) {
2546 chip->write_buf(mtd, oob, chip->ecc.postpad);
2547 oob += chip->ecc.postpad;
2548 }
2549 }
2550
2551 size = mtd->oobsize - (oob - chip->oob_poi);
2552 if (size)
2553 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002554
2555 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002556}
2557/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002558 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002559 * @mtd: mtd info structure
2560 * @chip: nand chip info structure
2561 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002562 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002563 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002564 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002565static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002566 const uint8_t *buf, int oob_required,
2567 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568{
Boris Brezillon846031d2016-02-03 20:11:00 +01002569 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002570 int eccbytes = chip->ecc.bytes;
2571 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002572 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002573 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002574
Brian Norris7854d3f2011-06-23 14:12:08 -07002575 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002576 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2577 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002578
Boris Brezillon846031d2016-02-03 20:11:00 +01002579 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2580 chip->ecc.total);
2581 if (ret)
2582 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002583
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002584 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002585}
2586
2587/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002588 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002589 * @mtd: mtd info structure
2590 * @chip: nand chip info structure
2591 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002592 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002593 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002594 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002595static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002596 const uint8_t *buf, int oob_required,
2597 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002598{
Boris Brezillon846031d2016-02-03 20:11:00 +01002599 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002600 int eccbytes = chip->ecc.bytes;
2601 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002602 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002603 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002604
2605 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2606 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002607 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002608 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2609 }
2610
Boris Brezillon846031d2016-02-03 20:11:00 +01002611 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2612 chip->ecc.total);
2613 if (ret)
2614 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002615
2616 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002617
2618 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002619}
2620
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302621
2622/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002623 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302624 * @mtd: mtd info structure
2625 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002626 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302627 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002628 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302629 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002630 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302631 */
2632static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2633 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002634 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002635 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302636{
2637 uint8_t *oob_buf = chip->oob_poi;
2638 uint8_t *ecc_calc = chip->buffers->ecccalc;
2639 int ecc_size = chip->ecc.size;
2640 int ecc_bytes = chip->ecc.bytes;
2641 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302642 uint32_t start_step = offset / ecc_size;
2643 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2644 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002645 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302646
2647 for (step = 0; step < ecc_steps; step++) {
2648 /* configure controller for WRITE access */
2649 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2650
2651 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002652 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302653
2654 /* mask ECC of un-touched subpages by padding 0xFF */
2655 if ((step < start_step) || (step > end_step))
2656 memset(ecc_calc, 0xff, ecc_bytes);
2657 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002658 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302659
2660 /* mask OOB of un-touched subpages by padding 0xFF */
2661 /* if oob_required, preserve OOB metadata of written subpage */
2662 if (!oob_required || (step < start_step) || (step > end_step))
2663 memset(oob_buf, 0xff, oob_bytes);
2664
Brian Norrisd6a950802013-08-08 17:16:36 -07002665 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302666 ecc_calc += ecc_bytes;
2667 oob_buf += oob_bytes;
2668 }
2669
2670 /* copy calculated ECC for whole page to chip->buffer->oob */
2671 /* this include masked-value(0xFF) for unwritten subpages */
2672 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002673 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2674 chip->ecc.total);
2675 if (ret)
2676 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302677
2678 /* write OOB buffer to NAND device */
2679 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2680
2681 return 0;
2682}
2683
2684
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002685/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002686 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002687 * @mtd: mtd info structure
2688 * @chip: nand chip info structure
2689 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002690 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002691 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002692 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002693 * The hw generator calculates the error syndrome automatically. Therefore we
2694 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002695 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002696static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002697 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002698 const uint8_t *buf, int oob_required,
2699 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002700{
2701 int i, eccsize = chip->ecc.size;
2702 int eccbytes = chip->ecc.bytes;
2703 int eccsteps = chip->ecc.steps;
2704 const uint8_t *p = buf;
2705 uint8_t *oob = chip->oob_poi;
2706
2707 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2708
2709 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2710 chip->write_buf(mtd, p, eccsize);
2711
2712 if (chip->ecc.prepad) {
2713 chip->write_buf(mtd, oob, chip->ecc.prepad);
2714 oob += chip->ecc.prepad;
2715 }
2716
2717 chip->ecc.calculate(mtd, p, oob);
2718 chip->write_buf(mtd, oob, eccbytes);
2719 oob += eccbytes;
2720
2721 if (chip->ecc.postpad) {
2722 chip->write_buf(mtd, oob, chip->ecc.postpad);
2723 oob += chip->ecc.postpad;
2724 }
2725 }
2726
2727 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002728 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002729 if (i)
2730 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002731
2732 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002733}
2734
2735/**
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002736 * nand_write_page - write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002737 * @mtd: MTD device structure
2738 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302739 * @offset: address offset within the page
2740 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002741 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002742 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 * @page: page number to write
2744 * @cached: cached programming
2745 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002746 */
2747static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302748 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002749 int oob_required, int page, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002750{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302751 int status, subpage;
2752
2753 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2754 chip->ecc.write_subpage)
2755 subpage = offset || (data_len < mtd->writesize);
2756 else
2757 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002758
Marc Gonzalez3371d662016-11-15 10:56:20 +01002759 if (nand_standard_page_accessors(&chip->ecc))
2760 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002761
David Woodhouse956e9442006-09-25 17:12:39 +01002762 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302763 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002764 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302765 else if (subpage)
2766 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002767 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002768 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002769 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2770 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002771
2772 if (status < 0)
2773 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002774
Boris Brezillon41145642017-05-16 18:27:49 +02002775 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002776 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002777
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002778 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002779 if (status & NAND_STATUS_FAIL)
2780 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002781 }
2782
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002783 return 0;
2784}
2785
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002786/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002787 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002788 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002789 * @oob: oob data buffer
2790 * @len: oob data write length
2791 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002792 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002793static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2794 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002795{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002796 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002797 int ret;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002798
2799 /*
2800 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2801 * data from a previous OOB read.
2802 */
2803 memset(chip->oob_poi, 0xff, mtd->oobsize);
2804
Florian Fainellif8ac0412010-09-07 13:23:43 +02002805 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002806
Brian Norris0612b9d2011-08-30 18:45:40 -07002807 case MTD_OPS_PLACE_OOB:
2808 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002809 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2810 return oob + len;
2811
Boris Brezillon846031d2016-02-03 20:11:00 +01002812 case MTD_OPS_AUTO_OOB:
2813 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2814 ops->ooboffs, len);
2815 BUG_ON(ret);
2816 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002817
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002818 default:
2819 BUG();
2820 }
2821 return NULL;
2822}
2823
Florian Fainellif8ac0412010-09-07 13:23:43 +02002824#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002825
2826/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002827 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002828 * @mtd: MTD device structure
2829 * @to: offset to write to
2830 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002831 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002832 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002833 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002834static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2835 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002836{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002837 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002838 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002839 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002840
2841 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002842 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002843
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002844 uint8_t *oob = ops->oobbuf;
2845 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302846 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002847 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002848
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002849 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002850 if (!writelen)
2851 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002852
Brian Norris8b6e50c2011-05-25 14:59:01 -07002853 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002854 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002855 pr_notice("%s: attempt to write non page aligned data\n",
2856 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002857 return -EINVAL;
2858 }
2859
Thomas Gleixner29072b92006-09-28 15:38:36 +02002860 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002861
Thomas Gleixner6a930962006-06-28 00:11:45 +02002862 chipnr = (int)(to >> chip->chip_shift);
2863 chip->select_chip(mtd, chipnr);
2864
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002865 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002866 if (nand_check_wp(mtd)) {
2867 ret = -EIO;
2868 goto err_out;
2869 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002870
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002871 realpage = (int)(to >> chip->page_shift);
2872 page = realpage & chip->pagemask;
2873 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2874
2875 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002876 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2877 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002878 chip->pagebuf = -1;
2879
Maxim Levitsky782ce792010-02-22 20:39:36 +02002880 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002881 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2882 ret = -EINVAL;
2883 goto err_out;
2884 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002885
Florian Fainellif8ac0412010-09-07 13:23:43 +02002886 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002887 int bytes = mtd->writesize;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002888 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002889 int use_bufpoi;
Hector Palacios144f4c92016-07-18 10:39:18 +02002890 int part_pagewr = (column || writelen < mtd->writesize);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002891
Kamal Dasu66507c72014-05-01 20:51:19 -04002892 if (part_pagewr)
2893 use_bufpoi = 1;
2894 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
Masahiro Yamada477544c2017-03-30 17:15:05 +09002895 use_bufpoi = !virt_addr_valid(buf) ||
2896 !IS_ALIGNED((unsigned long)buf,
2897 chip->buf_align);
Kamal Dasu66507c72014-05-01 20:51:19 -04002898 else
2899 use_bufpoi = 0;
2900
2901 /* Partial page write?, or need to use bounce buffer */
2902 if (use_bufpoi) {
2903 pr_debug("%s: using write bounce buffer for buf@%p\n",
2904 __func__, buf);
Kamal Dasu66507c72014-05-01 20:51:19 -04002905 if (part_pagewr)
2906 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002907 chip->pagebuf = -1;
2908 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2909 memcpy(&chip->buffers->databuf[column], buf, bytes);
2910 wbuf = chip->buffers->databuf;
2911 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002912
Maxim Levitsky782ce792010-02-22 20:39:36 +02002913 if (unlikely(oob)) {
2914 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002915 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002916 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002917 } else {
2918 /* We still need to erase leftover OOB data */
2919 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002920 }
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002921
2922 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillon0b4773fd2017-05-16 00:17:41 +02002923 oob_required, page,
Boris Brezillonf107d7a2017-03-16 09:02:42 +01002924 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002925 if (ret)
2926 break;
2927
2928 writelen -= bytes;
2929 if (!writelen)
2930 break;
2931
Thomas Gleixner29072b92006-09-28 15:38:36 +02002932 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002933 buf += bytes;
2934 realpage++;
2935
2936 page = realpage & chip->pagemask;
2937 /* Check, if we cross a chip boundary */
2938 if (!page) {
2939 chipnr++;
2940 chip->select_chip(mtd, -1);
2941 chip->select_chip(mtd, chipnr);
2942 }
2943 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002945 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002946 if (unlikely(oob))
2947 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002948
2949err_out:
2950 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002951 return ret;
2952}
2953
2954/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002955 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002956 * @mtd: MTD device structure
2957 * @to: offset to write to
2958 * @len: number of bytes to write
2959 * @retlen: pointer to variable to store the number of written bytes
2960 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002961 *
2962 * NAND write with ECC. Used when performing writes in interrupt context, this
2963 * may for example be called by mtdoops when writing an oops while in panic.
2964 */
2965static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2966 size_t *retlen, const uint8_t *buf)
2967{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002968 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002969 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002970 int ret;
2971
Brian Norris8b6e50c2011-05-25 14:59:01 -07002972 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002973 panic_nand_wait(mtd, chip, 400);
2974
Brian Norris8b6e50c2011-05-25 14:59:01 -07002975 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002976 panic_nand_get_device(chip, mtd, FL_WRITING);
2977
Brian Norris0ec56dc2015-02-28 02:02:30 -08002978 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002979 ops.len = len;
2980 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002981 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002982
Brian Norris4a89ff82011-08-30 18:45:45 -07002983 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002984
Brian Norris4a89ff82011-08-30 18:45:45 -07002985 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002986 return ret;
2987}
2988
2989/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002990 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002991 * @mtd: MTD device structure
2992 * @to: offset to write to
2993 * @len: number of bytes to write
2994 * @retlen: pointer to variable to store the number of written bytes
2995 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002997 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002999static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003000 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Brian Norris4a89ff82011-08-30 18:45:45 -07003002 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003003 int ret;
3004
Huang Shijie6a8214a2012-11-19 14:43:30 +08003005 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08003006 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07003007 ops.len = len;
3008 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08003009 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07003010 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07003011 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003012 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003013 return ret;
3014}
3015
3016/**
3017 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003018 * @mtd: MTD device structure
3019 * @to: offset to write to
3020 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003021 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003022 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003023 */
3024static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3025 struct mtd_oob_ops *ops)
3026{
Adrian Hunter03736152007-01-31 17:58:29 +02003027 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003028 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Brian Norris289c0522011-07-19 10:06:09 -07003030 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05303031 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Boris BREZILLON29f10582016-03-07 10:46:52 +01003033 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02003034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02003036 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07003037 pr_debug("%s: attempt to write past end of page\n",
3038 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return -EINVAL;
3040 }
3041
Adrian Hunter03736152007-01-31 17:58:29 +02003042 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003043 pr_debug("%s: attempt to start write outside oob\n",
3044 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003045 return -EINVAL;
3046 }
3047
Jason Liu775adc3d42011-02-25 13:06:18 +08003048 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02003049 if (unlikely(to >= mtd->size ||
3050 ops->ooboffs + ops->ooblen >
3051 ((mtd->size >> chip->page_shift) -
3052 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07003053 pr_debug("%s: attempt to write beyond end of device\n",
3054 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02003055 return -EINVAL;
3056 }
3057
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003058 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02003059
3060 /*
3061 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
3062 * of my DiskOnChip 2000 test units) will clear the whole data page too
3063 * if we don't do this. I have no clue why, but I seem to have 'fixed'
3064 * it in the doc2000 driver in August 1999. dwmw2.
3065 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02003066 nand_reset(chip, chipnr);
3067
3068 chip->select_chip(mtd, chipnr);
3069
3070 /* Shift to get page */
3071 page = (int)(to >> chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003074 if (nand_check_wp(mtd)) {
3075 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003076 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08003077 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003080 if (page == chip->pagebuf)
3081 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02003083 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07003084
Brian Norris0612b9d2011-08-30 18:45:40 -07003085 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07003086 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3087 else
3088 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003089
Huang Shijieb0bb6902012-11-19 14:43:29 +08003090 chip->select_chip(mtd, -1);
3091
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003092 if (status)
3093 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Vitaly Wool70145682006-11-03 18:20:38 +03003095 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003097 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003100/**
3101 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07003102 * @mtd: MTD device structure
3103 * @to: offset to write to
3104 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003105 */
3106static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3107 struct mtd_oob_ops *ops)
3108{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003109 int ret = -ENOTSUPP;
3110
3111 ops->retlen = 0;
3112
3113 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03003114 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07003115 pr_debug("%s: attempt to write beyond end of device\n",
3116 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003117 return -EINVAL;
3118 }
3119
Huang Shijie6a8214a2012-11-19 14:43:30 +08003120 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003121
Florian Fainellif8ac0412010-09-07 13:23:43 +02003122 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07003123 case MTD_OPS_PLACE_OOB:
3124 case MTD_OPS_AUTO_OOB:
3125 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003126 break;
3127
3128 default:
3129 goto out;
3130 }
3131
3132 if (!ops->datbuf)
3133 ret = nand_do_write_oob(mtd, to, ops);
3134 else
3135 ret = nand_do_write_ops(mtd, to, ops);
3136
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003137out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003138 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 return ret;
3140}
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142/**
Brian Norris49c50b92014-05-06 16:02:19 -07003143 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07003144 * @mtd: MTD device structure
3145 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 *
Brian Norris49c50b92014-05-06 16:02:19 -07003147 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 */
Brian Norris49c50b92014-05-06 16:02:19 -07003149static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003151 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003153 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3154 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07003155
3156 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
3159/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003161 * @mtd: MTD device structure
3162 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003164 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003166static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
David Woodhousee0c7d762006-05-13 18:07:53 +01003168 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171/**
Brian Norris7854d3f2011-06-23 14:12:08 -07003172 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003173 * @mtd: MTD device structure
3174 * @instr: erase instruction
3175 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003177 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003179int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3180 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Adrian Hunter69423d92008-12-10 13:37:21 +00003182 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003183 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00003184 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Brian Norris289c0522011-07-19 10:06:09 -07003186 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3187 __func__, (unsigned long long)instr->addr,
3188 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05303190 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003194 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003197 page = (int)(instr->addr >> chip->page_shift);
3198 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003201 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003204 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /* Check, if it is write protected */
3207 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07003208 pr_debug("%s: device is write protected!\n",
3209 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 instr->state = MTD_ERASE_FAILED;
3211 goto erase_exit;
3212 }
3213
3214 /* Loop through the pages */
3215 len = instr->len;
3216
3217 instr->state = MTD_ERASING;
3218
3219 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01003220 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003221 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05303222 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07003223 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3224 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 instr->state = MTD_ERASE_FAILED;
3226 goto erase_exit;
3227 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003228
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003229 /*
3230 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07003231 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003232 */
3233 if (page <= chip->pagebuf && chip->pagebuf <
3234 (page + pages_per_block))
3235 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Brian Norris49c50b92014-05-06 16:02:19 -07003237 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00003240 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07003241 pr_debug("%s: failed erase, page 0x%08x\n",
3242 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00003244 instr->fail_addr =
3245 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 goto erase_exit;
3247 }
David A. Marlin30f464b2005-01-17 18:35:25 +00003248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03003250 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 page += pages_per_block;
3252
3253 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003254 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003256 chip->select_chip(mtd, -1);
3257 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
3259 }
3260 instr->state = MTD_ERASE_DONE;
3261
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003262erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08003267 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 nand_release_device(mtd);
3269
David Woodhouse49defc02007-10-06 15:01:59 -04003270 /* Do call back function */
3271 if (!ret)
3272 mtd_erase_callback(instr);
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /* Return more or less happy */
3275 return ret;
3276}
3277
3278/**
3279 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07003280 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003282 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003284static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
Brian Norris289c0522011-07-19 10:06:09 -07003286 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08003289 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01003291 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003295 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003296 * @mtd: MTD device structure
3297 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003299static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Archit Taneja9f3e0422016-02-03 14:29:49 +05303301 struct nand_chip *chip = mtd_to_nand(mtd);
3302 int chipnr = (int)(offs >> chip->chip_shift);
3303 int ret;
3304
3305 /* Select the NAND device */
3306 nand_get_device(mtd, FL_READING);
3307 chip->select_chip(mtd, chipnr);
3308
3309 ret = nand_block_checkbad(mtd, offs, 0);
3310
3311 chip->select_chip(mtd, -1);
3312 nand_release_device(mtd);
3313
3314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
3317/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003318 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003319 * @mtd: MTD device structure
3320 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003322static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 int ret;
3325
Florian Fainellif8ac0412010-09-07 13:23:43 +02003326 ret = nand_block_isbad(mtd, ofs);
3327 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003328 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (ret > 0)
3330 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003331 return ret;
3332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Brian Norris5a0edb22013-07-30 17:52:58 -07003334 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337/**
Zach Brown56718422017-01-10 13:30:20 -06003338 * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd
3339 * @mtd: MTD device structure
3340 * @ofs: offset relative to mtd start
3341 * @len: length of mtd
3342 */
3343static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3344{
3345 struct nand_chip *chip = mtd_to_nand(mtd);
3346 u32 part_start_block;
3347 u32 part_end_block;
3348 u32 part_start_die;
3349 u32 part_end_die;
3350
3351 /*
3352 * max_bb_per_die and blocks_per_die used to determine
3353 * the maximum bad block count.
3354 */
3355 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3356 return -ENOTSUPP;
3357
3358 /* Get the start and end of the partition in erase blocks. */
3359 part_start_block = mtd_div_by_eb(ofs, mtd);
3360 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3361
3362 /* Get the start and end LUNs of the partition. */
3363 part_start_die = part_start_block / chip->blocks_per_die;
3364 part_end_die = part_end_block / chip->blocks_per_die;
3365
3366 /*
3367 * Look up the bad blocks per unit and multiply by the number of units
3368 * that the partition spans.
3369 */
3370 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3371}
3372
3373/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003374 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3375 * @mtd: MTD device structure
3376 * @chip: nand chip info structure
3377 * @addr: feature address.
3378 * @subfeature_param: the subfeature parameters, a four bytes array.
3379 */
3380static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3381 int addr, uint8_t *subfeature_param)
3382{
3383 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003384 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003385
David Mosbergerd914c932013-05-29 15:30:13 +03003386 if (!chip->onfi_version ||
3387 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3388 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003389 return -EINVAL;
3390
3391 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003392 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3393 chip->write_byte(mtd, subfeature_param[i]);
3394
Huang Shijie7db03ec2012-09-13 14:57:52 +08003395 status = chip->waitfunc(mtd, chip);
3396 if (status & NAND_STATUS_FAIL)
3397 return -EIO;
3398 return 0;
3399}
3400
3401/**
3402 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3403 * @mtd: MTD device structure
3404 * @chip: nand chip info structure
3405 * @addr: feature address.
3406 * @subfeature_param: the subfeature parameters, a four bytes array.
3407 */
3408static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3409 int addr, uint8_t *subfeature_param)
3410{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003411 int i;
3412
David Mosbergerd914c932013-05-29 15:30:13 +03003413 if (!chip->onfi_version ||
3414 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3415 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003416 return -EINVAL;
3417
Huang Shijie7db03ec2012-09-13 14:57:52 +08003418 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003419 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3420 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003421 return 0;
3422}
3423
3424/**
Boris Brezillon4a78cc62017-05-26 17:10:15 +02003425 * nand_onfi_get_set_features_notsupp - set/get features stub returning
3426 * -ENOTSUPP
3427 * @mtd: MTD device structure
3428 * @chip: nand chip info structure
3429 * @addr: feature address.
3430 * @subfeature_param: the subfeature parameters, a four bytes array.
3431 *
3432 * Should be used by NAND controller drivers that do not support the SET/GET
3433 * FEATURES operations.
3434 */
3435int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3436 struct nand_chip *chip, int addr,
3437 u8 *subfeature_param)
3438{
3439 return -ENOTSUPP;
3440}
3441EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3442
3443/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003444 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003445 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003446 */
3447static int nand_suspend(struct mtd_info *mtd)
3448{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003449 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003450}
3451
3452/**
3453 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003454 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003455 */
3456static void nand_resume(struct mtd_info *mtd)
3457{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003458 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003459
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003460 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003461 nand_release_device(mtd);
3462 else
Brian Norrisd0370212011-07-19 10:06:08 -07003463 pr_err("%s called for a chip which is not in suspended state\n",
3464 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003465}
3466
Scott Branden72ea4032014-11-20 11:18:05 -08003467/**
3468 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3469 * prevent further operations
3470 * @mtd: MTD device structure
3471 */
3472static void nand_shutdown(struct mtd_info *mtd)
3473{
Brian Norris9ca641b2015-11-09 16:37:28 -08003474 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003475}
3476
Brian Norris8b6e50c2011-05-25 14:59:01 -07003477/* Set default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003478static void nand_set_defaults(struct nand_chip *chip)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003479{
Boris Brezillon29a198a2016-05-24 20:17:48 +02003480 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003483 if (!chip->chip_delay)
3484 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
3486 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003487 if (chip->cmdfunc == NULL)
3488 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003491 if (chip->waitfunc == NULL)
3492 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003494 if (!chip->select_chip)
3495 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003496
Huang Shijie4204ccc2013-08-16 10:10:07 +08003497 /* set for ONFI nand */
3498 if (!chip->onfi_set_features)
3499 chip->onfi_set_features = nand_onfi_set_features;
3500 if (!chip->onfi_get_features)
3501 chip->onfi_get_features = nand_onfi_get_features;
3502
Brian Norris68e80782013-07-18 01:17:02 -07003503 /* If called twice, pointers that depend on busw may need to be reset */
3504 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003505 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3506 if (!chip->read_word)
3507 chip->read_word = nand_read_word;
3508 if (!chip->block_bad)
3509 chip->block_bad = nand_block_bad;
3510 if (!chip->block_markbad)
3511 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003512 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003513 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003514 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3515 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003516 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003517 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003518 if (!chip->scan_bbt)
3519 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003520
3521 if (!chip->controller) {
3522 chip->controller = &chip->hwcontrol;
Marc Gonzalezd45bc582016-07-27 11:23:52 +02003523 nand_hw_control_init(chip->controller);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003524 }
3525
Masahiro Yamada477544c2017-03-30 17:15:05 +09003526 if (!chip->buf_align)
3527 chip->buf_align = 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003528}
3529
Brian Norris8b6e50c2011-05-25 14:59:01 -07003530/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003531static void sanitize_string(uint8_t *s, size_t len)
3532{
3533 ssize_t i;
3534
Brian Norris8b6e50c2011-05-25 14:59:01 -07003535 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003536 s[len - 1] = 0;
3537
Brian Norris8b6e50c2011-05-25 14:59:01 -07003538 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003539 for (i = 0; i < len - 1; i++) {
3540 if (s[i] < ' ' || s[i] > 127)
3541 s[i] = '?';
3542 }
3543
Brian Norris8b6e50c2011-05-25 14:59:01 -07003544 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003545 strim(s);
3546}
3547
3548static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3549{
3550 int i;
3551 while (len--) {
3552 crc ^= *p++ << 8;
3553 for (i = 0; i < 8; i++)
3554 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3555 }
3556
3557 return crc;
3558}
3559
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003560/* Parse the Extended Parameter Page. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003561static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3562 struct nand_onfi_params *p)
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003563{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003564 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003565 struct onfi_ext_param_page *ep;
3566 struct onfi_ext_section *s;
3567 struct onfi_ext_ecc_info *ecc;
3568 uint8_t *cursor;
3569 int ret = -EINVAL;
3570 int len;
3571 int i;
3572
3573 len = le16_to_cpu(p->ext_param_page_length) * 16;
3574 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003575 if (!ep)
3576 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003577
3578 /* Send our own NAND_CMD_PARAM. */
3579 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3580
3581 /* Use the Change Read Column command to skip the ONFI param pages. */
3582 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3583 sizeof(*p) * p->num_of_param_pages , -1);
3584
3585 /* Read out the Extended Parameter Page. */
3586 chip->read_buf(mtd, (uint8_t *)ep, len);
3587 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3588 != le16_to_cpu(ep->crc))) {
3589 pr_debug("fail in the CRC.\n");
3590 goto ext_out;
3591 }
3592
3593 /*
3594 * Check the signature.
3595 * Do not strictly follow the ONFI spec, maybe changed in future.
3596 */
3597 if (strncmp(ep->sig, "EPPS", 4)) {
3598 pr_debug("The signature is invalid.\n");
3599 goto ext_out;
3600 }
3601
3602 /* find the ECC section. */
3603 cursor = (uint8_t *)(ep + 1);
3604 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3605 s = ep->sections + i;
3606 if (s->type == ONFI_SECTION_TYPE_2)
3607 break;
3608 cursor += s->length * 16;
3609 }
3610 if (i == ONFI_EXT_SECTION_MAX) {
3611 pr_debug("We can not find the ECC section.\n");
3612 goto ext_out;
3613 }
3614
3615 /* get the info we want. */
3616 ecc = (struct onfi_ext_ecc_info *)cursor;
3617
Brian Norris4ae7d222013-09-16 18:20:21 -07003618 if (!ecc->codeword_size) {
3619 pr_debug("Invalid codeword size\n");
3620 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003621 }
3622
Brian Norris4ae7d222013-09-16 18:20:21 -07003623 chip->ecc_strength_ds = ecc->ecc_bits;
3624 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003625 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003626
3627ext_out:
3628 kfree(ep);
3629 return ret;
3630}
3631
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003632/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003633 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003634 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003635static int nand_flash_detect_onfi(struct nand_chip *chip)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003636{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003637 struct mtd_info *mtd = nand_to_mtd(chip);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003638 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003639 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003640 int val;
3641
Brian Norris7854d3f2011-06-23 14:12:08 -07003642 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003643 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3644 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3645 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3646 return 0;
3647
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003648 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3649 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003650 for (j = 0; j < sizeof(*p); j++)
3651 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003652 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3653 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003654 break;
3655 }
3656 }
3657
Brian Norrisc7f23a72013-08-13 10:51:55 -07003658 if (i == 3) {
3659 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003660 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003661 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003662
Brian Norris8b6e50c2011-05-25 14:59:01 -07003663 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003664 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003665 if (val & (1 << 5))
3666 chip->onfi_version = 23;
3667 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003668 chip->onfi_version = 22;
3669 else if (val & (1 << 3))
3670 chip->onfi_version = 21;
3671 else if (val & (1 << 2))
3672 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003673 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003674 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003675
3676 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003677 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003678 return 0;
3679 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003680
3681 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3682 sanitize_string(p->model, sizeof(p->model));
3683 if (!mtd->name)
3684 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003685
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003686 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003687
3688 /*
3689 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3690 * (don't ask me who thought of this...). MTD assumes that these
3691 * dimensions will be power-of-2, so just truncate the remaining area.
3692 */
3693 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3694 mtd->erasesize *= mtd->writesize;
3695
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003696 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003697
3698 /* See erasesize comment */
3699 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003700 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003701 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003702
Zach Brown34da5f52017-01-10 13:30:21 -06003703 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3704 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3705
Huang Shijiee2985fc2013-05-17 11:17:30 +08003706 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003707 chip->options |= NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003708
Huang Shijie10c86ba2013-05-17 11:17:26 +08003709 if (p->ecc_bits != 0xff) {
3710 chip->ecc_strength_ds = p->ecc_bits;
3711 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003712 } else if (chip->onfi_version >= 21 &&
3713 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3714
3715 /*
3716 * The nand_flash_detect_ext_param_page() uses the
3717 * Change Read Column command which maybe not supported
3718 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3719 * now. We do not replace user supplied command function.
3720 */
3721 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3722 chip->cmdfunc = nand_command_lp;
3723
3724 /* The Extended Parameter Page is supported since ONFI 2.1. */
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003725 if (nand_flash_detect_ext_param_page(chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003726 pr_warn("Failed to detect ONFI extended param page\n");
3727 } else {
3728 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003729 }
3730
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003731 return 1;
3732}
3733
3734/*
Huang Shijie91361812014-02-21 13:39:40 +08003735 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3736 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003737static int nand_flash_detect_jedec(struct nand_chip *chip)
Huang Shijie91361812014-02-21 13:39:40 +08003738{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003739 struct mtd_info *mtd = nand_to_mtd(chip);
Huang Shijie91361812014-02-21 13:39:40 +08003740 struct nand_jedec_params *p = &chip->jedec_params;
3741 struct jedec_ecc_info *ecc;
3742 int val;
3743 int i, j;
3744
3745 /* Try JEDEC for unknown chip or LP */
3746 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3747 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3748 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3749 chip->read_byte(mtd) != 'C')
3750 return 0;
3751
3752 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3753 for (i = 0; i < 3; i++) {
3754 for (j = 0; j < sizeof(*p); j++)
3755 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3756
3757 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3758 le16_to_cpu(p->crc))
3759 break;
3760 }
3761
3762 if (i == 3) {
3763 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3764 return 0;
3765 }
3766
3767 /* Check version */
3768 val = le16_to_cpu(p->revision);
3769 if (val & (1 << 2))
3770 chip->jedec_version = 10;
3771 else if (val & (1 << 1))
3772 chip->jedec_version = 1; /* vendor specific version */
3773
3774 if (!chip->jedec_version) {
3775 pr_info("unsupported JEDEC version: %d\n", val);
3776 return 0;
3777 }
3778
3779 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3780 sanitize_string(p->model, sizeof(p->model));
3781 if (!mtd->name)
3782 mtd->name = p->model;
3783
3784 mtd->writesize = le32_to_cpu(p->byte_per_page);
3785
3786 /* Please reference to the comment for nand_flash_detect_onfi. */
3787 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3788 mtd->erasesize *= mtd->writesize;
3789
3790 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3791
3792 /* Please reference to the comment for nand_flash_detect_onfi. */
3793 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3794 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3795 chip->bits_per_cell = p->bits_per_cell;
3796
3797 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
Boris Brezillon29a198a2016-05-24 20:17:48 +02003798 chip->options |= NAND_BUSWIDTH_16;
Huang Shijie91361812014-02-21 13:39:40 +08003799
3800 /* ECC info */
3801 ecc = &p->ecc_info[0];
3802
3803 if (ecc->codeword_size >= 9) {
3804 chip->ecc_strength_ds = ecc->ecc_bits;
3805 chip->ecc_step_ds = 1 << ecc->codeword_size;
3806 } else {
3807 pr_warn("Invalid codeword size\n");
3808 }
3809
3810 return 1;
3811}
3812
3813/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003814 * nand_id_has_period - Check if an ID string has a given wraparound period
3815 * @id_data: the ID string
3816 * @arrlen: the length of the @id_data array
3817 * @period: the period of repitition
3818 *
3819 * Check if an ID string is repeated within a given sequence of bytes at
3820 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003821 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003822 * if the repetition has a period of @period; otherwise, returns zero.
3823 */
3824static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3825{
3826 int i, j;
3827 for (i = 0; i < period; i++)
3828 for (j = i + period; j < arrlen; j += period)
3829 if (id_data[i] != id_data[j])
3830 return 0;
3831 return 1;
3832}
3833
3834/*
3835 * nand_id_len - Get the length of an ID string returned by CMD_READID
3836 * @id_data: the ID string
3837 * @arrlen: the length of the @id_data array
3838
3839 * Returns the length of the ID string, according to known wraparound/trailing
3840 * zero patterns. If no pattern exists, returns the length of the array.
3841 */
3842static int nand_id_len(u8 *id_data, int arrlen)
3843{
3844 int last_nonzero, period;
3845
3846 /* Find last non-zero byte */
3847 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3848 if (id_data[last_nonzero])
3849 break;
3850
3851 /* All zeros */
3852 if (last_nonzero < 0)
3853 return 0;
3854
3855 /* Calculate wraparound period */
3856 for (period = 1; period < arrlen; period++)
3857 if (nand_id_has_period(id_data, arrlen, period))
3858 break;
3859
3860 /* There's a repeated pattern */
3861 if (period < arrlen)
3862 return period;
3863
3864 /* There are trailing zeros */
3865 if (last_nonzero < arrlen - 1)
3866 return last_nonzero + 1;
3867
3868 /* No pattern detected */
3869 return arrlen;
3870}
3871
Huang Shijie7db906b2013-09-25 14:58:11 +08003872/* Extract the bits of per cell from the 3rd byte of the extended ID */
3873static int nand_get_bits_per_cell(u8 cellinfo)
3874{
3875 int bits;
3876
3877 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3878 bits >>= NAND_CI_CELLTYPE_SHIFT;
3879 return bits + 1;
3880}
3881
Brian Norrise3b88bd2012-09-24 20:40:52 -07003882/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003883 * Many new NAND share similar device ID codes, which represent the size of the
3884 * chip. The rest of the parameters must be decoded according to generic or
3885 * manufacturer-specific "extended ID" decoding patterns.
3886 */
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003887void nand_decode_ext_id(struct nand_chip *chip)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003888{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003889 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon9b2d61f2016-06-08 10:34:57 +02003890 int extid;
Boris Brezillon7f501f02016-05-24 19:20:05 +02003891 u8 *id_data = chip->id.data;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003892 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003893 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003894 /* The 4th id byte is the important one */
3895 extid = id_data[3];
3896
Boris Brezillon01389b62016-06-08 10:30:18 +02003897 /* Calc pagesize */
3898 mtd->writesize = 1024 << (extid & 0x03);
3899 extid >>= 2;
3900 /* Calc oobsize */
3901 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3902 extid >>= 2;
3903 /* Calc blocksize. Blocksize is multiples of 64KiB */
3904 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3905 extid >>= 2;
3906 /* Get buswidth information */
3907 if (extid & 0x1)
3908 chip->options |= NAND_BUSWIDTH_16;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003909}
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003910EXPORT_SYMBOL_GPL(nand_decode_ext_id);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003911
3912/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003913 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3914 * decodes a matching ID table entry and assigns the MTD size parameters for
3915 * the chip.
3916 */
Boris Brezillon29a198a2016-05-24 20:17:48 +02003917static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
Brian Norrisf23a4812012-09-24 20:40:51 -07003918{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003919 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norrisf23a4812012-09-24 20:40:51 -07003920
3921 mtd->erasesize = type->erasesize;
3922 mtd->writesize = type->pagesize;
3923 mtd->oobsize = mtd->writesize / 32;
Brian Norrisf23a4812012-09-24 20:40:51 -07003924
Huang Shijie1c195e92013-09-25 14:58:12 +08003925 /* All legacy ID NAND are small-page, SLC */
3926 chip->bits_per_cell = 1;
Brian Norrisf23a4812012-09-24 20:40:51 -07003927}
3928
3929/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003930 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3931 * heuristic patterns using various detected parameters (e.g., manufacturer,
3932 * page size, cell-type information).
3933 */
Boris Brezillon7f501f02016-05-24 19:20:05 +02003934static void nand_decode_bbm_options(struct nand_chip *chip)
Brian Norris7e74c2d2012-09-24 20:40:49 -07003935{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003936 struct mtd_info *mtd = nand_to_mtd(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07003937
3938 /* Set the bad block position */
3939 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3940 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3941 else
3942 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Brian Norris7e74c2d2012-09-24 20:40:49 -07003943}
3944
Huang Shijieec6e87e2013-03-15 11:01:00 +08003945static inline bool is_full_id_nand(struct nand_flash_dev *type)
3946{
3947 return type->id_len;
3948}
3949
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003950static bool find_full_id_nand(struct nand_chip *chip,
Boris Brezillon29a198a2016-05-24 20:17:48 +02003951 struct nand_flash_dev *type)
Huang Shijieec6e87e2013-03-15 11:01:00 +08003952{
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003953 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillon7f501f02016-05-24 19:20:05 +02003954 u8 *id_data = chip->id.data;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02003955
Huang Shijieec6e87e2013-03-15 11:01:00 +08003956 if (!strncmp(type->id, id_data, type->id_len)) {
3957 mtd->writesize = type->pagesize;
3958 mtd->erasesize = type->erasesize;
3959 mtd->oobsize = type->oobsize;
3960
Huang Shijie7db906b2013-09-25 14:58:11 +08003961 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003962 chip->chipsize = (uint64_t)type->chipsize << 20;
3963 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003964 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3965 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003966 chip->onfi_timing_mode_default =
3967 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003968
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003969 if (!mtd->name)
3970 mtd->name = type->name;
3971
Huang Shijieec6e87e2013-03-15 11:01:00 +08003972 return true;
3973 }
3974 return false;
3975}
3976
Brian Norris7e74c2d2012-09-24 20:40:49 -07003977/*
Boris Brezillonabbe26d2016-06-08 09:32:55 +02003978 * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
3979 * compliant and does not have a full-id or legacy-id entry in the nand_ids
3980 * table.
3981 */
3982static void nand_manufacturer_detect(struct nand_chip *chip)
3983{
3984 /*
3985 * Try manufacturer detection if available and use
3986 * nand_decode_ext_id() otherwise.
3987 */
3988 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3989 chip->manufacturer.desc->ops->detect)
3990 chip->manufacturer.desc->ops->detect(chip);
3991 else
3992 nand_decode_ext_id(chip);
3993}
3994
3995/*
3996 * Manufacturer initialization. This function is called for all NANDs including
3997 * ONFI and JEDEC compliant ones.
3998 * Manufacturer drivers should put all their specific initialization code in
3999 * their ->init() hook.
4000 */
4001static int nand_manufacturer_init(struct nand_chip *chip)
4002{
4003 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4004 !chip->manufacturer.desc->ops->init)
4005 return 0;
4006
4007 return chip->manufacturer.desc->ops->init(chip);
4008}
4009
4010/*
4011 * Manufacturer cleanup. This function is called for all NANDs including
4012 * ONFI and JEDEC compliant ones.
4013 * Manufacturer drivers should put all their specific cleanup code in their
4014 * ->cleanup() hook.
4015 */
4016static void nand_manufacturer_cleanup(struct nand_chip *chip)
4017{
4018 /* Release manufacturer private data */
4019 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4020 chip->manufacturer.desc->ops->cleanup)
4021 chip->manufacturer.desc->ops->cleanup(chip);
4022}
4023
4024/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004025 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004026 */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004027static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004028{
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004029 const struct nand_manufacturer *manufacturer;
Boris Brezilloncbe435a2016-05-24 16:56:22 +02004030 struct mtd_info *mtd = nand_to_mtd(chip);
Cai Zhiyongbb770822013-12-25 20:11:15 +08004031 int busw;
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004032 int i, ret;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004033 u8 *id_data = chip->id.data;
4034 u8 maf_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Karl Beldanef89a882008-09-15 14:37:29 +02004036 /*
4037 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07004038 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02004039 */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004040 nand_reset(chip, 0);
4041
4042 /* Select the device */
4043 chip->select_chip(mtd, 0);
Karl Beldanef89a882008-09-15 14:37:29 +02004044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004046 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 /* Read manufacturer and device IDs */
Boris Brezillon7f501f02016-05-24 19:20:05 +02004049 maf_id = chip->read_byte(mtd);
4050 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Brian Norris8b6e50c2011-05-25 14:59:01 -07004052 /*
4053 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01004054 * interface concerns can cause random data which looks like a
4055 * possibly credible NAND flash to appear. If the two results do
4056 * not match, ignore the device completely.
4057 */
4058
4059 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4060
Brian Norris4aef9b72012-09-24 20:40:48 -07004061 /* Read entire ID string */
4062 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07004063 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01004064
Boris Brezillon7f501f02016-05-24 19:20:05 +02004065 if (id_data[0] != maf_id || id_data[1] != dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03004066 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004067 maf_id, dev_id, id_data[0], id_data[1]);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004068 return -ENODEV;
Ben Dooksed8165c2008-04-14 14:58:58 +01004069 }
4070
Boris Brezillon7f501f02016-05-24 19:20:05 +02004071 chip->id.len = nand_id_len(id_data, 8);
4072
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004073 /* Try to identify manufacturer */
4074 manufacturer = nand_get_manufacturer(maf_id);
4075 chip->manufacturer.desc = manufacturer;
4076
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004077 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00004078 type = nand_flash_ids;
4079
Boris Brezillon29a198a2016-05-24 20:17:48 +02004080 /*
4081 * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic
4082 * override it.
4083 * This is required to make sure initial NAND bus width set by the
4084 * NAND controller driver is coherent with the real NAND bus width
4085 * (extracted by auto-detection code).
4086 */
4087 busw = chip->options & NAND_BUSWIDTH_16;
4088
4089 /*
4090 * The flag is only set (never cleared), reset it to its default value
4091 * before starting auto-detection.
4092 */
4093 chip->options &= ~NAND_BUSWIDTH_16;
4094
Huang Shijieec6e87e2013-03-15 11:01:00 +08004095 for (; type->name != NULL; type++) {
4096 if (is_full_id_nand(type)) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004097 if (find_full_id_nand(chip, type))
Huang Shijieec6e87e2013-03-15 11:01:00 +08004098 goto ident_done;
Boris Brezillon7f501f02016-05-24 19:20:05 +02004099 } else if (dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07004100 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08004101 }
4102 }
David Woodhouse5e81e882010-02-26 18:32:56 +00004103
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004104 chip->onfi_version = 0;
4105 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09004106 /* Check if the chip is ONFI compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004107 if (nand_flash_detect_onfi(chip))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02004108 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08004109
4110 /* Check if the chip is JEDEC compliant */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004111 if (nand_flash_detect_jedec(chip))
Huang Shijie91361812014-02-21 13:39:40 +08004112 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004113 }
4114
David Woodhouse5e81e882010-02-26 18:32:56 +00004115 if (!type->name)
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004116 return -ENODEV;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004117
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02004118 if (!mtd->name)
4119 mtd->name = type->name;
4120
Adrian Hunter69423d92008-12-10 13:37:21 +00004121 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004122
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004123 if (!type->pagesize)
4124 nand_manufacturer_detect(chip);
4125 else
Boris Brezillon29a198a2016-05-24 20:17:48 +02004126 nand_decode_id(chip, type);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004127
Brian Norrisbf7a01b2012-07-13 09:28:24 -07004128 /* Get chip options */
4129 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004130
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02004131ident_done:
4132
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004133 if (chip->options & NAND_BUSWIDTH_AUTO) {
Boris Brezillon29a198a2016-05-24 20:17:48 +02004134 WARN_ON(busw & NAND_BUSWIDTH_16);
4135 nand_set_defaults(chip);
Matthieu CASTET64b37b22012-11-06 11:51:44 +01004136 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4137 /*
4138 * Check, if buswidth is correct. Hardware drivers should set
4139 * chip correct!
4140 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03004141 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004142 maf_id, dev_id);
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004143 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4144 mtd->name);
Boris Brezillon29a198a2016-05-24 20:17:48 +02004145 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4146 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004147 return -EINVAL;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004148 }
4149
Boris Brezillon7f501f02016-05-24 19:20:05 +02004150 nand_decode_bbm_options(chip);
Brian Norris7e74c2d2012-09-24 20:40:49 -07004151
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004152 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004153 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07004154 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004155 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004156
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004157 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004158 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00004159 if (chip->chipsize & 0xffffffff)
4160 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004161 else {
4162 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4163 chip->chip_shift += 32 - 1;
4164 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004165
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03004166 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07004167 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004168
Brian Norris8b6e50c2011-05-25 14:59:01 -07004169 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004170 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4171 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004172
Boris Brezillonabbe26d2016-06-08 09:32:55 +02004173 ret = nand_manufacturer_init(chip);
4174 if (ret)
4175 return ret;
4176
Ezequiel Garcia20171642013-11-25 08:30:31 -03004177 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
Boris Brezillon7f501f02016-05-24 19:20:05 +02004178 maf_id, dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004179
4180 if (chip->onfi_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004181 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4182 chip->onfi_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004183 else if (chip->jedec_version)
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004184 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4185 chip->jedec_params.model);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004186 else
Boris Brezillonbcc678c2017-01-07 15:48:25 +01004187 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4188 type->name);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08004189
Rafał Miłecki3755a992014-10-21 00:01:04 +02004190 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08004191 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02004192 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004193 return 0;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004194}
4195
Boris Brezillond48f62b2016-04-01 14:54:32 +02004196static const char * const nand_ecc_modes[] = {
4197 [NAND_ECC_NONE] = "none",
4198 [NAND_ECC_SOFT] = "soft",
4199 [NAND_ECC_HW] = "hw",
4200 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4201 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004202 [NAND_ECC_ON_DIE] = "on-die",
Boris Brezillond48f62b2016-04-01 14:54:32 +02004203};
4204
4205static int of_get_nand_ecc_mode(struct device_node *np)
4206{
4207 const char *pm;
4208 int err, i;
4209
4210 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4211 if (err < 0)
4212 return err;
4213
4214 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4215 if (!strcasecmp(pm, nand_ecc_modes[i]))
4216 return i;
4217
Rafał Miłeckiae211bc2016-04-17 22:53:06 +02004218 /*
4219 * For backward compatibility we support few obsoleted values that don't
4220 * have their mappings into nand_ecc_modes_t anymore (they were merged
4221 * with other enums).
4222 */
4223 if (!strcasecmp(pm, "soft_bch"))
4224 return NAND_ECC_SOFT;
4225
Boris Brezillond48f62b2016-04-01 14:54:32 +02004226 return -ENODEV;
4227}
4228
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004229static const char * const nand_ecc_algos[] = {
4230 [NAND_ECC_HAMMING] = "hamming",
4231 [NAND_ECC_BCH] = "bch",
4232};
4233
Boris Brezillond48f62b2016-04-01 14:54:32 +02004234static int of_get_nand_ecc_algo(struct device_node *np)
4235{
4236 const char *pm;
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004237 int err, i;
Boris Brezillond48f62b2016-04-01 14:54:32 +02004238
Rafał Miłeckiba4f46b2016-04-22 13:23:13 +02004239 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4240 if (!err) {
4241 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4242 if (!strcasecmp(pm, nand_ecc_algos[i]))
4243 return i;
4244 return -ENODEV;
4245 }
Boris Brezillond48f62b2016-04-01 14:54:32 +02004246
4247 /*
4248 * For backward compatibility we also read "nand-ecc-mode" checking
4249 * for some obsoleted values that were specifying ECC algorithm.
4250 */
4251 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4252 if (err < 0)
4253 return err;
4254
4255 if (!strcasecmp(pm, "soft"))
4256 return NAND_ECC_HAMMING;
4257 else if (!strcasecmp(pm, "soft_bch"))
4258 return NAND_ECC_BCH;
4259
4260 return -ENODEV;
4261}
4262
4263static int of_get_nand_ecc_step_size(struct device_node *np)
4264{
4265 int ret;
4266 u32 val;
4267
4268 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4269 return ret ? ret : val;
4270}
4271
4272static int of_get_nand_ecc_strength(struct device_node *np)
4273{
4274 int ret;
4275 u32 val;
4276
4277 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4278 return ret ? ret : val;
4279}
4280
4281static int of_get_nand_bus_width(struct device_node *np)
4282{
4283 u32 val;
4284
4285 if (of_property_read_u32(np, "nand-bus-width", &val))
4286 return 8;
4287
4288 switch (val) {
4289 case 8:
4290 case 16:
4291 return val;
4292 default:
4293 return -EIO;
4294 }
4295}
4296
4297static bool of_get_nand_on_flash_bbt(struct device_node *np)
4298{
4299 return of_property_read_bool(np, "nand-on-flash-bbt");
4300}
4301
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004302static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08004303{
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004304 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01004305 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08004306
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004307 if (!dn)
4308 return 0;
4309
Brian Norris5844fee2015-01-23 00:22:27 -08004310 if (of_get_nand_bus_width(dn) == 16)
4311 chip->options |= NAND_BUSWIDTH_16;
4312
4313 if (of_get_nand_on_flash_bbt(dn))
4314 chip->bbt_options |= NAND_BBT_USE_FLASH;
4315
4316 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01004317 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08004318 ecc_strength = of_get_nand_ecc_strength(dn);
4319 ecc_step = of_get_nand_ecc_step_size(dn);
4320
Brian Norris5844fee2015-01-23 00:22:27 -08004321 if (ecc_mode >= 0)
4322 chip->ecc.mode = ecc_mode;
4323
Rafał Miłecki79082452016-03-23 11:19:02 +01004324 if (ecc_algo >= 0)
4325 chip->ecc.algo = ecc_algo;
4326
Brian Norris5844fee2015-01-23 00:22:27 -08004327 if (ecc_strength >= 0)
4328 chip->ecc.strength = ecc_strength;
4329
4330 if (ecc_step > 0)
4331 chip->ecc.size = ecc_step;
4332
Boris Brezillonba78ee02016-06-08 17:04:22 +02004333 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4334 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4335
Brian Norris5844fee2015-01-23 00:22:27 -08004336 return 0;
4337}
4338
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004339/**
David Woodhouse3b85c322006-09-25 17:06:53 +01004340 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004341 * @mtd: MTD device structure
4342 * @maxchips: number of chips to scan for
4343 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004344 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004345 * This is the first phase of the normal nand_scan() function. It reads the
4346 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004347 *
4348 */
David Woodhouse5e81e882010-02-26 18:32:56 +00004349int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4350 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004351{
Cai Zhiyongbb770822013-12-25 20:11:15 +08004352 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01004353 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5844fee2015-01-23 00:22:27 -08004354 int ret;
4355
Boris BREZILLON7194a29a2015-12-10 09:00:37 +01004356 ret = nand_dt_init(chip);
4357 if (ret)
4358 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004359
Brian Norrisf7a8e382016-01-05 10:39:45 -08004360 if (!mtd->name && mtd->dev.parent)
4361 mtd->name = dev_name(mtd->dev.parent);
4362
Andrey Smirnov76fe3342016-07-21 14:59:20 -07004363 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4364 /*
4365 * Default functions assigned for chip_select() and
4366 * cmdfunc() both expect cmd_ctrl() to be populated,
4367 * so we need to check that that's the case
4368 */
4369 pr_err("chip.cmd_ctrl() callback is not provided");
4370 return -EINVAL;
4371 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004372 /* Set the default functions */
Boris Brezillon29a198a2016-05-24 20:17:48 +02004373 nand_set_defaults(chip);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004374
4375 /* Read the flash type */
Boris Brezillon7bb42792016-05-24 20:55:33 +02004376 ret = nand_detect(chip, table);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004377 if (ret) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004378 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004379 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004380 chip->select_chip(mtd, -1);
Masahiro Yamada4722c0e2016-11-04 17:49:08 +09004381 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 }
4383
Boris Brezillon73f907f2016-10-24 16:46:20 +02004384 /* Initialize the ->data_interface field. */
Boris Brezillond8e725d2016-09-15 10:32:50 +02004385 ret = nand_init_data_interface(chip);
4386 if (ret)
Brian Norris78771042017-05-01 17:04:53 -07004387 goto err_nand_init;
Boris Brezillond8e725d2016-09-15 10:32:50 +02004388
Boris Brezillon73f907f2016-10-24 16:46:20 +02004389 /*
4390 * Setup the data interface correctly on the chip and controller side.
4391 * This explicit call to nand_setup_data_interface() is only required
4392 * for the first die, because nand_reset() has been called before
4393 * ->data_interface and ->default_onfi_timing_mode were set.
4394 * For the other dies, nand_reset() will automatically switch to the
4395 * best mode for us.
4396 */
Boris Brezillon104e4422017-03-16 09:35:58 +01004397 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon73f907f2016-10-24 16:46:20 +02004398 if (ret)
Brian Norris78771042017-05-01 17:04:53 -07004399 goto err_nand_init;
Boris Brezillon73f907f2016-10-24 16:46:20 +02004400
Boris Brezillon7f501f02016-05-24 19:20:05 +02004401 nand_maf_id = chip->id.data[0];
4402 nand_dev_id = chip->id.data[1];
4403
Huang Shijie07300162012-11-09 16:23:45 +08004404 chip->select_chip(mtd, -1);
4405
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004406 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004407 for (i = 1; i < maxchips; i++) {
Karl Beldanef89a882008-09-15 14:37:29 +02004408 /* See comment in nand_get_flash_type for reset */
Boris Brezillon73f907f2016-10-24 16:46:20 +02004409 nand_reset(chip, i);
4410
4411 chip->select_chip(mtd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004413 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004415 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004416 nand_dev_id != chip->read_byte(mtd)) {
4417 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 break;
Huang Shijie07300162012-11-09 16:23:45 +08004419 }
4420 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 }
4422 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004423 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004426 chip->numchips = i;
4427 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
David Woodhouse3b85c322006-09-25 17:06:53 +01004429 return 0;
Brian Norris78771042017-05-01 17:04:53 -07004430
4431err_nand_init:
4432 /* Free manufacturer priv data. */
4433 nand_manufacturer_cleanup(chip);
4434
4435 return ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004436}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004437EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004438
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004439static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4440{
4441 struct nand_chip *chip = mtd_to_nand(mtd);
4442 struct nand_ecc_ctrl *ecc = &chip->ecc;
4443
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004444 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004445 return -EINVAL;
4446
4447 switch (ecc->algo) {
4448 case NAND_ECC_HAMMING:
4449 ecc->calculate = nand_calculate_ecc;
4450 ecc->correct = nand_correct_data;
4451 ecc->read_page = nand_read_page_swecc;
4452 ecc->read_subpage = nand_read_subpage;
4453 ecc->write_page = nand_write_page_swecc;
4454 ecc->read_page_raw = nand_read_page_raw;
4455 ecc->write_page_raw = nand_write_page_raw;
4456 ecc->read_oob = nand_read_oob_std;
4457 ecc->write_oob = nand_write_oob_std;
4458 if (!ecc->size)
4459 ecc->size = 256;
4460 ecc->bytes = 3;
4461 ecc->strength = 1;
4462 return 0;
4463 case NAND_ECC_BCH:
4464 if (!mtd_nand_has_bch()) {
4465 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4466 return -EINVAL;
4467 }
4468 ecc->calculate = nand_bch_calculate_ecc;
4469 ecc->correct = nand_bch_correct_data;
4470 ecc->read_page = nand_read_page_swecc;
4471 ecc->read_subpage = nand_read_subpage;
4472 ecc->write_page = nand_write_page_swecc;
4473 ecc->read_page_raw = nand_read_page_raw;
4474 ecc->write_page_raw = nand_write_page_raw;
4475 ecc->read_oob = nand_read_oob_std;
4476 ecc->write_oob = nand_write_oob_std;
Boris Brezillon8bbba482016-06-08 17:04:23 +02004477
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004478 /*
4479 * Board driver should supply ecc.size and ecc.strength
4480 * values to select how many bits are correctable.
4481 * Otherwise, default to 4 bits for large page devices.
4482 */
4483 if (!ecc->size && (mtd->oobsize >= 64)) {
4484 ecc->size = 512;
4485 ecc->strength = 4;
4486 }
4487
4488 /*
4489 * if no ecc placement scheme was provided pickup the default
4490 * large page one.
4491 */
4492 if (!mtd->ooblayout) {
4493 /* handle large page devices only */
4494 if (mtd->oobsize < 64) {
4495 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4496 return -EINVAL;
4497 }
4498
4499 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Boris Brezillon8bbba482016-06-08 17:04:23 +02004500
4501 }
4502
4503 /*
4504 * We can only maximize ECC config when the default layout is
4505 * used, otherwise we don't know how many bytes can really be
4506 * used.
4507 */
4508 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4509 ecc->options & NAND_ECC_MAXIMIZE) {
4510 int steps, bytes;
4511
4512 /* Always prefer 1k blocks over 512bytes ones */
4513 ecc->size = 1024;
4514 steps = mtd->writesize / ecc->size;
4515
4516 /* Reserve 2 bytes for the BBM */
4517 bytes = (mtd->oobsize - 2) / steps;
4518 ecc->strength = bytes * 8 / fls(8 * ecc->size);
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004519 }
4520
4521 /* See nand_bch_init() for details. */
4522 ecc->bytes = 0;
4523 ecc->priv = nand_bch_init(mtd);
4524 if (!ecc->priv) {
4525 WARN(1, "BCH ECC initialization failed!\n");
4526 return -EINVAL;
4527 }
4528 return 0;
4529 default:
4530 WARN(1, "Unsupported ECC algorithm!\n");
4531 return -EINVAL;
4532 }
4533}
4534
Masahiro Yamada2c8f8af2017-06-07 20:52:10 +09004535/**
4536 * nand_check_ecc_caps - check the sanity of preset ECC settings
4537 * @chip: nand chip info structure
4538 * @caps: ECC caps info structure
4539 * @oobavail: OOB size that the ECC engine can use
4540 *
4541 * When ECC step size and strength are already set, check if they are supported
4542 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4543 * On success, the calculated ECC bytes is set.
4544 */
4545int nand_check_ecc_caps(struct nand_chip *chip,
4546 const struct nand_ecc_caps *caps, int oobavail)
4547{
4548 struct mtd_info *mtd = nand_to_mtd(chip);
4549 const struct nand_ecc_step_info *stepinfo;
4550 int preset_step = chip->ecc.size;
4551 int preset_strength = chip->ecc.strength;
4552 int nsteps, ecc_bytes;
4553 int i, j;
4554
4555 if (WARN_ON(oobavail < 0))
4556 return -EINVAL;
4557
4558 if (!preset_step || !preset_strength)
4559 return -ENODATA;
4560
4561 nsteps = mtd->writesize / preset_step;
4562
4563 for (i = 0; i < caps->nstepinfos; i++) {
4564 stepinfo = &caps->stepinfos[i];
4565
4566 if (stepinfo->stepsize != preset_step)
4567 continue;
4568
4569 for (j = 0; j < stepinfo->nstrengths; j++) {
4570 if (stepinfo->strengths[j] != preset_strength)
4571 continue;
4572
4573 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4574 preset_strength);
4575 if (WARN_ON_ONCE(ecc_bytes < 0))
4576 return ecc_bytes;
4577
4578 if (ecc_bytes * nsteps > oobavail) {
4579 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4580 preset_step, preset_strength);
4581 return -ENOSPC;
4582 }
4583
4584 chip->ecc.bytes = ecc_bytes;
4585
4586 return 0;
4587 }
4588 }
4589
4590 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4591 preset_step, preset_strength);
4592
4593 return -ENOTSUPP;
4594}
4595EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4596
4597/**
4598 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4599 * @chip: nand chip info structure
4600 * @caps: ECC engine caps info structure
4601 * @oobavail: OOB size that the ECC engine can use
4602 *
4603 * If a chip's ECC requirement is provided, try to meet it with the least
4604 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4605 * On success, the chosen ECC settings are set.
4606 */
4607int nand_match_ecc_req(struct nand_chip *chip,
4608 const struct nand_ecc_caps *caps, int oobavail)
4609{
4610 struct mtd_info *mtd = nand_to_mtd(chip);
4611 const struct nand_ecc_step_info *stepinfo;
4612 int req_step = chip->ecc_step_ds;
4613 int req_strength = chip->ecc_strength_ds;
4614 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4615 int best_step, best_strength, best_ecc_bytes;
4616 int best_ecc_bytes_total = INT_MAX;
4617 int i, j;
4618
4619 if (WARN_ON(oobavail < 0))
4620 return -EINVAL;
4621
4622 /* No information provided by the NAND chip */
4623 if (!req_step || !req_strength)
4624 return -ENOTSUPP;
4625
4626 /* number of correctable bits the chip requires in a page */
4627 req_corr = mtd->writesize / req_step * req_strength;
4628
4629 for (i = 0; i < caps->nstepinfos; i++) {
4630 stepinfo = &caps->stepinfos[i];
4631 step_size = stepinfo->stepsize;
4632
4633 for (j = 0; j < stepinfo->nstrengths; j++) {
4634 strength = stepinfo->strengths[j];
4635
4636 /*
4637 * If both step size and strength are smaller than the
4638 * chip's requirement, it is not easy to compare the
4639 * resulted reliability.
4640 */
4641 if (step_size < req_step && strength < req_strength)
4642 continue;
4643
4644 if (mtd->writesize % step_size)
4645 continue;
4646
4647 nsteps = mtd->writesize / step_size;
4648
4649 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4650 if (WARN_ON_ONCE(ecc_bytes < 0))
4651 continue;
4652 ecc_bytes_total = ecc_bytes * nsteps;
4653
4654 if (ecc_bytes_total > oobavail ||
4655 strength * nsteps < req_corr)
4656 continue;
4657
4658 /*
4659 * We assume the best is to meet the chip's requrement
4660 * with the least number of ECC bytes.
4661 */
4662 if (ecc_bytes_total < best_ecc_bytes_total) {
4663 best_ecc_bytes_total = ecc_bytes_total;
4664 best_step = step_size;
4665 best_strength = strength;
4666 best_ecc_bytes = ecc_bytes;
4667 }
4668 }
4669 }
4670
4671 if (best_ecc_bytes_total == INT_MAX)
4672 return -ENOTSUPP;
4673
4674 chip->ecc.size = best_step;
4675 chip->ecc.strength = best_strength;
4676 chip->ecc.bytes = best_ecc_bytes;
4677
4678 return 0;
4679}
4680EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4681
4682/**
4683 * nand_maximize_ecc - choose the max ECC strength available
4684 * @chip: nand chip info structure
4685 * @caps: ECC engine caps info structure
4686 * @oobavail: OOB size that the ECC engine can use
4687 *
4688 * Choose the max ECC strength that is supported on the controller, and can fit
4689 * within the chip's OOB. On success, the chosen ECC settings are set.
4690 */
4691int nand_maximize_ecc(struct nand_chip *chip,
4692 const struct nand_ecc_caps *caps, int oobavail)
4693{
4694 struct mtd_info *mtd = nand_to_mtd(chip);
4695 const struct nand_ecc_step_info *stepinfo;
4696 int step_size, strength, nsteps, ecc_bytes, corr;
4697 int best_corr = 0;
4698 int best_step = 0;
4699 int best_strength, best_ecc_bytes;
4700 int i, j;
4701
4702 if (WARN_ON(oobavail < 0))
4703 return -EINVAL;
4704
4705 for (i = 0; i < caps->nstepinfos; i++) {
4706 stepinfo = &caps->stepinfos[i];
4707 step_size = stepinfo->stepsize;
4708
4709 /* If chip->ecc.size is already set, respect it */
4710 if (chip->ecc.size && step_size != chip->ecc.size)
4711 continue;
4712
4713 for (j = 0; j < stepinfo->nstrengths; j++) {
4714 strength = stepinfo->strengths[j];
4715
4716 if (mtd->writesize % step_size)
4717 continue;
4718
4719 nsteps = mtd->writesize / step_size;
4720
4721 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4722 if (WARN_ON_ONCE(ecc_bytes < 0))
4723 continue;
4724
4725 if (ecc_bytes * nsteps > oobavail)
4726 continue;
4727
4728 corr = strength * nsteps;
4729
4730 /*
4731 * If the number of correctable bits is the same,
4732 * bigger step_size has more reliability.
4733 */
4734 if (corr > best_corr ||
4735 (corr == best_corr && step_size > best_step)) {
4736 best_corr = corr;
4737 best_step = step_size;
4738 best_strength = strength;
4739 best_ecc_bytes = ecc_bytes;
4740 }
4741 }
4742 }
4743
4744 if (!best_corr)
4745 return -ENOTSUPP;
4746
4747 chip->ecc.size = best_step;
4748 chip->ecc.strength = best_strength;
4749 chip->ecc.bytes = best_ecc_bytes;
4750
4751 return 0;
4752}
4753EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4754
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004755/*
4756 * Check if the chip configuration meet the datasheet requirements.
4757
4758 * If our configuration corrects A bits per B bytes and the minimum
4759 * required correction level is X bits per Y bytes, then we must ensure
4760 * both of the following are true:
4761 *
4762 * (1) A / B >= X / Y
4763 * (2) A >= X
4764 *
4765 * Requirement (1) ensures we can correct for the required bitflip density.
4766 * Requirement (2) ensures we can correct even when all bitflips are clumped
4767 * in the same sector.
4768 */
4769static bool nand_ecc_strength_good(struct mtd_info *mtd)
4770{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004771 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004772 struct nand_ecc_ctrl *ecc = &chip->ecc;
4773 int corr, ds_corr;
4774
4775 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4776 /* Not enough information */
4777 return true;
4778
4779 /*
4780 * We get the number of corrected bits per page to compare
4781 * the correction density.
4782 */
4783 corr = (mtd->writesize * ecc->strength) / ecc->size;
4784 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4785
4786 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4787}
David Woodhouse3b85c322006-09-25 17:06:53 +01004788
Marc Gonzalez3371d662016-11-15 10:56:20 +01004789static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4790{
4791 struct nand_ecc_ctrl *ecc = &chip->ecc;
4792
4793 if (nand_standard_page_accessors(ecc))
4794 return false;
4795
4796 /*
4797 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4798 * controller driver implements all the page accessors because
4799 * default helpers are not suitable when the core does not
4800 * send the READ0/PAGEPROG commands.
4801 */
4802 return (!ecc->read_page || !ecc->write_page ||
4803 !ecc->read_page_raw || !ecc->write_page_raw ||
4804 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4805 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4806 ecc->hwctl && ecc->calculate));
4807}
4808
David Woodhouse3b85c322006-09-25 17:06:53 +01004809/**
4810 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004811 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004812 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004813 * This is the second phase of the normal nand_scan() function. It fills out
4814 * all the uninitialized function pointers with the defaults and scans for a
4815 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004816 */
4817int nand_scan_tail(struct mtd_info *mtd)
4818{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004819 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004820 struct nand_ecc_ctrl *ecc = &chip->ecc;
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004821 struct nand_buffers *nbuf = NULL;
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004822 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004823
Brian Norrise2414f42012-02-06 13:44:00 -08004824 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004825 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
Brian Norris78771042017-05-01 17:04:53 -07004826 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
4827 ret = -EINVAL;
4828 goto err_ident;
4829 }
Brian Norrise2414f42012-02-06 13:44:00 -08004830
Marc Gonzalez3371d662016-11-15 10:56:20 +01004831 if (invalid_ecc_page_accessors(chip)) {
4832 pr_err("Invalid ECC page accessors setup\n");
Brian Norris78771042017-05-01 17:04:53 -07004833 ret = -EINVAL;
4834 goto err_ident;
Marc Gonzalez3371d662016-11-15 10:56:20 +01004835 }
4836
Huang Shijief02ea4e2014-01-13 14:27:12 +08004837 if (!(chip->options & NAND_OWN_BUFFERS)) {
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004838 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
Brian Norris78771042017-05-01 17:04:53 -07004839 if (!nbuf) {
4840 ret = -ENOMEM;
4841 goto err_ident;
4842 }
Masahiro Yamada3deb9972017-03-30 17:15:04 +09004843
4844 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4845 if (!nbuf->ecccalc) {
4846 ret = -ENOMEM;
4847 goto err_free;
4848 }
4849
4850 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4851 if (!nbuf->ecccode) {
4852 ret = -ENOMEM;
4853 goto err_free;
4854 }
4855
4856 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4857 GFP_KERNEL);
4858 if (!nbuf->databuf) {
4859 ret = -ENOMEM;
4860 goto err_free;
4861 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004862
4863 chip->buffers = nbuf;
4864 } else {
Brian Norris78771042017-05-01 17:04:53 -07004865 if (!chip->buffers) {
4866 ret = -ENOMEM;
4867 goto err_ident;
4868 }
Huang Shijief02ea4e2014-01-13 14:27:12 +08004869 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004870
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004871 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004872 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004873
4874 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004875 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004876 */
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004877 if (!mtd->ooblayout &&
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02004878 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004879 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 case 8:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 case 16:
Boris Brezillon41b207a2016-02-03 19:06:15 +01004882 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 break;
4884 case 64:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004885 case 128:
Alexander Couzens6a623e02017-05-02 12:19:00 +02004886 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004889 WARN(1, "No oob scheme defined for oobsize %d\n",
4890 mtd->oobsize);
4891 ret = -EINVAL;
4892 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
4894 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004895
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004896 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004897 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004898 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004899 */
David Woodhouse956e9442006-09-25 17:12:39 +01004900
Huang Shijie97de79e02013-10-18 14:20:53 +08004901 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004902 case NAND_ECC_HW_OOB_FIRST:
4903 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004904 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004905 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4906 ret = -EINVAL;
4907 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004908 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004909 if (!ecc->read_page)
4910 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004911
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004912 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004913 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004914 if (!ecc->read_page)
4915 ecc->read_page = nand_read_page_hwecc;
4916 if (!ecc->write_page)
4917 ecc->write_page = nand_write_page_hwecc;
4918 if (!ecc->read_page_raw)
4919 ecc->read_page_raw = nand_read_page_raw;
4920 if (!ecc->write_page_raw)
4921 ecc->write_page_raw = nand_write_page_raw;
4922 if (!ecc->read_oob)
4923 ecc->read_oob = nand_read_oob_std;
4924 if (!ecc->write_oob)
4925 ecc->write_oob = nand_write_oob_std;
4926 if (!ecc->read_subpage)
4927 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004928 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004929 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004930
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004931 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004932 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4933 (!ecc->read_page ||
4934 ecc->read_page == nand_read_page_hwecc ||
4935 !ecc->write_page ||
4936 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004937 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4938 ret = -EINVAL;
4939 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004940 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004941 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004942 if (!ecc->read_page)
4943 ecc->read_page = nand_read_page_syndrome;
4944 if (!ecc->write_page)
4945 ecc->write_page = nand_write_page_syndrome;
4946 if (!ecc->read_page_raw)
4947 ecc->read_page_raw = nand_read_page_raw_syndrome;
4948 if (!ecc->write_page_raw)
4949 ecc->write_page_raw = nand_write_page_raw_syndrome;
4950 if (!ecc->read_oob)
4951 ecc->read_oob = nand_read_oob_syndrome;
4952 if (!ecc->write_oob)
4953 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004954
Huang Shijie97de79e02013-10-18 14:20:53 +08004955 if (mtd->writesize >= ecc->size) {
4956 if (!ecc->strength) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004957 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4958 ret = -EINVAL;
4959 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004960 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004961 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004962 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004963 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4964 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004965 ecc->mode = NAND_ECC_SOFT;
Rafał Miłeckie9d4fae2016-04-17 22:53:02 +02004966 ecc->algo = NAND_ECC_HAMMING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004968 case NAND_ECC_SOFT:
Rafał Miłecki06f384c2016-04-17 22:53:05 +02004969 ret = nand_set_ecc_soft_ops(mtd);
4970 if (ret) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03004971 ret = -EINVAL;
4972 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004973 }
4974 break;
4975
Thomas Petazzoni785818f2017-04-29 11:06:43 +02004976 case NAND_ECC_ON_DIE:
4977 if (!ecc->read_page || !ecc->write_page) {
4978 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4979 ret = -EINVAL;
4980 goto err_free;
4981 }
4982 if (!ecc->read_oob)
4983 ecc->read_oob = nand_read_oob_std;
4984 if (!ecc->write_oob)
4985 ecc->write_oob = nand_write_oob_std;
4986 break;
4987
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004988 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004989 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004990 ecc->read_page = nand_read_page_raw;
4991 ecc->write_page = nand_write_page_raw;
4992 ecc->read_oob = nand_read_oob_std;
4993 ecc->read_page_raw = nand_read_page_raw;
4994 ecc->write_page_raw = nand_write_page_raw;
4995 ecc->write_oob = nand_write_oob_std;
4996 ecc->size = mtd->writesize;
4997 ecc->bytes = 0;
4998 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 break;
David Woodhouse956e9442006-09-25 17:12:39 +01005000
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 default:
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005002 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5003 ret = -EINVAL;
5004 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Brian Norris9ce244b2011-08-30 18:45:37 -07005007 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08005008 if (!ecc->read_oob_raw)
5009 ecc->read_oob_raw = ecc->read_oob;
5010 if (!ecc->write_oob_raw)
5011 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07005012
Boris Brezillon846031d2016-02-03 20:11:00 +01005013 /* propagate ecc info to mtd_info */
Boris Brezillon846031d2016-02-03 20:11:00 +01005014 mtd->ecc_strength = ecc->strength;
5015 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03005016
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02005017 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005018 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07005019 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02005020 */
Huang Shijie97de79e02013-10-18 14:20:53 +08005021 ecc->steps = mtd->writesize / ecc->size;
5022 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005023 WARN(1, "Invalid ECC parameters\n");
5024 ret = -EINVAL;
5025 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 }
Huang Shijie97de79e02013-10-18 14:20:53 +08005027 ecc->total = ecc->steps * ecc->bytes;
Masahiro Yamada79e03482017-05-25 13:50:20 +09005028 if (ecc->total > mtd->oobsize) {
5029 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5030 ret = -EINVAL;
5031 goto err_free;
5032 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005033
Boris Brezillon846031d2016-02-03 20:11:00 +01005034 /*
5035 * The number of bytes available for a client to place data into
5036 * the out of band area.
5037 */
5038 ret = mtd_ooblayout_count_freebytes(mtd);
5039 if (ret < 0)
5040 ret = 0;
5041
5042 mtd->oobavail = ret;
5043
5044 /* ECC sanity check: warn if it's too weak */
5045 if (!nand_ecc_strength_good(mtd))
5046 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5047 mtd->name);
5048
Brian Norris8b6e50c2011-05-25 14:59:01 -07005049 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08005050 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08005051 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02005052 case 2:
5053 mtd->subpage_sft = 1;
5054 break;
5055 case 4:
5056 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01005057 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02005058 mtd->subpage_sft = 2;
5059 break;
5060 }
5061 }
5062 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5063
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02005064 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005065 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005068 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005070 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09305071 switch (ecc->mode) {
5072 case NAND_ECC_SOFT:
Ron Lee4007e2d2014-04-25 15:01:35 +09305073 if (chip->page_shift > 9)
5074 chip->options |= NAND_SUBPAGE_READ;
5075 break;
5076
5077 default:
5078 break;
5079 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05005080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08005082 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02005083 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5084 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005085 mtd->_erase = nand_erase;
5086 mtd->_point = NULL;
5087 mtd->_unpoint = NULL;
5088 mtd->_read = nand_read;
5089 mtd->_write = nand_write;
5090 mtd->_panic_write = panic_nand_write;
5091 mtd->_read_oob = nand_read_oob;
5092 mtd->_write_oob = nand_write_oob;
5093 mtd->_sync = nand_sync;
5094 mtd->_lock = NULL;
5095 mtd->_unlock = NULL;
5096 mtd->_suspend = nand_suspend;
5097 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08005098 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03005099 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02005100 mtd->_block_isbad = nand_block_isbad;
5101 mtd->_block_markbad = nand_block_markbad;
Zach Brown56718422017-01-10 13:30:20 -06005102 mtd->_max_bad_blocks = nand_max_bad_blocks;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01005103 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03005105 /*
5106 * Initialize bitflip_threshold to its default prior scan_bbt() call.
5107 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
5108 * properly set.
5109 */
5110 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08005111 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005113 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005114 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00005115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
5117 /* Build bad block table */
Brian Norris44d41822017-05-01 17:04:50 -07005118 ret = chip->scan_bbt(mtd);
5119 if (ret)
5120 goto err_free;
5121 return 0;
5122
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005123err_free:
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005124 if (nbuf) {
5125 kfree(nbuf->databuf);
5126 kfree(nbuf->ecccode);
5127 kfree(nbuf->ecccalc);
5128 kfree(nbuf);
5129 }
Brian Norris78771042017-05-01 17:04:53 -07005130
5131err_ident:
5132 /* Clean up nand_scan_ident(). */
5133
5134 /* Free manufacturer priv data. */
5135 nand_manufacturer_cleanup(chip);
5136
Ezequiel García11eaf6d2016-04-01 18:29:24 -03005137 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005139EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Brian Norris8b6e50c2011-05-25 14:59:01 -07005141/*
5142 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005143 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07005144 * to call us from in-kernel code if the core NAND support is modular.
5145 */
David Woodhouse3b85c322006-09-25 17:06:53 +01005146#ifdef MODULE
5147#define caller_is_module() (1)
5148#else
5149#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06005150 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01005151#endif
5152
5153/**
5154 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07005155 * @mtd: MTD device structure
5156 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01005157 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07005158 * This fills out all the uninitialized function pointers with the defaults.
5159 * The flash ID is read and the mtd/chip structures are filled with the
Ezequiel García20c07a52016-04-01 18:29:23 -03005160 * appropriate values.
David Woodhouse3b85c322006-09-25 17:06:53 +01005161 */
5162int nand_scan(struct mtd_info *mtd, int maxchips)
5163{
5164 int ret;
5165
David Woodhouse5e81e882010-02-26 18:32:56 +00005166 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01005167 if (!ret)
5168 ret = nand_scan_tail(mtd);
5169 return ret;
5170}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005171EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01005172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173/**
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005174 * nand_cleanup - [NAND Interface] Free resources held by the NAND device
5175 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07005176 */
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005177void nand_cleanup(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
Rafał Miłeckie4225ae2016-04-17 22:53:07 +02005179 if (chip->ecc.mode == NAND_ECC_SOFT &&
Rafał Miłecki06f384c2016-04-17 22:53:05 +02005180 chip->ecc.algo == NAND_ECC_BCH)
Ivan Djelic193bd402011-03-11 11:05:33 +01005181 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5182
Boris Brezillond8e725d2016-09-15 10:32:50 +02005183 nand_release_data_interface(chip);
5184
Jesper Juhlfa671642005-11-07 01:01:27 -08005185 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02005186 kfree(chip->bbt);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005187 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5188 kfree(chip->buffers->databuf);
5189 kfree(chip->buffers->ecccode);
5190 kfree(chip->buffers->ecccalc);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01005191 kfree(chip->buffers);
Masahiro Yamada3deb9972017-03-30 17:15:04 +09005192 }
Brian Norris58373ff2010-07-15 12:15:44 -07005193
5194 /* Free bad block descriptor memory */
5195 if (chip->badblock_pattern && chip->badblock_pattern->options
5196 & NAND_BBT_DYNAMICSTRUCT)
5197 kfree(chip->badblock_pattern);
Boris Brezillonabbe26d2016-06-08 09:32:55 +02005198
5199 /* Free manufacturer priv data. */
5200 nand_manufacturer_cleanup(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201}
Richard Weinbergerd44154f2016-09-21 11:44:41 +02005202EXPORT_SYMBOL_GPL(nand_cleanup);
5203
5204/**
5205 * nand_release - [NAND Interface] Unregister the MTD device and free resources
5206 * held by the NAND device
5207 * @mtd: MTD device structure
5208 */
5209void nand_release(struct mtd_info *mtd)
5210{
5211 mtd_device_unregister(mtd);
5212 nand_cleanup(mtd_to_nand(mtd));
5213}
David Woodhousee0c7d762006-05-13 18:07:53 +01005214EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08005215
David Woodhousee0c7d762006-05-13 18:07:53 +01005216MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02005217MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5218MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01005219MODULE_DESCRIPTION("Generic NAND flash driver code");