blob: 376976d5795bbdb95e5e680dbe7a8e8f818fa0ef [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020011 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020014 *
William Juul52c07962007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
William Juul52c07962007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapin3a38a552013-01-14 03:46:50 +000024 * if we have HW ECC support.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020025 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Scott Wood3628f002008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020028 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020035#include <common.h>
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +010036
William Juul52c07962007-10-31 13:53:06 +010037#define ENOTSUPP 524 /* Operation is not supported */
38
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020039#include <malloc.h>
40#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010041#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000042#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020043#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020046#include <linux/mtd/nand_bch.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020047
Stefan Roesefa252ea2009-04-24 15:58:33 +020048#ifdef CONFIG_MTD_PARTITIONS
49#include <linux/mtd/partitions.h>
50#endif
51
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020052#include <asm/io.h>
53#include <asm/errno.h>
54
Peter Tyserf9f36222009-02-04 13:47:22 -060055/*
56 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
57 * a flash. NAND flash is initialized prior to interrupts so standard timers
58 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
59 * which is greater than (max NAND reset time / NAND status read time).
60 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
61 */
62#ifndef CONFIG_SYS_NAND_RESET_CNT
63#define CONFIG_SYS_NAND_RESET_CNT 200000
64#endif
65
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020066/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010067static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020068 .eccbytes = 3,
69 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010070 .oobfree = {
71 {.offset = 3,
72 .length = 2},
73 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020074 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020075};
76
William Juul52c07962007-10-31 13:53:06 +010077static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020078 .eccbytes = 6,
79 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010080 .oobfree = {
81 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020082 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020083};
84
William Juul52c07962007-10-31 13:53:06 +010085static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020086 .eccbytes = 24,
87 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010088 40, 41, 42, 43, 44, 45, 46, 47,
89 48, 49, 50, 51, 52, 53, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63},
91 .oobfree = {
92 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020093 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094};
95
William Juul52c07962007-10-31 13:53:06 +010096static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020097 .eccbytes = 48,
98 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020099 80, 81, 82, 83, 84, 85, 86, 87,
100 88, 89, 90, 91, 92, 93, 94, 95,
101 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +0100102 104, 105, 106, 107, 108, 109, 110, 111,
103 112, 113, 114, 115, 116, 117, 118, 119,
104 120, 121, 122, 123, 124, 125, 126, 127},
105 .oobfree = {
106 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +0200107 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200108};
109
William Juul52c07962007-10-31 13:53:06 +0100110static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
111 int new_state);
112
113static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
114 struct mtd_oob_ops *ops);
115
116static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200117
Christian Hitzb8a6b372011-10-12 09:32:02 +0200118static int check_offs_len(struct mtd_info *mtd,
119 loff_t ofs, uint64_t len)
120{
121 struct nand_chip *chip = mtd->priv;
122 int ret = 0;
123
124 /* Start address must align on block boundary */
125 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
126 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
127 ret = -EINVAL;
128 }
129
130 /* Length must align on block boundary */
131 if (len & ((1 << chip->phys_erase_shift) - 1)) {
132 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
133 __func__);
134 ret = -EINVAL;
135 }
136
Christian Hitzb8a6b372011-10-12 09:32:02 +0200137 return ret;
138}
139
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200140/**
141 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000142 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200143 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000144 * Deselect, release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200146static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100147{
Christian Hitzb8a6b372011-10-12 09:32:02 +0200148 struct nand_chip *chip = mtd->priv;
149
150 /* De-select the NAND device */
151 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100152}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200153
154/**
155 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000156 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200157 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000158 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200159 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000160uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161{
William Juul52c07962007-10-31 13:53:06 +0100162 struct nand_chip *chip = mtd->priv;
163 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200164}
165
166/**
167 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000168 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
169 * @mtd: MTD device structure
170 *
171 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200172 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200173 */
William Juul52c07962007-10-31 13:53:06 +0100174static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200175{
William Juul52c07962007-10-31 13:53:06 +0100176 struct nand_chip *chip = mtd->priv;
177 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200178}
179
180/**
181 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000182 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200183 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000184 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200185 */
186static u16 nand_read_word(struct mtd_info *mtd)
187{
William Juul52c07962007-10-31 13:53:06 +0100188 struct nand_chip *chip = mtd->priv;
189 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200190}
191
192/**
193 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000194 * @mtd: MTD device structure
195 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200196 *
197 * Default select function for 1 chip devices.
198 */
William Juul52c07962007-10-31 13:53:06 +0100199static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200200{
William Juul52c07962007-10-31 13:53:06 +0100201 struct nand_chip *chip = mtd->priv;
202
203 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200204 case -1:
William Juul52c07962007-10-31 13:53:06 +0100205 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200206 break;
207 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200208 break;
209
210 default:
211 BUG();
212 }
213}
214
215/**
216 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000217 * @mtd: MTD device structure
218 * @buf: data buffer
219 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200220 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000221 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200222 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000223void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200224{
225 int i;
William Juul52c07962007-10-31 13:53:06 +0100226 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200227
William Juul52c07962007-10-31 13:53:06 +0100228 for (i = 0; i < len; i++)
229 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200230}
231
232/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200233 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000234 * @mtd: MTD device structure
235 * @buf: buffer to store date
236 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200237 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000238 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200239 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400240void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200241{
242 int i;
William Juul52c07962007-10-31 13:53:06 +0100243 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200244
William Juul52c07962007-10-31 13:53:06 +0100245 for (i = 0; i < len; i++)
246 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200247}
248
249/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200250 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200254 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000255 * Default verify function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200256 */
William Juul52c07962007-10-31 13:53:06 +0100257static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200258{
259 int i;
William Juul52c07962007-10-31 13:53:06 +0100260 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200261
William Juul52c07962007-10-31 13:53:06 +0100262 for (i = 0; i < len; i++)
263 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200264 return -EFAULT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200265 return 0;
266}
267
268/**
269 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000270 * @mtd: MTD device structure
271 * @buf: data buffer
272 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200273 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000274 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200275 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000276void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200277{
278 int i;
William Juul52c07962007-10-31 13:53:06 +0100279 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200280 u16 *p = (u16 *) buf;
281 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200282
William Juul52c07962007-10-31 13:53:06 +0100283 for (i = 0; i < len; i++)
284 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200285
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286}
287
288/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200289 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000290 * @mtd: MTD device structure
291 * @buf: buffer to store date
292 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200293 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000294 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200295 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400296void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200297{
298 int i;
William Juul52c07962007-10-31 13:53:06 +0100299 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200300 u16 *p = (u16 *) buf;
301 len >>= 1;
302
William Juul52c07962007-10-31 13:53:06 +0100303 for (i = 0; i < len; i++)
304 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305}
306
307/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200308 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000309 * @mtd: MTD device structure
310 * @buf: buffer containing the data to compare
311 * @len: number of bytes to compare
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000313 * Default verify function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200314 */
William Juul52c07962007-10-31 13:53:06 +0100315static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316{
317 int i;
William Juul52c07962007-10-31 13:53:06 +0100318 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200319 u16 *p = (u16 *) buf;
320 len >>= 1;
321
William Juul52c07962007-10-31 13:53:06 +0100322 for (i = 0; i < len; i++)
323 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200324 return -EFAULT;
325
326 return 0;
327}
328
329/**
330 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000331 * @mtd: MTD device structure
332 * @ofs: offset from device start
333 * @getchip: 0, if the chip is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200335 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336 */
337static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
338{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000339 int page, chipnr, res = 0, i = 0;
William Juul52c07962007-10-31 13:53:06 +0100340 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341 u16 bad;
342
Sergey Lapin3a38a552013-01-14 03:46:50 +0000343 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200344 ofs += mtd->erasesize - mtd->writesize;
345
William Juul52c07962007-10-31 13:53:06 +0100346 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200347
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200348 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100349 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350
William Juul52c07962007-10-31 13:53:06 +0100351 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200352
353 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100354 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200355 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200356
Sergey Lapin3a38a552013-01-14 03:46:50 +0000357 do {
358 if (chip->options & NAND_BUSWIDTH_16) {
359 chip->cmdfunc(mtd, NAND_CMD_READOOB,
360 chip->badblockpos & 0xFE, page);
361 bad = cpu_to_le16(chip->read_word(mtd));
362 if (chip->badblockpos & 0x1)
363 bad >>= 8;
364 else
365 bad &= 0xFF;
366 } else {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
368 page);
369 bad = chip->read_byte(mtd);
370 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200371
Sergey Lapin3a38a552013-01-14 03:46:50 +0000372 if (likely(chip->badblockbits == 8))
373 res = bad != 0xFF;
374 else
375 res = hweight8(bad) < chip->badblockbits;
376 ofs += mtd->writesize;
377 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
378 i++;
379 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200380
William Juul52c07962007-10-31 13:53:06 +0100381 if (getchip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200382 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200383
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200384 return res;
385}
386
387/**
388 * nand_default_block_markbad - [DEFAULT] mark a block bad
Sergey Lapin3a38a552013-01-14 03:46:50 +0000389 * @mtd: MTD device structure
390 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200391 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000392 * This is the default implementation, which can be overridden by a hardware
393 * specific driver. We try operations in the following order, according to our
394 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
395 * (1) erase the affected block, to allow OOB marker to be written cleanly
396 * (2) update in-memory BBT
397 * (3) write bad block marker to OOB area of affected block
398 * (4) update flash-based BBT
399 * Note that we retain the first error encountered in (3) or (4), finish the
400 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200401*/
402static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
403{
William Juul52c07962007-10-31 13:53:06 +0100404 struct nand_chip *chip = mtd->priv;
405 uint8_t buf[2] = { 0, 0 };
Sergey Lapin3a38a552013-01-14 03:46:50 +0000406 int block, res, ret = 0, i = 0;
407 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200408
Sergey Lapin3a38a552013-01-14 03:46:50 +0000409 if (write_oob) {
410 struct erase_info einfo;
411
412 /* Attempt erase before marking OOB */
413 memset(&einfo, 0, sizeof(einfo));
414 einfo.mtd = mtd;
415 einfo.addr = ofs;
416 einfo.len = 1 << chip->phys_erase_shift;
417 nand_erase_nand(mtd, &einfo, 0);
418 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200419
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200420 /* Get block number */
William Juul52c07962007-10-31 13:53:06 +0100421 block = (int)(ofs >> chip->bbt_erase_shift);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000422 /* Mark block bad in memory-based BBT */
William Juul52c07962007-10-31 13:53:06 +0100423 if (chip->bbt)
424 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200425
Sergey Lapin3a38a552013-01-14 03:46:50 +0000426 /* Write bad block marker to OOB */
427 if (write_oob) {
428 struct mtd_oob_ops ops;
429 loff_t wr_ofs = ofs;
430
Scott Wood3628f002008-10-24 16:20:43 -0500431 nand_get_device(chip, mtd, FL_WRITING);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200432
Sergey Lapin3a38a552013-01-14 03:46:50 +0000433 ops.datbuf = NULL;
434 ops.oobbuf = buf;
435 ops.ooboffs = chip->badblockpos;
436 if (chip->options & NAND_BUSWIDTH_16) {
437 ops.ooboffs &= ~0x01;
438 ops.len = ops.ooblen = 2;
439 } else {
440 ops.len = ops.ooblen = 1;
441 }
442 ops.mode = MTD_OPS_PLACE_OOB;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200443
Sergey Lapin3a38a552013-01-14 03:46:50 +0000444 /* Write to first/last page(s) if necessary */
445 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
446 wr_ofs += mtd->erasesize - mtd->writesize;
447 do {
448 res = nand_do_write_oob(mtd, wr_ofs, &ops);
449 if (!ret)
450 ret = res;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200451
Christian Hitzb8a6b372011-10-12 09:32:02 +0200452 i++;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000453 wr_ofs += mtd->writesize;
454 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200455
Scott Wood3628f002008-10-24 16:20:43 -0500456 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100457 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000458
459 /* Update flash-based bad block table */
460 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
461 res = nand_update_bbt(mtd, ofs);
462 if (!ret)
463 ret = res;
464 }
465
William Juul52c07962007-10-31 13:53:06 +0100466 if (!ret)
467 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500468
William Juul52c07962007-10-31 13:53:06 +0100469 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200470}
471
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200472/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200473 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000474 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200475 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000476 * Check, if the device is write protected. The function expects, that the
477 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200478 */
William Juul52c07962007-10-31 13:53:06 +0100479static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200480{
William Juul52c07962007-10-31 13:53:06 +0100481 struct nand_chip *chip = mtd->priv;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200482
Sergey Lapin3a38a552013-01-14 03:46:50 +0000483 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200484 if (chip->options & NAND_BROKEN_XD)
485 return 0;
486
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200487 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100488 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
489 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200490}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100491
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200492/**
493 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapin3a38a552013-01-14 03:46:50 +0000494 * @mtd: MTD device structure
495 * @ofs: offset from device start
496 * @getchip: 0, if the chip is already selected
497 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200498 *
499 * Check, if the block is bad. Either by reading the bad block table or
500 * calling of the scan function.
501 */
William Juul52c07962007-10-31 13:53:06 +0100502static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
503 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200504{
William Juul52c07962007-10-31 13:53:06 +0100505 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200506
Scott Woodf2f5c9e2012-02-20 14:50:39 -0600507 if (!(chip->options & NAND_BBT_SCANNED)) {
508 chip->options |= NAND_BBT_SCANNED;
509 chip->scan_bbt(mtd);
510 }
511
William Juul52c07962007-10-31 13:53:06 +0100512 if (!chip->bbt)
513 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200514
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200515 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100516 return nand_isbad_bbt(mtd, ofs, allowbbt);
517}
518
Sergey Lapin3a38a552013-01-14 03:46:50 +0000519/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juul52c07962007-10-31 13:53:06 +0100520void nand_wait_ready(struct mtd_info *mtd)
521{
522 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200523 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000524 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100525
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000526 time_start = get_timer(0);
Stefan Roesea5c312c2008-01-05 16:43:25 +0100527
Sergey Lapin3a38a552013-01-14 03:46:50 +0000528 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000529 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100530 if (chip->dev_ready)
531 if (chip->dev_ready(mtd))
532 break;
533 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200534}
535
536/**
537 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000538 * @mtd: MTD device structure
539 * @command: the command to be sent
540 * @column: the column address for this command, -1 if none
541 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200542 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000543 * Send command to NAND device. This function is used for small page devices
544 * (256/512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200545 */
William Juul52c07962007-10-31 13:53:06 +0100546static void nand_command(struct mtd_info *mtd, unsigned int command,
547 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200548{
William Juul52c07962007-10-31 13:53:06 +0100549 register struct nand_chip *chip = mtd->priv;
550 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyserf9f36222009-02-04 13:47:22 -0600551 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200552
Sergey Lapin3a38a552013-01-14 03:46:50 +0000553 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200554 if (command == NAND_CMD_SEQIN) {
555 int readcmd;
556
William Juul52c07962007-10-31 13:53:06 +0100557 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200558 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100559 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
564 } else {
565 column -= 256;
566 readcmd = NAND_CMD_READ1;
567 }
William Juul52c07962007-10-31 13:53:06 +0100568 chip->cmd_ctrl(mtd, readcmd, ctrl);
569 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200570 }
William Juul52c07962007-10-31 13:53:06 +0100571 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200572
Sergey Lapin3a38a552013-01-14 03:46:50 +0000573 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100574 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
575 /* Serially input address */
576 if (column != -1) {
577 /* Adjust columns for 16 bit buswidth */
Brian Norris67675222014-05-06 00:46:17 +0530578 if ((chip->options & NAND_BUSWIDTH_16) &&
579 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100580 column >>= 1;
581 chip->cmd_ctrl(mtd, column, ctrl);
582 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200583 }
William Juul52c07962007-10-31 13:53:06 +0100584 if (page_addr != -1) {
585 chip->cmd_ctrl(mtd, page_addr, ctrl);
586 ctrl &= ~NAND_CTRL_CHANGE;
587 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
588 /* One more address cycle for devices > 32MiB */
589 if (chip->chipsize > (32 << 20))
590 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
591 }
592 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200593
594 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000595 * Program and erase have their own busy handlers status and sequential
596 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100597 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200598 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200599
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200600 case NAND_CMD_PAGEPROG:
601 case NAND_CMD_ERASE1:
602 case NAND_CMD_ERASE2:
603 case NAND_CMD_SEQIN:
604 case NAND_CMD_STATUS:
605 return;
606
607 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100608 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200609 break;
William Juul52c07962007-10-31 13:53:06 +0100610 udelay(chip->chip_delay);
611 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
612 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
613 chip->cmd_ctrl(mtd,
614 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600615 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
616 (rst_sts_cnt--));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617 return;
618
William Juul52c07962007-10-31 13:53:06 +0100619 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200620 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200621 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200622 * If we don't have access to the busy pin, we apply the given
623 * command delay
William Juul52c07962007-10-31 13:53:06 +0100624 */
625 if (!chip->dev_ready) {
626 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200627 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200628 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200629 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000630 /*
631 * Apply this short delay always to ensure that we do wait tWB in
632 * any case on any machine.
633 */
William Juul52c07962007-10-31 13:53:06 +0100634 ndelay(100);
635
636 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200637}
638
639/**
640 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000641 * @mtd: MTD device structure
642 * @command: the command to be sent
643 * @column: the column address for this command, -1 if none
644 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200645 *
William Juul52c07962007-10-31 13:53:06 +0100646 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000647 * devices. We don't have the separate regions as we have in the small page
648 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200649 */
William Juul52c07962007-10-31 13:53:06 +0100650static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
651 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200652{
William Juul52c07962007-10-31 13:53:06 +0100653 register struct nand_chip *chip = mtd->priv;
Peter Tyserf9f36222009-02-04 13:47:22 -0600654 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200655
656 /* Emulate NAND_CMD_READOOB */
657 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100658 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200659 command = NAND_CMD_READ0;
660 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200661
William Juul52c07962007-10-31 13:53:06 +0100662 /* Command latch cycle */
663 chip->cmd_ctrl(mtd, command & 0xff,
664 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200665
666 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100667 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200668
669 /* Serially input address */
670 if (column != -1) {
671 /* Adjust columns for 16 bit buswidth */
Brian Norris67675222014-05-06 00:46:17 +0530672 if ((chip->options & NAND_BUSWIDTH_16) &&
673 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100675 chip->cmd_ctrl(mtd, column, ctrl);
676 ctrl &= ~NAND_CTRL_CHANGE;
677 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200678 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200679 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100680 chip->cmd_ctrl(mtd, page_addr, ctrl);
681 chip->cmd_ctrl(mtd, page_addr >> 8,
682 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200683 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100684 if (chip->chipsize > (128 << 20))
685 chip->cmd_ctrl(mtd, page_addr >> 16,
686 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200687 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200688 }
William Juul52c07962007-10-31 13:53:06 +0100689 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200690
691 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000692 * Program and erase have their own busy handlers status, sequential
693 * in, and deplete1 need no delay.
William Juul52c07962007-10-31 13:53:06 +0100694 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200695 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200696
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200697 case NAND_CMD_CACHEDPROG:
698 case NAND_CMD_PAGEPROG:
699 case NAND_CMD_ERASE1:
700 case NAND_CMD_ERASE2:
701 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100702 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703 case NAND_CMD_STATUS:
William Juul52c07962007-10-31 13:53:06 +0100704 case NAND_CMD_DEPLETE1:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200705 return;
706
William Juul52c07962007-10-31 13:53:06 +0100707 case NAND_CMD_STATUS_ERROR:
708 case NAND_CMD_STATUS_ERROR0:
709 case NAND_CMD_STATUS_ERROR1:
710 case NAND_CMD_STATUS_ERROR2:
711 case NAND_CMD_STATUS_ERROR3:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000712 /* Read error status commands require only a short delay */
William Juul52c07962007-10-31 13:53:06 +0100713 udelay(chip->chip_delay);
714 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715
716 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100717 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200718 break;
William Juul52c07962007-10-31 13:53:06 +0100719 udelay(chip->chip_delay);
720 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
721 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
722 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
723 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600724 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
725 (rst_sts_cnt--));
William Juul52c07962007-10-31 13:53:06 +0100726 return;
727
728 case NAND_CMD_RNDOUT:
729 /* No ready / busy check necessary */
730 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
731 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
732 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
733 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200734 return;
735
736 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100737 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
738 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
739 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
740 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200741
William Juul52c07962007-10-31 13:53:06 +0100742 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200743 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200744 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200745 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000746 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100747 */
748 if (!chip->dev_ready) {
749 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200751 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200752 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200753
Sergey Lapin3a38a552013-01-14 03:46:50 +0000754 /*
755 * Apply this short delay always to ensure that we do wait tWB in
756 * any case on any machine.
757 */
William Juul52c07962007-10-31 13:53:06 +0100758 ndelay(100);
759
760 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200761}
762
763/**
764 * nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000765 * @chip: the nand chip descriptor
766 * @mtd: MTD device structure
767 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200768 *
769 * Get the device and lock it for exclusive access
770 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200771static int
772nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100773{
Christian Hitzb8a6b372011-10-12 09:32:02 +0200774 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100775 return 0;
776}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200777
778/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000779 * nand_wait - [DEFAULT] wait until the command is done
780 * @mtd: MTD device structure
781 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200782 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000783 * Wait for command done. This applies to erase and program only. Erase can
784 * take up to 400ms and program up to 20ms according to general NAND and
785 * SmartMedia specs.
William Juul52c07962007-10-31 13:53:06 +0100786 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200787static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200788{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100789 unsigned long timeo;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200790 int state = chip->state;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000791 u32 time_start;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100792
793 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200794 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100795 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200796 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100797
Christian Hitzb8a6b372011-10-12 09:32:02 +0200798 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
799 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100800 else
Christian Hitzb8a6b372011-10-12 09:32:02 +0200801 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100802
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000803 time_start = get_timer(0);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100804
805 while (1) {
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000806 if (get_timer(time_start) > timeo) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100807 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100808 return 0x01;
809 }
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100810
Christian Hitzb8a6b372011-10-12 09:32:02 +0200811 if (chip->dev_ready) {
812 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100813 break;
814 } else {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200815 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100816 break;
817 }
818 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100819#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000820 time_start = get_timer(0);
821 while (get_timer(time_start) < 10)
822 ;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100823#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100824
Christian Hitzb8a6b372011-10-12 09:32:02 +0200825 return (int)chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200826}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200827
828/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000829 * nand_read_page_raw - [INTERN] read raw page data without ecc
830 * @mtd: mtd info structure
831 * @chip: nand chip info structure
832 * @buf: buffer to store read data
833 * @oob_required: caller requires OOB data read to chip->oob_poi
834 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -0500835 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000836 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200837 */
William Juul52c07962007-10-31 13:53:06 +0100838static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000839 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200840{
William Juul52c07962007-10-31 13:53:06 +0100841 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000842 if (oob_required)
843 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +0100844 return 0;
845}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200846
William Juul52c07962007-10-31 13:53:06 +0100847/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000848 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
849 * @mtd: mtd info structure
850 * @chip: nand chip info structure
851 * @buf: buffer to store read data
852 * @oob_required: caller requires OOB data read to chip->oob_poi
853 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -0500854 *
855 * We need a special oob layout and handling even when OOB isn't used.
856 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200857static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000858 struct nand_chip *chip, uint8_t *buf,
859 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -0500860{
861 int eccsize = chip->ecc.size;
862 int eccbytes = chip->ecc.bytes;
863 uint8_t *oob = chip->oob_poi;
864 int steps, size;
865
866 for (steps = chip->ecc.steps; steps > 0; steps--) {
867 chip->read_buf(mtd, buf, eccsize);
868 buf += eccsize;
869
870 if (chip->ecc.prepad) {
871 chip->read_buf(mtd, oob, chip->ecc.prepad);
872 oob += chip->ecc.prepad;
873 }
874
875 chip->read_buf(mtd, oob, eccbytes);
876 oob += eccbytes;
877
878 if (chip->ecc.postpad) {
879 chip->read_buf(mtd, oob, chip->ecc.postpad);
880 oob += chip->ecc.postpad;
881 }
882 }
883
884 size = mtd->oobsize - (oob - chip->oob_poi);
885 if (size)
886 chip->read_buf(mtd, oob, size);
887
888 return 0;
889}
890
891/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000892 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
893 * @mtd: mtd info structure
894 * @chip: nand chip info structure
895 * @buf: buffer to store read data
896 * @oob_required: caller requires OOB data read to chip->oob_poi
897 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +0100898 */
899static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000900 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +0100901{
902 int i, eccsize = chip->ecc.size;
903 int eccbytes = chip->ecc.bytes;
904 int eccsteps = chip->ecc.steps;
905 uint8_t *p = buf;
906 uint8_t *ecc_calc = chip->buffers->ecccalc;
907 uint8_t *ecc_code = chip->buffers->ecccode;
908 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200909
Sergey Lapin3a38a552013-01-14 03:46:50 +0000910 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200911
William Juul52c07962007-10-31 13:53:06 +0100912 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
913 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200914
William Juul52c07962007-10-31 13:53:06 +0100915 for (i = 0; i < chip->ecc.total; i++)
916 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200917
William Juul52c07962007-10-31 13:53:06 +0100918 eccsteps = chip->ecc.steps;
919 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200920
William Juul52c07962007-10-31 13:53:06 +0100921 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
922 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200923
William Juul52c07962007-10-31 13:53:06 +0100924 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood3628f002008-10-24 16:20:43 -0500925 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +0100926 mtd->ecc_stats.failed++;
927 else
928 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200929 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200930 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200931}
932
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200933/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000934 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
935 * @mtd: mtd info structure
936 * @chip: nand chip info structure
937 * @data_offs: offset of requested data within the page
938 * @readlen: data length
939 * @bufpoi: buffer to store read data
Scott Wood3628f002008-10-24 16:20:43 -0500940 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200941static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
942 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Scott Wood3628f002008-10-24 16:20:43 -0500943{
944 int start_step, end_step, num_steps;
945 uint32_t *eccpos = chip->ecc.layout->eccpos;
946 uint8_t *p;
947 int data_col_addr, i, gaps = 0;
948 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
949 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200950 int index = 0;
Scott Wood3628f002008-10-24 16:20:43 -0500951
Sergey Lapin3a38a552013-01-14 03:46:50 +0000952 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -0500953 start_step = data_offs / chip->ecc.size;
954 end_step = (data_offs + readlen - 1) / chip->ecc.size;
955 num_steps = end_step - start_step + 1;
956
Sergey Lapin3a38a552013-01-14 03:46:50 +0000957 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -0500958 datafrag_len = num_steps * chip->ecc.size;
959 eccfrag_len = num_steps * chip->ecc.bytes;
960
961 data_col_addr = start_step * chip->ecc.size;
962 /* If we read not a page aligned data */
963 if (data_col_addr != 0)
964 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
965
966 p = bufpoi + data_col_addr;
967 chip->read_buf(mtd, p, datafrag_len);
968
Sergey Lapin3a38a552013-01-14 03:46:50 +0000969 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -0500970 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
971 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
972
Sergey Lapin3a38a552013-01-14 03:46:50 +0000973 /*
974 * The performance is faster if we position offsets according to
975 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
976 */
Scott Wood3628f002008-10-24 16:20:43 -0500977 for (i = 0; i < eccfrag_len - 1; i++) {
978 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
979 eccpos[i + start_step * chip->ecc.bytes + 1]) {
980 gaps = 1;
981 break;
982 }
983 }
984 if (gaps) {
985 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
986 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
987 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000988 /*
989 * Send the command to read the particular ECC bytes take care
990 * about buswidth alignment in read_buf.
991 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200992 index = start_step * chip->ecc.bytes;
993
994 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -0500995 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200996 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -0500997 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200998 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -0500999 aligned_len++;
1000
Christian Hitzb8a6b372011-10-12 09:32:02 +02001001 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1002 mtd->writesize + aligned_pos, -1);
Scott Wood3628f002008-10-24 16:20:43 -05001003 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1004 }
1005
1006 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001007 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001008
1009 p = bufpoi + data_col_addr;
1010 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1011 int stat;
1012
Christian Hitzb8a6b372011-10-12 09:32:02 +02001013 stat = chip->ecc.correct(mtd, p,
1014 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1015 if (stat < 0)
Scott Wood3628f002008-10-24 16:20:43 -05001016 mtd->ecc_stats.failed++;
1017 else
1018 mtd->ecc_stats.corrected += stat;
1019 }
1020 return 0;
1021}
1022
1023/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001024 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1025 * @mtd: mtd info structure
1026 * @chip: nand chip info structure
1027 * @buf: buffer to store read data
1028 * @oob_required: caller requires OOB data read to chip->oob_poi
1029 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001030 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001031 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001032 */
William Juul52c07962007-10-31 13:53:06 +01001033static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001034 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001035{
William Juul52c07962007-10-31 13:53:06 +01001036 int i, eccsize = chip->ecc.size;
1037 int eccbytes = chip->ecc.bytes;
1038 int eccsteps = chip->ecc.steps;
1039 uint8_t *p = buf;
1040 uint8_t *ecc_calc = chip->buffers->ecccalc;
1041 uint8_t *ecc_code = chip->buffers->ecccode;
1042 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001043
William Juul52c07962007-10-31 13:53:06 +01001044 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1045 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1046 chip->read_buf(mtd, p, eccsize);
1047 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1048 }
1049 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001050
William Juul52c07962007-10-31 13:53:06 +01001051 for (i = 0; i < chip->ecc.total; i++)
1052 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001053
William Juul52c07962007-10-31 13:53:06 +01001054 eccsteps = chip->ecc.steps;
1055 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001056
William Juul52c07962007-10-31 13:53:06 +01001057 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1058 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001059
William Juul52c07962007-10-31 13:53:06 +01001060 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj16add232009-11-07 14:25:03 -05001061 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001062 mtd->ecc_stats.failed++;
1063 else
1064 mtd->ecc_stats.corrected += stat;
1065 }
1066 return 0;
1067}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001068
William Juul52c07962007-10-31 13:53:06 +01001069/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001070 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1071 * @mtd: mtd info structure
1072 * @chip: nand chip info structure
1073 * @buf: buffer to store read data
1074 * @oob_required: caller requires OOB data read to chip->oob_poi
1075 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001076 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001077 * Hardware ECC for large page chips, require OOB to be read first. For this
1078 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1079 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1080 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1081 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001082 */
1083static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001084 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001085{
1086 int i, eccsize = chip->ecc.size;
1087 int eccbytes = chip->ecc.bytes;
1088 int eccsteps = chip->ecc.steps;
1089 uint8_t *p = buf;
1090 uint8_t *ecc_code = chip->buffers->ecccode;
1091 uint32_t *eccpos = chip->ecc.layout->eccpos;
1092 uint8_t *ecc_calc = chip->buffers->ecccalc;
1093
1094 /* Read the OOB area first */
1095 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1096 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1097 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1098
1099 for (i = 0; i < chip->ecc.total; i++)
1100 ecc_code[i] = chip->oob_poi[eccpos[i]];
1101
1102 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1103 int stat;
1104
1105 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1106 chip->read_buf(mtd, p, eccsize);
1107 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1108
1109 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1110 if (stat < 0)
1111 mtd->ecc_stats.failed++;
1112 else
1113 mtd->ecc_stats.corrected += stat;
1114 }
1115 return 0;
1116}
1117
1118/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001119 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1120 * @mtd: mtd info structure
1121 * @chip: nand chip info structure
1122 * @buf: buffer to store read data
1123 * @oob_required: caller requires OOB data read to chip->oob_poi
1124 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001125 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001126 * The hw generator calculates the error syndrome automatically. Therefore we
1127 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01001128 */
1129static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001130 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001131{
1132 int i, eccsize = chip->ecc.size;
1133 int eccbytes = chip->ecc.bytes;
1134 int eccsteps = chip->ecc.steps;
1135 uint8_t *p = buf;
1136 uint8_t *oob = chip->oob_poi;
1137
1138 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1139 int stat;
1140
1141 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1142 chip->read_buf(mtd, p, eccsize);
1143
1144 if (chip->ecc.prepad) {
1145 chip->read_buf(mtd, oob, chip->ecc.prepad);
1146 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001147 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001148
William Juul52c07962007-10-31 13:53:06 +01001149 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1150 chip->read_buf(mtd, oob, eccbytes);
1151 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001152
Scott Wood3628f002008-10-24 16:20:43 -05001153 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001154 mtd->ecc_stats.failed++;
1155 else
1156 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001157
William Juul52c07962007-10-31 13:53:06 +01001158 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001159
William Juul52c07962007-10-31 13:53:06 +01001160 if (chip->ecc.postpad) {
1161 chip->read_buf(mtd, oob, chip->ecc.postpad);
1162 oob += chip->ecc.postpad;
1163 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001164 }
William Juul52c07962007-10-31 13:53:06 +01001165
1166 /* Calculate remaining oob bytes */
1167 i = mtd->oobsize - (oob - chip->oob_poi);
1168 if (i)
1169 chip->read_buf(mtd, oob, i);
1170
1171 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001172}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001173
1174/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001175 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1176 * @chip: nand chip structure
1177 * @oob: oob destination address
1178 * @ops: oob ops structure
1179 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01001180 */
1181static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1182 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001183{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001184 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001185
Sergey Lapin3a38a552013-01-14 03:46:50 +00001186 case MTD_OPS_PLACE_OOB:
1187 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001188 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1189 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001190
Sergey Lapin3a38a552013-01-14 03:46:50 +00001191 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01001192 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1193 uint32_t boffs = 0, roffs = ops->ooboffs;
1194 size_t bytes = 0;
1195
Christian Hitz13fc0e22011-10-12 09:32:01 +02001196 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001197 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01001198 if (unlikely(roffs)) {
1199 if (roffs >= free->length) {
1200 roffs -= free->length;
1201 continue;
1202 }
1203 boffs = free->offset + roffs;
1204 bytes = min_t(size_t, len,
1205 (free->length - roffs));
1206 roffs = 0;
1207 } else {
1208 bytes = min_t(size_t, len, free->length);
1209 boffs = free->offset;
1210 }
1211 memcpy(oob, chip->oob_poi + boffs, bytes);
1212 oob += bytes;
1213 }
1214 return oob;
1215 }
1216 default:
1217 BUG();
1218 }
1219 return NULL;
1220}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001221
1222/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001223 * nand_do_read_ops - [INTERN] Read data with ECC
1224 * @mtd: MTD device structure
1225 * @from: offset to read from
1226 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001227 *
William Juul52c07962007-10-31 13:53:06 +01001228 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001229 */
William Juul52c07962007-10-31 13:53:06 +01001230static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1231 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001232{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001233 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juul52c07962007-10-31 13:53:06 +01001234 struct nand_chip *chip = mtd->priv;
1235 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01001236 int ret = 0;
1237 uint32_t readlen = ops->len;
1238 uint32_t oobreadlen = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001239 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitzb8a6b372011-10-12 09:32:02 +02001240 mtd->oobavail : mtd->oobsize;
1241
William Juul52c07962007-10-31 13:53:06 +01001242 uint8_t *bufpoi, *oob, *buf;
Paul Burton700a76c2013-09-04 15:16:56 +01001243 unsigned int max_bitflips = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001244
William Juul52c07962007-10-31 13:53:06 +01001245 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001246
William Juul52c07962007-10-31 13:53:06 +01001247 chipnr = (int)(from >> chip->chip_shift);
1248 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001249
William Juul52c07962007-10-31 13:53:06 +01001250 realpage = (int)(from >> chip->page_shift);
1251 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001252
William Juul52c07962007-10-31 13:53:06 +01001253 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001254
William Juul52c07962007-10-31 13:53:06 +01001255 buf = ops->datbuf;
1256 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001257 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001258
Christian Hitz13fc0e22011-10-12 09:32:01 +02001259 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06001260 WATCHDOG_RESET();
1261
William Juul52c07962007-10-31 13:53:06 +01001262 bytes = min(mtd->writesize - col, readlen);
1263 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001264
Sergey Lapin3a38a552013-01-14 03:46:50 +00001265 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01001266 if (realpage != chip->pagebuf || oob) {
1267 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001268
Sergey Lapin3a38a552013-01-14 03:46:50 +00001269 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001270
Paul Burton700a76c2013-09-04 15:16:56 +01001271 /*
1272 * Now read the page into the buffer. Absent an error,
1273 * the read methods return max bitflips per ecc step.
1274 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001275 if (unlikely(ops->mode == MTD_OPS_RAW))
1276 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1277 oob_required,
1278 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001279 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1280 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02001281 ret = chip->ecc.read_subpage(mtd, chip,
1282 col, bytes, bufpoi);
William Juul52c07962007-10-31 13:53:06 +01001283 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001284 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001285 oob_required, page);
1286 if (ret < 0) {
1287 if (!aligned)
1288 /* Invalidate page cache */
1289 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01001290 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001291 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001292
Paul Burton700a76c2013-09-04 15:16:56 +01001293 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1294
William Juul52c07962007-10-31 13:53:06 +01001295 /* Transfer not aligned data */
1296 if (!aligned) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001297 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00001298 !(mtd->ecc_stats.failed - stats.failed) &&
Paul Burton700a76c2013-09-04 15:16:56 +01001299 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05001300 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01001301 chip->pagebuf_bitflips = ret;
1302 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001303 /* Invalidate page cache */
1304 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01001305 }
William Juul52c07962007-10-31 13:53:06 +01001306 memcpy(buf, chip->buffers->databuf + col, bytes);
1307 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001308
William Juul52c07962007-10-31 13:53:06 +01001309 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001310
William Juul52c07962007-10-31 13:53:06 +01001311 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001312 int toread = min(oobreadlen, max_oobsize);
1313
1314 if (toread) {
1315 oob = nand_transfer_oob(chip,
1316 oob, ops, toread);
1317 oobreadlen -= toread;
1318 }
William Juul52c07962007-10-31 13:53:06 +01001319 }
William Juul52c07962007-10-31 13:53:06 +01001320 } else {
1321 memcpy(buf, chip->buffers->databuf + col, bytes);
1322 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01001323 max_bitflips = max_t(unsigned int, max_bitflips,
1324 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01001325 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001326
William Juul52c07962007-10-31 13:53:06 +01001327 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001328
William Juul52c07962007-10-31 13:53:06 +01001329 if (!readlen)
1330 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001331
Sergey Lapin3a38a552013-01-14 03:46:50 +00001332 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01001333 col = 0;
1334 /* Increment page address */
1335 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001336
William Juul52c07962007-10-31 13:53:06 +01001337 page = realpage & chip->pagemask;
1338 /* Check, if we cross a chip boundary */
1339 if (!page) {
1340 chipnr++;
1341 chip->select_chip(mtd, -1);
1342 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001343 }
William Juul52c07962007-10-31 13:53:06 +01001344 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001345
William Juul52c07962007-10-31 13:53:06 +01001346 ops->retlen = ops->len - (size_t) readlen;
1347 if (oob)
1348 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001349
William Juul52c07962007-10-31 13:53:06 +01001350 if (ret)
1351 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001352
William Juul52c07962007-10-31 13:53:06 +01001353 if (mtd->ecc_stats.failed - stats.failed)
1354 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001355
Paul Burton700a76c2013-09-04 15:16:56 +01001356 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001357}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001358
William Juul52c07962007-10-31 13:53:06 +01001359/**
Christian Hitz13fc0e22011-10-12 09:32:01 +02001360 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapin3a38a552013-01-14 03:46:50 +00001361 * @mtd: MTD device structure
1362 * @from: offset to read from
1363 * @len: number of bytes to read
1364 * @retlen: pointer to variable to store the number of read bytes
1365 * @buf: the databuffer to put data
William Juul52c07962007-10-31 13:53:06 +01001366 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001367 * Get hold of the chip and call nand_do_read.
William Juul52c07962007-10-31 13:53:06 +01001368 */
1369static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1370 size_t *retlen, uint8_t *buf)
1371{
1372 struct nand_chip *chip = mtd->priv;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001373 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01001374 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001375
William Juul52c07962007-10-31 13:53:06 +01001376 nand_get_device(chip, mtd, FL_READING);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001377 ops.len = len;
1378 ops.datbuf = buf;
1379 ops.oobbuf = NULL;
1380 ops.mode = MTD_OPS_PLACE_OOB;
1381 ret = nand_do_read_ops(mtd, from, &ops);
1382 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01001383 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001384 return ret;
1385}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001386
William Juul52c07962007-10-31 13:53:06 +01001387/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001388 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1389 * @mtd: mtd info structure
1390 * @chip: nand chip info structure
1391 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001392 */
1393static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001394 int page)
William Juul52c07962007-10-31 13:53:06 +01001395{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001396 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juul52c07962007-10-31 13:53:06 +01001397 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001398 return 0;
William Juul52c07962007-10-31 13:53:06 +01001399}
1400
1401/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001402 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01001403 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00001404 * @mtd: mtd info structure
1405 * @chip: nand chip info structure
1406 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001407 */
1408static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001409 int page)
William Juul52c07962007-10-31 13:53:06 +01001410{
1411 uint8_t *buf = chip->oob_poi;
1412 int length = mtd->oobsize;
1413 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1414 int eccsize = chip->ecc.size;
1415 uint8_t *bufpoi = buf;
1416 int i, toread, sndrnd = 0, pos;
1417
1418 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1419 for (i = 0; i < chip->ecc.steps; i++) {
1420 if (sndrnd) {
1421 pos = eccsize + i * (eccsize + chunk);
1422 if (mtd->writesize > 512)
1423 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1424 else
1425 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001426 } else
William Juul52c07962007-10-31 13:53:06 +01001427 sndrnd = 1;
1428 toread = min_t(int, length, chunk);
1429 chip->read_buf(mtd, bufpoi, toread);
1430 bufpoi += toread;
1431 length -= toread;
1432 }
1433 if (length > 0)
1434 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001435
Sergey Lapin3a38a552013-01-14 03:46:50 +00001436 return 0;
William Juul52c07962007-10-31 13:53:06 +01001437}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001438
William Juul52c07962007-10-31 13:53:06 +01001439/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001440 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1441 * @mtd: mtd info structure
1442 * @chip: nand chip info structure
1443 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001444 */
1445static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1446 int page)
1447{
1448 int status = 0;
1449 const uint8_t *buf = chip->oob_poi;
1450 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001451
William Juul52c07962007-10-31 13:53:06 +01001452 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1453 chip->write_buf(mtd, buf, length);
1454 /* Send command to program the OOB data */
1455 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001456
William Juul52c07962007-10-31 13:53:06 +01001457 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001458
William Juul52c07962007-10-31 13:53:06 +01001459 return status & NAND_STATUS_FAIL ? -EIO : 0;
1460}
1461
1462/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001463 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1464 * with syndrome - only for large page flash
1465 * @mtd: mtd info structure
1466 * @chip: nand chip info structure
1467 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001468 */
1469static int nand_write_oob_syndrome(struct mtd_info *mtd,
1470 struct nand_chip *chip, int page)
1471{
1472 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1473 int eccsize = chip->ecc.size, length = mtd->oobsize;
1474 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1475 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001476
1477 /*
William Juul52c07962007-10-31 13:53:06 +01001478 * data-ecc-data-ecc ... ecc-oob
1479 * or
1480 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001481 */
William Juul52c07962007-10-31 13:53:06 +01001482 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1483 pos = steps * (eccsize + chunk);
1484 steps = 0;
1485 } else
1486 pos = eccsize;
1487
1488 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1489 for (i = 0; i < steps; i++) {
1490 if (sndcmd) {
1491 if (mtd->writesize <= 512) {
1492 uint32_t fill = 0xFFFFFFFF;
1493
1494 len = eccsize;
1495 while (len > 0) {
1496 int num = min_t(int, len, 4);
1497 chip->write_buf(mtd, (uint8_t *)&fill,
1498 num);
1499 len -= num;
1500 }
1501 } else {
1502 pos = eccsize + i * (eccsize + chunk);
1503 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1504 }
1505 } else
1506 sndcmd = 1;
1507 len = min_t(int, length, chunk);
1508 chip->write_buf(mtd, bufpoi, len);
1509 bufpoi += len;
1510 length -= len;
1511 }
1512 if (length > 0)
1513 chip->write_buf(mtd, bufpoi, length);
1514
1515 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1516 status = chip->waitfunc(mtd, chip);
1517
1518 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001519}
1520
1521/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001522 * nand_do_read_oob - [INTERN] NAND read out-of-band
1523 * @mtd: MTD device structure
1524 * @from: offset to read from
1525 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001526 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001527 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001528 */
William Juul52c07962007-10-31 13:53:06 +01001529static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1530 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001531{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001532 int page, realpage, chipnr;
William Juul52c07962007-10-31 13:53:06 +01001533 struct nand_chip *chip = mtd->priv;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001534 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01001535 int readlen = ops->ooblen;
1536 int len;
1537 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001538 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001539
Christian Hitz13fc0e22011-10-12 09:32:01 +02001540 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1541 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001542
Sergey Lapin3a38a552013-01-14 03:46:50 +00001543 stats = mtd->ecc_stats;
1544
1545 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juul52c07962007-10-31 13:53:06 +01001546 len = chip->ecc.layout->oobavail;
1547 else
1548 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001549
William Juul52c07962007-10-31 13:53:06 +01001550 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02001551 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1552 "outside oob\n", __func__);
William Juul52c07962007-10-31 13:53:06 +01001553 return -EINVAL;
1554 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001555
1556 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001557 if (unlikely(from >= mtd->size ||
1558 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1559 (from >> chip->page_shift)) * len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02001560 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1561 "of device\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001562 return -EINVAL;
1563 }
1564
William Juul52c07962007-10-31 13:53:06 +01001565 chipnr = (int)(from >> chip->chip_shift);
1566 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001567
William Juul52c07962007-10-31 13:53:06 +01001568 /* Shift to get page */
1569 realpage = (int)(from >> chip->page_shift);
1570 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001571
Christian Hitz13fc0e22011-10-12 09:32:01 +02001572 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06001573 WATCHDOG_RESET();
Sergey Lapin3a38a552013-01-14 03:46:50 +00001574 if (ops->mode == MTD_OPS_RAW)
1575 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1576 else
1577 ret = chip->ecc.read_oob(mtd, chip, page);
1578
1579 if (ret < 0)
1580 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001581
William Juul52c07962007-10-31 13:53:06 +01001582 len = min(len, readlen);
1583 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001584
William Juul52c07962007-10-31 13:53:06 +01001585 readlen -= len;
1586 if (!readlen)
1587 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001588
William Juul52c07962007-10-31 13:53:06 +01001589 /* Increment page address */
1590 realpage++;
1591
1592 page = realpage & chip->pagemask;
1593 /* Check, if we cross a chip boundary */
1594 if (!page) {
1595 chipnr++;
1596 chip->select_chip(mtd, -1);
1597 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001598 }
William Juul52c07962007-10-31 13:53:06 +01001599 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001600
Sergey Lapin3a38a552013-01-14 03:46:50 +00001601 ops->oobretlen = ops->ooblen - readlen;
1602
1603 if (ret < 0)
1604 return ret;
1605
1606 if (mtd->ecc_stats.failed - stats.failed)
1607 return -EBADMSG;
1608
1609 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001610}
1611
1612/**
William Juul52c07962007-10-31 13:53:06 +01001613 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00001614 * @mtd: MTD device structure
1615 * @from: offset to read from
1616 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001617 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001618 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001619 */
William Juul52c07962007-10-31 13:53:06 +01001620static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1621 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001622{
William Juul52c07962007-10-31 13:53:06 +01001623 struct nand_chip *chip = mtd->priv;
1624 int ret = -ENOTSUPP;
1625
1626 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001627
1628 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001629 if (ops->datbuf && (from + ops->len) > mtd->size) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02001630 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1631 "beyond end of device\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001632 return -EINVAL;
1633 }
1634
William Juul52c07962007-10-31 13:53:06 +01001635 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001636
Christian Hitz13fc0e22011-10-12 09:32:01 +02001637 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001638 case MTD_OPS_PLACE_OOB:
1639 case MTD_OPS_AUTO_OOB:
1640 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001641 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001642
William Juul52c07962007-10-31 13:53:06 +01001643 default:
1644 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001645 }
1646
William Juul52c07962007-10-31 13:53:06 +01001647 if (!ops->datbuf)
1648 ret = nand_do_read_oob(mtd, from, ops);
1649 else
1650 ret = nand_do_read_ops(mtd, from, ops);
1651
Christian Hitz13fc0e22011-10-12 09:32:01 +02001652out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001653 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001654 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001655}
1656
1657
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001658/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001659 * nand_write_page_raw - [INTERN] raw page write function
1660 * @mtd: mtd info structure
1661 * @chip: nand chip info structure
1662 * @buf: data buffer
1663 * @oob_required: must write chip->oob_poi to OOB
David Brownellee86b8d2009-11-07 16:27:01 -05001664 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001665 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01001666 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001667static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1668 const uint8_t *buf, int oob_required)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001669{
William Juul52c07962007-10-31 13:53:06 +01001670 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001671 if (oob_required)
1672 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1673
1674 return 0;
William Juul52c07962007-10-31 13:53:06 +01001675}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001676
William Juul52c07962007-10-31 13:53:06 +01001677/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001678 * nand_write_page_raw_syndrome - [INTERN] raw page write function
1679 * @mtd: mtd info structure
1680 * @chip: nand chip info structure
1681 * @buf: data buffer
1682 * @oob_required: must write chip->oob_poi to OOB
David Brownellee86b8d2009-11-07 16:27:01 -05001683 *
1684 * We need a special oob layout and handling even when ECC isn't checked.
1685 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001686static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02001687 struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001688 const uint8_t *buf, int oob_required)
David Brownellee86b8d2009-11-07 16:27:01 -05001689{
1690 int eccsize = chip->ecc.size;
1691 int eccbytes = chip->ecc.bytes;
1692 uint8_t *oob = chip->oob_poi;
1693 int steps, size;
1694
1695 for (steps = chip->ecc.steps; steps > 0; steps--) {
1696 chip->write_buf(mtd, buf, eccsize);
1697 buf += eccsize;
1698
1699 if (chip->ecc.prepad) {
1700 chip->write_buf(mtd, oob, chip->ecc.prepad);
1701 oob += chip->ecc.prepad;
1702 }
1703
1704 chip->read_buf(mtd, oob, eccbytes);
1705 oob += eccbytes;
1706
1707 if (chip->ecc.postpad) {
1708 chip->write_buf(mtd, oob, chip->ecc.postpad);
1709 oob += chip->ecc.postpad;
1710 }
1711 }
1712
1713 size = mtd->oobsize - (oob - chip->oob_poi);
1714 if (size)
1715 chip->write_buf(mtd, oob, size);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001716
1717 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05001718}
1719/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001720 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
1721 * @mtd: mtd info structure
1722 * @chip: nand chip info structure
1723 * @buf: data buffer
1724 * @oob_required: must write chip->oob_poi to OOB
William Juul52c07962007-10-31 13:53:06 +01001725 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001726static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1727 const uint8_t *buf, int oob_required)
William Juul52c07962007-10-31 13:53:06 +01001728{
1729 int i, eccsize = chip->ecc.size;
1730 int eccbytes = chip->ecc.bytes;
1731 int eccsteps = chip->ecc.steps;
1732 uint8_t *ecc_calc = chip->buffers->ecccalc;
1733 const uint8_t *p = buf;
1734 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001735
Sergey Lapin3a38a552013-01-14 03:46:50 +00001736 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01001737 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1738 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001739
William Juul52c07962007-10-31 13:53:06 +01001740 for (i = 0; i < chip->ecc.total; i++)
1741 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001742
Sergey Lapin3a38a552013-01-14 03:46:50 +00001743 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001744}
1745
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001746/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001747 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
1748 * @mtd: mtd info structure
1749 * @chip: nand chip info structure
1750 * @buf: data buffer
1751 * @oob_required: must write chip->oob_poi to OOB
William Juul52c07962007-10-31 13:53:06 +01001752 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001753static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1754 const uint8_t *buf, int oob_required)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001755{
William Juul52c07962007-10-31 13:53:06 +01001756 int i, eccsize = chip->ecc.size;
1757 int eccbytes = chip->ecc.bytes;
1758 int eccsteps = chip->ecc.steps;
1759 uint8_t *ecc_calc = chip->buffers->ecccalc;
1760 const uint8_t *p = buf;
1761 uint32_t *eccpos = chip->ecc.layout->eccpos;
1762
1763 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1764 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1765 chip->write_buf(mtd, p, eccsize);
1766 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1767 }
1768
1769 for (i = 0; i < chip->ecc.total; i++)
1770 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1771
1772 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001773
1774 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001775}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001776
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001777/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001778 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
1779 * @mtd: mtd info structure
1780 * @chip: nand chip info structure
1781 * @buf: data buffer
1782 * @oob_required: must write chip->oob_poi to OOB
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001783 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001784 * The hw generator calculates the error syndrome automatically. Therefore we
1785 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001786 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001787static int nand_write_page_syndrome(struct mtd_info *mtd,
1788 struct nand_chip *chip,
1789 const uint8_t *buf, int oob_required)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001790{
William Juul52c07962007-10-31 13:53:06 +01001791 int i, eccsize = chip->ecc.size;
1792 int eccbytes = chip->ecc.bytes;
1793 int eccsteps = chip->ecc.steps;
1794 const uint8_t *p = buf;
1795 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001796
William Juul52c07962007-10-31 13:53:06 +01001797 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001798
William Juul52c07962007-10-31 13:53:06 +01001799 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1800 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001801
William Juul52c07962007-10-31 13:53:06 +01001802 if (chip->ecc.prepad) {
1803 chip->write_buf(mtd, oob, chip->ecc.prepad);
1804 oob += chip->ecc.prepad;
1805 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001806
William Juul52c07962007-10-31 13:53:06 +01001807 chip->ecc.calculate(mtd, p, oob);
1808 chip->write_buf(mtd, oob, eccbytes);
1809 oob += eccbytes;
1810
1811 if (chip->ecc.postpad) {
1812 chip->write_buf(mtd, oob, chip->ecc.postpad);
1813 oob += chip->ecc.postpad;
1814 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001815 }
1816
William Juul52c07962007-10-31 13:53:06 +01001817 /* Calculate remaining oob bytes */
1818 i = mtd->oobsize - (oob - chip->oob_poi);
1819 if (i)
1820 chip->write_buf(mtd, oob, i);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001821
1822 return 0;
William Juul52c07962007-10-31 13:53:06 +01001823}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001824
William Juul52c07962007-10-31 13:53:06 +01001825/**
1826 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00001827 * @mtd: MTD device structure
1828 * @chip: NAND chip descriptor
1829 * @buf: the data to write
1830 * @oob_required: must write chip->oob_poi to OOB
1831 * @page: page number to write
1832 * @cached: cached programming
1833 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01001834 */
1835static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001836 const uint8_t *buf, int oob_required, int page,
1837 int cached, int raw)
William Juul52c07962007-10-31 13:53:06 +01001838{
1839 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001840
William Juul52c07962007-10-31 13:53:06 +01001841 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1842
1843 if (unlikely(raw))
Sergey Lapin3a38a552013-01-14 03:46:50 +00001844 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
William Juul52c07962007-10-31 13:53:06 +01001845 else
Sergey Lapin3a38a552013-01-14 03:46:50 +00001846 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1847
1848 if (status < 0)
1849 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001850
William Juul52c07962007-10-31 13:53:06 +01001851 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00001852 * Cached progamming disabled for now. Not sure if it's worth the
1853 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juul52c07962007-10-31 13:53:06 +01001854 */
1855 cached = 0;
1856
1857 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1858
1859 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1860 status = chip->waitfunc(mtd, chip);
1861 /*
1862 * See if operation failed and additional status checks are
Sergey Lapin3a38a552013-01-14 03:46:50 +00001863 * available.
William Juul52c07962007-10-31 13:53:06 +01001864 */
1865 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1866 status = chip->errstat(mtd, chip, FL_WRITING, status,
1867 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001868
William Juul52c07962007-10-31 13:53:06 +01001869 if (status & NAND_STATUS_FAIL)
1870 return -EIO;
1871 } else {
1872 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1873 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001874 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001875
William Juul52c07962007-10-31 13:53:06 +01001876#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1877 /* Send command to read back the data */
1878 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001879
William Juul52c07962007-10-31 13:53:06 +01001880 if (chip->verify_buf(mtd, buf, mtd->writesize))
1881 return -EIO;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001882
1883 /* Make sure the next page prog is preceded by a status read */
1884 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01001885#endif
1886 return 0;
1887}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001888
William Juul52c07962007-10-31 13:53:06 +01001889/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001890 * nand_fill_oob - [INTERN] Transfer client buffer to oob
1891 * @mtd: MTD device structure
1892 * @oob: oob data buffer
1893 * @len: oob data write length
1894 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01001895 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001896static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
1897 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01001898{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001899 struct nand_chip *chip = mtd->priv;
1900
1901 /*
1902 * Initialise to all 0xFF, to avoid the possibility of left over OOB
1903 * data from a previous OOB read.
1904 */
1905 memset(chip->oob_poi, 0xff, mtd->oobsize);
1906
Christian Hitz13fc0e22011-10-12 09:32:01 +02001907 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001908
Sergey Lapin3a38a552013-01-14 03:46:50 +00001909 case MTD_OPS_PLACE_OOB:
1910 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001911 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1912 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001913
Sergey Lapin3a38a552013-01-14 03:46:50 +00001914 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01001915 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1916 uint32_t boffs = 0, woffs = ops->ooboffs;
1917 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001918
Christian Hitz13fc0e22011-10-12 09:32:01 +02001919 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001920 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01001921 if (unlikely(woffs)) {
1922 if (woffs >= free->length) {
1923 woffs -= free->length;
1924 continue;
1925 }
1926 boffs = free->offset + woffs;
1927 bytes = min_t(size_t, len,
1928 (free->length - woffs));
1929 woffs = 0;
1930 } else {
1931 bytes = min_t(size_t, len, free->length);
1932 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001933 }
William Juul52c07962007-10-31 13:53:06 +01001934 memcpy(chip->oob_poi + boffs, oob, bytes);
1935 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001936 }
William Juul52c07962007-10-31 13:53:06 +01001937 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001938 }
William Juul52c07962007-10-31 13:53:06 +01001939 default:
1940 BUG();
1941 }
1942 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001943}
1944
Christian Hitzb8a6b372011-10-12 09:32:02 +02001945#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001946
1947/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001948 * nand_do_write_ops - [INTERN] NAND write with ECC
1949 * @mtd: MTD device structure
1950 * @to: offset to write to
1951 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001952 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001953 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001954 */
William Juul52c07962007-10-31 13:53:06 +01001955static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1956 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001957{
William Juul52c07962007-10-31 13:53:06 +01001958 int chipnr, realpage, page, blockmask, column;
1959 struct nand_chip *chip = mtd->priv;
1960 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001961
1962 uint32_t oobwritelen = ops->ooblen;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001963 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitzb8a6b372011-10-12 09:32:02 +02001964 mtd->oobavail : mtd->oobsize;
1965
William Juul52c07962007-10-31 13:53:06 +01001966 uint8_t *oob = ops->oobbuf;
1967 uint8_t *buf = ops->datbuf;
1968 int ret, subpage;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001969 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001970
William Juul52c07962007-10-31 13:53:06 +01001971 ops->retlen = 0;
1972 if (!writelen)
1973 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001974
William Juul52c07962007-10-31 13:53:06 +01001975 column = to & (mtd->writesize - 1);
1976 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001977
William Juul52c07962007-10-31 13:53:06 +01001978 if (subpage && oob)
1979 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001980
William Juul52c07962007-10-31 13:53:06 +01001981 chipnr = (int)(to >> chip->chip_shift);
1982 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001983
1984 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01001985 if (nand_check_wp(mtd)) {
1986 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1987 return -EIO;
1988 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001989
William Juul52c07962007-10-31 13:53:06 +01001990 realpage = (int)(to >> chip->page_shift);
1991 page = realpage & chip->pagemask;
1992 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001993
William Juul52c07962007-10-31 13:53:06 +01001994 /* Invalidate the page cache, when we write to the cached page */
1995 if (to <= (chip->pagebuf << chip->page_shift) &&
1996 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1997 chip->pagebuf = -1;
1998
Christian Hitzb8a6b372011-10-12 09:32:02 +02001999 /* Don't allow multipage oob writes with offset */
2000 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2001 return -EINVAL;
2002
Christian Hitz13fc0e22011-10-12 09:32:01 +02002003 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002004 WATCHDOG_RESET();
2005
William Juul52c07962007-10-31 13:53:06 +01002006 int bytes = mtd->writesize;
2007 int cached = writelen > bytes && page != blockmask;
2008 uint8_t *wbuf = buf;
2009
Sergey Lapin3a38a552013-01-14 03:46:50 +00002010 /* Partial page write? */
htbegin64214b22013-03-01 22:59:27 +00002011 if (unlikely(column || writelen < mtd->writesize)) {
William Juul52c07962007-10-31 13:53:06 +01002012 cached = 0;
2013 bytes = min_t(int, bytes - column, (int) writelen);
2014 chip->pagebuf = -1;
2015 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2016 memcpy(&chip->buffers->databuf[column], buf, bytes);
2017 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002018 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002019
Christian Hitzb8a6b372011-10-12 09:32:02 +02002020 if (unlikely(oob)) {
2021 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002022 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002023 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002024 } else {
2025 /* We still need to erase leftover OOB data */
2026 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002027 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002028
Sergey Lapin3a38a552013-01-14 03:46:50 +00002029 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2030 cached, (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01002031 if (ret)
2032 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002033
William Juul52c07962007-10-31 13:53:06 +01002034 writelen -= bytes;
2035 if (!writelen)
2036 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002037
William Juul52c07962007-10-31 13:53:06 +01002038 column = 0;
2039 buf += bytes;
2040 realpage++;
2041
2042 page = realpage & chip->pagemask;
2043 /* Check, if we cross a chip boundary */
2044 if (!page) {
2045 chipnr++;
2046 chip->select_chip(mtd, -1);
2047 chip->select_chip(mtd, chipnr);
2048 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002049 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002050
William Juul52c07962007-10-31 13:53:06 +01002051 ops->retlen = ops->len - writelen;
2052 if (unlikely(oob))
2053 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002054 return ret;
2055}
2056
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002057/**
William Juul52c07962007-10-31 13:53:06 +01002058 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00002059 * @mtd: MTD device structure
2060 * @to: offset to write to
2061 * @len: number of bytes to write
2062 * @retlen: pointer to variable to store the number of written bytes
2063 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002064 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002065 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002066 */
William Juul52c07962007-10-31 13:53:06 +01002067static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2068 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002069{
William Juul52c07962007-10-31 13:53:06 +01002070 struct nand_chip *chip = mtd->priv;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002071 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01002072 int ret;
2073
William Juul52c07962007-10-31 13:53:06 +01002074 nand_get_device(chip, mtd, FL_WRITING);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002075 ops.len = len;
2076 ops.datbuf = (uint8_t *)buf;
2077 ops.oobbuf = NULL;
2078 ops.mode = MTD_OPS_PLACE_OOB;
2079 ret = nand_do_write_ops(mtd, to, &ops);
2080 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01002081 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002082 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002083}
2084
2085/**
William Juul52c07962007-10-31 13:53:06 +01002086 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002087 * @mtd: MTD device structure
2088 * @to: offset to write to
2089 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002090 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002091 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002092 */
William Juul52c07962007-10-31 13:53:06 +01002093static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2094 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002095{
William Juul52c07962007-10-31 13:53:06 +01002096 int chipnr, page, status, len;
2097 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002098
Christian Hitz13fc0e22011-10-12 09:32:01 +02002099 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2100 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002101
Sergey Lapin3a38a552013-01-14 03:46:50 +00002102 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juul52c07962007-10-31 13:53:06 +01002103 len = chip->ecc.layout->oobavail;
2104 else
2105 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002106
2107 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002108 if ((ops->ooboffs + ops->ooblen) > len) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002109 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2110 "past end of page\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002111 return -EINVAL;
2112 }
2113
William Juul52c07962007-10-31 13:53:06 +01002114 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002115 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2116 "write outside oob\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002117 return -EINVAL;
2118 }
2119
Christian Hitz13fc0e22011-10-12 09:32:01 +02002120 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002121 if (unlikely(to >= mtd->size ||
2122 ops->ooboffs + ops->ooblen >
2123 ((mtd->size >> chip->page_shift) -
2124 (to >> chip->page_shift)) * len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002125 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2126 "end of device\n", __func__);
William Juul52c07962007-10-31 13:53:06 +01002127 return -EINVAL;
2128 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002129
William Juul52c07962007-10-31 13:53:06 +01002130 chipnr = (int)(to >> chip->chip_shift);
2131 chip->select_chip(mtd, chipnr);
2132
2133 /* Shift to get page */
2134 page = (int)(to >> chip->page_shift);
2135
2136 /*
2137 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2138 * of my DiskOnChip 2000 test units) will clear the whole data page too
2139 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2140 * it in the doc2000 driver in August 1999. dwmw2.
2141 */
2142 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002143
2144 /* Check, if it is write protected */
2145 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01002146 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002147
William Juul52c07962007-10-31 13:53:06 +01002148 /* Invalidate the page cache, if we write to the cached page */
2149 if (page == chip->pagebuf)
2150 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002151
Sergey Lapin3a38a552013-01-14 03:46:50 +00002152 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2153
2154 if (ops->mode == MTD_OPS_RAW)
2155 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2156 else
2157 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002158
William Juul52c07962007-10-31 13:53:06 +01002159 if (status)
2160 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002161
William Juul52c07962007-10-31 13:53:06 +01002162 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002163
William Juul52c07962007-10-31 13:53:06 +01002164 return 0;
2165}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002166
William Juul52c07962007-10-31 13:53:06 +01002167/**
2168 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002169 * @mtd: MTD device structure
2170 * @to: offset to write to
2171 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01002172 */
2173static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2174 struct mtd_oob_ops *ops)
2175{
2176 struct nand_chip *chip = mtd->priv;
2177 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002178
William Juul52c07962007-10-31 13:53:06 +01002179 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002180
William Juul52c07962007-10-31 13:53:06 +01002181 /* Do not allow writes past end of device */
2182 if (ops->datbuf && (to + ops->len) > mtd->size) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002183 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2184 "end of device\n", __func__);
William Juul52c07962007-10-31 13:53:06 +01002185 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002186 }
William Juul52c07962007-10-31 13:53:06 +01002187
2188 nand_get_device(chip, mtd, FL_WRITING);
2189
Christian Hitz13fc0e22011-10-12 09:32:01 +02002190 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002191 case MTD_OPS_PLACE_OOB:
2192 case MTD_OPS_AUTO_OOB:
2193 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002194 break;
2195
2196 default:
2197 goto out;
2198 }
2199
2200 if (!ops->datbuf)
2201 ret = nand_do_write_oob(mtd, to, ops);
2202 else
2203 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002204
Christian Hitz13fc0e22011-10-12 09:32:01 +02002205out:
William Juul52c07962007-10-31 13:53:06 +01002206 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002207 return ret;
2208}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002209
2210/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002211 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2212 * @mtd: MTD device structure
2213 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002214 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002215 * Standard erase command for NAND chips.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002216 */
William Juul52c07962007-10-31 13:53:06 +01002217static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002218{
William Juul52c07962007-10-31 13:53:06 +01002219 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002220 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002221 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2222 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002223}
2224
2225/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002226 * multi_erase_cmd - [GENERIC] AND specific block erase command function
2227 * @mtd: MTD device structure
2228 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002229 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002230 * AND multi block erase command function. Erase 4 consecutive blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002231 */
William Juul52c07962007-10-31 13:53:06 +01002232static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002233{
William Juul52c07962007-10-31 13:53:06 +01002234 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002235 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002236 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2237 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2238 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2239 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2240 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002241}
2242
2243/**
2244 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002245 * @mtd: MTD device structure
2246 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002247 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002248 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002249 */
William Juul52c07962007-10-31 13:53:06 +01002250static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002251{
William Juul52c07962007-10-31 13:53:06 +01002252 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002253}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002254
William Juul52c07962007-10-31 13:53:06 +01002255#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002256/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002257 * nand_erase_nand - [INTERN] erase block(s)
2258 * @mtd: MTD device structure
2259 * @instr: erase instruction
2260 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002261 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002262 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002263 */
William Juul52c07962007-10-31 13:53:06 +01002264int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2265 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002266{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002267 int page, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002268 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002269 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juul52c07962007-10-31 13:53:06 +01002270 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002271 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002272
Christian Hitz13fc0e22011-10-12 09:32:01 +02002273 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2274 __func__, (unsigned long long)instr->addr,
2275 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002276
Christian Hitzb8a6b372011-10-12 09:32:02 +02002277 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002278 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002279
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002280 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002281 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002282
2283 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002284 page = (int)(instr->addr >> chip->page_shift);
2285 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002286
2287 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002288 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002289
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002290 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002291 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002292
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002293 /* Check, if it is write protected */
2294 if (nand_check_wp(mtd)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002295 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2296 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002297 instr->state = MTD_ERASE_FAILED;
2298 goto erase_exit;
2299 }
2300
William Juul52c07962007-10-31 13:53:06 +01002301 /*
2302 * If BBT requires refresh, set the BBT page mask to see if the BBT
2303 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2304 * can not be matched. This is also done when the bbt is actually
Sergey Lapin3a38a552013-01-14 03:46:50 +00002305 * erased to avoid recursive updates.
William Juul52c07962007-10-31 13:53:06 +01002306 */
2307 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2308 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2309
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002310 /* Loop through the pages */
2311 len = instr->len;
2312
2313 instr->state = MTD_ERASING;
2314
2315 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002316 WATCHDOG_RESET();
Sergey Lapin3a38a552013-01-14 03:46:50 +00002317 /* Check if we have a bad block, we do not erase bad blocks! */
Marek Vasut971d9a12011-09-12 06:04:06 +02002318 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juul52c07962007-10-31 13:53:06 +01002319 chip->page_shift, 0, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002320 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2321 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002322 instr->state = MTD_ERASE_FAILED;
2323 goto erase_exit;
2324 }
William Juul52c07962007-10-31 13:53:06 +01002325
2326 /*
2327 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00002328 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01002329 */
2330 if (page <= chip->pagebuf && chip->pagebuf <
2331 (page + pages_per_block))
2332 chip->pagebuf = -1;
2333
2334 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002335
William Juul52c07962007-10-31 13:53:06 +01002336 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002337
William Juul52c07962007-10-31 13:53:06 +01002338 /*
2339 * See if operation failed and additional status checks are
2340 * available
2341 */
2342 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2343 status = chip->errstat(mtd, chip, FL_ERASING,
2344 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002345
2346 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002347 if (status & NAND_STATUS_FAIL) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002348 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2349 "page 0x%08x\n", __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002350 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002351 instr->fail_addr =
2352 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002353 goto erase_exit;
2354 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002355
William Juul52c07962007-10-31 13:53:06 +01002356 /*
2357 * If BBT requires refresh, set the BBT rewrite flag to the
Sergey Lapin3a38a552013-01-14 03:46:50 +00002358 * page being erased.
William Juul52c07962007-10-31 13:53:06 +01002359 */
2360 if (bbt_masked_page != 0xffffffff &&
2361 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002362 rewrite_bbt[chipnr] =
2363 ((loff_t)page << chip->page_shift);
William Juul52c07962007-10-31 13:53:06 +01002364
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002365 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002366 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002367 page += pages_per_block;
2368
2369 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002370 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002371 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002372 chip->select_chip(mtd, -1);
2373 chip->select_chip(mtd, chipnr);
2374
2375 /*
2376 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Sergey Lapin3a38a552013-01-14 03:46:50 +00002377 * page mask to see if this BBT should be rewritten.
William Juul52c07962007-10-31 13:53:06 +01002378 */
2379 if (bbt_masked_page != 0xffffffff &&
2380 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2381 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2382 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002383 }
2384 }
2385 instr->state = MTD_ERASE_DONE;
2386
Christian Hitz13fc0e22011-10-12 09:32:01 +02002387erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002388
2389 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002390
2391 /* Deselect and wake up anyone waiting on the device */
2392 nand_release_device(mtd);
2393
Scott Wood3628f002008-10-24 16:20:43 -05002394 /* Do call back function */
2395 if (!ret)
2396 mtd_erase_callback(instr);
2397
William Juul52c07962007-10-31 13:53:06 +01002398 /*
2399 * If BBT requires refresh and erase was successful, rewrite any
Sergey Lapin3a38a552013-01-14 03:46:50 +00002400 * selected bad block tables.
William Juul52c07962007-10-31 13:53:06 +01002401 */
2402 if (bbt_masked_page == 0xffffffff || ret)
2403 return ret;
2404
2405 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2406 if (!rewrite_bbt[chipnr])
2407 continue;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002408 /* Update the BBT for chip */
Christian Hitz13fc0e22011-10-12 09:32:01 +02002409 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2410 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2411 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
William Juul52c07962007-10-31 13:53:06 +01002412 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2413 }
2414
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002415 /* Return more or less happy */
2416 return ret;
2417}
2418
2419/**
2420 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00002421 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002422 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002423 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002424 */
William Juul52c07962007-10-31 13:53:06 +01002425static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002426{
William Juul52c07962007-10-31 13:53:06 +01002427 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002428
Christian Hitz13fc0e22011-10-12 09:32:01 +02002429 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002430
2431 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002432 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002433 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002434 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002435}
2436
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002437/**
William Juul52c07962007-10-31 13:53:06 +01002438 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002439 * @mtd: MTD device structure
2440 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002441 */
William Juul52c07962007-10-31 13:53:06 +01002442static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002443{
William Juul52c07962007-10-31 13:53:06 +01002444 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002445}
2446
2447/**
William Juul52c07962007-10-31 13:53:06 +01002448 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002449 * @mtd: MTD device structure
2450 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002451 */
William Juul52c07962007-10-31 13:53:06 +01002452static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002453{
William Juul52c07962007-10-31 13:53:06 +01002454 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002455 int ret;
2456
Christian Hitzb8a6b372011-10-12 09:32:02 +02002457 ret = nand_block_isbad(mtd, ofs);
2458 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002459 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002460 if (ret > 0)
2461 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002462 return ret;
2463 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002464
William Juul52c07962007-10-31 13:53:06 +01002465 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002466}
2467
Sergey Lapin3a38a552013-01-14 03:46:50 +00002468 /**
2469 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2470 * @mtd: MTD device structure
2471 * @chip: nand chip info structure
2472 * @addr: feature address.
2473 * @subfeature_param: the subfeature parameters, a four bytes array.
2474 */
2475static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2476 int addr, uint8_t *subfeature_param)
2477{
2478 int status;
2479
2480 if (!chip->onfi_version)
2481 return -EINVAL;
2482
2483 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2484 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2485 status = chip->waitfunc(mtd, chip);
2486 if (status & NAND_STATUS_FAIL)
2487 return -EIO;
2488 return 0;
2489}
2490
2491/**
2492 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2493 * @mtd: MTD device structure
2494 * @chip: nand chip info structure
2495 * @addr: feature address.
2496 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01002497 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002498static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2499 int addr, uint8_t *subfeature_param)
2500{
2501 if (!chip->onfi_version)
2502 return -EINVAL;
2503
2504 /* clear the sub feature parameters */
2505 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2506
2507 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2508 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2509 return 0;
2510}
2511
2512/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01002513static void nand_set_defaults(struct nand_chip *chip, int busw)
2514{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002515 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002516 if (!chip->chip_delay)
2517 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002518
2519 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002520 if (chip->cmdfunc == NULL)
2521 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002522
2523 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002524 if (chip->waitfunc == NULL)
2525 chip->waitfunc = nand_wait;
2526
2527 if (!chip->select_chip)
2528 chip->select_chip = nand_select_chip;
2529 if (!chip->read_byte)
2530 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2531 if (!chip->read_word)
2532 chip->read_word = nand_read_word;
2533 if (!chip->block_bad)
2534 chip->block_bad = nand_block_bad;
2535 if (!chip->block_markbad)
2536 chip->block_markbad = nand_default_block_markbad;
2537 if (!chip->write_buf)
2538 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2539 if (!chip->read_buf)
2540 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2541 if (!chip->verify_buf)
2542 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2543 if (!chip->scan_bbt)
2544 chip->scan_bbt = nand_default_bbt;
Scott Woodda0f4392010-08-25 17:42:49 -05002545 if (!chip->controller)
William Juul52c07962007-10-31 13:53:06 +01002546 chip->controller = &chip->hwcontrol;
William Juul52c07962007-10-31 13:53:06 +01002547}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002548
Florian Fainellic98a9352011-02-25 00:01:34 +00002549#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Sergey Lapin3a38a552013-01-14 03:46:50 +00002550/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002551static void sanitize_string(char *s, size_t len)
2552{
2553 ssize_t i;
2554
Sergey Lapin3a38a552013-01-14 03:46:50 +00002555 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002556 s[len - 1] = 0;
2557
Sergey Lapin3a38a552013-01-14 03:46:50 +00002558 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002559 for (i = 0; i < len - 1; i++) {
2560 if (s[i] < ' ' || s[i] > 127)
2561 s[i] = '?';
2562 }
2563
Sergey Lapin3a38a552013-01-14 03:46:50 +00002564 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002565 strim(s);
2566}
2567
Florian Fainellic98a9352011-02-25 00:01:34 +00002568static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2569{
2570 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00002571 while (len--) {
2572 crc ^= *p++ << 8;
2573 for (i = 0; i < 8; i++)
2574 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2575 }
2576
2577 return crc;
2578}
2579
2580/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00002581 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00002582 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02002583static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00002584 int *busw)
2585{
2586 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisf3832302014-05-06 00:46:16 +05302587 int i, j;
Florian Fainellic98a9352011-02-25 00:01:34 +00002588 int val;
2589
Sergey Lapin3a38a552013-01-14 03:46:50 +00002590 /* Try ONFI for unknown chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00002591 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2592 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2593 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2594 return 0;
2595
Florian Fainellic98a9352011-02-25 00:01:34 +00002596 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2597 for (i = 0; i < 3; i++) {
Brian Norrisf3832302014-05-06 00:46:16 +05302598 for (j = 0; j < sizeof(*p); j++)
2599 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00002600 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02002601 le16_to_cpu(p->crc)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002602 pr_info("ONFI param page %d valid\n", i);
Florian Fainellic98a9352011-02-25 00:01:34 +00002603 break;
2604 }
2605 }
2606
2607 if (i == 3)
2608 return 0;
2609
Sergey Lapin3a38a552013-01-14 03:46:50 +00002610 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00002611 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002612 if (val & (1 << 5))
2613 chip->onfi_version = 23;
2614 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00002615 chip->onfi_version = 22;
2616 else if (val & (1 << 3))
2617 chip->onfi_version = 21;
2618 else if (val & (1 << 2))
2619 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002620 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00002621 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002622 else
2623 chip->onfi_version = 0;
2624
2625 if (!chip->onfi_version) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002626 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002627 return 0;
2628 }
Florian Fainellic98a9352011-02-25 00:01:34 +00002629
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002630 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2631 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00002632 if (!mtd->name)
2633 mtd->name = p->model;
Florian Fainellic98a9352011-02-25 00:01:34 +00002634 mtd->writesize = le32_to_cpu(p->byte_per_page);
2635 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2636 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01002637 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2638 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Florian Fainellic98a9352011-02-25 00:01:34 +00002639 *busw = 0;
2640 if (le16_to_cpu(p->features) & 1)
2641 *busw = NAND_BUSWIDTH_16;
2642
Sergey Lapin3a38a552013-01-14 03:46:50 +00002643 pr_info("ONFI flash detected\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00002644 return 1;
2645}
2646#else
2647static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2648 struct nand_chip *chip,
2649 int *busw)
2650{
2651 return 0;
2652}
2653#endif
2654
William Juul52c07962007-10-31 13:53:06 +01002655/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00002656 * nand_id_has_period - Check if an ID string has a given wraparound period
2657 * @id_data: the ID string
2658 * @arrlen: the length of the @id_data array
2659 * @period: the period of repitition
2660 *
2661 * Check if an ID string is repeated within a given sequence of bytes at
2662 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2663 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2664 * if the repetition has a period of @period; otherwise, returns zero.
2665 */
2666static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2667{
2668 int i, j;
2669 for (i = 0; i < period; i++)
2670 for (j = i + period; j < arrlen; j += period)
2671 if (id_data[i] != id_data[j])
2672 return 0;
2673 return 1;
2674}
2675
2676/*
2677 * nand_id_len - Get the length of an ID string returned by CMD_READID
2678 * @id_data: the ID string
2679 * @arrlen: the length of the @id_data array
2680
2681 * Returns the length of the ID string, according to known wraparound/trailing
2682 * zero patterns. If no pattern exists, returns the length of the array.
2683 */
2684static int nand_id_len(u8 *id_data, int arrlen)
2685{
2686 int last_nonzero, period;
2687
2688 /* Find last non-zero byte */
2689 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2690 if (id_data[last_nonzero])
2691 break;
2692
2693 /* All zeros */
2694 if (last_nonzero < 0)
2695 return 0;
2696
2697 /* Calculate wraparound period */
2698 for (period = 1; period < arrlen; period++)
2699 if (nand_id_has_period(id_data, arrlen, period))
2700 break;
2701
2702 /* There's a repeated pattern */
2703 if (period < arrlen)
2704 return period;
2705
2706 /* There are trailing zeros */
2707 if (last_nonzero < arrlen - 1)
2708 return last_nonzero + 1;
2709
2710 /* No pattern detected */
2711 return arrlen;
2712}
2713
2714/*
2715 * Many new NAND share similar device ID codes, which represent the size of the
2716 * chip. The rest of the parameters must be decoded according to generic or
2717 * manufacturer-specific "extended ID" decoding patterns.
2718 */
2719static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2720 u8 id_data[8], int *busw)
2721{
2722 int extid, id_len;
2723 /* The 3rd id byte holds MLC / multichip data */
2724 chip->cellinfo = id_data[2];
2725 /* The 4th id byte is the important one */
2726 extid = id_data[3];
2727
2728 id_len = nand_id_len(id_data, 8);
2729
2730 /*
2731 * Field definitions are in the following datasheets:
2732 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2733 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
2734 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
2735 *
2736 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2737 * ID to decide what to do.
2738 */
2739 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
2740 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2741 id_data[5] != 0x00) {
2742 /* Calc pagesize */
2743 mtd->writesize = 2048 << (extid & 0x03);
2744 extid >>= 2;
2745 /* Calc oobsize */
2746 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2747 case 1:
2748 mtd->oobsize = 128;
2749 break;
2750 case 2:
2751 mtd->oobsize = 218;
2752 break;
2753 case 3:
2754 mtd->oobsize = 400;
2755 break;
2756 case 4:
2757 mtd->oobsize = 436;
2758 break;
2759 case 5:
2760 mtd->oobsize = 512;
2761 break;
2762 case 6:
2763 default: /* Other cases are "reserved" (unknown) */
2764 mtd->oobsize = 640;
2765 break;
2766 }
2767 extid >>= 2;
2768 /* Calc blocksize */
2769 mtd->erasesize = (128 * 1024) <<
2770 (((extid >> 1) & 0x04) | (extid & 0x03));
2771 *busw = 0;
2772 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2773 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2774 unsigned int tmp;
2775
2776 /* Calc pagesize */
2777 mtd->writesize = 2048 << (extid & 0x03);
2778 extid >>= 2;
2779 /* Calc oobsize */
2780 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2781 case 0:
2782 mtd->oobsize = 128;
2783 break;
2784 case 1:
2785 mtd->oobsize = 224;
2786 break;
2787 case 2:
2788 mtd->oobsize = 448;
2789 break;
2790 case 3:
2791 mtd->oobsize = 64;
2792 break;
2793 case 4:
2794 mtd->oobsize = 32;
2795 break;
2796 case 5:
2797 mtd->oobsize = 16;
2798 break;
2799 default:
2800 mtd->oobsize = 640;
2801 break;
2802 }
2803 extid >>= 2;
2804 /* Calc blocksize */
2805 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
2806 if (tmp < 0x03)
2807 mtd->erasesize = (128 * 1024) << tmp;
2808 else if (tmp == 0x03)
2809 mtd->erasesize = 768 * 1024;
2810 else
2811 mtd->erasesize = (64 * 1024) << tmp;
2812 *busw = 0;
2813 } else {
2814 /* Calc pagesize */
2815 mtd->writesize = 1024 << (extid & 0x03);
2816 extid >>= 2;
2817 /* Calc oobsize */
2818 mtd->oobsize = (8 << (extid & 0x01)) *
2819 (mtd->writesize >> 9);
2820 extid >>= 2;
2821 /* Calc blocksize. Blocksize is multiples of 64KiB */
2822 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2823 extid >>= 2;
2824 /* Get buswidth information */
2825 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2826 }
2827}
2828
2829 /*
2830 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
2831 * decodes a matching ID table entry and assigns the MTD size parameters for
2832 * the chip.
2833 */
2834static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
2835 const struct nand_flash_dev *type, u8 id_data[8],
2836 int *busw)
2837{
2838 int maf_id = id_data[0];
2839
2840 mtd->erasesize = type->erasesize;
2841 mtd->writesize = type->pagesize;
2842 mtd->oobsize = mtd->writesize / 32;
2843 *busw = type->options & NAND_BUSWIDTH_16;
2844
2845 /*
2846 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
2847 * some Spansion chips have erasesize that conflicts with size
2848 * listed in nand_ids table.
2849 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
2850 */
2851 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
2852 && id_data[6] == 0x00 && id_data[7] == 0x00
2853 && mtd->writesize == 512) {
2854 mtd->erasesize = 128 * 1024;
2855 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
2856 }
2857}
2858
2859 /*
2860 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
2861 * heuristic patterns using various detected parameters (e.g., manufacturer,
2862 * page size, cell-type information).
2863 */
2864static void nand_decode_bbm_options(struct mtd_info *mtd,
2865 struct nand_chip *chip, u8 id_data[8])
2866{
2867 int maf_id = id_data[0];
2868
2869 /* Set the bad block position */
2870 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
2871 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2872 else
2873 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2874
2875 /*
2876 * Bad block marker is stored in the last page of each block on Samsung
2877 * and Hynix MLC devices; stored in first two pages of each block on
2878 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
2879 * AMD/Spansion, and Macronix. All others scan only the first page.
2880 */
2881 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2882 (maf_id == NAND_MFR_SAMSUNG ||
2883 maf_id == NAND_MFR_HYNIX))
2884 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
2885 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2886 (maf_id == NAND_MFR_SAMSUNG ||
2887 maf_id == NAND_MFR_HYNIX ||
2888 maf_id == NAND_MFR_TOSHIBA ||
2889 maf_id == NAND_MFR_AMD ||
2890 maf_id == NAND_MFR_MACRONIX)) ||
2891 (mtd->writesize == 2048 &&
2892 maf_id == NAND_MFR_MICRON))
2893 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
2894}
2895
2896/*
2897 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01002898 */
Mike Frysinger6c285f62010-10-20 01:15:21 +00002899static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01002900 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00002901 int busw,
2902 int *maf_id, int *dev_id,
Lei Wen75bde942011-01-06 09:48:18 +08002903 const struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01002904{
Kim Phillips0b2efcf2012-10-29 13:34:46 +00002905 const char *name;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002906 int i, maf_idx;
2907 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002908
2909 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002910 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002911
Karl Beldanb6322fc2008-09-15 16:08:03 +02002912 /*
2913 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002914 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02002915 */
2916 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2917
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002918 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002919 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002920
2921 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002922 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00002923 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002924
Sergey Lapin3a38a552013-01-14 03:46:50 +00002925 /*
2926 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05002927 * interface concerns can cause random data which looks like a
2928 * possibly credible NAND flash to appear. If the two results do
2929 * not match, ignore the device completely.
2930 */
2931
2932 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2933
Sergey Lapin3a38a552013-01-14 03:46:50 +00002934 /* Read entire ID string */
2935 for (i = 0; i < 8; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02002936 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05002937
Christian Hitzb8a6b372011-10-12 09:32:02 +02002938 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002939 pr_info("%s: second ID read did not match "
2940 "%02x,%02x against %02x,%02x\n", __func__,
2941 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05002942 return ERR_PTR(-ENODEV);
2943 }
2944
Lei Wen75bde942011-01-06 09:48:18 +08002945 if (!type)
2946 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002947
Lei Wen75bde942011-01-06 09:48:18 +08002948 for (; type->name != NULL; type++)
Florian Fainellic98a9352011-02-25 00:01:34 +00002949 if (*dev_id == type->id)
Wolfgang Denka4de8352011-02-02 22:36:10 +01002950 break;
Lei Wen75bde942011-01-06 09:48:18 +08002951
Christian Hitzb8a6b372011-10-12 09:32:02 +02002952 chip->onfi_version = 0;
2953 if (!type->name || !type->pagesize) {
2954 /* Check is chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002955 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02002956 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02002957 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002958
Christian Hitzb8a6b372011-10-12 09:32:02 +02002959 if (!type->name)
2960 return ERR_PTR(-ENODEV);
2961
William Juul52c07962007-10-31 13:53:06 +01002962 if (!mtd->name)
2963 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002964
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002965 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002966
Christian Hitzb8a6b372011-10-12 09:32:02 +02002967 if (!type->pagesize && chip->init_size) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002968 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitzb8a6b372011-10-12 09:32:02 +02002969 busw = chip->init_size(mtd, chip, id_data);
2970 } else if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002971 /* Decode parameters from extended ID */
2972 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002973 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002974 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002975 }
Florian Fainellic98a9352011-02-25 00:01:34 +00002976 /* Get chip options, preserve non chip based options */
Marek Vasutfc417192012-08-30 13:39:38 +00002977 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00002978
Sergey Lapin3a38a552013-01-14 03:46:50 +00002979 /*
2980 * Check if chip is not a Samsung device. Do not clear the
2981 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02002982 */
2983 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2984 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2985ident_done:
2986
William Juul52c07962007-10-31 13:53:06 +01002987 /* Try to identify manufacturer */
2988 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2989 if (nand_manuf_ids[maf_idx].id == *maf_id)
2990 break;
2991 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002992
William Juul52c07962007-10-31 13:53:06 +01002993 /*
2994 * Check, if buswidth is correct. Hardware drivers should set
Sergey Lapin3a38a552013-01-14 03:46:50 +00002995 * chip correct!
William Juul52c07962007-10-31 13:53:06 +01002996 */
2997 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002998 pr_info("NAND device: Manufacturer ID:"
2999 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3000 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
3001 pr_warn("NAND bus width %d instead %d bit\n",
3002 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3003 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01003004 return ERR_PTR(-EINVAL);
3005 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003006
Sergey Lapin3a38a552013-01-14 03:46:50 +00003007 nand_decode_bbm_options(mtd, chip, id_data);
3008
William Juul52c07962007-10-31 13:53:06 +01003009 /* Calculate the address shift from the page size */
3010 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003011 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01003012 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003013
William Juul52c07962007-10-31 13:53:06 +01003014 chip->bbt_erase_shift = chip->phys_erase_shift =
3015 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003016 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05003017 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003018 else {
3019 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3020 chip->chip_shift += 32 - 1;
3021 }
3022
3023 chip->badblockbits = 8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003024
William Juul52c07962007-10-31 13:53:06 +01003025 /* Check for AND chips with 4 page planes */
3026 if (chip->options & NAND_4PAGE_ARRAY)
3027 chip->erase_cmd = multi_erase_cmd;
3028 else
3029 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003030
Sergey Lapin3a38a552013-01-14 03:46:50 +00003031 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01003032 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3033 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003034
Kim Phillips0b2efcf2012-10-29 13:34:46 +00003035 name = type->name;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003036#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Kim Phillips0b2efcf2012-10-29 13:34:46 +00003037 if (chip->onfi_version)
3038 name = chip->onfi_params.model;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003039#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003040 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3041 " page size: %d, OOB size: %d\n",
3042 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3043 name,
3044 mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01003045
3046 return type;
3047}
3048
3049/**
3050 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003051 * @mtd: MTD device structure
3052 * @maxchips: number of chips to scan for
3053 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01003054 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003055 * This is the first phase of the normal nand_scan() function. It reads the
3056 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01003057 *
3058 * The mtd->owner field must be set to the module of the caller.
3059 */
Lei Wen75bde942011-01-06 09:48:18 +08003060int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3061 const struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01003062{
Florian Fainellic98a9352011-02-25 00:01:34 +00003063 int i, busw, nand_maf_id, nand_dev_id;
William Juul52c07962007-10-31 13:53:06 +01003064 struct nand_chip *chip = mtd->priv;
Mike Frysinger6c285f62010-10-20 01:15:21 +00003065 const struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003066
William Juul52c07962007-10-31 13:53:06 +01003067 /* Get buswidth to select the correct functions */
3068 busw = chip->options & NAND_BUSWIDTH_16;
3069 /* Set the default functions */
3070 nand_set_defaults(chip, busw);
3071
3072 /* Read the flash type */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003073 type = nand_get_flash_type(mtd, chip, busw,
3074 &nand_maf_id, &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01003075
3076 if (IS_ERR(type)) {
Peter Tyserf96047a2009-02-04 13:39:40 -06003077#ifndef CONFIG_SYS_NAND_QUIET_TEST
Sergey Lapin3a38a552013-01-14 03:46:50 +00003078 pr_warn("No NAND device found\n");
Peter Tyserf96047a2009-02-04 13:39:40 -06003079#endif
William Juul52c07962007-10-31 13:53:06 +01003080 chip->select_chip(mtd, -1);
3081 return PTR_ERR(type);
3082 }
3083
3084 /* Check for a chip array */
3085 for (i = 1; i < maxchips; i++) {
3086 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02003087 /* See comment in nand_get_flash_type for reset */
3088 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01003089 /* Send the command for reading device ID */
3090 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003091 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003092 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainellic98a9352011-02-25 00:01:34 +00003093 nand_dev_id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003094 break;
3095 }
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01003096#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003097 if (i > 1)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003098 pr_info("%d NAND chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01003099#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003100
William Juul52c07962007-10-31 13:53:06 +01003101 /* Store the number of chips and calc total size for mtd */
3102 chip->numchips = i;
3103 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003104
William Juul52c07962007-10-31 13:53:06 +01003105 return 0;
3106}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003107
William Juul52c07962007-10-31 13:53:06 +01003108
3109/**
3110 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003111 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01003112 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003113 * This is the second phase of the normal nand_scan() function. It fills out
3114 * all the uninitialized function pointers with the defaults and scans for a
3115 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01003116 */
3117int nand_scan_tail(struct mtd_info *mtd)
3118{
3119 int i;
3120 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003121
Sergey Lapin3a38a552013-01-14 03:46:50 +00003122 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3123 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3124 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3125
William Juul52c07962007-10-31 13:53:06 +01003126 if (!(chip->options & NAND_OWN_BUFFERS))
Simon Glass78851792012-07-29 20:53:25 +00003127 chip->buffers = memalign(ARCH_DMA_MINALIGN,
3128 sizeof(*chip->buffers));
William Juul52c07962007-10-31 13:53:06 +01003129 if (!chip->buffers)
3130 return -ENOMEM;
3131
3132 /* Set the internal oob buffer location, just after the page data */
3133 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3134
3135 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003136 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01003137 */
Christian Hitz55f7bca2011-10-12 09:31:59 +02003138 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003139 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003140 case 8:
William Juul52c07962007-10-31 13:53:06 +01003141 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003142 break;
3143 case 16:
William Juul52c07962007-10-31 13:53:06 +01003144 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003145 break;
3146 case 64:
William Juul52c07962007-10-31 13:53:06 +01003147 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003148 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003149 case 128:
William Juul52c07962007-10-31 13:53:06 +01003150 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003151 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003152 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003153 pr_warn("No oob scheme defined for oobsize %d\n",
3154 mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003155 }
3156 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003157
William Juul52c07962007-10-31 13:53:06 +01003158 if (!chip->write_page)
3159 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003160
Sergey Lapin3a38a552013-01-14 03:46:50 +00003161 /* set for ONFI nand */
3162 if (!chip->onfi_set_features)
3163 chip->onfi_set_features = nand_onfi_set_features;
3164 if (!chip->onfi_get_features)
3165 chip->onfi_get_features = nand_onfi_get_features;
3166
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003167 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003168 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01003169 * selected and we have 256 byte pagesize fallback to software ECC
3170 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003171
William Juul52c07962007-10-31 13:53:06 +01003172 switch (chip->ecc.mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04003173 case NAND_ECC_HW_OOB_FIRST:
3174 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3175 if (!chip->ecc.calculate || !chip->ecc.correct ||
3176 !chip->ecc.hwctl) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003177 pr_warn("No ECC functions supplied; "
3178 "hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04003179 BUG();
3180 }
3181 if (!chip->ecc.read_page)
3182 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3183
William Juul52c07962007-10-31 13:53:06 +01003184 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003185 /* Use standard hwecc read page function? */
William Juul52c07962007-10-31 13:53:06 +01003186 if (!chip->ecc.read_page)
3187 chip->ecc.read_page = nand_read_page_hwecc;
3188 if (!chip->ecc.write_page)
3189 chip->ecc.write_page = nand_write_page_hwecc;
David Brownellee86b8d2009-11-07 16:27:01 -05003190 if (!chip->ecc.read_page_raw)
3191 chip->ecc.read_page_raw = nand_read_page_raw;
3192 if (!chip->ecc.write_page_raw)
3193 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01003194 if (!chip->ecc.read_oob)
3195 chip->ecc.read_oob = nand_read_oob_std;
3196 if (!chip->ecc.write_oob)
3197 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003198
William Juul52c07962007-10-31 13:53:06 +01003199 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05003200 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3201 !chip->ecc.hwctl) &&
3202 (!chip->ecc.read_page ||
3203 chip->ecc.read_page == nand_read_page_hwecc ||
3204 !chip->ecc.write_page ||
3205 chip->ecc.write_page == nand_write_page_hwecc)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003206 pr_warn("No ECC functions supplied; "
3207 "hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01003208 BUG();
3209 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00003210 /* Use standard syndrome read/write page function? */
William Juul52c07962007-10-31 13:53:06 +01003211 if (!chip->ecc.read_page)
3212 chip->ecc.read_page = nand_read_page_syndrome;
3213 if (!chip->ecc.write_page)
3214 chip->ecc.write_page = nand_write_page_syndrome;
David Brownellee86b8d2009-11-07 16:27:01 -05003215 if (!chip->ecc.read_page_raw)
3216 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3217 if (!chip->ecc.write_page_raw)
3218 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juul52c07962007-10-31 13:53:06 +01003219 if (!chip->ecc.read_oob)
3220 chip->ecc.read_oob = nand_read_oob_syndrome;
3221 if (!chip->ecc.write_oob)
3222 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003223
Sergey Lapin3a38a552013-01-14 03:46:50 +00003224 if (mtd->writesize >= chip->ecc.size) {
3225 if (!chip->ecc.strength) {
3226 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3227 BUG();
3228 }
William Juul52c07962007-10-31 13:53:06 +01003229 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003230 }
3231 pr_warn("%d byte HW ECC not possible on "
3232 "%d byte page size, fallback to SW ECC\n",
3233 chip->ecc.size, mtd->writesize);
William Juul52c07962007-10-31 13:53:06 +01003234 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003235
William Juul52c07962007-10-31 13:53:06 +01003236 case NAND_ECC_SOFT:
3237 chip->ecc.calculate = nand_calculate_ecc;
3238 chip->ecc.correct = nand_correct_data;
3239 chip->ecc.read_page = nand_read_page_swecc;
Scott Wood3628f002008-10-24 16:20:43 -05003240 chip->ecc.read_subpage = nand_read_subpage;
William Juul52c07962007-10-31 13:53:06 +01003241 chip->ecc.write_page = nand_write_page_swecc;
David Brownellee86b8d2009-11-07 16:27:01 -05003242 chip->ecc.read_page_raw = nand_read_page_raw;
3243 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01003244 chip->ecc.read_oob = nand_read_oob_std;
3245 chip->ecc.write_oob = nand_write_oob_std;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003246 if (!chip->ecc.size)
3247 chip->ecc.size = 256;
William Juul52c07962007-10-31 13:53:06 +01003248 chip->ecc.bytes = 3;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003249 chip->ecc.strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003250 break;
3251
Christian Hitz55f7bca2011-10-12 09:31:59 +02003252 case NAND_ECC_SOFT_BCH:
3253 if (!mtd_nand_has_bch()) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003254 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Christian Hitz55f7bca2011-10-12 09:31:59 +02003255 return -EINVAL;
3256 }
3257 chip->ecc.calculate = nand_bch_calculate_ecc;
3258 chip->ecc.correct = nand_bch_correct_data;
3259 chip->ecc.read_page = nand_read_page_swecc;
3260 chip->ecc.read_subpage = nand_read_subpage;
3261 chip->ecc.write_page = nand_write_page_swecc;
3262 chip->ecc.read_page_raw = nand_read_page_raw;
3263 chip->ecc.write_page_raw = nand_write_page_raw;
3264 chip->ecc.read_oob = nand_read_oob_std;
3265 chip->ecc.write_oob = nand_write_oob_std;
3266 /*
3267 * Board driver should supply ecc.size and ecc.bytes values to
3268 * select how many bits are correctable; see nand_bch_init()
Sergey Lapin3a38a552013-01-14 03:46:50 +00003269 * for details. Otherwise, default to 4 bits for large page
3270 * devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02003271 */
3272 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3273 chip->ecc.size = 512;
3274 chip->ecc.bytes = 7;
3275 }
3276 chip->ecc.priv = nand_bch_init(mtd,
3277 chip->ecc.size,
3278 chip->ecc.bytes,
3279 &chip->ecc.layout);
3280 if (!chip->ecc.priv)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003281 pr_warn("BCH ECC initialization failed!\n");
3282 chip->ecc.strength =
3283 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Christian Hitz55f7bca2011-10-12 09:31:59 +02003284 break;
3285
William Juul52c07962007-10-31 13:53:06 +01003286 case NAND_ECC_NONE:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003287 pr_warn("NAND_ECC_NONE selected by board driver. "
Wolfgang Denkec7fbf52013-10-04 17:43:24 +02003288 "This is not recommended !!\n");
William Juul52c07962007-10-31 13:53:06 +01003289 chip->ecc.read_page = nand_read_page_raw;
3290 chip->ecc.write_page = nand_write_page_raw;
3291 chip->ecc.read_oob = nand_read_oob_std;
David Brownellee86b8d2009-11-07 16:27:01 -05003292 chip->ecc.read_page_raw = nand_read_page_raw;
3293 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01003294 chip->ecc.write_oob = nand_write_oob_std;
3295 chip->ecc.size = mtd->writesize;
3296 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003297 break;
3298
3299 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003300 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
William Juul52c07962007-10-31 13:53:06 +01003301 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003302 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003303
Sergey Lapin3a38a552013-01-14 03:46:50 +00003304 /* For many systems, the standard OOB write also works for raw */
3305 if (!chip->ecc.read_oob_raw)
3306 chip->ecc.read_oob_raw = chip->ecc.read_oob;
3307 if (!chip->ecc.write_oob_raw)
3308 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3309
William Juul52c07962007-10-31 13:53:06 +01003310 /*
3311 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00003312 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01003313 */
3314 chip->ecc.layout->oobavail = 0;
Sandeep Paulrajeec6cb32009-11-07 14:25:18 -05003315 for (i = 0; chip->ecc.layout->oobfree[i].length
3316 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juul52c07962007-10-31 13:53:06 +01003317 chip->ecc.layout->oobavail +=
3318 chip->ecc.layout->oobfree[i].length;
3319 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003320
William Juul52c07962007-10-31 13:53:06 +01003321 /*
3322 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00003323 * mode.
William Juul52c07962007-10-31 13:53:06 +01003324 */
3325 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003326 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003327 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01003328 BUG();
3329 }
3330 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003331
Sergey Lapin3a38a552013-01-14 03:46:50 +00003332 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
William Juul52c07962007-10-31 13:53:06 +01003333 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3334 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02003335 switch (chip->ecc.steps) {
William Juul52c07962007-10-31 13:53:06 +01003336 case 2:
3337 mtd->subpage_sft = 1;
3338 break;
3339 case 4:
3340 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05003341 case 16:
William Juul52c07962007-10-31 13:53:06 +01003342 mtd->subpage_sft = 2;
3343 break;
3344 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003345 }
William Juul52c07962007-10-31 13:53:06 +01003346 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003347
William Juul52c07962007-10-31 13:53:06 +01003348 /* Initialize state */
3349 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003350
3351 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01003352 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003353
3354 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01003355 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003356
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00003357 /* Large page NAND with SOFT_ECC should support subpage reads */
3358 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3359 chip->options |= NAND_SUBPAGE_READ;
3360
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003361 /* Fill in remaining MTD driver data */
3362 mtd->type = MTD_NANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003363 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3364 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003365 mtd->_erase = nand_erase;
3366 mtd->_point = NULL;
3367 mtd->_unpoint = NULL;
3368 mtd->_read = nand_read;
3369 mtd->_write = nand_write;
3370 mtd->_read_oob = nand_read_oob;
3371 mtd->_write_oob = nand_write_oob;
3372 mtd->_sync = nand_sync;
3373 mtd->_lock = NULL;
3374 mtd->_unlock = NULL;
3375 mtd->_block_isbad = nand_block_isbad;
3376 mtd->_block_markbad = nand_block_markbad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003377
Sergey Lapin3a38a552013-01-14 03:46:50 +00003378 /* propagate ecc info to mtd_info */
William Juul52c07962007-10-31 13:53:06 +01003379 mtd->ecclayout = chip->ecc.layout;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003380 mtd->ecc_strength = chip->ecc.strength;
3381 /*
3382 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3383 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3384 * properly set.
3385 */
3386 if (!mtd->bitflip_threshold)
3387 mtd->bitflip_threshold = mtd->ecc_strength;
William Juul52c07962007-10-31 13:53:06 +01003388
3389 /* Check, if we should skip the bad block table scan */
3390 if (chip->options & NAND_SKIP_BBTSCAN)
Scott Woodf2f5c9e2012-02-20 14:50:39 -06003391 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01003392
Scott Woodf2f5c9e2012-02-20 14:50:39 -06003393 return 0;
William Juul52c07962007-10-31 13:53:06 +01003394}
3395
William Juul52c07962007-10-31 13:53:06 +01003396/**
3397 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003398 * @mtd: MTD device structure
3399 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01003400 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003401 * This fills out all the uninitialized function pointers with the defaults.
3402 * The flash ID is read and the mtd/chip structures are filled with the
3403 * appropriate values. The mtd->owner field must be set to the module of the
3404 * caller.
William Juul52c07962007-10-31 13:53:06 +01003405 */
3406int nand_scan(struct mtd_info *mtd, int maxchips)
3407{
3408 int ret;
3409
Lei Wen75bde942011-01-06 09:48:18 +08003410 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01003411 if (!ret)
3412 ret = nand_scan_tail(mtd);
3413 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003414}
3415
3416/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003417 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003418 * @mtd: MTD device structure
3419 */
William Juul52c07962007-10-31 13:53:06 +01003420void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003421{
William Juul52c07962007-10-31 13:53:06 +01003422 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003423
Christian Hitz55f7bca2011-10-12 09:31:59 +02003424 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3425 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3426
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003427#ifdef CONFIG_MTD_PARTITIONS
3428 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01003429 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003430#endif
William Juul52c07962007-10-31 13:53:06 +01003431
3432 /* Free bad block table memory */
3433 kfree(chip->bbt);
3434 if (!(chip->options & NAND_OWN_BUFFERS))
3435 kfree(chip->buffers);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003436
3437 /* Free bad block descriptor memory */
3438 if (chip->badblock_pattern && chip->badblock_pattern->options
3439 & NAND_BBT_DYNAMICSTRUCT)
3440 kfree(chip->badblock_pattern);
William Juul52c07962007-10-31 13:53:06 +01003441}