blob: 7855fd2d5c60178acf4156ec10b400e7be94294f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +020010 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020013 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020015 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000016 * David Woodhouse for adding multichip support
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
20 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020021 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070024 * if we have HW ECC support.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030027 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
32 *
33 */
34
David Woodhouse552d9202006-05-14 01:20:46 +010035#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020038#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010045#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020053static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020060 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020063static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066 .oobfree = {
67 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020068 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020071static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .eccbytes = 24,
73 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010074 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020077 .oobfree = {
78 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020079 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Thomas Gleixner81ec5362007-12-12 17:27:03 +010082static struct nand_ecclayout nand_oob_128 = {
83 .eccbytes = 48,
84 .eccpos = {
85 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
88 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020093 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010094};
95
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020096static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +020097 int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020099static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
100 struct mtd_oob_ops *ops);
101
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200102/*
Joe Perches8e87d782008-02-03 17:22:34 +0200103 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200104 * compiled away when LED support is disabled.
105 */
106DEFINE_LED_TRIGGER(nand_led_trigger);
107
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530108static int check_offs_len(struct mtd_info *mtd,
109 loff_t ofs, uint64_t len)
110{
111 struct nand_chip *chip = mtd->priv;
112 int ret = 0;
113
114 /* Start address must align on block boundary */
115 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700116 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530117 ret = -EINVAL;
118 }
119
120 /* Length must align on block boundary */
121 if (len & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700122 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530123 ret = -EINVAL;
124 }
125
126 /* Do not allow past end of device */
127 if (ofs + len > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -0700128 pr_debug("%s: past end of device\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530129 ret = -EINVAL;
130 }
131
132 return ret;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
136 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700137 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000138 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700139 * Deselect, release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100141static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200143 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* De-select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200146 chip->select_chip(mtd, -1);
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100147
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200148 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200149 spin_lock(&chip->controller->lock);
150 chip->controller->active = NULL;
151 chip->state = FL_READY;
152 wake_up(&chip->controller->wq);
153 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/**
157 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700158 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700160 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200162static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200164 struct nand_chip *chip = mtd->priv;
165 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700170 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700171 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700173 * Default read function for 16bit buswidth with endianness conversion.
174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200176static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200178 struct nand_chip *chip = mtd->priv;
179 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700184 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700186 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188static u16 nand_read_word(struct mtd_info *mtd)
189{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190 struct nand_chip *chip = mtd->priv;
191 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700196 * @mtd: MTD device structure
197 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Default select function for 1 chip devices.
200 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200201static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200203 struct nand_chip *chip = mtd->priv;
204
205 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200207 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211
212 default:
213 BUG();
214 }
215}
216
217/**
218 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700219 * @mtd: MTD device structure
220 * @buf: data buffer
221 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700223 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200225static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200228 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
David Woodhousee0c7d762006-05-13 18:07:53 +0100230 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200231 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000235 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700236 * @mtd: MTD device structure
237 * @buf: buffer to store date
238 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700240 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200242static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200245 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Woodhousee0c7d762006-05-13 18:07:53 +0100247 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200248 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000252 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700253 * @mtd: MTD device structure
254 * @buf: buffer containing the data to compare
255 * @len: number of bytes to compare
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700257 * Default verify function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200259static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Woodhousee0c7d762006-05-13 18:07:53 +0100264 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200265 if (buf[i] != readb(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return 0;
268}
269
270/**
271 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700272 * @mtd: MTD device structure
273 * @buf: data buffer
274 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700276 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200278static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200281 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 u16 *p = (u16 *) buf;
283 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000284
David Woodhousee0c7d762006-05-13 18:07:53 +0100285 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000291 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700292 * @mtd: MTD device structure
293 * @buf: buffer to store date
294 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700296 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200298static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200301 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 u16 *p = (u16 *) buf;
303 len >>= 1;
304
David Woodhousee0c7d762006-05-13 18:07:53 +0100305 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700311 * @mtd: MTD device structure
312 * @buf: buffer containing the data to compare
313 * @len: number of bytes to compare
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700315 * Default verify function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200317static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200320 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 u16 *p = (u16 *) buf;
322 len >>= 1;
323
David Woodhousee0c7d762006-05-13 18:07:53 +0100324 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200325 if (p[i] != readw(chip->IO_ADDR_R))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -EFAULT;
327
328 return 0;
329}
330
331/**
332 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * @mtd: MTD device structure
334 * @ofs: offset from device start
335 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000337 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
340{
Brian Norriscdbec052012-01-13 18:11:48 -0800341 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200342 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 u16 bad;
344
Brian Norris5fb15492011-05-31 16:31:21 -0700345 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700346 ofs += mtd->erasesize - mtd->writesize;
347
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100348 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200351 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200353 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200356 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Brian Norriscdbec052012-01-13 18:11:48 -0800359 do {
360 if (chip->options & NAND_BUSWIDTH_16) {
361 chip->cmdfunc(mtd, NAND_CMD_READOOB,
362 chip->badblockpos & 0xFE, page);
363 bad = cpu_to_le16(chip->read_word(mtd));
364 if (chip->badblockpos & 0x1)
365 bad >>= 8;
366 else
367 bad &= 0xFF;
368 } else {
369 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
370 page);
371 bad = chip->read_byte(mtd);
372 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000373
Brian Norriscdbec052012-01-13 18:11:48 -0800374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
378 ofs += mtd->writesize;
379 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
380 i++;
381 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200382
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200383 if (getchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 nand_release_device(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return res;
387}
388
389/**
390 * nand_default_block_markbad - [DEFAULT] mark a block bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700391 * @mtd: MTD device structure
392 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394 * This is the default implementation, which can be overridden by a hardware
395 * specific driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396*/
397static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200399 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200400 uint8_t buf[2] = { 0, 0 };
Brian Norris02ed70b2010-07-21 16:53:47 -0700401 int block, ret, i = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000402
Brian Norris00918422012-01-13 18:11:47 -0800403 if (!(chip->bbt_options & NAND_BBT_USE_FLASH)) {
404 struct erase_info einfo;
405
406 /* Attempt erase before marking OOB */
407 memset(&einfo, 0, sizeof(einfo));
408 einfo.mtd = mtd;
409 einfo.addr = ofs;
410 einfo.len = 1 << chip->phys_erase_shift;
411 nand_erase_nand(mtd, &einfo, 0);
412 }
413
Brian Norris5fb15492011-05-31 16:31:21 -0700414 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700415 ofs += mtd->erasesize - mtd->writesize;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400418 block = (int)(ofs >> chip->bbt_erase_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200419 if (chip->bbt)
420 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Brian Norris8b6e50c2011-05-25 14:59:01 -0700422 /* Do we have a flash based bad block table? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700423 if (chip->bbt_options & NAND_BBT_USE_FLASH)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200424 ret = nand_update_bbt(mtd, ofs);
425 else {
Brian Norris4a89ff82011-08-30 18:45:45 -0700426 struct mtd_oob_ops ops;
427
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300428 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000429
Brian Norrisa0dc5522011-05-31 16:31:20 -0700430 /*
431 * Write to first two pages if necessary. If we write to more
432 * than one location, the first error encountered quits the
433 * procedure. We write two bytes per location, so we dont have
434 * to mess with 16 bit access.
Brian Norris02ed70b2010-07-21 16:53:47 -0700435 */
Brian Norris4a89ff82011-08-30 18:45:45 -0700436 ops.len = ops.ooblen = 2;
437 ops.datbuf = NULL;
438 ops.oobbuf = buf;
439 ops.ooboffs = chip->badblockpos & ~0x01;
Brian Norris23b1a992011-10-14 20:09:33 -0700440 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris02ed70b2010-07-21 16:53:47 -0700441 do {
Brian Norris4a89ff82011-08-30 18:45:45 -0700442 ret = nand_do_write_oob(mtd, ofs, &ops);
Brian Norris02ed70b2010-07-21 16:53:47 -0700443
Brian Norris02ed70b2010-07-21 16:53:47 -0700444 i++;
445 ofs += mtd->writesize;
Brian Norris5fb15492011-05-31 16:31:21 -0700446 } while (!ret && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE) &&
Brian Norris02ed70b2010-07-21 16:53:47 -0700447 i < 2);
448
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300449 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200450 }
451 if (!ret)
452 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300453
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200454 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000457/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700459 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700461 * Check, if the device is write protected. The function expects, that the
462 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100464static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200466 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200467
Brian Norris8b6e50c2011-05-25 14:59:01 -0700468 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200469 if (chip->options & NAND_BROKEN_XD)
470 return 0;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200473 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
474 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477/**
478 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700479 * @mtd: MTD device structure
480 * @ofs: offset from device start
481 * @getchip: 0, if the chip is already selected
482 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *
484 * Check, if the block is bad. Either by reading the bad block table or
485 * calling of the scan function.
486 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200487static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
488 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200490 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000491
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200492 if (!chip->bbt)
493 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100496 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200499/**
500 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700501 * @mtd: MTD device structure
502 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200503 *
504 * Helper function for nand_wait_ready used when needing to wait in interrupt
505 * context.
506 */
507static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
508{
509 struct nand_chip *chip = mtd->priv;
510 int i;
511
512 /* Wait for the device to get ready */
513 for (i = 0; i < timeo; i++) {
514 if (chip->dev_ready(mtd))
515 break;
516 touch_softlockup_watchdog();
517 mdelay(1);
518 }
519}
520
Brian Norris7854d3f2011-06-23 14:12:08 -0700521/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100522void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000523{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200524 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100525 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000526
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200527 /* 400ms timeout */
528 if (in_interrupt() || oops_in_progress)
529 return panic_nand_wait_ready(mtd, 400);
530
Richard Purdie8fe833c2006-03-31 02:31:14 -0800531 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700532 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000533 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200534 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800535 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700536 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000537 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800538 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000539}
David Woodhouse4b648b02006-09-25 17:05:24 +0100540EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/**
543 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 * @mtd: MTD device structure
545 * @command: the command to be sent
546 * @column: the column address for this command, -1 if none
547 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700549 * Send command to NAND device. This function is used for small page devices
550 * (256/512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200552static void nand_command(struct mtd_info *mtd, unsigned int command,
553 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200555 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200556 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (command == NAND_CMD_SEQIN) {
560 int readcmd;
561
Joern Engel28318772006-05-22 23:18:05 +0200562 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200564 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 readcmd = NAND_CMD_READOOB;
566 } else if (column < 256) {
567 /* First 256 bytes --> READ0 */
568 readcmd = NAND_CMD_READ0;
569 } else {
570 column -= 256;
571 readcmd = NAND_CMD_READ1;
572 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200573 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200574 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200576 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Brian Norris8b6e50c2011-05-25 14:59:01 -0700578 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200579 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
580 /* Serially input address */
581 if (column != -1) {
582 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200583 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200585 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200586 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200588 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200589 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200590 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200591 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200592 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200593 if (chip->chipsize > (32 << 20))
594 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200595 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000597
598 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700599 * Program and erase have their own busy handlers status and sequential
600 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 case NAND_CMD_PAGEPROG:
605 case NAND_CMD_ERASE1:
606 case NAND_CMD_ERASE2:
607 case NAND_CMD_SEQIN:
608 case NAND_CMD_STATUS:
609 return;
610
611 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200612 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200614 udelay(chip->chip_delay);
615 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200616 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200617 chip->cmd_ctrl(mtd,
618 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200619 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
620 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return;
622
David Woodhousee0c7d762006-05-13 18:07:53 +0100623 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000625 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * If we don't have access to the busy pin, we apply the given
627 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100628 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200629 if (!chip->dev_ready) {
630 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700634 /*
635 * Apply this short delay always to ensure that we do wait tWB in
636 * any case on any machine.
637 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100638 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000639
640 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/**
644 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700645 * @mtd: MTD device structure
646 * @command: the command to be sent
647 * @column: the column address for this command, -1 if none
648 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200650 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700651 * devices. We don't have the separate regions as we have in the small page
652 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200654static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
655 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200657 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Emulate NAND_CMD_READOOB */
660 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200661 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 command = NAND_CMD_READ0;
663 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000664
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200665 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200666 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200667 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200670 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* Serially input address */
673 if (column != -1) {
674 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200675 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200677 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200678 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200679 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200682 chip->cmd_ctrl(mtd, page_addr, ctrl);
683 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200684 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200686 if (chip->chipsize > (128 << 20))
687 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200688 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200691 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000692
693 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700694 * Program and erase have their own busy handlers status, sequential
695 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case NAND_CMD_CACHEDPROG:
700 case NAND_CMD_PAGEPROG:
701 case NAND_CMD_ERASE1:
702 case NAND_CMD_ERASE2:
703 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200704 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000706 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return;
708
David A. Marlin30f464b2005-01-17 18:35:25 +0000709 case NAND_CMD_STATUS_ERROR:
710 case NAND_CMD_STATUS_ERROR0:
711 case NAND_CMD_STATUS_ERROR1:
712 case NAND_CMD_STATUS_ERROR2:
713 case NAND_CMD_STATUS_ERROR3:
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 /* Read error status commands require only a short delay */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200715 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000716 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200719 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200722 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
723 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
724 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
725 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200726 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
727 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return;
729
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200730 case NAND_CMD_RNDOUT:
731 /* No ready / busy check necessary */
732 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
733 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
734 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
735 NAND_NCE | NAND_CTRL_CHANGE);
736 return;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200739 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
740 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
741 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
742 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743
David Woodhousee0c7d762006-05-13 18:07:53 +0100744 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000746 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200750 if (!chip->dev_ready) {
751 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000755
Brian Norris8b6e50c2011-05-25 14:59:01 -0700756 /*
757 * Apply this short delay always to ensure that we do wait tWB in
758 * any case on any machine.
759 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100760 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000761
762 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200766 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700767 * @chip: the nand chip descriptor
768 * @mtd: MTD device structure
769 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200770 *
771 * Used when in panic, no locks are taken.
772 */
773static void panic_nand_get_device(struct nand_chip *chip,
774 struct mtd_info *mtd, int new_state)
775{
Brian Norris7854d3f2011-06-23 14:12:08 -0700776 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200777 chip->controller->active = chip;
778 chip->state = new_state;
779}
780
781/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700783 * @chip: the nand chip descriptor
784 * @mtd: MTD device structure
785 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
787 * Get the device and lock it for exclusive access
788 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200789static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200790nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200792 spinlock_t *lock = &chip->controller->lock;
793 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100794 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200795retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100796 spin_lock(lock);
797
vimal singhb8b3ee92009-07-09 20:41:22 +0530798 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200799 if (!chip->controller->active)
800 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200801
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200802 if (chip->controller->active == chip && chip->state == FL_READY) {
803 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100804 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100805 return 0;
806 }
807 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800808 if (chip->controller->active->state == FL_PM_SUSPENDED) {
809 chip->state = FL_PM_SUSPENDED;
810 spin_unlock(lock);
811 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800812 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100813 }
814 set_current_state(TASK_UNINTERRUPTIBLE);
815 add_wait_queue(wq, &wait);
816 spin_unlock(lock);
817 schedule();
818 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto retry;
820}
821
822/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700823 * panic_nand_wait - [GENERIC] wait until the command is done
824 * @mtd: MTD device structure
825 * @chip: NAND chip structure
826 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200827 *
828 * Wait for command done. This is a helper function for nand_wait used when
829 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400830 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200831 */
832static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
833 unsigned long timeo)
834{
835 int i;
836 for (i = 0; i < timeo; i++) {
837 if (chip->dev_ready) {
838 if (chip->dev_ready(mtd))
839 break;
840 } else {
841 if (chip->read_byte(mtd) & NAND_STATUS_READY)
842 break;
843 }
844 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200845 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200846}
847
848/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700849 * nand_wait - [DEFAULT] wait until the command is done
850 * @mtd: MTD device structure
851 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700853 * Wait for command done. This applies to erase and program only. Erase can
854 * take up to 400ms and program up to 20ms according to general NAND and
855 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700856 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200857static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859
David Woodhousee0c7d762006-05-13 18:07:53 +0100860 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200861 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100864 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100866 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Richard Purdie8fe833c2006-03-31 02:31:14 -0800868 led_trigger_event(nand_led_trigger, LED_FULL);
869
Brian Norris8b6e50c2011-05-25 14:59:01 -0700870 /*
871 * Apply this short delay always to ensure that we do wait tWB in any
872 * case on any machine.
873 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100874 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200876 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
877 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000878 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200879 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200881 if (in_interrupt() || oops_in_progress)
882 panic_nand_wait(mtd, chip, timeo);
883 else {
884 while (time_before(jiffies, timeo)) {
885 if (chip->dev_ready) {
886 if (chip->dev_ready(mtd))
887 break;
888 } else {
889 if (chip->read_byte(mtd) & NAND_STATUS_READY)
890 break;
891 }
892 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800895 led_trigger_event(nand_led_trigger, LED_OFF);
896
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200897 status = (int)chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return status;
899}
900
901/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700902 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700903 * @mtd: mtd info
904 * @ofs: offset to start unlock from
905 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700906 * @invert: when = 0, unlock the range of blocks within the lower and
907 * upper boundary address
908 * when = 1, unlock the range of blocks outside the boundaries
909 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530910 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700911 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530912 */
913static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
914 uint64_t len, int invert)
915{
916 int ret = 0;
917 int status, page;
918 struct nand_chip *chip = mtd->priv;
919
920 /* Submit address of first page to unlock */
921 page = ofs >> chip->page_shift;
922 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
923
924 /* Submit address of last page to unlock */
925 page = (ofs + len) >> chip->page_shift;
926 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
927 (page | invert) & chip->pagemask);
928
929 /* Call wait ready function */
930 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530931 /* See if device thinks it succeeded */
932 if (status & 0x01) {
Brian Norris289c0522011-07-19 10:06:09 -0700933 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530934 __func__, status);
935 ret = -EIO;
936 }
937
938 return ret;
939}
940
941/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700942 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700943 * @mtd: mtd info
944 * @ofs: offset to start unlock from
945 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530946 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700947 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530948 */
949int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
950{
951 int ret = 0;
952 int chipnr;
953 struct nand_chip *chip = mtd->priv;
954
Brian Norris289c0522011-07-19 10:06:09 -0700955 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530956 __func__, (unsigned long long)ofs, len);
957
958 if (check_offs_len(mtd, ofs, len))
959 ret = -EINVAL;
960
961 /* Align to last block address if size addresses end of the device */
962 if (ofs + len == mtd->size)
963 len -= mtd->erasesize;
964
965 nand_get_device(chip, mtd, FL_UNLOCKING);
966
967 /* Shift to get chip number */
968 chipnr = ofs >> chip->chip_shift;
969
970 chip->select_chip(mtd, chipnr);
971
972 /* Check, if it is write protected */
973 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700974 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530975 __func__);
976 ret = -EIO;
977 goto out;
978 }
979
980 ret = __nand_unlock(mtd, ofs, len, 0);
981
982out:
Vimal Singh7d70f332010-02-08 15:50:49 +0530983 nand_release_device(mtd);
984
985 return ret;
986}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200987EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530988
989/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700990 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700991 * @mtd: mtd info
992 * @ofs: offset to start unlock from
993 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530994 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700995 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
996 * have this feature, but it allows only to lock all blocks, not for specified
997 * range for block. Implementing 'lock' feature by making use of 'unlock', for
998 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530999 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301001 */
1002int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1003{
1004 int ret = 0;
1005 int chipnr, status, page;
1006 struct nand_chip *chip = mtd->priv;
1007
Brian Norris289c0522011-07-19 10:06:09 -07001008 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301009 __func__, (unsigned long long)ofs, len);
1010
1011 if (check_offs_len(mtd, ofs, len))
1012 ret = -EINVAL;
1013
1014 nand_get_device(chip, mtd, FL_LOCKING);
1015
1016 /* Shift to get chip number */
1017 chipnr = ofs >> chip->chip_shift;
1018
1019 chip->select_chip(mtd, chipnr);
1020
1021 /* Check, if it is write protected */
1022 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001023 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301024 __func__);
1025 status = MTD_ERASE_FAILED;
1026 ret = -EIO;
1027 goto out;
1028 }
1029
1030 /* Submit address of first page to lock */
1031 page = ofs >> chip->page_shift;
1032 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1033
1034 /* Call wait ready function */
1035 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301036 /* See if device thinks it succeeded */
1037 if (status & 0x01) {
Brian Norris289c0522011-07-19 10:06:09 -07001038 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301039 __func__, status);
1040 ret = -EIO;
1041 goto out;
1042 }
1043
1044 ret = __nand_unlock(mtd, ofs, len, 0x1);
1045
1046out:
Vimal Singh7d70f332010-02-08 15:50:49 +05301047 nand_release_device(mtd);
1048
1049 return ret;
1050}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001051EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301052
1053/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001054 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001055 * @mtd: mtd info structure
1056 * @chip: nand chip info structure
1057 * @buf: buffer to store read data
1058 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001059 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001060 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001061 */
1062static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001063 uint8_t *buf, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001064{
1065 chip->read_buf(mtd, buf, mtd->writesize);
1066 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1067 return 0;
1068}
1069
1070/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001071 * nand_read_page_raw_syndrome - [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 *
1077 * We need a special oob layout and handling even when OOB isn't used.
1078 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001079static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1080 struct nand_chip *chip,
1081 uint8_t *buf, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001082{
1083 int eccsize = chip->ecc.size;
1084 int eccbytes = chip->ecc.bytes;
1085 uint8_t *oob = chip->oob_poi;
1086 int steps, size;
1087
1088 for (steps = chip->ecc.steps; steps > 0; steps--) {
1089 chip->read_buf(mtd, buf, eccsize);
1090 buf += eccsize;
1091
1092 if (chip->ecc.prepad) {
1093 chip->read_buf(mtd, oob, chip->ecc.prepad);
1094 oob += chip->ecc.prepad;
1095 }
1096
1097 chip->read_buf(mtd, oob, eccbytes);
1098 oob += eccbytes;
1099
1100 if (chip->ecc.postpad) {
1101 chip->read_buf(mtd, oob, chip->ecc.postpad);
1102 oob += chip->ecc.postpad;
1103 }
1104 }
1105
1106 size = mtd->oobsize - (oob - chip->oob_poi);
1107 if (size)
1108 chip->read_buf(mtd, oob, size);
1109
1110 return 0;
1111}
1112
1113/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001114 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001115 * @mtd: mtd info structure
1116 * @chip: nand chip info structure
1117 * @buf: buffer to store read data
1118 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001119 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001120static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001121 uint8_t *buf, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001123 int i, eccsize = chip->ecc.size;
1124 int eccbytes = chip->ecc.bytes;
1125 int eccsteps = chip->ecc.steps;
1126 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001127 uint8_t *ecc_calc = chip->buffers->ecccalc;
1128 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001129 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001130
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001131 chip->ecc.read_page_raw(mtd, chip, buf, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001132
1133 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1134 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1135
1136 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001137 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001138
1139 eccsteps = chip->ecc.steps;
1140 p = buf;
1141
1142 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1143 int stat;
1144
1145 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001146 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001147 mtd->ecc_stats.failed++;
1148 else
1149 mtd->ecc_stats.corrected += stat;
1150 }
1151 return 0;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001155 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001156 * @mtd: mtd info structure
1157 * @chip: nand chip info structure
1158 * @data_offs: offset of requested data within the page
1159 * @readlen: data length
1160 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001161 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001162static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1163 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001164{
1165 int start_step, end_step, num_steps;
1166 uint32_t *eccpos = chip->ecc.layout->eccpos;
1167 uint8_t *p;
1168 int data_col_addr, i, gaps = 0;
1169 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1170 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001171 int index = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001172
Brian Norris7854d3f2011-06-23 14:12:08 -07001173 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001174 start_step = data_offs / chip->ecc.size;
1175 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1176 num_steps = end_step - start_step + 1;
1177
Brian Norris8b6e50c2011-05-25 14:59:01 -07001178 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001179 datafrag_len = num_steps * chip->ecc.size;
1180 eccfrag_len = num_steps * chip->ecc.bytes;
1181
1182 data_col_addr = start_step * chip->ecc.size;
1183 /* If we read not a page aligned data */
1184 if (data_col_addr != 0)
1185 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1186
1187 p = bufpoi + data_col_addr;
1188 chip->read_buf(mtd, p, datafrag_len);
1189
Brian Norris8b6e50c2011-05-25 14:59:01 -07001190 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001191 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1192 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1193
Brian Norris8b6e50c2011-05-25 14:59:01 -07001194 /*
1195 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001196 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001197 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001198 for (i = 0; i < eccfrag_len - 1; i++) {
1199 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1200 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1201 gaps = 1;
1202 break;
1203 }
1204 }
1205 if (gaps) {
1206 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1207 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1208 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001209 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001210 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001211 * about buswidth alignment in read_buf.
1212 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001213 index = start_step * chip->ecc.bytes;
1214
1215 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001216 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001217 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001218 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001219 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001220 aligned_len++;
1221
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001222 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1223 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001224 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1225 }
1226
1227 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001228 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001229
1230 p = bufpoi + data_col_addr;
1231 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1232 int stat;
1233
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001234 stat = chip->ecc.correct(mtd, p,
1235 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Baruch Siach12c8eb92010-08-09 07:20:23 +03001236 if (stat < 0)
Alexey Korolev3d459552008-05-15 17:23:18 +01001237 mtd->ecc_stats.failed++;
1238 else
1239 mtd->ecc_stats.corrected += stat;
1240 }
1241 return 0;
1242}
1243
1244/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001245 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001246 * @mtd: mtd info structure
1247 * @chip: nand chip info structure
1248 * @buf: buffer to store read data
1249 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001250 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001251 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001252 */
1253static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001254 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001255{
1256 int i, eccsize = chip->ecc.size;
1257 int eccbytes = chip->ecc.bytes;
1258 int eccsteps = chip->ecc.steps;
1259 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001260 uint8_t *ecc_calc = chip->buffers->ecccalc;
1261 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001262 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001263
1264 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1265 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1266 chip->read_buf(mtd, p, eccsize);
1267 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1268 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001269 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001270
1271 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001272 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001273
1274 eccsteps = chip->ecc.steps;
1275 p = buf;
1276
1277 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1278 int stat;
1279
1280 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001281 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001282 mtd->ecc_stats.failed++;
1283 else
1284 mtd->ecc_stats.corrected += stat;
1285 }
1286 return 0;
1287}
1288
1289/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001290 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001291 * @mtd: mtd info structure
1292 * @chip: nand chip info structure
1293 * @buf: buffer to store read data
1294 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001295 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001296 * Hardware ECC for large page chips, require OOB to be read first. For this
1297 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1298 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1299 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1300 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001301 */
1302static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1303 struct nand_chip *chip, uint8_t *buf, int page)
1304{
1305 int i, eccsize = chip->ecc.size;
1306 int eccbytes = chip->ecc.bytes;
1307 int eccsteps = chip->ecc.steps;
1308 uint8_t *p = buf;
1309 uint8_t *ecc_code = chip->buffers->ecccode;
1310 uint32_t *eccpos = chip->ecc.layout->eccpos;
1311 uint8_t *ecc_calc = chip->buffers->ecccalc;
1312
1313 /* Read the OOB area first */
1314 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1315 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1316 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1317
1318 for (i = 0; i < chip->ecc.total; i++)
1319 ecc_code[i] = chip->oob_poi[eccpos[i]];
1320
1321 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1322 int stat;
1323
1324 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1325 chip->read_buf(mtd, p, eccsize);
1326 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1327
1328 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1329 if (stat < 0)
1330 mtd->ecc_stats.failed++;
1331 else
1332 mtd->ecc_stats.corrected += stat;
1333 }
1334 return 0;
1335}
1336
1337/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001338 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001339 * @mtd: mtd info structure
1340 * @chip: nand chip info structure
1341 * @buf: buffer to store read data
1342 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001343 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001344 * The hw generator calculates the error syndrome automatically. Therefore we
1345 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001346 */
1347static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001348 uint8_t *buf, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001349{
1350 int i, eccsize = chip->ecc.size;
1351 int eccbytes = chip->ecc.bytes;
1352 int eccsteps = chip->ecc.steps;
1353 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001354 uint8_t *oob = chip->oob_poi;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001355
1356 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1357 int stat;
1358
1359 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1360 chip->read_buf(mtd, p, eccsize);
1361
1362 if (chip->ecc.prepad) {
1363 chip->read_buf(mtd, oob, chip->ecc.prepad);
1364 oob += chip->ecc.prepad;
1365 }
1366
1367 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1368 chip->read_buf(mtd, oob, eccbytes);
1369 stat = chip->ecc.correct(mtd, p, oob, NULL);
1370
Matt Reimerc32b8dc2007-10-17 14:33:23 -07001371 if (stat < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001372 mtd->ecc_stats.failed++;
1373 else
1374 mtd->ecc_stats.corrected += stat;
1375
1376 oob += eccbytes;
1377
1378 if (chip->ecc.postpad) {
1379 chip->read_buf(mtd, oob, chip->ecc.postpad);
1380 oob += chip->ecc.postpad;
1381 }
1382 }
1383
1384 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001385 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001386 if (i)
1387 chip->read_buf(mtd, oob, i);
1388
1389 return 0;
1390}
1391
1392/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001393 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001394 * @chip: nand chip structure
1395 * @oob: oob destination address
1396 * @ops: oob ops structure
1397 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001398 */
1399static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001400 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001401{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001402 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001403
Brian Norris0612b9d2011-08-30 18:45:40 -07001404 case MTD_OPS_PLACE_OOB:
1405 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001406 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1407 return oob + len;
1408
Brian Norris0612b9d2011-08-30 18:45:40 -07001409 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001410 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001411 uint32_t boffs = 0, roffs = ops->ooboffs;
1412 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001413
Florian Fainellif8ac0412010-09-07 13:23:43 +02001414 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001415 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001416 if (unlikely(roffs)) {
1417 if (roffs >= free->length) {
1418 roffs -= free->length;
1419 continue;
1420 }
1421 boffs = free->offset + roffs;
1422 bytes = min_t(size_t, len,
1423 (free->length - roffs));
1424 roffs = 0;
1425 } else {
1426 bytes = min_t(size_t, len, free->length);
1427 boffs = free->offset;
1428 }
1429 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001430 oob += bytes;
1431 }
1432 return oob;
1433 }
1434 default:
1435 BUG();
1436 }
1437 return NULL;
1438}
1439
1440/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001441 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001442 * @mtd: MTD device structure
1443 * @from: offset to read from
1444 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001445 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001446 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001447 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001448static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1449 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001450{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001451 int chipnr, page, realpage, col, bytes, aligned;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001452 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001453 struct mtd_ecc_stats stats;
1454 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1455 int sndcmd = 1;
1456 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001457 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001458 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001459 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001460 mtd->oobavail : mtd->oobsize;
1461
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001462 uint8_t *bufpoi, *oob, *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001464 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001466 chipnr = (int)(from >> chip->chip_shift);
1467 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001469 realpage = (int)(from >> chip->page_shift);
1470 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001472 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001474 buf = ops->datbuf;
1475 oob = ops->oobbuf;
1476
Florian Fainellif8ac0412010-09-07 13:23:43 +02001477 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001478 bytes = min(mtd->writesize - col, readlen);
1479 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001480
Brian Norris8b6e50c2011-05-25 14:59:01 -07001481 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001482 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001483 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001485 if (likely(sndcmd)) {
1486 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1487 sndcmd = 0;
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001490 /* Now read the page into the buffer */
Brian Norris0612b9d2011-08-30 18:45:40 -07001491 if (unlikely(ops->mode == MTD_OPS_RAW))
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001492 ret = chip->ecc.read_page_raw(mtd, chip,
1493 bufpoi, page);
Alexey Korolev3d459552008-05-15 17:23:18 +01001494 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001495 ret = chip->ecc.read_subpage(mtd, chip,
1496 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001497 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001498 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1499 page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001500 if (ret < 0) {
1501 if (!aligned)
1502 /* Invalidate page cache */
1503 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001504 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001505 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001506
1507 /* Transfer not aligned data */
1508 if (!aligned) {
Artem Bityutskiyc1194c72010-09-03 22:01:16 +03001509 if (!NAND_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001510 !(mtd->ecc_stats.failed - stats.failed) &&
1511 (ops->mode != MTD_OPS_RAW))
Alexey Korolev3d459552008-05-15 17:23:18 +01001512 chip->pagebuf = realpage;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001513 else
1514 /* Invalidate page cache */
1515 chip->pagebuf = -1;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001516 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001518
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001519 buf += bytes;
1520
1521 if (unlikely(oob)) {
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001522
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001523 int toread = min(oobreadlen, max_oobsize);
1524
1525 if (toread) {
1526 oob = nand_transfer_oob(chip,
1527 oob, ops, toread);
1528 oobreadlen -= toread;
1529 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001530 }
1531
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001532 if (!(chip->options & NAND_NO_READRDY)) {
1533 /*
1534 * Apply delay or wait for ready/busy pin. Do
1535 * this before the AUTOINCR check, so no
1536 * problems arise if a chip which does auto
1537 * increment is marked as NOAUTOINCR by the
1538 * board driver.
1539 */
1540 if (!chip->dev_ready)
1541 udelay(chip->chip_delay);
1542 else
1543 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001545 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001546 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001547 buf += bytes;
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001550 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001551
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001552 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Brian Norris8b6e50c2011-05-25 14:59:01 -07001555 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 col = 0;
1557 /* Increment page address */
1558 realpage++;
1559
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001560 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Check, if we cross a chip boundary */
1562 if (!page) {
1563 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001564 chip->select_chip(mtd, -1);
1565 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001567
Brian Norris8b6e50c2011-05-25 14:59:01 -07001568 /*
1569 * Check, if the chip supports auto page increment or if we
1570 * have hit a block boundary.
David Woodhousee0c7d762006-05-13 18:07:53 +01001571 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001572 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001573 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001576 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001577 if (oob)
1578 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001580 if (ret)
1581 return ret;
1582
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001583 if (mtd->ecc_stats.failed - stats.failed)
1584 return -EBADMSG;
1585
1586 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001587}
1588
1589/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001590 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001591 * @mtd: MTD device structure
1592 * @from: offset to read from
1593 * @len: number of bytes to read
1594 * @retlen: pointer to variable to store the number of read bytes
1595 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001596 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001597 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001598 */
1599static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1600 size_t *retlen, uint8_t *buf)
1601{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001602 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07001603 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001604 int ret;
1605
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001606 /* Do not allow reads past end of device */
1607 if ((from + len) > mtd->size)
1608 return -EINVAL;
1609 if (!len)
1610 return 0;
1611
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001612 nand_get_device(chip, mtd, FL_READING);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001613
Brian Norris4a89ff82011-08-30 18:45:45 -07001614 ops.len = len;
1615 ops.datbuf = buf;
1616 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07001617 ops.mode = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001618
Brian Norris4a89ff82011-08-30 18:45:45 -07001619 ret = nand_do_read_ops(mtd, from, &ops);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001620
Brian Norris4a89ff82011-08-30 18:45:45 -07001621 *retlen = ops.retlen;
Richard Purdie7fd5aec2006-08-27 01:23:33 -07001622
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001623 nand_release_device(mtd);
1624
1625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001629 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001630 * @mtd: mtd info structure
1631 * @chip: nand chip info structure
1632 * @page: page number to read
1633 * @sndcmd: flag whether to issue read command or not
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001634 */
1635static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1636 int page, int sndcmd)
1637{
1638 if (sndcmd) {
1639 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1640 sndcmd = 0;
1641 }
1642 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1643 return sndcmd;
1644}
1645
1646/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001647 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001648 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001649 * @mtd: mtd info structure
1650 * @chip: nand chip info structure
1651 * @page: page number to read
1652 * @sndcmd: flag whether to issue read command or not
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001653 */
1654static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1655 int page, int sndcmd)
1656{
1657 uint8_t *buf = chip->oob_poi;
1658 int length = mtd->oobsize;
1659 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1660 int eccsize = chip->ecc.size;
1661 uint8_t *bufpoi = buf;
1662 int i, toread, sndrnd = 0, pos;
1663
1664 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1665 for (i = 0; i < chip->ecc.steps; i++) {
1666 if (sndrnd) {
1667 pos = eccsize + i * (eccsize + chunk);
1668 if (mtd->writesize > 512)
1669 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1670 else
1671 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1672 } else
1673 sndrnd = 1;
1674 toread = min_t(int, length, chunk);
1675 chip->read_buf(mtd, bufpoi, toread);
1676 bufpoi += toread;
1677 length -= toread;
1678 }
1679 if (length > 0)
1680 chip->read_buf(mtd, bufpoi, length);
1681
1682 return 1;
1683}
1684
1685/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001686 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001687 * @mtd: mtd info structure
1688 * @chip: nand chip info structure
1689 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001690 */
1691static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1692 int page)
1693{
1694 int status = 0;
1695 const uint8_t *buf = chip->oob_poi;
1696 int length = mtd->oobsize;
1697
1698 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1699 chip->write_buf(mtd, buf, length);
1700 /* Send command to program the OOB data */
1701 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1702
1703 status = chip->waitfunc(mtd, chip);
1704
Savin Zlobec0d420f92006-06-21 11:51:20 +02001705 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001706}
1707
1708/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001709 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001710 * with syndrome - only for large page flash
1711 * @mtd: mtd info structure
1712 * @chip: nand chip info structure
1713 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001714 */
1715static int nand_write_oob_syndrome(struct mtd_info *mtd,
1716 struct nand_chip *chip, int page)
1717{
1718 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1719 int eccsize = chip->ecc.size, length = mtd->oobsize;
1720 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1721 const uint8_t *bufpoi = chip->oob_poi;
1722
1723 /*
1724 * data-ecc-data-ecc ... ecc-oob
1725 * or
1726 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1727 */
1728 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1729 pos = steps * (eccsize + chunk);
1730 steps = 0;
1731 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001732 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001733
1734 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1735 for (i = 0; i < steps; i++) {
1736 if (sndcmd) {
1737 if (mtd->writesize <= 512) {
1738 uint32_t fill = 0xFFFFFFFF;
1739
1740 len = eccsize;
1741 while (len > 0) {
1742 int num = min_t(int, len, 4);
1743 chip->write_buf(mtd, (uint8_t *)&fill,
1744 num);
1745 len -= num;
1746 }
1747 } else {
1748 pos = eccsize + i * (eccsize + chunk);
1749 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1750 }
1751 } else
1752 sndcmd = 1;
1753 len = min_t(int, length, chunk);
1754 chip->write_buf(mtd, bufpoi, len);
1755 bufpoi += len;
1756 length -= len;
1757 }
1758 if (length > 0)
1759 chip->write_buf(mtd, bufpoi, length);
1760
1761 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1762 status = chip->waitfunc(mtd, chip);
1763
1764 return status & NAND_STATUS_FAIL ? -EIO : 0;
1765}
1766
1767/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001768 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001769 * @mtd: MTD device structure
1770 * @from: offset to read from
1771 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001773 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001775static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1776 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001778 int page, realpage, chipnr, sndcmd = 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001779 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001780 struct mtd_ecc_stats stats;
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001781 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Vitaly Wool70145682006-11-03 18:20:38 +03001782 int readlen = ops->ooblen;
1783 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001784 uint8_t *buf = ops->oobbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Brian Norris289c0522011-07-19 10:06:09 -07001786 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301787 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Brian Norris041e4572011-06-23 16:45:24 -07001789 stats = mtd->ecc_stats;
1790
Brian Norris0612b9d2011-08-30 18:45:40 -07001791 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001792 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001793 else
1794 len = mtd->oobsize;
1795
1796 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001797 pr_debug("%s: attempt to start read outside oob\n",
1798 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001799 return -EINVAL;
1800 }
1801
1802 /* Do not allow reads past end of device */
1803 if (unlikely(from >= mtd->size ||
1804 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1805 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001806 pr_debug("%s: attempt to read beyond end of device\n",
1807 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001808 return -EINVAL;
1809 }
Vitaly Wool70145682006-11-03 18:20:38 +03001810
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001811 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001812 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001814 /* Shift to get page */
1815 realpage = (int)(from >> chip->page_shift);
1816 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Florian Fainellif8ac0412010-09-07 13:23:43 +02001818 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001819 if (ops->mode == MTD_OPS_RAW)
Brian Norrisc46f6482011-08-30 18:45:38 -07001820 sndcmd = chip->ecc.read_oob_raw(mtd, chip, page, sndcmd);
1821 else
1822 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
Vitaly Wool70145682006-11-03 18:20:38 +03001823
1824 len = min(len, readlen);
1825 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001826
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001827 if (!(chip->options & NAND_NO_READRDY)) {
1828 /*
1829 * Apply delay or wait for ready/busy pin. Do this
1830 * before the AUTOINCR check, so no problems arise if a
1831 * chip which does auto increment is marked as
1832 * NOAUTOINCR by the board driver.
Thomas Gleixner19870da2005-07-15 14:53:51 +01001833 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001834 if (!chip->dev_ready)
1835 udelay(chip->chip_delay);
Thomas Gleixner19870da2005-07-15 14:53:51 +01001836 else
1837 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001839
Vitaly Wool70145682006-11-03 18:20:38 +03001840 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001841 if (!readlen)
1842 break;
1843
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001844 /* Increment page address */
1845 realpage++;
1846
1847 page = realpage & chip->pagemask;
1848 /* Check, if we cross a chip boundary */
1849 if (!page) {
1850 chipnr++;
1851 chip->select_chip(mtd, -1);
1852 chip->select_chip(mtd, chipnr);
1853 }
1854
Brian Norris8b6e50c2011-05-25 14:59:01 -07001855 /*
1856 * Check, if the chip supports auto page increment or if we
1857 * have hit a block boundary.
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001858 */
1859 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1860 sndcmd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
Vitaly Wool70145682006-11-03 18:20:38 +03001863 ops->oobretlen = ops->ooblen;
Brian Norris041e4572011-06-23 16:45:24 -07001864
1865 if (mtd->ecc_stats.failed - stats.failed)
1866 return -EBADMSG;
1867
1868 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001872 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001873 * @mtd: MTD device structure
1874 * @from: offset to read from
1875 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001877 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001879static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1880 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001882 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001883 int ret = -ENOTSUPP;
1884
1885 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001888 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001889 pr_debug("%s: attempt to read beyond end of device\n",
1890 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return -EINVAL;
1892 }
1893
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001894 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Florian Fainellif8ac0412010-09-07 13:23:43 +02001896 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001897 case MTD_OPS_PLACE_OOB:
1898 case MTD_OPS_AUTO_OOB:
1899 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001900 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001901
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001902 default:
1903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001906 if (!ops->datbuf)
1907 ret = nand_do_read_oob(mtd, from, ops);
1908 else
1909 ret = nand_do_read_ops(mtd, from, ops);
1910
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001911out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001913 return ret;
1914}
1915
1916
1917/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001918 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001919 * @mtd: mtd info structure
1920 * @chip: nand chip info structure
1921 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001922 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001923 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001924 */
1925static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1926 const uint8_t *buf)
1927{
1928 chip->write_buf(mtd, buf, mtd->writesize);
1929 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001932/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001933 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001934 * @mtd: mtd info structure
1935 * @chip: nand chip info structure
1936 * @buf: data buffer
David Brownell52ff49d2009-03-04 12:01:36 -08001937 *
1938 * We need a special oob layout and handling even when ECC isn't checked.
1939 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001940static void nand_write_page_raw_syndrome(struct mtd_info *mtd,
1941 struct nand_chip *chip,
1942 const uint8_t *buf)
David Brownell52ff49d2009-03-04 12:01:36 -08001943{
1944 int eccsize = chip->ecc.size;
1945 int eccbytes = chip->ecc.bytes;
1946 uint8_t *oob = chip->oob_poi;
1947 int steps, size;
1948
1949 for (steps = chip->ecc.steps; steps > 0; steps--) {
1950 chip->write_buf(mtd, buf, eccsize);
1951 buf += eccsize;
1952
1953 if (chip->ecc.prepad) {
1954 chip->write_buf(mtd, oob, chip->ecc.prepad);
1955 oob += chip->ecc.prepad;
1956 }
1957
1958 chip->read_buf(mtd, oob, eccbytes);
1959 oob += eccbytes;
1960
1961 if (chip->ecc.postpad) {
1962 chip->write_buf(mtd, oob, chip->ecc.postpad);
1963 oob += chip->ecc.postpad;
1964 }
1965 }
1966
1967 size = mtd->oobsize - (oob - chip->oob_poi);
1968 if (size)
1969 chip->write_buf(mtd, oob, size);
1970}
1971/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001972 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001973 * @mtd: mtd info structure
1974 * @chip: nand chip info structure
1975 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001976 */
1977static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1978 const uint8_t *buf)
1979{
1980 int i, eccsize = chip->ecc.size;
1981 int eccbytes = chip->ecc.bytes;
1982 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001983 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001984 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001985 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001986
Brian Norris7854d3f2011-06-23 14:12:08 -07001987 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001988 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1989 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001990
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001991 for (i = 0; i < chip->ecc.total; i++)
1992 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001993
Thomas Gleixner90424de2007-04-05 11:44:05 +02001994 chip->ecc.write_page_raw(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001995}
1996
1997/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001998 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001999 * @mtd: mtd info structure
2000 * @chip: nand chip info structure
2001 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002002 */
2003static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2004 const uint8_t *buf)
2005{
2006 int i, eccsize = chip->ecc.size;
2007 int eccbytes = chip->ecc.bytes;
2008 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002009 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002010 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002011 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002012
2013 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2014 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002015 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002016 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2017 }
2018
2019 for (i = 0; i < chip->ecc.total; i++)
2020 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2021
2022 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2023}
2024
2025/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002026 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002027 * @mtd: mtd info structure
2028 * @chip: nand chip info structure
2029 * @buf: data buffer
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002030 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002031 * The hw generator calculates the error syndrome automatically. Therefore we
2032 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002033 */
2034static void nand_write_page_syndrome(struct mtd_info *mtd,
2035 struct nand_chip *chip, const uint8_t *buf)
2036{
2037 int i, eccsize = chip->ecc.size;
2038 int eccbytes = chip->ecc.bytes;
2039 int eccsteps = chip->ecc.steps;
2040 const uint8_t *p = buf;
2041 uint8_t *oob = chip->oob_poi;
2042
2043 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2044
2045 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2046 chip->write_buf(mtd, p, eccsize);
2047
2048 if (chip->ecc.prepad) {
2049 chip->write_buf(mtd, oob, chip->ecc.prepad);
2050 oob += chip->ecc.prepad;
2051 }
2052
2053 chip->ecc.calculate(mtd, p, oob);
2054 chip->write_buf(mtd, oob, eccbytes);
2055 oob += eccbytes;
2056
2057 if (chip->ecc.postpad) {
2058 chip->write_buf(mtd, oob, chip->ecc.postpad);
2059 oob += chip->ecc.postpad;
2060 }
2061 }
2062
2063 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002064 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002065 if (i)
2066 chip->write_buf(mtd, oob, i);
2067}
2068
2069/**
David Woodhouse956e9442006-09-25 17:12:39 +01002070 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002071 * @mtd: MTD device structure
2072 * @chip: NAND chip descriptor
2073 * @buf: the data to write
2074 * @page: page number to write
2075 * @cached: cached programming
2076 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002077 */
2078static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
David Woodhouse956e9442006-09-25 17:12:39 +01002079 const uint8_t *buf, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002080{
2081 int status;
2082
2083 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2084
David Woodhouse956e9442006-09-25 17:12:39 +01002085 if (unlikely(raw))
2086 chip->ecc.write_page_raw(mtd, chip, buf);
2087 else
2088 chip->ecc.write_page(mtd, chip, buf);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002089
2090 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002091 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002092 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002093 */
2094 cached = 0;
2095
2096 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2097
2098 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002099 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002100 /*
2101 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002102 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002103 */
2104 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2105 status = chip->errstat(mtd, chip, FL_WRITING, status,
2106 page);
2107
2108 if (status & NAND_STATUS_FAIL)
2109 return -EIO;
2110 } else {
2111 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002112 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002113 }
2114
2115#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
2116 /* Send command to read back the data */
2117 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2118
2119 if (chip->verify_buf(mtd, buf, mtd->writesize))
2120 return -EIO;
2121#endif
2122 return 0;
2123}
2124
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002125/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002126 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002127 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002128 * @oob: oob data buffer
2129 * @len: oob data write length
2130 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002131 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002132static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2133 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002134{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002135 struct nand_chip *chip = mtd->priv;
2136
2137 /*
2138 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2139 * data from a previous OOB read.
2140 */
2141 memset(chip->oob_poi, 0xff, mtd->oobsize);
2142
Florian Fainellif8ac0412010-09-07 13:23:43 +02002143 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002144
Brian Norris0612b9d2011-08-30 18:45:40 -07002145 case MTD_OPS_PLACE_OOB:
2146 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002147 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2148 return oob + len;
2149
Brian Norris0612b9d2011-08-30 18:45:40 -07002150 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002151 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002152 uint32_t boffs = 0, woffs = ops->ooboffs;
2153 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002154
Florian Fainellif8ac0412010-09-07 13:23:43 +02002155 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002156 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002157 if (unlikely(woffs)) {
2158 if (woffs >= free->length) {
2159 woffs -= free->length;
2160 continue;
2161 }
2162 boffs = free->offset + woffs;
2163 bytes = min_t(size_t, len,
2164 (free->length - woffs));
2165 woffs = 0;
2166 } else {
2167 bytes = min_t(size_t, len, free->length);
2168 boffs = free->offset;
2169 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002170 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002171 oob += bytes;
2172 }
2173 return oob;
2174 }
2175 default:
2176 BUG();
2177 }
2178 return NULL;
2179}
2180
Florian Fainellif8ac0412010-09-07 13:23:43 +02002181#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002182
2183/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002184 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002185 * @mtd: MTD device structure
2186 * @to: offset to write to
2187 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002188 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002189 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002190 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002191static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2192 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002193{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002194 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002195 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002196 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002197
2198 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002199 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002200 mtd->oobavail : mtd->oobsize;
2201
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002202 uint8_t *oob = ops->oobbuf;
2203 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002204 int ret, subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002205
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002206 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002207 if (!writelen)
2208 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002209
Brian Norris8b6e50c2011-05-25 14:59:01 -07002210 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002211 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002212 pr_notice("%s: attempt to write non page aligned data\n",
2213 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002214 return -EINVAL;
2215 }
2216
Thomas Gleixner29072b92006-09-28 15:38:36 +02002217 column = to & (mtd->writesize - 1);
2218 subpage = column || (writelen & (mtd->writesize - 1));
2219
2220 if (subpage && oob)
2221 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002222
Thomas Gleixner6a930962006-06-28 00:11:45 +02002223 chipnr = (int)(to >> chip->chip_shift);
2224 chip->select_chip(mtd, chipnr);
2225
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002226 /* Check, if it is write protected */
2227 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002228 return -EIO;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002229
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002230 realpage = (int)(to >> chip->page_shift);
2231 page = realpage & chip->pagemask;
2232 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2233
2234 /* Invalidate the page cache, when we write to the cached page */
2235 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002236 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237 chip->pagebuf = -1;
2238
Maxim Levitsky782ce792010-02-22 20:39:36 +02002239 /* Don't allow multipage oob writes with offset */
Jon Poveycdcf12b2010-09-30 20:41:34 +09002240 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
Maxim Levitsky782ce792010-02-22 20:39:36 +02002241 return -EINVAL;
2242
Florian Fainellif8ac0412010-09-07 13:23:43 +02002243 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002244 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002245 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002246 uint8_t *wbuf = buf;
2247
Brian Norris8b6e50c2011-05-25 14:59:01 -07002248 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002249 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2250 cached = 0;
2251 bytes = min_t(int, bytes - column, (int) writelen);
2252 chip->pagebuf = -1;
2253 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2254 memcpy(&chip->buffers->databuf[column], buf, bytes);
2255 wbuf = chip->buffers->databuf;
2256 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002257
Maxim Levitsky782ce792010-02-22 20:39:36 +02002258 if (unlikely(oob)) {
2259 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002260 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002261 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002262 } else {
2263 /* We still need to erase leftover OOB data */
2264 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002265 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002266
Thomas Gleixner29072b92006-09-28 15:38:36 +02002267 ret = chip->write_page(mtd, chip, wbuf, page, cached,
Brian Norris0612b9d2011-08-30 18:45:40 -07002268 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002269 if (ret)
2270 break;
2271
2272 writelen -= bytes;
2273 if (!writelen)
2274 break;
2275
Thomas Gleixner29072b92006-09-28 15:38:36 +02002276 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002277 buf += bytes;
2278 realpage++;
2279
2280 page = realpage & chip->pagemask;
2281 /* Check, if we cross a chip boundary */
2282 if (!page) {
2283 chipnr++;
2284 chip->select_chip(mtd, -1);
2285 chip->select_chip(mtd, chipnr);
2286 }
2287 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002288
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002289 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002290 if (unlikely(oob))
2291 ops->oobretlen = ops->ooblen;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002292 return ret;
2293}
2294
2295/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002296 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002297 * @mtd: MTD device structure
2298 * @to: offset to write to
2299 * @len: number of bytes to write
2300 * @retlen: pointer to variable to store the number of written bytes
2301 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002302 *
2303 * NAND write with ECC. Used when performing writes in interrupt context, this
2304 * may for example be called by mtdoops when writing an oops while in panic.
2305 */
2306static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2307 size_t *retlen, const uint8_t *buf)
2308{
2309 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002310 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002311 int ret;
2312
2313 /* Do not allow reads past end of device */
2314 if ((to + len) > mtd->size)
2315 return -EINVAL;
2316 if (!len)
2317 return 0;
2318
Brian Norris8b6e50c2011-05-25 14:59:01 -07002319 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002320 panic_nand_wait(mtd, chip, 400);
2321
Brian Norris8b6e50c2011-05-25 14:59:01 -07002322 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002323 panic_nand_get_device(chip, mtd, FL_WRITING);
2324
Brian Norris4a89ff82011-08-30 18:45:45 -07002325 ops.len = len;
2326 ops.datbuf = (uint8_t *)buf;
2327 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07002328 ops.mode = 0;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002329
Brian Norris4a89ff82011-08-30 18:45:45 -07002330 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002331
Brian Norris4a89ff82011-08-30 18:45:45 -07002332 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002333 return ret;
2334}
2335
2336/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002337 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002338 * @mtd: MTD device structure
2339 * @to: offset to write to
2340 * @len: number of bytes to write
2341 * @retlen: pointer to variable to store the number of written bytes
2342 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002344 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002346static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002347 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002349 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002350 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002351 int ret;
2352
2353 /* Do not allow reads past end of device */
2354 if ((to + len) > mtd->size)
2355 return -EINVAL;
2356 if (!len)
2357 return 0;
2358
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002359 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002360
Brian Norris4a89ff82011-08-30 18:45:45 -07002361 ops.len = len;
2362 ops.datbuf = (uint8_t *)buf;
2363 ops.oobbuf = NULL;
Brian Norris23b1a992011-10-14 20:09:33 -07002364 ops.mode = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002365
Brian Norris4a89ff82011-08-30 18:45:45 -07002366 ret = nand_do_write_ops(mtd, to, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002367
Brian Norris4a89ff82011-08-30 18:45:45 -07002368 *retlen = ops.retlen;
Richard Purdie7fd5aec2006-08-27 01:23:33 -07002369
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002370 nand_release_device(mtd);
2371
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002372 return ret;
2373}
2374
2375/**
2376 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002377 * @mtd: MTD device structure
2378 * @to: offset to write to
2379 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002380 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002381 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002382 */
2383static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2384 struct mtd_oob_ops *ops)
2385{
Adrian Hunter03736152007-01-31 17:58:29 +02002386 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002387 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Brian Norris289c0522011-07-19 10:06:09 -07002389 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302390 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Brian Norris0612b9d2011-08-30 18:45:40 -07002392 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002393 len = chip->ecc.layout->oobavail;
2394 else
2395 len = mtd->oobsize;
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002398 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002399 pr_debug("%s: attempt to write past end of page\n",
2400 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return -EINVAL;
2402 }
2403
Adrian Hunter03736152007-01-31 17:58:29 +02002404 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002405 pr_debug("%s: attempt to start write outside oob\n",
2406 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002407 return -EINVAL;
2408 }
2409
Jason Liu775adc32011-02-25 13:06:18 +08002410 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002411 if (unlikely(to >= mtd->size ||
2412 ops->ooboffs + ops->ooblen >
2413 ((mtd->size >> chip->page_shift) -
2414 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002415 pr_debug("%s: attempt to write beyond end of device\n",
2416 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002417 return -EINVAL;
2418 }
2419
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002420 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002421 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002423 /* Shift to get page */
2424 page = (int)(to >> chip->page_shift);
2425
2426 /*
2427 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2428 * of my DiskOnChip 2000 test units) will clear the whole data page too
2429 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2430 * it in the doc2000 driver in August 1999. dwmw2.
2431 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002432 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
2434 /* Check, if it is write protected */
2435 if (nand_check_wp(mtd))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002436 return -EROFS;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002439 if (page == chip->pagebuf)
2440 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002442 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002443
Brian Norris0612b9d2011-08-30 18:45:40 -07002444 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002445 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2446 else
2447 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002448
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002449 if (status)
2450 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Vitaly Wool70145682006-11-03 18:20:38 +03002452 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002454 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002457/**
2458 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002459 * @mtd: MTD device structure
2460 * @to: offset to write to
2461 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462 */
2463static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2464 struct mtd_oob_ops *ops)
2465{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002466 struct nand_chip *chip = mtd->priv;
2467 int ret = -ENOTSUPP;
2468
2469 ops->retlen = 0;
2470
2471 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002472 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002473 pr_debug("%s: attempt to write beyond end of device\n",
2474 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002475 return -EINVAL;
2476 }
2477
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002478 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479
Florian Fainellif8ac0412010-09-07 13:23:43 +02002480 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002481 case MTD_OPS_PLACE_OOB:
2482 case MTD_OPS_AUTO_OOB:
2483 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002484 break;
2485
2486 default:
2487 goto out;
2488 }
2489
2490 if (!ops->datbuf)
2491 ret = nand_do_write_oob(mtd, to, ops);
2492 else
2493 ret = nand_do_write_ops(mtd, to, ops);
2494
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002495out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return ret;
2498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002501 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002502 * @mtd: MTD device structure
2503 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002505 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002507static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002509 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002511 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2512 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002516 * multi_erase_cmd - [GENERIC] AND specific block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002517 * @mtd: MTD device structure
2518 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002520 * AND multi block erase command function. Erase 4 consecutive blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002522static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002524 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002526 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2527 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2528 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2529 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2530 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
2533/**
2534 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002535 * @mtd: MTD device structure
2536 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002538 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002540static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
David Woodhousee0c7d762006-05-13 18:07:53 +01002542 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002544
David A. Marlin30f464b2005-01-17 18:35:25 +00002545#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002547 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002548 * @mtd: MTD device structure
2549 * @instr: erase instruction
2550 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002552 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002554int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2555 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Adrian Hunter69423d92008-12-10 13:37:21 +00002557 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002558 struct nand_chip *chip = mtd->priv;
Florian Fainellif8ac0412010-09-07 13:23:43 +02002559 loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002560 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002561 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Brian Norris289c0522011-07-19 10:06:09 -07002563 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2564 __func__, (unsigned long long)instr->addr,
2565 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302567 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Adrian Hunterbb0eb212008-08-12 12:40:50 +03002570 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002573 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002576 page = (int)(instr->addr >> chip->page_shift);
2577 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002583 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /* Check, if it is write protected */
2586 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002587 pr_debug("%s: device is write protected!\n",
2588 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 instr->state = MTD_ERASE_FAILED;
2590 goto erase_exit;
2591 }
2592
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002593 /*
2594 * If BBT requires refresh, set the BBT page mask to see if the BBT
2595 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2596 * can not be matched. This is also done when the bbt is actually
Brian Norris7854d3f2011-06-23 14:12:08 -07002597 * erased to avoid recursive updates.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002598 */
2599 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2600 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* Loop through the pages */
2603 len = instr->len;
2604
2605 instr->state = MTD_ERASING;
2606
2607 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002608 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002609 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2610 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002611 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2612 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 instr->state = MTD_ERASE_FAILED;
2614 goto erase_exit;
2615 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002616
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002617 /*
2618 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002619 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002620 */
2621 if (page <= chip->pagebuf && chip->pagebuf <
2622 (page + pages_per_block))
2623 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002626
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002627 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002629 /*
2630 * See if operation failed and additional status checks are
2631 * available
2632 */
2633 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2634 status = chip->errstat(mtd, chip, FL_ERASING,
2635 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002638 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002639 pr_debug("%s: failed erase, page 0x%08x\n",
2640 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002642 instr->fail_addr =
2643 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 goto erase_exit;
2645 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002646
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002647 /*
2648 * If BBT requires refresh, set the BBT rewrite flag to the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002649 * page being erased.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002650 */
2651 if (bbt_masked_page != 0xffffffff &&
2652 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002653 rewrite_bbt[chipnr] =
2654 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002657 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 page += pages_per_block;
2659
2660 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002661 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002663 chip->select_chip(mtd, -1);
2664 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002665
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002666 /*
2667 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Brian Norris8b6e50c2011-05-25 14:59:01 -07002668 * page mask to see if this BBT should be rewritten.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002669 */
2670 if (bbt_masked_page != 0xffffffff &&
2671 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2672 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2673 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 }
2676 instr->state = MTD_ERASE_DONE;
2677
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002678erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 /* Deselect and wake up anyone waiting on the device */
2683 nand_release_device(mtd);
2684
David Woodhouse49defc02007-10-06 15:01:59 -04002685 /* Do call back function */
2686 if (!ret)
2687 mtd_erase_callback(instr);
2688
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002689 /*
2690 * If BBT requires refresh and erase was successful, rewrite any
Brian Norris8b6e50c2011-05-25 14:59:01 -07002691 * selected bad block tables.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002692 */
2693 if (bbt_masked_page == 0xffffffff || ret)
2694 return ret;
2695
2696 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2697 if (!rewrite_bbt[chipnr])
2698 continue;
Brian Norris8b6e50c2011-05-25 14:59:01 -07002699 /* Update the BBT for chip */
Brian Norris289c0522011-07-19 10:06:09 -07002700 pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2701 __func__, chipnr, rewrite_bbt[chipnr],
2702 chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002703 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002704 }
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* Return more or less happy */
2707 return ret;
2708}
2709
2710/**
2711 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002712 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002714 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002716static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002718 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Brian Norris289c0522011-07-19 10:06:09 -07002720 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002725 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002729 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002730 * @mtd: MTD device structure
2731 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002733static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 /* Check for invalid offset */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002736 if (offs > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return -EINVAL;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002738
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002739 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
2742/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002743 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002744 * @mtd: MTD device structure
2745 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002747static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002749 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 int ret;
2751
Florian Fainellif8ac0412010-09-07 13:23:43 +02002752 ret = nand_block_isbad(mtd, ofs);
2753 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002754 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if (ret > 0)
2756 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002757 return ret;
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002760 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
2763/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002764 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002765 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002766 */
2767static int nand_suspend(struct mtd_info *mtd)
2768{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002769 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002770
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002771 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002772}
2773
2774/**
2775 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002776 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002777 */
2778static void nand_resume(struct mtd_info *mtd)
2779{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002780 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002781
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002782 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002783 nand_release_device(mtd);
2784 else
Brian Norrisd0370212011-07-19 10:06:08 -07002785 pr_err("%s called for a chip which is not in suspended state\n",
2786 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002787}
2788
Brian Norris8b6e50c2011-05-25 14:59:01 -07002789/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002790static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002793 if (!chip->chip_delay)
2794 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002797 if (chip->cmdfunc == NULL)
2798 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002801 if (chip->waitfunc == NULL)
2802 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002804 if (!chip->select_chip)
2805 chip->select_chip = nand_select_chip;
2806 if (!chip->read_byte)
2807 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2808 if (!chip->read_word)
2809 chip->read_word = nand_read_word;
2810 if (!chip->block_bad)
2811 chip->block_bad = nand_block_bad;
2812 if (!chip->block_markbad)
2813 chip->block_markbad = nand_default_block_markbad;
2814 if (!chip->write_buf)
2815 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2816 if (!chip->read_buf)
2817 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2818 if (!chip->verify_buf)
2819 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2820 if (!chip->scan_bbt)
2821 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002822
2823 if (!chip->controller) {
2824 chip->controller = &chip->hwcontrol;
2825 spin_lock_init(&chip->controller->lock);
2826 init_waitqueue_head(&chip->controller->wq);
2827 }
2828
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002829}
2830
Brian Norris8b6e50c2011-05-25 14:59:01 -07002831/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002832static void sanitize_string(uint8_t *s, size_t len)
2833{
2834 ssize_t i;
2835
Brian Norris8b6e50c2011-05-25 14:59:01 -07002836 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002837 s[len - 1] = 0;
2838
Brian Norris8b6e50c2011-05-25 14:59:01 -07002839 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002840 for (i = 0; i < len - 1; i++) {
2841 if (s[i] < ' ' || s[i] > 127)
2842 s[i] = '?';
2843 }
2844
Brian Norris8b6e50c2011-05-25 14:59:01 -07002845 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002846 strim(s);
2847}
2848
2849static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2850{
2851 int i;
2852 while (len--) {
2853 crc ^= *p++ << 8;
2854 for (i = 0; i < 8; i++)
2855 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2856 }
2857
2858 return crc;
2859}
2860
2861/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002862 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002863 */
2864static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002865 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002866{
2867 struct nand_onfi_params *p = &chip->onfi_params;
2868 int i;
2869 int val;
2870
Brian Norris7854d3f2011-06-23 14:12:08 -07002871 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002872 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2873 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2874 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2875 return 0;
2876
Brian Norris9a4d4d62011-07-19 10:06:07 -07002877 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002878 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2879 for (i = 0; i < 3; i++) {
2880 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2881 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2882 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002883 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002884 break;
2885 }
2886 }
2887
2888 if (i == 3)
2889 return 0;
2890
Brian Norris8b6e50c2011-05-25 14:59:01 -07002891 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002892 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002893 if (val & (1 << 5))
2894 chip->onfi_version = 23;
2895 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002896 chip->onfi_version = 22;
2897 else if (val & (1 << 3))
2898 chip->onfi_version = 21;
2899 else if (val & (1 << 2))
2900 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002901 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002902 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002903 else
2904 chip->onfi_version = 0;
2905
2906 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002907 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002908 return 0;
2909 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002910
2911 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2912 sanitize_string(p->model, sizeof(p->model));
2913 if (!mtd->name)
2914 mtd->name = p->model;
2915 mtd->writesize = le32_to_cpu(p->byte_per_page);
2916 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2917 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
David Woodhouse4ccb3b42010-12-03 16:36:34 +00002918 chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002919 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002920 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002921 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002922
2923 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2924 chip->options |= (NAND_NO_READRDY |
2925 NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;
2926
2927 return 1;
2928}
2929
2930/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002931 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002932 */
2933static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002934 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002935 int busw,
2936 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00002937 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002938{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002939 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07002940 u8 id_data[8];
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002941 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002944 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Karl Beldanef89a882008-09-15 14:37:29 +02002946 /*
2947 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002948 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02002949 */
2950 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002953 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002956 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002957 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Brian Norris8b6e50c2011-05-25 14:59:01 -07002959 /*
2960 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01002961 * interface concerns can cause random data which looks like a
2962 * possibly credible NAND flash to appear. If the two results do
2963 * not match, ignore the device completely.
2964 */
2965
2966 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2967
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002968 for (i = 0; i < 2; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07002969 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01002970
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002971 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002972 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07002973 "%02x,%02x against %02x,%02x\n", __func__,
2974 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01002975 return ERR_PTR(-ENODEV);
2976 }
2977
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002978 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00002979 type = nand_flash_ids;
2980
2981 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002982 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02002983 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00002984
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002985 chip->onfi_version = 0;
2986 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002987 /* Check is chip is ONFI compliant */
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002988 ret = nand_flash_detect_onfi(mtd, chip, &busw);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002989 if (ret)
2990 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002991 }
2992
2993 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2994
2995 /* Read entire ID string */
2996
2997 for (i = 0; i < 8; i++)
2998 id_data[i] = chip->read_byte(mtd);
2999
David Woodhouse5e81e882010-02-26 18:32:56 +00003000 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003001 return ERR_PTR(-ENODEV);
3002
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003003 if (!mtd->name)
3004 mtd->name = type->name;
3005
Adrian Hunter69423d92008-12-10 13:37:21 +00003006 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003007
Huang Shijie12a40a52010-09-27 10:43:53 +08003008 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003009 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003010 busw = chip->init_size(mtd, chip, id_data);
3011 } else if (!type->pagesize) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003012 int extid;
Thomas Gleixner29072b92006-09-28 15:38:36 +02003013 /* The 3rd id byte holds MLC / multichip data */
Kevin Cernekee426c4572010-05-04 20:58:03 -07003014 chip->cellinfo = id_data[2];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003015 /* The 4th id byte is the important one */
Kevin Cernekee426c4572010-05-04 20:58:03 -07003016 extid = id_data[3];
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003017
Kevin Cernekee426c4572010-05-04 20:58:03 -07003018 /*
3019 * Field definitions are in the following datasheets:
3020 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norris34c5bf62010-08-20 10:50:43 -07003021 * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003022 *
3023 * Check for wraparound + Samsung ID + nonzero 6th byte
3024 * to decide what to do.
3025 */
3026 if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
3027 id_data[0] == NAND_MFR_SAMSUNG &&
Tilman Sauerbeckcfe3fda2010-08-20 14:01:47 -07003028 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Kevin Cernekee426c4572010-05-04 20:58:03 -07003029 id_data[5] != 0x00) {
3030 /* Calc pagesize */
3031 mtd->writesize = 2048 << (extid & 0x03);
3032 extid >>= 2;
3033 /* Calc oobsize */
Brian Norris34c5bf62010-08-20 10:50:43 -07003034 switch (extid & 0x03) {
3035 case 1:
3036 mtd->oobsize = 128;
3037 break;
3038 case 2:
3039 mtd->oobsize = 218;
3040 break;
3041 case 3:
3042 mtd->oobsize = 400;
3043 break;
3044 default:
3045 mtd->oobsize = 436;
3046 break;
3047 }
Kevin Cernekee426c4572010-05-04 20:58:03 -07003048 extid >>= 2;
3049 /* Calc blocksize */
3050 mtd->erasesize = (128 * 1024) <<
3051 (((extid >> 1) & 0x04) | (extid & 0x03));
3052 busw = 0;
3053 } else {
3054 /* Calc pagesize */
3055 mtd->writesize = 1024 << (extid & 0x03);
3056 extid >>= 2;
3057 /* Calc oobsize */
3058 mtd->oobsize = (8 << (extid & 0x01)) *
3059 (mtd->writesize >> 9);
3060 extid >>= 2;
3061 /* Calc blocksize. Blocksize is multiples of 64KiB */
3062 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3063 extid >>= 2;
3064 /* Get buswidth information */
3065 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3066 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003067 } else {
3068 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003069 * Old devices have chip data hardcoded in the device id table.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003070 */
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003071 mtd->erasesize = type->erasesize;
3072 mtd->writesize = type->pagesize;
Thomas Gleixner4cbb9b82006-05-23 12:37:31 +02003073 mtd->oobsize = mtd->writesize / 32;
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003074 busw = type->options & NAND_BUSWIDTH_16;
Brian Norris2173bae2010-08-19 08:11:02 -07003075
3076 /*
3077 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3078 * some Spansion chips have erasesize that conflicts with size
Brian Norris8b6e50c2011-05-25 14:59:01 -07003079 * listed in nand_ids table.
Brian Norris2173bae2010-08-19 08:11:02 -07003080 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3081 */
3082 if (*maf_id == NAND_MFR_AMD && id_data[4] != 0x00 &&
3083 id_data[5] == 0x00 && id_data[6] == 0x00 &&
3084 id_data[7] == 0x00 && mtd->writesize == 512) {
3085 mtd->erasesize = 128 * 1024;
3086 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3087 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003088 }
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003089 /* Get chip options, preserve non chip based options */
3090 chip->options &= ~NAND_CHIPOPTIONS_MSK;
3091 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
3092
Brian Norris8b6e50c2011-05-25 14:59:01 -07003093 /*
3094 * Check if chip is not a Samsung device. Do not clear the
3095 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003096 */
3097 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3098 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3099ident_done:
3100
3101 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003102 * Set chip as a default. Board drivers can override it, if necessary.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003103 */
3104 chip->options |= NAND_NO_AUTOINCR;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003105
3106 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003107 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003108 if (nand_manuf_ids[maf_idx].id == *maf_id)
3109 break;
3110 }
3111
3112 /*
3113 * Check, if buswidth is correct. Hardware drivers should set
Brian Norris8b6e50c2011-05-25 14:59:01 -07003114 * chip correct!
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003115 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003116 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003117 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003118 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3119 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003120 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003121 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3122 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003123 return ERR_PTR(-EINVAL);
3124 }
3125
3126 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003127 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003128 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003129 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003130
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003131 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003132 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003133 if (chip->chipsize & 0xffffffff)
3134 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003135 else {
3136 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3137 chip->chip_shift += 32 - 1;
3138 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003139
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003140 chip->badblockbits = 8;
3141
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003142 /* Set the bad block position */
Brian Norris065a1ed2010-08-18 11:25:04 -07003143 if (mtd->writesize > 512 || (busw & NAND_BUSWIDTH_16))
Brian Norrisc7b28e22010-07-13 15:13:00 -07003144 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
Brian Norris065a1ed2010-08-18 11:25:04 -07003145 else
3146 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003147
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07003148 /*
3149 * Bad block marker is stored in the last page of each block
Brian Norrisc7b28e22010-07-13 15:13:00 -07003150 * on Samsung and Hynix MLC devices; stored in first two pages
3151 * of each block on Micron devices with 2KiB pages and on
Brian Norris8c342332011-11-02 13:34:44 -07003152 * SLC Samsung, Hynix, Toshiba, AMD/Spansion, and Macronix.
3153 * All others scan only the first page.
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -07003154 */
3155 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3156 (*maf_id == NAND_MFR_SAMSUNG ||
3157 *maf_id == NAND_MFR_HYNIX))
Brian Norris5fb15492011-05-31 16:31:21 -07003158 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07003159 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3160 (*maf_id == NAND_MFR_SAMSUNG ||
3161 *maf_id == NAND_MFR_HYNIX ||
Brian Norris13ed7ae2010-08-20 12:36:12 -07003162 *maf_id == NAND_MFR_TOSHIBA ||
Brian Norris8c342332011-11-02 13:34:44 -07003163 *maf_id == NAND_MFR_AMD ||
3164 *maf_id == NAND_MFR_MACRONIX)) ||
Brian Norrisc7b28e22010-07-13 15:13:00 -07003165 (mtd->writesize == 2048 &&
3166 *maf_id == NAND_MFR_MICRON))
Brian Norris5fb15492011-05-31 16:31:21 -07003167 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
Brian Norrisc7b28e22010-07-13 15:13:00 -07003168
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003169 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003170 if (chip->options & NAND_4PAGE_ARRAY)
3171 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003172 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003173 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003174
Brian Norris8b6e50c2011-05-25 14:59:01 -07003175 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003176 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3177 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003178
Brian Norris9a4d4d62011-07-19 10:06:07 -07003179 pr_info("NAND device: Manufacturer ID:"
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003180 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
3181 nand_manuf_ids[maf_idx].name,
Brian Norris0b524fb2010-12-12 00:23:32 -08003182 chip->onfi_version ? chip->onfi_params.model : type->name);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003183
3184 return type;
3185}
3186
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003187/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003188 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003189 * @mtd: MTD device structure
3190 * @maxchips: number of chips to scan for
3191 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003192 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003193 * This is the first phase of the normal nand_scan() function. It reads the
3194 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003195 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003196 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003197 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003198int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3199 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003200{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003201 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003202 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003203 struct nand_flash_dev *type;
3204
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003205 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003206 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003207 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003208 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003209
3210 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003211 type = nand_get_flash_type(mtd, chip, busw,
3212 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003213
3214 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003215 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003216 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003217 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003218 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
3220
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003221 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003222 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003223 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003224 /* See comment in nand_get_flash_type for reset */
3225 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003227 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003229 if (nand_maf_id != chip->read_byte(mtd) ||
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003230 nand_dev_id != chip->read_byte(mtd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 break;
3232 }
3233 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003234 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003237 chip->numchips = i;
3238 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
David Woodhouse3b85c322006-09-25 17:06:53 +01003240 return 0;
3241}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003242EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003243
3244
3245/**
3246 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003247 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003248 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003249 * This is the second phase of the normal nand_scan() function. It fills out
3250 * all the uninitialized function pointers with the defaults and scans for a
3251 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003252 */
3253int nand_scan_tail(struct mtd_info *mtd)
3254{
3255 int i;
3256 struct nand_chip *chip = mtd->priv;
3257
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003258 if (!(chip->options & NAND_OWN_BUFFERS))
3259 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3260 if (!chip->buffers)
3261 return -ENOMEM;
3262
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003263 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003264 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003265
3266 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003267 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003268 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003269 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003270 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003272 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 break;
3274 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003275 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 break;
3277 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003278 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003280 case 128:
3281 chip->ecc.layout = &nand_oob_128;
3282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003284 pr_warn("No oob scheme defined for oobsize %d\n",
3285 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 BUG();
3287 }
3288 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003289
David Woodhouse956e9442006-09-25 17:12:39 +01003290 if (!chip->write_page)
3291 chip->write_page = nand_write_page;
3292
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003293 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003294 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003295 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003296 */
David Woodhouse956e9442006-09-25 17:12:39 +01003297
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003298 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003299 case NAND_ECC_HW_OOB_FIRST:
3300 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3301 if (!chip->ecc.calculate || !chip->ecc.correct ||
3302 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003303 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003304 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003305 BUG();
3306 }
3307 if (!chip->ecc.read_page)
3308 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3309
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003310 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003311 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003312 if (!chip->ecc.read_page)
3313 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003314 if (!chip->ecc.write_page)
3315 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003316 if (!chip->ecc.read_page_raw)
3317 chip->ecc.read_page_raw = nand_read_page_raw;
3318 if (!chip->ecc.write_page_raw)
3319 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003320 if (!chip->ecc.read_oob)
3321 chip->ecc.read_oob = nand_read_oob_std;
3322 if (!chip->ecc.write_oob)
3323 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003324
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003325 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003326 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3327 !chip->ecc.hwctl) &&
3328 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003329 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003330 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003331 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003332 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003333 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003334 BUG();
3335 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003336 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003337 if (!chip->ecc.read_page)
3338 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003339 if (!chip->ecc.write_page)
3340 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003341 if (!chip->ecc.read_page_raw)
3342 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3343 if (!chip->ecc.write_page_raw)
3344 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003345 if (!chip->ecc.read_oob)
3346 chip->ecc.read_oob = nand_read_oob_syndrome;
3347 if (!chip->ecc.write_oob)
3348 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003349
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003350 if (mtd->writesize >= chip->ecc.size)
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003351 break;
Brian Norris9a4d4d62011-07-19 10:06:07 -07003352 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003353 "%d byte page size, fallback to SW ECC\n",
3354 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003355 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003357 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003358 chip->ecc.calculate = nand_calculate_ecc;
3359 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003360 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003361 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003362 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003363 chip->ecc.read_page_raw = nand_read_page_raw;
3364 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003365 chip->ecc.read_oob = nand_read_oob_std;
3366 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003367 if (!chip->ecc.size)
3368 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003369 chip->ecc.bytes = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003371
Ivan Djelic193bd402011-03-11 11:05:33 +01003372 case NAND_ECC_SOFT_BCH:
3373 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003374 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003375 BUG();
3376 }
3377 chip->ecc.calculate = nand_bch_calculate_ecc;
3378 chip->ecc.correct = nand_bch_correct_data;
3379 chip->ecc.read_page = nand_read_page_swecc;
3380 chip->ecc.read_subpage = nand_read_subpage;
3381 chip->ecc.write_page = nand_write_page_swecc;
3382 chip->ecc.read_page_raw = nand_read_page_raw;
3383 chip->ecc.write_page_raw = nand_write_page_raw;
3384 chip->ecc.read_oob = nand_read_oob_std;
3385 chip->ecc.write_oob = nand_write_oob_std;
3386 /*
3387 * Board driver should supply ecc.size and ecc.bytes values to
3388 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003389 * for details. Otherwise, default to 4 bits for large page
3390 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003391 */
3392 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3393 chip->ecc.size = 512;
3394 chip->ecc.bytes = 7;
3395 }
3396 chip->ecc.priv = nand_bch_init(mtd,
3397 chip->ecc.size,
3398 chip->ecc.bytes,
3399 &chip->ecc.layout);
3400 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003401 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003402 BUG();
3403 }
3404 break;
3405
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003406 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003407 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003408 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003409 chip->ecc.read_page = nand_read_page_raw;
3410 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003411 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003412 chip->ecc.read_page_raw = nand_read_page_raw;
3413 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003414 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003415 chip->ecc.size = mtd->writesize;
3416 chip->ecc.bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003420 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003421 BUG();
3422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Brian Norris9ce244b2011-08-30 18:45:37 -07003424 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003425 if (!chip->ecc.read_oob_raw)
3426 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003427 if (!chip->ecc.write_oob_raw)
3428 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3429
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003430 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003431 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003432 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003433 */
3434 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003435 for (i = 0; chip->ecc.layout->oobfree[i].length
3436 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003437 chip->ecc.layout->oobavail +=
3438 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003439 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003440
3441 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003442 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003443 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003444 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003445 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003446 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003447 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003448 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003450 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003451
Brian Norris8b6e50c2011-05-25 14:59:01 -07003452 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003453 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3454 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003455 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003456 case 2:
3457 mtd->subpage_sft = 1;
3458 break;
3459 case 4:
3460 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003461 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003462 mtd->subpage_sft = 2;
3463 break;
3464 }
3465 }
3466 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3467
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003468 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003469 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 /* De-select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003472 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
3474 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003475 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
3477 /* Fill in remaining MTD driver data */
3478 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003479 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3480 MTD_CAP_NANDFLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 mtd->erase = nand_erase;
3482 mtd->point = NULL;
3483 mtd->unpoint = NULL;
3484 mtd->read = nand_read;
3485 mtd->write = nand_write;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02003486 mtd->panic_write = panic_nand_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 mtd->read_oob = nand_read_oob;
3488 mtd->write_oob = nand_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 mtd->sync = nand_sync;
3490 mtd->lock = NULL;
3491 mtd->unlock = NULL;
Vitaly Wool962034f2005-09-15 14:58:53 +01003492 mtd->suspend = nand_suspend;
3493 mtd->resume = nand_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 mtd->block_isbad = nand_block_isbad;
3495 mtd->block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003496 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003498 /* propagate ecc.layout to mtd_info */
3499 mtd->ecclayout = chip->ecc.layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003501 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003502 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003506 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003508EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Brian Norris8b6e50c2011-05-25 14:59:01 -07003510/*
3511 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003512 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003513 * to call us from in-kernel code if the core NAND support is modular.
3514 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003515#ifdef MODULE
3516#define caller_is_module() (1)
3517#else
3518#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003519 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003520#endif
3521
3522/**
3523 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003524 * @mtd: MTD device structure
3525 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003526 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003527 * This fills out all the uninitialized function pointers with the defaults.
3528 * The flash ID is read and the mtd/chip structures are filled with the
3529 * appropriate values. The mtd->owner field must be set to the module of the
3530 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003531 */
3532int nand_scan(struct mtd_info *mtd, int maxchips)
3533{
3534 int ret;
3535
3536 /* Many callers got this wrong, so check for it for a while... */
3537 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003538 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003539 BUG();
3540 }
3541
David Woodhouse5e81e882010-02-26 18:32:56 +00003542 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003543 if (!ret)
3544 ret = nand_scan_tail(mtd);
3545 return ret;
3546}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003547EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003550 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003551 * @mtd: MTD device structure
3552 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003553void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003555 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Ivan Djelic193bd402011-03-11 11:05:33 +01003557 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3558 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3559
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003560 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Jesper Juhlfa671642005-11-07 01:01:27 -08003562 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003563 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003564 if (!(chip->options & NAND_OWN_BUFFERS))
3565 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003566
3567 /* Free bad block descriptor memory */
3568 if (chip->badblock_pattern && chip->badblock_pattern->options
3569 & NAND_BBT_DYNAMICSTRUCT)
3570 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
David Woodhousee0c7d762006-05-13 18:07:53 +01003572EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003573
3574static int __init nand_base_init(void)
3575{
3576 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3577 return 0;
3578}
3579
3580static void __exit nand_base_exit(void)
3581{
3582 led_trigger_unregister_simple(nand_led_trigger);
3583}
3584
3585module_init(nand_base_init);
3586module_exit(nand_base_exit);
3587
David Woodhousee0c7d762006-05-13 18:07:53 +01003588MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003589MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3590MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003591MODULE_DESCRIPTION("Generic NAND flash driver code");