blob: b03953f5b9cc062a8cb09206dfe249ef3a5da0a7 [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
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Scott 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>
Wolfgang Denk932394a2005-08-17 12:55:25 +020042#include <linux/mtd/compat.h>
43#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,
74 .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,
82 . 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,
93 .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 = {
Wolfgang Denkd1a24f02011-02-02 22:36:10 +010099 80, 81, 82, 83, 84, 85, 86, 87,
100 88, 89, 90, 91, 92, 93, 94, 95,
William Juulcfa460a2007-10-31 13:53:06 +0100101 96, 97, 98, 99, 100, 101, 102, 103,
102 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,
107 .length = 78}}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200108};
109
William Juulcfa460a2007-10-31 13:53:06 +0100110
111static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
112 int new_state);
113
114static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
115 struct mtd_oob_ops *ops);
116
117static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200118
Wolfgang Denk932394a2005-08-17 12:55:25 +0200119/**
120 * nand_release_device - [GENERIC] release chip
121 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200122 *
123 * Deselect, release chip lock and wake up anyone waiting on the device
Wolfgang Denk932394a2005-08-17 12:55:25 +0200124 */
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100125static void nand_release_device (struct mtd_info *mtd)
126{
127 struct nand_chip *this = mtd->priv;
128 this->select_chip(mtd, -1); /* De-select the NAND device */
129}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200130
131/**
132 * nand_read_byte - [DEFAULT] read one byte from the chip
133 * @mtd: MTD device structure
134 *
135 * Default read function for 8bit buswith
136 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000137uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200138{
William Juulcfa460a2007-10-31 13:53:06 +0100139 struct nand_chip *chip = mtd->priv;
140 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200141}
142
143/**
144 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
145 * @mtd: MTD device structure
146 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200147 * Default read function for 16bit buswith with
Wolfgang Denk932394a2005-08-17 12:55:25 +0200148 * endianess conversion
149 */
William Juulcfa460a2007-10-31 13:53:06 +0100150static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200151{
William Juulcfa460a2007-10-31 13:53:06 +0100152 struct nand_chip *chip = mtd->priv;
153 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200154}
155
156/**
157 * nand_read_word - [DEFAULT] read one word from the chip
158 * @mtd: MTD device structure
159 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200160 * Default read function for 16bit buswith without
Wolfgang Denk932394a2005-08-17 12:55:25 +0200161 * endianess conversion
162 */
163static u16 nand_read_word(struct mtd_info *mtd)
164{
William Juulcfa460a2007-10-31 13:53:06 +0100165 struct nand_chip *chip = mtd->priv;
166 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200167}
168
169/**
170 * nand_select_chip - [DEFAULT] control CE line
171 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +0100172 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200173 *
174 * Default select function for 1 chip devices.
175 */
William Juulcfa460a2007-10-31 13:53:06 +0100176static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200177{
William Juulcfa460a2007-10-31 13:53:06 +0100178 struct nand_chip *chip = mtd->priv;
179
180 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200181 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100182 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200183 break;
184 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200185 break;
186
187 default:
188 BUG();
189 }
190}
191
192/**
193 * nand_write_buf - [DEFAULT] write buffer to chip
194 * @mtd: MTD device structure
195 * @buf: data buffer
196 * @len: number of bytes to write
197 *
198 * Default write function for 8bit buswith
199 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000200void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200201{
202 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100203 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200204
William Juulcfa460a2007-10-31 13:53:06 +0100205 for (i = 0; i < len; i++)
206 writeb(buf[i], chip->IO_ADDR_W);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200207}
208
209/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200210 * nand_read_buf - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200211 * @mtd: MTD device structure
212 * @buf: buffer to store date
213 * @len: number of bytes to read
214 *
215 * Default read function for 8bit buswith
216 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400217void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200218{
219 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100220 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200221
William Juulcfa460a2007-10-31 13:53:06 +0100222 for (i = 0; i < len; i++)
223 buf[i] = readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200224}
225
226/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200227 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200228 * @mtd: MTD device structure
229 * @buf: buffer containing the data to compare
230 * @len: number of bytes to compare
231 *
232 * Default verify function for 8bit buswith
233 */
William Juulcfa460a2007-10-31 13:53:06 +0100234static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200235{
236 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100237 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200238
William Juulcfa460a2007-10-31 13:53:06 +0100239 for (i = 0; i < len; i++)
240 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200241 return -EFAULT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200242 return 0;
243}
244
245/**
246 * nand_write_buf16 - [DEFAULT] write buffer to chip
247 * @mtd: MTD device structure
248 * @buf: data buffer
249 * @len: number of bytes to write
250 *
251 * Default write function for 16bit buswith
252 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000253void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200254{
255 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100256 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200257 u16 *p = (u16 *) buf;
258 len >>= 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200259
William Juulcfa460a2007-10-31 13:53:06 +0100260 for (i = 0; i < len; i++)
261 writew(p[i], chip->IO_ADDR_W);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200262
Wolfgang Denk932394a2005-08-17 12:55:25 +0200263}
264
265/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200266 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200267 * @mtd: MTD device structure
268 * @buf: buffer to store date
269 * @len: number of bytes to read
270 *
271 * Default read function for 16bit buswith
272 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400273void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200274{
275 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100276 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200277 u16 *p = (u16 *) buf;
278 len >>= 1;
279
William Juulcfa460a2007-10-31 13:53:06 +0100280 for (i = 0; i < len; i++)
281 p[i] = readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200282}
283
284/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200285 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Wolfgang Denk932394a2005-08-17 12:55:25 +0200286 * @mtd: MTD device structure
287 * @buf: buffer containing the data to compare
288 * @len: number of bytes to compare
289 *
290 * Default verify function for 16bit buswith
291 */
William Juulcfa460a2007-10-31 13:53:06 +0100292static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200293{
294 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100295 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200296 u16 *p = (u16 *) buf;
297 len >>= 1;
298
William Juulcfa460a2007-10-31 13:53:06 +0100299 for (i = 0; i < len; i++)
300 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200301 return -EFAULT;
302
303 return 0;
304}
305
306/**
307 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
308 * @mtd: MTD device structure
309 * @ofs: offset from device start
310 * @getchip: 0, if the chip is already selected
311 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200312 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200313 */
314static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
315{
316 int page, chipnr, res = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100317 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200318 u16 bad;
319
William Juulcfa460a2007-10-31 13:53:06 +0100320 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200321
Wolfgang Denk932394a2005-08-17 12:55:25 +0200322 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100323 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200324
William Juulcfa460a2007-10-31 13:53:06 +0100325 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200326
327 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100328 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200329 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200330
William Juulcfa460a2007-10-31 13:53:06 +0100331 if (chip->options & NAND_BUSWIDTH_16) {
332 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
333 page);
334 bad = cpu_to_le16(chip->read_word(mtd));
335 if (chip->badblockpos & 0x1)
336 bad >>= 8;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200337 if ((bad & 0xFF) != 0xff)
338 res = 1;
339 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100340 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
341 if (chip->read_byte(mtd) != 0xff)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200342 res = 1;
343 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200344
William Juulcfa460a2007-10-31 13:53:06 +0100345 if (getchip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200346 nand_release_device(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200347
Wolfgang Denk932394a2005-08-17 12:55:25 +0200348 return res;
349}
350
351/**
352 * nand_default_block_markbad - [DEFAULT] mark a block bad
353 * @mtd: MTD device structure
354 * @ofs: offset from device start
355 *
356 * This is the default implementation, which can be overridden by
357 * a hardware specific driver.
358*/
359static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
360{
William Juulcfa460a2007-10-31 13:53:06 +0100361 struct nand_chip *chip = mtd->priv;
362 uint8_t buf[2] = { 0, 0 };
363 int block, ret;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200364
Wolfgang Denk932394a2005-08-17 12:55:25 +0200365 /* Get block number */
William Juulcfa460a2007-10-31 13:53:06 +0100366 block = (int)(ofs >> chip->bbt_erase_shift);
367 if (chip->bbt)
368 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200369
370 /* Do we have a flash based bad block table ? */
William Juulcfa460a2007-10-31 13:53:06 +0100371 if (chip->options & NAND_USE_FLASH_BBT)
372 ret = nand_update_bbt(mtd, ofs);
373 else {
374 /* We write two bytes, so we dont have to mess with 16 bit
375 * access
376 */
Scott Woodc45912d2008-10-24 16:20:43 -0500377 nand_get_device(chip, mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +0100378 ofs += mtd->oobsize;
379 chip->ops.len = chip->ops.ooblen = 2;
380 chip->ops.datbuf = NULL;
381 chip->ops.oobbuf = buf;
382 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200383
William Juulcfa460a2007-10-31 13:53:06 +0100384 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Scott Woodc45912d2008-10-24 16:20:43 -0500385 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100386 }
387 if (!ret)
388 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500389
William Juulcfa460a2007-10-31 13:53:06 +0100390 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200391}
392
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200393/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200394 * nand_check_wp - [GENERIC] check if the chip is write protected
395 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200396 * Check, if the device is write protected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200397 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200398 * The function expects, that the device is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200399 */
William Juulcfa460a2007-10-31 13:53:06 +0100400static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200401{
William Juulcfa460a2007-10-31 13:53:06 +0100402 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200403 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100404 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
405 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200406}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100407
Wolfgang Denk932394a2005-08-17 12:55:25 +0200408/**
409 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
410 * @mtd: MTD device structure
411 * @ofs: offset from device start
412 * @getchip: 0, if the chip is already selected
413 * @allowbbt: 1, if its allowed to access the bbt area
414 *
415 * Check, if the block is bad. Either by reading the bad block table or
416 * calling of the scan function.
417 */
William Juulcfa460a2007-10-31 13:53:06 +0100418static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
419 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200420{
William Juulcfa460a2007-10-31 13:53:06 +0100421 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200422
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200423 if (!(chip->options & NAND_BBT_SCANNED)) {
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200424 chip->options |= NAND_BBT_SCANNED;
Scott Woodff49ea82008-12-16 14:24:16 -0600425 chip->scan_bbt(mtd);
Ilya Yanok13f0fd92008-06-30 15:34:40 +0200426 }
427
William Juulcfa460a2007-10-31 13:53:06 +0100428 if (!chip->bbt)
429 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200430
Wolfgang Denk932394a2005-08-17 12:55:25 +0200431 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100432 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200433}
434
William Juulcfa460a2007-10-31 13:53:06 +0100435/*
436 * Wait for the ready pin, after a command
437 * The timeout is catched later.
438 */
William Juulcfa460a2007-10-31 13:53:06 +0100439void nand_wait_ready(struct mtd_info *mtd)
440{
441 struct nand_chip *chip = mtd->priv;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200442 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000443 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100444
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000445 time_start = get_timer(0);
Stefan Roese12072262008-01-05 16:43:25 +0100446
447 /* wait until command is processed or timeout occures */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000448 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100449 if (chip->dev_ready)
450 if (chip->dev_ready(mtd))
451 break;
452 }
William Juulcfa460a2007-10-31 13:53:06 +0100453}
William Juulcfa460a2007-10-31 13:53:06 +0100454
Wolfgang Denk932394a2005-08-17 12:55:25 +0200455/**
456 * nand_command - [DEFAULT] Send command to NAND device
457 * @mtd: MTD device structure
458 * @command: the command to be sent
459 * @column: the column address for this command, -1 if none
460 * @page_addr: the page address for this command, -1 if none
461 *
462 * Send command to NAND device. This function is used for small page
463 * devices (256/512 Bytes per page)
464 */
William Juulcfa460a2007-10-31 13:53:06 +0100465static void nand_command(struct mtd_info *mtd, unsigned int command,
466 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200467{
William Juulcfa460a2007-10-31 13:53:06 +0100468 register struct nand_chip *chip = mtd->priv;
469 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600470 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200471
Wolfgang Denk932394a2005-08-17 12:55:25 +0200472 /*
473 * Write out the command to the device.
474 */
475 if (command == NAND_CMD_SEQIN) {
476 int readcmd;
477
William Juulcfa460a2007-10-31 13:53:06 +0100478 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200479 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100480 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200481 readcmd = NAND_CMD_READOOB;
482 } else if (column < 256) {
483 /* First 256 bytes --> READ0 */
484 readcmd = NAND_CMD_READ0;
485 } else {
486 column -= 256;
487 readcmd = NAND_CMD_READ1;
488 }
William Juulcfa460a2007-10-31 13:53:06 +0100489 chip->cmd_ctrl(mtd, readcmd, ctrl);
490 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200491 }
William Juulcfa460a2007-10-31 13:53:06 +0100492 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200493
William Juulcfa460a2007-10-31 13:53:06 +0100494 /*
495 * Address cycle, when necessary
496 */
497 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
498 /* Serially input address */
499 if (column != -1) {
500 /* Adjust columns for 16 bit buswidth */
501 if (chip->options & NAND_BUSWIDTH_16)
502 column >>= 1;
503 chip->cmd_ctrl(mtd, column, ctrl);
504 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200505 }
William Juulcfa460a2007-10-31 13:53:06 +0100506 if (page_addr != -1) {
507 chip->cmd_ctrl(mtd, page_addr, ctrl);
508 ctrl &= ~NAND_CTRL_CHANGE;
509 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
510 /* One more address cycle for devices > 32MiB */
511 if (chip->chipsize > (32 << 20))
512 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
513 }
514 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200515
516 /*
517 * program and erase have their own busy handlers
Wolfgang Denk932394a2005-08-17 12:55:25 +0200518 * status and sequential in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100519 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200520 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200521
Wolfgang Denk932394a2005-08-17 12:55:25 +0200522 case NAND_CMD_PAGEPROG:
523 case NAND_CMD_ERASE1:
524 case NAND_CMD_ERASE2:
525 case NAND_CMD_SEQIN:
526 case NAND_CMD_STATUS:
527 return;
528
529 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100530 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200531 break;
William Juulcfa460a2007-10-31 13:53:06 +0100532 udelay(chip->chip_delay);
533 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
534 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
535 chip->cmd_ctrl(mtd,
536 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600537 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
538 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200539 return;
540
William Juulcfa460a2007-10-31 13:53:06 +0100541 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200542 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200543 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200544 * If we don't have access to the busy pin, we apply the given
545 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100546 */
547 if (!chip->dev_ready) {
548 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200549 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200550 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200551 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200552 /* Apply this short delay always to ensure that we do wait tWB in
553 * any case on any machine. */
William Juulcfa460a2007-10-31 13:53:06 +0100554 ndelay(100);
555
556 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200557}
558
559/**
560 * nand_command_lp - [DEFAULT] Send command to NAND large page device
561 * @mtd: MTD device structure
562 * @command: the command to be sent
563 * @column: the column address for this command, -1 if none
564 * @page_addr: the page address for this command, -1 if none
565 *
William Juulcfa460a2007-10-31 13:53:06 +0100566 * Send command to NAND device. This is the version for the new large page
567 * devices We dont have the separate regions as we have in the small page
568 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200569 */
William Juulcfa460a2007-10-31 13:53:06 +0100570static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
571 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200572{
William Juulcfa460a2007-10-31 13:53:06 +0100573 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600574 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200575
576 /* Emulate NAND_CMD_READOOB */
577 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100578 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200579 command = NAND_CMD_READ0;
580 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200581
William Juulcfa460a2007-10-31 13:53:06 +0100582 /* Command latch cycle */
583 chip->cmd_ctrl(mtd, command & 0xff,
584 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200585
586 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100587 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200588
589 /* Serially input address */
590 if (column != -1) {
591 /* Adjust columns for 16 bit buswidth */
William Juulcfa460a2007-10-31 13:53:06 +0100592 if (chip->options & NAND_BUSWIDTH_16)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200593 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100594 chip->cmd_ctrl(mtd, column, ctrl);
595 ctrl &= ~NAND_CTRL_CHANGE;
596 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200597 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200598 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100599 chip->cmd_ctrl(mtd, page_addr, ctrl);
600 chip->cmd_ctrl(mtd, page_addr >> 8,
601 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200602 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100603 if (chip->chipsize > (128 << 20))
604 chip->cmd_ctrl(mtd, page_addr >> 16,
605 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200606 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200607 }
William Juulcfa460a2007-10-31 13:53:06 +0100608 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200609
610 /*
611 * program and erase have their own busy handlers
William Juulcfa460a2007-10-31 13:53:06 +0100612 * status, sequential in, and deplete1 need no delay
613 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200614 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200615
Wolfgang Denk932394a2005-08-17 12:55:25 +0200616 case NAND_CMD_CACHEDPROG:
617 case NAND_CMD_PAGEPROG:
618 case NAND_CMD_ERASE1:
619 case NAND_CMD_ERASE2:
620 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100621 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200622 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100623 case NAND_CMD_DEPLETE1:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200624 return;
625
William Juulcfa460a2007-10-31 13:53:06 +0100626 /*
627 * read error status commands require only a short delay
628 */
629 case NAND_CMD_STATUS_ERROR:
630 case NAND_CMD_STATUS_ERROR0:
631 case NAND_CMD_STATUS_ERROR1:
632 case NAND_CMD_STATUS_ERROR2:
633 case NAND_CMD_STATUS_ERROR3:
634 udelay(chip->chip_delay);
635 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200636
637 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100638 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200639 break;
William Juulcfa460a2007-10-31 13:53:06 +0100640 udelay(chip->chip_delay);
641 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
642 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
643 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
644 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600645 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
646 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100647 return;
648
649 case NAND_CMD_RNDOUT:
650 /* No ready / busy check necessary */
651 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
652 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
653 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
654 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200655 return;
656
657 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100658 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
659 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
660 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
661 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200662
William Juulcfa460a2007-10-31 13:53:06 +0100663 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200664 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200665 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200666 * If we don't have access to the busy pin, we apply the given
667 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100668 */
669 if (!chip->dev_ready) {
670 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200671 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200672 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200673 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200674
Wolfgang Denk932394a2005-08-17 12:55:25 +0200675 /* Apply this short delay always to ensure that we do wait tWB in
676 * any case on any machine. */
William Juulcfa460a2007-10-31 13:53:06 +0100677 ndelay(100);
678
679 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200680}
681
682/**
683 * nand_get_device - [GENERIC] Get chip for selected access
William Juulcfa460a2007-10-31 13:53:06 +0100684 * @chip: the nand chip descriptor
Wolfgang Denk932394a2005-08-17 12:55:25 +0200685 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200686 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200687 *
688 * Get the device and lock it for exclusive access
689 */
William Juulcfa460a2007-10-31 13:53:06 +0100690static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
691{
Marcel Ziswilereafcabd2008-06-22 16:30:06 +0200692 this->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100693 return 0;
694}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200695
696/**
697 * nand_wait - [DEFAULT] wait until the command is done
698 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +0100699 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200700 *
701 * Wait for command done. This applies to erase and program only
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200702 * Erase can take up to 400ms and program up to 20ms according to
Wolfgang Denk932394a2005-08-17 12:55:25 +0200703 * general NAND and SmartMedia specs
William Juulcfa460a2007-10-31 13:53:06 +0100704 */
William Juulcfa460a2007-10-31 13:53:06 +0100705static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200706{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100707 unsigned long timeo;
William Juulcfa460a2007-10-31 13:53:06 +0100708 int state = this->state;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000709 u32 time_start;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100710
711 if (state == FL_ERASING)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200712 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100713 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200714 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100715
716 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
717 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
718 else
719 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
720
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000721 time_start = get_timer(0);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100722
723 while (1) {
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000724 if (get_timer(time_start) > timeo) {
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100725 printf("Timeout!");
Stefan Roese15784862006-11-27 17:22:19 +0100726 return 0x01;
727 }
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100728
729 if (this->dev_ready) {
730 if (this->dev_ready(mtd))
731 break;
732 } else {
733 if (this->read_byte(mtd) & NAND_STATUS_READY)
734 break;
735 }
736 }
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100737#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000738 time_start = get_timer(0);
739 while (get_timer(time_start) < 10)
740 ;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +0100741#endif /* PPCHAMELON_NAND_TIMER_HACK */
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100742
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100743 return this->read_byte(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200744}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200745
746/**
William Juulcfa460a2007-10-31 13:53:06 +0100747 * nand_read_page_raw - [Intern] read raw page data without ecc
748 * @mtd: mtd info structure
749 * @chip: nand chip info structure
750 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500751 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -0500752 *
753 * Not for syndrome calculating ecc controllers, which use a special oob layout
Wolfgang Denk932394a2005-08-17 12:55:25 +0200754 */
William Juulcfa460a2007-10-31 13:53:06 +0100755static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400756 uint8_t *buf, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200757{
William Juulcfa460a2007-10-31 13:53:06 +0100758 chip->read_buf(mtd, buf, mtd->writesize);
759 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
760 return 0;
761}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200762
William Juulcfa460a2007-10-31 13:53:06 +0100763/**
David Brownell7e866612009-11-07 16:27:01 -0500764 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
765 * @mtd: mtd info structure
766 * @chip: nand chip info structure
767 * @buf: buffer to store read data
768 * @page: page number to read
769 *
770 * We need a special oob layout and handling even when OOB isn't used.
771 */
772static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
773 uint8_t *buf, int page)
774{
775 int eccsize = chip->ecc.size;
776 int eccbytes = chip->ecc.bytes;
777 uint8_t *oob = chip->oob_poi;
778 int steps, size;
779
780 for (steps = chip->ecc.steps; steps > 0; steps--) {
781 chip->read_buf(mtd, buf, eccsize);
782 buf += eccsize;
783
784 if (chip->ecc.prepad) {
785 chip->read_buf(mtd, oob, chip->ecc.prepad);
786 oob += chip->ecc.prepad;
787 }
788
789 chip->read_buf(mtd, oob, eccbytes);
790 oob += eccbytes;
791
792 if (chip->ecc.postpad) {
793 chip->read_buf(mtd, oob, chip->ecc.postpad);
794 oob += chip->ecc.postpad;
795 }
796 }
797
798 size = mtd->oobsize - (oob - chip->oob_poi);
799 if (size)
800 chip->read_buf(mtd, oob, size);
801
802 return 0;
803}
804
805/**
William Juulcfa460a2007-10-31 13:53:06 +0100806 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
807 * @mtd: mtd info structure
808 * @chip: nand chip info structure
809 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500810 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +0100811 */
812static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400813 uint8_t *buf, int page)
William Juulcfa460a2007-10-31 13:53:06 +0100814{
815 int i, eccsize = chip->ecc.size;
816 int eccbytes = chip->ecc.bytes;
817 int eccsteps = chip->ecc.steps;
818 uint8_t *p = buf;
819 uint8_t *ecc_calc = chip->buffers->ecccalc;
820 uint8_t *ecc_code = chip->buffers->ecccode;
821 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200822
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400823 chip->ecc.read_page_raw(mtd, chip, buf, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200824
William Juulcfa460a2007-10-31 13:53:06 +0100825 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
826 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200827
William Juulcfa460a2007-10-31 13:53:06 +0100828 for (i = 0; i < chip->ecc.total; i++)
829 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200830
William Juulcfa460a2007-10-31 13:53:06 +0100831 eccsteps = chip->ecc.steps;
832 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200833
William Juulcfa460a2007-10-31 13:53:06 +0100834 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
835 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200836
William Juulcfa460a2007-10-31 13:53:06 +0100837 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodc45912d2008-10-24 16:20:43 -0500838 if (stat < 0)
839 mtd->ecc_stats.failed++;
840 else
841 mtd->ecc_stats.corrected += stat;
842 }
843 return 0;
844}
845
846/**
847 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
848 * @mtd: mtd info structure
849 * @chip: nand chip info structure
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500850 * @data_offs: offset of requested data within the page
851 * @readlen: data length
852 * @bufpoi: buffer to store read data
Scott Woodc45912d2008-10-24 16:20:43 -0500853 */
854static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
855{
856 int start_step, end_step, num_steps;
857 uint32_t *eccpos = chip->ecc.layout->eccpos;
858 uint8_t *p;
859 int data_col_addr, i, gaps = 0;
860 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
861 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
862
863 /* Column address wihin the page aligned to ECC size (256bytes). */
864 start_step = data_offs / chip->ecc.size;
865 end_step = (data_offs + readlen - 1) / chip->ecc.size;
866 num_steps = end_step - start_step + 1;
867
868 /* Data size aligned to ECC ecc.size*/
869 datafrag_len = num_steps * chip->ecc.size;
870 eccfrag_len = num_steps * chip->ecc.bytes;
871
872 data_col_addr = start_step * chip->ecc.size;
873 /* If we read not a page aligned data */
874 if (data_col_addr != 0)
875 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
876
877 p = bufpoi + data_col_addr;
878 chip->read_buf(mtd, p, datafrag_len);
879
880 /* Calculate ECC */
881 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
882 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
883
884 /* The performance is faster if to position offsets
885 according to ecc.pos. Let make sure here that
886 there are no gaps in ecc positions */
887 for (i = 0; i < eccfrag_len - 1; i++) {
888 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
889 eccpos[i + start_step * chip->ecc.bytes + 1]) {
890 gaps = 1;
891 break;
892 }
893 }
894 if (gaps) {
895 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
896 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
897 } else {
898 /* send the command to read the particular ecc bytes */
899 /* take care about buswidth alignment in read_buf */
900 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
901 aligned_len = eccfrag_len;
902 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
903 aligned_len++;
904 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
905 aligned_len++;
906
907 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
908 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
909 }
910
911 for (i = 0; i < eccfrag_len; i++)
912 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
913
914 p = bufpoi + data_col_addr;
915 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
916 int stat;
917
918 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Sandeep Paulraj6cd752f2009-11-16 13:32:01 -0500919 if (stat == -1)
William Juulcfa460a2007-10-31 13:53:06 +0100920 mtd->ecc_stats.failed++;
921 else
922 mtd->ecc_stats.corrected += stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200923 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200924 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200925}
926
Wolfgang Denk932394a2005-08-17 12:55:25 +0200927/**
William Juulcfa460a2007-10-31 13:53:06 +0100928 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
929 * @mtd: mtd info structure
930 * @chip: nand chip info structure
931 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500932 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200933 *
William Juulcfa460a2007-10-31 13:53:06 +0100934 * Not for syndrome calculating ecc controllers which need a special oob layout
Wolfgang Denk932394a2005-08-17 12:55:25 +0200935 */
William Juulcfa460a2007-10-31 13:53:06 +0100936static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -0400937 uint8_t *buf, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200938{
William Juulcfa460a2007-10-31 13:53:06 +0100939 int i, eccsize = chip->ecc.size;
940 int eccbytes = chip->ecc.bytes;
941 int eccsteps = chip->ecc.steps;
942 uint8_t *p = buf;
943 uint8_t *ecc_calc = chip->buffers->ecccalc;
944 uint8_t *ecc_code = chip->buffers->ecccode;
945 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200946
William Juulcfa460a2007-10-31 13:53:06 +0100947 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
948 chip->ecc.hwctl(mtd, NAND_ECC_READ);
949 chip->read_buf(mtd, p, eccsize);
950 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
951 }
952 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200953
William Juulcfa460a2007-10-31 13:53:06 +0100954 for (i = 0; i < chip->ecc.total; i++)
955 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +0200956
William Juulcfa460a2007-10-31 13:53:06 +0100957 eccsteps = chip->ecc.steps;
958 p = buf;
959
960 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
961 int stat;
962
963 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Sandeep Paulraj18b5a4b2009-11-07 14:25:03 -0500964 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +0100965 mtd->ecc_stats.failed++;
966 else
967 mtd->ecc_stats.corrected += stat;
968 }
969 return 0;
970}
971
972/**
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -0400973 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
974 * @mtd: mtd info structure
975 * @chip: nand chip info structure
976 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -0500977 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -0400978 *
979 * Hardware ECC for large page chips, require OOB to be read first.
980 * For this ECC mode, the write_page method is re-used from ECC_HW.
981 * These methods read/write ECC from the OOB area, unlike the
982 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
983 * "infix ECC" scheme and reads/writes ECC from the data area, by
984 * overwriting the NAND manufacturer bad block markings.
985 */
986static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
987 struct nand_chip *chip, uint8_t *buf, int page)
988{
989 int i, eccsize = chip->ecc.size;
990 int eccbytes = chip->ecc.bytes;
991 int eccsteps = chip->ecc.steps;
992 uint8_t *p = buf;
993 uint8_t *ecc_code = chip->buffers->ecccode;
994 uint32_t *eccpos = chip->ecc.layout->eccpos;
995 uint8_t *ecc_calc = chip->buffers->ecccalc;
996
997 /* Read the OOB area first */
998 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
999 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1000 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1001
1002 for (i = 0; i < chip->ecc.total; i++)
1003 ecc_code[i] = chip->oob_poi[eccpos[i]];
1004
1005 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1006 int stat;
1007
1008 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1009 chip->read_buf(mtd, p, eccsize);
1010 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1011
1012 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1013 if (stat < 0)
1014 mtd->ecc_stats.failed++;
1015 else
1016 mtd->ecc_stats.corrected += stat;
1017 }
1018 return 0;
1019}
1020
1021/**
William Juulcfa460a2007-10-31 13:53:06 +01001022 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1023 * @mtd: mtd info structure
1024 * @chip: nand chip info structure
1025 * @buf: buffer to store read data
Sandeep Paulraje25ee032009-11-07 14:24:50 -05001026 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001027 *
1028 * The hw generator calculates the error syndrome automatically. Therefor
1029 * we need a special oob layout and handling.
1030 */
1031static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001032 uint8_t *buf, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001033{
1034 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 *oob = chip->oob_poi;
1039
1040 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1041 int stat;
1042
1043 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1044 chip->read_buf(mtd, p, eccsize);
1045
1046 if (chip->ecc.prepad) {
1047 chip->read_buf(mtd, oob, chip->ecc.prepad);
1048 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001049 }
1050
William Juulcfa460a2007-10-31 13:53:06 +01001051 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1052 chip->read_buf(mtd, oob, eccbytes);
1053 stat = chip->ecc.correct(mtd, p, oob, NULL);
1054
Scott Woodc45912d2008-10-24 16:20:43 -05001055 if (stat < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001056 mtd->ecc_stats.failed++;
1057 else
1058 mtd->ecc_stats.corrected += stat;
1059
1060 oob += eccbytes;
1061
1062 if (chip->ecc.postpad) {
1063 chip->read_buf(mtd, oob, chip->ecc.postpad);
1064 oob += chip->ecc.postpad;
1065 }
1066 }
1067
1068 /* Calculate remaining oob bytes */
1069 i = mtd->oobsize - (oob - chip->oob_poi);
1070 if (i)
1071 chip->read_buf(mtd, oob, i);
1072
1073 return 0;
1074}
1075
1076/**
1077 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1078 * @chip: nand chip structure
1079 * @oob: oob destination address
1080 * @ops: oob ops structure
1081 * @len: size of oob to transfer
1082 */
1083static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1084 struct mtd_oob_ops *ops, size_t len)
1085{
1086 switch(ops->mode) {
1087
1088 case MTD_OOB_PLACE:
1089 case MTD_OOB_RAW:
1090 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1091 return oob + len;
1092
1093 case MTD_OOB_AUTO: {
1094 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1095 uint32_t boffs = 0, roffs = ops->ooboffs;
1096 size_t bytes = 0;
1097
1098 for(; free->length && len; free++, len -= bytes) {
1099 /* Read request not from offset 0 ? */
1100 if (unlikely(roffs)) {
1101 if (roffs >= free->length) {
1102 roffs -= free->length;
1103 continue;
1104 }
1105 boffs = free->offset + roffs;
1106 bytes = min_t(size_t, len,
1107 (free->length - roffs));
1108 roffs = 0;
1109 } else {
1110 bytes = min_t(size_t, len, free->length);
1111 boffs = free->offset;
1112 }
1113 memcpy(oob, chip->oob_poi + boffs, bytes);
1114 oob += bytes;
1115 }
1116 return oob;
1117 }
1118 default:
1119 BUG();
1120 }
1121 return NULL;
1122}
1123
1124/**
1125 * nand_do_read_ops - [Internal] Read data with ECC
1126 *
1127 * @mtd: MTD device structure
1128 * @from: offset to read from
1129 * @ops: oob ops structure
1130 *
1131 * Internal function. Called with chip held.
1132 */
1133static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1134 struct mtd_oob_ops *ops)
1135{
1136 int chipnr, page, realpage, col, bytes, aligned;
1137 struct nand_chip *chip = mtd->priv;
1138 struct mtd_ecc_stats stats;
1139 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1140 int sndcmd = 1;
1141 int ret = 0;
1142 uint32_t readlen = ops->len;
1143 uint32_t oobreadlen = ops->ooblen;
1144 uint8_t *bufpoi, *oob, *buf;
1145
1146 stats = mtd->ecc_stats;
1147
1148 chipnr = (int)(from >> chip->chip_shift);
1149 chip->select_chip(mtd, chipnr);
1150
1151 realpage = (int)(from >> chip->page_shift);
1152 page = realpage & chip->pagemask;
1153
1154 col = (int)(from & (mtd->writesize - 1));
1155
1156 buf = ops->datbuf;
1157 oob = ops->oobbuf;
1158
1159 while(1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001160 WATCHDOG_RESET();
1161
William Juulcfa460a2007-10-31 13:53:06 +01001162 bytes = min(mtd->writesize - col, readlen);
1163 aligned = (bytes == mtd->writesize);
1164
1165 /* Is the current page in the buffer ? */
1166 if (realpage != chip->pagebuf || oob) {
1167 bufpoi = aligned ? buf : chip->buffers->databuf;
1168
1169 if (likely(sndcmd)) {
1170 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1171 sndcmd = 0;
1172 }
1173
1174 /* Now read the page into the buffer */
1175 if (unlikely(ops->mode == MTD_OOB_RAW))
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001176 ret = chip->ecc.read_page_raw(mtd, chip,
1177 bufpoi, page);
Scott Woodc45912d2008-10-24 16:20:43 -05001178 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1179 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
William Juulcfa460a2007-10-31 13:53:06 +01001180 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001181 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1182 page);
William Juulcfa460a2007-10-31 13:53:06 +01001183 if (ret < 0)
1184 break;
1185
1186 /* Transfer not aligned data */
1187 if (!aligned) {
Scott Woodc45912d2008-10-24 16:20:43 -05001188 if (!NAND_SUBPAGE_READ(chip) && !oob)
1189 chip->pagebuf = realpage;
William Juulcfa460a2007-10-31 13:53:06 +01001190 memcpy(buf, chip->buffers->databuf + col, bytes);
1191 }
1192
1193 buf += bytes;
1194
1195 if (unlikely(oob)) {
1196 /* Raw mode does data:oob:data:oob */
1197 if (ops->mode != MTD_OOB_RAW) {
1198 int toread = min(oobreadlen,
1199 chip->ecc.layout->oobavail);
1200 if (toread) {
1201 oob = nand_transfer_oob(chip,
1202 oob, ops, toread);
1203 oobreadlen -= toread;
1204 }
1205 } else
1206 buf = nand_transfer_oob(chip,
1207 buf, ops, mtd->oobsize);
1208 }
1209
1210 if (!(chip->options & NAND_NO_READRDY)) {
1211 /*
1212 * Apply delay or wait for ready/busy pin. Do
1213 * this before the AUTOINCR check, so no
1214 * problems arise if a chip which does auto
1215 * increment is marked as NOAUTOINCR by the
1216 * board driver.
1217 */
1218 if (!chip->dev_ready)
1219 udelay(chip->chip_delay);
1220 else
1221 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001222 }
1223 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001224 memcpy(buf, chip->buffers->databuf + col, bytes);
1225 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001226 }
1227
William Juulcfa460a2007-10-31 13:53:06 +01001228 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001229
William Juulcfa460a2007-10-31 13:53:06 +01001230 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001231 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001232
1233 /* For subsequent reads align to page boundary. */
1234 col = 0;
1235 /* Increment page address */
1236 realpage++;
1237
William Juulcfa460a2007-10-31 13:53:06 +01001238 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001239 /* Check, if we cross a chip boundary */
1240 if (!page) {
1241 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001242 chip->select_chip(mtd, -1);
1243 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001244 }
William Juulcfa460a2007-10-31 13:53:06 +01001245
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001246 /* Check, if the chip supports auto page increment
1247 * or if we have hit a block boundary.
William Juulcfa460a2007-10-31 13:53:06 +01001248 */
1249 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001250 sndcmd = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001251 }
1252
William Juulcfa460a2007-10-31 13:53:06 +01001253 ops->retlen = ops->len - (size_t) readlen;
1254 if (oob)
1255 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001256
William Juulcfa460a2007-10-31 13:53:06 +01001257 if (ret)
1258 return ret;
1259
1260 if (mtd->ecc_stats.failed - stats.failed)
1261 return -EBADMSG;
1262
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01001263 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001264}
1265
1266/**
William Juulcfa460a2007-10-31 13:53:06 +01001267 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
Wolfgang Denk932394a2005-08-17 12:55:25 +02001268 * @mtd: MTD device structure
1269 * @from: offset to read from
1270 * @len: number of bytes to read
1271 * @retlen: pointer to variable to store the number of read bytes
1272 * @buf: the databuffer to put data
1273 *
William Juulcfa460a2007-10-31 13:53:06 +01001274 * Get hold of the chip and call nand_do_read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001275 */
William Juulcfa460a2007-10-31 13:53:06 +01001276static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1277 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001278{
William Juulcfa460a2007-10-31 13:53:06 +01001279 struct nand_chip *chip = mtd->priv;
1280 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001281
1282 /* Do not allow reads past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01001283 if ((from + len) > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001284 return -EINVAL;
William Juulcfa460a2007-10-31 13:53:06 +01001285 if (!len)
1286 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001287
William Juulcfa460a2007-10-31 13:53:06 +01001288 nand_get_device(chip, mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001289
William Juulcfa460a2007-10-31 13:53:06 +01001290 chip->ops.len = len;
1291 chip->ops.datbuf = buf;
1292 chip->ops.oobbuf = NULL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001293
William Juulcfa460a2007-10-31 13:53:06 +01001294 ret = nand_do_read_ops(mtd, from, &chip->ops);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001295
William Juulcfa460a2007-10-31 13:53:06 +01001296 *retlen = chip->ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001297
Wolfgang Denk932394a2005-08-17 12:55:25 +02001298 nand_release_device(mtd);
1299
1300 return ret;
1301}
1302
William Juulcfa460a2007-10-31 13:53:06 +01001303/**
1304 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1305 * @mtd: mtd info structure
1306 * @chip: nand chip info structure
1307 * @page: page number to read
1308 * @sndcmd: flag whether to issue read command or not
1309 */
1310static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1311 int page, int sndcmd)
1312{
1313 if (sndcmd) {
1314 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1315 sndcmd = 0;
1316 }
1317 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1318 return sndcmd;
1319}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001320
1321/**
William Juulcfa460a2007-10-31 13:53:06 +01001322 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1323 * with syndromes
1324 * @mtd: mtd info structure
1325 * @chip: nand chip info structure
1326 * @page: page number to read
1327 * @sndcmd: flag whether to issue read command or not
1328 */
1329static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1330 int page, int sndcmd)
1331{
1332 uint8_t *buf = chip->oob_poi;
1333 int length = mtd->oobsize;
1334 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1335 int eccsize = chip->ecc.size;
1336 uint8_t *bufpoi = buf;
1337 int i, toread, sndrnd = 0, pos;
1338
1339 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1340 for (i = 0; i < chip->ecc.steps; i++) {
1341 if (sndrnd) {
1342 pos = eccsize + i * (eccsize + chunk);
1343 if (mtd->writesize > 512)
1344 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1345 else
1346 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1347 } else
1348 sndrnd = 1;
1349 toread = min_t(int, length, chunk);
1350 chip->read_buf(mtd, bufpoi, toread);
1351 bufpoi += toread;
1352 length -= toread;
1353 }
1354 if (length > 0)
1355 chip->read_buf(mtd, bufpoi, length);
1356
1357 return 1;
1358}
1359
1360/**
1361 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1362 * @mtd: mtd info structure
1363 * @chip: nand chip info structure
1364 * @page: page number to write
1365 */
1366static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1367 int page)
1368{
1369 int status = 0;
1370 const uint8_t *buf = chip->oob_poi;
1371 int length = mtd->oobsize;
1372
1373 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1374 chip->write_buf(mtd, buf, length);
1375 /* Send command to program the OOB data */
1376 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1377
1378 status = chip->waitfunc(mtd, chip);
1379
1380 return status & NAND_STATUS_FAIL ? -EIO : 0;
1381}
1382
1383/**
1384 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1385 * with syndrome - only for large page flash !
1386 * @mtd: mtd info structure
1387 * @chip: nand chip info structure
1388 * @page: page number to write
1389 */
1390static int nand_write_oob_syndrome(struct mtd_info *mtd,
1391 struct nand_chip *chip, int page)
1392{
1393 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1394 int eccsize = chip->ecc.size, length = mtd->oobsize;
1395 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1396 const uint8_t *bufpoi = chip->oob_poi;
1397
1398 /*
1399 * data-ecc-data-ecc ... ecc-oob
1400 * or
1401 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1402 */
1403 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1404 pos = steps * (eccsize + chunk);
1405 steps = 0;
1406 } else
1407 pos = eccsize;
1408
1409 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1410 for (i = 0; i < steps; i++) {
1411 if (sndcmd) {
1412 if (mtd->writesize <= 512) {
1413 uint32_t fill = 0xFFFFFFFF;
1414
1415 len = eccsize;
1416 while (len > 0) {
1417 int num = min_t(int, len, 4);
1418 chip->write_buf(mtd, (uint8_t *)&fill,
1419 num);
1420 len -= num;
1421 }
1422 } else {
1423 pos = eccsize + i * (eccsize + chunk);
1424 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1425 }
1426 } else
1427 sndcmd = 1;
1428 len = min_t(int, length, chunk);
1429 chip->write_buf(mtd, bufpoi, len);
1430 bufpoi += len;
1431 length -= len;
1432 }
1433 if (length > 0)
1434 chip->write_buf(mtd, bufpoi, length);
1435
1436 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1437 status = chip->waitfunc(mtd, chip);
1438
1439 return status & NAND_STATUS_FAIL ? -EIO : 0;
1440}
1441
1442/**
1443 * nand_do_read_oob - [Intern] NAND read out-of-band
1444 * @mtd: MTD device structure
1445 * @from: offset to read from
1446 * @ops: oob operations description structure
1447 *
1448 * NAND read out-of-band data from the spare area
1449 */
1450static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1451 struct mtd_oob_ops *ops)
1452{
1453 int page, realpage, chipnr, sndcmd = 1;
1454 struct nand_chip *chip = mtd->priv;
1455 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1456 int readlen = ops->ooblen;
1457 int len;
1458 uint8_t *buf = ops->oobbuf;
1459
1460 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01001461 (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001462
1463 if (ops->mode == MTD_OOB_AUTO)
1464 len = chip->ecc.layout->oobavail;
1465 else
1466 len = mtd->oobsize;
1467
1468 if (unlikely(ops->ooboffs >= len)) {
1469 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01001470 "Attempt to start read outside oob\n");
William Juulcfa460a2007-10-31 13:53:06 +01001471 return -EINVAL;
1472 }
1473
1474 /* Do not allow reads past end of device */
1475 if (unlikely(from >= mtd->size ||
1476 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1477 (from >> chip->page_shift)) * len)) {
1478 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01001479 "Attempt read beyond end of device\n");
William Juulcfa460a2007-10-31 13:53:06 +01001480 return -EINVAL;
1481 }
1482
1483 chipnr = (int)(from >> chip->chip_shift);
1484 chip->select_chip(mtd, chipnr);
1485
1486 /* Shift to get page */
1487 realpage = (int)(from >> chip->page_shift);
1488 page = realpage & chip->pagemask;
1489
1490 while(1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001491 WATCHDOG_RESET();
William Juulcfa460a2007-10-31 13:53:06 +01001492 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1493
1494 len = min(len, readlen);
1495 buf = nand_transfer_oob(chip, buf, ops, len);
1496
1497 if (!(chip->options & NAND_NO_READRDY)) {
1498 /*
1499 * Apply delay or wait for ready/busy pin. Do this
1500 * before the AUTOINCR check, so no problems arise if a
1501 * chip which does auto increment is marked as
1502 * NOAUTOINCR by the board driver.
1503 */
1504 if (!chip->dev_ready)
1505 udelay(chip->chip_delay);
1506 else
1507 nand_wait_ready(mtd);
1508 }
1509
1510 readlen -= len;
1511 if (!readlen)
1512 break;
1513
1514 /* Increment page address */
1515 realpage++;
1516
1517 page = realpage & chip->pagemask;
1518 /* Check, if we cross a chip boundary */
1519 if (!page) {
1520 chipnr++;
1521 chip->select_chip(mtd, -1);
1522 chip->select_chip(mtd, chipnr);
1523 }
1524
1525 /* Check, if the chip supports auto page increment
1526 * or if we have hit a block boundary.
1527 */
1528 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1529 sndcmd = 1;
1530 }
1531
1532 ops->oobretlen = ops->ooblen;
1533 return 0;
1534}
1535
1536/**
1537 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1538 * @mtd: MTD device structure
1539 * @from: offset to read from
1540 * @ops: oob operation description structure
1541 *
1542 * NAND read data and/or out-of-band data
1543 */
1544static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1545 struct mtd_oob_ops *ops)
1546{
1547 struct nand_chip *chip = mtd->priv;
1548 int ret = -ENOTSUPP;
1549
1550 ops->retlen = 0;
1551
1552 /* Do not allow reads past end of device */
1553 if (ops->datbuf && (from + ops->len) > mtd->size) {
1554 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01001555 "Attempt read beyond end of device\n");
William Juulcfa460a2007-10-31 13:53:06 +01001556 return -EINVAL;
1557 }
1558
1559 nand_get_device(chip, mtd, FL_READING);
1560
1561 switch(ops->mode) {
1562 case MTD_OOB_PLACE:
1563 case MTD_OOB_AUTO:
1564 case MTD_OOB_RAW:
1565 break;
1566
1567 default:
1568 goto out;
1569 }
1570
1571 if (!ops->datbuf)
1572 ret = nand_do_read_oob(mtd, from, ops);
1573 else
1574 ret = nand_do_read_ops(mtd, from, ops);
1575
1576 out:
1577 nand_release_device(mtd);
1578 return ret;
1579}
1580
1581
1582/**
1583 * nand_write_page_raw - [Intern] raw page write function
1584 * @mtd: mtd info structure
1585 * @chip: nand chip info structure
1586 * @buf: data buffer
David Brownell7e866612009-11-07 16:27:01 -05001587 *
1588 * Not for syndrome calculating ecc controllers, which use a special oob layout
William Juulcfa460a2007-10-31 13:53:06 +01001589 */
1590static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1591 const uint8_t *buf)
1592{
1593 chip->write_buf(mtd, buf, mtd->writesize);
1594 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1595}
1596
1597/**
David Brownell7e866612009-11-07 16:27:01 -05001598 * nand_write_page_raw_syndrome - [Intern] raw page write function
1599 * @mtd: mtd info structure
1600 * @chip: nand chip info structure
1601 * @buf: data buffer
1602 *
1603 * We need a special oob layout and handling even when ECC isn't checked.
1604 */
1605static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1606 const uint8_t *buf)
1607{
1608 int eccsize = chip->ecc.size;
1609 int eccbytes = chip->ecc.bytes;
1610 uint8_t *oob = chip->oob_poi;
1611 int steps, size;
1612
1613 for (steps = chip->ecc.steps; steps > 0; steps--) {
1614 chip->write_buf(mtd, buf, eccsize);
1615 buf += eccsize;
1616
1617 if (chip->ecc.prepad) {
1618 chip->write_buf(mtd, oob, chip->ecc.prepad);
1619 oob += chip->ecc.prepad;
1620 }
1621
1622 chip->read_buf(mtd, oob, eccbytes);
1623 oob += eccbytes;
1624
1625 if (chip->ecc.postpad) {
1626 chip->write_buf(mtd, oob, chip->ecc.postpad);
1627 oob += chip->ecc.postpad;
1628 }
1629 }
1630
1631 size = mtd->oobsize - (oob - chip->oob_poi);
1632 if (size)
1633 chip->write_buf(mtd, oob, size);
1634}
1635/**
William Juulcfa460a2007-10-31 13:53:06 +01001636 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1637 * @mtd: mtd info structure
1638 * @chip: nand chip info structure
1639 * @buf: data buffer
1640 */
1641static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1642 const uint8_t *buf)
1643{
1644 int i, eccsize = chip->ecc.size;
1645 int eccbytes = chip->ecc.bytes;
1646 int eccsteps = chip->ecc.steps;
1647 uint8_t *ecc_calc = chip->buffers->ecccalc;
1648 const uint8_t *p = buf;
1649 uint32_t *eccpos = chip->ecc.layout->eccpos;
1650
1651 /* Software ecc calculation */
1652 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1653 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1654
1655 for (i = 0; i < chip->ecc.total; i++)
1656 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1657
1658 chip->ecc.write_page_raw(mtd, chip, buf);
1659}
1660
1661/**
1662 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1663 * @mtd: mtd info structure
1664 * @chip: nand chip info structure
1665 * @buf: data buffer
1666 */
1667static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1668 const uint8_t *buf)
1669{
1670 int i, eccsize = chip->ecc.size;
1671 int eccbytes = chip->ecc.bytes;
1672 int eccsteps = chip->ecc.steps;
1673 uint8_t *ecc_calc = chip->buffers->ecccalc;
1674 const uint8_t *p = buf;
1675 uint32_t *eccpos = chip->ecc.layout->eccpos;
1676
1677 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1678 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1679 chip->write_buf(mtd, p, eccsize);
1680 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1681 }
1682
1683 for (i = 0; i < chip->ecc.total; i++)
1684 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1685
1686 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1687}
1688
1689/**
1690 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1691 * @mtd: mtd info structure
1692 * @chip: nand chip info structure
1693 * @buf: data buffer
1694 *
1695 * The hw generator calculates the error syndrome automatically. Therefor
1696 * we need a special oob layout and handling.
1697 */
1698static void nand_write_page_syndrome(struct mtd_info *mtd,
1699 struct nand_chip *chip, const uint8_t *buf)
1700{
1701 int i, eccsize = chip->ecc.size;
1702 int eccbytes = chip->ecc.bytes;
1703 int eccsteps = chip->ecc.steps;
1704 const uint8_t *p = buf;
1705 uint8_t *oob = chip->oob_poi;
1706
1707 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1708
1709 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1710 chip->write_buf(mtd, p, eccsize);
1711
1712 if (chip->ecc.prepad) {
1713 chip->write_buf(mtd, oob, chip->ecc.prepad);
1714 oob += chip->ecc.prepad;
1715 }
1716
1717 chip->ecc.calculate(mtd, p, oob);
1718 chip->write_buf(mtd, oob, eccbytes);
1719 oob += eccbytes;
1720
1721 if (chip->ecc.postpad) {
1722 chip->write_buf(mtd, oob, chip->ecc.postpad);
1723 oob += chip->ecc.postpad;
1724 }
1725 }
1726
1727 /* Calculate remaining oob bytes */
1728 i = mtd->oobsize - (oob - chip->oob_poi);
1729 if (i)
1730 chip->write_buf(mtd, oob, i);
1731}
1732
1733/**
1734 * nand_write_page - [REPLACEABLE] write one page
1735 * @mtd: MTD device structure
1736 * @chip: NAND chip descriptor
1737 * @buf: the data to write
1738 * @page: page number to write
1739 * @cached: cached programming
1740 * @raw: use _raw version of write_page
1741 */
1742static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1743 const uint8_t *buf, int page, int cached, int raw)
1744{
1745 int status;
1746
1747 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1748
1749 if (unlikely(raw))
1750 chip->ecc.write_page_raw(mtd, chip, buf);
1751 else
1752 chip->ecc.write_page(mtd, chip, buf);
1753
1754 /*
1755 * Cached progamming disabled for now, Not sure if its worth the
1756 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1757 */
1758 cached = 0;
1759
1760 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1761
1762 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1763 status = chip->waitfunc(mtd, chip);
1764 /*
1765 * See if operation failed and additional status checks are
1766 * available
1767 */
1768 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1769 status = chip->errstat(mtd, chip, FL_WRITING, status,
1770 page);
1771
1772 if (status & NAND_STATUS_FAIL)
1773 return -EIO;
1774 } else {
1775 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1776 status = chip->waitfunc(mtd, chip);
1777 }
1778
1779#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1780 /* Send command to read back the data */
1781 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1782
1783 if (chip->verify_buf(mtd, buf, mtd->writesize))
1784 return -EIO;
1785#endif
1786 return 0;
1787}
1788
1789/**
1790 * nand_fill_oob - [Internal] Transfer client buffer to oob
1791 * @chip: nand chip structure
1792 * @oob: oob data buffer
1793 * @ops: oob ops structure
1794 */
1795static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1796 struct mtd_oob_ops *ops)
1797{
1798 size_t len = ops->ooblen;
1799
1800 switch(ops->mode) {
1801
1802 case MTD_OOB_PLACE:
1803 case MTD_OOB_RAW:
1804 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1805 return oob + len;
1806
1807 case MTD_OOB_AUTO: {
1808 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1809 uint32_t boffs = 0, woffs = ops->ooboffs;
1810 size_t bytes = 0;
1811
1812 for(; free->length && len; free++, len -= bytes) {
1813 /* Write request not from offset 0 ? */
1814 if (unlikely(woffs)) {
1815 if (woffs >= free->length) {
1816 woffs -= free->length;
1817 continue;
1818 }
1819 boffs = free->offset + woffs;
1820 bytes = min_t(size_t, len,
1821 (free->length - woffs));
1822 woffs = 0;
1823 } else {
1824 bytes = min_t(size_t, len, free->length);
1825 boffs = free->offset;
1826 }
1827 memcpy(chip->oob_poi + boffs, oob, bytes);
1828 oob += bytes;
1829 }
1830 return oob;
1831 }
1832 default:
1833 BUG();
1834 }
1835 return NULL;
1836}
1837
1838#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1839
1840/**
1841 * nand_do_write_ops - [Internal] NAND write with ECC
1842 * @mtd: MTD device structure
1843 * @to: offset to write to
1844 * @ops: oob operations description structure
1845 *
1846 * NAND write with ECC
1847 */
1848static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1849 struct mtd_oob_ops *ops)
1850{
1851 int chipnr, realpage, page, blockmask, column;
1852 struct nand_chip *chip = mtd->priv;
1853 uint32_t writelen = ops->len;
1854 uint8_t *oob = ops->oobbuf;
1855 uint8_t *buf = ops->datbuf;
1856 int ret, subpage;
1857
1858 ops->retlen = 0;
1859 if (!writelen)
1860 return 0;
1861
William Juulcfa460a2007-10-31 13:53:06 +01001862 column = to & (mtd->writesize - 1);
1863 subpage = column || (writelen & (mtd->writesize - 1));
1864
1865 if (subpage && oob)
1866 return -EINVAL;
1867
1868 chipnr = (int)(to >> chip->chip_shift);
1869 chip->select_chip(mtd, chipnr);
1870
1871 /* Check, if it is write protected */
1872 if (nand_check_wp(mtd)) {
1873 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1874 return -EIO;
1875 }
1876
1877 realpage = (int)(to >> chip->page_shift);
1878 page = realpage & chip->pagemask;
1879 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1880
1881 /* Invalidate the page cache, when we write to the cached page */
1882 if (to <= (chip->pagebuf << chip->page_shift) &&
1883 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1884 chip->pagebuf = -1;
1885
1886 /* If we're not given explicit OOB data, let it be 0xFF */
1887 if (likely(!oob))
1888 memset(chip->oob_poi, 0xff, mtd->oobsize);
1889
1890 while(1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001891 WATCHDOG_RESET();
1892
William Juulcfa460a2007-10-31 13:53:06 +01001893 int bytes = mtd->writesize;
1894 int cached = writelen > bytes && page != blockmask;
1895 uint8_t *wbuf = buf;
1896
1897 /* Partial page write ? */
1898 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1899 cached = 0;
1900 bytes = min_t(int, bytes - column, (int) writelen);
1901 chip->pagebuf = -1;
1902 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1903 memcpy(&chip->buffers->databuf[column], buf, bytes);
1904 wbuf = chip->buffers->databuf;
1905 }
1906
1907 if (unlikely(oob))
1908 oob = nand_fill_oob(chip, oob, ops);
1909
1910 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1911 (ops->mode == MTD_OOB_RAW));
1912 if (ret)
1913 break;
1914
1915 writelen -= bytes;
1916 if (!writelen)
1917 break;
1918
1919 column = 0;
1920 buf += bytes;
1921 realpage++;
1922
1923 page = realpage & chip->pagemask;
1924 /* Check, if we cross a chip boundary */
1925 if (!page) {
1926 chipnr++;
1927 chip->select_chip(mtd, -1);
1928 chip->select_chip(mtd, chipnr);
1929 }
1930 }
1931
1932 ops->retlen = ops->len - writelen;
1933 if (unlikely(oob))
1934 ops->oobretlen = ops->ooblen;
1935 return ret;
1936}
1937
1938/**
1939 * nand_write - [MTD Interface] NAND write with ECC
Wolfgang Denk932394a2005-08-17 12:55:25 +02001940 * @mtd: MTD device structure
1941 * @to: offset to write to
1942 * @len: number of bytes to write
1943 * @retlen: pointer to variable to store the number of written bytes
1944 * @buf: the data to write
1945 *
William Juulcfa460a2007-10-31 13:53:06 +01001946 * NAND write with ECC
1947 */
1948static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1949 size_t *retlen, const uint8_t *buf)
1950{
1951 struct nand_chip *chip = mtd->priv;
1952 int ret;
1953
Ben Gardinerbee038e2011-05-24 10:18:34 -04001954 /* Do not allow writes past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01001955 if ((to + len) > mtd->size)
1956 return -EINVAL;
1957 if (!len)
1958 return 0;
1959
1960 nand_get_device(chip, mtd, FL_WRITING);
1961
1962 chip->ops.len = len;
1963 chip->ops.datbuf = (uint8_t *)buf;
1964 chip->ops.oobbuf = NULL;
1965
1966 ret = nand_do_write_ops(mtd, to, &chip->ops);
1967
1968 *retlen = chip->ops.retlen;
1969
1970 nand_release_device(mtd);
1971
1972 return ret;
1973}
1974
1975/**
1976 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1977 * @mtd: MTD device structure
1978 * @to: offset to write to
1979 * @ops: oob operation description structure
1980 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02001981 * NAND write out-of-band
1982 */
William Juulcfa460a2007-10-31 13:53:06 +01001983static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1984 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001985{
William Juulcfa460a2007-10-31 13:53:06 +01001986 int chipnr, page, status, len;
1987 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001988
Scott Wood3167c532008-06-20 12:38:57 -05001989 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01001990 (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001991
William Juulcfa460a2007-10-31 13:53:06 +01001992 if (ops->mode == MTD_OOB_AUTO)
1993 len = chip->ecc.layout->oobavail;
1994 else
1995 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001996
1997 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01001998 if ((ops->ooboffs + ops->ooblen) > len) {
Scott Wood3167c532008-06-20 12:38:57 -05001999 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002000 "Attempt to write past end of page\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002001 return -EINVAL;
2002 }
2003
William Juulcfa460a2007-10-31 13:53:06 +01002004 if (unlikely(ops->ooboffs >= len)) {
2005 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002006 "Attempt to start write outside oob\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002007 return -EINVAL;
2008 }
2009
William Juulcfa460a2007-10-31 13:53:06 +01002010 /* Do not allow reads past end of device */
2011 if (unlikely(to >= mtd->size ||
2012 ops->ooboffs + ops->ooblen >
2013 ((mtd->size >> chip->page_shift) -
2014 (to >> chip->page_shift)) * len)) {
2015 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002016 "Attempt write beyond end of device\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002017 return -EINVAL;
2018 }
2019
William Juulcfa460a2007-10-31 13:53:06 +01002020 chipnr = (int)(to >> chip->chip_shift);
2021 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002022
William Juulcfa460a2007-10-31 13:53:06 +01002023 /* Shift to get page */
2024 page = (int)(to >> chip->page_shift);
2025
2026 /*
2027 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2028 * of my DiskOnChip 2000 test units) will clear the whole data page too
2029 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2030 * it in the doc2000 driver in August 1999. dwmw2.
2031 */
2032 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002033
2034 /* Check, if it is write protected */
2035 if (nand_check_wp(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002036 return -EROFS;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002037
Wolfgang Denk932394a2005-08-17 12:55:25 +02002038 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002039 if (page == chip->pagebuf)
2040 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002041
William Juulcfa460a2007-10-31 13:53:06 +01002042 memset(chip->oob_poi, 0xff, mtd->oobsize);
2043 nand_fill_oob(chip, ops->oobbuf, ops);
2044 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2045 memset(chip->oob_poi, 0xff, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002046
William Juulcfa460a2007-10-31 13:53:06 +01002047 if (status)
2048 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002049
William Juulcfa460a2007-10-31 13:53:06 +01002050 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002051
William Juulcfa460a2007-10-31 13:53:06 +01002052 return 0;
2053}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002054
William Juulcfa460a2007-10-31 13:53:06 +01002055/**
2056 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2057 * @mtd: MTD device structure
2058 * @to: offset to write to
2059 * @ops: oob operation description structure
2060 */
2061static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2062 struct mtd_oob_ops *ops)
2063{
2064 struct nand_chip *chip = mtd->priv;
2065 int ret = -ENOTSUPP;
2066
2067 ops->retlen = 0;
2068
2069 /* Do not allow writes past end of device */
2070 if (ops->datbuf && (to + ops->len) > mtd->size) {
2071 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002072 "Attempt read beyond end of device\n");
William Juulcfa460a2007-10-31 13:53:06 +01002073 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002074 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002075
William Juulcfa460a2007-10-31 13:53:06 +01002076 nand_get_device(chip, mtd, FL_WRITING);
2077
2078 switch(ops->mode) {
2079 case MTD_OOB_PLACE:
2080 case MTD_OOB_AUTO:
2081 case MTD_OOB_RAW:
2082 break;
2083
2084 default:
2085 goto out;
2086 }
2087
2088 if (!ops->datbuf)
2089 ret = nand_do_write_oob(mtd, to, ops);
2090 else
2091 ret = nand_do_write_ops(mtd, to, ops);
2092
2093 out:
2094 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002095 return ret;
2096}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002097
2098/**
2099 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2100 * @mtd: MTD device structure
2101 * @page: the page address of the block which will be erased
2102 *
2103 * Standard erase command for NAND chips
2104 */
William Juulcfa460a2007-10-31 13:53:06 +01002105static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002106{
William Juulcfa460a2007-10-31 13:53:06 +01002107 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002108 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002109 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2110 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002111}
2112
2113/**
2114 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2115 * @mtd: MTD device structure
2116 * @page: the page address of the block which will be erased
2117 *
2118 * AND multi block erase command function
2119 * Erase 4 consecutive blocks
2120 */
William Juulcfa460a2007-10-31 13:53:06 +01002121static void multi_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002122{
William Juulcfa460a2007-10-31 13:53:06 +01002123 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002124 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002125 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2126 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2127 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2128 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2129 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002130}
2131
2132/**
2133 * nand_erase - [MTD Interface] erase block(s)
2134 * @mtd: MTD device structure
2135 * @instr: erase instruction
2136 *
2137 * Erase one ore more blocks
2138 */
William Juulcfa460a2007-10-31 13:53:06 +01002139static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002140{
William Juulcfa460a2007-10-31 13:53:06 +01002141 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002142}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002143
William Juulcfa460a2007-10-31 13:53:06 +01002144#define BBT_PAGE_MASK 0xffffff3f
Wolfgang Denk932394a2005-08-17 12:55:25 +02002145/**
William Juulcfa460a2007-10-31 13:53:06 +01002146 * nand_erase_nand - [Internal] erase block(s)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002147 * @mtd: MTD device structure
2148 * @instr: erase instruction
2149 * @allowbbt: allow erasing the bbt area
2150 *
2151 * Erase one ore more blocks
2152 */
William Juulcfa460a2007-10-31 13:53:06 +01002153int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2154 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002155{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002156 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002157 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002158 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
William Juulcfa460a2007-10-31 13:53:06 +01002159 unsigned int bbt_masked_page = 0xffffffff;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002160 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002161
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002162 MTDDEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, "
2163 "len = %llu\n", (unsigned long long) instr->addr,
2164 (unsigned long long) instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002165
2166 /* Start address must align on block boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002167 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wood3167c532008-06-20 12:38:57 -05002168 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002169 return -EINVAL;
2170 }
2171
2172 /* Length must align on block boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002173 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
Scott Wood3167c532008-06-20 12:38:57 -05002174 MTDDEBUG (MTD_DEBUG_LEVEL0,
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002175 "nand_erase: Length not block aligned\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002176 return -EINVAL;
2177 }
2178
2179 /* Do not allow erase past end of device */
2180 if ((instr->len + instr->addr) > mtd->size) {
Scott Wood3167c532008-06-20 12:38:57 -05002181 MTDDEBUG (MTD_DEBUG_LEVEL0,
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002182 "nand_erase: Erase past end of device\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002183 return -EINVAL;
2184 }
2185
2186 instr->fail_addr = 0xffffffff;
2187
2188 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002189 nand_get_device(chip, mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002190
2191 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002192 page = (int)(instr->addr >> chip->page_shift);
2193 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002194
2195 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002196 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002197
Wolfgang Denk932394a2005-08-17 12:55:25 +02002198 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002199 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002200
Wolfgang Denk932394a2005-08-17 12:55:25 +02002201 /* Check, if it is write protected */
2202 if (nand_check_wp(mtd)) {
Scott Wood3167c532008-06-20 12:38:57 -05002203 MTDDEBUG (MTD_DEBUG_LEVEL0,
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002204 "nand_erase: Device is write protected!!!\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002205 instr->state = MTD_ERASE_FAILED;
2206 goto erase_exit;
2207 }
2208
William Juulcfa460a2007-10-31 13:53:06 +01002209 /*
2210 * If BBT requires refresh, set the BBT page mask to see if the BBT
2211 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2212 * can not be matched. This is also done when the bbt is actually
2213 * erased to avoid recusrsive updates
2214 */
2215 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2216 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2217
Wolfgang Denk932394a2005-08-17 12:55:25 +02002218 /* Loop through the pages */
2219 len = instr->len;
2220
2221 instr->state = MTD_ERASING;
2222
2223 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002224 WATCHDOG_RESET();
William Juulcfa460a2007-10-31 13:53:06 +01002225 /*
2226 * heck if we have a bad block, we do not erase bad blocks !
2227 */
Marek Vasut6d414192011-09-12 06:04:06 +02002228 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002229 chip->page_shift, 0, allowbbt)) {
2230 printk(KERN_WARNING "nand_erase: attempt to erase a "
2231 "bad block at page 0x%08x\n", page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002232 instr->state = MTD_ERASE_FAILED;
2233 goto erase_exit;
2234 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002235
William Juulcfa460a2007-10-31 13:53:06 +01002236 /*
2237 * Invalidate the page cache, if we erase the block which
2238 * contains the current cached page
2239 */
2240 if (page <= chip->pagebuf && chip->pagebuf <
2241 (page + pages_per_block))
2242 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002243
William Juulcfa460a2007-10-31 13:53:06 +01002244 chip->erase_cmd(mtd, page & chip->pagemask);
2245
2246 status = chip->waitfunc(mtd, chip);
2247
2248 /*
2249 * See if operation failed and additional status checks are
2250 * available
2251 */
2252 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2253 status = chip->errstat(mtd, chip, FL_ERASING,
2254 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002255
2256 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002257 if (status & NAND_STATUS_FAIL) {
Scott Wood3167c532008-06-20 12:38:57 -05002258 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002259 "Failed erase, page 0x%08x\n", page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002260 instr->state = MTD_ERASE_FAILED;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002261 instr->fail_addr = ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002262 goto erase_exit;
2263 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002264
William Juulcfa460a2007-10-31 13:53:06 +01002265 /*
2266 * If BBT requires refresh, set the BBT rewrite flag to the
2267 * page being erased
2268 */
2269 if (bbt_masked_page != 0xffffffff &&
2270 (page & BBT_PAGE_MASK) == bbt_masked_page)
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002271 rewrite_bbt[chipnr] =
2272 ((loff_t)page << chip->page_shift);
William Juulcfa460a2007-10-31 13:53:06 +01002273
Wolfgang Denk932394a2005-08-17 12:55:25 +02002274 /* Increment page address and decrement length */
William Juulcfa460a2007-10-31 13:53:06 +01002275 len -= (1 << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002276 page += pages_per_block;
2277
2278 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002279 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002280 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002281 chip->select_chip(mtd, -1);
2282 chip->select_chip(mtd, chipnr);
2283
2284 /*
2285 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2286 * page mask to see if this BBT should be rewritten
2287 */
2288 if (bbt_masked_page != 0xffffffff &&
2289 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2290 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2291 BBT_PAGE_MASK;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002292 }
2293 }
2294 instr->state = MTD_ERASE_DONE;
2295
William Juulcfa460a2007-10-31 13:53:06 +01002296 erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002297
2298 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002299
2300 /* Deselect and wake up anyone waiting on the device */
2301 nand_release_device(mtd);
2302
Scott Woodc45912d2008-10-24 16:20:43 -05002303 /* Do call back function */
2304 if (!ret)
2305 mtd_erase_callback(instr);
2306
William Juulcfa460a2007-10-31 13:53:06 +01002307 /*
2308 * If BBT requires refresh and erase was successful, rewrite any
2309 * selected bad block tables
2310 */
2311 if (bbt_masked_page == 0xffffffff || ret)
2312 return ret;
2313
2314 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2315 if (!rewrite_bbt[chipnr])
2316 continue;
2317 /* update the BBT for chip */
2318 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002319 "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2320 chip->bbt_td->pages[chipnr]);
William Juulcfa460a2007-10-31 13:53:06 +01002321 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2322 }
2323
Wolfgang Denk932394a2005-08-17 12:55:25 +02002324 /* Return more or less happy */
2325 return ret;
2326}
2327
2328/**
2329 * nand_sync - [MTD Interface] sync
2330 * @mtd: MTD device structure
2331 *
2332 * Sync is actually a wait for chip ready function
2333 */
William Juulcfa460a2007-10-31 13:53:06 +01002334static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002335{
William Juulcfa460a2007-10-31 13:53:06 +01002336 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002337
Scott Wood3167c532008-06-20 12:38:57 -05002338 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02002339
2340 /* Grab the lock and see if the device is available */
William Juulcfa460a2007-10-31 13:53:06 +01002341 nand_get_device(chip, mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002342 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002343 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002344}
2345
Wolfgang Denk932394a2005-08-17 12:55:25 +02002346/**
William Juulcfa460a2007-10-31 13:53:06 +01002347 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02002348 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002349 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002350 */
William Juulcfa460a2007-10-31 13:53:06 +01002351static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002352{
2353 /* Check for invalid offset */
William Juulcfa460a2007-10-31 13:53:06 +01002354 if (offs > mtd->size)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002355 return -EINVAL;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002356
William Juulcfa460a2007-10-31 13:53:06 +01002357 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002358}
2359
2360/**
William Juulcfa460a2007-10-31 13:53:06 +01002361 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02002362 * @mtd: MTD device structure
2363 * @ofs: offset relative to mtd start
2364 */
William Juulcfa460a2007-10-31 13:53:06 +01002365static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002366{
William Juulcfa460a2007-10-31 13:53:06 +01002367 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002368 int ret;
2369
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002370 if ((ret = nand_block_isbad(mtd, ofs))) {
2371 /* If it was bad already, return success and do nothing. */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002372 if (ret > 0)
2373 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002374 return ret;
2375 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002376
William Juulcfa460a2007-10-31 13:53:06 +01002377 return chip->block_markbad(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002378}
2379
William Juulcfa460a2007-10-31 13:53:06 +01002380/*
2381 * Set default functions
2382 */
2383static void nand_set_defaults(struct nand_chip *chip, int busw)
2384{
2385 /* check for proper chip_delay setup, set 20us if not */
2386 if (!chip->chip_delay)
2387 chip->chip_delay = 20;
2388
2389 /* check, if a user supplied command function given */
2390 if (chip->cmdfunc == NULL)
2391 chip->cmdfunc = nand_command;
2392
2393 /* check, if a user supplied wait function given */
2394 if (chip->waitfunc == NULL)
2395 chip->waitfunc = nand_wait;
2396
2397 if (!chip->select_chip)
2398 chip->select_chip = nand_select_chip;
2399 if (!chip->read_byte)
2400 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2401 if (!chip->read_word)
2402 chip->read_word = nand_read_word;
2403 if (!chip->block_bad)
2404 chip->block_bad = nand_block_bad;
2405 if (!chip->block_markbad)
2406 chip->block_markbad = nand_default_block_markbad;
2407 if (!chip->write_buf)
2408 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2409 if (!chip->read_buf)
2410 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2411 if (!chip->verify_buf)
2412 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2413 if (!chip->scan_bbt)
2414 chip->scan_bbt = nand_default_bbt;
Scott Wood5b8e6bb2010-08-25 17:42:49 -05002415 if (!chip->controller)
William Juulcfa460a2007-10-31 13:53:06 +01002416 chip->controller = &chip->hwcontrol;
William Juulcfa460a2007-10-31 13:53:06 +01002417}
2418
Florian Fainelli0272c712011-02-25 00:01:34 +00002419#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2420static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01002421{
Florian Fainelli0272c712011-02-25 00:01:34 +00002422 int i;
William Juulcfa460a2007-10-31 13:53:06 +01002423
Florian Fainelli0272c712011-02-25 00:01:34 +00002424 while (len--) {
2425 crc ^= *p++ << 8;
2426 for (i = 0; i < 8; i++)
2427 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05002428 }
2429
Florian Fainelli0272c712011-02-25 00:01:34 +00002430 return crc;
2431}
William Juulcfa460a2007-10-31 13:53:06 +01002432
Florian Fainelli0272c712011-02-25 00:01:34 +00002433/*
2434 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise
2435 */
2436static int nand_flash_detect_onfi(struct mtd_info *mtd,
2437 struct nand_chip *chip,
2438 int *busw)
2439{
2440 struct nand_onfi_params *p = &chip->onfi_params;
2441 int i;
2442 int val;
2443
2444 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2445 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2446 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2447 return 0;
2448
2449 printk(KERN_INFO "ONFI flash detected\n");
2450 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2451 for (i = 0; i < 3; i++) {
2452 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2453 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2454 le16_to_cpu(p->crc)) {
2455 printk(KERN_INFO "ONFI param page %d valid\n", i);
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002456 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00002457 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02002458 }
William Juulcfa460a2007-10-31 13:53:06 +01002459
Florian Fainelli0272c712011-02-25 00:01:34 +00002460 if (i == 3)
2461 return 0;
2462
2463 /* check version */
2464 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002465 if (val & (1 << 5))
2466 chip->onfi_version = 23;
2467 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00002468 chip->onfi_version = 22;
2469 else if (val & (1 << 3))
2470 chip->onfi_version = 21;
2471 else if (val & (1 << 2))
2472 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002473 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00002474 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02002475 else
2476 chip->onfi_version = 0;
2477
2478 if (!chip->onfi_version) {
2479 printk(KERN_INFO "%s: unsupported ONFI "
2480 "version: %d\n", __func__, val);
2481 return 0;
2482 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002483
William Juulcfa460a2007-10-31 13:53:06 +01002484 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00002485 mtd->name = p->model;
William Juulcfa460a2007-10-31 13:53:06 +01002486
Florian Fainelli0272c712011-02-25 00:01:34 +00002487 mtd->writesize = le32_to_cpu(p->byte_per_page);
2488 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2489 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Florian Fainellia931f492011-04-03 18:23:52 +02002490 chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
Florian Fainelli0272c712011-02-25 00:01:34 +00002491 *busw = 0;
2492 if (le16_to_cpu(p->features) & 1)
2493 *busw = NAND_BUSWIDTH_16;
William Juulcfa460a2007-10-31 13:53:06 +01002494
Florian Fainelli0272c712011-02-25 00:01:34 +00002495 return 1;
2496}
2497#else
2498static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2499 struct nand_chip *chip,
2500 int *busw)
2501{
2502 return 0;
2503}
2504#endif
2505
2506static void nand_flash_detect_non_onfi(struct mtd_info *mtd,
2507 struct nand_chip *chip,
2508 const struct nand_flash_dev *type,
2509 int *busw)
2510{
William Juulcfa460a2007-10-31 13:53:06 +01002511 /* Newer devices have all the information in additional id bytes */
2512 if (!type->pagesize) {
2513 int extid;
2514 /* The 3rd id byte holds MLC / multichip data */
2515 chip->cellinfo = chip->read_byte(mtd);
2516 /* The 4th id byte is the important one */
2517 extid = chip->read_byte(mtd);
2518 /* Calc pagesize */
2519 mtd->writesize = 1024 << (extid & 0x3);
2520 extid >>= 2;
2521 /* Calc oobsize */
2522 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2523 extid >>= 2;
2524 /* Calc blocksize. Blocksize is multiples of 64KiB */
2525 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2526 extid >>= 2;
2527 /* Get buswidth information */
Florian Fainelli0272c712011-02-25 00:01:34 +00002528 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002529
2530 } else {
2531 /*
2532 * Old devices have chip data hardcoded in the device id table
2533 */
2534 mtd->erasesize = type->erasesize;
2535 mtd->writesize = type->pagesize;
2536 mtd->oobsize = mtd->writesize / 32;
Florian Fainelli0272c712011-02-25 00:01:34 +00002537 *busw = type->options & NAND_BUSWIDTH_16;
William Juulcfa460a2007-10-31 13:53:06 +01002538 }
Florian Fainelli0272c712011-02-25 00:01:34 +00002539}
2540
2541/*
2542 * Get the flash and manufacturer id and lookup if the type is supported
2543 */
2544static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2545 struct nand_chip *chip,
2546 int busw,
2547 int *maf_id, int *dev_id,
2548 const struct nand_flash_dev *type)
2549{
2550 int ret, maf_idx;
2551 int tmp_id, tmp_manf;
2552
2553 /* Select the device */
2554 chip->select_chip(mtd, 0);
2555
2556 /*
2557 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2558 * after power-up
2559 */
2560 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2561
2562 /* Send the command for reading device ID */
2563 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2564
2565 /* Read manufacturer and device IDs */
2566 *maf_id = chip->read_byte(mtd);
2567 *dev_id = chip->read_byte(mtd);
2568
2569 /* Try again to make sure, as some systems the bus-hold or other
2570 * interface concerns can cause random data which looks like a
2571 * possibly credible NAND flash to appear. If the two results do
2572 * not match, ignore the device completely.
2573 */
2574
2575 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2576
2577 /* Read manufacturer and device IDs */
2578
2579 tmp_manf = chip->read_byte(mtd);
2580 tmp_id = chip->read_byte(mtd);
2581
2582 if (tmp_manf != *maf_id || tmp_id != *dev_id) {
2583 printk(KERN_INFO "%s: second ID read did not match "
2584 "%02x,%02x against %02x,%02x\n", __func__,
2585 *maf_id, *dev_id, tmp_manf, tmp_id);
2586 return ERR_PTR(-ENODEV);
2587 }
2588
2589 if (!type)
2590 type = nand_flash_ids;
2591
2592 for (; type->name != NULL; type++)
2593 if (*dev_id == type->id)
2594 break;
2595
2596 if (!type->name) {
2597 /* supress warning if there is no nand */
2598 if (*maf_id != 0x00 && *maf_id != 0xff &&
2599 *dev_id != 0x00 && *dev_id != 0xff)
2600 printk(KERN_INFO "%s: unknown NAND device: "
2601 "Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
2602 __func__, *maf_id, *dev_id);
2603 return ERR_PTR(-ENODEV);
2604 }
2605
2606 if (!mtd->name)
2607 mtd->name = type->name;
2608
2609 chip->chipsize = (uint64_t)type->chipsize << 20;
2610 chip->onfi_version = 0;
2611
2612 ret = nand_flash_detect_onfi(mtd, chip, &busw);
2613 if (!ret)
2614 nand_flash_detect_non_onfi(mtd, chip, type, &busw);
2615
2616 /* Get chip options, preserve non chip based options */
2617 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2618 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2619
2620 /*
2621 * Set chip as a default. Board drivers can override it, if necessary
2622 */
2623 chip->options |= NAND_NO_AUTOINCR;
William Juulcfa460a2007-10-31 13:53:06 +01002624
2625 /* Try to identify manufacturer */
2626 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2627 if (nand_manuf_ids[maf_idx].id == *maf_id)
2628 break;
2629 }
2630
2631 /*
2632 * Check, if buswidth is correct. Hardware drivers should set
2633 * chip correct !
2634 */
2635 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2636 printk(KERN_INFO "NAND device: Manufacturer ID:"
2637 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
Florian Fainelli0272c712011-02-25 00:01:34 +00002638 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
William Juulcfa460a2007-10-31 13:53:06 +01002639 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2640 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2641 busw ? 16 : 8);
2642 return ERR_PTR(-EINVAL);
2643 }
2644
2645 /* Calculate the address shift from the page size */
2646 chip->page_shift = ffs(mtd->writesize) - 1;
2647 /* Convert chipsize to number of pages per chip -1. */
2648 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2649
2650 chip->bbt_erase_shift = chip->phys_erase_shift =
2651 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002652 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05002653 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002654 else
2655 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 31;
William Juulcfa460a2007-10-31 13:53:06 +01002656
2657 /* Set the bad block position */
2658 chip->badblockpos = mtd->writesize > 512 ?
2659 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2660
William Juulcfa460a2007-10-31 13:53:06 +01002661 /* Check if chip is a not a samsung device. Do not clear the
2662 * options for chips which are not having an extended id.
2663 */
2664 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2665 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2666
2667 /* Check for AND chips with 4 page planes */
2668 if (chip->options & NAND_4PAGE_ARRAY)
2669 chip->erase_cmd = multi_erase_cmd;
2670 else
2671 chip->erase_cmd = single_erase_cmd;
2672
2673 /* Do not replace user supplied command function ! */
2674 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2675 chip->cmdfunc = nand_command_lp;
2676
Stefan Roesee52b34d2008-01-10 18:47:33 +01002677 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
Florian Fainelli0272c712011-02-25 00:01:34 +00002678 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01002679 nand_manuf_ids[maf_idx].name, type->name);
William Juulcfa460a2007-10-31 13:53:06 +01002680
2681 return type;
2682}
2683
2684/**
2685 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2686 * @mtd: MTD device structure
2687 * @maxchips: Number of chips to scan for
Lei Wen245eb902011-01-06 09:48:18 +08002688 * @table: Alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01002689 *
2690 * This is the first phase of the normal nand_scan() function. It
2691 * reads the flash ID and sets up MTD fields accordingly.
2692 *
2693 * The mtd->owner field must be set to the module of the caller.
2694 */
Lei Wen245eb902011-01-06 09:48:18 +08002695int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2696 const struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01002697{
Florian Fainelli0272c712011-02-25 00:01:34 +00002698 int i, busw, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01002699 struct nand_chip *chip = mtd->priv;
Mike Frysinger0bdecd82010-10-20 01:15:21 +00002700 const struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01002701
2702 /* Get buswidth to select the correct functions */
2703 busw = chip->options & NAND_BUSWIDTH_16;
2704 /* Set the default functions */
2705 nand_set_defaults(chip, busw);
2706
2707 /* Read the flash type */
Florian Fainelli0272c712011-02-25 00:01:34 +00002708 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01002709
2710 if (IS_ERR(type)) {
Peter Tyser10dc6a92009-02-04 13:39:40 -06002711#ifndef CONFIG_SYS_NAND_QUIET_TEST
William Juulcfa460a2007-10-31 13:53:06 +01002712 printk(KERN_WARNING "No NAND device found!!!\n");
Peter Tyser10dc6a92009-02-04 13:39:40 -06002713#endif
William Juulcfa460a2007-10-31 13:53:06 +01002714 chip->select_chip(mtd, -1);
2715 return PTR_ERR(type);
2716 }
2717
2718 /* Check for a chip array */
2719 for (i = 1; i < maxchips; i++) {
2720 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02002721 /* See comment in nand_get_flash_type for reset */
2722 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002723 /* Send the command for reading device ID */
2724 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2725 /* Read manufacturer and device IDs */
2726 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainelli0272c712011-02-25 00:01:34 +00002727 nand_dev_id != chip->read_byte(mtd))
William Juulcfa460a2007-10-31 13:53:06 +01002728 break;
2729 }
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01002730#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01002731 if (i > 1)
2732 printk(KERN_INFO "%d NAND chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01002733#endif
William Juulcfa460a2007-10-31 13:53:06 +01002734
2735 /* Store the number of chips and calc total size for mtd */
2736 chip->numchips = i;
2737 mtd->size = i * chip->chipsize;
2738
2739 return 0;
2740}
2741
2742
2743/**
2744 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2745 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01002746 *
2747 * This is the second phase of the normal nand_scan() function. It
2748 * fills out all the uninitialized function pointers with the defaults
2749 * and scans for a bad block table if appropriate.
2750 */
2751int nand_scan_tail(struct mtd_info *mtd)
2752{
2753 int i;
2754 struct nand_chip *chip = mtd->priv;
2755
2756 if (!(chip->options & NAND_OWN_BUFFERS))
2757 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2758 if (!chip->buffers)
2759 return -ENOMEM;
2760
2761 /* Set the internal oob buffer location, just after the page data */
2762 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2763
2764 /*
2765 * If no default placement scheme is given, select an appropriate one
2766 */
Christian Hitz4c6de852011-10-12 09:31:59 +02002767 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01002768 switch (mtd->oobsize) {
2769 case 8:
2770 chip->ecc.layout = &nand_oob_8;
2771 break;
2772 case 16:
2773 chip->ecc.layout = &nand_oob_16;
2774 break;
2775 case 64:
2776 chip->ecc.layout = &nand_oob_64;
2777 break;
2778 case 128:
2779 chip->ecc.layout = &nand_oob_128;
2780 break;
2781 default:
2782 printk(KERN_WARNING "No oob scheme defined for "
2783 "oobsize %d\n", mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01002784 }
2785 }
2786
2787 if (!chip->write_page)
2788 chip->write_page = nand_write_page;
2789
2790 /*
2791 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2792 * selected and we have 256 byte pagesize fallback to software ECC
2793 */
William Juulcfa460a2007-10-31 13:53:06 +01002794
2795 switch (chip->ecc.mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04002796 case NAND_ECC_HW_OOB_FIRST:
2797 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2798 if (!chip->ecc.calculate || !chip->ecc.correct ||
2799 !chip->ecc.hwctl) {
2800 printk(KERN_WARNING "No ECC functions supplied, "
2801 "Hardware ECC not possible\n");
2802 BUG();
2803 }
2804 if (!chip->ecc.read_page)
2805 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2806
William Juulcfa460a2007-10-31 13:53:06 +01002807 case NAND_ECC_HW:
2808 /* Use standard hwecc read page function ? */
2809 if (!chip->ecc.read_page)
2810 chip->ecc.read_page = nand_read_page_hwecc;
2811 if (!chip->ecc.write_page)
2812 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell7e866612009-11-07 16:27:01 -05002813 if (!chip->ecc.read_page_raw)
2814 chip->ecc.read_page_raw = nand_read_page_raw;
2815 if (!chip->ecc.write_page_raw)
2816 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01002817 if (!chip->ecc.read_oob)
2818 chip->ecc.read_oob = nand_read_oob_std;
2819 if (!chip->ecc.write_oob)
2820 chip->ecc.write_oob = nand_write_oob_std;
2821
2822 case NAND_ECC_HW_SYNDROME:
Scott Wood41ef8c72008-03-18 15:29:14 -05002823 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2824 !chip->ecc.hwctl) &&
2825 (!chip->ecc.read_page ||
2826 chip->ecc.read_page == nand_read_page_hwecc ||
2827 !chip->ecc.write_page ||
2828 chip->ecc.write_page == nand_write_page_hwecc)) {
William Juulcfa460a2007-10-31 13:53:06 +01002829 printk(KERN_WARNING "No ECC functions supplied, "
2830 "Hardware ECC not possible\n");
2831 BUG();
2832 }
2833 /* Use standard syndrome read/write page function ? */
2834 if (!chip->ecc.read_page)
2835 chip->ecc.read_page = nand_read_page_syndrome;
2836 if (!chip->ecc.write_page)
2837 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell7e866612009-11-07 16:27:01 -05002838 if (!chip->ecc.read_page_raw)
2839 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2840 if (!chip->ecc.write_page_raw)
2841 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01002842 if (!chip->ecc.read_oob)
2843 chip->ecc.read_oob = nand_read_oob_syndrome;
2844 if (!chip->ecc.write_oob)
2845 chip->ecc.write_oob = nand_write_oob_syndrome;
2846
2847 if (mtd->writesize >= chip->ecc.size)
2848 break;
2849 printk(KERN_WARNING "%d byte HW ECC not possible on "
2850 "%d byte page size, fallback to SW ECC\n",
2851 chip->ecc.size, mtd->writesize);
2852 chip->ecc.mode = NAND_ECC_SOFT;
2853
2854 case NAND_ECC_SOFT:
2855 chip->ecc.calculate = nand_calculate_ecc;
2856 chip->ecc.correct = nand_correct_data;
2857 chip->ecc.read_page = nand_read_page_swecc;
Scott Woodc45912d2008-10-24 16:20:43 -05002858 chip->ecc.read_subpage = nand_read_subpage;
William Juulcfa460a2007-10-31 13:53:06 +01002859 chip->ecc.write_page = nand_write_page_swecc;
David Brownell7e866612009-11-07 16:27:01 -05002860 chip->ecc.read_page_raw = nand_read_page_raw;
2861 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01002862 chip->ecc.read_oob = nand_read_oob_std;
2863 chip->ecc.write_oob = nand_write_oob_std;
2864 chip->ecc.size = 256;
2865 chip->ecc.bytes = 3;
2866 break;
2867
Christian Hitz4c6de852011-10-12 09:31:59 +02002868 case NAND_ECC_SOFT_BCH:
2869 if (!mtd_nand_has_bch()) {
2870 printk(KERN_WARNING "CONFIG_MTD_ECC_BCH not enabled\n");
2871 return -EINVAL;
2872 }
2873 chip->ecc.calculate = nand_bch_calculate_ecc;
2874 chip->ecc.correct = nand_bch_correct_data;
2875 chip->ecc.read_page = nand_read_page_swecc;
2876 chip->ecc.read_subpage = nand_read_subpage;
2877 chip->ecc.write_page = nand_write_page_swecc;
2878 chip->ecc.read_page_raw = nand_read_page_raw;
2879 chip->ecc.write_page_raw = nand_write_page_raw;
2880 chip->ecc.read_oob = nand_read_oob_std;
2881 chip->ecc.write_oob = nand_write_oob_std;
2882 /*
2883 * Board driver should supply ecc.size and ecc.bytes values to
2884 * select how many bits are correctable; see nand_bch_init()
2885 * for details.
2886 * Otherwise, default to 4 bits for large page devices
2887 */
2888 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
2889 chip->ecc.size = 512;
2890 chip->ecc.bytes = 7;
2891 }
2892 chip->ecc.priv = nand_bch_init(mtd,
2893 chip->ecc.size,
2894 chip->ecc.bytes,
2895 &chip->ecc.layout);
2896 if (!chip->ecc.priv)
2897 printk(KERN_WARNING "BCH ECC initialization failed!\n");
2898
2899 break;
2900
William Juulcfa460a2007-10-31 13:53:06 +01002901 case NAND_ECC_NONE:
2902 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2903 "This is not recommended !!\n");
2904 chip->ecc.read_page = nand_read_page_raw;
2905 chip->ecc.write_page = nand_write_page_raw;
2906 chip->ecc.read_oob = nand_read_oob_std;
David Brownell7e866612009-11-07 16:27:01 -05002907 chip->ecc.read_page_raw = nand_read_page_raw;
2908 chip->ecc.write_page_raw = nand_write_page_raw;
William Juulcfa460a2007-10-31 13:53:06 +01002909 chip->ecc.write_oob = nand_write_oob_std;
2910 chip->ecc.size = mtd->writesize;
2911 chip->ecc.bytes = 0;
2912 break;
2913
2914 default:
2915 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2916 chip->ecc.mode);
2917 BUG();
2918 }
2919
2920 /*
2921 * The number of bytes available for a client to place data into
2922 * the out of band area
2923 */
2924 chip->ecc.layout->oobavail = 0;
Sandeep Paulraj5df3c2b2009-11-07 14:25:18 -05002925 for (i = 0; chip->ecc.layout->oobfree[i].length
2926 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
William Juulcfa460a2007-10-31 13:53:06 +01002927 chip->ecc.layout->oobavail +=
2928 chip->ecc.layout->oobfree[i].length;
2929 mtd->oobavail = chip->ecc.layout->oobavail;
2930
2931 /*
2932 * Set the number of read / write steps for one page depending on ECC
2933 * mode
2934 */
2935 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2936 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2937 printk(KERN_WARNING "Invalid ecc parameters\n");
2938 BUG();
2939 }
2940 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2941
2942 /*
2943 * Allow subpage writes up to ecc.steps. Not possible for MLC
2944 * FLASH.
2945 */
2946 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2947 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2948 switch(chip->ecc.steps) {
2949 case 2:
2950 mtd->subpage_sft = 1;
2951 break;
2952 case 4:
2953 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05002954 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01002955 mtd->subpage_sft = 2;
2956 break;
2957 }
2958 }
2959 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2960
2961 /* Initialize state */
2962 chip->state = FL_READY;
2963
2964 /* De-select the device */
2965 chip->select_chip(mtd, -1);
2966
2967 /* Invalidate the pagebuffer reference */
2968 chip->pagebuf = -1;
2969
2970 /* Fill in remaining MTD driver data */
2971 mtd->type = MTD_NANDFLASH;
2972 mtd->flags = MTD_CAP_NANDFLASH;
2973 mtd->erase = nand_erase;
2974 mtd->point = NULL;
2975 mtd->unpoint = NULL;
2976 mtd->read = nand_read;
2977 mtd->write = nand_write;
2978 mtd->read_oob = nand_read_oob;
2979 mtd->write_oob = nand_write_oob;
2980 mtd->sync = nand_sync;
2981 mtd->lock = NULL;
2982 mtd->unlock = NULL;
William Juulcfa460a2007-10-31 13:53:06 +01002983 mtd->block_isbad = nand_block_isbad;
2984 mtd->block_markbad = nand_block_markbad;
2985
2986 /* propagate ecc.layout to mtd_info */
2987 mtd->ecclayout = chip->ecc.layout;
2988
2989 /* Check, if we should skip the bad block table scan */
2990 if (chip->options & NAND_SKIP_BBTSCAN)
Ilya Yanok13f0fd92008-06-30 15:34:40 +02002991 chip->options |= NAND_BBT_SCANNED;
William Juulcfa460a2007-10-31 13:53:06 +01002992
Ilya Yanok13f0fd92008-06-30 15:34:40 +02002993 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002994}
2995
William Juulcfa460a2007-10-31 13:53:06 +01002996/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002997 * nand_scan - [NAND Interface] Scan for the NAND device
2998 * @mtd: MTD device structure
2999 * @maxchips: Number of chips to scan for
3000 *
William Juulcfa460a2007-10-31 13:53:06 +01003001 * This fills out all the uninitialized function pointers
Wolfgang Denk932394a2005-08-17 12:55:25 +02003002 * with the defaults.
3003 * The flash ID is read and the mtd/chip structures are
William Juulcfa460a2007-10-31 13:53:06 +01003004 * filled with the appropriate values.
3005 * The mtd->owner field must be set to the module of the caller
Wolfgang Denk932394a2005-08-17 12:55:25 +02003006 *
3007 */
William Juulcfa460a2007-10-31 13:53:06 +01003008int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003009{
William Juulcfa460a2007-10-31 13:53:06 +01003010 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003011
Lei Wen245eb902011-01-06 09:48:18 +08003012 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01003013 if (!ret)
3014 ret = nand_scan_tail(mtd);
3015 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003016}
3017
3018/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02003019 * nand_release - [NAND Interface] Free resources held by the NAND device
Wolfgang Denk932394a2005-08-17 12:55:25 +02003020 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003021*/
3022void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003023{
William Juulcfa460a2007-10-31 13:53:06 +01003024 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02003025
Christian Hitz4c6de852011-10-12 09:31:59 +02003026 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3027 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3028
Wolfgang Denk932394a2005-08-17 12:55:25 +02003029#ifdef CONFIG_MTD_PARTITIONS
3030 /* Deregister partitions */
William Juulcfa460a2007-10-31 13:53:06 +01003031 del_mtd_partitions(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003032#endif
William Juulcfa460a2007-10-31 13:53:06 +01003033
3034 /* Free bad block table memory */
3035 kfree(chip->bbt);
3036 if (!(chip->options & NAND_OWN_BUFFERS))
3037 kfree(chip->buffers);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003038}