blob: ee6a6f866b50270d59b649cbcf0fbddb709c0997 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +020010 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030027 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
David Woodhouse552d9202006-05-14 01:20:46 +010035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/compatmac.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/io.h>
50
51#ifdef CONFIG_MTD_PARTITIONS
52#include <linux/mtd/partitions.h>
53#endif
54
55/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .eccbytes = 3,
58 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020059 .oobfree = {
60 {.offset = 3,
61 .length = 2},
62 {.offset = 6,
63 .length = 2}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .eccbytes = 6,
68 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069 .oobfree = {
70 {.offset = 8,
71 . length = 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020074static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .eccbytes = 24,
76 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010077 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020080 .oobfree = {
81 {.offset = 2,
82 .length = 38}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Thomas Gleixner81ec5362007-12-12 17:27:03 +010085static struct nand_ecclayout nand_oob_128 = {
86 .eccbytes = 48,
87 .eccpos = {
88 80, 81, 82, 83, 84, 85, 86, 87,
89 88, 89, 90, 91, 92, 93, 94, 95,
90 96, 97, 98, 99, 100, 101, 102, 103,
91 104, 105, 106, 107, 108, 109, 110, 111,
92 112, 113, 114, 115, 116, 117, 118, 119,
93 120, 121, 122, 123, 124, 125, 126, 127},
94 .oobfree = {
95 {.offset = 2,
96 .length = 78}}
97};
98
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020099static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200100 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200102static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
103 struct mtd_oob_ops *ops);
104
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200105/*
Joe Perches8e87d782008-02-03 17:22:34 +0200106 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200107 * compiled away when LED support is disabled.
108 */
109DEFINE_LED_TRIGGER(nand_led_trigger);
110
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530111static int check_offs_len(struct mtd_info *mtd,
112 loff_t ofs, uint64_t len)
113{
114 struct nand_chip *chip = mtd->priv;
115 int ret = 0;
116
117 /* Start address must align on block boundary */
118 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
119 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
120 ret = -EINVAL;
121 }
122
123 /* Length must align on block boundary */
124 if (len & ((1 << chip->phys_erase_shift) - 1)) {
125 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
126 __func__);
127 ret = -EINVAL;
128 }
129
130 /* Do not allow past end of device */
131 if (ofs + len > mtd->size) {
132 DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
133 __func__);
134 ret = -EINVAL;
135 }
136
137 return ret;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
141 * nand_release_device - [GENERIC] release chip
142 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000143 *
144 * Deselect, release chip lock and wake up anyone waiting on the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100146static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200148 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200151 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100152
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200153 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200154 spin_lock(&chip->controller->lock);
155 chip->controller->active = NULL;
156 chip->state = FL_READY;
157 wake_up(&chip->controller->wq);
158 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/**
162 * nand_read_byte - [DEFAULT] read one byte from the chip
163 * @mtd: MTD device structure
164 *
165 * Default read function for 8bit buswith
166 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200167static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200169 struct nand_chip *chip = mtd->priv;
170 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
175 * @mtd: MTD device structure
176 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000177 * Default read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * endianess conversion
179 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200180static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200182 struct nand_chip *chip = mtd->priv;
183 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * nand_read_word - [DEFAULT] read one word from the chip
188 * @mtd: MTD device structure
189 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000190 * Default read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * endianess conversion
192 */
193static u16 nand_read_word(struct mtd_info *mtd)
194{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200195 struct nand_chip *chip = mtd->priv;
196 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * nand_select_chip - [DEFAULT] control CE line
201 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700202 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
204 * Default select function for 1 chip devices.
205 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200206static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200208 struct nand_chip *chip = mtd->priv;
209
210 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200212 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216
217 default:
218 BUG();
219 }
220}
221
222/**
223 * nand_write_buf - [DEFAULT] write buffer to chip
224 * @mtd: MTD device structure
225 * @buf: data buffer
226 * @len: number of bytes to write
227 *
228 * Default write function for 8bit buswith
229 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200230static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200233 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
David Woodhousee0c7d762006-05-13 18:07:53 +0100235 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200236 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 * nand_read_buf - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * @mtd: MTD device structure
242 * @buf: buffer to store date
243 * @len: number of bytes to read
244 *
245 * Default read function for 8bit buswith
246 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200247static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200250 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Woodhousee0c7d762006-05-13 18:07:53 +0100252 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200253 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000257 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * @mtd: MTD device structure
259 * @buf: buffer containing the data to compare
260 * @len: number of bytes to compare
261 *
262 * Default verify function for 8bit buswith
263 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200264static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200267 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
David Woodhousee0c7d762006-05-13 18:07:53 +0100269 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200270 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273}
274
275/**
276 * nand_write_buf16 - [DEFAULT] write buffer to chip
277 * @mtd: MTD device structure
278 * @buf: data buffer
279 * @len: number of bytes to write
280 *
281 * Default write function for 16bit buswith
282 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200283static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 u16 *p = (u16 *) buf;
288 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000289
David Woodhousee0c7d762006-05-13 18:07:53 +0100290 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200291 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000296 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * @mtd: MTD device structure
298 * @buf: buffer to store date
299 * @len: number of bytes to read
300 *
301 * Default read function for 16bit buswith
302 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200303static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 u16 *p = (u16 *) buf;
308 len >>= 1;
309
David Woodhousee0c7d762006-05-13 18:07:53 +0100310 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200311 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000315 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * @mtd: MTD device structure
317 * @buf: buffer containing the data to compare
318 * @len: number of bytes to compare
319 *
320 * Default verify function for 16bit buswith
321 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200322static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200325 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 *p = (u16 *) buf;
327 len >>= 1;
328
David Woodhousee0c7d762006-05-13 18:07:53 +0100329 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200330 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EFAULT;
332
333 return 0;
334}
335
336/**
337 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
338 * @mtd: MTD device structure
339 * @ofs: offset from device start
340 * @getchip: 0, if the chip is already selected
341 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000342 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
344static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
345{
346 int page, chipnr, res = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200347 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 u16 bad;
349
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 Gleixner58dd8f2b2006-05-23 11:52:35 +0200399 uint8_t buf[2] = { 0, 0 };
Brian Norris02ed70b2010-07-21 16:53:47 -0700400 int block, ret, i = 0;
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 {
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300414 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000415
Brian Norris02ed70b2010-07-21 16:53:47 -0700416 /* Write to first two pages and to byte 1 and 6 if necessary.
417 * If we write to more than one location, the first error
418 * encountered quits the procedure. We write two bytes per
419 * location, so we dont have to mess with 16 bit access.
420 */
421 do {
422 chip->ops.len = chip->ops.ooblen = 2;
423 chip->ops.datbuf = NULL;
424 chip->ops.oobbuf = buf;
425 chip->ops.ooboffs = chip->badblockpos & ~0x01;
426
427 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
428
429 if (!ret && (chip->options & NAND_BBT_SCANBYTE1AND6)) {
430 chip->ops.ooboffs = NAND_SMALL_BADBLOCK_POS
431 & ~0x01;
432 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
433 }
434 i++;
435 ofs += mtd->writesize;
436 } while (!ret && (chip->options & NAND_BBT_SCAN2NDPAGE) &&
437 i < 2);
438
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300439 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200440 }
441 if (!ret)
442 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300443
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * nand_check_wp - [GENERIC] check if the chip is write protected
449 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000450 * Check, if the device is write protected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000452 * The function expects, that the device is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100454static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200456 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200457
458 /* broken xD cards report WP despite being writable */
459 if (chip->options & NAND_BROKEN_XD)
460 return 0;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200463 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
464 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
467/**
468 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
469 * @mtd: MTD device structure
470 * @ofs: offset from device start
471 * @getchip: 0, if the chip is already selected
472 * @allowbbt: 1, if its allowed to access the bbt area
473 *
474 * Check, if the block is bad. Either by reading the bad block table or
475 * calling of the scan function.
476 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200477static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
478 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200480 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000481
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200482 if (!chip->bbt)
483 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100486 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200489/**
490 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
491 * @mtd: MTD device structure
492 * @timeo: Timeout
493 *
494 * Helper function for nand_wait_ready used when needing to wait in interrupt
495 * context.
496 */
497static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
498{
499 struct nand_chip *chip = mtd->priv;
500 int i;
501
502 /* Wait for the device to get ready */
503 for (i = 0; i < timeo; i++) {
504 if (chip->dev_ready(mtd))
505 break;
506 touch_softlockup_watchdog();
507 mdelay(1);
508 }
509}
510
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000511/*
Thomas Gleixner3b887752005-02-22 21:56:49 +0000512 * Wait for the ready pin, after a command
513 * The timeout is catched later.
514 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100515void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000516{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200517 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100518 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000519
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200520 /* 400ms timeout */
521 if (in_interrupt() || oops_in_progress)
522 return panic_nand_wait_ready(mtd, 400);
523
Richard Purdie8fe833c2006-03-31 02:31:14 -0800524 led_trigger_event(nand_led_trigger, LED_FULL);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000525 /* wait until command is processed or timeout occures */
526 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200527 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800528 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700529 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000530 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800531 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000532}
David Woodhouse4b648b02006-09-25 17:05:24 +0100533EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/**
536 * nand_command - [DEFAULT] Send command to NAND device
537 * @mtd: MTD device structure
538 * @command: the command to be sent
539 * @column: the column address for this command, -1 if none
540 * @page_addr: the page address for this command, -1 if none
541 *
542 * Send command to NAND device. This function is used for small page
543 * devices (256/512 Bytes per page)
544 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200545static void nand_command(struct mtd_info *mtd, unsigned int command,
546 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200548 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200549 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /*
552 * Write out the command to the device.
553 */
554 if (command == NAND_CMD_SEQIN) {
555 int readcmd;
556
Joern Engel28318772006-05-22 23:18:05 +0200557 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200559 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 readcmd = NAND_CMD_READOOB;
561 } else if (column < 256) {
562 /* First 256 bytes --> READ0 */
563 readcmd = NAND_CMD_READ0;
564 } else {
565 column -= 256;
566 readcmd = NAND_CMD_READ1;
567 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200568 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200569 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200571 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200573 /*
574 * Address cycle, when necessary
575 */
576 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
577 /* Serially input address */
578 if (column != -1) {
579 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200580 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200581 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200583 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200585 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200586 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200587 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200588 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200589 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200590 if (chip->chipsize > (32 << 20))
591 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200592 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200593 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000594
595 /*
596 * program and erase have their own busy handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case NAND_CMD_PAGEPROG:
602 case NAND_CMD_ERASE1:
603 case NAND_CMD_ERASE2:
604 case NAND_CMD_SEQIN:
605 case NAND_CMD_STATUS:
606 return;
607
608 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200609 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200611 udelay(chip->chip_delay);
612 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200613 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200614 chip->cmd_ctrl(mtd,
615 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200616 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return;
618
David Woodhousee0c7d762006-05-13 18:07:53 +0100619 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000621 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * If we don't have access to the busy pin, we apply the given
623 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100624 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200625 if (!chip->dev_ready) {
626 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Apply this short delay always to ensure that we do wait tWB in
631 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100632 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000633
634 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/**
638 * nand_command_lp - [DEFAULT] Send command to NAND large page device
639 * @mtd: MTD device structure
640 * @command: the command to be sent
641 * @column: the column address for this command, -1 if none
642 * @page_addr: the page address for this command, -1 if none
643 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200644 * Send command to NAND device. This is the version for the new large page
645 * devices We dont have the separate regions as we have in the small page
646 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200648static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
649 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200651 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* Emulate NAND_CMD_READOOB */
654 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200655 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 command = NAND_CMD_READ0;
657 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000658
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* Serially input address */
667 if (column != -1) {
668 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200669 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200671 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200672 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200673 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200676 chip->cmd_ctrl(mtd, page_addr, ctrl);
677 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200678 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200680 if (chip->chipsize > (128 << 20))
681 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200682 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000686
687 /*
688 * program and erase have their own busy handlers
David A. Marlin30f464b2005-01-17 18:35:25 +0000689 * status, sequential in, and deplete1 need no delay
690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 case NAND_CMD_CACHEDPROG:
694 case NAND_CMD_PAGEPROG:
695 case NAND_CMD_ERASE1:
696 case NAND_CMD_ERASE2:
697 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200698 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000700 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
702
David Woodhousee0c7d762006-05-13 18:07:53 +0100703 /*
704 * read error status commands require only a short delay
705 */
David A. Marlin30f464b2005-01-17 18:35:25 +0000706 case NAND_CMD_STATUS_ERROR:
707 case NAND_CMD_STATUS_ERROR0:
708 case NAND_CMD_STATUS_ERROR1:
709 case NAND_CMD_STATUS_ERROR2:
710 case NAND_CMD_STATUS_ERROR3:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200711 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200715 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200717 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200718 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
719 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
720 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
721 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return;
724
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200725 case NAND_CMD_RNDOUT:
726 /* No ready / busy check necessary */
727 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
728 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
729 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
730 NAND_NCE | NAND_CTRL_CHANGE);
731 return;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200734 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
735 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
736 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
737 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000738
David Woodhousee0c7d762006-05-13 18:07:53 +0100739 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * If we don't have access to the busy pin, we apply the given
743 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100744 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200745 if (!chip->dev_ready) {
746 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /* Apply this short delay always to ensure that we do wait tWB in
752 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100753 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000754
755 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200759 * panic_nand_get_device - [GENERIC] Get chip for selected access
760 * @chip: the nand chip descriptor
761 * @mtd: MTD device structure
762 * @new_state: the state which is requested
763 *
764 * Used when in panic, no locks are taken.
765 */
766static void panic_nand_get_device(struct nand_chip *chip,
767 struct mtd_info *mtd, int new_state)
768{
769 /* Hardware controller shared among independend devices */
770 chip->controller->active = chip;
771 chip->state = new_state;
772}
773
774/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * nand_get_device - [GENERIC] Get chip for selected access
Randy Dunlap844d3b42006-06-28 21:48:27 -0700776 * @chip: the nand chip descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000778 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
780 * Get the device and lock it for exclusive access
781 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200782static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200783nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200785 spinlock_t *lock = &chip->controller->lock;
786 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100787 DECLARE_WAITQUEUE(wait, current);
David Woodhousee0c7d762006-05-13 18:07:53 +0100788 retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100789 spin_lock(lock);
790
vimal singhb8b3ee92009-07-09 20:41:22 +0530791 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200792 if (!chip->controller->active)
793 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200794
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200795 if (chip->controller->active == chip && chip->state == FL_READY) {
796 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100797 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100798 return 0;
799 }
800 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800801 if (chip->controller->active->state == FL_PM_SUSPENDED) {
802 chip->state = FL_PM_SUSPENDED;
803 spin_unlock(lock);
804 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800805 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100806 }
807 set_current_state(TASK_UNINTERRUPTIBLE);
808 add_wait_queue(wq, &wait);
809 spin_unlock(lock);
810 schedule();
811 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto retry;
813}
814
815/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200816 * panic_nand_wait - [GENERIC] wait until the command is done
817 * @mtd: MTD device structure
818 * @chip: NAND chip structure
819 * @timeo: Timeout
820 *
821 * Wait for command done. This is a helper function for nand_wait used when
822 * we are in interrupt context. May happen when in panic and trying to write
823 * an oops trough mtdoops.
824 */
825static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
826 unsigned long timeo)
827{
828 int i;
829 for (i = 0; i < timeo; i++) {
830 if (chip->dev_ready) {
831 if (chip->dev_ready(mtd))
832 break;
833 } else {
834 if (chip->read_byte(mtd) & NAND_STATUS_READY)
835 break;
836 }
837 mdelay(1);
838 }
839}
840
841/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * nand_wait - [DEFAULT] wait until the command is done
843 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -0700844 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 *
846 * Wait for command done. This applies to erase and program only
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000847 * Erase can take up to 400ms and program up to 20ms according to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * general NAND and SmartMedia specs
Randy Dunlap844d3b42006-06-28 21:48:27 -0700849 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200850static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852
David Woodhousee0c7d762006-05-13 18:07:53 +0100853 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200854 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100857 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100859 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Richard Purdie8fe833c2006-03-31 02:31:14 -0800861 led_trigger_event(nand_led_trigger, LED_FULL);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Apply this short delay always to ensure that we do wait tWB in
864 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100865 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200867 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
868 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000869 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200870 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200872 if (in_interrupt() || oops_in_progress)
873 panic_nand_wait(mtd, chip, timeo);
874 else {
875 while (time_before(jiffies, timeo)) {
876 if (chip->dev_ready) {
877 if (chip->dev_ready(mtd))
878 break;
879 } else {
880 if (chip->read_byte(mtd) & NAND_STATUS_READY)
881 break;
882 }
883 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800886 led_trigger_event(nand_led_trigger, LED_OFF);
887
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200888 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return status;
890}
891
892/**
Vimal Singh7d70f332010-02-08 15:50:49 +0530893 * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
894 *
895 * @param mtd - mtd info
896 * @param ofs - offset to start unlock from
897 * @param len - length to unlock
898 * @invert - when = 0, unlock the range of blocks within the lower and
899 * upper boundary address
900 * whne = 1, unlock the range of blocks outside the boundaries
901 * of the lower and upper boundary address
902 *
903 * @return - unlock status
904 */
905static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
906 uint64_t len, int invert)
907{
908 int ret = 0;
909 int status, page;
910 struct nand_chip *chip = mtd->priv;
911
912 /* Submit address of first page to unlock */
913 page = ofs >> chip->page_shift;
914 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
915
916 /* Submit address of last page to unlock */
917 page = (ofs + len) >> chip->page_shift;
918 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
919 (page | invert) & chip->pagemask);
920
921 /* Call wait ready function */
922 status = chip->waitfunc(mtd, chip);
923 udelay(1000);
924 /* See if device thinks it succeeded */
925 if (status & 0x01) {
926 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
927 __func__, status);
928 ret = -EIO;
929 }
930
931 return ret;
932}
933
934/**
935 * nand_unlock - [REPLACABLE] unlocks specified locked blockes
936 *
937 * @param mtd - mtd info
938 * @param ofs - offset to start unlock from
939 * @param len - length to unlock
940 *
941 * @return - unlock status
942 */
943int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
944{
945 int ret = 0;
946 int chipnr;
947 struct nand_chip *chip = mtd->priv;
948
949 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
950 __func__, (unsigned long long)ofs, len);
951
952 if (check_offs_len(mtd, ofs, len))
953 ret = -EINVAL;
954
955 /* Align to last block address if size addresses end of the device */
956 if (ofs + len == mtd->size)
957 len -= mtd->erasesize;
958
959 nand_get_device(chip, mtd, FL_UNLOCKING);
960
961 /* Shift to get chip number */
962 chipnr = ofs >> chip->chip_shift;
963
964 chip->select_chip(mtd, chipnr);
965
966 /* Check, if it is write protected */
967 if (nand_check_wp(mtd)) {
968 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
969 __func__);
970 ret = -EIO;
971 goto out;
972 }
973
974 ret = __nand_unlock(mtd, ofs, len, 0);
975
976out:
977 /* de-select the NAND device */
978 chip->select_chip(mtd, -1);
979
980 nand_release_device(mtd);
981
982 return ret;
983}
984
985/**
986 * nand_lock - [REPLACABLE] locks all blockes present in the device
987 *
988 * @param mtd - mtd info
989 * @param ofs - offset to start unlock from
990 * @param len - length to unlock
991 *
992 * @return - lock status
993 *
994 * This feature is not support in many NAND parts. 'Micron' NAND parts
995 * do have this feature, but it allows only to lock all blocks not for
996 * specified range for block.
997 *
998 * Implementing 'lock' feature by making use of 'unlock', for now.
999 */
1000int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1001{
1002 int ret = 0;
1003 int chipnr, status, page;
1004 struct nand_chip *chip = mtd->priv;
1005
1006 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
1007 __func__, (unsigned long long)ofs, len);
1008
1009 if (check_offs_len(mtd, ofs, len))
1010 ret = -EINVAL;
1011
1012 nand_get_device(chip, mtd, FL_LOCKING);
1013
1014 /* Shift to get chip number */
1015 chipnr = ofs >> chip->chip_shift;
1016
1017 chip->select_chip(mtd, chipnr);
1018
1019 /* Check, if it is write protected */
1020 if (nand_check_wp(mtd)) {
1021 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
1022 __func__);
1023 status = MTD_ERASE_FAILED;
1024 ret = -EIO;
1025 goto out;
1026 }
1027
1028 /* Submit address of first page to lock */
1029 page = ofs >> chip->page_shift;
1030 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1031
1032 /* Call wait ready function */
1033 status = chip->waitfunc(mtd, chip);
1034 udelay(1000);
1035 /* See if device thinks it succeeded */
1036 if (status & 0x01) {
1037 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
1038 __func__, status);
1039 ret = -EIO;
1040 goto out;
1041 }
1042
1043 ret = __nand_unlock(mtd, ofs, len, 0x1);
1044
1045out:
1046 /* de-select the NAND device */
1047 chip->select_chip(mtd, -1);
1048
1049 nand_release_device(mtd);
1050
1051 return ret;
1052}
1053
1054/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001055 * nand_read_page_raw - [Intern] read raw page data without ecc
1056 * @mtd: mtd info structure
1057 * @chip: nand chip info structure
1058 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001059 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001060 *
1061 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001062 */
1063static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001064 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001065{
1066 chip->read_buf(mtd, buf, mtd->writesize);
1067 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1068 return 0;
1069}
1070
1071/**
David Brownell52ff49d2009-03-04 12:01:36 -08001072 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
1073 * @mtd: mtd info structure
1074 * @chip: nand chip info structure
1075 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001076 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001077 *
1078 * We need a special oob layout and handling even when OOB isn't used.
1079 */
1080static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001081 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001082{
1083 int eccsize = chip->ecc.size;
1084 int eccbytes = chip->ecc.bytes;
1085 uint8_t *oob = chip->oob_poi;
1086 int steps, size;
1087
1088 for (steps = chip->ecc.steps; steps > 0; steps--) {
1089 chip->read_buf(mtd, buf, eccsize);
1090 buf += eccsize;
1091
1092 if (chip->ecc.prepad) {
1093 chip->read_buf(mtd, oob, chip->ecc.prepad);
1094 oob += chip->ecc.prepad;
1095 }
1096
1097 chip->read_buf(mtd, oob, eccbytes);
1098 oob += eccbytes;
1099
1100 if (chip->ecc.postpad) {
1101 chip->read_buf(mtd, oob, chip->ecc.postpad);
1102 oob += chip->ecc.postpad;
1103 }
1104 }
1105
1106 size = mtd->oobsize - (oob - chip->oob_poi);
1107 if (size)
1108 chip->read_buf(mtd, oob, size);
1109
1110 return 0;
1111}
1112
1113/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001114 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001115 * @mtd: mtd info structure
1116 * @chip: nand chip info structure
1117 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001118 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001119 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001120static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001121 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001123 int i, eccsize = chip->ecc.size;
1124 int eccbytes = chip->ecc.bytes;
1125 int eccsteps = chip->ecc.steps;
1126 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001127 uint8_t *ecc_calc = chip->buffers->ecccalc;
1128 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001129 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001130
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001131 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001132
1133 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1134 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1135
1136 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001137 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001138
1139 eccsteps = chip->ecc.steps;
1140 p = buf;
1141
1142 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1143 int stat;
1144
1145 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001146 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001147 mtd->ecc_stats.failed++;
1148 else
1149 mtd->ecc_stats.corrected += stat;
1150 }
1151 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/**
Alexey Korolev3d459552008-05-15 17:23:18 +01001155 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
1156 * @mtd: mtd info structure
1157 * @chip: nand chip info structure
Alexey Korolev17c1d2b2008-08-20 22:32:08 +01001158 * @data_offs: offset of requested data within the page
1159 * @readlen: data length
1160 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001161 */
1162static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
1163{
1164 int start_step, end_step, num_steps;
1165 uint32_t *eccpos = chip->ecc.layout->eccpos;
1166 uint8_t *p;
1167 int data_col_addr, i, gaps = 0;
1168 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1169 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1170
1171 /* Column address wihin the page aligned to ECC size (256bytes). */
1172 start_step = data_offs / chip->ecc.size;
1173 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1174 num_steps = end_step - start_step + 1;
1175
1176 /* Data size aligned to ECC ecc.size*/
1177 datafrag_len = num_steps * chip->ecc.size;
1178 eccfrag_len = num_steps * chip->ecc.bytes;
1179
1180 data_col_addr = start_step * chip->ecc.size;
1181 /* If we read not a page aligned data */
1182 if (data_col_addr != 0)
1183 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1184
1185 p = bufpoi + data_col_addr;
1186 chip->read_buf(mtd, p, datafrag_len);
1187
1188 /* Calculate ECC */
1189 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1190 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1191
1192 /* The performance is faster if to position offsets
1193 according to ecc.pos. Let make sure here that
1194 there are no gaps in ecc positions */
1195 for (i = 0; i < eccfrag_len - 1; i++) {
1196 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1197 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1198 gaps = 1;
1199 break;
1200 }
1201 }
1202 if (gaps) {
1203 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1204 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1205 } else {
1206 /* send the command to read the particular ecc bytes */
1207 /* take care about buswidth alignment in read_buf */
1208 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
1209 aligned_len = eccfrag_len;
1210 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
1211 aligned_len++;
1212 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
1213 aligned_len++;
1214
1215 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
1216 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1217 }
1218
1219 for (i = 0; i < eccfrag_len; i++)
1220 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
1221
1222 p = bufpoi + data_col_addr;
1223 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1224 int stat;
1225
1226 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1227 if (stat == -1)
1228 mtd->ecc_stats.failed++;
1229 else
1230 mtd->ecc_stats.corrected += stat;
1231 }
1232 return 0;
1233}
1234
1235/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001236 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001237 * @mtd: mtd info structure
1238 * @chip: nand chip info structure
1239 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001240 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001241 *
1242 * Not for syndrome calculating ecc controllers which need a special oob layout
1243 */
1244static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001245 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001246{
1247 int i, eccsize = chip->ecc.size;
1248 int eccbytes = chip->ecc.bytes;
1249 int eccsteps = chip->ecc.steps;
1250 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001251 uint8_t *ecc_calc = chip->buffers->ecccalc;
1252 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001253 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001254
1255 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1256 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1257 chip->read_buf(mtd, p, eccsize);
1258 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1259 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001260 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001261
1262 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001263 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001264
1265 eccsteps = chip->ecc.steps;
1266 p = buf;
1267
1268 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1269 int stat;
1270
1271 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001272 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001273 mtd->ecc_stats.failed++;
1274 else
1275 mtd->ecc_stats.corrected += stat;
1276 }
1277 return 0;
1278}
1279
1280/**
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001281 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
1282 * @mtd: mtd info structure
1283 * @chip: nand chip info structure
1284 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001285 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001286 *
1287 * Hardware ECC for large page chips, require OOB to be read first.
1288 * For this ECC mode, the write_page method is re-used from ECC_HW.
1289 * These methods read/write ECC from the OOB area, unlike the
1290 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
1291 * "infix ECC" scheme and reads/writes ECC from the data area, by
1292 * overwriting the NAND manufacturer bad block markings.
1293 */
1294static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1295 struct nand_chip *chip, uint8_t *buf, int page)
1296{
1297 int i, eccsize = chip->ecc.size;
1298 int eccbytes = chip->ecc.bytes;
1299 int eccsteps = chip->ecc.steps;
1300 uint8_t *p = buf;
1301 uint8_t *ecc_code = chip->buffers->ecccode;
1302 uint32_t *eccpos = chip->ecc.layout->eccpos;
1303 uint8_t *ecc_calc = chip->buffers->ecccalc;
1304
1305 /* Read the OOB area first */
1306 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1307 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1308 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1309
1310 for (i = 0; i < chip->ecc.total; i++)
1311 ecc_code[i] = chip->oob_poi[eccpos[i]];
1312
1313 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1314 int stat;
1315
1316 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1317 chip->read_buf(mtd, p, eccsize);
1318 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1319
1320 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1321 if (stat < 0)
1322 mtd->ecc_stats.failed++;
1323 else
1324 mtd->ecc_stats.corrected += stat;
1325 }
1326 return 0;
1327}
1328
1329/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001330 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001331 * @mtd: mtd info structure
1332 * @chip: nand chip info structure
1333 * @buf: buffer to store read data
Jaswinder Singh Rajput58475fb2009-09-24 13:04:53 +01001334 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001335 *
1336 * The hw generator calculates the error syndrome automatically. Therefor
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001337 * we need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001338 */
1339static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001340 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001341{
1342 int i, eccsize = chip->ecc.size;
1343 int eccbytes = chip->ecc.bytes;
1344 int eccsteps = chip->ecc.steps;
1345 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001346 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001347
1348 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1349 int stat;
1350
1351 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1352 chip->read_buf(mtd, p, eccsize);
1353
1354 if (chip->ecc.prepad) {
1355 chip->read_buf(mtd, oob, chip->ecc.prepad);
1356 oob += chip->ecc.prepad;
1357 }
1358
1359 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1360 chip->read_buf(mtd, oob, eccbytes);
1361 stat = chip->ecc.correct(mtd, p, oob, NULL);
1362
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001363 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001364 mtd->ecc_stats.failed++;
1365 else
1366 mtd->ecc_stats.corrected += stat;
1367
1368 oob += eccbytes;
1369
1370 if (chip->ecc.postpad) {
1371 chip->read_buf(mtd, oob, chip->ecc.postpad);
1372 oob += chip->ecc.postpad;
1373 }
1374 }
1375
1376 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001377 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001378 if (i)
1379 chip->read_buf(mtd, oob, i);
1380
1381 return 0;
1382}
1383
1384/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001385 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1386 * @chip: nand chip structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07001387 * @oob: oob destination address
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001388 * @ops: oob ops structure
Vitaly Wool70145682006-11-03 18:20:38 +03001389 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001390 */
1391static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001392 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001393{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001394 switch(ops->mode) {
1395
1396 case MTD_OOB_PLACE:
1397 case MTD_OOB_RAW:
1398 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1399 return oob + len;
1400
1401 case MTD_OOB_AUTO: {
1402 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001403 uint32_t boffs = 0, roffs = ops->ooboffs;
1404 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001405
1406 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001407 /* Read request not from offset 0 ? */
1408 if (unlikely(roffs)) {
1409 if (roffs >= free->length) {
1410 roffs -= free->length;
1411 continue;
1412 }
1413 boffs = free->offset + roffs;
1414 bytes = min_t(size_t, len,
1415 (free->length - roffs));
1416 roffs = 0;
1417 } else {
1418 bytes = min_t(size_t, len, free->length);
1419 boffs = free->offset;
1420 }
1421 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001422 oob += bytes;
1423 }
1424 return oob;
1425 }
1426 default:
1427 BUG();
1428 }
1429 return NULL;
1430}
1431
1432/**
1433 * nand_do_read_ops - [Internal] Read data with ECC
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001434 *
David A. Marlin068e3c02005-01-24 03:07:46 +00001435 * @mtd: MTD device structure
1436 * @from: offset to read from
Randy Dunlap844d3b42006-06-28 21:48:27 -07001437 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001438 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001439 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001440 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001441static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1442 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001443{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001445 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001446 struct mtd_ecc_stats stats;
1447 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1448 int sndcmd = 1;
1449 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001451 uint32_t oobreadlen = ops->ooblen;
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001452 uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
1453 mtd->oobavail : mtd->oobsize;
1454
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001455 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001457 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001459 chipnr = (int)(from >> chip->chip_shift);
1460 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001462 realpage = (int)(from >> chip->page_shift);
1463 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001465 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001467 buf = ops->datbuf;
1468 oob = ops->oobbuf;
1469
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001470 while(1) {
1471 bytes = min(mtd->writesize - col, readlen);
1472 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001473
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001474 /* Is the current page in the buffer ? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001475 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001476 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001478 if (likely(sndcmd)) {
1479 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1480 sndcmd = 0;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001483 /* Now read the page into the buffer */
David Woodhouse956e9442006-09-25 17:12:39 +01001484 if (unlikely(ops->mode == MTD_OOB_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001485 ret = chip->ecc.read_page_raw(mtd, chip,
1486 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001487 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1488 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001489 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001490 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1491 page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001492 if (ret < 0)
David Woodhousee0c7d762006-05-13 18:07:53 +01001493 break;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001494
1495 /* Transfer not aligned data */
1496 if (!aligned) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001497 if (!NAND_SUBPAGE_READ(chip) && !oob)
1498 chip->pagebuf = realpage;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001499 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001502 buf += bytes;
1503
1504 if (unlikely(oob)) {
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001505
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001506 int toread = min(oobreadlen, max_oobsize);
1507
1508 if (toread) {
1509 oob = nand_transfer_oob(chip,
1510 oob, ops, toread);
1511 oobreadlen -= toread;
1512 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001513 }
1514
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001515 if (!(chip->options & NAND_NO_READRDY)) {
1516 /*
1517 * Apply delay or wait for ready/busy pin. Do
1518 * this before the AUTOINCR check, so no
1519 * problems arise if a chip which does auto
1520 * increment is marked as NOAUTOINCR by the
1521 * board driver.
1522 */
1523 if (!chip->dev_ready)
1524 udelay(chip->chip_delay);
1525 else
1526 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001528 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001529 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001530 buf += bytes;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001533 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001534
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001535 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /* For subsequent reads align to page boundary. */
1539 col = 0;
1540 /* Increment page address */
1541 realpage++;
1542
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001543 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* Check, if we cross a chip boundary */
1545 if (!page) {
1546 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001547 chip->select_chip(mtd, -1);
1548 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001551 /* Check, if the chip supports auto page increment
1552 * or if we have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001553 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001554 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001555 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001558 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001559 if (oob)
1560 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001562 if (ret)
1563 return ret;
1564
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001565 if (mtd->ecc_stats.failed - stats.failed)
1566 return -EBADMSG;
1567
1568 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001569}
1570
1571/**
1572 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1573 * @mtd: MTD device structure
1574 * @from: offset to read from
1575 * @len: number of bytes to read
1576 * @retlen: pointer to variable to store the number of read bytes
1577 * @buf: the databuffer to put data
1578 *
1579 * Get hold of the chip and call nand_do_read
1580 */
1581static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1582 size_t *retlen, uint8_t *buf)
1583{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001584 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001585 int ret;
1586
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001587 /* Do not allow reads past end of device */
1588 if ((from + len) > mtd->size)
1589 return -EINVAL;
1590 if (!len)
1591 return 0;
1592
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001593 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001594
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001595 chip->ops.len = len;
1596 chip->ops.datbuf = buf;
1597 chip->ops.oobbuf = NULL;
1598
1599 ret = nand_do_read_ops(mtd, from, &chip->ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001600
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001601 *retlen = chip->ops.retlen;
1602
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001603 nand_release_device(mtd);
1604
1605 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/**
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001609 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1610 * @mtd: mtd info structure
1611 * @chip: nand chip info structure
1612 * @page: page number to read
1613 * @sndcmd: flag whether to issue read command or not
1614 */
1615static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1616 int page, int sndcmd)
1617{
1618 if (sndcmd) {
1619 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1620 sndcmd = 0;
1621 }
1622 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1623 return sndcmd;
1624}
1625
1626/**
1627 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1628 * with syndromes
1629 * @mtd: mtd info structure
1630 * @chip: nand chip info structure
1631 * @page: page number to read
1632 * @sndcmd: flag whether to issue read command or not
1633 */
1634static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1635 int page, int sndcmd)
1636{
1637 uint8_t *buf = chip->oob_poi;
1638 int length = mtd->oobsize;
1639 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1640 int eccsize = chip->ecc.size;
1641 uint8_t *bufpoi = buf;
1642 int i, toread, sndrnd = 0, pos;
1643
1644 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1645 for (i = 0; i < chip->ecc.steps; i++) {
1646 if (sndrnd) {
1647 pos = eccsize + i * (eccsize + chunk);
1648 if (mtd->writesize > 512)
1649 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1650 else
1651 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1652 } else
1653 sndrnd = 1;
1654 toread = min_t(int, length, chunk);
1655 chip->read_buf(mtd, bufpoi, toread);
1656 bufpoi += toread;
1657 length -= toread;
1658 }
1659 if (length > 0)
1660 chip->read_buf(mtd, bufpoi, length);
1661
1662 return 1;
1663}
1664
1665/**
1666 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1667 * @mtd: mtd info structure
1668 * @chip: nand chip info structure
1669 * @page: page number to write
1670 */
1671static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1672 int page)
1673{
1674 int status = 0;
1675 const uint8_t *buf = chip->oob_poi;
1676 int length = mtd->oobsize;
1677
1678 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1679 chip->write_buf(mtd, buf, length);
1680 /* Send command to program the OOB data */
1681 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1682
1683 status = chip->waitfunc(mtd, chip);
1684
Savin Zlobec0d420f92006-06-21 11:51:20 +02001685 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001686}
1687
1688/**
1689 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1690 * with syndrome - only for large page flash !
1691 * @mtd: mtd info structure
1692 * @chip: nand chip info structure
1693 * @page: page number to write
1694 */
1695static int nand_write_oob_syndrome(struct mtd_info *mtd,
1696 struct nand_chip *chip, int page)
1697{
1698 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1699 int eccsize = chip->ecc.size, length = mtd->oobsize;
1700 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1701 const uint8_t *bufpoi = chip->oob_poi;
1702
1703 /*
1704 * data-ecc-data-ecc ... ecc-oob
1705 * or
1706 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1707 */
1708 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1709 pos = steps * (eccsize + chunk);
1710 steps = 0;
1711 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001712 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001713
1714 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1715 for (i = 0; i < steps; i++) {
1716 if (sndcmd) {
1717 if (mtd->writesize <= 512) {
1718 uint32_t fill = 0xFFFFFFFF;
1719
1720 len = eccsize;
1721 while (len > 0) {
1722 int num = min_t(int, len, 4);
1723 chip->write_buf(mtd, (uint8_t *)&fill,
1724 num);
1725 len -= num;
1726 }
1727 } else {
1728 pos = eccsize + i * (eccsize + chunk);
1729 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1730 }
1731 } else
1732 sndcmd = 1;
1733 len = min_t(int, length, chunk);
1734 chip->write_buf(mtd, bufpoi, len);
1735 bufpoi += len;
1736 length -= len;
1737 }
1738 if (length > 0)
1739 chip->write_buf(mtd, bufpoi, length);
1740
1741 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1742 status = chip->waitfunc(mtd, chip);
1743
1744 return status & NAND_STATUS_FAIL ? -EIO : 0;
1745}
1746
1747/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001748 * nand_do_read_oob - [Intern] NAND read out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * @mtd: MTD device structure
1750 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001751 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
1753 * NAND read out-of-band data from the spare area
1754 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001755static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1756 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001758 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001759 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001760 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001761 int readlen = ops->ooblen;
1762 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001763 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
vimal singh20d8e242009-07-07 15:49:49 +05301765 DEBUG(MTD_DEBUG_LEVEL3, "%s: from = 0x%08Lx, len = %i\n",
1766 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Adrian Hunter03736152007-01-31 17:58:29 +02001768 if (ops->mode == MTD_OOB_AUTO)
Vitaly Wool70145682006-11-03 18:20:38 +03001769 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001770 else
1771 len = mtd->oobsize;
1772
1773 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301774 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start read "
1775 "outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001776 return -EINVAL;
1777 }
1778
1779 /* Do not allow reads past end of device */
1780 if (unlikely(from >= mtd->size ||
1781 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1782 (from >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05301783 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read beyond end "
1784 "of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001785 return -EINVAL;
1786 }
Vitaly Wool70145682006-11-03 18:20:38 +03001787
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001788 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001789 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001791 /* Shift to get page */
1792 realpage = (int)(from >> chip->page_shift);
1793 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001795 while(1) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001796 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001797
1798 len = min(len, readlen);
1799 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001800
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001801 if (!(chip->options & NAND_NO_READRDY)) {
1802 /*
1803 * Apply delay or wait for ready/busy pin. Do this
1804 * before the AUTOINCR check, so no problems arise if a
1805 * chip which does auto increment is marked as
1806 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001807 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001808 if (!chip->dev_ready)
1809 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001810 else
1811 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001813
Vitaly Wool70145682006-11-03 18:20:38 +03001814 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001815 if (!readlen)
1816 break;
1817
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001818 /* Increment page address */
1819 realpage++;
1820
1821 page = realpage & chip->pagemask;
1822 /* Check, if we cross a chip boundary */
1823 if (!page) {
1824 chipnr++;
1825 chip->select_chip(mtd, -1);
1826 chip->select_chip(mtd, chipnr);
1827 }
1828
1829 /* Check, if the chip supports auto page increment
1830 * or if we have hit a block boundary.
1831 */
1832 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1833 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Vitaly Wool70145682006-11-03 18:20:38 +03001836 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
1838}
1839
1840/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001841 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 * @from: offset to read from
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001844 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001846 * NAND read data and/or out-of-band data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1849 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001851 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852 int ret = -ENOTSUPP;
1853
1854 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001857 if (ops->datbuf && (from + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05301858 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt read "
1859 "beyond end of device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return -EINVAL;
1861 }
1862
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001863 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001865 switch(ops->mode) {
1866 case MTD_OOB_PLACE:
1867 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001869 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001870
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001871 default:
1872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875 if (!ops->datbuf)
1876 ret = nand_do_read_oob(mtd, from, ops);
1877 else
1878 ret = nand_do_read_ops(mtd, from, ops);
1879
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001880 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001882 return ret;
1883}
1884
1885
1886/**
1887 * nand_write_page_raw - [Intern] raw page write function
1888 * @mtd: mtd info structure
1889 * @chip: nand chip info structure
1890 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001891 *
1892 * Not for syndrome calculating ecc controllers, which use a special oob layout
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001893 */
1894static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1895 const uint8_t *buf)
1896{
1897 chip->write_buf(mtd, buf, mtd->writesize);
1898 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001901/**
David Brownell52ff49d2009-03-04 12:01:36 -08001902 * nand_write_page_raw_syndrome - [Intern] raw page write function
1903 * @mtd: mtd info structure
1904 * @chip: nand chip info structure
1905 * @buf: data buffer
1906 *
1907 * We need a special oob layout and handling even when ECC isn't checked.
1908 */
1909static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1910 const uint8_t *buf)
1911{
1912 int eccsize = chip->ecc.size;
1913 int eccbytes = chip->ecc.bytes;
1914 uint8_t *oob = chip->oob_poi;
1915 int steps, size;
1916
1917 for (steps = chip->ecc.steps; steps > 0; steps--) {
1918 chip->write_buf(mtd, buf, eccsize);
1919 buf += eccsize;
1920
1921 if (chip->ecc.prepad) {
1922 chip->write_buf(mtd, oob, chip->ecc.prepad);
1923 oob += chip->ecc.prepad;
1924 }
1925
1926 chip->read_buf(mtd, oob, eccbytes);
1927 oob += eccbytes;
1928
1929 if (chip->ecc.postpad) {
1930 chip->write_buf(mtd, oob, chip->ecc.postpad);
1931 oob += chip->ecc.postpad;
1932 }
1933 }
1934
1935 size = mtd->oobsize - (oob - chip->oob_poi);
1936 if (size)
1937 chip->write_buf(mtd, oob, size);
1938}
1939/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001940 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001941 * @mtd: mtd info structure
1942 * @chip: nand chip info structure
1943 * @buf: data buffer
1944 */
1945static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1946 const uint8_t *buf)
1947{
1948 int i, eccsize = chip->ecc.size;
1949 int eccbytes = chip->ecc.bytes;
1950 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001951 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001952 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001953 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001954
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001955 /* Software ecc calculation */
1956 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1957 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001958
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001959 for (i = 0; i < chip->ecc.total; i++)
1960 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001961
Thomas Gleixner90424de2007-04-05 11:44:05 +02001962 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001963}
1964
1965/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001966 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001967 * @mtd: mtd info structure
1968 * @chip: nand chip info structure
1969 * @buf: data buffer
1970 */
1971static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1972 const uint8_t *buf)
1973{
1974 int i, eccsize = chip->ecc.size;
1975 int eccbytes = chip->ecc.bytes;
1976 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001977 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001978 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001979 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001980
1981 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1982 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001983 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001984 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1985 }
1986
1987 for (i = 0; i < chip->ecc.total; i++)
1988 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1989
1990 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1991}
1992
1993/**
Artem Bityutskiyd29ebdb2006-10-19 16:04:02 +03001994 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001995 * @mtd: mtd info structure
1996 * @chip: nand chip info structure
1997 * @buf: data buffer
1998 *
1999 * The hw generator calculates the error syndrome automatically. Therefor
2000 * we need a special oob layout and handling.
2001 */
2002static void nand_write_page_syndrome(struct mtd_info *mtd,
2003 struct nand_chip *chip, const uint8_t *buf)
2004{
2005 int i, eccsize = chip->ecc.size;
2006 int eccbytes = chip->ecc.bytes;
2007 int eccsteps = chip->ecc.steps;
2008 const uint8_t *p = buf;
2009 uint8_t *oob = chip->oob_poi;
2010
2011 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2012
2013 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2014 chip->write_buf(mtd, p, eccsize);
2015
2016 if (chip->ecc.prepad) {
2017 chip->write_buf(mtd, oob, chip->ecc.prepad);
2018 oob += chip->ecc.prepad;
2019 }
2020
2021 chip->ecc.calculate(mtd, p, oob);
2022 chip->write_buf(mtd, oob, eccbytes);
2023 oob += eccbytes;
2024
2025 if (chip->ecc.postpad) {
2026 chip->write_buf(mtd, oob, chip->ecc.postpad);
2027 oob += chip->ecc.postpad;
2028 }
2029 }
2030
2031 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002032 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002033 if (i)
2034 chip->write_buf(mtd, oob, i);
2035}
2036
2037/**
David Woodhouse956e9442006-09-25 17:12:39 +01002038 * nand_write_page - [REPLACEABLE] write one page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002039 * @mtd: MTD device structure
2040 * @chip: NAND chip descriptor
2041 * @buf: the data to write
2042 * @page: page number to write
2043 * @cached: cached programming
Jesper Juhlefbfe96c2006-10-27 23:24:47 +02002044 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002045 */
2046static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01002047 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002048{
2049 int status;
2050
2051 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2052
David Woodhouse956e9442006-09-25 17:12:39 +01002053 if (unlikely(raw))
2054 chip->ecc.write_page_raw(mtd, chip, buf);
2055 else
2056 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002057
2058 /*
2059 * Cached progamming disabled for now, Not sure if its worth the
2060 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
2061 */
2062 cached = 0;
2063
2064 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2065
2066 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002067 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002068 /*
2069 * See if operation failed and additional status checks are
2070 * available
2071 */
2072 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2073 status = chip->errstat(mtd, chip, FL_WRITING, status,
2074 page);
2075
2076 if (status & NAND_STATUS_FAIL)
2077 return -EIO;
2078 } else {
2079 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002080 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002081 }
2082
2083#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2084 /* Send command to read back the data */
2085 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2086
2087 if (chip->verify_buf(mtd, buf, mtd->writesize))
2088 return -EIO;
2089#endif
2090 return 0;
2091}
2092
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002093/**
2094 * nand_fill_oob - [Internal] Transfer client buffer to oob
2095 * @chip: nand chip structure
2096 * @oob: oob data buffer
2097 * @ops: oob ops structure
2098 */
Maxim Levitsky782ce792010-02-22 20:39:36 +02002099static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
2100 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002101{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002102 switch(ops->mode) {
2103
2104 case MTD_OOB_PLACE:
2105 case MTD_OOB_RAW:
2106 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2107 return oob + len;
2108
2109 case MTD_OOB_AUTO: {
2110 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002111 uint32_t boffs = 0, woffs = ops->ooboffs;
2112 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002113
2114 for(; free->length && len; free++, len -= bytes) {
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002115 /* Write request not from offset 0 ? */
2116 if (unlikely(woffs)) {
2117 if (woffs >= free->length) {
2118 woffs -= free->length;
2119 continue;
2120 }
2121 boffs = free->offset + woffs;
2122 bytes = min_t(size_t, len,
2123 (free->length - woffs));
2124 woffs = 0;
2125 } else {
2126 bytes = min_t(size_t, len, free->length);
2127 boffs = free->offset;
2128 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002129 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002130 oob += bytes;
2131 }
2132 return oob;
2133 }
2134 default:
2135 BUG();
2136 }
2137 return NULL;
2138}
2139
Thomas Gleixner29072b92006-09-28 15:38:36 +02002140#define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002141
2142/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002143 * nand_do_write_ops - [Internal] NAND write with ECC
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002144 * @mtd: MTD device structure
2145 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002146 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002147 *
2148 * NAND write with ECC
2149 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002150static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2151 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002152{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002153 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002155 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002156
2157 uint32_t oobwritelen = ops->ooblen;
2158 uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
2159 mtd->oobavail : mtd->oobsize;
2160
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002161 uint8_t *oob = ops->oobbuf;
2162 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002163 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002164
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002166 if (!writelen)
2167 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002168
2169 /* reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002170 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302171 printk(KERN_NOTICE "%s: Attempt to write not "
2172 "page aligned data\n", __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002173 return -EINVAL;
2174 }
2175
Thomas Gleixner29072b92006-09-28 15:38:36 +02002176 column = to & (mtd->writesize - 1);
2177 subpage = column || (writelen & (mtd->writesize - 1));
2178
2179 if (subpage && oob)
2180 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002181
Thomas Gleixner6a930962006-06-28 00:11:45 +02002182 chipnr = (int)(to >> chip->chip_shift);
2183 chip->select_chip(mtd, chipnr);
2184
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002185 /* Check, if it is write protected */
2186 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002187 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002188
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002189 realpage = (int)(to >> chip->page_shift);
2190 page = realpage & chip->pagemask;
2191 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2192
2193 /* Invalidate the page cache, when we write to the cached page */
2194 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002195 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002196 chip->pagebuf = -1;
2197
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01002198 /* If we're not given explicit OOB data, let it be 0xFF */
2199 if (likely(!oob))
2200 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002201
Maxim Levitsky782ce792010-02-22 20:39:36 +02002202 /* Don't allow multipage oob writes with offset */
2203 if (ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2204 return -EINVAL;
2205
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002206 while(1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002207 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002208 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002209 uint8_t *wbuf = buf;
2210
2211 /* Partial page write ? */
2212 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2213 cached = 0;
2214 bytes = min_t(int, bytes - column, (int) writelen);
2215 chip->pagebuf = -1;
2216 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2217 memcpy(&chip->buffers->databuf[column], buf, bytes);
2218 wbuf = chip->buffers->databuf;
2219 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002220
Maxim Levitsky782ce792010-02-22 20:39:36 +02002221 if (unlikely(oob)) {
2222 size_t len = min(oobwritelen, oobmaxlen);
2223 oob = nand_fill_oob(chip, oob, len, ops);
2224 oobwritelen -= len;
2225 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002226
Thomas Gleixner29072b92006-09-28 15:38:36 +02002227 ret = chip->write_page(mtd, chip, wbuf, page, cached,
David Woodhouse956e9442006-09-25 17:12:39 +01002228 (ops->mode == MTD_OOB_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002229 if (ret)
2230 break;
2231
2232 writelen -= bytes;
2233 if (!writelen)
2234 break;
2235
Thomas Gleixner29072b92006-09-28 15:38:36 +02002236 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237 buf += bytes;
2238 realpage++;
2239
2240 page = realpage & chip->pagemask;
2241 /* Check, if we cross a chip boundary */
2242 if (!page) {
2243 chipnr++;
2244 chip->select_chip(mtd, -1);
2245 chip->select_chip(mtd, chipnr);
2246 }
2247 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002248
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002249 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002250 if (unlikely(oob))
2251 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002252 return ret;
2253}
2254
2255/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002256 * panic_nand_write - [MTD Interface] NAND write with ECC
2257 * @mtd: MTD device structure
2258 * @to: offset to write to
2259 * @len: number of bytes to write
2260 * @retlen: pointer to variable to store the number of written bytes
2261 * @buf: the data to write
2262 *
2263 * NAND write with ECC. Used when performing writes in interrupt context, this
2264 * may for example be called by mtdoops when writing an oops while in panic.
2265 */
2266static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2267 size_t *retlen, const uint8_t *buf)
2268{
2269 struct nand_chip *chip = mtd->priv;
2270 int ret;
2271
2272 /* Do not allow reads past end of device */
2273 if ((to + len) > mtd->size)
2274 return -EINVAL;
2275 if (!len)
2276 return 0;
2277
2278 /* Wait for the device to get ready. */
2279 panic_nand_wait(mtd, chip, 400);
2280
2281 /* Grab the device. */
2282 panic_nand_get_device(chip, mtd, FL_WRITING);
2283
2284 chip->ops.len = len;
2285 chip->ops.datbuf = (uint8_t *)buf;
2286 chip->ops.oobbuf = NULL;
2287
2288 ret = nand_do_write_ops(mtd, to, &chip->ops);
2289
2290 *retlen = chip->ops.retlen;
2291 return ret;
2292}
2293
2294/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002295 * nand_write - [MTD Interface] NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 * @mtd: MTD device structure
2297 * @to: offset to write to
2298 * @len: number of bytes to write
2299 * @retlen: pointer to variable to store the number of written bytes
2300 * @buf: the data to write
2301 *
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002302 * NAND write with ECC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002304static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002305 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002307 struct nand_chip *chip = mtd->priv;
2308 int ret;
2309
2310 /* Do not allow reads past end of device */
2311 if ((to + len) > mtd->size)
2312 return -EINVAL;
2313 if (!len)
2314 return 0;
2315
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002316 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002317
2318 chip->ops.len = len;
2319 chip->ops.datbuf = (uint8_t *)buf;
2320 chip->ops.oobbuf = NULL;
2321
2322 ret = nand_do_write_ops(mtd, to, &chip->ops);
2323
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002324 *retlen = chip->ops.retlen;
2325
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002326 nand_release_device(mtd);
2327
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328 return ret;
2329}
2330
2331/**
2332 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2333 * @mtd: MTD device structure
2334 * @to: offset to write to
2335 * @ops: oob operation description structure
2336 *
2337 * NAND write out-of-band
2338 */
2339static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2340 struct mtd_oob_ops *ops)
2341{
Adrian Hunter03736152007-01-31 17:58:29 +02002342 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002343 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
vimal singh20d8e242009-07-07 15:49:49 +05302345 DEBUG(MTD_DEBUG_LEVEL3, "%s: to = 0x%08x, len = %i\n",
2346 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Adrian Hunter03736152007-01-31 17:58:29 +02002348 if (ops->mode == MTD_OOB_AUTO)
2349 len = chip->ecc.layout->oobavail;
2350 else
2351 len = mtd->oobsize;
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002354 if ((ops->ooboffs + ops->ooblen) > len) {
vimal singh20d8e242009-07-07 15:49:49 +05302355 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to write "
2356 "past end of page\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return -EINVAL;
2358 }
2359
Adrian Hunter03736152007-01-31 17:58:29 +02002360 if (unlikely(ops->ooboffs >= len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302361 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt to start "
2362 "write outside oob\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002363 return -EINVAL;
2364 }
2365
2366 /* Do not allow reads past end of device */
2367 if (unlikely(to >= mtd->size ||
2368 ops->ooboffs + ops->ooblen >
2369 ((mtd->size >> chip->page_shift) -
2370 (to >> chip->page_shift)) * len)) {
vimal singh20d8e242009-07-07 15:49:49 +05302371 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2372 "end of device\n", __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002373 return -EINVAL;
2374 }
2375
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002376 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002377 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002379 /* Shift to get page */
2380 page = (int)(to >> chip->page_shift);
2381
2382 /*
2383 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2384 * of my DiskOnChip 2000 test units) will clear the whole data page too
2385 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2386 * it in the doc2000 driver in August 1999. dwmw2.
2387 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002388 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* Check, if it is write protected */
2391 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002392 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002395 if (page == chip->pagebuf)
2396 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002398 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002399 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002400 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2401 memset(chip->oob_poi, 0xff, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002402
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002403 if (status)
2404 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Vitaly Wool70145682006-11-03 18:20:38 +03002406 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002408 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002409}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002411/**
2412 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2413 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002414 * @to: offset to write to
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002415 * @ops: oob operation description structure
2416 */
2417static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2418 struct mtd_oob_ops *ops)
2419{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002420 struct nand_chip *chip = mtd->priv;
2421 int ret = -ENOTSUPP;
2422
2423 ops->retlen = 0;
2424
2425 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002426 if (ops->datbuf && (to + ops->len) > mtd->size) {
vimal singh20d8e242009-07-07 15:49:49 +05302427 DEBUG(MTD_DEBUG_LEVEL0, "%s: Attempt write beyond "
2428 "end of device\n", __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002429 return -EINVAL;
2430 }
2431
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002432 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002433
2434 switch(ops->mode) {
2435 case MTD_OOB_PLACE:
2436 case MTD_OOB_AUTO:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002437 case MTD_OOB_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002438 break;
2439
2440 default:
2441 goto out;
2442 }
2443
2444 if (!ops->datbuf)
2445 ret = nand_do_write_oob(mtd, to, ops);
2446 else
2447 ret = nand_do_write_ops(mtd, to, ops);
2448
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002449 out:
2450 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return ret;
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2456 * @mtd: MTD device structure
2457 * @page: the page address of the block which will be erased
2458 *
2459 * Standard erase command for NAND chips
2460 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002461static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002463 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002465 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2466 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
2469/**
2470 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2471 * @mtd: MTD device structure
2472 * @page: the page address of the block which will be erased
2473 *
2474 * AND multi block erase command function
2475 * Erase 4 consecutive blocks
2476 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002477static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002479 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002481 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2482 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2483 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2484 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2485 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
2488/**
2489 * nand_erase - [MTD Interface] erase block(s)
2490 * @mtd: MTD device structure
2491 * @instr: erase instruction
2492 *
2493 * Erase one ore more blocks
2494 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002495static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
David Woodhousee0c7d762006-05-13 18:07:53 +01002497 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002499
David A. Marlin30f464b2005-01-17 18:35:25 +00002500#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002502 * nand_erase_nand - [Internal] erase block(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 * @mtd: MTD device structure
2504 * @instr: erase instruction
2505 * @allowbbt: allow erasing the bbt area
2506 *
2507 * Erase one ore more blocks
2508 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002509int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2510 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Adrian Hunter69423d92008-12-10 13:37:21 +00002512 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002513 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002514 loff_t rewrite_bbt[NAND_MAX_CHIPS]={0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002515 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002516 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
vimal singh20d8e242009-07-07 15:49:49 +05302518 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
2519 __func__, (unsigned long long)instr->addr,
2520 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302522 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002525 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002531 page = (int)(instr->addr >> chip->page_shift);
2532 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002535 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002538 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /* Check, if it is write protected */
2541 if (nand_check_wp(mtd)) {
vimal singh20d8e242009-07-07 15:49:49 +05302542 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
2543 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 instr->state = MTD_ERASE_FAILED;
2545 goto erase_exit;
2546 }
2547
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002548 /*
2549 * If BBT requires refresh, set the BBT page mask to see if the BBT
2550 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2551 * can not be matched. This is also done when the bbt is actually
2552 * erased to avoid recusrsive updates
2553 */
2554 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2555 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /* Loop through the pages */
2558 len = instr->len;
2559
2560 instr->state = MTD_ERASING;
2561
2562 while (len) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002563 /*
2564 * heck if we have a bad block, we do not erase bad blocks !
2565 */
2566 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2567 chip->page_shift, 0, allowbbt)) {
vimal singh20d8e242009-07-07 15:49:49 +05302568 printk(KERN_WARNING "%s: attempt to erase a bad block "
2569 "at page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 instr->state = MTD_ERASE_FAILED;
2571 goto erase_exit;
2572 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002573
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002574 /*
2575 * Invalidate the page cache, if we erase the block which
2576 * contains the current cached page
2577 */
2578 if (page <= chip->pagebuf && chip->pagebuf <
2579 (page + pages_per_block))
2580 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002582 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002583
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002584 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002586 /*
2587 * See if operation failed and additional status checks are
2588 * available
2589 */
2590 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2591 status = chip->errstat(mtd, chip, FL_ERASING,
2592 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002595 if (status & NAND_STATUS_FAIL) {
vimal singh20d8e242009-07-07 15:49:49 +05302596 DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed erase, "
2597 "page 0x%08x\n", __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002599 instr->fail_addr =
2600 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 goto erase_exit;
2602 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002603
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002604 /*
2605 * If BBT requires refresh, set the BBT rewrite flag to the
2606 * page being erased
2607 */
2608 if (bbt_masked_page != 0xffffffff &&
2609 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002610 rewrite_bbt[chipnr] =
2611 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002614 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 page += pages_per_block;
2616
2617 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002618 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002620 chip->select_chip(mtd, -1);
2621 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002622
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002623 /*
2624 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2625 * page mask to see if this BBT should be rewritten
2626 */
2627 if (bbt_masked_page != 0xffffffff &&
2628 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2629 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2630 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 }
2633 instr->state = MTD_ERASE_DONE;
2634
David Woodhousee0c7d762006-05-13 18:07:53 +01002635 erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 /* Deselect and wake up anyone waiting on the device */
2640 nand_release_device(mtd);
2641
David Woodhouse49defc02007-10-06 15:01:59 -04002642 /* Do call back function */
2643 if (!ret)
2644 mtd_erase_callback(instr);
2645
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002646 /*
2647 * If BBT requires refresh and erase was successful, rewrite any
2648 * selected bad block tables
2649 */
2650 if (bbt_masked_page == 0xffffffff || ret)
2651 return ret;
2652
2653 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2654 if (!rewrite_bbt[chipnr])
2655 continue;
2656 /* update the BBT for chip */
vimal singh20d8e242009-07-07 15:49:49 +05302657 DEBUG(MTD_DEBUG_LEVEL0, "%s: nand_update_bbt "
2658 "(%d:0x%0llx 0x%0x)\n", __func__, chipnr,
2659 rewrite_bbt[chipnr], chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002660 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002661 }
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /* Return more or less happy */
2664 return ret;
2665}
2666
2667/**
2668 * nand_sync - [MTD Interface] sync
2669 * @mtd: MTD device structure
2670 *
2671 * Sync is actually a wait for chip ready function
2672 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002673static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002675 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
vimal singh20d8e242009-07-07 15:49:49 +05302677 DEBUG(MTD_DEBUG_LEVEL3, "%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002680 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002682 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002686 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 * @mtd: MTD device structure
Randy Dunlap844d3b42006-06-28 21:48:27 -07002688 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002690static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
2692 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002693 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002695
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002696 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002700 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * @mtd: MTD device structure
2702 * @ofs: offset relative to mtd start
2703 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002704static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002706 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 int ret;
2708
David Woodhousee0c7d762006-05-13 18:07:53 +01002709 if ((ret = nand_block_isbad(mtd, ofs))) {
2710 /* If it was bad already, return success and do nothing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (ret > 0)
2712 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002713 return ret;
2714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002716 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
2719/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002720 * nand_suspend - [MTD Interface] Suspend the NAND flash
2721 * @mtd: MTD device structure
2722 */
2723static int nand_suspend(struct mtd_info *mtd)
2724{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002725 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002726
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002727 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002728}
2729
2730/**
2731 * nand_resume - [MTD Interface] Resume the NAND flash
2732 * @mtd: MTD device structure
2733 */
2734static void nand_resume(struct mtd_info *mtd)
2735{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002736 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002737
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002738 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002739 nand_release_device(mtd);
2740 else
vimal singh20d8e242009-07-07 15:49:49 +05302741 printk(KERN_ERR "%s called for a chip which is not "
2742 "in suspended state\n", __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002743}
2744
Thomas Gleixnera36ed292006-05-23 11:37:03 +02002745/*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002746 * Set default functions
2747 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002748static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002751 if (!chip->chip_delay)
2752 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002755 if (chip->cmdfunc == NULL)
2756 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002759 if (chip->waitfunc == NULL)
2760 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002762 if (!chip->select_chip)
2763 chip->select_chip = nand_select_chip;
2764 if (!chip->read_byte)
2765 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2766 if (!chip->read_word)
2767 chip->read_word = nand_read_word;
2768 if (!chip->block_bad)
2769 chip->block_bad = nand_block_bad;
2770 if (!chip->block_markbad)
2771 chip->block_markbad = nand_default_block_markbad;
2772 if (!chip->write_buf)
2773 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2774 if (!chip->read_buf)
2775 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2776 if (!chip->verify_buf)
2777 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2778 if (!chip->scan_bbt)
2779 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002780
2781 if (!chip->controller) {
2782 chip->controller = &chip->hwcontrol;
2783 spin_lock_init(&chip->controller->lock);
2784 init_waitqueue_head(&chip->controller->wq);
2785 }
2786
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002787}
2788
2789/*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002790 * Get the flash and manufacturer id and lookup if the type is supported
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002791 */
2792static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002793 struct nand_chip *chip,
David Woodhouse5e81e882010-02-26 18:32:56 +00002794 int busw, int *maf_id,
2795 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002796{
Kevin Cernekee426c4572010-05-04 20:58:03 -07002797 int i, dev_id, maf_idx;
2798 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002801 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Karl Beldanef89a882008-09-15 14:37:29 +02002803 /*
2804 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2805 * after power-up
2806 */
2807 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002810 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002813 *maf_id = chip->read_byte(mtd);
2814 dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Ben Dooksed8165c2008-04-14 14:58:58 +01002816 /* Try again to make sure, as some systems the bus-hold or other
2817 * interface concerns can cause random data which looks like a
2818 * possibly credible NAND flash to appear. If the two results do
2819 * not match, ignore the device completely.
2820 */
2821
2822 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2823
Kevin Cernekee426c4572010-05-04 20:58:03 -07002824 /* Read entire ID string */
Ben Dooksed8165c2008-04-14 14:58:58 +01002825
Kevin Cernekee426c4572010-05-04 20:58:03 -07002826 for (i = 0; i < 8; i++)
2827 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01002828
Kevin Cernekee426c4572010-05-04 20:58:03 -07002829 if (id_data[0] != *maf_id || id_data[1] != dev_id) {
Ben Dooksed8165c2008-04-14 14:58:58 +01002830 printk(KERN_INFO "%s: second ID read did not match "
2831 "%02x,%02x against %02x,%02x\n", __func__,
Kevin Cernekee426c4572010-05-04 20:58:03 -07002832 *maf_id, dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01002833 return ERR_PTR(-ENODEV);
2834 }
2835
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002836 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00002837 type = nand_flash_ids;
2838
2839 for (; type->name != NULL; type++)
2840 if (dev_id == type->id)
2841 break;
2842
2843 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002844 return ERR_PTR(-ENODEV);
2845
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002846 if (!mtd->name)
2847 mtd->name = type->name;
2848
Adrian Hunter69423d92008-12-10 13:37:21 +00002849 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002850
2851 /* Newer devices have all the information in additional id bytes */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002852 if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002853 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002854 /* The 3rd id byte holds MLC / multichip data */
Kevin Cernekee426c4572010-05-04 20:58:03 -07002855 chip->cellinfo = id_data[2];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002856 /* The 4th id byte is the important one */
Kevin Cernekee426c4572010-05-04 20:58:03 -07002857 extid = id_data[3];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002858
Kevin Cernekee426c4572010-05-04 20:58:03 -07002859 /*
2860 * Field definitions are in the following datasheets:
2861 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
2862 * New style (6 byte ID): Samsung K9GAG08U0D (p.40)
2863 *
2864 * Check for wraparound + Samsung ID + nonzero 6th byte
2865 * to decide what to do.
2866 */
2867 if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
2868 id_data[0] == NAND_MFR_SAMSUNG &&
2869 id_data[5] != 0x00) {
2870 /* Calc pagesize */
2871 mtd->writesize = 2048 << (extid & 0x03);
2872 extid >>= 2;
2873 /* Calc oobsize */
2874 mtd->oobsize = (extid & 0x03) == 0x01 ? 128 : 218;
2875 extid >>= 2;
2876 /* Calc blocksize */
2877 mtd->erasesize = (128 * 1024) <<
2878 (((extid >> 1) & 0x04) | (extid & 0x03));
2879 busw = 0;
2880 } else {
2881 /* Calc pagesize */
2882 mtd->writesize = 1024 << (extid & 0x03);
2883 extid >>= 2;
2884 /* Calc oobsize */
2885 mtd->oobsize = (8 << (extid & 0x01)) *
2886 (mtd->writesize >> 9);
2887 extid >>= 2;
2888 /* Calc blocksize. Blocksize is multiples of 64KiB */
2889 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2890 extid >>= 2;
2891 /* Get buswidth information */
2892 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2893 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002894 } else {
2895 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002896 * Old devices have chip data hardcoded in the device id table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002897 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002898 mtd->erasesize = type->erasesize;
2899 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02002900 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002901 busw = type->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002902 }
2903
2904 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01002905 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002906 if (nand_manuf_ids[maf_idx].id == *maf_id)
2907 break;
2908 }
2909
2910 /*
2911 * Check, if buswidth is correct. Hardware drivers should set
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002912 * chip correct !
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002913 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002914 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002915 printk(KERN_INFO "NAND device: Manufacturer ID:"
2916 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2917 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2918 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002919 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002920 busw ? 16 : 8);
2921 return ERR_PTR(-EINVAL);
2922 }
2923
2924 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002925 chip->page_shift = ffs(mtd->writesize) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002926 /* Convert chipsize to number of pages per chip -1. */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002927 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002928
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002929 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002930 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00002931 if (chip->chipsize & 0xffffffff)
2932 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2933 else
2934 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 32 - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002935
2936 /* Set the bad block position */
Brian Norrisc7b28e22010-07-13 15:13:00 -07002937 if (!(busw & NAND_BUSWIDTH_16) && (*maf_id == NAND_MFR_STMICRO ||
2938 (*maf_id == NAND_MFR_SAMSUNG &&
2939 mtd->writesize == 512) ||
2940 *maf_id == NAND_MFR_AMD))
2941 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
2942 else
2943 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
2944
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002945
2946 /* Get chip options, preserve non chip based options */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002947 chip->options &= ~NAND_CHIPOPTIONS_MSK;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002948 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002949
2950 /*
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002951 * Set chip as a default. Board drivers can override it, if necessary
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002952 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002954
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002955 /* Check if chip is a not a samsung device. Do not clear the
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002956 * options for chips which are not having an extended id.
2957 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02002958 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002959 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002960
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07002961 /*
2962 * Bad block marker is stored in the last page of each block
Brian Norrisc7b28e22010-07-13 15:13:00 -07002963 * on Samsung and Hynix MLC devices; stored in first two pages
2964 * of each block on Micron devices with 2KiB pages and on
2965 * SLC Samsung, Hynix, and AMD/Spansion. All others scan only
2966 * the first page.
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07002967 */
2968 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2969 (*maf_id == NAND_MFR_SAMSUNG ||
2970 *maf_id == NAND_MFR_HYNIX))
Brian Norris30fe8112010-06-23 13:36:02 -07002971 chip->options |= NAND_BBT_SCANLASTPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07002972 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
2973 (*maf_id == NAND_MFR_SAMSUNG ||
2974 *maf_id == NAND_MFR_HYNIX ||
2975 *maf_id == NAND_MFR_AMD)) ||
2976 (mtd->writesize == 2048 &&
2977 *maf_id == NAND_MFR_MICRON))
2978 chip->options |= NAND_BBT_SCAN2NDPAGE;
2979
Brian Norris58373ff2010-07-15 12:15:44 -07002980 /*
2981 * Numonyx/ST 2K pages, x8 bus use BOTH byte 1 and 6
2982 */
2983 if (!(busw & NAND_BUSWIDTH_16) &&
2984 *maf_id == NAND_MFR_STMICRO &&
2985 mtd->writesize == 2048) {
2986 chip->options |= NAND_BBT_SCANBYTE1AND6;
2987 chip->badblockpos = 0;
2988 }
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07002989
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002990 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002991 if (chip->options & NAND_4PAGE_ARRAY)
2992 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002993 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002994 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002995
2996 /* Do not replace user supplied command function ! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002997 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2998 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002999
3000 printk(KERN_INFO "NAND device: Manufacturer ID:"
3001 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
3002 nand_manuf_ids[maf_idx].name, type->name);
3003
3004 return type;
3005}
3006
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003007/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003008 * nand_scan_ident - [NAND Interface] Scan for the NAND device
3009 * @mtd: MTD device structure
3010 * @maxchips: Number of chips to scan for
David Woodhouse5e81e882010-02-26 18:32:56 +00003011 * @table: Alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003012 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003013 * This is the first phase of the normal nand_scan() function. It
3014 * reads the flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003015 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003016 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003017 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003018int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3019 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003020{
3021 int i, busw, nand_maf_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003022 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003023 struct nand_flash_dev *type;
3024
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003025 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003026 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003027 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003028 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003029
3030 /* Read the flash type */
David Woodhouse5e81e882010-02-26 18:32:56 +00003031 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003032
3033 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003034 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3035 printk(KERN_WARNING "No NAND device found.\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003036 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003037 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003040 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003041 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003042 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003043 /* See comment in nand_get_flash_type for reset */
3044 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003046 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003048 if (nand_maf_id != chip->read_byte(mtd) ||
3049 type->id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 break;
3051 }
3052 if (i > 1)
3053 printk(KERN_INFO "%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003056 chip->numchips = i;
3057 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
David Woodhouse3b85c322006-09-25 17:06:53 +01003059 return 0;
3060}
3061
3062
3063/**
3064 * nand_scan_tail - [NAND Interface] Scan for the NAND device
3065 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003066 *
3067 * This is the second phase of the normal nand_scan() function. It
3068 * fills out all the uninitialized function pointers with the defaults
3069 * and scans for a bad block table if appropriate.
3070 */
3071int nand_scan_tail(struct mtd_info *mtd)
3072{
3073 int i;
3074 struct nand_chip *chip = mtd->priv;
3075
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003076 if (!(chip->options & NAND_OWN_BUFFERS))
3077 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3078 if (!chip->buffers)
3079 return -ENOMEM;
3080
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003081 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003082 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003083
3084 /*
3085 * If no default placement scheme is given, select an appropriate one
3086 */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003087 if (!chip->ecc.layout) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003088 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003090 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 break;
3092 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003093 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 break;
3095 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003096 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003098 case 128:
3099 chip->ecc.layout = &nand_oob_128;
3100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003102 printk(KERN_WARNING "No oob scheme defined for "
3103 "oobsize %d\n", mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 BUG();
3105 }
3106 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003107
David Woodhouse956e9442006-09-25 17:12:39 +01003108 if (!chip->write_page)
3109 chip->write_page = nand_write_page;
3110
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003111 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003112 * check ECC mode, default to software if 3byte/512byte hardware ECC is
3113 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003114 */
David Woodhouse956e9442006-09-25 17:12:39 +01003115
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003116 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003117 case NAND_ECC_HW_OOB_FIRST:
3118 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3119 if (!chip->ecc.calculate || !chip->ecc.correct ||
3120 !chip->ecc.hwctl) {
3121 printk(KERN_WARNING "No ECC functions supplied; "
3122 "Hardware ECC not possible\n");
3123 BUG();
3124 }
3125 if (!chip->ecc.read_page)
3126 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3127
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003128 case NAND_ECC_HW:
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003129 /* Use standard hwecc read page function ? */
3130 if (!chip->ecc.read_page)
3131 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003132 if (!chip->ecc.write_page)
3133 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003134 if (!chip->ecc.read_page_raw)
3135 chip->ecc.read_page_raw = nand_read_page_raw;
3136 if (!chip->ecc.write_page_raw)
3137 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003138 if (!chip->ecc.read_oob)
3139 chip->ecc.read_oob = nand_read_oob_std;
3140 if (!chip->ecc.write_oob)
3141 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003142
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003143 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003144 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3145 !chip->ecc.hwctl) &&
3146 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003147 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003148 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003149 chip->ecc.write_page == nand_write_page_hwecc)) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003150 printk(KERN_WARNING "No ECC functions supplied; "
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003151 "Hardware ECC not possible\n");
3152 BUG();
3153 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003154 /* Use standard syndrome read/write page function ? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003155 if (!chip->ecc.read_page)
3156 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003157 if (!chip->ecc.write_page)
3158 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003159 if (!chip->ecc.read_page_raw)
3160 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3161 if (!chip->ecc.write_page_raw)
3162 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003163 if (!chip->ecc.read_oob)
3164 chip->ecc.read_oob = nand_read_oob_syndrome;
3165 if (!chip->ecc.write_oob)
3166 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003167
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003168 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003169 break;
3170 printk(KERN_WARNING "%d byte HW ECC not possible on "
3171 "%d byte page size, fallback to SW ECC\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003172 chip->ecc.size, mtd->writesize);
3173 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003175 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003176 chip->ecc.calculate = nand_calculate_ecc;
3177 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003178 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003179 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003180 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003181 chip->ecc.read_page_raw = nand_read_page_raw;
3182 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003183 chip->ecc.read_oob = nand_read_oob_std;
3184 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003185 if (!chip->ecc.size)
3186 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003187 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003189
3190 case NAND_ECC_NONE:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003191 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
3192 "This is not recommended !!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003193 chip->ecc.read_page = nand_read_page_raw;
3194 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003195 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003196 chip->ecc.read_page_raw = nand_read_page_raw;
3197 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003198 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003199 chip->ecc.size = mtd->writesize;
3200 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 default:
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003204 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003205 chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003206 BUG();
3207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003209 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003210 * The number of bytes available for a client to place data into
3211 * the out of band area
3212 */
3213 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003214 for (i = 0; chip->ecc.layout->oobfree[i].length
3215 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003216 chip->ecc.layout->oobavail +=
3217 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003218 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003219
3220 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003221 * Set the number of read / write steps for one page depending on ECC
3222 * mode
3223 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003224 chip->ecc.steps = mtd->writesize / chip->ecc.size;
3225 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003226 printk(KERN_WARNING "Invalid ecc parameters\n");
3227 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003229 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003230
Thomas Gleixner29072b92006-09-28 15:38:36 +02003231 /*
3232 * Allow subpage writes up to ecc.steps. Not possible for MLC
3233 * FLASH.
3234 */
3235 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3236 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3237 switch(chip->ecc.steps) {
3238 case 2:
3239 mtd->subpage_sft = 1;
3240 break;
3241 case 4:
3242 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003243 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003244 mtd->subpage_sft = 2;
3245 break;
3246 }
3247 }
3248 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3249
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003250 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003251 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003254 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
3256 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003257 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 /* Fill in remaining MTD driver data */
3260 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003261 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3262 MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 mtd->erase = nand_erase;
3264 mtd->point = NULL;
3265 mtd->unpoint = NULL;
3266 mtd->read = nand_read;
3267 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003268 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 mtd->read_oob = nand_read_oob;
3270 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 mtd->sync = nand_sync;
3272 mtd->lock = NULL;
3273 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01003274 mtd->suspend = nand_suspend;
3275 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 mtd->block_isbad = nand_block_isbad;
3277 mtd->block_markbad = nand_block_markbad;
3278
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003279 /* propagate ecc.layout to mtd_info */
3280 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003282 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003283 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003287 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
3289
Rusty Russella6e6abd2009-03-31 13:05:31 -06003290/* is_module_text_address() isn't exported, and it's mostly a pointless
David Woodhouse3b85c322006-09-25 17:06:53 +01003291 test if this is a module _anyway_ -- they'd have to try _really_ hard
3292 to call us from in-kernel code if the core NAND support is modular. */
3293#ifdef MODULE
3294#define caller_is_module() (1)
3295#else
3296#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003297 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003298#endif
3299
3300/**
3301 * nand_scan - [NAND Interface] Scan for the NAND device
3302 * @mtd: MTD device structure
3303 * @maxchips: Number of chips to scan for
3304 *
3305 * This fills out all the uninitialized function pointers
3306 * with the defaults.
3307 * The flash ID is read and the mtd/chip structures are
3308 * filled with the appropriate values.
3309 * The mtd->owner field must be set to the module of the caller
3310 *
3311 */
3312int nand_scan(struct mtd_info *mtd, int maxchips)
3313{
3314 int ret;
3315
3316 /* Many callers got this wrong, so check for it for a while... */
3317 if (!mtd->owner && caller_is_module()) {
vimal singh20d8e242009-07-07 15:49:49 +05303318 printk(KERN_CRIT "%s called with NULL mtd->owner!\n",
3319 __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003320 BUG();
3321 }
3322
David Woodhouse5e81e882010-02-26 18:32:56 +00003323 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003324 if (!ret)
3325 ret = nand_scan_tail(mtd);
3326 return ret;
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003330 * nand_release - [NAND Interface] Free resources held by the NAND device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 * @mtd: MTD device structure
3332*/
David Woodhousee0c7d762006-05-13 18:07:53 +01003333void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003335 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337#ifdef CONFIG_MTD_PARTITIONS
3338 /* Deregister partitions */
David Woodhousee0c7d762006-05-13 18:07:53 +01003339 del_mtd_partitions(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340#endif
3341 /* Deregister the device */
David Woodhousee0c7d762006-05-13 18:07:53 +01003342 del_mtd_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Jesper Juhlfa671642005-11-07 01:01:27 -08003344 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003345 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003346 if (!(chip->options & NAND_OWN_BUFFERS))
3347 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003348
3349 /* Free bad block descriptor memory */
3350 if (chip->badblock_pattern && chip->badblock_pattern->options
3351 & NAND_BBT_DYNAMICSTRUCT)
3352 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Vimal Singh7d70f332010-02-08 15:50:49 +05303355EXPORT_SYMBOL_GPL(nand_lock);
3356EXPORT_SYMBOL_GPL(nand_unlock);
David Woodhousee0c7d762006-05-13 18:07:53 +01003357EXPORT_SYMBOL_GPL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003358EXPORT_SYMBOL_GPL(nand_scan_ident);
3359EXPORT_SYMBOL_GPL(nand_scan_tail);
David Woodhousee0c7d762006-05-13 18:07:53 +01003360EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003361
3362static int __init nand_base_init(void)
3363{
3364 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3365 return 0;
3366}
3367
3368static void __exit nand_base_exit(void)
3369{
3370 led_trigger_unregister_simple(nand_led_trigger);
3371}
3372
3373module_init(nand_base_init);
3374module_exit(nand_base_exit);
3375
David Woodhousee0c7d762006-05-13 18:07:53 +01003376MODULE_LICENSE("GPL");
3377MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
3378MODULE_DESCRIPTION("Generic NAND flash driver code");