blob: 861a87d4d16b9e366694dd93b63de56c98ba30a6 [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
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530126 return ret;
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/**
130 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700131 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000132 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800133 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100135static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200137 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200139 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200140 spin_lock(&chip->controller->lock);
141 chip->controller->active = NULL;
142 chip->state = FL_READY;
143 wake_up(&chip->controller->wq);
144 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147/**
148 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700149 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700151 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200153static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200155 struct nand_chip *chip = mtd->priv;
156 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/**
Masanari Iida064a7692012-11-09 23:20:58 +0900160 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700161 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700162 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700164 * Default read function for 16bit buswidth with endianness conversion.
165 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200167static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200169 struct nand_chip *chip = mtd->priv;
170 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700175 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700177 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
179static u16 nand_read_word(struct mtd_info *mtd)
180{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200181 struct nand_chip *chip = mtd->priv;
182 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700187 * @mtd: MTD device structure
188 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
190 * Default select function for 1 chip devices.
191 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200192static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200194 struct nand_chip *chip = mtd->priv;
195
196 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200198 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202
203 default:
204 BUG();
205 }
206}
207
208/**
209 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700210 * @mtd: MTD device structure
211 * @buf: data buffer
212 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700214 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200216static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200219 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Woodhousee0c7d762006-05-13 18:07:53 +0100221 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200222 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000226 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700227 * @mtd: MTD device structure
228 * @buf: buffer to store date
229 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700231 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200233static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200236 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
David Woodhousee0c7d762006-05-13 18:07:53 +0100238 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200239 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700244 * @mtd: MTD device structure
245 * @buf: data buffer
246 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700248 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200250static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200253 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 u16 *p = (u16 *) buf;
255 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000256
David Woodhousee0c7d762006-05-13 18:07:53 +0100257 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200258 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000263 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700264 * @mtd: MTD device structure
265 * @buf: buffer to store date
266 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700268 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200270static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200273 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 u16 *p = (u16 *) buf;
275 len >>= 1;
276
David Woodhousee0c7d762006-05-13 18:07:53 +0100277 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200278 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700283 * @mtd: MTD device structure
284 * @ofs: offset from device start
285 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000287 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
290{
Brian Norriscdbec052012-01-13 18:11:48 -0800291 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200292 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 u16 bad;
294
Brian Norris5fb15492011-05-31 16:31:21 -0700295 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700296 ofs += mtd->erasesize - mtd->writesize;
297
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100298 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200301 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200303 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200306 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Brian Norriscdbec052012-01-13 18:11:48 -0800309 do {
310 if (chip->options & NAND_BUSWIDTH_16) {
311 chip->cmdfunc(mtd, NAND_CMD_READOOB,
312 chip->badblockpos & 0xFE, page);
313 bad = cpu_to_le16(chip->read_word(mtd));
314 if (chip->badblockpos & 0x1)
315 bad >>= 8;
316 else
317 bad &= 0xFF;
318 } else {
319 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
320 page);
321 bad = chip->read_byte(mtd);
322 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000323
Brian Norriscdbec052012-01-13 18:11:48 -0800324 if (likely(chip->badblockbits == 8))
325 res = bad != 0xFF;
326 else
327 res = hweight8(bad) < chip->badblockbits;
328 ofs += mtd->writesize;
329 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
330 i++;
331 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200332
Huang Shijieb0bb6902012-11-19 14:43:29 +0800333 if (getchip) {
334 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800336 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return res;
339}
340
341/**
342 * nand_default_block_markbad - [DEFAULT] mark a block bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700343 * @mtd: MTD device structure
344 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700346 * This is the default implementation, which can be overridden by a hardware
Brian Norrise2414f42012-02-06 13:44:00 -0800347 * specific driver. We try operations in the following order, according to our
348 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
349 * (1) erase the affected block, to allow OOB marker to be written cleanly
350 * (2) update in-memory BBT
351 * (3) write bad block marker to OOB area of affected block
352 * (4) update flash-based BBT
353 * Note that we retain the first error encountered in (3) or (4), finish the
354 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355*/
356static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
357{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200358 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200359 uint8_t buf[2] = { 0, 0 };
Brian Norrise2414f42012-02-06 13:44:00 -0800360 int block, res, ret = 0, i = 0;
361 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000362
Brian Norrise2414f42012-02-06 13:44:00 -0800363 if (write_oob) {
Brian Norris00918422012-01-13 18:11:47 -0800364 struct erase_info einfo;
365
366 /* Attempt erase before marking OOB */
367 memset(&einfo, 0, sizeof(einfo));
368 einfo.mtd = mtd;
369 einfo.addr = ofs;
370 einfo.len = 1 << chip->phys_erase_shift;
371 nand_erase_nand(mtd, &einfo, 0);
372 }
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400375 block = (int)(ofs >> chip->bbt_erase_shift);
Brian Norrise2414f42012-02-06 13:44:00 -0800376 /* Mark block bad in memory-based BBT */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200377 if (chip->bbt)
378 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Brian Norrise2414f42012-02-06 13:44:00 -0800380 /* Write bad block marker to OOB */
381 if (write_oob) {
Brian Norris4a89ff82011-08-30 18:45:45 -0700382 struct mtd_oob_ops ops;
Brian Norrisdf698622012-01-20 20:38:03 -0800383 loff_t wr_ofs = ofs;
Brian Norris4a89ff82011-08-30 18:45:45 -0700384
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300385 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000386
Brian Norris4a89ff82011-08-30 18:45:45 -0700387 ops.datbuf = NULL;
388 ops.oobbuf = buf;
Brian Norris85443312012-01-13 18:11:49 -0800389 ops.ooboffs = chip->badblockpos;
390 if (chip->options & NAND_BUSWIDTH_16) {
391 ops.ooboffs &= ~0x01;
392 ops.len = ops.ooblen = 2;
393 } else {
394 ops.len = ops.ooblen = 1;
395 }
Brian Norris23b1a992011-10-14 20:09:33 -0700396 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norrisdf698622012-01-20 20:38:03 -0800397
Brian Norrise2414f42012-02-06 13:44:00 -0800398 /* Write to first/last page(s) if necessary */
Brian Norrisdf698622012-01-20 20:38:03 -0800399 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
400 wr_ofs += mtd->erasesize - mtd->writesize;
Brian Norris02ed70b2010-07-21 16:53:47 -0700401 do {
Brian Norrise2414f42012-02-06 13:44:00 -0800402 res = nand_do_write_oob(mtd, wr_ofs, &ops);
403 if (!ret)
404 ret = res;
Brian Norris02ed70b2010-07-21 16:53:47 -0700405
Brian Norris02ed70b2010-07-21 16:53:47 -0700406 i++;
Brian Norrisdf698622012-01-20 20:38:03 -0800407 wr_ofs += mtd->writesize;
Brian Norrise2414f42012-02-06 13:44:00 -0800408 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Brian Norris02ed70b2010-07-21 16:53:47 -0700409
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300410 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200411 }
Brian Norrise2414f42012-02-06 13:44:00 -0800412
413 /* Update flash-based bad block table */
414 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
415 res = nand_update_bbt(mtd, ofs);
416 if (!ret)
417 ret = res;
418 }
419
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200420 if (!ret)
421 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300422
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000426/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700428 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700430 * Check, if the device is write protected. The function expects, that the
431 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100433static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200435 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200436
Brian Norris8b6e50c2011-05-25 14:59:01 -0700437 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200438 if (chip->options & NAND_BROKEN_XD)
439 return 0;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200442 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
443 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/**
447 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700448 * @mtd: MTD device structure
449 * @ofs: offset from device start
450 * @getchip: 0, if the chip is already selected
451 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
453 * Check, if the block is bad. Either by reading the bad block table or
454 * calling of the scan function.
455 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200456static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
457 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200459 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000460
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200461 if (!chip->bbt)
462 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100465 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200468/**
469 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700470 * @mtd: MTD device structure
471 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200472 *
473 * Helper function for nand_wait_ready used when needing to wait in interrupt
474 * context.
475 */
476static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
477{
478 struct nand_chip *chip = mtd->priv;
479 int i;
480
481 /* Wait for the device to get ready */
482 for (i = 0; i < timeo; i++) {
483 if (chip->dev_ready(mtd))
484 break;
485 touch_softlockup_watchdog();
486 mdelay(1);
487 }
488}
489
Brian Norris7854d3f2011-06-23 14:12:08 -0700490/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100491void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000492{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200493 struct nand_chip *chip = mtd->priv;
David Woodhousee0c7d762006-05-13 18:07:53 +0100494 unsigned long timeo = jiffies + 2;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000495
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200496 /* 400ms timeout */
497 if (in_interrupt() || oops_in_progress)
498 return panic_nand_wait_ready(mtd, 400);
499
Richard Purdie8fe833c2006-03-31 02:31:14 -0800500 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700501 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000502 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200503 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800504 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700505 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000506 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800507 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000508}
David Woodhouse4b648b02006-09-25 17:05:24 +0100509EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/**
512 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700513 * @mtd: MTD device structure
514 * @command: the command to be sent
515 * @column: the column address for this command, -1 if none
516 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700518 * Send command to NAND device. This function is used for small page devices
519 * (256/512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200521static void nand_command(struct mtd_info *mtd, unsigned int command,
522 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200524 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200525 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Brian Norris8b6e50c2011-05-25 14:59:01 -0700527 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (command == NAND_CMD_SEQIN) {
529 int readcmd;
530
Joern Engel28318772006-05-22 23:18:05 +0200531 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200533 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 readcmd = NAND_CMD_READOOB;
535 } else if (column < 256) {
536 /* First 256 bytes --> READ0 */
537 readcmd = NAND_CMD_READ0;
538 } else {
539 column -= 256;
540 readcmd = NAND_CMD_READ1;
541 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200542 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200543 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200545 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Brian Norris8b6e50c2011-05-25 14:59:01 -0700547 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200548 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
549 /* Serially input address */
550 if (column != -1) {
551 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200552 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200553 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200554 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200555 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200557 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200558 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200559 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200560 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200561 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200562 if (chip->chipsize > (32 << 20))
563 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200564 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200565 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000566
567 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700568 * Program and erase have their own busy handlers status and sequential
569 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 case NAND_CMD_PAGEPROG:
574 case NAND_CMD_ERASE1:
575 case NAND_CMD_ERASE2:
576 case NAND_CMD_SEQIN:
577 case NAND_CMD_STATUS:
578 return;
579
580 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200583 udelay(chip->chip_delay);
584 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200585 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200586 chip->cmd_ctrl(mtd,
587 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200588 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
589 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return;
591
David Woodhousee0c7d762006-05-13 18:07:53 +0100592 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000594 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * If we don't have access to the busy pin, we apply the given
596 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100597 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200598 if (!chip->dev_ready) {
599 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700603 /*
604 * Apply this short delay always to ensure that we do wait tWB in
605 * any case on any machine.
606 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100607 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000608
609 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/**
613 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700614 * @mtd: MTD device structure
615 * @command: the command to be sent
616 * @column: the column address for this command, -1 if none
617 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700620 * devices. We don't have the separate regions as we have in the small page
621 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
624 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200626 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Emulate NAND_CMD_READOOB */
629 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200630 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 command = NAND_CMD_READ0;
632 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000633
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200634 /* Command latch cycle */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200635 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200636 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200639 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* Serially input address */
642 if (column != -1) {
643 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200646 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200648 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200651 chip->cmd_ctrl(mtd, page_addr, ctrl);
652 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 if (chip->chipsize > (128 << 20))
656 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200657 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000661
662 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700663 * Program and erase have their own busy handlers status, sequential
664 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000665 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 case NAND_CMD_CACHEDPROG:
669 case NAND_CMD_PAGEPROG:
670 case NAND_CMD_ERASE1:
671 case NAND_CMD_ERASE2:
672 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200673 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000675 case NAND_CMD_DEPLETE1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
677
David A. Marlin30f464b2005-01-17 18:35:25 +0000678 case NAND_CMD_STATUS_ERROR:
679 case NAND_CMD_STATUS_ERROR0:
680 case NAND_CMD_STATUS_ERROR1:
681 case NAND_CMD_STATUS_ERROR2:
682 case NAND_CMD_STATUS_ERROR3:
Brian Norris8b6e50c2011-05-25 14:59:01 -0700683 /* Read error status commands require only a short delay */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200684 udelay(chip->chip_delay);
David A. Marlin30f464b2005-01-17 18:35:25 +0000685 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200688 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200690 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200691 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
692 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
693 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
694 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200695 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
696 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return;
698
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200699 case NAND_CMD_RNDOUT:
700 /* No ready / busy check necessary */
701 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
702 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
703 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
704 NAND_NCE | NAND_CTRL_CHANGE);
705 return;
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200708 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
709 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
710 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
711 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000712
David Woodhousee0c7d762006-05-13 18:07:53 +0100713 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000715 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700717 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100718 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200719 if (!chip->dev_ready) {
720 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000724
Brian Norris8b6e50c2011-05-25 14:59:01 -0700725 /*
726 * Apply this short delay always to ensure that we do wait tWB in
727 * any case on any machine.
728 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000730
731 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200735 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700736 * @chip: the nand chip descriptor
737 * @mtd: MTD device structure
738 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200739 *
740 * Used when in panic, no locks are taken.
741 */
742static void panic_nand_get_device(struct nand_chip *chip,
743 struct mtd_info *mtd, int new_state)
744{
Brian Norris7854d3f2011-06-23 14:12:08 -0700745 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200746 chip->controller->active = chip;
747 chip->state = new_state;
748}
749
750/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700752 * @chip: the nand chip descriptor
753 * @mtd: MTD device structure
754 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *
756 * Get the device and lock it for exclusive access
757 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200758static int
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200759nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200761 spinlock_t *lock = &chip->controller->lock;
762 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100763 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200764retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100765 spin_lock(lock);
766
vimal singhb8b3ee92009-07-09 20:41:22 +0530767 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200768 if (!chip->controller->active)
769 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200770
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200771 if (chip->controller->active == chip && chip->state == FL_READY) {
772 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100773 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100774 return 0;
775 }
776 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800777 if (chip->controller->active->state == FL_PM_SUSPENDED) {
778 chip->state = FL_PM_SUSPENDED;
779 spin_unlock(lock);
780 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800781 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100782 }
783 set_current_state(TASK_UNINTERRUPTIBLE);
784 add_wait_queue(wq, &wait);
785 spin_unlock(lock);
786 schedule();
787 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto retry;
789}
790
791/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 * panic_nand_wait - [GENERIC] wait until the command is done
793 * @mtd: MTD device structure
794 * @chip: NAND chip structure
795 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200796 *
797 * Wait for command done. This is a helper function for nand_wait used when
798 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400799 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200800 */
801static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
802 unsigned long timeo)
803{
804 int i;
805 for (i = 0; i < timeo; i++) {
806 if (chip->dev_ready) {
807 if (chip->dev_ready(mtd))
808 break;
809 } else {
810 if (chip->read_byte(mtd) & NAND_STATUS_READY)
811 break;
812 }
813 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200814 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200815}
816
817/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 * nand_wait - [DEFAULT] wait until the command is done
819 * @mtd: MTD device structure
820 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700822 * Wait for command done. This applies to erase and program only. Erase can
823 * take up to 400ms and program up to 20ms according to general NAND and
824 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700825 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200826static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828
David Woodhousee0c7d762006-05-13 18:07:53 +0100829 unsigned long timeo = jiffies;
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200830 int status, state = chip->state;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (state == FL_ERASING)
David Woodhousee0c7d762006-05-13 18:07:53 +0100833 timeo += (HZ * 400) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100835 timeo += (HZ * 20) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Richard Purdie8fe833c2006-03-31 02:31:14 -0800837 led_trigger_event(nand_led_trigger, LED_FULL);
838
Brian Norris8b6e50c2011-05-25 14:59:01 -0700839 /*
840 * Apply this short delay always to ensure that we do wait tWB in any
841 * case on any machine.
842 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200845 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
846 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000847 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200848 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200850 if (in_interrupt() || oops_in_progress)
851 panic_nand_wait(mtd, chip, timeo);
852 else {
853 while (time_before(jiffies, timeo)) {
854 if (chip->dev_ready) {
855 if (chip->dev_ready(mtd))
856 break;
857 } else {
858 if (chip->read_byte(mtd) & NAND_STATUS_READY)
859 break;
860 }
861 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800864 led_trigger_event(nand_led_trigger, LED_OFF);
865
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200866 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100867 /* This can happen if in case of timeout or buggy dev_ready */
868 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return status;
870}
871
872/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700873 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700874 * @mtd: mtd info
875 * @ofs: offset to start unlock from
876 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700877 * @invert: when = 0, unlock the range of blocks within the lower and
878 * upper boundary address
879 * when = 1, unlock the range of blocks outside the boundaries
880 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530881 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700882 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530883 */
884static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
885 uint64_t len, int invert)
886{
887 int ret = 0;
888 int status, page;
889 struct nand_chip *chip = mtd->priv;
890
891 /* Submit address of first page to unlock */
892 page = ofs >> chip->page_shift;
893 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
894
895 /* Submit address of last page to unlock */
896 page = (ofs + len) >> chip->page_shift;
897 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
898 (page | invert) & chip->pagemask);
899
900 /* Call wait ready function */
901 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530902 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400903 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700904 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530905 __func__, status);
906 ret = -EIO;
907 }
908
909 return ret;
910}
911
912/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700913 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700914 * @mtd: mtd info
915 * @ofs: offset to start unlock from
916 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530917 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700918 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530919 */
920int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
921{
922 int ret = 0;
923 int chipnr;
924 struct nand_chip *chip = mtd->priv;
925
Brian Norris289c0522011-07-19 10:06:09 -0700926 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530927 __func__, (unsigned long long)ofs, len);
928
929 if (check_offs_len(mtd, ofs, len))
930 ret = -EINVAL;
931
932 /* Align to last block address if size addresses end of the device */
933 if (ofs + len == mtd->size)
934 len -= mtd->erasesize;
935
936 nand_get_device(chip, mtd, FL_UNLOCKING);
937
938 /* Shift to get chip number */
939 chipnr = ofs >> chip->chip_shift;
940
941 chip->select_chip(mtd, chipnr);
942
943 /* Check, if it is write protected */
944 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700945 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530946 __func__);
947 ret = -EIO;
948 goto out;
949 }
950
951 ret = __nand_unlock(mtd, ofs, len, 0);
952
953out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800954 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530955 nand_release_device(mtd);
956
957 return ret;
958}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200959EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530960
961/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700962 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700963 * @mtd: mtd info
964 * @ofs: offset to start unlock from
965 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530966 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700967 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
968 * have this feature, but it allows only to lock all blocks, not for specified
969 * range for block. Implementing 'lock' feature by making use of 'unlock', for
970 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530971 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700972 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530973 */
974int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
975{
976 int ret = 0;
977 int chipnr, status, page;
978 struct nand_chip *chip = mtd->priv;
979
Brian Norris289c0522011-07-19 10:06:09 -0700980 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530981 __func__, (unsigned long long)ofs, len);
982
983 if (check_offs_len(mtd, ofs, len))
984 ret = -EINVAL;
985
986 nand_get_device(chip, mtd, FL_LOCKING);
987
988 /* Shift to get chip number */
989 chipnr = ofs >> chip->chip_shift;
990
991 chip->select_chip(mtd, chipnr);
992
993 /* Check, if it is write protected */
994 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700995 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530996 __func__);
997 status = MTD_ERASE_FAILED;
998 ret = -EIO;
999 goto out;
1000 }
1001
1002 /* Submit address of first page to lock */
1003 page = ofs >> chip->page_shift;
1004 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1005
1006 /* Call wait ready function */
1007 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301008 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001009 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001010 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301011 __func__, status);
1012 ret = -EIO;
1013 goto out;
1014 }
1015
1016 ret = __nand_unlock(mtd, ofs, len, 0x1);
1017
1018out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001019 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301020 nand_release_device(mtd);
1021
1022 return ret;
1023}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001024EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301025
1026/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001027 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001028 * @mtd: mtd info structure
1029 * @chip: nand chip info structure
1030 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001031 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001032 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001033 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001034 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001035 */
1036static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001037 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001038{
1039 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001040 if (oob_required)
1041 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001042 return 0;
1043}
1044
1045/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001046 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001047 * @mtd: mtd info structure
1048 * @chip: nand chip info structure
1049 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001050 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001051 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001052 *
1053 * We need a special oob layout and handling even when OOB isn't used.
1054 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001055static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001056 struct nand_chip *chip, uint8_t *buf,
1057 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001058{
1059 int eccsize = chip->ecc.size;
1060 int eccbytes = chip->ecc.bytes;
1061 uint8_t *oob = chip->oob_poi;
1062 int steps, size;
1063
1064 for (steps = chip->ecc.steps; steps > 0; steps--) {
1065 chip->read_buf(mtd, buf, eccsize);
1066 buf += eccsize;
1067
1068 if (chip->ecc.prepad) {
1069 chip->read_buf(mtd, oob, chip->ecc.prepad);
1070 oob += chip->ecc.prepad;
1071 }
1072
1073 chip->read_buf(mtd, oob, eccbytes);
1074 oob += eccbytes;
1075
1076 if (chip->ecc.postpad) {
1077 chip->read_buf(mtd, oob, chip->ecc.postpad);
1078 oob += chip->ecc.postpad;
1079 }
1080 }
1081
1082 size = mtd->oobsize - (oob - chip->oob_poi);
1083 if (size)
1084 chip->read_buf(mtd, oob, size);
1085
1086 return 0;
1087}
1088
1089/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001090 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001091 * @mtd: mtd info structure
1092 * @chip: nand chip info structure
1093 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001094 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001095 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001096 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001097static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001098 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001100 int i, eccsize = chip->ecc.size;
1101 int eccbytes = chip->ecc.bytes;
1102 int eccsteps = chip->ecc.steps;
1103 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001104 uint8_t *ecc_calc = chip->buffers->ecccalc;
1105 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001106 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001107 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001108
Brian Norris1fbb9382012-05-02 10:14:55 -07001109 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001110
1111 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1112 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1113
1114 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001115 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001116
1117 eccsteps = chip->ecc.steps;
1118 p = buf;
1119
1120 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1121 int stat;
1122
1123 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001124 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001125 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001126 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001127 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001128 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1129 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001130 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001131 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001135 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001136 * @mtd: mtd info structure
1137 * @chip: nand chip info structure
1138 * @data_offs: offset of requested data within the page
1139 * @readlen: data length
1140 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001141 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001142static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1143 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001144{
1145 int start_step, end_step, num_steps;
1146 uint32_t *eccpos = chip->ecc.layout->eccpos;
1147 uint8_t *p;
1148 int data_col_addr, i, gaps = 0;
1149 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1150 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001151 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001152 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001153
Brian Norris7854d3f2011-06-23 14:12:08 -07001154 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001155 start_step = data_offs / chip->ecc.size;
1156 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1157 num_steps = end_step - start_step + 1;
1158
Brian Norris8b6e50c2011-05-25 14:59:01 -07001159 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001160 datafrag_len = num_steps * chip->ecc.size;
1161 eccfrag_len = num_steps * chip->ecc.bytes;
1162
1163 data_col_addr = start_step * chip->ecc.size;
1164 /* If we read not a page aligned data */
1165 if (data_col_addr != 0)
1166 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1167
1168 p = bufpoi + data_col_addr;
1169 chip->read_buf(mtd, p, datafrag_len);
1170
Brian Norris8b6e50c2011-05-25 14:59:01 -07001171 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001172 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1173 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1174
Brian Norris8b6e50c2011-05-25 14:59:01 -07001175 /*
1176 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001177 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001178 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001179 for (i = 0; i < eccfrag_len - 1; i++) {
1180 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1181 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1182 gaps = 1;
1183 break;
1184 }
1185 }
1186 if (gaps) {
1187 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1188 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1189 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001190 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001191 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001192 * about buswidth alignment in read_buf.
1193 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001194 index = start_step * chip->ecc.bytes;
1195
1196 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001197 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001198 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001199 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001200 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001201 aligned_len++;
1202
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001203 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1204 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001205 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1206 }
1207
1208 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001209 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001210
1211 p = bufpoi + data_col_addr;
1212 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1213 int stat;
1214
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001215 stat = chip->ecc.correct(mtd, p,
1216 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001217 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001218 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001219 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001220 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001221 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1222 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001223 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001224 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001225}
1226
1227/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001228 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001229 * @mtd: mtd info structure
1230 * @chip: nand chip info structure
1231 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001232 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001233 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001234 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001235 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001236 */
1237static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001238 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001239{
1240 int i, eccsize = chip->ecc.size;
1241 int eccbytes = chip->ecc.bytes;
1242 int eccsteps = chip->ecc.steps;
1243 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001244 uint8_t *ecc_calc = chip->buffers->ecccalc;
1245 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001246 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001247 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001248
1249 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1250 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1251 chip->read_buf(mtd, p, eccsize);
1252 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1253 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001254 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001255
1256 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001257 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001258
1259 eccsteps = chip->ecc.steps;
1260 p = buf;
1261
1262 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1263 int stat;
1264
1265 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001266 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001267 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001268 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001269 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001270 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1271 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001272 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001273 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001274}
1275
1276/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001277 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001278 * @mtd: mtd info structure
1279 * @chip: nand chip info structure
1280 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001281 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001282 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001283 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001284 * Hardware ECC for large page chips, require OOB to be read first. For this
1285 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1286 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1287 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1288 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001289 */
1290static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001291 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001292{
1293 int i, eccsize = chip->ecc.size;
1294 int eccbytes = chip->ecc.bytes;
1295 int eccsteps = chip->ecc.steps;
1296 uint8_t *p = buf;
1297 uint8_t *ecc_code = chip->buffers->ecccode;
1298 uint32_t *eccpos = chip->ecc.layout->eccpos;
1299 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001300 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001301
1302 /* Read the OOB area first */
1303 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1304 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1305 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1306
1307 for (i = 0; i < chip->ecc.total; i++)
1308 ecc_code[i] = chip->oob_poi[eccpos[i]];
1309
1310 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1311 int stat;
1312
1313 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1314 chip->read_buf(mtd, p, eccsize);
1315 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1316
1317 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001318 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001319 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001320 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001321 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001322 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1323 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001324 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001325 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001326}
1327
1328/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001329 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001330 * @mtd: mtd info structure
1331 * @chip: nand chip info structure
1332 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001333 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001334 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001335 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001336 * The hw generator calculates the error syndrome automatically. Therefore we
1337 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001338 */
1339static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001340 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001341{
1342 int i, eccsize = chip->ecc.size;
1343 int eccbytes = chip->ecc.bytes;
1344 int eccsteps = chip->ecc.steps;
1345 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001346 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001348
1349 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1350 int stat;
1351
1352 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1353 chip->read_buf(mtd, p, eccsize);
1354
1355 if (chip->ecc.prepad) {
1356 chip->read_buf(mtd, oob, chip->ecc.prepad);
1357 oob += chip->ecc.prepad;
1358 }
1359
1360 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1361 chip->read_buf(mtd, oob, eccbytes);
1362 stat = chip->ecc.correct(mtd, p, oob, NULL);
1363
Mike Dunn3f91e942012-04-25 12:06:09 -07001364 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001365 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001366 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001367 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001368 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1369 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001370
1371 oob += eccbytes;
1372
1373 if (chip->ecc.postpad) {
1374 chip->read_buf(mtd, oob, chip->ecc.postpad);
1375 oob += chip->ecc.postpad;
1376 }
1377 }
1378
1379 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001380 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001381 if (i)
1382 chip->read_buf(mtd, oob, i);
1383
Mike Dunn3f91e942012-04-25 12:06:09 -07001384 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001385}
1386
1387/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001388 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001389 * @chip: nand chip structure
1390 * @oob: oob destination address
1391 * @ops: oob ops structure
1392 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001393 */
1394static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001395 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001396{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001397 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001398
Brian Norris0612b9d2011-08-30 18:45:40 -07001399 case MTD_OPS_PLACE_OOB:
1400 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001401 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1402 return oob + len;
1403
Brian Norris0612b9d2011-08-30 18:45:40 -07001404 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001405 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001406 uint32_t boffs = 0, roffs = ops->ooboffs;
1407 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001408
Florian Fainellif8ac0412010-09-07 13:23:43 +02001409 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001410 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001411 if (unlikely(roffs)) {
1412 if (roffs >= free->length) {
1413 roffs -= free->length;
1414 continue;
1415 }
1416 boffs = free->offset + roffs;
1417 bytes = min_t(size_t, len,
1418 (free->length - roffs));
1419 roffs = 0;
1420 } else {
1421 bytes = min_t(size_t, len, free->length);
1422 boffs = free->offset;
1423 }
1424 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001425 oob += bytes;
1426 }
1427 return oob;
1428 }
1429 default:
1430 BUG();
1431 }
1432 return NULL;
1433}
1434
1435/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001436 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001437 * @mtd: MTD device structure
1438 * @from: offset to read from
1439 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001440 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001441 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001442 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001443static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1444 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001445{
Brian Norrise47f3db2012-05-02 10:14:56 -07001446 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001447 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001448 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001449 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001451 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001452 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001453 mtd->oobavail : mtd->oobsize;
1454
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001455 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001456 unsigned int max_bitflips = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001458 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001460 chipnr = (int)(from >> chip->chip_shift);
1461 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001463 realpage = (int)(from >> chip->page_shift);
1464 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001466 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001468 buf = ops->datbuf;
1469 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001470 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001471
Florian Fainellif8ac0412010-09-07 13:23:43 +02001472 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001473 bytes = min(mtd->writesize - col, readlen);
1474 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001475
Brian Norris8b6e50c2011-05-25 14:59:01 -07001476 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001477 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001478 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Brian Norrisc00a0992012-05-01 17:12:54 -07001480 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Mike Dunnedbc45402012-04-25 12:06:11 -07001482 /*
1483 * Now read the page into the buffer. Absent an error,
1484 * the read methods return max bitflips per ecc step.
1485 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001486 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001487 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001488 oob_required,
1489 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001490 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1491 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001492 ret = chip->ecc.read_subpage(mtd, chip,
1493 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001494 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001495 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001496 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001497 if (ret < 0) {
1498 if (!aligned)
1499 /* Invalidate page cache */
1500 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001501 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001502 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001503
Mike Dunnedbc45402012-04-25 12:06:11 -07001504 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1505
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001506 /* Transfer not aligned data */
1507 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001508 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001509 !(mtd->ecc_stats.failed - stats.failed) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001510 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001511 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001512 chip->pagebuf_bitflips = ret;
1513 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001514 /* Invalidate page cache */
1515 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001516 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001517 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001519
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001520 buf += bytes;
1521
1522 if (unlikely(oob)) {
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 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001531 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001532 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001533 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001534 max_bitflips = max_t(unsigned int, max_bitflips,
1535 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001538 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001539
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001540 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001541 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Brian Norris8b6e50c2011-05-25 14:59:01 -07001543 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 col = 0;
1545 /* Increment page address */
1546 realpage++;
1547
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001548 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /* Check, if we cross a chip boundary */
1550 if (!page) {
1551 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001552 chip->select_chip(mtd, -1);
1553 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001556 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001558 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001559 if (oob)
1560 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Mike Dunn3f91e942012-04-25 12:06:09 -07001562 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001563 return ret;
1564
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001565 if (mtd->ecc_stats.failed - stats.failed)
1566 return -EBADMSG;
1567
Mike Dunnedbc45402012-04-25 12:06:11 -07001568 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001569}
1570
1571/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001572 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001573 * @mtd: MTD device structure
1574 * @from: offset to read from
1575 * @len: number of bytes to read
1576 * @retlen: pointer to variable to store the number of read bytes
1577 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001578 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001579 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001580 */
1581static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1582 size_t *retlen, uint8_t *buf)
1583{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001584 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07001585 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001586 int ret;
1587
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001588 nand_get_device(chip, mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001589 ops.len = len;
1590 ops.datbuf = buf;
1591 ops.oobbuf = NULL;
Huang Shijie11041ae62012-07-03 16:44:14 +08001592 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001593 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001594 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001595 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
1599/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001600 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001601 * @mtd: mtd info structure
1602 * @chip: nand chip info structure
1603 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001604 */
1605static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001606 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001607{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001608 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001609 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001610 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001611}
1612
1613/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001614 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001615 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001616 * @mtd: mtd info structure
1617 * @chip: nand chip info structure
1618 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001619 */
1620static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001621 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001622{
1623 uint8_t *buf = chip->oob_poi;
1624 int length = mtd->oobsize;
1625 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1626 int eccsize = chip->ecc.size;
1627 uint8_t *bufpoi = buf;
1628 int i, toread, sndrnd = 0, pos;
1629
1630 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1631 for (i = 0; i < chip->ecc.steps; i++) {
1632 if (sndrnd) {
1633 pos = eccsize + i * (eccsize + chunk);
1634 if (mtd->writesize > 512)
1635 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1636 else
1637 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1638 } else
1639 sndrnd = 1;
1640 toread = min_t(int, length, chunk);
1641 chip->read_buf(mtd, bufpoi, toread);
1642 bufpoi += toread;
1643 length -= toread;
1644 }
1645 if (length > 0)
1646 chip->read_buf(mtd, bufpoi, length);
1647
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001648 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001649}
1650
1651/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001652 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001653 * @mtd: mtd info structure
1654 * @chip: nand chip info structure
1655 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001656 */
1657static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1658 int page)
1659{
1660 int status = 0;
1661 const uint8_t *buf = chip->oob_poi;
1662 int length = mtd->oobsize;
1663
1664 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1665 chip->write_buf(mtd, buf, length);
1666 /* Send command to program the OOB data */
1667 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1668
1669 status = chip->waitfunc(mtd, chip);
1670
Savin Zlobec0d420f92006-06-21 11:51:20 +02001671 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001672}
1673
1674/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001675 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001676 * with syndrome - only for large page flash
1677 * @mtd: mtd info structure
1678 * @chip: nand chip info structure
1679 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001680 */
1681static int nand_write_oob_syndrome(struct mtd_info *mtd,
1682 struct nand_chip *chip, int page)
1683{
1684 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1685 int eccsize = chip->ecc.size, length = mtd->oobsize;
1686 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1687 const uint8_t *bufpoi = chip->oob_poi;
1688
1689 /*
1690 * data-ecc-data-ecc ... ecc-oob
1691 * or
1692 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1693 */
1694 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1695 pos = steps * (eccsize + chunk);
1696 steps = 0;
1697 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001698 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001699
1700 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1701 for (i = 0; i < steps; i++) {
1702 if (sndcmd) {
1703 if (mtd->writesize <= 512) {
1704 uint32_t fill = 0xFFFFFFFF;
1705
1706 len = eccsize;
1707 while (len > 0) {
1708 int num = min_t(int, len, 4);
1709 chip->write_buf(mtd, (uint8_t *)&fill,
1710 num);
1711 len -= num;
1712 }
1713 } else {
1714 pos = eccsize + i * (eccsize + chunk);
1715 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1716 }
1717 } else
1718 sndcmd = 1;
1719 len = min_t(int, length, chunk);
1720 chip->write_buf(mtd, bufpoi, len);
1721 bufpoi += len;
1722 length -= len;
1723 }
1724 if (length > 0)
1725 chip->write_buf(mtd, bufpoi, length);
1726
1727 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1728 status = chip->waitfunc(mtd, chip);
1729
1730 return status & NAND_STATUS_FAIL ? -EIO : 0;
1731}
1732
1733/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001734 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001735 * @mtd: MTD device structure
1736 * @from: offset to read from
1737 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001739 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001741static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1742 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Brian Norrisc00a0992012-05-01 17:12:54 -07001744 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001745 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001746 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001747 int readlen = ops->ooblen;
1748 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001749 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001750 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Brian Norris289c0522011-07-19 10:06:09 -07001752 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301753 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Brian Norris041e4572011-06-23 16:45:24 -07001755 stats = mtd->ecc_stats;
1756
Brian Norris0612b9d2011-08-30 18:45:40 -07001757 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001758 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001759 else
1760 len = mtd->oobsize;
1761
1762 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001763 pr_debug("%s: attempt to start read outside oob\n",
1764 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001765 return -EINVAL;
1766 }
1767
1768 /* Do not allow reads past end of device */
1769 if (unlikely(from >= mtd->size ||
1770 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1771 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001772 pr_debug("%s: attempt to read beyond end of device\n",
1773 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001774 return -EINVAL;
1775 }
Vitaly Wool70145682006-11-03 18:20:38 +03001776
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001777 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001778 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001780 /* Shift to get page */
1781 realpage = (int)(from >> chip->page_shift);
1782 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Florian Fainellif8ac0412010-09-07 13:23:43 +02001784 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001785 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001786 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001787 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001788 ret = chip->ecc.read_oob(mtd, chip, page);
1789
1790 if (ret < 0)
1791 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001792
1793 len = min(len, readlen);
1794 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001795
Vitaly Wool70145682006-11-03 18:20:38 +03001796 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001797 if (!readlen)
1798 break;
1799
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001800 /* Increment page address */
1801 realpage++;
1802
1803 page = realpage & chip->pagemask;
1804 /* Check, if we cross a chip boundary */
1805 if (!page) {
1806 chipnr++;
1807 chip->select_chip(mtd, -1);
1808 chip->select_chip(mtd, chipnr);
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001811 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001813 ops->oobretlen = ops->ooblen - readlen;
1814
1815 if (ret < 0)
1816 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001817
1818 if (mtd->ecc_stats.failed - stats.failed)
1819 return -EBADMSG;
1820
1821 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001825 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001826 * @mtd: MTD device structure
1827 * @from: offset to read from
1828 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001830 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001832static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1833 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001835 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001836 int ret = -ENOTSUPP;
1837
1838 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001841 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001842 pr_debug("%s: attempt to read beyond end of device\n",
1843 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return -EINVAL;
1845 }
1846
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001847 nand_get_device(chip, mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Florian Fainellif8ac0412010-09-07 13:23:43 +02001849 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001850 case MTD_OPS_PLACE_OOB:
1851 case MTD_OPS_AUTO_OOB:
1852 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001853 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001854
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001855 default:
1856 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001859 if (!ops->datbuf)
1860 ret = nand_do_read_oob(mtd, from, ops);
1861 else
1862 ret = nand_do_read_ops(mtd, from, ops);
1863
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001866 return ret;
1867}
1868
1869
1870/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001871 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001872 * @mtd: mtd info structure
1873 * @chip: nand chip info structure
1874 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001875 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001876 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001877 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001878 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001879static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001880 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001881{
1882 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001883 if (oob_required)
1884 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001885
1886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001889/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001890 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001891 * @mtd: mtd info structure
1892 * @chip: nand chip info structure
1893 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001894 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001895 *
1896 * We need a special oob layout and handling even when ECC isn't checked.
1897 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001898static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001899 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001900 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001901{
1902 int eccsize = chip->ecc.size;
1903 int eccbytes = chip->ecc.bytes;
1904 uint8_t *oob = chip->oob_poi;
1905 int steps, size;
1906
1907 for (steps = chip->ecc.steps; steps > 0; steps--) {
1908 chip->write_buf(mtd, buf, eccsize);
1909 buf += eccsize;
1910
1911 if (chip->ecc.prepad) {
1912 chip->write_buf(mtd, oob, chip->ecc.prepad);
1913 oob += chip->ecc.prepad;
1914 }
1915
1916 chip->read_buf(mtd, oob, eccbytes);
1917 oob += eccbytes;
1918
1919 if (chip->ecc.postpad) {
1920 chip->write_buf(mtd, oob, chip->ecc.postpad);
1921 oob += chip->ecc.postpad;
1922 }
1923 }
1924
1925 size = mtd->oobsize - (oob - chip->oob_poi);
1926 if (size)
1927 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001928
1929 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001930}
1931/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001932 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001933 * @mtd: mtd info structure
1934 * @chip: nand chip info structure
1935 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001936 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001937 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001938static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001939 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001940{
1941 int i, eccsize = chip->ecc.size;
1942 int eccbytes = chip->ecc.bytes;
1943 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001944 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001945 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001946 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001947
Brian Norris7854d3f2011-06-23 14:12:08 -07001948 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001949 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1950 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001951
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001952 for (i = 0; i < chip->ecc.total; i++)
1953 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001954
Josh Wufdbad98d2012-06-25 18:07:45 +08001955 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001956}
1957
1958/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001959 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001960 * @mtd: mtd info structure
1961 * @chip: nand chip info structure
1962 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001963 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001964 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001965static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001966 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001967{
1968 int i, eccsize = chip->ecc.size;
1969 int eccbytes = chip->ecc.bytes;
1970 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001971 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001972 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001973 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001974
1975 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1976 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001977 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001978 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1979 }
1980
1981 for (i = 0; i < chip->ecc.total; i++)
1982 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1983
1984 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001985
1986 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001987}
1988
1989/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001990 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07001991 * @mtd: mtd info structure
1992 * @chip: nand chip info structure
1993 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001994 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001995 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001996 * The hw generator calculates the error syndrome automatically. Therefore we
1997 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001998 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001999static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002000 struct nand_chip *chip,
2001 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002002{
2003 int i, eccsize = chip->ecc.size;
2004 int eccbytes = chip->ecc.bytes;
2005 int eccsteps = chip->ecc.steps;
2006 const uint8_t *p = buf;
2007 uint8_t *oob = chip->oob_poi;
2008
2009 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2010
2011 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2012 chip->write_buf(mtd, p, eccsize);
2013
2014 if (chip->ecc.prepad) {
2015 chip->write_buf(mtd, oob, chip->ecc.prepad);
2016 oob += chip->ecc.prepad;
2017 }
2018
2019 chip->ecc.calculate(mtd, p, oob);
2020 chip->write_buf(mtd, oob, eccbytes);
2021 oob += eccbytes;
2022
2023 if (chip->ecc.postpad) {
2024 chip->write_buf(mtd, oob, chip->ecc.postpad);
2025 oob += chip->ecc.postpad;
2026 }
2027 }
2028
2029 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002030 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002031 if (i)
2032 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002033
2034 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002035}
2036
2037/**
David Woodhouse956e9442006-09-25 17:12:39 +01002038 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002039 * @mtd: MTD device structure
2040 * @chip: NAND chip descriptor
2041 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002042 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002043 * @page: page number to write
2044 * @cached: cached programming
2045 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002046 */
2047static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002048 const uint8_t *buf, int oob_required, int page,
2049 int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002050{
2051 int status;
2052
2053 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2054
David Woodhouse956e9442006-09-25 17:12:39 +01002055 if (unlikely(raw))
Josh Wufdbad98d2012-06-25 18:07:45 +08002056 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002057 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002058 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2059
2060 if (status < 0)
2061 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002062
2063 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002064 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002065 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002066 */
2067 cached = 0;
2068
2069 if (!cached || !(chip->options & NAND_CACHEPRG)) {
2070
2071 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002072 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002073 /*
2074 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002075 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002076 */
2077 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2078 status = chip->errstat(mtd, chip, FL_WRITING, status,
2079 page);
2080
2081 if (status & NAND_STATUS_FAIL)
2082 return -EIO;
2083 } else {
2084 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002085 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002086 }
2087
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002088 return 0;
2089}
2090
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002091/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002092 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002093 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002094 * @oob: oob data buffer
2095 * @len: oob data write length
2096 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002097 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002098static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2099 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002100{
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002101 struct nand_chip *chip = mtd->priv;
2102
2103 /*
2104 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2105 * data from a previous OOB read.
2106 */
2107 memset(chip->oob_poi, 0xff, mtd->oobsize);
2108
Florian Fainellif8ac0412010-09-07 13:23:43 +02002109 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002110
Brian Norris0612b9d2011-08-30 18:45:40 -07002111 case MTD_OPS_PLACE_OOB:
2112 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002113 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2114 return oob + len;
2115
Brian Norris0612b9d2011-08-30 18:45:40 -07002116 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002117 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002118 uint32_t boffs = 0, woffs = ops->ooboffs;
2119 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002120
Florian Fainellif8ac0412010-09-07 13:23:43 +02002121 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002122 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002123 if (unlikely(woffs)) {
2124 if (woffs >= free->length) {
2125 woffs -= free->length;
2126 continue;
2127 }
2128 boffs = free->offset + woffs;
2129 bytes = min_t(size_t, len,
2130 (free->length - woffs));
2131 woffs = 0;
2132 } else {
2133 bytes = min_t(size_t, len, free->length);
2134 boffs = free->offset;
2135 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002136 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002137 oob += bytes;
2138 }
2139 return oob;
2140 }
2141 default:
2142 BUG();
2143 }
2144 return NULL;
2145}
2146
Florian Fainellif8ac0412010-09-07 13:23:43 +02002147#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002148
2149/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002150 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002151 * @mtd: MTD device structure
2152 * @to: offset to write to
2153 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002155 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002156 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002157static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2158 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002159{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002160 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002161 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002162 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002163
2164 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002165 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002166 mtd->oobavail : mtd->oobsize;
2167
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002168 uint8_t *oob = ops->oobbuf;
2169 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002170 int ret, subpage;
Brian Norrise47f3db2012-05-02 10:14:56 -07002171 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002172
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002173 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002174 if (!writelen)
2175 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002176
Brian Norris8b6e50c2011-05-25 14:59:01 -07002177 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002178 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002179 pr_notice("%s: attempt to write non page aligned data\n",
2180 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002181 return -EINVAL;
2182 }
2183
Thomas Gleixner29072b92006-09-28 15:38:36 +02002184 column = to & (mtd->writesize - 1);
2185 subpage = column || (writelen & (mtd->writesize - 1));
2186
2187 if (subpage && oob)
2188 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002189
Thomas Gleixner6a930962006-06-28 00:11:45 +02002190 chipnr = (int)(to >> chip->chip_shift);
2191 chip->select_chip(mtd, chipnr);
2192
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002193 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002194 if (nand_check_wp(mtd)) {
2195 ret = -EIO;
2196 goto err_out;
2197 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002198
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002199 realpage = (int)(to >> chip->page_shift);
2200 page = realpage & chip->pagemask;
2201 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2202
2203 /* Invalidate the page cache, when we write to the cached page */
2204 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002205 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002206 chip->pagebuf = -1;
2207
Maxim Levitsky782ce792010-02-22 20:39:36 +02002208 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002209 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2210 ret = -EINVAL;
2211 goto err_out;
2212 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002213
Florian Fainellif8ac0412010-09-07 13:23:43 +02002214 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002215 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002216 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002217 uint8_t *wbuf = buf;
2218
Brian Norris8b6e50c2011-05-25 14:59:01 -07002219 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002220 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2221 cached = 0;
2222 bytes = min_t(int, bytes - column, (int) writelen);
2223 chip->pagebuf = -1;
2224 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2225 memcpy(&chip->buffers->databuf[column], buf, bytes);
2226 wbuf = chip->buffers->databuf;
2227 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002228
Maxim Levitsky782ce792010-02-22 20:39:36 +02002229 if (unlikely(oob)) {
2230 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002231 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002232 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002233 } else {
2234 /* We still need to erase leftover OOB data */
2235 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002236 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002237
Brian Norrise47f3db2012-05-02 10:14:56 -07002238 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2239 cached, (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002240 if (ret)
2241 break;
2242
2243 writelen -= bytes;
2244 if (!writelen)
2245 break;
2246
Thomas Gleixner29072b92006-09-28 15:38:36 +02002247 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002248 buf += bytes;
2249 realpage++;
2250
2251 page = realpage & chip->pagemask;
2252 /* Check, if we cross a chip boundary */
2253 if (!page) {
2254 chipnr++;
2255 chip->select_chip(mtd, -1);
2256 chip->select_chip(mtd, chipnr);
2257 }
2258 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002259
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002260 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002261 if (unlikely(oob))
2262 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002263
2264err_out:
2265 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002266 return ret;
2267}
2268
2269/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002270 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002271 * @mtd: MTD device structure
2272 * @to: offset to write to
2273 * @len: number of bytes to write
2274 * @retlen: pointer to variable to store the number of written bytes
2275 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002276 *
2277 * NAND write with ECC. Used when performing writes in interrupt context, this
2278 * may for example be called by mtdoops when writing an oops while in panic.
2279 */
2280static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2281 size_t *retlen, const uint8_t *buf)
2282{
2283 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002284 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002285 int ret;
2286
Brian Norris8b6e50c2011-05-25 14:59:01 -07002287 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002288 panic_nand_wait(mtd, chip, 400);
2289
Brian Norris8b6e50c2011-05-25 14:59:01 -07002290 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002291 panic_nand_get_device(chip, mtd, FL_WRITING);
2292
Brian Norris4a89ff82011-08-30 18:45:45 -07002293 ops.len = len;
2294 ops.datbuf = (uint8_t *)buf;
2295 ops.oobbuf = NULL;
Huang Shijie11041ae62012-07-03 16:44:14 +08002296 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002297
Brian Norris4a89ff82011-08-30 18:45:45 -07002298 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002299
Brian Norris4a89ff82011-08-30 18:45:45 -07002300 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002301 return ret;
2302}
2303
2304/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002305 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002306 * @mtd: MTD device structure
2307 * @to: offset to write to
2308 * @len: number of bytes to write
2309 * @retlen: pointer to variable to store the number of written bytes
2310 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002312 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002314static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002315 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002317 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002318 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002319 int ret;
2320
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002321 nand_get_device(chip, mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002322 ops.len = len;
2323 ops.datbuf = (uint8_t *)buf;
2324 ops.oobbuf = NULL;
Huang Shijie11041ae62012-07-03 16:44:14 +08002325 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002326 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002327 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002329 return ret;
2330}
2331
2332/**
2333 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002334 * @mtd: MTD device structure
2335 * @to: offset to write to
2336 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002337 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002338 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002339 */
2340static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2341 struct mtd_oob_ops *ops)
2342{
Adrian Hunter03736152007-01-31 17:58:29 +02002343 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002344 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Brian Norris289c0522011-07-19 10:06:09 -07002346 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302347 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Brian Norris0612b9d2011-08-30 18:45:40 -07002349 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002350 len = chip->ecc.layout->oobavail;
2351 else
2352 len = mtd->oobsize;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002355 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002356 pr_debug("%s: attempt to write past end of page\n",
2357 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return -EINVAL;
2359 }
2360
Adrian Hunter03736152007-01-31 17:58:29 +02002361 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002362 pr_debug("%s: attempt to start write outside oob\n",
2363 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002364 return -EINVAL;
2365 }
2366
Jason Liu775adc3d42011-02-25 13:06:18 +08002367 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002368 if (unlikely(to >= mtd->size ||
2369 ops->ooboffs + ops->ooblen >
2370 ((mtd->size >> chip->page_shift) -
2371 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002372 pr_debug("%s: attempt to write beyond end of device\n",
2373 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002374 return -EINVAL;
2375 }
2376
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002377 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002378 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002380 /* Shift to get page */
2381 page = (int)(to >> chip->page_shift);
2382
2383 /*
2384 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2385 * of my DiskOnChip 2000 test units) will clear the whole data page too
2386 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2387 * it in the doc2000 driver in August 1999. dwmw2.
2388 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002389 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002392 if (nand_check_wp(mtd)) {
2393 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002394 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002395 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002398 if (page == chip->pagebuf)
2399 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002401 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002402
Brian Norris0612b9d2011-08-30 18:45:40 -07002403 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002404 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2405 else
2406 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002407
Huang Shijieb0bb6902012-11-19 14:43:29 +08002408 chip->select_chip(mtd, -1);
2409
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002410 if (status)
2411 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Vitaly Wool70145682006-11-03 18:20:38 +03002413 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002415 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002418/**
2419 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002420 * @mtd: MTD device structure
2421 * @to: offset to write to
2422 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002423 */
2424static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2425 struct mtd_oob_ops *ops)
2426{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002427 struct nand_chip *chip = mtd->priv;
2428 int ret = -ENOTSUPP;
2429
2430 ops->retlen = 0;
2431
2432 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002433 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002434 pr_debug("%s: attempt to write beyond end of device\n",
2435 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002436 return -EINVAL;
2437 }
2438
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002439 nand_get_device(chip, mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002440
Florian Fainellif8ac0412010-09-07 13:23:43 +02002441 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002442 case MTD_OPS_PLACE_OOB:
2443 case MTD_OPS_AUTO_OOB:
2444 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002445 break;
2446
2447 default:
2448 goto out;
2449 }
2450
2451 if (!ops->datbuf)
2452 ret = nand_do_write_oob(mtd, to, ops);
2453 else
2454 ret = nand_do_write_ops(mtd, to, ops);
2455
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002456out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002457 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return ret;
2459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002462 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002463 * @mtd: MTD device structure
2464 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002466 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002468static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002470 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002472 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2473 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
2476/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002477 * multi_erase_cmd - [GENERIC] AND specific block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002478 * @mtd: MTD device structure
2479 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002481 * AND multi block erase command function. Erase 4 consecutive blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002483static void multi_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002485 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002487 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2488 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2489 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2490 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2491 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494/**
2495 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002496 * @mtd: MTD device structure
2497 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002499 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002501static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
David Woodhousee0c7d762006-05-13 18:07:53 +01002503 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002505
David A. Marlin30f464b2005-01-17 18:35:25 +00002506#define BBT_PAGE_MASK 0xffffff3f
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002508 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002509 * @mtd: MTD device structure
2510 * @instr: erase instruction
2511 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002513 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002515int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2516 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Adrian Hunter69423d92008-12-10 13:37:21 +00002518 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002519 struct nand_chip *chip = mtd->priv;
Florian Fainellif8ac0412010-09-07 13:23:43 +02002520 loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002521 unsigned int bbt_masked_page = 0xffffffff;
Adrian Hunter69423d92008-12-10 13:37:21 +00002522 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Brian Norris289c0522011-07-19 10:06:09 -07002524 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2525 __func__, (unsigned long long)instr->addr,
2526 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302528 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002532 nand_get_device(chip, mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002535 page = (int)(instr->addr >> chip->page_shift);
2536 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002539 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002542 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* Check, if it is write protected */
2545 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002546 pr_debug("%s: device is write protected!\n",
2547 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 instr->state = MTD_ERASE_FAILED;
2549 goto erase_exit;
2550 }
2551
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002552 /*
2553 * If BBT requires refresh, set the BBT page mask to see if the BBT
2554 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2555 * can not be matched. This is also done when the bbt is actually
Brian Norris7854d3f2011-06-23 14:12:08 -07002556 * erased to avoid recursive updates.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002557 */
2558 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2559 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
David A. Marlin30f464b2005-01-17 18:35:25 +00002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /* Loop through the pages */
2562 len = instr->len;
2563
2564 instr->state = MTD_ERASING;
2565
2566 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002567 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002568 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2569 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002570 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2571 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 instr->state = MTD_ERASE_FAILED;
2573 goto erase_exit;
2574 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002575
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002576 /*
2577 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002578 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 */
2580 if (page <= chip->pagebuf && chip->pagebuf <
2581 (page + pages_per_block))
2582 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002584 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002585
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002586 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002588 /*
2589 * See if operation failed and additional status checks are
2590 * available
2591 */
2592 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2593 status = chip->errstat(mtd, chip, FL_ERASING,
2594 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002597 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002598 pr_debug("%s: failed erase, page 0x%08x\n",
2599 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002601 instr->fail_addr =
2602 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 goto erase_exit;
2604 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002605
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002606 /*
2607 * If BBT requires refresh, set the BBT rewrite flag to the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002608 * page being erased.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002609 */
2610 if (bbt_masked_page != 0xffffffff &&
2611 (page & BBT_PAGE_MASK) == bbt_masked_page)
Adrian Hunter69423d92008-12-10 13:37:21 +00002612 rewrite_bbt[chipnr] =
2613 ((loff_t)page << chip->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002616 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 page += pages_per_block;
2618
2619 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002620 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002622 chip->select_chip(mtd, -1);
2623 chip->select_chip(mtd, chipnr);
David A. Marlin30f464b2005-01-17 18:35:25 +00002624
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 /*
2626 * If BBT requires refresh and BBT-PERCHIP, set the BBT
Brian Norris8b6e50c2011-05-25 14:59:01 -07002627 * page mask to see if this BBT should be rewritten.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002628 */
2629 if (bbt_masked_page != 0xffffffff &&
2630 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2631 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2632 BBT_PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
2634 }
2635 instr->state = MTD_ERASE_DONE;
2636
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002637erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002642 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 nand_release_device(mtd);
2644
David Woodhouse49defc02007-10-06 15:01:59 -04002645 /* Do call back function */
2646 if (!ret)
2647 mtd_erase_callback(instr);
2648
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002649 /*
2650 * If BBT requires refresh and erase was successful, rewrite any
Brian Norris8b6e50c2011-05-25 14:59:01 -07002651 * selected bad block tables.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002652 */
2653 if (bbt_masked_page == 0xffffffff || ret)
2654 return ret;
2655
2656 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2657 if (!rewrite_bbt[chipnr])
2658 continue;
Brian Norris8b6e50c2011-05-25 14:59:01 -07002659 /* Update the BBT for chip */
Brian Norris289c0522011-07-19 10:06:09 -07002660 pr_debug("%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2661 __func__, chipnr, rewrite_bbt[chipnr],
2662 chip->bbt_td->pages[chipnr]);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002663 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
David A. Marlin30f464b2005-01-17 18:35:25 +00002664 }
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 /* Return more or less happy */
2667 return ret;
2668}
2669
2670/**
2671 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002672 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002674 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002676static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002678 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Brian Norris289c0522011-07-19 10:06:09 -07002680 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 /* Grab the lock and see if the device is available */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002683 nand_get_device(chip, mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002685 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002689 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002690 * @mtd: MTD device structure
2691 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002693static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002695 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
2698/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002699 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002700 * @mtd: MTD device structure
2701 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002703static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002705 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int ret;
2707
Florian Fainellif8ac0412010-09-07 13:23:43 +02002708 ret = nand_block_isbad(mtd, ofs);
2709 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002710 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (ret > 0)
2712 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002713 return ret;
2714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002716 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
2719/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002720 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2721 * @mtd: MTD device structure
2722 * @chip: nand chip info structure
2723 * @addr: feature address.
2724 * @subfeature_param: the subfeature parameters, a four bytes array.
2725 */
2726static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2727 int addr, uint8_t *subfeature_param)
2728{
2729 int status;
2730
2731 if (!chip->onfi_version)
2732 return -EINVAL;
2733
2734 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2735 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2736 status = chip->waitfunc(mtd, chip);
2737 if (status & NAND_STATUS_FAIL)
2738 return -EIO;
2739 return 0;
2740}
2741
2742/**
2743 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2744 * @mtd: MTD device structure
2745 * @chip: nand chip info structure
2746 * @addr: feature address.
2747 * @subfeature_param: the subfeature parameters, a four bytes array.
2748 */
2749static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2750 int addr, uint8_t *subfeature_param)
2751{
2752 if (!chip->onfi_version)
2753 return -EINVAL;
2754
2755 /* clear the sub feature parameters */
2756 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2757
2758 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2759 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2760 return 0;
2761}
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;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002818 if (!chip->scan_bbt)
2819 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002820
2821 if (!chip->controller) {
2822 chip->controller = &chip->hwcontrol;
2823 spin_lock_init(&chip->controller->lock);
2824 init_waitqueue_head(&chip->controller->wq);
2825 }
2826
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002827}
2828
Brian Norris8b6e50c2011-05-25 14:59:01 -07002829/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002830static void sanitize_string(uint8_t *s, size_t len)
2831{
2832 ssize_t i;
2833
Brian Norris8b6e50c2011-05-25 14:59:01 -07002834 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002835 s[len - 1] = 0;
2836
Brian Norris8b6e50c2011-05-25 14:59:01 -07002837 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002838 for (i = 0; i < len - 1; i++) {
2839 if (s[i] < ' ' || s[i] > 127)
2840 s[i] = '?';
2841 }
2842
Brian Norris8b6e50c2011-05-25 14:59:01 -07002843 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002844 strim(s);
2845}
2846
2847static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2848{
2849 int i;
2850 while (len--) {
2851 crc ^= *p++ << 8;
2852 for (i = 0; i < 8; i++)
2853 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2854 }
2855
2856 return crc;
2857}
2858
2859/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002860 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002861 */
2862static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002863 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002864{
2865 struct nand_onfi_params *p = &chip->onfi_params;
2866 int i;
2867 int val;
2868
Brian Norris7854d3f2011-06-23 14:12:08 -07002869 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002870 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2871 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2872 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2873 return 0;
2874
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002875 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2876 for (i = 0; i < 3; i++) {
2877 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2878 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2879 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002880 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002881 break;
2882 }
2883 }
2884
2885 if (i == 3)
2886 return 0;
2887
Brian Norris8b6e50c2011-05-25 14:59:01 -07002888 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002889 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002890 if (val & (1 << 5))
2891 chip->onfi_version = 23;
2892 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002893 chip->onfi_version = 22;
2894 else if (val & (1 << 3))
2895 chip->onfi_version = 21;
2896 else if (val & (1 << 2))
2897 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002898 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002899 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002900 else
2901 chip->onfi_version = 0;
2902
2903 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002904 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002905 return 0;
2906 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002907
2908 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2909 sanitize_string(p->model, sizeof(p->model));
2910 if (!mtd->name)
2911 mtd->name = p->model;
2912 mtd->writesize = le32_to_cpu(p->byte_per_page);
2913 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2914 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTET63795752012-03-19 15:35:25 +01002915 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2916 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002917 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002918 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002919 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002920
Huang Shijied42b5de2012-02-17 11:22:37 +08002921 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002922 return 1;
2923}
2924
2925/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07002926 * nand_id_has_period - Check if an ID string has a given wraparound period
2927 * @id_data: the ID string
2928 * @arrlen: the length of the @id_data array
2929 * @period: the period of repitition
2930 *
2931 * Check if an ID string is repeated within a given sequence of bytes at
2932 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
2933 * period of 2). This is a helper function for nand_id_len(). Returns non-zero
2934 * if the repetition has a period of @period; otherwise, returns zero.
2935 */
2936static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2937{
2938 int i, j;
2939 for (i = 0; i < period; i++)
2940 for (j = i + period; j < arrlen; j += period)
2941 if (id_data[i] != id_data[j])
2942 return 0;
2943 return 1;
2944}
2945
2946/*
2947 * nand_id_len - Get the length of an ID string returned by CMD_READID
2948 * @id_data: the ID string
2949 * @arrlen: the length of the @id_data array
2950
2951 * Returns the length of the ID string, according to known wraparound/trailing
2952 * zero patterns. If no pattern exists, returns the length of the array.
2953 */
2954static int nand_id_len(u8 *id_data, int arrlen)
2955{
2956 int last_nonzero, period;
2957
2958 /* Find last non-zero byte */
2959 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2960 if (id_data[last_nonzero])
2961 break;
2962
2963 /* All zeros */
2964 if (last_nonzero < 0)
2965 return 0;
2966
2967 /* Calculate wraparound period */
2968 for (period = 1; period < arrlen; period++)
2969 if (nand_id_has_period(id_data, arrlen, period))
2970 break;
2971
2972 /* There's a repeated pattern */
2973 if (period < arrlen)
2974 return period;
2975
2976 /* There are trailing zeros */
2977 if (last_nonzero < arrlen - 1)
2978 return last_nonzero + 1;
2979
2980 /* No pattern detected */
2981 return arrlen;
2982}
2983
2984/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002985 * Many new NAND share similar device ID codes, which represent the size of the
2986 * chip. The rest of the parameters must be decoded according to generic or
2987 * manufacturer-specific "extended ID" decoding patterns.
2988 */
2989static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2990 u8 id_data[8], int *busw)
2991{
Brian Norrise3b88bd2012-09-24 20:40:52 -07002992 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002993 /* The 3rd id byte holds MLC / multichip data */
2994 chip->cellinfo = id_data[2];
2995 /* The 4th id byte is the important one */
2996 extid = id_data[3];
2997
Brian Norrise3b88bd2012-09-24 20:40:52 -07002998 id_len = nand_id_len(id_data, 8);
2999
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003000 /*
3001 * Field definitions are in the following datasheets:
3002 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003003 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003004 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003005 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003006 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3007 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003008 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003009 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Brian Norris6924d992012-11-14 21:46:30 -08003010 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Brian Norrisaf451af2012-10-09 23:26:06 -07003011 id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003012 /* Calc pagesize */
3013 mtd->writesize = 2048 << (extid & 0x03);
3014 extid >>= 2;
3015 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003016 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003017 case 1:
3018 mtd->oobsize = 128;
3019 break;
3020 case 2:
3021 mtd->oobsize = 218;
3022 break;
3023 case 3:
3024 mtd->oobsize = 400;
3025 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003026 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003027 mtd->oobsize = 436;
3028 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003029 case 5:
3030 mtd->oobsize = 512;
3031 break;
3032 case 6:
3033 default: /* Other cases are "reserved" (unknown) */
3034 mtd->oobsize = 640;
3035 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003036 }
3037 extid >>= 2;
3038 /* Calc blocksize */
3039 mtd->erasesize = (128 * 1024) <<
3040 (((extid >> 1) & 0x04) | (extid & 0x03));
3041 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003042 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3043 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3044 unsigned int tmp;
3045
3046 /* Calc pagesize */
3047 mtd->writesize = 2048 << (extid & 0x03);
3048 extid >>= 2;
3049 /* Calc oobsize */
3050 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3051 case 0:
3052 mtd->oobsize = 128;
3053 break;
3054 case 1:
3055 mtd->oobsize = 224;
3056 break;
3057 case 2:
3058 mtd->oobsize = 448;
3059 break;
3060 case 3:
3061 mtd->oobsize = 64;
3062 break;
3063 case 4:
3064 mtd->oobsize = 32;
3065 break;
3066 case 5:
3067 mtd->oobsize = 16;
3068 break;
3069 default:
3070 mtd->oobsize = 640;
3071 break;
3072 }
3073 extid >>= 2;
3074 /* Calc blocksize */
3075 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3076 if (tmp < 0x03)
3077 mtd->erasesize = (128 * 1024) << tmp;
3078 else if (tmp == 0x03)
3079 mtd->erasesize = 768 * 1024;
3080 else
3081 mtd->erasesize = (64 * 1024) << tmp;
3082 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003083 } else {
3084 /* Calc pagesize */
3085 mtd->writesize = 1024 << (extid & 0x03);
3086 extid >>= 2;
3087 /* Calc oobsize */
3088 mtd->oobsize = (8 << (extid & 0x01)) *
3089 (mtd->writesize >> 9);
3090 extid >>= 2;
3091 /* Calc blocksize. Blocksize is multiples of 64KiB */
3092 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3093 extid >>= 2;
3094 /* Get buswidth information */
3095 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3096 }
3097}
3098
3099/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003100 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3101 * decodes a matching ID table entry and assigns the MTD size parameters for
3102 * the chip.
3103 */
3104static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3105 struct nand_flash_dev *type, u8 id_data[8],
3106 int *busw)
3107{
3108 int maf_id = id_data[0];
3109
3110 mtd->erasesize = type->erasesize;
3111 mtd->writesize = type->pagesize;
3112 mtd->oobsize = mtd->writesize / 32;
3113 *busw = type->options & NAND_BUSWIDTH_16;
3114
3115 /*
3116 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3117 * some Spansion chips have erasesize that conflicts with size
3118 * listed in nand_ids table.
3119 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3120 */
3121 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3122 && id_data[6] == 0x00 && id_data[7] == 0x00
3123 && mtd->writesize == 512) {
3124 mtd->erasesize = 128 * 1024;
3125 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3126 }
3127}
3128
3129/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003130 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3131 * heuristic patterns using various detected parameters (e.g., manufacturer,
3132 * page size, cell-type information).
3133 */
3134static void nand_decode_bbm_options(struct mtd_info *mtd,
3135 struct nand_chip *chip, u8 id_data[8])
3136{
3137 int maf_id = id_data[0];
3138
3139 /* Set the bad block position */
3140 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3141 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3142 else
3143 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3144
3145 /*
3146 * Bad block marker is stored in the last page of each block on Samsung
3147 * and Hynix MLC devices; stored in first two pages of each block on
3148 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3149 * AMD/Spansion, and Macronix. All others scan only the first page.
3150 */
3151 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3152 (maf_id == NAND_MFR_SAMSUNG ||
3153 maf_id == NAND_MFR_HYNIX))
3154 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3155 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3156 (maf_id == NAND_MFR_SAMSUNG ||
3157 maf_id == NAND_MFR_HYNIX ||
3158 maf_id == NAND_MFR_TOSHIBA ||
3159 maf_id == NAND_MFR_AMD ||
3160 maf_id == NAND_MFR_MACRONIX)) ||
3161 (mtd->writesize == 2048 &&
3162 maf_id == NAND_MFR_MICRON))
3163 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3164}
3165
3166/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003167 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003168 */
3169static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003170 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003171 int busw,
3172 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003173 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003174{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003175 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003176 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003179 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Karl Beldanef89a882008-09-15 14:37:29 +02003181 /*
3182 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003183 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003184 */
3185 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003188 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003191 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003192 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Brian Norris8b6e50c2011-05-25 14:59:01 -07003194 /*
3195 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003196 * interface concerns can cause random data which looks like a
3197 * possibly credible NAND flash to appear. If the two results do
3198 * not match, ignore the device completely.
3199 */
3200
3201 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3202
Brian Norris4aef9b72012-09-24 20:40:48 -07003203 /* Read entire ID string */
3204 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003205 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003206
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003207 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003208 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07003209 "%02x,%02x against %02x,%02x\n", __func__,
3210 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003211 return ERR_PTR(-ENODEV);
3212 }
3213
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003214 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003215 type = nand_flash_ids;
3216
3217 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003218 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02003219 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00003220
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003221 chip->onfi_version = 0;
3222 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003223 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003224 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003225 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003226 }
3227
David Woodhouse5e81e882010-02-26 18:32:56 +00003228 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003229 return ERR_PTR(-ENODEV);
3230
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003231 if (!mtd->name)
3232 mtd->name = type->name;
3233
Adrian Hunter69423d92008-12-10 13:37:21 +00003234 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003235
Huang Shijie12a40a52010-09-27 10:43:53 +08003236 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003237 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003238 busw = chip->init_size(mtd, chip, id_data);
3239 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003240 /* Decode parameters from extended ID */
3241 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003242 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003243 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003244 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003245 /* Get chip options */
3246 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003247
Brian Norris8b6e50c2011-05-25 14:59:01 -07003248 /*
3249 * Check if chip is not a Samsung device. Do not clear the
3250 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003251 */
3252 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3253 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3254ident_done:
3255
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003256 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003257 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003258 if (nand_manuf_ids[maf_idx].id == *maf_id)
3259 break;
3260 }
3261
3262 /*
3263 * Check, if buswidth is correct. Hardware drivers should set
Brian Norris8b6e50c2011-05-25 14:59:01 -07003264 * chip correct!
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003265 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003266 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003267 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003268 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3269 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003270 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003271 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3272 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003273 return ERR_PTR(-EINVAL);
3274 }
3275
Brian Norris7e74c2d2012-09-24 20:40:49 -07003276 nand_decode_bbm_options(mtd, chip, id_data);
3277
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003278 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003279 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003280 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003281 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003282
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003283 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003284 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003285 if (chip->chipsize & 0xffffffff)
3286 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003287 else {
3288 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3289 chip->chip_shift += 32 - 1;
3290 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003291
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003292 chip->badblockbits = 8;
3293
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003294 /* Check for AND chips with 4 page planes */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003295 if (chip->options & NAND_4PAGE_ARRAY)
3296 chip->erase_cmd = multi_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003297 else
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003298 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003299
Brian Norris8b6e50c2011-05-25 14:59:01 -07003300 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003301 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3302 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003303
Huang Shijie886bd332012-04-09 11:41:37 +08003304 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3305 " page size: %d, OOB size: %d\n",
3306 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3307 chip->onfi_version ? chip->onfi_params.model : type->name,
3308 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003309
3310 return type;
3311}
3312
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003313/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003314 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003315 * @mtd: MTD device structure
3316 * @maxchips: number of chips to scan for
3317 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003318 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003319 * This is the first phase of the normal nand_scan() function. It reads the
3320 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003321 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003322 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003323 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003324int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3325 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003326{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003327 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003328 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003329 struct nand_flash_dev *type;
3330
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003331 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003332 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003333 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003334 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003335
3336 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003337 type = nand_get_flash_type(mtd, chip, busw,
3338 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003339
3340 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003341 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003342 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003343 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003344 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
3346
Huang Shijie07300162012-11-09 16:23:45 +08003347 chip->select_chip(mtd, -1);
3348
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003349 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003350 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003351 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003352 /* See comment in nand_get_flash_type for reset */
3353 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003355 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003357 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003358 nand_dev_id != chip->read_byte(mtd)) {
3359 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 break;
Huang Shijie07300162012-11-09 16:23:45 +08003361 }
3362 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003365 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003368 chip->numchips = i;
3369 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
David Woodhouse3b85c322006-09-25 17:06:53 +01003371 return 0;
3372}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003373EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003374
3375
3376/**
3377 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003378 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003379 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003380 * This is the second phase of the normal nand_scan() function. It fills out
3381 * all the uninitialized function pointers with the defaults and scans for a
3382 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003383 */
3384int nand_scan_tail(struct mtd_info *mtd)
3385{
3386 int i;
3387 struct nand_chip *chip = mtd->priv;
3388
Brian Norrise2414f42012-02-06 13:44:00 -08003389 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3390 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3391 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3392
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003393 if (!(chip->options & NAND_OWN_BUFFERS))
3394 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3395 if (!chip->buffers)
3396 return -ENOMEM;
3397
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003398 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003399 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003400
3401 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003402 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003403 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003404 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003405 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003407 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 break;
3409 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003410 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 break;
3412 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003413 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003415 case 128:
3416 chip->ecc.layout = &nand_oob_128;
3417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003419 pr_warn("No oob scheme defined for oobsize %d\n",
3420 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 BUG();
3422 }
3423 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003424
David Woodhouse956e9442006-09-25 17:12:39 +01003425 if (!chip->write_page)
3426 chip->write_page = nand_write_page;
3427
Huang Shijie7db03ec2012-09-13 14:57:52 +08003428 /* set for ONFI nand */
3429 if (!chip->onfi_set_features)
3430 chip->onfi_set_features = nand_onfi_set_features;
3431 if (!chip->onfi_get_features)
3432 chip->onfi_get_features = nand_onfi_get_features;
3433
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003434 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003435 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003436 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003437 */
David Woodhouse956e9442006-09-25 17:12:39 +01003438
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003439 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003440 case NAND_ECC_HW_OOB_FIRST:
3441 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3442 if (!chip->ecc.calculate || !chip->ecc.correct ||
3443 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003444 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003445 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003446 BUG();
3447 }
3448 if (!chip->ecc.read_page)
3449 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3450
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003451 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003452 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003453 if (!chip->ecc.read_page)
3454 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003455 if (!chip->ecc.write_page)
3456 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003457 if (!chip->ecc.read_page_raw)
3458 chip->ecc.read_page_raw = nand_read_page_raw;
3459 if (!chip->ecc.write_page_raw)
3460 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003461 if (!chip->ecc.read_oob)
3462 chip->ecc.read_oob = nand_read_oob_std;
3463 if (!chip->ecc.write_oob)
3464 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003465
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003466 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003467 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3468 !chip->ecc.hwctl) &&
3469 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003470 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003471 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003472 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003473 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003474 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003475 BUG();
3476 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003477 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003478 if (!chip->ecc.read_page)
3479 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003480 if (!chip->ecc.write_page)
3481 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003482 if (!chip->ecc.read_page_raw)
3483 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3484 if (!chip->ecc.write_page_raw)
3485 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003486 if (!chip->ecc.read_oob)
3487 chip->ecc.read_oob = nand_read_oob_syndrome;
3488 if (!chip->ecc.write_oob)
3489 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003490
Mike Dunne2788c92012-04-25 12:06:10 -07003491 if (mtd->writesize >= chip->ecc.size) {
3492 if (!chip->ecc.strength) {
3493 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3494 BUG();
3495 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003496 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003497 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003498 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003499 "%d byte page size, fallback to SW ECC\n",
3500 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003501 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003503 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003504 chip->ecc.calculate = nand_calculate_ecc;
3505 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003506 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003507 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003508 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003509 chip->ecc.read_page_raw = nand_read_page_raw;
3510 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003511 chip->ecc.read_oob = nand_read_oob_std;
3512 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003513 if (!chip->ecc.size)
3514 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003515 chip->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003516 chip->ecc.strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003518
Ivan Djelic193bd402011-03-11 11:05:33 +01003519 case NAND_ECC_SOFT_BCH:
3520 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003521 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003522 BUG();
3523 }
3524 chip->ecc.calculate = nand_bch_calculate_ecc;
3525 chip->ecc.correct = nand_bch_correct_data;
3526 chip->ecc.read_page = nand_read_page_swecc;
3527 chip->ecc.read_subpage = nand_read_subpage;
3528 chip->ecc.write_page = nand_write_page_swecc;
3529 chip->ecc.read_page_raw = nand_read_page_raw;
3530 chip->ecc.write_page_raw = nand_write_page_raw;
3531 chip->ecc.read_oob = nand_read_oob_std;
3532 chip->ecc.write_oob = nand_write_oob_std;
3533 /*
3534 * Board driver should supply ecc.size and ecc.bytes values to
3535 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003536 * for details. Otherwise, default to 4 bits for large page
3537 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003538 */
3539 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3540 chip->ecc.size = 512;
3541 chip->ecc.bytes = 7;
3542 }
3543 chip->ecc.priv = nand_bch_init(mtd,
3544 chip->ecc.size,
3545 chip->ecc.bytes,
3546 &chip->ecc.layout);
3547 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003548 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003549 BUG();
3550 }
Mike Dunn6a918ba2012-03-11 14:21:11 -07003551 chip->ecc.strength =
Mike Dunne2788c92012-04-25 12:06:10 -07003552 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003553 break;
3554
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003555 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003556 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003557 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003558 chip->ecc.read_page = nand_read_page_raw;
3559 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003560 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003561 chip->ecc.read_page_raw = nand_read_page_raw;
3562 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003563 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003564 chip->ecc.size = mtd->writesize;
3565 chip->ecc.bytes = 0;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003566 chip->ecc.strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003570 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003571 BUG();
3572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Brian Norris9ce244b2011-08-30 18:45:37 -07003574 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003575 if (!chip->ecc.read_oob_raw)
3576 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003577 if (!chip->ecc.write_oob_raw)
3578 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3579
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003580 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003581 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003582 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003583 */
3584 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003585 for (i = 0; chip->ecc.layout->oobfree[i].length
3586 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003587 chip->ecc.layout->oobavail +=
3588 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003589 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003590
3591 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003592 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003593 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003594 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003595 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003596 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003597 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003598 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003600 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003601
Brian Norris8b6e50c2011-05-25 14:59:01 -07003602 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003603 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3604 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003605 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003606 case 2:
3607 mtd->subpage_sft = 1;
3608 break;
3609 case 4:
3610 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003611 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003612 mtd->subpage_sft = 2;
3613 break;
3614 }
3615 }
3616 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3617
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003618 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003619 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003622 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003624 /* Large page NAND with SOFT_ECC should support subpage reads */
3625 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3626 chip->options |= NAND_SUBPAGE_READ;
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* Fill in remaining MTD driver data */
3629 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003630 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3631 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003632 mtd->_erase = nand_erase;
3633 mtd->_point = NULL;
3634 mtd->_unpoint = NULL;
3635 mtd->_read = nand_read;
3636 mtd->_write = nand_write;
3637 mtd->_panic_write = panic_nand_write;
3638 mtd->_read_oob = nand_read_oob;
3639 mtd->_write_oob = nand_write_oob;
3640 mtd->_sync = nand_sync;
3641 mtd->_lock = NULL;
3642 mtd->_unlock = NULL;
3643 mtd->_suspend = nand_suspend;
3644 mtd->_resume = nand_resume;
3645 mtd->_block_isbad = nand_block_isbad;
3646 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003647 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Mike Dunn6a918ba2012-03-11 14:21:11 -07003649 /* propagate ecc info to mtd_info */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003650 mtd->ecclayout = chip->ecc.layout;
Mike Dunn86c20722012-04-25 12:06:05 -07003651 mtd->ecc_strength = chip->ecc.strength;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003652 /*
3653 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3654 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3655 * properly set.
3656 */
3657 if (!mtd->bitflip_threshold)
3658 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003660 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003661 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003665 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003667EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Brian Norris8b6e50c2011-05-25 14:59:01 -07003669/*
3670 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003671 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003672 * to call us from in-kernel code if the core NAND support is modular.
3673 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003674#ifdef MODULE
3675#define caller_is_module() (1)
3676#else
3677#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003678 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003679#endif
3680
3681/**
3682 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003683 * @mtd: MTD device structure
3684 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003685 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003686 * This fills out all the uninitialized function pointers with the defaults.
3687 * The flash ID is read and the mtd/chip structures are filled with the
3688 * appropriate values. The mtd->owner field must be set to the module of the
3689 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003690 */
3691int nand_scan(struct mtd_info *mtd, int maxchips)
3692{
3693 int ret;
3694
3695 /* Many callers got this wrong, so check for it for a while... */
3696 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003697 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003698 BUG();
3699 }
3700
David Woodhouse5e81e882010-02-26 18:32:56 +00003701 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003702 if (!ret)
3703 ret = nand_scan_tail(mtd);
3704 return ret;
3705}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003706EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003709 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003710 * @mtd: MTD device structure
3711 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003712void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003714 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Ivan Djelic193bd402011-03-11 11:05:33 +01003716 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3717 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3718
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003719 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Jesper Juhlfa671642005-11-07 01:01:27 -08003721 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003722 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003723 if (!(chip->options & NAND_OWN_BUFFERS))
3724 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003725
3726 /* Free bad block descriptor memory */
3727 if (chip->badblock_pattern && chip->badblock_pattern->options
3728 & NAND_BBT_DYNAMICSTRUCT)
3729 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
David Woodhousee0c7d762006-05-13 18:07:53 +01003731EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003732
3733static int __init nand_base_init(void)
3734{
3735 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3736 return 0;
3737}
3738
3739static void __exit nand_base_exit(void)
3740{
3741 led_trigger_unregister_simple(nand_led_trigger);
3742}
3743
3744module_init(nand_base_init);
3745module_exit(nand_base_exit);
3746
David Woodhousee0c7d762006-05-13 18:07:53 +01003747MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003748MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3749MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003750MODULE_DESCRIPTION("Generic NAND flash driver code");