blob: 42c63927609dbee8697c691c8e97515cf509b990 [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
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200828 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800829 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Richard Purdie8fe833c2006-03-31 02:31:14 -0800831 led_trigger_event(nand_led_trigger, LED_FULL);
832
Brian Norris8b6e50c2011-05-25 14:59:01 -0700833 /*
834 * Apply this short delay always to ensure that we do wait tWB in any
835 * case on any machine.
836 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100837 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200839 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
840 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000841 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200842 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200844 if (in_interrupt() || oops_in_progress)
845 panic_nand_wait(mtd, chip, timeo);
846 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800847 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200848 while (time_before(jiffies, timeo)) {
849 if (chip->dev_ready) {
850 if (chip->dev_ready(mtd))
851 break;
852 } else {
853 if (chip->read_byte(mtd) & NAND_STATUS_READY)
854 break;
855 }
856 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800859 led_trigger_event(nand_led_trigger, LED_OFF);
860
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200861 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100862 /* This can happen if in case of timeout or buggy dev_ready */
863 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return status;
865}
866
867/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700868 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700869 * @mtd: mtd info
870 * @ofs: offset to start unlock from
871 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700872 * @invert: when = 0, unlock the range of blocks within the lower and
873 * upper boundary address
874 * when = 1, unlock the range of blocks outside the boundaries
875 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530876 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700877 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530878 */
879static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
880 uint64_t len, int invert)
881{
882 int ret = 0;
883 int status, page;
884 struct nand_chip *chip = mtd->priv;
885
886 /* Submit address of first page to unlock */
887 page = ofs >> chip->page_shift;
888 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
889
890 /* Submit address of last page to unlock */
891 page = (ofs + len) >> chip->page_shift;
892 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
893 (page | invert) & chip->pagemask);
894
895 /* Call wait ready function */
896 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530897 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400898 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700899 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530900 __func__, status);
901 ret = -EIO;
902 }
903
904 return ret;
905}
906
907/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700908 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700909 * @mtd: mtd info
910 * @ofs: offset to start unlock from
911 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530912 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700913 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530914 */
915int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
916{
917 int ret = 0;
918 int chipnr;
919 struct nand_chip *chip = mtd->priv;
920
Brian Norris289c0522011-07-19 10:06:09 -0700921 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530922 __func__, (unsigned long long)ofs, len);
923
924 if (check_offs_len(mtd, ofs, len))
925 ret = -EINVAL;
926
927 /* Align to last block address if size addresses end of the device */
928 if (ofs + len == mtd->size)
929 len -= mtd->erasesize;
930
Huang Shijie6a8214a2012-11-19 14:43:30 +0800931 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530932
933 /* Shift to get chip number */
934 chipnr = ofs >> chip->chip_shift;
935
936 chip->select_chip(mtd, chipnr);
937
938 /* Check, if it is write protected */
939 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700940 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530941 __func__);
942 ret = -EIO;
943 goto out;
944 }
945
946 ret = __nand_unlock(mtd, ofs, len, 0);
947
948out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800949 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530950 nand_release_device(mtd);
951
952 return ret;
953}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200954EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530955
956/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700957 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700958 * @mtd: mtd info
959 * @ofs: offset to start unlock from
960 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530961 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700962 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
963 * have this feature, but it allows only to lock all blocks, not for specified
964 * range for block. Implementing 'lock' feature by making use of 'unlock', for
965 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530966 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700967 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530968 */
969int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
970{
971 int ret = 0;
972 int chipnr, status, page;
973 struct nand_chip *chip = mtd->priv;
974
Brian Norris289c0522011-07-19 10:06:09 -0700975 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530976 __func__, (unsigned long long)ofs, len);
977
978 if (check_offs_len(mtd, ofs, len))
979 ret = -EINVAL;
980
Huang Shijie6a8214a2012-11-19 14:43:30 +0800981 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530982
983 /* Shift to get chip number */
984 chipnr = ofs >> chip->chip_shift;
985
986 chip->select_chip(mtd, chipnr);
987
988 /* Check, if it is write protected */
989 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700990 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530991 __func__);
992 status = MTD_ERASE_FAILED;
993 ret = -EIO;
994 goto out;
995 }
996
997 /* Submit address of first page to lock */
998 page = ofs >> chip->page_shift;
999 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1000
1001 /* Call wait ready function */
1002 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301003 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001004 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001005 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301006 __func__, status);
1007 ret = -EIO;
1008 goto out;
1009 }
1010
1011 ret = __nand_unlock(mtd, ofs, len, 0x1);
1012
1013out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001014 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301015 nand_release_device(mtd);
1016
1017 return ret;
1018}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001019EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301020
1021/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001022 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001023 * @mtd: mtd info structure
1024 * @chip: nand chip info structure
1025 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001026 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001027 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001028 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001029 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001030 */
1031static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001032 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001033{
1034 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001035 if (oob_required)
1036 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001037 return 0;
1038}
1039
1040/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001041 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001042 * @mtd: mtd info structure
1043 * @chip: nand chip info structure
1044 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001045 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001046 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001047 *
1048 * We need a special oob layout and handling even when OOB isn't used.
1049 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001050static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001051 struct nand_chip *chip, uint8_t *buf,
1052 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001053{
1054 int eccsize = chip->ecc.size;
1055 int eccbytes = chip->ecc.bytes;
1056 uint8_t *oob = chip->oob_poi;
1057 int steps, size;
1058
1059 for (steps = chip->ecc.steps; steps > 0; steps--) {
1060 chip->read_buf(mtd, buf, eccsize);
1061 buf += eccsize;
1062
1063 if (chip->ecc.prepad) {
1064 chip->read_buf(mtd, oob, chip->ecc.prepad);
1065 oob += chip->ecc.prepad;
1066 }
1067
1068 chip->read_buf(mtd, oob, eccbytes);
1069 oob += eccbytes;
1070
1071 if (chip->ecc.postpad) {
1072 chip->read_buf(mtd, oob, chip->ecc.postpad);
1073 oob += chip->ecc.postpad;
1074 }
1075 }
1076
1077 size = mtd->oobsize - (oob - chip->oob_poi);
1078 if (size)
1079 chip->read_buf(mtd, oob, size);
1080
1081 return 0;
1082}
1083
1084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001085 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001086 * @mtd: mtd info structure
1087 * @chip: nand chip info structure
1088 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001089 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001090 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001091 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001092static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001093 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001095 int i, eccsize = chip->ecc.size;
1096 int eccbytes = chip->ecc.bytes;
1097 int eccsteps = chip->ecc.steps;
1098 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001099 uint8_t *ecc_calc = chip->buffers->ecccalc;
1100 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001101 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001102 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001103
Brian Norris1fbb9382012-05-02 10:14:55 -07001104 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001105
1106 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1107 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1108
1109 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001110 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001111
1112 eccsteps = chip->ecc.steps;
1113 p = buf;
1114
1115 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1116 int stat;
1117
1118 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001119 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001120 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001121 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001122 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001123 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1124 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001125 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001126 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001130 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001131 * @mtd: mtd info structure
1132 * @chip: nand chip info structure
1133 * @data_offs: offset of requested data within the page
1134 * @readlen: data length
1135 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001136 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001137static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1138 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001139{
1140 int start_step, end_step, num_steps;
1141 uint32_t *eccpos = chip->ecc.layout->eccpos;
1142 uint8_t *p;
1143 int data_col_addr, i, gaps = 0;
1144 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1145 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001146 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001147 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001148
Brian Norris7854d3f2011-06-23 14:12:08 -07001149 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001150 start_step = data_offs / chip->ecc.size;
1151 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1152 num_steps = end_step - start_step + 1;
1153
Brian Norris8b6e50c2011-05-25 14:59:01 -07001154 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001155 datafrag_len = num_steps * chip->ecc.size;
1156 eccfrag_len = num_steps * chip->ecc.bytes;
1157
1158 data_col_addr = start_step * chip->ecc.size;
1159 /* If we read not a page aligned data */
1160 if (data_col_addr != 0)
1161 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1162
1163 p = bufpoi + data_col_addr;
1164 chip->read_buf(mtd, p, datafrag_len);
1165
Brian Norris8b6e50c2011-05-25 14:59:01 -07001166 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001167 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1168 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1169
Brian Norris8b6e50c2011-05-25 14:59:01 -07001170 /*
1171 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001172 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001173 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001174 for (i = 0; i < eccfrag_len - 1; i++) {
1175 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1176 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1177 gaps = 1;
1178 break;
1179 }
1180 }
1181 if (gaps) {
1182 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1183 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1184 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001185 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001186 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001187 * about buswidth alignment in read_buf.
1188 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001189 index = start_step * chip->ecc.bytes;
1190
1191 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001192 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001193 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001194 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001195 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001196 aligned_len++;
1197
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001198 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1199 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001200 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1201 }
1202
1203 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001204 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001205
1206 p = bufpoi + data_col_addr;
1207 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1208 int stat;
1209
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001210 stat = chip->ecc.correct(mtd, p,
1211 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001212 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001213 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001214 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001215 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001216 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1217 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001218 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001219 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001220}
1221
1222/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001223 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001224 * @mtd: mtd info structure
1225 * @chip: nand chip info structure
1226 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001227 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001229 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001230 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001231 */
1232static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001233 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001234{
1235 int i, eccsize = chip->ecc.size;
1236 int eccbytes = chip->ecc.bytes;
1237 int eccsteps = chip->ecc.steps;
1238 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001239 uint8_t *ecc_calc = chip->buffers->ecccalc;
1240 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001241 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001242 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001243
1244 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1245 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1246 chip->read_buf(mtd, p, eccsize);
1247 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1248 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001249 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001250
1251 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001252 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001253
1254 eccsteps = chip->ecc.steps;
1255 p = buf;
1256
1257 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1258 int stat;
1259
1260 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001261 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001262 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001263 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001264 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001265 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1266 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001267 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001268 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001269}
1270
1271/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001272 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001273 * @mtd: mtd info structure
1274 * @chip: nand chip info structure
1275 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001276 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001277 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001278 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001279 * Hardware ECC for large page chips, require OOB to be read first. For this
1280 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1281 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1282 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1283 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001284 */
1285static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001286 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001287{
1288 int i, eccsize = chip->ecc.size;
1289 int eccbytes = chip->ecc.bytes;
1290 int eccsteps = chip->ecc.steps;
1291 uint8_t *p = buf;
1292 uint8_t *ecc_code = chip->buffers->ecccode;
1293 uint32_t *eccpos = chip->ecc.layout->eccpos;
1294 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001295 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001296
1297 /* Read the OOB area first */
1298 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1299 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1300 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1301
1302 for (i = 0; i < chip->ecc.total; i++)
1303 ecc_code[i] = chip->oob_poi[eccpos[i]];
1304
1305 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1306 int stat;
1307
1308 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1309 chip->read_buf(mtd, p, eccsize);
1310 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1311
1312 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001313 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001314 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001315 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001316 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001317 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1318 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001319 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001320 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001321}
1322
1323/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001324 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001325 * @mtd: mtd info structure
1326 * @chip: nand chip info structure
1327 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001328 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001329 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001330 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001331 * The hw generator calculates the error syndrome automatically. Therefore we
1332 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001333 */
1334static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001335 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001336{
1337 int i, eccsize = chip->ecc.size;
1338 int eccbytes = chip->ecc.bytes;
1339 int eccsteps = chip->ecc.steps;
1340 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001341 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001342 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001343
1344 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1345 int stat;
1346
1347 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1348 chip->read_buf(mtd, p, eccsize);
1349
1350 if (chip->ecc.prepad) {
1351 chip->read_buf(mtd, oob, chip->ecc.prepad);
1352 oob += chip->ecc.prepad;
1353 }
1354
1355 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1356 chip->read_buf(mtd, oob, eccbytes);
1357 stat = chip->ecc.correct(mtd, p, oob, NULL);
1358
Mike Dunn3f91e942012-04-25 12:06:09 -07001359 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001360 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001361 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001362 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001363 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1364 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001365
1366 oob += eccbytes;
1367
1368 if (chip->ecc.postpad) {
1369 chip->read_buf(mtd, oob, chip->ecc.postpad);
1370 oob += chip->ecc.postpad;
1371 }
1372 }
1373
1374 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001375 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001376 if (i)
1377 chip->read_buf(mtd, oob, i);
1378
Mike Dunn3f91e942012-04-25 12:06:09 -07001379 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001380}
1381
1382/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001383 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001384 * @chip: nand chip structure
1385 * @oob: oob destination address
1386 * @ops: oob ops structure
1387 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001388 */
1389static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001390 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001391{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001392 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001393
Brian Norris0612b9d2011-08-30 18:45:40 -07001394 case MTD_OPS_PLACE_OOB:
1395 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001396 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1397 return oob + len;
1398
Brian Norris0612b9d2011-08-30 18:45:40 -07001399 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001400 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001401 uint32_t boffs = 0, roffs = ops->ooboffs;
1402 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001403
Florian Fainellif8ac0412010-09-07 13:23:43 +02001404 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001405 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001406 if (unlikely(roffs)) {
1407 if (roffs >= free->length) {
1408 roffs -= free->length;
1409 continue;
1410 }
1411 boffs = free->offset + roffs;
1412 bytes = min_t(size_t, len,
1413 (free->length - roffs));
1414 roffs = 0;
1415 } else {
1416 bytes = min_t(size_t, len, free->length);
1417 boffs = free->offset;
1418 }
1419 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001420 oob += bytes;
1421 }
1422 return oob;
1423 }
1424 default:
1425 BUG();
1426 }
1427 return NULL;
1428}
1429
1430/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001431 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001432 * @mtd: MTD device structure
1433 * @from: offset to read from
1434 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001435 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001436 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001437 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001438static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1439 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001440{
Brian Norrise47f3db2012-05-02 10:14:56 -07001441 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001442 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001443 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001445 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001446 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001447 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001448 mtd->oobavail : mtd->oobsize;
1449
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001451 unsigned int max_bitflips = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001453 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001455 chipnr = (int)(from >> chip->chip_shift);
1456 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001458 realpage = (int)(from >> chip->page_shift);
1459 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001461 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001463 buf = ops->datbuf;
1464 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001465 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001466
Florian Fainellif8ac0412010-09-07 13:23:43 +02001467 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001468 bytes = min(mtd->writesize - col, readlen);
1469 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001470
Brian Norris8b6e50c2011-05-25 14:59:01 -07001471 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001472 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001473 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Brian Norrisc00a0992012-05-01 17:12:54 -07001475 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Mike Dunnedbc45402012-04-25 12:06:11 -07001477 /*
1478 * Now read the page into the buffer. Absent an error,
1479 * the read methods return max bitflips per ecc step.
1480 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001481 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001482 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001483 oob_required,
1484 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001485 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1486 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001487 ret = chip->ecc.read_subpage(mtd, chip,
1488 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001489 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001490 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001491 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001492 if (ret < 0) {
1493 if (!aligned)
1494 /* Invalidate page cache */
1495 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001496 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001497 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001498
Mike Dunnedbc45402012-04-25 12:06:11 -07001499 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1500
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001501 /* Transfer not aligned data */
1502 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001503 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001504 !(mtd->ecc_stats.failed - stats.failed) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001505 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001506 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001507 chip->pagebuf_bitflips = ret;
1508 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001509 /* Invalidate page cache */
1510 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001511 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001512 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001514
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001515 buf += bytes;
1516
1517 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001518 int toread = min(oobreadlen, max_oobsize);
1519
1520 if (toread) {
1521 oob = nand_transfer_oob(chip,
1522 oob, ops, toread);
1523 oobreadlen -= toread;
1524 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001525 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001526
1527 if (chip->options & NAND_NEED_READRDY) {
1528 /* Apply delay or wait for ready/busy pin */
1529 if (!chip->dev_ready)
1530 udelay(chip->chip_delay);
1531 else
1532 nand_wait_ready(mtd);
1533 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001534 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001535 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001536 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001537 max_bitflips = max_t(unsigned int, max_bitflips,
1538 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001541 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001542
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001543 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Brian Norris8b6e50c2011-05-25 14:59:01 -07001546 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 col = 0;
1548 /* Increment page address */
1549 realpage++;
1550
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001551 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* Check, if we cross a chip boundary */
1553 if (!page) {
1554 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001555 chip->select_chip(mtd, -1);
1556 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001559 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001561 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001562 if (oob)
1563 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Mike Dunn3f91e942012-04-25 12:06:09 -07001565 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001566 return ret;
1567
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001568 if (mtd->ecc_stats.failed - stats.failed)
1569 return -EBADMSG;
1570
Mike Dunnedbc45402012-04-25 12:06:11 -07001571 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001572}
1573
1574/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001575 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001576 * @mtd: MTD device structure
1577 * @from: offset to read from
1578 * @len: number of bytes to read
1579 * @retlen: pointer to variable to store the number of read bytes
1580 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001581 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001582 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001583 */
1584static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1585 size_t *retlen, uint8_t *buf)
1586{
Brian Norris4a89ff82011-08-30 18:45:45 -07001587 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001588 int ret;
1589
Huang Shijie6a8214a2012-11-19 14:43:30 +08001590 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001591 ops.len = len;
1592 ops.datbuf = buf;
1593 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001594 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001595 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001596 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001597 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001602 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001603 * @mtd: mtd info structure
1604 * @chip: nand chip info structure
1605 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001606 */
1607static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001608 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001609{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001610 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001611 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001612 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001613}
1614
1615/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001616 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001617 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001618 * @mtd: mtd info structure
1619 * @chip: nand chip info structure
1620 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001621 */
1622static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001623 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001624{
1625 uint8_t *buf = chip->oob_poi;
1626 int length = mtd->oobsize;
1627 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1628 int eccsize = chip->ecc.size;
1629 uint8_t *bufpoi = buf;
1630 int i, toread, sndrnd = 0, pos;
1631
1632 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1633 for (i = 0; i < chip->ecc.steps; i++) {
1634 if (sndrnd) {
1635 pos = eccsize + i * (eccsize + chunk);
1636 if (mtd->writesize > 512)
1637 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1638 else
1639 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1640 } else
1641 sndrnd = 1;
1642 toread = min_t(int, length, chunk);
1643 chip->read_buf(mtd, bufpoi, toread);
1644 bufpoi += toread;
1645 length -= toread;
1646 }
1647 if (length > 0)
1648 chip->read_buf(mtd, bufpoi, length);
1649
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001650 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001651}
1652
1653/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001654 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001655 * @mtd: mtd info structure
1656 * @chip: nand chip info structure
1657 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001658 */
1659static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1660 int page)
1661{
1662 int status = 0;
1663 const uint8_t *buf = chip->oob_poi;
1664 int length = mtd->oobsize;
1665
1666 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1667 chip->write_buf(mtd, buf, length);
1668 /* Send command to program the OOB data */
1669 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1670
1671 status = chip->waitfunc(mtd, chip);
1672
Savin Zlobec0d420f92006-06-21 11:51:20 +02001673 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001674}
1675
1676/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001677 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001678 * with syndrome - only for large page flash
1679 * @mtd: mtd info structure
1680 * @chip: nand chip info structure
1681 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001682 */
1683static int nand_write_oob_syndrome(struct mtd_info *mtd,
1684 struct nand_chip *chip, int page)
1685{
1686 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1687 int eccsize = chip->ecc.size, length = mtd->oobsize;
1688 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1689 const uint8_t *bufpoi = chip->oob_poi;
1690
1691 /*
1692 * data-ecc-data-ecc ... ecc-oob
1693 * or
1694 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1695 */
1696 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1697 pos = steps * (eccsize + chunk);
1698 steps = 0;
1699 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001700 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001701
1702 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1703 for (i = 0; i < steps; i++) {
1704 if (sndcmd) {
1705 if (mtd->writesize <= 512) {
1706 uint32_t fill = 0xFFFFFFFF;
1707
1708 len = eccsize;
1709 while (len > 0) {
1710 int num = min_t(int, len, 4);
1711 chip->write_buf(mtd, (uint8_t *)&fill,
1712 num);
1713 len -= num;
1714 }
1715 } else {
1716 pos = eccsize + i * (eccsize + chunk);
1717 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1718 }
1719 } else
1720 sndcmd = 1;
1721 len = min_t(int, length, chunk);
1722 chip->write_buf(mtd, bufpoi, len);
1723 bufpoi += len;
1724 length -= len;
1725 }
1726 if (length > 0)
1727 chip->write_buf(mtd, bufpoi, length);
1728
1729 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1730 status = chip->waitfunc(mtd, chip);
1731
1732 return status & NAND_STATUS_FAIL ? -EIO : 0;
1733}
1734
1735/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001736 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001737 * @mtd: MTD device structure
1738 * @from: offset to read from
1739 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001741 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001743static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1744 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Brian Norrisc00a0992012-05-01 17:12:54 -07001746 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001747 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001748 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001749 int readlen = ops->ooblen;
1750 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001751 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001752 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Brian Norris289c0522011-07-19 10:06:09 -07001754 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301755 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Brian Norris041e4572011-06-23 16:45:24 -07001757 stats = mtd->ecc_stats;
1758
Brian Norris0612b9d2011-08-30 18:45:40 -07001759 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001760 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001761 else
1762 len = mtd->oobsize;
1763
1764 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001765 pr_debug("%s: attempt to start read outside oob\n",
1766 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001767 return -EINVAL;
1768 }
1769
1770 /* Do not allow reads past end of device */
1771 if (unlikely(from >= mtd->size ||
1772 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1773 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001774 pr_debug("%s: attempt to read beyond end of device\n",
1775 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001776 return -EINVAL;
1777 }
Vitaly Wool70145682006-11-03 18:20:38 +03001778
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001779 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001780 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001782 /* Shift to get page */
1783 realpage = (int)(from >> chip->page_shift);
1784 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Florian Fainellif8ac0412010-09-07 13:23:43 +02001786 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001787 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001788 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001789 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001790 ret = chip->ecc.read_oob(mtd, chip, page);
1791
1792 if (ret < 0)
1793 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001794
1795 len = min(len, readlen);
1796 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001797
Brian Norris5bc7c332013-03-13 09:51:31 -07001798 if (chip->options & NAND_NEED_READRDY) {
1799 /* Apply delay or wait for ready/busy pin */
1800 if (!chip->dev_ready)
1801 udelay(chip->chip_delay);
1802 else
1803 nand_wait_ready(mtd);
1804 }
1805
Vitaly Wool70145682006-11-03 18:20:38 +03001806 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001807 if (!readlen)
1808 break;
1809
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001810 /* Increment page address */
1811 realpage++;
1812
1813 page = realpage & chip->pagemask;
1814 /* Check, if we cross a chip boundary */
1815 if (!page) {
1816 chipnr++;
1817 chip->select_chip(mtd, -1);
1818 chip->select_chip(mtd, chipnr);
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001821 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001823 ops->oobretlen = ops->ooblen - readlen;
1824
1825 if (ret < 0)
1826 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001827
1828 if (mtd->ecc_stats.failed - stats.failed)
1829 return -EBADMSG;
1830
1831 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
1834/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001835 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001836 * @mtd: MTD device structure
1837 * @from: offset to read from
1838 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001840 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001842static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1843 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001845 int ret = -ENOTSUPP;
1846
1847 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001850 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001851 pr_debug("%s: attempt to read beyond end of device\n",
1852 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return -EINVAL;
1854 }
1855
Huang Shijie6a8214a2012-11-19 14:43:30 +08001856 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Florian Fainellif8ac0412010-09-07 13:23:43 +02001858 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001859 case MTD_OPS_PLACE_OOB:
1860 case MTD_OPS_AUTO_OOB:
1861 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001862 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001863
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001864 default:
1865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 if (!ops->datbuf)
1869 ret = nand_do_read_oob(mtd, from, ops);
1870 else
1871 ret = nand_do_read_ops(mtd, from, ops);
1872
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001873out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875 return ret;
1876}
1877
1878
1879/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001880 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001881 * @mtd: mtd info structure
1882 * @chip: nand chip info structure
1883 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001884 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001885 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001886 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001887 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001888static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001889 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001890{
1891 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001892 if (oob_required)
1893 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001894
1895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001898/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001899 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001900 * @mtd: mtd info structure
1901 * @chip: nand chip info structure
1902 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001903 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001904 *
1905 * We need a special oob layout and handling even when ECC isn't checked.
1906 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001907static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001908 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001909 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001910{
1911 int eccsize = chip->ecc.size;
1912 int eccbytes = chip->ecc.bytes;
1913 uint8_t *oob = chip->oob_poi;
1914 int steps, size;
1915
1916 for (steps = chip->ecc.steps; steps > 0; steps--) {
1917 chip->write_buf(mtd, buf, eccsize);
1918 buf += eccsize;
1919
1920 if (chip->ecc.prepad) {
1921 chip->write_buf(mtd, oob, chip->ecc.prepad);
1922 oob += chip->ecc.prepad;
1923 }
1924
1925 chip->read_buf(mtd, oob, eccbytes);
1926 oob += eccbytes;
1927
1928 if (chip->ecc.postpad) {
1929 chip->write_buf(mtd, oob, chip->ecc.postpad);
1930 oob += chip->ecc.postpad;
1931 }
1932 }
1933
1934 size = mtd->oobsize - (oob - chip->oob_poi);
1935 if (size)
1936 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001937
1938 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001939}
1940/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001941 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001942 * @mtd: mtd info structure
1943 * @chip: nand chip info structure
1944 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001945 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001946 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001947static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001948 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001949{
1950 int i, eccsize = chip->ecc.size;
1951 int eccbytes = chip->ecc.bytes;
1952 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001953 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001954 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001955 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001956
Brian Norris7854d3f2011-06-23 14:12:08 -07001957 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001958 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1959 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001960
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001961 for (i = 0; i < chip->ecc.total; i++)
1962 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001963
Josh Wufdbad98d2012-06-25 18:07:45 +08001964 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001965}
1966
1967/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001968 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001969 * @mtd: mtd info structure
1970 * @chip: nand chip info structure
1971 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001972 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001973 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001974static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001975 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001976{
1977 int i, eccsize = chip->ecc.size;
1978 int eccbytes = chip->ecc.bytes;
1979 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001980 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001981 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001982 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001983
1984 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1985 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001986 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001987 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1988 }
1989
1990 for (i = 0; i < chip->ecc.total; i++)
1991 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1992
1993 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001994
1995 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001996}
1997
1998/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001999 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002000 * @mtd: mtd info structure
2001 * @chip: nand chip info structure
2002 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002003 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002004 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002005 * The hw generator calculates the error syndrome automatically. Therefore we
2006 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002007 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002008static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002009 struct nand_chip *chip,
2010 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002011{
2012 int i, eccsize = chip->ecc.size;
2013 int eccbytes = chip->ecc.bytes;
2014 int eccsteps = chip->ecc.steps;
2015 const uint8_t *p = buf;
2016 uint8_t *oob = chip->oob_poi;
2017
2018 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2019
2020 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2021 chip->write_buf(mtd, p, eccsize);
2022
2023 if (chip->ecc.prepad) {
2024 chip->write_buf(mtd, oob, chip->ecc.prepad);
2025 oob += chip->ecc.prepad;
2026 }
2027
2028 chip->ecc.calculate(mtd, p, oob);
2029 chip->write_buf(mtd, oob, eccbytes);
2030 oob += eccbytes;
2031
2032 if (chip->ecc.postpad) {
2033 chip->write_buf(mtd, oob, chip->ecc.postpad);
2034 oob += chip->ecc.postpad;
2035 }
2036 }
2037
2038 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002039 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002040 if (i)
2041 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002042
2043 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002044}
2045
2046/**
David Woodhouse956e9442006-09-25 17:12:39 +01002047 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002048 * @mtd: MTD device structure
2049 * @chip: NAND chip descriptor
2050 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002051 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002052 * @page: page number to write
2053 * @cached: cached programming
2054 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002055 */
2056static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002057 const uint8_t *buf, int oob_required, int page,
2058 int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002059{
2060 int status;
2061
2062 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2063
David Woodhouse956e9442006-09-25 17:12:39 +01002064 if (unlikely(raw))
Josh Wufdbad98d2012-06-25 18:07:45 +08002065 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002066 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002067 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2068
2069 if (status < 0)
2070 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002071
2072 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002073 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002074 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002075 */
2076 cached = 0;
2077
2078 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2079
2080 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002081 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002082 /*
2083 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002084 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002085 */
2086 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2087 status = chip->errstat(mtd, chip, FL_WRITING, status,
2088 page);
2089
2090 if (status & NAND_STATUS_FAIL)
2091 return -EIO;
2092 } else {
2093 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002094 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002095 }
2096
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002097 return 0;
2098}
2099
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002100/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002101 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002102 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002103 * @oob: oob data buffer
2104 * @len: oob data write length
2105 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002106 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002107static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2108 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002109{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002110 struct nand_chip *chip = mtd->priv;
2111
2112 /*
2113 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2114 * data from a previous OOB read.
2115 */
2116 memset(chip->oob_poi, 0xff, mtd->oobsize);
2117
Florian Fainellif8ac0412010-09-07 13:23:43 +02002118 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002119
Brian Norris0612b9d2011-08-30 18:45:40 -07002120 case MTD_OPS_PLACE_OOB:
2121 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002122 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2123 return oob + len;
2124
Brian Norris0612b9d2011-08-30 18:45:40 -07002125 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002126 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002127 uint32_t boffs = 0, woffs = ops->ooboffs;
2128 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002129
Florian Fainellif8ac0412010-09-07 13:23:43 +02002130 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002131 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002132 if (unlikely(woffs)) {
2133 if (woffs >= free->length) {
2134 woffs -= free->length;
2135 continue;
2136 }
2137 boffs = free->offset + woffs;
2138 bytes = min_t(size_t, len,
2139 (free->length - woffs));
2140 woffs = 0;
2141 } else {
2142 bytes = min_t(size_t, len, free->length);
2143 boffs = free->offset;
2144 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002145 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002146 oob += bytes;
2147 }
2148 return oob;
2149 }
2150 default:
2151 BUG();
2152 }
2153 return NULL;
2154}
2155
Florian Fainellif8ac0412010-09-07 13:23:43 +02002156#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002157
2158/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002159 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002160 * @mtd: MTD device structure
2161 * @to: offset to write to
2162 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002163 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002164 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002165 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002166static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2167 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002168{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002169 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002170 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002171 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002172
2173 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002174 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002175 mtd->oobavail : mtd->oobsize;
2176
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002177 uint8_t *oob = ops->oobbuf;
2178 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002179 int ret, subpage;
Brian Norrise47f3db2012-05-02 10:14:56 -07002180 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002181
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002182 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002183 if (!writelen)
2184 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002185
Brian Norris8b6e50c2011-05-25 14:59:01 -07002186 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002187 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002188 pr_notice("%s: attempt to write non page aligned data\n",
2189 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002190 return -EINVAL;
2191 }
2192
Thomas Gleixner29072b92006-09-28 15:38:36 +02002193 column = to & (mtd->writesize - 1);
2194 subpage = column || (writelen & (mtd->writesize - 1));
2195
2196 if (subpage && oob)
2197 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002198
Thomas Gleixner6a930962006-06-28 00:11:45 +02002199 chipnr = (int)(to >> chip->chip_shift);
2200 chip->select_chip(mtd, chipnr);
2201
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002202 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002203 if (nand_check_wp(mtd)) {
2204 ret = -EIO;
2205 goto err_out;
2206 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002207
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002208 realpage = (int)(to >> chip->page_shift);
2209 page = realpage & chip->pagemask;
2210 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2211
2212 /* Invalidate the page cache, when we write to the cached page */
2213 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002214 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002215 chip->pagebuf = -1;
2216
Maxim Levitsky782ce792010-02-22 20:39:36 +02002217 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002218 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2219 ret = -EINVAL;
2220 goto err_out;
2221 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002222
Florian Fainellif8ac0412010-09-07 13:23:43 +02002223 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002224 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002225 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002226 uint8_t *wbuf = buf;
2227
Brian Norris8b6e50c2011-05-25 14:59:01 -07002228 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002229 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2230 cached = 0;
2231 bytes = min_t(int, bytes - column, (int) writelen);
2232 chip->pagebuf = -1;
2233 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2234 memcpy(&chip->buffers->databuf[column], buf, bytes);
2235 wbuf = chip->buffers->databuf;
2236 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237
Maxim Levitsky782ce792010-02-22 20:39:36 +02002238 if (unlikely(oob)) {
2239 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002240 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002241 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002242 } else {
2243 /* We still need to erase leftover OOB data */
2244 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002245 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002246
Brian Norrise47f3db2012-05-02 10:14:56 -07002247 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2248 cached, (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249 if (ret)
2250 break;
2251
2252 writelen -= bytes;
2253 if (!writelen)
2254 break;
2255
Thomas Gleixner29072b92006-09-28 15:38:36 +02002256 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002257 buf += bytes;
2258 realpage++;
2259
2260 page = realpage & chip->pagemask;
2261 /* Check, if we cross a chip boundary */
2262 if (!page) {
2263 chipnr++;
2264 chip->select_chip(mtd, -1);
2265 chip->select_chip(mtd, chipnr);
2266 }
2267 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002268
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002269 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002270 if (unlikely(oob))
2271 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002272
2273err_out:
2274 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002275 return ret;
2276}
2277
2278/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002279 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002280 * @mtd: MTD device structure
2281 * @to: offset to write to
2282 * @len: number of bytes to write
2283 * @retlen: pointer to variable to store the number of written bytes
2284 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002285 *
2286 * NAND write with ECC. Used when performing writes in interrupt context, this
2287 * may for example be called by mtdoops when writing an oops while in panic.
2288 */
2289static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2290 size_t *retlen, const uint8_t *buf)
2291{
2292 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002293 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002294 int ret;
2295
Brian Norris8b6e50c2011-05-25 14:59:01 -07002296 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002297 panic_nand_wait(mtd, chip, 400);
2298
Brian Norris8b6e50c2011-05-25 14:59:01 -07002299 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002300 panic_nand_get_device(chip, mtd, FL_WRITING);
2301
Brian Norris4a89ff82011-08-30 18:45:45 -07002302 ops.len = len;
2303 ops.datbuf = (uint8_t *)buf;
2304 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002305 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002306
Brian Norris4a89ff82011-08-30 18:45:45 -07002307 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002308
Brian Norris4a89ff82011-08-30 18:45:45 -07002309 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002310 return ret;
2311}
2312
2313/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002314 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002315 * @mtd: MTD device structure
2316 * @to: offset to write to
2317 * @len: number of bytes to write
2318 * @retlen: pointer to variable to store the number of written bytes
2319 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002321 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002323static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002324 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Brian Norris4a89ff82011-08-30 18:45:45 -07002326 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002327 int ret;
2328
Huang Shijie6a8214a2012-11-19 14:43:30 +08002329 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002330 ops.len = len;
2331 ops.datbuf = (uint8_t *)buf;
2332 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002333 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002334 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002335 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002336 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002337 return ret;
2338}
2339
2340/**
2341 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002342 * @mtd: MTD device structure
2343 * @to: offset to write to
2344 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002345 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002346 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002347 */
2348static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2349 struct mtd_oob_ops *ops)
2350{
Adrian Hunter03736152007-01-31 17:58:29 +02002351 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002352 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Brian Norris289c0522011-07-19 10:06:09 -07002354 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302355 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Brian Norris0612b9d2011-08-30 18:45:40 -07002357 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002358 len = chip->ecc.layout->oobavail;
2359 else
2360 len = mtd->oobsize;
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002363 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002364 pr_debug("%s: attempt to write past end of page\n",
2365 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return -EINVAL;
2367 }
2368
Adrian Hunter03736152007-01-31 17:58:29 +02002369 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002370 pr_debug("%s: attempt to start write outside oob\n",
2371 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002372 return -EINVAL;
2373 }
2374
Jason Liu775adc32011-02-25 13:06:18 +08002375 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002376 if (unlikely(to >= mtd->size ||
2377 ops->ooboffs + ops->ooblen >
2378 ((mtd->size >> chip->page_shift) -
2379 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002380 pr_debug("%s: attempt to write beyond end of device\n",
2381 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002382 return -EINVAL;
2383 }
2384
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002385 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002386 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002388 /* Shift to get page */
2389 page = (int)(to >> chip->page_shift);
2390
2391 /*
2392 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2393 * of my DiskOnChip 2000 test units) will clear the whole data page too
2394 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2395 * it in the doc2000 driver in August 1999. dwmw2.
2396 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002397 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002400 if (nand_check_wp(mtd)) {
2401 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002402 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002403 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002406 if (page == chip->pagebuf)
2407 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002409 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002410
Brian Norris0612b9d2011-08-30 18:45:40 -07002411 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002412 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2413 else
2414 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002415
Huang Shijieb0bb6902012-11-19 14:43:29 +08002416 chip->select_chip(mtd, -1);
2417
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002418 if (status)
2419 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Vitaly Wool70145682006-11-03 18:20:38 +03002421 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002423 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002424}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002426/**
2427 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002428 * @mtd: MTD device structure
2429 * @to: offset to write to
2430 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002431 */
2432static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2433 struct mtd_oob_ops *ops)
2434{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002435 int ret = -ENOTSUPP;
2436
2437 ops->retlen = 0;
2438
2439 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002440 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002441 pr_debug("%s: attempt to write beyond end of device\n",
2442 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002443 return -EINVAL;
2444 }
2445
Huang Shijie6a8214a2012-11-19 14:43:30 +08002446 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002447
Florian Fainellif8ac0412010-09-07 13:23:43 +02002448 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002449 case MTD_OPS_PLACE_OOB:
2450 case MTD_OPS_AUTO_OOB:
2451 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002452 break;
2453
2454 default:
2455 goto out;
2456 }
2457
2458 if (!ops->datbuf)
2459 ret = nand_do_write_oob(mtd, to, ops);
2460 else
2461 ret = nand_do_write_ops(mtd, to, ops);
2462
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002463out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002464 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return ret;
2466}
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002469 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002470 * @mtd: MTD device structure
2471 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002473 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002475static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002477 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002479 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2480 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
2483/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002484 * multi_erase_cmd - [GENERIC] AND specific block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002485 * @mtd: MTD device structure
2486 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002488 * AND multi block erase command function. Erase 4 consecutive blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002490static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002492 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002494 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2495 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2496 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2497 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2498 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
2501/**
2502 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002503 * @mtd: MTD device structure
2504 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002506 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002508static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
David Woodhousee0c7d762006-05-13 18:07:53 +01002510 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002512
David A. Marlin30f464b2005-01-17 18:35:25 +00002513#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002515 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002516 * @mtd: MTD device structure
2517 * @instr: erase instruction
2518 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002520 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002522int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2523 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Adrian Hunter69423d92008-12-10 13:37:21 +00002525 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002526 struct nand_chip *chip = mtd->priv;
Florian Fainellif8ac0412010-09-07 13:23:43 +02002527 loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002529 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Brian Norris289c0522011-07-19 10:06:09 -07002531 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2532 __func__, (unsigned long long)instr->addr,
2533 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302535 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002539 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002542 page = (int)(instr->addr >> chip->page_shift);
2543 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002546 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002549 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 /* Check, if it is write protected */
2552 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002553 pr_debug("%s: device is write protected!\n",
2554 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 instr->state = MTD_ERASE_FAILED;
2556 goto erase_exit;
2557 }
2558
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002559 /*
2560 * If BBT requires refresh, set the BBT page mask to see if the BBT
2561 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2562 * can not be matched. This is also done when the bbt is actually
Brian Norris7854d3f2011-06-23 14:12:08 -07002563 * erased to avoid recursive updates.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002564 */
2565 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2566 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /* Loop through the pages */
2569 len = instr->len;
2570
2571 instr->state = MTD_ERASING;
2572
2573 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002574 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002575 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2576 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002577 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2578 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 instr->state = MTD_ERASE_FAILED;
2580 goto erase_exit;
2581 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002582
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002583 /*
2584 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002585 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002586 */
2587 if (page <= chip->pagebuf && chip->pagebuf <
2588 (page + pages_per_block))
2589 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002591 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002592
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002593 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002595 /*
2596 * See if operation failed and additional status checks are
2597 * available
2598 */
2599 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2600 status = chip->errstat(mtd, chip, FL_ERASING,
2601 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002604 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002605 pr_debug("%s: failed erase, page 0x%08x\n",
2606 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002608 instr->fail_addr =
2609 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 goto erase_exit;
2611 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002612
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002613 /*
2614 * If BBT requires refresh, set the BBT rewrite flag to the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002615 * page being erased.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002616 */
2617 if (bbt_masked_page != 0xffffffff &&
2618 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002619 rewrite_bbt[chipnr] =
2620 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002623 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 page += pages_per_block;
2625
2626 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002627 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002629 chip->select_chip(mtd, -1);
2630 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002631
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002632 /*
2633 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Brian Norris8b6e50c2011-05-25 14:59:01 -07002634 * page mask to see if this BBT should be rewritten.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002635 */
2636 if (bbt_masked_page != 0xffffffff &&
2637 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2638 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2639 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641 }
2642 instr->state = MTD_ERASE_DONE;
2643
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002644erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002649 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 nand_release_device(mtd);
2651
David Woodhouse49defc02007-10-06 15:01:59 -04002652 /* Do call back function */
2653 if (!ret)
2654 mtd_erase_callback(instr);
2655
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002656 /*
2657 * If BBT requires refresh and erase was successful, rewrite any
Brian Norris8b6e50c2011-05-25 14:59:01 -07002658 * selected bad block tables.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002659 */
2660 if (bbt_masked_page == 0xffffffff || ret)
2661 return ret;
2662
2663 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2664 if (!rewrite_bbt[chipnr])
2665 continue;
Brian Norris8b6e50c2011-05-25 14:59:01 -07002666 /* Update the BBT for chip */
Brian Norris289c0522011-07-19 10:06:09 -07002667 pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2668 __func__, chipnr, rewrite_bbt[chipnr],
2669 chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002670 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002671 }
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /* Return more or less happy */
2674 return ret;
2675}
2676
2677/**
2678 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002679 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002681 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002683static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Brian Norris289c0522011-07-19 10:06:09 -07002685 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002688 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002690 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002694 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002695 * @mtd: MTD device structure
2696 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002698static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002700 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
2703/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002704 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002705 * @mtd: MTD device structure
2706 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002708static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002710 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 int ret;
2712
Florian Fainellif8ac0412010-09-07 13:23:43 +02002713 ret = nand_block_isbad(mtd, ofs);
2714 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002715 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (ret > 0)
2717 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002718 return ret;
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002721 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
2724/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002725 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2726 * @mtd: MTD device structure
2727 * @chip: nand chip info structure
2728 * @addr: feature address.
2729 * @subfeature_param: the subfeature parameters, a four bytes array.
2730 */
2731static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2732 int addr, uint8_t *subfeature_param)
2733{
2734 int status;
2735
2736 if (!chip->onfi_version)
2737 return -EINVAL;
2738
2739 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2740 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2741 status = chip->waitfunc(mtd, chip);
2742 if (status & NAND_STATUS_FAIL)
2743 return -EIO;
2744 return 0;
2745}
2746
2747/**
2748 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2749 * @mtd: MTD device structure
2750 * @chip: nand chip info structure
2751 * @addr: feature address.
2752 * @subfeature_param: the subfeature parameters, a four bytes array.
2753 */
2754static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2755 int addr, uint8_t *subfeature_param)
2756{
2757 if (!chip->onfi_version)
2758 return -EINVAL;
2759
2760 /* clear the sub feature parameters */
2761 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2762
2763 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2764 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2765 return 0;
2766}
2767
2768/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002769 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002770 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002771 */
2772static int nand_suspend(struct mtd_info *mtd)
2773{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002774 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002775}
2776
2777/**
2778 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002779 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002780 */
2781static void nand_resume(struct mtd_info *mtd)
2782{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002784
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002785 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002786 nand_release_device(mtd);
2787 else
Brian Norrisd0370212011-07-19 10:06:08 -07002788 pr_err("%s called for a chip which is not in suspended state\n",
2789 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002790}
2791
Brian Norris8b6e50c2011-05-25 14:59:01 -07002792/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002793static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002794{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002796 if (!chip->chip_delay)
2797 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002800 if (chip->cmdfunc == NULL)
2801 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002804 if (chip->waitfunc == NULL)
2805 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002807 if (!chip->select_chip)
2808 chip->select_chip = nand_select_chip;
2809 if (!chip->read_byte)
2810 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2811 if (!chip->read_word)
2812 chip->read_word = nand_read_word;
2813 if (!chip->block_bad)
2814 chip->block_bad = nand_block_bad;
2815 if (!chip->block_markbad)
2816 chip->block_markbad = nand_default_block_markbad;
2817 if (!chip->write_buf)
2818 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2819 if (!chip->read_buf)
2820 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002821 if (!chip->scan_bbt)
2822 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002823
2824 if (!chip->controller) {
2825 chip->controller = &chip->hwcontrol;
2826 spin_lock_init(&chip->controller->lock);
2827 init_waitqueue_head(&chip->controller->wq);
2828 }
2829
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002830}
2831
Brian Norris8b6e50c2011-05-25 14:59:01 -07002832/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002833static void sanitize_string(uint8_t *s, size_t len)
2834{
2835 ssize_t i;
2836
Brian Norris8b6e50c2011-05-25 14:59:01 -07002837 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002838 s[len - 1] = 0;
2839
Brian Norris8b6e50c2011-05-25 14:59:01 -07002840 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002841 for (i = 0; i < len - 1; i++) {
2842 if (s[i] < ' ' || s[i] > 127)
2843 s[i] = '?';
2844 }
2845
Brian Norris8b6e50c2011-05-25 14:59:01 -07002846 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002847 strim(s);
2848}
2849
2850static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2851{
2852 int i;
2853 while (len--) {
2854 crc ^= *p++ << 8;
2855 for (i = 0; i < 8; i++)
2856 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2857 }
2858
2859 return crc;
2860}
2861
2862/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002863 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002864 */
2865static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002866 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002867{
2868 struct nand_onfi_params *p = &chip->onfi_params;
2869 int i;
2870 int val;
2871
Matthieu CASTET0ce82b72013-01-16 15:25:45 +01002872 /* ONFI need to be probed in 8 bits mode, and 16 bits should be selected with NAND_BUSWIDTH_AUTO */
2873 if (chip->options & NAND_BUSWIDTH_16) {
2874 pr_err("Trying ONFI probe in 16 bits mode, aborting !\n");
2875 return 0;
2876 }
Brian Norris7854d3f2011-06-23 14:12:08 -07002877 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002878 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2879 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2880 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2881 return 0;
2882
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002883 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2884 for (i = 0; i < 3; i++) {
2885 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2886 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2887 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002888 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002889 break;
2890 }
2891 }
2892
2893 if (i == 3)
2894 return 0;
2895
Brian Norris8b6e50c2011-05-25 14:59:01 -07002896 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002897 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002898 if (val & (1 << 5))
2899 chip->onfi_version = 23;
2900 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002901 chip->onfi_version = 22;
2902 else if (val & (1 << 3))
2903 chip->onfi_version = 21;
2904 else if (val & (1 << 2))
2905 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002906 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002907 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002908 else
2909 chip->onfi_version = 0;
2910
2911 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002912 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002913 return 0;
2914 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002915
2916 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2917 sanitize_string(p->model, sizeof(p->model));
2918 if (!mtd->name)
2919 mtd->name = p->model;
2920 mtd->writesize = le32_to_cpu(p->byte_per_page);
2921 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2922 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTET63795752012-03-19 15:35:25 +01002923 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2924 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002925 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002926 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002927 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002928
Huang Shijied42b5de2012-02-17 11:22:37 +08002929 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002930 return 1;
2931}
2932
2933/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07002934 * nand_id_has_period - Check if an ID string has a given wraparound period
2935 * @id_data: the ID string
2936 * @arrlen: the length of the @id_data array
2937 * @period: the period of repitition
2938 *
2939 * Check if an ID string is repeated within a given sequence of bytes at
2940 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08002941 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07002942 * if the repetition has a period of @period; otherwise, returns zero.
2943 */
2944static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2945{
2946 int i, j;
2947 for (i = 0; i < period; i++)
2948 for (j = i + period; j < arrlen; j += period)
2949 if (id_data[i] != id_data[j])
2950 return 0;
2951 return 1;
2952}
2953
2954/*
2955 * nand_id_len - Get the length of an ID string returned by CMD_READID
2956 * @id_data: the ID string
2957 * @arrlen: the length of the @id_data array
2958
2959 * Returns the length of the ID string, according to known wraparound/trailing
2960 * zero patterns. If no pattern exists, returns the length of the array.
2961 */
2962static int nand_id_len(u8 *id_data, int arrlen)
2963{
2964 int last_nonzero, period;
2965
2966 /* Find last non-zero byte */
2967 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2968 if (id_data[last_nonzero])
2969 break;
2970
2971 /* All zeros */
2972 if (last_nonzero < 0)
2973 return 0;
2974
2975 /* Calculate wraparound period */
2976 for (period = 1; period < arrlen; period++)
2977 if (nand_id_has_period(id_data, arrlen, period))
2978 break;
2979
2980 /* There's a repeated pattern */
2981 if (period < arrlen)
2982 return period;
2983
2984 /* There are trailing zeros */
2985 if (last_nonzero < arrlen - 1)
2986 return last_nonzero + 1;
2987
2988 /* No pattern detected */
2989 return arrlen;
2990}
2991
2992/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002993 * Many new NAND share similar device ID codes, which represent the size of the
2994 * chip. The rest of the parameters must be decoded according to generic or
2995 * manufacturer-specific "extended ID" decoding patterns.
2996 */
2997static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2998 u8 id_data[8], int *busw)
2999{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003000 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003001 /* The 3rd id byte holds MLC / multichip data */
3002 chip->cellinfo = id_data[2];
3003 /* The 4th id byte is the important one */
3004 extid = id_data[3];
3005
Brian Norrise3b88bd2012-09-24 20:40:52 -07003006 id_len = nand_id_len(id_data, 8);
3007
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003008 /*
3009 * Field definitions are in the following datasheets:
3010 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003011 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003012 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003013 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003014 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3015 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003016 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003017 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Brian Norris6924d992012-11-14 21:46:30 -08003018 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Brian Norrisaf451af2012-10-09 23:26:06 -07003019 id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003020 /* Calc pagesize */
3021 mtd->writesize = 2048 << (extid & 0x03);
3022 extid >>= 2;
3023 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003024 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003025 case 1:
3026 mtd->oobsize = 128;
3027 break;
3028 case 2:
3029 mtd->oobsize = 218;
3030 break;
3031 case 3:
3032 mtd->oobsize = 400;
3033 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003034 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003035 mtd->oobsize = 436;
3036 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003037 case 5:
3038 mtd->oobsize = 512;
3039 break;
3040 case 6:
3041 default: /* Other cases are "reserved" (unknown) */
3042 mtd->oobsize = 640;
3043 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003044 }
3045 extid >>= 2;
3046 /* Calc blocksize */
3047 mtd->erasesize = (128 * 1024) <<
3048 (((extid >> 1) & 0x04) | (extid & 0x03));
3049 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003050 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3051 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3052 unsigned int tmp;
3053
3054 /* Calc pagesize */
3055 mtd->writesize = 2048 << (extid & 0x03);
3056 extid >>= 2;
3057 /* Calc oobsize */
3058 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3059 case 0:
3060 mtd->oobsize = 128;
3061 break;
3062 case 1:
3063 mtd->oobsize = 224;
3064 break;
3065 case 2:
3066 mtd->oobsize = 448;
3067 break;
3068 case 3:
3069 mtd->oobsize = 64;
3070 break;
3071 case 4:
3072 mtd->oobsize = 32;
3073 break;
3074 case 5:
3075 mtd->oobsize = 16;
3076 break;
3077 default:
3078 mtd->oobsize = 640;
3079 break;
3080 }
3081 extid >>= 2;
3082 /* Calc blocksize */
3083 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3084 if (tmp < 0x03)
3085 mtd->erasesize = (128 * 1024) << tmp;
3086 else if (tmp == 0x03)
3087 mtd->erasesize = 768 * 1024;
3088 else
3089 mtd->erasesize = (64 * 1024) << tmp;
3090 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003091 } else {
3092 /* Calc pagesize */
3093 mtd->writesize = 1024 << (extid & 0x03);
3094 extid >>= 2;
3095 /* Calc oobsize */
3096 mtd->oobsize = (8 << (extid & 0x01)) *
3097 (mtd->writesize >> 9);
3098 extid >>= 2;
3099 /* Calc blocksize. Blocksize is multiples of 64KiB */
3100 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3101 extid >>= 2;
3102 /* Get buswidth information */
3103 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3104 }
3105}
3106
3107/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003108 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3109 * decodes a matching ID table entry and assigns the MTD size parameters for
3110 * the chip.
3111 */
3112static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3113 struct nand_flash_dev *type, u8 id_data[8],
3114 int *busw)
3115{
3116 int maf_id = id_data[0];
3117
3118 mtd->erasesize = type->erasesize;
3119 mtd->writesize = type->pagesize;
3120 mtd->oobsize = mtd->writesize / 32;
3121 *busw = type->options & NAND_BUSWIDTH_16;
3122
3123 /*
3124 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3125 * some Spansion chips have erasesize that conflicts with size
3126 * listed in nand_ids table.
3127 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3128 */
3129 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3130 && id_data[6] == 0x00 && id_data[7] == 0x00
3131 && mtd->writesize == 512) {
3132 mtd->erasesize = 128 * 1024;
3133 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3134 }
3135}
3136
3137/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003138 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3139 * heuristic patterns using various detected parameters (e.g., manufacturer,
3140 * page size, cell-type information).
3141 */
3142static void nand_decode_bbm_options(struct mtd_info *mtd,
3143 struct nand_chip *chip, u8 id_data[8])
3144{
3145 int maf_id = id_data[0];
3146
3147 /* Set the bad block position */
3148 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3149 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3150 else
3151 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3152
3153 /*
3154 * Bad block marker is stored in the last page of each block on Samsung
3155 * and Hynix MLC devices; stored in first two pages of each block on
3156 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3157 * AMD/Spansion, and Macronix. All others scan only the first page.
3158 */
3159 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3160 (maf_id == NAND_MFR_SAMSUNG ||
3161 maf_id == NAND_MFR_HYNIX))
3162 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3163 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3164 (maf_id == NAND_MFR_SAMSUNG ||
3165 maf_id == NAND_MFR_HYNIX ||
3166 maf_id == NAND_MFR_TOSHIBA ||
3167 maf_id == NAND_MFR_AMD ||
3168 maf_id == NAND_MFR_MACRONIX)) ||
3169 (mtd->writesize == 2048 &&
3170 maf_id == NAND_MFR_MICRON))
3171 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3172}
3173
3174/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003175 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003176 */
3177static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003178 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003179 int busw,
3180 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003181 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003182{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003183 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003184 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003187 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Karl Beldanef89a882008-09-15 14:37:29 +02003189 /*
3190 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003191 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003192 */
3193 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003196 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003199 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003200 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Brian Norris8b6e50c2011-05-25 14:59:01 -07003202 /*
3203 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003204 * interface concerns can cause random data which looks like a
3205 * possibly credible NAND flash to appear. If the two results do
3206 * not match, ignore the device completely.
3207 */
3208
3209 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3210
Brian Norris4aef9b72012-09-24 20:40:48 -07003211 /* Read entire ID string */
3212 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003213 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003214
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003215 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003216 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07003217 "%02x,%02x against %02x,%02x\n", __func__,
3218 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003219 return ERR_PTR(-ENODEV);
3220 }
3221
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003222 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003223 type = nand_flash_ids;
3224
3225 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003226 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02003227 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00003228
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003229 chip->onfi_version = 0;
3230 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003231 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003232 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003233 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003234 }
3235
David Woodhouse5e81e882010-02-26 18:32:56 +00003236 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003237 return ERR_PTR(-ENODEV);
3238
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003239 if (!mtd->name)
3240 mtd->name = type->name;
3241
Adrian Hunter69423d92008-12-10 13:37:21 +00003242 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003243
Huang Shijie12a40a52010-09-27 10:43:53 +08003244 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003245 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003246 busw = chip->init_size(mtd, chip, id_data);
3247 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003248 /* Decode parameters from extended ID */
3249 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003250 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003251 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003252 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003253 /* Get chip options */
3254 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003255
Brian Norris8b6e50c2011-05-25 14:59:01 -07003256 /*
3257 * Check if chip is not a Samsung device. Do not clear the
3258 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003259 */
3260 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3261 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3262ident_done:
3263
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003264 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003265 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003266 if (nand_manuf_ids[maf_idx].id == *maf_id)
3267 break;
3268 }
3269
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003270 if (chip->options & NAND_BUSWIDTH_AUTO) {
3271 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3272 chip->options |= busw;
3273 nand_set_defaults(chip, busw);
3274 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3275 /*
3276 * Check, if buswidth is correct. Hardware drivers should set
3277 * chip correct!
3278 */
Brian Norris9a4d4d62011-07-19 10:06:07 -07003279 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003280 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3281 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003282 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003283 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3284 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003285 return ERR_PTR(-EINVAL);
3286 }
3287
Brian Norris7e74c2d2012-09-24 20:40:49 -07003288 nand_decode_bbm_options(mtd, chip, id_data);
3289
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003290 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003291 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003292 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003293 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003294
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003295 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003296 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003297 if (chip->chipsize & 0xffffffff)
3298 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003299 else {
3300 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3301 chip->chip_shift += 32 - 1;
3302 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003303
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003304 chip->badblockbits = 8;
3305
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003306 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003307 if (chip->options & NAND_4PAGE_ARRAY)
3308 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003309 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003310 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003311
Brian Norris8b6e50c2011-05-25 14:59:01 -07003312 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003313 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3314 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003315
Huang Shijie886bd332012-04-09 11:41:37 +08003316 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003317 " %dMiB, page size: %d, OOB size: %d\n",
Huang Shijie886bd332012-04-09 11:41:37 +08003318 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3319 chip->onfi_version ? chip->onfi_params.model : type->name,
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003320 (int)(chip->chipsize >> 20), mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003321
3322 return type;
3323}
3324
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003325/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003326 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003327 * @mtd: MTD device structure
3328 * @maxchips: number of chips to scan for
3329 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003330 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003331 * This is the first phase of the normal nand_scan() function. It reads the
3332 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003333 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003334 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003335 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003336int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3337 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003338{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003339 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003340 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003341 struct nand_flash_dev *type;
3342
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003343 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003344 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003345 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003346 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003347
3348 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003349 type = nand_get_flash_type(mtd, chip, busw,
3350 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003351
3352 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003353 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003354 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003355 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003356 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
3358
Huang Shijie07300162012-11-09 16:23:45 +08003359 chip->select_chip(mtd, -1);
3360
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003361 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003362 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003363 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003364 /* See comment in nand_get_flash_type for reset */
3365 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003367 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003369 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003370 nand_dev_id != chip->read_byte(mtd)) {
3371 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 break;
Huang Shijie07300162012-11-09 16:23:45 +08003373 }
3374 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003377 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003380 chip->numchips = i;
3381 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
David Woodhouse3b85c322006-09-25 17:06:53 +01003383 return 0;
3384}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003385EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003386
3387
3388/**
3389 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003390 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003391 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003392 * This is the second phase of the normal nand_scan() function. It fills out
3393 * all the uninitialized function pointers with the defaults and scans for a
3394 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003395 */
3396int nand_scan_tail(struct mtd_info *mtd)
3397{
3398 int i;
3399 struct nand_chip *chip = mtd->priv;
3400
Brian Norrise2414f42012-02-06 13:44:00 -08003401 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3402 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3403 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3404
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003405 if (!(chip->options & NAND_OWN_BUFFERS))
3406 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3407 if (!chip->buffers)
3408 return -ENOMEM;
3409
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003410 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003411 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003412
3413 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003414 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003415 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003416 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003417 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003419 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 break;
3421 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003422 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 break;
3424 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003425 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003427 case 128:
3428 chip->ecc.layout = &nand_oob_128;
3429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003431 pr_warn("No oob scheme defined for oobsize %d\n",
3432 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 BUG();
3434 }
3435 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003436
David Woodhouse956e9442006-09-25 17:12:39 +01003437 if (!chip->write_page)
3438 chip->write_page = nand_write_page;
3439
Huang Shijie7db03ec2012-09-13 14:57:52 +08003440 /* set for ONFI nand */
3441 if (!chip->onfi_set_features)
3442 chip->onfi_set_features = nand_onfi_set_features;
3443 if (!chip->onfi_get_features)
3444 chip->onfi_get_features = nand_onfi_get_features;
3445
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003446 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003447 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003448 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003449 */
David Woodhouse956e9442006-09-25 17:12:39 +01003450
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003451 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003452 case NAND_ECC_HW_OOB_FIRST:
3453 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3454 if (!chip->ecc.calculate || !chip->ecc.correct ||
3455 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003456 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003457 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003458 BUG();
3459 }
3460 if (!chip->ecc.read_page)
3461 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3462
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003463 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003464 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003465 if (!chip->ecc.read_page)
3466 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003467 if (!chip->ecc.write_page)
3468 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003469 if (!chip->ecc.read_page_raw)
3470 chip->ecc.read_page_raw = nand_read_page_raw;
3471 if (!chip->ecc.write_page_raw)
3472 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003473 if (!chip->ecc.read_oob)
3474 chip->ecc.read_oob = nand_read_oob_std;
3475 if (!chip->ecc.write_oob)
3476 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003477
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003478 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003479 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3480 !chip->ecc.hwctl) &&
3481 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003482 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003483 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003484 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003485 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003486 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003487 BUG();
3488 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003489 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003490 if (!chip->ecc.read_page)
3491 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003492 if (!chip->ecc.write_page)
3493 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003494 if (!chip->ecc.read_page_raw)
3495 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3496 if (!chip->ecc.write_page_raw)
3497 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003498 if (!chip->ecc.read_oob)
3499 chip->ecc.read_oob = nand_read_oob_syndrome;
3500 if (!chip->ecc.write_oob)
3501 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003502
Mike Dunne2788c92012-04-25 12:06:10 -07003503 if (mtd->writesize >= chip->ecc.size) {
3504 if (!chip->ecc.strength) {
3505 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3506 BUG();
3507 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003508 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003509 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003510 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003511 "%d byte page size, fallback to SW ECC\n",
3512 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003513 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003515 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003516 chip->ecc.calculate = nand_calculate_ecc;
3517 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003518 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003519 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003520 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003521 chip->ecc.read_page_raw = nand_read_page_raw;
3522 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003523 chip->ecc.read_oob = nand_read_oob_std;
3524 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003525 if (!chip->ecc.size)
3526 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003527 chip->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003528 chip->ecc.strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003530
Ivan Djelic193bd402011-03-11 11:05:33 +01003531 case NAND_ECC_SOFT_BCH:
3532 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003533 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003534 BUG();
3535 }
3536 chip->ecc.calculate = nand_bch_calculate_ecc;
3537 chip->ecc.correct = nand_bch_correct_data;
3538 chip->ecc.read_page = nand_read_page_swecc;
3539 chip->ecc.read_subpage = nand_read_subpage;
3540 chip->ecc.write_page = nand_write_page_swecc;
3541 chip->ecc.read_page_raw = nand_read_page_raw;
3542 chip->ecc.write_page_raw = nand_write_page_raw;
3543 chip->ecc.read_oob = nand_read_oob_std;
3544 chip->ecc.write_oob = nand_write_oob_std;
3545 /*
3546 * Board driver should supply ecc.size and ecc.bytes values to
3547 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003548 * for details. Otherwise, default to 4 bits for large page
3549 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003550 */
3551 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3552 chip->ecc.size = 512;
3553 chip->ecc.bytes = 7;
3554 }
3555 chip->ecc.priv = nand_bch_init(mtd,
3556 chip->ecc.size,
3557 chip->ecc.bytes,
3558 &chip->ecc.layout);
3559 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003560 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003561 BUG();
3562 }
Mike Dunn6a918ba2012-03-11 14:21:11 -07003563 chip->ecc.strength =
Mike Dunne2788c92012-04-25 12:06:10 -07003564 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003565 break;
3566
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003567 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003568 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003569 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003570 chip->ecc.read_page = nand_read_page_raw;
3571 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003572 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003573 chip->ecc.read_page_raw = nand_read_page_raw;
3574 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003575 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003576 chip->ecc.size = mtd->writesize;
3577 chip->ecc.bytes = 0;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003578 chip->ecc.strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003582 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003583 BUG();
3584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Brian Norris9ce244b2011-08-30 18:45:37 -07003586 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003587 if (!chip->ecc.read_oob_raw)
3588 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003589 if (!chip->ecc.write_oob_raw)
3590 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3591
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003592 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003593 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003594 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003595 */
3596 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003597 for (i = 0; chip->ecc.layout->oobfree[i].length
3598 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003599 chip->ecc.layout->oobavail +=
3600 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003601 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003602
3603 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003604 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003605 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003606 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003607 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003608 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003609 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003610 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003612 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003613
Brian Norris8b6e50c2011-05-25 14:59:01 -07003614 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003615 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3616 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003617 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003618 case 2:
3619 mtd->subpage_sft = 1;
3620 break;
3621 case 4:
3622 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003623 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003624 mtd->subpage_sft = 2;
3625 break;
3626 }
3627 }
3628 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3629
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003630 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003631 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003634 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003636 /* Large page NAND with SOFT_ECC should support subpage reads */
3637 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3638 chip->options |= NAND_SUBPAGE_READ;
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 /* Fill in remaining MTD driver data */
3641 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003642 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3643 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003644 mtd->_erase = nand_erase;
3645 mtd->_point = NULL;
3646 mtd->_unpoint = NULL;
3647 mtd->_read = nand_read;
3648 mtd->_write = nand_write;
3649 mtd->_panic_write = panic_nand_write;
3650 mtd->_read_oob = nand_read_oob;
3651 mtd->_write_oob = nand_write_oob;
3652 mtd->_sync = nand_sync;
3653 mtd->_lock = NULL;
3654 mtd->_unlock = NULL;
3655 mtd->_suspend = nand_suspend;
3656 mtd->_resume = nand_resume;
3657 mtd->_block_isbad = nand_block_isbad;
3658 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003659 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Mike Dunn6a918ba2012-03-11 14:21:11 -07003661 /* propagate ecc info to mtd_info */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003662 mtd->ecclayout = chip->ecc.layout;
Mike Dunn86c20722012-04-25 12:06:05 -07003663 mtd->ecc_strength = chip->ecc.strength;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003664 /*
3665 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3666 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3667 * properly set.
3668 */
3669 if (!mtd->bitflip_threshold)
3670 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003672 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003673 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
3676 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003677 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003679EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Brian Norris8b6e50c2011-05-25 14:59:01 -07003681/*
3682 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003683 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003684 * to call us from in-kernel code if the core NAND support is modular.
3685 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003686#ifdef MODULE
3687#define caller_is_module() (1)
3688#else
3689#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003690 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003691#endif
3692
3693/**
3694 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003695 * @mtd: MTD device structure
3696 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003697 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003698 * This fills out all the uninitialized function pointers with the defaults.
3699 * The flash ID is read and the mtd/chip structures are filled with the
3700 * appropriate values. The mtd->owner field must be set to the module of the
3701 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003702 */
3703int nand_scan(struct mtd_info *mtd, int maxchips)
3704{
3705 int ret;
3706
3707 /* Many callers got this wrong, so check for it for a while... */
3708 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003709 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003710 BUG();
3711 }
3712
David Woodhouse5e81e882010-02-26 18:32:56 +00003713 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003714 if (!ret)
3715 ret = nand_scan_tail(mtd);
3716 return ret;
3717}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003718EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003721 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003722 * @mtd: MTD device structure
3723 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003724void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003726 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Ivan Djelic193bd402011-03-11 11:05:33 +01003728 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3729 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3730
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003731 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Jesper Juhlfa671642005-11-07 01:01:27 -08003733 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003734 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003735 if (!(chip->options & NAND_OWN_BUFFERS))
3736 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003737
3738 /* Free bad block descriptor memory */
3739 if (chip->badblock_pattern && chip->badblock_pattern->options
3740 & NAND_BBT_DYNAMICSTRUCT)
3741 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
David Woodhousee0c7d762006-05-13 18:07:53 +01003743EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003744
3745static int __init nand_base_init(void)
3746{
3747 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3748 return 0;
3749}
3750
3751static void __exit nand_base_exit(void)
3752{
3753 led_trigger_unregister_simple(nand_led_trigger);
3754}
3755
3756module_init(nand_base_init);
3757module_exit(nand_base_exit);
3758
David Woodhousee0c7d762006-05-13 18:07:53 +01003759MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003760MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3761MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003762MODULE_DESCRIPTION("Generic NAND flash driver code");