blob: 05f8243ed1d392fc1477cd1d3e0df35dff5fbee0 [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
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020096static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020097 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020099static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
100 struct mtd_oob_ops *ops);
101
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200102/*
Joe Perches8e87d782008-02-03 17:22:34 +0200103 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200104 * compiled away when LED support is disabled.
105 */
106DEFINE_LED_TRIGGER(nand_led_trigger);
107
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530108static int check_offs_len(struct mtd_info *mtd,
109 loff_t ofs, uint64_t len)
110{
111 struct nand_chip *chip = mtd->priv;
112 int ret = 0;
113
114 /* Start address must align on block boundary */
115 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700116 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530117 ret = -EINVAL;
118 }
119
120 /* Length must align on block boundary */
121 if (len & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700122 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530123 ret = -EINVAL;
124 }
125
126 /* Do not allow past end of device */
127 if (ofs + len > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -0700128 pr_debug("%s: past end of device\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530129 ret = -EINVAL;
130 }
131
132 return ret;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
136 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700137 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000138 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700139 * Deselect, release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100141static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200143 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200146 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100147
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200148 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200149 spin_lock(&chip->controller->lock);
150 chip->controller->active = NULL;
151 chip->state = FL_READY;
152 wake_up(&chip->controller->wq);
153 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/**
157 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700158 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700160 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200162static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200164 struct nand_chip *chip = mtd->priv;
165 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700170 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700171 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700173 * Default read function for 16bit buswidth with endianness conversion.
174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200176static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200178 struct nand_chip *chip = mtd->priv;
179 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700184 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700186 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188static u16 nand_read_word(struct mtd_info *mtd)
189{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190 struct nand_chip *chip = mtd->priv;
191 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700196 * @mtd: MTD device structure
197 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Default select function for 1 chip devices.
200 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200201static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200203 struct nand_chip *chip = mtd->priv;
204
205 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200207 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211
212 default:
213 BUG();
214 }
215}
216
217/**
218 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700219 * @mtd: MTD device structure
220 * @buf: data buffer
221 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700223 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200225static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200228 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
David Woodhousee0c7d762006-05-13 18:07:53 +0100230 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200231 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000235 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700236 * @mtd: MTD device structure
237 * @buf: buffer to store date
238 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700240 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200242static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200245 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Woodhousee0c7d762006-05-13 18:07:53 +0100247 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000252 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700253 * @mtd: MTD device structure
254 * @buf: buffer containing the data to compare
255 * @len: number of bytes to compare
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700257 * Default verify function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200259static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Woodhousee0c7d762006-05-13 18:07:53 +0100264 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200265 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return 0;
268}
269
270/**
271 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700272 * @mtd: MTD device structure
273 * @buf: data buffer
274 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700276 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200278static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200281 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 u16 *p = (u16 *) buf;
283 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000284
David Woodhousee0c7d762006-05-13 18:07:53 +0100285 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000291 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700292 * @mtd: MTD device structure
293 * @buf: buffer to store date
294 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700296 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200298static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200301 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 u16 *p = (u16 *) buf;
303 len >>= 1;
304
David Woodhousee0c7d762006-05-13 18:07:53 +0100305 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700311 * @mtd: MTD device structure
312 * @buf: buffer containing the data to compare
313 * @len: number of bytes to compare
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700315 * Default verify function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200317static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200320 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 u16 *p = (u16 *) buf;
322 len >>= 1;
323
David Woodhousee0c7d762006-05-13 18:07:53 +0100324 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200325 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EFAULT;
327
328 return 0;
329}
330
331/**
332 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * @mtd: MTD device structure
334 * @ofs: offset from device start
335 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000337 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
340{
Brian Norriscdbec052012-01-13 18:11:48 -0800341 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200342 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 u16 bad;
344
Brian Norris5fb15492011-05-31 16:31:21 -0700345 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700346 ofs += mtd->erasesize - mtd->writesize;
347
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100348 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200351 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200353 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200356 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Brian Norriscdbec052012-01-13 18:11:48 -0800359 do {
360 if (chip->options & NAND_BUSWIDTH_16) {
361 chip->cmdfunc(mtd, NAND_CMD_READOOB,
362 chip->badblockpos & 0xFE, page);
363 bad = cpu_to_le16(chip->read_word(mtd));
364 if (chip->badblockpos & 0x1)
365 bad >>= 8;
366 else
367 bad &= 0xFF;
368 } else {
369 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
370 page);
371 bad = chip->read_byte(mtd);
372 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000373
Brian Norriscdbec052012-01-13 18:11:48 -0800374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
378 ofs += mtd->writesize;
379 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
380 i++;
381 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200382
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200383 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return res;
387}
388
389/**
390 * nand_default_block_markbad - [DEFAULT] mark a block bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700391 * @mtd: MTD device structure
392 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394 * This is the default implementation, which can be overridden by a hardware
395 * specific driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396*/
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200399 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200400 uint8_t buf[2] = { 0, 0 };
Brian Norris02ed70b2010-07-21 16:53:47 -0700401 int block, ret, i = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Brian Norris00918422012-01-13 18:11:47 -0800403 if (!(chip->bbt_options & NAND_BBT_USE_FLASH)) {
404 struct erase_info einfo;
405
406 /* Attempt erase before marking OOB */
407 memset(&einfo, 0, sizeof(einfo));
408 einfo.mtd = mtd;
409 einfo.addr = ofs;
410 einfo.len = 1 << chip->phys_erase_shift;
411 nand_erase_nand(mtd, &einfo, 0);
412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400415 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200416 if (chip->bbt)
417 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Brian Norris8b6e50c2011-05-25 14:59:01 -0700419 /* Do we have a flash based bad block table? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700420 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200421 ret = nand_update_bbt(mtd, ofs);
422 else {
Brian Norris4a89ff82011-08-30 18:45:45 -0700423 struct mtd_oob_ops ops;
Brian Norrisdf698622012-01-20 20:38:03 -0800424 loff_t wr_ofs = ofs;
Brian Norris4a89ff82011-08-30 18:45:45 -0700425
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300426 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000427
Brian Norrisa0dc5522011-05-31 16:31:20 -0700428 /*
Brian Norrisdf698622012-01-20 20:38:03 -0800429 * Write to first/last page(s) if necessary. If we write to more
Brian Norrisa0dc5522011-05-31 16:31:20 -0700430 * than one location, the first error encountered quits the
Brian Norris85443312012-01-13 18:11:49 -0800431 * procedure.
Brian Norris02ed70b2010-07-21 16:53:47 -0700432 */
Brian Norris4a89ff82011-08-30 18:45:45 -0700433 ops.datbuf = NULL;
434 ops.oobbuf = buf;
Brian Norris85443312012-01-13 18:11:49 -0800435 ops.ooboffs = chip->badblockpos;
436 if (chip->options & NAND_BUSWIDTH_16) {
437 ops.ooboffs &= ~0x01;
438 ops.len = ops.ooblen = 2;
439 } else {
440 ops.len = ops.ooblen = 1;
441 }
Brian Norris23b1a992011-10-14 20:09:33 -0700442 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norrisdf698622012-01-20 20:38:03 -0800443
444 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
445 wr_ofs += mtd->erasesize - mtd->writesize;
Brian Norris02ed70b2010-07-21 16:53:47 -0700446 do {
Brian Norrisdf698622012-01-20 20:38:03 -0800447 ret = nand_do_write_oob(mtd, wr_ofs, &ops);
Brian Norris02ed70b2010-07-21 16:53:47 -0700448
Brian Norris02ed70b2010-07-21 16:53:47 -0700449 i++;
Brian Norrisdf698622012-01-20 20:38:03 -0800450 wr_ofs += mtd->writesize;
Brian Norris5fb15492011-05-31 16:31:21 -0700451 } while (!ret && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE) &&
Brian Norris02ed70b2010-07-21 16:53:47 -0700452 i < 2);
453
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300454 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200455 }
456 if (!ret)
457 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300458
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000462/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700464 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700466 * Check, if the device is write protected. The function expects, that the
467 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100469static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200471 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200472
Brian Norris8b6e50c2011-05-25 14:59:01 -0700473 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200474 if (chip->options & NAND_BROKEN_XD)
475 return 0;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200478 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
479 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/**
483 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700484 * @mtd: MTD device structure
485 * @ofs: offset from device start
486 * @getchip: 0, if the chip is already selected
487 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
489 * Check, if the block is bad. Either by reading the bad block table or
490 * calling of the scan function.
491 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200492static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
493 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200495 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000496
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200497 if (!chip->bbt)
498 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100501 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200504/**
505 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700506 * @mtd: MTD device structure
507 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200508 *
509 * Helper function for nand_wait_ready used when needing to wait in interrupt
510 * context.
511 */
512static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
513{
514 struct nand_chip *chip = mtd->priv;
515 int i;
516
517 /* Wait for the device to get ready */
518 for (i = 0; i < timeo; i++) {
519 if (chip->dev_ready(mtd))
520 break;
521 touch_softlockup_watchdog();
522 mdelay(1);
523 }
524}
525
Brian Norris7854d3f2011-06-23 14:12:08 -0700526/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100527void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000528{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200529 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100530 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000531
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200532 /* 400ms timeout */
533 if (in_interrupt() || oops_in_progress)
534 return panic_nand_wait_ready(mtd, 400);
535
Richard Purdie8fe833c2006-03-31 02:31:14 -0800536 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700537 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000538 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200539 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800540 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700541 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000542 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800543 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000544}
David Woodhouse4b648b02006-09-25 17:05:24 +0100545EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/**
548 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700549 * @mtd: MTD device structure
550 * @command: the command to be sent
551 * @column: the column address for this command, -1 if none
552 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700554 * Send command to NAND device. This function is used for small page devices
555 * (256/512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200557static void nand_command(struct mtd_info *mtd, unsigned int command,
558 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200560 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200561 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Brian Norris8b6e50c2011-05-25 14:59:01 -0700563 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (command == NAND_CMD_SEQIN) {
565 int readcmd;
566
Joern Engel28318772006-05-22 23:18:05 +0200567 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200569 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 readcmd = NAND_CMD_READOOB;
571 } else if (column < 256) {
572 /* First 256 bytes --> READ0 */
573 readcmd = NAND_CMD_READ0;
574 } else {
575 column -= 256;
576 readcmd = NAND_CMD_READ1;
577 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200578 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200579 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Brian Norris8b6e50c2011-05-25 14:59:01 -0700583 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
585 /* Serially input address */
586 if (column != -1) {
587 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200588 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200589 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200590 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200591 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200593 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200594 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200595 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200597 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200598 if (chip->chipsize > (32 << 20))
599 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200600 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200601 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000602
603 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700604 * Program and erase have their own busy handlers status and sequential
605 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case NAND_CMD_PAGEPROG:
610 case NAND_CMD_ERASE1:
611 case NAND_CMD_ERASE2:
612 case NAND_CMD_SEQIN:
613 case NAND_CMD_STATUS:
614 return;
615
616 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200617 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200619 udelay(chip->chip_delay);
620 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200621 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200622 chip->cmd_ctrl(mtd,
623 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200624 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
625 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return;
627
David Woodhousee0c7d762006-05-13 18:07:53 +0100628 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000630 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * If we don't have access to the busy pin, we apply the given
632 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100633 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200634 if (!chip->dev_ready) {
635 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700639 /*
640 * Apply this short delay always to ensure that we do wait tWB in
641 * any case on any machine.
642 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100643 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000644
645 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/**
649 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700650 * @mtd: MTD device structure
651 * @command: the command to be sent
652 * @column: the column address for this command, -1 if none
653 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200655 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700656 * devices. We don't have the separate regions as we have in the small page
657 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
660 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 /* Emulate NAND_CMD_READOOB */
665 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200666 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 command = NAND_CMD_READ0;
668 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000669
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200670 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200671 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200672 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200675 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* Serially input address */
678 if (column != -1) {
679 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200680 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200682 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200683 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200684 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200687 chip->cmd_ctrl(mtd, page_addr, ctrl);
688 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200689 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200691 if (chip->chipsize > (128 << 20))
692 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200696 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000697
698 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700699 * Program and erase have their own busy handlers status, sequential
700 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case NAND_CMD_CACHEDPROG:
705 case NAND_CMD_PAGEPROG:
706 case NAND_CMD_ERASE1:
707 case NAND_CMD_ERASE2:
708 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200709 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000711 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return;
713
David A. Marlin30f464b2005-01-17 18:35:25 +0000714 case NAND_CMD_STATUS_ERROR:
715 case NAND_CMD_STATUS_ERROR0:
716 case NAND_CMD_STATUS_ERROR1:
717 case NAND_CMD_STATUS_ERROR2:
718 case NAND_CMD_STATUS_ERROR3:
Brian Norris8b6e50c2011-05-25 14:59:01 -0700719 /* Read error status commands require only a short delay */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200720 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000721 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200724 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200726 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200727 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
728 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
729 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
730 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200731 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
732 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return;
734
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200735 case NAND_CMD_RNDOUT:
736 /* No ready / busy check necessary */
737 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
738 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
739 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
740 NAND_NCE | NAND_CTRL_CHANGE);
741 return;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200744 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
745 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
746 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
747 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000748
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000751 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700753 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100754 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 if (!chip->dev_ready) {
756 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000760
Brian Norris8b6e50c2011-05-25 14:59:01 -0700761 /*
762 * Apply this short delay always to ensure that we do wait tWB in
763 * any case on any machine.
764 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100765 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000766
767 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200771 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700772 * @chip: the nand chip descriptor
773 * @mtd: MTD device structure
774 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200775 *
776 * Used when in panic, no locks are taken.
777 */
778static void panic_nand_get_device(struct nand_chip *chip,
779 struct mtd_info *mtd, int new_state)
780{
Brian Norris7854d3f2011-06-23 14:12:08 -0700781 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200782 chip->controller->active = chip;
783 chip->state = new_state;
784}
785
786/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 * @chip: the nand chip descriptor
789 * @mtd: MTD device structure
790 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *
792 * Get the device and lock it for exclusive access
793 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200794static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200795nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200797 spinlock_t *lock = &chip->controller->lock;
798 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100799 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200800retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100801 spin_lock(lock);
802
vimal singhb8b3ee92009-07-09 20:41:22 +0530803 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200804 if (!chip->controller->active)
805 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200806
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200807 if (chip->controller->active == chip && chip->state == FL_READY) {
808 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100809 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100810 return 0;
811 }
812 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800813 if (chip->controller->active->state == FL_PM_SUSPENDED) {
814 chip->state = FL_PM_SUSPENDED;
815 spin_unlock(lock);
816 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800817 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100818 }
819 set_current_state(TASK_UNINTERRUPTIBLE);
820 add_wait_queue(wq, &wait);
821 spin_unlock(lock);
822 schedule();
823 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 goto retry;
825}
826
827/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700828 * panic_nand_wait - [GENERIC] wait until the command is done
829 * @mtd: MTD device structure
830 * @chip: NAND chip structure
831 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200832 *
833 * Wait for command done. This is a helper function for nand_wait used when
834 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400835 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200836 */
837static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
838 unsigned long timeo)
839{
840 int i;
841 for (i = 0; i < timeo; i++) {
842 if (chip->dev_ready) {
843 if (chip->dev_ready(mtd))
844 break;
845 } else {
846 if (chip->read_byte(mtd) & NAND_STATUS_READY)
847 break;
848 }
849 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200850 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200851}
852
853/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700854 * nand_wait - [DEFAULT] wait until the command is done
855 * @mtd: MTD device structure
856 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700858 * Wait for command done. This applies to erase and program only. Erase can
859 * take up to 400ms and program up to 20ms according to general NAND and
860 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700861 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200862static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864
David Woodhousee0c7d762006-05-13 18:07:53 +0100865 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200866 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100869 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100871 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Richard Purdie8fe833c2006-03-31 02:31:14 -0800873 led_trigger_event(nand_led_trigger, LED_FULL);
874
Brian Norris8b6e50c2011-05-25 14:59:01 -0700875 /*
876 * Apply this short delay always to ensure that we do wait tWB in any
877 * case on any machine.
878 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100879 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200881 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
882 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000883 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200884 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200886 if (in_interrupt() || oops_in_progress)
887 panic_nand_wait(mtd, chip, timeo);
888 else {
889 while (time_before(jiffies, timeo)) {
890 if (chip->dev_ready) {
891 if (chip->dev_ready(mtd))
892 break;
893 } else {
894 if (chip->read_byte(mtd) & NAND_STATUS_READY)
895 break;
896 }
897 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800900 led_trigger_event(nand_led_trigger, LED_OFF);
901
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200902 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return status;
904}
905
906/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700907 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700908 * @mtd: mtd info
909 * @ofs: offset to start unlock from
910 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700911 * @invert: when = 0, unlock the range of blocks within the lower and
912 * upper boundary address
913 * when = 1, unlock the range of blocks outside the boundaries
914 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530915 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700916 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530917 */
918static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
919 uint64_t len, int invert)
920{
921 int ret = 0;
922 int status, page;
923 struct nand_chip *chip = mtd->priv;
924
925 /* Submit address of first page to unlock */
926 page = ofs >> chip->page_shift;
927 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
928
929 /* Submit address of last page to unlock */
930 page = (ofs + len) >> chip->page_shift;
931 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
932 (page | invert) & chip->pagemask);
933
934 /* Call wait ready function */
935 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530936 /* See if device thinks it succeeded */
937 if (status & 0x01) {
Brian Norris289c0522011-07-19 10:06:09 -0700938 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530939 __func__, status);
940 ret = -EIO;
941 }
942
943 return ret;
944}
945
946/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700947 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700948 * @mtd: mtd info
949 * @ofs: offset to start unlock from
950 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530951 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700952 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530953 */
954int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
955{
956 int ret = 0;
957 int chipnr;
958 struct nand_chip *chip = mtd->priv;
959
Brian Norris289c0522011-07-19 10:06:09 -0700960 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530961 __func__, (unsigned long long)ofs, len);
962
963 if (check_offs_len(mtd, ofs, len))
964 ret = -EINVAL;
965
966 /* Align to last block address if size addresses end of the device */
967 if (ofs + len == mtd->size)
968 len -= mtd->erasesize;
969
970 nand_get_device(chip, mtd, FL_UNLOCKING);
971
972 /* Shift to get chip number */
973 chipnr = ofs >> chip->chip_shift;
974
975 chip->select_chip(mtd, chipnr);
976
977 /* Check, if it is write protected */
978 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700979 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530980 __func__);
981 ret = -EIO;
982 goto out;
983 }
984
985 ret = __nand_unlock(mtd, ofs, len, 0);
986
987out:
Vimal Singh7d70f332010-02-08 15:50:49 +0530988 nand_release_device(mtd);
989
990 return ret;
991}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200992EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530993
994/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700995 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700996 * @mtd: mtd info
997 * @ofs: offset to start unlock from
998 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530999 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1001 * have this feature, but it allows only to lock all blocks, not for specified
1002 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1003 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301004 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001005 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301006 */
1007int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1008{
1009 int ret = 0;
1010 int chipnr, status, page;
1011 struct nand_chip *chip = mtd->priv;
1012
Brian Norris289c0522011-07-19 10:06:09 -07001013 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301014 __func__, (unsigned long long)ofs, len);
1015
1016 if (check_offs_len(mtd, ofs, len))
1017 ret = -EINVAL;
1018
1019 nand_get_device(chip, mtd, FL_LOCKING);
1020
1021 /* Shift to get chip number */
1022 chipnr = ofs >> chip->chip_shift;
1023
1024 chip->select_chip(mtd, chipnr);
1025
1026 /* Check, if it is write protected */
1027 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001028 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301029 __func__);
1030 status = MTD_ERASE_FAILED;
1031 ret = -EIO;
1032 goto out;
1033 }
1034
1035 /* Submit address of first page to lock */
1036 page = ofs >> chip->page_shift;
1037 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1038
1039 /* Call wait ready function */
1040 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301041 /* See if device thinks it succeeded */
1042 if (status & 0x01) {
Brian Norris289c0522011-07-19 10:06:09 -07001043 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301044 __func__, status);
1045 ret = -EIO;
1046 goto out;
1047 }
1048
1049 ret = __nand_unlock(mtd, ofs, len, 0x1);
1050
1051out:
Vimal Singh7d70f332010-02-08 15:50:49 +05301052 nand_release_device(mtd);
1053
1054 return ret;
1055}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001056EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301057
1058/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001059 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001060 * @mtd: mtd info structure
1061 * @chip: nand chip info structure
1062 * @buf: buffer to store read data
1063 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001064 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001065 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001066 */
1067static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001068 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001069{
1070 chip->read_buf(mtd, buf, mtd->writesize);
1071 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1072 return 0;
1073}
1074
1075/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001076 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001077 * @mtd: mtd info structure
1078 * @chip: nand chip info structure
1079 * @buf: buffer to store read data
1080 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001081 *
1082 * We need a special oob layout and handling even when OOB isn't used.
1083 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001084static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1085 struct nand_chip *chip,
1086 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001087{
1088 int eccsize = chip->ecc.size;
1089 int eccbytes = chip->ecc.bytes;
1090 uint8_t *oob = chip->oob_poi;
1091 int steps, size;
1092
1093 for (steps = chip->ecc.steps; steps > 0; steps--) {
1094 chip->read_buf(mtd, buf, eccsize);
1095 buf += eccsize;
1096
1097 if (chip->ecc.prepad) {
1098 chip->read_buf(mtd, oob, chip->ecc.prepad);
1099 oob += chip->ecc.prepad;
1100 }
1101
1102 chip->read_buf(mtd, oob, eccbytes);
1103 oob += eccbytes;
1104
1105 if (chip->ecc.postpad) {
1106 chip->read_buf(mtd, oob, chip->ecc.postpad);
1107 oob += chip->ecc.postpad;
1108 }
1109 }
1110
1111 size = mtd->oobsize - (oob - chip->oob_poi);
1112 if (size)
1113 chip->read_buf(mtd, oob, size);
1114
1115 return 0;
1116}
1117
1118/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001119 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001120 * @mtd: mtd info structure
1121 * @chip: nand chip info structure
1122 * @buf: buffer to store read data
1123 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001124 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001125static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001126 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001128 int i, eccsize = chip->ecc.size;
1129 int eccbytes = chip->ecc.bytes;
1130 int eccsteps = chip->ecc.steps;
1131 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001132 uint8_t *ecc_calc = chip->buffers->ecccalc;
1133 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001134 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001135
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001136 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001137
1138 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1139 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1140
1141 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001142 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001143
1144 eccsteps = chip->ecc.steps;
1145 p = buf;
1146
1147 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1148 int stat;
1149
1150 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001151 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001152 mtd->ecc_stats.failed++;
1153 else
1154 mtd->ecc_stats.corrected += stat;
1155 }
1156 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001160 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001161 * @mtd: mtd info structure
1162 * @chip: nand chip info structure
1163 * @data_offs: offset of requested data within the page
1164 * @readlen: data length
1165 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001166 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001167static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1168 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001169{
1170 int start_step, end_step, num_steps;
1171 uint32_t *eccpos = chip->ecc.layout->eccpos;
1172 uint8_t *p;
1173 int data_col_addr, i, gaps = 0;
1174 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1175 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001176 int index = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001177
Brian Norris7854d3f2011-06-23 14:12:08 -07001178 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001179 start_step = data_offs / chip->ecc.size;
1180 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1181 num_steps = end_step - start_step + 1;
1182
Brian Norris8b6e50c2011-05-25 14:59:01 -07001183 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001184 datafrag_len = num_steps * chip->ecc.size;
1185 eccfrag_len = num_steps * chip->ecc.bytes;
1186
1187 data_col_addr = start_step * chip->ecc.size;
1188 /* If we read not a page aligned data */
1189 if (data_col_addr != 0)
1190 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1191
1192 p = bufpoi + data_col_addr;
1193 chip->read_buf(mtd, p, datafrag_len);
1194
Brian Norris8b6e50c2011-05-25 14:59:01 -07001195 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001196 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1197 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1198
Brian Norris8b6e50c2011-05-25 14:59:01 -07001199 /*
1200 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001201 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001202 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001203 for (i = 0; i < eccfrag_len - 1; i++) {
1204 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1205 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1206 gaps = 1;
1207 break;
1208 }
1209 }
1210 if (gaps) {
1211 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1212 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1213 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001214 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001215 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001216 * about buswidth alignment in read_buf.
1217 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001218 index = start_step * chip->ecc.bytes;
1219
1220 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001221 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001222 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001223 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001224 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001225 aligned_len++;
1226
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001227 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1228 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001229 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1230 }
1231
1232 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001233 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001234
1235 p = bufpoi + data_col_addr;
1236 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1237 int stat;
1238
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001239 stat = chip->ecc.correct(mtd, p,
1240 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Baruch Siach12c8eb92010-08-09 07:20:23 +03001241 if (stat < 0)
Alexey Korolev3d459552008-05-15 17:23:18 +01001242 mtd->ecc_stats.failed++;
1243 else
1244 mtd->ecc_stats.corrected += stat;
1245 }
1246 return 0;
1247}
1248
1249/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001250 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 * @mtd: mtd info structure
1252 * @chip: nand chip info structure
1253 * @buf: buffer to store read data
1254 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001255 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001256 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001257 */
1258static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001259 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001260{
1261 int i, eccsize = chip->ecc.size;
1262 int eccbytes = chip->ecc.bytes;
1263 int eccsteps = chip->ecc.steps;
1264 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001265 uint8_t *ecc_calc = chip->buffers->ecccalc;
1266 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001267 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001268
1269 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1270 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1271 chip->read_buf(mtd, p, eccsize);
1272 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1273 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001274 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001275
1276 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001277 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001278
1279 eccsteps = chip->ecc.steps;
1280 p = buf;
1281
1282 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1283 int stat;
1284
1285 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001286 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001287 mtd->ecc_stats.failed++;
1288 else
1289 mtd->ecc_stats.corrected += stat;
1290 }
1291 return 0;
1292}
1293
1294/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001295 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001296 * @mtd: mtd info structure
1297 * @chip: nand chip info structure
1298 * @buf: buffer to store read data
1299 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001300 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001301 * Hardware ECC for large page chips, require OOB to be read first. For this
1302 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1303 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1304 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1305 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001306 */
1307static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1308 struct nand_chip *chip, uint8_t *buf, int page)
1309{
1310 int i, eccsize = chip->ecc.size;
1311 int eccbytes = chip->ecc.bytes;
1312 int eccsteps = chip->ecc.steps;
1313 uint8_t *p = buf;
1314 uint8_t *ecc_code = chip->buffers->ecccode;
1315 uint32_t *eccpos = chip->ecc.layout->eccpos;
1316 uint8_t *ecc_calc = chip->buffers->ecccalc;
1317
1318 /* Read the OOB area first */
1319 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1320 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1321 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1322
1323 for (i = 0; i < chip->ecc.total; i++)
1324 ecc_code[i] = chip->oob_poi[eccpos[i]];
1325
1326 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1327 int stat;
1328
1329 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1330 chip->read_buf(mtd, p, eccsize);
1331 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1332
1333 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1334 if (stat < 0)
1335 mtd->ecc_stats.failed++;
1336 else
1337 mtd->ecc_stats.corrected += stat;
1338 }
1339 return 0;
1340}
1341
1342/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001343 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001344 * @mtd: mtd info structure
1345 * @chip: nand chip info structure
1346 * @buf: buffer to store read data
1347 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001348 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001349 * The hw generator calculates the error syndrome automatically. Therefore we
1350 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001351 */
1352static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001353 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001354{
1355 int i, eccsize = chip->ecc.size;
1356 int eccbytes = chip->ecc.bytes;
1357 int eccsteps = chip->ecc.steps;
1358 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001359 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001360
1361 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1362 int stat;
1363
1364 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1365 chip->read_buf(mtd, p, eccsize);
1366
1367 if (chip->ecc.prepad) {
1368 chip->read_buf(mtd, oob, chip->ecc.prepad);
1369 oob += chip->ecc.prepad;
1370 }
1371
1372 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1373 chip->read_buf(mtd, oob, eccbytes);
1374 stat = chip->ecc.correct(mtd, p, oob, NULL);
1375
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001376 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001377 mtd->ecc_stats.failed++;
1378 else
1379 mtd->ecc_stats.corrected += stat;
1380
1381 oob += eccbytes;
1382
1383 if (chip->ecc.postpad) {
1384 chip->read_buf(mtd, oob, chip->ecc.postpad);
1385 oob += chip->ecc.postpad;
1386 }
1387 }
1388
1389 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001390 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001391 if (i)
1392 chip->read_buf(mtd, oob, i);
1393
1394 return 0;
1395}
1396
1397/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001398 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001399 * @chip: nand chip structure
1400 * @oob: oob destination address
1401 * @ops: oob ops structure
1402 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001403 */
1404static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001405 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001406{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001407 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001408
Brian Norris0612b9d2011-08-30 18:45:40 -07001409 case MTD_OPS_PLACE_OOB:
1410 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001411 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1412 return oob + len;
1413
Brian Norris0612b9d2011-08-30 18:45:40 -07001414 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001415 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001416 uint32_t boffs = 0, roffs = ops->ooboffs;
1417 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001418
Florian Fainellif8ac0412010-09-07 13:23:43 +02001419 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001420 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001421 if (unlikely(roffs)) {
1422 if (roffs >= free->length) {
1423 roffs -= free->length;
1424 continue;
1425 }
1426 boffs = free->offset + roffs;
1427 bytes = min_t(size_t, len,
1428 (free->length - roffs));
1429 roffs = 0;
1430 } else {
1431 bytes = min_t(size_t, len, free->length);
1432 boffs = free->offset;
1433 }
1434 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001435 oob += bytes;
1436 }
1437 return oob;
1438 }
1439 default:
1440 BUG();
1441 }
1442 return NULL;
1443}
1444
1445/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001446 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001447 * @mtd: MTD device structure
1448 * @from: offset to read from
1449 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001450 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001451 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001452 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001453static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1454 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001455{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001456 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001457 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001458 struct mtd_ecc_stats stats;
1459 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1460 int sndcmd = 1;
1461 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001462 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001463 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001464 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001465 mtd->oobavail : mtd->oobsize;
1466
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001467 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001469 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001471 chipnr = (int)(from >> chip->chip_shift);
1472 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001474 realpage = (int)(from >> chip->page_shift);
1475 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001477 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001479 buf = ops->datbuf;
1480 oob = ops->oobbuf;
1481
Florian Fainellif8ac0412010-09-07 13:23:43 +02001482 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001483 bytes = min(mtd->writesize - col, readlen);
1484 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001485
Brian Norris8b6e50c2011-05-25 14:59:01 -07001486 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001487 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001488 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001490 if (likely(sndcmd)) {
1491 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1492 sndcmd = 0;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001495 /* Now read the page into the buffer */
Brian Norris0612b9d2011-08-30 18:45:40 -07001496 if (unlikely(ops->mode == MTD_OPS_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001497 ret = chip->ecc.read_page_raw(mtd, chip,
1498 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001499 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001500 ret = chip->ecc.read_subpage(mtd, chip,
1501 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001502 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001503 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1504 page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001505 if (ret < 0) {
1506 if (!aligned)
1507 /* Invalidate page cache */
1508 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001509 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001510 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001511
1512 /* Transfer not aligned data */
1513 if (!aligned) {
Artem Bityutskiyc1194c72010-09-03 22:01:16 +03001514 if (!NAND_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001515 !(mtd->ecc_stats.failed - stats.failed) &&
1516 (ops->mode != MTD_OPS_RAW))
Alexey Korolev3d459552008-05-15 17:23:18 +01001517 chip->pagebuf = realpage;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001518 else
1519 /* Invalidate page cache */
1520 chip->pagebuf = -1;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001521 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001523
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001524 buf += bytes;
1525
1526 if (unlikely(oob)) {
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001527
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001528 int toread = min(oobreadlen, max_oobsize);
1529
1530 if (toread) {
1531 oob = nand_transfer_oob(chip,
1532 oob, ops, toread);
1533 oobreadlen -= toread;
1534 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001535 }
1536
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001537 if (!(chip->options & NAND_NO_READRDY)) {
1538 /*
1539 * Apply delay or wait for ready/busy pin. Do
1540 * this before the AUTOINCR check, so no
1541 * problems arise if a chip which does auto
1542 * increment is marked as NOAUTOINCR by the
1543 * board driver.
1544 */
1545 if (!chip->dev_ready)
1546 udelay(chip->chip_delay);
1547 else
1548 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001550 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001551 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001552 buf += bytes;
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001555 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001556
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001557 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Brian Norris8b6e50c2011-05-25 14:59:01 -07001560 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 col = 0;
1562 /* Increment page address */
1563 realpage++;
1564
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001565 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /* Check, if we cross a chip boundary */
1567 if (!page) {
1568 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001569 chip->select_chip(mtd, -1);
1570 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001572
Brian Norris8b6e50c2011-05-25 14:59:01 -07001573 /*
1574 * Check, if the chip supports auto page increment or if we
1575 * have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001576 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001577 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001578 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001581 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001582 if (oob)
1583 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001585 if (ret)
1586 return ret;
1587
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001588 if (mtd->ecc_stats.failed - stats.failed)
1589 return -EBADMSG;
1590
1591 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001592}
1593
1594/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001595 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001596 * @mtd: MTD device structure
1597 * @from: offset to read from
1598 * @len: number of bytes to read
1599 * @retlen: pointer to variable to store the number of read bytes
1600 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001601 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001602 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001603 */
1604static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1605 size_t *retlen, uint8_t *buf)
1606{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001607 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07001608 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001609 int ret;
1610
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001611 /* Do not allow reads past end of device */
1612 if ((from + len) > mtd->size)
1613 return -EINVAL;
1614 if (!len)
1615 return 0;
1616
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001617 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001618
Brian Norris4a89ff82011-08-30 18:45:45 -07001619 ops.len = len;
1620 ops.datbuf = buf;
1621 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07001622 ops.mode = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001623
Brian Norris4a89ff82011-08-30 18:45:45 -07001624 ret = nand_do_read_ops(mtd, from, &ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001625
Brian Norris4a89ff82011-08-30 18:45:45 -07001626 *retlen = ops.retlen;
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001627
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001628 nand_release_device(mtd);
1629
1630 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
1633/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001634 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001635 * @mtd: mtd info structure
1636 * @chip: nand chip info structure
1637 * @page: page number to read
1638 * @sndcmd: flag whether to issue read command or not
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001639 */
1640static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1641 int page, int sndcmd)
1642{
1643 if (sndcmd) {
1644 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1645 sndcmd = 0;
1646 }
1647 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1648 return sndcmd;
1649}
1650
1651/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001652 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001653 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001654 * @mtd: mtd info structure
1655 * @chip: nand chip info structure
1656 * @page: page number to read
1657 * @sndcmd: flag whether to issue read command or not
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001658 */
1659static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1660 int page, int sndcmd)
1661{
1662 uint8_t *buf = chip->oob_poi;
1663 int length = mtd->oobsize;
1664 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1665 int eccsize = chip->ecc.size;
1666 uint8_t *bufpoi = buf;
1667 int i, toread, sndrnd = 0, pos;
1668
1669 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1670 for (i = 0; i < chip->ecc.steps; i++) {
1671 if (sndrnd) {
1672 pos = eccsize + i * (eccsize + chunk);
1673 if (mtd->writesize > 512)
1674 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1675 else
1676 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1677 } else
1678 sndrnd = 1;
1679 toread = min_t(int, length, chunk);
1680 chip->read_buf(mtd, bufpoi, toread);
1681 bufpoi += toread;
1682 length -= toread;
1683 }
1684 if (length > 0)
1685 chip->read_buf(mtd, bufpoi, length);
1686
1687 return 1;
1688}
1689
1690/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001691 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001692 * @mtd: mtd info structure
1693 * @chip: nand chip info structure
1694 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001695 */
1696static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1697 int page)
1698{
1699 int status = 0;
1700 const uint8_t *buf = chip->oob_poi;
1701 int length = mtd->oobsize;
1702
1703 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1704 chip->write_buf(mtd, buf, length);
1705 /* Send command to program the OOB data */
1706 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1707
1708 status = chip->waitfunc(mtd, chip);
1709
Savin Zlobec0d420f92006-06-21 11:51:20 +02001710 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001711}
1712
1713/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001714 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001715 * with syndrome - only for large page flash
1716 * @mtd: mtd info structure
1717 * @chip: nand chip info structure
1718 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001719 */
1720static int nand_write_oob_syndrome(struct mtd_info *mtd,
1721 struct nand_chip *chip, int page)
1722{
1723 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1724 int eccsize = chip->ecc.size, length = mtd->oobsize;
1725 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1726 const uint8_t *bufpoi = chip->oob_poi;
1727
1728 /*
1729 * data-ecc-data-ecc ... ecc-oob
1730 * or
1731 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1732 */
1733 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1734 pos = steps * (eccsize + chunk);
1735 steps = 0;
1736 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001737 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001738
1739 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1740 for (i = 0; i < steps; i++) {
1741 if (sndcmd) {
1742 if (mtd->writesize <= 512) {
1743 uint32_t fill = 0xFFFFFFFF;
1744
1745 len = eccsize;
1746 while (len > 0) {
1747 int num = min_t(int, len, 4);
1748 chip->write_buf(mtd, (uint8_t *)&fill,
1749 num);
1750 len -= num;
1751 }
1752 } else {
1753 pos = eccsize + i * (eccsize + chunk);
1754 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1755 }
1756 } else
1757 sndcmd = 1;
1758 len = min_t(int, length, chunk);
1759 chip->write_buf(mtd, bufpoi, len);
1760 bufpoi += len;
1761 length -= len;
1762 }
1763 if (length > 0)
1764 chip->write_buf(mtd, bufpoi, length);
1765
1766 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1767 status = chip->waitfunc(mtd, chip);
1768
1769 return status & NAND_STATUS_FAIL ? -EIO : 0;
1770}
1771
1772/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001773 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001774 * @mtd: MTD device structure
1775 * @from: offset to read from
1776 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001778 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001780static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1781 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001783 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001784 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001785 struct mtd_ecc_stats stats;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001786 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001787 int readlen = ops->ooblen;
1788 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001789 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Brian Norris289c0522011-07-19 10:06:09 -07001791 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301792 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Brian Norris041e4572011-06-23 16:45:24 -07001794 stats = mtd->ecc_stats;
1795
Brian Norris0612b9d2011-08-30 18:45:40 -07001796 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001797 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001798 else
1799 len = mtd->oobsize;
1800
1801 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001802 pr_debug("%s: attempt to start read outside oob\n",
1803 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001804 return -EINVAL;
1805 }
1806
1807 /* Do not allow reads past end of device */
1808 if (unlikely(from >= mtd->size ||
1809 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1810 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001811 pr_debug("%s: attempt to read beyond end of device\n",
1812 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001813 return -EINVAL;
1814 }
Vitaly Wool70145682006-11-03 18:20:38 +03001815
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001816 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001817 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001819 /* Shift to get page */
1820 realpage = (int)(from >> chip->page_shift);
1821 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Florian Fainellif8ac0412010-09-07 13:23:43 +02001823 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001824 if (ops->mode == MTD_OPS_RAW)
Brian Norrisc46f6482011-08-30 18:45:38 -07001825 sndcmd = chip->ecc.read_oob_raw(mtd, chip, page, sndcmd);
1826 else
1827 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001828
1829 len = min(len, readlen);
1830 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001831
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001832 if (!(chip->options & NAND_NO_READRDY)) {
1833 /*
1834 * Apply delay or wait for ready/busy pin. Do this
1835 * before the AUTOINCR check, so no problems arise if a
1836 * chip which does auto increment is marked as
1837 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001838 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001839 if (!chip->dev_ready)
1840 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001841 else
1842 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001844
Vitaly Wool70145682006-11-03 18:20:38 +03001845 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001846 if (!readlen)
1847 break;
1848
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001849 /* Increment page address */
1850 realpage++;
1851
1852 page = realpage & chip->pagemask;
1853 /* Check, if we cross a chip boundary */
1854 if (!page) {
1855 chipnr++;
1856 chip->select_chip(mtd, -1);
1857 chip->select_chip(mtd, chipnr);
1858 }
1859
Brian Norris8b6e50c2011-05-25 14:59:01 -07001860 /*
1861 * Check, if the chip supports auto page increment or if we
1862 * have hit a block boundary.
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001863 */
1864 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1865 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
Vitaly Wool70145682006-11-03 18:20:38 +03001868 ops->oobretlen = ops->ooblen;
Brian Norris041e4572011-06-23 16:45:24 -07001869
1870 if (mtd->ecc_stats.failed - stats.failed)
1871 return -EBADMSG;
1872
1873 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
1876/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001877 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001878 * @mtd: MTD device structure
1879 * @from: offset to read from
1880 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001882 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001884static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1885 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001887 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001888 int ret = -ENOTSUPP;
1889
1890 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001893 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001894 pr_debug("%s: attempt to read beyond end of device\n",
1895 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return -EINVAL;
1897 }
1898
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001899 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Florian Fainellif8ac0412010-09-07 13:23:43 +02001901 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001902 case MTD_OPS_PLACE_OOB:
1903 case MTD_OPS_AUTO_OOB:
1904 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001905 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001906
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001907 default:
1908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001911 if (!ops->datbuf)
1912 ret = nand_do_read_oob(mtd, from, ops);
1913 else
1914 ret = nand_do_read_ops(mtd, from, ops);
1915
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001918 return ret;
1919}
1920
1921
1922/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001923 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001924 * @mtd: mtd info structure
1925 * @chip: nand chip info structure
1926 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001927 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001928 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001929 */
1930static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1931 const uint8_t *buf)
1932{
1933 chip->write_buf(mtd, buf, mtd->writesize);
1934 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001937/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001938 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001939 * @mtd: mtd info structure
1940 * @chip: nand chip info structure
1941 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001942 *
1943 * We need a special oob layout and handling even when ECC isn't checked.
1944 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001945static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
1946 struct nand_chip *chip,
1947 const uint8_t *buf)
David Brownell52ff49d2009-03-04 12:01:36 -08001948{
1949 int eccsize = chip->ecc.size;
1950 int eccbytes = chip->ecc.bytes;
1951 uint8_t *oob = chip->oob_poi;
1952 int steps, size;
1953
1954 for (steps = chip->ecc.steps; steps > 0; steps--) {
1955 chip->write_buf(mtd, buf, eccsize);
1956 buf += eccsize;
1957
1958 if (chip->ecc.prepad) {
1959 chip->write_buf(mtd, oob, chip->ecc.prepad);
1960 oob += chip->ecc.prepad;
1961 }
1962
1963 chip->read_buf(mtd, oob, eccbytes);
1964 oob += eccbytes;
1965
1966 if (chip->ecc.postpad) {
1967 chip->write_buf(mtd, oob, chip->ecc.postpad);
1968 oob += chip->ecc.postpad;
1969 }
1970 }
1971
1972 size = mtd->oobsize - (oob - chip->oob_poi);
1973 if (size)
1974 chip->write_buf(mtd, oob, size);
1975}
1976/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001977 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001978 * @mtd: mtd info structure
1979 * @chip: nand chip info structure
1980 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001981 */
1982static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1983 const uint8_t *buf)
1984{
1985 int i, eccsize = chip->ecc.size;
1986 int eccbytes = chip->ecc.bytes;
1987 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001988 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001989 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001990 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001991
Brian Norris7854d3f2011-06-23 14:12:08 -07001992 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001993 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1994 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001995
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001996 for (i = 0; i < chip->ecc.total; i++)
1997 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001998
Thomas Gleixner90424de2007-04-05 11:44:05 +02001999 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002000}
2001
2002/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002003 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002004 * @mtd: mtd info structure
2005 * @chip: nand chip info structure
2006 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002007 */
2008static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2009 const uint8_t *buf)
2010{
2011 int i, eccsize = chip->ecc.size;
2012 int eccbytes = chip->ecc.bytes;
2013 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002014 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002015 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002016 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002017
2018 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2019 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002020 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002021 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2022 }
2023
2024 for (i = 0; i < chip->ecc.total; i++)
2025 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2026
2027 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2028}
2029
2030/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002031 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002032 * @mtd: mtd info structure
2033 * @chip: nand chip info structure
2034 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002035 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002036 * The hw generator calculates the error syndrome automatically. Therefore we
2037 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002038 */
2039static void nand_write_page_syndrome(struct mtd_info *mtd,
2040 struct nand_chip *chip, const uint8_t *buf)
2041{
2042 int i, eccsize = chip->ecc.size;
2043 int eccbytes = chip->ecc.bytes;
2044 int eccsteps = chip->ecc.steps;
2045 const uint8_t *p = buf;
2046 uint8_t *oob = chip->oob_poi;
2047
2048 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2049
2050 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2051 chip->write_buf(mtd, p, eccsize);
2052
2053 if (chip->ecc.prepad) {
2054 chip->write_buf(mtd, oob, chip->ecc.prepad);
2055 oob += chip->ecc.prepad;
2056 }
2057
2058 chip->ecc.calculate(mtd, p, oob);
2059 chip->write_buf(mtd, oob, eccbytes);
2060 oob += eccbytes;
2061
2062 if (chip->ecc.postpad) {
2063 chip->write_buf(mtd, oob, chip->ecc.postpad);
2064 oob += chip->ecc.postpad;
2065 }
2066 }
2067
2068 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002069 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002070 if (i)
2071 chip->write_buf(mtd, oob, i);
2072}
2073
2074/**
David Woodhouse956e9442006-09-25 17:12:39 +01002075 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002076 * @mtd: MTD device structure
2077 * @chip: NAND chip descriptor
2078 * @buf: the data to write
2079 * @page: page number to write
2080 * @cached: cached programming
2081 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002082 */
2083static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01002084 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002085{
2086 int status;
2087
2088 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2089
David Woodhouse956e9442006-09-25 17:12:39 +01002090 if (unlikely(raw))
2091 chip->ecc.write_page_raw(mtd, chip, buf);
2092 else
2093 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002094
2095 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002096 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002097 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002098 */
2099 cached = 0;
2100
2101 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2102
2103 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002104 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002105 /*
2106 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002107 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002108 */
2109 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2110 status = chip->errstat(mtd, chip, FL_WRITING, status,
2111 page);
2112
2113 if (status & NAND_STATUS_FAIL)
2114 return -EIO;
2115 } else {
2116 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002117 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002118 }
2119
2120#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2121 /* Send command to read back the data */
2122 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2123
2124 if (chip->verify_buf(mtd, buf, mtd->writesize))
2125 return -EIO;
2126#endif
2127 return 0;
2128}
2129
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002130/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002131 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002132 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002133 * @oob: oob data buffer
2134 * @len: oob data write length
2135 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002136 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002137static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2138 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002139{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002140 struct nand_chip *chip = mtd->priv;
2141
2142 /*
2143 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2144 * data from a previous OOB read.
2145 */
2146 memset(chip->oob_poi, 0xff, mtd->oobsize);
2147
Florian Fainellif8ac0412010-09-07 13:23:43 +02002148 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002149
Brian Norris0612b9d2011-08-30 18:45:40 -07002150 case MTD_OPS_PLACE_OOB:
2151 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002152 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2153 return oob + len;
2154
Brian Norris0612b9d2011-08-30 18:45:40 -07002155 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002156 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002157 uint32_t boffs = 0, woffs = ops->ooboffs;
2158 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002159
Florian Fainellif8ac0412010-09-07 13:23:43 +02002160 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002161 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002162 if (unlikely(woffs)) {
2163 if (woffs >= free->length) {
2164 woffs -= free->length;
2165 continue;
2166 }
2167 boffs = free->offset + woffs;
2168 bytes = min_t(size_t, len,
2169 (free->length - woffs));
2170 woffs = 0;
2171 } else {
2172 bytes = min_t(size_t, len, free->length);
2173 boffs = free->offset;
2174 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002175 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002176 oob += bytes;
2177 }
2178 return oob;
2179 }
2180 default:
2181 BUG();
2182 }
2183 return NULL;
2184}
2185
Florian Fainellif8ac0412010-09-07 13:23:43 +02002186#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002187
2188/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002189 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002190 * @mtd: MTD device structure
2191 * @to: offset to write to
2192 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002193 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002194 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002195 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002196static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2197 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002198{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002199 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002200 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002201 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002202
2203 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002204 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002205 mtd->oobavail : mtd->oobsize;
2206
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002207 uint8_t *oob = ops->oobbuf;
2208 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002209 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002210
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002211 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002212 if (!writelen)
2213 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002214
Brian Norris8b6e50c2011-05-25 14:59:01 -07002215 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002216 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002217 pr_notice("%s: attempt to write non page aligned data\n",
2218 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002219 return -EINVAL;
2220 }
2221
Thomas Gleixner29072b92006-09-28 15:38:36 +02002222 column = to & (mtd->writesize - 1);
2223 subpage = column || (writelen & (mtd->writesize - 1));
2224
2225 if (subpage && oob)
2226 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002227
Thomas Gleixner6a930962006-06-28 00:11:45 +02002228 chipnr = (int)(to >> chip->chip_shift);
2229 chip->select_chip(mtd, chipnr);
2230
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002231 /* Check, if it is write protected */
2232 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002233 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002234
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002235 realpage = (int)(to >> chip->page_shift);
2236 page = realpage & chip->pagemask;
2237 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2238
2239 /* Invalidate the page cache, when we write to the cached page */
2240 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002241 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002242 chip->pagebuf = -1;
2243
Maxim Levitsky782ce792010-02-22 20:39:36 +02002244 /* Don't allow multipage oob writes with offset */
Jon Poveycdcf12b2010-09-30 20:41:34 +09002245 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
Maxim Levitsky782ce792010-02-22 20:39:36 +02002246 return -EINVAL;
2247
Florian Fainellif8ac0412010-09-07 13:23:43 +02002248 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002249 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002250 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002251 uint8_t *wbuf = buf;
2252
Brian Norris8b6e50c2011-05-25 14:59:01 -07002253 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002254 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2255 cached = 0;
2256 bytes = min_t(int, bytes - column, (int) writelen);
2257 chip->pagebuf = -1;
2258 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2259 memcpy(&chip->buffers->databuf[column], buf, bytes);
2260 wbuf = chip->buffers->databuf;
2261 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002262
Maxim Levitsky782ce792010-02-22 20:39:36 +02002263 if (unlikely(oob)) {
2264 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002265 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002266 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002267 } else {
2268 /* We still need to erase leftover OOB data */
2269 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002270 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002271
Thomas Gleixner29072b92006-09-28 15:38:36 +02002272 ret = chip->write_page(mtd, chip, wbuf, page, cached,
Brian Norris0612b9d2011-08-30 18:45:40 -07002273 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002274 if (ret)
2275 break;
2276
2277 writelen -= bytes;
2278 if (!writelen)
2279 break;
2280
Thomas Gleixner29072b92006-09-28 15:38:36 +02002281 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002282 buf += bytes;
2283 realpage++;
2284
2285 page = realpage & chip->pagemask;
2286 /* Check, if we cross a chip boundary */
2287 if (!page) {
2288 chipnr++;
2289 chip->select_chip(mtd, -1);
2290 chip->select_chip(mtd, chipnr);
2291 }
2292 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002293
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002294 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002295 if (unlikely(oob))
2296 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002297 return ret;
2298}
2299
2300/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002301 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002302 * @mtd: MTD device structure
2303 * @to: offset to write to
2304 * @len: number of bytes to write
2305 * @retlen: pointer to variable to store the number of written bytes
2306 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002307 *
2308 * NAND write with ECC. Used when performing writes in interrupt context, this
2309 * may for example be called by mtdoops when writing an oops while in panic.
2310 */
2311static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2312 size_t *retlen, const uint8_t *buf)
2313{
2314 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002315 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002316 int ret;
2317
2318 /* Do not allow reads past end of device */
2319 if ((to + len) > mtd->size)
2320 return -EINVAL;
2321 if (!len)
2322 return 0;
2323
Brian Norris8b6e50c2011-05-25 14:59:01 -07002324 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002325 panic_nand_wait(mtd, chip, 400);
2326
Brian Norris8b6e50c2011-05-25 14:59:01 -07002327 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002328 panic_nand_get_device(chip, mtd, FL_WRITING);
2329
Brian Norris4a89ff82011-08-30 18:45:45 -07002330 ops.len = len;
2331 ops.datbuf = (uint8_t *)buf;
2332 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07002333 ops.mode = 0;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002334
Brian Norris4a89ff82011-08-30 18:45:45 -07002335 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002336
Brian Norris4a89ff82011-08-30 18:45:45 -07002337 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002338 return ret;
2339}
2340
2341/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002342 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002343 * @mtd: MTD device structure
2344 * @to: offset to write to
2345 * @len: number of bytes to write
2346 * @retlen: pointer to variable to store the number of written bytes
2347 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002349 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002351static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002352 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002354 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002355 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002356 int ret;
2357
2358 /* Do not allow reads past end of device */
2359 if ((to + len) > mtd->size)
2360 return -EINVAL;
2361 if (!len)
2362 return 0;
2363
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002364 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002365
Brian Norris4a89ff82011-08-30 18:45:45 -07002366 ops.len = len;
2367 ops.datbuf = (uint8_t *)buf;
2368 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07002369 ops.mode = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002370
Brian Norris4a89ff82011-08-30 18:45:45 -07002371 ret = nand_do_write_ops(mtd, to, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002372
Brian Norris4a89ff82011-08-30 18:45:45 -07002373 *retlen = ops.retlen;
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002374
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002375 nand_release_device(mtd);
2376
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002377 return ret;
2378}
2379
2380/**
2381 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002382 * @mtd: MTD device structure
2383 * @to: offset to write to
2384 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002385 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002386 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002387 */
2388static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2389 struct mtd_oob_ops *ops)
2390{
Adrian Hunter03736152007-01-31 17:58:29 +02002391 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002392 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Brian Norris289c0522011-07-19 10:06:09 -07002394 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302395 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Brian Norris0612b9d2011-08-30 18:45:40 -07002397 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002398 len = chip->ecc.layout->oobavail;
2399 else
2400 len = mtd->oobsize;
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002403 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002404 pr_debug("%s: attempt to write past end of page\n",
2405 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return -EINVAL;
2407 }
2408
Adrian Hunter03736152007-01-31 17:58:29 +02002409 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002410 pr_debug("%s: attempt to start write outside oob\n",
2411 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002412 return -EINVAL;
2413 }
2414
Jason Liu775adc32011-02-25 13:06:18 +08002415 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002416 if (unlikely(to >= mtd->size ||
2417 ops->ooboffs + ops->ooblen >
2418 ((mtd->size >> chip->page_shift) -
2419 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002420 pr_debug("%s: attempt to write beyond end of device\n",
2421 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002422 return -EINVAL;
2423 }
2424
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002425 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002426 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002428 /* Shift to get page */
2429 page = (int)(to >> chip->page_shift);
2430
2431 /*
2432 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2433 * of my DiskOnChip 2000 test units) will clear the whole data page too
2434 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2435 * it in the doc2000 driver in August 1999. dwmw2.
2436 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002437 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 /* Check, if it is write protected */
2440 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002441 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002444 if (page == chip->pagebuf)
2445 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002447 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002448
Brian Norris0612b9d2011-08-30 18:45:40 -07002449 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002450 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2451 else
2452 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002453
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002454 if (status)
2455 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Vitaly Wool70145682006-11-03 18:20:38 +03002457 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002459 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462/**
2463 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002464 * @mtd: MTD device structure
2465 * @to: offset to write to
2466 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002467 */
2468static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2469 struct mtd_oob_ops *ops)
2470{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002471 struct nand_chip *chip = mtd->priv;
2472 int ret = -ENOTSUPP;
2473
2474 ops->retlen = 0;
2475
2476 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002477 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002478 pr_debug("%s: attempt to write beyond end of device\n",
2479 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002480 return -EINVAL;
2481 }
2482
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002483 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002484
Florian Fainellif8ac0412010-09-07 13:23:43 +02002485 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002486 case MTD_OPS_PLACE_OOB:
2487 case MTD_OPS_AUTO_OOB:
2488 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002489 break;
2490
2491 default:
2492 goto out;
2493 }
2494
2495 if (!ops->datbuf)
2496 ret = nand_do_write_oob(mtd, to, ops);
2497 else
2498 ret = nand_do_write_ops(mtd, to, ops);
2499
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002500out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002501 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return ret;
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002506 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002507 * @mtd: MTD device structure
2508 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002510 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002512static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002514 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002516 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2517 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
2520/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002521 * multi_erase_cmd - [GENERIC] AND specific block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002522 * @mtd: MTD device structure
2523 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002525 * AND multi block erase command function. Erase 4 consecutive blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002527static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002529 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002531 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2532 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2533 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2534 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2535 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/**
2539 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002540 * @mtd: MTD device structure
2541 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002543 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002545static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
David Woodhousee0c7d762006-05-13 18:07:53 +01002547 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002549
David A. Marlin30f464b2005-01-17 18:35:25 +00002550#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002552 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002553 * @mtd: MTD device structure
2554 * @instr: erase instruction
2555 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002557 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002559int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2560 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Adrian Hunter69423d92008-12-10 13:37:21 +00002562 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002563 struct nand_chip *chip = mtd->priv;
Florian Fainellif8ac0412010-09-07 13:23:43 +02002564 loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002565 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002566 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Brian Norris289c0522011-07-19 10:06:09 -07002568 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2569 __func__, (unsigned long long)instr->addr,
2570 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302572 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002575 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002578 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002581 page = (int)(instr->addr >> chip->page_shift);
2582 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002585 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002588 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* Check, if it is write protected */
2591 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002592 pr_debug("%s: device is write protected!\n",
2593 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 instr->state = MTD_ERASE_FAILED;
2595 goto erase_exit;
2596 }
2597
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002598 /*
2599 * If BBT requires refresh, set the BBT page mask to see if the BBT
2600 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2601 * can not be matched. This is also done when the bbt is actually
Brian Norris7854d3f2011-06-23 14:12:08 -07002602 * erased to avoid recursive updates.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002603 */
2604 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2605 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /* Loop through the pages */
2608 len = instr->len;
2609
2610 instr->state = MTD_ERASING;
2611
2612 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002613 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002614 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2615 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002616 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2617 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 instr->state = MTD_ERASE_FAILED;
2619 goto erase_exit;
2620 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002621
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002622 /*
2623 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002624 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 */
2626 if (page <= chip->pagebuf && chip->pagebuf <
2627 (page + pages_per_block))
2628 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002630 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002631
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002632 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002634 /*
2635 * See if operation failed and additional status checks are
2636 * available
2637 */
2638 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2639 status = chip->errstat(mtd, chip, FL_ERASING,
2640 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002643 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002644 pr_debug("%s: failed erase, page 0x%08x\n",
2645 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002647 instr->fail_addr =
2648 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 goto erase_exit;
2650 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002651
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002652 /*
2653 * If BBT requires refresh, set the BBT rewrite flag to the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 * page being erased.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655 */
2656 if (bbt_masked_page != 0xffffffff &&
2657 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002658 rewrite_bbt[chipnr] =
2659 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002662 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 page += pages_per_block;
2664
2665 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002666 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002668 chip->select_chip(mtd, -1);
2669 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002670
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002671 /*
2672 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Brian Norris8b6e50c2011-05-25 14:59:01 -07002673 * page mask to see if this BBT should be rewritten.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002674 */
2675 if (bbt_masked_page != 0xffffffff &&
2676 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2677 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2678 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680 }
2681 instr->state = MTD_ERASE_DONE;
2682
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002683erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 /* Deselect and wake up anyone waiting on the device */
2688 nand_release_device(mtd);
2689
David Woodhouse49defc02007-10-06 15:01:59 -04002690 /* Do call back function */
2691 if (!ret)
2692 mtd_erase_callback(instr);
2693
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002694 /*
2695 * If BBT requires refresh and erase was successful, rewrite any
Brian Norris8b6e50c2011-05-25 14:59:01 -07002696 * selected bad block tables.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002697 */
2698 if (bbt_masked_page == 0xffffffff || ret)
2699 return ret;
2700
2701 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2702 if (!rewrite_bbt[chipnr])
2703 continue;
Brian Norris8b6e50c2011-05-25 14:59:01 -07002704 /* Update the BBT for chip */
Brian Norris289c0522011-07-19 10:06:09 -07002705 pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2706 __func__, chipnr, rewrite_bbt[chipnr],
2707 chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002708 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002709 }
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /* Return more or less happy */
2712 return ret;
2713}
2714
2715/**
2716 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002717 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002719 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002721static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Brian Norris289c0522011-07-19 10:06:09 -07002725 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002728 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002730 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002734 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002735 * @mtd: MTD device structure
2736 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002738static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
2740 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002741 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002743
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002744 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
2747/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002748 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002749 * @mtd: MTD device structure
2750 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002752static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002754 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 int ret;
2756
Florian Fainellif8ac0412010-09-07 13:23:43 +02002757 ret = nand_block_isbad(mtd, ofs);
2758 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002759 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (ret > 0)
2761 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002762 return ret;
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002765 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
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{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002774 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002775
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002776 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002777}
2778
2779/**
2780 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002781 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002782 */
2783static void nand_resume(struct mtd_info *mtd)
2784{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002785 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002786
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002787 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002788 nand_release_device(mtd);
2789 else
Brian Norrisd0370212011-07-19 10:06:08 -07002790 pr_err("%s called for a chip which is not in suspended state\n",
2791 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002792}
2793
Brian Norris8b6e50c2011-05-25 14:59:01 -07002794/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002795static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002798 if (!chip->chip_delay)
2799 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002802 if (chip->cmdfunc == NULL)
2803 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002806 if (chip->waitfunc == NULL)
2807 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002809 if (!chip->select_chip)
2810 chip->select_chip = nand_select_chip;
2811 if (!chip->read_byte)
2812 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2813 if (!chip->read_word)
2814 chip->read_word = nand_read_word;
2815 if (!chip->block_bad)
2816 chip->block_bad = nand_block_bad;
2817 if (!chip->block_markbad)
2818 chip->block_markbad = nand_default_block_markbad;
2819 if (!chip->write_buf)
2820 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2821 if (!chip->read_buf)
2822 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2823 if (!chip->verify_buf)
2824 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2825 if (!chip->scan_bbt)
2826 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002827
2828 if (!chip->controller) {
2829 chip->controller = &chip->hwcontrol;
2830 spin_lock_init(&chip->controller->lock);
2831 init_waitqueue_head(&chip->controller->wq);
2832 }
2833
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002834}
2835
Brian Norris8b6e50c2011-05-25 14:59:01 -07002836/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002837static void sanitize_string(uint8_t *s, size_t len)
2838{
2839 ssize_t i;
2840
Brian Norris8b6e50c2011-05-25 14:59:01 -07002841 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002842 s[len - 1] = 0;
2843
Brian Norris8b6e50c2011-05-25 14:59:01 -07002844 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002845 for (i = 0; i < len - 1; i++) {
2846 if (s[i] < ' ' || s[i] > 127)
2847 s[i] = '?';
2848 }
2849
Brian Norris8b6e50c2011-05-25 14:59:01 -07002850 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002851 strim(s);
2852}
2853
2854static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2855{
2856 int i;
2857 while (len--) {
2858 crc ^= *p++ << 8;
2859 for (i = 0; i < 8; i++)
2860 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2861 }
2862
2863 return crc;
2864}
2865
2866/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002867 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002868 */
2869static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002870 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002871{
2872 struct nand_onfi_params *p = &chip->onfi_params;
2873 int i;
2874 int val;
2875
Brian Norris7854d3f2011-06-23 14:12:08 -07002876 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002877 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2878 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2879 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2880 return 0;
2881
Brian Norris9a4d4d62011-07-19 10:06:07 -07002882 pr_info("ONFI flash detected\n");
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);
David Woodhouse4ccb3b42010-12-03 16:36:34 +00002923 chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002924 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002925 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002926 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002927
2928 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2929 chip->options |= (NAND_NO_READRDY |
2930 NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
2931
2932 return 1;
2933}
2934
2935/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002936 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002937 */
2938static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002939 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002940 int busw,
2941 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00002942 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002943{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002944 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07002945 u8 id_data[8];
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002946 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002949 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Karl Beldanef89a882008-09-15 14:37:29 +02002951 /*
2952 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002953 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02002954 */
2955 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002958 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002961 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002962 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Brian Norris8b6e50c2011-05-25 14:59:01 -07002964 /*
2965 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01002966 * interface concerns can cause random data which looks like a
2967 * possibly credible NAND flash to appear. If the two results do
2968 * not match, ignore the device completely.
2969 */
2970
2971 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2972
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002973 for (i = 0; i < 2; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07002974 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01002975
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002976 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002977 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07002978 "%02x,%02x against %02x,%02x\n", __func__,
2979 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01002980 return ERR_PTR(-ENODEV);
2981 }
2982
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002983 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00002984 type = nand_flash_ids;
2985
2986 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002987 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02002988 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00002989
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002990 chip->onfi_version = 0;
2991 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002992 /* Check is chip is ONFI compliant */
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002993 ret = nand_flash_detect_onfi(mtd, chip, &busw);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002994 if (ret)
2995 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002996 }
2997
2998 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2999
3000 /* Read entire ID string */
3001
3002 for (i = 0; i < 8; i++)
3003 id_data[i] = chip->read_byte(mtd);
3004
David Woodhouse5e81e882010-02-26 18:32:56 +00003005 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003006 return ERR_PTR(-ENODEV);
3007
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003008 if (!mtd->name)
3009 mtd->name = type->name;
3010
Adrian Hunter69423d92008-12-10 13:37:21 +00003011 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003012
Huang Shijie12a40a52010-09-27 10:43:53 +08003013 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003014 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003015 busw = chip->init_size(mtd, chip, id_data);
3016 } else if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003017 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003018 /* The 3rd id byte holds MLC / multichip data */
Kevin Cernekee426c4572010-05-04 20:58:03 -07003019 chip->cellinfo = id_data[2];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003020 /* The 4th id byte is the important one */
Kevin Cernekee426c4572010-05-04 20:58:03 -07003021 extid = id_data[3];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003022
Kevin Cernekee426c4572010-05-04 20:58:03 -07003023 /*
3024 * Field definitions are in the following datasheets:
3025 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norris34c5bf62010-08-20 10:50:43 -07003026 * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003027 *
3028 * Check for wraparound + Samsung ID + nonzero 6th byte
3029 * to decide what to do.
3030 */
3031 if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
3032 id_data[0] == NAND_MFR_SAMSUNG &&
Tilman Sauerbeckcfe3fda2010-08-20 14:01:47 -07003033 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Kevin Cernekee426c4572010-05-04 20:58:03 -07003034 id_data[5] != 0x00) {
3035 /* Calc pagesize */
3036 mtd->writesize = 2048 << (extid & 0x03);
3037 extid >>= 2;
3038 /* Calc oobsize */
Brian Norris34c5bf62010-08-20 10:50:43 -07003039 switch (extid & 0x03) {
3040 case 1:
3041 mtd->oobsize = 128;
3042 break;
3043 case 2:
3044 mtd->oobsize = 218;
3045 break;
3046 case 3:
3047 mtd->oobsize = 400;
3048 break;
3049 default:
3050 mtd->oobsize = 436;
3051 break;
3052 }
Kevin Cernekee426c4572010-05-04 20:58:03 -07003053 extid >>= 2;
3054 /* Calc blocksize */
3055 mtd->erasesize = (128 * 1024) <<
3056 (((extid >> 1) & 0x04) | (extid & 0x03));
3057 busw = 0;
3058 } else {
3059 /* Calc pagesize */
3060 mtd->writesize = 1024 << (extid & 0x03);
3061 extid >>= 2;
3062 /* Calc oobsize */
3063 mtd->oobsize = (8 << (extid & 0x01)) *
3064 (mtd->writesize >> 9);
3065 extid >>= 2;
3066 /* Calc blocksize. Blocksize is multiples of 64KiB */
3067 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3068 extid >>= 2;
3069 /* Get buswidth information */
3070 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3071 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003072 } else {
3073 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003074 * Old devices have chip data hardcoded in the device id table.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003075 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003076 mtd->erasesize = type->erasesize;
3077 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02003078 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003079 busw = type->options & NAND_BUSWIDTH_16;
Brian Norris2173bae2010-08-19 08:11:02 -07003080
3081 /*
3082 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3083 * some Spansion chips have erasesize that conflicts with size
Brian Norris8b6e50c2011-05-25 14:59:01 -07003084 * listed in nand_ids table.
Brian Norris2173bae2010-08-19 08:11:02 -07003085 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3086 */
3087 if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
3088 id_data[5] == 0x00 && id_data[6] == 0x00 &&
3089 id_data[7] == 0x00 && mtd->writesize == 512) {
3090 mtd->erasesize = 128 * 1024;
3091 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3092 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003093 }
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003094 /* Get chip options, preserve non chip based options */
3095 chip->options &= ~NAND_CHIPOPTIONS_MSK;
3096 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
3097
Brian Norris8b6e50c2011-05-25 14:59:01 -07003098 /*
3099 * Check if chip is not a Samsung device. Do not clear the
3100 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003101 */
3102 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3103 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3104ident_done:
3105
3106 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003107 * Set chip as a default. Board drivers can override it, if necessary.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003108 */
3109 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003110
3111 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003112 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003113 if (nand_manuf_ids[maf_idx].id == *maf_id)
3114 break;
3115 }
3116
3117 /*
3118 * Check, if buswidth is correct. Hardware drivers should set
Brian Norris8b6e50c2011-05-25 14:59:01 -07003119 * chip correct!
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003120 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003121 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003122 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003123 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3124 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003125 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003126 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3127 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003128 return ERR_PTR(-EINVAL);
3129 }
3130
3131 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003132 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003133 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003134 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003135
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003136 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003137 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003138 if (chip->chipsize & 0xffffffff)
3139 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003140 else {
3141 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3142 chip->chip_shift += 32 - 1;
3143 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003144
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003145 chip->badblockbits = 8;
3146
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003147 /* Set the bad block position */
Brian Norris065a1ed2010-08-18 11:25:04 -07003148 if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
Brian Norrisc7b28e22010-07-13 15:13:00 -07003149 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
Brian Norris065a1ed2010-08-18 11:25:04 -07003150 else
3151 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003152
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07003153 /*
3154 * Bad block marker is stored in the last page of each block
Brian Norrisc7b28e22010-07-13 15:13:00 -07003155 * on Samsung and Hynix MLC devices; stored in first two pages
3156 * of each block on Micron devices with 2KiB pages and on
Brian Norris8c342332011-11-02 13:34:44 -07003157 * SLC Samsung, Hynix, Toshiba, AMD/Spansion, and Macronix.
3158 * All others scan only the first page.
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07003159 */
3160 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3161 (*maf_id == NAND_MFR_SAMSUNG ||
3162 *maf_id == NAND_MFR_HYNIX))
Brian Norris5fb15492011-05-31 16:31:21 -07003163 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07003164 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3165 (*maf_id == NAND_MFR_SAMSUNG ||
3166 *maf_id == NAND_MFR_HYNIX ||
Brian Norris13ed7ae2010-08-20 12:36:12 -07003167 *maf_id == NAND_MFR_TOSHIBA ||
Brian Norris8c342332011-11-02 13:34:44 -07003168 *maf_id == NAND_MFR_AMD ||
3169 *maf_id == NAND_MFR_MACRONIX)) ||
Brian Norrisc7b28e22010-07-13 15:13:00 -07003170 (mtd->writesize == 2048 &&
3171 *maf_id == NAND_MFR_MICRON))
Brian Norris5fb15492011-05-31 16:31:21 -07003172 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07003173
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003174 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003175 if (chip->options & NAND_4PAGE_ARRAY)
3176 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003177 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003178 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003179
Brian Norris8b6e50c2011-05-25 14:59:01 -07003180 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003181 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3182 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003183
Brian Norris9a4d4d62011-07-19 10:06:07 -07003184 pr_info("NAND device: Manufacturer ID:"
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003185 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
3186 nand_manuf_ids[maf_idx].name,
Brian Norris0b524fb2010-12-12 00:23:32 -08003187 chip->onfi_version ? chip->onfi_params.model : type->name);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003188
3189 return type;
3190}
3191
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003192/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003193 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003194 * @mtd: MTD device structure
3195 * @maxchips: number of chips to scan for
3196 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003197 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003198 * This is the first phase of the normal nand_scan() function. It reads the
3199 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003200 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003201 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003202 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003203int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3204 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003205{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003206 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003207 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003208 struct nand_flash_dev *type;
3209
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003210 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003211 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003212 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003213 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003214
3215 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003216 type = nand_get_flash_type(mtd, chip, busw,
3217 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003218
3219 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003220 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003221 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003222 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003223 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 }
3225
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003226 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003227 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003228 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003229 /* See comment in nand_get_flash_type for reset */
3230 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003232 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003234 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003235 nand_dev_id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 break;
3237 }
3238 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003239 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003242 chip->numchips = i;
3243 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
David Woodhouse3b85c322006-09-25 17:06:53 +01003245 return 0;
3246}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003247EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003248
3249
3250/**
3251 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003252 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003253 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003254 * This is the second phase of the normal nand_scan() function. It fills out
3255 * all the uninitialized function pointers with the defaults and scans for a
3256 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003257 */
3258int nand_scan_tail(struct mtd_info *mtd)
3259{
3260 int i;
3261 struct nand_chip *chip = mtd->priv;
3262
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003263 if (!(chip->options & NAND_OWN_BUFFERS))
3264 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3265 if (!chip->buffers)
3266 return -ENOMEM;
3267
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003268 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003269 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003270
3271 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003272 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003273 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003274 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003275 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003277 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 break;
3279 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003280 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 break;
3282 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003283 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003285 case 128:
3286 chip->ecc.layout = &nand_oob_128;
3287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003289 pr_warn("No oob scheme defined for oobsize %d\n",
3290 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 BUG();
3292 }
3293 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003294
David Woodhouse956e9442006-09-25 17:12:39 +01003295 if (!chip->write_page)
3296 chip->write_page = nand_write_page;
3297
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003298 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003299 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003300 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003301 */
David Woodhouse956e9442006-09-25 17:12:39 +01003302
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003303 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003304 case NAND_ECC_HW_OOB_FIRST:
3305 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3306 if (!chip->ecc.calculate || !chip->ecc.correct ||
3307 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003308 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003309 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003310 BUG();
3311 }
3312 if (!chip->ecc.read_page)
3313 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3314
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003315 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003316 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003317 if (!chip->ecc.read_page)
3318 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003319 if (!chip->ecc.write_page)
3320 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003321 if (!chip->ecc.read_page_raw)
3322 chip->ecc.read_page_raw = nand_read_page_raw;
3323 if (!chip->ecc.write_page_raw)
3324 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003325 if (!chip->ecc.read_oob)
3326 chip->ecc.read_oob = nand_read_oob_std;
3327 if (!chip->ecc.write_oob)
3328 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003329
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003330 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003331 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3332 !chip->ecc.hwctl) &&
3333 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003334 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003335 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003336 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003337 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003338 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003339 BUG();
3340 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003341 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003342 if (!chip->ecc.read_page)
3343 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003344 if (!chip->ecc.write_page)
3345 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003346 if (!chip->ecc.read_page_raw)
3347 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3348 if (!chip->ecc.write_page_raw)
3349 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003350 if (!chip->ecc.read_oob)
3351 chip->ecc.read_oob = nand_read_oob_syndrome;
3352 if (!chip->ecc.write_oob)
3353 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003354
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003355 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003356 break;
Brian Norris9a4d4d62011-07-19 10:06:07 -07003357 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003358 "%d byte page size, fallback to SW ECC\n",
3359 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003360 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003362 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003363 chip->ecc.calculate = nand_calculate_ecc;
3364 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003365 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003366 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003367 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003368 chip->ecc.read_page_raw = nand_read_page_raw;
3369 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003370 chip->ecc.read_oob = nand_read_oob_std;
3371 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003372 if (!chip->ecc.size)
3373 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003374 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003376
Ivan Djelic193bd402011-03-11 11:05:33 +01003377 case NAND_ECC_SOFT_BCH:
3378 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003379 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003380 BUG();
3381 }
3382 chip->ecc.calculate = nand_bch_calculate_ecc;
3383 chip->ecc.correct = nand_bch_correct_data;
3384 chip->ecc.read_page = nand_read_page_swecc;
3385 chip->ecc.read_subpage = nand_read_subpage;
3386 chip->ecc.write_page = nand_write_page_swecc;
3387 chip->ecc.read_page_raw = nand_read_page_raw;
3388 chip->ecc.write_page_raw = nand_write_page_raw;
3389 chip->ecc.read_oob = nand_read_oob_std;
3390 chip->ecc.write_oob = nand_write_oob_std;
3391 /*
3392 * Board driver should supply ecc.size and ecc.bytes values to
3393 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003394 * for details. Otherwise, default to 4 bits for large page
3395 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003396 */
3397 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3398 chip->ecc.size = 512;
3399 chip->ecc.bytes = 7;
3400 }
3401 chip->ecc.priv = nand_bch_init(mtd,
3402 chip->ecc.size,
3403 chip->ecc.bytes,
3404 &chip->ecc.layout);
3405 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003406 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003407 BUG();
3408 }
3409 break;
3410
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003411 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003412 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003413 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003414 chip->ecc.read_page = nand_read_page_raw;
3415 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003416 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003417 chip->ecc.read_page_raw = nand_read_page_raw;
3418 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003419 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003420 chip->ecc.size = mtd->writesize;
3421 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003425 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003426 BUG();
3427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Brian Norris9ce244b2011-08-30 18:45:37 -07003429 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003430 if (!chip->ecc.read_oob_raw)
3431 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003432 if (!chip->ecc.write_oob_raw)
3433 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3434
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003435 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003436 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003437 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003438 */
3439 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003440 for (i = 0; chip->ecc.layout->oobfree[i].length
3441 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003442 chip->ecc.layout->oobavail +=
3443 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003444 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003445
3446 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003447 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003448 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003449 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003450 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003451 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003452 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003453 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003455 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003456
Brian Norris8b6e50c2011-05-25 14:59:01 -07003457 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003458 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3459 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003460 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003461 case 2:
3462 mtd->subpage_sft = 1;
3463 break;
3464 case 4:
3465 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003466 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003467 mtd->subpage_sft = 2;
3468 break;
3469 }
3470 }
3471 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3472
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003473 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003474 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003477 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
3479 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003480 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
3482 /* Fill in remaining MTD driver data */
3483 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003484 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3485 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003486 mtd->_erase = nand_erase;
3487 mtd->_point = NULL;
3488 mtd->_unpoint = NULL;
3489 mtd->_read = nand_read;
3490 mtd->_write = nand_write;
3491 mtd->_panic_write = panic_nand_write;
3492 mtd->_read_oob = nand_read_oob;
3493 mtd->_write_oob = nand_write_oob;
3494 mtd->_sync = nand_sync;
3495 mtd->_lock = NULL;
3496 mtd->_unlock = NULL;
3497 mtd->_suspend = nand_suspend;
3498 mtd->_resume = nand_resume;
3499 mtd->_block_isbad = nand_block_isbad;
3500 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003501 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003503 /* propagate ecc.layout to mtd_info */
3504 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003506 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003507 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003511 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003513EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Brian Norris8b6e50c2011-05-25 14:59:01 -07003515/*
3516 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003517 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003518 * to call us from in-kernel code if the core NAND support is modular.
3519 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003520#ifdef MODULE
3521#define caller_is_module() (1)
3522#else
3523#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003524 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003525#endif
3526
3527/**
3528 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003529 * @mtd: MTD device structure
3530 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003531 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003532 * This fills out all the uninitialized function pointers with the defaults.
3533 * The flash ID is read and the mtd/chip structures are filled with the
3534 * appropriate values. The mtd->owner field must be set to the module of the
3535 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003536 */
3537int nand_scan(struct mtd_info *mtd, int maxchips)
3538{
3539 int ret;
3540
3541 /* Many callers got this wrong, so check for it for a while... */
3542 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003543 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003544 BUG();
3545 }
3546
David Woodhouse5e81e882010-02-26 18:32:56 +00003547 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003548 if (!ret)
3549 ret = nand_scan_tail(mtd);
3550 return ret;
3551}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003552EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003555 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003556 * @mtd: MTD device structure
3557 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003558void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003560 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Ivan Djelic193bd402011-03-11 11:05:33 +01003562 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3563 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3564
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003565 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Jesper Juhlfa671642005-11-07 01:01:27 -08003567 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003568 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003569 if (!(chip->options & NAND_OWN_BUFFERS))
3570 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003571
3572 /* Free bad block descriptor memory */
3573 if (chip->badblock_pattern && chip->badblock_pattern->options
3574 & NAND_BBT_DYNAMICSTRUCT)
3575 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
David Woodhousee0c7d762006-05-13 18:07:53 +01003577EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003578
3579static int __init nand_base_init(void)
3580{
3581 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3582 return 0;
3583}
3584
3585static void __exit nand_base_exit(void)
3586{
3587 led_trigger_unregister_simple(nand_led_trigger);
3588}
3589
3590module_init(nand_base_init);
3591module_exit(nand_base_exit);
3592
David Woodhousee0c7d762006-05-13 18:07:53 +01003593MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003594MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3595MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003596MODULE_DESCRIPTION("Generic NAND flash driver code");