blob: 376976d5795bbdb95e5e680dbe7a8e8f818fa0ef [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 */
Brian Norris27ce9e42014-05-06 00:46:17 +0530578 if ((chip->options & NAND_BUSWIDTH_16) &&
579 !nand_opcode_8bits(command))
William Juulcfa460a2007-10-31 13:53:06 +0100580 column >>= 1;
581 chip->cmd_ctrl(mtd, column, ctrl);
582 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200583 }
William Juulcfa460a2007-10-31 13:53:06 +0100584 if (page_addr != -1) {
585 chip->cmd_ctrl(mtd, page_addr, ctrl);
586 ctrl &= ~NAND_CTRL_CHANGE;
587 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
588 /* One more address cycle for devices > 32MiB */
589 if (chip->chipsize > (32 << 20))
590 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
591 }
592 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200593
594 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000595 * Program and erase have their own busy handlers status and sequential
596 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100597 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200598 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200599
Wolfgang Denk932394a2005-08-17 12:55:25 +0200600 case NAND_CMD_PAGEPROG:
601 case NAND_CMD_ERASE1:
602 case NAND_CMD_ERASE2:
603 case NAND_CMD_SEQIN:
604 case NAND_CMD_STATUS:
605 return;
606
607 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100608 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200609 break;
William Juulcfa460a2007-10-31 13:53:06 +0100610 udelay(chip->chip_delay);
611 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
612 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
613 chip->cmd_ctrl(mtd,
614 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600615 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
616 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200617 return;
618
William Juulcfa460a2007-10-31 13:53:06 +0100619 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200620 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200621 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200622 * If we don't have access to the busy pin, we apply the given
623 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100624 */
625 if (!chip->dev_ready) {
626 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200627 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200628 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200629 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000630 /*
631 * Apply this short delay always to ensure that we do wait tWB in
632 * any case on any machine.
633 */
William Juulcfa460a2007-10-31 13:53:06 +0100634 ndelay(100);
635
636 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200637}
638
639/**
640 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000641 * @mtd: MTD device structure
642 * @command: the command to be sent
643 * @column: the column address for this command, -1 if none
644 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200645 *
William Juulcfa460a2007-10-31 13:53:06 +0100646 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000647 * devices. We don't have the separate regions as we have in the small page
648 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200649 */
William Juulcfa460a2007-10-31 13:53:06 +0100650static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
651 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200652{
William Juulcfa460a2007-10-31 13:53:06 +0100653 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600654 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200655
656 /* Emulate NAND_CMD_READOOB */
657 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100658 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200659 command = NAND_CMD_READ0;
660 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200661
William Juulcfa460a2007-10-31 13:53:06 +0100662 /* Command latch cycle */
663 chip->cmd_ctrl(mtd, command & 0xff,
664 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200665
666 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100667 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200668
669 /* Serially input address */
670 if (column != -1) {
671 /* Adjust columns for 16 bit buswidth */
Brian Norris27ce9e42014-05-06 00:46:17 +0530672 if ((chip->options & NAND_BUSWIDTH_16) &&
673 !nand_opcode_8bits(command))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200674 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100675 chip->cmd_ctrl(mtd, column, ctrl);
676 ctrl &= ~NAND_CTRL_CHANGE;
677 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200678 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200679 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100680 chip->cmd_ctrl(mtd, page_addr, ctrl);
681 chip->cmd_ctrl(mtd, page_addr >> 8,
682 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200683 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100684 if (chip->chipsize > (128 << 20))
685 chip->cmd_ctrl(mtd, page_addr >> 16,
686 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200687 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200688 }
William Juulcfa460a2007-10-31 13:53:06 +0100689 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200690
691 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000692 * Program and erase have their own busy handlers status, sequential
693 * in, and deplete1 need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100694 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200695 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200696
Wolfgang Denk932394a2005-08-17 12:55:25 +0200697 case NAND_CMD_CACHEDPROG:
698 case NAND_CMD_PAGEPROG:
699 case NAND_CMD_ERASE1:
700 case NAND_CMD_ERASE2:
701 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100702 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200703 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100704 case NAND_CMD_DEPLETE1:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200705 return;
706
William Juulcfa460a2007-10-31 13:53:06 +0100707 case NAND_CMD_STATUS_ERROR:
708 case NAND_CMD_STATUS_ERROR0:
709 case NAND_CMD_STATUS_ERROR1:
710 case NAND_CMD_STATUS_ERROR2:
711 case NAND_CMD_STATUS_ERROR3:
Sergey Lapindfe64e22013-01-14 03:46:50 +0000712 /* Read error status commands require only a short delay */
William Juulcfa460a2007-10-31 13:53:06 +0100713 udelay(chip->chip_delay);
714 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200715
716 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100717 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200718 break;
William Juulcfa460a2007-10-31 13:53:06 +0100719 udelay(chip->chip_delay);
720 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
721 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
722 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
723 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600724 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
725 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100726 return;
727
728 case NAND_CMD_RNDOUT:
729 /* No ready / busy check necessary */
730 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
731 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
732 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
733 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200734 return;
735
736 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100737 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
738 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
739 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
740 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200741
William Juulcfa460a2007-10-31 13:53:06 +0100742 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200743 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200744 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200745 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000746 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100747 */
748 if (!chip->dev_ready) {
749 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200750 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200751 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200752 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200753
Sergey Lapindfe64e22013-01-14 03:46:50 +0000754 /*
755 * Apply this short delay always to ensure that we do wait tWB in
756 * any case on any machine.
757 */
William Juulcfa460a2007-10-31 13:53:06 +0100758 ndelay(100);
759
760 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200761}
762
763/**
764 * nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000765 * @chip: the nand chip descriptor
766 * @mtd: MTD device structure
767 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200768 *
769 * Get the device and lock it for exclusive access
770 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200771static int
772nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100773{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200774 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100775 return 0;
776}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200777
778/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000779 * nand_wait - [DEFAULT] wait until the command is done
780 * @mtd: MTD device structure
781 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200782 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000783 * Wait for command done. This applies to erase and program only. Erase can
784 * take up to 400ms and program up to 20ms according to general NAND and
785 * SmartMedia specs.
William Juulcfa460a2007-10-31 13:53:06 +0100786 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200787static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200788{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100789 unsigned long timeo;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200790 int state = chip->state;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000791 u32 time_start;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100792
793 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200794 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100795 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200796 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100797
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200798 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
799 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100800 else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200801 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100802
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000803 time_start = get_timer(0);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100804
805 while (1) {
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000806 if (get_timer(time_start) > timeo) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100807 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100808 return 0x01;
809 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100810
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200811 if (chip->dev_ready) {
812 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100813 break;
814 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200815 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100816 break;
817 }
818 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100819#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000820 time_start = get_timer(0);
821 while (get_timer(time_start) < 10)
822 ;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100823#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100824
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200825 return (int)chip->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200826}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200827
828/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000829 * nand_read_page_raw - [INTERN] read raw page data without ecc
830 * @mtd: mtd info structure
831 * @chip: nand chip info structure
832 * @buf: buffer to store read data
833 * @oob_required: caller requires OOB data read to chip->oob_poi
834 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500835 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000836 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200837 */
William Juulcfa460a2007-10-31 13:53:06 +0100838static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000839 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200840{
William Juulcfa460a2007-10-31 13:53:06 +0100841 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000842 if (oob_required)
843 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +0100844 return 0;
845}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200846
William Juulcfa460a2007-10-31 13:53:06 +0100847/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000848 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
849 * @mtd: mtd info structure
850 * @chip: nand chip info structure
851 * @buf: buffer to store read data
852 * @oob_required: caller requires OOB data read to chip->oob_poi
853 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500854 *
855 * We need a special oob layout and handling even when OOB isn't used.
856 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200857static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000858 struct nand_chip *chip, uint8_t *buf,
859 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -0500860{
861 int eccsize = chip->ecc.size;
862 int eccbytes = chip->ecc.bytes;
863 uint8_t *oob = chip->oob_poi;
864 int steps, size;
865
866 for (steps = chip->ecc.steps; steps > 0; steps--) {
867 chip->read_buf(mtd, buf, eccsize);
868 buf += eccsize;
869
870 if (chip->ecc.prepad) {
871 chip->read_buf(mtd, oob, chip->ecc.prepad);
872 oob += chip->ecc.prepad;
873 }
874
875 chip->read_buf(mtd, oob, eccbytes);
876 oob += eccbytes;
877
878 if (chip->ecc.postpad) {
879 chip->read_buf(mtd, oob, chip->ecc.postpad);
880 oob += chip->ecc.postpad;
881 }
882 }
883
884 size = mtd->oobsize - (oob - chip->oob_poi);
885 if (size)
886 chip->read_buf(mtd, oob, size);
887
888 return 0;
889}
890
891/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000892 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
893 * @mtd: mtd info structure
894 * @chip: nand chip info structure
895 * @buf: buffer to store read data
896 * @oob_required: caller requires OOB data read to chip->oob_poi
897 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +0100898 */
899static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000900 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +0100901{
902 int i, eccsize = chip->ecc.size;
903 int eccbytes = chip->ecc.bytes;
904 int eccsteps = chip->ecc.steps;
905 uint8_t *p = buf;
906 uint8_t *ecc_calc = chip->buffers->ecccalc;
907 uint8_t *ecc_code = chip->buffers->ecccode;
908 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200909
Sergey Lapindfe64e22013-01-14 03:46:50 +0000910 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200911
William Juulcfa460a2007-10-31 13:53:06 +0100912 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
913 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200914
William Juulcfa460a2007-10-31 13:53:06 +0100915 for (i = 0; i < chip->ecc.total; i++)
916 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200917
William Juulcfa460a2007-10-31 13:53:06 +0100918 eccsteps = chip->ecc.steps;
919 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200920
William Juulcfa460a2007-10-31 13:53:06 +0100921 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
922 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200923
William Juulcfa460a2007-10-31 13:53:06 +0100924 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodc45912d2008-10-24 16:20:43 -0500925 if (stat < 0)
926 mtd->ecc_stats.failed++;
927 else
928 mtd->ecc_stats.corrected += stat;
929 }
930 return 0;
931}
932
933/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000934 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
935 * @mtd: mtd info structure
936 * @chip: nand chip info structure
937 * @data_offs: offset of requested data within the page
938 * @readlen: data length
939 * @bufpoi: buffer to store read data
Scott Woodc45912d2008-10-24 16:20:43 -0500940 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200941static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
942 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Scott Woodc45912d2008-10-24 16:20:43 -0500943{
944 int start_step, end_step, num_steps;
945 uint32_t *eccpos = chip->ecc.layout->eccpos;
946 uint8_t *p;
947 int data_col_addr, i, gaps = 0;
948 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
949 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200950 int index = 0;
Scott Woodc45912d2008-10-24 16:20:43 -0500951
Sergey Lapindfe64e22013-01-14 03:46:50 +0000952 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -0500953 start_step = data_offs / chip->ecc.size;
954 end_step = (data_offs + readlen - 1) / chip->ecc.size;
955 num_steps = end_step - start_step + 1;
956
Sergey Lapindfe64e22013-01-14 03:46:50 +0000957 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -0500958 datafrag_len = num_steps * chip->ecc.size;
959 eccfrag_len = num_steps * chip->ecc.bytes;
960
961 data_col_addr = start_step * chip->ecc.size;
962 /* If we read not a page aligned data */
963 if (data_col_addr != 0)
964 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
965
966 p = bufpoi + data_col_addr;
967 chip->read_buf(mtd, p, datafrag_len);
968
Sergey Lapindfe64e22013-01-14 03:46:50 +0000969 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -0500970 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
971 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
972
Sergey Lapindfe64e22013-01-14 03:46:50 +0000973 /*
974 * The performance is faster if we position offsets according to
975 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
976 */
Scott Woodc45912d2008-10-24 16:20:43 -0500977 for (i = 0; i < eccfrag_len - 1; i++) {
978 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
979 eccpos[i + start_step * chip->ecc.bytes + 1]) {
980 gaps = 1;
981 break;
982 }
983 }
984 if (gaps) {
985 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
986 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
987 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000988 /*
989 * Send the command to read the particular ECC bytes take care
990 * about buswidth alignment in read_buf.
991 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200992 index = start_step * chip->ecc.bytes;
993
994 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -0500995 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200996 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -0500997 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200998 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -0500999 aligned_len++;
1000
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001001 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1002 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001003 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1004 }
1005
1006 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001007 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001008
1009 p = bufpoi + data_col_addr;
1010 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1011 int stat;
1012
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001013 stat = chip->ecc.correct(mtd, p,
1014 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1015 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001016 mtd->ecc_stats.failed++;
1017 else
1018 mtd->ecc_stats.corrected += stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001019 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001020 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001021}
1022
Wolfgang Denk932394a2005-08-17 12:55:25 +02001023/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001024 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1025 * @mtd: mtd info structure
1026 * @chip: nand chip info structure
1027 * @buf: buffer to store read data
1028 * @oob_required: caller requires OOB data read to chip->oob_poi
1029 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001030 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001031 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001032 */
William Juulcfa460a2007-10-31 13:53:06 +01001033static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001034 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001035{
William Juulcfa460a2007-10-31 13:53:06 +01001036 int i, eccsize = chip->ecc.size;
1037 int eccbytes = chip->ecc.bytes;
1038 int eccsteps = chip->ecc.steps;
1039 uint8_t *p = buf;
1040 uint8_t *ecc_calc = chip->buffers->ecccalc;
1041 uint8_t *ecc_code = chip->buffers->ecccode;
1042 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001043
William Juulcfa460a2007-10-31 13:53:06 +01001044 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1045 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1046 chip->read_buf(mtd, p, eccsize);
1047 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1048 }
1049 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001050
William Juulcfa460a2007-10-31 13:53:06 +01001051 for (i = 0; i < chip->ecc.total; i++)
1052 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001053
William Juulcfa460a2007-10-31 13:53:06 +01001054 eccsteps = chip->ecc.steps;
1055 p = buf;
1056
1057 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1058 int stat;
1059
1060 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj18b5a4b2009-11-07 14:25:03 -05001061 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001062 mtd->ecc_stats.failed++;
1063 else
1064 mtd->ecc_stats.corrected += stat;
1065 }
1066 return 0;
1067}
1068
1069/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001070 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1071 * @mtd: mtd info structure
1072 * @chip: nand chip info structure
1073 * @buf: buffer to store read data
1074 * @oob_required: caller requires OOB data read to chip->oob_poi
1075 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001076 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001077 * Hardware ECC for large page chips, require OOB to be read first. For this
1078 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1079 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1080 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1081 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001082 */
1083static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001084 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001085{
1086 int i, eccsize = chip->ecc.size;
1087 int eccbytes = chip->ecc.bytes;
1088 int eccsteps = chip->ecc.steps;
1089 uint8_t *p = buf;
1090 uint8_t *ecc_code = chip->buffers->ecccode;
1091 uint32_t *eccpos = chip->ecc.layout->eccpos;
1092 uint8_t *ecc_calc = chip->buffers->ecccalc;
1093
1094 /* Read the OOB area first */
1095 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1096 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1097 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1098
1099 for (i = 0; i < chip->ecc.total; i++)
1100 ecc_code[i] = chip->oob_poi[eccpos[i]];
1101
1102 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1103 int stat;
1104
1105 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1106 chip->read_buf(mtd, p, eccsize);
1107 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1108
1109 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1110 if (stat < 0)
1111 mtd->ecc_stats.failed++;
1112 else
1113 mtd->ecc_stats.corrected += stat;
1114 }
1115 return 0;
1116}
1117
1118/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001119 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1120 * @mtd: mtd info structure
1121 * @chip: nand chip info structure
1122 * @buf: buffer to store read data
1123 * @oob_required: caller requires OOB data read to chip->oob_poi
1124 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001125 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001126 * The hw generator calculates the error syndrome automatically. Therefore we
1127 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001128 */
1129static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001130 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001131{
1132 int i, eccsize = chip->ecc.size;
1133 int eccbytes = chip->ecc.bytes;
1134 int eccsteps = chip->ecc.steps;
1135 uint8_t *p = buf;
1136 uint8_t *oob = chip->oob_poi;
1137
1138 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1139 int stat;
1140
1141 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1142 chip->read_buf(mtd, p, eccsize);
1143
1144 if (chip->ecc.prepad) {
1145 chip->read_buf(mtd, oob, chip->ecc.prepad);
1146 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001147 }
1148
William Juulcfa460a2007-10-31 13:53:06 +01001149 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1150 chip->read_buf(mtd, oob, eccbytes);
1151 stat = chip->ecc.correct(mtd, p, oob, NULL);
1152
Scott Woodc45912d2008-10-24 16:20:43 -05001153 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001154 mtd->ecc_stats.failed++;
1155 else
1156 mtd->ecc_stats.corrected += stat;
1157
1158 oob += eccbytes;
1159
1160 if (chip->ecc.postpad) {
1161 chip->read_buf(mtd, oob, chip->ecc.postpad);
1162 oob += chip->ecc.postpad;
1163 }
1164 }
1165
1166 /* Calculate remaining oob bytes */
1167 i = mtd->oobsize - (oob - chip->oob_poi);
1168 if (i)
1169 chip->read_buf(mtd, oob, i);
1170
1171 return 0;
1172}
1173
1174/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001175 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1176 * @chip: nand chip structure
1177 * @oob: oob destination address
1178 * @ops: oob ops structure
1179 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001180 */
1181static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1182 struct mtd_oob_ops *ops, size_t len)
1183{
Christian Hitz90e3f392011-10-12 09:32:01 +02001184 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001185
Sergey Lapindfe64e22013-01-14 03:46:50 +00001186 case MTD_OPS_PLACE_OOB:
1187 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001188 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1189 return oob + len;
1190
Sergey Lapindfe64e22013-01-14 03:46:50 +00001191 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001192 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1193 uint32_t boffs = 0, roffs = ops->ooboffs;
1194 size_t bytes = 0;
1195
Christian Hitz90e3f392011-10-12 09:32:01 +02001196 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001197 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001198 if (unlikely(roffs)) {
1199 if (roffs >= free->length) {
1200 roffs -= free->length;
1201 continue;
1202 }
1203 boffs = free->offset + roffs;
1204 bytes = min_t(size_t, len,
1205 (free->length - roffs));
1206 roffs = 0;
1207 } else {
1208 bytes = min_t(size_t, len, free->length);
1209 boffs = free->offset;
1210 }
1211 memcpy(oob, chip->oob_poi + boffs, bytes);
1212 oob += bytes;
1213 }
1214 return oob;
1215 }
1216 default:
1217 BUG();
1218 }
1219 return NULL;
1220}
1221
1222/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001223 * nand_do_read_ops - [INTERN] Read data with ECC
1224 * @mtd: MTD device structure
1225 * @from: offset to read from
1226 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001227 *
1228 * Internal function. Called with chip held.
1229 */
1230static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1231 struct mtd_oob_ops *ops)
1232{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001233 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juulcfa460a2007-10-31 13:53:06 +01001234 struct nand_chip *chip = mtd->priv;
1235 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001236 int ret = 0;
1237 uint32_t readlen = ops->len;
1238 uint32_t oobreadlen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001239 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001240 mtd->oobavail : mtd->oobsize;
1241
William Juulcfa460a2007-10-31 13:53:06 +01001242 uint8_t *bufpoi, *oob, *buf;
Paul Burton40462e52013-09-04 15:16:56 +01001243 unsigned int max_bitflips = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001244
1245 stats = mtd->ecc_stats;
1246
1247 chipnr = (int)(from >> chip->chip_shift);
1248 chip->select_chip(mtd, chipnr);
1249
1250 realpage = (int)(from >> chip->page_shift);
1251 page = realpage & chip->pagemask;
1252
1253 col = (int)(from & (mtd->writesize - 1));
1254
1255 buf = ops->datbuf;
1256 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001257 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001258
Christian Hitz90e3f392011-10-12 09:32:01 +02001259 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001260 WATCHDOG_RESET();
1261
William Juulcfa460a2007-10-31 13:53:06 +01001262 bytes = min(mtd->writesize - col, readlen);
1263 aligned = (bytes == mtd->writesize);
1264
Sergey Lapindfe64e22013-01-14 03:46:50 +00001265 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001266 if (realpage != chip->pagebuf || oob) {
1267 bufpoi = aligned ? buf : chip->buffers->databuf;
1268
Sergey Lapindfe64e22013-01-14 03:46:50 +00001269 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001270
Paul Burton40462e52013-09-04 15:16:56 +01001271 /*
1272 * Now read the page into the buffer. Absent an error,
1273 * the read methods return max bitflips per ecc step.
1274 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001275 if (unlikely(ops->mode == MTD_OPS_RAW))
1276 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1277 oob_required,
1278 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001279 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1280 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001281 ret = chip->ecc.read_subpage(mtd, chip,
1282 col, bytes, bufpoi);
William Juulcfa460a2007-10-31 13:53:06 +01001283 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001284 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001285 oob_required, page);
1286 if (ret < 0) {
1287 if (!aligned)
1288 /* Invalidate page cache */
1289 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001290 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001291 }
William Juulcfa460a2007-10-31 13:53:06 +01001292
Paul Burton40462e52013-09-04 15:16:56 +01001293 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1294
William Juulcfa460a2007-10-31 13:53:06 +01001295 /* Transfer not aligned data */
1296 if (!aligned) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001297 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00001298 !(mtd->ecc_stats.failed - stats.failed) &&
Paul Burton40462e52013-09-04 15:16:56 +01001299 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001300 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001301 chip->pagebuf_bitflips = ret;
1302 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001303 /* Invalidate page cache */
1304 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001305 }
William Juulcfa460a2007-10-31 13:53:06 +01001306 memcpy(buf, chip->buffers->databuf + col, bytes);
1307 }
1308
1309 buf += bytes;
1310
1311 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001312 int toread = min(oobreadlen, max_oobsize);
1313
1314 if (toread) {
1315 oob = nand_transfer_oob(chip,
1316 oob, ops, toread);
1317 oobreadlen -= toread;
1318 }
William Juulcfa460a2007-10-31 13:53:06 +01001319 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001320 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001321 memcpy(buf, chip->buffers->databuf + col, bytes);
1322 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001323 max_bitflips = max_t(unsigned int, max_bitflips,
1324 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001325 }
1326
William Juulcfa460a2007-10-31 13:53:06 +01001327 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001328
William Juulcfa460a2007-10-31 13:53:06 +01001329 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001330 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001331
Sergey Lapindfe64e22013-01-14 03:46:50 +00001332 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001333 col = 0;
1334 /* Increment page address */
1335 realpage++;
1336
William Juulcfa460a2007-10-31 13:53:06 +01001337 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001338 /* Check, if we cross a chip boundary */
1339 if (!page) {
1340 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001341 chip->select_chip(mtd, -1);
1342 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001343 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001344 }
1345
William Juulcfa460a2007-10-31 13:53:06 +01001346 ops->retlen = ops->len - (size_t) readlen;
1347 if (oob)
1348 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001349
William Juulcfa460a2007-10-31 13:53:06 +01001350 if (ret)
1351 return ret;
1352
1353 if (mtd->ecc_stats.failed - stats.failed)
1354 return -EBADMSG;
1355
Paul Burton40462e52013-09-04 15:16:56 +01001356 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001357}
1358
1359/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001360 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001361 * @mtd: MTD device structure
1362 * @from: offset to read from
1363 * @len: number of bytes to read
1364 * @retlen: pointer to variable to store the number of read bytes
1365 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001366 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001367 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001368 */
William Juulcfa460a2007-10-31 13:53:06 +01001369static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1370 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001371{
William Juulcfa460a2007-10-31 13:53:06 +01001372 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001373 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001374 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001375
William Juulcfa460a2007-10-31 13:53:06 +01001376 nand_get_device(chip, mtd, FL_READING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001377 ops.len = len;
1378 ops.datbuf = buf;
1379 ops.oobbuf = NULL;
1380 ops.mode = MTD_OPS_PLACE_OOB;
1381 ret = nand_do_read_ops(mtd, from, &ops);
1382 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001383 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001384 return ret;
1385}
1386
William Juulcfa460a2007-10-31 13:53:06 +01001387/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001388 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1389 * @mtd: mtd info structure
1390 * @chip: nand chip info structure
1391 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001392 */
1393static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001394 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001395{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001396 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001397 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001398 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001399}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001400
1401/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001402 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001403 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001404 * @mtd: mtd info structure
1405 * @chip: nand chip info structure
1406 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001407 */
1408static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001409 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001410{
1411 uint8_t *buf = chip->oob_poi;
1412 int length = mtd->oobsize;
1413 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1414 int eccsize = chip->ecc.size;
1415 uint8_t *bufpoi = buf;
1416 int i, toread, sndrnd = 0, pos;
1417
1418 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1419 for (i = 0; i < chip->ecc.steps; i++) {
1420 if (sndrnd) {
1421 pos = eccsize + i * (eccsize + chunk);
1422 if (mtd->writesize > 512)
1423 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1424 else
1425 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1426 } else
1427 sndrnd = 1;
1428 toread = min_t(int, length, chunk);
1429 chip->read_buf(mtd, bufpoi, toread);
1430 bufpoi += toread;
1431 length -= toread;
1432 }
1433 if (length > 0)
1434 chip->read_buf(mtd, bufpoi, length);
1435
Sergey Lapindfe64e22013-01-14 03:46:50 +00001436 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001437}
1438
1439/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001440 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1441 * @mtd: mtd info structure
1442 * @chip: nand chip info structure
1443 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001444 */
1445static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1446 int page)
1447{
1448 int status = 0;
1449 const uint8_t *buf = chip->oob_poi;
1450 int length = mtd->oobsize;
1451
1452 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1453 chip->write_buf(mtd, buf, length);
1454 /* Send command to program the OOB data */
1455 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1456
1457 status = chip->waitfunc(mtd, chip);
1458
1459 return status & NAND_STATUS_FAIL ? -EIO : 0;
1460}
1461
1462/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001463 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1464 * with syndrome - only for large page flash
1465 * @mtd: mtd info structure
1466 * @chip: nand chip info structure
1467 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001468 */
1469static int nand_write_oob_syndrome(struct mtd_info *mtd,
1470 struct nand_chip *chip, int page)
1471{
1472 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1473 int eccsize = chip->ecc.size, length = mtd->oobsize;
1474 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1475 const uint8_t *bufpoi = chip->oob_poi;
1476
1477 /*
1478 * data-ecc-data-ecc ... ecc-oob
1479 * or
1480 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1481 */
1482 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1483 pos = steps * (eccsize + chunk);
1484 steps = 0;
1485 } else
1486 pos = eccsize;
1487
1488 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1489 for (i = 0; i < steps; i++) {
1490 if (sndcmd) {
1491 if (mtd->writesize <= 512) {
1492 uint32_t fill = 0xFFFFFFFF;
1493
1494 len = eccsize;
1495 while (len > 0) {
1496 int num = min_t(int, len, 4);
1497 chip->write_buf(mtd, (uint8_t *)&fill,
1498 num);
1499 len -= num;
1500 }
1501 } else {
1502 pos = eccsize + i * (eccsize + chunk);
1503 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1504 }
1505 } else
1506 sndcmd = 1;
1507 len = min_t(int, length, chunk);
1508 chip->write_buf(mtd, bufpoi, len);
1509 bufpoi += len;
1510 length -= len;
1511 }
1512 if (length > 0)
1513 chip->write_buf(mtd, bufpoi, length);
1514
1515 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1516 status = chip->waitfunc(mtd, chip);
1517
1518 return status & NAND_STATUS_FAIL ? -EIO : 0;
1519}
1520
1521/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001522 * nand_do_read_oob - [INTERN] NAND read out-of-band
1523 * @mtd: MTD device structure
1524 * @from: offset to read from
1525 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001526 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001527 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01001528 */
1529static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1530 struct mtd_oob_ops *ops)
1531{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001532 int page, realpage, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01001533 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001534 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001535 int readlen = ops->ooblen;
1536 int len;
1537 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001538 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001539
Christian Hitz90e3f392011-10-12 09:32:01 +02001540 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1541 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001542
Sergey Lapindfe64e22013-01-14 03:46:50 +00001543 stats = mtd->ecc_stats;
1544
1545 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01001546 len = chip->ecc.layout->oobavail;
1547 else
1548 len = mtd->oobsize;
1549
1550 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001551 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1552 "outside oob\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001553 return -EINVAL;
1554 }
1555
1556 /* Do not allow reads past end of device */
1557 if (unlikely(from >= mtd->size ||
1558 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1559 (from >> chip->page_shift)) * len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001560 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1561 "of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001562 return -EINVAL;
1563 }
1564
1565 chipnr = (int)(from >> chip->chip_shift);
1566 chip->select_chip(mtd, chipnr);
1567
1568 /* Shift to get page */
1569 realpage = (int)(from >> chip->page_shift);
1570 page = realpage & chip->pagemask;
1571
Christian Hitz90e3f392011-10-12 09:32:01 +02001572 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001573 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00001574 if (ops->mode == MTD_OPS_RAW)
1575 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1576 else
1577 ret = chip->ecc.read_oob(mtd, chip, page);
1578
1579 if (ret < 0)
1580 break;
William Juulcfa460a2007-10-31 13:53:06 +01001581
1582 len = min(len, readlen);
1583 buf = nand_transfer_oob(chip, buf, ops, len);
1584
William Juulcfa460a2007-10-31 13:53:06 +01001585 readlen -= len;
1586 if (!readlen)
1587 break;
1588
1589 /* Increment page address */
1590 realpage++;
1591
1592 page = realpage & chip->pagemask;
1593 /* Check, if we cross a chip boundary */
1594 if (!page) {
1595 chipnr++;
1596 chip->select_chip(mtd, -1);
1597 chip->select_chip(mtd, chipnr);
1598 }
William Juulcfa460a2007-10-31 13:53:06 +01001599 }
1600
Sergey Lapindfe64e22013-01-14 03:46:50 +00001601 ops->oobretlen = ops->ooblen - readlen;
1602
1603 if (ret < 0)
1604 return ret;
1605
1606 if (mtd->ecc_stats.failed - stats.failed)
1607 return -EBADMSG;
1608
1609 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001610}
1611
1612/**
1613 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00001614 * @mtd: MTD device structure
1615 * @from: offset to read from
1616 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01001617 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001618 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01001619 */
1620static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1621 struct mtd_oob_ops *ops)
1622{
1623 struct nand_chip *chip = mtd->priv;
1624 int ret = -ENOTSUPP;
1625
1626 ops->retlen = 0;
1627
1628 /* Do not allow reads past end of device */
1629 if (ops->datbuf && (from + ops->len) > mtd->size) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001630 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1631 "beyond end of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001632 return -EINVAL;
1633 }
1634
1635 nand_get_device(chip, mtd, FL_READING);
1636
Christian Hitz90e3f392011-10-12 09:32:01 +02001637 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001638 case MTD_OPS_PLACE_OOB:
1639 case MTD_OPS_AUTO_OOB:
1640 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001641 break;
1642
1643 default:
1644 goto out;
1645 }
1646
1647 if (!ops->datbuf)
1648 ret = nand_do_read_oob(mtd, from, ops);
1649 else
1650 ret = nand_do_read_ops(mtd, from, ops);
1651
Christian Hitz90e3f392011-10-12 09:32:01 +02001652out:
William Juulcfa460a2007-10-31 13:53:06 +01001653 nand_release_device(mtd);
1654 return ret;
1655}
1656
1657
1658/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001659 * nand_write_page_raw - [INTERN] raw page write function
1660 * @mtd: mtd info structure
1661 * @chip: nand chip info structure
1662 * @buf: data buffer
1663 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05001664 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001665 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01001666 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001667static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1668 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001669{
1670 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001671 if (oob_required)
1672 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1673
1674 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001675}
1676
1677/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001678 * nand_write_page_raw_syndrome - [INTERN] raw page write function
1679 * @mtd: mtd info structure
1680 * @chip: nand chip info structure
1681 * @buf: data buffer
1682 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05001683 *
1684 * We need a special oob layout and handling even when ECC isn't checked.
1685 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001686static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02001687 struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001688 const uint8_t *buf, int oob_required)
David Brownell7e866612009-11-07 16:27:01 -05001689{
1690 int eccsize = chip->ecc.size;
1691 int eccbytes = chip->ecc.bytes;
1692 uint8_t *oob = chip->oob_poi;
1693 int steps, size;
1694
1695 for (steps = chip->ecc.steps; steps > 0; steps--) {
1696 chip->write_buf(mtd, buf, eccsize);
1697 buf += eccsize;
1698
1699 if (chip->ecc.prepad) {
1700 chip->write_buf(mtd, oob, chip->ecc.prepad);
1701 oob += chip->ecc.prepad;
1702 }
1703
1704 chip->read_buf(mtd, oob, eccbytes);
1705 oob += eccbytes;
1706
1707 if (chip->ecc.postpad) {
1708 chip->write_buf(mtd, oob, chip->ecc.postpad);
1709 oob += chip->ecc.postpad;
1710 }
1711 }
1712
1713 size = mtd->oobsize - (oob - chip->oob_poi);
1714 if (size)
1715 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001716
1717 return 0;
David Brownell7e866612009-11-07 16:27:01 -05001718}
1719/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001720 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
1721 * @mtd: mtd info structure
1722 * @chip: nand chip info structure
1723 * @buf: data buffer
1724 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001725 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001726static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1727 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001728{
1729 int i, eccsize = chip->ecc.size;
1730 int eccbytes = chip->ecc.bytes;
1731 int eccsteps = chip->ecc.steps;
1732 uint8_t *ecc_calc = chip->buffers->ecccalc;
1733 const uint8_t *p = buf;
1734 uint32_t *eccpos = chip->ecc.layout->eccpos;
1735
Sergey Lapindfe64e22013-01-14 03:46:50 +00001736 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01001737 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1738 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1739
1740 for (i = 0; i < chip->ecc.total; i++)
1741 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1742
Sergey Lapindfe64e22013-01-14 03:46:50 +00001743 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
William Juulcfa460a2007-10-31 13:53:06 +01001744}
1745
1746/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001747 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
1748 * @mtd: mtd info structure
1749 * @chip: nand chip info structure
1750 * @buf: data buffer
1751 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001752 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001753static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1754 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001755{
1756 int i, eccsize = chip->ecc.size;
1757 int eccbytes = chip->ecc.bytes;
1758 int eccsteps = chip->ecc.steps;
1759 uint8_t *ecc_calc = chip->buffers->ecccalc;
1760 const uint8_t *p = buf;
1761 uint32_t *eccpos = chip->ecc.layout->eccpos;
1762
1763 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1764 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1765 chip->write_buf(mtd, p, eccsize);
1766 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1767 }
1768
1769 for (i = 0; i < chip->ecc.total; i++)
1770 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1771
1772 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001773
1774 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001775}
1776
1777/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001778 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
1779 * @mtd: mtd info structure
1780 * @chip: nand chip info structure
1781 * @buf: data buffer
1782 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001783 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001784 * The hw generator calculates the error syndrome automatically. Therefore we
1785 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001786 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001787static int nand_write_page_syndrome(struct mtd_info *mtd,
1788 struct nand_chip *chip,
1789 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001790{
1791 int i, eccsize = chip->ecc.size;
1792 int eccbytes = chip->ecc.bytes;
1793 int eccsteps = chip->ecc.steps;
1794 const uint8_t *p = buf;
1795 uint8_t *oob = chip->oob_poi;
1796
1797 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1798
1799 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1800 chip->write_buf(mtd, p, eccsize);
1801
1802 if (chip->ecc.prepad) {
1803 chip->write_buf(mtd, oob, chip->ecc.prepad);
1804 oob += chip->ecc.prepad;
1805 }
1806
1807 chip->ecc.calculate(mtd, p, oob);
1808 chip->write_buf(mtd, oob, eccbytes);
1809 oob += eccbytes;
1810
1811 if (chip->ecc.postpad) {
1812 chip->write_buf(mtd, oob, chip->ecc.postpad);
1813 oob += chip->ecc.postpad;
1814 }
1815 }
1816
1817 /* Calculate remaining oob bytes */
1818 i = mtd->oobsize - (oob - chip->oob_poi);
1819 if (i)
1820 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001821
1822 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001823}
1824
1825/**
1826 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00001827 * @mtd: MTD device structure
1828 * @chip: NAND chip descriptor
1829 * @buf: the data to write
1830 * @oob_required: must write chip->oob_poi to OOB
1831 * @page: page number to write
1832 * @cached: cached programming
1833 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01001834 */
1835static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001836 const uint8_t *buf, int oob_required, int page,
1837 int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01001838{
1839 int status;
1840
1841 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1842
1843 if (unlikely(raw))
Sergey Lapindfe64e22013-01-14 03:46:50 +00001844 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
William Juulcfa460a2007-10-31 13:53:06 +01001845 else
Sergey Lapindfe64e22013-01-14 03:46:50 +00001846 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1847
1848 if (status < 0)
1849 return status;
William Juulcfa460a2007-10-31 13:53:06 +01001850
1851 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00001852 * Cached progamming disabled for now. Not sure if it's worth the
1853 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01001854 */
1855 cached = 0;
1856
1857 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1858
1859 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1860 status = chip->waitfunc(mtd, chip);
1861 /*
1862 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00001863 * available.
William Juulcfa460a2007-10-31 13:53:06 +01001864 */
1865 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1866 status = chip->errstat(mtd, chip, FL_WRITING, status,
1867 page);
1868
1869 if (status & NAND_STATUS_FAIL)
1870 return -EIO;
1871 } else {
1872 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1873 status = chip->waitfunc(mtd, chip);
1874 }
1875
1876#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1877 /* Send command to read back the data */
1878 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1879
1880 if (chip->verify_buf(mtd, buf, mtd->writesize))
1881 return -EIO;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001882
1883 /* Make sure the next page prog is preceded by a status read */
1884 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01001885#endif
1886 return 0;
1887}
1888
1889/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001890 * nand_fill_oob - [INTERN] Transfer client buffer to oob
1891 * @mtd: MTD device structure
1892 * @oob: oob data buffer
1893 * @len: oob data write length
1894 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001895 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001896static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
1897 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01001898{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001899 struct nand_chip *chip = mtd->priv;
1900
1901 /*
1902 * Initialise to all 0xFF, to avoid the possibility of left over OOB
1903 * data from a previous OOB read.
1904 */
1905 memset(chip->oob_poi, 0xff, mtd->oobsize);
1906
Christian Hitz90e3f392011-10-12 09:32:01 +02001907 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001908
Sergey Lapindfe64e22013-01-14 03:46:50 +00001909 case MTD_OPS_PLACE_OOB:
1910 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001911 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1912 return oob + len;
1913
Sergey Lapindfe64e22013-01-14 03:46:50 +00001914 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001915 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1916 uint32_t boffs = 0, woffs = ops->ooboffs;
1917 size_t bytes = 0;
1918
Christian Hitz90e3f392011-10-12 09:32:01 +02001919 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001920 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001921 if (unlikely(woffs)) {
1922 if (woffs >= free->length) {
1923 woffs -= free->length;
1924 continue;
1925 }
1926 boffs = free->offset + woffs;
1927 bytes = min_t(size_t, len,
1928 (free->length - woffs));
1929 woffs = 0;
1930 } else {
1931 bytes = min_t(size_t, len, free->length);
1932 boffs = free->offset;
1933 }
1934 memcpy(chip->oob_poi + boffs, oob, bytes);
1935 oob += bytes;
1936 }
1937 return oob;
1938 }
1939 default:
1940 BUG();
1941 }
1942 return NULL;
1943}
1944
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001945#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01001946
1947/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001948 * nand_do_write_ops - [INTERN] NAND write with ECC
1949 * @mtd: MTD device structure
1950 * @to: offset to write to
1951 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001952 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001953 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01001954 */
1955static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1956 struct mtd_oob_ops *ops)
1957{
1958 int chipnr, realpage, page, blockmask, column;
1959 struct nand_chip *chip = mtd->priv;
1960 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001961
1962 uint32_t oobwritelen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001963 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001964 mtd->oobavail : mtd->oobsize;
1965
William Juulcfa460a2007-10-31 13:53:06 +01001966 uint8_t *oob = ops->oobbuf;
1967 uint8_t *buf = ops->datbuf;
1968 int ret, subpage;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001969 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001970
1971 ops->retlen = 0;
1972 if (!writelen)
1973 return 0;
1974
William Juulcfa460a2007-10-31 13:53:06 +01001975 column = to & (mtd->writesize - 1);
1976 subpage = column || (writelen & (mtd->writesize - 1));
1977
1978 if (subpage && oob)
1979 return -EINVAL;
1980
1981 chipnr = (int)(to >> chip->chip_shift);
1982 chip->select_chip(mtd, chipnr);
1983
1984 /* Check, if it is write protected */
1985 if (nand_check_wp(mtd)) {
1986 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1987 return -EIO;
1988 }
1989
1990 realpage = (int)(to >> chip->page_shift);
1991 page = realpage & chip->pagemask;
1992 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1993
1994 /* Invalidate the page cache, when we write to the cached page */
1995 if (to <= (chip->pagebuf << chip->page_shift) &&
1996 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1997 chip->pagebuf = -1;
1998
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001999 /* Don't allow multipage oob writes with offset */
2000 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2001 return -EINVAL;
2002
Christian Hitz90e3f392011-10-12 09:32:01 +02002003 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002004 WATCHDOG_RESET();
2005
William Juulcfa460a2007-10-31 13:53:06 +01002006 int bytes = mtd->writesize;
2007 int cached = writelen > bytes && page != blockmask;
2008 uint8_t *wbuf = buf;
2009
Sergey Lapindfe64e22013-01-14 03:46:50 +00002010 /* Partial page write? */
htbegin070fd8e2013-03-01 22:59:27 +00002011 if (unlikely(column || writelen < mtd->writesize)) {
William Juulcfa460a2007-10-31 13:53:06 +01002012 cached = 0;
2013 bytes = min_t(int, bytes - column, (int) writelen);
2014 chip->pagebuf = -1;
2015 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2016 memcpy(&chip->buffers->databuf[column], buf, bytes);
2017 wbuf = chip->buffers->databuf;
2018 }
2019
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002020 if (unlikely(oob)) {
2021 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002022 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002023 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002024 } else {
2025 /* We still need to erase leftover OOB data */
2026 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002027 }
William Juulcfa460a2007-10-31 13:53:06 +01002028
Sergey Lapindfe64e22013-01-14 03:46:50 +00002029 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2030 cached, (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002031 if (ret)
2032 break;
2033
2034 writelen -= bytes;
2035 if (!writelen)
2036 break;
2037
2038 column = 0;
2039 buf += bytes;
2040 realpage++;
2041
2042 page = realpage & chip->pagemask;
2043 /* Check, if we cross a chip boundary */
2044 if (!page) {
2045 chipnr++;
2046 chip->select_chip(mtd, -1);
2047 chip->select_chip(mtd, chipnr);
2048 }
2049 }
2050
2051 ops->retlen = ops->len - writelen;
2052 if (unlikely(oob))
2053 ops->oobretlen = ops->ooblen;
2054 return ret;
2055}
2056
2057/**
2058 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002059 * @mtd: MTD device structure
2060 * @to: offset to write to
2061 * @len: number of bytes to write
2062 * @retlen: pointer to variable to store the number of written bytes
2063 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002064 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002065 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002066 */
2067static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2068 size_t *retlen, const uint8_t *buf)
2069{
2070 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002071 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002072 int ret;
2073
William Juulcfa460a2007-10-31 13:53:06 +01002074 nand_get_device(chip, mtd, FL_WRITING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002075 ops.len = len;
2076 ops.datbuf = (uint8_t *)buf;
2077 ops.oobbuf = NULL;
2078 ops.mode = MTD_OPS_PLACE_OOB;
2079 ret = nand_do_write_ops(mtd, to, &ops);
2080 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002081 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002082 return ret;
2083}
2084
2085/**
2086 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002087 * @mtd: MTD device structure
2088 * @to: offset to write to
2089 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002090 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002091 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002092 */
William Juulcfa460a2007-10-31 13:53:06 +01002093static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2094 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002095{
William Juulcfa460a2007-10-31 13:53:06 +01002096 int chipnr, page, status, len;
2097 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002098
Christian Hitz90e3f392011-10-12 09:32:01 +02002099 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2100 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002101
Sergey Lapindfe64e22013-01-14 03:46:50 +00002102 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002103 len = chip->ecc.layout->oobavail;
2104 else
2105 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002106
2107 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002108 if ((ops->ooboffs + ops->ooblen) > len) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002109 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2110 "past end of page\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002111 return -EINVAL;
2112 }
2113
William Juulcfa460a2007-10-31 13:53:06 +01002114 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002115 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2116 "write outside oob\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002117 return -EINVAL;
2118 }
2119
Christian Hitz90e3f392011-10-12 09:32:01 +02002120 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002121 if (unlikely(to >= mtd->size ||
2122 ops->ooboffs + ops->ooblen >
2123 ((mtd->size >> chip->page_shift) -
2124 (to >> chip->page_shift)) * len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002125 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2126 "end of device\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002127 return -EINVAL;
2128 }
2129
William Juulcfa460a2007-10-31 13:53:06 +01002130 chipnr = (int)(to >> chip->chip_shift);
2131 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002132
William Juulcfa460a2007-10-31 13:53:06 +01002133 /* Shift to get page */
2134 page = (int)(to >> chip->page_shift);
2135
2136 /*
2137 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2138 * of my DiskOnChip 2000 test units) will clear the whole data page too
2139 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2140 * it in the doc2000 driver in August 1999. dwmw2.
2141 */
2142 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002143
2144 /* Check, if it is write protected */
2145 if (nand_check_wp(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002146 return -EROFS;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002147
Wolfgang Denk932394a2005-08-17 12:55:25 +02002148 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002149 if (page == chip->pagebuf)
2150 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002151
Sergey Lapindfe64e22013-01-14 03:46:50 +00002152 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2153
2154 if (ops->mode == MTD_OPS_RAW)
2155 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2156 else
2157 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002158
William Juulcfa460a2007-10-31 13:53:06 +01002159 if (status)
2160 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002161
William Juulcfa460a2007-10-31 13:53:06 +01002162 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002163
William Juulcfa460a2007-10-31 13:53:06 +01002164 return 0;
2165}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002166
William Juulcfa460a2007-10-31 13:53:06 +01002167/**
2168 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002169 * @mtd: MTD device structure
2170 * @to: offset to write to
2171 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002172 */
2173static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2174 struct mtd_oob_ops *ops)
2175{
2176 struct nand_chip *chip = mtd->priv;
2177 int ret = -ENOTSUPP;
2178
2179 ops->retlen = 0;
2180
2181 /* Do not allow writes past end of device */
2182 if (ops->datbuf && (to + ops->len) > mtd->size) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002183 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2184 "end of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002185 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002186 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002187
William Juulcfa460a2007-10-31 13:53:06 +01002188 nand_get_device(chip, mtd, FL_WRITING);
2189
Christian Hitz90e3f392011-10-12 09:32:01 +02002190 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002191 case MTD_OPS_PLACE_OOB:
2192 case MTD_OPS_AUTO_OOB:
2193 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002194 break;
2195
2196 default:
2197 goto out;
2198 }
2199
2200 if (!ops->datbuf)
2201 ret = nand_do_write_oob(mtd, to, ops);
2202 else
2203 ret = nand_do_write_ops(mtd, to, ops);
2204
Christian Hitz90e3f392011-10-12 09:32:01 +02002205out:
William Juulcfa460a2007-10-31 13:53:06 +01002206 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002207 return ret;
2208}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002209
2210/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002211 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2212 * @mtd: MTD device structure
2213 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002214 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002215 * Standard erase command for NAND chips.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002216 */
William Juulcfa460a2007-10-31 13:53:06 +01002217static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002218{
William Juulcfa460a2007-10-31 13:53:06 +01002219 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002220 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002221 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2222 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002223}
2224
2225/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002226 * multi_erase_cmd - [GENERIC] AND specific block erase command function
2227 * @mtd: MTD device structure
2228 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002229 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002230 * AND multi block erase command function. Erase 4 consecutive blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002231 */
William Juulcfa460a2007-10-31 13:53:06 +01002232static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002233{
William Juulcfa460a2007-10-31 13:53:06 +01002234 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002235 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002236 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2237 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2238 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2239 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2240 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002241}
2242
2243/**
2244 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002245 * @mtd: MTD device structure
2246 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002247 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002248 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002249 */
William Juulcfa460a2007-10-31 13:53:06 +01002250static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002251{
William Juulcfa460a2007-10-31 13:53:06 +01002252 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002253}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002254
William Juulcfa460a2007-10-31 13:53:06 +01002255#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk932394a2005-08-17 12:55:25 +02002256/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002257 * nand_erase_nand - [INTERN] erase block(s)
2258 * @mtd: MTD device structure
2259 * @instr: erase instruction
2260 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002261 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002262 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002263 */
William Juulcfa460a2007-10-31 13:53:06 +01002264int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2265 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002266{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002267 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002268 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002269 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juulcfa460a2007-10-31 13:53:06 +01002270 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002271 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002272
Christian Hitz90e3f392011-10-12 09:32:01 +02002273 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2274 __func__, (unsigned long long)instr->addr,
2275 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002276
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002277 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002278 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002279
Wolfgang Denk932394a2005-08-17 12:55:25 +02002280 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002281 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002282
2283 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002284 page = (int)(instr->addr >> chip->page_shift);
2285 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002286
2287 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002288 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002289
Wolfgang Denk932394a2005-08-17 12:55:25 +02002290 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002291 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002292
Wolfgang Denk932394a2005-08-17 12:55:25 +02002293 /* Check, if it is write protected */
2294 if (nand_check_wp(mtd)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002295 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2296 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002297 instr->state = MTD_ERASE_FAILED;
2298 goto erase_exit;
2299 }
2300
William Juulcfa460a2007-10-31 13:53:06 +01002301 /*
2302 * If BBT requires refresh, set the BBT page mask to see if the BBT
2303 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2304 * can not be matched. This is also done when the bbt is actually
Sergey Lapindfe64e22013-01-14 03:46:50 +00002305 * erased to avoid recursive updates.
William Juulcfa460a2007-10-31 13:53:06 +01002306 */
2307 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2308 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2309
Wolfgang Denk932394a2005-08-17 12:55:25 +02002310 /* Loop through the pages */
2311 len = instr->len;
2312
2313 instr->state = MTD_ERASING;
2314
2315 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002316 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00002317 /* Check if we have a bad block, we do not erase bad blocks! */
Marek Vasut6d414192011-09-12 06:04:06 +02002318 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002319 chip->page_shift, 0, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002320 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2321 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002322 instr->state = MTD_ERASE_FAILED;
2323 goto erase_exit;
2324 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002325
William Juulcfa460a2007-10-31 13:53:06 +01002326 /*
2327 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002328 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002329 */
2330 if (page <= chip->pagebuf && chip->pagebuf <
2331 (page + pages_per_block))
2332 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002333
William Juulcfa460a2007-10-31 13:53:06 +01002334 chip->erase_cmd(mtd, page & chip->pagemask);
2335
2336 status = chip->waitfunc(mtd, chip);
2337
2338 /*
2339 * See if operation failed and additional status checks are
2340 * available
2341 */
2342 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2343 status = chip->errstat(mtd, chip, FL_ERASING,
2344 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002345
2346 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002347 if (status & NAND_STATUS_FAIL) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002348 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2349 "page 0x%08x\n", __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002350 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002351 instr->fail_addr =
2352 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002353 goto erase_exit;
2354 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002355
William Juulcfa460a2007-10-31 13:53:06 +01002356 /*
2357 * If BBT requires refresh, set the BBT rewrite flag to the
Sergey Lapindfe64e22013-01-14 03:46:50 +00002358 * page being erased.
William Juulcfa460a2007-10-31 13:53:06 +01002359 */
2360 if (bbt_masked_page != 0xffffffff &&
2361 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002362 rewrite_bbt[chipnr] =
2363 ((loff_t)page << chip->page_shift);
William Juulcfa460a2007-10-31 13:53:06 +01002364
Wolfgang Denk932394a2005-08-17 12:55:25 +02002365 /* Increment page address and decrement length */
William Juulcfa460a2007-10-31 13:53:06 +01002366 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002367 page += pages_per_block;
2368
2369 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002370 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002371 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002372 chip->select_chip(mtd, -1);
2373 chip->select_chip(mtd, chipnr);
2374
2375 /*
2376 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Sergey Lapindfe64e22013-01-14 03:46:50 +00002377 * page mask to see if this BBT should be rewritten.
William Juulcfa460a2007-10-31 13:53:06 +01002378 */
2379 if (bbt_masked_page != 0xffffffff &&
2380 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2381 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2382 BBT_PAGE_MASK;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002383 }
2384 }
2385 instr->state = MTD_ERASE_DONE;
2386
Christian Hitz90e3f392011-10-12 09:32:01 +02002387erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002388
2389 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002390
2391 /* Deselect and wake up anyone waiting on the device */
2392 nand_release_device(mtd);
2393
Scott Woodc45912d2008-10-24 16:20:43 -05002394 /* Do call back function */
2395 if (!ret)
2396 mtd_erase_callback(instr);
2397
William Juulcfa460a2007-10-31 13:53:06 +01002398 /*
2399 * If BBT requires refresh and erase was successful, rewrite any
Sergey Lapindfe64e22013-01-14 03:46:50 +00002400 * selected bad block tables.
William Juulcfa460a2007-10-31 13:53:06 +01002401 */
2402 if (bbt_masked_page == 0xffffffff || ret)
2403 return ret;
2404
2405 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2406 if (!rewrite_bbt[chipnr])
2407 continue;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002408 /* Update the BBT for chip */
Christian Hitz90e3f392011-10-12 09:32:01 +02002409 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2410 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2411 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
William Juulcfa460a2007-10-31 13:53:06 +01002412 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2413 }
2414
Wolfgang Denk932394a2005-08-17 12:55:25 +02002415 /* Return more or less happy */
2416 return ret;
2417}
2418
2419/**
2420 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002421 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002422 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002423 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002424 */
William Juulcfa460a2007-10-31 13:53:06 +01002425static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002426{
William Juulcfa460a2007-10-31 13:53:06 +01002427 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002428
Christian Hitz90e3f392011-10-12 09:32:01 +02002429 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002430
2431 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002432 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002433 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002434 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002435}
2436
Wolfgang Denk932394a2005-08-17 12:55:25 +02002437/**
William Juulcfa460a2007-10-31 13:53:06 +01002438 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002439 * @mtd: MTD device structure
2440 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002441 */
William Juulcfa460a2007-10-31 13:53:06 +01002442static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002443{
William Juulcfa460a2007-10-31 13:53:06 +01002444 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002445}
2446
2447/**
William Juulcfa460a2007-10-31 13:53:06 +01002448 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002449 * @mtd: MTD device structure
2450 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002451 */
William Juulcfa460a2007-10-31 13:53:06 +01002452static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002453{
William Juulcfa460a2007-10-31 13:53:06 +01002454 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002455 int ret;
2456
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002457 ret = nand_block_isbad(mtd, ofs);
2458 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002459 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002460 if (ret > 0)
2461 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002462 return ret;
2463 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002464
William Juulcfa460a2007-10-31 13:53:06 +01002465 return chip->block_markbad(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002466}
2467
Sergey Lapindfe64e22013-01-14 03:46:50 +00002468 /**
2469 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2470 * @mtd: MTD device structure
2471 * @chip: nand chip info structure
2472 * @addr: feature address.
2473 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01002474 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002475static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2476 int addr, uint8_t *subfeature_param)
2477{
2478 int status;
2479
2480 if (!chip->onfi_version)
2481 return -EINVAL;
2482
2483 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2484 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2485 status = chip->waitfunc(mtd, chip);
2486 if (status & NAND_STATUS_FAIL)
2487 return -EIO;
2488 return 0;
2489}
2490
2491/**
2492 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2493 * @mtd: MTD device structure
2494 * @chip: nand chip info structure
2495 * @addr: feature address.
2496 * @subfeature_param: the subfeature parameters, a four bytes array.
2497 */
2498static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2499 int addr, uint8_t *subfeature_param)
2500{
2501 if (!chip->onfi_version)
2502 return -EINVAL;
2503
2504 /* clear the sub feature parameters */
2505 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2506
2507 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2508 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2509 return 0;
2510}
2511
2512/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01002513static void nand_set_defaults(struct nand_chip *chip, int busw)
2514{
2515 /* check for proper chip_delay setup, set 20us if not */
2516 if (!chip->chip_delay)
2517 chip->chip_delay = 20;
2518
2519 /* check, if a user supplied command function given */
2520 if (chip->cmdfunc == NULL)
2521 chip->cmdfunc = nand_command;
2522
2523 /* check, if a user supplied wait function given */
2524 if (chip->waitfunc == NULL)
2525 chip->waitfunc = nand_wait;
2526
2527 if (!chip->select_chip)
2528 chip->select_chip = nand_select_chip;
2529 if (!chip->read_byte)
2530 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2531 if (!chip->read_word)
2532 chip->read_word = nand_read_word;
2533 if (!chip->block_bad)
2534 chip->block_bad = nand_block_bad;
2535 if (!chip->block_markbad)
2536 chip->block_markbad = nand_default_block_markbad;
2537 if (!chip->write_buf)
2538 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2539 if (!chip->read_buf)
2540 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2541 if (!chip->verify_buf)
2542 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2543 if (!chip->scan_bbt)
2544 chip->scan_bbt = nand_default_bbt;
Scott Wood5b8e6bb2010-08-25 17:42:49 -05002545 if (!chip->controller)
William Juulcfa460a2007-10-31 13:53:06 +01002546 chip->controller = &chip->hwcontrol;
William Juulcfa460a2007-10-31 13:53:06 +01002547}
2548
Florian Fainelli0272c712011-02-25 00:01:34 +00002549#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Sergey Lapindfe64e22013-01-14 03:46:50 +00002550/* Sanitize ONFI strings so we can safely print them */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002551static void sanitize_string(char *s, size_t len)
2552{
2553 ssize_t i;
2554
Sergey Lapindfe64e22013-01-14 03:46:50 +00002555 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002556 s[len - 1] = 0;
2557
Sergey Lapindfe64e22013-01-14 03:46:50 +00002558 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002559 for (i = 0; i < len - 1; i++) {
2560 if (s[i] < ' ' || s[i] > 127)
2561 s[i] = '?';
2562 }
2563
Sergey Lapindfe64e22013-01-14 03:46:50 +00002564 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002565 strim(s);
2566}
2567
Florian Fainelli0272c712011-02-25 00:01:34 +00002568static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01002569{
Florian Fainelli0272c712011-02-25 00:01:34 +00002570 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00002571 while (len--) {
2572 crc ^= *p++ << 8;
2573 for (i = 0; i < 8; i++)
2574 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05002575 }
2576
Florian Fainelli0272c712011-02-25 00:01:34 +00002577 return crc;
2578}
William Juulcfa460a2007-10-31 13:53:06 +01002579
Florian Fainelli0272c712011-02-25 00:01:34 +00002580/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002581 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00002582 */
Christian Hitz90e3f392011-10-12 09:32:01 +02002583static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00002584 int *busw)
2585{
2586 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05302587 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00002588 int val;
2589
Sergey Lapindfe64e22013-01-14 03:46:50 +00002590 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00002591 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2592 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2593 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2594 return 0;
2595
Florian Fainelli0272c712011-02-25 00:01:34 +00002596 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2597 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05302598 for (j = 0; j < sizeof(*p); j++)
2599 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00002600 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02002601 le16_to_cpu(p->crc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002602 pr_info("ONFI param page %d valid\n", i);
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002603 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00002604 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02002605 }
William Juulcfa460a2007-10-31 13:53:06 +01002606
Florian Fainelli0272c712011-02-25 00:01:34 +00002607 if (i == 3)
2608 return 0;
2609
Sergey Lapindfe64e22013-01-14 03:46:50 +00002610 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00002611 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002612 if (val & (1 << 5))
2613 chip->onfi_version = 23;
2614 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00002615 chip->onfi_version = 22;
2616 else if (val & (1 << 3))
2617 chip->onfi_version = 21;
2618 else if (val & (1 << 2))
2619 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002620 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00002621 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002622 else
2623 chip->onfi_version = 0;
2624
2625 if (!chip->onfi_version) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002626 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002627 return 0;
2628 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002629
Christian Hitz5454ddb2011-10-12 09:32:05 +02002630 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2631 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01002632 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00002633 mtd->name = p->model;
Florian Fainelli0272c712011-02-25 00:01:34 +00002634 mtd->writesize = le32_to_cpu(p->byte_per_page);
2635 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2636 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTETd62e9ca2012-03-19 15:35:25 +01002637 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2638 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Florian Fainelli0272c712011-02-25 00:01:34 +00002639 *busw = 0;
2640 if (le16_to_cpu(p->features) & 1)
2641 *busw = NAND_BUSWIDTH_16;
William Juulcfa460a2007-10-31 13:53:06 +01002642
Sergey Lapindfe64e22013-01-14 03:46:50 +00002643 pr_info("ONFI flash detected\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00002644 return 1;
2645}
2646#else
2647static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2648 struct nand_chip *chip,
2649 int *busw)
2650{
2651 return 0;
2652}
2653#endif
2654
Florian Fainelli0272c712011-02-25 00:01:34 +00002655/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002656 * nand_id_has_period - Check if an ID string has a given wraparound period
2657 * @id_data: the ID string
2658 * @arrlen: the length of the @id_data array
2659 * @period: the period of repitition
2660 *
2661 * Check if an ID string is repeated within a given sequence of bytes at
2662 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2663 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2664 * if the repetition has a period of @period; otherwise, returns zero.
2665 */
2666static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2667{
2668 int i, j;
2669 for (i = 0; i < period; i++)
2670 for (j = i + period; j < arrlen; j += period)
2671 if (id_data[i] != id_data[j])
2672 return 0;
2673 return 1;
2674}
2675
2676/*
2677 * nand_id_len - Get the length of an ID string returned by CMD_READID
2678 * @id_data: the ID string
2679 * @arrlen: the length of the @id_data array
2680
2681 * Returns the length of the ID string, according to known wraparound/trailing
2682 * zero patterns. If no pattern exists, returns the length of the array.
2683 */
2684static int nand_id_len(u8 *id_data, int arrlen)
2685{
2686 int last_nonzero, period;
2687
2688 /* Find last non-zero byte */
2689 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2690 if (id_data[last_nonzero])
2691 break;
2692
2693 /* All zeros */
2694 if (last_nonzero < 0)
2695 return 0;
2696
2697 /* Calculate wraparound period */
2698 for (period = 1; period < arrlen; period++)
2699 if (nand_id_has_period(id_data, arrlen, period))
2700 break;
2701
2702 /* There's a repeated pattern */
2703 if (period < arrlen)
2704 return period;
2705
2706 /* There are trailing zeros */
2707 if (last_nonzero < arrlen - 1)
2708 return last_nonzero + 1;
2709
2710 /* No pattern detected */
2711 return arrlen;
2712}
2713
2714/*
2715 * Many new NAND share similar device ID codes, which represent the size of the
2716 * chip. The rest of the parameters must be decoded according to generic or
2717 * manufacturer-specific "extended ID" decoding patterns.
2718 */
2719static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2720 u8 id_data[8], int *busw)
2721{
2722 int extid, id_len;
2723 /* The 3rd id byte holds MLC / multichip data */
2724 chip->cellinfo = id_data[2];
2725 /* The 4th id byte is the important one */
2726 extid = id_data[3];
2727
2728 id_len = nand_id_len(id_data, 8);
2729
2730 /*
2731 * Field definitions are in the following datasheets:
2732 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2733 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
2734 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
2735 *
2736 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2737 * ID to decide what to do.
2738 */
2739 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
2740 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2741 id_data[5] != 0x00) {
2742 /* Calc pagesize */
2743 mtd->writesize = 2048 << (extid & 0x03);
2744 extid >>= 2;
2745 /* Calc oobsize */
2746 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2747 case 1:
2748 mtd->oobsize = 128;
2749 break;
2750 case 2:
2751 mtd->oobsize = 218;
2752 break;
2753 case 3:
2754 mtd->oobsize = 400;
2755 break;
2756 case 4:
2757 mtd->oobsize = 436;
2758 break;
2759 case 5:
2760 mtd->oobsize = 512;
2761 break;
2762 case 6:
2763 default: /* Other cases are "reserved" (unknown) */
2764 mtd->oobsize = 640;
2765 break;
2766 }
2767 extid >>= 2;
2768 /* Calc blocksize */
2769 mtd->erasesize = (128 * 1024) <<
2770 (((extid >> 1) & 0x04) | (extid & 0x03));
2771 *busw = 0;
2772 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2773 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2774 unsigned int tmp;
2775
2776 /* Calc pagesize */
2777 mtd->writesize = 2048 << (extid & 0x03);
2778 extid >>= 2;
2779 /* Calc oobsize */
2780 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2781 case 0:
2782 mtd->oobsize = 128;
2783 break;
2784 case 1:
2785 mtd->oobsize = 224;
2786 break;
2787 case 2:
2788 mtd->oobsize = 448;
2789 break;
2790 case 3:
2791 mtd->oobsize = 64;
2792 break;
2793 case 4:
2794 mtd->oobsize = 32;
2795 break;
2796 case 5:
2797 mtd->oobsize = 16;
2798 break;
2799 default:
2800 mtd->oobsize = 640;
2801 break;
2802 }
2803 extid >>= 2;
2804 /* Calc blocksize */
2805 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
2806 if (tmp < 0x03)
2807 mtd->erasesize = (128 * 1024) << tmp;
2808 else if (tmp == 0x03)
2809 mtd->erasesize = 768 * 1024;
2810 else
2811 mtd->erasesize = (64 * 1024) << tmp;
2812 *busw = 0;
2813 } else {
2814 /* Calc pagesize */
2815 mtd->writesize = 1024 << (extid & 0x03);
2816 extid >>= 2;
2817 /* Calc oobsize */
2818 mtd->oobsize = (8 << (extid & 0x01)) *
2819 (mtd->writesize >> 9);
2820 extid >>= 2;
2821 /* Calc blocksize. Blocksize is multiples of 64KiB */
2822 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2823 extid >>= 2;
2824 /* Get buswidth information */
2825 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2826 }
2827}
2828
2829 /*
2830 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
2831 * decodes a matching ID table entry and assigns the MTD size parameters for
2832 * the chip.
2833 */
2834static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
2835 const struct nand_flash_dev *type, u8 id_data[8],
2836 int *busw)
2837{
2838 int maf_id = id_data[0];
2839
2840 mtd->erasesize = type->erasesize;
2841 mtd->writesize = type->pagesize;
2842 mtd->oobsize = mtd->writesize / 32;
2843 *busw = type->options & NAND_BUSWIDTH_16;
2844
2845 /*
2846 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
2847 * some Spansion chips have erasesize that conflicts with size
2848 * listed in nand_ids table.
2849 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
2850 */
2851 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
2852 && id_data[6] == 0x00 && id_data[7] == 0x00
2853 && mtd->writesize == 512) {
2854 mtd->erasesize = 128 * 1024;
2855 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
2856 }
2857}
2858
2859 /*
2860 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
2861 * heuristic patterns using various detected parameters (e.g., manufacturer,
2862 * page size, cell-type information).
2863 */
2864static void nand_decode_bbm_options(struct mtd_info *mtd,
2865 struct nand_chip *chip, u8 id_data[8])
2866{
2867 int maf_id = id_data[0];
2868
2869 /* Set the bad block position */
2870 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
2871 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2872 else
2873 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2874
2875 /*
2876 * Bad block marker is stored in the last page of each block on Samsung
2877 * and Hynix MLC devices; stored in first two pages of each block on
2878 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
2879 * AMD/Spansion, and Macronix. All others scan only the first page.
2880 */
2881 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2882 (maf_id == NAND_MFR_SAMSUNG ||
2883 maf_id == NAND_MFR_HYNIX))
2884 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
2885 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2886 (maf_id == NAND_MFR_SAMSUNG ||
2887 maf_id == NAND_MFR_HYNIX ||
2888 maf_id == NAND_MFR_TOSHIBA ||
2889 maf_id == NAND_MFR_AMD ||
2890 maf_id == NAND_MFR_MACRONIX)) ||
2891 (mtd->writesize == 2048 &&
2892 maf_id == NAND_MFR_MICRON))
2893 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
2894}
2895
2896/*
2897 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00002898 */
2899static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2900 struct nand_chip *chip,
2901 int busw,
2902 int *maf_id, int *dev_id,
2903 const struct nand_flash_dev *type)
2904{
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00002905 const char *name;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002906 int i, maf_idx;
2907 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00002908
2909 /* Select the device */
2910 chip->select_chip(mtd, 0);
2911
2912 /*
2913 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002914 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00002915 */
2916 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2917
2918 /* Send the command for reading device ID */
2919 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2920
2921 /* Read manufacturer and device IDs */
2922 *maf_id = chip->read_byte(mtd);
2923 *dev_id = chip->read_byte(mtd);
2924
Sergey Lapindfe64e22013-01-14 03:46:50 +00002925 /*
2926 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00002927 * interface concerns can cause random data which looks like a
2928 * possibly credible NAND flash to appear. If the two results do
2929 * not match, ignore the device completely.
2930 */
2931
2932 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2933
Sergey Lapindfe64e22013-01-14 03:46:50 +00002934 /* Read entire ID string */
2935 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002936 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00002937
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002938 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002939 pr_info("%s: second ID read did not match "
2940 "%02x,%02x against %02x,%02x\n", __func__,
2941 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00002942 return ERR_PTR(-ENODEV);
2943 }
2944
2945 if (!type)
2946 type = nand_flash_ids;
2947
2948 for (; type->name != NULL; type++)
2949 if (*dev_id == type->id)
2950 break;
2951
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002952 chip->onfi_version = 0;
2953 if (!type->name || !type->pagesize) {
2954 /* Check is chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002955 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002956 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00002957 }
2958
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002959 if (!type->name)
2960 return ERR_PTR(-ENODEV);
2961
Florian Fainelli0272c712011-02-25 00:01:34 +00002962 if (!mtd->name)
2963 mtd->name = type->name;
2964
2965 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00002966
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002967 if (!type->pagesize && chip->init_size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002968 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002969 busw = chip->init_size(mtd, chip, id_data);
2970 } else if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002971 /* Decode parameters from extended ID */
2972 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002973 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002974 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002975 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002976 /* Get chip options, preserve non chip based options */
Marek Vasut9c790a72012-08-30 13:39:38 +00002977 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00002978
Sergey Lapindfe64e22013-01-14 03:46:50 +00002979 /*
2980 * Check if chip is not a Samsung device. Do not clear the
2981 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002982 */
2983 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2984 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2985ident_done:
2986
William Juulcfa460a2007-10-31 13:53:06 +01002987 /* Try to identify manufacturer */
2988 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2989 if (nand_manuf_ids[maf_idx].id == *maf_id)
2990 break;
2991 }
2992
2993 /*
2994 * Check, if buswidth is correct. Hardware drivers should set
Sergey Lapindfe64e22013-01-14 03:46:50 +00002995 * chip correct!
William Juulcfa460a2007-10-31 13:53:06 +01002996 */
2997 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002998 pr_info("NAND device: Manufacturer ID:"
2999 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3000 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
3001 pr_warn("NAND bus width %d instead %d bit\n",
3002 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3003 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003004 return ERR_PTR(-EINVAL);
3005 }
3006
Sergey Lapindfe64e22013-01-14 03:46:50 +00003007 nand_decode_bbm_options(mtd, chip, id_data);
3008
William Juulcfa460a2007-10-31 13:53:06 +01003009 /* Calculate the address shift from the page size */
3010 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003011 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003012 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3013
3014 chip->bbt_erase_shift = chip->phys_erase_shift =
3015 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003016 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003017 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003018 else {
3019 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3020 chip->chip_shift += 32 - 1;
3021 }
3022
3023 chip->badblockbits = 8;
William Juulcfa460a2007-10-31 13:53:06 +01003024
William Juulcfa460a2007-10-31 13:53:06 +01003025 /* Check for AND chips with 4 page planes */
3026 if (chip->options & NAND_4PAGE_ARRAY)
3027 chip->erase_cmd = multi_erase_cmd;
3028 else
3029 chip->erase_cmd = single_erase_cmd;
3030
Sergey Lapindfe64e22013-01-14 03:46:50 +00003031 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003032 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3033 chip->cmdfunc = nand_command_lp;
3034
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00003035 name = type->name;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003036#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00003037 if (chip->onfi_version)
3038 name = chip->onfi_params.model;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003039#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003040 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3041 " page size: %d, OOB size: %d\n",
3042 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3043 name,
3044 mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003045
3046 return type;
3047}
3048
3049/**
3050 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003051 * @mtd: MTD device structure
3052 * @maxchips: number of chips to scan for
3053 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003054 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003055 * This is the first phase of the normal nand_scan() function. It reads the
3056 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003057 *
3058 * The mtd->owner field must be set to the module of the caller.
3059 */
Lei Wen245eb902011-01-06 09:48:18 +08003060int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3061 const struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003062{
Florian Fainelli0272c712011-02-25 00:01:34 +00003063 int i, busw, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01003064 struct nand_chip *chip = mtd->priv;
Mike Frysinger0bdecd82010-10-20 01:15:21 +00003065 const struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003066
3067 /* Get buswidth to select the correct functions */
3068 busw = chip->options & NAND_BUSWIDTH_16;
3069 /* Set the default functions */
3070 nand_set_defaults(chip, busw);
3071
3072 /* Read the flash type */
Christian Hitz90e3f392011-10-12 09:32:01 +02003073 type = nand_get_flash_type(mtd, chip, busw,
3074 &nand_maf_id, &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003075
3076 if (IS_ERR(type)) {
Peter Tyser10dc6a92009-02-04 13:39:40 -06003077#ifndef CONFIG_SYS_NAND_QUIET_TEST
Sergey Lapindfe64e22013-01-14 03:46:50 +00003078 pr_warn("No NAND device found\n");
Peter Tyser10dc6a92009-02-04 13:39:40 -06003079#endif
William Juulcfa460a2007-10-31 13:53:06 +01003080 chip->select_chip(mtd, -1);
3081 return PTR_ERR(type);
3082 }
3083
3084 /* Check for a chip array */
3085 for (i = 1; i < maxchips; i++) {
3086 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003087 /* See comment in nand_get_flash_type for reset */
3088 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003089 /* Send the command for reading device ID */
3090 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3091 /* Read manufacturer and device IDs */
3092 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainelli0272c712011-02-25 00:01:34 +00003093 nand_dev_id != chip->read_byte(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01003094 break;
3095 }
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003096#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003097 if (i > 1)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003098 pr_info("%d NAND chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003099#endif
William Juulcfa460a2007-10-31 13:53:06 +01003100
3101 /* Store the number of chips and calc total size for mtd */
3102 chip->numchips = i;
3103 mtd->size = i * chip->chipsize;
3104
3105 return 0;
3106}
3107
3108
3109/**
3110 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003111 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003112 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003113 * This is the second phase of the normal nand_scan() function. It fills out
3114 * all the uninitialized function pointers with the defaults and scans for a
3115 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003116 */
3117int nand_scan_tail(struct mtd_info *mtd)
3118{
3119 int i;
3120 struct nand_chip *chip = mtd->priv;
3121
Sergey Lapindfe64e22013-01-14 03:46:50 +00003122 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3123 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3124 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3125
William Juulcfa460a2007-10-31 13:53:06 +01003126 if (!(chip->options & NAND_OWN_BUFFERS))
Simon Glassb5725952012-07-29 20:53:25 +00003127 chip->buffers = memalign(ARCH_DMA_MINALIGN,
3128 sizeof(*chip->buffers));
William Juulcfa460a2007-10-31 13:53:06 +01003129 if (!chip->buffers)
3130 return -ENOMEM;
3131
3132 /* Set the internal oob buffer location, just after the page data */
3133 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3134
3135 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003136 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01003137 */
Christian Hitz4c6de852011-10-12 09:31:59 +02003138 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01003139 switch (mtd->oobsize) {
3140 case 8:
3141 chip->ecc.layout = &nand_oob_8;
3142 break;
3143 case 16:
3144 chip->ecc.layout = &nand_oob_16;
3145 break;
3146 case 64:
3147 chip->ecc.layout = &nand_oob_64;
3148 break;
3149 case 128:
3150 chip->ecc.layout = &nand_oob_128;
3151 break;
3152 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003153 pr_warn("No oob scheme defined for oobsize %d\n",
3154 mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003155 }
3156 }
3157
3158 if (!chip->write_page)
3159 chip->write_page = nand_write_page;
3160
Sergey Lapindfe64e22013-01-14 03:46:50 +00003161 /* set for ONFI nand */
3162 if (!chip->onfi_set_features)
3163 chip->onfi_set_features = nand_onfi_set_features;
3164 if (!chip->onfi_get_features)
3165 chip->onfi_get_features = nand_onfi_get_features;
3166
William Juulcfa460a2007-10-31 13:53:06 +01003167 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003168 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01003169 * selected and we have 256 byte pagesize fallback to software ECC
3170 */
William Juulcfa460a2007-10-31 13:53:06 +01003171
3172 switch (chip->ecc.mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003173 case NAND_ECC_HW_OOB_FIRST:
3174 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3175 if (!chip->ecc.calculate || !chip->ecc.correct ||
3176 !chip->ecc.hwctl) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003177 pr_warn("No ECC functions supplied; "
3178 "hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003179 BUG();
3180 }
3181 if (!chip->ecc.read_page)
3182 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3183
William Juulcfa460a2007-10-31 13:53:06 +01003184 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003185 /* Use standard hwecc read page function? */
William Juulcfa460a2007-10-31 13:53:06 +01003186 if (!chip->ecc.read_page)
3187 chip->ecc.read_page = nand_read_page_hwecc;
3188 if (!chip->ecc.write_page)
3189 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell7e866612009-11-07 16:27:01 -05003190 if (!chip->ecc.read_page_raw)
3191 chip->ecc.read_page_raw = nand_read_page_raw;
3192 if (!chip->ecc.write_page_raw)
3193 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003194 if (!chip->ecc.read_oob)
3195 chip->ecc.read_oob = nand_read_oob_std;
3196 if (!chip->ecc.write_oob)
3197 chip->ecc.write_oob = nand_write_oob_std;
3198
3199 case NAND_ECC_HW_SYNDROME:
Scott Wood41ef8c72008-03-18 15:29:14 -05003200 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3201 !chip->ecc.hwctl) &&
3202 (!chip->ecc.read_page ||
3203 chip->ecc.read_page == nand_read_page_hwecc ||
3204 !chip->ecc.write_page ||
3205 chip->ecc.write_page == nand_write_page_hwecc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003206 pr_warn("No ECC functions supplied; "
3207 "hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01003208 BUG();
3209 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00003210 /* Use standard syndrome read/write page function? */
William Juulcfa460a2007-10-31 13:53:06 +01003211 if (!chip->ecc.read_page)
3212 chip->ecc.read_page = nand_read_page_syndrome;
3213 if (!chip->ecc.write_page)
3214 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell7e866612009-11-07 16:27:01 -05003215 if (!chip->ecc.read_page_raw)
3216 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3217 if (!chip->ecc.write_page_raw)
3218 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01003219 if (!chip->ecc.read_oob)
3220 chip->ecc.read_oob = nand_read_oob_syndrome;
3221 if (!chip->ecc.write_oob)
3222 chip->ecc.write_oob = nand_write_oob_syndrome;
3223
Sergey Lapindfe64e22013-01-14 03:46:50 +00003224 if (mtd->writesize >= chip->ecc.size) {
3225 if (!chip->ecc.strength) {
3226 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3227 BUG();
3228 }
William Juulcfa460a2007-10-31 13:53:06 +01003229 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003230 }
3231 pr_warn("%d byte HW ECC not possible on "
3232 "%d byte page size, fallback to SW ECC\n",
3233 chip->ecc.size, mtd->writesize);
William Juulcfa460a2007-10-31 13:53:06 +01003234 chip->ecc.mode = NAND_ECC_SOFT;
3235
3236 case NAND_ECC_SOFT:
3237 chip->ecc.calculate = nand_calculate_ecc;
3238 chip->ecc.correct = nand_correct_data;
3239 chip->ecc.read_page = nand_read_page_swecc;
Scott Woodc45912d2008-10-24 16:20:43 -05003240 chip->ecc.read_subpage = nand_read_subpage;
William Juulcfa460a2007-10-31 13:53:06 +01003241 chip->ecc.write_page = nand_write_page_swecc;
David Brownell7e866612009-11-07 16:27:01 -05003242 chip->ecc.read_page_raw = nand_read_page_raw;
3243 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003244 chip->ecc.read_oob = nand_read_oob_std;
3245 chip->ecc.write_oob = nand_write_oob_std;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003246 if (!chip->ecc.size)
3247 chip->ecc.size = 256;
William Juulcfa460a2007-10-31 13:53:06 +01003248 chip->ecc.bytes = 3;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003249 chip->ecc.strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01003250 break;
3251
Christian Hitz4c6de852011-10-12 09:31:59 +02003252 case NAND_ECC_SOFT_BCH:
3253 if (!mtd_nand_has_bch()) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003254 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Christian Hitz4c6de852011-10-12 09:31:59 +02003255 return -EINVAL;
3256 }
3257 chip->ecc.calculate = nand_bch_calculate_ecc;
3258 chip->ecc.correct = nand_bch_correct_data;
3259 chip->ecc.read_page = nand_read_page_swecc;
3260 chip->ecc.read_subpage = nand_read_subpage;
3261 chip->ecc.write_page = nand_write_page_swecc;
3262 chip->ecc.read_page_raw = nand_read_page_raw;
3263 chip->ecc.write_page_raw = nand_write_page_raw;
3264 chip->ecc.read_oob = nand_read_oob_std;
3265 chip->ecc.write_oob = nand_write_oob_std;
3266 /*
3267 * Board driver should supply ecc.size and ecc.bytes values to
3268 * select how many bits are correctable; see nand_bch_init()
Sergey Lapindfe64e22013-01-14 03:46:50 +00003269 * for details. Otherwise, default to 4 bits for large page
3270 * devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02003271 */
3272 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3273 chip->ecc.size = 512;
3274 chip->ecc.bytes = 7;
3275 }
3276 chip->ecc.priv = nand_bch_init(mtd,
3277 chip->ecc.size,
3278 chip->ecc.bytes,
3279 &chip->ecc.layout);
3280 if (!chip->ecc.priv)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003281 pr_warn("BCH ECC initialization failed!\n");
3282 chip->ecc.strength =
3283 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Christian Hitz4c6de852011-10-12 09:31:59 +02003284 break;
3285
William Juulcfa460a2007-10-31 13:53:06 +01003286 case NAND_ECC_NONE:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003287 pr_warn("NAND_ECC_NONE selected by board driver. "
Wolfgang Denk93e14592013-10-04 17:43:24 +02003288 "This is not recommended !!\n");
William Juulcfa460a2007-10-31 13:53:06 +01003289 chip->ecc.read_page = nand_read_page_raw;
3290 chip->ecc.write_page = nand_write_page_raw;
3291 chip->ecc.read_oob = nand_read_oob_std;
David Brownell7e866612009-11-07 16:27:01 -05003292 chip->ecc.read_page_raw = nand_read_page_raw;
3293 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003294 chip->ecc.write_oob = nand_write_oob_std;
3295 chip->ecc.size = mtd->writesize;
3296 chip->ecc.bytes = 0;
3297 break;
3298
3299 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003300 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
William Juulcfa460a2007-10-31 13:53:06 +01003301 BUG();
3302 }
3303
Sergey Lapindfe64e22013-01-14 03:46:50 +00003304 /* For many systems, the standard OOB write also works for raw */
3305 if (!chip->ecc.read_oob_raw)
3306 chip->ecc.read_oob_raw = chip->ecc.read_oob;
3307 if (!chip->ecc.write_oob_raw)
3308 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3309
William Juulcfa460a2007-10-31 13:53:06 +01003310 /*
3311 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00003312 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01003313 */
3314 chip->ecc.layout->oobavail = 0;
Sandeep Paulraj5df3c2b2009-11-07 14:25:18 -05003315 for (i = 0; chip->ecc.layout->oobfree[i].length
3316 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juulcfa460a2007-10-31 13:53:06 +01003317 chip->ecc.layout->oobavail +=
3318 chip->ecc.layout->oobfree[i].length;
3319 mtd->oobavail = chip->ecc.layout->oobavail;
3320
3321 /*
3322 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00003323 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01003324 */
3325 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Christian Hitz90e3f392011-10-12 09:32:01 +02003326 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003327 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01003328 BUG();
3329 }
3330 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
3331
Sergey Lapindfe64e22013-01-14 03:46:50 +00003332 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
William Juulcfa460a2007-10-31 13:53:06 +01003333 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3334 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02003335 switch (chip->ecc.steps) {
William Juulcfa460a2007-10-31 13:53:06 +01003336 case 2:
3337 mtd->subpage_sft = 1;
3338 break;
3339 case 4:
3340 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05003341 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01003342 mtd->subpage_sft = 2;
3343 break;
3344 }
3345 }
3346 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3347
3348 /* Initialize state */
3349 chip->state = FL_READY;
3350
3351 /* De-select the device */
3352 chip->select_chip(mtd, -1);
3353
3354 /* Invalidate the pagebuffer reference */
3355 chip->pagebuf = -1;
3356
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00003357 /* Large page NAND with SOFT_ECC should support subpage reads */
3358 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3359 chip->options |= NAND_SUBPAGE_READ;
3360
William Juulcfa460a2007-10-31 13:53:06 +01003361 /* Fill in remaining MTD driver data */
3362 mtd->type = MTD_NANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003363 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3364 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003365 mtd->_erase = nand_erase;
3366 mtd->_point = NULL;
3367 mtd->_unpoint = NULL;
3368 mtd->_read = nand_read;
3369 mtd->_write = nand_write;
3370 mtd->_read_oob = nand_read_oob;
3371 mtd->_write_oob = nand_write_oob;
3372 mtd->_sync = nand_sync;
3373 mtd->_lock = NULL;
3374 mtd->_unlock = NULL;
3375 mtd->_block_isbad = nand_block_isbad;
3376 mtd->_block_markbad = nand_block_markbad;
William Juulcfa460a2007-10-31 13:53:06 +01003377
Sergey Lapindfe64e22013-01-14 03:46:50 +00003378 /* propagate ecc info to mtd_info */
William Juulcfa460a2007-10-31 13:53:06 +01003379 mtd->ecclayout = chip->ecc.layout;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003380 mtd->ecc_strength = chip->ecc.strength;
3381 /*
3382 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3383 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3384 * properly set.
3385 */
3386 if (!mtd->bitflip_threshold)
3387 mtd->bitflip_threshold = mtd->ecc_strength;
William Juulcfa460a2007-10-31 13:53:06 +01003388
3389 /* Check, if we should skip the bad block table scan */
3390 if (chip->options & NAND_SKIP_BBTSCAN)
Scott Woodfb494542012-02-20 14:50:39 -06003391 chip->options |= NAND_BBT_SCANNED;
William Juulcfa460a2007-10-31 13:53:06 +01003392
Scott Woodfb494542012-02-20 14:50:39 -06003393 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01003394}
3395
William Juulcfa460a2007-10-31 13:53:06 +01003396/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02003397 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003398 * @mtd: MTD device structure
3399 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02003400 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003401 * This fills out all the uninitialized function pointers with the defaults.
3402 * The flash ID is read and the mtd/chip structures are filled with the
3403 * appropriate values. The mtd->owner field must be set to the module of the
3404 * caller.
Wolfgang Denk932394a2005-08-17 12:55:25 +02003405 */
William Juulcfa460a2007-10-31 13:53:06 +01003406int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003407{
William Juulcfa460a2007-10-31 13:53:06 +01003408 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003409
Lei Wen245eb902011-01-06 09:48:18 +08003410 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01003411 if (!ret)
3412 ret = nand_scan_tail(mtd);
3413 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003414}
3415
3416/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02003417 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003418 * @mtd: MTD device structure
3419 */
William Juulcfa460a2007-10-31 13:53:06 +01003420void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003421{
William Juulcfa460a2007-10-31 13:53:06 +01003422 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003423
Christian Hitz4c6de852011-10-12 09:31:59 +02003424 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3425 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3426
Wolfgang Denk932394a2005-08-17 12:55:25 +02003427#ifdef CONFIG_MTD_PARTITIONS
3428 /* Deregister partitions */
William Juulcfa460a2007-10-31 13:53:06 +01003429 del_mtd_partitions(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003430#endif
William Juulcfa460a2007-10-31 13:53:06 +01003431
3432 /* Free bad block table memory */
3433 kfree(chip->bbt);
3434 if (!(chip->options & NAND_OWN_BUFFERS))
3435 kfree(chip->buffers);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003436
3437 /* Free bad block descriptor memory */
3438 if (chip->badblock_pattern && chip->badblock_pattern->options
3439 & NAND_BBT_DYNAMICSTRUCT)
3440 kfree(chip->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003441}