blob: ed62e1ee0f815b0829d97307113c10d1d214bf09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +020010 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030027 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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
David Woodhouse552d9202006-05-14 01:20:46 +010035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/compatmac.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
51#ifdef CONFIG_MTD_PARTITIONS
52#include <linux/mtd/partitions.h>
53#endif
54
55/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .eccbytes = 3,
58 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020059 .oobfree = {
60 {.offset = 3,
61 .length = 2},
62 {.offset = 6,
63 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .eccbytes = 6,
68 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069 .oobfree = {
70 {.offset = 8,
71 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020074static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .eccbytes = 24,
76 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010077 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020080 .oobfree = {
81 {.offset = 2,
82 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Thomas Gleixner81ec5362007-12-12 17:27:03 +010085static struct nand_ecclayout nand_oob_128 = {
86 .eccbytes = 48,
87 .eccpos = {
88 80, 81, 82, 83, 84, 85, 86, 87,
89 88, 89, 90, 91, 92, 93, 94, 95,
90 96, 97, 98, 99, 100, 101, 102, 103,
91 104, 105, 106, 107, 108, 109, 110, 111,
92 112, 113, 114, 115, 116, 117, 118, 119,
93 120, 121, 122, 123, 124, 125, 126, 127},
94 .oobfree = {
95 {.offset = 2,
96 .length = 78}}
97};
98
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020099static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200100 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200102static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
103 struct mtd_oob_ops *ops);
104
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200105/*
Joe Perches8e87d782008-02-03 17:22:34 +0200106 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200107 * compiled away when LED support is disabled.
108 */
109DEFINE_LED_TRIGGER(nand_led_trigger);
110
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530111static int check_offs_len(struct mtd_info *mtd,
112 loff_t ofs, uint64_t len)
113{
114 struct nand_chip *chip = mtd->priv;
115 int ret = 0;
116
117 /* Start address must align on block boundary */
118 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
119 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
120 ret = -EINVAL;
121 }
122
123 /* Length must align on block boundary */
124 if (len & ((1 << chip->phys_erase_shift) - 1)) {
125 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
126 __func__);
127 ret = -EINVAL;
128 }
129
130 /* Do not allow past end of device */
131 if (ofs + len > mtd->size) {
132 DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
133 __func__);
134 ret = -EINVAL;
135 }
136
137 return ret;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000143 *
144 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100146static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200148 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200151 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100152
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200153 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200154 spin_lock(&chip->controller->lock);
155 chip->controller->active = NULL;
156 chip->state = FL_READY;
157 wake_up(&chip->controller->wq);
158 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/**
162 * nand_read_byte - [DEFAULT] read one byte from the chip
163 * @mtd: MTD device structure
164 *
165 * Default read function for 8bit buswith
166 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200167static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200169 struct nand_chip *chip = mtd->priv;
170 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
175 * @mtd: MTD device structure
176 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000177 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * endianess conversion
179 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200180static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200182 struct nand_chip *chip = mtd->priv;
183 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * nand_read_word - [DEFAULT] read one word from the chip
188 * @mtd: MTD device structure
189 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000190 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * endianess conversion
192 */
193static u16 nand_read_word(struct mtd_info *mtd)
194{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200195 struct nand_chip *chip = mtd->priv;
196 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * nand_select_chip - [DEFAULT] control CE line
201 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700202 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
204 * Default select function for 1 chip devices.
205 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200206static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200208 struct nand_chip *chip = mtd->priv;
209
210 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200212 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216
217 default:
218 BUG();
219 }
220}
221
222/**
223 * nand_write_buf - [DEFAULT] write buffer to chip
224 * @mtd: MTD device structure
225 * @buf: data buffer
226 * @len: number of bytes to write
227 *
228 * Default write function for 8bit buswith
229 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200230static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200233 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
David Woodhousee0c7d762006-05-13 18:07:53 +0100235 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200236 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * @mtd: MTD device structure
242 * @buf: buffer to store date
243 * @len: number of bytes to read
244 *
245 * Default read function for 8bit buswith
246 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200247static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200250 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Woodhousee0c7d762006-05-13 18:07:53 +0100252 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200253 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000257 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * @mtd: MTD device structure
259 * @buf: buffer containing the data to compare
260 * @len: number of bytes to compare
261 *
262 * Default verify function for 8bit buswith
263 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200264static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200267 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
David Woodhousee0c7d762006-05-13 18:07:53 +0100269 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200270 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273}
274
275/**
276 * nand_write_buf16 - [DEFAULT] write buffer to chip
277 * @mtd: MTD device structure
278 * @buf: data buffer
279 * @len: number of bytes to write
280 *
281 * Default write function for 16bit buswith
282 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200283static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 u16 *p = (u16 *) buf;
288 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000289
David Woodhousee0c7d762006-05-13 18:07:53 +0100290 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200291 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000296 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * @mtd: MTD device structure
298 * @buf: buffer to store date
299 * @len: number of bytes to read
300 *
301 * Default read function for 16bit buswith
302 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200303static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u16 *p = (u16 *) buf;
308 len >>= 1;
309
David Woodhousee0c7d762006-05-13 18:07:53 +0100310 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200311 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000315 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * @mtd: MTD device structure
317 * @buf: buffer containing the data to compare
318 * @len: number of bytes to compare
319 *
320 * Default verify function for 16bit buswith
321 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200322static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200325 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 *p = (u16 *) buf;
327 len >>= 1;
328
David Woodhousee0c7d762006-05-13 18:07:53 +0100329 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200330 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EFAULT;
332
333 return 0;
334}
335
336/**
337 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
338 * @mtd: MTD device structure
339 * @ofs: offset from device start
340 * @getchip: 0, if the chip is already selected
341 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000342 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
345{
346 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200347 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 u16 bad;
349
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100350 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200353 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200355 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200358 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200361 if (chip->options & NAND_BUSWIDTH_16) {
362 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100363 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200364 bad = cpu_to_le16(chip->read_word(mtd));
365 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000366 bad >>= 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if ((bad & 0xFF) != 0xff)
368 res = 1;
369 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200371 if (chip->read_byte(mtd) != 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 res = 1;
373 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000374
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200375 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return res;
379}
380
381/**
382 * nand_default_block_markbad - [DEFAULT] mark a block bad
383 * @mtd: MTD device structure
384 * @ofs: offset from device start
385 *
386 * This is the default implementation, which can be overridden by
387 * a hardware specific driver.
388*/
389static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
390{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200391 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200392 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200393 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400396 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200397 if (chip->bbt)
398 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200401 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200402 ret = nand_update_bbt(mtd, ofs);
403 else {
404 /* We write two bytes, so we dont have to mess with 16 bit
405 * access
406 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300407 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200408 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200409 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200410 chip->ops.datbuf = NULL;
411 chip->ops.oobbuf = buf;
412 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000413
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200414 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300415 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200416 }
417 if (!ret)
418 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300419
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000423/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * nand_check_wp - [GENERIC] check if the chip is write protected
425 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000426 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000428 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100430static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200432 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200434 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
435 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438/**
439 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
440 * @mtd: MTD device structure
441 * @ofs: offset from device start
442 * @getchip: 0, if the chip is already selected
443 * @allowbbt: 1, if its allowed to access the bbt area
444 *
445 * Check, if the block is bad. Either by reading the bad block table or
446 * calling of the scan function.
447 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200448static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
449 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200451 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000452
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200453 if (!chip->bbt)
454 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100457 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200460/**
461 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
462 * @mtd: MTD device structure
463 * @timeo: Timeout
464 *
465 * Helper function for nand_wait_ready used when needing to wait in interrupt
466 * context.
467 */
468static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
469{
470 struct nand_chip *chip = mtd->priv;
471 int i;
472
473 /* Wait for the device to get ready */
474 for (i = 0; i < timeo; i++) {
475 if (chip->dev_ready(mtd))
476 break;
477 touch_softlockup_watchdog();
478 mdelay(1);
479 }
480}
481
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000482/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000483 * Wait for the ready pin, after a command
484 * The timeout is catched later.
485 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100486void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000487{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100489 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000490
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200491 /* 400ms timeout */
492 if (in_interrupt() || oops_in_progress)
493 return panic_nand_wait_ready(mtd, 400);
494
Richard Purdie8fe833c2006-03-31 02:31:14 -0800495 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000496 /* wait until command is processed or timeout occures */
497 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200498 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800499 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700500 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000501 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800502 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000503}
David Woodhouse4b648b02006-09-25 17:05:24 +0100504EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/**
507 * nand_command - [DEFAULT] Send command to NAND device
508 * @mtd: MTD device structure
509 * @command: the command to be sent
510 * @column: the column address for this command, -1 if none
511 * @page_addr: the page address for this command, -1 if none
512 *
513 * Send command to NAND device. This function is used for small page
514 * devices (256/512 Bytes per page)
515 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200516static void nand_command(struct mtd_info *mtd, unsigned int command,
517 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200519 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200520 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * Write out the command to the device.
524 */
525 if (command == NAND_CMD_SEQIN) {
526 int readcmd;
527
Joern Engel28318772006-05-22 23:18:05 +0200528 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200530 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 readcmd = NAND_CMD_READOOB;
532 } else if (column < 256) {
533 /* First 256 bytes --> READ0 */
534 readcmd = NAND_CMD_READ0;
535 } else {
536 column -= 256;
537 readcmd = NAND_CMD_READ1;
538 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200539 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200540 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200542 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200544 /*
545 * Address cycle, when necessary
546 */
547 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
548 /* Serially input address */
549 if (column != -1) {
550 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200551 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200552 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200553 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200554 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200556 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200557 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200558 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200559 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200561 if (chip->chipsize > (32 << 20))
562 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200563 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200564 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000565
566 /*
567 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 case NAND_CMD_PAGEPROG:
573 case NAND_CMD_ERASE1:
574 case NAND_CMD_ERASE2:
575 case NAND_CMD_SEQIN:
576 case NAND_CMD_STATUS:
577 return;
578
579 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200580 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 udelay(chip->chip_delay);
583 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200585 chip->cmd_ctrl(mtd,
586 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200587 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return;
589
David Woodhousee0c7d762006-05-13 18:07:53 +0100590 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000592 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * If we don't have access to the busy pin, we apply the given
594 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100595 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 if (!chip->dev_ready) {
597 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* Apply this short delay always to ensure that we do wait tWB in
602 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100603 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000604
605 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/**
609 * nand_command_lp - [DEFAULT] Send command to NAND large page device
610 * @mtd: MTD device structure
611 * @command: the command to be sent
612 * @column: the column address for this command, -1 if none
613 * @page_addr: the page address for this command, -1 if none
614 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200615 * Send command to NAND device. This is the version for the new large page
616 * devices We dont have the separate regions as we have in the small page
617 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
620 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200622 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Emulate NAND_CMD_READOOB */
625 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200626 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 command = NAND_CMD_READ0;
628 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000629
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200630 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200631 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200632 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* Serially input address */
638 if (column != -1) {
639 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200640 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200643 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 chip->cmd_ctrl(mtd, page_addr, ctrl);
648 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200649 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200651 if (chip->chipsize > (128 << 20))
652 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200656 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657
658 /*
659 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000660 * status, sequential in, and deplete1 need no delay
661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 case NAND_CMD_CACHEDPROG:
665 case NAND_CMD_PAGEPROG:
666 case NAND_CMD_ERASE1:
667 case NAND_CMD_ERASE2:
668 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200669 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000671 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return;
673
David Woodhousee0c7d762006-05-13 18:07:53 +0100674 /*
675 * read error status commands require only a short delay
676 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000677 case NAND_CMD_STATUS_ERROR:
678 case NAND_CMD_STATUS_ERROR0:
679 case NAND_CMD_STATUS_ERROR1:
680 case NAND_CMD_STATUS_ERROR2:
681 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200682 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000683 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200686 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200688 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200689 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
690 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
691 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
692 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200693 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return;
695
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200696 case NAND_CMD_RNDOUT:
697 /* No ready / busy check necessary */
698 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
699 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
700 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
701 NAND_NCE | NAND_CTRL_CHANGE);
702 return;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200705 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
706 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
707 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
708 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
David Woodhousee0c7d762006-05-13 18:07:53 +0100710 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000712 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * If we don't have access to the busy pin, we apply the given
714 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100715 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200716 if (!chip->dev_ready) {
717 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Apply this short delay always to ensure that we do wait tWB in
723 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100724 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000725
726 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200730 * panic_nand_get_device - [GENERIC] Get chip for selected access
731 * @chip: the nand chip descriptor
732 * @mtd: MTD device structure
733 * @new_state: the state which is requested
734 *
735 * Used when in panic, no locks are taken.
736 */
737static void panic_nand_get_device(struct nand_chip *chip,
738 struct mtd_info *mtd, int new_state)
739{
740 /* Hardware controller shared among independend devices */
741 chip->controller->active = chip;
742 chip->state = new_state;
743}
744
745/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700747 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000749 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 *
751 * Get the device and lock it for exclusive access
752 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200753static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200756 spinlock_t *lock = &chip->controller->lock;
757 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100758 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100759 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100760 spin_lock(lock);
761
vimal singhb8b3ee92009-07-09 20:41:22 +0530762 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200763 if (!chip->controller->active)
764 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200765
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200766 if (chip->controller->active == chip && chip->state == FL_READY) {
767 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100768 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100769 return 0;
770 }
771 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800772 if (chip->controller->active->state == FL_PM_SUSPENDED) {
773 chip->state = FL_PM_SUSPENDED;
774 spin_unlock(lock);
775 return 0;
776 } else {
777 spin_unlock(lock);
778 return -EAGAIN;
779 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100780 }
781 set_current_state(TASK_UNINTERRUPTIBLE);
782 add_wait_queue(wq, &wait);
783 spin_unlock(lock);
784 schedule();
785 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto retry;
787}
788
789/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200790 * panic_nand_wait - [GENERIC] wait until the command is done
791 * @mtd: MTD device structure
792 * @chip: NAND chip structure
793 * @timeo: Timeout
794 *
795 * Wait for command done. This is a helper function for nand_wait used when
796 * we are in interrupt context. May happen when in panic and trying to write
797 * an oops trough mtdoops.
798 */
799static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
800 unsigned long timeo)
801{
802 int i;
803 for (i = 0; i < timeo; i++) {
804 if (chip->dev_ready) {
805 if (chip->dev_ready(mtd))
806 break;
807 } else {
808 if (chip->read_byte(mtd) & NAND_STATUS_READY)
809 break;
810 }
811 mdelay(1);
812 }
813}
814
815/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * nand_wait - [DEFAULT] wait until the command is done
817 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700818 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000821 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700823 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200824static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826
David Woodhousee0c7d762006-05-13 18:07:53 +0100827 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200828 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100833 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Richard Purdie8fe833c2006-03-31 02:31:14 -0800835 led_trigger_event(nand_led_trigger, LED_FULL);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* Apply this short delay always to ensure that we do wait tWB in
838 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100839 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200841 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
842 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000843 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200844 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200846 if (in_interrupt() || oops_in_progress)
847 panic_nand_wait(mtd, chip, timeo);
848 else {
849 while (time_before(jiffies, timeo)) {
850 if (chip->dev_ready) {
851 if (chip->dev_ready(mtd))
852 break;
853 } else {
854 if (chip->read_byte(mtd) & NAND_STATUS_READY)
855 break;
856 }
857 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800860 led_trigger_event(nand_led_trigger, LED_OFF);
861
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200862 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return status;
864}
865
866/**
Vimal Singh7d70f332010-02-08 15:50:49 +0530867 * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
868 *
869 * @param mtd - mtd info
870 * @param ofs - offset to start unlock from
871 * @param len - length to unlock
872 * @invert - when = 0, unlock the range of blocks within the lower and
873 * upper boundary address
874 * whne = 1, unlock the range of blocks outside the boundaries
875 * of the lower and upper boundary address
876 *
877 * @return - unlock status
878 */
879static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
880 uint64_t len, int invert)
881{
882 int ret = 0;
883 int status, page;
884 struct nand_chip *chip = mtd->priv;
885
886 /* Submit address of first page to unlock */
887 page = ofs >> chip->page_shift;
888 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
889
890 /* Submit address of last page to unlock */
891 page = (ofs + len) >> chip->page_shift;
892 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
893 (page | invert) & chip->pagemask);
894
895 /* Call wait ready function */
896 status = chip->waitfunc(mtd, chip);
897 udelay(1000);
898 /* See if device thinks it succeeded */
899 if (status & 0x01) {
900 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
901 __func__, status);
902 ret = -EIO;
903 }
904
905 return ret;
906}
907
908/**
909 * nand_unlock - [REPLACABLE] unlocks specified locked blockes
910 *
911 * @param mtd - mtd info
912 * @param ofs - offset to start unlock from
913 * @param len - length to unlock
914 *
915 * @return - unlock status
916 */
917int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
918{
919 int ret = 0;
920 int chipnr;
921 struct nand_chip *chip = mtd->priv;
922
923 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
924 __func__, (unsigned long long)ofs, len);
925
926 if (check_offs_len(mtd, ofs, len))
927 ret = -EINVAL;
928
929 /* Align to last block address if size addresses end of the device */
930 if (ofs + len == mtd->size)
931 len -= mtd->erasesize;
932
933 nand_get_device(chip, mtd, FL_UNLOCKING);
934
935 /* Shift to get chip number */
936 chipnr = ofs >> chip->chip_shift;
937
938 chip->select_chip(mtd, chipnr);
939
940 /* Check, if it is write protected */
941 if (nand_check_wp(mtd)) {
942 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
943 __func__);
944 ret = -EIO;
945 goto out;
946 }
947
948 ret = __nand_unlock(mtd, ofs, len, 0);
949
950out:
951 /* de-select the NAND device */
952 chip->select_chip(mtd, -1);
953
954 nand_release_device(mtd);
955
956 return ret;
957}
958
959/**
960 * nand_lock - [REPLACABLE] locks all blockes present in the device
961 *
962 * @param mtd - mtd info
963 * @param ofs - offset to start unlock from
964 * @param len - length to unlock
965 *
966 * @return - lock status
967 *
968 * This feature is not support in many NAND parts. 'Micron' NAND parts
969 * do have this feature, but it allows only to lock all blocks not for
970 * specified range for block.
971 *
972 * Implementing 'lock' feature by making use of 'unlock', for now.
973 */
974int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
975{
976 int ret = 0;
977 int chipnr, status, page;
978 struct nand_chip *chip = mtd->priv;
979
980 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
981 __func__, (unsigned long long)ofs, len);
982
983 if (check_offs_len(mtd, ofs, len))
984 ret = -EINVAL;
985
986 nand_get_device(chip, mtd, FL_LOCKING);
987
988 /* Shift to get chip number */
989 chipnr = ofs >> chip->chip_shift;
990
991 chip->select_chip(mtd, chipnr);
992
993 /* Check, if it is write protected */
994 if (nand_check_wp(mtd)) {
995 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
996 __func__);
997 status = MTD_ERASE_FAILED;
998 ret = -EIO;
999 goto out;
1000 }
1001
1002 /* Submit address of first page to lock */
1003 page = ofs >> chip->page_shift;
1004 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1005
1006 /* Call wait ready function */
1007 status = chip->waitfunc(mtd, chip);
1008 udelay(1000);
1009 /* See if device thinks it succeeded */
1010 if (status & 0x01) {
1011 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
1012 __func__, status);
1013 ret = -EIO;
1014 goto out;
1015 }
1016
1017 ret = __nand_unlock(mtd, ofs, len, 0x1);
1018
1019out:
1020 /* de-select the NAND device */
1021 chip->select_chip(mtd, -1);
1022
1023 nand_release_device(mtd);
1024
1025 return ret;
1026}
1027
1028/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001029 * nand_read_page_raw - [Intern] read raw page data without ecc
1030 * @mtd: mtd info structure
1031 * @chip: nand chip info structure
1032 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001033 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001034 *
1035 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001036 */
1037static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001038 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001039{
1040 chip->read_buf(mtd, buf, mtd->writesize);
1041 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1042 return 0;
1043}
1044
1045/**
David Brownell52ff49d2009-03-04 12:01:36 -08001046 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
1047 * @mtd: mtd info structure
1048 * @chip: nand chip info structure
1049 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001050 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001051 *
1052 * We need a special oob layout and handling even when OOB isn't used.
1053 */
1054static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001055 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001056{
1057 int eccsize = chip->ecc.size;
1058 int eccbytes = chip->ecc.bytes;
1059 uint8_t *oob = chip->oob_poi;
1060 int steps, size;
1061
1062 for (steps = chip->ecc.steps; steps > 0; steps--) {
1063 chip->read_buf(mtd, buf, eccsize);
1064 buf += eccsize;
1065
1066 if (chip->ecc.prepad) {
1067 chip->read_buf(mtd, oob, chip->ecc.prepad);
1068 oob += chip->ecc.prepad;
1069 }
1070
1071 chip->read_buf(mtd, oob, eccbytes);
1072 oob += eccbytes;
1073
1074 if (chip->ecc.postpad) {
1075 chip->read_buf(mtd, oob, chip->ecc.postpad);
1076 oob += chip->ecc.postpad;
1077 }
1078 }
1079
1080 size = mtd->oobsize - (oob - chip->oob_poi);
1081 if (size)
1082 chip->read_buf(mtd, oob, size);
1083
1084 return 0;
1085}
1086
1087/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001088 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001089 * @mtd: mtd info structure
1090 * @chip: nand chip info structure
1091 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001092 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001093 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001094static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001095 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001097 int i, eccsize = chip->ecc.size;
1098 int eccbytes = chip->ecc.bytes;
1099 int eccsteps = chip->ecc.steps;
1100 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001101 uint8_t *ecc_calc = chip->buffers->ecccalc;
1102 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001103 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001104
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001105 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001106
1107 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1108 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1109
1110 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001111 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001112
1113 eccsteps = chip->ecc.steps;
1114 p = buf;
1115
1116 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1117 int stat;
1118
1119 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001120 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001121 mtd->ecc_stats.failed++;
1122 else
1123 mtd->ecc_stats.corrected += stat;
1124 }
1125 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/**
Alexey Korolev3d459552008-05-15 17:23:18 +01001129 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
1130 * @mtd: mtd info structure
1131 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +01001132 * @data_offs: offset of requested data within the page
1133 * @readlen: data length
1134 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001135 */
1136static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
1137{
1138 int start_step, end_step, num_steps;
1139 uint32_t *eccpos = chip->ecc.layout->eccpos;
1140 uint8_t *p;
1141 int data_col_addr, i, gaps = 0;
1142 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1143 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1144
1145 /* Column address wihin the page aligned to ECC size (256bytes). */
1146 start_step = data_offs / chip->ecc.size;
1147 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1148 num_steps = end_step - start_step + 1;
1149
1150 /* Data size aligned to ECC ecc.size*/
1151 datafrag_len = num_steps * chip->ecc.size;
1152 eccfrag_len = num_steps * chip->ecc.bytes;
1153
1154 data_col_addr = start_step * chip->ecc.size;
1155 /* If we read not a page aligned data */
1156 if (data_col_addr != 0)
1157 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1158
1159 p = bufpoi + data_col_addr;
1160 chip->read_buf(mtd, p, datafrag_len);
1161
1162 /* Calculate ECC */
1163 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1164 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1165
1166 /* The performance is faster if to position offsets
1167 according to ecc.pos. Let make sure here that
1168 there are no gaps in ecc positions */
1169 for (i = 0; i < eccfrag_len - 1; i++) {
1170 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1171 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1172 gaps = 1;
1173 break;
1174 }
1175 }
1176 if (gaps) {
1177 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1178 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1179 } else {
1180 /* send the command to read the particular ecc bytes */
1181 /* take care about buswidth alignment in read_buf */
1182 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1183 aligned_len = eccfrag_len;
1184 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1185 aligned_len++;
1186 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1187 aligned_len++;
1188
1189 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1190 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1191 }
1192
1193 for (i = 0; i < eccfrag_len; i++)
1194 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1195
1196 p = bufpoi + data_col_addr;
1197 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1198 int stat;
1199
1200 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1201 if (stat == -1)
1202 mtd->ecc_stats.failed++;
1203 else
1204 mtd->ecc_stats.corrected += stat;
1205 }
1206 return 0;
1207}
1208
1209/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001210 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001211 * @mtd: mtd info structure
1212 * @chip: nand chip info structure
1213 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001214 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001215 *
1216 * Not for syndrome calculating ecc controllers which need a special oob layout
1217 */
1218static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001219 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001220{
1221 int i, eccsize = chip->ecc.size;
1222 int eccbytes = chip->ecc.bytes;
1223 int eccsteps = chip->ecc.steps;
1224 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001225 uint8_t *ecc_calc = chip->buffers->ecccalc;
1226 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001227 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001228
1229 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1230 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1231 chip->read_buf(mtd, p, eccsize);
1232 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1233 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001234 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001235
1236 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001237 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001238
1239 eccsteps = chip->ecc.steps;
1240 p = buf;
1241
1242 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1243 int stat;
1244
1245 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001246 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001247 mtd->ecc_stats.failed++;
1248 else
1249 mtd->ecc_stats.corrected += stat;
1250 }
1251 return 0;
1252}
1253
1254/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001255 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1256 * @mtd: mtd info structure
1257 * @chip: nand chip info structure
1258 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001259 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001260 *
1261 * Hardware ECC for large page chips, require OOB to be read first.
1262 * For this ECC mode, the write_page method is re-used from ECC_HW.
1263 * These methods read/write ECC from the OOB area, unlike the
1264 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1265 * "infix ECC" scheme and reads/writes ECC from the data area, by
1266 * overwriting the NAND manufacturer bad block markings.
1267 */
1268static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1269 struct nand_chip *chip, uint8_t *buf, int page)
1270{
1271 int i, eccsize = chip->ecc.size;
1272 int eccbytes = chip->ecc.bytes;
1273 int eccsteps = chip->ecc.steps;
1274 uint8_t *p = buf;
1275 uint8_t *ecc_code = chip->buffers->ecccode;
1276 uint32_t *eccpos = chip->ecc.layout->eccpos;
1277 uint8_t *ecc_calc = chip->buffers->ecccalc;
1278
1279 /* Read the OOB area first */
1280 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1281 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1282 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1283
1284 for (i = 0; i < chip->ecc.total; i++)
1285 ecc_code[i] = chip->oob_poi[eccpos[i]];
1286
1287 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1288 int stat;
1289
1290 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1291 chip->read_buf(mtd, p, eccsize);
1292 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1293
1294 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1295 if (stat < 0)
1296 mtd->ecc_stats.failed++;
1297 else
1298 mtd->ecc_stats.corrected += stat;
1299 }
1300 return 0;
1301}
1302
1303/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001304 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001305 * @mtd: mtd info structure
1306 * @chip: nand chip info structure
1307 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001308 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001309 *
1310 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001311 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001312 */
1313static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001314 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001315{
1316 int i, eccsize = chip->ecc.size;
1317 int eccbytes = chip->ecc.bytes;
1318 int eccsteps = chip->ecc.steps;
1319 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001320 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001321
1322 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1323 int stat;
1324
1325 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1326 chip->read_buf(mtd, p, eccsize);
1327
1328 if (chip->ecc.prepad) {
1329 chip->read_buf(mtd, oob, chip->ecc.prepad);
1330 oob += chip->ecc.prepad;
1331 }
1332
1333 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1334 chip->read_buf(mtd, oob, eccbytes);
1335 stat = chip->ecc.correct(mtd, p, oob, NULL);
1336
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001337 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001338 mtd->ecc_stats.failed++;
1339 else
1340 mtd->ecc_stats.corrected += stat;
1341
1342 oob += eccbytes;
1343
1344 if (chip->ecc.postpad) {
1345 chip->read_buf(mtd, oob, chip->ecc.postpad);
1346 oob += chip->ecc.postpad;
1347 }
1348 }
1349
1350 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001351 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001352 if (i)
1353 chip->read_buf(mtd, oob, i);
1354
1355 return 0;
1356}
1357
1358/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001359 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1360 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001361 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001362 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001363 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001364 */
1365static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001366 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001367{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001368 switch(ops->mode) {
1369
1370 case MTD_OOB_PLACE:
1371 case MTD_OOB_RAW:
1372 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1373 return oob + len;
1374
1375 case MTD_OOB_AUTO: {
1376 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001377 uint32_t boffs = 0, roffs = ops->ooboffs;
1378 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001379
1380 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001381 /* Read request not from offset 0 ? */
1382 if (unlikely(roffs)) {
1383 if (roffs >= free->length) {
1384 roffs -= free->length;
1385 continue;
1386 }
1387 boffs = free->offset + roffs;
1388 bytes = min_t(size_t, len,
1389 (free->length - roffs));
1390 roffs = 0;
1391 } else {
1392 bytes = min_t(size_t, len, free->length);
1393 boffs = free->offset;
1394 }
1395 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001396 oob += bytes;
1397 }
1398 return oob;
1399 }
1400 default:
1401 BUG();
1402 }
1403 return NULL;
1404}
1405
1406/**
1407 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001408 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001409 * @mtd: MTD device structure
1410 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001411 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001412 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001413 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001414 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001415static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1416 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001417{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001418 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001419 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001420 struct mtd_ecc_stats stats;
1421 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1422 int sndcmd = 1;
1423 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001424 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001425 uint32_t oobreadlen = ops->ooblen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001426 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001428 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001430 chipnr = (int)(from >> chip->chip_shift);
1431 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001433 realpage = (int)(from >> chip->page_shift);
1434 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001436 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001438 buf = ops->datbuf;
1439 oob = ops->oobbuf;
1440
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001441 while(1) {
1442 bytes = min(mtd->writesize - col, readlen);
1443 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001444
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001445 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001446 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001447 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001449 if (likely(sndcmd)) {
1450 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1451 sndcmd = 0;
1452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001454 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001455 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001456 ret = chip->ecc.read_page_raw(mtd, chip,
1457 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001458 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1459 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001460 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001461 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1462 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001463 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001464 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001465
1466 /* Transfer not aligned data */
1467 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001468 if (!NAND_SUBPAGE_READ(chip) && !oob)
1469 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001470 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001472
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001473 buf += bytes;
1474
1475 if (unlikely(oob)) {
1476 /* Raw mode does data:oob:data:oob */
Vitaly Wool70145682006-11-03 18:20:38 +03001477 if (ops->mode != MTD_OOB_RAW) {
1478 int toread = min(oobreadlen,
1479 chip->ecc.layout->oobavail);
1480 if (toread) {
1481 oob = nand_transfer_oob(chip,
1482 oob, ops, toread);
1483 oobreadlen -= toread;
1484 }
1485 } else
1486 buf = nand_transfer_oob(chip,
1487 buf, ops, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001488 }
1489
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001490 if (!(chip->options & NAND_NO_READRDY)) {
1491 /*
1492 * Apply delay or wait for ready/busy pin. Do
1493 * this before the AUTOINCR check, so no
1494 * problems arise if a chip which does auto
1495 * increment is marked as NOAUTOINCR by the
1496 * board driver.
1497 */
1498 if (!chip->dev_ready)
1499 udelay(chip->chip_delay);
1500 else
1501 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001503 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001504 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001505 buf += bytes;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001508 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001509
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001510 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* For subsequent reads align to page boundary. */
1514 col = 0;
1515 /* Increment page address */
1516 realpage++;
1517
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001518 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /* Check, if we cross a chip boundary */
1520 if (!page) {
1521 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001522 chip->select_chip(mtd, -1);
1523 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001525
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001526 /* Check, if the chip supports auto page increment
1527 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001528 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001529 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001530 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001533 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001534 if (oob)
1535 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001537 if (ret)
1538 return ret;
1539
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001540 if (mtd->ecc_stats.failed - stats.failed)
1541 return -EBADMSG;
1542
1543 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001544}
1545
1546/**
1547 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1548 * @mtd: MTD device structure
1549 * @from: offset to read from
1550 * @len: number of bytes to read
1551 * @retlen: pointer to variable to store the number of read bytes
1552 * @buf: the databuffer to put data
1553 *
1554 * Get hold of the chip and call nand_do_read
1555 */
1556static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1557 size_t *retlen, uint8_t *buf)
1558{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001559 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001560 int ret;
1561
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001562 /* Do not allow reads past end of device */
1563 if ((from + len) > mtd->size)
1564 return -EINVAL;
1565 if (!len)
1566 return 0;
1567
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001568 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001569
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001570 chip->ops.len = len;
1571 chip->ops.datbuf = buf;
1572 chip->ops.oobbuf = NULL;
1573
1574 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001575
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001576 *retlen = chip->ops.retlen;
1577
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001578 nand_release_device(mtd);
1579
1580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001584 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1585 * @mtd: mtd info structure
1586 * @chip: nand chip info structure
1587 * @page: page number to read
1588 * @sndcmd: flag whether to issue read command or not
1589 */
1590static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1591 int page, int sndcmd)
1592{
1593 if (sndcmd) {
1594 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1595 sndcmd = 0;
1596 }
1597 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1598 return sndcmd;
1599}
1600
1601/**
1602 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1603 * with syndromes
1604 * @mtd: mtd info structure
1605 * @chip: nand chip info structure
1606 * @page: page number to read
1607 * @sndcmd: flag whether to issue read command or not
1608 */
1609static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1610 int page, int sndcmd)
1611{
1612 uint8_t *buf = chip->oob_poi;
1613 int length = mtd->oobsize;
1614 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1615 int eccsize = chip->ecc.size;
1616 uint8_t *bufpoi = buf;
1617 int i, toread, sndrnd = 0, pos;
1618
1619 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1620 for (i = 0; i < chip->ecc.steps; i++) {
1621 if (sndrnd) {
1622 pos = eccsize + i * (eccsize + chunk);
1623 if (mtd->writesize > 512)
1624 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1625 else
1626 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1627 } else
1628 sndrnd = 1;
1629 toread = min_t(int, length, chunk);
1630 chip->read_buf(mtd, bufpoi, toread);
1631 bufpoi += toread;
1632 length -= toread;
1633 }
1634 if (length > 0)
1635 chip->read_buf(mtd, bufpoi, length);
1636
1637 return 1;
1638}
1639
1640/**
1641 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1642 * @mtd: mtd info structure
1643 * @chip: nand chip info structure
1644 * @page: page number to write
1645 */
1646static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1647 int page)
1648{
1649 int status = 0;
1650 const uint8_t *buf = chip->oob_poi;
1651 int length = mtd->oobsize;
1652
1653 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1654 chip->write_buf(mtd, buf, length);
1655 /* Send command to program the OOB data */
1656 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1657
1658 status = chip->waitfunc(mtd, chip);
1659
Savin Zlobec0d420f92006-06-21 11:51:20 +02001660 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001661}
1662
1663/**
1664 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1665 * with syndrome - only for large page flash !
1666 * @mtd: mtd info structure
1667 * @chip: nand chip info structure
1668 * @page: page number to write
1669 */
1670static int nand_write_oob_syndrome(struct mtd_info *mtd,
1671 struct nand_chip *chip, int page)
1672{
1673 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1674 int eccsize = chip->ecc.size, length = mtd->oobsize;
1675 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1676 const uint8_t *bufpoi = chip->oob_poi;
1677
1678 /*
1679 * data-ecc-data-ecc ... ecc-oob
1680 * or
1681 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1682 */
1683 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1684 pos = steps * (eccsize + chunk);
1685 steps = 0;
1686 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001687 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001688
1689 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1690 for (i = 0; i < steps; i++) {
1691 if (sndcmd) {
1692 if (mtd->writesize <= 512) {
1693 uint32_t fill = 0xFFFFFFFF;
1694
1695 len = eccsize;
1696 while (len > 0) {
1697 int num = min_t(int, len, 4);
1698 chip->write_buf(mtd, (uint8_t *)&fill,
1699 num);
1700 len -= num;
1701 }
1702 } else {
1703 pos = eccsize + i * (eccsize + chunk);
1704 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1705 }
1706 } else
1707 sndcmd = 1;
1708 len = min_t(int, length, chunk);
1709 chip->write_buf(mtd, bufpoi, len);
1710 bufpoi += len;
1711 length -= len;
1712 }
1713 if (length > 0)
1714 chip->write_buf(mtd, bufpoi, length);
1715
1716 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1717 status = chip->waitfunc(mtd, chip);
1718
1719 return status & NAND_STATUS_FAIL ? -EIO : 0;
1720}
1721
1722/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001723 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * @mtd: MTD device structure
1725 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001726 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 *
1728 * NAND read out-of-band data from the spare area
1729 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001730static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1731 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001733 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001734 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001735 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001736 int readlen = ops->ooblen;
1737 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001738 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
vimal singh20d8e242009-07-07 15:49:49 +05301740 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1741 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Adrian Hunter03736152007-01-31 17:58:29 +02001743 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001744 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001745 else
1746 len = mtd->oobsize;
1747
1748 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301749 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1750 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001751 return -EINVAL;
1752 }
1753
1754 /* Do not allow reads past end of device */
1755 if (unlikely(from >= mtd->size ||
1756 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1757 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301758 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1759 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001760 return -EINVAL;
1761 }
Vitaly Wool70145682006-11-03 18:20:38 +03001762
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001763 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001764 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001766 /* Shift to get page */
1767 realpage = (int)(from >> chip->page_shift);
1768 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001770 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001771 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001772
1773 len = min(len, readlen);
1774 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001775
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001776 if (!(chip->options & NAND_NO_READRDY)) {
1777 /*
1778 * Apply delay or wait for ready/busy pin. Do this
1779 * before the AUTOINCR check, so no problems arise if a
1780 * chip which does auto increment is marked as
1781 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001782 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001783 if (!chip->dev_ready)
1784 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001785 else
1786 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001788
Vitaly Wool70145682006-11-03 18:20:38 +03001789 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001790 if (!readlen)
1791 break;
1792
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001793 /* Increment page address */
1794 realpage++;
1795
1796 page = realpage & chip->pagemask;
1797 /* Check, if we cross a chip boundary */
1798 if (!page) {
1799 chipnr++;
1800 chip->select_chip(mtd, -1);
1801 chip->select_chip(mtd, chipnr);
1802 }
1803
1804 /* Check, if the chip supports auto page increment
1805 * or if we have hit a block boundary.
1806 */
1807 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1808 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810
Vitaly Wool70145682006-11-03 18:20:38 +03001811 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813}
1814
1815/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001816 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001819 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001821 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001823static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1824 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001826 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001827 int ret = -ENOTSUPP;
1828
1829 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001832 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301833 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1834 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return -EINVAL;
1836 }
1837
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001838 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001840 switch(ops->mode) {
1841 case MTD_OOB_PLACE:
1842 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001843 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001844 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001845
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001846 default:
1847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001850 if (!ops->datbuf)
1851 ret = nand_do_read_oob(mtd, from, ops);
1852 else
1853 ret = nand_do_read_ops(mtd, from, ops);
1854
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001855 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001857 return ret;
1858}
1859
1860
1861/**
1862 * nand_write_page_raw - [Intern] raw page write function
1863 * @mtd: mtd info structure
1864 * @chip: nand chip info structure
1865 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001866 *
1867 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 */
1869static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1870 const uint8_t *buf)
1871{
1872 chip->write_buf(mtd, buf, mtd->writesize);
1873 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001876/**
David Brownell52ff49d2009-03-04 12:01:36 -08001877 * nand_write_page_raw_syndrome - [Intern] raw page write function
1878 * @mtd: mtd info structure
1879 * @chip: nand chip info structure
1880 * @buf: data buffer
1881 *
1882 * We need a special oob layout and handling even when ECC isn't checked.
1883 */
1884static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1885 const uint8_t *buf)
1886{
1887 int eccsize = chip->ecc.size;
1888 int eccbytes = chip->ecc.bytes;
1889 uint8_t *oob = chip->oob_poi;
1890 int steps, size;
1891
1892 for (steps = chip->ecc.steps; steps > 0; steps--) {
1893 chip->write_buf(mtd, buf, eccsize);
1894 buf += eccsize;
1895
1896 if (chip->ecc.prepad) {
1897 chip->write_buf(mtd, oob, chip->ecc.prepad);
1898 oob += chip->ecc.prepad;
1899 }
1900
1901 chip->read_buf(mtd, oob, eccbytes);
1902 oob += eccbytes;
1903
1904 if (chip->ecc.postpad) {
1905 chip->write_buf(mtd, oob, chip->ecc.postpad);
1906 oob += chip->ecc.postpad;
1907 }
1908 }
1909
1910 size = mtd->oobsize - (oob - chip->oob_poi);
1911 if (size)
1912 chip->write_buf(mtd, oob, size);
1913}
1914/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001915 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001916 * @mtd: mtd info structure
1917 * @chip: nand chip info structure
1918 * @buf: data buffer
1919 */
1920static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1921 const uint8_t *buf)
1922{
1923 int i, eccsize = chip->ecc.size;
1924 int eccbytes = chip->ecc.bytes;
1925 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001926 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001927 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001928 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001929
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930 /* Software ecc calculation */
1931 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1932 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001933
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001934 for (i = 0; i < chip->ecc.total; i++)
1935 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001936
Thomas Gleixner90424de2007-04-05 11:44:05 +02001937 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001938}
1939
1940/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001941 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001942 * @mtd: mtd info structure
1943 * @chip: nand chip info structure
1944 * @buf: data buffer
1945 */
1946static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1947 const uint8_t *buf)
1948{
1949 int i, eccsize = chip->ecc.size;
1950 int eccbytes = chip->ecc.bytes;
1951 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001952 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001953 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001954 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001955
1956 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1957 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001958 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001959 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1960 }
1961
1962 for (i = 0; i < chip->ecc.total; i++)
1963 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1964
1965 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1966}
1967
1968/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001969 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001970 * @mtd: mtd info structure
1971 * @chip: nand chip info structure
1972 * @buf: data buffer
1973 *
1974 * The hw generator calculates the error syndrome automatically. Therefor
1975 * we need a special oob layout and handling.
1976 */
1977static void nand_write_page_syndrome(struct mtd_info *mtd,
1978 struct nand_chip *chip, const uint8_t *buf)
1979{
1980 int i, eccsize = chip->ecc.size;
1981 int eccbytes = chip->ecc.bytes;
1982 int eccsteps = chip->ecc.steps;
1983 const uint8_t *p = buf;
1984 uint8_t *oob = chip->oob_poi;
1985
1986 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1987
1988 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1989 chip->write_buf(mtd, p, eccsize);
1990
1991 if (chip->ecc.prepad) {
1992 chip->write_buf(mtd, oob, chip->ecc.prepad);
1993 oob += chip->ecc.prepad;
1994 }
1995
1996 chip->ecc.calculate(mtd, p, oob);
1997 chip->write_buf(mtd, oob, eccbytes);
1998 oob += eccbytes;
1999
2000 if (chip->ecc.postpad) {
2001 chip->write_buf(mtd, oob, chip->ecc.postpad);
2002 oob += chip->ecc.postpad;
2003 }
2004 }
2005
2006 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002007 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002008 if (i)
2009 chip->write_buf(mtd, oob, i);
2010}
2011
2012/**
David Woodhouse956e9442006-09-25 17:12:39 +01002013 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002014 * @mtd: MTD device structure
2015 * @chip: NAND chip descriptor
2016 * @buf: the data to write
2017 * @page: page number to write
2018 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02002019 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002020 */
2021static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01002022 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002023{
2024 int status;
2025
2026 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2027
David Woodhouse956e9442006-09-25 17:12:39 +01002028 if (unlikely(raw))
2029 chip->ecc.write_page_raw(mtd, chip, buf);
2030 else
2031 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002032
2033 /*
2034 * Cached progamming disabled for now, Not sure if its worth the
2035 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
2036 */
2037 cached = 0;
2038
2039 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2040
2041 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002042 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002043 /*
2044 * See if operation failed and additional status checks are
2045 * available
2046 */
2047 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2048 status = chip->errstat(mtd, chip, FL_WRITING, status,
2049 page);
2050
2051 if (status & NAND_STATUS_FAIL)
2052 return -EIO;
2053 } else {
2054 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002055 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002056 }
2057
2058#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2059 /* Send command to read back the data */
2060 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2061
2062 if (chip->verify_buf(mtd, buf, mtd->writesize))
2063 return -EIO;
2064#endif
2065 return 0;
2066}
2067
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002068/**
2069 * nand_fill_oob - [Internal] Transfer client buffer to oob
2070 * @chip: nand chip structure
2071 * @oob: oob data buffer
2072 * @ops: oob ops structure
2073 */
2074static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
2075 struct mtd_oob_ops *ops)
2076{
2077 size_t len = ops->ooblen;
2078
2079 switch(ops->mode) {
2080
2081 case MTD_OOB_PLACE:
2082 case MTD_OOB_RAW:
2083 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2084 return oob + len;
2085
2086 case MTD_OOB_AUTO: {
2087 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002088 uint32_t boffs = 0, woffs = ops->ooboffs;
2089 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002090
2091 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002092 /* Write request not from offset 0 ? */
2093 if (unlikely(woffs)) {
2094 if (woffs >= free->length) {
2095 woffs -= free->length;
2096 continue;
2097 }
2098 boffs = free->offset + woffs;
2099 bytes = min_t(size_t, len,
2100 (free->length - woffs));
2101 woffs = 0;
2102 } else {
2103 bytes = min_t(size_t, len, free->length);
2104 boffs = free->offset;
2105 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002106 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002107 oob += bytes;
2108 }
2109 return oob;
2110 }
2111 default:
2112 BUG();
2113 }
2114 return NULL;
2115}
2116
Thomas Gleixner29072b92006-09-28 15:38:36 +02002117#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002118
2119/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002120 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002121 * @mtd: MTD device structure
2122 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002123 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002124 *
2125 * NAND write with ECC
2126 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002127static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2128 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002129{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002130 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002131 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002132 uint32_t writelen = ops->len;
2133 uint8_t *oob = ops->oobbuf;
2134 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002135 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002136
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002137 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002138 if (!writelen)
2139 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002140
2141 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002142 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302143 printk(KERN_NOTICE "%s: Attempt to write not "
2144 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002145 return -EINVAL;
2146 }
2147
Thomas Gleixner29072b92006-09-28 15:38:36 +02002148 column = to & (mtd->writesize - 1);
2149 subpage = column || (writelen & (mtd->writesize - 1));
2150
2151 if (subpage && oob)
2152 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002153
Thomas Gleixner6a930962006-06-28 00:11:45 +02002154 chipnr = (int)(to >> chip->chip_shift);
2155 chip->select_chip(mtd, chipnr);
2156
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002157 /* Check, if it is write protected */
2158 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002159 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002160
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002161 realpage = (int)(to >> chip->page_shift);
2162 page = realpage & chip->pagemask;
2163 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2164
2165 /* Invalidate the page cache, when we write to the cached page */
2166 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002167 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002168 chip->pagebuf = -1;
2169
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002170 /* If we're not given explicit OOB data, let it be 0xFF */
2171 if (likely(!oob))
2172 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002173
2174 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002175 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002176 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002177 uint8_t *wbuf = buf;
2178
2179 /* Partial page write ? */
2180 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2181 cached = 0;
2182 bytes = min_t(int, bytes - column, (int) writelen);
2183 chip->pagebuf = -1;
2184 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2185 memcpy(&chip->buffers->databuf[column], buf, bytes);
2186 wbuf = chip->buffers->databuf;
2187 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002188
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002189 if (unlikely(oob))
2190 oob = nand_fill_oob(chip, oob, ops);
2191
Thomas Gleixner29072b92006-09-28 15:38:36 +02002192 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01002193 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002194 if (ret)
2195 break;
2196
2197 writelen -= bytes;
2198 if (!writelen)
2199 break;
2200
Thomas Gleixner29072b92006-09-28 15:38:36 +02002201 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002202 buf += bytes;
2203 realpage++;
2204
2205 page = realpage & chip->pagemask;
2206 /* Check, if we cross a chip boundary */
2207 if (!page) {
2208 chipnr++;
2209 chip->select_chip(mtd, -1);
2210 chip->select_chip(mtd, chipnr);
2211 }
2212 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002213
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002214 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002215 if (unlikely(oob))
2216 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002217 return ret;
2218}
2219
2220/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002221 * panic_nand_write - [MTD Interface] NAND write with ECC
2222 * @mtd: MTD device structure
2223 * @to: offset to write to
2224 * @len: number of bytes to write
2225 * @retlen: pointer to variable to store the number of written bytes
2226 * @buf: the data to write
2227 *
2228 * NAND write with ECC. Used when performing writes in interrupt context, this
2229 * may for example be called by mtdoops when writing an oops while in panic.
2230 */
2231static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2232 size_t *retlen, const uint8_t *buf)
2233{
2234 struct nand_chip *chip = mtd->priv;
2235 int ret;
2236
2237 /* Do not allow reads past end of device */
2238 if ((to + len) > mtd->size)
2239 return -EINVAL;
2240 if (!len)
2241 return 0;
2242
2243 /* Wait for the device to get ready. */
2244 panic_nand_wait(mtd, chip, 400);
2245
2246 /* Grab the device. */
2247 panic_nand_get_device(chip, mtd, FL_WRITING);
2248
2249 chip->ops.len = len;
2250 chip->ops.datbuf = (uint8_t *)buf;
2251 chip->ops.oobbuf = NULL;
2252
2253 ret = nand_do_write_ops(mtd, to, &chip->ops);
2254
2255 *retlen = chip->ops.retlen;
2256 return ret;
2257}
2258
2259/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002260 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * @mtd: MTD device structure
2262 * @to: offset to write to
2263 * @len: number of bytes to write
2264 * @retlen: pointer to variable to store the number of written bytes
2265 * @buf: the data to write
2266 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002267 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002269static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002270 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002272 struct nand_chip *chip = mtd->priv;
2273 int ret;
2274
2275 /* Do not allow reads past end of device */
2276 if ((to + len) > mtd->size)
2277 return -EINVAL;
2278 if (!len)
2279 return 0;
2280
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002281 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002282
2283 chip->ops.len = len;
2284 chip->ops.datbuf = (uint8_t *)buf;
2285 chip->ops.oobbuf = NULL;
2286
2287 ret = nand_do_write_ops(mtd, to, &chip->ops);
2288
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002289 *retlen = chip->ops.retlen;
2290
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002291 nand_release_device(mtd);
2292
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002293 return ret;
2294}
2295
2296/**
2297 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2298 * @mtd: MTD device structure
2299 * @to: offset to write to
2300 * @ops: oob operation description structure
2301 *
2302 * NAND write out-of-band
2303 */
2304static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2305 struct mtd_oob_ops *ops)
2306{
Adrian Hunter03736152007-01-31 17:58:29 +02002307 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002308 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
vimal singh20d8e242009-07-07 15:49:49 +05302310 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2311 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Adrian Hunter03736152007-01-31 17:58:29 +02002313 if (ops->mode == MTD_OOB_AUTO)
2314 len = chip->ecc.layout->oobavail;
2315 else
2316 len = mtd->oobsize;
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002319 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302320 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2321 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return -EINVAL;
2323 }
2324
Adrian Hunter03736152007-01-31 17:58:29 +02002325 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302326 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2327 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002328 return -EINVAL;
2329 }
2330
2331 /* Do not allow reads past end of device */
2332 if (unlikely(to >= mtd->size ||
2333 ops->ooboffs + ops->ooblen >
2334 ((mtd->size >> chip->page_shift) -
2335 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302336 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2337 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002338 return -EINVAL;
2339 }
2340
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002341 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002342 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002344 /* Shift to get page */
2345 page = (int)(to >> chip->page_shift);
2346
2347 /*
2348 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2349 * of my DiskOnChip 2000 test units) will clear the whole data page too
2350 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2351 * it in the doc2000 driver in August 1999. dwmw2.
2352 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002353 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 /* Check, if it is write protected */
2356 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002357 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002360 if (page == chip->pagebuf)
2361 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002363 memset(chip->oob_poi, 0xff, mtd->oobsize);
2364 nand_fill_oob(chip, ops->oobbuf, ops);
2365 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2366 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002367
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002368 if (status)
2369 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Vitaly Wool70145682006-11-03 18:20:38 +03002371 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002373 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002376/**
2377 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2378 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002379 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002380 * @ops: oob operation description structure
2381 */
2382static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2383 struct mtd_oob_ops *ops)
2384{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002385 struct nand_chip *chip = mtd->priv;
2386 int ret = -ENOTSUPP;
2387
2388 ops->retlen = 0;
2389
2390 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002391 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302392 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2393 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002394 return -EINVAL;
2395 }
2396
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002397 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002398
2399 switch(ops->mode) {
2400 case MTD_OOB_PLACE:
2401 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002402 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002403 break;
2404
2405 default:
2406 goto out;
2407 }
2408
2409 if (!ops->datbuf)
2410 ret = nand_do_write_oob(mtd, to, ops);
2411 else
2412 ret = nand_do_write_ops(mtd, to, ops);
2413
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002414 out:
2415 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return ret;
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2421 * @mtd: MTD device structure
2422 * @page: the page address of the block which will be erased
2423 *
2424 * Standard erase command for NAND chips
2425 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002426static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002428 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002430 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2431 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
2434/**
2435 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2436 * @mtd: MTD device structure
2437 * @page: the page address of the block which will be erased
2438 *
2439 * AND multi block erase command function
2440 * Erase 4 consecutive blocks
2441 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002442static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002444 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002446 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2447 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2448 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2449 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2450 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
2452
2453/**
2454 * nand_erase - [MTD Interface] erase block(s)
2455 * @mtd: MTD device structure
2456 * @instr: erase instruction
2457 *
2458 * Erase one ore more blocks
2459 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002460static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
David Woodhousee0c7d762006-05-13 18:07:53 +01002462 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002464
David A. Marlin30f464b2005-01-17 18:35:25 +00002465#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002467 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * @mtd: MTD device structure
2469 * @instr: erase instruction
2470 * @allowbbt: allow erasing the bbt area
2471 *
2472 * Erase one ore more blocks
2473 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002474int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2475 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Adrian Hunter69423d92008-12-10 13:37:21 +00002477 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002478 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002479 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002480 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002481 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
vimal singh20d8e242009-07-07 15:49:49 +05302483 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2484 __func__, (unsigned long long)instr->addr,
2485 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302487 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002490 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002493 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002496 page = (int)(instr->addr >> chip->page_shift);
2497 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002500 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002503 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 /* Check, if it is write protected */
2506 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302507 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2508 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 instr->state = MTD_ERASE_FAILED;
2510 goto erase_exit;
2511 }
2512
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002513 /*
2514 * If BBT requires refresh, set the BBT page mask to see if the BBT
2515 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2516 * can not be matched. This is also done when the bbt is actually
2517 * erased to avoid recusrsive updates
2518 */
2519 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2520 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 /* Loop through the pages */
2523 len = instr->len;
2524
2525 instr->state = MTD_ERASING;
2526
2527 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 /*
2529 * heck if we have a bad block, we do not erase bad blocks !
2530 */
2531 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2532 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302533 printk(KERN_WARNING "%s: attempt to erase a bad block "
2534 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 instr->state = MTD_ERASE_FAILED;
2536 goto erase_exit;
2537 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002538
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002539 /*
2540 * Invalidate the page cache, if we erase the block which
2541 * contains the current cached page
2542 */
2543 if (page <= chip->pagebuf && chip->pagebuf <
2544 (page + pages_per_block))
2545 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002547 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002548
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002549 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002551 /*
2552 * See if operation failed and additional status checks are
2553 * available
2554 */
2555 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2556 status = chip->errstat(mtd, chip, FL_ERASING,
2557 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002560 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302561 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2562 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002564 instr->fail_addr =
2565 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 goto erase_exit;
2567 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002568
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002569 /*
2570 * If BBT requires refresh, set the BBT rewrite flag to the
2571 * page being erased
2572 */
2573 if (bbt_masked_page != 0xffffffff &&
2574 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002575 rewrite_bbt[chipnr] =
2576 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 page += pages_per_block;
2581
2582 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002583 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002585 chip->select_chip(mtd, -1);
2586 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002587
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002588 /*
2589 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2590 * page mask to see if this BBT should be rewritten
2591 */
2592 if (bbt_masked_page != 0xffffffff &&
2593 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2594 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2595 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597 }
2598 instr->state = MTD_ERASE_DONE;
2599
David Woodhousee0c7d762006-05-13 18:07:53 +01002600 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
2602 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 /* Deselect and wake up anyone waiting on the device */
2605 nand_release_device(mtd);
2606
David Woodhouse49defc02007-10-06 15:01:59 -04002607 /* Do call back function */
2608 if (!ret)
2609 mtd_erase_callback(instr);
2610
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002611 /*
2612 * If BBT requires refresh and erase was successful, rewrite any
2613 * selected bad block tables
2614 */
2615 if (bbt_masked_page == 0xffffffff || ret)
2616 return ret;
2617
2618 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2619 if (!rewrite_bbt[chipnr])
2620 continue;
2621 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302622 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2623 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2624 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002626 }
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 /* Return more or less happy */
2629 return ret;
2630}
2631
2632/**
2633 * nand_sync - [MTD Interface] sync
2634 * @mtd: MTD device structure
2635 *
2636 * Sync is actually a wait for chip ready function
2637 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002638static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002640 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
vimal singh20d8e242009-07-07 15:49:49 +05302642 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002645 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002647 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002651 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002653 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002658 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002660
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002661 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
2664/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002665 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 * @mtd: MTD device structure
2667 * @ofs: offset relative to mtd start
2668 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002669static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002671 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int ret;
2673
David Woodhousee0c7d762006-05-13 18:07:53 +01002674 if ((ret = nand_block_isbad(mtd, ofs))) {
2675 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (ret > 0)
2677 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002678 return ret;
2679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002681 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
2684/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002685 * nand_suspend - [MTD Interface] Suspend the NAND flash
2686 * @mtd: MTD device structure
2687 */
2688static int nand_suspend(struct mtd_info *mtd)
2689{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002690 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002691
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002692 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002693}
2694
2695/**
2696 * nand_resume - [MTD Interface] Resume the NAND flash
2697 * @mtd: MTD device structure
2698 */
2699static void nand_resume(struct mtd_info *mtd)
2700{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002701 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002702
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002703 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002704 nand_release_device(mtd);
2705 else
vimal singh20d8e242009-07-07 15:49:49 +05302706 printk(KERN_ERR "%s called for a chip which is not "
2707 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002708}
2709
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002710/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002711 * Set default functions
2712 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002713static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002716 if (!chip->chip_delay)
2717 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002720 if (chip->cmdfunc == NULL)
2721 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002724 if (chip->waitfunc == NULL)
2725 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002727 if (!chip->select_chip)
2728 chip->select_chip = nand_select_chip;
2729 if (!chip->read_byte)
2730 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2731 if (!chip->read_word)
2732 chip->read_word = nand_read_word;
2733 if (!chip->block_bad)
2734 chip->block_bad = nand_block_bad;
2735 if (!chip->block_markbad)
2736 chip->block_markbad = nand_default_block_markbad;
2737 if (!chip->write_buf)
2738 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2739 if (!chip->read_buf)
2740 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2741 if (!chip->verify_buf)
2742 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2743 if (!chip->scan_bbt)
2744 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002745
2746 if (!chip->controller) {
2747 chip->controller = &chip->hwcontrol;
2748 spin_lock_init(&chip->controller->lock);
2749 init_waitqueue_head(&chip->controller->wq);
2750 }
2751
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002752}
2753
2754/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002755 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002756 */
2757static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002758 struct nand_chip *chip,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002759 int busw, int *maf_id)
2760{
2761 struct nand_flash_dev *type = NULL;
2762 int i, dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002763 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002766 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Karl Beldanef89a882008-09-15 14:37:29 +02002768 /*
2769 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2770 * after power-up
2771 */
2772 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002775 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002778 *maf_id = chip->read_byte(mtd);
2779 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Ben Dooksed8165c2008-04-14 14:58:58 +01002781 /* Try again to make sure, as some systems the bus-hold or other
2782 * interface concerns can cause random data which looks like a
2783 * possibly credible NAND flash to appear. If the two results do
2784 * not match, ignore the device completely.
2785 */
2786
2787 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2788
2789 /* Read manufacturer and device IDs */
2790
2791 tmp_manf = chip->read_byte(mtd);
2792 tmp_id = chip->read_byte(mtd);
2793
2794 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2795 printk(KERN_INFO "%s: second ID read did not match "
2796 "%02x,%02x against %02x,%02x\n", __func__,
2797 *maf_id, dev_id, tmp_manf, tmp_id);
2798 return ERR_PTR(-ENODEV);
2799 }
2800
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002801 /* Lookup the flash id */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002803 if (dev_id == nand_flash_ids[i].id) {
2804 type = &nand_flash_ids[i];
2805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
2808
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002809 if (!type)
2810 return ERR_PTR(-ENODEV);
2811
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002812 if (!mtd->name)
2813 mtd->name = type->name;
2814
Adrian Hunter69423d92008-12-10 13:37:21 +00002815 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002816
2817 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002818 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002819 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002820 /* The 3rd id byte holds MLC / multichip data */
2821 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002822 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002823 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002824 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002825 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002826 extid >>= 2;
2827 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002828 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002829 extid >>= 2;
2830 /* Calc blocksize. Blocksize is multiples of 64KiB */
2831 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2832 extid >>= 2;
2833 /* Get buswidth information */
2834 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2835
2836 } else {
2837 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002838 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002839 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002840 mtd->erasesize = type->erasesize;
2841 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002842 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002843 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002844 }
2845
2846 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002847 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002848 if (nand_manuf_ids[maf_idx].id == *maf_id)
2849 break;
2850 }
2851
2852 /*
2853 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002854 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002855 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002856 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002857 printk(KERN_INFO "NAND device: Manufacturer ID:"
2858 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2859 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2860 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002861 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002862 busw ? 16 : 8);
2863 return ERR_PTR(-EINVAL);
2864 }
2865
2866 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002867 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002868 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002869 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002870
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002871 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002872 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002873 if (chip->chipsize & 0xffffffff)
2874 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2875 else
2876 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002877
2878 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002879 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002880 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2881
2882 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002883 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002884 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002885
2886 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002887 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002888 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002889 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002890
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002891 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002892 * options for chips which are not having an extended id.
2893 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002894 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002895 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002896
2897 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002898 if (chip->options & NAND_4PAGE_ARRAY)
2899 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002900 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002901 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002902
2903 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002904 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2905 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002906
2907 printk(KERN_INFO "NAND device: Manufacturer ID:"
2908 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2909 nand_manuf_ids[maf_idx].name, type->name);
2910
2911 return type;
2912}
2913
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002914/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002915 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2916 * @mtd: MTD device structure
2917 * @maxchips: Number of chips to scan for
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002918 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002919 * This is the first phase of the normal nand_scan() function. It
2920 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002921 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002922 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002923 */
David Woodhouse3b85c322006-09-25 17:06:53 +01002924int nand_scan_ident(struct mtd_info *mtd, int maxchips)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002925{
2926 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002927 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002928 struct nand_flash_dev *type;
2929
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002930 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002931 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002932 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002933 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002934
2935 /* Read the flash type */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002936 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002937
2938 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00002939 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
2940 printk(KERN_WARNING "No NAND device found.\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002941 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002942 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002945 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002946 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002947 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002948 /* See comment in nand_get_flash_type for reset */
2949 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002951 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 if (nand_maf_id != chip->read_byte(mtd) ||
2954 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 break;
2956 }
2957 if (i > 1)
2958 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002961 chip->numchips = i;
2962 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
David Woodhouse3b85c322006-09-25 17:06:53 +01002964 return 0;
2965}
2966
2967
2968/**
2969 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2970 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002971 *
2972 * This is the second phase of the normal nand_scan() function. It
2973 * fills out all the uninitialized function pointers with the defaults
2974 * and scans for a bad block table if appropriate.
2975 */
2976int nand_scan_tail(struct mtd_info *mtd)
2977{
2978 int i;
2979 struct nand_chip *chip = mtd->priv;
2980
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002981 if (!(chip->options & NAND_OWN_BUFFERS))
2982 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2983 if (!chip->buffers)
2984 return -ENOMEM;
2985
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002986 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002987 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002988
2989 /*
2990 * If no default placement scheme is given, select an appropriate one
2991 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002992 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002993 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002995 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 break;
2997 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02002998 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 break;
3000 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003001 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003003 case 128:
3004 chip->ecc.layout = &nand_oob_128;
3005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003007 printk(KERN_WARNING "No oob scheme defined for "
3008 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 BUG();
3010 }
3011 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003012
David Woodhouse956e9442006-09-25 17:12:39 +01003013 if (!chip->write_page)
3014 chip->write_page = nand_write_page;
3015
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003016 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003017 * check ECC mode, default to software if 3byte/512byte hardware ECC is
3018 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003019 */
David Woodhouse956e9442006-09-25 17:12:39 +01003020
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003021 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003022 case NAND_ECC_HW_OOB_FIRST:
3023 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3024 if (!chip->ecc.calculate || !chip->ecc.correct ||
3025 !chip->ecc.hwctl) {
3026 printk(KERN_WARNING "No ECC functions supplied; "
3027 "Hardware ECC not possible\n");
3028 BUG();
3029 }
3030 if (!chip->ecc.read_page)
3031 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3032
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003033 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003034 /* Use standard hwecc read page function ? */
3035 if (!chip->ecc.read_page)
3036 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003037 if (!chip->ecc.write_page)
3038 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003039 if (!chip->ecc.read_page_raw)
3040 chip->ecc.read_page_raw = nand_read_page_raw;
3041 if (!chip->ecc.write_page_raw)
3042 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003043 if (!chip->ecc.read_oob)
3044 chip->ecc.read_oob = nand_read_oob_std;
3045 if (!chip->ecc.write_oob)
3046 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003047
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003048 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003049 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3050 !chip->ecc.hwctl) &&
3051 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003052 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003053 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003054 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003055 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003056 "Hardware ECC not possible\n");
3057 BUG();
3058 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003059 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003060 if (!chip->ecc.read_page)
3061 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003062 if (!chip->ecc.write_page)
3063 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003064 if (!chip->ecc.read_page_raw)
3065 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3066 if (!chip->ecc.write_page_raw)
3067 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003068 if (!chip->ecc.read_oob)
3069 chip->ecc.read_oob = nand_read_oob_syndrome;
3070 if (!chip->ecc.write_oob)
3071 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003072
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003073 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003074 break;
3075 printk(KERN_WARNING "%d byte HW ECC not possible on "
3076 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003077 chip->ecc.size, mtd->writesize);
3078 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003080 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003081 chip->ecc.calculate = nand_calculate_ecc;
3082 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003083 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003084 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003085 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003086 chip->ecc.read_page_raw = nand_read_page_raw;
3087 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003088 chip->ecc.read_oob = nand_read_oob_std;
3089 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003090 if (!chip->ecc.size)
3091 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003092 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003094
3095 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003096 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
3097 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003098 chip->ecc.read_page = nand_read_page_raw;
3099 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003100 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003101 chip->ecc.read_page_raw = nand_read_page_raw;
3102 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003103 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003104 chip->ecc.size = mtd->writesize;
3105 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003109 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003110 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003111 BUG();
3112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003114 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003115 * The number of bytes available for a client to place data into
3116 * the out of band area
3117 */
3118 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003119 for (i = 0; chip->ecc.layout->oobfree[i].length
3120 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003121 chip->ecc.layout->oobavail +=
3122 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003123 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003124
3125 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003126 * Set the number of read / write steps for one page depending on ECC
3127 * mode
3128 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003129 chip->ecc.steps = mtd->writesize / chip->ecc.size;
3130 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003131 printk(KERN_WARNING "Invalid ecc parameters\n");
3132 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003134 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003135
Thomas Gleixner29072b92006-09-28 15:38:36 +02003136 /*
3137 * Allow subpage writes up to ecc.steps. Not possible for MLC
3138 * FLASH.
3139 */
3140 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3141 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3142 switch(chip->ecc.steps) {
3143 case 2:
3144 mtd->subpage_sft = 1;
3145 break;
3146 case 4:
3147 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003148 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003149 mtd->subpage_sft = 2;
3150 break;
3151 }
3152 }
3153 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3154
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003155 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003156 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003159 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003162 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 /* Fill in remaining MTD driver data */
3165 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02003166 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 mtd->erase = nand_erase;
3168 mtd->point = NULL;
3169 mtd->unpoint = NULL;
3170 mtd->read = nand_read;
3171 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003172 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 mtd->read_oob = nand_read_oob;
3174 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 mtd->sync = nand_sync;
3176 mtd->lock = NULL;
3177 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01003178 mtd->suspend = nand_suspend;
3179 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 mtd->block_isbad = nand_block_isbad;
3181 mtd->block_markbad = nand_block_markbad;
3182
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003183 /* propagate ecc.layout to mtd_info */
3184 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003186 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003187 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003191 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Rusty Russella6e6abd2009-03-31 13:05:31 -06003194/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01003195 test if this is a module _anyway_ -- they'd have to try _really_ hard
3196 to call us from in-kernel code if the core NAND support is modular. */
3197#ifdef MODULE
3198#define caller_is_module() (1)
3199#else
3200#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003201 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003202#endif
3203
3204/**
3205 * nand_scan - [NAND Interface] Scan for the NAND device
3206 * @mtd: MTD device structure
3207 * @maxchips: Number of chips to scan for
3208 *
3209 * This fills out all the uninitialized function pointers
3210 * with the defaults.
3211 * The flash ID is read and the mtd/chip structures are
3212 * filled with the appropriate values.
3213 * The mtd->owner field must be set to the module of the caller
3214 *
3215 */
3216int nand_scan(struct mtd_info *mtd, int maxchips)
3217{
3218 int ret;
3219
3220 /* Many callers got this wrong, so check for it for a while... */
3221 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05303222 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3223 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003224 BUG();
3225 }
3226
3227 ret = nand_scan_ident(mtd, maxchips);
3228 if (!ret)
3229 ret = nand_scan_tail(mtd);
3230 return ret;
3231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003234 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * @mtd: MTD device structure
3236*/
David Woodhousee0c7d762006-05-13 18:07:53 +01003237void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003239 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
3241#ifdef CONFIG_MTD_PARTITIONS
3242 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01003243 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244#endif
3245 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01003246 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Jesper Juhlfa671642005-11-07 01:01:27 -08003248 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003249 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003250 if (!(chip->options & NAND_OWN_BUFFERS))
3251 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Vimal Singh7d70f332010-02-08 15:50:49 +05303254EXPORT_SYMBOL_GPL(nand_lock);
3255EXPORT_SYMBOL_GPL(nand_unlock);
David Woodhousee0c7d762006-05-13 18:07:53 +01003256EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003257EXPORT_SYMBOL_GPL(nand_scan_ident);
3258EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01003259EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003260
3261static int __init nand_base_init(void)
3262{
3263 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3264 return 0;
3265}
3266
3267static void __exit nand_base_exit(void)
3268{
3269 led_trigger_unregister_simple(nand_led_trigger);
3270}
3271
3272module_init(nand_base_init);
3273module_exit(nand_base_exit);
3274
David Woodhousee0c7d762006-05-13 18:07:53 +01003275MODULE_LICENSE("GPL");
3276MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3277MODULE_DESCRIPTION("Generic NAND flash driver code");