blob: 9e05cef4179f3ddfc10e26f7e116f32c399364e2 [file] [log] [blame]
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +02008 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02009 * Additional technical information is available on
Scott Woodc45912d2008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020011 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juulcfa460a2007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020014 *
William Juulcfa460a2007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk932394a2005-08-17 12:55:25 +020022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapindfe64e22013-01-14 03:46:50 +000024 * if we have HW ECC support.
Wolfgang Denk932394a2005-08-17 12:55:25 +020025 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Scott Woodc45912d2008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk932394a2005-08-17 12:55:25 +020028 *
Wolfgang Denk932394a2005-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 Denk932394a2005-08-17 12:55:25 +020035#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010036
William Juulcfa460a2007-10-31 13:53:06 +010037#define ENOTSUPP 524 /* Operation is not supported */
38
Wolfgang Denk932394a2005-08-17 12:55:25 +020039#include <malloc.h>
40#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010041#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000042#include <linux/compat.h>
Wolfgang Denk932394a2005-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 Hitz4c6de852011-10-12 09:31:59 +020046#include <linux/mtd/nand_bch.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020047
Stefan Roese10bb62d2009-04-24 15:58:33 +020048#ifdef CONFIG_MTD_PARTITIONS
49#include <linux/mtd/partitions.h>
50#endif
51
Wolfgang Denk932394a2005-08-17 12:55:25 +020052#include <asm/io.h>
53#include <asm/errno.h>
54
Peter Tyser8da60122009-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 Denk932394a2005-08-17 12:55:25 +020066/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010067static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020068 .eccbytes = 3,
69 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010070 .oobfree = {
71 {.offset = 3,
72 .length = 2},
73 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020074 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020075};
76
William Juulcfa460a2007-10-31 13:53:06 +010077static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020078 .eccbytes = 6,
79 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010080 .oobfree = {
81 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020082 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020083};
84
William Juulcfa460a2007-10-31 13:53:06 +010085static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020086 .eccbytes = 24,
87 .eccpos = {
William Juulcfa460a2007-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 Hitz90e3f392011-10-12 09:32:01 +020093 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020094};
95
William Juulcfa460a2007-10-31 13:53:06 +010096static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +020097 .eccbytes = 48,
98 .eccpos = {
Christian Hitz90e3f392011-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 Juulcfa460a2007-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 Hitz90e3f392011-10-12 09:32:01 +0200107 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200108};
109
William Juulcfa460a2007-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 Poselenov248ae5c2008-06-06 15:42:43 +0200117
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200118static int check_offs_len(struct mtd_info *mtd,
119 loff_t ofs, uint64_t len)
120{
121 struct nand_chip *chip = mtd->priv;
122 int ret = 0;
123
124 /* Start address must align on block boundary */
125 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
126 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
127 ret = -EINVAL;
128 }
129
130 /* Length must align on block boundary */
131 if (len & ((1 << chip->phys_erase_shift) - 1)) {
132 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
133 __func__);
134 ret = -EINVAL;
135 }
136
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200137 return ret;
138}
139
Wolfgang Denk932394a2005-08-17 12:55:25 +0200140/**
141 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000142 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200143 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000144 * Deselect, release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200145 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200146static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100147{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200148 struct nand_chip *chip = mtd->priv;
149
150 /* De-select the NAND device */
151 chip->select_chip(mtd, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100152}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200153
154/**
155 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000156 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200157 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000158 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200159 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000160uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200161{
William Juulcfa460a2007-10-31 13:53:06 +0100162 struct nand_chip *chip = mtd->priv;
163 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200164}
165
166/**
167 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000168 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
169 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200170 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000171 * Default read function for 16bit buswidth with endianness conversion.
172 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200173 */
William Juulcfa460a2007-10-31 13:53:06 +0100174static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200175{
William Juulcfa460a2007-10-31 13:53:06 +0100176 struct nand_chip *chip = mtd->priv;
177 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200178}
179
180/**
181 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000182 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200183 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000184 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200185 */
186static u16 nand_read_word(struct mtd_info *mtd)
187{
William Juulcfa460a2007-10-31 13:53:06 +0100188 struct nand_chip *chip = mtd->priv;
189 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200190}
191
192/**
193 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000194 * @mtd: MTD device structure
195 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200196 *
197 * Default select function for 1 chip devices.
198 */
William Juulcfa460a2007-10-31 13:53:06 +0100199static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200200{
William Juulcfa460a2007-10-31 13:53:06 +0100201 struct nand_chip *chip = mtd->priv;
202
203 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200204 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100205 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200206 break;
207 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200208 break;
209
210 default:
211 BUG();
212 }
213}
214
215/**
216 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000217 * @mtd: MTD device structure
218 * @buf: data buffer
219 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200220 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000221 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200222 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000223void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200224{
225 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100226 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200227
William Juulcfa460a2007-10-31 13:53:06 +0100228 for (i = 0; i < len; i++)
229 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200230}
231
232/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200233 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000234 * @mtd: MTD device structure
235 * @buf: buffer to store date
236 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200237 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000238 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200239 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400240void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200241{
242 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100243 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200244
William Juulcfa460a2007-10-31 13:53:06 +0100245 for (i = 0; i < len; i++)
246 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200247}
248
249/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200250 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200254 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000255 * Default verify function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200256 */
William Juulcfa460a2007-10-31 13:53:06 +0100257static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200258{
259 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100260 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200261
William Juulcfa460a2007-10-31 13:53:06 +0100262 for (i = 0; i < len; i++)
263 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200264 return -EFAULT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200265 return 0;
266}
267
268/**
269 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000270 * @mtd: MTD device structure
271 * @buf: data buffer
272 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200273 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000274 * Default write function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200275 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000276void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200277{
278 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100279 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200280 u16 *p = (u16 *) buf;
281 len >>= 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200282
William Juulcfa460a2007-10-31 13:53:06 +0100283 for (i = 0; i < len; i++)
284 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200285
Wolfgang Denk932394a2005-08-17 12:55:25 +0200286}
287
288/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200289 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000290 * @mtd: MTD device structure
291 * @buf: buffer to store date
292 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200293 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000294 * Default read function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200295 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400296void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200297{
298 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100299 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200300 u16 *p = (u16 *) buf;
301 len >>= 1;
302
William Juulcfa460a2007-10-31 13:53:06 +0100303 for (i = 0; i < len; i++)
304 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200305}
306
307/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200308 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000309 * @mtd: MTD device structure
310 * @buf: buffer containing the data to compare
311 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200312 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000313 * Default verify function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200314 */
William Juulcfa460a2007-10-31 13:53:06 +0100315static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200316{
317 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100318 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200319 u16 *p = (u16 *) buf;
320 len >>= 1;
321
William Juulcfa460a2007-10-31 13:53:06 +0100322 for (i = 0; i < len; i++)
323 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200324 return -EFAULT;
325
326 return 0;
327}
328
329/**
330 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000331 * @mtd: MTD device structure
332 * @ofs: offset from device start
333 * @getchip: 0, if the chip is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200334 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200335 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200336 */
337static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
338{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000339 int page, chipnr, res = 0, i = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100340 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200341 u16 bad;
342
Sergey Lapindfe64e22013-01-14 03:46:50 +0000343 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200344 ofs += mtd->erasesize - mtd->writesize;
345
William Juulcfa460a2007-10-31 13:53:06 +0100346 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200347
Wolfgang Denk932394a2005-08-17 12:55:25 +0200348 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100349 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200350
William Juulcfa460a2007-10-31 13:53:06 +0100351 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200352
353 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100354 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200355 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200356
Sergey Lapindfe64e22013-01-14 03:46:50 +0000357 do {
358 if (chip->options & NAND_BUSWIDTH_16) {
359 chip->cmdfunc(mtd, NAND_CMD_READOOB,
360 chip->badblockpos & 0xFE, page);
361 bad = cpu_to_le16(chip->read_word(mtd));
362 if (chip->badblockpos & 0x1)
363 bad >>= 8;
364 else
365 bad &= 0xFF;
366 } else {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
368 page);
369 bad = chip->read_byte(mtd);
370 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200371
Sergey Lapindfe64e22013-01-14 03:46:50 +0000372 if (likely(chip->badblockbits == 8))
373 res = bad != 0xFF;
374 else
375 res = hweight8(bad) < chip->badblockbits;
376 ofs += mtd->writesize;
377 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
378 i++;
379 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200380
William Juulcfa460a2007-10-31 13:53:06 +0100381 if (getchip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200382 nand_release_device(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200383
Wolfgang Denk932394a2005-08-17 12:55:25 +0200384 return res;
385}
386
387/**
388 * nand_default_block_markbad - [DEFAULT] mark a block bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000389 * @mtd: MTD device structure
390 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200391 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000392 * This is the default implementation, which can be overridden by a hardware
393 * specific driver. We try operations in the following order, according to our
394 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
395 * (1) erase the affected block, to allow OOB marker to be written cleanly
396 * (2) update in-memory BBT
397 * (3) write bad block marker to OOB area of affected block
398 * (4) update flash-based BBT
399 * Note that we retain the first error encountered in (3) or (4), finish the
400 * procedures, and dump the error in the end.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200401*/
402static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
403{
William Juulcfa460a2007-10-31 13:53:06 +0100404 struct nand_chip *chip = mtd->priv;
405 uint8_t buf[2] = { 0, 0 };
Sergey Lapindfe64e22013-01-14 03:46:50 +0000406 int block, res, ret = 0, i = 0;
407 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200408
Sergey Lapindfe64e22013-01-14 03:46:50 +0000409 if (write_oob) {
410 struct erase_info einfo;
411
412 /* Attempt erase before marking OOB */
413 memset(&einfo, 0, sizeof(einfo));
414 einfo.mtd = mtd;
415 einfo.addr = ofs;
416 einfo.len = 1 << chip->phys_erase_shift;
417 nand_erase_nand(mtd, &einfo, 0);
418 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200419
Wolfgang Denk932394a2005-08-17 12:55:25 +0200420 /* Get block number */
William Juulcfa460a2007-10-31 13:53:06 +0100421 block = (int)(ofs >> chip->bbt_erase_shift);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000422 /* Mark block bad in memory-based BBT */
William Juulcfa460a2007-10-31 13:53:06 +0100423 if (chip->bbt)
424 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200425
Sergey Lapindfe64e22013-01-14 03:46:50 +0000426 /* Write bad block marker to OOB */
427 if (write_oob) {
428 struct mtd_oob_ops ops;
429 loff_t wr_ofs = ofs;
430
Scott Woodc45912d2008-10-24 16:20:43 -0500431 nand_get_device(chip, mtd, FL_WRITING);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200432
Sergey Lapindfe64e22013-01-14 03:46:50 +0000433 ops.datbuf = NULL;
434 ops.oobbuf = buf;
435 ops.ooboffs = chip->badblockpos;
436 if (chip->options & NAND_BUSWIDTH_16) {
437 ops.ooboffs &= ~0x01;
438 ops.len = ops.ooblen = 2;
439 } else {
440 ops.len = ops.ooblen = 1;
441 }
442 ops.mode = MTD_OPS_PLACE_OOB;
443
444 /* Write to first/last page(s) if necessary */
445 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
446 wr_ofs += mtd->erasesize - mtd->writesize;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200447 do {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000448 res = nand_do_write_oob(mtd, wr_ofs, &ops);
449 if (!ret)
450 ret = res;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200451
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200452 i++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000453 wr_ofs += mtd->writesize;
454 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200455
Scott Woodc45912d2008-10-24 16:20:43 -0500456 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100457 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000458
459 /* Update flash-based bad block table */
460 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
461 res = nand_update_bbt(mtd, ofs);
462 if (!ret)
463 ret = res;
464 }
465
William Juulcfa460a2007-10-31 13:53:06 +0100466 if (!ret)
467 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500468
William Juulcfa460a2007-10-31 13:53:06 +0100469 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200470}
471
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200472/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200473 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000474 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200475 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000476 * Check, if the device is write protected. The function expects, that the
477 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200478 */
William Juulcfa460a2007-10-31 13:53:06 +0100479static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200480{
William Juulcfa460a2007-10-31 13:53:06 +0100481 struct nand_chip *chip = mtd->priv;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200482
Sergey Lapindfe64e22013-01-14 03:46:50 +0000483 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200484 if (chip->options & NAND_BROKEN_XD)
485 return 0;
486
Wolfgang Denk932394a2005-08-17 12:55:25 +0200487 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100488 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
489 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200490}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100491
Wolfgang Denk932394a2005-08-17 12:55:25 +0200492/**
493 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000494 * @mtd: MTD device structure
495 * @ofs: offset from device start
496 * @getchip: 0, if the chip is already selected
497 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200498 *
499 * Check, if the block is bad. Either by reading the bad block table or
500 * calling of the scan function.
501 */
William Juulcfa460a2007-10-31 13:53:06 +0100502static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
503 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200504{
William Juulcfa460a2007-10-31 13:53:06 +0100505 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200506
Scott Woodfb494542012-02-20 14:50:39 -0600507 if (!(chip->options & NAND_BBT_SCANNED)) {
508 chip->options |= NAND_BBT_SCANNED;
509 chip->scan_bbt(mtd);
510 }
511
William Juulcfa460a2007-10-31 13:53:06 +0100512 if (!chip->bbt)
513 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200514
Wolfgang Denk932394a2005-08-17 12:55:25 +0200515 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100516 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200517}
518
Sergey Lapindfe64e22013-01-14 03:46:50 +0000519/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juulcfa460a2007-10-31 13:53:06 +0100520void nand_wait_ready(struct mtd_info *mtd)
521{
522 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200523 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000524 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100525
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000526 time_start = get_timer(0);
Stefan Roese12072262008-01-05 16:43:25 +0100527
Sergey Lapindfe64e22013-01-14 03:46:50 +0000528 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000529 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100530 if (chip->dev_ready)
531 if (chip->dev_ready(mtd))
532 break;
533 }
William Juulcfa460a2007-10-31 13:53:06 +0100534}
William Juulcfa460a2007-10-31 13:53:06 +0100535
Wolfgang Denk932394a2005-08-17 12:55:25 +0200536/**
537 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000538 * @mtd: MTD device structure
539 * @command: the command to be sent
540 * @column: the column address for this command, -1 if none
541 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200542 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000543 * Send command to NAND device. This function is used for small page devices
544 * (256/512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200545 */
William Juulcfa460a2007-10-31 13:53:06 +0100546static void nand_command(struct mtd_info *mtd, unsigned int command,
547 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200548{
William Juulcfa460a2007-10-31 13:53:06 +0100549 register struct nand_chip *chip = mtd->priv;
550 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600551 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200552
Sergey Lapindfe64e22013-01-14 03:46:50 +0000553 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200554 if (command == NAND_CMD_SEQIN) {
555 int readcmd;
556
William Juulcfa460a2007-10-31 13:53:06 +0100557 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200558 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100559 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
564 } else {
565 column -= 256;
566 readcmd = NAND_CMD_READ1;
567 }
William Juulcfa460a2007-10-31 13:53:06 +0100568 chip->cmd_ctrl(mtd, readcmd, ctrl);
569 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200570 }
William Juulcfa460a2007-10-31 13:53:06 +0100571 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200572
Sergey Lapindfe64e22013-01-14 03:46:50 +0000573 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100574 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
575 /* Serially input address */
576 if (column != -1) {
577 /* Adjust columns for 16 bit buswidth */
578 if (chip->options & NAND_BUSWIDTH_16)
579 column >>= 1;
580 chip->cmd_ctrl(mtd, column, ctrl);
581 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200582 }
William Juulcfa460a2007-10-31 13:53:06 +0100583 if (page_addr != -1) {
584 chip->cmd_ctrl(mtd, page_addr, ctrl);
585 ctrl &= ~NAND_CTRL_CHANGE;
586 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
587 /* One more address cycle for devices > 32MiB */
588 if (chip->chipsize > (32 << 20))
589 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
590 }
591 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200592
593 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000594 * Program and erase have their own busy handlers status and sequential
595 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100596 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200597 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200598
Wolfgang Denk932394a2005-08-17 12:55:25 +0200599 case NAND_CMD_PAGEPROG:
600 case NAND_CMD_ERASE1:
601 case NAND_CMD_ERASE2:
602 case NAND_CMD_SEQIN:
603 case NAND_CMD_STATUS:
604 return;
605
606 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100607 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200608 break;
William Juulcfa460a2007-10-31 13:53:06 +0100609 udelay(chip->chip_delay);
610 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
611 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
612 chip->cmd_ctrl(mtd,
613 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600614 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
615 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200616 return;
617
William Juulcfa460a2007-10-31 13:53:06 +0100618 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200619 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200620 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200621 * If we don't have access to the busy pin, we apply the given
622 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100623 */
624 if (!chip->dev_ready) {
625 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200626 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200627 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200628 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000629 /*
630 * Apply this short delay always to ensure that we do wait tWB in
631 * any case on any machine.
632 */
William Juulcfa460a2007-10-31 13:53:06 +0100633 ndelay(100);
634
635 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200636}
637
638/**
639 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000640 * @mtd: MTD device structure
641 * @command: the command to be sent
642 * @column: the column address for this command, -1 if none
643 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200644 *
William Juulcfa460a2007-10-31 13:53:06 +0100645 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000646 * devices. We don't have the separate regions as we have in the small page
647 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200648 */
William Juulcfa460a2007-10-31 13:53:06 +0100649static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
650 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200651{
William Juulcfa460a2007-10-31 13:53:06 +0100652 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600653 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200654
655 /* Emulate NAND_CMD_READOOB */
656 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100657 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200658 command = NAND_CMD_READ0;
659 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200660
William Juulcfa460a2007-10-31 13:53:06 +0100661 /* Command latch cycle */
662 chip->cmd_ctrl(mtd, command & 0xff,
663 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200664
665 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100666 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200667
668 /* Serially input address */
669 if (column != -1) {
670 /* Adjust columns for 16 bit buswidth */
William Juulcfa460a2007-10-31 13:53:06 +0100671 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200672 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100673 chip->cmd_ctrl(mtd, column, ctrl);
674 ctrl &= ~NAND_CTRL_CHANGE;
675 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200676 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200677 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100678 chip->cmd_ctrl(mtd, page_addr, ctrl);
679 chip->cmd_ctrl(mtd, page_addr >> 8,
680 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200681 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100682 if (chip->chipsize > (128 << 20))
683 chip->cmd_ctrl(mtd, page_addr >> 16,
684 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200685 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200686 }
William Juulcfa460a2007-10-31 13:53:06 +0100687 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200688
689 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000690 * Program and erase have their own busy handlers status, sequential
691 * in, and deplete1 need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100692 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200693 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200694
Wolfgang Denk932394a2005-08-17 12:55:25 +0200695 case NAND_CMD_CACHEDPROG:
696 case NAND_CMD_PAGEPROG:
697 case NAND_CMD_ERASE1:
698 case NAND_CMD_ERASE2:
699 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100700 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200701 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100702 case NAND_CMD_DEPLETE1:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200703 return;
704
William Juulcfa460a2007-10-31 13:53:06 +0100705 case NAND_CMD_STATUS_ERROR:
706 case NAND_CMD_STATUS_ERROR0:
707 case NAND_CMD_STATUS_ERROR1:
708 case NAND_CMD_STATUS_ERROR2:
709 case NAND_CMD_STATUS_ERROR3:
Sergey Lapindfe64e22013-01-14 03:46:50 +0000710 /* Read error status commands require only a short delay */
William Juulcfa460a2007-10-31 13:53:06 +0100711 udelay(chip->chip_delay);
712 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200713
714 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100715 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200716 break;
William Juulcfa460a2007-10-31 13:53:06 +0100717 udelay(chip->chip_delay);
718 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
719 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
720 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
721 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600722 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
723 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100724 return;
725
726 case NAND_CMD_RNDOUT:
727 /* No ready / busy check necessary */
728 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
729 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
730 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
731 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200732 return;
733
734 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100735 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
736 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
737 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
738 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200739
William Juulcfa460a2007-10-31 13:53:06 +0100740 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200741 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200742 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200743 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000744 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100745 */
746 if (!chip->dev_ready) {
747 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200748 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200749 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200750 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200751
Sergey Lapindfe64e22013-01-14 03:46:50 +0000752 /*
753 * Apply this short delay always to ensure that we do wait tWB in
754 * any case on any machine.
755 */
William Juulcfa460a2007-10-31 13:53:06 +0100756 ndelay(100);
757
758 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200759}
760
761/**
762 * nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000763 * @chip: the nand chip descriptor
764 * @mtd: MTD device structure
765 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200766 *
767 * Get the device and lock it for exclusive access
768 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200769static int
770nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100771{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200772 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100773 return 0;
774}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200775
776/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000777 * nand_wait - [DEFAULT] wait until the command is done
778 * @mtd: MTD device structure
779 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200780 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000781 * Wait for command done. This applies to erase and program only. Erase can
782 * take up to 400ms and program up to 20ms according to general NAND and
783 * SmartMedia specs.
William Juulcfa460a2007-10-31 13:53:06 +0100784 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200785static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200786{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100787 unsigned long timeo;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200788 int state = chip->state;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000789 u32 time_start;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100790
791 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200792 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100793 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200794 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100795
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200796 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
797 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100798 else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200799 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100800
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000801 time_start = get_timer(0);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100802
803 while (1) {
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000804 if (get_timer(time_start) > timeo) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100805 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100806 return 0x01;
807 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100808
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200809 if (chip->dev_ready) {
810 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100811 break;
812 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200813 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100814 break;
815 }
816 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100817#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000818 time_start = get_timer(0);
819 while (get_timer(time_start) < 10)
820 ;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100821#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100822
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200823 return (int)chip->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200824}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200825
826/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000827 * nand_read_page_raw - [INTERN] read raw page data without ecc
828 * @mtd: mtd info structure
829 * @chip: nand chip info structure
830 * @buf: buffer to store read data
831 * @oob_required: caller requires OOB data read to chip->oob_poi
832 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500833 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000834 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200835 */
William Juulcfa460a2007-10-31 13:53:06 +0100836static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000837 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200838{
William Juulcfa460a2007-10-31 13:53:06 +0100839 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000840 if (oob_required)
841 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +0100842 return 0;
843}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200844
William Juulcfa460a2007-10-31 13:53:06 +0100845/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000846 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
847 * @mtd: mtd info structure
848 * @chip: nand chip info structure
849 * @buf: buffer to store read data
850 * @oob_required: caller requires OOB data read to chip->oob_poi
851 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500852 *
853 * We need a special oob layout and handling even when OOB isn't used.
854 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200855static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000856 struct nand_chip *chip, uint8_t *buf,
857 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -0500858{
859 int eccsize = chip->ecc.size;
860 int eccbytes = chip->ecc.bytes;
861 uint8_t *oob = chip->oob_poi;
862 int steps, size;
863
864 for (steps = chip->ecc.steps; steps > 0; steps--) {
865 chip->read_buf(mtd, buf, eccsize);
866 buf += eccsize;
867
868 if (chip->ecc.prepad) {
869 chip->read_buf(mtd, oob, chip->ecc.prepad);
870 oob += chip->ecc.prepad;
871 }
872
873 chip->read_buf(mtd, oob, eccbytes);
874 oob += eccbytes;
875
876 if (chip->ecc.postpad) {
877 chip->read_buf(mtd, oob, chip->ecc.postpad);
878 oob += chip->ecc.postpad;
879 }
880 }
881
882 size = mtd->oobsize - (oob - chip->oob_poi);
883 if (size)
884 chip->read_buf(mtd, oob, size);
885
886 return 0;
887}
888
889/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000890 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
891 * @mtd: mtd info structure
892 * @chip: nand chip info structure
893 * @buf: buffer to store read data
894 * @oob_required: caller requires OOB data read to chip->oob_poi
895 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +0100896 */
897static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000898 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +0100899{
900 int i, eccsize = chip->ecc.size;
901 int eccbytes = chip->ecc.bytes;
902 int eccsteps = chip->ecc.steps;
903 uint8_t *p = buf;
904 uint8_t *ecc_calc = chip->buffers->ecccalc;
905 uint8_t *ecc_code = chip->buffers->ecccode;
906 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200907
Sergey Lapindfe64e22013-01-14 03:46:50 +0000908 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200909
William Juulcfa460a2007-10-31 13:53:06 +0100910 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
911 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200912
William Juulcfa460a2007-10-31 13:53:06 +0100913 for (i = 0; i < chip->ecc.total; i++)
914 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200915
William Juulcfa460a2007-10-31 13:53:06 +0100916 eccsteps = chip->ecc.steps;
917 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200918
William Juulcfa460a2007-10-31 13:53:06 +0100919 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
920 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200921
William Juulcfa460a2007-10-31 13:53:06 +0100922 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodc45912d2008-10-24 16:20:43 -0500923 if (stat < 0)
924 mtd->ecc_stats.failed++;
925 else
926 mtd->ecc_stats.corrected += stat;
927 }
928 return 0;
929}
930
931/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000932 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
933 * @mtd: mtd info structure
934 * @chip: nand chip info structure
935 * @data_offs: offset of requested data within the page
936 * @readlen: data length
937 * @bufpoi: buffer to store read data
Scott Woodc45912d2008-10-24 16:20:43 -0500938 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200939static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
940 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Scott Woodc45912d2008-10-24 16:20:43 -0500941{
942 int start_step, end_step, num_steps;
943 uint32_t *eccpos = chip->ecc.layout->eccpos;
944 uint8_t *p;
945 int data_col_addr, i, gaps = 0;
946 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
947 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200948 int index = 0;
Scott Woodc45912d2008-10-24 16:20:43 -0500949
Sergey Lapindfe64e22013-01-14 03:46:50 +0000950 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -0500951 start_step = data_offs / chip->ecc.size;
952 end_step = (data_offs + readlen - 1) / chip->ecc.size;
953 num_steps = end_step - start_step + 1;
954
Sergey Lapindfe64e22013-01-14 03:46:50 +0000955 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -0500956 datafrag_len = num_steps * chip->ecc.size;
957 eccfrag_len = num_steps * chip->ecc.bytes;
958
959 data_col_addr = start_step * chip->ecc.size;
960 /* If we read not a page aligned data */
961 if (data_col_addr != 0)
962 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
963
964 p = bufpoi + data_col_addr;
965 chip->read_buf(mtd, p, datafrag_len);
966
Sergey Lapindfe64e22013-01-14 03:46:50 +0000967 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -0500968 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
969 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
970
Sergey Lapindfe64e22013-01-14 03:46:50 +0000971 /*
972 * The performance is faster if we position offsets according to
973 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
974 */
Scott Woodc45912d2008-10-24 16:20:43 -0500975 for (i = 0; i < eccfrag_len - 1; i++) {
976 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
977 eccpos[i + start_step * chip->ecc.bytes + 1]) {
978 gaps = 1;
979 break;
980 }
981 }
982 if (gaps) {
983 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
984 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
985 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000986 /*
987 * Send the command to read the particular ECC bytes take care
988 * about buswidth alignment in read_buf.
989 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200990 index = start_step * chip->ecc.bytes;
991
992 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -0500993 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200994 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -0500995 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200996 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -0500997 aligned_len++;
998
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200999 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1000 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001001 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1002 }
1003
1004 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001005 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001006
1007 p = bufpoi + data_col_addr;
1008 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1009 int stat;
1010
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001011 stat = chip->ecc.correct(mtd, p,
1012 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1013 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001014 mtd->ecc_stats.failed++;
1015 else
1016 mtd->ecc_stats.corrected += stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001017 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001018 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001019}
1020
Wolfgang Denk932394a2005-08-17 12:55:25 +02001021/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001022 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1023 * @mtd: mtd info structure
1024 * @chip: nand chip info structure
1025 * @buf: buffer to store read data
1026 * @oob_required: caller requires OOB data read to chip->oob_poi
1027 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001028 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001029 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001030 */
William Juulcfa460a2007-10-31 13:53:06 +01001031static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001032 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001033{
William Juulcfa460a2007-10-31 13:53:06 +01001034 int i, eccsize = chip->ecc.size;
1035 int eccbytes = chip->ecc.bytes;
1036 int eccsteps = chip->ecc.steps;
1037 uint8_t *p = buf;
1038 uint8_t *ecc_calc = chip->buffers->ecccalc;
1039 uint8_t *ecc_code = chip->buffers->ecccode;
1040 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001041
William Juulcfa460a2007-10-31 13:53:06 +01001042 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1043 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1044 chip->read_buf(mtd, p, eccsize);
1045 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1046 }
1047 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001048
William Juulcfa460a2007-10-31 13:53:06 +01001049 for (i = 0; i < chip->ecc.total; i++)
1050 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001051
William Juulcfa460a2007-10-31 13:53:06 +01001052 eccsteps = chip->ecc.steps;
1053 p = buf;
1054
1055 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1056 int stat;
1057
1058 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj18b5a4b2009-11-07 14:25:03 -05001059 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001060 mtd->ecc_stats.failed++;
1061 else
1062 mtd->ecc_stats.corrected += stat;
1063 }
1064 return 0;
1065}
1066
1067/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001068 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1069 * @mtd: mtd info structure
1070 * @chip: nand chip info structure
1071 * @buf: buffer to store read data
1072 * @oob_required: caller requires OOB data read to chip->oob_poi
1073 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001074 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001075 * Hardware ECC for large page chips, require OOB to be read first. For this
1076 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1077 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1078 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1079 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001080 */
1081static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001082 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001083{
1084 int i, eccsize = chip->ecc.size;
1085 int eccbytes = chip->ecc.bytes;
1086 int eccsteps = chip->ecc.steps;
1087 uint8_t *p = buf;
1088 uint8_t *ecc_code = chip->buffers->ecccode;
1089 uint32_t *eccpos = chip->ecc.layout->eccpos;
1090 uint8_t *ecc_calc = chip->buffers->ecccalc;
1091
1092 /* Read the OOB area first */
1093 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1094 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1095 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1096
1097 for (i = 0; i < chip->ecc.total; i++)
1098 ecc_code[i] = chip->oob_poi[eccpos[i]];
1099
1100 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1101 int stat;
1102
1103 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1104 chip->read_buf(mtd, p, eccsize);
1105 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1106
1107 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1108 if (stat < 0)
1109 mtd->ecc_stats.failed++;
1110 else
1111 mtd->ecc_stats.corrected += stat;
1112 }
1113 return 0;
1114}
1115
1116/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001117 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1118 * @mtd: mtd info structure
1119 * @chip: nand chip info structure
1120 * @buf: buffer to store read data
1121 * @oob_required: caller requires OOB data read to chip->oob_poi
1122 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001123 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001124 * The hw generator calculates the error syndrome automatically. Therefore we
1125 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001126 */
1127static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001128 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001129{
1130 int i, eccsize = chip->ecc.size;
1131 int eccbytes = chip->ecc.bytes;
1132 int eccsteps = chip->ecc.steps;
1133 uint8_t *p = buf;
1134 uint8_t *oob = chip->oob_poi;
1135
1136 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1137 int stat;
1138
1139 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1140 chip->read_buf(mtd, p, eccsize);
1141
1142 if (chip->ecc.prepad) {
1143 chip->read_buf(mtd, oob, chip->ecc.prepad);
1144 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001145 }
1146
William Juulcfa460a2007-10-31 13:53:06 +01001147 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1148 chip->read_buf(mtd, oob, eccbytes);
1149 stat = chip->ecc.correct(mtd, p, oob, NULL);
1150
Scott Woodc45912d2008-10-24 16:20:43 -05001151 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001152 mtd->ecc_stats.failed++;
1153 else
1154 mtd->ecc_stats.corrected += stat;
1155
1156 oob += eccbytes;
1157
1158 if (chip->ecc.postpad) {
1159 chip->read_buf(mtd, oob, chip->ecc.postpad);
1160 oob += chip->ecc.postpad;
1161 }
1162 }
1163
1164 /* Calculate remaining oob bytes */
1165 i = mtd->oobsize - (oob - chip->oob_poi);
1166 if (i)
1167 chip->read_buf(mtd, oob, i);
1168
1169 return 0;
1170}
1171
1172/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001173 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1174 * @chip: nand chip structure
1175 * @oob: oob destination address
1176 * @ops: oob ops structure
1177 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001178 */
1179static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1180 struct mtd_oob_ops *ops, size_t len)
1181{
Christian Hitz90e3f392011-10-12 09:32:01 +02001182 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001183
Sergey Lapindfe64e22013-01-14 03:46:50 +00001184 case MTD_OPS_PLACE_OOB:
1185 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001186 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1187 return oob + len;
1188
Sergey Lapindfe64e22013-01-14 03:46:50 +00001189 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001190 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1191 uint32_t boffs = 0, roffs = ops->ooboffs;
1192 size_t bytes = 0;
1193
Christian Hitz90e3f392011-10-12 09:32:01 +02001194 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001195 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001196 if (unlikely(roffs)) {
1197 if (roffs >= free->length) {
1198 roffs -= free->length;
1199 continue;
1200 }
1201 boffs = free->offset + roffs;
1202 bytes = min_t(size_t, len,
1203 (free->length - roffs));
1204 roffs = 0;
1205 } else {
1206 bytes = min_t(size_t, len, free->length);
1207 boffs = free->offset;
1208 }
1209 memcpy(oob, chip->oob_poi + boffs, bytes);
1210 oob += bytes;
1211 }
1212 return oob;
1213 }
1214 default:
1215 BUG();
1216 }
1217 return NULL;
1218}
1219
1220/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001221 * nand_do_read_ops - [INTERN] Read data with ECC
1222 * @mtd: MTD device structure
1223 * @from: offset to read from
1224 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001225 *
1226 * Internal function. Called with chip held.
1227 */
1228static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1229 struct mtd_oob_ops *ops)
1230{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001231 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juulcfa460a2007-10-31 13:53:06 +01001232 struct nand_chip *chip = mtd->priv;
1233 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001234 int ret = 0;
1235 uint32_t readlen = ops->len;
1236 uint32_t oobreadlen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001237 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001238 mtd->oobavail : mtd->oobsize;
1239
William Juulcfa460a2007-10-31 13:53:06 +01001240 uint8_t *bufpoi, *oob, *buf;
1241
1242 stats = mtd->ecc_stats;
1243
1244 chipnr = (int)(from >> chip->chip_shift);
1245 chip->select_chip(mtd, chipnr);
1246
1247 realpage = (int)(from >> chip->page_shift);
1248 page = realpage & chip->pagemask;
1249
1250 col = (int)(from & (mtd->writesize - 1));
1251
1252 buf = ops->datbuf;
1253 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001254 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001255
Christian Hitz90e3f392011-10-12 09:32:01 +02001256 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001257 WATCHDOG_RESET();
1258
William Juulcfa460a2007-10-31 13:53:06 +01001259 bytes = min(mtd->writesize - col, readlen);
1260 aligned = (bytes == mtd->writesize);
1261
Sergey Lapindfe64e22013-01-14 03:46:50 +00001262 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001263 if (realpage != chip->pagebuf || oob) {
1264 bufpoi = aligned ? buf : chip->buffers->databuf;
1265
Sergey Lapindfe64e22013-01-14 03:46:50 +00001266 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001267
1268 /* Now read the page into the buffer */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001269 if (unlikely(ops->mode == MTD_OPS_RAW))
1270 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1271 oob_required,
1272 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001273 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1274 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001275 ret = chip->ecc.read_subpage(mtd, chip,
1276 col, bytes, bufpoi);
William Juulcfa460a2007-10-31 13:53:06 +01001277 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001278 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001279 oob_required, page);
1280 if (ret < 0) {
1281 if (!aligned)
1282 /* Invalidate page cache */
1283 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001284 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001285 }
William Juulcfa460a2007-10-31 13:53:06 +01001286
1287 /* Transfer not aligned data */
1288 if (!aligned) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001289 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00001290 !(mtd->ecc_stats.failed - stats.failed) &&
1291 (ops->mode != MTD_OPS_RAW))
Scott Woodc45912d2008-10-24 16:20:43 -05001292 chip->pagebuf = realpage;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001293 else
1294 /* Invalidate page cache */
1295 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001296 memcpy(buf, chip->buffers->databuf + col, bytes);
1297 }
1298
1299 buf += bytes;
1300
1301 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001302 int toread = min(oobreadlen, max_oobsize);
1303
1304 if (toread) {
1305 oob = nand_transfer_oob(chip,
1306 oob, ops, toread);
1307 oobreadlen -= toread;
1308 }
William Juulcfa460a2007-10-31 13:53:06 +01001309 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001310 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001311 memcpy(buf, chip->buffers->databuf + col, bytes);
1312 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001313 }
1314
William Juulcfa460a2007-10-31 13:53:06 +01001315 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001316
William Juulcfa460a2007-10-31 13:53:06 +01001317 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001318 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001319
Sergey Lapindfe64e22013-01-14 03:46:50 +00001320 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001321 col = 0;
1322 /* Increment page address */
1323 realpage++;
1324
William Juulcfa460a2007-10-31 13:53:06 +01001325 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001326 /* Check, if we cross a chip boundary */
1327 if (!page) {
1328 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001329 chip->select_chip(mtd, -1);
1330 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001331 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001332 }
1333
William Juulcfa460a2007-10-31 13:53:06 +01001334 ops->retlen = ops->len - (size_t) readlen;
1335 if (oob)
1336 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001337
William Juulcfa460a2007-10-31 13:53:06 +01001338 if (ret)
1339 return ret;
1340
1341 if (mtd->ecc_stats.failed - stats.failed)
1342 return -EBADMSG;
1343
Christian Hitz90e3f392011-10-12 09:32:01 +02001344 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001345}
1346
1347/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001348 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001349 * @mtd: MTD device structure
1350 * @from: offset to read from
1351 * @len: number of bytes to read
1352 * @retlen: pointer to variable to store the number of read bytes
1353 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001354 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001355 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001356 */
William Juulcfa460a2007-10-31 13:53:06 +01001357static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1358 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001359{
William Juulcfa460a2007-10-31 13:53:06 +01001360 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001361 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001362 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001363
William Juulcfa460a2007-10-31 13:53:06 +01001364 nand_get_device(chip, mtd, FL_READING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001365 ops.len = len;
1366 ops.datbuf = buf;
1367 ops.oobbuf = NULL;
1368 ops.mode = MTD_OPS_PLACE_OOB;
1369 ret = nand_do_read_ops(mtd, from, &ops);
1370 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001371 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001372 return ret;
1373}
1374
William Juulcfa460a2007-10-31 13:53:06 +01001375/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001376 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1377 * @mtd: mtd info structure
1378 * @chip: nand chip info structure
1379 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001380 */
1381static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001382 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001383{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001384 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001385 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001386 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001387}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001388
1389/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001390 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001391 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001392 * @mtd: mtd info structure
1393 * @chip: nand chip info structure
1394 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001395 */
1396static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001397 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001398{
1399 uint8_t *buf = chip->oob_poi;
1400 int length = mtd->oobsize;
1401 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1402 int eccsize = chip->ecc.size;
1403 uint8_t *bufpoi = buf;
1404 int i, toread, sndrnd = 0, pos;
1405
1406 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1407 for (i = 0; i < chip->ecc.steps; i++) {
1408 if (sndrnd) {
1409 pos = eccsize + i * (eccsize + chunk);
1410 if (mtd->writesize > 512)
1411 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1412 else
1413 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1414 } else
1415 sndrnd = 1;
1416 toread = min_t(int, length, chunk);
1417 chip->read_buf(mtd, bufpoi, toread);
1418 bufpoi += toread;
1419 length -= toread;
1420 }
1421 if (length > 0)
1422 chip->read_buf(mtd, bufpoi, length);
1423
Sergey Lapindfe64e22013-01-14 03:46:50 +00001424 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001425}
1426
1427/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001428 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1429 * @mtd: mtd info structure
1430 * @chip: nand chip info structure
1431 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001432 */
1433static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1434 int page)
1435{
1436 int status = 0;
1437 const uint8_t *buf = chip->oob_poi;
1438 int length = mtd->oobsize;
1439
1440 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1441 chip->write_buf(mtd, buf, length);
1442 /* Send command to program the OOB data */
1443 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1444
1445 status = chip->waitfunc(mtd, chip);
1446
1447 return status & NAND_STATUS_FAIL ? -EIO : 0;
1448}
1449
1450/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001451 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1452 * with syndrome - only for large page flash
1453 * @mtd: mtd info structure
1454 * @chip: nand chip info structure
1455 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001456 */
1457static int nand_write_oob_syndrome(struct mtd_info *mtd,
1458 struct nand_chip *chip, int page)
1459{
1460 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1461 int eccsize = chip->ecc.size, length = mtd->oobsize;
1462 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1463 const uint8_t *bufpoi = chip->oob_poi;
1464
1465 /*
1466 * data-ecc-data-ecc ... ecc-oob
1467 * or
1468 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1469 */
1470 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1471 pos = steps * (eccsize + chunk);
1472 steps = 0;
1473 } else
1474 pos = eccsize;
1475
1476 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1477 for (i = 0; i < steps; i++) {
1478 if (sndcmd) {
1479 if (mtd->writesize <= 512) {
1480 uint32_t fill = 0xFFFFFFFF;
1481
1482 len = eccsize;
1483 while (len > 0) {
1484 int num = min_t(int, len, 4);
1485 chip->write_buf(mtd, (uint8_t *)&fill,
1486 num);
1487 len -= num;
1488 }
1489 } else {
1490 pos = eccsize + i * (eccsize + chunk);
1491 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1492 }
1493 } else
1494 sndcmd = 1;
1495 len = min_t(int, length, chunk);
1496 chip->write_buf(mtd, bufpoi, len);
1497 bufpoi += len;
1498 length -= len;
1499 }
1500 if (length > 0)
1501 chip->write_buf(mtd, bufpoi, length);
1502
1503 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1504 status = chip->waitfunc(mtd, chip);
1505
1506 return status & NAND_STATUS_FAIL ? -EIO : 0;
1507}
1508
1509/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001510 * nand_do_read_oob - [INTERN] NAND read out-of-band
1511 * @mtd: MTD device structure
1512 * @from: offset to read from
1513 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001514 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001515 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01001516 */
1517static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1518 struct mtd_oob_ops *ops)
1519{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001520 int page, realpage, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01001521 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001522 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001523 int readlen = ops->ooblen;
1524 int len;
1525 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001526 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001527
Christian Hitz90e3f392011-10-12 09:32:01 +02001528 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1529 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001530
Sergey Lapindfe64e22013-01-14 03:46:50 +00001531 stats = mtd->ecc_stats;
1532
1533 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01001534 len = chip->ecc.layout->oobavail;
1535 else
1536 len = mtd->oobsize;
1537
1538 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001539 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1540 "outside oob\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001541 return -EINVAL;
1542 }
1543
1544 /* Do not allow reads past end of device */
1545 if (unlikely(from >= mtd->size ||
1546 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1547 (from >> chip->page_shift)) * len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001548 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1549 "of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001550 return -EINVAL;
1551 }
1552
1553 chipnr = (int)(from >> chip->chip_shift);
1554 chip->select_chip(mtd, chipnr);
1555
1556 /* Shift to get page */
1557 realpage = (int)(from >> chip->page_shift);
1558 page = realpage & chip->pagemask;
1559
Christian Hitz90e3f392011-10-12 09:32:01 +02001560 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001561 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00001562 if (ops->mode == MTD_OPS_RAW)
1563 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1564 else
1565 ret = chip->ecc.read_oob(mtd, chip, page);
1566
1567 if (ret < 0)
1568 break;
William Juulcfa460a2007-10-31 13:53:06 +01001569
1570 len = min(len, readlen);
1571 buf = nand_transfer_oob(chip, buf, ops, len);
1572
William Juulcfa460a2007-10-31 13:53:06 +01001573 readlen -= len;
1574 if (!readlen)
1575 break;
1576
1577 /* Increment page address */
1578 realpage++;
1579
1580 page = realpage & chip->pagemask;
1581 /* Check, if we cross a chip boundary */
1582 if (!page) {
1583 chipnr++;
1584 chip->select_chip(mtd, -1);
1585 chip->select_chip(mtd, chipnr);
1586 }
William Juulcfa460a2007-10-31 13:53:06 +01001587 }
1588
Sergey Lapindfe64e22013-01-14 03:46:50 +00001589 ops->oobretlen = ops->ooblen - readlen;
1590
1591 if (ret < 0)
1592 return ret;
1593
1594 if (mtd->ecc_stats.failed - stats.failed)
1595 return -EBADMSG;
1596
1597 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001598}
1599
1600/**
1601 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00001602 * @mtd: MTD device structure
1603 * @from: offset to read from
1604 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01001605 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001606 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01001607 */
1608static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1609 struct mtd_oob_ops *ops)
1610{
1611 struct nand_chip *chip = mtd->priv;
1612 int ret = -ENOTSUPP;
1613
1614 ops->retlen = 0;
1615
1616 /* Do not allow reads past end of device */
1617 if (ops->datbuf && (from + ops->len) > mtd->size) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001618 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1619 "beyond end of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001620 return -EINVAL;
1621 }
1622
1623 nand_get_device(chip, mtd, FL_READING);
1624
Christian Hitz90e3f392011-10-12 09:32:01 +02001625 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001626 case MTD_OPS_PLACE_OOB:
1627 case MTD_OPS_AUTO_OOB:
1628 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001629 break;
1630
1631 default:
1632 goto out;
1633 }
1634
1635 if (!ops->datbuf)
1636 ret = nand_do_read_oob(mtd, from, ops);
1637 else
1638 ret = nand_do_read_ops(mtd, from, ops);
1639
Christian Hitz90e3f392011-10-12 09:32:01 +02001640out:
William Juulcfa460a2007-10-31 13:53:06 +01001641 nand_release_device(mtd);
1642 return ret;
1643}
1644
1645
1646/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001647 * nand_write_page_raw - [INTERN] raw page write function
1648 * @mtd: mtd info structure
1649 * @chip: nand chip info structure
1650 * @buf: data buffer
1651 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05001652 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001653 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01001654 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001655static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1656 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001657{
1658 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001659 if (oob_required)
1660 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1661
1662 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001663}
1664
1665/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001666 * 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 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05001671 *
1672 * We need a special oob layout and handling even when ECC isn't checked.
1673 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001674static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02001675 struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001676 const uint8_t *buf, int oob_required)
David Brownell7e866612009-11-07 16:27:01 -05001677{
1678 int eccsize = chip->ecc.size;
1679 int eccbytes = chip->ecc.bytes;
1680 uint8_t *oob = chip->oob_poi;
1681 int steps, size;
1682
1683 for (steps = chip->ecc.steps; steps > 0; steps--) {
1684 chip->write_buf(mtd, buf, eccsize);
1685 buf += eccsize;
1686
1687 if (chip->ecc.prepad) {
1688 chip->write_buf(mtd, oob, chip->ecc.prepad);
1689 oob += chip->ecc.prepad;
1690 }
1691
1692 chip->read_buf(mtd, oob, eccbytes);
1693 oob += eccbytes;
1694
1695 if (chip->ecc.postpad) {
1696 chip->write_buf(mtd, oob, chip->ecc.postpad);
1697 oob += chip->ecc.postpad;
1698 }
1699 }
1700
1701 size = mtd->oobsize - (oob - chip->oob_poi);
1702 if (size)
1703 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001704
1705 return 0;
David Brownell7e866612009-11-07 16:27:01 -05001706}
1707/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001708 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
1709 * @mtd: mtd info structure
1710 * @chip: nand chip info structure
1711 * @buf: data buffer
1712 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001713 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001714static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1715 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001716{
1717 int i, eccsize = chip->ecc.size;
1718 int eccbytes = chip->ecc.bytes;
1719 int eccsteps = chip->ecc.steps;
1720 uint8_t *ecc_calc = chip->buffers->ecccalc;
1721 const uint8_t *p = buf;
1722 uint32_t *eccpos = chip->ecc.layout->eccpos;
1723
Sergey Lapindfe64e22013-01-14 03:46:50 +00001724 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01001725 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1726 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1727
1728 for (i = 0; i < chip->ecc.total; i++)
1729 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1730
Sergey Lapindfe64e22013-01-14 03:46:50 +00001731 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
William Juulcfa460a2007-10-31 13:53:06 +01001732}
1733
1734/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001735 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
1736 * @mtd: mtd info structure
1737 * @chip: nand chip info structure
1738 * @buf: data buffer
1739 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001740 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001741static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1742 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001743{
1744 int i, eccsize = chip->ecc.size;
1745 int eccbytes = chip->ecc.bytes;
1746 int eccsteps = chip->ecc.steps;
1747 uint8_t *ecc_calc = chip->buffers->ecccalc;
1748 const uint8_t *p = buf;
1749 uint32_t *eccpos = chip->ecc.layout->eccpos;
1750
1751 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1752 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1753 chip->write_buf(mtd, p, eccsize);
1754 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1755 }
1756
1757 for (i = 0; i < chip->ecc.total; i++)
1758 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1759
1760 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001761
1762 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001763}
1764
1765/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001766 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
1767 * @mtd: mtd info structure
1768 * @chip: nand chip info structure
1769 * @buf: data buffer
1770 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001771 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001772 * The hw generator calculates the error syndrome automatically. Therefore we
1773 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001774 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001775static int nand_write_page_syndrome(struct mtd_info *mtd,
1776 struct nand_chip *chip,
1777 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001778{
1779 int i, eccsize = chip->ecc.size;
1780 int eccbytes = chip->ecc.bytes;
1781 int eccsteps = chip->ecc.steps;
1782 const uint8_t *p = buf;
1783 uint8_t *oob = chip->oob_poi;
1784
1785 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1786
1787 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1788 chip->write_buf(mtd, p, eccsize);
1789
1790 if (chip->ecc.prepad) {
1791 chip->write_buf(mtd, oob, chip->ecc.prepad);
1792 oob += chip->ecc.prepad;
1793 }
1794
1795 chip->ecc.calculate(mtd, p, oob);
1796 chip->write_buf(mtd, oob, eccbytes);
1797 oob += eccbytes;
1798
1799 if (chip->ecc.postpad) {
1800 chip->write_buf(mtd, oob, chip->ecc.postpad);
1801 oob += chip->ecc.postpad;
1802 }
1803 }
1804
1805 /* Calculate remaining oob bytes */
1806 i = mtd->oobsize - (oob - chip->oob_poi);
1807 if (i)
1808 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001809
1810 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001811}
1812
1813/**
1814 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00001815 * @mtd: MTD device structure
1816 * @chip: NAND chip descriptor
1817 * @buf: the data to write
1818 * @oob_required: must write chip->oob_poi to OOB
1819 * @page: page number to write
1820 * @cached: cached programming
1821 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01001822 */
1823static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001824 const uint8_t *buf, int oob_required, int page,
1825 int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01001826{
1827 int status;
1828
1829 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1830
1831 if (unlikely(raw))
Sergey Lapindfe64e22013-01-14 03:46:50 +00001832 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
William Juulcfa460a2007-10-31 13:53:06 +01001833 else
Sergey Lapindfe64e22013-01-14 03:46:50 +00001834 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1835
1836 if (status < 0)
1837 return status;
William Juulcfa460a2007-10-31 13:53:06 +01001838
1839 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00001840 * Cached progamming disabled for now. Not sure if it's worth the
1841 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01001842 */
1843 cached = 0;
1844
1845 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1846
1847 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1848 status = chip->waitfunc(mtd, chip);
1849 /*
1850 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00001851 * available.
William Juulcfa460a2007-10-31 13:53:06 +01001852 */
1853 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1854 status = chip->errstat(mtd, chip, FL_WRITING, status,
1855 page);
1856
1857 if (status & NAND_STATUS_FAIL)
1858 return -EIO;
1859 } else {
1860 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1861 status = chip->waitfunc(mtd, chip);
1862 }
1863
1864#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1865 /* Send command to read back the data */
1866 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1867
1868 if (chip->verify_buf(mtd, buf, mtd->writesize))
1869 return -EIO;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001870
1871 /* Make sure the next page prog is preceded by a status read */
1872 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01001873#endif
1874 return 0;
1875}
1876
1877/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001878 * nand_fill_oob - [INTERN] Transfer client buffer to oob
1879 * @mtd: MTD device structure
1880 * @oob: oob data buffer
1881 * @len: oob data write length
1882 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001883 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001884static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
1885 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01001886{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001887 struct nand_chip *chip = mtd->priv;
1888
1889 /*
1890 * Initialise to all 0xFF, to avoid the possibility of left over OOB
1891 * data from a previous OOB read.
1892 */
1893 memset(chip->oob_poi, 0xff, mtd->oobsize);
1894
Christian Hitz90e3f392011-10-12 09:32:01 +02001895 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001896
Sergey Lapindfe64e22013-01-14 03:46:50 +00001897 case MTD_OPS_PLACE_OOB:
1898 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001899 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1900 return oob + len;
1901
Sergey Lapindfe64e22013-01-14 03:46:50 +00001902 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001903 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1904 uint32_t boffs = 0, woffs = ops->ooboffs;
1905 size_t bytes = 0;
1906
Christian Hitz90e3f392011-10-12 09:32:01 +02001907 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001908 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001909 if (unlikely(woffs)) {
1910 if (woffs >= free->length) {
1911 woffs -= free->length;
1912 continue;
1913 }
1914 boffs = free->offset + woffs;
1915 bytes = min_t(size_t, len,
1916 (free->length - woffs));
1917 woffs = 0;
1918 } else {
1919 bytes = min_t(size_t, len, free->length);
1920 boffs = free->offset;
1921 }
1922 memcpy(chip->oob_poi + boffs, oob, bytes);
1923 oob += bytes;
1924 }
1925 return oob;
1926 }
1927 default:
1928 BUG();
1929 }
1930 return NULL;
1931}
1932
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001933#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01001934
1935/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001936 * nand_do_write_ops - [INTERN] NAND write with ECC
1937 * @mtd: MTD device structure
1938 * @to: offset to write to
1939 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001940 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001941 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01001942 */
1943static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1944 struct mtd_oob_ops *ops)
1945{
1946 int chipnr, realpage, page, blockmask, column;
1947 struct nand_chip *chip = mtd->priv;
1948 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001949
1950 uint32_t oobwritelen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001951 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001952 mtd->oobavail : mtd->oobsize;
1953
William Juulcfa460a2007-10-31 13:53:06 +01001954 uint8_t *oob = ops->oobbuf;
1955 uint8_t *buf = ops->datbuf;
1956 int ret, subpage;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001957 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001958
1959 ops->retlen = 0;
1960 if (!writelen)
1961 return 0;
1962
William Juulcfa460a2007-10-31 13:53:06 +01001963 column = to & (mtd->writesize - 1);
1964 subpage = column || (writelen & (mtd->writesize - 1));
1965
1966 if (subpage && oob)
1967 return -EINVAL;
1968
1969 chipnr = (int)(to >> chip->chip_shift);
1970 chip->select_chip(mtd, chipnr);
1971
1972 /* Check, if it is write protected */
1973 if (nand_check_wp(mtd)) {
1974 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1975 return -EIO;
1976 }
1977
1978 realpage = (int)(to >> chip->page_shift);
1979 page = realpage & chip->pagemask;
1980 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1981
1982 /* Invalidate the page cache, when we write to the cached page */
1983 if (to <= (chip->pagebuf << chip->page_shift) &&
1984 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1985 chip->pagebuf = -1;
1986
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001987 /* Don't allow multipage oob writes with offset */
1988 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
1989 return -EINVAL;
1990
Christian Hitz90e3f392011-10-12 09:32:01 +02001991 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001992 WATCHDOG_RESET();
1993
William Juulcfa460a2007-10-31 13:53:06 +01001994 int bytes = mtd->writesize;
1995 int cached = writelen > bytes && page != blockmask;
1996 uint8_t *wbuf = buf;
1997
Sergey Lapindfe64e22013-01-14 03:46:50 +00001998 /* Partial page write? */
htbegin070fd8e2013-03-01 22:59:27 +00001999 if (unlikely(column || writelen < mtd->writesize)) {
William Juulcfa460a2007-10-31 13:53:06 +01002000 cached = 0;
2001 bytes = min_t(int, bytes - column, (int) writelen);
2002 chip->pagebuf = -1;
2003 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2004 memcpy(&chip->buffers->databuf[column], buf, bytes);
2005 wbuf = chip->buffers->databuf;
2006 }
2007
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002008 if (unlikely(oob)) {
2009 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002010 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002011 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002012 } else {
2013 /* We still need to erase leftover OOB data */
2014 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002015 }
William Juulcfa460a2007-10-31 13:53:06 +01002016
Sergey Lapindfe64e22013-01-14 03:46:50 +00002017 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2018 cached, (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002019 if (ret)
2020 break;
2021
2022 writelen -= bytes;
2023 if (!writelen)
2024 break;
2025
2026 column = 0;
2027 buf += bytes;
2028 realpage++;
2029
2030 page = realpage & chip->pagemask;
2031 /* Check, if we cross a chip boundary */
2032 if (!page) {
2033 chipnr++;
2034 chip->select_chip(mtd, -1);
2035 chip->select_chip(mtd, chipnr);
2036 }
2037 }
2038
2039 ops->retlen = ops->len - writelen;
2040 if (unlikely(oob))
2041 ops->oobretlen = ops->ooblen;
2042 return ret;
2043}
2044
2045/**
2046 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002047 * @mtd: MTD device structure
2048 * @to: offset to write to
2049 * @len: number of bytes to write
2050 * @retlen: pointer to variable to store the number of written bytes
2051 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002052 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002053 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002054 */
2055static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2056 size_t *retlen, const uint8_t *buf)
2057{
2058 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002059 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002060 int ret;
2061
William Juulcfa460a2007-10-31 13:53:06 +01002062 nand_get_device(chip, mtd, FL_WRITING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002063 ops.len = len;
2064 ops.datbuf = (uint8_t *)buf;
2065 ops.oobbuf = NULL;
2066 ops.mode = MTD_OPS_PLACE_OOB;
2067 ret = nand_do_write_ops(mtd, to, &ops);
2068 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002069 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002070 return ret;
2071}
2072
2073/**
2074 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002075 * @mtd: MTD device structure
2076 * @to: offset to write to
2077 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002078 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002079 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002080 */
William Juulcfa460a2007-10-31 13:53:06 +01002081static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2082 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002083{
William Juulcfa460a2007-10-31 13:53:06 +01002084 int chipnr, page, status, len;
2085 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002086
Christian Hitz90e3f392011-10-12 09:32:01 +02002087 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2088 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002089
Sergey Lapindfe64e22013-01-14 03:46:50 +00002090 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002091 len = chip->ecc.layout->oobavail;
2092 else
2093 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002094
2095 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002096 if ((ops->ooboffs + ops->ooblen) > len) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002097 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2098 "past end of page\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002099 return -EINVAL;
2100 }
2101
William Juulcfa460a2007-10-31 13:53:06 +01002102 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002103 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2104 "write outside oob\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002105 return -EINVAL;
2106 }
2107
Christian Hitz90e3f392011-10-12 09:32:01 +02002108 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002109 if (unlikely(to >= mtd->size ||
2110 ops->ooboffs + ops->ooblen >
2111 ((mtd->size >> chip->page_shift) -
2112 (to >> chip->page_shift)) * len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002113 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2114 "end of device\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002115 return -EINVAL;
2116 }
2117
William Juulcfa460a2007-10-31 13:53:06 +01002118 chipnr = (int)(to >> chip->chip_shift);
2119 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002120
William Juulcfa460a2007-10-31 13:53:06 +01002121 /* Shift to get page */
2122 page = (int)(to >> chip->page_shift);
2123
2124 /*
2125 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2126 * of my DiskOnChip 2000 test units) will clear the whole data page too
2127 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2128 * it in the doc2000 driver in August 1999. dwmw2.
2129 */
2130 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002131
2132 /* Check, if it is write protected */
2133 if (nand_check_wp(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002134 return -EROFS;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002135
Wolfgang Denk932394a2005-08-17 12:55:25 +02002136 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002137 if (page == chip->pagebuf)
2138 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002139
Sergey Lapindfe64e22013-01-14 03:46:50 +00002140 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2141
2142 if (ops->mode == MTD_OPS_RAW)
2143 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2144 else
2145 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002146
William Juulcfa460a2007-10-31 13:53:06 +01002147 if (status)
2148 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002149
William Juulcfa460a2007-10-31 13:53:06 +01002150 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002151
William Juulcfa460a2007-10-31 13:53:06 +01002152 return 0;
2153}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002154
William Juulcfa460a2007-10-31 13:53:06 +01002155/**
2156 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002157 * @mtd: MTD device structure
2158 * @to: offset to write to
2159 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002160 */
2161static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2162 struct mtd_oob_ops *ops)
2163{
2164 struct nand_chip *chip = mtd->priv;
2165 int ret = -ENOTSUPP;
2166
2167 ops->retlen = 0;
2168
2169 /* Do not allow writes past end of device */
2170 if (ops->datbuf && (to + ops->len) > mtd->size) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002171 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2172 "end of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002173 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002174 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002175
William Juulcfa460a2007-10-31 13:53:06 +01002176 nand_get_device(chip, mtd, FL_WRITING);
2177
Christian Hitz90e3f392011-10-12 09:32:01 +02002178 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002179 case MTD_OPS_PLACE_OOB:
2180 case MTD_OPS_AUTO_OOB:
2181 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002182 break;
2183
2184 default:
2185 goto out;
2186 }
2187
2188 if (!ops->datbuf)
2189 ret = nand_do_write_oob(mtd, to, ops);
2190 else
2191 ret = nand_do_write_ops(mtd, to, ops);
2192
Christian Hitz90e3f392011-10-12 09:32:01 +02002193out:
William Juulcfa460a2007-10-31 13:53:06 +01002194 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002195 return ret;
2196}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002197
2198/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002199 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2200 * @mtd: MTD device structure
2201 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002202 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002203 * Standard erase command for NAND chips.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002204 */
William Juulcfa460a2007-10-31 13:53:06 +01002205static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002206{
William Juulcfa460a2007-10-31 13:53:06 +01002207 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002208 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002209 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2210 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002211}
2212
2213/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002214 * multi_erase_cmd - [GENERIC] AND specific block erase command function
2215 * @mtd: MTD device structure
2216 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002217 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002218 * AND multi block erase command function. Erase 4 consecutive blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002219 */
William Juulcfa460a2007-10-31 13:53:06 +01002220static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002221{
William Juulcfa460a2007-10-31 13:53:06 +01002222 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002223 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002224 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2225 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2226 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2227 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2228 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002229}
2230
2231/**
2232 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002233 * @mtd: MTD device structure
2234 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002235 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002236 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002237 */
William Juulcfa460a2007-10-31 13:53:06 +01002238static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002239{
William Juulcfa460a2007-10-31 13:53:06 +01002240 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002241}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002242
William Juulcfa460a2007-10-31 13:53:06 +01002243#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk932394a2005-08-17 12:55:25 +02002244/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002245 * nand_erase_nand - [INTERN] erase block(s)
2246 * @mtd: MTD device structure
2247 * @instr: erase instruction
2248 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002249 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002250 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002251 */
William Juulcfa460a2007-10-31 13:53:06 +01002252int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2253 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002254{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002255 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002256 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002257 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juulcfa460a2007-10-31 13:53:06 +01002258 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002259 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002260
Christian Hitz90e3f392011-10-12 09:32:01 +02002261 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2262 __func__, (unsigned long long)instr->addr,
2263 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002264
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002265 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002266 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002267
Wolfgang Denk932394a2005-08-17 12:55:25 +02002268 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002269 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002270
2271 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002272 page = (int)(instr->addr >> chip->page_shift);
2273 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002274
2275 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002276 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002277
Wolfgang Denk932394a2005-08-17 12:55:25 +02002278 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002279 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002280
Wolfgang Denk932394a2005-08-17 12:55:25 +02002281 /* Check, if it is write protected */
2282 if (nand_check_wp(mtd)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002283 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2284 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002285 instr->state = MTD_ERASE_FAILED;
2286 goto erase_exit;
2287 }
2288
William Juulcfa460a2007-10-31 13:53:06 +01002289 /*
2290 * If BBT requires refresh, set the BBT page mask to see if the BBT
2291 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2292 * can not be matched. This is also done when the bbt is actually
Sergey Lapindfe64e22013-01-14 03:46:50 +00002293 * erased to avoid recursive updates.
William Juulcfa460a2007-10-31 13:53:06 +01002294 */
2295 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2296 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2297
Wolfgang Denk932394a2005-08-17 12:55:25 +02002298 /* Loop through the pages */
2299 len = instr->len;
2300
2301 instr->state = MTD_ERASING;
2302
2303 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002304 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00002305 /* Check if we have a bad block, we do not erase bad blocks! */
Marek Vasut6d414192011-09-12 06:04:06 +02002306 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002307 chip->page_shift, 0, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002308 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2309 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002310 instr->state = MTD_ERASE_FAILED;
2311 goto erase_exit;
2312 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002313
William Juulcfa460a2007-10-31 13:53:06 +01002314 /*
2315 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002316 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002317 */
2318 if (page <= chip->pagebuf && chip->pagebuf <
2319 (page + pages_per_block))
2320 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002321
William Juulcfa460a2007-10-31 13:53:06 +01002322 chip->erase_cmd(mtd, page & chip->pagemask);
2323
2324 status = chip->waitfunc(mtd, chip);
2325
2326 /*
2327 * See if operation failed and additional status checks are
2328 * available
2329 */
2330 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2331 status = chip->errstat(mtd, chip, FL_ERASING,
2332 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002333
2334 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002335 if (status & NAND_STATUS_FAIL) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002336 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2337 "page 0x%08x\n", __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002338 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002339 instr->fail_addr =
2340 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002341 goto erase_exit;
2342 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002343
William Juulcfa460a2007-10-31 13:53:06 +01002344 /*
2345 * If BBT requires refresh, set the BBT rewrite flag to the
Sergey Lapindfe64e22013-01-14 03:46:50 +00002346 * page being erased.
William Juulcfa460a2007-10-31 13:53:06 +01002347 */
2348 if (bbt_masked_page != 0xffffffff &&
2349 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002350 rewrite_bbt[chipnr] =
2351 ((loff_t)page << chip->page_shift);
William Juulcfa460a2007-10-31 13:53:06 +01002352
Wolfgang Denk932394a2005-08-17 12:55:25 +02002353 /* Increment page address and decrement length */
William Juulcfa460a2007-10-31 13:53:06 +01002354 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002355 page += pages_per_block;
2356
2357 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002358 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002359 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002360 chip->select_chip(mtd, -1);
2361 chip->select_chip(mtd, chipnr);
2362
2363 /*
2364 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Sergey Lapindfe64e22013-01-14 03:46:50 +00002365 * page mask to see if this BBT should be rewritten.
William Juulcfa460a2007-10-31 13:53:06 +01002366 */
2367 if (bbt_masked_page != 0xffffffff &&
2368 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2369 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2370 BBT_PAGE_MASK;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002371 }
2372 }
2373 instr->state = MTD_ERASE_DONE;
2374
Christian Hitz90e3f392011-10-12 09:32:01 +02002375erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002376
2377 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002378
2379 /* Deselect and wake up anyone waiting on the device */
2380 nand_release_device(mtd);
2381
Scott Woodc45912d2008-10-24 16:20:43 -05002382 /* Do call back function */
2383 if (!ret)
2384 mtd_erase_callback(instr);
2385
William Juulcfa460a2007-10-31 13:53:06 +01002386 /*
2387 * If BBT requires refresh and erase was successful, rewrite any
Sergey Lapindfe64e22013-01-14 03:46:50 +00002388 * selected bad block tables.
William Juulcfa460a2007-10-31 13:53:06 +01002389 */
2390 if (bbt_masked_page == 0xffffffff || ret)
2391 return ret;
2392
2393 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2394 if (!rewrite_bbt[chipnr])
2395 continue;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002396 /* Update the BBT for chip */
Christian Hitz90e3f392011-10-12 09:32:01 +02002397 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2398 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2399 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
William Juulcfa460a2007-10-31 13:53:06 +01002400 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2401 }
2402
Wolfgang Denk932394a2005-08-17 12:55:25 +02002403 /* Return more or less happy */
2404 return ret;
2405}
2406
2407/**
2408 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002409 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002410 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002411 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002412 */
William Juulcfa460a2007-10-31 13:53:06 +01002413static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002414{
William Juulcfa460a2007-10-31 13:53:06 +01002415 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002416
Christian Hitz90e3f392011-10-12 09:32:01 +02002417 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002418
2419 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002420 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002421 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002422 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002423}
2424
Wolfgang Denk932394a2005-08-17 12:55:25 +02002425/**
William Juulcfa460a2007-10-31 13:53:06 +01002426 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002427 * @mtd: MTD device structure
2428 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002429 */
William Juulcfa460a2007-10-31 13:53:06 +01002430static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002431{
William Juulcfa460a2007-10-31 13:53:06 +01002432 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002433}
2434
2435/**
William Juulcfa460a2007-10-31 13:53:06 +01002436 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002437 * @mtd: MTD device structure
2438 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002439 */
William Juulcfa460a2007-10-31 13:53:06 +01002440static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002441{
William Juulcfa460a2007-10-31 13:53:06 +01002442 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002443 int ret;
2444
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002445 ret = nand_block_isbad(mtd, ofs);
2446 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002447 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002448 if (ret > 0)
2449 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002450 return ret;
2451 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002452
William Juulcfa460a2007-10-31 13:53:06 +01002453 return chip->block_markbad(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002454}
2455
Sergey Lapindfe64e22013-01-14 03:46:50 +00002456 /**
2457 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2458 * @mtd: MTD device structure
2459 * @chip: nand chip info structure
2460 * @addr: feature address.
2461 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01002462 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002463static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2464 int addr, uint8_t *subfeature_param)
2465{
2466 int status;
2467
2468 if (!chip->onfi_version)
2469 return -EINVAL;
2470
2471 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2472 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2473 status = chip->waitfunc(mtd, chip);
2474 if (status & NAND_STATUS_FAIL)
2475 return -EIO;
2476 return 0;
2477}
2478
2479/**
2480 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2481 * @mtd: MTD device structure
2482 * @chip: nand chip info structure
2483 * @addr: feature address.
2484 * @subfeature_param: the subfeature parameters, a four bytes array.
2485 */
2486static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2487 int addr, uint8_t *subfeature_param)
2488{
2489 if (!chip->onfi_version)
2490 return -EINVAL;
2491
2492 /* clear the sub feature parameters */
2493 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2494
2495 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2496 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2497 return 0;
2498}
2499
2500/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01002501static void nand_set_defaults(struct nand_chip *chip, int busw)
2502{
2503 /* check for proper chip_delay setup, set 20us if not */
2504 if (!chip->chip_delay)
2505 chip->chip_delay = 20;
2506
2507 /* check, if a user supplied command function given */
2508 if (chip->cmdfunc == NULL)
2509 chip->cmdfunc = nand_command;
2510
2511 /* check, if a user supplied wait function given */
2512 if (chip->waitfunc == NULL)
2513 chip->waitfunc = nand_wait;
2514
2515 if (!chip->select_chip)
2516 chip->select_chip = nand_select_chip;
2517 if (!chip->read_byte)
2518 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2519 if (!chip->read_word)
2520 chip->read_word = nand_read_word;
2521 if (!chip->block_bad)
2522 chip->block_bad = nand_block_bad;
2523 if (!chip->block_markbad)
2524 chip->block_markbad = nand_default_block_markbad;
2525 if (!chip->write_buf)
2526 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2527 if (!chip->read_buf)
2528 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2529 if (!chip->verify_buf)
2530 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2531 if (!chip->scan_bbt)
2532 chip->scan_bbt = nand_default_bbt;
Scott Wood5b8e6bb2010-08-25 17:42:49 -05002533 if (!chip->controller)
William Juulcfa460a2007-10-31 13:53:06 +01002534 chip->controller = &chip->hwcontrol;
William Juulcfa460a2007-10-31 13:53:06 +01002535}
2536
Florian Fainelli0272c712011-02-25 00:01:34 +00002537#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Sergey Lapindfe64e22013-01-14 03:46:50 +00002538/* Sanitize ONFI strings so we can safely print them */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002539static void sanitize_string(char *s, size_t len)
2540{
2541 ssize_t i;
2542
Sergey Lapindfe64e22013-01-14 03:46:50 +00002543 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002544 s[len - 1] = 0;
2545
Sergey Lapindfe64e22013-01-14 03:46:50 +00002546 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002547 for (i = 0; i < len - 1; i++) {
2548 if (s[i] < ' ' || s[i] > 127)
2549 s[i] = '?';
2550 }
2551
Sergey Lapindfe64e22013-01-14 03:46:50 +00002552 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002553 strim(s);
2554}
2555
Florian Fainelli0272c712011-02-25 00:01:34 +00002556static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01002557{
Florian Fainelli0272c712011-02-25 00:01:34 +00002558 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00002559 while (len--) {
2560 crc ^= *p++ << 8;
2561 for (i = 0; i < 8; i++)
2562 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05002563 }
2564
Florian Fainelli0272c712011-02-25 00:01:34 +00002565 return crc;
2566}
William Juulcfa460a2007-10-31 13:53:06 +01002567
Florian Fainelli0272c712011-02-25 00:01:34 +00002568/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002569 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00002570 */
Christian Hitz90e3f392011-10-12 09:32:01 +02002571static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00002572 int *busw)
2573{
2574 struct nand_onfi_params *p = &chip->onfi_params;
2575 int i;
2576 int val;
2577
Sergey Lapindfe64e22013-01-14 03:46:50 +00002578 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00002579 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2580 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2581 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2582 return 0;
2583
Florian Fainelli0272c712011-02-25 00:01:34 +00002584 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2585 for (i = 0; i < 3; i++) {
2586 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2587 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02002588 le16_to_cpu(p->crc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002589 pr_info("ONFI param page %d valid\n", i);
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002590 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00002591 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02002592 }
William Juulcfa460a2007-10-31 13:53:06 +01002593
Florian Fainelli0272c712011-02-25 00:01:34 +00002594 if (i == 3)
2595 return 0;
2596
Sergey Lapindfe64e22013-01-14 03:46:50 +00002597 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00002598 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002599 if (val & (1 << 5))
2600 chip->onfi_version = 23;
2601 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00002602 chip->onfi_version = 22;
2603 else if (val & (1 << 3))
2604 chip->onfi_version = 21;
2605 else if (val & (1 << 2))
2606 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002607 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00002608 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002609 else
2610 chip->onfi_version = 0;
2611
2612 if (!chip->onfi_version) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002613 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002614 return 0;
2615 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002616
Christian Hitz5454ddb2011-10-12 09:32:05 +02002617 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2618 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01002619 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00002620 mtd->name = p->model;
Florian Fainelli0272c712011-02-25 00:01:34 +00002621 mtd->writesize = le32_to_cpu(p->byte_per_page);
2622 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2623 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTETd62e9ca2012-03-19 15:35:25 +01002624 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2625 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Florian Fainelli0272c712011-02-25 00:01:34 +00002626 *busw = 0;
2627 if (le16_to_cpu(p->features) & 1)
2628 *busw = NAND_BUSWIDTH_16;
William Juulcfa460a2007-10-31 13:53:06 +01002629
Sergey Lapindfe64e22013-01-14 03:46:50 +00002630 pr_info("ONFI flash detected\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00002631 return 1;
2632}
2633#else
2634static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2635 struct nand_chip *chip,
2636 int *busw)
2637{
2638 return 0;
2639}
2640#endif
2641
Florian Fainelli0272c712011-02-25 00:01:34 +00002642/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002643 * nand_id_has_period - Check if an ID string has a given wraparound period
2644 * @id_data: the ID string
2645 * @arrlen: the length of the @id_data array
2646 * @period: the period of repitition
2647 *
2648 * Check if an ID string is repeated within a given sequence of bytes at
2649 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2650 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2651 * if the repetition has a period of @period; otherwise, returns zero.
2652 */
2653static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2654{
2655 int i, j;
2656 for (i = 0; i < period; i++)
2657 for (j = i + period; j < arrlen; j += period)
2658 if (id_data[i] != id_data[j])
2659 return 0;
2660 return 1;
2661}
2662
2663/*
2664 * nand_id_len - Get the length of an ID string returned by CMD_READID
2665 * @id_data: the ID string
2666 * @arrlen: the length of the @id_data array
2667
2668 * Returns the length of the ID string, according to known wraparound/trailing
2669 * zero patterns. If no pattern exists, returns the length of the array.
2670 */
2671static int nand_id_len(u8 *id_data, int arrlen)
2672{
2673 int last_nonzero, period;
2674
2675 /* Find last non-zero byte */
2676 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2677 if (id_data[last_nonzero])
2678 break;
2679
2680 /* All zeros */
2681 if (last_nonzero < 0)
2682 return 0;
2683
2684 /* Calculate wraparound period */
2685 for (period = 1; period < arrlen; period++)
2686 if (nand_id_has_period(id_data, arrlen, period))
2687 break;
2688
2689 /* There's a repeated pattern */
2690 if (period < arrlen)
2691 return period;
2692
2693 /* There are trailing zeros */
2694 if (last_nonzero < arrlen - 1)
2695 return last_nonzero + 1;
2696
2697 /* No pattern detected */
2698 return arrlen;
2699}
2700
2701/*
2702 * Many new NAND share similar device ID codes, which represent the size of the
2703 * chip. The rest of the parameters must be decoded according to generic or
2704 * manufacturer-specific "extended ID" decoding patterns.
2705 */
2706static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2707 u8 id_data[8], int *busw)
2708{
2709 int extid, id_len;
2710 /* The 3rd id byte holds MLC / multichip data */
2711 chip->cellinfo = id_data[2];
2712 /* The 4th id byte is the important one */
2713 extid = id_data[3];
2714
2715 id_len = nand_id_len(id_data, 8);
2716
2717 /*
2718 * Field definitions are in the following datasheets:
2719 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2720 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
2721 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
2722 *
2723 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2724 * ID to decide what to do.
2725 */
2726 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
2727 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2728 id_data[5] != 0x00) {
2729 /* Calc pagesize */
2730 mtd->writesize = 2048 << (extid & 0x03);
2731 extid >>= 2;
2732 /* Calc oobsize */
2733 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2734 case 1:
2735 mtd->oobsize = 128;
2736 break;
2737 case 2:
2738 mtd->oobsize = 218;
2739 break;
2740 case 3:
2741 mtd->oobsize = 400;
2742 break;
2743 case 4:
2744 mtd->oobsize = 436;
2745 break;
2746 case 5:
2747 mtd->oobsize = 512;
2748 break;
2749 case 6:
2750 default: /* Other cases are "reserved" (unknown) */
2751 mtd->oobsize = 640;
2752 break;
2753 }
2754 extid >>= 2;
2755 /* Calc blocksize */
2756 mtd->erasesize = (128 * 1024) <<
2757 (((extid >> 1) & 0x04) | (extid & 0x03));
2758 *busw = 0;
2759 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2760 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2761 unsigned int tmp;
2762
2763 /* Calc pagesize */
2764 mtd->writesize = 2048 << (extid & 0x03);
2765 extid >>= 2;
2766 /* Calc oobsize */
2767 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2768 case 0:
2769 mtd->oobsize = 128;
2770 break;
2771 case 1:
2772 mtd->oobsize = 224;
2773 break;
2774 case 2:
2775 mtd->oobsize = 448;
2776 break;
2777 case 3:
2778 mtd->oobsize = 64;
2779 break;
2780 case 4:
2781 mtd->oobsize = 32;
2782 break;
2783 case 5:
2784 mtd->oobsize = 16;
2785 break;
2786 default:
2787 mtd->oobsize = 640;
2788 break;
2789 }
2790 extid >>= 2;
2791 /* Calc blocksize */
2792 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
2793 if (tmp < 0x03)
2794 mtd->erasesize = (128 * 1024) << tmp;
2795 else if (tmp == 0x03)
2796 mtd->erasesize = 768 * 1024;
2797 else
2798 mtd->erasesize = (64 * 1024) << tmp;
2799 *busw = 0;
2800 } else {
2801 /* Calc pagesize */
2802 mtd->writesize = 1024 << (extid & 0x03);
2803 extid >>= 2;
2804 /* Calc oobsize */
2805 mtd->oobsize = (8 << (extid & 0x01)) *
2806 (mtd->writesize >> 9);
2807 extid >>= 2;
2808 /* Calc blocksize. Blocksize is multiples of 64KiB */
2809 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2810 extid >>= 2;
2811 /* Get buswidth information */
2812 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2813 }
2814}
2815
2816 /*
2817 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
2818 * decodes a matching ID table entry and assigns the MTD size parameters for
2819 * the chip.
2820 */
2821static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
2822 const struct nand_flash_dev *type, u8 id_data[8],
2823 int *busw)
2824{
2825 int maf_id = id_data[0];
2826
2827 mtd->erasesize = type->erasesize;
2828 mtd->writesize = type->pagesize;
2829 mtd->oobsize = mtd->writesize / 32;
2830 *busw = type->options & NAND_BUSWIDTH_16;
2831
2832 /*
2833 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
2834 * some Spansion chips have erasesize that conflicts with size
2835 * listed in nand_ids table.
2836 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
2837 */
2838 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
2839 && id_data[6] == 0x00 && id_data[7] == 0x00
2840 && mtd->writesize == 512) {
2841 mtd->erasesize = 128 * 1024;
2842 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
2843 }
2844}
2845
2846 /*
2847 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
2848 * heuristic patterns using various detected parameters (e.g., manufacturer,
2849 * page size, cell-type information).
2850 */
2851static void nand_decode_bbm_options(struct mtd_info *mtd,
2852 struct nand_chip *chip, u8 id_data[8])
2853{
2854 int maf_id = id_data[0];
2855
2856 /* Set the bad block position */
2857 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
2858 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2859 else
2860 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2861
2862 /*
2863 * Bad block marker is stored in the last page of each block on Samsung
2864 * and Hynix MLC devices; stored in first two pages of each block on
2865 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
2866 * AMD/Spansion, and Macronix. All others scan only the first page.
2867 */
2868 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2869 (maf_id == NAND_MFR_SAMSUNG ||
2870 maf_id == NAND_MFR_HYNIX))
2871 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
2872 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2873 (maf_id == NAND_MFR_SAMSUNG ||
2874 maf_id == NAND_MFR_HYNIX ||
2875 maf_id == NAND_MFR_TOSHIBA ||
2876 maf_id == NAND_MFR_AMD ||
2877 maf_id == NAND_MFR_MACRONIX)) ||
2878 (mtd->writesize == 2048 &&
2879 maf_id == NAND_MFR_MICRON))
2880 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
2881}
2882
2883/*
2884 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00002885 */
2886static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2887 struct nand_chip *chip,
2888 int busw,
2889 int *maf_id, int *dev_id,
2890 const struct nand_flash_dev *type)
2891{
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00002892 const char *name;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002893 int i, maf_idx;
2894 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00002895
2896 /* Select the device */
2897 chip->select_chip(mtd, 0);
2898
2899 /*
2900 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002901 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00002902 */
2903 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2904
2905 /* Send the command for reading device ID */
2906 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2907
2908 /* Read manufacturer and device IDs */
2909 *maf_id = chip->read_byte(mtd);
2910 *dev_id = chip->read_byte(mtd);
2911
Sergey Lapindfe64e22013-01-14 03:46:50 +00002912 /*
2913 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00002914 * interface concerns can cause random data which looks like a
2915 * possibly credible NAND flash to appear. If the two results do
2916 * not match, ignore the device completely.
2917 */
2918
2919 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2920
Sergey Lapindfe64e22013-01-14 03:46:50 +00002921 /* Read entire ID string */
2922 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002923 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00002924
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002925 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002926 pr_info("%s: second ID read did not match "
2927 "%02x,%02x against %02x,%02x\n", __func__,
2928 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00002929 return ERR_PTR(-ENODEV);
2930 }
2931
2932 if (!type)
2933 type = nand_flash_ids;
2934
2935 for (; type->name != NULL; type++)
2936 if (*dev_id == type->id)
2937 break;
2938
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002939 chip->onfi_version = 0;
2940 if (!type->name || !type->pagesize) {
2941 /* Check is chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002942 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002943 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00002944 }
2945
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002946 if (!type->name)
2947 return ERR_PTR(-ENODEV);
2948
Florian Fainelli0272c712011-02-25 00:01:34 +00002949 if (!mtd->name)
2950 mtd->name = type->name;
2951
2952 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00002953
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002954 if (!type->pagesize && chip->init_size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002955 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002956 busw = chip->init_size(mtd, chip, id_data);
2957 } else if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002958 /* Decode parameters from extended ID */
2959 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002960 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002961 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002962 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002963 /* Get chip options, preserve non chip based options */
Marek Vasut9c790a72012-08-30 13:39:38 +00002964 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00002965
Sergey Lapindfe64e22013-01-14 03:46:50 +00002966 /*
2967 * Check if chip is not a Samsung device. Do not clear the
2968 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002969 */
2970 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2971 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2972ident_done:
2973
William Juulcfa460a2007-10-31 13:53:06 +01002974 /* Try to identify manufacturer */
2975 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2976 if (nand_manuf_ids[maf_idx].id == *maf_id)
2977 break;
2978 }
2979
2980 /*
2981 * Check, if buswidth is correct. Hardware drivers should set
Sergey Lapindfe64e22013-01-14 03:46:50 +00002982 * chip correct!
William Juulcfa460a2007-10-31 13:53:06 +01002983 */
2984 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002985 pr_info("NAND device: Manufacturer ID:"
2986 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2987 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2988 pr_warn("NAND bus width %d instead %d bit\n",
2989 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2990 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01002991 return ERR_PTR(-EINVAL);
2992 }
2993
Sergey Lapindfe64e22013-01-14 03:46:50 +00002994 nand_decode_bbm_options(mtd, chip, id_data);
2995
William Juulcfa460a2007-10-31 13:53:06 +01002996 /* Calculate the address shift from the page size */
2997 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002998 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01002999 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3000
3001 chip->bbt_erase_shift = chip->phys_erase_shift =
3002 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003003 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003004 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003005 else {
3006 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3007 chip->chip_shift += 32 - 1;
3008 }
3009
3010 chip->badblockbits = 8;
William Juulcfa460a2007-10-31 13:53:06 +01003011
William Juulcfa460a2007-10-31 13:53:06 +01003012 /* Check for AND chips with 4 page planes */
3013 if (chip->options & NAND_4PAGE_ARRAY)
3014 chip->erase_cmd = multi_erase_cmd;
3015 else
3016 chip->erase_cmd = single_erase_cmd;
3017
Sergey Lapindfe64e22013-01-14 03:46:50 +00003018 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003019 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3020 chip->cmdfunc = nand_command_lp;
3021
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00003022 name = type->name;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003023#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00003024 if (chip->onfi_version)
3025 name = chip->onfi_params.model;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003026#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003027 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3028 " page size: %d, OOB size: %d\n",
3029 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3030 name,
3031 mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003032
3033 return type;
3034}
3035
3036/**
3037 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003038 * @mtd: MTD device structure
3039 * @maxchips: number of chips to scan for
3040 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003041 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003042 * This is the first phase of the normal nand_scan() function. It reads the
3043 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003044 *
3045 * The mtd->owner field must be set to the module of the caller.
3046 */
Lei Wen245eb902011-01-06 09:48:18 +08003047int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3048 const struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003049{
Florian Fainelli0272c712011-02-25 00:01:34 +00003050 int i, busw, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01003051 struct nand_chip *chip = mtd->priv;
Mike Frysinger0bdecd82010-10-20 01:15:21 +00003052 const struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003053
3054 /* Get buswidth to select the correct functions */
3055 busw = chip->options & NAND_BUSWIDTH_16;
3056 /* Set the default functions */
3057 nand_set_defaults(chip, busw);
3058
3059 /* Read the flash type */
Christian Hitz90e3f392011-10-12 09:32:01 +02003060 type = nand_get_flash_type(mtd, chip, busw,
3061 &nand_maf_id, &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003062
3063 if (IS_ERR(type)) {
Peter Tyser10dc6a92009-02-04 13:39:40 -06003064#ifndef CONFIG_SYS_NAND_QUIET_TEST
Sergey Lapindfe64e22013-01-14 03:46:50 +00003065 pr_warn("No NAND device found\n");
Peter Tyser10dc6a92009-02-04 13:39:40 -06003066#endif
William Juulcfa460a2007-10-31 13:53:06 +01003067 chip->select_chip(mtd, -1);
3068 return PTR_ERR(type);
3069 }
3070
3071 /* Check for a chip array */
3072 for (i = 1; i < maxchips; i++) {
3073 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003074 /* See comment in nand_get_flash_type for reset */
3075 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003076 /* Send the command for reading device ID */
3077 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3078 /* Read manufacturer and device IDs */
3079 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainelli0272c712011-02-25 00:01:34 +00003080 nand_dev_id != chip->read_byte(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01003081 break;
3082 }
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003083#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003084 if (i > 1)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003085 pr_info("%d NAND chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003086#endif
William Juulcfa460a2007-10-31 13:53:06 +01003087
3088 /* Store the number of chips and calc total size for mtd */
3089 chip->numchips = i;
3090 mtd->size = i * chip->chipsize;
3091
3092 return 0;
3093}
3094
3095
3096/**
3097 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003098 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003099 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003100 * This is the second phase of the normal nand_scan() function. It fills out
3101 * all the uninitialized function pointers with the defaults and scans for a
3102 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003103 */
3104int nand_scan_tail(struct mtd_info *mtd)
3105{
3106 int i;
3107 struct nand_chip *chip = mtd->priv;
3108
Sergey Lapindfe64e22013-01-14 03:46:50 +00003109 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3110 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3111 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3112
William Juulcfa460a2007-10-31 13:53:06 +01003113 if (!(chip->options & NAND_OWN_BUFFERS))
Simon Glassb5725952012-07-29 20:53:25 +00003114 chip->buffers = memalign(ARCH_DMA_MINALIGN,
3115 sizeof(*chip->buffers));
William Juulcfa460a2007-10-31 13:53:06 +01003116 if (!chip->buffers)
3117 return -ENOMEM;
3118
3119 /* Set the internal oob buffer location, just after the page data */
3120 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3121
3122 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003123 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01003124 */
Christian Hitz4c6de852011-10-12 09:31:59 +02003125 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01003126 switch (mtd->oobsize) {
3127 case 8:
3128 chip->ecc.layout = &nand_oob_8;
3129 break;
3130 case 16:
3131 chip->ecc.layout = &nand_oob_16;
3132 break;
3133 case 64:
3134 chip->ecc.layout = &nand_oob_64;
3135 break;
3136 case 128:
3137 chip->ecc.layout = &nand_oob_128;
3138 break;
3139 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003140 pr_warn("No oob scheme defined for oobsize %d\n",
3141 mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003142 }
3143 }
3144
3145 if (!chip->write_page)
3146 chip->write_page = nand_write_page;
3147
Sergey Lapindfe64e22013-01-14 03:46:50 +00003148 /* set for ONFI nand */
3149 if (!chip->onfi_set_features)
3150 chip->onfi_set_features = nand_onfi_set_features;
3151 if (!chip->onfi_get_features)
3152 chip->onfi_get_features = nand_onfi_get_features;
3153
William Juulcfa460a2007-10-31 13:53:06 +01003154 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003155 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01003156 * selected and we have 256 byte pagesize fallback to software ECC
3157 */
William Juulcfa460a2007-10-31 13:53:06 +01003158
3159 switch (chip->ecc.mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003160 case NAND_ECC_HW_OOB_FIRST:
3161 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3162 if (!chip->ecc.calculate || !chip->ecc.correct ||
3163 !chip->ecc.hwctl) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003164 pr_warn("No ECC functions supplied; "
3165 "hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003166 BUG();
3167 }
3168 if (!chip->ecc.read_page)
3169 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3170
William Juulcfa460a2007-10-31 13:53:06 +01003171 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003172 /* Use standard hwecc read page function? */
William Juulcfa460a2007-10-31 13:53:06 +01003173 if (!chip->ecc.read_page)
3174 chip->ecc.read_page = nand_read_page_hwecc;
3175 if (!chip->ecc.write_page)
3176 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell7e866612009-11-07 16:27:01 -05003177 if (!chip->ecc.read_page_raw)
3178 chip->ecc.read_page_raw = nand_read_page_raw;
3179 if (!chip->ecc.write_page_raw)
3180 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003181 if (!chip->ecc.read_oob)
3182 chip->ecc.read_oob = nand_read_oob_std;
3183 if (!chip->ecc.write_oob)
3184 chip->ecc.write_oob = nand_write_oob_std;
3185
3186 case NAND_ECC_HW_SYNDROME:
Scott Wood41ef8c72008-03-18 15:29:14 -05003187 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3188 !chip->ecc.hwctl) &&
3189 (!chip->ecc.read_page ||
3190 chip->ecc.read_page == nand_read_page_hwecc ||
3191 !chip->ecc.write_page ||
3192 chip->ecc.write_page == nand_write_page_hwecc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003193 pr_warn("No ECC functions supplied; "
3194 "hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01003195 BUG();
3196 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00003197 /* Use standard syndrome read/write page function? */
William Juulcfa460a2007-10-31 13:53:06 +01003198 if (!chip->ecc.read_page)
3199 chip->ecc.read_page = nand_read_page_syndrome;
3200 if (!chip->ecc.write_page)
3201 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell7e866612009-11-07 16:27:01 -05003202 if (!chip->ecc.read_page_raw)
3203 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3204 if (!chip->ecc.write_page_raw)
3205 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01003206 if (!chip->ecc.read_oob)
3207 chip->ecc.read_oob = nand_read_oob_syndrome;
3208 if (!chip->ecc.write_oob)
3209 chip->ecc.write_oob = nand_write_oob_syndrome;
3210
Sergey Lapindfe64e22013-01-14 03:46:50 +00003211 if (mtd->writesize >= chip->ecc.size) {
3212 if (!chip->ecc.strength) {
3213 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3214 BUG();
3215 }
William Juulcfa460a2007-10-31 13:53:06 +01003216 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003217 }
3218 pr_warn("%d byte HW ECC not possible on "
3219 "%d byte page size, fallback to SW ECC\n",
3220 chip->ecc.size, mtd->writesize);
William Juulcfa460a2007-10-31 13:53:06 +01003221 chip->ecc.mode = NAND_ECC_SOFT;
3222
3223 case NAND_ECC_SOFT:
3224 chip->ecc.calculate = nand_calculate_ecc;
3225 chip->ecc.correct = nand_correct_data;
3226 chip->ecc.read_page = nand_read_page_swecc;
Scott Woodc45912d2008-10-24 16:20:43 -05003227 chip->ecc.read_subpage = nand_read_subpage;
William Juulcfa460a2007-10-31 13:53:06 +01003228 chip->ecc.write_page = nand_write_page_swecc;
David Brownell7e866612009-11-07 16:27:01 -05003229 chip->ecc.read_page_raw = nand_read_page_raw;
3230 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003231 chip->ecc.read_oob = nand_read_oob_std;
3232 chip->ecc.write_oob = nand_write_oob_std;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003233 if (!chip->ecc.size)
3234 chip->ecc.size = 256;
William Juulcfa460a2007-10-31 13:53:06 +01003235 chip->ecc.bytes = 3;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003236 chip->ecc.strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01003237 break;
3238
Christian Hitz4c6de852011-10-12 09:31:59 +02003239 case NAND_ECC_SOFT_BCH:
3240 if (!mtd_nand_has_bch()) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003241 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Christian Hitz4c6de852011-10-12 09:31:59 +02003242 return -EINVAL;
3243 }
3244 chip->ecc.calculate = nand_bch_calculate_ecc;
3245 chip->ecc.correct = nand_bch_correct_data;
3246 chip->ecc.read_page = nand_read_page_swecc;
3247 chip->ecc.read_subpage = nand_read_subpage;
3248 chip->ecc.write_page = nand_write_page_swecc;
3249 chip->ecc.read_page_raw = nand_read_page_raw;
3250 chip->ecc.write_page_raw = nand_write_page_raw;
3251 chip->ecc.read_oob = nand_read_oob_std;
3252 chip->ecc.write_oob = nand_write_oob_std;
3253 /*
3254 * Board driver should supply ecc.size and ecc.bytes values to
3255 * select how many bits are correctable; see nand_bch_init()
Sergey Lapindfe64e22013-01-14 03:46:50 +00003256 * for details. Otherwise, default to 4 bits for large page
3257 * devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02003258 */
3259 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3260 chip->ecc.size = 512;
3261 chip->ecc.bytes = 7;
3262 }
3263 chip->ecc.priv = nand_bch_init(mtd,
3264 chip->ecc.size,
3265 chip->ecc.bytes,
3266 &chip->ecc.layout);
3267 if (!chip->ecc.priv)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003268 pr_warn("BCH ECC initialization failed!\n");
3269 chip->ecc.strength =
3270 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Christian Hitz4c6de852011-10-12 09:31:59 +02003271 break;
3272
William Juulcfa460a2007-10-31 13:53:06 +01003273 case NAND_ECC_NONE:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003274 pr_warn("NAND_ECC_NONE selected by board driver. "
3275 "This is not recommended !!\n");
William Juulcfa460a2007-10-31 13:53:06 +01003276 chip->ecc.read_page = nand_read_page_raw;
3277 chip->ecc.write_page = nand_write_page_raw;
3278 chip->ecc.read_oob = nand_read_oob_std;
David Brownell7e866612009-11-07 16:27:01 -05003279 chip->ecc.read_page_raw = nand_read_page_raw;
3280 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003281 chip->ecc.write_oob = nand_write_oob_std;
3282 chip->ecc.size = mtd->writesize;
3283 chip->ecc.bytes = 0;
3284 break;
3285
3286 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003287 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
William Juulcfa460a2007-10-31 13:53:06 +01003288 BUG();
3289 }
3290
Sergey Lapindfe64e22013-01-14 03:46:50 +00003291 /* For many systems, the standard OOB write also works for raw */
3292 if (!chip->ecc.read_oob_raw)
3293 chip->ecc.read_oob_raw = chip->ecc.read_oob;
3294 if (!chip->ecc.write_oob_raw)
3295 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3296
William Juulcfa460a2007-10-31 13:53:06 +01003297 /*
3298 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00003299 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01003300 */
3301 chip->ecc.layout->oobavail = 0;
Sandeep Paulraj5df3c2b2009-11-07 14:25:18 -05003302 for (i = 0; chip->ecc.layout->oobfree[i].length
3303 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juulcfa460a2007-10-31 13:53:06 +01003304 chip->ecc.layout->oobavail +=
3305 chip->ecc.layout->oobfree[i].length;
3306 mtd->oobavail = chip->ecc.layout->oobavail;
3307
3308 /*
3309 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00003310 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01003311 */
3312 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Christian Hitz90e3f392011-10-12 09:32:01 +02003313 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003314 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01003315 BUG();
3316 }
3317 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
3318
Sergey Lapindfe64e22013-01-14 03:46:50 +00003319 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
William Juulcfa460a2007-10-31 13:53:06 +01003320 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3321 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02003322 switch (chip->ecc.steps) {
William Juulcfa460a2007-10-31 13:53:06 +01003323 case 2:
3324 mtd->subpage_sft = 1;
3325 break;
3326 case 4:
3327 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05003328 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01003329 mtd->subpage_sft = 2;
3330 break;
3331 }
3332 }
3333 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3334
3335 /* Initialize state */
3336 chip->state = FL_READY;
3337
3338 /* De-select the device */
3339 chip->select_chip(mtd, -1);
3340
3341 /* Invalidate the pagebuffer reference */
3342 chip->pagebuf = -1;
3343
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00003344 /* Large page NAND with SOFT_ECC should support subpage reads */
3345 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3346 chip->options |= NAND_SUBPAGE_READ;
3347
William Juulcfa460a2007-10-31 13:53:06 +01003348 /* Fill in remaining MTD driver data */
3349 mtd->type = MTD_NANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003350 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3351 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003352 mtd->_erase = nand_erase;
3353 mtd->_point = NULL;
3354 mtd->_unpoint = NULL;
3355 mtd->_read = nand_read;
3356 mtd->_write = nand_write;
3357 mtd->_read_oob = nand_read_oob;
3358 mtd->_write_oob = nand_write_oob;
3359 mtd->_sync = nand_sync;
3360 mtd->_lock = NULL;
3361 mtd->_unlock = NULL;
3362 mtd->_block_isbad = nand_block_isbad;
3363 mtd->_block_markbad = nand_block_markbad;
William Juulcfa460a2007-10-31 13:53:06 +01003364
Sergey Lapindfe64e22013-01-14 03:46:50 +00003365 /* propagate ecc info to mtd_info */
William Juulcfa460a2007-10-31 13:53:06 +01003366 mtd->ecclayout = chip->ecc.layout;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003367 mtd->ecc_strength = chip->ecc.strength;
3368 /*
3369 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3370 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3371 * properly set.
3372 */
3373 if (!mtd->bitflip_threshold)
3374 mtd->bitflip_threshold = mtd->ecc_strength;
William Juulcfa460a2007-10-31 13:53:06 +01003375
3376 /* Check, if we should skip the bad block table scan */
3377 if (chip->options & NAND_SKIP_BBTSCAN)
Scott Woodfb494542012-02-20 14:50:39 -06003378 chip->options |= NAND_BBT_SCANNED;
William Juulcfa460a2007-10-31 13:53:06 +01003379
Scott Woodfb494542012-02-20 14:50:39 -06003380 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01003381}
3382
William Juulcfa460a2007-10-31 13:53:06 +01003383/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02003384 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003385 * @mtd: MTD device structure
3386 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02003387 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003388 * This fills out all the uninitialized function pointers with the defaults.
3389 * The flash ID is read and the mtd/chip structures are filled with the
3390 * appropriate values. The mtd->owner field must be set to the module of the
3391 * caller.
Wolfgang Denk932394a2005-08-17 12:55:25 +02003392 */
William Juulcfa460a2007-10-31 13:53:06 +01003393int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003394{
William Juulcfa460a2007-10-31 13:53:06 +01003395 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003396
Lei Wen245eb902011-01-06 09:48:18 +08003397 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01003398 if (!ret)
3399 ret = nand_scan_tail(mtd);
3400 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003401}
3402
3403/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02003404 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003405 * @mtd: MTD device structure
3406 */
William Juulcfa460a2007-10-31 13:53:06 +01003407void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003408{
William Juulcfa460a2007-10-31 13:53:06 +01003409 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003410
Christian Hitz4c6de852011-10-12 09:31:59 +02003411 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3412 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3413
Wolfgang Denk932394a2005-08-17 12:55:25 +02003414#ifdef CONFIG_MTD_PARTITIONS
3415 /* Deregister partitions */
William Juulcfa460a2007-10-31 13:53:06 +01003416 del_mtd_partitions(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003417#endif
William Juulcfa460a2007-10-31 13:53:06 +01003418
3419 /* Free bad block table memory */
3420 kfree(chip->bbt);
3421 if (!(chip->options & NAND_OWN_BUFFERS))
3422 kfree(chip->buffers);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003423
3424 /* Free bad block descriptor memory */
3425 if (chip->badblock_pattern && chip->badblock_pattern->options
3426 & NAND_BBT_DYNAMICSTRUCT)
3427 kfree(chip->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003428}