blob: 1c4823696be27c495bb4fa41c152e239c75b3b97 [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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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 Gleixner58dd8f22006-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;
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200367 else
368 bad &= 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200371 bad = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000373
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
378
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200379 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return res;
383}
384
385/**
386 * nand_default_block_markbad - [DEFAULT] mark a block bad
387 * @mtd: MTD device structure
388 * @ofs: offset from device start
389 *
390 * This is the default implementation, which can be overridden by
391 * a hardware specific driver.
392*/
393static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
394{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200395 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f22006-05-23 11:52:35 +0200396 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200397 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400400 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200401 if (chip->bbt)
402 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200405 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200406 ret = nand_update_bbt(mtd, ofs);
407 else {
408 /* We write two bytes, so we dont have to mess with 16 bit
409 * access
410 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300411 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200412 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200413 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200414 chip->ops.datbuf = NULL;
415 chip->ops.oobbuf = buf;
416 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000417
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200418 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300419 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200420 }
421 if (!ret)
422 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300423
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * nand_check_wp - [GENERIC] check if the chip is write protected
429 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000430 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000432 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100434static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200436 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200438 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
439 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/**
443 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
444 * @mtd: MTD device structure
445 * @ofs: offset from device start
446 * @getchip: 0, if the chip is already selected
447 * @allowbbt: 1, if its allowed to access the bbt area
448 *
449 * Check, if the block is bad. Either by reading the bad block table or
450 * calling of the scan function.
451 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200452static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
453 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200455 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000456
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200457 if (!chip->bbt)
458 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100461 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200464/**
465 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
466 * @mtd: MTD device structure
467 * @timeo: Timeout
468 *
469 * Helper function for nand_wait_ready used when needing to wait in interrupt
470 * context.
471 */
472static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
473{
474 struct nand_chip *chip = mtd->priv;
475 int i;
476
477 /* Wait for the device to get ready */
478 for (i = 0; i < timeo; i++) {
479 if (chip->dev_ready(mtd))
480 break;
481 touch_softlockup_watchdog();
482 mdelay(1);
483 }
484}
485
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000487 * Wait for the ready pin, after a command
488 * The timeout is catched later.
489 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100490void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000491{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200492 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100493 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000494
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200495 /* 400ms timeout */
496 if (in_interrupt() || oops_in_progress)
497 return panic_nand_wait_ready(mtd, 400);
498
Richard Purdie8fe833c2006-03-31 02:31:14 -0800499 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000500 /* wait until command is processed or timeout occures */
501 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800503 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700504 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000505 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800506 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000507}
David Woodhouse4b648b02006-09-25 17:05:24 +0100508EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/**
511 * nand_command - [DEFAULT] Send command to NAND device
512 * @mtd: MTD device structure
513 * @command: the command to be sent
514 * @column: the column address for this command, -1 if none
515 * @page_addr: the page address for this command, -1 if none
516 *
517 * Send command to NAND device. This function is used for small page
518 * devices (256/512 Bytes per page)
519 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200520static void nand_command(struct mtd_info *mtd, unsigned int command,
521 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200523 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200524 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * Write out the command to the device.
528 */
529 if (command == NAND_CMD_SEQIN) {
530 int readcmd;
531
Joern Engel28318772006-05-22 23:18:05 +0200532 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200534 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 readcmd = NAND_CMD_READOOB;
536 } else if (column < 256) {
537 /* First 256 bytes --> READ0 */
538 readcmd = NAND_CMD_READ0;
539 } else {
540 column -= 256;
541 readcmd = NAND_CMD_READ1;
542 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200543 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200544 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200546 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200548 /*
549 * Address cycle, when necessary
550 */
551 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
552 /* Serially input address */
553 if (column != -1) {
554 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200555 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200556 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200557 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200558 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200561 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200562 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200563 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200564 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200565 if (chip->chipsize > (32 << 20))
566 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200567 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200568 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000569
570 /*
571 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 case NAND_CMD_PAGEPROG:
577 case NAND_CMD_ERASE1:
578 case NAND_CMD_ERASE2:
579 case NAND_CMD_SEQIN:
580 case NAND_CMD_STATUS:
581 return;
582
583 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200584 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200586 udelay(chip->chip_delay);
587 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200588 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200589 chip->cmd_ctrl(mtd,
590 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200591 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return;
593
David Woodhousee0c7d762006-05-13 18:07:53 +0100594 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000596 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * If we don't have access to the busy pin, we apply the given
598 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100599 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200600 if (!chip->dev_ready) {
601 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Apply this short delay always to ensure that we do wait tWB in
606 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100607 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000608
609 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/**
613 * nand_command_lp - [DEFAULT] Send command to NAND large page device
614 * @mtd: MTD device structure
615 * @command: the command to be sent
616 * @column: the column address for this command, -1 if none
617 * @page_addr: the page address for this command, -1 if none
618 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 * Send command to NAND device. This is the version for the new large page
620 * devices We dont have the separate regions as we have in the small page
621 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
624 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200626 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Emulate NAND_CMD_READOOB */
629 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200630 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 command = NAND_CMD_READ0;
632 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000633
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200634 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200635 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200636 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200639 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* Serially input address */
642 if (column != -1) {
643 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200646 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200648 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200651 chip->cmd_ctrl(mtd, page_addr, ctrl);
652 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->chipsize > (128 << 20))
656 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000661
662 /*
663 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000664 * status, sequential in, and deplete1 need no delay
665 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 case NAND_CMD_CACHEDPROG:
669 case NAND_CMD_PAGEPROG:
670 case NAND_CMD_ERASE1:
671 case NAND_CMD_ERASE2:
672 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200673 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000675 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
677
David Woodhousee0c7d762006-05-13 18:07:53 +0100678 /*
679 * read error status commands require only a short delay
680 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000681 case NAND_CMD_STATUS_ERROR:
682 case NAND_CMD_STATUS_ERROR0:
683 case NAND_CMD_STATUS_ERROR1:
684 case NAND_CMD_STATUS_ERROR2:
685 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200686 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200690 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200692 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200693 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
694 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
695 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
696 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200697 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
699
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200700 case NAND_CMD_RNDOUT:
701 /* No ready / busy check necessary */
702 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
703 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
704 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
705 NAND_NCE | NAND_CTRL_CHANGE);
706 return;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200709 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
710 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
711 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
712 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000713
David Woodhousee0c7d762006-05-13 18:07:53 +0100714 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000716 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * If we don't have access to the busy pin, we apply the given
718 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100719 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200720 if (!chip->dev_ready) {
721 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Apply this short delay always to ensure that we do wait tWB in
727 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100728 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000729
730 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200734 * panic_nand_get_device - [GENERIC] Get chip for selected access
735 * @chip: the nand chip descriptor
736 * @mtd: MTD device structure
737 * @new_state: the state which is requested
738 *
739 * Used when in panic, no locks are taken.
740 */
741static void panic_nand_get_device(struct nand_chip *chip,
742 struct mtd_info *mtd, int new_state)
743{
744 /* Hardware controller shared among independend devices */
745 chip->controller->active = chip;
746 chip->state = new_state;
747}
748
749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700751 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 *
755 * Get the device and lock it for exclusive access
756 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200757static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200758nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200760 spinlock_t *lock = &chip->controller->lock;
761 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100762 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100763 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100764 spin_lock(lock);
765
vimal singhb8b3ee92009-07-09 20:41:22 +0530766 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 if (!chip->controller->active)
768 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200769
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200770 if (chip->controller->active == chip && chip->state == FL_READY) {
771 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100772 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100773 return 0;
774 }
775 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800776 if (chip->controller->active->state == FL_PM_SUSPENDED) {
777 chip->state = FL_PM_SUSPENDED;
778 spin_unlock(lock);
779 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800780 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100781 }
782 set_current_state(TASK_UNINTERRUPTIBLE);
783 add_wait_queue(wq, &wait);
784 spin_unlock(lock);
785 schedule();
786 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto retry;
788}
789
790/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200791 * panic_nand_wait - [GENERIC] wait until the command is done
792 * @mtd: MTD device structure
793 * @chip: NAND chip structure
794 * @timeo: Timeout
795 *
796 * Wait for command done. This is a helper function for nand_wait used when
797 * we are in interrupt context. May happen when in panic and trying to write
798 * an oops trough mtdoops.
799 */
800static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
801 unsigned long timeo)
802{
803 int i;
804 for (i = 0; i < timeo; i++) {
805 if (chip->dev_ready) {
806 if (chip->dev_ready(mtd))
807 break;
808 } else {
809 if (chip->read_byte(mtd) & NAND_STATUS_READY)
810 break;
811 }
812 mdelay(1);
813 }
814}
815
816/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * nand_wait - [DEFAULT] wait until the command is done
818 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700819 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
821 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000822 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700824 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200825static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200829 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100832 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100834 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Richard Purdie8fe833c2006-03-31 02:31:14 -0800836 led_trigger_event(nand_led_trigger, LED_FULL);
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Apply this short delay always to ensure that we do wait tWB in
839 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100840 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200842 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
843 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000844 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200845 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200847 if (in_interrupt() || oops_in_progress)
848 panic_nand_wait(mtd, chip, timeo);
849 else {
850 while (time_before(jiffies, timeo)) {
851 if (chip->dev_ready) {
852 if (chip->dev_ready(mtd))
853 break;
854 } else {
855 if (chip->read_byte(mtd) & NAND_STATUS_READY)
856 break;
857 }
858 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800861 led_trigger_event(nand_led_trigger, LED_OFF);
862
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200863 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return status;
865}
866
867/**
Vimal Singh7d70f332010-02-08 15:50:49 +0530868 * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
869 *
870 * @param mtd - mtd info
871 * @param ofs - offset to start unlock from
872 * @param len - length to unlock
873 * @invert - when = 0, unlock the range of blocks within the lower and
874 * upper boundary address
875 * whne = 1, unlock the range of blocks outside the boundaries
876 * of the lower and upper boundary address
877 *
878 * @return - unlock status
879 */
880static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
881 uint64_t len, int invert)
882{
883 int ret = 0;
884 int status, page;
885 struct nand_chip *chip = mtd->priv;
886
887 /* Submit address of first page to unlock */
888 page = ofs >> chip->page_shift;
889 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
890
891 /* Submit address of last page to unlock */
892 page = (ofs + len) >> chip->page_shift;
893 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
894 (page | invert) & chip->pagemask);
895
896 /* Call wait ready function */
897 status = chip->waitfunc(mtd, chip);
898 udelay(1000);
899 /* See if device thinks it succeeded */
900 if (status & 0x01) {
901 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
902 __func__, status);
903 ret = -EIO;
904 }
905
906 return ret;
907}
908
909/**
910 * nand_unlock - [REPLACABLE] unlocks specified locked blockes
911 *
912 * @param mtd - mtd info
913 * @param ofs - offset to start unlock from
914 * @param len - length to unlock
915 *
916 * @return - unlock status
917 */
918int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
919{
920 int ret = 0;
921 int chipnr;
922 struct nand_chip *chip = mtd->priv;
923
924 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
925 __func__, (unsigned long long)ofs, len);
926
927 if (check_offs_len(mtd, ofs, len))
928 ret = -EINVAL;
929
930 /* Align to last block address if size addresses end of the device */
931 if (ofs + len == mtd->size)
932 len -= mtd->erasesize;
933
934 nand_get_device(chip, mtd, FL_UNLOCKING);
935
936 /* Shift to get chip number */
937 chipnr = ofs >> chip->chip_shift;
938
939 chip->select_chip(mtd, chipnr);
940
941 /* Check, if it is write protected */
942 if (nand_check_wp(mtd)) {
943 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
944 __func__);
945 ret = -EIO;
946 goto out;
947 }
948
949 ret = __nand_unlock(mtd, ofs, len, 0);
950
951out:
952 /* de-select the NAND device */
953 chip->select_chip(mtd, -1);
954
955 nand_release_device(mtd);
956
957 return ret;
958}
959
960/**
961 * nand_lock - [REPLACABLE] locks all blockes present in the device
962 *
963 * @param mtd - mtd info
964 * @param ofs - offset to start unlock from
965 * @param len - length to unlock
966 *
967 * @return - lock status
968 *
969 * This feature is not support in many NAND parts. 'Micron' NAND parts
970 * do have this feature, but it allows only to lock all blocks not for
971 * specified range for block.
972 *
973 * Implementing 'lock' feature by making use of 'unlock', for now.
974 */
975int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
976{
977 int ret = 0;
978 int chipnr, status, page;
979 struct nand_chip *chip = mtd->priv;
980
981 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
982 __func__, (unsigned long long)ofs, len);
983
984 if (check_offs_len(mtd, ofs, len))
985 ret = -EINVAL;
986
987 nand_get_device(chip, mtd, FL_LOCKING);
988
989 /* Shift to get chip number */
990 chipnr = ofs >> chip->chip_shift;
991
992 chip->select_chip(mtd, chipnr);
993
994 /* Check, if it is write protected */
995 if (nand_check_wp(mtd)) {
996 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
997 __func__);
998 status = MTD_ERASE_FAILED;
999 ret = -EIO;
1000 goto out;
1001 }
1002
1003 /* Submit address of first page to lock */
1004 page = ofs >> chip->page_shift;
1005 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1006
1007 /* Call wait ready function */
1008 status = chip->waitfunc(mtd, chip);
1009 udelay(1000);
1010 /* See if device thinks it succeeded */
1011 if (status & 0x01) {
1012 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
1013 __func__, status);
1014 ret = -EIO;
1015 goto out;
1016 }
1017
1018 ret = __nand_unlock(mtd, ofs, len, 0x1);
1019
1020out:
1021 /* de-select the NAND device */
1022 chip->select_chip(mtd, -1);
1023
1024 nand_release_device(mtd);
1025
1026 return ret;
1027}
1028
1029/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001030 * nand_read_page_raw - [Intern] read raw page data without ecc
1031 * @mtd: mtd info structure
1032 * @chip: nand chip info structure
1033 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001034 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001035 *
1036 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001037 */
1038static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001039 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001040{
1041 chip->read_buf(mtd, buf, mtd->writesize);
1042 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1043 return 0;
1044}
1045
1046/**
David Brownell52ff49d2009-03-04 12:01:36 -08001047 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
1048 * @mtd: mtd info structure
1049 * @chip: nand chip info structure
1050 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001051 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001052 *
1053 * We need a special oob layout and handling even when OOB isn't used.
1054 */
1055static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001056 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001057{
1058 int eccsize = chip->ecc.size;
1059 int eccbytes = chip->ecc.bytes;
1060 uint8_t *oob = chip->oob_poi;
1061 int steps, size;
1062
1063 for (steps = chip->ecc.steps; steps > 0; steps--) {
1064 chip->read_buf(mtd, buf, eccsize);
1065 buf += eccsize;
1066
1067 if (chip->ecc.prepad) {
1068 chip->read_buf(mtd, oob, chip->ecc.prepad);
1069 oob += chip->ecc.prepad;
1070 }
1071
1072 chip->read_buf(mtd, oob, eccbytes);
1073 oob += eccbytes;
1074
1075 if (chip->ecc.postpad) {
1076 chip->read_buf(mtd, oob, chip->ecc.postpad);
1077 oob += chip->ecc.postpad;
1078 }
1079 }
1080
1081 size = mtd->oobsize - (oob - chip->oob_poi);
1082 if (size)
1083 chip->read_buf(mtd, oob, size);
1084
1085 return 0;
1086}
1087
1088/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001089 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001090 * @mtd: mtd info structure
1091 * @chip: nand chip info structure
1092 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001093 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001094 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001095static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001096 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001098 int i, eccsize = chip->ecc.size;
1099 int eccbytes = chip->ecc.bytes;
1100 int eccsteps = chip->ecc.steps;
1101 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001102 uint8_t *ecc_calc = chip->buffers->ecccalc;
1103 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001104 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001105
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001106 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001107
1108 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1109 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1110
1111 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001112 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001113
1114 eccsteps = chip->ecc.steps;
1115 p = buf;
1116
1117 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1118 int stat;
1119
1120 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001121 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001122 mtd->ecc_stats.failed++;
1123 else
1124 mtd->ecc_stats.corrected += stat;
1125 }
1126 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/**
Alexey Korolev3d459552008-05-15 17:23:18 +01001130 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
1131 * @mtd: mtd info structure
1132 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +01001133 * @data_offs: offset of requested data within the page
1134 * @readlen: data length
1135 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001136 */
1137static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
1138{
1139 int start_step, end_step, num_steps;
1140 uint32_t *eccpos = chip->ecc.layout->eccpos;
1141 uint8_t *p;
1142 int data_col_addr, i, gaps = 0;
1143 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1144 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1145
1146 /* Column address wihin the page aligned to ECC size (256bytes). */
1147 start_step = data_offs / chip->ecc.size;
1148 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1149 num_steps = end_step - start_step + 1;
1150
1151 /* Data size aligned to ECC ecc.size*/
1152 datafrag_len = num_steps * chip->ecc.size;
1153 eccfrag_len = num_steps * chip->ecc.bytes;
1154
1155 data_col_addr = start_step * chip->ecc.size;
1156 /* If we read not a page aligned data */
1157 if (data_col_addr != 0)
1158 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1159
1160 p = bufpoi + data_col_addr;
1161 chip->read_buf(mtd, p, datafrag_len);
1162
1163 /* Calculate ECC */
1164 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1165 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1166
1167 /* The performance is faster if to position offsets
1168 according to ecc.pos. Let make sure here that
1169 there are no gaps in ecc positions */
1170 for (i = 0; i < eccfrag_len - 1; i++) {
1171 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1172 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1173 gaps = 1;
1174 break;
1175 }
1176 }
1177 if (gaps) {
1178 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1179 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1180 } else {
1181 /* send the command to read the particular ecc bytes */
1182 /* take care about buswidth alignment in read_buf */
1183 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1184 aligned_len = eccfrag_len;
1185 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1186 aligned_len++;
1187 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1188 aligned_len++;
1189
1190 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1191 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1192 }
1193
1194 for (i = 0; i < eccfrag_len; i++)
1195 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1196
1197 p = bufpoi + data_col_addr;
1198 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1199 int stat;
1200
1201 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1202 if (stat == -1)
1203 mtd->ecc_stats.failed++;
1204 else
1205 mtd->ecc_stats.corrected += stat;
1206 }
1207 return 0;
1208}
1209
1210/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001211 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001212 * @mtd: mtd info structure
1213 * @chip: nand chip info structure
1214 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001215 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001216 *
1217 * Not for syndrome calculating ecc controllers which need a special oob layout
1218 */
1219static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001220 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001221{
1222 int i, eccsize = chip->ecc.size;
1223 int eccbytes = chip->ecc.bytes;
1224 int eccsteps = chip->ecc.steps;
1225 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001226 uint8_t *ecc_calc = chip->buffers->ecccalc;
1227 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001228 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001229
1230 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1231 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1232 chip->read_buf(mtd, p, eccsize);
1233 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1234 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001235 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001236
1237 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001238 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001239
1240 eccsteps = chip->ecc.steps;
1241 p = buf;
1242
1243 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1244 int stat;
1245
1246 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001247 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001248 mtd->ecc_stats.failed++;
1249 else
1250 mtd->ecc_stats.corrected += stat;
1251 }
1252 return 0;
1253}
1254
1255/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001256 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1257 * @mtd: mtd info structure
1258 * @chip: nand chip info structure
1259 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001260 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001261 *
1262 * Hardware ECC for large page chips, require OOB to be read first.
1263 * For this ECC mode, the write_page method is re-used from ECC_HW.
1264 * These methods read/write ECC from the OOB area, unlike the
1265 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1266 * "infix ECC" scheme and reads/writes ECC from the data area, by
1267 * overwriting the NAND manufacturer bad block markings.
1268 */
1269static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1270 struct nand_chip *chip, uint8_t *buf, int page)
1271{
1272 int i, eccsize = chip->ecc.size;
1273 int eccbytes = chip->ecc.bytes;
1274 int eccsteps = chip->ecc.steps;
1275 uint8_t *p = buf;
1276 uint8_t *ecc_code = chip->buffers->ecccode;
1277 uint32_t *eccpos = chip->ecc.layout->eccpos;
1278 uint8_t *ecc_calc = chip->buffers->ecccalc;
1279
1280 /* Read the OOB area first */
1281 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1282 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1283 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1284
1285 for (i = 0; i < chip->ecc.total; i++)
1286 ecc_code[i] = chip->oob_poi[eccpos[i]];
1287
1288 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1289 int stat;
1290
1291 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1292 chip->read_buf(mtd, p, eccsize);
1293 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1294
1295 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1296 if (stat < 0)
1297 mtd->ecc_stats.failed++;
1298 else
1299 mtd->ecc_stats.corrected += stat;
1300 }
1301 return 0;
1302}
1303
1304/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001305 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001306 * @mtd: mtd info structure
1307 * @chip: nand chip info structure
1308 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001309 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001310 *
1311 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001312 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313 */
1314static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001315 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001316{
1317 int i, eccsize = chip->ecc.size;
1318 int eccbytes = chip->ecc.bytes;
1319 int eccsteps = chip->ecc.steps;
1320 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001321 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001322
1323 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1324 int stat;
1325
1326 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1327 chip->read_buf(mtd, p, eccsize);
1328
1329 if (chip->ecc.prepad) {
1330 chip->read_buf(mtd, oob, chip->ecc.prepad);
1331 oob += chip->ecc.prepad;
1332 }
1333
1334 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1335 chip->read_buf(mtd, oob, eccbytes);
1336 stat = chip->ecc.correct(mtd, p, oob, NULL);
1337
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001338 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001339 mtd->ecc_stats.failed++;
1340 else
1341 mtd->ecc_stats.corrected += stat;
1342
1343 oob += eccbytes;
1344
1345 if (chip->ecc.postpad) {
1346 chip->read_buf(mtd, oob, chip->ecc.postpad);
1347 oob += chip->ecc.postpad;
1348 }
1349 }
1350
1351 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001352 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001353 if (i)
1354 chip->read_buf(mtd, oob, i);
1355
1356 return 0;
1357}
1358
1359/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001360 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1361 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001362 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001363 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001364 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001365 */
1366static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001367 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001368{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001369 switch(ops->mode) {
1370
1371 case MTD_OOB_PLACE:
1372 case MTD_OOB_RAW:
1373 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1374 return oob + len;
1375
1376 case MTD_OOB_AUTO: {
1377 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001378 uint32_t boffs = 0, roffs = ops->ooboffs;
1379 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001380
1381 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001382 /* Read request not from offset 0 ? */
1383 if (unlikely(roffs)) {
1384 if (roffs >= free->length) {
1385 roffs -= free->length;
1386 continue;
1387 }
1388 boffs = free->offset + roffs;
1389 bytes = min_t(size_t, len,
1390 (free->length - roffs));
1391 roffs = 0;
1392 } else {
1393 bytes = min_t(size_t, len, free->length);
1394 boffs = free->offset;
1395 }
1396 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001397 oob += bytes;
1398 }
1399 return oob;
1400 }
1401 default:
1402 BUG();
1403 }
1404 return NULL;
1405}
1406
1407/**
1408 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001409 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001410 * @mtd: MTD device structure
1411 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001412 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001413 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001414 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001415 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001416static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1417 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001418{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001419 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001420 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001421 struct mtd_ecc_stats stats;
1422 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1423 int sndcmd = 1;
1424 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001425 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001426 uint32_t oobreadlen = ops->ooblen;
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001427 uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
1428 mtd->oobavail : mtd->oobsize;
1429
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001430 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001432 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001434 chipnr = (int)(from >> chip->chip_shift);
1435 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001437 realpage = (int)(from >> chip->page_shift);
1438 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001440 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001442 buf = ops->datbuf;
1443 oob = ops->oobbuf;
1444
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001445 while(1) {
1446 bytes = min(mtd->writesize - col, readlen);
1447 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001448
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001449 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001451 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001453 if (likely(sndcmd)) {
1454 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1455 sndcmd = 0;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001458 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001459 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001460 ret = chip->ecc.read_page_raw(mtd, chip,
1461 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001462 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1463 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001464 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001465 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1466 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001467 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001468 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001469
1470 /* Transfer not aligned data */
1471 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001472 if (!NAND_SUBPAGE_READ(chip) && !oob)
1473 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001474 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001476
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001477 buf += bytes;
1478
1479 if (unlikely(oob)) {
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001480
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001481 int toread = min(oobreadlen, max_oobsize);
1482
1483 if (toread) {
1484 oob = nand_transfer_oob(chip,
1485 oob, ops, toread);
1486 oobreadlen -= toread;
1487 }
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 */
Maxim Levitsky782ce792010-02-22 20:39:36 +02002074static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
2075 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002076{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002077 switch(ops->mode) {
2078
2079 case MTD_OOB_PLACE:
2080 case MTD_OOB_RAW:
2081 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2082 return oob + len;
2083
2084 case MTD_OOB_AUTO: {
2085 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002086 uint32_t boffs = 0, woffs = ops->ooboffs;
2087 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002088
2089 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002090 /* Write request not from offset 0 ? */
2091 if (unlikely(woffs)) {
2092 if (woffs >= free->length) {
2093 woffs -= free->length;
2094 continue;
2095 }
2096 boffs = free->offset + woffs;
2097 bytes = min_t(size_t, len,
2098 (free->length - woffs));
2099 woffs = 0;
2100 } else {
2101 bytes = min_t(size_t, len, free->length);
2102 boffs = free->offset;
2103 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002104 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002105 oob += bytes;
2106 }
2107 return oob;
2108 }
2109 default:
2110 BUG();
2111 }
2112 return NULL;
2113}
2114
Thomas Gleixner29072b92006-09-28 15:38:36 +02002115#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002116
2117/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002118 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002119 * @mtd: MTD device structure
2120 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002121 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002122 *
2123 * NAND write with ECC
2124 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002125static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2126 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002127{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002128 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002129 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002130 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002131
2132 uint32_t oobwritelen = ops->ooblen;
2133 uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
2134 mtd->oobavail : mtd->oobsize;
2135
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002136 uint8_t *oob = ops->oobbuf;
2137 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002138 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002139
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002140 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002141 if (!writelen)
2142 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002143
2144 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002145 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302146 printk(KERN_NOTICE "%s: Attempt to write not "
2147 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002148 return -EINVAL;
2149 }
2150
Thomas Gleixner29072b92006-09-28 15:38:36 +02002151 column = to & (mtd->writesize - 1);
2152 subpage = column || (writelen & (mtd->writesize - 1));
2153
2154 if (subpage && oob)
2155 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002156
Thomas Gleixner6a930962006-06-28 00:11:45 +02002157 chipnr = (int)(to >> chip->chip_shift);
2158 chip->select_chip(mtd, chipnr);
2159
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002160 /* Check, if it is write protected */
2161 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002162 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002163
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002164 realpage = (int)(to >> chip->page_shift);
2165 page = realpage & chip->pagemask;
2166 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2167
2168 /* Invalidate the page cache, when we write to the cached page */
2169 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002170 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002171 chip->pagebuf = -1;
2172
David Woodhouse7dcdcbe2006-10-21 17:09:53 +01002173 /* If we're not given explicit OOB data, let it be 0xFF */
2174 if (likely(!oob))
2175 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002176
Maxim Levitsky782ce792010-02-22 20:39:36 +02002177 /* Don't allow multipage oob writes with offset */
2178 if (ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2179 return -EINVAL;
2180
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002181 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002182 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002183 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002184 uint8_t *wbuf = buf;
2185
2186 /* Partial page write ? */
2187 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2188 cached = 0;
2189 bytes = min_t(int, bytes - column, (int) writelen);
2190 chip->pagebuf = -1;
2191 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2192 memcpy(&chip->buffers->databuf[column], buf, bytes);
2193 wbuf = chip->buffers->databuf;
2194 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002195
Maxim Levitsky782ce792010-02-22 20:39:36 +02002196 if (unlikely(oob)) {
2197 size_t len = min(oobwritelen, oobmaxlen);
2198 oob = nand_fill_oob(chip, oob, len, ops);
2199 oobwritelen -= len;
2200 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002201
Thomas Gleixner29072b92006-09-28 15:38:36 +02002202 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01002203 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002204 if (ret)
2205 break;
2206
2207 writelen -= bytes;
2208 if (!writelen)
2209 break;
2210
Thomas Gleixner29072b92006-09-28 15:38:36 +02002211 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002212 buf += bytes;
2213 realpage++;
2214
2215 page = realpage & chip->pagemask;
2216 /* Check, if we cross a chip boundary */
2217 if (!page) {
2218 chipnr++;
2219 chip->select_chip(mtd, -1);
2220 chip->select_chip(mtd, chipnr);
2221 }
2222 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002223
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002224 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002225 if (unlikely(oob))
2226 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002227 return ret;
2228}
2229
2230/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002231 * panic_nand_write - [MTD Interface] NAND write with ECC
2232 * @mtd: MTD device structure
2233 * @to: offset to write to
2234 * @len: number of bytes to write
2235 * @retlen: pointer to variable to store the number of written bytes
2236 * @buf: the data to write
2237 *
2238 * NAND write with ECC. Used when performing writes in interrupt context, this
2239 * may for example be called by mtdoops when writing an oops while in panic.
2240 */
2241static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2242 size_t *retlen, const uint8_t *buf)
2243{
2244 struct nand_chip *chip = mtd->priv;
2245 int ret;
2246
2247 /* Do not allow reads past end of device */
2248 if ((to + len) > mtd->size)
2249 return -EINVAL;
2250 if (!len)
2251 return 0;
2252
2253 /* Wait for the device to get ready. */
2254 panic_nand_wait(mtd, chip, 400);
2255
2256 /* Grab the device. */
2257 panic_nand_get_device(chip, mtd, FL_WRITING);
2258
2259 chip->ops.len = len;
2260 chip->ops.datbuf = (uint8_t *)buf;
2261 chip->ops.oobbuf = NULL;
2262
2263 ret = nand_do_write_ops(mtd, to, &chip->ops);
2264
2265 *retlen = chip->ops.retlen;
2266 return ret;
2267}
2268
2269/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002270 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * @mtd: MTD device structure
2272 * @to: offset to write to
2273 * @len: number of bytes to write
2274 * @retlen: pointer to variable to store the number of written bytes
2275 * @buf: the data to write
2276 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002277 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002279static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002280 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002282 struct nand_chip *chip = mtd->priv;
2283 int ret;
2284
2285 /* Do not allow reads past end of device */
2286 if ((to + len) > mtd->size)
2287 return -EINVAL;
2288 if (!len)
2289 return 0;
2290
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002291 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002292
2293 chip->ops.len = len;
2294 chip->ops.datbuf = (uint8_t *)buf;
2295 chip->ops.oobbuf = NULL;
2296
2297 ret = nand_do_write_ops(mtd, to, &chip->ops);
2298
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002299 *retlen = chip->ops.retlen;
2300
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002301 nand_release_device(mtd);
2302
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002303 return ret;
2304}
2305
2306/**
2307 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2308 * @mtd: MTD device structure
2309 * @to: offset to write to
2310 * @ops: oob operation description structure
2311 *
2312 * NAND write out-of-band
2313 */
2314static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2315 struct mtd_oob_ops *ops)
2316{
Adrian Hunter03736152007-01-31 17:58:29 +02002317 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002318 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
vimal singh20d8e242009-07-07 15:49:49 +05302320 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2321 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Adrian Hunter03736152007-01-31 17:58:29 +02002323 if (ops->mode == MTD_OOB_AUTO)
2324 len = chip->ecc.layout->oobavail;
2325 else
2326 len = mtd->oobsize;
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002329 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302330 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2331 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return -EINVAL;
2333 }
2334
Adrian Hunter03736152007-01-31 17:58:29 +02002335 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302336 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2337 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002338 return -EINVAL;
2339 }
2340
2341 /* Do not allow reads past end of device */
2342 if (unlikely(to >= mtd->size ||
2343 ops->ooboffs + ops->ooblen >
2344 ((mtd->size >> chip->page_shift) -
2345 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302346 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2347 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002348 return -EINVAL;
2349 }
2350
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002351 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002352 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002354 /* Shift to get page */
2355 page = (int)(to >> chip->page_shift);
2356
2357 /*
2358 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2359 * of my DiskOnChip 2000 test units) will clear the whole data page too
2360 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2361 * it in the doc2000 driver in August 1999. dwmw2.
2362 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002363 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 /* Check, if it is write protected */
2366 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002367 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002370 if (page == chip->pagebuf)
2371 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002373 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002374 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002375 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2376 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002377
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002378 if (status)
2379 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Vitaly Wool70145682006-11-03 18:20:38 +03002381 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002383 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002386/**
2387 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2388 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002389 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002390 * @ops: oob operation description structure
2391 */
2392static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2393 struct mtd_oob_ops *ops)
2394{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002395 struct nand_chip *chip = mtd->priv;
2396 int ret = -ENOTSUPP;
2397
2398 ops->retlen = 0;
2399
2400 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002401 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302402 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2403 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002404 return -EINVAL;
2405 }
2406
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002407 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002408
2409 switch(ops->mode) {
2410 case MTD_OOB_PLACE:
2411 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002412 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002413 break;
2414
2415 default:
2416 goto out;
2417 }
2418
2419 if (!ops->datbuf)
2420 ret = nand_do_write_oob(mtd, to, ops);
2421 else
2422 ret = nand_do_write_ops(mtd, to, ops);
2423
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002424 out:
2425 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return ret;
2427}
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2431 * @mtd: MTD device structure
2432 * @page: the page address of the block which will be erased
2433 *
2434 * Standard erase command for NAND chips
2435 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002436static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002438 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002440 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2441 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444/**
2445 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2446 * @mtd: MTD device structure
2447 * @page: the page address of the block which will be erased
2448 *
2449 * AND multi block erase command function
2450 * Erase 4 consecutive blocks
2451 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002452static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002454 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002456 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2457 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2458 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2459 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2460 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
2463/**
2464 * nand_erase - [MTD Interface] erase block(s)
2465 * @mtd: MTD device structure
2466 * @instr: erase instruction
2467 *
2468 * Erase one ore more blocks
2469 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002470static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
David Woodhousee0c7d762006-05-13 18:07:53 +01002472 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002474
David A. Marlin30f464b2005-01-17 18:35:25 +00002475#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002477 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 * @mtd: MTD device structure
2479 * @instr: erase instruction
2480 * @allowbbt: allow erasing the bbt area
2481 *
2482 * Erase one ore more blocks
2483 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002484int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2485 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Adrian Hunter69423d92008-12-10 13:37:21 +00002487 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002488 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002489 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002490 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002491 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
vimal singh20d8e242009-07-07 15:49:49 +05302493 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2494 __func__, (unsigned long long)instr->addr,
2495 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302497 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002500 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002503 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002506 page = (int)(instr->addr >> chip->page_shift);
2507 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002510 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002513 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* Check, if it is write protected */
2516 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302517 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2518 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 instr->state = MTD_ERASE_FAILED;
2520 goto erase_exit;
2521 }
2522
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002523 /*
2524 * If BBT requires refresh, set the BBT page mask to see if the BBT
2525 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2526 * can not be matched. This is also done when the bbt is actually
2527 * erased to avoid recusrsive updates
2528 */
2529 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2530 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /* Loop through the pages */
2533 len = instr->len;
2534
2535 instr->state = MTD_ERASING;
2536
2537 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002538 /*
2539 * heck if we have a bad block, we do not erase bad blocks !
2540 */
2541 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2542 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302543 printk(KERN_WARNING "%s: attempt to erase a bad block "
2544 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 instr->state = MTD_ERASE_FAILED;
2546 goto erase_exit;
2547 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002548
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002549 /*
2550 * Invalidate the page cache, if we erase the block which
2551 * contains the current cached page
2552 */
2553 if (page <= chip->pagebuf && chip->pagebuf <
2554 (page + pages_per_block))
2555 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002557 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002558
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002559 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002561 /*
2562 * See if operation failed and additional status checks are
2563 * available
2564 */
2565 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2566 status = chip->errstat(mtd, chip, FL_ERASING,
2567 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002570 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302571 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2572 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002574 instr->fail_addr =
2575 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 goto erase_exit;
2577 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002578
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 /*
2580 * If BBT requires refresh, set the BBT rewrite flag to the
2581 * page being erased
2582 */
2583 if (bbt_masked_page != 0xffffffff &&
2584 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002585 rewrite_bbt[chipnr] =
2586 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002589 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 page += pages_per_block;
2591
2592 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002593 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002595 chip->select_chip(mtd, -1);
2596 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002597
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002598 /*
2599 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2600 * page mask to see if this BBT should be rewritten
2601 */
2602 if (bbt_masked_page != 0xffffffff &&
2603 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2604 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2605 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607 }
2608 instr->state = MTD_ERASE_DONE;
2609
David Woodhousee0c7d762006-05-13 18:07:53 +01002610 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
2614 /* Deselect and wake up anyone waiting on the device */
2615 nand_release_device(mtd);
2616
David Woodhouse49defc02007-10-06 15:01:59 -04002617 /* Do call back function */
2618 if (!ret)
2619 mtd_erase_callback(instr);
2620
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002621 /*
2622 * If BBT requires refresh and erase was successful, rewrite any
2623 * selected bad block tables
2624 */
2625 if (bbt_masked_page == 0xffffffff || ret)
2626 return ret;
2627
2628 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2629 if (!rewrite_bbt[chipnr])
2630 continue;
2631 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302632 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2633 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2634 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002635 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002636 }
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /* Return more or less happy */
2639 return ret;
2640}
2641
2642/**
2643 * nand_sync - [MTD Interface] sync
2644 * @mtd: MTD device structure
2645 *
2646 * Sync is actually a wait for chip ready function
2647 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002648static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002650 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
vimal singh20d8e242009-07-07 15:49:49 +05302652 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002657 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002661 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002663 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002665static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002668 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002670
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002671 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002675 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 * @mtd: MTD device structure
2677 * @ofs: offset relative to mtd start
2678 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002679static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002681 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 int ret;
2683
David Woodhousee0c7d762006-05-13 18:07:53 +01002684 if ((ret = nand_block_isbad(mtd, ofs))) {
2685 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (ret > 0)
2687 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002688 return ret;
2689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002691 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
2694/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002695 * nand_suspend - [MTD Interface] Suspend the NAND flash
2696 * @mtd: MTD device structure
2697 */
2698static int nand_suspend(struct mtd_info *mtd)
2699{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002700 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002701
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002702 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002703}
2704
2705/**
2706 * nand_resume - [MTD Interface] Resume the NAND flash
2707 * @mtd: MTD device structure
2708 */
2709static void nand_resume(struct mtd_info *mtd)
2710{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002711 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002712
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002713 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002714 nand_release_device(mtd);
2715 else
vimal singh20d8e242009-07-07 15:49:49 +05302716 printk(KERN_ERR "%s called for a chip which is not "
2717 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002718}
2719
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002720/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002721 * Set default functions
2722 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002726 if (!chip->chip_delay)
2727 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002730 if (chip->cmdfunc == NULL)
2731 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002734 if (chip->waitfunc == NULL)
2735 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002737 if (!chip->select_chip)
2738 chip->select_chip = nand_select_chip;
2739 if (!chip->read_byte)
2740 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2741 if (!chip->read_word)
2742 chip->read_word = nand_read_word;
2743 if (!chip->block_bad)
2744 chip->block_bad = nand_block_bad;
2745 if (!chip->block_markbad)
2746 chip->block_markbad = nand_default_block_markbad;
2747 if (!chip->write_buf)
2748 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2749 if (!chip->read_buf)
2750 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2751 if (!chip->verify_buf)
2752 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2753 if (!chip->scan_bbt)
2754 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002755
2756 if (!chip->controller) {
2757 chip->controller = &chip->hwcontrol;
2758 spin_lock_init(&chip->controller->lock);
2759 init_waitqueue_head(&chip->controller->wq);
2760 }
2761
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002762}
2763
2764/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002765 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002766 */
2767static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002768 struct nand_chip *chip,
David Woodhouse5e81e882010-02-26 18:32:56 +00002769 int busw, int *maf_id,
2770 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002771{
David Woodhouse5e81e882010-02-26 18:32:56 +00002772 int dev_id, maf_idx;
Ben Dooksed8165c2008-04-14 14:58:58 +01002773 int tmp_id, tmp_manf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002776 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Karl Beldanef89a882008-09-15 14:37:29 +02002778 /*
2779 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2780 * after power-up
2781 */
2782 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002785 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002788 *maf_id = chip->read_byte(mtd);
2789 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Ben Dooksed8165c2008-04-14 14:58:58 +01002791 /* Try again to make sure, as some systems the bus-hold or other
2792 * interface concerns can cause random data which looks like a
2793 * possibly credible NAND flash to appear. If the two results do
2794 * not match, ignore the device completely.
2795 */
2796
2797 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2798
2799 /* Read manufacturer and device IDs */
2800
2801 tmp_manf = chip->read_byte(mtd);
2802 tmp_id = chip->read_byte(mtd);
2803
2804 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2805 printk(KERN_INFO "%s: second ID read did not match "
2806 "%02x,%02x against %02x,%02x\n", __func__,
2807 *maf_id, dev_id, tmp_manf, tmp_id);
2808 return ERR_PTR(-ENODEV);
2809 }
2810
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002811 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00002812 type = nand_flash_ids;
2813
2814 for (; type->name != NULL; type++)
2815 if (dev_id == type->id)
2816 break;
2817
2818 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002819 return ERR_PTR(-ENODEV);
2820
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002821 if (!mtd->name)
2822 mtd->name = type->name;
2823
Adrian Hunter69423d92008-12-10 13:37:21 +00002824 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002825
2826 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002827 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002828 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002829 /* The 3rd id byte holds MLC / multichip data */
2830 chip->cellinfo = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002831 /* The 4th id byte is the important one */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002832 extid = chip->read_byte(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002833 /* Calc pagesize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002834 mtd->writesize = 1024 << (extid & 0x3);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002835 extid >>= 2;
2836 /* Calc oobsize */
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002837 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002838 extid >>= 2;
2839 /* Calc blocksize. Blocksize is multiples of 64KiB */
2840 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2841 extid >>= 2;
2842 /* Get buswidth information */
2843 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2844
2845 } else {
2846 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002847 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002848 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002849 mtd->erasesize = type->erasesize;
2850 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002851 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002852 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002853 }
2854
2855 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002856 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002857 if (nand_manuf_ids[maf_idx].id == *maf_id)
2858 break;
2859 }
2860
2861 /*
2862 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002863 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002864 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002865 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002866 printk(KERN_INFO "NAND device: Manufacturer ID:"
2867 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2868 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2869 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002870 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002871 busw ? 16 : 8);
2872 return ERR_PTR(-EINVAL);
2873 }
2874
2875 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002876 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002877 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002878 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002879
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002880 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002881 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002882 if (chip->chipsize & 0xffffffff)
2883 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2884 else
2885 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002886
2887 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002888 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002889 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Maxim Levitskye0b58d02010-02-22 20:39:38 +02002890 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002891
2892 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002893 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002894 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002895
2896 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002897 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002898 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002899 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002900
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002901 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002902 * options for chips which are not having an extended id.
2903 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002904 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002905 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002906
2907 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002908 if (chip->options & NAND_4PAGE_ARRAY)
2909 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002910 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002911 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002912
2913 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002914 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2915 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002916
2917 printk(KERN_INFO "NAND device: Manufacturer ID:"
2918 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2919 nand_manuf_ids[maf_idx].name, type->name);
2920
2921 return type;
2922}
2923
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002924/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002925 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2926 * @mtd: MTD device structure
2927 * @maxchips: Number of chips to scan for
David Woodhouse5e81e882010-02-26 18:32:56 +00002928 * @table: Alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002929 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002930 * This is the first phase of the normal nand_scan() function. It
2931 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002932 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002933 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002934 */
David Woodhouse5e81e882010-02-26 18:32:56 +00002935int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2936 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002937{
2938 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002939 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002940 struct nand_flash_dev *type;
2941
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002942 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002943 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002944 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002945 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002946
2947 /* Read the flash type */
David Woodhouse5e81e882010-02-26 18:32:56 +00002948 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002949
2950 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00002951 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
2952 printk(KERN_WARNING "No NAND device found.\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002954 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002957 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01002958 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002959 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02002960 /* See comment in nand_get_flash_type for reset */
2961 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002963 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002965 if (nand_maf_id != chip->read_byte(mtd) ||
2966 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 break;
2968 }
2969 if (i > 1)
2970 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002973 chip->numchips = i;
2974 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
David Woodhouse3b85c322006-09-25 17:06:53 +01002976 return 0;
2977}
2978
2979
2980/**
2981 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2982 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01002983 *
2984 * This is the second phase of the normal nand_scan() function. It
2985 * fills out all the uninitialized function pointers with the defaults
2986 * and scans for a bad block table if appropriate.
2987 */
2988int nand_scan_tail(struct mtd_info *mtd)
2989{
2990 int i;
2991 struct nand_chip *chip = mtd->priv;
2992
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002993 if (!(chip->options & NAND_OWN_BUFFERS))
2994 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2995 if (!chip->buffers)
2996 return -ENOMEM;
2997
David Woodhouse7dcdcbe2006-10-21 17:09:53 +01002998 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01002999 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003000
3001 /*
3002 * If no default placement scheme is given, select an appropriate one
3003 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003004 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003005 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003007 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 break;
3009 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003010 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 break;
3012 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003013 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003015 case 128:
3016 chip->ecc.layout = &nand_oob_128;
3017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003019 printk(KERN_WARNING "No oob scheme defined for "
3020 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 BUG();
3022 }
3023 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003024
David Woodhouse956e9442006-09-25 17:12:39 +01003025 if (!chip->write_page)
3026 chip->write_page = nand_write_page;
3027
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003028 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003029 * check ECC mode, default to software if 3byte/512byte hardware ECC is
3030 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003031 */
David Woodhouse956e9442006-09-25 17:12:39 +01003032
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003033 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003034 case NAND_ECC_HW_OOB_FIRST:
3035 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3036 if (!chip->ecc.calculate || !chip->ecc.correct ||
3037 !chip->ecc.hwctl) {
3038 printk(KERN_WARNING "No ECC functions supplied; "
3039 "Hardware ECC not possible\n");
3040 BUG();
3041 }
3042 if (!chip->ecc.read_page)
3043 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3044
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003045 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003046 /* Use standard hwecc read page function ? */
3047 if (!chip->ecc.read_page)
3048 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003049 if (!chip->ecc.write_page)
3050 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003051 if (!chip->ecc.read_page_raw)
3052 chip->ecc.read_page_raw = nand_read_page_raw;
3053 if (!chip->ecc.write_page_raw)
3054 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003055 if (!chip->ecc.read_oob)
3056 chip->ecc.read_oob = nand_read_oob_std;
3057 if (!chip->ecc.write_oob)
3058 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003059
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003060 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003061 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3062 !chip->ecc.hwctl) &&
3063 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003064 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003065 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003066 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003067 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003068 "Hardware ECC not possible\n");
3069 BUG();
3070 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003071 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003072 if (!chip->ecc.read_page)
3073 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003074 if (!chip->ecc.write_page)
3075 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003076 if (!chip->ecc.read_page_raw)
3077 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3078 if (!chip->ecc.write_page_raw)
3079 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003080 if (!chip->ecc.read_oob)
3081 chip->ecc.read_oob = nand_read_oob_syndrome;
3082 if (!chip->ecc.write_oob)
3083 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003084
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003085 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003086 break;
3087 printk(KERN_WARNING "%d byte HW ECC not possible on "
3088 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003089 chip->ecc.size, mtd->writesize);
3090 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003092 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003093 chip->ecc.calculate = nand_calculate_ecc;
3094 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003095 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003096 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003097 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003098 chip->ecc.read_page_raw = nand_read_page_raw;
3099 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003100 chip->ecc.read_oob = nand_read_oob_std;
3101 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003102 if (!chip->ecc.size)
3103 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003104 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003106
3107 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003108 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
3109 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003110 chip->ecc.read_page = nand_read_page_raw;
3111 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003112 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003113 chip->ecc.read_page_raw = nand_read_page_raw;
3114 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003115 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003116 chip->ecc.size = mtd->writesize;
3117 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003121 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003122 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003123 BUG();
3124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003126 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003127 * The number of bytes available for a client to place data into
3128 * the out of band area
3129 */
3130 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003131 for (i = 0; chip->ecc.layout->oobfree[i].length
3132 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003133 chip->ecc.layout->oobavail +=
3134 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003135 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003136
3137 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003138 * Set the number of read / write steps for one page depending on ECC
3139 * mode
3140 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003141 chip->ecc.steps = mtd->writesize / chip->ecc.size;
3142 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003143 printk(KERN_WARNING "Invalid ecc parameters\n");
3144 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003146 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003147
Thomas Gleixner29072b92006-09-28 15:38:36 +02003148 /*
3149 * Allow subpage writes up to ecc.steps. Not possible for MLC
3150 * FLASH.
3151 */
3152 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3153 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3154 switch(chip->ecc.steps) {
3155 case 2:
3156 mtd->subpage_sft = 1;
3157 break;
3158 case 4:
3159 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003160 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003161 mtd->subpage_sft = 2;
3162 break;
3163 }
3164 }
3165 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3166
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003167 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003168 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003171 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003174 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* Fill in remaining MTD driver data */
3177 mtd->type = MTD_NANDFLASH;
Joern Engel5fa43392006-05-22 23:18:29 +02003178 mtd->flags = MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 mtd->erase = nand_erase;
3180 mtd->point = NULL;
3181 mtd->unpoint = NULL;
3182 mtd->read = nand_read;
3183 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003184 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 mtd->read_oob = nand_read_oob;
3186 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 mtd->sync = nand_sync;
3188 mtd->lock = NULL;
3189 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01003190 mtd->suspend = nand_suspend;
3191 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 mtd->block_isbad = nand_block_isbad;
3193 mtd->block_markbad = nand_block_markbad;
3194
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003195 /* propagate ecc.layout to mtd_info */
3196 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003198 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003199 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003203 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
Rusty Russella6e6abd2009-03-31 13:05:31 -06003206/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01003207 test if this is a module _anyway_ -- they'd have to try _really_ hard
3208 to call us from in-kernel code if the core NAND support is modular. */
3209#ifdef MODULE
3210#define caller_is_module() (1)
3211#else
3212#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003213 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003214#endif
3215
3216/**
3217 * nand_scan - [NAND Interface] Scan for the NAND device
3218 * @mtd: MTD device structure
3219 * @maxchips: Number of chips to scan for
3220 *
3221 * This fills out all the uninitialized function pointers
3222 * with the defaults.
3223 * The flash ID is read and the mtd/chip structures are
3224 * filled with the appropriate values.
3225 * The mtd->owner field must be set to the module of the caller
3226 *
3227 */
3228int nand_scan(struct mtd_info *mtd, int maxchips)
3229{
3230 int ret;
3231
3232 /* Many callers got this wrong, so check for it for a while... */
3233 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05303234 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3235 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003236 BUG();
3237 }
3238
David Woodhouse5e81e882010-02-26 18:32:56 +00003239 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003240 if (!ret)
3241 ret = nand_scan_tail(mtd);
3242 return ret;
3243}
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003246 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 * @mtd: MTD device structure
3248*/
David Woodhousee0c7d762006-05-13 18:07:53 +01003249void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003251 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253#ifdef CONFIG_MTD_PARTITIONS
3254 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01003255 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256#endif
3257 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01003258 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Jesper Juhlfa671642005-11-07 01:01:27 -08003260 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003261 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003262 if (!(chip->options & NAND_OWN_BUFFERS))
3263 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
Vimal Singh7d70f332010-02-08 15:50:49 +05303266EXPORT_SYMBOL_GPL(nand_lock);
3267EXPORT_SYMBOL_GPL(nand_unlock);
David Woodhousee0c7d762006-05-13 18:07:53 +01003268EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003269EXPORT_SYMBOL_GPL(nand_scan_ident);
3270EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01003271EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003272
3273static int __init nand_base_init(void)
3274{
3275 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3276 return 0;
3277}
3278
3279static void __exit nand_base_exit(void)
3280{
3281 led_trigger_unregister_simple(nand_led_trigger);
3282}
3283
3284module_init(nand_base_init);
3285module_exit(nand_base_exit);
3286
David Woodhousee0c7d762006-05-13 18:07:53 +01003287MODULE_LICENSE("GPL");
3288MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3289MODULE_DESCRIPTION("Generic NAND flash driver code");