blob: 71f5027889f992801feb039a80a520e14466fa72 [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
24 * if we have HW ecc support.
25 * 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
137 /* Do not allow past end of device */
138 if (ofs + len > mtd->size) {
139 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
140 __func__);
141 ret = -EINVAL;
142 }
143
144 return ret;
145}
146
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200147/**
148 * nand_release_device - [GENERIC] release chip
149 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200150 *
151 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200152 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200153static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100154{
Christian Hitzb8a6b372011-10-12 09:32:02 +0200155 struct nand_chip *chip = mtd->priv;
156
157 /* De-select the NAND device */
158 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100159}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160
161/**
162 * nand_read_byte - [DEFAULT] read one byte from the chip
163 * @mtd: MTD device structure
164 *
165 * Default read function for 8bit buswith
166 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000167uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200168{
William Juul52c07962007-10-31 13:53:06 +0100169 struct nand_chip *chip = mtd->priv;
170 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200171}
172
173/**
174 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
175 * @mtd: MTD device structure
176 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200177 * Default read function for 16bit buswith with
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200178 * endianess conversion
179 */
William Juul52c07962007-10-31 13:53:06 +0100180static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200181{
William Juul52c07962007-10-31 13:53:06 +0100182 struct nand_chip *chip = mtd->priv;
183 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184}
185
186/**
187 * nand_read_word - [DEFAULT] read one word from the chip
188 * @mtd: MTD device structure
189 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200190 * Default read function for 16bit buswith without
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200191 * endianess conversion
192 */
193static u16 nand_read_word(struct mtd_info *mtd)
194{
William Juul52c07962007-10-31 13:53:06 +0100195 struct nand_chip *chip = mtd->priv;
196 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200197}
198
199/**
200 * nand_select_chip - [DEFAULT] control CE line
201 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100202 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200203 *
204 * Default select function for 1 chip devices.
205 */
William Juul52c07962007-10-31 13:53:06 +0100206static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200207{
William Juul52c07962007-10-31 13:53:06 +0100208 struct nand_chip *chip = mtd->priv;
209
210 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200211 case -1:
William Juul52c07962007-10-31 13:53:06 +0100212 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200213 break;
214 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200215 break;
216
217 default:
218 BUG();
219 }
220}
221
222/**
223 * nand_write_buf - [DEFAULT] write buffer to chip
224 * @mtd: MTD device structure
225 * @buf: data buffer
226 * @len: number of bytes to write
227 *
228 * Default write function for 8bit buswith
229 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000230void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200231{
232 int i;
William Juul52c07962007-10-31 13:53:06 +0100233 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200234
William Juul52c07962007-10-31 13:53:06 +0100235 for (i = 0; i < len; i++)
236 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200237}
238
239/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200240 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200241 * @mtd: MTD device structure
242 * @buf: buffer to store date
243 * @len: number of bytes to read
244 *
245 * Default read function for 8bit buswith
246 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400247void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200248{
249 int i;
William Juul52c07962007-10-31 13:53:06 +0100250 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200251
William Juul52c07962007-10-31 13:53:06 +0100252 for (i = 0; i < len; i++)
253 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200254}
255
256/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200257 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200258 * @mtd: MTD device structure
259 * @buf: buffer containing the data to compare
260 * @len: number of bytes to compare
261 *
262 * Default verify function for 8bit buswith
263 */
William Juul52c07962007-10-31 13:53:06 +0100264static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200265{
266 int i;
William Juul52c07962007-10-31 13:53:06 +0100267 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200268
William Juul52c07962007-10-31 13:53:06 +0100269 for (i = 0; i < len; i++)
270 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200271 return -EFAULT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200272 return 0;
273}
274
275/**
276 * nand_write_buf16 - [DEFAULT] write buffer to chip
277 * @mtd: MTD device structure
278 * @buf: data buffer
279 * @len: number of bytes to write
280 *
281 * Default write function for 16bit buswith
282 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000283void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200284{
285 int i;
William Juul52c07962007-10-31 13:53:06 +0100286 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200287 u16 *p = (u16 *) buf;
288 len >>= 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200289
William Juul52c07962007-10-31 13:53:06 +0100290 for (i = 0; i < len; i++)
291 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200292
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200293}
294
295/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200296 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200297 * @mtd: MTD device structure
298 * @buf: buffer to store date
299 * @len: number of bytes to read
300 *
301 * Default read function for 16bit buswith
302 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400303void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200304{
305 int i;
William Juul52c07962007-10-31 13:53:06 +0100306 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307 u16 *p = (u16 *) buf;
308 len >>= 1;
309
William Juul52c07962007-10-31 13:53:06 +0100310 for (i = 0; i < len; i++)
311 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312}
313
314/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200315 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316 * @mtd: MTD device structure
317 * @buf: buffer containing the data to compare
318 * @len: number of bytes to compare
319 *
320 * Default verify function for 16bit buswith
321 */
William Juul52c07962007-10-31 13:53:06 +0100322static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323{
324 int i;
William Juul52c07962007-10-31 13:53:06 +0100325 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200326 u16 *p = (u16 *) buf;
327 len >>= 1;
328
William Juul52c07962007-10-31 13:53:06 +0100329 for (i = 0; i < len; i++)
330 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200331 return -EFAULT;
332
333 return 0;
334}
335
336/**
337 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
338 * @mtd: MTD device structure
339 * @ofs: offset from device start
340 * @getchip: 0, if the chip is already selected
341 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200342 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200343 */
344static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
345{
346 int page, chipnr, res = 0;
William Juul52c07962007-10-31 13:53:06 +0100347 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200348 u16 bad;
349
Christian Hitzb8a6b372011-10-12 09:32:02 +0200350 if (chip->options & NAND_BBT_SCANLASTPAGE)
351 ofs += mtd->erasesize - mtd->writesize;
352
William Juul52c07962007-10-31 13:53:06 +0100353 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200354
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200355 if (getchip) {
William Juul52c07962007-10-31 13:53:06 +0100356 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200357
William Juul52c07962007-10-31 13:53:06 +0100358 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200359
360 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +0100361 chip->select_chip(mtd, chipnr);
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200362 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200363
William Juul52c07962007-10-31 13:53:06 +0100364 if (chip->options & NAND_BUSWIDTH_16) {
365 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
366 page);
367 bad = cpu_to_le16(chip->read_word(mtd));
368 if (chip->badblockpos & 0x1)
369 bad >>= 8;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200370 else
371 bad &= 0xFF;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200372 } else {
William Juul52c07962007-10-31 13:53:06 +0100373 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200374 bad = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200375 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200376
Christian Hitzb8a6b372011-10-12 09:32:02 +0200377 if (likely(chip->badblockbits == 8))
378 res = bad != 0xFF;
379 else
380 res = hweight8(bad) < chip->badblockbits;
381
William Juul52c07962007-10-31 13:53:06 +0100382 if (getchip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200383 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200384
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200385 return res;
386}
387
388/**
389 * nand_default_block_markbad - [DEFAULT] mark a block bad
390 * @mtd: MTD device structure
391 * @ofs: offset from device start
392 *
393 * This is the default implementation, which can be overridden by
394 * a hardware specific driver.
395*/
396static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
397{
William Juul52c07962007-10-31 13:53:06 +0100398 struct nand_chip *chip = mtd->priv;
399 uint8_t buf[2] = { 0, 0 };
Christian Hitzb8a6b372011-10-12 09:32:02 +0200400 int block, ret, i = 0;
401
402 if (chip->options & NAND_BBT_SCANLASTPAGE)
403 ofs += mtd->erasesize - mtd->writesize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200404
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200405 /* Get block number */
William Juul52c07962007-10-31 13:53:06 +0100406 block = (int)(ofs >> chip->bbt_erase_shift);
407 if (chip->bbt)
408 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200409
410 /* Do we have a flash based bad block table ? */
William Juul52c07962007-10-31 13:53:06 +0100411 if (chip->options & NAND_USE_FLASH_BBT)
412 ret = nand_update_bbt(mtd, ofs);
413 else {
Scott Wood3628f002008-10-24 16:20:43 -0500414 nand_get_device(chip, mtd, FL_WRITING);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200415
416 /* Write to first two pages and to byte 1 and 6 if necessary.
417 * If we write to more than one location, the first error
418 * encountered quits the procedure. We write two bytes per
419 * location, so we dont have to mess with 16 bit access.
420 */
421 do {
422 chip->ops.len = chip->ops.ooblen = 2;
423 chip->ops.datbuf = NULL;
424 chip->ops.oobbuf = buf;
425 chip->ops.ooboffs = chip->badblockpos & ~0x01;
426
427 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200428
Christian Hitzb8a6b372011-10-12 09:32:02 +0200429 if (!ret && (chip->options & NAND_BBT_SCANBYTE1AND6)) {
430 chip->ops.ooboffs = NAND_SMALL_BADBLOCK_POS
431 & ~0x01;
432 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
433 }
434 i++;
435 ofs += mtd->writesize;
436 } while (!ret && (chip->options & NAND_BBT_SCAN2NDPAGE) &&
437 i < 2);
438
Scott Wood3628f002008-10-24 16:20:43 -0500439 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100440 }
441 if (!ret)
442 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500443
William Juul52c07962007-10-31 13:53:06 +0100444 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200445}
446
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200447/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200448 * nand_check_wp - [GENERIC] check if the chip is write protected
449 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200450 * Check, if the device is write protected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200451 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200452 * The function expects, that the device is already selected
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200453 */
William Juul52c07962007-10-31 13:53:06 +0100454static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200455{
William Juul52c07962007-10-31 13:53:06 +0100456 struct nand_chip *chip = mtd->priv;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200457
458 /* broken xD cards report WP despite being writable */
459 if (chip->options & NAND_BROKEN_XD)
460 return 0;
461
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200462 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100463 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
464 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200465}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100466
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200467/**
468 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
469 * @mtd: MTD device structure
470 * @ofs: offset from device start
471 * @getchip: 0, if the chip is already selected
472 * @allowbbt: 1, if its allowed to access the bbt area
473 *
474 * Check, if the block is bad. Either by reading the bad block table or
475 * calling of the scan function.
476 */
William Juul52c07962007-10-31 13:53:06 +0100477static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
478 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200479{
William Juul52c07962007-10-31 13:53:06 +0100480 struct nand_chip *chip = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200481
Scott Woodf2f5c9e2012-02-20 14:50:39 -0600482 if (!(chip->options & NAND_BBT_SCANNED)) {
483 chip->options |= NAND_BBT_SCANNED;
484 chip->scan_bbt(mtd);
485 }
486
William Juul52c07962007-10-31 13:53:06 +0100487 if (!chip->bbt)
488 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200489
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200490 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100491 return nand_isbad_bbt(mtd, ofs, allowbbt);
492}
493
494/*
495 * Wait for the ready pin, after a command
496 * The timeout is catched later.
497 */
William Juul52c07962007-10-31 13:53:06 +0100498void nand_wait_ready(struct mtd_info *mtd)
499{
500 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200501 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000502 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100503
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000504 time_start = get_timer(0);
Stefan Roesea5c312c2008-01-05 16:43:25 +0100505
506 /* wait until command is processed or timeout occures */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000507 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100508 if (chip->dev_ready)
509 if (chip->dev_ready(mtd))
510 break;
511 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200512}
513
514/**
515 * nand_command - [DEFAULT] Send command to NAND device
516 * @mtd: MTD device structure
517 * @command: the command to be sent
518 * @column: the column address for this command, -1 if none
519 * @page_addr: the page address for this command, -1 if none
520 *
521 * Send command to NAND device. This function is used for small page
522 * devices (256/512 Bytes per page)
523 */
William Juul52c07962007-10-31 13:53:06 +0100524static void nand_command(struct mtd_info *mtd, unsigned int command,
525 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200526{
William Juul52c07962007-10-31 13:53:06 +0100527 register struct nand_chip *chip = mtd->priv;
528 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyserf9f36222009-02-04 13:47:22 -0600529 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200530
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200531 /*
532 * Write out the command to the device.
533 */
534 if (command == NAND_CMD_SEQIN) {
535 int readcmd;
536
William Juul52c07962007-10-31 13:53:06 +0100537 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200538 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100539 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200540 readcmd = NAND_CMD_READOOB;
541 } else if (column < 256) {
542 /* First 256 bytes --> READ0 */
543 readcmd = NAND_CMD_READ0;
544 } else {
545 column -= 256;
546 readcmd = NAND_CMD_READ1;
547 }
William Juul52c07962007-10-31 13:53:06 +0100548 chip->cmd_ctrl(mtd, readcmd, ctrl);
549 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200550 }
William Juul52c07962007-10-31 13:53:06 +0100551 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200552
William Juul52c07962007-10-31 13:53:06 +0100553 /*
554 * Address cycle, when necessary
555 */
556 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
557 /* Serially input address */
558 if (column != -1) {
559 /* Adjust columns for 16 bit buswidth */
560 if (chip->options & NAND_BUSWIDTH_16)
561 column >>= 1;
562 chip->cmd_ctrl(mtd, column, ctrl);
563 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200564 }
William Juul52c07962007-10-31 13:53:06 +0100565 if (page_addr != -1) {
566 chip->cmd_ctrl(mtd, page_addr, ctrl);
567 ctrl &= ~NAND_CTRL_CHANGE;
568 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
569 /* One more address cycle for devices > 32MiB */
570 if (chip->chipsize > (32 << 20))
571 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
572 }
573 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200574
575 /*
576 * program and erase have their own busy handlers
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200577 * status and sequential in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100578 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200579 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200580
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200581 case NAND_CMD_PAGEPROG:
582 case NAND_CMD_ERASE1:
583 case NAND_CMD_ERASE2:
584 case NAND_CMD_SEQIN:
585 case NAND_CMD_STATUS:
586 return;
587
588 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100589 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200590 break;
William Juul52c07962007-10-31 13:53:06 +0100591 udelay(chip->chip_delay);
592 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
593 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
594 chip->cmd_ctrl(mtd,
595 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600596 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
597 (rst_sts_cnt--));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200598 return;
599
William Juul52c07962007-10-31 13:53:06 +0100600 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200601 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200602 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 * If we don't have access to the busy pin, we apply the given
604 * command delay
William Juul52c07962007-10-31 13:53:06 +0100605 */
606 if (!chip->dev_ready) {
607 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200608 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200609 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200610 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 /* Apply this short delay always to ensure that we do wait tWB in
612 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100613 ndelay(100);
614
615 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200616}
617
618/**
619 * nand_command_lp - [DEFAULT] Send command to NAND large page device
620 * @mtd: MTD device structure
621 * @command: the command to be sent
622 * @column: the column address for this command, -1 if none
623 * @page_addr: the page address for this command, -1 if none
624 *
William Juul52c07962007-10-31 13:53:06 +0100625 * Send command to NAND device. This is the version for the new large page
626 * devices We dont have the separate regions as we have in the small page
627 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200628 */
William Juul52c07962007-10-31 13:53:06 +0100629static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
630 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200631{
William Juul52c07962007-10-31 13:53:06 +0100632 register struct nand_chip *chip = mtd->priv;
Peter Tyserf9f36222009-02-04 13:47:22 -0600633 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200634
635 /* Emulate NAND_CMD_READOOB */
636 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100637 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200638 command = NAND_CMD_READ0;
639 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200640
William Juul52c07962007-10-31 13:53:06 +0100641 /* Command latch cycle */
642 chip->cmd_ctrl(mtd, command & 0xff,
643 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200644
645 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100646 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647
648 /* Serially input address */
649 if (column != -1) {
650 /* Adjust columns for 16 bit buswidth */
William Juul52c07962007-10-31 13:53:06 +0100651 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200652 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100653 chip->cmd_ctrl(mtd, column, ctrl);
654 ctrl &= ~NAND_CTRL_CHANGE;
655 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200656 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200657 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100658 chip->cmd_ctrl(mtd, page_addr, ctrl);
659 chip->cmd_ctrl(mtd, page_addr >> 8,
660 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200661 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100662 if (chip->chipsize > (128 << 20))
663 chip->cmd_ctrl(mtd, page_addr >> 16,
664 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200665 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200666 }
William Juul52c07962007-10-31 13:53:06 +0100667 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200668
669 /*
670 * program and erase have their own busy handlers
William Juul52c07962007-10-31 13:53:06 +0100671 * status, sequential in, and deplete1 need no delay
672 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200673 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200674
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200675 case NAND_CMD_CACHEDPROG:
676 case NAND_CMD_PAGEPROG:
677 case NAND_CMD_ERASE1:
678 case NAND_CMD_ERASE2:
679 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100680 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200681 case NAND_CMD_STATUS:
William Juul52c07962007-10-31 13:53:06 +0100682 case NAND_CMD_DEPLETE1:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200683 return;
684
William Juul52c07962007-10-31 13:53:06 +0100685 /*
686 * read error status commands require only a short delay
687 */
688 case NAND_CMD_STATUS_ERROR:
689 case NAND_CMD_STATUS_ERROR0:
690 case NAND_CMD_STATUS_ERROR1:
691 case NAND_CMD_STATUS_ERROR2:
692 case NAND_CMD_STATUS_ERROR3:
693 udelay(chip->chip_delay);
694 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200695
696 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100697 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200698 break;
William Juul52c07962007-10-31 13:53:06 +0100699 udelay(chip->chip_delay);
700 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
701 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
702 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
703 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyserf9f36222009-02-04 13:47:22 -0600704 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
705 (rst_sts_cnt--));
William Juul52c07962007-10-31 13:53:06 +0100706 return;
707
708 case NAND_CMD_RNDOUT:
709 /* No ready / busy check necessary */
710 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
711 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
712 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
713 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200714 return;
715
716 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100717 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
718 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
719 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
720 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200721
William Juul52c07962007-10-31 13:53:06 +0100722 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200723 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200724 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200725 * If we don't have access to the busy pin, we apply the given
726 * command delay
William Juul52c07962007-10-31 13:53:06 +0100727 */
728 if (!chip->dev_ready) {
729 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200730 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200731 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200733
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200734 /* Apply this short delay always to ensure that we do wait tWB in
735 * any case on any machine. */
William Juul52c07962007-10-31 13:53:06 +0100736 ndelay(100);
737
738 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200739}
740
741/**
742 * nand_get_device - [GENERIC] Get chip for selected access
William Juul52c07962007-10-31 13:53:06 +0100743 * @chip: the nand chip descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200744 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200745 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200746 *
747 * Get the device and lock it for exclusive access
748 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200749static int
750nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100751{
Christian Hitzb8a6b372011-10-12 09:32:02 +0200752 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100753 return 0;
754}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200755
756/**
757 * nand_wait - [DEFAULT] wait until the command is done
758 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +0100759 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200760 *
761 * Wait for command done. This applies to erase and program only
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200762 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200763 * general NAND and SmartMedia specs
William Juul52c07962007-10-31 13:53:06 +0100764 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200765static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200766{
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100767 unsigned long timeo;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200768 int state = chip->state;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000769 u32 time_start;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100770
771 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200772 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100773 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200774 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100775
Christian Hitzb8a6b372011-10-12 09:32:02 +0200776 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
777 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100778 else
Christian Hitzb8a6b372011-10-12 09:32:02 +0200779 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100780
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000781 time_start = get_timer(0);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100782
783 while (1) {
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000784 if (get_timer(time_start) > timeo) {
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100785 printf("Timeout!");
Stefan Roeseef26d242006-11-27 17:22:19 +0100786 return 0x01;
787 }
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100788
Christian Hitzb8a6b372011-10-12 09:32:02 +0200789 if (chip->dev_ready) {
790 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100791 break;
792 } else {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200793 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100794 break;
795 }
796 }
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100797#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000798 time_start = get_timer(0);
799 while (get_timer(time_start) < 10)
800 ;
Bartlomiej Sieka582f1a32006-03-05 18:57:33 +0100801#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100802
Christian Hitzb8a6b372011-10-12 09:32:02 +0200803 return (int)chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200804}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200805
806/**
William Juul52c07962007-10-31 13:53:06 +0100807 * nand_read_page_raw - [Intern] read raw page data without ecc
808 * @mtd: mtd info structure
809 * @chip: nand chip info structure
810 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -0500811 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -0500812 *
813 * Not for syndrome calculating ecc controllers, which use a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200814 */
William Juul52c07962007-10-31 13:53:06 +0100815static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400816 uint8_t *buf, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200817{
William Juul52c07962007-10-31 13:53:06 +0100818 chip->read_buf(mtd, buf, mtd->writesize);
819 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
820 return 0;
821}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200822
William Juul52c07962007-10-31 13:53:06 +0100823/**
David Brownellee86b8d2009-11-07 16:27:01 -0500824 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
825 * @mtd: mtd info structure
826 * @chip: nand chip info structure
827 * @buf: buffer to store read data
828 * @page: page number to read
829 *
830 * We need a special oob layout and handling even when OOB isn't used.
831 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200832static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
833 struct nand_chip *chip,
834 uint8_t *buf, int page)
David Brownellee86b8d2009-11-07 16:27:01 -0500835{
836 int eccsize = chip->ecc.size;
837 int eccbytes = chip->ecc.bytes;
838 uint8_t *oob = chip->oob_poi;
839 int steps, size;
840
841 for (steps = chip->ecc.steps; steps > 0; steps--) {
842 chip->read_buf(mtd, buf, eccsize);
843 buf += eccsize;
844
845 if (chip->ecc.prepad) {
846 chip->read_buf(mtd, oob, chip->ecc.prepad);
847 oob += chip->ecc.prepad;
848 }
849
850 chip->read_buf(mtd, oob, eccbytes);
851 oob += eccbytes;
852
853 if (chip->ecc.postpad) {
854 chip->read_buf(mtd, oob, chip->ecc.postpad);
855 oob += chip->ecc.postpad;
856 }
857 }
858
859 size = mtd->oobsize - (oob - chip->oob_poi);
860 if (size)
861 chip->read_buf(mtd, oob, size);
862
863 return 0;
864}
865
866/**
William Juul52c07962007-10-31 13:53:06 +0100867 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
868 * @mtd: mtd info structure
869 * @chip: nand chip info structure
870 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -0500871 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +0100872 */
873static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400874 uint8_t *buf, int page)
William Juul52c07962007-10-31 13:53:06 +0100875{
876 int i, eccsize = chip->ecc.size;
877 int eccbytes = chip->ecc.bytes;
878 int eccsteps = chip->ecc.steps;
879 uint8_t *p = buf;
880 uint8_t *ecc_calc = chip->buffers->ecccalc;
881 uint8_t *ecc_code = chip->buffers->ecccode;
882 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200883
Sandeep Paulraj883189e2009-08-10 13:27:46 -0400884 chip->ecc.read_page_raw(mtd, chip, buf, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200885
William Juul52c07962007-10-31 13:53:06 +0100886 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
887 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200888
William Juul52c07962007-10-31 13:53:06 +0100889 for (i = 0; i < chip->ecc.total; i++)
890 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200891
William Juul52c07962007-10-31 13:53:06 +0100892 eccsteps = chip->ecc.steps;
893 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200894
William Juul52c07962007-10-31 13:53:06 +0100895 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
896 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200897
William Juul52c07962007-10-31 13:53:06 +0100898 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood3628f002008-10-24 16:20:43 -0500899 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +0100900 mtd->ecc_stats.failed++;
901 else
902 mtd->ecc_stats.corrected += stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200903 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200904 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200905}
906
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200907/**
Scott Wood3628f002008-10-24 16:20:43 -0500908 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
909 * @mtd: mtd info structure
910 * @chip: nand chip info structure
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -0500911 * @data_offs: offset of requested data within the page
912 * @readlen: data length
913 * @bufpoi: buffer to store read data
Scott Wood3628f002008-10-24 16:20:43 -0500914 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200915static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
916 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Scott Wood3628f002008-10-24 16:20:43 -0500917{
918 int start_step, end_step, num_steps;
919 uint32_t *eccpos = chip->ecc.layout->eccpos;
920 uint8_t *p;
921 int data_col_addr, i, gaps = 0;
922 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
923 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200924 int index = 0;
Scott Wood3628f002008-10-24 16:20:43 -0500925
926 /* Column address wihin the page aligned to ECC size (256bytes). */
927 start_step = data_offs / chip->ecc.size;
928 end_step = (data_offs + readlen - 1) / chip->ecc.size;
929 num_steps = end_step - start_step + 1;
930
931 /* Data size aligned to ECC ecc.size*/
932 datafrag_len = num_steps * chip->ecc.size;
933 eccfrag_len = num_steps * chip->ecc.bytes;
934
935 data_col_addr = start_step * chip->ecc.size;
936 /* If we read not a page aligned data */
937 if (data_col_addr != 0)
938 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
939
940 p = bufpoi + data_col_addr;
941 chip->read_buf(mtd, p, datafrag_len);
942
943 /* Calculate ECC */
944 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
945 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
946
947 /* The performance is faster if to position offsets
948 according to ecc.pos. Let make sure here that
949 there are no gaps in ecc positions */
950 for (i = 0; i < eccfrag_len - 1; i++) {
951 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
952 eccpos[i + start_step * chip->ecc.bytes + 1]) {
953 gaps = 1;
954 break;
955 }
956 }
957 if (gaps) {
958 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
959 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
960 } else {
961 /* send the command to read the particular ecc bytes */
962 /* take care about buswidth alignment in read_buf */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200963 index = start_step * chip->ecc.bytes;
964
965 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -0500966 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200967 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -0500968 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200969 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -0500970 aligned_len++;
971
Christian Hitzb8a6b372011-10-12 09:32:02 +0200972 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
973 mtd->writesize + aligned_pos, -1);
Scott Wood3628f002008-10-24 16:20:43 -0500974 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
975 }
976
977 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200978 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -0500979
980 p = bufpoi + data_col_addr;
981 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
982 int stat;
983
Christian Hitzb8a6b372011-10-12 09:32:02 +0200984 stat = chip->ecc.correct(mtd, p,
985 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
986 if (stat < 0)
Scott Wood3628f002008-10-24 16:20:43 -0500987 mtd->ecc_stats.failed++;
988 else
989 mtd->ecc_stats.corrected += stat;
990 }
991 return 0;
992}
993
994/**
William Juul52c07962007-10-31 13:53:06 +0100995 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
996 * @mtd: mtd info structure
997 * @chip: nand chip info structure
998 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -0500999 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001000 *
William Juul52c07962007-10-31 13:53:06 +01001001 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001002 */
William Juul52c07962007-10-31 13:53:06 +01001003static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001004 uint8_t *buf, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001005{
William Juul52c07962007-10-31 13:53:06 +01001006 int i, eccsize = chip->ecc.size;
1007 int eccbytes = chip->ecc.bytes;
1008 int eccsteps = chip->ecc.steps;
1009 uint8_t *p = buf;
1010 uint8_t *ecc_calc = chip->buffers->ecccalc;
1011 uint8_t *ecc_code = chip->buffers->ecccode;
1012 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001013
William Juul52c07962007-10-31 13:53:06 +01001014 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1015 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1016 chip->read_buf(mtd, p, eccsize);
1017 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1018 }
1019 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001020
William Juul52c07962007-10-31 13:53:06 +01001021 for (i = 0; i < chip->ecc.total; i++)
1022 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001023
William Juul52c07962007-10-31 13:53:06 +01001024 eccsteps = chip->ecc.steps;
1025 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001026
William Juul52c07962007-10-31 13:53:06 +01001027 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1028 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001029
William Juul52c07962007-10-31 13:53:06 +01001030 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj16add232009-11-07 14:25:03 -05001031 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001032 mtd->ecc_stats.failed++;
1033 else
1034 mtd->ecc_stats.corrected += stat;
1035 }
1036 return 0;
1037}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001038
William Juul52c07962007-10-31 13:53:06 +01001039/**
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001040 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1041 * @mtd: mtd info structure
1042 * @chip: nand chip info structure
1043 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -05001044 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001045 *
1046 * Hardware ECC for large page chips, require OOB to be read first.
1047 * For this ECC mode, the write_page method is re-used from ECC_HW.
1048 * These methods read/write ECC from the OOB area, unlike the
1049 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1050 * "infix ECC" scheme and reads/writes ECC from the data area, by
1051 * overwriting the NAND manufacturer bad block markings.
1052 */
1053static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1054 struct nand_chip *chip, uint8_t *buf, int page)
1055{
1056 int i, eccsize = chip->ecc.size;
1057 int eccbytes = chip->ecc.bytes;
1058 int eccsteps = chip->ecc.steps;
1059 uint8_t *p = buf;
1060 uint8_t *ecc_code = chip->buffers->ecccode;
1061 uint32_t *eccpos = chip->ecc.layout->eccpos;
1062 uint8_t *ecc_calc = chip->buffers->ecccalc;
1063
1064 /* Read the OOB area first */
1065 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1066 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1067 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1068
1069 for (i = 0; i < chip->ecc.total; i++)
1070 ecc_code[i] = chip->oob_poi[eccpos[i]];
1071
1072 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1073 int stat;
1074
1075 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1076 chip->read_buf(mtd, p, eccsize);
1077 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1078
1079 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1080 if (stat < 0)
1081 mtd->ecc_stats.failed++;
1082 else
1083 mtd->ecc_stats.corrected += stat;
1084 }
1085 return 0;
1086}
1087
1088/**
William Juul52c07962007-10-31 13:53:06 +01001089 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1090 * @mtd: mtd info structure
1091 * @chip: nand chip info structure
1092 * @buf: buffer to store read data
Sandeep Paulraj22bc7ba2009-11-07 14:24:50 -05001093 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001094 *
1095 * The hw generator calculates the error syndrome automatically. Therefor
1096 * we need a special oob layout and handling.
1097 */
1098static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001099 uint8_t *buf, int page)
William Juul52c07962007-10-31 13:53:06 +01001100{
1101 int i, eccsize = chip->ecc.size;
1102 int eccbytes = chip->ecc.bytes;
1103 int eccsteps = chip->ecc.steps;
1104 uint8_t *p = buf;
1105 uint8_t *oob = chip->oob_poi;
1106
1107 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1108 int stat;
1109
1110 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1111 chip->read_buf(mtd, p, eccsize);
1112
1113 if (chip->ecc.prepad) {
1114 chip->read_buf(mtd, oob, chip->ecc.prepad);
1115 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001116 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001117
William Juul52c07962007-10-31 13:53:06 +01001118 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1119 chip->read_buf(mtd, oob, eccbytes);
1120 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001121
Scott Wood3628f002008-10-24 16:20:43 -05001122 if (stat < 0)
William Juul52c07962007-10-31 13:53:06 +01001123 mtd->ecc_stats.failed++;
1124 else
1125 mtd->ecc_stats.corrected += stat;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001126
William Juul52c07962007-10-31 13:53:06 +01001127 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001128
William Juul52c07962007-10-31 13:53:06 +01001129 if (chip->ecc.postpad) {
1130 chip->read_buf(mtd, oob, chip->ecc.postpad);
1131 oob += chip->ecc.postpad;
1132 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001133 }
William Juul52c07962007-10-31 13:53:06 +01001134
1135 /* Calculate remaining oob bytes */
1136 i = mtd->oobsize - (oob - chip->oob_poi);
1137 if (i)
1138 chip->read_buf(mtd, oob, i);
1139
1140 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001141}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001142
1143/**
William Juul52c07962007-10-31 13:53:06 +01001144 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1145 * @chip: nand chip structure
1146 * @oob: oob destination address
1147 * @ops: oob ops structure
1148 * @len: size of oob to transfer
1149 */
1150static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1151 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001152{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001153 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001154
1155 case MTD_OOB_PLACE:
1156 case MTD_OOB_RAW:
1157 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1158 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001159
William Juul52c07962007-10-31 13:53:06 +01001160 case MTD_OOB_AUTO: {
1161 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1162 uint32_t boffs = 0, roffs = ops->ooboffs;
1163 size_t bytes = 0;
1164
Christian Hitz13fc0e22011-10-12 09:32:01 +02001165 for (; free->length && len; free++, len -= bytes) {
William Juul52c07962007-10-31 13:53:06 +01001166 /* Read request not from offset 0 ? */
1167 if (unlikely(roffs)) {
1168 if (roffs >= free->length) {
1169 roffs -= free->length;
1170 continue;
1171 }
1172 boffs = free->offset + roffs;
1173 bytes = min_t(size_t, len,
1174 (free->length - roffs));
1175 roffs = 0;
1176 } else {
1177 bytes = min_t(size_t, len, free->length);
1178 boffs = free->offset;
1179 }
1180 memcpy(oob, chip->oob_poi + boffs, bytes);
1181 oob += bytes;
1182 }
1183 return oob;
1184 }
1185 default:
1186 BUG();
1187 }
1188 return NULL;
1189}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001190
1191/**
William Juul52c07962007-10-31 13:53:06 +01001192 * nand_do_read_ops - [Internal] Read data with ECC
1193 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001194 * @mtd: MTD device structure
1195 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001196 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001197 *
William Juul52c07962007-10-31 13:53:06 +01001198 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001199 */
William Juul52c07962007-10-31 13:53:06 +01001200static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1201 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001202{
William Juul52c07962007-10-31 13:53:06 +01001203 int chipnr, page, realpage, col, bytes, aligned;
1204 struct nand_chip *chip = mtd->priv;
1205 struct mtd_ecc_stats stats;
1206 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1207 int sndcmd = 1;
1208 int ret = 0;
1209 uint32_t readlen = ops->len;
1210 uint32_t oobreadlen = ops->ooblen;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001211 uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
1212 mtd->oobavail : mtd->oobsize;
1213
William Juul52c07962007-10-31 13:53:06 +01001214 uint8_t *bufpoi, *oob, *buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001215
William Juul52c07962007-10-31 13:53:06 +01001216 stats = mtd->ecc_stats;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001217
William Juul52c07962007-10-31 13:53:06 +01001218 chipnr = (int)(from >> chip->chip_shift);
1219 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001220
William Juul52c07962007-10-31 13:53:06 +01001221 realpage = (int)(from >> chip->page_shift);
1222 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001223
William Juul52c07962007-10-31 13:53:06 +01001224 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001225
William Juul52c07962007-10-31 13:53:06 +01001226 buf = ops->datbuf;
1227 oob = ops->oobbuf;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001228
Christian Hitz13fc0e22011-10-12 09:32:01 +02001229 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06001230 WATCHDOG_RESET();
1231
William Juul52c07962007-10-31 13:53:06 +01001232 bytes = min(mtd->writesize - col, readlen);
1233 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001234
William Juul52c07962007-10-31 13:53:06 +01001235 /* Is the current page in the buffer ? */
1236 if (realpage != chip->pagebuf || oob) {
1237 bufpoi = aligned ? buf : chip->buffers->databuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001238
William Juul52c07962007-10-31 13:53:06 +01001239 if (likely(sndcmd)) {
1240 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1241 sndcmd = 0;
1242 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001243
William Juul52c07962007-10-31 13:53:06 +01001244 /* Now read the page into the buffer */
1245 if (unlikely(ops->mode == MTD_OOB_RAW))
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001246 ret = chip->ecc.read_page_raw(mtd, chip,
Christian Hitz13fc0e22011-10-12 09:32:01 +02001247 bufpoi, page);
Scott Wood3628f002008-10-24 16:20:43 -05001248 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02001249 ret = chip->ecc.read_subpage(mtd, chip,
1250 col, bytes, bufpoi);
William Juul52c07962007-10-31 13:53:06 +01001251 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001252 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Christian Hitz13fc0e22011-10-12 09:32:01 +02001253 page);
William Juul52c07962007-10-31 13:53:06 +01001254 if (ret < 0)
1255 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001256
William Juul52c07962007-10-31 13:53:06 +01001257 /* Transfer not aligned data */
1258 if (!aligned) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001259 if (!NAND_SUBPAGE_READ(chip) && !oob &&
1260 !(mtd->ecc_stats.failed - stats.failed))
Scott Wood3628f002008-10-24 16:20:43 -05001261 chip->pagebuf = realpage;
William Juul52c07962007-10-31 13:53:06 +01001262 memcpy(buf, chip->buffers->databuf + col, bytes);
1263 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001264
William Juul52c07962007-10-31 13:53:06 +01001265 buf += bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001266
William Juul52c07962007-10-31 13:53:06 +01001267 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001268
1269 int toread = min(oobreadlen, max_oobsize);
1270
1271 if (toread) {
1272 oob = nand_transfer_oob(chip,
1273 oob, ops, toread);
1274 oobreadlen -= toread;
1275 }
William Juul52c07962007-10-31 13:53:06 +01001276 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001277
William Juul52c07962007-10-31 13:53:06 +01001278 if (!(chip->options & NAND_NO_READRDY)) {
1279 /*
1280 * Apply delay or wait for ready/busy pin. Do
1281 * this before the AUTOINCR check, so no
1282 * problems arise if a chip which does auto
1283 * increment is marked as NOAUTOINCR by the
1284 * board driver.
1285 */
1286 if (!chip->dev_ready)
1287 udelay(chip->chip_delay);
1288 else
1289 nand_wait_ready(mtd);
1290 }
1291 } else {
1292 memcpy(buf, chip->buffers->databuf + col, bytes);
1293 buf += bytes;
1294 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001295
William Juul52c07962007-10-31 13:53:06 +01001296 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001297
William Juul52c07962007-10-31 13:53:06 +01001298 if (!readlen)
1299 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001300
William Juul52c07962007-10-31 13:53:06 +01001301 /* For subsequent reads align to page boundary. */
1302 col = 0;
1303 /* Increment page address */
1304 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001305
William Juul52c07962007-10-31 13:53:06 +01001306 page = realpage & chip->pagemask;
1307 /* Check, if we cross a chip boundary */
1308 if (!page) {
1309 chipnr++;
1310 chip->select_chip(mtd, -1);
1311 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001312 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001313
William Juul52c07962007-10-31 13:53:06 +01001314 /* Check, if the chip supports auto page increment
1315 * or if we have hit a block boundary.
1316 */
1317 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1318 sndcmd = 1;
1319 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001320
William Juul52c07962007-10-31 13:53:06 +01001321 ops->retlen = ops->len - (size_t) readlen;
1322 if (oob)
1323 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001324
William Juul52c07962007-10-31 13:53:06 +01001325 if (ret)
1326 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001327
William Juul52c07962007-10-31 13:53:06 +01001328 if (mtd->ecc_stats.failed - stats.failed)
1329 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001330
Christian Hitz13fc0e22011-10-12 09:32:01 +02001331 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juul52c07962007-10-31 13:53:06 +01001332}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001333
William Juul52c07962007-10-31 13:53:06 +01001334/**
Christian Hitz13fc0e22011-10-12 09:32:01 +02001335 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
William Juul52c07962007-10-31 13:53:06 +01001336 * @mtd: MTD device structure
1337 * @from: offset to read from
1338 * @len: number of bytes to read
1339 * @retlen: pointer to variable to store the number of read bytes
1340 * @buf: the databuffer to put data
1341 *
1342 * Get hold of the chip and call nand_do_read
1343 */
1344static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1345 size_t *retlen, uint8_t *buf)
1346{
1347 struct nand_chip *chip = mtd->priv;
1348 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001349
William Juul52c07962007-10-31 13:53:06 +01001350 /* Do not allow reads past end of device */
1351 if ((from + len) > mtd->size)
1352 return -EINVAL;
1353 if (!len)
1354 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001355
William Juul52c07962007-10-31 13:53:06 +01001356 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001357
William Juul52c07962007-10-31 13:53:06 +01001358 chip->ops.len = len;
1359 chip->ops.datbuf = buf;
1360 chip->ops.oobbuf = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001361
William Juul52c07962007-10-31 13:53:06 +01001362 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001363
William Juul52c07962007-10-31 13:53:06 +01001364 *retlen = chip->ops.retlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001365
William Juul52c07962007-10-31 13:53:06 +01001366 nand_release_device(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001367
William Juul52c07962007-10-31 13:53:06 +01001368 return ret;
1369}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001370
William Juul52c07962007-10-31 13:53:06 +01001371/**
1372 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1373 * @mtd: mtd info structure
1374 * @chip: nand chip info structure
1375 * @page: page number to read
1376 * @sndcmd: flag whether to issue read command or not
1377 */
1378static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1379 int page, int sndcmd)
1380{
1381 if (sndcmd) {
1382 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1383 sndcmd = 0;
1384 }
1385 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1386 return sndcmd;
1387}
1388
1389/**
1390 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1391 * with syndromes
1392 * @mtd: mtd info structure
1393 * @chip: nand chip info structure
1394 * @page: page number to read
1395 * @sndcmd: flag whether to issue read command or not
1396 */
1397static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1398 int page, int sndcmd)
1399{
1400 uint8_t *buf = chip->oob_poi;
1401 int length = mtd->oobsize;
1402 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1403 int eccsize = chip->ecc.size;
1404 uint8_t *bufpoi = buf;
1405 int i, toread, sndrnd = 0, pos;
1406
1407 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1408 for (i = 0; i < chip->ecc.steps; i++) {
1409 if (sndrnd) {
1410 pos = eccsize + i * (eccsize + chunk);
1411 if (mtd->writesize > 512)
1412 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1413 else
1414 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001415 } else
William Juul52c07962007-10-31 13:53:06 +01001416 sndrnd = 1;
1417 toread = min_t(int, length, chunk);
1418 chip->read_buf(mtd, bufpoi, toread);
1419 bufpoi += toread;
1420 length -= toread;
1421 }
1422 if (length > 0)
1423 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001424
William Juul52c07962007-10-31 13:53:06 +01001425 return 1;
1426}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001427
William Juul52c07962007-10-31 13:53:06 +01001428/**
1429 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1430 * @mtd: mtd info structure
1431 * @chip: nand chip info structure
1432 * @page: page number to write
1433 */
1434static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1435 int page)
1436{
1437 int status = 0;
1438 const uint8_t *buf = chip->oob_poi;
1439 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001440
William Juul52c07962007-10-31 13:53:06 +01001441 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1442 chip->write_buf(mtd, buf, length);
1443 /* Send command to program the OOB data */
1444 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001445
William Juul52c07962007-10-31 13:53:06 +01001446 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001447
William Juul52c07962007-10-31 13:53:06 +01001448 return status & NAND_STATUS_FAIL ? -EIO : 0;
1449}
1450
1451/**
1452 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1453 * with syndrome - only for large page flash !
1454 * @mtd: mtd info structure
1455 * @chip: nand chip info structure
1456 * @page: page number to write
1457 */
1458static int nand_write_oob_syndrome(struct mtd_info *mtd,
1459 struct nand_chip *chip, int page)
1460{
1461 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1462 int eccsize = chip->ecc.size, length = mtd->oobsize;
1463 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1464 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001465
1466 /*
William Juul52c07962007-10-31 13:53:06 +01001467 * data-ecc-data-ecc ... ecc-oob
1468 * or
1469 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001470 */
William Juul52c07962007-10-31 13:53:06 +01001471 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1472 pos = steps * (eccsize + chunk);
1473 steps = 0;
1474 } else
1475 pos = eccsize;
1476
1477 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1478 for (i = 0; i < steps; i++) {
1479 if (sndcmd) {
1480 if (mtd->writesize <= 512) {
1481 uint32_t fill = 0xFFFFFFFF;
1482
1483 len = eccsize;
1484 while (len > 0) {
1485 int num = min_t(int, len, 4);
1486 chip->write_buf(mtd, (uint8_t *)&fill,
1487 num);
1488 len -= num;
1489 }
1490 } else {
1491 pos = eccsize + i * (eccsize + chunk);
1492 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1493 }
1494 } else
1495 sndcmd = 1;
1496 len = min_t(int, length, chunk);
1497 chip->write_buf(mtd, bufpoi, len);
1498 bufpoi += len;
1499 length -= len;
1500 }
1501 if (length > 0)
1502 chip->write_buf(mtd, bufpoi, length);
1503
1504 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1505 status = chip->waitfunc(mtd, chip);
1506
1507 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001508}
1509
1510/**
William Juul52c07962007-10-31 13:53:06 +01001511 * nand_do_read_oob - [Intern] NAND read out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001512 * @mtd: MTD device structure
1513 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001514 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001515 *
1516 * NAND read out-of-band data from the spare area
1517 */
William Juul52c07962007-10-31 13:53:06 +01001518static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1519 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001520{
William Juul52c07962007-10-31 13:53:06 +01001521 int page, realpage, chipnr, sndcmd = 1;
1522 struct nand_chip *chip = mtd->priv;
1523 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1524 int readlen = ops->ooblen;
1525 int len;
1526 uint8_t *buf = ops->oobbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001527
Christian Hitz13fc0e22011-10-12 09:32:01 +02001528 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1529 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001530
William Juul52c07962007-10-31 13:53:06 +01001531 if (ops->mode == MTD_OOB_AUTO)
1532 len = chip->ecc.layout->oobavail;
1533 else
1534 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001535
William Juul52c07962007-10-31 13:53:06 +01001536 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02001537 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1538 "outside oob\n", __func__);
William Juul52c07962007-10-31 13:53:06 +01001539 return -EINVAL;
1540 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001541
1542 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001543 if (unlikely(from >= mtd->size ||
1544 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1545 (from >> chip->page_shift)) * len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02001546 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1547 "of device\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001548 return -EINVAL;
1549 }
1550
William Juul52c07962007-10-31 13:53:06 +01001551 chipnr = (int)(from >> chip->chip_shift);
1552 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001553
William Juul52c07962007-10-31 13:53:06 +01001554 /* Shift to get page */
1555 realpage = (int)(from >> chip->page_shift);
1556 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001557
Christian Hitz13fc0e22011-10-12 09:32:01 +02001558 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06001559 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01001560 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001561
William Juul52c07962007-10-31 13:53:06 +01001562 len = min(len, readlen);
1563 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001564
William Juul52c07962007-10-31 13:53:06 +01001565 if (!(chip->options & NAND_NO_READRDY)) {
1566 /*
1567 * Apply delay or wait for ready/busy pin. Do this
1568 * before the AUTOINCR check, so no problems arise if a
1569 * chip which does auto increment is marked as
1570 * NOAUTOINCR by the board driver.
1571 */
1572 if (!chip->dev_ready)
1573 udelay(chip->chip_delay);
1574 else
1575 nand_wait_ready(mtd);
1576 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001577
William Juul52c07962007-10-31 13:53:06 +01001578 readlen -= len;
1579 if (!readlen)
1580 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001581
William Juul52c07962007-10-31 13:53:06 +01001582 /* Increment page address */
1583 realpage++;
1584
1585 page = realpage & chip->pagemask;
1586 /* Check, if we cross a chip boundary */
1587 if (!page) {
1588 chipnr++;
1589 chip->select_chip(mtd, -1);
1590 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001591 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001592
William Juul52c07962007-10-31 13:53:06 +01001593 /* Check, if the chip supports auto page increment
1594 * or if we have hit a block boundary.
1595 */
1596 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1597 sndcmd = 1;
1598 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001599
William Juul52c07962007-10-31 13:53:06 +01001600 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001601 return 0;
1602}
1603
1604/**
William Juul52c07962007-10-31 13:53:06 +01001605 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001606 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001607 * @from: offset to read from
William Juul52c07962007-10-31 13:53:06 +01001608 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001609 *
William Juul52c07962007-10-31 13:53:06 +01001610 * NAND read data and/or out-of-band data
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001611 */
William Juul52c07962007-10-31 13:53:06 +01001612static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1613 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001614{
William Juul52c07962007-10-31 13:53:06 +01001615 struct nand_chip *chip = mtd->priv;
1616 int ret = -ENOTSUPP;
1617
1618 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001619
1620 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001621 if (ops->datbuf && (from + ops->len) > mtd->size) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02001622 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1623 "beyond end of device\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001624 return -EINVAL;
1625 }
1626
William Juul52c07962007-10-31 13:53:06 +01001627 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001628
Christian Hitz13fc0e22011-10-12 09:32:01 +02001629 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001630 case MTD_OOB_PLACE:
1631 case MTD_OOB_AUTO:
1632 case MTD_OOB_RAW:
1633 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001634
William Juul52c07962007-10-31 13:53:06 +01001635 default:
1636 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001637 }
1638
William Juul52c07962007-10-31 13:53:06 +01001639 if (!ops->datbuf)
1640 ret = nand_do_read_oob(mtd, from, ops);
1641 else
1642 ret = nand_do_read_ops(mtd, from, ops);
1643
Christian Hitz13fc0e22011-10-12 09:32:01 +02001644out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001645 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001646 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001647}
1648
1649
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001650/**
William Juul52c07962007-10-31 13:53:06 +01001651 * nand_write_page_raw - [Intern] raw page write function
1652 * @mtd: mtd info structure
1653 * @chip: nand chip info structure
1654 * @buf: data buffer
David Brownellee86b8d2009-11-07 16:27:01 -05001655 *
1656 * Not for syndrome calculating ecc controllers, which use a special oob layout
William Juul52c07962007-10-31 13:53:06 +01001657 */
1658static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1659 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001660{
William Juul52c07962007-10-31 13:53:06 +01001661 chip->write_buf(mtd, buf, mtd->writesize);
1662 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1663}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001664
William Juul52c07962007-10-31 13:53:06 +01001665/**
David Brownellee86b8d2009-11-07 16:27:01 -05001666 * nand_write_page_raw_syndrome - [Intern] raw page write function
1667 * @mtd: mtd info structure
1668 * @chip: nand chip info structure
1669 * @buf: data buffer
1670 *
1671 * We need a special oob layout and handling even when ECC isn't checked.
1672 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001673static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
1674 struct nand_chip *chip,
1675 const uint8_t *buf)
David Brownellee86b8d2009-11-07 16:27:01 -05001676{
1677 int eccsize = chip->ecc.size;
1678 int eccbytes = chip->ecc.bytes;
1679 uint8_t *oob = chip->oob_poi;
1680 int steps, size;
1681
1682 for (steps = chip->ecc.steps; steps > 0; steps--) {
1683 chip->write_buf(mtd, buf, eccsize);
1684 buf += eccsize;
1685
1686 if (chip->ecc.prepad) {
1687 chip->write_buf(mtd, oob, chip->ecc.prepad);
1688 oob += chip->ecc.prepad;
1689 }
1690
1691 chip->read_buf(mtd, oob, eccbytes);
1692 oob += eccbytes;
1693
1694 if (chip->ecc.postpad) {
1695 chip->write_buf(mtd, oob, chip->ecc.postpad);
1696 oob += chip->ecc.postpad;
1697 }
1698 }
1699
1700 size = mtd->oobsize - (oob - chip->oob_poi);
1701 if (size)
1702 chip->write_buf(mtd, oob, size);
1703}
1704/**
William Juul52c07962007-10-31 13:53:06 +01001705 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1706 * @mtd: mtd info structure
1707 * @chip: nand chip info structure
1708 * @buf: data buffer
1709 */
1710static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1711 const uint8_t *buf)
1712{
1713 int i, eccsize = chip->ecc.size;
1714 int eccbytes = chip->ecc.bytes;
1715 int eccsteps = chip->ecc.steps;
1716 uint8_t *ecc_calc = chip->buffers->ecccalc;
1717 const uint8_t *p = buf;
1718 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001719
William Juul52c07962007-10-31 13:53:06 +01001720 /* Software ecc calculation */
1721 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1722 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001723
William Juul52c07962007-10-31 13:53:06 +01001724 for (i = 0; i < chip->ecc.total; i++)
1725 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001726
William Juul52c07962007-10-31 13:53:06 +01001727 chip->ecc.write_page_raw(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001728}
1729
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001730/**
William Juul52c07962007-10-31 13:53:06 +01001731 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1732 * @mtd: mtd info structure
1733 * @chip: nand chip info structure
1734 * @buf: data buffer
1735 */
1736static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1737 const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001738{
William Juul52c07962007-10-31 13:53:06 +01001739 int i, eccsize = chip->ecc.size;
1740 int eccbytes = chip->ecc.bytes;
1741 int eccsteps = chip->ecc.steps;
1742 uint8_t *ecc_calc = chip->buffers->ecccalc;
1743 const uint8_t *p = buf;
1744 uint32_t *eccpos = chip->ecc.layout->eccpos;
1745
1746 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1747 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1748 chip->write_buf(mtd, p, eccsize);
1749 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1750 }
1751
1752 for (i = 0; i < chip->ecc.total; i++)
1753 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1754
1755 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001756}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001757
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001758/**
William Juul52c07962007-10-31 13:53:06 +01001759 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1760 * @mtd: mtd info structure
1761 * @chip: nand chip info structure
1762 * @buf: data buffer
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001763 *
William Juul52c07962007-10-31 13:53:06 +01001764 * The hw generator calculates the error syndrome automatically. Therefor
1765 * we need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001766 */
William Juul52c07962007-10-31 13:53:06 +01001767static void nand_write_page_syndrome(struct mtd_info *mtd,
1768 struct nand_chip *chip, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001769{
William Juul52c07962007-10-31 13:53:06 +01001770 int i, eccsize = chip->ecc.size;
1771 int eccbytes = chip->ecc.bytes;
1772 int eccsteps = chip->ecc.steps;
1773 const uint8_t *p = buf;
1774 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001775
William Juul52c07962007-10-31 13:53:06 +01001776 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001777
William Juul52c07962007-10-31 13:53:06 +01001778 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1779 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001780
William Juul52c07962007-10-31 13:53:06 +01001781 if (chip->ecc.prepad) {
1782 chip->write_buf(mtd, oob, chip->ecc.prepad);
1783 oob += chip->ecc.prepad;
1784 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001785
William Juul52c07962007-10-31 13:53:06 +01001786 chip->ecc.calculate(mtd, p, oob);
1787 chip->write_buf(mtd, oob, eccbytes);
1788 oob += eccbytes;
1789
1790 if (chip->ecc.postpad) {
1791 chip->write_buf(mtd, oob, chip->ecc.postpad);
1792 oob += chip->ecc.postpad;
1793 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001794 }
1795
William Juul52c07962007-10-31 13:53:06 +01001796 /* Calculate remaining oob bytes */
1797 i = mtd->oobsize - (oob - chip->oob_poi);
1798 if (i)
1799 chip->write_buf(mtd, oob, i);
1800}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001801
William Juul52c07962007-10-31 13:53:06 +01001802/**
1803 * nand_write_page - [REPLACEABLE] write one page
1804 * @mtd: MTD device structure
1805 * @chip: NAND chip descriptor
1806 * @buf: the data to write
1807 * @page: page number to write
1808 * @cached: cached programming
1809 * @raw: use _raw version of write_page
1810 */
1811static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1812 const uint8_t *buf, int page, int cached, int raw)
1813{
1814 int status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001815
William Juul52c07962007-10-31 13:53:06 +01001816 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1817
1818 if (unlikely(raw))
1819 chip->ecc.write_page_raw(mtd, chip, buf);
1820 else
1821 chip->ecc.write_page(mtd, chip, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001822
William Juul52c07962007-10-31 13:53:06 +01001823 /*
1824 * Cached progamming disabled for now, Not sure if its worth the
1825 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1826 */
1827 cached = 0;
1828
1829 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1830
1831 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1832 status = chip->waitfunc(mtd, chip);
1833 /*
1834 * See if operation failed and additional status checks are
1835 * available
1836 */
1837 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1838 status = chip->errstat(mtd, chip, FL_WRITING, status,
1839 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001840
William Juul52c07962007-10-31 13:53:06 +01001841 if (status & NAND_STATUS_FAIL)
1842 return -EIO;
1843 } else {
1844 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1845 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001846 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001847
William Juul52c07962007-10-31 13:53:06 +01001848#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1849 /* Send command to read back the data */
1850 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001851
William Juul52c07962007-10-31 13:53:06 +01001852 if (chip->verify_buf(mtd, buf, mtd->writesize))
1853 return -EIO;
1854#endif
1855 return 0;
1856}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001857
William Juul52c07962007-10-31 13:53:06 +01001858/**
1859 * nand_fill_oob - [Internal] Transfer client buffer to oob
1860 * @chip: nand chip structure
1861 * @oob: oob data buffer
Christian Hitzb8a6b372011-10-12 09:32:02 +02001862 * @len: oob data write length
William Juul52c07962007-10-31 13:53:06 +01001863 * @ops: oob ops structure
1864 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001865static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
1866 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01001867{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001868 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001869
William Juul52c07962007-10-31 13:53:06 +01001870 case MTD_OOB_PLACE:
1871 case MTD_OOB_RAW:
1872 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1873 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874
William Juul52c07962007-10-31 13:53:06 +01001875 case MTD_OOB_AUTO: {
1876 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1877 uint32_t boffs = 0, woffs = ops->ooboffs;
1878 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001879
Christian Hitz13fc0e22011-10-12 09:32:01 +02001880 for (; free->length && len; free++, len -= bytes) {
William Juul52c07962007-10-31 13:53:06 +01001881 /* Write request not from offset 0 ? */
1882 if (unlikely(woffs)) {
1883 if (woffs >= free->length) {
1884 woffs -= free->length;
1885 continue;
1886 }
1887 boffs = free->offset + woffs;
1888 bytes = min_t(size_t, len,
1889 (free->length - woffs));
1890 woffs = 0;
1891 } else {
1892 bytes = min_t(size_t, len, free->length);
1893 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001894 }
William Juul52c07962007-10-31 13:53:06 +01001895 memcpy(chip->oob_poi + boffs, oob, bytes);
1896 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001897 }
William Juul52c07962007-10-31 13:53:06 +01001898 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001899 }
William Juul52c07962007-10-31 13:53:06 +01001900 default:
1901 BUG();
1902 }
1903 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001904}
1905
Christian Hitzb8a6b372011-10-12 09:32:02 +02001906#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001907
1908/**
William Juul52c07962007-10-31 13:53:06 +01001909 * nand_do_write_ops - [Internal] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001910 * @mtd: MTD device structure
1911 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01001912 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001913 *
William Juul52c07962007-10-31 13:53:06 +01001914 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001915 */
William Juul52c07962007-10-31 13:53:06 +01001916static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1917 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001918{
William Juul52c07962007-10-31 13:53:06 +01001919 int chipnr, realpage, page, blockmask, column;
1920 struct nand_chip *chip = mtd->priv;
1921 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001922
1923 uint32_t oobwritelen = ops->ooblen;
1924 uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
1925 mtd->oobavail : mtd->oobsize;
1926
William Juul52c07962007-10-31 13:53:06 +01001927 uint8_t *oob = ops->oobbuf;
1928 uint8_t *buf = ops->datbuf;
1929 int ret, subpage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001930
William Juul52c07962007-10-31 13:53:06 +01001931 ops->retlen = 0;
1932 if (!writelen)
1933 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001934
William Juul52c07962007-10-31 13:53:06 +01001935 column = to & (mtd->writesize - 1);
1936 subpage = column || (writelen & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001937
William Juul52c07962007-10-31 13:53:06 +01001938 if (subpage && oob)
1939 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001940
William Juul52c07962007-10-31 13:53:06 +01001941 chipnr = (int)(to >> chip->chip_shift);
1942 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001943
1944 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01001945 if (nand_check_wp(mtd)) {
1946 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1947 return -EIO;
1948 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001949
William Juul52c07962007-10-31 13:53:06 +01001950 realpage = (int)(to >> chip->page_shift);
1951 page = realpage & chip->pagemask;
1952 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001953
William Juul52c07962007-10-31 13:53:06 +01001954 /* Invalidate the page cache, when we write to the cached page */
1955 if (to <= (chip->pagebuf << chip->page_shift) &&
1956 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1957 chip->pagebuf = -1;
1958
1959 /* If we're not given explicit OOB data, let it be 0xFF */
1960 if (likely(!oob))
1961 memset(chip->oob_poi, 0xff, mtd->oobsize);
1962
Christian Hitzb8a6b372011-10-12 09:32:02 +02001963 /* Don't allow multipage oob writes with offset */
1964 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
1965 return -EINVAL;
1966
Christian Hitz13fc0e22011-10-12 09:32:01 +02001967 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06001968 WATCHDOG_RESET();
1969
William Juul52c07962007-10-31 13:53:06 +01001970 int bytes = mtd->writesize;
1971 int cached = writelen > bytes && page != blockmask;
1972 uint8_t *wbuf = buf;
1973
1974 /* Partial page write ? */
1975 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1976 cached = 0;
1977 bytes = min_t(int, bytes - column, (int) writelen);
1978 chip->pagebuf = -1;
1979 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1980 memcpy(&chip->buffers->databuf[column], buf, bytes);
1981 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02001982 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001983
Christian Hitzb8a6b372011-10-12 09:32:02 +02001984 if (unlikely(oob)) {
1985 size_t len = min(oobwritelen, oobmaxlen);
1986 oob = nand_fill_oob(chip, oob, len, ops);
1987 oobwritelen -= len;
1988 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001989
William Juul52c07962007-10-31 13:53:06 +01001990 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1991 (ops->mode == MTD_OOB_RAW));
1992 if (ret)
1993 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001994
William Juul52c07962007-10-31 13:53:06 +01001995 writelen -= bytes;
1996 if (!writelen)
1997 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001998
William Juul52c07962007-10-31 13:53:06 +01001999 column = 0;
2000 buf += bytes;
2001 realpage++;
2002
2003 page = realpage & chip->pagemask;
2004 /* Check, if we cross a chip boundary */
2005 if (!page) {
2006 chipnr++;
2007 chip->select_chip(mtd, -1);
2008 chip->select_chip(mtd, chipnr);
2009 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002010 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002011
William Juul52c07962007-10-31 13:53:06 +01002012 ops->retlen = ops->len - writelen;
2013 if (unlikely(oob))
2014 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002015 return ret;
2016}
2017
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002018/**
William Juul52c07962007-10-31 13:53:06 +01002019 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002020 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002021 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01002022 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002023 * @retlen: pointer to variable to store the number of written bytes
William Juul52c07962007-10-31 13:53:06 +01002024 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002025 *
William Juul52c07962007-10-31 13:53:06 +01002026 * NAND write with ECC
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002027 */
William Juul52c07962007-10-31 13:53:06 +01002028static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2029 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002030{
William Juul52c07962007-10-31 13:53:06 +01002031 struct nand_chip *chip = mtd->priv;
2032 int ret;
2033
Ben Gardiner2c489ac2011-05-24 10:18:34 -04002034 /* Do not allow writes past end of device */
William Juul52c07962007-10-31 13:53:06 +01002035 if ((to + len) > mtd->size)
2036 return -EINVAL;
2037 if (!len)
2038 return 0;
2039
2040 nand_get_device(chip, mtd, FL_WRITING);
2041
2042 chip->ops.len = len;
2043 chip->ops.datbuf = (uint8_t *)buf;
2044 chip->ops.oobbuf = NULL;
2045
2046 ret = nand_do_write_ops(mtd, to, &chip->ops);
2047
2048 *retlen = chip->ops.retlen;
2049
2050 nand_release_device(mtd);
2051
2052 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002053}
2054
2055/**
William Juul52c07962007-10-31 13:53:06 +01002056 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002057 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002058 * @to: offset to write to
William Juul52c07962007-10-31 13:53:06 +01002059 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002060 *
William Juul52c07962007-10-31 13:53:06 +01002061 * NAND write out-of-band
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002062 */
William Juul52c07962007-10-31 13:53:06 +01002063static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2064 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002065{
William Juul52c07962007-10-31 13:53:06 +01002066 int chipnr, page, status, len;
2067 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002068
Christian Hitz13fc0e22011-10-12 09:32:01 +02002069 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2070 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002071
William Juul52c07962007-10-31 13:53:06 +01002072 if (ops->mode == MTD_OOB_AUTO)
2073 len = chip->ecc.layout->oobavail;
2074 else
2075 len = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002076
2077 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002078 if ((ops->ooboffs + ops->ooblen) > len) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002079 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2080 "past end of page\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002081 return -EINVAL;
2082 }
2083
William Juul52c07962007-10-31 13:53:06 +01002084 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002085 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2086 "write outside oob\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002087 return -EINVAL;
2088 }
2089
Christian Hitz13fc0e22011-10-12 09:32:01 +02002090 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002091 if (unlikely(to >= mtd->size ||
2092 ops->ooboffs + ops->ooblen >
2093 ((mtd->size >> chip->page_shift) -
2094 (to >> chip->page_shift)) * len)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002095 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2096 "end of device\n", __func__);
William Juul52c07962007-10-31 13:53:06 +01002097 return -EINVAL;
2098 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002099
William Juul52c07962007-10-31 13:53:06 +01002100 chipnr = (int)(to >> chip->chip_shift);
2101 chip->select_chip(mtd, chipnr);
2102
2103 /* Shift to get page */
2104 page = (int)(to >> chip->page_shift);
2105
2106 /*
2107 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2108 * of my DiskOnChip 2000 test units) will clear the whole data page too
2109 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2110 * it in the doc2000 driver in August 1999. dwmw2.
2111 */
2112 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002113
2114 /* Check, if it is write protected */
2115 if (nand_check_wp(mtd))
William Juul52c07962007-10-31 13:53:06 +01002116 return -EROFS;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002117
William Juul52c07962007-10-31 13:53:06 +01002118 /* Invalidate the page cache, if we write to the cached page */
2119 if (page == chip->pagebuf)
2120 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002121
William Juul52c07962007-10-31 13:53:06 +01002122 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002123 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
William Juul52c07962007-10-31 13:53:06 +01002124 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2125 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002126
William Juul52c07962007-10-31 13:53:06 +01002127 if (status)
2128 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002129
William Juul52c07962007-10-31 13:53:06 +01002130 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002131
William Juul52c07962007-10-31 13:53:06 +01002132 return 0;
2133}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002134
William Juul52c07962007-10-31 13:53:06 +01002135/**
2136 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2137 * @mtd: MTD device structure
2138 * @to: offset to write to
2139 * @ops: oob operation description structure
2140 */
2141static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2142 struct mtd_oob_ops *ops)
2143{
2144 struct nand_chip *chip = mtd->priv;
2145 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002146
William Juul52c07962007-10-31 13:53:06 +01002147 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002148
William Juul52c07962007-10-31 13:53:06 +01002149 /* Do not allow writes past end of device */
2150 if (ops->datbuf && (to + ops->len) > mtd->size) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002151 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2152 "end of device\n", __func__);
William Juul52c07962007-10-31 13:53:06 +01002153 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002154 }
William Juul52c07962007-10-31 13:53:06 +01002155
2156 nand_get_device(chip, mtd, FL_WRITING);
2157
Christian Hitz13fc0e22011-10-12 09:32:01 +02002158 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01002159 case MTD_OOB_PLACE:
2160 case MTD_OOB_AUTO:
2161 case MTD_OOB_RAW:
2162 break;
2163
2164 default:
2165 goto out;
2166 }
2167
2168 if (!ops->datbuf)
2169 ret = nand_do_write_oob(mtd, to, ops);
2170 else
2171 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002172
Christian Hitz13fc0e22011-10-12 09:32:01 +02002173out:
William Juul52c07962007-10-31 13:53:06 +01002174 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002175 return ret;
2176}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002177
2178/**
2179 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2180 * @mtd: MTD device structure
2181 * @page: the page address of the block which will be erased
2182 *
2183 * Standard erase command for NAND chips
2184 */
William Juul52c07962007-10-31 13:53:06 +01002185static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002186{
William Juul52c07962007-10-31 13:53:06 +01002187 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002188 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002189 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2190 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002191}
2192
2193/**
2194 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2195 * @mtd: MTD device structure
2196 * @page: the page address of the block which will be erased
2197 *
2198 * AND multi block erase command function
2199 * Erase 4 consecutive blocks
2200 */
William Juul52c07962007-10-31 13:53:06 +01002201static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002202{
William Juul52c07962007-10-31 13:53:06 +01002203 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002204 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002205 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2206 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2207 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2208 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2209 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002210}
2211
2212/**
2213 * nand_erase - [MTD Interface] erase block(s)
2214 * @mtd: MTD device structure
2215 * @instr: erase instruction
2216 *
2217 * Erase one ore more blocks
2218 */
William Juul52c07962007-10-31 13:53:06 +01002219static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002220{
William Juul52c07962007-10-31 13:53:06 +01002221 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002222}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002223
William Juul52c07962007-10-31 13:53:06 +01002224#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002225/**
William Juul52c07962007-10-31 13:53:06 +01002226 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002227 * @mtd: MTD device structure
2228 * @instr: erase instruction
2229 * @allowbbt: allow erasing the bbt area
2230 *
2231 * Erase one ore more blocks
2232 */
William Juul52c07962007-10-31 13:53:06 +01002233int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2234 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002235{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002236 int page, status, pages_per_block, ret, chipnr;
William Juul52c07962007-10-31 13:53:06 +01002237 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002238 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juul52c07962007-10-31 13:53:06 +01002239 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002240 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002241
Christian Hitz13fc0e22011-10-12 09:32:01 +02002242 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2243 __func__, (unsigned long long)instr->addr,
2244 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002245
Christian Hitzb8a6b372011-10-12 09:32:02 +02002246 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002247 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002248
Christian Hitzb8a6b372011-10-12 09:32:02 +02002249 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002250
2251 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002252 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002253
2254 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002255 page = (int)(instr->addr >> chip->page_shift);
2256 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002257
2258 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002259 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002260
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002261 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002262 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002263
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002264 /* Check, if it is write protected */
2265 if (nand_check_wp(mtd)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002266 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2267 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002268 instr->state = MTD_ERASE_FAILED;
2269 goto erase_exit;
2270 }
2271
William Juul52c07962007-10-31 13:53:06 +01002272 /*
2273 * If BBT requires refresh, set the BBT page mask to see if the BBT
2274 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2275 * can not be matched. This is also done when the bbt is actually
2276 * erased to avoid recusrsive updates
2277 */
2278 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2279 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2280
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002281 /* Loop through the pages */
2282 len = instr->len;
2283
2284 instr->state = MTD_ERASING;
2285
2286 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002287 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01002288 /*
2289 * heck if we have a bad block, we do not erase bad blocks !
2290 */
Marek Vasut971d9a12011-09-12 06:04:06 +02002291 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juul52c07962007-10-31 13:53:06 +01002292 chip->page_shift, 0, allowbbt)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002293 printk(KERN_WARNING "%s: attempt to erase a bad block "
2294 "at page 0x%08x\n", __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002295 instr->state = MTD_ERASE_FAILED;
2296 goto erase_exit;
2297 }
William Juul52c07962007-10-31 13:53:06 +01002298
2299 /*
2300 * Invalidate the page cache, if we erase the block which
2301 * contains the current cached page
2302 */
2303 if (page <= chip->pagebuf && chip->pagebuf <
2304 (page + pages_per_block))
2305 chip->pagebuf = -1;
2306
2307 chip->erase_cmd(mtd, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002308
William Juul52c07962007-10-31 13:53:06 +01002309 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002310
William Juul52c07962007-10-31 13:53:06 +01002311 /*
2312 * See if operation failed and additional status checks are
2313 * available
2314 */
2315 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2316 status = chip->errstat(mtd, chip, FL_ERASING,
2317 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002318
2319 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002320 if (status & NAND_STATUS_FAIL) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002321 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2322 "page 0x%08x\n", __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002323 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002324 instr->fail_addr =
2325 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002326 goto erase_exit;
2327 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002328
William Juul52c07962007-10-31 13:53:06 +01002329 /*
2330 * If BBT requires refresh, set the BBT rewrite flag to the
2331 * page being erased
2332 */
2333 if (bbt_masked_page != 0xffffffff &&
2334 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002335 rewrite_bbt[chipnr] =
2336 ((loff_t)page << chip->page_shift);
William Juul52c07962007-10-31 13:53:06 +01002337
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002338 /* Increment page address and decrement length */
William Juul52c07962007-10-31 13:53:06 +01002339 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002340 page += pages_per_block;
2341
2342 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002343 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002344 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002345 chip->select_chip(mtd, -1);
2346 chip->select_chip(mtd, chipnr);
2347
2348 /*
2349 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2350 * page mask to see if this BBT should be rewritten
2351 */
2352 if (bbt_masked_page != 0xffffffff &&
2353 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2354 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2355 BBT_PAGE_MASK;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002356 }
2357 }
2358 instr->state = MTD_ERASE_DONE;
2359
Christian Hitz13fc0e22011-10-12 09:32:01 +02002360erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002361
2362 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002363
2364 /* Deselect and wake up anyone waiting on the device */
2365 nand_release_device(mtd);
2366
Scott Wood3628f002008-10-24 16:20:43 -05002367 /* Do call back function */
2368 if (!ret)
2369 mtd_erase_callback(instr);
2370
William Juul52c07962007-10-31 13:53:06 +01002371 /*
2372 * If BBT requires refresh and erase was successful, rewrite any
2373 * selected bad block tables
2374 */
2375 if (bbt_masked_page == 0xffffffff || ret)
2376 return ret;
2377
2378 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2379 if (!rewrite_bbt[chipnr])
2380 continue;
2381 /* update the BBT for chip */
Christian Hitz13fc0e22011-10-12 09:32:01 +02002382 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2383 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2384 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
William Juul52c07962007-10-31 13:53:06 +01002385 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2386 }
2387
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002388 /* Return more or less happy */
2389 return ret;
2390}
2391
2392/**
2393 * nand_sync - [MTD Interface] sync
2394 * @mtd: MTD device structure
2395 *
2396 * Sync is actually a wait for chip ready function
2397 */
William Juul52c07962007-10-31 13:53:06 +01002398static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002399{
William Juul52c07962007-10-31 13:53:06 +01002400 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002401
Christian Hitz13fc0e22011-10-12 09:32:01 +02002402 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002403
2404 /* Grab the lock and see if the device is available */
William Juul52c07962007-10-31 13:53:06 +01002405 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002406 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002407 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002408}
2409
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002410/**
William Juul52c07962007-10-31 13:53:06 +01002411 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002412 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002413 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002414 */
William Juul52c07962007-10-31 13:53:06 +01002415static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002416{
2417 /* Check for invalid offset */
William Juul52c07962007-10-31 13:53:06 +01002418 if (offs > mtd->size)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002419 return -EINVAL;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002420
William Juul52c07962007-10-31 13:53:06 +01002421 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002422}
2423
2424/**
William Juul52c07962007-10-31 13:53:06 +01002425 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002426 * @mtd: MTD device structure
2427 * @ofs: offset relative to mtd start
2428 */
William Juul52c07962007-10-31 13:53:06 +01002429static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002430{
William Juul52c07962007-10-31 13:53:06 +01002431 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002432 int ret;
2433
Christian Hitzb8a6b372011-10-12 09:32:02 +02002434 ret = nand_block_isbad(mtd, ofs);
2435 if (ret) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002436 /* If it was bad already, return success and do nothing. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002437 if (ret > 0)
2438 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002439 return ret;
2440 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002441
William Juul52c07962007-10-31 13:53:06 +01002442 return chip->block_markbad(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002443}
2444
William Juul52c07962007-10-31 13:53:06 +01002445/*
2446 * Set default functions
2447 */
2448static void nand_set_defaults(struct nand_chip *chip, int busw)
2449{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002450 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002451 if (!chip->chip_delay)
2452 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002453
2454 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002455 if (chip->cmdfunc == NULL)
2456 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002457
2458 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002459 if (chip->waitfunc == NULL)
2460 chip->waitfunc = nand_wait;
2461
2462 if (!chip->select_chip)
2463 chip->select_chip = nand_select_chip;
2464 if (!chip->read_byte)
2465 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2466 if (!chip->read_word)
2467 chip->read_word = nand_read_word;
2468 if (!chip->block_bad)
2469 chip->block_bad = nand_block_bad;
2470 if (!chip->block_markbad)
2471 chip->block_markbad = nand_default_block_markbad;
2472 if (!chip->write_buf)
2473 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2474 if (!chip->read_buf)
2475 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2476 if (!chip->verify_buf)
2477 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2478 if (!chip->scan_bbt)
2479 chip->scan_bbt = nand_default_bbt;
Scott Woodda0f4392010-08-25 17:42:49 -05002480 if (!chip->controller)
William Juul52c07962007-10-31 13:53:06 +01002481 chip->controller = &chip->hwcontrol;
William Juul52c07962007-10-31 13:53:06 +01002482}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002483
Florian Fainellic98a9352011-02-25 00:01:34 +00002484#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002485/*
2486 * sanitize ONFI strings so we can safely print them
2487 */
2488static void sanitize_string(char *s, size_t len)
2489{
2490 ssize_t i;
2491
2492 /* null terminate */
2493 s[len - 1] = 0;
2494
2495 /* remove non printable chars */
2496 for (i = 0; i < len - 1; i++) {
2497 if (s[i] < ' ' || s[i] > 127)
2498 s[i] = '?';
2499 }
2500
2501 /* remove trailing spaces */
2502 strim(s);
2503}
2504
Florian Fainellic98a9352011-02-25 00:01:34 +00002505static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2506{
2507 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00002508 while (len--) {
2509 crc ^= *p++ << 8;
2510 for (i = 0; i < 8; i++)
2511 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2512 }
2513
2514 return crc;
2515}
2516
2517/*
2518 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise
2519 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02002520static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00002521 int *busw)
2522{
2523 struct nand_onfi_params *p = &chip->onfi_params;
2524 int i;
2525 int val;
2526
Christian Hitzb8a6b372011-10-12 09:32:02 +02002527 /* try ONFI for unknow chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00002528 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2529 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2530 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2531 return 0;
2532
Stefan Roese0643e432012-05-07 21:29:30 +00002533 MTDDEBUG(MTD_DEBUG_LEVEL0, "ONFI flash detected\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00002534 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2535 for (i = 0; i < 3; i++) {
2536 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2537 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02002538 le16_to_cpu(p->crc)) {
Stefan Roese0643e432012-05-07 21:29:30 +00002539 MTDDEBUG(MTD_DEBUG_LEVEL0,
2540 "ONFI param page %d valid\n", i);
Florian Fainellic98a9352011-02-25 00:01:34 +00002541 break;
2542 }
2543 }
2544
2545 if (i == 3)
2546 return 0;
2547
2548 /* check version */
2549 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002550 if (val & (1 << 5))
2551 chip->onfi_version = 23;
2552 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00002553 chip->onfi_version = 22;
2554 else if (val & (1 << 3))
2555 chip->onfi_version = 21;
2556 else if (val & (1 << 2))
2557 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002558 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00002559 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002560 else
2561 chip->onfi_version = 0;
2562
2563 if (!chip->onfi_version) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002564 printk(KERN_INFO "%s: unsupported ONFI version: %d\n",
2565 __func__, val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02002566 return 0;
2567 }
Florian Fainellic98a9352011-02-25 00:01:34 +00002568
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002569 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2570 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00002571 if (!mtd->name)
2572 mtd->name = p->model;
Florian Fainellic98a9352011-02-25 00:01:34 +00002573 mtd->writesize = le32_to_cpu(p->byte_per_page);
2574 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2575 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01002576 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2577 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Florian Fainellic98a9352011-02-25 00:01:34 +00002578 *busw = 0;
2579 if (le16_to_cpu(p->features) & 1)
2580 *busw = NAND_BUSWIDTH_16;
2581
Marek Vasutfc417192012-08-30 13:39:38 +00002582 chip->options |= NAND_NO_READRDY | NAND_NO_AUTOINCR;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002583
Florian Fainellic98a9352011-02-25 00:01:34 +00002584 return 1;
2585}
2586#else
2587static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2588 struct nand_chip *chip,
2589 int *busw)
2590{
2591 return 0;
2592}
2593#endif
2594
William Juul52c07962007-10-31 13:53:06 +01002595/*
2596 * Get the flash and manufacturer id and lookup if the type is supported
2597 */
Mike Frysinger6c285f62010-10-20 01:15:21 +00002598static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01002599 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00002600 int busw,
2601 int *maf_id, int *dev_id,
Lei Wen75bde942011-01-06 09:48:18 +08002602 const struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01002603{
Christian Hitzb8a6b372011-10-12 09:32:02 +02002604 int i, maf_idx;
2605 u8 id_data[8];
2606 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002607
2608 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01002609 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002610
Karl Beldanb6322fc2008-09-15 16:08:03 +02002611 /*
2612 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2613 * after power-up
2614 */
2615 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2616
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002617 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01002618 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002619
2620 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002621 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00002622 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002623
Scott Wood3628f002008-10-24 16:20:43 -05002624 /* Try again to make sure, as some systems the bus-hold or other
2625 * interface concerns can cause random data which looks like a
2626 * possibly credible NAND flash to appear. If the two results do
2627 * not match, ignore the device completely.
2628 */
2629
2630 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2631
Christian Hitzb8a6b372011-10-12 09:32:02 +02002632 for (i = 0; i < 2; i++)
2633 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05002634
Christian Hitzb8a6b372011-10-12 09:32:02 +02002635 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Scott Wood3628f002008-10-24 16:20:43 -05002636 printk(KERN_INFO "%s: second ID read did not match "
2637 "%02x,%02x against %02x,%02x\n", __func__,
Christian Hitzb8a6b372011-10-12 09:32:02 +02002638 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05002639 return ERR_PTR(-ENODEV);
2640 }
2641
Lei Wen75bde942011-01-06 09:48:18 +08002642 if (!type)
2643 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002644
Lei Wen75bde942011-01-06 09:48:18 +08002645 for (; type->name != NULL; type++)
Florian Fainellic98a9352011-02-25 00:01:34 +00002646 if (*dev_id == type->id)
Wolfgang Denka4de8352011-02-02 22:36:10 +01002647 break;
Lei Wen75bde942011-01-06 09:48:18 +08002648
Christian Hitzb8a6b372011-10-12 09:32:02 +02002649 chip->onfi_version = 0;
2650 if (!type->name || !type->pagesize) {
2651 /* Check is chip is ONFI compliant */
2652 ret = nand_flash_detect_onfi(mtd, chip, &busw);
2653 if (ret)
2654 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02002655 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002656
Christian Hitzb8a6b372011-10-12 09:32:02 +02002657 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2658
2659 /* Read entire ID string */
2660
2661 for (i = 0; i < 8; i++)
2662 id_data[i] = chip->read_byte(mtd);
2663
2664 if (!type->name)
2665 return ERR_PTR(-ENODEV);
2666
William Juul52c07962007-10-31 13:53:06 +01002667 if (!mtd->name)
2668 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002669
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002670 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002671
Christian Hitzb8a6b372011-10-12 09:32:02 +02002672 if (!type->pagesize && chip->init_size) {
2673 /* set the pagesize, oobsize, erasesize by the driver*/
2674 busw = chip->init_size(mtd, chip, id_data);
2675 } else if (!type->pagesize) {
2676 int extid;
2677 /* The 3rd id byte holds MLC / multichip data */
2678 chip->cellinfo = id_data[2];
2679 /* The 4th id byte is the important one */
2680 extid = id_data[3];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002681
Christian Hitzb8a6b372011-10-12 09:32:02 +02002682 /*
2683 * Field definitions are in the following datasheets:
2684 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2685 * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
2686 *
2687 * Check for wraparound + Samsung ID + nonzero 6th byte
2688 * to decide what to do.
2689 */
2690 if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
2691 id_data[0] == NAND_MFR_SAMSUNG &&
2692 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2693 id_data[5] != 0x00) {
2694 /* Calc pagesize */
2695 mtd->writesize = 2048 << (extid & 0x03);
2696 extid >>= 2;
2697 /* Calc oobsize */
2698 switch (extid & 0x03) {
2699 case 1:
2700 mtd->oobsize = 128;
2701 break;
2702 case 2:
2703 mtd->oobsize = 218;
2704 break;
2705 case 3:
2706 mtd->oobsize = 400;
2707 break;
2708 default:
2709 mtd->oobsize = 436;
2710 break;
2711 }
2712 extid >>= 2;
2713 /* Calc blocksize */
2714 mtd->erasesize = (128 * 1024) <<
2715 (((extid >> 1) & 0x04) | (extid & 0x03));
2716 busw = 0;
2717 } else {
2718 /* Calc pagesize */
2719 mtd->writesize = 1024 << (extid & 0x03);
2720 extid >>= 2;
2721 /* Calc oobsize */
2722 mtd->oobsize = (8 << (extid & 0x01)) *
2723 (mtd->writesize >> 9);
2724 extid >>= 2;
2725 /* Calc blocksize. Blocksize is multiples of 64KiB */
2726 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2727 extid >>= 2;
2728 /* Get buswidth information */
2729 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2730 }
2731 } else {
2732 /*
2733 * Old devices have chip data hardcoded in the device id table
2734 */
2735 mtd->erasesize = type->erasesize;
2736 mtd->writesize = type->pagesize;
2737 mtd->oobsize = mtd->writesize / 32;
2738 busw = type->options & NAND_BUSWIDTH_16;
2739
2740 /*
2741 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
2742 * some Spansion chips have erasesize that conflicts with size
2743 * listed in nand_ids table
2744 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
2745 */
2746 if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
2747 id_data[5] == 0x00 && id_data[6] == 0x00 &&
2748 id_data[7] == 0x00 && mtd->writesize == 512) {
2749 mtd->erasesize = 128 * 1024;
2750 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
2751 }
2752 }
Florian Fainellic98a9352011-02-25 00:01:34 +00002753 /* Get chip options, preserve non chip based options */
Marek Vasutfc417192012-08-30 13:39:38 +00002754 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00002755
Christian Hitzb8a6b372011-10-12 09:32:02 +02002756 /* Check if chip is a not a samsung device. Do not clear the
2757 * options for chips which are not having an extended id.
2758 */
2759 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2760 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2761ident_done:
2762
Florian Fainellic98a9352011-02-25 00:01:34 +00002763 /*
2764 * Set chip as a default. Board drivers can override it, if necessary
2765 */
2766 chip->options |= NAND_NO_AUTOINCR;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002767
William Juul52c07962007-10-31 13:53:06 +01002768 /* Try to identify manufacturer */
2769 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2770 if (nand_manuf_ids[maf_idx].id == *maf_id)
2771 break;
2772 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002773
William Juul52c07962007-10-31 13:53:06 +01002774 /*
2775 * Check, if buswidth is correct. Hardware drivers should set
2776 * chip correct !
2777 */
2778 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2779 printk(KERN_INFO "NAND device: Manufacturer ID:"
2780 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
Florian Fainellic98a9352011-02-25 00:01:34 +00002781 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
William Juul52c07962007-10-31 13:53:06 +01002782 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2783 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2784 busw ? 16 : 8);
2785 return ERR_PTR(-EINVAL);
2786 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002787
William Juul52c07962007-10-31 13:53:06 +01002788 /* Calculate the address shift from the page size */
2789 chip->page_shift = ffs(mtd->writesize) - 1;
2790 /* Convert chipsize to number of pages per chip -1. */
2791 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002792
William Juul52c07962007-10-31 13:53:06 +01002793 chip->bbt_erase_shift = chip->phys_erase_shift =
2794 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002795 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05002796 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002797 else {
2798 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
2799 chip->chip_shift += 32 - 1;
2800 }
2801
2802 chip->badblockbits = 8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002803
William Juul52c07962007-10-31 13:53:06 +01002804 /* Set the bad block position */
Christian Hitzb8a6b372011-10-12 09:32:02 +02002805 if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
2806 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2807 else
2808 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002809
Christian Hitzb8a6b372011-10-12 09:32:02 +02002810 /*
2811 * Bad block marker is stored in the last page of each block
2812 * on Samsung and Hynix MLC devices; stored in first two pages
2813 * of each block on Micron devices with 2KiB pages and on
2814 * SLC Samsung, Hynix, Toshiba and AMD/Spansion. All others scan
2815 * only the first page.
William Juul52c07962007-10-31 13:53:06 +01002816 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02002817 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2818 (*maf_id == NAND_MFR_SAMSUNG ||
2819 *maf_id == NAND_MFR_HYNIX))
2820 chip->options |= NAND_BBT_SCANLASTPAGE;
2821 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2822 (*maf_id == NAND_MFR_SAMSUNG ||
2823 *maf_id == NAND_MFR_HYNIX ||
2824 *maf_id == NAND_MFR_TOSHIBA ||
2825 *maf_id == NAND_MFR_AMD)) ||
2826 (mtd->writesize == 2048 &&
2827 *maf_id == NAND_MFR_MICRON))
2828 chip->options |= NAND_BBT_SCAN2NDPAGE;
2829
2830 /*
2831 * Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6
2832 */
2833 if (!(busw & NAND_BUSWIDTH_16) &&
2834 *maf_id == NAND_MFR_STMICRO &&
2835 mtd->writesize == 2048) {
2836 chip->options |= NAND_BBT_SCANBYTE1AND6;
2837 chip->badblockpos = 0;
2838 }
William Juul52c07962007-10-31 13:53:06 +01002839
2840 /* Check for AND chips with 4 page planes */
2841 if (chip->options & NAND_4PAGE_ARRAY)
2842 chip->erase_cmd = multi_erase_cmd;
2843 else
2844 chip->erase_cmd = single_erase_cmd;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002845
William Juul52c07962007-10-31 13:53:06 +01002846 /* Do not replace user supplied command function ! */
2847 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2848 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002849
Christian Hitzb8a6b372011-10-12 09:32:02 +02002850 /* TODO onfi flash name */
Stefan Roese4fe71432008-01-10 18:47:33 +01002851 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
Christian Hitzb8a6b372011-10-12 09:32:02 +02002852 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
2853 nand_manuf_ids[maf_idx].name,
2854#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2855 chip->onfi_version ? chip->onfi_params.model : type->name);
2856#else
2857 type->name);
2858#endif
William Juul52c07962007-10-31 13:53:06 +01002859
2860 return type;
2861}
2862
2863/**
2864 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2865 * @mtd: MTD device structure
2866 * @maxchips: Number of chips to scan for
Lei Wen75bde942011-01-06 09:48:18 +08002867 * @table: Alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01002868 *
2869 * This is the first phase of the normal nand_scan() function. It
2870 * reads the flash ID and sets up MTD fields accordingly.
2871 *
2872 * The mtd->owner field must be set to the module of the caller.
2873 */
Lei Wen75bde942011-01-06 09:48:18 +08002874int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2875 const struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01002876{
Florian Fainellic98a9352011-02-25 00:01:34 +00002877 int i, busw, nand_maf_id, nand_dev_id;
William Juul52c07962007-10-31 13:53:06 +01002878 struct nand_chip *chip = mtd->priv;
Mike Frysinger6c285f62010-10-20 01:15:21 +00002879 const struct nand_flash_dev *type;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002880
William Juul52c07962007-10-31 13:53:06 +01002881 /* Get buswidth to select the correct functions */
2882 busw = chip->options & NAND_BUSWIDTH_16;
2883 /* Set the default functions */
2884 nand_set_defaults(chip, busw);
2885
2886 /* Read the flash type */
Christian Hitz13fc0e22011-10-12 09:32:01 +02002887 type = nand_get_flash_type(mtd, chip, busw,
2888 &nand_maf_id, &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01002889
2890 if (IS_ERR(type)) {
Peter Tyserf96047a2009-02-04 13:39:40 -06002891#ifndef CONFIG_SYS_NAND_QUIET_TEST
William Juul52c07962007-10-31 13:53:06 +01002892 printk(KERN_WARNING "No NAND device found!!!\n");
Peter Tyserf96047a2009-02-04 13:39:40 -06002893#endif
William Juul52c07962007-10-31 13:53:06 +01002894 chip->select_chip(mtd, -1);
2895 return PTR_ERR(type);
2896 }
2897
2898 /* Check for a chip array */
2899 for (i = 1; i < maxchips; i++) {
2900 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02002901 /* See comment in nand_get_flash_type for reset */
2902 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002903 /* Send the command for reading device ID */
2904 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002905 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01002906 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainellic98a9352011-02-25 00:01:34 +00002907 nand_dev_id != chip->read_byte(mtd))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002908 break;
2909 }
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01002910#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002911 if (i > 1)
2912 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01002913#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002914
William Juul52c07962007-10-31 13:53:06 +01002915 /* Store the number of chips and calc total size for mtd */
2916 chip->numchips = i;
2917 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002918
William Juul52c07962007-10-31 13:53:06 +01002919 return 0;
2920}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002921
William Juul52c07962007-10-31 13:53:06 +01002922
2923/**
2924 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2925 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01002926 *
2927 * This is the second phase of the normal nand_scan() function. It
2928 * fills out all the uninitialized function pointers with the defaults
2929 * and scans for a bad block table if appropriate.
2930 */
2931int nand_scan_tail(struct mtd_info *mtd)
2932{
2933 int i;
2934 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002935
William Juul52c07962007-10-31 13:53:06 +01002936 if (!(chip->options & NAND_OWN_BUFFERS))
Simon Glass78851792012-07-29 20:53:25 +00002937 chip->buffers = memalign(ARCH_DMA_MINALIGN,
2938 sizeof(*chip->buffers));
William Juul52c07962007-10-31 13:53:06 +01002939 if (!chip->buffers)
2940 return -ENOMEM;
2941
2942 /* Set the internal oob buffer location, just after the page data */
2943 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2944
2945 /*
2946 * If no default placement scheme is given, select an appropriate one
2947 */
Christian Hitz55f7bca2011-10-12 09:31:59 +02002948 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002949 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002950 case 8:
William Juul52c07962007-10-31 13:53:06 +01002951 chip->ecc.layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002952 break;
2953 case 16:
William Juul52c07962007-10-31 13:53:06 +01002954 chip->ecc.layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002955 break;
2956 case 64:
William Juul52c07962007-10-31 13:53:06 +01002957 chip->ecc.layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002958 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002959 case 128:
William Juul52c07962007-10-31 13:53:06 +01002960 chip->ecc.layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002961 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002962 default:
William Juul52c07962007-10-31 13:53:06 +01002963 printk(KERN_WARNING "No oob scheme defined for "
2964 "oobsize %d\n", mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002965 }
2966 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002967
William Juul52c07962007-10-31 13:53:06 +01002968 if (!chip->write_page)
2969 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002970
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002971 /*
William Juul52c07962007-10-31 13:53:06 +01002972 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2973 * selected and we have 256 byte pagesize fallback to software ECC
2974 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002975
William Juul52c07962007-10-31 13:53:06 +01002976 switch (chip->ecc.mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002977 case NAND_ECC_HW_OOB_FIRST:
2978 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2979 if (!chip->ecc.calculate || !chip->ecc.correct ||
2980 !chip->ecc.hwctl) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02002981 printk(KERN_WARNING "No ECC functions supplied; "
Sandeep Paulrajdea40702009-08-10 13:27:56 -04002982 "Hardware ECC not possible\n");
2983 BUG();
2984 }
2985 if (!chip->ecc.read_page)
2986 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2987
William Juul52c07962007-10-31 13:53:06 +01002988 case NAND_ECC_HW:
2989 /* Use standard hwecc read page function ? */
2990 if (!chip->ecc.read_page)
2991 chip->ecc.read_page = nand_read_page_hwecc;
2992 if (!chip->ecc.write_page)
2993 chip->ecc.write_page = nand_write_page_hwecc;
David Brownellee86b8d2009-11-07 16:27:01 -05002994 if (!chip->ecc.read_page_raw)
2995 chip->ecc.read_page_raw = nand_read_page_raw;
2996 if (!chip->ecc.write_page_raw)
2997 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01002998 if (!chip->ecc.read_oob)
2999 chip->ecc.read_oob = nand_read_oob_std;
3000 if (!chip->ecc.write_oob)
3001 chip->ecc.write_oob = nand_write_oob_std;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003002
William Juul52c07962007-10-31 13:53:06 +01003003 case NAND_ECC_HW_SYNDROME:
Scott Wood5fd2c302008-03-18 15:29:14 -05003004 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3005 !chip->ecc.hwctl) &&
3006 (!chip->ecc.read_page ||
3007 chip->ecc.read_page == nand_read_page_hwecc ||
3008 !chip->ecc.write_page ||
3009 chip->ecc.write_page == nand_write_page_hwecc)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02003010 printk(KERN_WARNING "No ECC functions supplied; "
William Juul52c07962007-10-31 13:53:06 +01003011 "Hardware ECC not possible\n");
3012 BUG();
3013 }
3014 /* Use standard syndrome read/write page function ? */
3015 if (!chip->ecc.read_page)
3016 chip->ecc.read_page = nand_read_page_syndrome;
3017 if (!chip->ecc.write_page)
3018 chip->ecc.write_page = nand_write_page_syndrome;
David Brownellee86b8d2009-11-07 16:27:01 -05003019 if (!chip->ecc.read_page_raw)
3020 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3021 if (!chip->ecc.write_page_raw)
3022 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juul52c07962007-10-31 13:53:06 +01003023 if (!chip->ecc.read_oob)
3024 chip->ecc.read_oob = nand_read_oob_syndrome;
3025 if (!chip->ecc.write_oob)
3026 chip->ecc.write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003027
William Juul52c07962007-10-31 13:53:06 +01003028 if (mtd->writesize >= chip->ecc.size)
3029 break;
3030 printk(KERN_WARNING "%d byte HW ECC not possible on "
3031 "%d byte page size, fallback to SW ECC\n",
3032 chip->ecc.size, mtd->writesize);
3033 chip->ecc.mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003034
William Juul52c07962007-10-31 13:53:06 +01003035 case NAND_ECC_SOFT:
3036 chip->ecc.calculate = nand_calculate_ecc;
3037 chip->ecc.correct = nand_correct_data;
3038 chip->ecc.read_page = nand_read_page_swecc;
Scott Wood3628f002008-10-24 16:20:43 -05003039 chip->ecc.read_subpage = nand_read_subpage;
William Juul52c07962007-10-31 13:53:06 +01003040 chip->ecc.write_page = nand_write_page_swecc;
David Brownellee86b8d2009-11-07 16:27:01 -05003041 chip->ecc.read_page_raw = nand_read_page_raw;
3042 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01003043 chip->ecc.read_oob = nand_read_oob_std;
3044 chip->ecc.write_oob = nand_write_oob_std;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003045 if (!chip->ecc.size)
3046 chip->ecc.size = 256;
William Juul52c07962007-10-31 13:53:06 +01003047 chip->ecc.bytes = 3;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003048 break;
3049
Christian Hitz55f7bca2011-10-12 09:31:59 +02003050 case NAND_ECC_SOFT_BCH:
3051 if (!mtd_nand_has_bch()) {
3052 printk(KERN_WARNING "CONFIG_MTD_ECC_BCH not enabled\n");
3053 return -EINVAL;
3054 }
3055 chip->ecc.calculate = nand_bch_calculate_ecc;
3056 chip->ecc.correct = nand_bch_correct_data;
3057 chip->ecc.read_page = nand_read_page_swecc;
3058 chip->ecc.read_subpage = nand_read_subpage;
3059 chip->ecc.write_page = nand_write_page_swecc;
3060 chip->ecc.read_page_raw = nand_read_page_raw;
3061 chip->ecc.write_page_raw = nand_write_page_raw;
3062 chip->ecc.read_oob = nand_read_oob_std;
3063 chip->ecc.write_oob = nand_write_oob_std;
3064 /*
3065 * Board driver should supply ecc.size and ecc.bytes values to
3066 * select how many bits are correctable; see nand_bch_init()
3067 * for details.
3068 * Otherwise, default to 4 bits for large page devices
3069 */
3070 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3071 chip->ecc.size = 512;
3072 chip->ecc.bytes = 7;
3073 }
3074 chip->ecc.priv = nand_bch_init(mtd,
3075 chip->ecc.size,
3076 chip->ecc.bytes,
3077 &chip->ecc.layout);
3078 if (!chip->ecc.priv)
3079 printk(KERN_WARNING "BCH ECC initialization failed!\n");
3080
3081 break;
3082
William Juul52c07962007-10-31 13:53:06 +01003083 case NAND_ECC_NONE:
3084 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
3085 "This is not recommended !!\n");
3086 chip->ecc.read_page = nand_read_page_raw;
3087 chip->ecc.write_page = nand_write_page_raw;
3088 chip->ecc.read_oob = nand_read_oob_std;
David Brownellee86b8d2009-11-07 16:27:01 -05003089 chip->ecc.read_page_raw = nand_read_page_raw;
3090 chip->ecc.write_page_raw = nand_write_page_raw;
William Juul52c07962007-10-31 13:53:06 +01003091 chip->ecc.write_oob = nand_write_oob_std;
3092 chip->ecc.size = mtd->writesize;
3093 chip->ecc.bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003094 break;
3095
3096 default:
William Juul52c07962007-10-31 13:53:06 +01003097 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
3098 chip->ecc.mode);
3099 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003100 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003101
William Juul52c07962007-10-31 13:53:06 +01003102 /*
3103 * The number of bytes available for a client to place data into
3104 * the out of band area
3105 */
3106 chip->ecc.layout->oobavail = 0;
Sandeep Paulrajeec6cb32009-11-07 14:25:18 -05003107 for (i = 0; chip->ecc.layout->oobfree[i].length
3108 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juul52c07962007-10-31 13:53:06 +01003109 chip->ecc.layout->oobavail +=
3110 chip->ecc.layout->oobfree[i].length;
3111 mtd->oobavail = chip->ecc.layout->oobavail;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003112
William Juul52c07962007-10-31 13:53:06 +01003113 /*
3114 * Set the number of read / write steps for one page depending on ECC
3115 * mode
3116 */
3117 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Christian Hitz13fc0e22011-10-12 09:32:01 +02003118 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
William Juul52c07962007-10-31 13:53:06 +01003119 printk(KERN_WARNING "Invalid ecc parameters\n");
3120 BUG();
3121 }
3122 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003123
William Juul52c07962007-10-31 13:53:06 +01003124 /*
3125 * Allow subpage writes up to ecc.steps. Not possible for MLC
3126 * FLASH.
3127 */
3128 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3129 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Christian Hitz13fc0e22011-10-12 09:32:01 +02003130 switch (chip->ecc.steps) {
William Juul52c07962007-10-31 13:53:06 +01003131 case 2:
3132 mtd->subpage_sft = 1;
3133 break;
3134 case 4:
3135 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05003136 case 16:
William Juul52c07962007-10-31 13:53:06 +01003137 mtd->subpage_sft = 2;
3138 break;
3139 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003140 }
William Juul52c07962007-10-31 13:53:06 +01003141 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003142
William Juul52c07962007-10-31 13:53:06 +01003143 /* Initialize state */
3144 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003145
3146 /* De-select the device */
William Juul52c07962007-10-31 13:53:06 +01003147 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003148
3149 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01003150 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003151
3152 /* Fill in remaining MTD driver data */
3153 mtd->type = MTD_NANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003154 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3155 MTD_CAP_NANDFLASH;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003156 mtd->erase = nand_erase;
3157 mtd->point = NULL;
3158 mtd->unpoint = NULL;
3159 mtd->read = nand_read;
3160 mtd->write = nand_write;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003161 mtd->read_oob = nand_read_oob;
3162 mtd->write_oob = nand_write_oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003163 mtd->sync = nand_sync;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003164 mtd->lock = NULL;
3165 mtd->unlock = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003166 mtd->block_isbad = nand_block_isbad;
3167 mtd->block_markbad = nand_block_markbad;
3168
William Juul52c07962007-10-31 13:53:06 +01003169 /* propagate ecc.layout to mtd_info */
3170 mtd->ecclayout = chip->ecc.layout;
3171
3172 /* Check, if we should skip the bad block table scan */
3173 if (chip->options & NAND_SKIP_BBTSCAN)
Scott Woodf2f5c9e2012-02-20 14:50:39 -06003174 chip->options |= NAND_BBT_SCANNED;
William Juul52c07962007-10-31 13:53:06 +01003175
Scott Woodf2f5c9e2012-02-20 14:50:39 -06003176 return 0;
William Juul52c07962007-10-31 13:53:06 +01003177}
3178
William Juul52c07962007-10-31 13:53:06 +01003179/**
3180 * nand_scan - [NAND Interface] Scan for the NAND device
3181 * @mtd: MTD device structure
3182 * @maxchips: Number of chips to scan for
3183 *
3184 * This fills out all the uninitialized function pointers
3185 * with the defaults.
3186 * The flash ID is read and the mtd/chip structures are
3187 * filled with the appropriate values.
3188 * The mtd->owner field must be set to the module of the caller
3189 *
3190 */
3191int nand_scan(struct mtd_info *mtd, int maxchips)
3192{
3193 int ret;
3194
Lei Wen75bde942011-01-06 09:48:18 +08003195 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01003196 if (!ret)
3197 ret = nand_scan_tail(mtd);
3198 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003199}
3200
3201/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003202 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003203 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01003204*/
3205void nand_release(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003206{
William Juul52c07962007-10-31 13:53:06 +01003207 struct nand_chip *chip = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003208
Christian Hitz55f7bca2011-10-12 09:31:59 +02003209 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3210 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3211
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003212#ifdef CONFIG_MTD_PARTITIONS
3213 /* Deregister partitions */
William Juul52c07962007-10-31 13:53:06 +01003214 del_mtd_partitions(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003215#endif
William Juul52c07962007-10-31 13:53:06 +01003216
3217 /* Free bad block table memory */
3218 kfree(chip->bbt);
3219 if (!(chip->options & NAND_OWN_BUFFERS))
3220 kfree(chip->buffers);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003221
3222 /* Free bad block descriptor memory */
3223 if (chip->badblock_pattern && chip->badblock_pattern->options
3224 & NAND_BBT_DYNAMICSTRUCT)
3225 kfree(chip->badblock_pattern);
William Juul52c07962007-10-31 13:53:06 +01003226}