blob: 6f58e1633e2f55cd4f31bd4c63ed2f3fcbfdab46 [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
Brian Norris7854d3f2011-06-23 14:12:08 -070024 * if we have HW ECC support.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * 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>
Ivan Djelic193bd402011-03-11 11:05:33 +010045#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020053static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020060 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020063static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066 .oobfree = {
67 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020068 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020071static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .eccbytes = 24,
73 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010074 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020077 .oobfree = {
78 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020079 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Thomas Gleixner81ec5362007-12-12 17:27:03 +010082static struct nand_ecclayout nand_oob_128 = {
83 .eccbytes = 48,
84 .eccpos = {
85 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
88 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020093 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010094};
95
Huang Shijie6a8214a2012-11-19 14:43:30 +080096static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020098static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
99 struct mtd_oob_ops *ops);
100
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200101/*
Joe Perches8e87d782008-02-03 17:22:34 +0200102 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200103 * compiled away when LED support is disabled.
104 */
105DEFINE_LED_TRIGGER(nand_led_trigger);
106
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530107static int check_offs_len(struct mtd_info *mtd,
108 loff_t ofs, uint64_t len)
109{
110 struct nand_chip *chip = mtd->priv;
111 int ret = 0;
112
113 /* Start address must align on block boundary */
114 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700115 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530116 ret = -EINVAL;
117 }
118
119 /* Length must align on block boundary */
120 if (len & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700121 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530122 ret = -EINVAL;
123 }
124
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530125 return ret;
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/**
129 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700130 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000131 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800132 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100134static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200136 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200138 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200139 spin_lock(&chip->controller->lock);
140 chip->controller->active = NULL;
141 chip->state = FL_READY;
142 wake_up(&chip->controller->wq);
143 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146/**
147 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700148 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700150 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200152static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200154 struct nand_chip *chip = mtd->priv;
155 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/**
Masanari Iida064a7692012-11-09 23:20:58 +0900159 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700160 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700161 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700163 * Default read function for 16bit buswidth with endianness conversion.
164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200166static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200168 struct nand_chip *chip = mtd->priv;
169 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700174 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700176 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178static u16 nand_read_word(struct mtd_info *mtd)
179{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200180 struct nand_chip *chip = mtd->priv;
181 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700186 * @mtd: MTD device structure
187 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
189 * Default select function for 1 chip devices.
190 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200191static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200193 struct nand_chip *chip = mtd->priv;
194
195 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200197 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 default:
203 BUG();
204 }
205}
206
207/**
208 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700209 * @mtd: MTD device structure
210 * @buf: data buffer
211 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700213 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200215static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200218 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
David Woodhousee0c7d762006-05-13 18:07:53 +0100220 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200221 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000225 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700226 * @mtd: MTD device structure
227 * @buf: buffer to store date
228 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700230 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200232static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200235 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
David Woodhousee0c7d762006-05-13 18:07:53 +0100237 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200238 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700243 * @mtd: MTD device structure
244 * @buf: data buffer
245 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700247 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200249static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200252 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 u16 *p = (u16 *) buf;
254 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000255
David Woodhousee0c7d762006-05-13 18:07:53 +0100256 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200257 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000262 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700263 * @mtd: MTD device structure
264 * @buf: buffer to store date
265 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700267 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200269static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200272 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 u16 *p = (u16 *) buf;
274 len >>= 1;
275
David Woodhousee0c7d762006-05-13 18:07:53 +0100276 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700282 * @mtd: MTD device structure
283 * @ofs: offset from device start
284 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000286 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
289{
Brian Norriscdbec052012-01-13 18:11:48 -0800290 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200291 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 u16 bad;
293
Brian Norris5fb15492011-05-31 16:31:21 -0700294 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700295 ofs += mtd->erasesize - mtd->writesize;
296
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100297 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200300 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Huang Shijie6a8214a2012-11-19 14:43:30 +0800302 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200305 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Brian Norriscdbec052012-01-13 18:11:48 -0800308 do {
309 if (chip->options & NAND_BUSWIDTH_16) {
310 chip->cmdfunc(mtd, NAND_CMD_READOOB,
311 chip->badblockpos & 0xFE, page);
312 bad = cpu_to_le16(chip->read_word(mtd));
313 if (chip->badblockpos & 0x1)
314 bad >>= 8;
315 else
316 bad &= 0xFF;
317 } else {
318 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
319 page);
320 bad = chip->read_byte(mtd);
321 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000322
Brian Norriscdbec052012-01-13 18:11:48 -0800323 if (likely(chip->badblockbits == 8))
324 res = bad != 0xFF;
325 else
326 res = hweight8(bad) < chip->badblockbits;
327 ofs += mtd->writesize;
328 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
329 i++;
330 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200331
Huang Shijieb0bb6902012-11-19 14:43:29 +0800332 if (getchip) {
333 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800335 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return res;
338}
339
340/**
341 * nand_default_block_markbad - [DEFAULT] mark a block bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700342 * @mtd: MTD device structure
343 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700345 * This is the default implementation, which can be overridden by a hardware
Brian Norrise2414f42012-02-06 13:44:00 -0800346 * specific driver. We try operations in the following order, according to our
347 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
348 * (1) erase the affected block, to allow OOB marker to be written cleanly
349 * (2) update in-memory BBT
350 * (3) write bad block marker to OOB area of affected block
351 * (4) update flash-based BBT
352 * Note that we retain the first error encountered in (3) or (4), finish the
353 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354*/
355static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
356{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200357 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200358 uint8_t buf[2] = { 0, 0 };
Brian Norrise2414f42012-02-06 13:44:00 -0800359 int block, res, ret = 0, i = 0;
360 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000361
Brian Norrise2414f42012-02-06 13:44:00 -0800362 if (write_oob) {
Brian Norris00918422012-01-13 18:11:47 -0800363 struct erase_info einfo;
364
365 /* Attempt erase before marking OOB */
366 memset(&einfo, 0, sizeof(einfo));
367 einfo.mtd = mtd;
368 einfo.addr = ofs;
369 einfo.len = 1 << chip->phys_erase_shift;
370 nand_erase_nand(mtd, &einfo, 0);
371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400374 block = (int)(ofs >> chip->bbt_erase_shift);
Brian Norrise2414f42012-02-06 13:44:00 -0800375 /* Mark block bad in memory-based BBT */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200376 if (chip->bbt)
377 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Brian Norrise2414f42012-02-06 13:44:00 -0800379 /* Write bad block marker to OOB */
380 if (write_oob) {
Brian Norris4a89ff82011-08-30 18:45:45 -0700381 struct mtd_oob_ops ops;
Brian Norrisdf698622012-01-20 20:38:03 -0800382 loff_t wr_ofs = ofs;
Brian Norris4a89ff82011-08-30 18:45:45 -0700383
Huang Shijie6a8214a2012-11-19 14:43:30 +0800384 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385
Brian Norris4a89ff82011-08-30 18:45:45 -0700386 ops.datbuf = NULL;
387 ops.oobbuf = buf;
Brian Norris85443312012-01-13 18:11:49 -0800388 ops.ooboffs = chip->badblockpos;
389 if (chip->options & NAND_BUSWIDTH_16) {
390 ops.ooboffs &= ~0x01;
391 ops.len = ops.ooblen = 2;
392 } else {
393 ops.len = ops.ooblen = 1;
394 }
Brian Norris23b1a992011-10-14 20:09:33 -0700395 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norrisdf698622012-01-20 20:38:03 -0800396
Brian Norrise2414f42012-02-06 13:44:00 -0800397 /* Write to first/last page(s) if necessary */
Brian Norrisdf698622012-01-20 20:38:03 -0800398 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
399 wr_ofs += mtd->erasesize - mtd->writesize;
Brian Norris02ed70b2010-07-21 16:53:47 -0700400 do {
Brian Norrise2414f42012-02-06 13:44:00 -0800401 res = nand_do_write_oob(mtd, wr_ofs, &ops);
402 if (!ret)
403 ret = res;
Brian Norris02ed70b2010-07-21 16:53:47 -0700404
Brian Norris02ed70b2010-07-21 16:53:47 -0700405 i++;
Brian Norrisdf698622012-01-20 20:38:03 -0800406 wr_ofs += mtd->writesize;
Brian Norrise2414f42012-02-06 13:44:00 -0800407 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Brian Norris02ed70b2010-07-21 16:53:47 -0700408
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300409 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200410 }
Brian Norrise2414f42012-02-06 13:44:00 -0800411
412 /* Update flash-based bad block table */
413 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
414 res = nand_update_bbt(mtd, ofs);
415 if (!ret)
416 ret = res;
417 }
418
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200419 if (!ret)
420 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300421
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200422 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000425/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700427 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700429 * Check, if the device is write protected. The function expects, that the
430 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100432static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200434 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200435
Brian Norris8b6e50c2011-05-25 14:59:01 -0700436 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200437 if (chip->options & NAND_BROKEN_XD)
438 return 0;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200441 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
442 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445/**
446 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700447 * @mtd: MTD device structure
448 * @ofs: offset from device start
449 * @getchip: 0, if the chip is already selected
450 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
452 * Check, if the block is bad. Either by reading the bad block table or
453 * calling of the scan function.
454 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200455static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
456 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200458 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000459
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200460 if (!chip->bbt)
461 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100464 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200467/**
468 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700469 * @mtd: MTD device structure
470 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200471 *
472 * Helper function for nand_wait_ready used when needing to wait in interrupt
473 * context.
474 */
475static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
476{
477 struct nand_chip *chip = mtd->priv;
478 int i;
479
480 /* Wait for the device to get ready */
481 for (i = 0; i < timeo; i++) {
482 if (chip->dev_ready(mtd))
483 break;
484 touch_softlockup_watchdog();
485 mdelay(1);
486 }
487}
488
Brian Norris7854d3f2011-06-23 14:12:08 -0700489/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100490void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000491{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200492 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100493 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000494
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200495 /* 400ms timeout */
496 if (in_interrupt() || oops_in_progress)
497 return panic_nand_wait_ready(mtd, 400);
498
Richard Purdie8fe833c2006-03-31 02:31:14 -0800499 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700500 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000501 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800503 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700504 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000505 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800506 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000507}
David Woodhouse4b648b02006-09-25 17:05:24 +0100508EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/**
511 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700512 * @mtd: MTD device structure
513 * @command: the command to be sent
514 * @column: the column address for this command, -1 if none
515 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700517 * Send command to NAND device. This function is used for small page devices
518 * (256/512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200520static void nand_command(struct mtd_info *mtd, unsigned int command,
521 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200523 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200524 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Brian Norris8b6e50c2011-05-25 14:59:01 -0700526 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (command == NAND_CMD_SEQIN) {
528 int readcmd;
529
Joern Engel28318772006-05-22 23:18:05 +0200530 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200532 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 readcmd = NAND_CMD_READOOB;
534 } else if (column < 256) {
535 /* First 256 bytes --> READ0 */
536 readcmd = NAND_CMD_READ0;
537 } else {
538 column -= 256;
539 readcmd = NAND_CMD_READ1;
540 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200541 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200542 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200544 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Brian Norris8b6e50c2011-05-25 14:59:01 -0700546 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200547 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
548 /* Serially input address */
549 if (column != -1) {
550 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200551 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200552 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200553 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200554 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200556 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200557 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200558 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200559 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200561 if (chip->chipsize > (32 << 20))
562 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200563 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200564 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000565
566 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 * Program and erase have their own busy handlers status and sequential
568 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 case NAND_CMD_PAGEPROG:
573 case NAND_CMD_ERASE1:
574 case NAND_CMD_ERASE2:
575 case NAND_CMD_SEQIN:
576 case NAND_CMD_STATUS:
577 return;
578
579 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200580 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 udelay(chip->chip_delay);
583 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200585 chip->cmd_ctrl(mtd,
586 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200587 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
588 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
590
David Woodhousee0c7d762006-05-13 18:07:53 +0100591 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000593 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * If we don't have access to the busy pin, we apply the given
595 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100596 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200597 if (!chip->dev_ready) {
598 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700602 /*
603 * Apply this short delay always to ensure that we do wait tWB in
604 * any case on any machine.
605 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100606 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000607
608 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/**
612 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700613 * @mtd: MTD device structure
614 * @command: the command to be sent
615 * @column: the column address for this command, -1 if none
616 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700619 * devices. We don't have the separate regions as we have in the small page
620 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200622static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
623 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200625 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* Emulate NAND_CMD_READOOB */
628 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200629 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 command = NAND_CMD_READ0;
631 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000632
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200634 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200638 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* Serially input address */
641 if (column != -1) {
642 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200643 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200645 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200646 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 chip->cmd_ctrl(mtd, page_addr, ctrl);
651 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200652 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 if (chip->chipsize > (128 << 20))
655 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200656 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200659 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000660
661 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700662 * Program and erase have their own busy handlers status, sequential
663 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 case NAND_CMD_CACHEDPROG:
668 case NAND_CMD_PAGEPROG:
669 case NAND_CMD_ERASE1:
670 case NAND_CMD_ERASE2:
671 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200672 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000674 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return;
676
David A. Marlin30f464b2005-01-17 18:35:25 +0000677 case NAND_CMD_STATUS_ERROR:
678 case NAND_CMD_STATUS_ERROR0:
679 case NAND_CMD_STATUS_ERROR1:
680 case NAND_CMD_STATUS_ERROR2:
681 case NAND_CMD_STATUS_ERROR3:
Brian Norris8b6e50c2011-05-25 14:59:01 -0700682 /* Read error status commands require only a short delay */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200687 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200689 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200690 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
691 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
692 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
693 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200694 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
695 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return;
697
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200698 case NAND_CMD_RNDOUT:
699 /* No ready / busy check necessary */
700 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
701 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
702 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
703 NAND_NCE | NAND_CTRL_CHANGE);
704 return;
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200707 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
708 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
709 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
710 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000711
David Woodhousee0c7d762006-05-13 18:07:53 +0100712 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000714 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700716 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100717 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200718 if (!chip->dev_ready) {
719 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000723
Brian Norris8b6e50c2011-05-25 14:59:01 -0700724 /*
725 * Apply this short delay always to ensure that we do wait tWB in
726 * any case on any machine.
727 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100728 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000729
730 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200734 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700735 * @chip: the nand chip descriptor
736 * @mtd: MTD device structure
737 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200738 *
739 * Used when in panic, no locks are taken.
740 */
741static void panic_nand_get_device(struct nand_chip *chip,
742 struct mtd_info *mtd, int new_state)
743{
Brian Norris7854d3f2011-06-23 14:12:08 -0700744 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200745 chip->controller->active = chip;
746 chip->state = new_state;
747}
748
749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700751 * @mtd: MTD device structure
752 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *
754 * Get the device and lock it for exclusive access
755 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200756static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800757nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800759 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200760 spinlock_t *lock = &chip->controller->lock;
761 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100762 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200763retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100764 spin_lock(lock);
765
vimal singhb8b3ee92009-07-09 20:41:22 +0530766 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 if (!chip->controller->active)
768 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200769
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200770 if (chip->controller->active == chip && chip->state == FL_READY) {
771 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100772 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100773 return 0;
774 }
775 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800776 if (chip->controller->active->state == FL_PM_SUSPENDED) {
777 chip->state = FL_PM_SUSPENDED;
778 spin_unlock(lock);
779 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800780 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100781 }
782 set_current_state(TASK_UNINTERRUPTIBLE);
783 add_wait_queue(wq, &wait);
784 spin_unlock(lock);
785 schedule();
786 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto retry;
788}
789
790/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 * panic_nand_wait - [GENERIC] wait until the command is done
792 * @mtd: MTD device structure
793 * @chip: NAND chip structure
794 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200795 *
796 * Wait for command done. This is a helper function for nand_wait used when
797 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400798 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200799 */
800static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
801 unsigned long timeo)
802{
803 int i;
804 for (i = 0; i < timeo; i++) {
805 if (chip->dev_ready) {
806 if (chip->dev_ready(mtd))
807 break;
808 } else {
809 if (chip->read_byte(mtd) & NAND_STATUS_READY)
810 break;
811 }
812 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200813 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200814}
815
816/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700817 * nand_wait - [DEFAULT] wait until the command is done
818 * @mtd: MTD device structure
819 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700821 * Wait for command done. This applies to erase and program only. Erase can
822 * take up to 400ms and program up to 20ms according to general NAND and
823 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700824 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200825static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200829 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100832 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100834 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Richard Purdie8fe833c2006-03-31 02:31:14 -0800836 led_trigger_event(nand_led_trigger, LED_FULL);
837
Brian Norris8b6e50c2011-05-25 14:59:01 -0700838 /*
839 * Apply this short delay always to ensure that we do wait tWB in any
840 * case on any machine.
841 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100842 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200844 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
845 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200847 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200849 if (in_interrupt() || oops_in_progress)
850 panic_nand_wait(mtd, chip, timeo);
851 else {
852 while (time_before(jiffies, timeo)) {
853 if (chip->dev_ready) {
854 if (chip->dev_ready(mtd))
855 break;
856 } else {
857 if (chip->read_byte(mtd) & NAND_STATUS_READY)
858 break;
859 }
860 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800863 led_trigger_event(nand_led_trigger, LED_OFF);
864
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200865 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100866 /* This can happen if in case of timeout or buggy dev_ready */
867 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return status;
869}
870
871/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700872 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700873 * @mtd: mtd info
874 * @ofs: offset to start unlock from
875 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700876 * @invert: when = 0, unlock the range of blocks within the lower and
877 * upper boundary address
878 * when = 1, unlock the range of blocks outside the boundaries
879 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530880 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700881 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530882 */
883static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
884 uint64_t len, int invert)
885{
886 int ret = 0;
887 int status, page;
888 struct nand_chip *chip = mtd->priv;
889
890 /* Submit address of first page to unlock */
891 page = ofs >> chip->page_shift;
892 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
893
894 /* Submit address of last page to unlock */
895 page = (ofs + len) >> chip->page_shift;
896 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
897 (page | invert) & chip->pagemask);
898
899 /* Call wait ready function */
900 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530901 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400902 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700903 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530904 __func__, status);
905 ret = -EIO;
906 }
907
908 return ret;
909}
910
911/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700912 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700913 * @mtd: mtd info
914 * @ofs: offset to start unlock from
915 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530916 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700917 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530918 */
919int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
920{
921 int ret = 0;
922 int chipnr;
923 struct nand_chip *chip = mtd->priv;
924
Brian Norris289c0522011-07-19 10:06:09 -0700925 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530926 __func__, (unsigned long long)ofs, len);
927
928 if (check_offs_len(mtd, ofs, len))
929 ret = -EINVAL;
930
931 /* Align to last block address if size addresses end of the device */
932 if (ofs + len == mtd->size)
933 len -= mtd->erasesize;
934
Huang Shijie6a8214a2012-11-19 14:43:30 +0800935 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530936
937 /* Shift to get chip number */
938 chipnr = ofs >> chip->chip_shift;
939
940 chip->select_chip(mtd, chipnr);
941
942 /* Check, if it is write protected */
943 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700944 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530945 __func__);
946 ret = -EIO;
947 goto out;
948 }
949
950 ret = __nand_unlock(mtd, ofs, len, 0);
951
952out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800953 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530954 nand_release_device(mtd);
955
956 return ret;
957}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200958EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530959
960/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700961 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700962 * @mtd: mtd info
963 * @ofs: offset to start unlock from
964 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530965 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700966 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
967 * have this feature, but it allows only to lock all blocks, not for specified
968 * range for block. Implementing 'lock' feature by making use of 'unlock', for
969 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530970 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700971 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530972 */
973int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
974{
975 int ret = 0;
976 int chipnr, status, page;
977 struct nand_chip *chip = mtd->priv;
978
Brian Norris289c0522011-07-19 10:06:09 -0700979 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530980 __func__, (unsigned long long)ofs, len);
981
982 if (check_offs_len(mtd, ofs, len))
983 ret = -EINVAL;
984
Huang Shijie6a8214a2012-11-19 14:43:30 +0800985 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530986
987 /* Shift to get chip number */
988 chipnr = ofs >> chip->chip_shift;
989
990 chip->select_chip(mtd, chipnr);
991
992 /* Check, if it is write protected */
993 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700994 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530995 __func__);
996 status = MTD_ERASE_FAILED;
997 ret = -EIO;
998 goto out;
999 }
1000
1001 /* Submit address of first page to lock */
1002 page = ofs >> chip->page_shift;
1003 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1004
1005 /* Call wait ready function */
1006 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301007 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001008 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001009 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301010 __func__, status);
1011 ret = -EIO;
1012 goto out;
1013 }
1014
1015 ret = __nand_unlock(mtd, ofs, len, 0x1);
1016
1017out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001018 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301019 nand_release_device(mtd);
1020
1021 return ret;
1022}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001023EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301024
1025/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001026 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001027 * @mtd: mtd info structure
1028 * @chip: nand chip info structure
1029 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001030 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001031 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001032 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001033 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001034 */
1035static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001036 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001037{
1038 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001039 if (oob_required)
1040 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001041 return 0;
1042}
1043
1044/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001045 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001046 * @mtd: mtd info structure
1047 * @chip: nand chip info structure
1048 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001049 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001050 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001051 *
1052 * We need a special oob layout and handling even when OOB isn't used.
1053 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001054static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001055 struct nand_chip *chip, uint8_t *buf,
1056 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001057{
1058 int eccsize = chip->ecc.size;
1059 int eccbytes = chip->ecc.bytes;
1060 uint8_t *oob = chip->oob_poi;
1061 int steps, size;
1062
1063 for (steps = chip->ecc.steps; steps > 0; steps--) {
1064 chip->read_buf(mtd, buf, eccsize);
1065 buf += eccsize;
1066
1067 if (chip->ecc.prepad) {
1068 chip->read_buf(mtd, oob, chip->ecc.prepad);
1069 oob += chip->ecc.prepad;
1070 }
1071
1072 chip->read_buf(mtd, oob, eccbytes);
1073 oob += eccbytes;
1074
1075 if (chip->ecc.postpad) {
1076 chip->read_buf(mtd, oob, chip->ecc.postpad);
1077 oob += chip->ecc.postpad;
1078 }
1079 }
1080
1081 size = mtd->oobsize - (oob - chip->oob_poi);
1082 if (size)
1083 chip->read_buf(mtd, oob, size);
1084
1085 return 0;
1086}
1087
1088/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001089 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001090 * @mtd: mtd info structure
1091 * @chip: nand chip info structure
1092 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001093 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001094 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001095 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001096static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001097 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001099 int i, eccsize = chip->ecc.size;
1100 int eccbytes = chip->ecc.bytes;
1101 int eccsteps = chip->ecc.steps;
1102 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001103 uint8_t *ecc_calc = chip->buffers->ecccalc;
1104 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001105 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001106 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001107
Brian Norris1fbb9382012-05-02 10:14:55 -07001108 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001109
1110 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1111 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1112
1113 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001114 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001115
1116 eccsteps = chip->ecc.steps;
1117 p = buf;
1118
1119 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1120 int stat;
1121
1122 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001123 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001124 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001125 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001126 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001127 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1128 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001129 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001130 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001134 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001135 * @mtd: mtd info structure
1136 * @chip: nand chip info structure
1137 * @data_offs: offset of requested data within the page
1138 * @readlen: data length
1139 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001140 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001141static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1142 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001143{
1144 int start_step, end_step, num_steps;
1145 uint32_t *eccpos = chip->ecc.layout->eccpos;
1146 uint8_t *p;
1147 int data_col_addr, i, gaps = 0;
1148 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1149 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001150 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001151 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001152
Brian Norris7854d3f2011-06-23 14:12:08 -07001153 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001154 start_step = data_offs / chip->ecc.size;
1155 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1156 num_steps = end_step - start_step + 1;
1157
Brian Norris8b6e50c2011-05-25 14:59:01 -07001158 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001159 datafrag_len = num_steps * chip->ecc.size;
1160 eccfrag_len = num_steps * chip->ecc.bytes;
1161
1162 data_col_addr = start_step * chip->ecc.size;
1163 /* If we read not a page aligned data */
1164 if (data_col_addr != 0)
1165 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1166
1167 p = bufpoi + data_col_addr;
1168 chip->read_buf(mtd, p, datafrag_len);
1169
Brian Norris8b6e50c2011-05-25 14:59:01 -07001170 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001171 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1172 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1173
Brian Norris8b6e50c2011-05-25 14:59:01 -07001174 /*
1175 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001176 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001177 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001178 for (i = 0; i < eccfrag_len - 1; i++) {
1179 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1180 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1181 gaps = 1;
1182 break;
1183 }
1184 }
1185 if (gaps) {
1186 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1187 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1188 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001189 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001190 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001191 * about buswidth alignment in read_buf.
1192 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001193 index = start_step * chip->ecc.bytes;
1194
1195 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001196 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001197 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001198 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001199 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001200 aligned_len++;
1201
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001202 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1203 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001204 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1205 }
1206
1207 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001208 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001209
1210 p = bufpoi + data_col_addr;
1211 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1212 int stat;
1213
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001214 stat = chip->ecc.correct(mtd, p,
1215 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001216 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001217 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001218 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001219 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001220 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1221 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001222 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001223 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001224}
1225
1226/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001227 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 * @mtd: mtd info structure
1229 * @chip: nand chip info structure
1230 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001231 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001232 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001233 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001234 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001235 */
1236static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001237 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001238{
1239 int i, eccsize = chip->ecc.size;
1240 int eccbytes = chip->ecc.bytes;
1241 int eccsteps = chip->ecc.steps;
1242 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001243 uint8_t *ecc_calc = chip->buffers->ecccalc;
1244 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001245 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001246 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001247
1248 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1249 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1250 chip->read_buf(mtd, p, eccsize);
1251 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1252 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001253 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001254
1255 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001256 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001257
1258 eccsteps = chip->ecc.steps;
1259 p = buf;
1260
1261 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1262 int stat;
1263
1264 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001265 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001266 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001267 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001268 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001269 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1270 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001271 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001272 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001273}
1274
1275/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001276 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001277 * @mtd: mtd info structure
1278 * @chip: nand chip info structure
1279 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001280 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001281 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001282 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001283 * Hardware ECC for large page chips, require OOB to be read first. For this
1284 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1285 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1286 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1287 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001288 */
1289static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001290 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001291{
1292 int i, eccsize = chip->ecc.size;
1293 int eccbytes = chip->ecc.bytes;
1294 int eccsteps = chip->ecc.steps;
1295 uint8_t *p = buf;
1296 uint8_t *ecc_code = chip->buffers->ecccode;
1297 uint32_t *eccpos = chip->ecc.layout->eccpos;
1298 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001299 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001300
1301 /* Read the OOB area first */
1302 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1303 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1304 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1305
1306 for (i = 0; i < chip->ecc.total; i++)
1307 ecc_code[i] = chip->oob_poi[eccpos[i]];
1308
1309 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1310 int stat;
1311
1312 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1313 chip->read_buf(mtd, p, eccsize);
1314 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1315
1316 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001317 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001318 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001319 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001320 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001321 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1322 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001323 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001324 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001325}
1326
1327/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001328 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001329 * @mtd: mtd info structure
1330 * @chip: nand chip info structure
1331 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001332 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001333 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001334 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001335 * The hw generator calculates the error syndrome automatically. Therefore we
1336 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001337 */
1338static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001339 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001340{
1341 int i, eccsize = chip->ecc.size;
1342 int eccbytes = chip->ecc.bytes;
1343 int eccsteps = chip->ecc.steps;
1344 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001345 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001346 unsigned int max_bitflips = 0;
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
Mike Dunn3f91e942012-04-25 12:06:09 -07001363 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001364 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001365 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001366 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001367 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1368 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001369
1370 oob += eccbytes;
1371
1372 if (chip->ecc.postpad) {
1373 chip->read_buf(mtd, oob, chip->ecc.postpad);
1374 oob += chip->ecc.postpad;
1375 }
1376 }
1377
1378 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001379 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001380 if (i)
1381 chip->read_buf(mtd, oob, i);
1382
Mike Dunn3f91e942012-04-25 12:06:09 -07001383 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001384}
1385
1386/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001387 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001388 * @chip: nand chip structure
1389 * @oob: oob destination address
1390 * @ops: oob ops structure
1391 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001392 */
1393static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001394 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001395{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001396 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001397
Brian Norris0612b9d2011-08-30 18:45:40 -07001398 case MTD_OPS_PLACE_OOB:
1399 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001400 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1401 return oob + len;
1402
Brian Norris0612b9d2011-08-30 18:45:40 -07001403 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001404 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001405 uint32_t boffs = 0, roffs = ops->ooboffs;
1406 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001407
Florian Fainellif8ac0412010-09-07 13:23:43 +02001408 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001409 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001410 if (unlikely(roffs)) {
1411 if (roffs >= free->length) {
1412 roffs -= free->length;
1413 continue;
1414 }
1415 boffs = free->offset + roffs;
1416 bytes = min_t(size_t, len,
1417 (free->length - roffs));
1418 roffs = 0;
1419 } else {
1420 bytes = min_t(size_t, len, free->length);
1421 boffs = free->offset;
1422 }
1423 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001424 oob += bytes;
1425 }
1426 return oob;
1427 }
1428 default:
1429 BUG();
1430 }
1431 return NULL;
1432}
1433
1434/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001435 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001436 * @mtd: MTD device structure
1437 * @from: offset to read from
1438 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001439 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001440 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001441 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001442static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1443 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001444{
Brian Norrise47f3db2012-05-02 10:14:56 -07001445 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001446 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001447 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001448 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001449 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001450 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001451 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001452 mtd->oobavail : mtd->oobsize;
1453
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001454 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001455 unsigned int max_bitflips = 0;
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;
Brian Norrise47f3db2012-05-02 10:14:56 -07001469 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001470
Florian Fainellif8ac0412010-09-07 13:23:43 +02001471 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001472 bytes = min(mtd->writesize - col, readlen);
1473 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001474
Brian Norris8b6e50c2011-05-25 14:59:01 -07001475 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001476 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001477 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Brian Norrisc00a0992012-05-01 17:12:54 -07001479 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Mike Dunnedbc45402012-04-25 12:06:11 -07001481 /*
1482 * Now read the page into the buffer. Absent an error,
1483 * the read methods return max bitflips per ecc step.
1484 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001485 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001486 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001487 oob_required,
1488 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001489 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1490 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001491 ret = chip->ecc.read_subpage(mtd, chip,
1492 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001493 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001494 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001495 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001496 if (ret < 0) {
1497 if (!aligned)
1498 /* Invalidate page cache */
1499 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001500 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001501 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001502
Mike Dunnedbc45402012-04-25 12:06:11 -07001503 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1504
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001505 /* Transfer not aligned data */
1506 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001507 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001508 !(mtd->ecc_stats.failed - stats.failed) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001509 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001510 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001511 chip->pagebuf_bitflips = ret;
1512 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001513 /* Invalidate page cache */
1514 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001515 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001516 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001518
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001519 buf += bytes;
1520
1521 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001522 int toread = min(oobreadlen, max_oobsize);
1523
1524 if (toread) {
1525 oob = nand_transfer_oob(chip,
1526 oob, ops, toread);
1527 oobreadlen -= toread;
1528 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001529 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001530 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001531 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001532 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001533 max_bitflips = max_t(unsigned int, max_bitflips,
1534 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001537 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001538
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001539 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Brian Norris8b6e50c2011-05-25 14:59:01 -07001542 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 col = 0;
1544 /* Increment page address */
1545 realpage++;
1546
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001547 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /* Check, if we cross a chip boundary */
1549 if (!page) {
1550 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001551 chip->select_chip(mtd, -1);
1552 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001555 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001557 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001558 if (oob)
1559 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Mike Dunn3f91e942012-04-25 12:06:09 -07001561 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001562 return ret;
1563
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001564 if (mtd->ecc_stats.failed - stats.failed)
1565 return -EBADMSG;
1566
Mike Dunnedbc45402012-04-25 12:06:11 -07001567 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001568}
1569
1570/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001571 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001572 * @mtd: MTD device structure
1573 * @from: offset to read from
1574 * @len: number of bytes to read
1575 * @retlen: pointer to variable to store the number of read bytes
1576 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001577 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001578 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001579 */
1580static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1581 size_t *retlen, uint8_t *buf)
1582{
Brian Norris4a89ff82011-08-30 18:45:45 -07001583 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001584 int ret;
1585
Huang Shijie6a8214a2012-11-19 14:43:30 +08001586 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001587 ops.len = len;
1588 ops.datbuf = buf;
1589 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001590 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001591 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001592 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001593 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001594 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001598 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001599 * @mtd: mtd info structure
1600 * @chip: nand chip info structure
1601 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001602 */
1603static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001604 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001605{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001606 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001607 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001608 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001609}
1610
1611/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001612 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001613 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001614 * @mtd: mtd info structure
1615 * @chip: nand chip info structure
1616 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001617 */
1618static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001619 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001620{
1621 uint8_t *buf = chip->oob_poi;
1622 int length = mtd->oobsize;
1623 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1624 int eccsize = chip->ecc.size;
1625 uint8_t *bufpoi = buf;
1626 int i, toread, sndrnd = 0, pos;
1627
1628 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1629 for (i = 0; i < chip->ecc.steps; i++) {
1630 if (sndrnd) {
1631 pos = eccsize + i * (eccsize + chunk);
1632 if (mtd->writesize > 512)
1633 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1634 else
1635 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1636 } else
1637 sndrnd = 1;
1638 toread = min_t(int, length, chunk);
1639 chip->read_buf(mtd, bufpoi, toread);
1640 bufpoi += toread;
1641 length -= toread;
1642 }
1643 if (length > 0)
1644 chip->read_buf(mtd, bufpoi, length);
1645
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001646 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001647}
1648
1649/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001650 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001651 * @mtd: mtd info structure
1652 * @chip: nand chip info structure
1653 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001654 */
1655static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1656 int page)
1657{
1658 int status = 0;
1659 const uint8_t *buf = chip->oob_poi;
1660 int length = mtd->oobsize;
1661
1662 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1663 chip->write_buf(mtd, buf, length);
1664 /* Send command to program the OOB data */
1665 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1666
1667 status = chip->waitfunc(mtd, chip);
1668
Savin Zlobec0d420f92006-06-21 11:51:20 +02001669 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001670}
1671
1672/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001673 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001674 * with syndrome - only for large page flash
1675 * @mtd: mtd info structure
1676 * @chip: nand chip info structure
1677 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001678 */
1679static int nand_write_oob_syndrome(struct mtd_info *mtd,
1680 struct nand_chip *chip, int page)
1681{
1682 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1683 int eccsize = chip->ecc.size, length = mtd->oobsize;
1684 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1685 const uint8_t *bufpoi = chip->oob_poi;
1686
1687 /*
1688 * data-ecc-data-ecc ... ecc-oob
1689 * or
1690 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1691 */
1692 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1693 pos = steps * (eccsize + chunk);
1694 steps = 0;
1695 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001696 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001697
1698 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1699 for (i = 0; i < steps; i++) {
1700 if (sndcmd) {
1701 if (mtd->writesize <= 512) {
1702 uint32_t fill = 0xFFFFFFFF;
1703
1704 len = eccsize;
1705 while (len > 0) {
1706 int num = min_t(int, len, 4);
1707 chip->write_buf(mtd, (uint8_t *)&fill,
1708 num);
1709 len -= num;
1710 }
1711 } else {
1712 pos = eccsize + i * (eccsize + chunk);
1713 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1714 }
1715 } else
1716 sndcmd = 1;
1717 len = min_t(int, length, chunk);
1718 chip->write_buf(mtd, bufpoi, len);
1719 bufpoi += len;
1720 length -= len;
1721 }
1722 if (length > 0)
1723 chip->write_buf(mtd, bufpoi, length);
1724
1725 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1726 status = chip->waitfunc(mtd, chip);
1727
1728 return status & NAND_STATUS_FAIL ? -EIO : 0;
1729}
1730
1731/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001732 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001733 * @mtd: MTD device structure
1734 * @from: offset to read from
1735 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001737 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001739static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1740 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Brian Norrisc00a0992012-05-01 17:12:54 -07001742 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001743 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001744 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001745 int readlen = ops->ooblen;
1746 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001747 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001748 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Brian Norris289c0522011-07-19 10:06:09 -07001750 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301751 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Brian Norris041e4572011-06-23 16:45:24 -07001753 stats = mtd->ecc_stats;
1754
Brian Norris0612b9d2011-08-30 18:45:40 -07001755 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001756 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001757 else
1758 len = mtd->oobsize;
1759
1760 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001761 pr_debug("%s: attempt to start read outside oob\n",
1762 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001763 return -EINVAL;
1764 }
1765
1766 /* Do not allow reads past end of device */
1767 if (unlikely(from >= mtd->size ||
1768 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1769 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001770 pr_debug("%s: attempt to read beyond end of device\n",
1771 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001772 return -EINVAL;
1773 }
Vitaly Wool70145682006-11-03 18:20:38 +03001774
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001775 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001776 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001778 /* Shift to get page */
1779 realpage = (int)(from >> chip->page_shift);
1780 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Florian Fainellif8ac0412010-09-07 13:23:43 +02001782 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001783 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001784 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001785 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001786 ret = chip->ecc.read_oob(mtd, chip, page);
1787
1788 if (ret < 0)
1789 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001790
1791 len = min(len, readlen);
1792 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001793
Vitaly Wool70145682006-11-03 18:20:38 +03001794 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001795 if (!readlen)
1796 break;
1797
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001798 /* Increment page address */
1799 realpage++;
1800
1801 page = realpage & chip->pagemask;
1802 /* Check, if we cross a chip boundary */
1803 if (!page) {
1804 chipnr++;
1805 chip->select_chip(mtd, -1);
1806 chip->select_chip(mtd, chipnr);
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001809 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001811 ops->oobretlen = ops->ooblen - readlen;
1812
1813 if (ret < 0)
1814 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001815
1816 if (mtd->ecc_stats.failed - stats.failed)
1817 return -EBADMSG;
1818
1819 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001823 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001824 * @mtd: MTD device structure
1825 * @from: offset to read from
1826 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001828 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001830static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1831 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001833 int ret = -ENOTSUPP;
1834
1835 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001838 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001839 pr_debug("%s: attempt to read beyond end of device\n",
1840 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return -EINVAL;
1842 }
1843
Huang Shijie6a8214a2012-11-19 14:43:30 +08001844 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Florian Fainellif8ac0412010-09-07 13:23:43 +02001846 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001847 case MTD_OPS_PLACE_OOB:
1848 case MTD_OPS_AUTO_OOB:
1849 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001850 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001851
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852 default:
1853 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001856 if (!ops->datbuf)
1857 ret = nand_do_read_oob(mtd, from, ops);
1858 else
1859 ret = nand_do_read_ops(mtd, from, ops);
1860
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001861out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001863 return ret;
1864}
1865
1866
1867/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001868 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001869 * @mtd: mtd info structure
1870 * @chip: nand chip info structure
1871 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001872 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001873 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001874 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001876static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001877 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001878{
1879 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001880 if (oob_required)
1881 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001882
1883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001886/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001887 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001888 * @mtd: mtd info structure
1889 * @chip: nand chip info structure
1890 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001891 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001892 *
1893 * We need a special oob layout and handling even when ECC isn't checked.
1894 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001895static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001896 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001897 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001898{
1899 int eccsize = chip->ecc.size;
1900 int eccbytes = chip->ecc.bytes;
1901 uint8_t *oob = chip->oob_poi;
1902 int steps, size;
1903
1904 for (steps = chip->ecc.steps; steps > 0; steps--) {
1905 chip->write_buf(mtd, buf, eccsize);
1906 buf += eccsize;
1907
1908 if (chip->ecc.prepad) {
1909 chip->write_buf(mtd, oob, chip->ecc.prepad);
1910 oob += chip->ecc.prepad;
1911 }
1912
1913 chip->read_buf(mtd, oob, eccbytes);
1914 oob += eccbytes;
1915
1916 if (chip->ecc.postpad) {
1917 chip->write_buf(mtd, oob, chip->ecc.postpad);
1918 oob += chip->ecc.postpad;
1919 }
1920 }
1921
1922 size = mtd->oobsize - (oob - chip->oob_poi);
1923 if (size)
1924 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001925
1926 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001927}
1928/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001929 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001930 * @mtd: mtd info structure
1931 * @chip: nand chip info structure
1932 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001933 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001934 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001935static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001936 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001937{
1938 int i, eccsize = chip->ecc.size;
1939 int eccbytes = chip->ecc.bytes;
1940 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001941 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001942 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001943 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001944
Brian Norris7854d3f2011-06-23 14:12:08 -07001945 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1947 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001949 for (i = 0; i < chip->ecc.total; i++)
1950 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001951
Josh Wufdbad98d2012-06-25 18:07:45 +08001952 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001953}
1954
1955/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001956 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001957 * @mtd: mtd info structure
1958 * @chip: nand chip info structure
1959 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001960 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001961 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001962static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001963 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001964{
1965 int i, eccsize = chip->ecc.size;
1966 int eccbytes = chip->ecc.bytes;
1967 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001968 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001969 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001970 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001971
1972 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1973 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001974 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001975 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1976 }
1977
1978 for (i = 0; i < chip->ecc.total; i++)
1979 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1980
1981 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001982
1983 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001984}
1985
1986/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001987 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07001988 * @mtd: mtd info structure
1989 * @chip: nand chip info structure
1990 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001991 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001992 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001993 * The hw generator calculates the error syndrome automatically. Therefore we
1994 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001995 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001996static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001997 struct nand_chip *chip,
1998 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001999{
2000 int i, eccsize = chip->ecc.size;
2001 int eccbytes = chip->ecc.bytes;
2002 int eccsteps = chip->ecc.steps;
2003 const uint8_t *p = buf;
2004 uint8_t *oob = chip->oob_poi;
2005
2006 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2007
2008 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2009 chip->write_buf(mtd, p, eccsize);
2010
2011 if (chip->ecc.prepad) {
2012 chip->write_buf(mtd, oob, chip->ecc.prepad);
2013 oob += chip->ecc.prepad;
2014 }
2015
2016 chip->ecc.calculate(mtd, p, oob);
2017 chip->write_buf(mtd, oob, eccbytes);
2018 oob += eccbytes;
2019
2020 if (chip->ecc.postpad) {
2021 chip->write_buf(mtd, oob, chip->ecc.postpad);
2022 oob += chip->ecc.postpad;
2023 }
2024 }
2025
2026 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002027 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002028 if (i)
2029 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002030
2031 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002032}
2033
2034/**
David Woodhouse956e9442006-09-25 17:12:39 +01002035 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002036 * @mtd: MTD device structure
2037 * @chip: NAND chip descriptor
2038 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002039 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002040 * @page: page number to write
2041 * @cached: cached programming
2042 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002043 */
2044static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002045 const uint8_t *buf, int oob_required, int page,
2046 int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002047{
2048 int status;
2049
2050 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2051
David Woodhouse956e9442006-09-25 17:12:39 +01002052 if (unlikely(raw))
Josh Wufdbad98d2012-06-25 18:07:45 +08002053 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002054 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002055 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2056
2057 if (status < 0)
2058 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002059
2060 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002061 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002062 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002063 */
2064 cached = 0;
2065
2066 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2067
2068 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002069 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002070 /*
2071 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002072 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002073 */
2074 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2075 status = chip->errstat(mtd, chip, FL_WRITING, status,
2076 page);
2077
2078 if (status & NAND_STATUS_FAIL)
2079 return -EIO;
2080 } else {
2081 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002082 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002083 }
2084
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002085 return 0;
2086}
2087
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002088/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002089 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002090 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002091 * @oob: oob data buffer
2092 * @len: oob data write length
2093 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002094 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002095static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2096 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002097{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002098 struct nand_chip *chip = mtd->priv;
2099
2100 /*
2101 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2102 * data from a previous OOB read.
2103 */
2104 memset(chip->oob_poi, 0xff, mtd->oobsize);
2105
Florian Fainellif8ac0412010-09-07 13:23:43 +02002106 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002107
Brian Norris0612b9d2011-08-30 18:45:40 -07002108 case MTD_OPS_PLACE_OOB:
2109 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002110 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2111 return oob + len;
2112
Brian Norris0612b9d2011-08-30 18:45:40 -07002113 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002114 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002115 uint32_t boffs = 0, woffs = ops->ooboffs;
2116 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002117
Florian Fainellif8ac0412010-09-07 13:23:43 +02002118 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002119 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002120 if (unlikely(woffs)) {
2121 if (woffs >= free->length) {
2122 woffs -= free->length;
2123 continue;
2124 }
2125 boffs = free->offset + woffs;
2126 bytes = min_t(size_t, len,
2127 (free->length - woffs));
2128 woffs = 0;
2129 } else {
2130 bytes = min_t(size_t, len, free->length);
2131 boffs = free->offset;
2132 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002133 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002134 oob += bytes;
2135 }
2136 return oob;
2137 }
2138 default:
2139 BUG();
2140 }
2141 return NULL;
2142}
2143
Florian Fainellif8ac0412010-09-07 13:23:43 +02002144#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002145
2146/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002147 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002148 * @mtd: MTD device structure
2149 * @to: offset to write to
2150 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002151 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002152 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002153 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002154static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2155 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002156{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002157 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002158 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002159 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002160
2161 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002162 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002163 mtd->oobavail : mtd->oobsize;
2164
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165 uint8_t *oob = ops->oobbuf;
2166 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002167 int ret, subpage;
Brian Norrise47f3db2012-05-02 10:14:56 -07002168 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002169
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002170 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002171 if (!writelen)
2172 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002173
Brian Norris8b6e50c2011-05-25 14:59:01 -07002174 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002175 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002176 pr_notice("%s: attempt to write non page aligned data\n",
2177 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002178 return -EINVAL;
2179 }
2180
Thomas Gleixner29072b92006-09-28 15:38:36 +02002181 column = to & (mtd->writesize - 1);
2182 subpage = column || (writelen & (mtd->writesize - 1));
2183
2184 if (subpage && oob)
2185 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002186
Thomas Gleixner6a930962006-06-28 00:11:45 +02002187 chipnr = (int)(to >> chip->chip_shift);
2188 chip->select_chip(mtd, chipnr);
2189
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002190 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002191 if (nand_check_wp(mtd)) {
2192 ret = -EIO;
2193 goto err_out;
2194 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002195
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002196 realpage = (int)(to >> chip->page_shift);
2197 page = realpage & chip->pagemask;
2198 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2199
2200 /* Invalidate the page cache, when we write to the cached page */
2201 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002202 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002203 chip->pagebuf = -1;
2204
Maxim Levitsky782ce792010-02-22 20:39:36 +02002205 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002206 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2207 ret = -EINVAL;
2208 goto err_out;
2209 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002210
Florian Fainellif8ac0412010-09-07 13:23:43 +02002211 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002212 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002213 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002214 uint8_t *wbuf = buf;
2215
Brian Norris8b6e50c2011-05-25 14:59:01 -07002216 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002217 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2218 cached = 0;
2219 bytes = min_t(int, bytes - column, (int) writelen);
2220 chip->pagebuf = -1;
2221 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2222 memcpy(&chip->buffers->databuf[column], buf, bytes);
2223 wbuf = chip->buffers->databuf;
2224 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002225
Maxim Levitsky782ce792010-02-22 20:39:36 +02002226 if (unlikely(oob)) {
2227 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002228 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002229 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002230 } else {
2231 /* We still need to erase leftover OOB data */
2232 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002233 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002234
Brian Norrise47f3db2012-05-02 10:14:56 -07002235 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2236 cached, (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237 if (ret)
2238 break;
2239
2240 writelen -= bytes;
2241 if (!writelen)
2242 break;
2243
Thomas Gleixner29072b92006-09-28 15:38:36 +02002244 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002245 buf += bytes;
2246 realpage++;
2247
2248 page = realpage & chip->pagemask;
2249 /* Check, if we cross a chip boundary */
2250 if (!page) {
2251 chipnr++;
2252 chip->select_chip(mtd, -1);
2253 chip->select_chip(mtd, chipnr);
2254 }
2255 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002256
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002257 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002258 if (unlikely(oob))
2259 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002260
2261err_out:
2262 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002263 return ret;
2264}
2265
2266/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002267 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002268 * @mtd: MTD device structure
2269 * @to: offset to write to
2270 * @len: number of bytes to write
2271 * @retlen: pointer to variable to store the number of written bytes
2272 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002273 *
2274 * NAND write with ECC. Used when performing writes in interrupt context, this
2275 * may for example be called by mtdoops when writing an oops while in panic.
2276 */
2277static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2278 size_t *retlen, const uint8_t *buf)
2279{
2280 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002281 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002282 int ret;
2283
Brian Norris8b6e50c2011-05-25 14:59:01 -07002284 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002285 panic_nand_wait(mtd, chip, 400);
2286
Brian Norris8b6e50c2011-05-25 14:59:01 -07002287 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002288 panic_nand_get_device(chip, mtd, FL_WRITING);
2289
Brian Norris4a89ff82011-08-30 18:45:45 -07002290 ops.len = len;
2291 ops.datbuf = (uint8_t *)buf;
2292 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002293 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002294
Brian Norris4a89ff82011-08-30 18:45:45 -07002295 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002296
Brian Norris4a89ff82011-08-30 18:45:45 -07002297 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002298 return ret;
2299}
2300
2301/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002302 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002303 * @mtd: MTD device structure
2304 * @to: offset to write to
2305 * @len: number of bytes to write
2306 * @retlen: pointer to variable to store the number of written bytes
2307 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002309 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002311static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002312 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Brian Norris4a89ff82011-08-30 18:45:45 -07002314 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002315 int ret;
2316
Huang Shijie6a8214a2012-11-19 14:43:30 +08002317 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002318 ops.len = len;
2319 ops.datbuf = (uint8_t *)buf;
2320 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002321 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002322 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002323 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002324 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002325 return ret;
2326}
2327
2328/**
2329 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002330 * @mtd: MTD device structure
2331 * @to: offset to write to
2332 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002333 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002334 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002335 */
2336static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2337 struct mtd_oob_ops *ops)
2338{
Adrian Hunter03736152007-01-31 17:58:29 +02002339 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002340 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Brian Norris289c0522011-07-19 10:06:09 -07002342 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302343 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Brian Norris0612b9d2011-08-30 18:45:40 -07002345 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002346 len = chip->ecc.layout->oobavail;
2347 else
2348 len = mtd->oobsize;
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002351 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002352 pr_debug("%s: attempt to write past end of page\n",
2353 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return -EINVAL;
2355 }
2356
Adrian Hunter03736152007-01-31 17:58:29 +02002357 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002358 pr_debug("%s: attempt to start write outside oob\n",
2359 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002360 return -EINVAL;
2361 }
2362
Jason Liu775adc32011-02-25 13:06:18 +08002363 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002364 if (unlikely(to >= mtd->size ||
2365 ops->ooboffs + ops->ooblen >
2366 ((mtd->size >> chip->page_shift) -
2367 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002368 pr_debug("%s: attempt to write beyond end of device\n",
2369 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002370 return -EINVAL;
2371 }
2372
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002373 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002374 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002376 /* Shift to get page */
2377 page = (int)(to >> chip->page_shift);
2378
2379 /*
2380 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2381 * of my DiskOnChip 2000 test units) will clear the whole data page too
2382 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2383 * it in the doc2000 driver in August 1999. dwmw2.
2384 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002385 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002388 if (nand_check_wp(mtd)) {
2389 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002390 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002391 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002394 if (page == chip->pagebuf)
2395 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002397 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002398
Brian Norris0612b9d2011-08-30 18:45:40 -07002399 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002400 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2401 else
2402 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002403
Huang Shijieb0bb6902012-11-19 14:43:29 +08002404 chip->select_chip(mtd, -1);
2405
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002406 if (status)
2407 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Vitaly Wool70145682006-11-03 18:20:38 +03002409 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002411 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002414/**
2415 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002416 * @mtd: MTD device structure
2417 * @to: offset to write to
2418 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002419 */
2420static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2421 struct mtd_oob_ops *ops)
2422{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002423 int ret = -ENOTSUPP;
2424
2425 ops->retlen = 0;
2426
2427 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002428 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002429 pr_debug("%s: attempt to write beyond end of device\n",
2430 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002431 return -EINVAL;
2432 }
2433
Huang Shijie6a8214a2012-11-19 14:43:30 +08002434 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002435
Florian Fainellif8ac0412010-09-07 13:23:43 +02002436 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002437 case MTD_OPS_PLACE_OOB:
2438 case MTD_OPS_AUTO_OOB:
2439 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002440 break;
2441
2442 default:
2443 goto out;
2444 }
2445
2446 if (!ops->datbuf)
2447 ret = nand_do_write_oob(mtd, to, ops);
2448 else
2449 ret = nand_do_write_ops(mtd, to, ops);
2450
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002451out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002452 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return ret;
2454}
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002457 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002458 * @mtd: MTD device structure
2459 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002461 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002463static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002465 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002467 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2468 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
2471/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002472 * multi_erase_cmd - [GENERIC] AND specific block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002473 * @mtd: MTD device structure
2474 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002476 * AND multi block erase command function. Erase 4 consecutive blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002478static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002480 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002482 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_ERASE1, -1, page);
2486 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489/**
2490 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002491 * @mtd: MTD device structure
2492 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002494 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002496static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
David Woodhousee0c7d762006-05-13 18:07:53 +01002498 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002500
David A. Marlin30f464b2005-01-17 18:35:25 +00002501#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002503 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002504 * @mtd: MTD device structure
2505 * @instr: erase instruction
2506 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002508 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002510int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2511 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Adrian Hunter69423d92008-12-10 13:37:21 +00002513 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002514 struct nand_chip *chip = mtd->priv;
Florian Fainellif8ac0412010-09-07 13:23:43 +02002515 loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002516 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002517 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Brian Norris289c0522011-07-19 10:06:09 -07002519 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2520 __func__, (unsigned long long)instr->addr,
2521 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302523 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002527 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002530 page = (int)(instr->addr >> chip->page_shift);
2531 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002534 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002537 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /* Check, if it is write protected */
2540 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002541 pr_debug("%s: device is write protected!\n",
2542 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 instr->state = MTD_ERASE_FAILED;
2544 goto erase_exit;
2545 }
2546
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002547 /*
2548 * If BBT requires refresh, set the BBT page mask to see if the BBT
2549 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2550 * can not be matched. This is also done when the bbt is actually
Brian Norris7854d3f2011-06-23 14:12:08 -07002551 * erased to avoid recursive updates.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002552 */
2553 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2554 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /* Loop through the pages */
2557 len = instr->len;
2558
2559 instr->state = MTD_ERASING;
2560
2561 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002562 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002563 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2564 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002565 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2566 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 instr->state = MTD_ERASE_FAILED;
2568 goto erase_exit;
2569 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002570
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002571 /*
2572 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002573 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002574 */
2575 if (page <= chip->pagebuf && chip->pagebuf <
2576 (page + pages_per_block))
2577 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002580
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002581 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002583 /*
2584 * See if operation failed and additional status checks are
2585 * available
2586 */
2587 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2588 status = chip->errstat(mtd, chip, FL_ERASING,
2589 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002592 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002593 pr_debug("%s: failed erase, page 0x%08x\n",
2594 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002596 instr->fail_addr =
2597 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 goto erase_exit;
2599 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002600
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002601 /*
2602 * If BBT requires refresh, set the BBT rewrite flag to the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002603 * page being erased.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002604 */
2605 if (bbt_masked_page != 0xffffffff &&
2606 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002607 rewrite_bbt[chipnr] =
2608 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002611 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 page += pages_per_block;
2613
2614 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002615 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002617 chip->select_chip(mtd, -1);
2618 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002619
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002620 /*
2621 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Brian Norris8b6e50c2011-05-25 14:59:01 -07002622 * page mask to see if this BBT should be rewritten.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002623 */
2624 if (bbt_masked_page != 0xffffffff &&
2625 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2626 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2627 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629 }
2630 instr->state = MTD_ERASE_DONE;
2631
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002632erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002637 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 nand_release_device(mtd);
2639
David Woodhouse49defc02007-10-06 15:01:59 -04002640 /* Do call back function */
2641 if (!ret)
2642 mtd_erase_callback(instr);
2643
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002644 /*
2645 * If BBT requires refresh and erase was successful, rewrite any
Brian Norris8b6e50c2011-05-25 14:59:01 -07002646 * selected bad block tables.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002647 */
2648 if (bbt_masked_page == 0xffffffff || ret)
2649 return ret;
2650
2651 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2652 if (!rewrite_bbt[chipnr])
2653 continue;
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 /* Update the BBT for chip */
Brian Norris289c0522011-07-19 10:06:09 -07002655 pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2656 __func__, chipnr, rewrite_bbt[chipnr],
2657 chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002658 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002659 }
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 /* Return more or less happy */
2662 return ret;
2663}
2664
2665/**
2666 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002667 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002669 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002671static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Brian Norris289c0522011-07-19 10:06:09 -07002673 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002676 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002678 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002682 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002683 * @mtd: MTD device structure
2684 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002686static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002688 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
2691/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002692 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002693 * @mtd: MTD device structure
2694 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002696static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002698 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 int ret;
2700
Florian Fainellif8ac0412010-09-07 13:23:43 +02002701 ret = nand_block_isbad(mtd, ofs);
2702 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002703 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (ret > 0)
2705 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002706 return ret;
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002709 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002713 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2714 * @mtd: MTD device structure
2715 * @chip: nand chip info structure
2716 * @addr: feature address.
2717 * @subfeature_param: the subfeature parameters, a four bytes array.
2718 */
2719static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2720 int addr, uint8_t *subfeature_param)
2721{
2722 int status;
2723
2724 if (!chip->onfi_version)
2725 return -EINVAL;
2726
2727 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2728 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2729 status = chip->waitfunc(mtd, chip);
2730 if (status & NAND_STATUS_FAIL)
2731 return -EIO;
2732 return 0;
2733}
2734
2735/**
2736 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2737 * @mtd: MTD device structure
2738 * @chip: nand chip info structure
2739 * @addr: feature address.
2740 * @subfeature_param: the subfeature parameters, a four bytes array.
2741 */
2742static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2743 int addr, uint8_t *subfeature_param)
2744{
2745 if (!chip->onfi_version)
2746 return -EINVAL;
2747
2748 /* clear the sub feature parameters */
2749 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2750
2751 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2752 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2753 return 0;
2754}
2755
2756/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002757 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002758 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002759 */
2760static int nand_suspend(struct mtd_info *mtd)
2761{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002762 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002763}
2764
2765/**
2766 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002767 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002768 */
2769static void nand_resume(struct mtd_info *mtd)
2770{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002771 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002772
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002773 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002774 nand_release_device(mtd);
2775 else
Brian Norrisd0370212011-07-19 10:06:08 -07002776 pr_err("%s called for a chip which is not in suspended state\n",
2777 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002778}
2779
Brian Norris8b6e50c2011-05-25 14:59:01 -07002780/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002781static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002782{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002784 if (!chip->chip_delay)
2785 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
2787 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002788 if (chip->cmdfunc == NULL)
2789 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002792 if (chip->waitfunc == NULL)
2793 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002795 if (!chip->select_chip)
2796 chip->select_chip = nand_select_chip;
2797 if (!chip->read_byte)
2798 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2799 if (!chip->read_word)
2800 chip->read_word = nand_read_word;
2801 if (!chip->block_bad)
2802 chip->block_bad = nand_block_bad;
2803 if (!chip->block_markbad)
2804 chip->block_markbad = nand_default_block_markbad;
2805 if (!chip->write_buf)
2806 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2807 if (!chip->read_buf)
2808 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002809 if (!chip->scan_bbt)
2810 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002811
2812 if (!chip->controller) {
2813 chip->controller = &chip->hwcontrol;
2814 spin_lock_init(&chip->controller->lock);
2815 init_waitqueue_head(&chip->controller->wq);
2816 }
2817
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002818}
2819
Brian Norris8b6e50c2011-05-25 14:59:01 -07002820/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002821static void sanitize_string(uint8_t *s, size_t len)
2822{
2823 ssize_t i;
2824
Brian Norris8b6e50c2011-05-25 14:59:01 -07002825 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002826 s[len - 1] = 0;
2827
Brian Norris8b6e50c2011-05-25 14:59:01 -07002828 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002829 for (i = 0; i < len - 1; i++) {
2830 if (s[i] < ' ' || s[i] > 127)
2831 s[i] = '?';
2832 }
2833
Brian Norris8b6e50c2011-05-25 14:59:01 -07002834 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002835 strim(s);
2836}
2837
2838static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2839{
2840 int i;
2841 while (len--) {
2842 crc ^= *p++ << 8;
2843 for (i = 0; i < 8; i++)
2844 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2845 }
2846
2847 return crc;
2848}
2849
2850/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002851 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002852 */
2853static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002854 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002855{
2856 struct nand_onfi_params *p = &chip->onfi_params;
2857 int i;
2858 int val;
2859
Brian Norris7854d3f2011-06-23 14:12:08 -07002860 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002861 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2862 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2863 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2864 return 0;
2865
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002866 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2867 for (i = 0; i < 3; i++) {
2868 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2869 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2870 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002871 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002872 break;
2873 }
2874 }
2875
2876 if (i == 3)
2877 return 0;
2878
Brian Norris8b6e50c2011-05-25 14:59:01 -07002879 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002880 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002881 if (val & (1 << 5))
2882 chip->onfi_version = 23;
2883 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002884 chip->onfi_version = 22;
2885 else if (val & (1 << 3))
2886 chip->onfi_version = 21;
2887 else if (val & (1 << 2))
2888 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002889 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002890 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002891 else
2892 chip->onfi_version = 0;
2893
2894 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002895 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002896 return 0;
2897 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002898
2899 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2900 sanitize_string(p->model, sizeof(p->model));
2901 if (!mtd->name)
2902 mtd->name = p->model;
2903 mtd->writesize = le32_to_cpu(p->byte_per_page);
2904 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2905 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTET63795752012-03-19 15:35:25 +01002906 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2907 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002908 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002909 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002910 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002911
Huang Shijied42b5de2012-02-17 11:22:37 +08002912 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002913 return 1;
2914}
2915
2916/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07002917 * nand_id_has_period - Check if an ID string has a given wraparound period
2918 * @id_data: the ID string
2919 * @arrlen: the length of the @id_data array
2920 * @period: the period of repitition
2921 *
2922 * Check if an ID string is repeated within a given sequence of bytes at
2923 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2924 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2925 * if the repetition has a period of @period; otherwise, returns zero.
2926 */
2927static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2928{
2929 int i, j;
2930 for (i = 0; i < period; i++)
2931 for (j = i + period; j < arrlen; j += period)
2932 if (id_data[i] != id_data[j])
2933 return 0;
2934 return 1;
2935}
2936
2937/*
2938 * nand_id_len - Get the length of an ID string returned by CMD_READID
2939 * @id_data: the ID string
2940 * @arrlen: the length of the @id_data array
2941
2942 * Returns the length of the ID string, according to known wraparound/trailing
2943 * zero patterns. If no pattern exists, returns the length of the array.
2944 */
2945static int nand_id_len(u8 *id_data, int arrlen)
2946{
2947 int last_nonzero, period;
2948
2949 /* Find last non-zero byte */
2950 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2951 if (id_data[last_nonzero])
2952 break;
2953
2954 /* All zeros */
2955 if (last_nonzero < 0)
2956 return 0;
2957
2958 /* Calculate wraparound period */
2959 for (period = 1; period < arrlen; period++)
2960 if (nand_id_has_period(id_data, arrlen, period))
2961 break;
2962
2963 /* There's a repeated pattern */
2964 if (period < arrlen)
2965 return period;
2966
2967 /* There are trailing zeros */
2968 if (last_nonzero < arrlen - 1)
2969 return last_nonzero + 1;
2970
2971 /* No pattern detected */
2972 return arrlen;
2973}
2974
2975/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002976 * Many new NAND share similar device ID codes, which represent the size of the
2977 * chip. The rest of the parameters must be decoded according to generic or
2978 * manufacturer-specific "extended ID" decoding patterns.
2979 */
2980static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2981 u8 id_data[8], int *busw)
2982{
Brian Norrise3b88bd2012-09-24 20:40:52 -07002983 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002984 /* The 3rd id byte holds MLC / multichip data */
2985 chip->cellinfo = id_data[2];
2986 /* The 4th id byte is the important one */
2987 extid = id_data[3];
2988
Brian Norrise3b88bd2012-09-24 20:40:52 -07002989 id_len = nand_id_len(id_data, 8);
2990
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002991 /*
2992 * Field definitions are in the following datasheets:
2993 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07002994 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07002995 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002996 *
Brian Norrisaf451af2012-10-09 23:26:06 -07002997 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2998 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002999 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003000 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Brian Norris6924d992012-11-14 21:46:30 -08003001 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Brian Norrisaf451af2012-10-09 23:26:06 -07003002 id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003003 /* Calc pagesize */
3004 mtd->writesize = 2048 << (extid & 0x03);
3005 extid >>= 2;
3006 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003007 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003008 case 1:
3009 mtd->oobsize = 128;
3010 break;
3011 case 2:
3012 mtd->oobsize = 218;
3013 break;
3014 case 3:
3015 mtd->oobsize = 400;
3016 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003017 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003018 mtd->oobsize = 436;
3019 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003020 case 5:
3021 mtd->oobsize = 512;
3022 break;
3023 case 6:
3024 default: /* Other cases are "reserved" (unknown) */
3025 mtd->oobsize = 640;
3026 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003027 }
3028 extid >>= 2;
3029 /* Calc blocksize */
3030 mtd->erasesize = (128 * 1024) <<
3031 (((extid >> 1) & 0x04) | (extid & 0x03));
3032 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003033 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3034 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3035 unsigned int tmp;
3036
3037 /* Calc pagesize */
3038 mtd->writesize = 2048 << (extid & 0x03);
3039 extid >>= 2;
3040 /* Calc oobsize */
3041 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3042 case 0:
3043 mtd->oobsize = 128;
3044 break;
3045 case 1:
3046 mtd->oobsize = 224;
3047 break;
3048 case 2:
3049 mtd->oobsize = 448;
3050 break;
3051 case 3:
3052 mtd->oobsize = 64;
3053 break;
3054 case 4:
3055 mtd->oobsize = 32;
3056 break;
3057 case 5:
3058 mtd->oobsize = 16;
3059 break;
3060 default:
3061 mtd->oobsize = 640;
3062 break;
3063 }
3064 extid >>= 2;
3065 /* Calc blocksize */
3066 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3067 if (tmp < 0x03)
3068 mtd->erasesize = (128 * 1024) << tmp;
3069 else if (tmp == 0x03)
3070 mtd->erasesize = 768 * 1024;
3071 else
3072 mtd->erasesize = (64 * 1024) << tmp;
3073 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003074 } else {
3075 /* Calc pagesize */
3076 mtd->writesize = 1024 << (extid & 0x03);
3077 extid >>= 2;
3078 /* Calc oobsize */
3079 mtd->oobsize = (8 << (extid & 0x01)) *
3080 (mtd->writesize >> 9);
3081 extid >>= 2;
3082 /* Calc blocksize. Blocksize is multiples of 64KiB */
3083 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3084 extid >>= 2;
3085 /* Get buswidth information */
3086 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3087 }
3088}
3089
3090/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003091 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3092 * decodes a matching ID table entry and assigns the MTD size parameters for
3093 * the chip.
3094 */
3095static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3096 struct nand_flash_dev *type, u8 id_data[8],
3097 int *busw)
3098{
3099 int maf_id = id_data[0];
3100
3101 mtd->erasesize = type->erasesize;
3102 mtd->writesize = type->pagesize;
3103 mtd->oobsize = mtd->writesize / 32;
3104 *busw = type->options & NAND_BUSWIDTH_16;
3105
3106 /*
3107 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3108 * some Spansion chips have erasesize that conflicts with size
3109 * listed in nand_ids table.
3110 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3111 */
3112 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3113 && id_data[6] == 0x00 && id_data[7] == 0x00
3114 && mtd->writesize == 512) {
3115 mtd->erasesize = 128 * 1024;
3116 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3117 }
3118}
3119
3120/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003121 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3122 * heuristic patterns using various detected parameters (e.g., manufacturer,
3123 * page size, cell-type information).
3124 */
3125static void nand_decode_bbm_options(struct mtd_info *mtd,
3126 struct nand_chip *chip, u8 id_data[8])
3127{
3128 int maf_id = id_data[0];
3129
3130 /* Set the bad block position */
3131 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3132 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3133 else
3134 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3135
3136 /*
3137 * Bad block marker is stored in the last page of each block on Samsung
3138 * and Hynix MLC devices; stored in first two pages of each block on
3139 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3140 * AMD/Spansion, and Macronix. All others scan only the first page.
3141 */
3142 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3143 (maf_id == NAND_MFR_SAMSUNG ||
3144 maf_id == NAND_MFR_HYNIX))
3145 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3146 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3147 (maf_id == NAND_MFR_SAMSUNG ||
3148 maf_id == NAND_MFR_HYNIX ||
3149 maf_id == NAND_MFR_TOSHIBA ||
3150 maf_id == NAND_MFR_AMD ||
3151 maf_id == NAND_MFR_MACRONIX)) ||
3152 (mtd->writesize == 2048 &&
3153 maf_id == NAND_MFR_MICRON))
3154 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3155}
3156
3157/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003159 */
3160static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003161 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003162 int busw,
3163 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003164 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003165{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003166 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003167 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003170 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Karl Beldanef89a882008-09-15 14:37:29 +02003172 /*
3173 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003174 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003175 */
3176 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003179 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003182 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003183 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Brian Norris8b6e50c2011-05-25 14:59:01 -07003185 /*
3186 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003187 * interface concerns can cause random data which looks like a
3188 * possibly credible NAND flash to appear. If the two results do
3189 * not match, ignore the device completely.
3190 */
3191
3192 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3193
Brian Norris4aef9b72012-09-24 20:40:48 -07003194 /* Read entire ID string */
3195 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003196 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003197
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003198 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003199 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07003200 "%02x,%02x against %02x,%02x\n", __func__,
3201 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003202 return ERR_PTR(-ENODEV);
3203 }
3204
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003205 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003206 type = nand_flash_ids;
3207
3208 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003209 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02003210 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00003211
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003212 chip->onfi_version = 0;
3213 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003214 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003215 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003216 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003217 }
3218
David Woodhouse5e81e882010-02-26 18:32:56 +00003219 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003220 return ERR_PTR(-ENODEV);
3221
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003222 if (!mtd->name)
3223 mtd->name = type->name;
3224
Adrian Hunter69423d92008-12-10 13:37:21 +00003225 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003226
Huang Shijie12a40a52010-09-27 10:43:53 +08003227 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003228 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003229 busw = chip->init_size(mtd, chip, id_data);
3230 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003231 /* Decode parameters from extended ID */
3232 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003233 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003234 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003235 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003236 /* Get chip options */
3237 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003238
Brian Norris8b6e50c2011-05-25 14:59:01 -07003239 /*
3240 * Check if chip is not a Samsung device. Do not clear the
3241 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003242 */
3243 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3244 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3245ident_done:
3246
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003247 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003248 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003249 if (nand_manuf_ids[maf_idx].id == *maf_id)
3250 break;
3251 }
3252
3253 /*
3254 * Check, if buswidth is correct. Hardware drivers should set
Brian Norris8b6e50c2011-05-25 14:59:01 -07003255 * chip correct!
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003256 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003257 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003258 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003259 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3260 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003261 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003262 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3263 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003264 return ERR_PTR(-EINVAL);
3265 }
3266
Brian Norris7e74c2d2012-09-24 20:40:49 -07003267 nand_decode_bbm_options(mtd, chip, id_data);
3268
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003269 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003270 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003271 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003272 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003273
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003274 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003275 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003276 if (chip->chipsize & 0xffffffff)
3277 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003278 else {
3279 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3280 chip->chip_shift += 32 - 1;
3281 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003282
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003283 chip->badblockbits = 8;
3284
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003285 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003286 if (chip->options & NAND_4PAGE_ARRAY)
3287 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003288 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003289 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003290
Brian Norris8b6e50c2011-05-25 14:59:01 -07003291 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003292 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3293 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003294
Huang Shijie886bd332012-04-09 11:41:37 +08003295 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003296 " %dMiB, page size: %d, OOB size: %d\n",
Huang Shijie886bd332012-04-09 11:41:37 +08003297 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3298 chip->onfi_version ? chip->onfi_params.model : type->name,
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003299 (int)(chip->chipsize >> 20), mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003300
3301 return type;
3302}
3303
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003304/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003305 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003306 * @mtd: MTD device structure
3307 * @maxchips: number of chips to scan for
3308 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003309 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003310 * This is the first phase of the normal nand_scan() function. It reads the
3311 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003312 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003313 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003314 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003315int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3316 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003317{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003318 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003319 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003320 struct nand_flash_dev *type;
3321
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003322 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003323 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003324 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003325 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003326
3327 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003328 type = nand_get_flash_type(mtd, chip, busw,
3329 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003330
3331 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003332 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003333 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003334 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003335 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
3337
Huang Shijie07300162012-11-09 16:23:45 +08003338 chip->select_chip(mtd, -1);
3339
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003340 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003341 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003342 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003343 /* See comment in nand_get_flash_type for reset */
3344 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003346 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003348 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003349 nand_dev_id != chip->read_byte(mtd)) {
3350 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 break;
Huang Shijie07300162012-11-09 16:23:45 +08003352 }
3353 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
3355 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003356 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003359 chip->numchips = i;
3360 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
David Woodhouse3b85c322006-09-25 17:06:53 +01003362 return 0;
3363}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003364EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003365
3366
3367/**
3368 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003369 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003370 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003371 * This is the second phase of the normal nand_scan() function. It fills out
3372 * all the uninitialized function pointers with the defaults and scans for a
3373 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003374 */
3375int nand_scan_tail(struct mtd_info *mtd)
3376{
3377 int i;
3378 struct nand_chip *chip = mtd->priv;
3379
Brian Norrise2414f42012-02-06 13:44:00 -08003380 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3381 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3382 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3383
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003384 if (!(chip->options & NAND_OWN_BUFFERS))
3385 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3386 if (!chip->buffers)
3387 return -ENOMEM;
3388
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003389 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003390 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003391
3392 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003393 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003394 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003395 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003396 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003398 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 break;
3400 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003401 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 break;
3403 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003404 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003406 case 128:
3407 chip->ecc.layout = &nand_oob_128;
3408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003410 pr_warn("No oob scheme defined for oobsize %d\n",
3411 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 BUG();
3413 }
3414 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003415
David Woodhouse956e9442006-09-25 17:12:39 +01003416 if (!chip->write_page)
3417 chip->write_page = nand_write_page;
3418
Huang Shijie7db03ec2012-09-13 14:57:52 +08003419 /* set for ONFI nand */
3420 if (!chip->onfi_set_features)
3421 chip->onfi_set_features = nand_onfi_set_features;
3422 if (!chip->onfi_get_features)
3423 chip->onfi_get_features = nand_onfi_get_features;
3424
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003425 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003426 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003427 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003428 */
David Woodhouse956e9442006-09-25 17:12:39 +01003429
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003430 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003431 case NAND_ECC_HW_OOB_FIRST:
3432 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3433 if (!chip->ecc.calculate || !chip->ecc.correct ||
3434 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003435 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003436 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003437 BUG();
3438 }
3439 if (!chip->ecc.read_page)
3440 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3441
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003442 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003443 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003444 if (!chip->ecc.read_page)
3445 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003446 if (!chip->ecc.write_page)
3447 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003448 if (!chip->ecc.read_page_raw)
3449 chip->ecc.read_page_raw = nand_read_page_raw;
3450 if (!chip->ecc.write_page_raw)
3451 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003452 if (!chip->ecc.read_oob)
3453 chip->ecc.read_oob = nand_read_oob_std;
3454 if (!chip->ecc.write_oob)
3455 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003456
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003457 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003458 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3459 !chip->ecc.hwctl) &&
3460 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003461 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003462 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003463 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003464 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003465 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003466 BUG();
3467 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003468 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003469 if (!chip->ecc.read_page)
3470 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003471 if (!chip->ecc.write_page)
3472 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003473 if (!chip->ecc.read_page_raw)
3474 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3475 if (!chip->ecc.write_page_raw)
3476 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003477 if (!chip->ecc.read_oob)
3478 chip->ecc.read_oob = nand_read_oob_syndrome;
3479 if (!chip->ecc.write_oob)
3480 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003481
Mike Dunne2788c92012-04-25 12:06:10 -07003482 if (mtd->writesize >= chip->ecc.size) {
3483 if (!chip->ecc.strength) {
3484 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3485 BUG();
3486 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003487 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003488 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003489 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003490 "%d byte page size, fallback to SW ECC\n",
3491 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003492 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003494 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003495 chip->ecc.calculate = nand_calculate_ecc;
3496 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003497 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003498 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003499 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003500 chip->ecc.read_page_raw = nand_read_page_raw;
3501 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003502 chip->ecc.read_oob = nand_read_oob_std;
3503 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003504 if (!chip->ecc.size)
3505 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003506 chip->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003507 chip->ecc.strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003509
Ivan Djelic193bd402011-03-11 11:05:33 +01003510 case NAND_ECC_SOFT_BCH:
3511 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003512 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003513 BUG();
3514 }
3515 chip->ecc.calculate = nand_bch_calculate_ecc;
3516 chip->ecc.correct = nand_bch_correct_data;
3517 chip->ecc.read_page = nand_read_page_swecc;
3518 chip->ecc.read_subpage = nand_read_subpage;
3519 chip->ecc.write_page = nand_write_page_swecc;
3520 chip->ecc.read_page_raw = nand_read_page_raw;
3521 chip->ecc.write_page_raw = nand_write_page_raw;
3522 chip->ecc.read_oob = nand_read_oob_std;
3523 chip->ecc.write_oob = nand_write_oob_std;
3524 /*
3525 * Board driver should supply ecc.size and ecc.bytes values to
3526 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003527 * for details. Otherwise, default to 4 bits for large page
3528 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003529 */
3530 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3531 chip->ecc.size = 512;
3532 chip->ecc.bytes = 7;
3533 }
3534 chip->ecc.priv = nand_bch_init(mtd,
3535 chip->ecc.size,
3536 chip->ecc.bytes,
3537 &chip->ecc.layout);
3538 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003539 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003540 BUG();
3541 }
Mike Dunn6a918ba2012-03-11 14:21:11 -07003542 chip->ecc.strength =
Mike Dunne2788c92012-04-25 12:06:10 -07003543 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003544 break;
3545
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003546 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003547 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003548 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003549 chip->ecc.read_page = nand_read_page_raw;
3550 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003551 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003552 chip->ecc.read_page_raw = nand_read_page_raw;
3553 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003554 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003555 chip->ecc.size = mtd->writesize;
3556 chip->ecc.bytes = 0;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003557 chip->ecc.strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003561 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003562 BUG();
3563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Brian Norris9ce244b2011-08-30 18:45:37 -07003565 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003566 if (!chip->ecc.read_oob_raw)
3567 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003568 if (!chip->ecc.write_oob_raw)
3569 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3570
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003571 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003572 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003573 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003574 */
3575 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003576 for (i = 0; chip->ecc.layout->oobfree[i].length
3577 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003578 chip->ecc.layout->oobavail +=
3579 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003580 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003581
3582 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003583 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003584 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003585 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003586 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003587 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003588 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003589 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003591 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003592
Brian Norris8b6e50c2011-05-25 14:59:01 -07003593 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003594 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3595 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003596 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003597 case 2:
3598 mtd->subpage_sft = 1;
3599 break;
3600 case 4:
3601 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003602 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003603 mtd->subpage_sft = 2;
3604 break;
3605 }
3606 }
3607 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3608
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003609 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003610 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003613 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003615 /* Large page NAND with SOFT_ECC should support subpage reads */
3616 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3617 chip->options |= NAND_SUBPAGE_READ;
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /* Fill in remaining MTD driver data */
3620 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003621 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3622 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003623 mtd->_erase = nand_erase;
3624 mtd->_point = NULL;
3625 mtd->_unpoint = NULL;
3626 mtd->_read = nand_read;
3627 mtd->_write = nand_write;
3628 mtd->_panic_write = panic_nand_write;
3629 mtd->_read_oob = nand_read_oob;
3630 mtd->_write_oob = nand_write_oob;
3631 mtd->_sync = nand_sync;
3632 mtd->_lock = NULL;
3633 mtd->_unlock = NULL;
3634 mtd->_suspend = nand_suspend;
3635 mtd->_resume = nand_resume;
3636 mtd->_block_isbad = nand_block_isbad;
3637 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003638 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Mike Dunn6a918ba2012-03-11 14:21:11 -07003640 /* propagate ecc info to mtd_info */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003641 mtd->ecclayout = chip->ecc.layout;
Mike Dunn86c20722012-04-25 12:06:05 -07003642 mtd->ecc_strength = chip->ecc.strength;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003643 /*
3644 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3645 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3646 * properly set.
3647 */
3648 if (!mtd->bitflip_threshold)
3649 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003651 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003652 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
3655 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003656 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003658EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Brian Norris8b6e50c2011-05-25 14:59:01 -07003660/*
3661 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003662 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003663 * to call us from in-kernel code if the core NAND support is modular.
3664 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003665#ifdef MODULE
3666#define caller_is_module() (1)
3667#else
3668#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003669 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003670#endif
3671
3672/**
3673 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003674 * @mtd: MTD device structure
3675 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003676 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003677 * This fills out all the uninitialized function pointers with the defaults.
3678 * The flash ID is read and the mtd/chip structures are filled with the
3679 * appropriate values. The mtd->owner field must be set to the module of the
3680 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003681 */
3682int nand_scan(struct mtd_info *mtd, int maxchips)
3683{
3684 int ret;
3685
3686 /* Many callers got this wrong, so check for it for a while... */
3687 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003688 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003689 BUG();
3690 }
3691
David Woodhouse5e81e882010-02-26 18:32:56 +00003692 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003693 if (!ret)
3694 ret = nand_scan_tail(mtd);
3695 return ret;
3696}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003697EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003700 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003701 * @mtd: MTD device structure
3702 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003703void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003705 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Ivan Djelic193bd402011-03-11 11:05:33 +01003707 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3708 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3709
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003710 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Jesper Juhlfa671642005-11-07 01:01:27 -08003712 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003713 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003714 if (!(chip->options & NAND_OWN_BUFFERS))
3715 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003716
3717 /* Free bad block descriptor memory */
3718 if (chip->badblock_pattern && chip->badblock_pattern->options
3719 & NAND_BBT_DYNAMICSTRUCT)
3720 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
David Woodhousee0c7d762006-05-13 18:07:53 +01003722EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003723
3724static int __init nand_base_init(void)
3725{
3726 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3727 return 0;
3728}
3729
3730static void __exit nand_base_exit(void)
3731{
3732 led_trigger_unregister_simple(nand_led_trigger);
3733}
3734
3735module_init(nand_base_init);
3736module_exit(nand_base_exit);
3737
David Woodhousee0c7d762006-05-13 18:07:53 +01003738MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003739MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3740MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003741MODULE_DESCRIPTION("Generic NAND flash driver code");