blob: 13a56d3e8aec246226c663020f8ecf60c5253f8a [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
Brian Norrise2414f42012-02-06 13:44:00 -0800395 * specific driver. We try operations in the following order, according to our
396 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
397 * (1) erase the affected block, to allow OOB marker to be written cleanly
398 * (2) update in-memory BBT
399 * (3) write bad block marker to OOB area of affected block
400 * (4) update flash-based BBT
401 * Note that we retain the first error encountered in (3) or (4), finish the
402 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403*/
404static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
405{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200406 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200407 uint8_t buf[2] = { 0, 0 };
Brian Norrise2414f42012-02-06 13:44:00 -0800408 int block, res, ret = 0, i = 0;
409 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000410
Brian Norrise2414f42012-02-06 13:44:00 -0800411 if (write_oob) {
Brian Norris00918422012-01-13 18:11:47 -0800412 struct erase_info einfo;
413
414 /* Attempt erase before marking OOB */
415 memset(&einfo, 0, sizeof(einfo));
416 einfo.mtd = mtd;
417 einfo.addr = ofs;
418 einfo.len = 1 << chip->phys_erase_shift;
419 nand_erase_nand(mtd, &einfo, 0);
420 }
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400423 block = (int)(ofs >> chip->bbt_erase_shift);
Brian Norrise2414f42012-02-06 13:44:00 -0800424 /* Mark block bad in memory-based BBT */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200425 if (chip->bbt)
426 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Brian Norrise2414f42012-02-06 13:44:00 -0800428 /* Write bad block marker to OOB */
429 if (write_oob) {
Brian Norris4a89ff82011-08-30 18:45:45 -0700430 struct mtd_oob_ops ops;
Brian Norrisdf698622012-01-20 20:38:03 -0800431 loff_t wr_ofs = ofs;
Brian Norris4a89ff82011-08-30 18:45:45 -0700432
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300433 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000434
Brian Norris4a89ff82011-08-30 18:45:45 -0700435 ops.datbuf = NULL;
436 ops.oobbuf = buf;
Brian Norris85443312012-01-13 18:11:49 -0800437 ops.ooboffs = chip->badblockpos;
438 if (chip->options & NAND_BUSWIDTH_16) {
439 ops.ooboffs &= ~0x01;
440 ops.len = ops.ooblen = 2;
441 } else {
442 ops.len = ops.ooblen = 1;
443 }
Brian Norris23b1a992011-10-14 20:09:33 -0700444 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norrisdf698622012-01-20 20:38:03 -0800445
Brian Norrise2414f42012-02-06 13:44:00 -0800446 /* Write to first/last page(s) if necessary */
Brian Norrisdf698622012-01-20 20:38:03 -0800447 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
448 wr_ofs += mtd->erasesize - mtd->writesize;
Brian Norris02ed70b2010-07-21 16:53:47 -0700449 do {
Brian Norrise2414f42012-02-06 13:44:00 -0800450 res = nand_do_write_oob(mtd, wr_ofs, &ops);
451 if (!ret)
452 ret = res;
Brian Norris02ed70b2010-07-21 16:53:47 -0700453
Brian Norris02ed70b2010-07-21 16:53:47 -0700454 i++;
Brian Norrisdf698622012-01-20 20:38:03 -0800455 wr_ofs += mtd->writesize;
Brian Norrise2414f42012-02-06 13:44:00 -0800456 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Brian Norris02ed70b2010-07-21 16:53:47 -0700457
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300458 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200459 }
Brian Norrise2414f42012-02-06 13:44:00 -0800460
461 /* Update flash-based bad block table */
462 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
463 res = nand_update_bbt(mtd, ofs);
464 if (!ret)
465 ret = res;
466 }
467
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200468 if (!ret)
469 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300470
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000474/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700476 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700478 * Check, if the device is write protected. The function expects, that the
479 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100481static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200483 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200484
Brian Norris8b6e50c2011-05-25 14:59:01 -0700485 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200486 if (chip->options & NAND_BROKEN_XD)
487 return 0;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200490 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
491 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/**
495 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700496 * @mtd: MTD device structure
497 * @ofs: offset from device start
498 * @getchip: 0, if the chip is already selected
499 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *
501 * Check, if the block is bad. Either by reading the bad block table or
502 * calling of the scan function.
503 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200504static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
505 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200507 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000508
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 if (!chip->bbt)
510 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100513 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200516/**
517 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700518 * @mtd: MTD device structure
519 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200520 *
521 * Helper function for nand_wait_ready used when needing to wait in interrupt
522 * context.
523 */
524static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
525{
526 struct nand_chip *chip = mtd->priv;
527 int i;
528
529 /* Wait for the device to get ready */
530 for (i = 0; i < timeo; i++) {
531 if (chip->dev_ready(mtd))
532 break;
533 touch_softlockup_watchdog();
534 mdelay(1);
535 }
536}
537
Brian Norris7854d3f2011-06-23 14:12:08 -0700538/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100539void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000540{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200541 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100542 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000543
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200544 /* 400ms timeout */
545 if (in_interrupt() || oops_in_progress)
546 return panic_nand_wait_ready(mtd, 400);
547
Richard Purdie8fe833c2006-03-31 02:31:14 -0800548 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700549 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000550 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200551 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800552 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700553 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000554 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800555 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000556}
David Woodhouse4b648b02006-09-25 17:05:24 +0100557EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/**
560 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700561 * @mtd: MTD device structure
562 * @command: the command to be sent
563 * @column: the column address for this command, -1 if none
564 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700566 * Send command to NAND device. This function is used for small page devices
567 * (256/512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200569static void nand_command(struct mtd_info *mtd, unsigned int command,
570 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200572 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200573 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Brian Norris8b6e50c2011-05-25 14:59:01 -0700575 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (command == NAND_CMD_SEQIN) {
577 int readcmd;
578
Joern Engel28318772006-05-22 23:18:05 +0200579 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200581 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 readcmd = NAND_CMD_READOOB;
583 } else if (column < 256) {
584 /* First 256 bytes --> READ0 */
585 readcmd = NAND_CMD_READ0;
586 } else {
587 column -= 256;
588 readcmd = NAND_CMD_READ1;
589 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200590 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200591 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200593 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200596 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
597 /* Serially input address */
598 if (column != -1) {
599 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200600 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200601 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200603 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200605 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200606 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200607 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200608 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200609 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200610 if (chip->chipsize > (32 << 20))
611 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200612 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000614
615 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700616 * Program and erase have their own busy handlers status and sequential
617 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 case NAND_CMD_PAGEPROG:
622 case NAND_CMD_ERASE1:
623 case NAND_CMD_ERASE2:
624 case NAND_CMD_SEQIN:
625 case NAND_CMD_STATUS:
626 return;
627
628 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200629 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200631 udelay(chip->chip_delay);
632 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200634 chip->cmd_ctrl(mtd,
635 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200636 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
637 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return;
639
David Woodhousee0c7d762006-05-13 18:07:53 +0100640 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000642 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * If we don't have access to the busy pin, we apply the given
644 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100645 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200646 if (!chip->dev_ready) {
647 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700651 /*
652 * Apply this short delay always to ensure that we do wait tWB in
653 * any case on any machine.
654 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000656
657 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/**
661 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700662 * @mtd: MTD device structure
663 * @command: the command to be sent
664 * @column: the column address for this command, -1 if none
665 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200667 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700668 * devices. We don't have the separate regions as we have in the small page
669 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200671static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
672 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200674 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* Emulate NAND_CMD_READOOB */
677 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200678 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 command = NAND_CMD_READ0;
680 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000681
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200682 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200684 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200687 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 /* Serially input address */
690 if (column != -1) {
691 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200692 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200694 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200695 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200696 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200699 chip->cmd_ctrl(mtd, page_addr, ctrl);
700 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200701 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200703 if (chip->chipsize > (128 << 20))
704 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200708 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
710 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700711 * Program and erase have their own busy handlers status, sequential
712 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case NAND_CMD_CACHEDPROG:
717 case NAND_CMD_PAGEPROG:
718 case NAND_CMD_ERASE1:
719 case NAND_CMD_ERASE2:
720 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200721 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000723 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return;
725
David A. Marlin30f464b2005-01-17 18:35:25 +0000726 case NAND_CMD_STATUS_ERROR:
727 case NAND_CMD_STATUS_ERROR0:
728 case NAND_CMD_STATUS_ERROR1:
729 case NAND_CMD_STATUS_ERROR2:
730 case NAND_CMD_STATUS_ERROR3:
Brian Norris8b6e50c2011-05-25 14:59:01 -0700731 /* Read error status commands require only a short delay */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200732 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000733 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200739 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
740 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
741 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
742 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200743 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
744 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return;
746
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200747 case NAND_CMD_RNDOUT:
748 /* No ready / busy check necessary */
749 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
750 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
751 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
752 NAND_NCE | NAND_CTRL_CHANGE);
753 return;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200756 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
757 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
758 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
759 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000760
David Woodhousee0c7d762006-05-13 18:07:53 +0100761 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000763 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700765 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100766 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 if (!chip->dev_ready) {
768 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000772
Brian Norris8b6e50c2011-05-25 14:59:01 -0700773 /*
774 * Apply this short delay always to ensure that we do wait tWB in
775 * any case on any machine.
776 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100777 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000778
779 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200783 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 * @chip: the nand chip descriptor
785 * @mtd: MTD device structure
786 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200787 *
788 * Used when in panic, no locks are taken.
789 */
790static void panic_nand_get_device(struct nand_chip *chip,
791 struct mtd_info *mtd, int new_state)
792{
Brian Norris7854d3f2011-06-23 14:12:08 -0700793 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200794 chip->controller->active = chip;
795 chip->state = new_state;
796}
797
798/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700800 * @chip: the nand chip descriptor
801 * @mtd: MTD device structure
802 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
804 * Get the device and lock it for exclusive access
805 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200806static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200807nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 spinlock_t *lock = &chip->controller->lock;
810 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200812retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100813 spin_lock(lock);
814
vimal singhb8b3ee92009-07-09 20:41:22 +0530815 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200816 if (!chip->controller->active)
817 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200818
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200819 if (chip->controller->active == chip && chip->state == FL_READY) {
820 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100821 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100822 return 0;
823 }
824 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800825 if (chip->controller->active->state == FL_PM_SUSPENDED) {
826 chip->state = FL_PM_SUSPENDED;
827 spin_unlock(lock);
828 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800829 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100830 }
831 set_current_state(TASK_UNINTERRUPTIBLE);
832 add_wait_queue(wq, &wait);
833 spin_unlock(lock);
834 schedule();
835 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto retry;
837}
838
839/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700840 * panic_nand_wait - [GENERIC] wait until the command is done
841 * @mtd: MTD device structure
842 * @chip: NAND chip structure
843 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200844 *
845 * Wait for command done. This is a helper function for nand_wait used when
846 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400847 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200848 */
849static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
850 unsigned long timeo)
851{
852 int i;
853 for (i = 0; i < timeo; i++) {
854 if (chip->dev_ready) {
855 if (chip->dev_ready(mtd))
856 break;
857 } else {
858 if (chip->read_byte(mtd) & NAND_STATUS_READY)
859 break;
860 }
861 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200862 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200863}
864
865/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700866 * nand_wait - [DEFAULT] wait until the command is done
867 * @mtd: MTD device structure
868 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700870 * Wait for command done. This applies to erase and program only. Erase can
871 * take up to 400ms and program up to 20ms according to general NAND and
872 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700873 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200874static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876
David Woodhousee0c7d762006-05-13 18:07:53 +0100877 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200878 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100881 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100883 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Richard Purdie8fe833c2006-03-31 02:31:14 -0800885 led_trigger_event(nand_led_trigger, LED_FULL);
886
Brian Norris8b6e50c2011-05-25 14:59:01 -0700887 /*
888 * Apply this short delay always to ensure that we do wait tWB in any
889 * case on any machine.
890 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100891 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200893 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
894 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000895 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200896 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200898 if (in_interrupt() || oops_in_progress)
899 panic_nand_wait(mtd, chip, timeo);
900 else {
901 while (time_before(jiffies, timeo)) {
902 if (chip->dev_ready) {
903 if (chip->dev_ready(mtd))
904 break;
905 } else {
906 if (chip->read_byte(mtd) & NAND_STATUS_READY)
907 break;
908 }
909 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800912 led_trigger_event(nand_led_trigger, LED_OFF);
913
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200914 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return status;
916}
917
918/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700919 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700920 * @mtd: mtd info
921 * @ofs: offset to start unlock from
922 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700923 * @invert: when = 0, unlock the range of blocks within the lower and
924 * upper boundary address
925 * when = 1, unlock the range of blocks outside the boundaries
926 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530927 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700928 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530929 */
930static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
931 uint64_t len, int invert)
932{
933 int ret = 0;
934 int status, page;
935 struct nand_chip *chip = mtd->priv;
936
937 /* Submit address of first page to unlock */
938 page = ofs >> chip->page_shift;
939 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
940
941 /* Submit address of last page to unlock */
942 page = (ofs + len) >> chip->page_shift;
943 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
944 (page | invert) & chip->pagemask);
945
946 /* Call wait ready function */
947 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530948 /* See if device thinks it succeeded */
949 if (status & 0x01) {
Brian Norris289c0522011-07-19 10:06:09 -0700950 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530951 __func__, status);
952 ret = -EIO;
953 }
954
955 return ret;
956}
957
958/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700959 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700960 * @mtd: mtd info
961 * @ofs: offset to start unlock from
962 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530963 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700964 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530965 */
966int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
967{
968 int ret = 0;
969 int chipnr;
970 struct nand_chip *chip = mtd->priv;
971
Brian Norris289c0522011-07-19 10:06:09 -0700972 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530973 __func__, (unsigned long long)ofs, len);
974
975 if (check_offs_len(mtd, ofs, len))
976 ret = -EINVAL;
977
978 /* Align to last block address if size addresses end of the device */
979 if (ofs + len == mtd->size)
980 len -= mtd->erasesize;
981
982 nand_get_device(chip, mtd, FL_UNLOCKING);
983
984 /* Shift to get chip number */
985 chipnr = ofs >> chip->chip_shift;
986
987 chip->select_chip(mtd, chipnr);
988
989 /* Check, if it is write protected */
990 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700991 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530992 __func__);
993 ret = -EIO;
994 goto out;
995 }
996
997 ret = __nand_unlock(mtd, ofs, len, 0);
998
999out:
Vimal Singh7d70f332010-02-08 15:50:49 +05301000 nand_release_device(mtd);
1001
1002 return ret;
1003}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001004EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301005
1006/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001007 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001008 * @mtd: mtd info
1009 * @ofs: offset to start unlock from
1010 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301011 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001012 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1013 * have this feature, but it allows only to lock all blocks, not for specified
1014 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1015 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301016 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001017 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301018 */
1019int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1020{
1021 int ret = 0;
1022 int chipnr, status, page;
1023 struct nand_chip *chip = mtd->priv;
1024
Brian Norris289c0522011-07-19 10:06:09 -07001025 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301026 __func__, (unsigned long long)ofs, len);
1027
1028 if (check_offs_len(mtd, ofs, len))
1029 ret = -EINVAL;
1030
1031 nand_get_device(chip, mtd, FL_LOCKING);
1032
1033 /* Shift to get chip number */
1034 chipnr = ofs >> chip->chip_shift;
1035
1036 chip->select_chip(mtd, chipnr);
1037
1038 /* Check, if it is write protected */
1039 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001040 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301041 __func__);
1042 status = MTD_ERASE_FAILED;
1043 ret = -EIO;
1044 goto out;
1045 }
1046
1047 /* Submit address of first page to lock */
1048 page = ofs >> chip->page_shift;
1049 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1050
1051 /* Call wait ready function */
1052 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301053 /* See if device thinks it succeeded */
1054 if (status & 0x01) {
Brian Norris289c0522011-07-19 10:06:09 -07001055 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301056 __func__, status);
1057 ret = -EIO;
1058 goto out;
1059 }
1060
1061 ret = __nand_unlock(mtd, ofs, len, 0x1);
1062
1063out:
Vimal Singh7d70f332010-02-08 15:50:49 +05301064 nand_release_device(mtd);
1065
1066 return ret;
1067}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001068EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301069
1070/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001071 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001072 * @mtd: mtd info structure
1073 * @chip: nand chip info structure
1074 * @buf: buffer to store read data
1075 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001076 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001077 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001078 */
1079static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001080 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001081{
1082 chip->read_buf(mtd, buf, mtd->writesize);
1083 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1084 return 0;
1085}
1086
1087/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001088 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001089 * @mtd: mtd info structure
1090 * @chip: nand chip info structure
1091 * @buf: buffer to store read data
1092 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001093 *
1094 * We need a special oob layout and handling even when OOB isn't used.
1095 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001096static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1097 struct nand_chip *chip,
1098 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001099{
1100 int eccsize = chip->ecc.size;
1101 int eccbytes = chip->ecc.bytes;
1102 uint8_t *oob = chip->oob_poi;
1103 int steps, size;
1104
1105 for (steps = chip->ecc.steps; steps > 0; steps--) {
1106 chip->read_buf(mtd, buf, eccsize);
1107 buf += eccsize;
1108
1109 if (chip->ecc.prepad) {
1110 chip->read_buf(mtd, oob, chip->ecc.prepad);
1111 oob += chip->ecc.prepad;
1112 }
1113
1114 chip->read_buf(mtd, oob, eccbytes);
1115 oob += eccbytes;
1116
1117 if (chip->ecc.postpad) {
1118 chip->read_buf(mtd, oob, chip->ecc.postpad);
1119 oob += chip->ecc.postpad;
1120 }
1121 }
1122
1123 size = mtd->oobsize - (oob - chip->oob_poi);
1124 if (size)
1125 chip->read_buf(mtd, oob, size);
1126
1127 return 0;
1128}
1129
1130/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001131 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001132 * @mtd: mtd info structure
1133 * @chip: nand chip info structure
1134 * @buf: buffer to store read data
1135 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001136 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001137static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001138 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001140 int i, eccsize = chip->ecc.size;
1141 int eccbytes = chip->ecc.bytes;
1142 int eccsteps = chip->ecc.steps;
1143 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001144 uint8_t *ecc_calc = chip->buffers->ecccalc;
1145 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001146 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001147
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001148 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001149
1150 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1151 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1152
1153 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001154 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001155
1156 eccsteps = chip->ecc.steps;
1157 p = buf;
1158
1159 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1160 int stat;
1161
1162 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001163 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001164 mtd->ecc_stats.failed++;
1165 else
1166 mtd->ecc_stats.corrected += stat;
1167 }
1168 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001169}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001172 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001173 * @mtd: mtd info structure
1174 * @chip: nand chip info structure
1175 * @data_offs: offset of requested data within the page
1176 * @readlen: data length
1177 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001178 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001179static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1180 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001181{
1182 int start_step, end_step, num_steps;
1183 uint32_t *eccpos = chip->ecc.layout->eccpos;
1184 uint8_t *p;
1185 int data_col_addr, i, gaps = 0;
1186 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1187 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001188 int index = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001189
Brian Norris7854d3f2011-06-23 14:12:08 -07001190 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001191 start_step = data_offs / chip->ecc.size;
1192 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1193 num_steps = end_step - start_step + 1;
1194
Brian Norris8b6e50c2011-05-25 14:59:01 -07001195 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001196 datafrag_len = num_steps * chip->ecc.size;
1197 eccfrag_len = num_steps * chip->ecc.bytes;
1198
1199 data_col_addr = start_step * chip->ecc.size;
1200 /* If we read not a page aligned data */
1201 if (data_col_addr != 0)
1202 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1203
1204 p = bufpoi + data_col_addr;
1205 chip->read_buf(mtd, p, datafrag_len);
1206
Brian Norris8b6e50c2011-05-25 14:59:01 -07001207 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001208 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1209 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1210
Brian Norris8b6e50c2011-05-25 14:59:01 -07001211 /*
1212 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001213 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001214 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001215 for (i = 0; i < eccfrag_len - 1; i++) {
1216 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1217 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1218 gaps = 1;
1219 break;
1220 }
1221 }
1222 if (gaps) {
1223 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1224 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1225 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001226 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001227 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 * about buswidth alignment in read_buf.
1229 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001230 index = start_step * chip->ecc.bytes;
1231
1232 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001233 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001234 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001235 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001236 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001237 aligned_len++;
1238
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001239 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1240 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001241 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1242 }
1243
1244 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001245 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001246
1247 p = bufpoi + data_col_addr;
1248 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1249 int stat;
1250
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001251 stat = chip->ecc.correct(mtd, p,
1252 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Baruch Siach12c8eb92010-08-09 07:20:23 +03001253 if (stat < 0)
Alexey Korolev3d459552008-05-15 17:23:18 +01001254 mtd->ecc_stats.failed++;
1255 else
1256 mtd->ecc_stats.corrected += stat;
1257 }
1258 return 0;
1259}
1260
1261/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001262 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001263 * @mtd: mtd info structure
1264 * @chip: nand chip info structure
1265 * @buf: buffer to store read data
1266 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001267 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001268 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001269 */
1270static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001271 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001272{
1273 int i, eccsize = chip->ecc.size;
1274 int eccbytes = chip->ecc.bytes;
1275 int eccsteps = chip->ecc.steps;
1276 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001277 uint8_t *ecc_calc = chip->buffers->ecccalc;
1278 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001279 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001280
1281 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1282 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1283 chip->read_buf(mtd, p, eccsize);
1284 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1285 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001286 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001287
1288 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001289 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001290
1291 eccsteps = chip->ecc.steps;
1292 p = buf;
1293
1294 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1295 int stat;
1296
1297 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001298 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001299 mtd->ecc_stats.failed++;
1300 else
1301 mtd->ecc_stats.corrected += stat;
1302 }
1303 return 0;
1304}
1305
1306/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001307 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001308 * @mtd: mtd info structure
1309 * @chip: nand chip info structure
1310 * @buf: buffer to store read data
1311 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001312 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001313 * Hardware ECC for large page chips, require OOB to be read first. For this
1314 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1315 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1316 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1317 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001318 */
1319static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1320 struct nand_chip *chip, uint8_t *buf, int page)
1321{
1322 int i, eccsize = chip->ecc.size;
1323 int eccbytes = chip->ecc.bytes;
1324 int eccsteps = chip->ecc.steps;
1325 uint8_t *p = buf;
1326 uint8_t *ecc_code = chip->buffers->ecccode;
1327 uint32_t *eccpos = chip->ecc.layout->eccpos;
1328 uint8_t *ecc_calc = chip->buffers->ecccalc;
1329
1330 /* Read the OOB area first */
1331 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1332 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1333 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1334
1335 for (i = 0; i < chip->ecc.total; i++)
1336 ecc_code[i] = chip->oob_poi[eccpos[i]];
1337
1338 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1339 int stat;
1340
1341 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1342 chip->read_buf(mtd, p, eccsize);
1343 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1344
1345 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1346 if (stat < 0)
1347 mtd->ecc_stats.failed++;
1348 else
1349 mtd->ecc_stats.corrected += stat;
1350 }
1351 return 0;
1352}
1353
1354/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001355 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001356 * @mtd: mtd info structure
1357 * @chip: nand chip info structure
1358 * @buf: buffer to store read data
1359 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001360 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 * The hw generator calculates the error syndrome automatically. Therefore we
1362 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001363 */
1364static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001365 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001366{
1367 int i, eccsize = chip->ecc.size;
1368 int eccbytes = chip->ecc.bytes;
1369 int eccsteps = chip->ecc.steps;
1370 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001371 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001372
1373 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1374 int stat;
1375
1376 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1377 chip->read_buf(mtd, p, eccsize);
1378
1379 if (chip->ecc.prepad) {
1380 chip->read_buf(mtd, oob, chip->ecc.prepad);
1381 oob += chip->ecc.prepad;
1382 }
1383
1384 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1385 chip->read_buf(mtd, oob, eccbytes);
1386 stat = chip->ecc.correct(mtd, p, oob, NULL);
1387
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001388 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001389 mtd->ecc_stats.failed++;
1390 else
1391 mtd->ecc_stats.corrected += stat;
1392
1393 oob += eccbytes;
1394
1395 if (chip->ecc.postpad) {
1396 chip->read_buf(mtd, oob, chip->ecc.postpad);
1397 oob += chip->ecc.postpad;
1398 }
1399 }
1400
1401 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001402 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001403 if (i)
1404 chip->read_buf(mtd, oob, i);
1405
1406 return 0;
1407}
1408
1409/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001410 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001411 * @chip: nand chip structure
1412 * @oob: oob destination address
1413 * @ops: oob ops structure
1414 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001415 */
1416static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001417 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001418{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001419 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001420
Brian Norris0612b9d2011-08-30 18:45:40 -07001421 case MTD_OPS_PLACE_OOB:
1422 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001423 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1424 return oob + len;
1425
Brian Norris0612b9d2011-08-30 18:45:40 -07001426 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001427 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001428 uint32_t boffs = 0, roffs = ops->ooboffs;
1429 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001430
Florian Fainellif8ac0412010-09-07 13:23:43 +02001431 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001432 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001433 if (unlikely(roffs)) {
1434 if (roffs >= free->length) {
1435 roffs -= free->length;
1436 continue;
1437 }
1438 boffs = free->offset + roffs;
1439 bytes = min_t(size_t, len,
1440 (free->length - roffs));
1441 roffs = 0;
1442 } else {
1443 bytes = min_t(size_t, len, free->length);
1444 boffs = free->offset;
1445 }
1446 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001447 oob += bytes;
1448 }
1449 return oob;
1450 }
1451 default:
1452 BUG();
1453 }
1454 return NULL;
1455}
1456
1457/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001458 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001459 * @mtd: MTD device structure
1460 * @from: offset to read from
1461 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001462 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001463 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001464 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001465static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1466 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001467{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001468 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001469 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001470 struct mtd_ecc_stats stats;
1471 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1472 int sndcmd = 1;
1473 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001474 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001475 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001476 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001477 mtd->oobavail : mtd->oobsize;
1478
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001479 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001481 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001483 chipnr = (int)(from >> chip->chip_shift);
1484 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001486 realpage = (int)(from >> chip->page_shift);
1487 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001489 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001491 buf = ops->datbuf;
1492 oob = ops->oobbuf;
1493
Florian Fainellif8ac0412010-09-07 13:23:43 +02001494 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001495 bytes = min(mtd->writesize - col, readlen);
1496 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001497
Brian Norris8b6e50c2011-05-25 14:59:01 -07001498 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001499 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001500 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001502 if (likely(sndcmd)) {
1503 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1504 sndcmd = 0;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001507 /* Now read the page into the buffer */
Brian Norris0612b9d2011-08-30 18:45:40 -07001508 if (unlikely(ops->mode == MTD_OPS_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001509 ret = chip->ecc.read_page_raw(mtd, chip,
1510 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001511 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001512 ret = chip->ecc.read_subpage(mtd, chip,
1513 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001514 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001515 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1516 page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001517 if (ret < 0) {
1518 if (!aligned)
1519 /* Invalidate page cache */
1520 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001521 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001522 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001523
1524 /* Transfer not aligned data */
1525 if (!aligned) {
Artem Bityutskiyc1194c72010-09-03 22:01:16 +03001526 if (!NAND_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001527 !(mtd->ecc_stats.failed - stats.failed) &&
1528 (ops->mode != MTD_OPS_RAW))
Alexey Korolev3d459552008-05-15 17:23:18 +01001529 chip->pagebuf = realpage;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001530 else
1531 /* Invalidate page cache */
1532 chip->pagebuf = -1;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001533 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001535
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001536 buf += bytes;
1537
1538 if (unlikely(oob)) {
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001539
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001540 int toread = min(oobreadlen, max_oobsize);
1541
1542 if (toread) {
1543 oob = nand_transfer_oob(chip,
1544 oob, ops, toread);
1545 oobreadlen -= toread;
1546 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001547 }
1548
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001549 if (!(chip->options & NAND_NO_READRDY)) {
1550 /*
1551 * Apply delay or wait for ready/busy pin. Do
1552 * this before the AUTOINCR check, so no
1553 * problems arise if a chip which does auto
1554 * increment is marked as NOAUTOINCR by the
1555 * board driver.
1556 */
1557 if (!chip->dev_ready)
1558 udelay(chip->chip_delay);
1559 else
1560 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001562 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001563 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001564 buf += bytes;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001567 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001568
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001569 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Brian Norris8b6e50c2011-05-25 14:59:01 -07001572 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 col = 0;
1574 /* Increment page address */
1575 realpage++;
1576
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001577 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /* Check, if we cross a chip boundary */
1579 if (!page) {
1580 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001581 chip->select_chip(mtd, -1);
1582 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001584
Brian Norris8b6e50c2011-05-25 14:59:01 -07001585 /*
1586 * Check, if the chip supports auto page increment or if we
1587 * have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001588 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001589 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001590 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001593 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001594 if (oob)
1595 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001597 if (ret)
1598 return ret;
1599
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001600 if (mtd->ecc_stats.failed - stats.failed)
1601 return -EBADMSG;
1602
1603 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001604}
1605
1606/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001607 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001608 * @mtd: MTD device structure
1609 * @from: offset to read from
1610 * @len: number of bytes to read
1611 * @retlen: pointer to variable to store the number of read bytes
1612 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001613 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001614 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001615 */
1616static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1617 size_t *retlen, uint8_t *buf)
1618{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001619 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07001620 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001621 int ret;
1622
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001623 /* Do not allow reads past end of device */
1624 if ((from + len) > mtd->size)
1625 return -EINVAL;
1626 if (!len)
1627 return 0;
1628
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001629 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001630
Brian Norris4a89ff82011-08-30 18:45:45 -07001631 ops.len = len;
1632 ops.datbuf = buf;
1633 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07001634 ops.mode = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001635
Brian Norris4a89ff82011-08-30 18:45:45 -07001636 ret = nand_do_read_ops(mtd, from, &ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001637
Brian Norris4a89ff82011-08-30 18:45:45 -07001638 *retlen = ops.retlen;
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001639
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001640 nand_release_device(mtd);
1641
1642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001646 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001647 * @mtd: mtd info structure
1648 * @chip: nand chip info structure
1649 * @page: page number to read
1650 * @sndcmd: flag whether to issue read command or not
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001651 */
1652static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1653 int page, int sndcmd)
1654{
1655 if (sndcmd) {
1656 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1657 sndcmd = 0;
1658 }
1659 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1660 return sndcmd;
1661}
1662
1663/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001664 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001665 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001666 * @mtd: mtd info structure
1667 * @chip: nand chip info structure
1668 * @page: page number to read
1669 * @sndcmd: flag whether to issue read command or not
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001670 */
1671static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1672 int page, int sndcmd)
1673{
1674 uint8_t *buf = chip->oob_poi;
1675 int length = mtd->oobsize;
1676 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1677 int eccsize = chip->ecc.size;
1678 uint8_t *bufpoi = buf;
1679 int i, toread, sndrnd = 0, pos;
1680
1681 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1682 for (i = 0; i < chip->ecc.steps; i++) {
1683 if (sndrnd) {
1684 pos = eccsize + i * (eccsize + chunk);
1685 if (mtd->writesize > 512)
1686 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1687 else
1688 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1689 } else
1690 sndrnd = 1;
1691 toread = min_t(int, length, chunk);
1692 chip->read_buf(mtd, bufpoi, toread);
1693 bufpoi += toread;
1694 length -= toread;
1695 }
1696 if (length > 0)
1697 chip->read_buf(mtd, bufpoi, length);
1698
1699 return 1;
1700}
1701
1702/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001703 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001704 * @mtd: mtd info structure
1705 * @chip: nand chip info structure
1706 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001707 */
1708static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1709 int page)
1710{
1711 int status = 0;
1712 const uint8_t *buf = chip->oob_poi;
1713 int length = mtd->oobsize;
1714
1715 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1716 chip->write_buf(mtd, buf, length);
1717 /* Send command to program the OOB data */
1718 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1719
1720 status = chip->waitfunc(mtd, chip);
1721
Savin Zlobec0d420f92006-06-21 11:51:20 +02001722 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001723}
1724
1725/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001726 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001727 * with syndrome - only for large page flash
1728 * @mtd: mtd info structure
1729 * @chip: nand chip info structure
1730 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001731 */
1732static int nand_write_oob_syndrome(struct mtd_info *mtd,
1733 struct nand_chip *chip, int page)
1734{
1735 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1736 int eccsize = chip->ecc.size, length = mtd->oobsize;
1737 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1738 const uint8_t *bufpoi = chip->oob_poi;
1739
1740 /*
1741 * data-ecc-data-ecc ... ecc-oob
1742 * or
1743 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1744 */
1745 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1746 pos = steps * (eccsize + chunk);
1747 steps = 0;
1748 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001749 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001750
1751 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1752 for (i = 0; i < steps; i++) {
1753 if (sndcmd) {
1754 if (mtd->writesize <= 512) {
1755 uint32_t fill = 0xFFFFFFFF;
1756
1757 len = eccsize;
1758 while (len > 0) {
1759 int num = min_t(int, len, 4);
1760 chip->write_buf(mtd, (uint8_t *)&fill,
1761 num);
1762 len -= num;
1763 }
1764 } else {
1765 pos = eccsize + i * (eccsize + chunk);
1766 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1767 }
1768 } else
1769 sndcmd = 1;
1770 len = min_t(int, length, chunk);
1771 chip->write_buf(mtd, bufpoi, len);
1772 bufpoi += len;
1773 length -= len;
1774 }
1775 if (length > 0)
1776 chip->write_buf(mtd, bufpoi, length);
1777
1778 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1779 status = chip->waitfunc(mtd, chip);
1780
1781 return status & NAND_STATUS_FAIL ? -EIO : 0;
1782}
1783
1784/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001785 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001786 * @mtd: MTD device structure
1787 * @from: offset to read from
1788 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001790 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001792static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1793 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001795 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001796 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001797 struct mtd_ecc_stats stats;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001798 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001799 int readlen = ops->ooblen;
1800 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001801 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Brian Norris289c0522011-07-19 10:06:09 -07001803 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301804 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Brian Norris041e4572011-06-23 16:45:24 -07001806 stats = mtd->ecc_stats;
1807
Brian Norris0612b9d2011-08-30 18:45:40 -07001808 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001809 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001810 else
1811 len = mtd->oobsize;
1812
1813 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001814 pr_debug("%s: attempt to start read outside oob\n",
1815 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001816 return -EINVAL;
1817 }
1818
1819 /* Do not allow reads past end of device */
1820 if (unlikely(from >= mtd->size ||
1821 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1822 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001823 pr_debug("%s: attempt to read beyond end of device\n",
1824 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001825 return -EINVAL;
1826 }
Vitaly Wool70145682006-11-03 18:20:38 +03001827
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001828 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001829 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001831 /* Shift to get page */
1832 realpage = (int)(from >> chip->page_shift);
1833 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Florian Fainellif8ac0412010-09-07 13:23:43 +02001835 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001836 if (ops->mode == MTD_OPS_RAW)
Brian Norrisc46f6482011-08-30 18:45:38 -07001837 sndcmd = chip->ecc.read_oob_raw(mtd, chip, page, sndcmd);
1838 else
1839 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001840
1841 len = min(len, readlen);
1842 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001843
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001844 if (!(chip->options & NAND_NO_READRDY)) {
1845 /*
1846 * Apply delay or wait for ready/busy pin. Do this
1847 * before the AUTOINCR check, so no problems arise if a
1848 * chip which does auto increment is marked as
1849 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001850 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001851 if (!chip->dev_ready)
1852 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001853 else
1854 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001856
Vitaly Wool70145682006-11-03 18:20:38 +03001857 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001858 if (!readlen)
1859 break;
1860
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001861 /* Increment page address */
1862 realpage++;
1863
1864 page = realpage & chip->pagemask;
1865 /* Check, if we cross a chip boundary */
1866 if (!page) {
1867 chipnr++;
1868 chip->select_chip(mtd, -1);
1869 chip->select_chip(mtd, chipnr);
1870 }
1871
Brian Norris8b6e50c2011-05-25 14:59:01 -07001872 /*
1873 * Check, if the chip supports auto page increment or if we
1874 * have hit a block boundary.
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001875 */
1876 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1877 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Vitaly Wool70145682006-11-03 18:20:38 +03001880 ops->oobretlen = ops->ooblen;
Brian Norris041e4572011-06-23 16:45:24 -07001881
1882 if (mtd->ecc_stats.failed - stats.failed)
1883 return -EBADMSG;
1884
1885 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
1888/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001889 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001890 * @mtd: MTD device structure
1891 * @from: offset to read from
1892 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001894 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001896static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1897 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001899 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001900 int ret = -ENOTSUPP;
1901
1902 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001905 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001906 pr_debug("%s: attempt to read beyond end of device\n",
1907 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return -EINVAL;
1909 }
1910
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001911 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Florian Fainellif8ac0412010-09-07 13:23:43 +02001913 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001914 case MTD_OPS_PLACE_OOB:
1915 case MTD_OPS_AUTO_OOB:
1916 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001917 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001918
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001919 default:
1920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923 if (!ops->datbuf)
1924 ret = nand_do_read_oob(mtd, from, ops);
1925 else
1926 ret = nand_do_read_ops(mtd, from, ops);
1927
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001928out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930 return ret;
1931}
1932
1933
1934/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001935 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001936 * @mtd: mtd info structure
1937 * @chip: nand chip info structure
1938 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001939 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001940 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941 */
1942static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1943 const uint8_t *buf)
1944{
1945 chip->write_buf(mtd, buf, mtd->writesize);
1946 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001949/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001950 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001951 * @mtd: mtd info structure
1952 * @chip: nand chip info structure
1953 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001954 *
1955 * We need a special oob layout and handling even when ECC isn't checked.
1956 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001957static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
1958 struct nand_chip *chip,
1959 const uint8_t *buf)
David Brownell52ff49d2009-03-04 12:01:36 -08001960{
1961 int eccsize = chip->ecc.size;
1962 int eccbytes = chip->ecc.bytes;
1963 uint8_t *oob = chip->oob_poi;
1964 int steps, size;
1965
1966 for (steps = chip->ecc.steps; steps > 0; steps--) {
1967 chip->write_buf(mtd, buf, eccsize);
1968 buf += eccsize;
1969
1970 if (chip->ecc.prepad) {
1971 chip->write_buf(mtd, oob, chip->ecc.prepad);
1972 oob += chip->ecc.prepad;
1973 }
1974
1975 chip->read_buf(mtd, oob, eccbytes);
1976 oob += eccbytes;
1977
1978 if (chip->ecc.postpad) {
1979 chip->write_buf(mtd, oob, chip->ecc.postpad);
1980 oob += chip->ecc.postpad;
1981 }
1982 }
1983
1984 size = mtd->oobsize - (oob - chip->oob_poi);
1985 if (size)
1986 chip->write_buf(mtd, oob, size);
1987}
1988/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001989 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001990 * @mtd: mtd info structure
1991 * @chip: nand chip info structure
1992 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001993 */
1994static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1995 const uint8_t *buf)
1996{
1997 int i, eccsize = chip->ecc.size;
1998 int eccbytes = chip->ecc.bytes;
1999 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002000 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002001 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002002 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002003
Brian Norris7854d3f2011-06-23 14:12:08 -07002004 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002005 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2006 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002007
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002008 for (i = 0; i < chip->ecc.total; i++)
2009 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002010
Thomas Gleixner90424de2007-04-05 11:44:05 +02002011 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002012}
2013
2014/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002015 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002016 * @mtd: mtd info structure
2017 * @chip: nand chip info structure
2018 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002019 */
2020static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2021 const uint8_t *buf)
2022{
2023 int i, eccsize = chip->ecc.size;
2024 int eccbytes = chip->ecc.bytes;
2025 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002026 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002027 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002028 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002029
2030 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2031 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002032 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002033 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2034 }
2035
2036 for (i = 0; i < chip->ecc.total; i++)
2037 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2038
2039 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2040}
2041
2042/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002043 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002044 * @mtd: mtd info structure
2045 * @chip: nand chip info structure
2046 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002047 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002048 * The hw generator calculates the error syndrome automatically. Therefore we
2049 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002050 */
2051static void nand_write_page_syndrome(struct mtd_info *mtd,
2052 struct nand_chip *chip, const uint8_t *buf)
2053{
2054 int i, eccsize = chip->ecc.size;
2055 int eccbytes = chip->ecc.bytes;
2056 int eccsteps = chip->ecc.steps;
2057 const uint8_t *p = buf;
2058 uint8_t *oob = chip->oob_poi;
2059
2060 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2061
2062 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2063 chip->write_buf(mtd, p, eccsize);
2064
2065 if (chip->ecc.prepad) {
2066 chip->write_buf(mtd, oob, chip->ecc.prepad);
2067 oob += chip->ecc.prepad;
2068 }
2069
2070 chip->ecc.calculate(mtd, p, oob);
2071 chip->write_buf(mtd, oob, eccbytes);
2072 oob += eccbytes;
2073
2074 if (chip->ecc.postpad) {
2075 chip->write_buf(mtd, oob, chip->ecc.postpad);
2076 oob += chip->ecc.postpad;
2077 }
2078 }
2079
2080 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002081 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002082 if (i)
2083 chip->write_buf(mtd, oob, i);
2084}
2085
2086/**
David Woodhouse956e9442006-09-25 17:12:39 +01002087 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002088 * @mtd: MTD device structure
2089 * @chip: NAND chip descriptor
2090 * @buf: the data to write
2091 * @page: page number to write
2092 * @cached: cached programming
2093 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002094 */
2095static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01002096 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002097{
2098 int status;
2099
2100 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2101
David Woodhouse956e9442006-09-25 17:12:39 +01002102 if (unlikely(raw))
2103 chip->ecc.write_page_raw(mtd, chip, buf);
2104 else
2105 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002106
2107 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002108 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002109 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002110 */
2111 cached = 0;
2112
2113 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2114
2115 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002116 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002117 /*
2118 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002119 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002120 */
2121 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2122 status = chip->errstat(mtd, chip, FL_WRITING, status,
2123 page);
2124
2125 if (status & NAND_STATUS_FAIL)
2126 return -EIO;
2127 } else {
2128 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002129 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002130 }
2131
2132#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2133 /* Send command to read back the data */
2134 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2135
2136 if (chip->verify_buf(mtd, buf, mtd->writesize))
2137 return -EIO;
2138#endif
2139 return 0;
2140}
2141
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002142/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002143 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002144 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002145 * @oob: oob data buffer
2146 * @len: oob data write length
2147 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002148 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002149static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2150 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002151{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002152 struct nand_chip *chip = mtd->priv;
2153
2154 /*
2155 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2156 * data from a previous OOB read.
2157 */
2158 memset(chip->oob_poi, 0xff, mtd->oobsize);
2159
Florian Fainellif8ac0412010-09-07 13:23:43 +02002160 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002161
Brian Norris0612b9d2011-08-30 18:45:40 -07002162 case MTD_OPS_PLACE_OOB:
2163 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002164 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2165 return oob + len;
2166
Brian Norris0612b9d2011-08-30 18:45:40 -07002167 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002168 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002169 uint32_t boffs = 0, woffs = ops->ooboffs;
2170 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002171
Florian Fainellif8ac0412010-09-07 13:23:43 +02002172 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002173 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002174 if (unlikely(woffs)) {
2175 if (woffs >= free->length) {
2176 woffs -= free->length;
2177 continue;
2178 }
2179 boffs = free->offset + woffs;
2180 bytes = min_t(size_t, len,
2181 (free->length - woffs));
2182 woffs = 0;
2183 } else {
2184 bytes = min_t(size_t, len, free->length);
2185 boffs = free->offset;
2186 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002187 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002188 oob += bytes;
2189 }
2190 return oob;
2191 }
2192 default:
2193 BUG();
2194 }
2195 return NULL;
2196}
2197
Florian Fainellif8ac0412010-09-07 13:23:43 +02002198#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002199
2200/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002201 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002202 * @mtd: MTD device structure
2203 * @to: offset to write to
2204 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002205 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002206 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002207 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002208static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2209 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002210{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002211 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002212 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002213 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002214
2215 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002216 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002217 mtd->oobavail : mtd->oobsize;
2218
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002219 uint8_t *oob = ops->oobbuf;
2220 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002221 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002222
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002223 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002224 if (!writelen)
2225 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002226
Brian Norris8b6e50c2011-05-25 14:59:01 -07002227 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002228 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002229 pr_notice("%s: attempt to write non page aligned data\n",
2230 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002231 return -EINVAL;
2232 }
2233
Thomas Gleixner29072b92006-09-28 15:38:36 +02002234 column = to & (mtd->writesize - 1);
2235 subpage = column || (writelen & (mtd->writesize - 1));
2236
2237 if (subpage && oob)
2238 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002239
Thomas Gleixner6a930962006-06-28 00:11:45 +02002240 chipnr = (int)(to >> chip->chip_shift);
2241 chip->select_chip(mtd, chipnr);
2242
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002243 /* Check, if it is write protected */
2244 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002245 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002246
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002247 realpage = (int)(to >> chip->page_shift);
2248 page = realpage & chip->pagemask;
2249 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2250
2251 /* Invalidate the page cache, when we write to the cached page */
2252 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002253 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002254 chip->pagebuf = -1;
2255
Maxim Levitsky782ce792010-02-22 20:39:36 +02002256 /* Don't allow multipage oob writes with offset */
Jon Poveycdcf12b2010-09-30 20:41:34 +09002257 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
Maxim Levitsky782ce792010-02-22 20:39:36 +02002258 return -EINVAL;
2259
Florian Fainellif8ac0412010-09-07 13:23:43 +02002260 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002261 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002262 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002263 uint8_t *wbuf = buf;
2264
Brian Norris8b6e50c2011-05-25 14:59:01 -07002265 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002266 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2267 cached = 0;
2268 bytes = min_t(int, bytes - column, (int) writelen);
2269 chip->pagebuf = -1;
2270 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2271 memcpy(&chip->buffers->databuf[column], buf, bytes);
2272 wbuf = chip->buffers->databuf;
2273 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002274
Maxim Levitsky782ce792010-02-22 20:39:36 +02002275 if (unlikely(oob)) {
2276 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002277 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002278 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002279 } else {
2280 /* We still need to erase leftover OOB data */
2281 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002282 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002283
Thomas Gleixner29072b92006-09-28 15:38:36 +02002284 ret = chip->write_page(mtd, chip, wbuf, page, cached,
Brian Norris0612b9d2011-08-30 18:45:40 -07002285 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002286 if (ret)
2287 break;
2288
2289 writelen -= bytes;
2290 if (!writelen)
2291 break;
2292
Thomas Gleixner29072b92006-09-28 15:38:36 +02002293 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002294 buf += bytes;
2295 realpage++;
2296
2297 page = realpage & chip->pagemask;
2298 /* Check, if we cross a chip boundary */
2299 if (!page) {
2300 chipnr++;
2301 chip->select_chip(mtd, -1);
2302 chip->select_chip(mtd, chipnr);
2303 }
2304 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002305
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002306 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002307 if (unlikely(oob))
2308 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309 return ret;
2310}
2311
2312/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002313 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002314 * @mtd: MTD device structure
2315 * @to: offset to write to
2316 * @len: number of bytes to write
2317 * @retlen: pointer to variable to store the number of written bytes
2318 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002319 *
2320 * NAND write with ECC. Used when performing writes in interrupt context, this
2321 * may for example be called by mtdoops when writing an oops while in panic.
2322 */
2323static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2324 size_t *retlen, const uint8_t *buf)
2325{
2326 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002327 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002328 int ret;
2329
2330 /* Do not allow reads past end of device */
2331 if ((to + len) > mtd->size)
2332 return -EINVAL;
2333 if (!len)
2334 return 0;
2335
Brian Norris8b6e50c2011-05-25 14:59:01 -07002336 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002337 panic_nand_wait(mtd, chip, 400);
2338
Brian Norris8b6e50c2011-05-25 14:59:01 -07002339 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002340 panic_nand_get_device(chip, mtd, FL_WRITING);
2341
Brian Norris4a89ff82011-08-30 18:45:45 -07002342 ops.len = len;
2343 ops.datbuf = (uint8_t *)buf;
2344 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07002345 ops.mode = 0;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002346
Brian Norris4a89ff82011-08-30 18:45:45 -07002347 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002348
Brian Norris4a89ff82011-08-30 18:45:45 -07002349 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002350 return ret;
2351}
2352
2353/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002354 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002355 * @mtd: MTD device structure
2356 * @to: offset to write to
2357 * @len: number of bytes to write
2358 * @retlen: pointer to variable to store the number of written bytes
2359 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002361 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002363static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002364 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002366 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002367 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002368 int ret;
2369
2370 /* Do not allow reads past end of device */
2371 if ((to + len) > mtd->size)
2372 return -EINVAL;
2373 if (!len)
2374 return 0;
2375
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002376 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002377
Brian Norris4a89ff82011-08-30 18:45:45 -07002378 ops.len = len;
2379 ops.datbuf = (uint8_t *)buf;
2380 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07002381 ops.mode = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002382
Brian Norris4a89ff82011-08-30 18:45:45 -07002383 ret = nand_do_write_ops(mtd, to, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002384
Brian Norris4a89ff82011-08-30 18:45:45 -07002385 *retlen = ops.retlen;
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002386
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002387 nand_release_device(mtd);
2388
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002389 return ret;
2390}
2391
2392/**
2393 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002394 * @mtd: MTD device structure
2395 * @to: offset to write to
2396 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002397 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002398 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002399 */
2400static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2401 struct mtd_oob_ops *ops)
2402{
Adrian Hunter03736152007-01-31 17:58:29 +02002403 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002404 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Brian Norris289c0522011-07-19 10:06:09 -07002406 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302407 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Brian Norris0612b9d2011-08-30 18:45:40 -07002409 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002410 len = chip->ecc.layout->oobavail;
2411 else
2412 len = mtd->oobsize;
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002415 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002416 pr_debug("%s: attempt to write past end of page\n",
2417 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -EINVAL;
2419 }
2420
Adrian Hunter03736152007-01-31 17:58:29 +02002421 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002422 pr_debug("%s: attempt to start write outside oob\n",
2423 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002424 return -EINVAL;
2425 }
2426
Jason Liu775adc32011-02-25 13:06:18 +08002427 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002428 if (unlikely(to >= mtd->size ||
2429 ops->ooboffs + ops->ooblen >
2430 ((mtd->size >> chip->page_shift) -
2431 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002432 pr_debug("%s: attempt to write beyond end of device\n",
2433 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002434 return -EINVAL;
2435 }
2436
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002437 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002438 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002440 /* Shift to get page */
2441 page = (int)(to >> chip->page_shift);
2442
2443 /*
2444 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2445 * of my DiskOnChip 2000 test units) will clear the whole data page too
2446 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2447 * it in the doc2000 driver in August 1999. dwmw2.
2448 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002449 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /* Check, if it is write protected */
2452 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002453 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002456 if (page == chip->pagebuf)
2457 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002459 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002460
Brian Norris0612b9d2011-08-30 18:45:40 -07002461 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002462 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2463 else
2464 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002465
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002466 if (status)
2467 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Vitaly Wool70145682006-11-03 18:20:38 +03002469 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002471 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002474/**
2475 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002476 * @mtd: MTD device structure
2477 * @to: offset to write to
2478 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479 */
2480static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2481 struct mtd_oob_ops *ops)
2482{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002483 struct nand_chip *chip = mtd->priv;
2484 int ret = -ENOTSUPP;
2485
2486 ops->retlen = 0;
2487
2488 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002489 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002490 pr_debug("%s: attempt to write beyond end of device\n",
2491 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002492 return -EINVAL;
2493 }
2494
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002495 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496
Florian Fainellif8ac0412010-09-07 13:23:43 +02002497 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002498 case MTD_OPS_PLACE_OOB:
2499 case MTD_OPS_AUTO_OOB:
2500 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002501 break;
2502
2503 default:
2504 goto out;
2505 }
2506
2507 if (!ops->datbuf)
2508 ret = nand_do_write_oob(mtd, to, ops);
2509 else
2510 ret = nand_do_write_ops(mtd, to, ops);
2511
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002512out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002513 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return ret;
2515}
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002518 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002519 * @mtd: MTD device structure
2520 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002522 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002524static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002526 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2529 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
2532/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002533 * multi_erase_cmd - [GENERIC] AND specific block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002534 * @mtd: MTD device structure
2535 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002537 * AND multi block erase command function. Erase 4 consecutive blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002539static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002541 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002543 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2544 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2545 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2546 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2547 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
2550/**
2551 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002552 * @mtd: MTD device structure
2553 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002555 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002557static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
David Woodhousee0c7d762006-05-13 18:07:53 +01002559 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002561
David A. Marlin30f464b2005-01-17 18:35:25 +00002562#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002564 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002565 * @mtd: MTD device structure
2566 * @instr: erase instruction
2567 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002569 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002571int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2572 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Adrian Hunter69423d92008-12-10 13:37:21 +00002574 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002575 struct nand_chip *chip = mtd->priv;
Florian Fainellif8ac0412010-09-07 13:23:43 +02002576 loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002577 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002578 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Brian Norris289c0522011-07-19 10:06:09 -07002580 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2581 __func__, (unsigned long long)instr->addr,
2582 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302584 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002587 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002590 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002593 page = (int)(instr->addr >> chip->page_shift);
2594 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002597 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002600 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* Check, if it is write protected */
2603 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002604 pr_debug("%s: device is write protected!\n",
2605 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 instr->state = MTD_ERASE_FAILED;
2607 goto erase_exit;
2608 }
2609
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002610 /*
2611 * If BBT requires refresh, set the BBT page mask to see if the BBT
2612 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2613 * can not be matched. This is also done when the bbt is actually
Brian Norris7854d3f2011-06-23 14:12:08 -07002614 * erased to avoid recursive updates.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002615 */
2616 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2617 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /* Loop through the pages */
2620 len = instr->len;
2621
2622 instr->state = MTD_ERASING;
2623
2624 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002625 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002626 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2627 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002628 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2629 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 instr->state = MTD_ERASE_FAILED;
2631 goto erase_exit;
2632 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002633
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002634 /*
2635 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002636 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002637 */
2638 if (page <= chip->pagebuf && chip->pagebuf <
2639 (page + pages_per_block))
2640 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002642 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002643
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002644 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002646 /*
2647 * See if operation failed and additional status checks are
2648 * available
2649 */
2650 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2651 status = chip->errstat(mtd, chip, FL_ERASING,
2652 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002655 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002656 pr_debug("%s: failed erase, page 0x%08x\n",
2657 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002659 instr->fail_addr =
2660 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 goto erase_exit;
2662 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002663
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002664 /*
2665 * If BBT requires refresh, set the BBT rewrite flag to the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002666 * page being erased.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002667 */
2668 if (bbt_masked_page != 0xffffffff &&
2669 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002670 rewrite_bbt[chipnr] =
2671 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002674 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 page += pages_per_block;
2676
2677 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002678 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002680 chip->select_chip(mtd, -1);
2681 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002682
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002683 /*
2684 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Brian Norris8b6e50c2011-05-25 14:59:01 -07002685 * page mask to see if this BBT should be rewritten.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002686 */
2687 if (bbt_masked_page != 0xffffffff &&
2688 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2689 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2690 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
2692 }
2693 instr->state = MTD_ERASE_DONE;
2694
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002695erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 /* Deselect and wake up anyone waiting on the device */
2700 nand_release_device(mtd);
2701
David Woodhouse49defc02007-10-06 15:01:59 -04002702 /* Do call back function */
2703 if (!ret)
2704 mtd_erase_callback(instr);
2705
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002706 /*
2707 * If BBT requires refresh and erase was successful, rewrite any
Brian Norris8b6e50c2011-05-25 14:59:01 -07002708 * selected bad block tables.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002709 */
2710 if (bbt_masked_page == 0xffffffff || ret)
2711 return ret;
2712
2713 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2714 if (!rewrite_bbt[chipnr])
2715 continue;
Brian Norris8b6e50c2011-05-25 14:59:01 -07002716 /* Update the BBT for chip */
Brian Norris289c0522011-07-19 10:06:09 -07002717 pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2718 __func__, chipnr, rewrite_bbt[chipnr],
2719 chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002720 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002721 }
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 /* Return more or less happy */
2724 return ret;
2725}
2726
2727/**
2728 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002729 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002731 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002733static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002735 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Brian Norris289c0522011-07-19 10:06:09 -07002737 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002740 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002742 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002746 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002747 * @mtd: MTD device structure
2748 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002750static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002753 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002755
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002756 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
2759/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002760 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002761 * @mtd: MTD device structure
2762 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002764static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002766 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 int ret;
2768
Florian Fainellif8ac0412010-09-07 13:23:43 +02002769 ret = nand_block_isbad(mtd, ofs);
2770 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002771 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (ret > 0)
2773 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002774 return ret;
2775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002777 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
2780/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002781 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002782 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002783 */
2784static int nand_suspend(struct mtd_info *mtd)
2785{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002786 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002787
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002788 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002789}
2790
2791/**
2792 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002793 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002794 */
2795static void nand_resume(struct mtd_info *mtd)
2796{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002797 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002798
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002799 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002800 nand_release_device(mtd);
2801 else
Brian Norrisd0370212011-07-19 10:06:08 -07002802 pr_err("%s called for a chip which is not in suspended state\n",
2803 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002804}
2805
Brian Norris8b6e50c2011-05-25 14:59:01 -07002806/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002807static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002810 if (!chip->chip_delay)
2811 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002814 if (chip->cmdfunc == NULL)
2815 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002818 if (chip->waitfunc == NULL)
2819 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002821 if (!chip->select_chip)
2822 chip->select_chip = nand_select_chip;
2823 if (!chip->read_byte)
2824 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2825 if (!chip->read_word)
2826 chip->read_word = nand_read_word;
2827 if (!chip->block_bad)
2828 chip->block_bad = nand_block_bad;
2829 if (!chip->block_markbad)
2830 chip->block_markbad = nand_default_block_markbad;
2831 if (!chip->write_buf)
2832 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2833 if (!chip->read_buf)
2834 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2835 if (!chip->verify_buf)
2836 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2837 if (!chip->scan_bbt)
2838 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002839
2840 if (!chip->controller) {
2841 chip->controller = &chip->hwcontrol;
2842 spin_lock_init(&chip->controller->lock);
2843 init_waitqueue_head(&chip->controller->wq);
2844 }
2845
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002846}
2847
Brian Norris8b6e50c2011-05-25 14:59:01 -07002848/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002849static void sanitize_string(uint8_t *s, size_t len)
2850{
2851 ssize_t i;
2852
Brian Norris8b6e50c2011-05-25 14:59:01 -07002853 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002854 s[len - 1] = 0;
2855
Brian Norris8b6e50c2011-05-25 14:59:01 -07002856 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002857 for (i = 0; i < len - 1; i++) {
2858 if (s[i] < ' ' || s[i] > 127)
2859 s[i] = '?';
2860 }
2861
Brian Norris8b6e50c2011-05-25 14:59:01 -07002862 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002863 strim(s);
2864}
2865
2866static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2867{
2868 int i;
2869 while (len--) {
2870 crc ^= *p++ << 8;
2871 for (i = 0; i < 8; i++)
2872 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2873 }
2874
2875 return crc;
2876}
2877
2878/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002879 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002880 */
2881static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002882 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002883{
2884 struct nand_onfi_params *p = &chip->onfi_params;
2885 int i;
2886 int val;
2887
Brian Norris7854d3f2011-06-23 14:12:08 -07002888 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002889 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2890 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2891 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2892 return 0;
2893
Brian Norris9a4d4d62011-07-19 10:06:07 -07002894 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002895 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2896 for (i = 0; i < 3; i++) {
2897 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2898 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2899 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002900 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002901 break;
2902 }
2903 }
2904
2905 if (i == 3)
2906 return 0;
2907
Brian Norris8b6e50c2011-05-25 14:59:01 -07002908 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002909 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002910 if (val & (1 << 5))
2911 chip->onfi_version = 23;
2912 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002913 chip->onfi_version = 22;
2914 else if (val & (1 << 3))
2915 chip->onfi_version = 21;
2916 else if (val & (1 << 2))
2917 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002918 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002919 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002920 else
2921 chip->onfi_version = 0;
2922
2923 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002924 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002925 return 0;
2926 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002927
2928 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2929 sanitize_string(p->model, sizeof(p->model));
2930 if (!mtd->name)
2931 mtd->name = p->model;
2932 mtd->writesize = le32_to_cpu(p->byte_per_page);
2933 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2934 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
David Woodhouse4ccb3b42010-12-03 16:36:34 +00002935 chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002936 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002937 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002938 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002939
2940 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2941 chip->options |= (NAND_NO_READRDY |
2942 NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
2943
2944 return 1;
2945}
2946
2947/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002948 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002949 */
2950static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002951 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002952 int busw,
2953 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00002954 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002955{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002956 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07002957 u8 id_data[8];
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002958 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002961 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Karl Beldanef89a882008-09-15 14:37:29 +02002963 /*
2964 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002965 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02002966 */
2967 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002970 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002973 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002974 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Brian Norris8b6e50c2011-05-25 14:59:01 -07002976 /*
2977 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01002978 * interface concerns can cause random data which looks like a
2979 * possibly credible NAND flash to appear. If the two results do
2980 * not match, ignore the device completely.
2981 */
2982
2983 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2984
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002985 for (i = 0; i < 2; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07002986 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01002987
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002988 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002989 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07002990 "%02x,%02x against %02x,%02x\n", __func__,
2991 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01002992 return ERR_PTR(-ENODEV);
2993 }
2994
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002995 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00002996 type = nand_flash_ids;
2997
2998 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002999 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02003000 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00003001
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003002 chip->onfi_version = 0;
3003 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003004 /* Check is chip is ONFI compliant */
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003005 ret = nand_flash_detect_onfi(mtd, chip, &busw);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003006 if (ret)
3007 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003008 }
3009
3010 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3011
3012 /* Read entire ID string */
3013
3014 for (i = 0; i < 8; i++)
3015 id_data[i] = chip->read_byte(mtd);
3016
David Woodhouse5e81e882010-02-26 18:32:56 +00003017 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003018 return ERR_PTR(-ENODEV);
3019
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003020 if (!mtd->name)
3021 mtd->name = type->name;
3022
Adrian Hunter69423d92008-12-10 13:37:21 +00003023 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003024
Huang Shijie12a40a52010-09-27 10:43:53 +08003025 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003026 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003027 busw = chip->init_size(mtd, chip, id_data);
3028 } else if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003029 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003030 /* The 3rd id byte holds MLC / multichip data */
Kevin Cernekee426c4572010-05-04 20:58:03 -07003031 chip->cellinfo = id_data[2];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003032 /* The 4th id byte is the important one */
Kevin Cernekee426c4572010-05-04 20:58:03 -07003033 extid = id_data[3];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003034
Kevin Cernekee426c4572010-05-04 20:58:03 -07003035 /*
3036 * Field definitions are in the following datasheets:
3037 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norris34c5bf62010-08-20 10:50:43 -07003038 * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003039 *
3040 * Check for wraparound + Samsung ID + nonzero 6th byte
3041 * to decide what to do.
3042 */
3043 if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
3044 id_data[0] == NAND_MFR_SAMSUNG &&
Tilman Sauerbeckcfe3fda2010-08-20 14:01:47 -07003045 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Kevin Cernekee426c4572010-05-04 20:58:03 -07003046 id_data[5] != 0x00) {
3047 /* Calc pagesize */
3048 mtd->writesize = 2048 << (extid & 0x03);
3049 extid >>= 2;
3050 /* Calc oobsize */
Brian Norris34c5bf62010-08-20 10:50:43 -07003051 switch (extid & 0x03) {
3052 case 1:
3053 mtd->oobsize = 128;
3054 break;
3055 case 2:
3056 mtd->oobsize = 218;
3057 break;
3058 case 3:
3059 mtd->oobsize = 400;
3060 break;
3061 default:
3062 mtd->oobsize = 436;
3063 break;
3064 }
Kevin Cernekee426c4572010-05-04 20:58:03 -07003065 extid >>= 2;
3066 /* Calc blocksize */
3067 mtd->erasesize = (128 * 1024) <<
3068 (((extid >> 1) & 0x04) | (extid & 0x03));
3069 busw = 0;
3070 } else {
3071 /* Calc pagesize */
3072 mtd->writesize = 1024 << (extid & 0x03);
3073 extid >>= 2;
3074 /* Calc oobsize */
3075 mtd->oobsize = (8 << (extid & 0x01)) *
3076 (mtd->writesize >> 9);
3077 extid >>= 2;
3078 /* Calc blocksize. Blocksize is multiples of 64KiB */
3079 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3080 extid >>= 2;
3081 /* Get buswidth information */
3082 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3083 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003084 } else {
3085 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003086 * Old devices have chip data hardcoded in the device id table.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003087 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003088 mtd->erasesize = type->erasesize;
3089 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02003090 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003091 busw = type->options & NAND_BUSWIDTH_16;
Brian Norris2173bae2010-08-19 08:11:02 -07003092
3093 /*
3094 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3095 * some Spansion chips have erasesize that conflicts with size
Brian Norris8b6e50c2011-05-25 14:59:01 -07003096 * listed in nand_ids table.
Brian Norris2173bae2010-08-19 08:11:02 -07003097 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3098 */
3099 if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
3100 id_data[5] == 0x00 && id_data[6] == 0x00 &&
3101 id_data[7] == 0x00 && mtd->writesize == 512) {
3102 mtd->erasesize = 128 * 1024;
3103 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3104 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003105 }
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003106 /* Get chip options, preserve non chip based options */
3107 chip->options &= ~NAND_CHIPOPTIONS_MSK;
3108 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
3109
Brian Norris8b6e50c2011-05-25 14:59:01 -07003110 /*
3111 * Check if chip is not a Samsung device. Do not clear the
3112 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003113 */
3114 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3115 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3116ident_done:
3117
3118 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003119 * Set chip as a default. Board drivers can override it, if necessary.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003120 */
3121 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003122
3123 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003124 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003125 if (nand_manuf_ids[maf_idx].id == *maf_id)
3126 break;
3127 }
3128
3129 /*
3130 * Check, if buswidth is correct. Hardware drivers should set
Brian Norris8b6e50c2011-05-25 14:59:01 -07003131 * chip correct!
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003132 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003133 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003134 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003135 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3136 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003137 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003138 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3139 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003140 return ERR_PTR(-EINVAL);
3141 }
3142
3143 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003144 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003145 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003146 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003147
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003148 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003149 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003150 if (chip->chipsize & 0xffffffff)
3151 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003152 else {
3153 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3154 chip->chip_shift += 32 - 1;
3155 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003156
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003157 chip->badblockbits = 8;
3158
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003159 /* Set the bad block position */
Brian Norris065a1ed2010-08-18 11:25:04 -07003160 if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
Brian Norrisc7b28e22010-07-13 15:13:00 -07003161 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
Brian Norris065a1ed2010-08-18 11:25:04 -07003162 else
3163 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003164
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07003165 /*
3166 * Bad block marker is stored in the last page of each block
Brian Norrisc7b28e22010-07-13 15:13:00 -07003167 * on Samsung and Hynix MLC devices; stored in first two pages
3168 * of each block on Micron devices with 2KiB pages and on
Brian Norris8c342332011-11-02 13:34:44 -07003169 * SLC Samsung, Hynix, Toshiba, AMD/Spansion, and Macronix.
3170 * All others scan only the first page.
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07003171 */
3172 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3173 (*maf_id == NAND_MFR_SAMSUNG ||
3174 *maf_id == NAND_MFR_HYNIX))
Brian Norris5fb15492011-05-31 16:31:21 -07003175 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07003176 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3177 (*maf_id == NAND_MFR_SAMSUNG ||
3178 *maf_id == NAND_MFR_HYNIX ||
Brian Norris13ed7ae2010-08-20 12:36:12 -07003179 *maf_id == NAND_MFR_TOSHIBA ||
Brian Norris8c342332011-11-02 13:34:44 -07003180 *maf_id == NAND_MFR_AMD ||
3181 *maf_id == NAND_MFR_MACRONIX)) ||
Brian Norrisc7b28e22010-07-13 15:13:00 -07003182 (mtd->writesize == 2048 &&
3183 *maf_id == NAND_MFR_MICRON))
Brian Norris5fb15492011-05-31 16:31:21 -07003184 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07003185
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003186 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003187 if (chip->options & NAND_4PAGE_ARRAY)
3188 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003189 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003190 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003191
Brian Norris8b6e50c2011-05-25 14:59:01 -07003192 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003193 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3194 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003195
Brian Norris9a4d4d62011-07-19 10:06:07 -07003196 pr_info("NAND device: Manufacturer ID:"
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003197 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
3198 nand_manuf_ids[maf_idx].name,
Brian Norris0b524fb2010-12-12 00:23:32 -08003199 chip->onfi_version ? chip->onfi_params.model : type->name);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003200
3201 return type;
3202}
3203
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003204/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003205 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003206 * @mtd: MTD device structure
3207 * @maxchips: number of chips to scan for
3208 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003209 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003210 * This is the first phase of the normal nand_scan() function. It reads the
3211 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003212 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003213 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003214 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003215int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3216 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003217{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003218 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003219 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003220 struct nand_flash_dev *type;
3221
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003222 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003223 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003224 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003225 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003226
3227 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003228 type = nand_get_flash_type(mtd, chip, busw,
3229 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003230
3231 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003232 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003233 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003234 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003235 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
3237
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003238 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003239 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003240 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003241 /* See comment in nand_get_flash_type for reset */
3242 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003244 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003246 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003247 nand_dev_id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 break;
3249 }
3250 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003251 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003254 chip->numchips = i;
3255 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
David Woodhouse3b85c322006-09-25 17:06:53 +01003257 return 0;
3258}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003259EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003260
3261
3262/**
3263 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003264 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003265 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003266 * This is the second phase of the normal nand_scan() function. It fills out
3267 * all the uninitialized function pointers with the defaults and scans for a
3268 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003269 */
3270int nand_scan_tail(struct mtd_info *mtd)
3271{
3272 int i;
3273 struct nand_chip *chip = mtd->priv;
3274
Brian Norrise2414f42012-02-06 13:44:00 -08003275 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3276 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3277 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3278
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003279 if (!(chip->options & NAND_OWN_BUFFERS))
3280 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3281 if (!chip->buffers)
3282 return -ENOMEM;
3283
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003284 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003285 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003286
3287 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003288 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003289 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003290 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003291 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003293 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 break;
3295 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003296 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 break;
3298 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003299 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003301 case 128:
3302 chip->ecc.layout = &nand_oob_128;
3303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003305 pr_warn("No oob scheme defined for oobsize %d\n",
3306 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 BUG();
3308 }
3309 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003310
David Woodhouse956e9442006-09-25 17:12:39 +01003311 if (!chip->write_page)
3312 chip->write_page = nand_write_page;
3313
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003314 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003315 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003316 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003317 */
David Woodhouse956e9442006-09-25 17:12:39 +01003318
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003319 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003320 case NAND_ECC_HW_OOB_FIRST:
3321 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3322 if (!chip->ecc.calculate || !chip->ecc.correct ||
3323 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003324 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003325 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003326 BUG();
3327 }
3328 if (!chip->ecc.read_page)
3329 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3330
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003331 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003332 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003333 if (!chip->ecc.read_page)
3334 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003335 if (!chip->ecc.write_page)
3336 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003337 if (!chip->ecc.read_page_raw)
3338 chip->ecc.read_page_raw = nand_read_page_raw;
3339 if (!chip->ecc.write_page_raw)
3340 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003341 if (!chip->ecc.read_oob)
3342 chip->ecc.read_oob = nand_read_oob_std;
3343 if (!chip->ecc.write_oob)
3344 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003345
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003346 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003347 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3348 !chip->ecc.hwctl) &&
3349 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003350 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003351 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003352 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003353 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003354 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003355 BUG();
3356 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003357 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003358 if (!chip->ecc.read_page)
3359 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003360 if (!chip->ecc.write_page)
3361 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003362 if (!chip->ecc.read_page_raw)
3363 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3364 if (!chip->ecc.write_page_raw)
3365 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003366 if (!chip->ecc.read_oob)
3367 chip->ecc.read_oob = nand_read_oob_syndrome;
3368 if (!chip->ecc.write_oob)
3369 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003370
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003371 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003372 break;
Brian Norris9a4d4d62011-07-19 10:06:07 -07003373 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003374 "%d byte page size, fallback to SW ECC\n",
3375 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003376 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003378 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003379 chip->ecc.calculate = nand_calculate_ecc;
3380 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003381 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003382 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003383 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003384 chip->ecc.read_page_raw = nand_read_page_raw;
3385 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003386 chip->ecc.read_oob = nand_read_oob_std;
3387 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003388 if (!chip->ecc.size)
3389 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003390 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003392
Ivan Djelic193bd402011-03-11 11:05:33 +01003393 case NAND_ECC_SOFT_BCH:
3394 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003395 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003396 BUG();
3397 }
3398 chip->ecc.calculate = nand_bch_calculate_ecc;
3399 chip->ecc.correct = nand_bch_correct_data;
3400 chip->ecc.read_page = nand_read_page_swecc;
3401 chip->ecc.read_subpage = nand_read_subpage;
3402 chip->ecc.write_page = nand_write_page_swecc;
3403 chip->ecc.read_page_raw = nand_read_page_raw;
3404 chip->ecc.write_page_raw = nand_write_page_raw;
3405 chip->ecc.read_oob = nand_read_oob_std;
3406 chip->ecc.write_oob = nand_write_oob_std;
3407 /*
3408 * Board driver should supply ecc.size and ecc.bytes values to
3409 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003410 * for details. Otherwise, default to 4 bits for large page
3411 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003412 */
3413 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3414 chip->ecc.size = 512;
3415 chip->ecc.bytes = 7;
3416 }
3417 chip->ecc.priv = nand_bch_init(mtd,
3418 chip->ecc.size,
3419 chip->ecc.bytes,
3420 &chip->ecc.layout);
3421 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003422 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003423 BUG();
3424 }
3425 break;
3426
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003427 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003428 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003429 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003430 chip->ecc.read_page = nand_read_page_raw;
3431 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003432 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003433 chip->ecc.read_page_raw = nand_read_page_raw;
3434 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003435 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003436 chip->ecc.size = mtd->writesize;
3437 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003441 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003442 BUG();
3443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Brian Norris9ce244b2011-08-30 18:45:37 -07003445 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003446 if (!chip->ecc.read_oob_raw)
3447 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003448 if (!chip->ecc.write_oob_raw)
3449 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3450
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003451 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003452 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003453 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003454 */
3455 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003456 for (i = 0; chip->ecc.layout->oobfree[i].length
3457 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003458 chip->ecc.layout->oobavail +=
3459 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003460 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003461
3462 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003463 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003464 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003465 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003466 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003467 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003468 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003469 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003471 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003472
Brian Norris8b6e50c2011-05-25 14:59:01 -07003473 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003474 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3475 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003476 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003477 case 2:
3478 mtd->subpage_sft = 1;
3479 break;
3480 case 4:
3481 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003482 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003483 mtd->subpage_sft = 2;
3484 break;
3485 }
3486 }
3487 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3488
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003489 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003490 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003493 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003496 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
3498 /* Fill in remaining MTD driver data */
3499 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003500 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3501 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003502 mtd->_erase = nand_erase;
3503 mtd->_point = NULL;
3504 mtd->_unpoint = NULL;
3505 mtd->_read = nand_read;
3506 mtd->_write = nand_write;
3507 mtd->_panic_write = panic_nand_write;
3508 mtd->_read_oob = nand_read_oob;
3509 mtd->_write_oob = nand_write_oob;
3510 mtd->_sync = nand_sync;
3511 mtd->_lock = NULL;
3512 mtd->_unlock = NULL;
3513 mtd->_suspend = nand_suspend;
3514 mtd->_resume = nand_resume;
3515 mtd->_block_isbad = nand_block_isbad;
3516 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003517 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003519 /* propagate ecc.layout to mtd_info */
3520 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003522 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003523 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003527 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003529EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Brian Norris8b6e50c2011-05-25 14:59:01 -07003531/*
3532 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003533 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003534 * to call us from in-kernel code if the core NAND support is modular.
3535 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003536#ifdef MODULE
3537#define caller_is_module() (1)
3538#else
3539#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003540 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003541#endif
3542
3543/**
3544 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003545 * @mtd: MTD device structure
3546 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003547 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003548 * This fills out all the uninitialized function pointers with the defaults.
3549 * The flash ID is read and the mtd/chip structures are filled with the
3550 * appropriate values. The mtd->owner field must be set to the module of the
3551 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003552 */
3553int nand_scan(struct mtd_info *mtd, int maxchips)
3554{
3555 int ret;
3556
3557 /* Many callers got this wrong, so check for it for a while... */
3558 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003559 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003560 BUG();
3561 }
3562
David Woodhouse5e81e882010-02-26 18:32:56 +00003563 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003564 if (!ret)
3565 ret = nand_scan_tail(mtd);
3566 return ret;
3567}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003568EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003571 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003572 * @mtd: MTD device structure
3573 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003574void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003576 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Ivan Djelic193bd402011-03-11 11:05:33 +01003578 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3579 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3580
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003581 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Jesper Juhlfa671642005-11-07 01:01:27 -08003583 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003584 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003585 if (!(chip->options & NAND_OWN_BUFFERS))
3586 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003587
3588 /* Free bad block descriptor memory */
3589 if (chip->badblock_pattern && chip->badblock_pattern->options
3590 & NAND_BBT_DYNAMICSTRUCT)
3591 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
David Woodhousee0c7d762006-05-13 18:07:53 +01003593EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003594
3595static int __init nand_base_init(void)
3596{
3597 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3598 return 0;
3599}
3600
3601static void __exit nand_base_exit(void)
3602{
3603 led_trigger_unregister_simple(nand_led_trigger);
3604}
3605
3606module_init(nand_base_init);
3607module_exit(nand_base_exit);
3608
David Woodhousee0c7d762006-05-13 18:07:53 +01003609MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003610MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3611MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003612MODULE_DESCRIPTION("Generic NAND flash driver code");