blob: 5d3232c634c60c16fa81c91c22a3ea4db1634fce [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02008 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02009 * Additional technical information is available on
Scott Woodc45912d2008-10-24 16:20:43 -050010 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020011 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juulcfa460a2007-10-31 13:53:06 +010013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020014 *
William Juulcfa460a2007-10-31 13:53:06 +010015 * Credits:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020016 * David Woodhouse for adding multichip support
17 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
William Juulcfa460a2007-10-31 13:53:06 +010021 * TODO:
Wolfgang Denk932394a2005-08-17 12:55:25 +020022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapindfe64e22013-01-14 03:46:50 +000024 * if we have HW ECC support.
Wolfgang Denk932394a2005-08-17 12:55:25 +020025 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Scott Woodc45912d2008-10-24 16:20:43 -050027 * BBT table is not serialized, has to be fixed
Wolfgang Denk932394a2005-08-17 12:55:25 +020028 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
Wolfgang Denk932394a2005-08-17 12:55:25 +020035#include <common.h>
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +010036
William Juulcfa460a2007-10-31 13:53:06 +010037#define ENOTSUPP 524 /* Operation is not supported */
38
Wolfgang Denk932394a2005-08-17 12:55:25 +020039#include <malloc.h>
40#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010041#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000042#include <linux/compat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020043#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/mtd/nand_ecc.h>
Christian Hitz4c6de852011-10-12 09:31:59 +020046#include <linux/mtd/nand_bch.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020047
Stefan Roese10bb62d2009-04-24 15:58:33 +020048#ifdef CONFIG_MTD_PARTITIONS
49#include <linux/mtd/partitions.h>
50#endif
51
Wolfgang Denk932394a2005-08-17 12:55:25 +020052#include <asm/io.h>
53#include <asm/errno.h>
54
Peter Tyser8da60122009-02-04 13:47:22 -060055/*
56 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
57 * a flash. NAND flash is initialized prior to interrupts so standard timers
58 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
59 * which is greater than (max NAND reset time / NAND status read time).
60 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
61 */
62#ifndef CONFIG_SYS_NAND_RESET_CNT
63#define CONFIG_SYS_NAND_RESET_CNT 200000
64#endif
65
Wolfgang Denk932394a2005-08-17 12:55:25 +020066/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010067static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020068 .eccbytes = 3,
69 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010070 .oobfree = {
71 {.offset = 3,
72 .length = 2},
73 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020074 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020075};
76
William Juulcfa460a2007-10-31 13:53:06 +010077static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020078 .eccbytes = 6,
79 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010080 .oobfree = {
81 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020082 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020083};
84
William Juulcfa460a2007-10-31 13:53:06 +010085static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020086 .eccbytes = 24,
87 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +010088 40, 41, 42, 43, 44, 45, 46, 47,
89 48, 49, 50, 51, 52, 53, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63},
91 .oobfree = {
92 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +020093 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020094};
95
William Juulcfa460a2007-10-31 13:53:06 +010096static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +020097 .eccbytes = 48,
98 .eccpos = {
Christian Hitz90e3f392011-10-12 09:32:01 +020099 80, 81, 82, 83, 84, 85, 86, 87,
100 88, 89, 90, 91, 92, 93, 94, 95,
101 96, 97, 98, 99, 100, 101, 102, 103,
William Juulcfa460a2007-10-31 13:53:06 +0100102 104, 105, 106, 107, 108, 109, 110, 111,
103 112, 113, 114, 115, 116, 117, 118, 119,
104 120, 121, 122, 123, 124, 125, 126, 127},
105 .oobfree = {
106 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200107 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200108};
109
William Juulcfa460a2007-10-31 13:53:06 +0100110static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
111 int new_state);
112
113static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
114 struct mtd_oob_ops *ops);
115
116static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200117
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200118static int check_offs_len(struct mtd_info *mtd,
119 loff_t ofs, uint64_t len)
120{
121 struct nand_chip *chip = mtd->priv;
122 int ret = 0;
123
124 /* Start address must align on block boundary */
125 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
126 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
127 ret = -EINVAL;
128 }
129
130 /* Length must align on block boundary */
131 if (len & ((1 << chip->phys_erase_shift) - 1)) {
132 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
133 __func__);
134 ret = -EINVAL;
135 }
136
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200137 return ret;
138}
139
Wolfgang Denk932394a2005-08-17 12:55:25 +0200140/**
141 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000142 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200143 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000144 * Deselect, release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200145 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200146static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100147{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200148 struct nand_chip *chip = mtd->priv;
149
150 /* De-select the NAND device */
151 chip->select_chip(mtd, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100152}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200153
154/**
155 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000156 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200157 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000158 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200159 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000160uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200161{
William Juulcfa460a2007-10-31 13:53:06 +0100162 struct nand_chip *chip = mtd->priv;
163 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200164}
165
166/**
167 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000168 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
169 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200170 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000171 * Default read function for 16bit buswidth with endianness conversion.
172 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200173 */
William Juulcfa460a2007-10-31 13:53:06 +0100174static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200175{
William Juulcfa460a2007-10-31 13:53:06 +0100176 struct nand_chip *chip = mtd->priv;
177 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200178}
179
180/**
181 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000182 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200183 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000184 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200185 */
186static u16 nand_read_word(struct mtd_info *mtd)
187{
William Juulcfa460a2007-10-31 13:53:06 +0100188 struct nand_chip *chip = mtd->priv;
189 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200190}
191
192/**
193 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000194 * @mtd: MTD device structure
195 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200196 *
197 * Default select function for 1 chip devices.
198 */
William Juulcfa460a2007-10-31 13:53:06 +0100199static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200200{
William Juulcfa460a2007-10-31 13:53:06 +0100201 struct nand_chip *chip = mtd->priv;
202
203 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200204 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100205 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200206 break;
207 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200208 break;
209
210 default:
211 BUG();
212 }
213}
214
215/**
216 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000217 * @mtd: MTD device structure
218 * @buf: data buffer
219 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200220 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000221 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200222 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000223void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200224{
225 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100226 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200227
William Juulcfa460a2007-10-31 13:53:06 +0100228 for (i = 0; i < len; i++)
229 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200230}
231
232/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200233 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000234 * @mtd: MTD device structure
235 * @buf: buffer to store date
236 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200237 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000238 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200239 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400240void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200241{
242 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100243 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200244
William Juulcfa460a2007-10-31 13:53:06 +0100245 for (i = 0; i < len; i++)
246 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200247}
248
249/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200250 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200254 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000255 * Default verify function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200256 */
William Juulcfa460a2007-10-31 13:53:06 +0100257static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200258{
259 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100260 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200261
William Juulcfa460a2007-10-31 13:53:06 +0100262 for (i = 0; i < len; i++)
263 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200264 return -EFAULT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200265 return 0;
266}
267
268/**
269 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000270 * @mtd: MTD device structure
271 * @buf: data buffer
272 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200273 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000274 * Default write function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200275 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000276void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200277{
278 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100279 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200280 u16 *p = (u16 *) buf;
281 len >>= 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200282
William Juulcfa460a2007-10-31 13:53:06 +0100283 for (i = 0; i < len; i++)
284 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200285
Wolfgang Denk932394a2005-08-17 12:55:25 +0200286}
287
288/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200289 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000290 * @mtd: MTD device structure
291 * @buf: buffer to store date
292 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200293 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000294 * Default read function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200295 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400296void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200297{
298 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100299 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200300 u16 *p = (u16 *) buf;
301 len >>= 1;
302
William Juulcfa460a2007-10-31 13:53:06 +0100303 for (i = 0; i < len; i++)
304 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200305}
306
307/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200308 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000309 * @mtd: MTD device structure
310 * @buf: buffer containing the data to compare
311 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200312 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000313 * Default verify function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200314 */
William Juulcfa460a2007-10-31 13:53:06 +0100315static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200316{
317 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100318 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200319 u16 *p = (u16 *) buf;
320 len >>= 1;
321
William Juulcfa460a2007-10-31 13:53:06 +0100322 for (i = 0; i < len; i++)
323 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200324 return -EFAULT;
325
326 return 0;
327}
328
329/**
330 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000331 * @mtd: MTD device structure
332 * @ofs: offset from device start
333 * @getchip: 0, if the chip is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200334 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200335 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200336 */
337static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
338{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000339 int page, chipnr, res = 0, i = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100340 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200341 u16 bad;
342
Sergey Lapindfe64e22013-01-14 03:46:50 +0000343 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200344 ofs += mtd->erasesize - mtd->writesize;
345
William Juulcfa460a2007-10-31 13:53:06 +0100346 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200347
Wolfgang Denk932394a2005-08-17 12:55:25 +0200348 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100349 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200350
William Juulcfa460a2007-10-31 13:53:06 +0100351 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200352
353 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100354 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200355 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200356
Sergey Lapindfe64e22013-01-14 03:46:50 +0000357 do {
358 if (chip->options & NAND_BUSWIDTH_16) {
359 chip->cmdfunc(mtd, NAND_CMD_READOOB,
360 chip->badblockpos & 0xFE, page);
361 bad = cpu_to_le16(chip->read_word(mtd));
362 if (chip->badblockpos & 0x1)
363 bad >>= 8;
364 else
365 bad &= 0xFF;
366 } else {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
368 page);
369 bad = chip->read_byte(mtd);
370 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200371
Sergey Lapindfe64e22013-01-14 03:46:50 +0000372 if (likely(chip->badblockbits == 8))
373 res = bad != 0xFF;
374 else
375 res = hweight8(bad) < chip->badblockbits;
376 ofs += mtd->writesize;
377 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
378 i++;
379 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200380
William Juulcfa460a2007-10-31 13:53:06 +0100381 if (getchip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200382 nand_release_device(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200383
Wolfgang Denk932394a2005-08-17 12:55:25 +0200384 return res;
385}
386
387/**
388 * nand_default_block_markbad - [DEFAULT] mark a block bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000389 * @mtd: MTD device structure
390 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200391 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000392 * This is the default implementation, which can be overridden by a hardware
393 * specific driver. We try operations in the following order, according to our
394 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
395 * (1) erase the affected block, to allow OOB marker to be written cleanly
396 * (2) update in-memory BBT
397 * (3) write bad block marker to OOB area of affected block
398 * (4) update flash-based BBT
399 * Note that we retain the first error encountered in (3) or (4), finish the
400 * procedures, and dump the error in the end.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200401*/
402static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
403{
William Juulcfa460a2007-10-31 13:53:06 +0100404 struct nand_chip *chip = mtd->priv;
405 uint8_t buf[2] = { 0, 0 };
Sergey Lapindfe64e22013-01-14 03:46:50 +0000406 int block, res, ret = 0, i = 0;
407 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200408
Sergey Lapindfe64e22013-01-14 03:46:50 +0000409 if (write_oob) {
410 struct erase_info einfo;
411
412 /* Attempt erase before marking OOB */
413 memset(&einfo, 0, sizeof(einfo));
414 einfo.mtd = mtd;
415 einfo.addr = ofs;
416 einfo.len = 1 << chip->phys_erase_shift;
417 nand_erase_nand(mtd, &einfo, 0);
418 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200419
Wolfgang Denk932394a2005-08-17 12:55:25 +0200420 /* Get block number */
William Juulcfa460a2007-10-31 13:53:06 +0100421 block = (int)(ofs >> chip->bbt_erase_shift);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000422 /* Mark block bad in memory-based BBT */
William Juulcfa460a2007-10-31 13:53:06 +0100423 if (chip->bbt)
424 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200425
Sergey Lapindfe64e22013-01-14 03:46:50 +0000426 /* Write bad block marker to OOB */
427 if (write_oob) {
428 struct mtd_oob_ops ops;
429 loff_t wr_ofs = ofs;
430
Scott Woodc45912d2008-10-24 16:20:43 -0500431 nand_get_device(chip, mtd, FL_WRITING);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200432
Sergey Lapindfe64e22013-01-14 03:46:50 +0000433 ops.datbuf = NULL;
434 ops.oobbuf = buf;
435 ops.ooboffs = chip->badblockpos;
436 if (chip->options & NAND_BUSWIDTH_16) {
437 ops.ooboffs &= ~0x01;
438 ops.len = ops.ooblen = 2;
439 } else {
440 ops.len = ops.ooblen = 1;
441 }
442 ops.mode = MTD_OPS_PLACE_OOB;
443
444 /* Write to first/last page(s) if necessary */
445 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
446 wr_ofs += mtd->erasesize - mtd->writesize;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200447 do {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000448 res = nand_do_write_oob(mtd, wr_ofs, &ops);
449 if (!ret)
450 ret = res;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200451
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200452 i++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000453 wr_ofs += mtd->writesize;
454 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200455
Scott Woodc45912d2008-10-24 16:20:43 -0500456 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100457 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000458
459 /* Update flash-based bad block table */
460 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
461 res = nand_update_bbt(mtd, ofs);
462 if (!ret)
463 ret = res;
464 }
465
William Juulcfa460a2007-10-31 13:53:06 +0100466 if (!ret)
467 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500468
William Juulcfa460a2007-10-31 13:53:06 +0100469 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200470}
471
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200472/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200473 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000474 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200475 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000476 * Check, if the device is write protected. The function expects, that the
477 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200478 */
William Juulcfa460a2007-10-31 13:53:06 +0100479static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200480{
William Juulcfa460a2007-10-31 13:53:06 +0100481 struct nand_chip *chip = mtd->priv;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200482
Sergey Lapindfe64e22013-01-14 03:46:50 +0000483 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200484 if (chip->options & NAND_BROKEN_XD)
485 return 0;
486
Wolfgang Denk932394a2005-08-17 12:55:25 +0200487 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100488 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
489 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200490}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100491
Wolfgang Denk932394a2005-08-17 12:55:25 +0200492/**
493 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000494 * @mtd: MTD device structure
495 * @ofs: offset from device start
496 * @getchip: 0, if the chip is already selected
497 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200498 *
499 * Check, if the block is bad. Either by reading the bad block table or
500 * calling of the scan function.
501 */
William Juulcfa460a2007-10-31 13:53:06 +0100502static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
503 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200504{
William Juulcfa460a2007-10-31 13:53:06 +0100505 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200506
Scott Woodfb494542012-02-20 14:50:39 -0600507 if (!(chip->options & NAND_BBT_SCANNED)) {
508 chip->options |= NAND_BBT_SCANNED;
509 chip->scan_bbt(mtd);
510 }
511
William Juulcfa460a2007-10-31 13:53:06 +0100512 if (!chip->bbt)
513 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200514
Wolfgang Denk932394a2005-08-17 12:55:25 +0200515 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100516 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200517}
518
Sergey Lapindfe64e22013-01-14 03:46:50 +0000519/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juulcfa460a2007-10-31 13:53:06 +0100520void nand_wait_ready(struct mtd_info *mtd)
521{
522 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200523 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000524 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100525
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000526 time_start = get_timer(0);
Stefan Roese12072262008-01-05 16:43:25 +0100527
Sergey Lapindfe64e22013-01-14 03:46:50 +0000528 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000529 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100530 if (chip->dev_ready)
531 if (chip->dev_ready(mtd))
532 break;
533 }
William Juulcfa460a2007-10-31 13:53:06 +0100534}
William Juulcfa460a2007-10-31 13:53:06 +0100535
Wolfgang Denk932394a2005-08-17 12:55:25 +0200536/**
537 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000538 * @mtd: MTD device structure
539 * @command: the command to be sent
540 * @column: the column address for this command, -1 if none
541 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200542 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000543 * Send command to NAND device. This function is used for small page devices
544 * (256/512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200545 */
William Juulcfa460a2007-10-31 13:53:06 +0100546static void nand_command(struct mtd_info *mtd, unsigned int command,
547 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200548{
William Juulcfa460a2007-10-31 13:53:06 +0100549 register struct nand_chip *chip = mtd->priv;
550 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600551 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200552
Sergey Lapindfe64e22013-01-14 03:46:50 +0000553 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200554 if (command == NAND_CMD_SEQIN) {
555 int readcmd;
556
William Juulcfa460a2007-10-31 13:53:06 +0100557 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200558 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100559 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
564 } else {
565 column -= 256;
566 readcmd = NAND_CMD_READ1;
567 }
William Juulcfa460a2007-10-31 13:53:06 +0100568 chip->cmd_ctrl(mtd, readcmd, ctrl);
569 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200570 }
William Juulcfa460a2007-10-31 13:53:06 +0100571 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200572
Sergey Lapindfe64e22013-01-14 03:46:50 +0000573 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100574 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
575 /* Serially input address */
576 if (column != -1) {
577 /* Adjust columns for 16 bit buswidth */
578 if (chip->options & NAND_BUSWIDTH_16)
579 column >>= 1;
580 chip->cmd_ctrl(mtd, column, ctrl);
581 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200582 }
William Juulcfa460a2007-10-31 13:53:06 +0100583 if (page_addr != -1) {
584 chip->cmd_ctrl(mtd, page_addr, ctrl);
585 ctrl &= ~NAND_CTRL_CHANGE;
586 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
587 /* One more address cycle for devices > 32MiB */
588 if (chip->chipsize > (32 << 20))
589 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
590 }
591 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200592
593 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000594 * Program and erase have their own busy handlers status and sequential
595 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100596 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200597 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200598
Wolfgang Denk932394a2005-08-17 12:55:25 +0200599 case NAND_CMD_PAGEPROG:
600 case NAND_CMD_ERASE1:
601 case NAND_CMD_ERASE2:
602 case NAND_CMD_SEQIN:
603 case NAND_CMD_STATUS:
604 return;
605
606 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100607 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200608 break;
William Juulcfa460a2007-10-31 13:53:06 +0100609 udelay(chip->chip_delay);
610 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
611 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
612 chip->cmd_ctrl(mtd,
613 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600614 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
615 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200616 return;
617
William Juulcfa460a2007-10-31 13:53:06 +0100618 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200619 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200620 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200621 * If we don't have access to the busy pin, we apply the given
622 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100623 */
624 if (!chip->dev_ready) {
625 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200626 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200627 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200628 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000629 /*
630 * Apply this short delay always to ensure that we do wait tWB in
631 * any case on any machine.
632 */
William Juulcfa460a2007-10-31 13:53:06 +0100633 ndelay(100);
634
635 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200636}
637
638/**
639 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000640 * @mtd: MTD device structure
641 * @command: the command to be sent
642 * @column: the column address for this command, -1 if none
643 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200644 *
William Juulcfa460a2007-10-31 13:53:06 +0100645 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000646 * devices. We don't have the separate regions as we have in the small page
647 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200648 */
William Juulcfa460a2007-10-31 13:53:06 +0100649static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
650 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200651{
William Juulcfa460a2007-10-31 13:53:06 +0100652 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600653 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200654
655 /* Emulate NAND_CMD_READOOB */
656 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100657 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200658 command = NAND_CMD_READ0;
659 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200660
William Juulcfa460a2007-10-31 13:53:06 +0100661 /* Command latch cycle */
662 chip->cmd_ctrl(mtd, command & 0xff,
663 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200664
665 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100666 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200667
668 /* Serially input address */
669 if (column != -1) {
670 /* Adjust columns for 16 bit buswidth */
William Juulcfa460a2007-10-31 13:53:06 +0100671 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200672 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100673 chip->cmd_ctrl(mtd, column, ctrl);
674 ctrl &= ~NAND_CTRL_CHANGE;
675 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200676 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200677 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100678 chip->cmd_ctrl(mtd, page_addr, ctrl);
679 chip->cmd_ctrl(mtd, page_addr >> 8,
680 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200681 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100682 if (chip->chipsize > (128 << 20))
683 chip->cmd_ctrl(mtd, page_addr >> 16,
684 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200685 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200686 }
William Juulcfa460a2007-10-31 13:53:06 +0100687 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200688
689 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000690 * Program and erase have their own busy handlers status, sequential
691 * in, and deplete1 need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100692 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200693 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200694
Wolfgang Denk932394a2005-08-17 12:55:25 +0200695 case NAND_CMD_CACHEDPROG:
696 case NAND_CMD_PAGEPROG:
697 case NAND_CMD_ERASE1:
698 case NAND_CMD_ERASE2:
699 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100700 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200701 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100702 case NAND_CMD_DEPLETE1:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200703 return;
704
William Juulcfa460a2007-10-31 13:53:06 +0100705 case NAND_CMD_STATUS_ERROR:
706 case NAND_CMD_STATUS_ERROR0:
707 case NAND_CMD_STATUS_ERROR1:
708 case NAND_CMD_STATUS_ERROR2:
709 case NAND_CMD_STATUS_ERROR3:
Sergey Lapindfe64e22013-01-14 03:46:50 +0000710 /* Read error status commands require only a short delay */
William Juulcfa460a2007-10-31 13:53:06 +0100711 udelay(chip->chip_delay);
712 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200713
714 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100715 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200716 break;
William Juulcfa460a2007-10-31 13:53:06 +0100717 udelay(chip->chip_delay);
718 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
719 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
720 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
721 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600722 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
723 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100724 return;
725
726 case NAND_CMD_RNDOUT:
727 /* No ready / busy check necessary */
728 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
729 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
730 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
731 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200732 return;
733
734 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100735 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
736 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
737 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
738 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200739
William Juulcfa460a2007-10-31 13:53:06 +0100740 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200741 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200742 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200743 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000744 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100745 */
746 if (!chip->dev_ready) {
747 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200748 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200749 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200750 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200751
Sergey Lapindfe64e22013-01-14 03:46:50 +0000752 /*
753 * Apply this short delay always to ensure that we do wait tWB in
754 * any case on any machine.
755 */
William Juulcfa460a2007-10-31 13:53:06 +0100756 ndelay(100);
757
758 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200759}
760
761/**
762 * nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000763 * @chip: the nand chip descriptor
764 * @mtd: MTD device structure
765 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200766 *
767 * Get the device and lock it for exclusive access
768 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200769static int
770nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100771{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200772 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100773 return 0;
774}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200775
776/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000777 * nand_wait - [DEFAULT] wait until the command is done
778 * @mtd: MTD device structure
779 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200780 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000781 * Wait for command done. This applies to erase and program only. Erase can
782 * take up to 400ms and program up to 20ms according to general NAND and
783 * SmartMedia specs.
William Juulcfa460a2007-10-31 13:53:06 +0100784 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200785static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200786{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100787 unsigned long timeo;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200788 int state = chip->state;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000789 u32 time_start;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100790
791 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200792 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100793 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200794 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100795
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200796 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
797 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100798 else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200799 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100800
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000801 time_start = get_timer(0);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100802
803 while (1) {
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000804 if (get_timer(time_start) > timeo) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100805 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100806 return 0x01;
807 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100808
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200809 if (chip->dev_ready) {
810 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100811 break;
812 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200813 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100814 break;
815 }
816 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100817#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000818 time_start = get_timer(0);
819 while (get_timer(time_start) < 10)
820 ;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100821#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100822
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200823 return (int)chip->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200824}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200825
826/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000827 * nand_read_page_raw - [INTERN] read raw page data without ecc
828 * @mtd: mtd info structure
829 * @chip: nand chip info structure
830 * @buf: buffer to store read data
831 * @oob_required: caller requires OOB data read to chip->oob_poi
832 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500833 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000834 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200835 */
William Juulcfa460a2007-10-31 13:53:06 +0100836static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000837 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200838{
William Juulcfa460a2007-10-31 13:53:06 +0100839 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000840 if (oob_required)
841 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +0100842 return 0;
843}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200844
William Juulcfa460a2007-10-31 13:53:06 +0100845/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000846 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
847 * @mtd: mtd info structure
848 * @chip: nand chip info structure
849 * @buf: buffer to store read data
850 * @oob_required: caller requires OOB data read to chip->oob_poi
851 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500852 *
853 * We need a special oob layout and handling even when OOB isn't used.
854 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200855static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000856 struct nand_chip *chip, uint8_t *buf,
857 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -0500858{
859 int eccsize = chip->ecc.size;
860 int eccbytes = chip->ecc.bytes;
861 uint8_t *oob = chip->oob_poi;
862 int steps, size;
863
864 for (steps = chip->ecc.steps; steps > 0; steps--) {
865 chip->read_buf(mtd, buf, eccsize);
866 buf += eccsize;
867
868 if (chip->ecc.prepad) {
869 chip->read_buf(mtd, oob, chip->ecc.prepad);
870 oob += chip->ecc.prepad;
871 }
872
873 chip->read_buf(mtd, oob, eccbytes);
874 oob += eccbytes;
875
876 if (chip->ecc.postpad) {
877 chip->read_buf(mtd, oob, chip->ecc.postpad);
878 oob += chip->ecc.postpad;
879 }
880 }
881
882 size = mtd->oobsize - (oob - chip->oob_poi);
883 if (size)
884 chip->read_buf(mtd, oob, size);
885
886 return 0;
887}
888
889/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000890 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
891 * @mtd: mtd info structure
892 * @chip: nand chip info structure
893 * @buf: buffer to store read data
894 * @oob_required: caller requires OOB data read to chip->oob_poi
895 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +0100896 */
897static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000898 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +0100899{
900 int i, eccsize = chip->ecc.size;
901 int eccbytes = chip->ecc.bytes;
902 int eccsteps = chip->ecc.steps;
903 uint8_t *p = buf;
904 uint8_t *ecc_calc = chip->buffers->ecccalc;
905 uint8_t *ecc_code = chip->buffers->ecccode;
906 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200907
Sergey Lapindfe64e22013-01-14 03:46:50 +0000908 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200909
William Juulcfa460a2007-10-31 13:53:06 +0100910 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
911 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200912
William Juulcfa460a2007-10-31 13:53:06 +0100913 for (i = 0; i < chip->ecc.total; i++)
914 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200915
William Juulcfa460a2007-10-31 13:53:06 +0100916 eccsteps = chip->ecc.steps;
917 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200918
William Juulcfa460a2007-10-31 13:53:06 +0100919 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
920 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200921
William Juulcfa460a2007-10-31 13:53:06 +0100922 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodc45912d2008-10-24 16:20:43 -0500923 if (stat < 0)
924 mtd->ecc_stats.failed++;
925 else
926 mtd->ecc_stats.corrected += stat;
927 }
928 return 0;
929}
930
931/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000932 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
933 * @mtd: mtd info structure
934 * @chip: nand chip info structure
935 * @data_offs: offset of requested data within the page
936 * @readlen: data length
937 * @bufpoi: buffer to store read data
Scott Woodc45912d2008-10-24 16:20:43 -0500938 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200939static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
940 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Scott Woodc45912d2008-10-24 16:20:43 -0500941{
942 int start_step, end_step, num_steps;
943 uint32_t *eccpos = chip->ecc.layout->eccpos;
944 uint8_t *p;
945 int data_col_addr, i, gaps = 0;
946 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
947 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200948 int index = 0;
Scott Woodc45912d2008-10-24 16:20:43 -0500949
Sergey Lapindfe64e22013-01-14 03:46:50 +0000950 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -0500951 start_step = data_offs / chip->ecc.size;
952 end_step = (data_offs + readlen - 1) / chip->ecc.size;
953 num_steps = end_step - start_step + 1;
954
Sergey Lapindfe64e22013-01-14 03:46:50 +0000955 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -0500956 datafrag_len = num_steps * chip->ecc.size;
957 eccfrag_len = num_steps * chip->ecc.bytes;
958
959 data_col_addr = start_step * chip->ecc.size;
960 /* If we read not a page aligned data */
961 if (data_col_addr != 0)
962 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
963
964 p = bufpoi + data_col_addr;
965 chip->read_buf(mtd, p, datafrag_len);
966
Sergey Lapindfe64e22013-01-14 03:46:50 +0000967 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -0500968 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
969 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
970
Sergey Lapindfe64e22013-01-14 03:46:50 +0000971 /*
972 * The performance is faster if we position offsets according to
973 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
974 */
Scott Woodc45912d2008-10-24 16:20:43 -0500975 for (i = 0; i < eccfrag_len - 1; i++) {
976 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
977 eccpos[i + start_step * chip->ecc.bytes + 1]) {
978 gaps = 1;
979 break;
980 }
981 }
982 if (gaps) {
983 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
984 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
985 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000986 /*
987 * Send the command to read the particular ECC bytes take care
988 * about buswidth alignment in read_buf.
989 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200990 index = start_step * chip->ecc.bytes;
991
992 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -0500993 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200994 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -0500995 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200996 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -0500997 aligned_len++;
998
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200999 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1000 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001001 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1002 }
1003
1004 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001005 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001006
1007 p = bufpoi + data_col_addr;
1008 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1009 int stat;
1010
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001011 stat = chip->ecc.correct(mtd, p,
1012 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1013 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001014 mtd->ecc_stats.failed++;
1015 else
1016 mtd->ecc_stats.corrected += stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001017 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001018 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001019}
1020
Wolfgang Denk932394a2005-08-17 12:55:25 +02001021/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001022 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1023 * @mtd: mtd info structure
1024 * @chip: nand chip info structure
1025 * @buf: buffer to store read data
1026 * @oob_required: caller requires OOB data read to chip->oob_poi
1027 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001028 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001029 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001030 */
William Juulcfa460a2007-10-31 13:53:06 +01001031static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001032 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001033{
William Juulcfa460a2007-10-31 13:53:06 +01001034 int i, eccsize = chip->ecc.size;
1035 int eccbytes = chip->ecc.bytes;
1036 int eccsteps = chip->ecc.steps;
1037 uint8_t *p = buf;
1038 uint8_t *ecc_calc = chip->buffers->ecccalc;
1039 uint8_t *ecc_code = chip->buffers->ecccode;
1040 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001041
William Juulcfa460a2007-10-31 13:53:06 +01001042 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1043 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1044 chip->read_buf(mtd, p, eccsize);
1045 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1046 }
1047 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001048
William Juulcfa460a2007-10-31 13:53:06 +01001049 for (i = 0; i < chip->ecc.total; i++)
1050 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001051
William Juulcfa460a2007-10-31 13:53:06 +01001052 eccsteps = chip->ecc.steps;
1053 p = buf;
1054
1055 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1056 int stat;
1057
1058 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj18b5a4b2009-11-07 14:25:03 -05001059 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001060 mtd->ecc_stats.failed++;
1061 else
1062 mtd->ecc_stats.corrected += stat;
1063 }
1064 return 0;
1065}
1066
1067/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001068 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1069 * @mtd: mtd info structure
1070 * @chip: nand chip info structure
1071 * @buf: buffer to store read data
1072 * @oob_required: caller requires OOB data read to chip->oob_poi
1073 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001074 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001075 * Hardware ECC for large page chips, require OOB to be read first. For this
1076 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1077 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1078 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1079 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001080 */
1081static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001082 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001083{
1084 int i, eccsize = chip->ecc.size;
1085 int eccbytes = chip->ecc.bytes;
1086 int eccsteps = chip->ecc.steps;
1087 uint8_t *p = buf;
1088 uint8_t *ecc_code = chip->buffers->ecccode;
1089 uint32_t *eccpos = chip->ecc.layout->eccpos;
1090 uint8_t *ecc_calc = chip->buffers->ecccalc;
1091
1092 /* Read the OOB area first */
1093 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1094 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1095 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1096
1097 for (i = 0; i < chip->ecc.total; i++)
1098 ecc_code[i] = chip->oob_poi[eccpos[i]];
1099
1100 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1101 int stat;
1102
1103 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1104 chip->read_buf(mtd, p, eccsize);
1105 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1106
1107 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1108 if (stat < 0)
1109 mtd->ecc_stats.failed++;
1110 else
1111 mtd->ecc_stats.corrected += stat;
1112 }
1113 return 0;
1114}
1115
1116/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001117 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1118 * @mtd: mtd info structure
1119 * @chip: nand chip info structure
1120 * @buf: buffer to store read data
1121 * @oob_required: caller requires OOB data read to chip->oob_poi
1122 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001123 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001124 * The hw generator calculates the error syndrome automatically. Therefore we
1125 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001126 */
1127static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001128 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001129{
1130 int i, eccsize = chip->ecc.size;
1131 int eccbytes = chip->ecc.bytes;
1132 int eccsteps = chip->ecc.steps;
1133 uint8_t *p = buf;
1134 uint8_t *oob = chip->oob_poi;
1135
1136 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1137 int stat;
1138
1139 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1140 chip->read_buf(mtd, p, eccsize);
1141
1142 if (chip->ecc.prepad) {
1143 chip->read_buf(mtd, oob, chip->ecc.prepad);
1144 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001145 }
1146
William Juulcfa460a2007-10-31 13:53:06 +01001147 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1148 chip->read_buf(mtd, oob, eccbytes);
1149 stat = chip->ecc.correct(mtd, p, oob, NULL);
1150
Scott Woodc45912d2008-10-24 16:20:43 -05001151 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001152 mtd->ecc_stats.failed++;
1153 else
1154 mtd->ecc_stats.corrected += stat;
1155
1156 oob += eccbytes;
1157
1158 if (chip->ecc.postpad) {
1159 chip->read_buf(mtd, oob, chip->ecc.postpad);
1160 oob += chip->ecc.postpad;
1161 }
1162 }
1163
1164 /* Calculate remaining oob bytes */
1165 i = mtd->oobsize - (oob - chip->oob_poi);
1166 if (i)
1167 chip->read_buf(mtd, oob, i);
1168
1169 return 0;
1170}
1171
1172/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001173 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1174 * @chip: nand chip structure
1175 * @oob: oob destination address
1176 * @ops: oob ops structure
1177 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001178 */
1179static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1180 struct mtd_oob_ops *ops, size_t len)
1181{
Christian Hitz90e3f392011-10-12 09:32:01 +02001182 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001183
Sergey Lapindfe64e22013-01-14 03:46:50 +00001184 case MTD_OPS_PLACE_OOB:
1185 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001186 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1187 return oob + len;
1188
Sergey Lapindfe64e22013-01-14 03:46:50 +00001189 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001190 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1191 uint32_t boffs = 0, roffs = ops->ooboffs;
1192 size_t bytes = 0;
1193
Christian Hitz90e3f392011-10-12 09:32:01 +02001194 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001195 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001196 if (unlikely(roffs)) {
1197 if (roffs >= free->length) {
1198 roffs -= free->length;
1199 continue;
1200 }
1201 boffs = free->offset + roffs;
1202 bytes = min_t(size_t, len,
1203 (free->length - roffs));
1204 roffs = 0;
1205 } else {
1206 bytes = min_t(size_t, len, free->length);
1207 boffs = free->offset;
1208 }
1209 memcpy(oob, chip->oob_poi + boffs, bytes);
1210 oob += bytes;
1211 }
1212 return oob;
1213 }
1214 default:
1215 BUG();
1216 }
1217 return NULL;
1218}
1219
1220/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001221 * nand_do_read_ops - [INTERN] Read data with ECC
1222 * @mtd: MTD device structure
1223 * @from: offset to read from
1224 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001225 *
1226 * Internal function. Called with chip held.
1227 */
1228static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1229 struct mtd_oob_ops *ops)
1230{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001231 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juulcfa460a2007-10-31 13:53:06 +01001232 struct nand_chip *chip = mtd->priv;
1233 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001234 int ret = 0;
1235 uint32_t readlen = ops->len;
1236 uint32_t oobreadlen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001237 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001238 mtd->oobavail : mtd->oobsize;
1239
William Juulcfa460a2007-10-31 13:53:06 +01001240 uint8_t *bufpoi, *oob, *buf;
Paul Burton40462e52013-09-04 15:16:56 +01001241 unsigned int max_bitflips = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001242
1243 stats = mtd->ecc_stats;
1244
1245 chipnr = (int)(from >> chip->chip_shift);
1246 chip->select_chip(mtd, chipnr);
1247
1248 realpage = (int)(from >> chip->page_shift);
1249 page = realpage & chip->pagemask;
1250
1251 col = (int)(from & (mtd->writesize - 1));
1252
1253 buf = ops->datbuf;
1254 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001255 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001256
Christian Hitz90e3f392011-10-12 09:32:01 +02001257 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001258 WATCHDOG_RESET();
1259
William Juulcfa460a2007-10-31 13:53:06 +01001260 bytes = min(mtd->writesize - col, readlen);
1261 aligned = (bytes == mtd->writesize);
1262
Sergey Lapindfe64e22013-01-14 03:46:50 +00001263 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001264 if (realpage != chip->pagebuf || oob) {
1265 bufpoi = aligned ? buf : chip->buffers->databuf;
1266
Sergey Lapindfe64e22013-01-14 03:46:50 +00001267 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001268
Paul Burton40462e52013-09-04 15:16:56 +01001269 /*
1270 * Now read the page into the buffer. Absent an error,
1271 * the read methods return max bitflips per ecc step.
1272 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001273 if (unlikely(ops->mode == MTD_OPS_RAW))
1274 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1275 oob_required,
1276 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001277 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1278 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001279 ret = chip->ecc.read_subpage(mtd, chip,
1280 col, bytes, bufpoi);
William Juulcfa460a2007-10-31 13:53:06 +01001281 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001282 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001283 oob_required, page);
1284 if (ret < 0) {
1285 if (!aligned)
1286 /* Invalidate page cache */
1287 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001288 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001289 }
William Juulcfa460a2007-10-31 13:53:06 +01001290
Paul Burton40462e52013-09-04 15:16:56 +01001291 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1292
William Juulcfa460a2007-10-31 13:53:06 +01001293 /* Transfer not aligned data */
1294 if (!aligned) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001295 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00001296 !(mtd->ecc_stats.failed - stats.failed) &&
Paul Burton40462e52013-09-04 15:16:56 +01001297 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001298 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001299 chip->pagebuf_bitflips = ret;
1300 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001301 /* Invalidate page cache */
1302 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001303 }
William Juulcfa460a2007-10-31 13:53:06 +01001304 memcpy(buf, chip->buffers->databuf + col, bytes);
1305 }
1306
1307 buf += bytes;
1308
1309 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001310 int toread = min(oobreadlen, max_oobsize);
1311
1312 if (toread) {
1313 oob = nand_transfer_oob(chip,
1314 oob, ops, toread);
1315 oobreadlen -= toread;
1316 }
William Juulcfa460a2007-10-31 13:53:06 +01001317 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001318 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001319 memcpy(buf, chip->buffers->databuf + col, bytes);
1320 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001321 max_bitflips = max_t(unsigned int, max_bitflips,
1322 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001323 }
1324
William Juulcfa460a2007-10-31 13:53:06 +01001325 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001326
William Juulcfa460a2007-10-31 13:53:06 +01001327 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001328 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001329
Sergey Lapindfe64e22013-01-14 03:46:50 +00001330 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001331 col = 0;
1332 /* Increment page address */
1333 realpage++;
1334
William Juulcfa460a2007-10-31 13:53:06 +01001335 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001336 /* Check, if we cross a chip boundary */
1337 if (!page) {
1338 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001339 chip->select_chip(mtd, -1);
1340 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001341 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001342 }
1343
William Juulcfa460a2007-10-31 13:53:06 +01001344 ops->retlen = ops->len - (size_t) readlen;
1345 if (oob)
1346 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001347
William Juulcfa460a2007-10-31 13:53:06 +01001348 if (ret)
1349 return ret;
1350
1351 if (mtd->ecc_stats.failed - stats.failed)
1352 return -EBADMSG;
1353
Paul Burton40462e52013-09-04 15:16:56 +01001354 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001355}
1356
1357/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001358 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001359 * @mtd: MTD device structure
1360 * @from: offset to read from
1361 * @len: number of bytes to read
1362 * @retlen: pointer to variable to store the number of read bytes
1363 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001364 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001365 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001366 */
William Juulcfa460a2007-10-31 13:53:06 +01001367static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1368 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001369{
William Juulcfa460a2007-10-31 13:53:06 +01001370 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001371 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001372 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001373
William Juulcfa460a2007-10-31 13:53:06 +01001374 nand_get_device(chip, mtd, FL_READING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001375 ops.len = len;
1376 ops.datbuf = buf;
1377 ops.oobbuf = NULL;
1378 ops.mode = MTD_OPS_PLACE_OOB;
1379 ret = nand_do_read_ops(mtd, from, &ops);
1380 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001381 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001382 return ret;
1383}
1384
William Juulcfa460a2007-10-31 13:53:06 +01001385/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001386 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1387 * @mtd: mtd info structure
1388 * @chip: nand chip info structure
1389 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001390 */
1391static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001392 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001393{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001394 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001395 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001396 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001397}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001398
1399/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001400 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001401 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001402 * @mtd: mtd info structure
1403 * @chip: nand chip info structure
1404 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001405 */
1406static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001407 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001408{
1409 uint8_t *buf = chip->oob_poi;
1410 int length = mtd->oobsize;
1411 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1412 int eccsize = chip->ecc.size;
1413 uint8_t *bufpoi = buf;
1414 int i, toread, sndrnd = 0, pos;
1415
1416 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1417 for (i = 0; i < chip->ecc.steps; i++) {
1418 if (sndrnd) {
1419 pos = eccsize + i * (eccsize + chunk);
1420 if (mtd->writesize > 512)
1421 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1422 else
1423 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1424 } else
1425 sndrnd = 1;
1426 toread = min_t(int, length, chunk);
1427 chip->read_buf(mtd, bufpoi, toread);
1428 bufpoi += toread;
1429 length -= toread;
1430 }
1431 if (length > 0)
1432 chip->read_buf(mtd, bufpoi, length);
1433
Sergey Lapindfe64e22013-01-14 03:46:50 +00001434 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001435}
1436
1437/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001438 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1439 * @mtd: mtd info structure
1440 * @chip: nand chip info structure
1441 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001442 */
1443static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1444 int page)
1445{
1446 int status = 0;
1447 const uint8_t *buf = chip->oob_poi;
1448 int length = mtd->oobsize;
1449
1450 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1451 chip->write_buf(mtd, buf, length);
1452 /* Send command to program the OOB data */
1453 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1454
1455 status = chip->waitfunc(mtd, chip);
1456
1457 return status & NAND_STATUS_FAIL ? -EIO : 0;
1458}
1459
1460/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001461 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1462 * with syndrome - only for large page flash
1463 * @mtd: mtd info structure
1464 * @chip: nand chip info structure
1465 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001466 */
1467static int nand_write_oob_syndrome(struct mtd_info *mtd,
1468 struct nand_chip *chip, int page)
1469{
1470 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1471 int eccsize = chip->ecc.size, length = mtd->oobsize;
1472 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1473 const uint8_t *bufpoi = chip->oob_poi;
1474
1475 /*
1476 * data-ecc-data-ecc ... ecc-oob
1477 * or
1478 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1479 */
1480 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1481 pos = steps * (eccsize + chunk);
1482 steps = 0;
1483 } else
1484 pos = eccsize;
1485
1486 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1487 for (i = 0; i < steps; i++) {
1488 if (sndcmd) {
1489 if (mtd->writesize <= 512) {
1490 uint32_t fill = 0xFFFFFFFF;
1491
1492 len = eccsize;
1493 while (len > 0) {
1494 int num = min_t(int, len, 4);
1495 chip->write_buf(mtd, (uint8_t *)&fill,
1496 num);
1497 len -= num;
1498 }
1499 } else {
1500 pos = eccsize + i * (eccsize + chunk);
1501 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1502 }
1503 } else
1504 sndcmd = 1;
1505 len = min_t(int, length, chunk);
1506 chip->write_buf(mtd, bufpoi, len);
1507 bufpoi += len;
1508 length -= len;
1509 }
1510 if (length > 0)
1511 chip->write_buf(mtd, bufpoi, length);
1512
1513 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1514 status = chip->waitfunc(mtd, chip);
1515
1516 return status & NAND_STATUS_FAIL ? -EIO : 0;
1517}
1518
1519/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001520 * nand_do_read_oob - [INTERN] NAND read out-of-band
1521 * @mtd: MTD device structure
1522 * @from: offset to read from
1523 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001524 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001525 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01001526 */
1527static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1528 struct mtd_oob_ops *ops)
1529{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001530 int page, realpage, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01001531 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001532 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001533 int readlen = ops->ooblen;
1534 int len;
1535 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001536 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001537
Christian Hitz90e3f392011-10-12 09:32:01 +02001538 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1539 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001540
Sergey Lapindfe64e22013-01-14 03:46:50 +00001541 stats = mtd->ecc_stats;
1542
1543 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01001544 len = chip->ecc.layout->oobavail;
1545 else
1546 len = mtd->oobsize;
1547
1548 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001549 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1550 "outside oob\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001551 return -EINVAL;
1552 }
1553
1554 /* Do not allow reads past end of device */
1555 if (unlikely(from >= mtd->size ||
1556 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1557 (from >> chip->page_shift)) * len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001558 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1559 "of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001560 return -EINVAL;
1561 }
1562
1563 chipnr = (int)(from >> chip->chip_shift);
1564 chip->select_chip(mtd, chipnr);
1565
1566 /* Shift to get page */
1567 realpage = (int)(from >> chip->page_shift);
1568 page = realpage & chip->pagemask;
1569
Christian Hitz90e3f392011-10-12 09:32:01 +02001570 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001571 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00001572 if (ops->mode == MTD_OPS_RAW)
1573 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1574 else
1575 ret = chip->ecc.read_oob(mtd, chip, page);
1576
1577 if (ret < 0)
1578 break;
William Juulcfa460a2007-10-31 13:53:06 +01001579
1580 len = min(len, readlen);
1581 buf = nand_transfer_oob(chip, buf, ops, len);
1582
William Juulcfa460a2007-10-31 13:53:06 +01001583 readlen -= len;
1584 if (!readlen)
1585 break;
1586
1587 /* Increment page address */
1588 realpage++;
1589
1590 page = realpage & chip->pagemask;
1591 /* Check, if we cross a chip boundary */
1592 if (!page) {
1593 chipnr++;
1594 chip->select_chip(mtd, -1);
1595 chip->select_chip(mtd, chipnr);
1596 }
William Juulcfa460a2007-10-31 13:53:06 +01001597 }
1598
Sergey Lapindfe64e22013-01-14 03:46:50 +00001599 ops->oobretlen = ops->ooblen - readlen;
1600
1601 if (ret < 0)
1602 return ret;
1603
1604 if (mtd->ecc_stats.failed - stats.failed)
1605 return -EBADMSG;
1606
1607 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001608}
1609
1610/**
1611 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00001612 * @mtd: MTD device structure
1613 * @from: offset to read from
1614 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01001615 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001616 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01001617 */
1618static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1619 struct mtd_oob_ops *ops)
1620{
1621 struct nand_chip *chip = mtd->priv;
1622 int ret = -ENOTSUPP;
1623
1624 ops->retlen = 0;
1625
1626 /* Do not allow reads past end of device */
1627 if (ops->datbuf && (from + ops->len) > mtd->size) {
Christian Hitz90e3f392011-10-12 09:32:01 +02001628 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1629 "beyond end of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001630 return -EINVAL;
1631 }
1632
1633 nand_get_device(chip, mtd, FL_READING);
1634
Christian Hitz90e3f392011-10-12 09:32:01 +02001635 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001636 case MTD_OPS_PLACE_OOB:
1637 case MTD_OPS_AUTO_OOB:
1638 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001639 break;
1640
1641 default:
1642 goto out;
1643 }
1644
1645 if (!ops->datbuf)
1646 ret = nand_do_read_oob(mtd, from, ops);
1647 else
1648 ret = nand_do_read_ops(mtd, from, ops);
1649
Christian Hitz90e3f392011-10-12 09:32:01 +02001650out:
William Juulcfa460a2007-10-31 13:53:06 +01001651 nand_release_device(mtd);
1652 return ret;
1653}
1654
1655
1656/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001657 * nand_write_page_raw - [INTERN] raw page write function
1658 * @mtd: mtd info structure
1659 * @chip: nand chip info structure
1660 * @buf: data buffer
1661 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05001662 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001663 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01001664 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001665static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1666 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001667{
1668 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001669 if (oob_required)
1670 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1671
1672 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001673}
1674
1675/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001676 * nand_write_page_raw_syndrome - [INTERN] raw page write function
1677 * @mtd: mtd info structure
1678 * @chip: nand chip info structure
1679 * @buf: data buffer
1680 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05001681 *
1682 * We need a special oob layout and handling even when ECC isn't checked.
1683 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001684static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02001685 struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001686 const uint8_t *buf, int oob_required)
David Brownell7e866612009-11-07 16:27:01 -05001687{
1688 int eccsize = chip->ecc.size;
1689 int eccbytes = chip->ecc.bytes;
1690 uint8_t *oob = chip->oob_poi;
1691 int steps, size;
1692
1693 for (steps = chip->ecc.steps; steps > 0; steps--) {
1694 chip->write_buf(mtd, buf, eccsize);
1695 buf += eccsize;
1696
1697 if (chip->ecc.prepad) {
1698 chip->write_buf(mtd, oob, chip->ecc.prepad);
1699 oob += chip->ecc.prepad;
1700 }
1701
1702 chip->read_buf(mtd, oob, eccbytes);
1703 oob += eccbytes;
1704
1705 if (chip->ecc.postpad) {
1706 chip->write_buf(mtd, oob, chip->ecc.postpad);
1707 oob += chip->ecc.postpad;
1708 }
1709 }
1710
1711 size = mtd->oobsize - (oob - chip->oob_poi);
1712 if (size)
1713 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001714
1715 return 0;
David Brownell7e866612009-11-07 16:27:01 -05001716}
1717/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001718 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
1719 * @mtd: mtd info structure
1720 * @chip: nand chip info structure
1721 * @buf: data buffer
1722 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001723 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001724static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1725 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001726{
1727 int i, eccsize = chip->ecc.size;
1728 int eccbytes = chip->ecc.bytes;
1729 int eccsteps = chip->ecc.steps;
1730 uint8_t *ecc_calc = chip->buffers->ecccalc;
1731 const uint8_t *p = buf;
1732 uint32_t *eccpos = chip->ecc.layout->eccpos;
1733
Sergey Lapindfe64e22013-01-14 03:46:50 +00001734 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01001735 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1736 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1737
1738 for (i = 0; i < chip->ecc.total; i++)
1739 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1740
Sergey Lapindfe64e22013-01-14 03:46:50 +00001741 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
William Juulcfa460a2007-10-31 13:53:06 +01001742}
1743
1744/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001745 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
1746 * @mtd: mtd info structure
1747 * @chip: nand chip info structure
1748 * @buf: data buffer
1749 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001750 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001751static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1752 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001753{
1754 int i, eccsize = chip->ecc.size;
1755 int eccbytes = chip->ecc.bytes;
1756 int eccsteps = chip->ecc.steps;
1757 uint8_t *ecc_calc = chip->buffers->ecccalc;
1758 const uint8_t *p = buf;
1759 uint32_t *eccpos = chip->ecc.layout->eccpos;
1760
1761 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1762 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1763 chip->write_buf(mtd, p, eccsize);
1764 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1765 }
1766
1767 for (i = 0; i < chip->ecc.total; i++)
1768 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1769
1770 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001771
1772 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001773}
1774
1775/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001776 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
1777 * @mtd: mtd info structure
1778 * @chip: nand chip info structure
1779 * @buf: data buffer
1780 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01001781 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001782 * The hw generator calculates the error syndrome automatically. Therefore we
1783 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001784 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001785static int nand_write_page_syndrome(struct mtd_info *mtd,
1786 struct nand_chip *chip,
1787 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01001788{
1789 int i, eccsize = chip->ecc.size;
1790 int eccbytes = chip->ecc.bytes;
1791 int eccsteps = chip->ecc.steps;
1792 const uint8_t *p = buf;
1793 uint8_t *oob = chip->oob_poi;
1794
1795 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1796
1797 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1798 chip->write_buf(mtd, p, eccsize);
1799
1800 if (chip->ecc.prepad) {
1801 chip->write_buf(mtd, oob, chip->ecc.prepad);
1802 oob += chip->ecc.prepad;
1803 }
1804
1805 chip->ecc.calculate(mtd, p, oob);
1806 chip->write_buf(mtd, oob, eccbytes);
1807 oob += eccbytes;
1808
1809 if (chip->ecc.postpad) {
1810 chip->write_buf(mtd, oob, chip->ecc.postpad);
1811 oob += chip->ecc.postpad;
1812 }
1813 }
1814
1815 /* Calculate remaining oob bytes */
1816 i = mtd->oobsize - (oob - chip->oob_poi);
1817 if (i)
1818 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001819
1820 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001821}
1822
1823/**
1824 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00001825 * @mtd: MTD device structure
1826 * @chip: NAND chip descriptor
1827 * @buf: the data to write
1828 * @oob_required: must write chip->oob_poi to OOB
1829 * @page: page number to write
1830 * @cached: cached programming
1831 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01001832 */
1833static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001834 const uint8_t *buf, int oob_required, int page,
1835 int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01001836{
1837 int status;
1838
1839 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1840
1841 if (unlikely(raw))
Sergey Lapindfe64e22013-01-14 03:46:50 +00001842 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
William Juulcfa460a2007-10-31 13:53:06 +01001843 else
Sergey Lapindfe64e22013-01-14 03:46:50 +00001844 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
1845
1846 if (status < 0)
1847 return status;
William Juulcfa460a2007-10-31 13:53:06 +01001848
1849 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00001850 * Cached progamming disabled for now. Not sure if it's worth the
1851 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01001852 */
1853 cached = 0;
1854
1855 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1856
1857 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1858 status = chip->waitfunc(mtd, chip);
1859 /*
1860 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00001861 * available.
William Juulcfa460a2007-10-31 13:53:06 +01001862 */
1863 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1864 status = chip->errstat(mtd, chip, FL_WRITING, status,
1865 page);
1866
1867 if (status & NAND_STATUS_FAIL)
1868 return -EIO;
1869 } else {
1870 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1871 status = chip->waitfunc(mtd, chip);
1872 }
1873
1874#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1875 /* Send command to read back the data */
1876 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1877
1878 if (chip->verify_buf(mtd, buf, mtd->writesize))
1879 return -EIO;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001880
1881 /* Make sure the next page prog is preceded by a status read */
1882 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01001883#endif
1884 return 0;
1885}
1886
1887/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001888 * nand_fill_oob - [INTERN] Transfer client buffer to oob
1889 * @mtd: MTD device structure
1890 * @oob: oob data buffer
1891 * @len: oob data write length
1892 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001893 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001894static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
1895 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01001896{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001897 struct nand_chip *chip = mtd->priv;
1898
1899 /*
1900 * Initialise to all 0xFF, to avoid the possibility of left over OOB
1901 * data from a previous OOB read.
1902 */
1903 memset(chip->oob_poi, 0xff, mtd->oobsize);
1904
Christian Hitz90e3f392011-10-12 09:32:01 +02001905 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001906
Sergey Lapindfe64e22013-01-14 03:46:50 +00001907 case MTD_OPS_PLACE_OOB:
1908 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001909 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1910 return oob + len;
1911
Sergey Lapindfe64e22013-01-14 03:46:50 +00001912 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001913 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1914 uint32_t boffs = 0, woffs = ops->ooboffs;
1915 size_t bytes = 0;
1916
Christian Hitz90e3f392011-10-12 09:32:01 +02001917 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001918 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001919 if (unlikely(woffs)) {
1920 if (woffs >= free->length) {
1921 woffs -= free->length;
1922 continue;
1923 }
1924 boffs = free->offset + woffs;
1925 bytes = min_t(size_t, len,
1926 (free->length - woffs));
1927 woffs = 0;
1928 } else {
1929 bytes = min_t(size_t, len, free->length);
1930 boffs = free->offset;
1931 }
1932 memcpy(chip->oob_poi + boffs, oob, bytes);
1933 oob += bytes;
1934 }
1935 return oob;
1936 }
1937 default:
1938 BUG();
1939 }
1940 return NULL;
1941}
1942
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001943#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01001944
1945/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001946 * nand_do_write_ops - [INTERN] NAND write with ECC
1947 * @mtd: MTD device structure
1948 * @to: offset to write to
1949 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001950 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001951 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01001952 */
1953static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1954 struct mtd_oob_ops *ops)
1955{
1956 int chipnr, realpage, page, blockmask, column;
1957 struct nand_chip *chip = mtd->priv;
1958 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001959
1960 uint32_t oobwritelen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001961 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001962 mtd->oobavail : mtd->oobsize;
1963
William Juulcfa460a2007-10-31 13:53:06 +01001964 uint8_t *oob = ops->oobbuf;
1965 uint8_t *buf = ops->datbuf;
1966 int ret, subpage;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001967 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001968
1969 ops->retlen = 0;
1970 if (!writelen)
1971 return 0;
1972
William Juulcfa460a2007-10-31 13:53:06 +01001973 column = to & (mtd->writesize - 1);
1974 subpage = column || (writelen & (mtd->writesize - 1));
1975
1976 if (subpage && oob)
1977 return -EINVAL;
1978
1979 chipnr = (int)(to >> chip->chip_shift);
1980 chip->select_chip(mtd, chipnr);
1981
1982 /* Check, if it is write protected */
1983 if (nand_check_wp(mtd)) {
1984 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1985 return -EIO;
1986 }
1987
1988 realpage = (int)(to >> chip->page_shift);
1989 page = realpage & chip->pagemask;
1990 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1991
1992 /* Invalidate the page cache, when we write to the cached page */
1993 if (to <= (chip->pagebuf << chip->page_shift) &&
1994 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1995 chip->pagebuf = -1;
1996
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001997 /* Don't allow multipage oob writes with offset */
1998 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
1999 return -EINVAL;
2000
Christian Hitz90e3f392011-10-12 09:32:01 +02002001 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002002 WATCHDOG_RESET();
2003
William Juulcfa460a2007-10-31 13:53:06 +01002004 int bytes = mtd->writesize;
2005 int cached = writelen > bytes && page != blockmask;
2006 uint8_t *wbuf = buf;
2007
Sergey Lapindfe64e22013-01-14 03:46:50 +00002008 /* Partial page write? */
htbegin070fd8e2013-03-01 22:59:27 +00002009 if (unlikely(column || writelen < mtd->writesize)) {
William Juulcfa460a2007-10-31 13:53:06 +01002010 cached = 0;
2011 bytes = min_t(int, bytes - column, (int) writelen);
2012 chip->pagebuf = -1;
2013 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2014 memcpy(&chip->buffers->databuf[column], buf, bytes);
2015 wbuf = chip->buffers->databuf;
2016 }
2017
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002018 if (unlikely(oob)) {
2019 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002020 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002021 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002022 } else {
2023 /* We still need to erase leftover OOB data */
2024 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002025 }
William Juulcfa460a2007-10-31 13:53:06 +01002026
Sergey Lapindfe64e22013-01-14 03:46:50 +00002027 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2028 cached, (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002029 if (ret)
2030 break;
2031
2032 writelen -= bytes;
2033 if (!writelen)
2034 break;
2035
2036 column = 0;
2037 buf += bytes;
2038 realpage++;
2039
2040 page = realpage & chip->pagemask;
2041 /* Check, if we cross a chip boundary */
2042 if (!page) {
2043 chipnr++;
2044 chip->select_chip(mtd, -1);
2045 chip->select_chip(mtd, chipnr);
2046 }
2047 }
2048
2049 ops->retlen = ops->len - writelen;
2050 if (unlikely(oob))
2051 ops->oobretlen = ops->ooblen;
2052 return ret;
2053}
2054
2055/**
2056 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002057 * @mtd: MTD device structure
2058 * @to: offset to write to
2059 * @len: number of bytes to write
2060 * @retlen: pointer to variable to store the number of written bytes
2061 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002062 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002063 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002064 */
2065static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2066 size_t *retlen, const uint8_t *buf)
2067{
2068 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002069 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002070 int ret;
2071
William Juulcfa460a2007-10-31 13:53:06 +01002072 nand_get_device(chip, mtd, FL_WRITING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002073 ops.len = len;
2074 ops.datbuf = (uint8_t *)buf;
2075 ops.oobbuf = NULL;
2076 ops.mode = MTD_OPS_PLACE_OOB;
2077 ret = nand_do_write_ops(mtd, to, &ops);
2078 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002079 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002080 return ret;
2081}
2082
2083/**
2084 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002085 * @mtd: MTD device structure
2086 * @to: offset to write to
2087 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002088 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002089 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002090 */
William Juulcfa460a2007-10-31 13:53:06 +01002091static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2092 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002093{
William Juulcfa460a2007-10-31 13:53:06 +01002094 int chipnr, page, status, len;
2095 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002096
Christian Hitz90e3f392011-10-12 09:32:01 +02002097 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2098 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002099
Sergey Lapindfe64e22013-01-14 03:46:50 +00002100 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002101 len = chip->ecc.layout->oobavail;
2102 else
2103 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002104
2105 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002106 if ((ops->ooboffs + ops->ooblen) > len) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002107 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2108 "past end of page\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002109 return -EINVAL;
2110 }
2111
William Juulcfa460a2007-10-31 13:53:06 +01002112 if (unlikely(ops->ooboffs >= len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002113 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2114 "write outside oob\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002115 return -EINVAL;
2116 }
2117
Christian Hitz90e3f392011-10-12 09:32:01 +02002118 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002119 if (unlikely(to >= mtd->size ||
2120 ops->ooboffs + ops->ooblen >
2121 ((mtd->size >> chip->page_shift) -
2122 (to >> chip->page_shift)) * len)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002123 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2124 "end of device\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002125 return -EINVAL;
2126 }
2127
William Juulcfa460a2007-10-31 13:53:06 +01002128 chipnr = (int)(to >> chip->chip_shift);
2129 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002130
William Juulcfa460a2007-10-31 13:53:06 +01002131 /* Shift to get page */
2132 page = (int)(to >> chip->page_shift);
2133
2134 /*
2135 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2136 * of my DiskOnChip 2000 test units) will clear the whole data page too
2137 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2138 * it in the doc2000 driver in August 1999. dwmw2.
2139 */
2140 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002141
2142 /* Check, if it is write protected */
2143 if (nand_check_wp(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002144 return -EROFS;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002145
Wolfgang Denk932394a2005-08-17 12:55:25 +02002146 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002147 if (page == chip->pagebuf)
2148 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002149
Sergey Lapindfe64e22013-01-14 03:46:50 +00002150 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2151
2152 if (ops->mode == MTD_OPS_RAW)
2153 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2154 else
2155 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002156
William Juulcfa460a2007-10-31 13:53:06 +01002157 if (status)
2158 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002159
William Juulcfa460a2007-10-31 13:53:06 +01002160 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002161
William Juulcfa460a2007-10-31 13:53:06 +01002162 return 0;
2163}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002164
William Juulcfa460a2007-10-31 13:53:06 +01002165/**
2166 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002167 * @mtd: MTD device structure
2168 * @to: offset to write to
2169 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002170 */
2171static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2172 struct mtd_oob_ops *ops)
2173{
2174 struct nand_chip *chip = mtd->priv;
2175 int ret = -ENOTSUPP;
2176
2177 ops->retlen = 0;
2178
2179 /* Do not allow writes past end of device */
2180 if (ops->datbuf && (to + ops->len) > mtd->size) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002181 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2182 "end of device\n", __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002183 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002184 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002185
William Juulcfa460a2007-10-31 13:53:06 +01002186 nand_get_device(chip, mtd, FL_WRITING);
2187
Christian Hitz90e3f392011-10-12 09:32:01 +02002188 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002189 case MTD_OPS_PLACE_OOB:
2190 case MTD_OPS_AUTO_OOB:
2191 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002192 break;
2193
2194 default:
2195 goto out;
2196 }
2197
2198 if (!ops->datbuf)
2199 ret = nand_do_write_oob(mtd, to, ops);
2200 else
2201 ret = nand_do_write_ops(mtd, to, ops);
2202
Christian Hitz90e3f392011-10-12 09:32:01 +02002203out:
William Juulcfa460a2007-10-31 13:53:06 +01002204 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002205 return ret;
2206}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002207
2208/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002209 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2210 * @mtd: MTD device structure
2211 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002212 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002213 * Standard erase command for NAND chips.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002214 */
William Juulcfa460a2007-10-31 13:53:06 +01002215static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002216{
William Juulcfa460a2007-10-31 13:53:06 +01002217 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002218 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002219 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2220 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002221}
2222
2223/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002224 * multi_erase_cmd - [GENERIC] AND specific block erase command function
2225 * @mtd: MTD device structure
2226 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002227 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002228 * AND multi block erase command function. Erase 4 consecutive blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002229 */
William Juulcfa460a2007-10-31 13:53:06 +01002230static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002231{
William Juulcfa460a2007-10-31 13:53:06 +01002232 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002233 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002234 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2235 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2236 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2237 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2238 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002239}
2240
2241/**
2242 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002243 * @mtd: MTD device structure
2244 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002245 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002246 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002247 */
William Juulcfa460a2007-10-31 13:53:06 +01002248static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002249{
William Juulcfa460a2007-10-31 13:53:06 +01002250 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002251}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002252
William Juulcfa460a2007-10-31 13:53:06 +01002253#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk932394a2005-08-17 12:55:25 +02002254/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002255 * nand_erase_nand - [INTERN] erase block(s)
2256 * @mtd: MTD device structure
2257 * @instr: erase instruction
2258 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002259 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002260 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002261 */
William Juulcfa460a2007-10-31 13:53:06 +01002262int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2263 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002264{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002265 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002266 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002267 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juulcfa460a2007-10-31 13:53:06 +01002268 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002269 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002270
Christian Hitz90e3f392011-10-12 09:32:01 +02002271 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2272 __func__, (unsigned long long)instr->addr,
2273 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002274
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002275 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002276 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002277
Wolfgang Denk932394a2005-08-17 12:55:25 +02002278 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002279 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002280
2281 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002282 page = (int)(instr->addr >> chip->page_shift);
2283 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002284
2285 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002286 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002287
Wolfgang Denk932394a2005-08-17 12:55:25 +02002288 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002289 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002290
Wolfgang Denk932394a2005-08-17 12:55:25 +02002291 /* Check, if it is write protected */
2292 if (nand_check_wp(mtd)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002293 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2294 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002295 instr->state = MTD_ERASE_FAILED;
2296 goto erase_exit;
2297 }
2298
William Juulcfa460a2007-10-31 13:53:06 +01002299 /*
2300 * If BBT requires refresh, set the BBT page mask to see if the BBT
2301 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2302 * can not be matched. This is also done when the bbt is actually
Sergey Lapindfe64e22013-01-14 03:46:50 +00002303 * erased to avoid recursive updates.
William Juulcfa460a2007-10-31 13:53:06 +01002304 */
2305 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2306 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2307
Wolfgang Denk932394a2005-08-17 12:55:25 +02002308 /* Loop through the pages */
2309 len = instr->len;
2310
2311 instr->state = MTD_ERASING;
2312
2313 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002314 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00002315 /* Check if we have a bad block, we do not erase bad blocks! */
Marek Vasut6d414192011-09-12 06:04:06 +02002316 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002317 chip->page_shift, 0, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002318 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2319 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002320 instr->state = MTD_ERASE_FAILED;
2321 goto erase_exit;
2322 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002323
William Juulcfa460a2007-10-31 13:53:06 +01002324 /*
2325 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002326 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002327 */
2328 if (page <= chip->pagebuf && chip->pagebuf <
2329 (page + pages_per_block))
2330 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002331
William Juulcfa460a2007-10-31 13:53:06 +01002332 chip->erase_cmd(mtd, page & chip->pagemask);
2333
2334 status = chip->waitfunc(mtd, chip);
2335
2336 /*
2337 * See if operation failed and additional status checks are
2338 * available
2339 */
2340 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2341 status = chip->errstat(mtd, chip, FL_ERASING,
2342 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002343
2344 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002345 if (status & NAND_STATUS_FAIL) {
Christian Hitz90e3f392011-10-12 09:32:01 +02002346 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2347 "page 0x%08x\n", __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002348 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002349 instr->fail_addr =
2350 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002351 goto erase_exit;
2352 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002353
William Juulcfa460a2007-10-31 13:53:06 +01002354 /*
2355 * If BBT requires refresh, set the BBT rewrite flag to the
Sergey Lapindfe64e22013-01-14 03:46:50 +00002356 * page being erased.
William Juulcfa460a2007-10-31 13:53:06 +01002357 */
2358 if (bbt_masked_page != 0xffffffff &&
2359 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002360 rewrite_bbt[chipnr] =
2361 ((loff_t)page << chip->page_shift);
William Juulcfa460a2007-10-31 13:53:06 +01002362
Wolfgang Denk932394a2005-08-17 12:55:25 +02002363 /* Increment page address and decrement length */
William Juulcfa460a2007-10-31 13:53:06 +01002364 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002365 page += pages_per_block;
2366
2367 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002368 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002369 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002370 chip->select_chip(mtd, -1);
2371 chip->select_chip(mtd, chipnr);
2372
2373 /*
2374 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Sergey Lapindfe64e22013-01-14 03:46:50 +00002375 * page mask to see if this BBT should be rewritten.
William Juulcfa460a2007-10-31 13:53:06 +01002376 */
2377 if (bbt_masked_page != 0xffffffff &&
2378 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2379 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2380 BBT_PAGE_MASK;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002381 }
2382 }
2383 instr->state = MTD_ERASE_DONE;
2384
Christian Hitz90e3f392011-10-12 09:32:01 +02002385erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002386
2387 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002388
2389 /* Deselect and wake up anyone waiting on the device */
2390 nand_release_device(mtd);
2391
Scott Woodc45912d2008-10-24 16:20:43 -05002392 /* Do call back function */
2393 if (!ret)
2394 mtd_erase_callback(instr);
2395
William Juulcfa460a2007-10-31 13:53:06 +01002396 /*
2397 * If BBT requires refresh and erase was successful, rewrite any
Sergey Lapindfe64e22013-01-14 03:46:50 +00002398 * selected bad block tables.
William Juulcfa460a2007-10-31 13:53:06 +01002399 */
2400 if (bbt_masked_page == 0xffffffff || ret)
2401 return ret;
2402
2403 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2404 if (!rewrite_bbt[chipnr])
2405 continue;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002406 /* Update the BBT for chip */
Christian Hitz90e3f392011-10-12 09:32:01 +02002407 MTDDEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2408 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2409 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
William Juulcfa460a2007-10-31 13:53:06 +01002410 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2411 }
2412
Wolfgang Denk932394a2005-08-17 12:55:25 +02002413 /* Return more or less happy */
2414 return ret;
2415}
2416
2417/**
2418 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002419 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002420 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002421 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002422 */
William Juulcfa460a2007-10-31 13:53:06 +01002423static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002424{
William Juulcfa460a2007-10-31 13:53:06 +01002425 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002426
Christian Hitz90e3f392011-10-12 09:32:01 +02002427 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002428
2429 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002430 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002431 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002432 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002433}
2434
Wolfgang Denk932394a2005-08-17 12:55:25 +02002435/**
William Juulcfa460a2007-10-31 13:53:06 +01002436 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002437 * @mtd: MTD device structure
2438 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002439 */
William Juulcfa460a2007-10-31 13:53:06 +01002440static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002441{
William Juulcfa460a2007-10-31 13:53:06 +01002442 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002443}
2444
2445/**
William Juulcfa460a2007-10-31 13:53:06 +01002446 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002447 * @mtd: MTD device structure
2448 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002449 */
William Juulcfa460a2007-10-31 13:53:06 +01002450static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002451{
William Juulcfa460a2007-10-31 13:53:06 +01002452 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002453 int ret;
2454
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002455 ret = nand_block_isbad(mtd, ofs);
2456 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002457 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002458 if (ret > 0)
2459 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002460 return ret;
2461 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002462
William Juulcfa460a2007-10-31 13:53:06 +01002463 return chip->block_markbad(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002464}
2465
Sergey Lapindfe64e22013-01-14 03:46:50 +00002466 /**
2467 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2468 * @mtd: MTD device structure
2469 * @chip: nand chip info structure
2470 * @addr: feature address.
2471 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01002472 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002473static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2474 int addr, uint8_t *subfeature_param)
2475{
2476 int status;
2477
2478 if (!chip->onfi_version)
2479 return -EINVAL;
2480
2481 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2482 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2483 status = chip->waitfunc(mtd, chip);
2484 if (status & NAND_STATUS_FAIL)
2485 return -EIO;
2486 return 0;
2487}
2488
2489/**
2490 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2491 * @mtd: MTD device structure
2492 * @chip: nand chip info structure
2493 * @addr: feature address.
2494 * @subfeature_param: the subfeature parameters, a four bytes array.
2495 */
2496static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2497 int addr, uint8_t *subfeature_param)
2498{
2499 if (!chip->onfi_version)
2500 return -EINVAL;
2501
2502 /* clear the sub feature parameters */
2503 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2504
2505 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2506 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2507 return 0;
2508}
2509
2510/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01002511static void nand_set_defaults(struct nand_chip *chip, int busw)
2512{
2513 /* check for proper chip_delay setup, set 20us if not */
2514 if (!chip->chip_delay)
2515 chip->chip_delay = 20;
2516
2517 /* check, if a user supplied command function given */
2518 if (chip->cmdfunc == NULL)
2519 chip->cmdfunc = nand_command;
2520
2521 /* check, if a user supplied wait function given */
2522 if (chip->waitfunc == NULL)
2523 chip->waitfunc = nand_wait;
2524
2525 if (!chip->select_chip)
2526 chip->select_chip = nand_select_chip;
2527 if (!chip->read_byte)
2528 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2529 if (!chip->read_word)
2530 chip->read_word = nand_read_word;
2531 if (!chip->block_bad)
2532 chip->block_bad = nand_block_bad;
2533 if (!chip->block_markbad)
2534 chip->block_markbad = nand_default_block_markbad;
2535 if (!chip->write_buf)
2536 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2537 if (!chip->read_buf)
2538 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2539 if (!chip->verify_buf)
2540 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2541 if (!chip->scan_bbt)
2542 chip->scan_bbt = nand_default_bbt;
Scott Wood5b8e6bb2010-08-25 17:42:49 -05002543 if (!chip->controller)
William Juulcfa460a2007-10-31 13:53:06 +01002544 chip->controller = &chip->hwcontrol;
William Juulcfa460a2007-10-31 13:53:06 +01002545}
2546
Florian Fainelli0272c712011-02-25 00:01:34 +00002547#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Sergey Lapindfe64e22013-01-14 03:46:50 +00002548/* Sanitize ONFI strings so we can safely print them */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002549static void sanitize_string(char *s, size_t len)
2550{
2551 ssize_t i;
2552
Sergey Lapindfe64e22013-01-14 03:46:50 +00002553 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002554 s[len - 1] = 0;
2555
Sergey Lapindfe64e22013-01-14 03:46:50 +00002556 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002557 for (i = 0; i < len - 1; i++) {
2558 if (s[i] < ' ' || s[i] > 127)
2559 s[i] = '?';
2560 }
2561
Sergey Lapindfe64e22013-01-14 03:46:50 +00002562 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002563 strim(s);
2564}
2565
Florian Fainelli0272c712011-02-25 00:01:34 +00002566static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01002567{
Florian Fainelli0272c712011-02-25 00:01:34 +00002568 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00002569 while (len--) {
2570 crc ^= *p++ << 8;
2571 for (i = 0; i < 8; i++)
2572 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05002573 }
2574
Florian Fainelli0272c712011-02-25 00:01:34 +00002575 return crc;
2576}
William Juulcfa460a2007-10-31 13:53:06 +01002577
Florian Fainelli0272c712011-02-25 00:01:34 +00002578/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002579 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00002580 */
Christian Hitz90e3f392011-10-12 09:32:01 +02002581static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00002582 int *busw)
2583{
2584 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05302585 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00002586 int val;
2587
Sergey Lapindfe64e22013-01-14 03:46:50 +00002588 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00002589 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2590 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2591 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2592 return 0;
2593
Florian Fainelli0272c712011-02-25 00:01:34 +00002594 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2595 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05302596 for (j = 0; j < sizeof(*p); j++)
2597 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00002598 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02002599 le16_to_cpu(p->crc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002600 pr_info("ONFI param page %d valid\n", i);
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002601 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00002602 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02002603 }
William Juulcfa460a2007-10-31 13:53:06 +01002604
Florian Fainelli0272c712011-02-25 00:01:34 +00002605 if (i == 3)
2606 return 0;
2607
Sergey Lapindfe64e22013-01-14 03:46:50 +00002608 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00002609 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002610 if (val & (1 << 5))
2611 chip->onfi_version = 23;
2612 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00002613 chip->onfi_version = 22;
2614 else if (val & (1 << 3))
2615 chip->onfi_version = 21;
2616 else if (val & (1 << 2))
2617 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002618 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00002619 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002620 else
2621 chip->onfi_version = 0;
2622
2623 if (!chip->onfi_version) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002624 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002625 return 0;
2626 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002627
Christian Hitz5454ddb2011-10-12 09:32:05 +02002628 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2629 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01002630 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00002631 mtd->name = p->model;
Florian Fainelli0272c712011-02-25 00:01:34 +00002632 mtd->writesize = le32_to_cpu(p->byte_per_page);
2633 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2634 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTETd62e9ca2012-03-19 15:35:25 +01002635 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2636 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Florian Fainelli0272c712011-02-25 00:01:34 +00002637 *busw = 0;
2638 if (le16_to_cpu(p->features) & 1)
2639 *busw = NAND_BUSWIDTH_16;
William Juulcfa460a2007-10-31 13:53:06 +01002640
Sergey Lapindfe64e22013-01-14 03:46:50 +00002641 pr_info("ONFI flash detected\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00002642 return 1;
2643}
2644#else
2645static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2646 struct nand_chip *chip,
2647 int *busw)
2648{
2649 return 0;
2650}
2651#endif
2652
Florian Fainelli0272c712011-02-25 00:01:34 +00002653/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002654 * nand_id_has_period - Check if an ID string has a given wraparound period
2655 * @id_data: the ID string
2656 * @arrlen: the length of the @id_data array
2657 * @period: the period of repitition
2658 *
2659 * Check if an ID string is repeated within a given sequence of bytes at
2660 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2661 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2662 * if the repetition has a period of @period; otherwise, returns zero.
2663 */
2664static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2665{
2666 int i, j;
2667 for (i = 0; i < period; i++)
2668 for (j = i + period; j < arrlen; j += period)
2669 if (id_data[i] != id_data[j])
2670 return 0;
2671 return 1;
2672}
2673
2674/*
2675 * nand_id_len - Get the length of an ID string returned by CMD_READID
2676 * @id_data: the ID string
2677 * @arrlen: the length of the @id_data array
2678
2679 * Returns the length of the ID string, according to known wraparound/trailing
2680 * zero patterns. If no pattern exists, returns the length of the array.
2681 */
2682static int nand_id_len(u8 *id_data, int arrlen)
2683{
2684 int last_nonzero, period;
2685
2686 /* Find last non-zero byte */
2687 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2688 if (id_data[last_nonzero])
2689 break;
2690
2691 /* All zeros */
2692 if (last_nonzero < 0)
2693 return 0;
2694
2695 /* Calculate wraparound period */
2696 for (period = 1; period < arrlen; period++)
2697 if (nand_id_has_period(id_data, arrlen, period))
2698 break;
2699
2700 /* There's a repeated pattern */
2701 if (period < arrlen)
2702 return period;
2703
2704 /* There are trailing zeros */
2705 if (last_nonzero < arrlen - 1)
2706 return last_nonzero + 1;
2707
2708 /* No pattern detected */
2709 return arrlen;
2710}
2711
2712/*
2713 * Many new NAND share similar device ID codes, which represent the size of the
2714 * chip. The rest of the parameters must be decoded according to generic or
2715 * manufacturer-specific "extended ID" decoding patterns.
2716 */
2717static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2718 u8 id_data[8], int *busw)
2719{
2720 int extid, id_len;
2721 /* The 3rd id byte holds MLC / multichip data */
2722 chip->cellinfo = id_data[2];
2723 /* The 4th id byte is the important one */
2724 extid = id_data[3];
2725
2726 id_len = nand_id_len(id_data, 8);
2727
2728 /*
2729 * Field definitions are in the following datasheets:
2730 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2731 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
2732 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
2733 *
2734 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2735 * ID to decide what to do.
2736 */
2737 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
2738 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2739 id_data[5] != 0x00) {
2740 /* Calc pagesize */
2741 mtd->writesize = 2048 << (extid & 0x03);
2742 extid >>= 2;
2743 /* Calc oobsize */
2744 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2745 case 1:
2746 mtd->oobsize = 128;
2747 break;
2748 case 2:
2749 mtd->oobsize = 218;
2750 break;
2751 case 3:
2752 mtd->oobsize = 400;
2753 break;
2754 case 4:
2755 mtd->oobsize = 436;
2756 break;
2757 case 5:
2758 mtd->oobsize = 512;
2759 break;
2760 case 6:
2761 default: /* Other cases are "reserved" (unknown) */
2762 mtd->oobsize = 640;
2763 break;
2764 }
2765 extid >>= 2;
2766 /* Calc blocksize */
2767 mtd->erasesize = (128 * 1024) <<
2768 (((extid >> 1) & 0x04) | (extid & 0x03));
2769 *busw = 0;
2770 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2771 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2772 unsigned int tmp;
2773
2774 /* Calc pagesize */
2775 mtd->writesize = 2048 << (extid & 0x03);
2776 extid >>= 2;
2777 /* Calc oobsize */
2778 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2779 case 0:
2780 mtd->oobsize = 128;
2781 break;
2782 case 1:
2783 mtd->oobsize = 224;
2784 break;
2785 case 2:
2786 mtd->oobsize = 448;
2787 break;
2788 case 3:
2789 mtd->oobsize = 64;
2790 break;
2791 case 4:
2792 mtd->oobsize = 32;
2793 break;
2794 case 5:
2795 mtd->oobsize = 16;
2796 break;
2797 default:
2798 mtd->oobsize = 640;
2799 break;
2800 }
2801 extid >>= 2;
2802 /* Calc blocksize */
2803 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
2804 if (tmp < 0x03)
2805 mtd->erasesize = (128 * 1024) << tmp;
2806 else if (tmp == 0x03)
2807 mtd->erasesize = 768 * 1024;
2808 else
2809 mtd->erasesize = (64 * 1024) << tmp;
2810 *busw = 0;
2811 } else {
2812 /* Calc pagesize */
2813 mtd->writesize = 1024 << (extid & 0x03);
2814 extid >>= 2;
2815 /* Calc oobsize */
2816 mtd->oobsize = (8 << (extid & 0x01)) *
2817 (mtd->writesize >> 9);
2818 extid >>= 2;
2819 /* Calc blocksize. Blocksize is multiples of 64KiB */
2820 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2821 extid >>= 2;
2822 /* Get buswidth information */
2823 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2824 }
2825}
2826
2827 /*
2828 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
2829 * decodes a matching ID table entry and assigns the MTD size parameters for
2830 * the chip.
2831 */
2832static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
2833 const struct nand_flash_dev *type, u8 id_data[8],
2834 int *busw)
2835{
2836 int maf_id = id_data[0];
2837
2838 mtd->erasesize = type->erasesize;
2839 mtd->writesize = type->pagesize;
2840 mtd->oobsize = mtd->writesize / 32;
2841 *busw = type->options & NAND_BUSWIDTH_16;
2842
2843 /*
2844 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
2845 * some Spansion chips have erasesize that conflicts with size
2846 * listed in nand_ids table.
2847 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
2848 */
2849 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
2850 && id_data[6] == 0x00 && id_data[7] == 0x00
2851 && mtd->writesize == 512) {
2852 mtd->erasesize = 128 * 1024;
2853 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
2854 }
2855}
2856
2857 /*
2858 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
2859 * heuristic patterns using various detected parameters (e.g., manufacturer,
2860 * page size, cell-type information).
2861 */
2862static void nand_decode_bbm_options(struct mtd_info *mtd,
2863 struct nand_chip *chip, u8 id_data[8])
2864{
2865 int maf_id = id_data[0];
2866
2867 /* Set the bad block position */
2868 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
2869 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2870 else
2871 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2872
2873 /*
2874 * Bad block marker is stored in the last page of each block on Samsung
2875 * and Hynix MLC devices; stored in first two pages of each block on
2876 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
2877 * AMD/Spansion, and Macronix. All others scan only the first page.
2878 */
2879 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2880 (maf_id == NAND_MFR_SAMSUNG ||
2881 maf_id == NAND_MFR_HYNIX))
2882 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
2883 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2884 (maf_id == NAND_MFR_SAMSUNG ||
2885 maf_id == NAND_MFR_HYNIX ||
2886 maf_id == NAND_MFR_TOSHIBA ||
2887 maf_id == NAND_MFR_AMD ||
2888 maf_id == NAND_MFR_MACRONIX)) ||
2889 (mtd->writesize == 2048 &&
2890 maf_id == NAND_MFR_MICRON))
2891 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
2892}
2893
2894/*
2895 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00002896 */
2897static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2898 struct nand_chip *chip,
2899 int busw,
2900 int *maf_id, int *dev_id,
2901 const struct nand_flash_dev *type)
2902{
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00002903 const char *name;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002904 int i, maf_idx;
2905 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00002906
2907 /* Select the device */
2908 chip->select_chip(mtd, 0);
2909
2910 /*
2911 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002912 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00002913 */
2914 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2915
2916 /* Send the command for reading device ID */
2917 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2918
2919 /* Read manufacturer and device IDs */
2920 *maf_id = chip->read_byte(mtd);
2921 *dev_id = chip->read_byte(mtd);
2922
Sergey Lapindfe64e22013-01-14 03:46:50 +00002923 /*
2924 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00002925 * interface concerns can cause random data which looks like a
2926 * possibly credible NAND flash to appear. If the two results do
2927 * not match, ignore the device completely.
2928 */
2929
2930 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2931
Sergey Lapindfe64e22013-01-14 03:46:50 +00002932 /* Read entire ID string */
2933 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002934 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00002935
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002936 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002937 pr_info("%s: second ID read did not match "
2938 "%02x,%02x against %02x,%02x\n", __func__,
2939 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00002940 return ERR_PTR(-ENODEV);
2941 }
2942
2943 if (!type)
2944 type = nand_flash_ids;
2945
2946 for (; type->name != NULL; type++)
2947 if (*dev_id == type->id)
2948 break;
2949
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002950 chip->onfi_version = 0;
2951 if (!type->name || !type->pagesize) {
2952 /* Check is chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002953 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002954 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00002955 }
2956
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002957 if (!type->name)
2958 return ERR_PTR(-ENODEV);
2959
Florian Fainelli0272c712011-02-25 00:01:34 +00002960 if (!mtd->name)
2961 mtd->name = type->name;
2962
2963 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00002964
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002965 if (!type->pagesize && chip->init_size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002966 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002967 busw = chip->init_size(mtd, chip, id_data);
2968 } else if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002969 /* Decode parameters from extended ID */
2970 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002971 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002972 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002973 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002974 /* Get chip options, preserve non chip based options */
Marek Vasut9c790a72012-08-30 13:39:38 +00002975 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00002976
Sergey Lapindfe64e22013-01-14 03:46:50 +00002977 /*
2978 * Check if chip is not a Samsung device. Do not clear the
2979 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002980 */
2981 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2982 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2983ident_done:
2984
William Juulcfa460a2007-10-31 13:53:06 +01002985 /* Try to identify manufacturer */
2986 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2987 if (nand_manuf_ids[maf_idx].id == *maf_id)
2988 break;
2989 }
2990
2991 /*
2992 * Check, if buswidth is correct. Hardware drivers should set
Sergey Lapindfe64e22013-01-14 03:46:50 +00002993 * chip correct!
William Juulcfa460a2007-10-31 13:53:06 +01002994 */
2995 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002996 pr_info("NAND device: Manufacturer ID:"
2997 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2998 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2999 pr_warn("NAND bus width %d instead %d bit\n",
3000 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3001 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003002 return ERR_PTR(-EINVAL);
3003 }
3004
Sergey Lapindfe64e22013-01-14 03:46:50 +00003005 nand_decode_bbm_options(mtd, chip, id_data);
3006
William Juulcfa460a2007-10-31 13:53:06 +01003007 /* Calculate the address shift from the page size */
3008 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003009 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003010 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3011
3012 chip->bbt_erase_shift = chip->phys_erase_shift =
3013 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003014 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003015 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003016 else {
3017 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3018 chip->chip_shift += 32 - 1;
3019 }
3020
3021 chip->badblockbits = 8;
William Juulcfa460a2007-10-31 13:53:06 +01003022
William Juulcfa460a2007-10-31 13:53:06 +01003023 /* Check for AND chips with 4 page planes */
3024 if (chip->options & NAND_4PAGE_ARRAY)
3025 chip->erase_cmd = multi_erase_cmd;
3026 else
3027 chip->erase_cmd = single_erase_cmd;
3028
Sergey Lapindfe64e22013-01-14 03:46:50 +00003029 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003030 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3031 chip->cmdfunc = nand_command_lp;
3032
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00003033 name = type->name;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003034#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Kim Phillips7d2ab9a2012-10-29 13:34:46 +00003035 if (chip->onfi_version)
3036 name = chip->onfi_params.model;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003037#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003038 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3039 " page size: %d, OOB size: %d\n",
3040 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3041 name,
3042 mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003043
3044 return type;
3045}
3046
3047/**
3048 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003049 * @mtd: MTD device structure
3050 * @maxchips: number of chips to scan for
3051 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003052 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003053 * This is the first phase of the normal nand_scan() function. It reads the
3054 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003055 *
3056 * The mtd->owner field must be set to the module of the caller.
3057 */
Lei Wen245eb902011-01-06 09:48:18 +08003058int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3059 const struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003060{
Florian Fainelli0272c712011-02-25 00:01:34 +00003061 int i, busw, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01003062 struct nand_chip *chip = mtd->priv;
Mike Frysinger0bdecd82010-10-20 01:15:21 +00003063 const struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003064
3065 /* Get buswidth to select the correct functions */
3066 busw = chip->options & NAND_BUSWIDTH_16;
3067 /* Set the default functions */
3068 nand_set_defaults(chip, busw);
3069
3070 /* Read the flash type */
Christian Hitz90e3f392011-10-12 09:32:01 +02003071 type = nand_get_flash_type(mtd, chip, busw,
3072 &nand_maf_id, &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003073
3074 if (IS_ERR(type)) {
Peter Tyser10dc6a92009-02-04 13:39:40 -06003075#ifndef CONFIG_SYS_NAND_QUIET_TEST
Sergey Lapindfe64e22013-01-14 03:46:50 +00003076 pr_warn("No NAND device found\n");
Peter Tyser10dc6a92009-02-04 13:39:40 -06003077#endif
William Juulcfa460a2007-10-31 13:53:06 +01003078 chip->select_chip(mtd, -1);
3079 return PTR_ERR(type);
3080 }
3081
3082 /* Check for a chip array */
3083 for (i = 1; i < maxchips; i++) {
3084 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003085 /* See comment in nand_get_flash_type for reset */
3086 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003087 /* Send the command for reading device ID */
3088 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3089 /* Read manufacturer and device IDs */
3090 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainelli0272c712011-02-25 00:01:34 +00003091 nand_dev_id != chip->read_byte(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01003092 break;
3093 }
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003094#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003095 if (i > 1)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003096 pr_info("%d NAND chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003097#endif
William Juulcfa460a2007-10-31 13:53:06 +01003098
3099 /* Store the number of chips and calc total size for mtd */
3100 chip->numchips = i;
3101 mtd->size = i * chip->chipsize;
3102
3103 return 0;
3104}
3105
3106
3107/**
3108 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003109 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003110 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003111 * This is the second phase of the normal nand_scan() function. It fills out
3112 * all the uninitialized function pointers with the defaults and scans for a
3113 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003114 */
3115int nand_scan_tail(struct mtd_info *mtd)
3116{
3117 int i;
3118 struct nand_chip *chip = mtd->priv;
3119
Sergey Lapindfe64e22013-01-14 03:46:50 +00003120 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3121 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3122 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3123
William Juulcfa460a2007-10-31 13:53:06 +01003124 if (!(chip->options & NAND_OWN_BUFFERS))
Simon Glassb5725952012-07-29 20:53:25 +00003125 chip->buffers = memalign(ARCH_DMA_MINALIGN,
3126 sizeof(*chip->buffers));
William Juulcfa460a2007-10-31 13:53:06 +01003127 if (!chip->buffers)
3128 return -ENOMEM;
3129
3130 /* Set the internal oob buffer location, just after the page data */
3131 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3132
3133 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003134 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01003135 */
Christian Hitz4c6de852011-10-12 09:31:59 +02003136 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01003137 switch (mtd->oobsize) {
3138 case 8:
3139 chip->ecc.layout = &nand_oob_8;
3140 break;
3141 case 16:
3142 chip->ecc.layout = &nand_oob_16;
3143 break;
3144 case 64:
3145 chip->ecc.layout = &nand_oob_64;
3146 break;
3147 case 128:
3148 chip->ecc.layout = &nand_oob_128;
3149 break;
3150 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003151 pr_warn("No oob scheme defined for oobsize %d\n",
3152 mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003153 }
3154 }
3155
3156 if (!chip->write_page)
3157 chip->write_page = nand_write_page;
3158
Sergey Lapindfe64e22013-01-14 03:46:50 +00003159 /* set for ONFI nand */
3160 if (!chip->onfi_set_features)
3161 chip->onfi_set_features = nand_onfi_set_features;
3162 if (!chip->onfi_get_features)
3163 chip->onfi_get_features = nand_onfi_get_features;
3164
William Juulcfa460a2007-10-31 13:53:06 +01003165 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003166 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01003167 * selected and we have 256 byte pagesize fallback to software ECC
3168 */
William Juulcfa460a2007-10-31 13:53:06 +01003169
3170 switch (chip->ecc.mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003171 case NAND_ECC_HW_OOB_FIRST:
3172 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3173 if (!chip->ecc.calculate || !chip->ecc.correct ||
3174 !chip->ecc.hwctl) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003175 pr_warn("No ECC functions supplied; "
3176 "hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003177 BUG();
3178 }
3179 if (!chip->ecc.read_page)
3180 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3181
William Juulcfa460a2007-10-31 13:53:06 +01003182 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003183 /* Use standard hwecc read page function? */
William Juulcfa460a2007-10-31 13:53:06 +01003184 if (!chip->ecc.read_page)
3185 chip->ecc.read_page = nand_read_page_hwecc;
3186 if (!chip->ecc.write_page)
3187 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell7e866612009-11-07 16:27:01 -05003188 if (!chip->ecc.read_page_raw)
3189 chip->ecc.read_page_raw = nand_read_page_raw;
3190 if (!chip->ecc.write_page_raw)
3191 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003192 if (!chip->ecc.read_oob)
3193 chip->ecc.read_oob = nand_read_oob_std;
3194 if (!chip->ecc.write_oob)
3195 chip->ecc.write_oob = nand_write_oob_std;
3196
3197 case NAND_ECC_HW_SYNDROME:
Scott Wood41ef8c72008-03-18 15:29:14 -05003198 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3199 !chip->ecc.hwctl) &&
3200 (!chip->ecc.read_page ||
3201 chip->ecc.read_page == nand_read_page_hwecc ||
3202 !chip->ecc.write_page ||
3203 chip->ecc.write_page == nand_write_page_hwecc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003204 pr_warn("No ECC functions supplied; "
3205 "hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01003206 BUG();
3207 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00003208 /* Use standard syndrome read/write page function? */
William Juulcfa460a2007-10-31 13:53:06 +01003209 if (!chip->ecc.read_page)
3210 chip->ecc.read_page = nand_read_page_syndrome;
3211 if (!chip->ecc.write_page)
3212 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell7e866612009-11-07 16:27:01 -05003213 if (!chip->ecc.read_page_raw)
3214 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3215 if (!chip->ecc.write_page_raw)
3216 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01003217 if (!chip->ecc.read_oob)
3218 chip->ecc.read_oob = nand_read_oob_syndrome;
3219 if (!chip->ecc.write_oob)
3220 chip->ecc.write_oob = nand_write_oob_syndrome;
3221
Sergey Lapindfe64e22013-01-14 03:46:50 +00003222 if (mtd->writesize >= chip->ecc.size) {
3223 if (!chip->ecc.strength) {
3224 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3225 BUG();
3226 }
William Juulcfa460a2007-10-31 13:53:06 +01003227 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003228 }
3229 pr_warn("%d byte HW ECC not possible on "
3230 "%d byte page size, fallback to SW ECC\n",
3231 chip->ecc.size, mtd->writesize);
William Juulcfa460a2007-10-31 13:53:06 +01003232 chip->ecc.mode = NAND_ECC_SOFT;
3233
3234 case NAND_ECC_SOFT:
3235 chip->ecc.calculate = nand_calculate_ecc;
3236 chip->ecc.correct = nand_correct_data;
3237 chip->ecc.read_page = nand_read_page_swecc;
Scott Woodc45912d2008-10-24 16:20:43 -05003238 chip->ecc.read_subpage = nand_read_subpage;
William Juulcfa460a2007-10-31 13:53:06 +01003239 chip->ecc.write_page = nand_write_page_swecc;
David Brownell7e866612009-11-07 16:27:01 -05003240 chip->ecc.read_page_raw = nand_read_page_raw;
3241 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003242 chip->ecc.read_oob = nand_read_oob_std;
3243 chip->ecc.write_oob = nand_write_oob_std;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003244 if (!chip->ecc.size)
3245 chip->ecc.size = 256;
William Juulcfa460a2007-10-31 13:53:06 +01003246 chip->ecc.bytes = 3;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003247 chip->ecc.strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01003248 break;
3249
Christian Hitz4c6de852011-10-12 09:31:59 +02003250 case NAND_ECC_SOFT_BCH:
3251 if (!mtd_nand_has_bch()) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003252 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Christian Hitz4c6de852011-10-12 09:31:59 +02003253 return -EINVAL;
3254 }
3255 chip->ecc.calculate = nand_bch_calculate_ecc;
3256 chip->ecc.correct = nand_bch_correct_data;
3257 chip->ecc.read_page = nand_read_page_swecc;
3258 chip->ecc.read_subpage = nand_read_subpage;
3259 chip->ecc.write_page = nand_write_page_swecc;
3260 chip->ecc.read_page_raw = nand_read_page_raw;
3261 chip->ecc.write_page_raw = nand_write_page_raw;
3262 chip->ecc.read_oob = nand_read_oob_std;
3263 chip->ecc.write_oob = nand_write_oob_std;
3264 /*
3265 * Board driver should supply ecc.size and ecc.bytes values to
3266 * select how many bits are correctable; see nand_bch_init()
Sergey Lapindfe64e22013-01-14 03:46:50 +00003267 * for details. Otherwise, default to 4 bits for large page
3268 * devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02003269 */
3270 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3271 chip->ecc.size = 512;
3272 chip->ecc.bytes = 7;
3273 }
3274 chip->ecc.priv = nand_bch_init(mtd,
3275 chip->ecc.size,
3276 chip->ecc.bytes,
3277 &chip->ecc.layout);
3278 if (!chip->ecc.priv)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003279 pr_warn("BCH ECC initialization failed!\n");
3280 chip->ecc.strength =
3281 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Christian Hitz4c6de852011-10-12 09:31:59 +02003282 break;
3283
William Juulcfa460a2007-10-31 13:53:06 +01003284 case NAND_ECC_NONE:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003285 pr_warn("NAND_ECC_NONE selected by board driver. "
Wolfgang Denk93e14592013-10-04 17:43:24 +02003286 "This is not recommended !!\n");
William Juulcfa460a2007-10-31 13:53:06 +01003287 chip->ecc.read_page = nand_read_page_raw;
3288 chip->ecc.write_page = nand_write_page_raw;
3289 chip->ecc.read_oob = nand_read_oob_std;
David Brownell7e866612009-11-07 16:27:01 -05003290 chip->ecc.read_page_raw = nand_read_page_raw;
3291 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01003292 chip->ecc.write_oob = nand_write_oob_std;
3293 chip->ecc.size = mtd->writesize;
3294 chip->ecc.bytes = 0;
3295 break;
3296
3297 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003298 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
William Juulcfa460a2007-10-31 13:53:06 +01003299 BUG();
3300 }
3301
Sergey Lapindfe64e22013-01-14 03:46:50 +00003302 /* For many systems, the standard OOB write also works for raw */
3303 if (!chip->ecc.read_oob_raw)
3304 chip->ecc.read_oob_raw = chip->ecc.read_oob;
3305 if (!chip->ecc.write_oob_raw)
3306 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3307
William Juulcfa460a2007-10-31 13:53:06 +01003308 /*
3309 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00003310 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01003311 */
3312 chip->ecc.layout->oobavail = 0;
Sandeep Paulraj5df3c2b2009-11-07 14:25:18 -05003313 for (i = 0; chip->ecc.layout->oobfree[i].length
3314 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juulcfa460a2007-10-31 13:53:06 +01003315 chip->ecc.layout->oobavail +=
3316 chip->ecc.layout->oobfree[i].length;
3317 mtd->oobavail = chip->ecc.layout->oobavail;
3318
3319 /*
3320 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00003321 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01003322 */
3323 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Christian Hitz90e3f392011-10-12 09:32:01 +02003324 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003325 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01003326 BUG();
3327 }
3328 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
3329
Sergey Lapindfe64e22013-01-14 03:46:50 +00003330 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
William Juulcfa460a2007-10-31 13:53:06 +01003331 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3332 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Christian Hitz90e3f392011-10-12 09:32:01 +02003333 switch (chip->ecc.steps) {
William Juulcfa460a2007-10-31 13:53:06 +01003334 case 2:
3335 mtd->subpage_sft = 1;
3336 break;
3337 case 4:
3338 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05003339 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01003340 mtd->subpage_sft = 2;
3341 break;
3342 }
3343 }
3344 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3345
3346 /* Initialize state */
3347 chip->state = FL_READY;
3348
3349 /* De-select the device */
3350 chip->select_chip(mtd, -1);
3351
3352 /* Invalidate the pagebuffer reference */
3353 chip->pagebuf = -1;
3354
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00003355 /* Large page NAND with SOFT_ECC should support subpage reads */
3356 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3357 chip->options |= NAND_SUBPAGE_READ;
3358
William Juulcfa460a2007-10-31 13:53:06 +01003359 /* Fill in remaining MTD driver data */
3360 mtd->type = MTD_NANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003361 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3362 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003363 mtd->_erase = nand_erase;
3364 mtd->_point = NULL;
3365 mtd->_unpoint = NULL;
3366 mtd->_read = nand_read;
3367 mtd->_write = nand_write;
3368 mtd->_read_oob = nand_read_oob;
3369 mtd->_write_oob = nand_write_oob;
3370 mtd->_sync = nand_sync;
3371 mtd->_lock = NULL;
3372 mtd->_unlock = NULL;
3373 mtd->_block_isbad = nand_block_isbad;
3374 mtd->_block_markbad = nand_block_markbad;
William Juulcfa460a2007-10-31 13:53:06 +01003375
Sergey Lapindfe64e22013-01-14 03:46:50 +00003376 /* propagate ecc info to mtd_info */
William Juulcfa460a2007-10-31 13:53:06 +01003377 mtd->ecclayout = chip->ecc.layout;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003378 mtd->ecc_strength = chip->ecc.strength;
3379 /*
3380 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3381 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3382 * properly set.
3383 */
3384 if (!mtd->bitflip_threshold)
3385 mtd->bitflip_threshold = mtd->ecc_strength;
William Juulcfa460a2007-10-31 13:53:06 +01003386
3387 /* Check, if we should skip the bad block table scan */
3388 if (chip->options & NAND_SKIP_BBTSCAN)
Scott Woodfb494542012-02-20 14:50:39 -06003389 chip->options |= NAND_BBT_SCANNED;
William Juulcfa460a2007-10-31 13:53:06 +01003390
Scott Woodfb494542012-02-20 14:50:39 -06003391 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01003392}
3393
William Juulcfa460a2007-10-31 13:53:06 +01003394/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02003395 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003396 * @mtd: MTD device structure
3397 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02003398 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003399 * This fills out all the uninitialized function pointers with the defaults.
3400 * The flash ID is read and the mtd/chip structures are filled with the
3401 * appropriate values. The mtd->owner field must be set to the module of the
3402 * caller.
Wolfgang Denk932394a2005-08-17 12:55:25 +02003403 */
William Juulcfa460a2007-10-31 13:53:06 +01003404int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003405{
William Juulcfa460a2007-10-31 13:53:06 +01003406 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003407
Lei Wen245eb902011-01-06 09:48:18 +08003408 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01003409 if (!ret)
3410 ret = nand_scan_tail(mtd);
3411 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003412}
3413
3414/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02003415 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003416 * @mtd: MTD device structure
3417 */
William Juulcfa460a2007-10-31 13:53:06 +01003418void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003419{
William Juulcfa460a2007-10-31 13:53:06 +01003420 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003421
Christian Hitz4c6de852011-10-12 09:31:59 +02003422 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3423 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3424
Wolfgang Denk932394a2005-08-17 12:55:25 +02003425#ifdef CONFIG_MTD_PARTITIONS
3426 /* Deregister partitions */
William Juulcfa460a2007-10-31 13:53:06 +01003427 del_mtd_partitions(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003428#endif
William Juulcfa460a2007-10-31 13:53:06 +01003429
3430 /* Free bad block table memory */
3431 kfree(chip->bbt);
3432 if (!(chip->options & NAND_OWN_BUFFERS))
3433 kfree(chip->buffers);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003434
3435 /* Free bad block descriptor memory */
3436 if (chip->badblock_pattern && chip->badblock_pattern->options
3437 & NAND_BBT_DYNAMICSTRUCT)
3438 kfree(chip->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003439}