blob: e6cf9aefef133674a13e31df4d68247f779a6a21 [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
Brian Norris30fe8112010-06-23 13:36:02 -0700350 if (chip->options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700351 ofs += mtd->erasesize - mtd->writesize;
352
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100353 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200356 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200358 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200361 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200364 if (chip->options & NAND_BUSWIDTH_16) {
365 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100366 page);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200367 bad = cpu_to_le16(chip->read_word(mtd));
368 if (chip->badblockpos & 0x1)
Vitaly Wool49196f32005-11-02 16:54:46 +0000369 bad >>= 8;
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200370 else
371 bad &= 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 } else {
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100373 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200374 bad = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000376
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200377 if (likely(chip->badblockbits == 8))
378 res = bad != 0xFF;
379 else
380 res = hweight8(bad) < chip->badblockbits;
381
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200382 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return res;
386}
387
388/**
389 * nand_default_block_markbad - [DEFAULT] mark a block bad
390 * @mtd: MTD device structure
391 * @ofs: offset from device start
392 *
393 * This is the default implementation, which can be overridden by
394 * a hardware specific driver.
395*/
396static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
397{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200398 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f22006-05-23 11:52:35 +0200399 uint8_t buf[2] = { 0, 0 };
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200400 int block, ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000401
Brian Norris30fe8112010-06-23 13:36:02 -0700402 if (chip->options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700403 ofs += mtd->erasesize - mtd->writesize;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400406 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200407 if (chip->bbt)
408 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Do we have a flash based bad block table ? */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200411 if (chip->options & NAND_USE_FLASH_BBT)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200412 ret = nand_update_bbt(mtd, ofs);
413 else {
414 /* We write two bytes, so we dont have to mess with 16 bit
415 * access
416 */
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300417 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200418 ofs += mtd->oobsize;
Ricard Wanderlöfff0dab62006-10-23 09:33:34 +0200419 chip->ops.len = chip->ops.ooblen = 2;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200420 chip->ops.datbuf = NULL;
421 chip->ops.oobbuf = buf;
422 chip->ops.ooboffs = chip->badblockpos & ~0x01;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000423
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200424 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300425 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200426 }
427 if (!ret)
428 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300429
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000433/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * nand_check_wp - [GENERIC] check if the chip is write protected
435 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000436 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000438 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100440static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200442 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200443
444 /* broken xD cards report WP despite being writable */
445 if (chip->options & NAND_BROKEN_XD)
446 return 0;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200449 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
450 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/**
454 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
455 * @mtd: MTD device structure
456 * @ofs: offset from device start
457 * @getchip: 0, if the chip is already selected
458 * @allowbbt: 1, if its allowed to access the bbt area
459 *
460 * Check, if the block is bad. Either by reading the bad block table or
461 * calling of the scan function.
462 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200463static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
464 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200466 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000467
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200468 if (!chip->bbt)
469 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100472 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200475/**
476 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
477 * @mtd: MTD device structure
478 * @timeo: Timeout
479 *
480 * Helper function for nand_wait_ready used when needing to wait in interrupt
481 * context.
482 */
483static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
484{
485 struct nand_chip *chip = mtd->priv;
486 int i;
487
488 /* Wait for the device to get ready */
489 for (i = 0; i < timeo; i++) {
490 if (chip->dev_ready(mtd))
491 break;
492 touch_softlockup_watchdog();
493 mdelay(1);
494 }
495}
496
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000497/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000498 * Wait for the ready pin, after a command
499 * The timeout is catched later.
500 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100501void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000502{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200503 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100504 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000505
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200506 /* 400ms timeout */
507 if (in_interrupt() || oops_in_progress)
508 return panic_nand_wait_ready(mtd, 400);
509
Richard Purdie8fe833c2006-03-31 02:31:14 -0800510 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000511 /* wait until command is processed or timeout occures */
512 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200513 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800514 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700515 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000516 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800517 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000518}
David Woodhouse4b648b02006-09-25 17:05:24 +0100519EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/**
522 * nand_command - [DEFAULT] Send command to NAND device
523 * @mtd: MTD device structure
524 * @command: the command to be sent
525 * @column: the column address for this command, -1 if none
526 * @page_addr: the page address for this command, -1 if none
527 *
528 * Send command to NAND device. This function is used for small page
529 * devices (256/512 Bytes per page)
530 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200531static void nand_command(struct mtd_info *mtd, unsigned int command,
532 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200534 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200535 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Write out the command to the device.
539 */
540 if (command == NAND_CMD_SEQIN) {
541 int readcmd;
542
Joern Engel28318772006-05-22 23:18:05 +0200543 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200545 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 readcmd = NAND_CMD_READOOB;
547 } else if (column < 256) {
548 /* First 256 bytes --> READ0 */
549 readcmd = NAND_CMD_READ0;
550 } else {
551 column -= 256;
552 readcmd = NAND_CMD_READ1;
553 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200554 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200555 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200557 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200559 /*
560 * Address cycle, when necessary
561 */
562 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
563 /* Serially input address */
564 if (column != -1) {
565 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200566 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200567 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200568 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200569 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200571 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200572 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200573 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200574 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200575 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200576 if (chip->chipsize > (32 << 20))
577 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200578 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200579 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000580
581 /*
582 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 case NAND_CMD_PAGEPROG:
588 case NAND_CMD_ERASE1:
589 case NAND_CMD_ERASE2:
590 case NAND_CMD_SEQIN:
591 case NAND_CMD_STATUS:
592 return;
593
594 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200595 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200597 udelay(chip->chip_delay);
598 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200599 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200600 chip->cmd_ctrl(mtd,
601 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return;
604
David Woodhousee0c7d762006-05-13 18:07:53 +0100605 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000607 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * If we don't have access to the busy pin, we apply the given
609 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100610 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200611 if (!chip->dev_ready) {
612 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Apply this short delay always to ensure that we do wait tWB in
617 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100618 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000619
620 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/**
624 * nand_command_lp - [DEFAULT] Send command to NAND large page device
625 * @mtd: MTD device structure
626 * @command: the command to be sent
627 * @column: the column address for this command, -1 if none
628 * @page_addr: the page address for this command, -1 if none
629 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200630 * Send command to NAND device. This is the version for the new large page
631 * devices We dont have the separate regions as we have in the small page
632 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200634static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
635 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200637 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* Emulate NAND_CMD_READOOB */
640 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200641 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 command = NAND_CMD_READ0;
643 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000644
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200645 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200646 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200650 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* Serially input address */
653 if (column != -1) {
654 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200657 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200658 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 chip->cmd_ctrl(mtd, page_addr, ctrl);
663 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200666 if (chip->chipsize > (128 << 20))
667 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200668 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200671 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672
673 /*
674 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000675 * status, sequential in, and deplete1 need no delay
676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 case NAND_CMD_CACHEDPROG:
680 case NAND_CMD_PAGEPROG:
681 case NAND_CMD_ERASE1:
682 case NAND_CMD_ERASE2:
683 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200684 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000686 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return;
688
David Woodhousee0c7d762006-05-13 18:07:53 +0100689 /*
690 * read error status commands require only a short delay
691 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000692 case NAND_CMD_STATUS_ERROR:
693 case NAND_CMD_STATUS_ERROR0:
694 case NAND_CMD_STATUS_ERROR1:
695 case NAND_CMD_STATUS_ERROR2:
696 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200697 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200701 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200703 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200704 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
705 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
706 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
707 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200708 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return;
710
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200711 case NAND_CMD_RNDOUT:
712 /* No ready / busy check necessary */
713 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
714 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
715 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
716 NAND_NCE | NAND_CTRL_CHANGE);
717 return;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200720 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
721 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
722 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
723 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000724
David Woodhousee0c7d762006-05-13 18:07:53 +0100725 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000727 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * If we don't have access to the busy pin, we apply the given
729 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100730 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200731 if (!chip->dev_ready) {
732 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* Apply this short delay always to ensure that we do wait tWB in
738 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100739 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000740
741 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200745 * panic_nand_get_device - [GENERIC] Get chip for selected access
746 * @chip: the nand chip descriptor
747 * @mtd: MTD device structure
748 * @new_state: the state which is requested
749 *
750 * Used when in panic, no locks are taken.
751 */
752static void panic_nand_get_device(struct nand_chip *chip,
753 struct mtd_info *mtd, int new_state)
754{
755 /* Hardware controller shared among independend devices */
756 chip->controller->active = chip;
757 chip->state = new_state;
758}
759
760/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700762 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * Get the device and lock it for exclusive access
767 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200768static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200769nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200771 spinlock_t *lock = &chip->controller->lock;
772 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100773 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100774 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100775 spin_lock(lock);
776
vimal singhb8b3ee92009-07-09 20:41:22 +0530777 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200778 if (!chip->controller->active)
779 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200780
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200781 if (chip->controller->active == chip && chip->state == FL_READY) {
782 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100783 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100784 return 0;
785 }
786 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800787 if (chip->controller->active->state == FL_PM_SUSPENDED) {
788 chip->state = FL_PM_SUSPENDED;
789 spin_unlock(lock);
790 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800791 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100792 }
793 set_current_state(TASK_UNINTERRUPTIBLE);
794 add_wait_queue(wq, &wait);
795 spin_unlock(lock);
796 schedule();
797 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto retry;
799}
800
801/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200802 * panic_nand_wait - [GENERIC] wait until the command is done
803 * @mtd: MTD device structure
804 * @chip: NAND chip structure
805 * @timeo: Timeout
806 *
807 * Wait for command done. This is a helper function for nand_wait used when
808 * we are in interrupt context. May happen when in panic and trying to write
809 * an oops trough mtdoops.
810 */
811static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
812 unsigned long timeo)
813{
814 int i;
815 for (i = 0; i < timeo; i++) {
816 if (chip->dev_ready) {
817 if (chip->dev_ready(mtd))
818 break;
819 } else {
820 if (chip->read_byte(mtd) & NAND_STATUS_READY)
821 break;
822 }
823 mdelay(1);
824 }
825}
826
827/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * nand_wait - [DEFAULT] wait until the command is done
829 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700830 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *
832 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000833 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700835 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200836static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838
David Woodhousee0c7d762006-05-13 18:07:53 +0100839 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200840 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100845 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Richard Purdie8fe833c2006-03-31 02:31:14 -0800847 led_trigger_event(nand_led_trigger, LED_FULL);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Apply this short delay always to ensure that we do wait tWB in
850 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100851 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200853 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
854 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200856 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200858 if (in_interrupt() || oops_in_progress)
859 panic_nand_wait(mtd, chip, timeo);
860 else {
861 while (time_before(jiffies, timeo)) {
862 if (chip->dev_ready) {
863 if (chip->dev_ready(mtd))
864 break;
865 } else {
866 if (chip->read_byte(mtd) & NAND_STATUS_READY)
867 break;
868 }
869 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800872 led_trigger_event(nand_led_trigger, LED_OFF);
873
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200874 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return status;
876}
877
878/**
Vimal Singh7d70f332010-02-08 15:50:49 +0530879 * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
880 *
881 * @param mtd - mtd info
882 * @param ofs - offset to start unlock from
883 * @param len - length to unlock
884 * @invert - when = 0, unlock the range of blocks within the lower and
885 * upper boundary address
886 * whne = 1, unlock the range of blocks outside the boundaries
887 * of the lower and upper boundary address
888 *
889 * @return - unlock status
890 */
891static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
892 uint64_t len, int invert)
893{
894 int ret = 0;
895 int status, page;
896 struct nand_chip *chip = mtd->priv;
897
898 /* Submit address of first page to unlock */
899 page = ofs >> chip->page_shift;
900 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
901
902 /* Submit address of last page to unlock */
903 page = (ofs + len) >> chip->page_shift;
904 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
905 (page | invert) & chip->pagemask);
906
907 /* Call wait ready function */
908 status = chip->waitfunc(mtd, chip);
909 udelay(1000);
910 /* See if device thinks it succeeded */
911 if (status & 0x01) {
912 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
913 __func__, status);
914 ret = -EIO;
915 }
916
917 return ret;
918}
919
920/**
921 * nand_unlock - [REPLACABLE] unlocks specified locked blockes
922 *
923 * @param mtd - mtd info
924 * @param ofs - offset to start unlock from
925 * @param len - length to unlock
926 *
927 * @return - unlock status
928 */
929int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
930{
931 int ret = 0;
932 int chipnr;
933 struct nand_chip *chip = mtd->priv;
934
935 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
936 __func__, (unsigned long long)ofs, len);
937
938 if (check_offs_len(mtd, ofs, len))
939 ret = -EINVAL;
940
941 /* Align to last block address if size addresses end of the device */
942 if (ofs + len == mtd->size)
943 len -= mtd->erasesize;
944
945 nand_get_device(chip, mtd, FL_UNLOCKING);
946
947 /* Shift to get chip number */
948 chipnr = ofs >> chip->chip_shift;
949
950 chip->select_chip(mtd, chipnr);
951
952 /* Check, if it is write protected */
953 if (nand_check_wp(mtd)) {
954 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
955 __func__);
956 ret = -EIO;
957 goto out;
958 }
959
960 ret = __nand_unlock(mtd, ofs, len, 0);
961
962out:
963 /* de-select the NAND device */
964 chip->select_chip(mtd, -1);
965
966 nand_release_device(mtd);
967
968 return ret;
969}
970
971/**
972 * nand_lock - [REPLACABLE] locks all blockes present in the device
973 *
974 * @param mtd - mtd info
975 * @param ofs - offset to start unlock from
976 * @param len - length to unlock
977 *
978 * @return - lock status
979 *
980 * This feature is not support in many NAND parts. 'Micron' NAND parts
981 * do have this feature, but it allows only to lock all blocks not for
982 * specified range for block.
983 *
984 * Implementing 'lock' feature by making use of 'unlock', for now.
985 */
986int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
987{
988 int ret = 0;
989 int chipnr, status, page;
990 struct nand_chip *chip = mtd->priv;
991
992 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
993 __func__, (unsigned long long)ofs, len);
994
995 if (check_offs_len(mtd, ofs, len))
996 ret = -EINVAL;
997
998 nand_get_device(chip, mtd, FL_LOCKING);
999
1000 /* Shift to get chip number */
1001 chipnr = ofs >> chip->chip_shift;
1002
1003 chip->select_chip(mtd, chipnr);
1004
1005 /* Check, if it is write protected */
1006 if (nand_check_wp(mtd)) {
1007 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
1008 __func__);
1009 status = MTD_ERASE_FAILED;
1010 ret = -EIO;
1011 goto out;
1012 }
1013
1014 /* Submit address of first page to lock */
1015 page = ofs >> chip->page_shift;
1016 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1017
1018 /* Call wait ready function */
1019 status = chip->waitfunc(mtd, chip);
1020 udelay(1000);
1021 /* See if device thinks it succeeded */
1022 if (status & 0x01) {
1023 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
1024 __func__, status);
1025 ret = -EIO;
1026 goto out;
1027 }
1028
1029 ret = __nand_unlock(mtd, ofs, len, 0x1);
1030
1031out:
1032 /* de-select the NAND device */
1033 chip->select_chip(mtd, -1);
1034
1035 nand_release_device(mtd);
1036
1037 return ret;
1038}
1039
1040/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001041 * nand_read_page_raw - [Intern] read raw page data without ecc
1042 * @mtd: mtd info structure
1043 * @chip: nand chip info structure
1044 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001045 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001046 *
1047 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001048 */
1049static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001050 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001051{
1052 chip->read_buf(mtd, buf, mtd->writesize);
1053 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1054 return 0;
1055}
1056
1057/**
David Brownell52ff49d2009-03-04 12:01:36 -08001058 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
1059 * @mtd: mtd info structure
1060 * @chip: nand chip info structure
1061 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001062 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001063 *
1064 * We need a special oob layout and handling even when OOB isn't used.
1065 */
1066static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001067 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001068{
1069 int eccsize = chip->ecc.size;
1070 int eccbytes = chip->ecc.bytes;
1071 uint8_t *oob = chip->oob_poi;
1072 int steps, size;
1073
1074 for (steps = chip->ecc.steps; steps > 0; steps--) {
1075 chip->read_buf(mtd, buf, eccsize);
1076 buf += eccsize;
1077
1078 if (chip->ecc.prepad) {
1079 chip->read_buf(mtd, oob, chip->ecc.prepad);
1080 oob += chip->ecc.prepad;
1081 }
1082
1083 chip->read_buf(mtd, oob, eccbytes);
1084 oob += eccbytes;
1085
1086 if (chip->ecc.postpad) {
1087 chip->read_buf(mtd, oob, chip->ecc.postpad);
1088 oob += chip->ecc.postpad;
1089 }
1090 }
1091
1092 size = mtd->oobsize - (oob - chip->oob_poi);
1093 if (size)
1094 chip->read_buf(mtd, oob, size);
1095
1096 return 0;
1097}
1098
1099/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001100 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001101 * @mtd: mtd info structure
1102 * @chip: nand chip info structure
1103 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001104 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001105 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001106static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001107 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001109 int i, eccsize = chip->ecc.size;
1110 int eccbytes = chip->ecc.bytes;
1111 int eccsteps = chip->ecc.steps;
1112 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001113 uint8_t *ecc_calc = chip->buffers->ecccalc;
1114 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001115 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001116
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001117 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001118
1119 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1120 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1121
1122 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001123 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001124
1125 eccsteps = chip->ecc.steps;
1126 p = buf;
1127
1128 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1129 int stat;
1130
1131 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001132 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001133 mtd->ecc_stats.failed++;
1134 else
1135 mtd->ecc_stats.corrected += stat;
1136 }
1137 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/**
Alexey Korolev3d459552008-05-15 17:23:18 +01001141 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
1142 * @mtd: mtd info structure
1143 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +01001144 * @data_offs: offset of requested data within the page
1145 * @readlen: data length
1146 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001147 */
1148static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
1149{
1150 int start_step, end_step, num_steps;
1151 uint32_t *eccpos = chip->ecc.layout->eccpos;
1152 uint8_t *p;
1153 int data_col_addr, i, gaps = 0;
1154 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1155 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1156
1157 /* Column address wihin the page aligned to ECC size (256bytes). */
1158 start_step = data_offs / chip->ecc.size;
1159 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1160 num_steps = end_step - start_step + 1;
1161
1162 /* Data size aligned to ECC ecc.size*/
1163 datafrag_len = num_steps * chip->ecc.size;
1164 eccfrag_len = num_steps * chip->ecc.bytes;
1165
1166 data_col_addr = start_step * chip->ecc.size;
1167 /* If we read not a page aligned data */
1168 if (data_col_addr != 0)
1169 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1170
1171 p = bufpoi + data_col_addr;
1172 chip->read_buf(mtd, p, datafrag_len);
1173
1174 /* Calculate ECC */
1175 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1176 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1177
1178 /* The performance is faster if to position offsets
1179 according to ecc.pos. Let make sure here that
1180 there are no gaps in ecc positions */
1181 for (i = 0; i < eccfrag_len - 1; i++) {
1182 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1183 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1184 gaps = 1;
1185 break;
1186 }
1187 }
1188 if (gaps) {
1189 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1190 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1191 } else {
1192 /* send the command to read the particular ecc bytes */
1193 /* take care about buswidth alignment in read_buf */
1194 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1195 aligned_len = eccfrag_len;
1196 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1197 aligned_len++;
1198 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1199 aligned_len++;
1200
1201 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1202 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1203 }
1204
1205 for (i = 0; i < eccfrag_len; i++)
1206 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1207
1208 p = bufpoi + data_col_addr;
1209 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1210 int stat;
1211
1212 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1213 if (stat == -1)
1214 mtd->ecc_stats.failed++;
1215 else
1216 mtd->ecc_stats.corrected += stat;
1217 }
1218 return 0;
1219}
1220
1221/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001222 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001223 * @mtd: mtd info structure
1224 * @chip: nand chip info structure
1225 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001226 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001227 *
1228 * Not for syndrome calculating ecc controllers which need a special oob layout
1229 */
1230static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001231 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001232{
1233 int i, eccsize = chip->ecc.size;
1234 int eccbytes = chip->ecc.bytes;
1235 int eccsteps = chip->ecc.steps;
1236 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001237 uint8_t *ecc_calc = chip->buffers->ecccalc;
1238 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001239 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001240
1241 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1242 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1243 chip->read_buf(mtd, p, eccsize);
1244 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1245 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001246 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001247
1248 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001249 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001250
1251 eccsteps = chip->ecc.steps;
1252 p = buf;
1253
1254 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1255 int stat;
1256
1257 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001258 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001259 mtd->ecc_stats.failed++;
1260 else
1261 mtd->ecc_stats.corrected += stat;
1262 }
1263 return 0;
1264}
1265
1266/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001267 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1268 * @mtd: mtd info structure
1269 * @chip: nand chip info structure
1270 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001271 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001272 *
1273 * Hardware ECC for large page chips, require OOB to be read first.
1274 * For this ECC mode, the write_page method is re-used from ECC_HW.
1275 * These methods read/write ECC from the OOB area, unlike the
1276 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1277 * "infix ECC" scheme and reads/writes ECC from the data area, by
1278 * overwriting the NAND manufacturer bad block markings.
1279 */
1280static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1281 struct nand_chip *chip, uint8_t *buf, int page)
1282{
1283 int i, eccsize = chip->ecc.size;
1284 int eccbytes = chip->ecc.bytes;
1285 int eccsteps = chip->ecc.steps;
1286 uint8_t *p = buf;
1287 uint8_t *ecc_code = chip->buffers->ecccode;
1288 uint32_t *eccpos = chip->ecc.layout->eccpos;
1289 uint8_t *ecc_calc = chip->buffers->ecccalc;
1290
1291 /* Read the OOB area first */
1292 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1293 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1294 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1295
1296 for (i = 0; i < chip->ecc.total; i++)
1297 ecc_code[i] = chip->oob_poi[eccpos[i]];
1298
1299 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1300 int stat;
1301
1302 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1303 chip->read_buf(mtd, p, eccsize);
1304 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1305
1306 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1307 if (stat < 0)
1308 mtd->ecc_stats.failed++;
1309 else
1310 mtd->ecc_stats.corrected += stat;
1311 }
1312 return 0;
1313}
1314
1315/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001316 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001317 * @mtd: mtd info structure
1318 * @chip: nand chip info structure
1319 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001320 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001321 *
1322 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001323 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001324 */
1325static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001326 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001327{
1328 int i, eccsize = chip->ecc.size;
1329 int eccbytes = chip->ecc.bytes;
1330 int eccsteps = chip->ecc.steps;
1331 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001332 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001333
1334 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1335 int stat;
1336
1337 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1338 chip->read_buf(mtd, p, eccsize);
1339
1340 if (chip->ecc.prepad) {
1341 chip->read_buf(mtd, oob, chip->ecc.prepad);
1342 oob += chip->ecc.prepad;
1343 }
1344
1345 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1346 chip->read_buf(mtd, oob, eccbytes);
1347 stat = chip->ecc.correct(mtd, p, oob, NULL);
1348
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001349 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001350 mtd->ecc_stats.failed++;
1351 else
1352 mtd->ecc_stats.corrected += stat;
1353
1354 oob += eccbytes;
1355
1356 if (chip->ecc.postpad) {
1357 chip->read_buf(mtd, oob, chip->ecc.postpad);
1358 oob += chip->ecc.postpad;
1359 }
1360 }
1361
1362 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001363 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001364 if (i)
1365 chip->read_buf(mtd, oob, i);
1366
1367 return 0;
1368}
1369
1370/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001371 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1372 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001373 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001374 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001375 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001376 */
1377static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001378 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001379{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001380 switch(ops->mode) {
1381
1382 case MTD_OOB_PLACE:
1383 case MTD_OOB_RAW:
1384 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1385 return oob + len;
1386
1387 case MTD_OOB_AUTO: {
1388 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001389 uint32_t boffs = 0, roffs = ops->ooboffs;
1390 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001391
1392 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001393 /* Read request not from offset 0 ? */
1394 if (unlikely(roffs)) {
1395 if (roffs >= free->length) {
1396 roffs -= free->length;
1397 continue;
1398 }
1399 boffs = free->offset + roffs;
1400 bytes = min_t(size_t, len,
1401 (free->length - roffs));
1402 roffs = 0;
1403 } else {
1404 bytes = min_t(size_t, len, free->length);
1405 boffs = free->offset;
1406 }
1407 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001408 oob += bytes;
1409 }
1410 return oob;
1411 }
1412 default:
1413 BUG();
1414 }
1415 return NULL;
1416}
1417
1418/**
1419 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001420 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001421 * @mtd: MTD device structure
1422 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001423 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001424 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001425 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001426 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001427static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1428 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001429{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001430 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001431 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001432 struct mtd_ecc_stats stats;
1433 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1434 int sndcmd = 1;
1435 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001436 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001437 uint32_t oobreadlen = ops->ooblen;
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001438 uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
1439 mtd->oobavail : mtd->oobsize;
1440
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001441 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001443 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001445 chipnr = (int)(from >> chip->chip_shift);
1446 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001448 realpage = (int)(from >> chip->page_shift);
1449 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001451 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001453 buf = ops->datbuf;
1454 oob = ops->oobbuf;
1455
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001456 while(1) {
1457 bytes = min(mtd->writesize - col, readlen);
1458 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001459
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001460 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001461 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001462 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001464 if (likely(sndcmd)) {
1465 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1466 sndcmd = 0;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001469 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001470 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001471 ret = chip->ecc.read_page_raw(mtd, chip,
1472 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001473 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1474 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001475 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001476 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1477 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001478 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001479 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001480
1481 /* Transfer not aligned data */
1482 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001483 if (!NAND_SUBPAGE_READ(chip) && !oob)
1484 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001485 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001487
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001488 buf += bytes;
1489
1490 if (unlikely(oob)) {
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001491
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001492 int toread = min(oobreadlen, max_oobsize);
1493
1494 if (toread) {
1495 oob = nand_transfer_oob(chip,
1496 oob, ops, toread);
1497 oobreadlen -= toread;
1498 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001499 }
1500
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001501 if (!(chip->options & NAND_NO_READRDY)) {
1502 /*
1503 * Apply delay or wait for ready/busy pin. Do
1504 * this before the AUTOINCR check, so no
1505 * problems arise if a chip which does auto
1506 * increment is marked as NOAUTOINCR by the
1507 * board driver.
1508 */
1509 if (!chip->dev_ready)
1510 udelay(chip->chip_delay);
1511 else
1512 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001514 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001515 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001516 buf += bytes;
1517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001519 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001520
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001521 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* For subsequent reads align to page boundary. */
1525 col = 0;
1526 /* Increment page address */
1527 realpage++;
1528
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001529 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* Check, if we cross a chip boundary */
1531 if (!page) {
1532 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001533 chip->select_chip(mtd, -1);
1534 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001536
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001537 /* Check, if the chip supports auto page increment
1538 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001539 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001540 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001541 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001544 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001545 if (oob)
1546 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001548 if (ret)
1549 return ret;
1550
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001551 if (mtd->ecc_stats.failed - stats.failed)
1552 return -EBADMSG;
1553
1554 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001555}
1556
1557/**
1558 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1559 * @mtd: MTD device structure
1560 * @from: offset to read from
1561 * @len: number of bytes to read
1562 * @retlen: pointer to variable to store the number of read bytes
1563 * @buf: the databuffer to put data
1564 *
1565 * Get hold of the chip and call nand_do_read
1566 */
1567static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1568 size_t *retlen, uint8_t *buf)
1569{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001570 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001571 int ret;
1572
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001573 /* Do not allow reads past end of device */
1574 if ((from + len) > mtd->size)
1575 return -EINVAL;
1576 if (!len)
1577 return 0;
1578
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001579 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001580
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001581 chip->ops.len = len;
1582 chip->ops.datbuf = buf;
1583 chip->ops.oobbuf = NULL;
1584
1585 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001586
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001587 *retlen = chip->ops.retlen;
1588
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001589 nand_release_device(mtd);
1590
1591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
1594/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001595 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1596 * @mtd: mtd info structure
1597 * @chip: nand chip info structure
1598 * @page: page number to read
1599 * @sndcmd: flag whether to issue read command or not
1600 */
1601static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1602 int page, int sndcmd)
1603{
1604 if (sndcmd) {
1605 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1606 sndcmd = 0;
1607 }
1608 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1609 return sndcmd;
1610}
1611
1612/**
1613 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1614 * with syndromes
1615 * @mtd: mtd info structure
1616 * @chip: nand chip info structure
1617 * @page: page number to read
1618 * @sndcmd: flag whether to issue read command or not
1619 */
1620static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1621 int page, int sndcmd)
1622{
1623 uint8_t *buf = chip->oob_poi;
1624 int length = mtd->oobsize;
1625 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1626 int eccsize = chip->ecc.size;
1627 uint8_t *bufpoi = buf;
1628 int i, toread, sndrnd = 0, pos;
1629
1630 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1631 for (i = 0; i < chip->ecc.steps; i++) {
1632 if (sndrnd) {
1633 pos = eccsize + i * (eccsize + chunk);
1634 if (mtd->writesize > 512)
1635 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1636 else
1637 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1638 } else
1639 sndrnd = 1;
1640 toread = min_t(int, length, chunk);
1641 chip->read_buf(mtd, bufpoi, toread);
1642 bufpoi += toread;
1643 length -= toread;
1644 }
1645 if (length > 0)
1646 chip->read_buf(mtd, bufpoi, length);
1647
1648 return 1;
1649}
1650
1651/**
1652 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1653 * @mtd: mtd info structure
1654 * @chip: nand chip info structure
1655 * @page: page number to write
1656 */
1657static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1658 int page)
1659{
1660 int status = 0;
1661 const uint8_t *buf = chip->oob_poi;
1662 int length = mtd->oobsize;
1663
1664 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1665 chip->write_buf(mtd, buf, length);
1666 /* Send command to program the OOB data */
1667 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1668
1669 status = chip->waitfunc(mtd, chip);
1670
Savin Zlobec0d420f92006-06-21 11:51:20 +02001671 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001672}
1673
1674/**
1675 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1676 * with syndrome - only for large page flash !
1677 * @mtd: mtd info structure
1678 * @chip: nand chip info structure
1679 * @page: page number to write
1680 */
1681static int nand_write_oob_syndrome(struct mtd_info *mtd,
1682 struct nand_chip *chip, int page)
1683{
1684 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1685 int eccsize = chip->ecc.size, length = mtd->oobsize;
1686 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1687 const uint8_t *bufpoi = chip->oob_poi;
1688
1689 /*
1690 * data-ecc-data-ecc ... ecc-oob
1691 * or
1692 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1693 */
1694 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1695 pos = steps * (eccsize + chunk);
1696 steps = 0;
1697 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001698 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001699
1700 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1701 for (i = 0; i < steps; i++) {
1702 if (sndcmd) {
1703 if (mtd->writesize <= 512) {
1704 uint32_t fill = 0xFFFFFFFF;
1705
1706 len = eccsize;
1707 while (len > 0) {
1708 int num = min_t(int, len, 4);
1709 chip->write_buf(mtd, (uint8_t *)&fill,
1710 num);
1711 len -= num;
1712 }
1713 } else {
1714 pos = eccsize + i * (eccsize + chunk);
1715 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1716 }
1717 } else
1718 sndcmd = 1;
1719 len = min_t(int, length, chunk);
1720 chip->write_buf(mtd, bufpoi, len);
1721 bufpoi += len;
1722 length -= len;
1723 }
1724 if (length > 0)
1725 chip->write_buf(mtd, bufpoi, length);
1726
1727 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1728 status = chip->waitfunc(mtd, chip);
1729
1730 return status & NAND_STATUS_FAIL ? -EIO : 0;
1731}
1732
1733/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001734 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * @mtd: MTD device structure
1736 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001737 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 *
1739 * NAND read out-of-band data from the spare area
1740 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001741static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1742 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001744 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001745 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001746 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001747 int readlen = ops->ooblen;
1748 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001749 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
vimal singh20d8e242009-07-07 15:49:49 +05301751 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1752 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Adrian Hunter03736152007-01-31 17:58:29 +02001754 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001755 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001756 else
1757 len = mtd->oobsize;
1758
1759 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301760 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1761 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001762 return -EINVAL;
1763 }
1764
1765 /* Do not allow reads past end of device */
1766 if (unlikely(from >= mtd->size ||
1767 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1768 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301769 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1770 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001771 return -EINVAL;
1772 }
Vitaly Wool70145682006-11-03 18:20:38 +03001773
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001774 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001775 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001777 /* Shift to get page */
1778 realpage = (int)(from >> chip->page_shift);
1779 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001781 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001782 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001783
1784 len = min(len, readlen);
1785 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001786
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001787 if (!(chip->options & NAND_NO_READRDY)) {
1788 /*
1789 * Apply delay or wait for ready/busy pin. Do this
1790 * before the AUTOINCR check, so no problems arise if a
1791 * chip which does auto increment is marked as
1792 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001793 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001794 if (!chip->dev_ready)
1795 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001796 else
1797 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001799
Vitaly Wool70145682006-11-03 18:20:38 +03001800 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001801 if (!readlen)
1802 break;
1803
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001804 /* Increment page address */
1805 realpage++;
1806
1807 page = realpage & chip->pagemask;
1808 /* Check, if we cross a chip boundary */
1809 if (!page) {
1810 chipnr++;
1811 chip->select_chip(mtd, -1);
1812 chip->select_chip(mtd, chipnr);
1813 }
1814
1815 /* Check, if the chip supports auto page increment
1816 * or if we have hit a block boundary.
1817 */
1818 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1819 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821
Vitaly Wool70145682006-11-03 18:20:38 +03001822 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return 0;
1824}
1825
1826/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001827 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001830 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001834static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1835 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001837 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001838 int ret = -ENOTSUPP;
1839
1840 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001843 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301844 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1845 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return -EINVAL;
1847 }
1848
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001849 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851 switch(ops->mode) {
1852 case MTD_OOB_PLACE:
1853 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001854 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001855 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001856
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001857 default:
1858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001861 if (!ops->datbuf)
1862 ret = nand_do_read_oob(mtd, from, ops);
1863 else
1864 ret = nand_do_read_ops(mtd, from, ops);
1865
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001866 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 return ret;
1869}
1870
1871
1872/**
1873 * nand_write_page_raw - [Intern] raw page write function
1874 * @mtd: mtd info structure
1875 * @chip: nand chip info structure
1876 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001877 *
1878 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001879 */
1880static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1881 const uint8_t *buf)
1882{
1883 chip->write_buf(mtd, buf, mtd->writesize);
1884 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001887/**
David Brownell52ff49d2009-03-04 12:01:36 -08001888 * nand_write_page_raw_syndrome - [Intern] raw page write function
1889 * @mtd: mtd info structure
1890 * @chip: nand chip info structure
1891 * @buf: data buffer
1892 *
1893 * We need a special oob layout and handling even when ECC isn't checked.
1894 */
1895static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1896 const uint8_t *buf)
1897{
1898 int eccsize = chip->ecc.size;
1899 int eccbytes = chip->ecc.bytes;
1900 uint8_t *oob = chip->oob_poi;
1901 int steps, size;
1902
1903 for (steps = chip->ecc.steps; steps > 0; steps--) {
1904 chip->write_buf(mtd, buf, eccsize);
1905 buf += eccsize;
1906
1907 if (chip->ecc.prepad) {
1908 chip->write_buf(mtd, oob, chip->ecc.prepad);
1909 oob += chip->ecc.prepad;
1910 }
1911
1912 chip->read_buf(mtd, oob, eccbytes);
1913 oob += eccbytes;
1914
1915 if (chip->ecc.postpad) {
1916 chip->write_buf(mtd, oob, chip->ecc.postpad);
1917 oob += chip->ecc.postpad;
1918 }
1919 }
1920
1921 size = mtd->oobsize - (oob - chip->oob_poi);
1922 if (size)
1923 chip->write_buf(mtd, oob, size);
1924}
1925/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001926 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001927 * @mtd: mtd info structure
1928 * @chip: nand chip info structure
1929 * @buf: data buffer
1930 */
1931static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1932 const uint8_t *buf)
1933{
1934 int i, eccsize = chip->ecc.size;
1935 int eccbytes = chip->ecc.bytes;
1936 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001937 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001938 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001939 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001940
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941 /* Software ecc calculation */
1942 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1943 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 for (i = 0; i < chip->ecc.total; i++)
1946 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001947
Thomas Gleixner90424de2007-04-05 11:44:05 +02001948 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001949}
1950
1951/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001952 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001953 * @mtd: mtd info structure
1954 * @chip: nand chip info structure
1955 * @buf: data buffer
1956 */
1957static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1958 const uint8_t *buf)
1959{
1960 int i, eccsize = chip->ecc.size;
1961 int eccbytes = chip->ecc.bytes;
1962 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001963 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001964 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001965 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001966
1967 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1968 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001969 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001970 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1971 }
1972
1973 for (i = 0; i < chip->ecc.total; i++)
1974 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1975
1976 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1977}
1978
1979/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001980 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001981 * @mtd: mtd info structure
1982 * @chip: nand chip info structure
1983 * @buf: data buffer
1984 *
1985 * The hw generator calculates the error syndrome automatically. Therefor
1986 * we need a special oob layout and handling.
1987 */
1988static void nand_write_page_syndrome(struct mtd_info *mtd,
1989 struct nand_chip *chip, const uint8_t *buf)
1990{
1991 int i, eccsize = chip->ecc.size;
1992 int eccbytes = chip->ecc.bytes;
1993 int eccsteps = chip->ecc.steps;
1994 const uint8_t *p = buf;
1995 uint8_t *oob = chip->oob_poi;
1996
1997 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1998
1999 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2000 chip->write_buf(mtd, p, eccsize);
2001
2002 if (chip->ecc.prepad) {
2003 chip->write_buf(mtd, oob, chip->ecc.prepad);
2004 oob += chip->ecc.prepad;
2005 }
2006
2007 chip->ecc.calculate(mtd, p, oob);
2008 chip->write_buf(mtd, oob, eccbytes);
2009 oob += eccbytes;
2010
2011 if (chip->ecc.postpad) {
2012 chip->write_buf(mtd, oob, chip->ecc.postpad);
2013 oob += chip->ecc.postpad;
2014 }
2015 }
2016
2017 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002018 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002019 if (i)
2020 chip->write_buf(mtd, oob, i);
2021}
2022
2023/**
David Woodhouse956e9442006-09-25 17:12:39 +01002024 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002025 * @mtd: MTD device structure
2026 * @chip: NAND chip descriptor
2027 * @buf: the data to write
2028 * @page: page number to write
2029 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02002030 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002031 */
2032static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01002033 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002034{
2035 int status;
2036
2037 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2038
David Woodhouse956e9442006-09-25 17:12:39 +01002039 if (unlikely(raw))
2040 chip->ecc.write_page_raw(mtd, chip, buf);
2041 else
2042 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002043
2044 /*
2045 * Cached progamming disabled for now, Not sure if its worth the
2046 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
2047 */
2048 cached = 0;
2049
2050 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2051
2052 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002053 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002054 /*
2055 * See if operation failed and additional status checks are
2056 * available
2057 */
2058 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2059 status = chip->errstat(mtd, chip, FL_WRITING, status,
2060 page);
2061
2062 if (status & NAND_STATUS_FAIL)
2063 return -EIO;
2064 } else {
2065 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002066 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002067 }
2068
2069#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2070 /* Send command to read back the data */
2071 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2072
2073 if (chip->verify_buf(mtd, buf, mtd->writesize))
2074 return -EIO;
2075#endif
2076 return 0;
2077}
2078
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002079/**
2080 * nand_fill_oob - [Internal] Transfer client buffer to oob
2081 * @chip: nand chip structure
2082 * @oob: oob data buffer
2083 * @ops: oob ops structure
2084 */
Maxim Levitsky782ce792010-02-22 20:39:36 +02002085static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
2086 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002087{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002088 switch(ops->mode) {
2089
2090 case MTD_OOB_PLACE:
2091 case MTD_OOB_RAW:
2092 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2093 return oob + len;
2094
2095 case MTD_OOB_AUTO: {
2096 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002097 uint32_t boffs = 0, woffs = ops->ooboffs;
2098 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002099
2100 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002101 /* Write request not from offset 0 ? */
2102 if (unlikely(woffs)) {
2103 if (woffs >= free->length) {
2104 woffs -= free->length;
2105 continue;
2106 }
2107 boffs = free->offset + woffs;
2108 bytes = min_t(size_t, len,
2109 (free->length - woffs));
2110 woffs = 0;
2111 } else {
2112 bytes = min_t(size_t, len, free->length);
2113 boffs = free->offset;
2114 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002115 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002116 oob += bytes;
2117 }
2118 return oob;
2119 }
2120 default:
2121 BUG();
2122 }
2123 return NULL;
2124}
2125
Thomas Gleixner29072b92006-09-28 15:38:36 +02002126#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002127
2128/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002129 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002130 * @mtd: MTD device structure
2131 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002132 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002133 *
2134 * NAND write with ECC
2135 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002136static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2137 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002138{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002139 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002140 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002141 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002142
2143 uint32_t oobwritelen = ops->ooblen;
2144 uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
2145 mtd->oobavail : mtd->oobsize;
2146
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002147 uint8_t *oob = ops->oobbuf;
2148 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002149 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002150
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002151 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002152 if (!writelen)
2153 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154
2155 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002156 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302157 printk(KERN_NOTICE "%s: Attempt to write not "
2158 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002159 return -EINVAL;
2160 }
2161
Thomas Gleixner29072b92006-09-28 15:38:36 +02002162 column = to & (mtd->writesize - 1);
2163 subpage = column || (writelen & (mtd->writesize - 1));
2164
2165 if (subpage && oob)
2166 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002167
Thomas Gleixner6a930962006-06-28 00:11:45 +02002168 chipnr = (int)(to >> chip->chip_shift);
2169 chip->select_chip(mtd, chipnr);
2170
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002171 /* Check, if it is write protected */
2172 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002173 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002174
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002175 realpage = (int)(to >> chip->page_shift);
2176 page = realpage & chip->pagemask;
2177 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2178
2179 /* Invalidate the page cache, when we write to the cached page */
2180 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002181 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002182 chip->pagebuf = -1;
2183
David Woodhouse7dcdcbe2006-10-21 17:09:53 +01002184 /* If we're not given explicit OOB data, let it be 0xFF */
2185 if (likely(!oob))
2186 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002187
Maxim Levitsky782ce792010-02-22 20:39:36 +02002188 /* Don't allow multipage oob writes with offset */
2189 if (ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2190 return -EINVAL;
2191
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002192 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002193 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002194 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002195 uint8_t *wbuf = buf;
2196
2197 /* Partial page write ? */
2198 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2199 cached = 0;
2200 bytes = min_t(int, bytes - column, (int) writelen);
2201 chip->pagebuf = -1;
2202 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2203 memcpy(&chip->buffers->databuf[column], buf, bytes);
2204 wbuf = chip->buffers->databuf;
2205 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002206
Maxim Levitsky782ce792010-02-22 20:39:36 +02002207 if (unlikely(oob)) {
2208 size_t len = min(oobwritelen, oobmaxlen);
2209 oob = nand_fill_oob(chip, oob, len, ops);
2210 oobwritelen -= len;
2211 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002212
Thomas Gleixner29072b92006-09-28 15:38:36 +02002213 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01002214 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002215 if (ret)
2216 break;
2217
2218 writelen -= bytes;
2219 if (!writelen)
2220 break;
2221
Thomas Gleixner29072b92006-09-28 15:38:36 +02002222 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002223 buf += bytes;
2224 realpage++;
2225
2226 page = realpage & chip->pagemask;
2227 /* Check, if we cross a chip boundary */
2228 if (!page) {
2229 chipnr++;
2230 chip->select_chip(mtd, -1);
2231 chip->select_chip(mtd, chipnr);
2232 }
2233 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002234
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002235 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002236 if (unlikely(oob))
2237 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002238 return ret;
2239}
2240
2241/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002242 * panic_nand_write - [MTD Interface] NAND write with ECC
2243 * @mtd: MTD device structure
2244 * @to: offset to write to
2245 * @len: number of bytes to write
2246 * @retlen: pointer to variable to store the number of written bytes
2247 * @buf: the data to write
2248 *
2249 * NAND write with ECC. Used when performing writes in interrupt context, this
2250 * may for example be called by mtdoops when writing an oops while in panic.
2251 */
2252static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2253 size_t *retlen, const uint8_t *buf)
2254{
2255 struct nand_chip *chip = mtd->priv;
2256 int ret;
2257
2258 /* Do not allow reads past end of device */
2259 if ((to + len) > mtd->size)
2260 return -EINVAL;
2261 if (!len)
2262 return 0;
2263
2264 /* Wait for the device to get ready. */
2265 panic_nand_wait(mtd, chip, 400);
2266
2267 /* Grab the device. */
2268 panic_nand_get_device(chip, mtd, FL_WRITING);
2269
2270 chip->ops.len = len;
2271 chip->ops.datbuf = (uint8_t *)buf;
2272 chip->ops.oobbuf = NULL;
2273
2274 ret = nand_do_write_ops(mtd, to, &chip->ops);
2275
2276 *retlen = chip->ops.retlen;
2277 return ret;
2278}
2279
2280/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002281 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 * @mtd: MTD device structure
2283 * @to: offset to write to
2284 * @len: number of bytes to write
2285 * @retlen: pointer to variable to store the number of written bytes
2286 * @buf: the data to write
2287 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002288 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002290static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002291 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002293 struct nand_chip *chip = mtd->priv;
2294 int ret;
2295
2296 /* Do not allow reads past end of device */
2297 if ((to + len) > mtd->size)
2298 return -EINVAL;
2299 if (!len)
2300 return 0;
2301
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002302 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002303
2304 chip->ops.len = len;
2305 chip->ops.datbuf = (uint8_t *)buf;
2306 chip->ops.oobbuf = NULL;
2307
2308 ret = nand_do_write_ops(mtd, to, &chip->ops);
2309
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002310 *retlen = chip->ops.retlen;
2311
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002312 nand_release_device(mtd);
2313
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002314 return ret;
2315}
2316
2317/**
2318 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2319 * @mtd: MTD device structure
2320 * @to: offset to write to
2321 * @ops: oob operation description structure
2322 *
2323 * NAND write out-of-band
2324 */
2325static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2326 struct mtd_oob_ops *ops)
2327{
Adrian Hunter03736152007-01-31 17:58:29 +02002328 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002329 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
vimal singh20d8e242009-07-07 15:49:49 +05302331 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2332 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Adrian Hunter03736152007-01-31 17:58:29 +02002334 if (ops->mode == MTD_OOB_AUTO)
2335 len = chip->ecc.layout->oobavail;
2336 else
2337 len = mtd->oobsize;
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002340 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302341 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2342 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return -EINVAL;
2344 }
2345
Adrian Hunter03736152007-01-31 17:58:29 +02002346 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302347 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2348 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002349 return -EINVAL;
2350 }
2351
2352 /* Do not allow reads past end of device */
2353 if (unlikely(to >= mtd->size ||
2354 ops->ooboffs + ops->ooblen >
2355 ((mtd->size >> chip->page_shift) -
2356 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302357 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2358 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002359 return -EINVAL;
2360 }
2361
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002362 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002363 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002365 /* Shift to get page */
2366 page = (int)(to >> chip->page_shift);
2367
2368 /*
2369 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2370 * of my DiskOnChip 2000 test units) will clear the whole data page too
2371 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2372 * it in the doc2000 driver in August 1999. dwmw2.
2373 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002374 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /* Check, if it is write protected */
2377 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002378 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002381 if (page == chip->pagebuf)
2382 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002384 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002385 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002386 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2387 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002388
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002389 if (status)
2390 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Vitaly Wool70145682006-11-03 18:20:38 +03002392 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002394 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002397/**
2398 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2399 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002400 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002401 * @ops: oob operation description structure
2402 */
2403static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2404 struct mtd_oob_ops *ops)
2405{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002406 struct nand_chip *chip = mtd->priv;
2407 int ret = -ENOTSUPP;
2408
2409 ops->retlen = 0;
2410
2411 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002412 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302413 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2414 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002415 return -EINVAL;
2416 }
2417
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002418 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002419
2420 switch(ops->mode) {
2421 case MTD_OOB_PLACE:
2422 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002423 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002424 break;
2425
2426 default:
2427 goto out;
2428 }
2429
2430 if (!ops->datbuf)
2431 ret = nand_do_write_oob(mtd, to, ops);
2432 else
2433 ret = nand_do_write_ops(mtd, to, ops);
2434
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002435 out:
2436 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return ret;
2438}
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2442 * @mtd: MTD device structure
2443 * @page: the page address of the block which will be erased
2444 *
2445 * Standard erase command for NAND chips
2446 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002447static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002449 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002451 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2452 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455/**
2456 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2457 * @mtd: MTD device structure
2458 * @page: the page address of the block which will be erased
2459 *
2460 * AND multi block erase command function
2461 * Erase 4 consecutive blocks
2462 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002463static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002465 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002467 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2468 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2469 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2470 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2471 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474/**
2475 * nand_erase - [MTD Interface] erase block(s)
2476 * @mtd: MTD device structure
2477 * @instr: erase instruction
2478 *
2479 * Erase one ore more blocks
2480 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002481static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
David Woodhousee0c7d762006-05-13 18:07:53 +01002483 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002485
David A. Marlin30f464b2005-01-17 18:35:25 +00002486#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002488 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 * @mtd: MTD device structure
2490 * @instr: erase instruction
2491 * @allowbbt: allow erasing the bbt area
2492 *
2493 * Erase one ore more blocks
2494 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002495int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2496 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Adrian Hunter69423d92008-12-10 13:37:21 +00002498 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002499 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002500 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002501 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002502 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
vimal singh20d8e242009-07-07 15:49:49 +05302504 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2505 __func__, (unsigned long long)instr->addr,
2506 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302508 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002511 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002514 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002517 page = (int)(instr->addr >> chip->page_shift);
2518 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002521 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002524 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* Check, if it is write protected */
2527 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302528 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2529 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 instr->state = MTD_ERASE_FAILED;
2531 goto erase_exit;
2532 }
2533
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002534 /*
2535 * If BBT requires refresh, set the BBT page mask to see if the BBT
2536 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2537 * can not be matched. This is also done when the bbt is actually
2538 * erased to avoid recusrsive updates
2539 */
2540 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2541 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /* Loop through the pages */
2544 len = instr->len;
2545
2546 instr->state = MTD_ERASING;
2547
2548 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002549 /*
2550 * heck if we have a bad block, we do not erase bad blocks !
2551 */
2552 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2553 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302554 printk(KERN_WARNING "%s: attempt to erase a bad block "
2555 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 instr->state = MTD_ERASE_FAILED;
2557 goto erase_exit;
2558 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002559
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002560 /*
2561 * Invalidate the page cache, if we erase the block which
2562 * contains the current cached page
2563 */
2564 if (page <= chip->pagebuf && chip->pagebuf <
2565 (page + pages_per_block))
2566 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002568 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002569
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002570 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002572 /*
2573 * See if operation failed and additional status checks are
2574 * available
2575 */
2576 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2577 status = chip->errstat(mtd, chip, FL_ERASING,
2578 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002581 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302582 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2583 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002585 instr->fail_addr =
2586 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 goto erase_exit;
2588 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002589
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002590 /*
2591 * If BBT requires refresh, set the BBT rewrite flag to the
2592 * page being erased
2593 */
2594 if (bbt_masked_page != 0xffffffff &&
2595 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002596 rewrite_bbt[chipnr] =
2597 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002600 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 page += pages_per_block;
2602
2603 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002604 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002606 chip->select_chip(mtd, -1);
2607 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002608
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002609 /*
2610 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2611 * page mask to see if this BBT should be rewritten
2612 */
2613 if (bbt_masked_page != 0xffffffff &&
2614 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2615 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2616 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 }
2619 instr->state = MTD_ERASE_DONE;
2620
David Woodhousee0c7d762006-05-13 18:07:53 +01002621 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 /* Deselect and wake up anyone waiting on the device */
2626 nand_release_device(mtd);
2627
David Woodhouse49defc02007-10-06 15:01:59 -04002628 /* Do call back function */
2629 if (!ret)
2630 mtd_erase_callback(instr);
2631
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002632 /*
2633 * If BBT requires refresh and erase was successful, rewrite any
2634 * selected bad block tables
2635 */
2636 if (bbt_masked_page == 0xffffffff || ret)
2637 return ret;
2638
2639 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2640 if (!rewrite_bbt[chipnr])
2641 continue;
2642 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302643 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2644 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2645 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002646 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002647 }
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /* Return more or less happy */
2650 return ret;
2651}
2652
2653/**
2654 * nand_sync - [MTD Interface] sync
2655 * @mtd: MTD device structure
2656 *
2657 * Sync is actually a wait for chip ready function
2658 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002659static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002661 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
vimal singh20d8e242009-07-07 15:49:49 +05302663 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002666 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002668 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002672 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002674 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002676static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
2678 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002679 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002681
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002682 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
2685/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002686 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 * @mtd: MTD device structure
2688 * @ofs: offset relative to mtd start
2689 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002690static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002692 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int ret;
2694
David Woodhousee0c7d762006-05-13 18:07:53 +01002695 if ((ret = nand_block_isbad(mtd, ofs))) {
2696 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (ret > 0)
2698 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002699 return ret;
2700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002702 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
2705/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002706 * nand_suspend - [MTD Interface] Suspend the NAND flash
2707 * @mtd: MTD device structure
2708 */
2709static int nand_suspend(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 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002714}
2715
2716/**
2717 * nand_resume - [MTD Interface] Resume the NAND flash
2718 * @mtd: MTD device structure
2719 */
2720static void nand_resume(struct mtd_info *mtd)
2721{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002722 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002723
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002724 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002725 nand_release_device(mtd);
2726 else
vimal singh20d8e242009-07-07 15:49:49 +05302727 printk(KERN_ERR "%s called for a chip which is not "
2728 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002729}
2730
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002731/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002732 * Set default functions
2733 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002734static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002737 if (!chip->chip_delay)
2738 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002741 if (chip->cmdfunc == NULL)
2742 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002745 if (chip->waitfunc == NULL)
2746 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002748 if (!chip->select_chip)
2749 chip->select_chip = nand_select_chip;
2750 if (!chip->read_byte)
2751 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2752 if (!chip->read_word)
2753 chip->read_word = nand_read_word;
2754 if (!chip->block_bad)
2755 chip->block_bad = nand_block_bad;
2756 if (!chip->block_markbad)
2757 chip->block_markbad = nand_default_block_markbad;
2758 if (!chip->write_buf)
2759 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2760 if (!chip->read_buf)
2761 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2762 if (!chip->verify_buf)
2763 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2764 if (!chip->scan_bbt)
2765 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002766
2767 if (!chip->controller) {
2768 chip->controller = &chip->hwcontrol;
2769 spin_lock_init(&chip->controller->lock);
2770 init_waitqueue_head(&chip->controller->wq);
2771 }
2772
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002773}
2774
2775/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002776 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002777 */
2778static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002779 struct nand_chip *chip,
David Woodhouse5e81e882010-02-26 18:32:56 +00002780 int busw, int *maf_id,
2781 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002782{
Kevin Cernekee426c4572010-05-04 20:58:03 -07002783 int i, dev_id, maf_idx;
2784 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002787 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Karl Beldanef89a882008-09-15 14:37:29 +02002789 /*
2790 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2791 * after power-up
2792 */
2793 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002796 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002799 *maf_id = chip->read_byte(mtd);
2800 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Ben Dooksed8165c2008-04-14 14:58:58 +01002802 /* Try again to make sure, as some systems the bus-hold or other
2803 * interface concerns can cause random data which looks like a
2804 * possibly credible NAND flash to appear. If the two results do
2805 * not match, ignore the device completely.
2806 */
2807
2808 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2809
Kevin Cernekee426c4572010-05-04 20:58:03 -07002810 /* Read entire ID string */
Ben Dooksed8165c2008-04-14 14:58:58 +01002811
Kevin Cernekee426c4572010-05-04 20:58:03 -07002812 for (i = 0; i < 8; i++)
2813 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01002814
Kevin Cernekee426c4572010-05-04 20:58:03 -07002815 if (id_data[0] != *maf_id || id_data[1] != dev_id) {
Ben Dooksed8165c2008-04-14 14:58:58 +01002816 printk(KERN_INFO "%s: second ID read did not match "
2817 "%02x,%02x against %02x,%02x\n", __func__,
Kevin Cernekee426c4572010-05-04 20:58:03 -07002818 *maf_id, dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01002819 return ERR_PTR(-ENODEV);
2820 }
2821
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002822 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00002823 type = nand_flash_ids;
2824
2825 for (; type->name != NULL; type++)
2826 if (dev_id == type->id)
2827 break;
2828
2829 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002830 return ERR_PTR(-ENODEV);
2831
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002832 if (!mtd->name)
2833 mtd->name = type->name;
2834
Adrian Hunter69423d92008-12-10 13:37:21 +00002835 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002836
2837 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002838 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002839 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002840 /* The 3rd id byte holds MLC / multichip data */
Kevin Cernekee426c4572010-05-04 20:58:03 -07002841 chip->cellinfo = id_data[2];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002842 /* The 4th id byte is the important one */
Kevin Cernekee426c4572010-05-04 20:58:03 -07002843 extid = id_data[3];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002844
Kevin Cernekee426c4572010-05-04 20:58:03 -07002845 /*
2846 * Field definitions are in the following datasheets:
2847 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2848 * New style (6 byte ID): Samsung K9GAG08U0D (p.40)
2849 *
2850 * Check for wraparound + Samsung ID + nonzero 6th byte
2851 * to decide what to do.
2852 */
2853 if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
2854 id_data[0] == NAND_MFR_SAMSUNG &&
2855 id_data[5] != 0x00) {
2856 /* Calc pagesize */
2857 mtd->writesize = 2048 << (extid & 0x03);
2858 extid >>= 2;
2859 /* Calc oobsize */
2860 mtd->oobsize = (extid & 0x03) == 0x01 ? 128 : 218;
2861 extid >>= 2;
2862 /* Calc blocksize */
2863 mtd->erasesize = (128 * 1024) <<
2864 (((extid >> 1) & 0x04) | (extid & 0x03));
2865 busw = 0;
2866 } else {
2867 /* Calc pagesize */
2868 mtd->writesize = 1024 << (extid & 0x03);
2869 extid >>= 2;
2870 /* Calc oobsize */
2871 mtd->oobsize = (8 << (extid & 0x01)) *
2872 (mtd->writesize >> 9);
2873 extid >>= 2;
2874 /* Calc blocksize. Blocksize is multiples of 64KiB */
2875 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2876 extid >>= 2;
2877 /* Get buswidth information */
2878 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2879 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002880 } else {
2881 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002882 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002883 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002884 mtd->erasesize = type->erasesize;
2885 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002886 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002887 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002888 }
2889
2890 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002891 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002892 if (nand_manuf_ids[maf_idx].id == *maf_id)
2893 break;
2894 }
2895
2896 /*
2897 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002898 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002899 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002900 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002901 printk(KERN_INFO "NAND device: Manufacturer ID:"
2902 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2903 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2904 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002905 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002906 busw ? 16 : 8);
2907 return ERR_PTR(-EINVAL);
2908 }
2909
2910 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002911 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002912 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002913 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002914
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002915 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002916 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002917 if (chip->chipsize & 0xffffffff)
2918 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2919 else
2920 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002921
2922 /* Set the bad block position */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002923 chip->badblockpos = mtd->writesize > 512 ?
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002924 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
Maxim Levitskye0b58d02010-02-22 20:39:38 +02002925 chip->badblockbits = 8;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002926
2927 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002928 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002929 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002930
2931 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002932 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002933 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002934 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002935
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002936 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002937 * options for chips which are not having an extended id.
2938 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002939 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002940 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002941
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07002942 /*
2943 * Bad block marker is stored in the last page of each block
2944 * on Samsung and Hynix MLC devices
2945 */
2946 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2947 (*maf_id == NAND_MFR_SAMSUNG ||
2948 *maf_id == NAND_MFR_HYNIX))
Brian Norris30fe8112010-06-23 13:36:02 -07002949 chip->options |= NAND_BBT_SCANLASTPAGE;
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07002950
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002951 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002952 if (chip->options & NAND_4PAGE_ARRAY)
2953 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002954 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002955 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002956
2957 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002958 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2959 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002960
2961 printk(KERN_INFO "NAND device: Manufacturer ID:"
2962 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2963 nand_manuf_ids[maf_idx].name, type->name);
2964
2965 return type;
2966}
2967
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002968/**
David Woodhouse3b85c322006-09-25 17:06:53 +01002969 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2970 * @mtd: MTD device structure
2971 * @maxchips: Number of chips to scan for
David Woodhouse5e81e882010-02-26 18:32:56 +00002972 * @table: Alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002973 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002974 * This is the first phase of the normal nand_scan() function. It
2975 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002976 *
David Woodhouse3b85c322006-09-25 17:06:53 +01002977 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002978 */
David Woodhouse5e81e882010-02-26 18:32:56 +00002979int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2980 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002981{
2982 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002983 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002984 struct nand_flash_dev *type;
2985
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002986 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002987 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002988 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002989 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002990
2991 /* Read the flash type */
David Woodhouse5e81e882010-02-26 18:32:56 +00002992 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002993
2994 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00002995 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
2996 printk(KERN_WARNING "No NAND device found.\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002997 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002998 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
3000
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003001 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003002 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003003 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003004 /* See comment in nand_get_flash_type for reset */
3005 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003007 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003009 if (nand_maf_id != chip->read_byte(mtd) ||
3010 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 break;
3012 }
3013 if (i > 1)
3014 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003017 chip->numchips = i;
3018 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
David Woodhouse3b85c322006-09-25 17:06:53 +01003020 return 0;
3021}
3022
3023
3024/**
3025 * nand_scan_tail - [NAND Interface] Scan for the NAND device
3026 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003027 *
3028 * This is the second phase of the normal nand_scan() function. It
3029 * fills out all the uninitialized function pointers with the defaults
3030 * and scans for a bad block table if appropriate.
3031 */
3032int nand_scan_tail(struct mtd_info *mtd)
3033{
3034 int i;
3035 struct nand_chip *chip = mtd->priv;
3036
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003037 if (!(chip->options & NAND_OWN_BUFFERS))
3038 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3039 if (!chip->buffers)
3040 return -ENOMEM;
3041
David Woodhouse7dcdcbe2006-10-21 17:09:53 +01003042 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003043 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003044
3045 /*
3046 * If no default placement scheme is given, select an appropriate one
3047 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003048 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003049 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003051 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 break;
3053 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003054 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 break;
3056 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003057 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003059 case 128:
3060 chip->ecc.layout = &nand_oob_128;
3061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003063 printk(KERN_WARNING "No oob scheme defined for "
3064 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 BUG();
3066 }
3067 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003068
David Woodhouse956e9442006-09-25 17:12:39 +01003069 if (!chip->write_page)
3070 chip->write_page = nand_write_page;
3071
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003072 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003073 * check ECC mode, default to software if 3byte/512byte hardware ECC is
3074 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003075 */
David Woodhouse956e9442006-09-25 17:12:39 +01003076
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003077 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003078 case NAND_ECC_HW_OOB_FIRST:
3079 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3080 if (!chip->ecc.calculate || !chip->ecc.correct ||
3081 !chip->ecc.hwctl) {
3082 printk(KERN_WARNING "No ECC functions supplied; "
3083 "Hardware ECC not possible\n");
3084 BUG();
3085 }
3086 if (!chip->ecc.read_page)
3087 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3088
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003089 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003090 /* Use standard hwecc read page function ? */
3091 if (!chip->ecc.read_page)
3092 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003093 if (!chip->ecc.write_page)
3094 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003095 if (!chip->ecc.read_page_raw)
3096 chip->ecc.read_page_raw = nand_read_page_raw;
3097 if (!chip->ecc.write_page_raw)
3098 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003099 if (!chip->ecc.read_oob)
3100 chip->ecc.read_oob = nand_read_oob_std;
3101 if (!chip->ecc.write_oob)
3102 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003103
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003104 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003105 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3106 !chip->ecc.hwctl) &&
3107 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003108 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003109 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003110 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003111 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003112 "Hardware ECC not possible\n");
3113 BUG();
3114 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003115 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003116 if (!chip->ecc.read_page)
3117 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003118 if (!chip->ecc.write_page)
3119 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003120 if (!chip->ecc.read_page_raw)
3121 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3122 if (!chip->ecc.write_page_raw)
3123 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003124 if (!chip->ecc.read_oob)
3125 chip->ecc.read_oob = nand_read_oob_syndrome;
3126 if (!chip->ecc.write_oob)
3127 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003128
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003129 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003130 break;
3131 printk(KERN_WARNING "%d byte HW ECC not possible on "
3132 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003133 chip->ecc.size, mtd->writesize);
3134 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003136 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003137 chip->ecc.calculate = nand_calculate_ecc;
3138 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003139 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003140 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003141 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003142 chip->ecc.read_page_raw = nand_read_page_raw;
3143 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003144 chip->ecc.read_oob = nand_read_oob_std;
3145 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003146 if (!chip->ecc.size)
3147 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003148 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003150
3151 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003152 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
3153 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003154 chip->ecc.read_page = nand_read_page_raw;
3155 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003156 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003157 chip->ecc.read_page_raw = nand_read_page_raw;
3158 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003159 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003160 chip->ecc.size = mtd->writesize;
3161 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003165 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003166 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003167 BUG();
3168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003170 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003171 * The number of bytes available for a client to place data into
3172 * the out of band area
3173 */
3174 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003175 for (i = 0; chip->ecc.layout->oobfree[i].length
3176 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003177 chip->ecc.layout->oobavail +=
3178 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003179 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003180
3181 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003182 * Set the number of read / write steps for one page depending on ECC
3183 * mode
3184 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003185 chip->ecc.steps = mtd->writesize / chip->ecc.size;
3186 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003187 printk(KERN_WARNING "Invalid ecc parameters\n");
3188 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003190 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003191
Thomas Gleixner29072b92006-09-28 15:38:36 +02003192 /*
3193 * Allow subpage writes up to ecc.steps. Not possible for MLC
3194 * FLASH.
3195 */
3196 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3197 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3198 switch(chip->ecc.steps) {
3199 case 2:
3200 mtd->subpage_sft = 1;
3201 break;
3202 case 4:
3203 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003204 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003205 mtd->subpage_sft = 2;
3206 break;
3207 }
3208 }
3209 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3210
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003211 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003212 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003215 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003218 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 /* Fill in remaining MTD driver data */
3221 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003222 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3223 MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 mtd->erase = nand_erase;
3225 mtd->point = NULL;
3226 mtd->unpoint = NULL;
3227 mtd->read = nand_read;
3228 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003229 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 mtd->read_oob = nand_read_oob;
3231 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 mtd->sync = nand_sync;
3233 mtd->lock = NULL;
3234 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01003235 mtd->suspend = nand_suspend;
3236 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 mtd->block_isbad = nand_block_isbad;
3238 mtd->block_markbad = nand_block_markbad;
3239
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003240 /* propagate ecc.layout to mtd_info */
3241 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003243 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003244 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003248 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Rusty Russella6e6abd2009-03-31 13:05:31 -06003251/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01003252 test if this is a module _anyway_ -- they'd have to try _really_ hard
3253 to call us from in-kernel code if the core NAND support is modular. */
3254#ifdef MODULE
3255#define caller_is_module() (1)
3256#else
3257#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003258 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003259#endif
3260
3261/**
3262 * nand_scan - [NAND Interface] Scan for the NAND device
3263 * @mtd: MTD device structure
3264 * @maxchips: Number of chips to scan for
3265 *
3266 * This fills out all the uninitialized function pointers
3267 * with the defaults.
3268 * The flash ID is read and the mtd/chip structures are
3269 * filled with the appropriate values.
3270 * The mtd->owner field must be set to the module of the caller
3271 *
3272 */
3273int nand_scan(struct mtd_info *mtd, int maxchips)
3274{
3275 int ret;
3276
3277 /* Many callers got this wrong, so check for it for a while... */
3278 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05303279 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3280 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003281 BUG();
3282 }
3283
David Woodhouse5e81e882010-02-26 18:32:56 +00003284 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003285 if (!ret)
3286 ret = nand_scan_tail(mtd);
3287 return ret;
3288}
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003291 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 * @mtd: MTD device structure
3293*/
David Woodhousee0c7d762006-05-13 18:07:53 +01003294void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003296 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298#ifdef CONFIG_MTD_PARTITIONS
3299 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01003300 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301#endif
3302 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01003303 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Jesper Juhlfa671642005-11-07 01:01:27 -08003305 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003306 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003307 if (!(chip->options & NAND_OWN_BUFFERS))
3308 kfree(chip->buffers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Vimal Singh7d70f332010-02-08 15:50:49 +05303311EXPORT_SYMBOL_GPL(nand_lock);
3312EXPORT_SYMBOL_GPL(nand_unlock);
David Woodhousee0c7d762006-05-13 18:07:53 +01003313EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003314EXPORT_SYMBOL_GPL(nand_scan_ident);
3315EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01003316EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003317
3318static int __init nand_base_init(void)
3319{
3320 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3321 return 0;
3322}
3323
3324static void __exit nand_base_exit(void)
3325{
3326 led_trigger_unregister_simple(nand_led_trigger);
3327}
3328
3329module_init(nand_base_init);
3330module_exit(nand_base_exit);
3331
David Woodhousee0c7d762006-05-13 18:07:53 +01003332MODULE_LICENSE("GPL");
3333MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3334MODULE_DESCRIPTION("Generic NAND flash driver code");