blob: ae9790b659b1241cc3fe5de4040e3e2cd2c69055 [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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02009 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020014 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * David Woodhouse for adding multichip support
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
18 * rework for 2K page size chips
19 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020020 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Enable cached programming for 2k page size chips
22 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070023 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030024 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License version 2 as
28 * published by the Free Software Foundation.
29 *
30 */
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/types.h>
39#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010042#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/interrupt.h>
44#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080045#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020050static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .eccbytes = 3,
52 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020053 .oobfree = {
54 {.offset = 3,
55 .length = 2},
56 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020057 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020060static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .eccbytes = 6,
62 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020063 .oobfree = {
64 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020065 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020068static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .eccbytes = 24,
70 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010071 40, 41, 42, 43, 44, 45, 46, 47,
72 48, 49, 50, 51, 52, 53, 54, 55,
73 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020074 .oobfree = {
75 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020076 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Thomas Gleixner81ec5362007-12-12 17:27:03 +010079static struct nand_ecclayout nand_oob_128 = {
80 .eccbytes = 48,
81 .eccpos = {
82 80, 81, 82, 83, 84, 85, 86, 87,
83 88, 89, 90, 91, 92, 93, 94, 95,
84 96, 97, 98, 99, 100, 101, 102, 103,
85 104, 105, 106, 107, 108, 109, 110, 111,
86 112, 113, 114, 115, 116, 117, 118, 119,
87 120, 121, 122, 123, 124, 125, 126, 127},
88 .oobfree = {
89 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020090 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010091};
92
Huang Shijie6a8214a2012-11-19 14:43:30 +080093static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020095static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
96 struct mtd_oob_ops *ops);
97
Thomas Gleixnerd470a972006-05-23 23:48:57 +020098/*
Joe Perches8e87d782008-02-03 17:22:34 +020099 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200100 * compiled away when LED support is disabled.
101 */
102DEFINE_LED_TRIGGER(nand_led_trigger);
103
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530104static int check_offs_len(struct mtd_info *mtd,
105 loff_t ofs, uint64_t len)
106{
107 struct nand_chip *chip = mtd->priv;
108 int ret = 0;
109
110 /* Start address must align on block boundary */
111 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700112 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530113 ret = -EINVAL;
114 }
115
116 /* Length must align on block boundary */
117 if (len & ((1 << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700118 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530119 ret = -EINVAL;
120 }
121
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530122 return ret;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/**
126 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700127 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000128 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800129 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100131static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200133 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200135 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200136 spin_lock(&chip->controller->lock);
137 chip->controller->active = NULL;
138 chip->state = FL_READY;
139 wake_up(&chip->controller->wq);
140 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/**
144 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700145 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700147 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200149static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200151 struct nand_chip *chip = mtd->priv;
152 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/**
Masanari Iida064a7692012-11-09 23:20:58 +0900156 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700157 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700158 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700160 * Default read function for 16bit buswidth with endianness conversion.
161 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200163static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200165 struct nand_chip *chip = mtd->priv;
166 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700171 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700173 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175static u16 nand_read_word(struct mtd_info *mtd)
176{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200177 struct nand_chip *chip = mtd->priv;
178 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700183 * @mtd: MTD device structure
184 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
186 * Default select function for 1 chip devices.
187 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200188static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190 struct nand_chip *chip = mtd->priv;
191
192 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200194 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198
199 default:
200 BUG();
201 }
202}
203
204/**
205 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700206 * @mtd: MTD device structure
207 * @buf: data buffer
208 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700210 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200212static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200215 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
David Woodhousee0c7d762006-05-13 18:07:53 +0100217 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200218 writeb(buf[i], chip->IO_ADDR_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000222 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700223 * @mtd: MTD device structure
224 * @buf: buffer to store date
225 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700227 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200229static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200232 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
David Woodhousee0c7d762006-05-13 18:07:53 +0100234 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200235 buf[i] = readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700240 * @mtd: MTD device structure
241 * @buf: data buffer
242 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700244 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200246static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200249 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u16 *p = (u16 *) buf;
251 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000252
David Woodhousee0c7d762006-05-13 18:07:53 +0100253 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200254 writew(p[i], chip->IO_ADDR_W);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000259 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700260 * @mtd: MTD device structure
261 * @buf: buffer to store date
262 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700264 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200266static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 int i;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200269 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 u16 *p = (u16 *) buf;
271 len >>= 1;
272
David Woodhousee0c7d762006-05-13 18:07:53 +0100273 for (i = 0; i < len; i++)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200274 p[i] = readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700279 * @mtd: MTD device structure
280 * @ofs: offset from device start
281 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000283 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
286{
Brian Norriscdbec052012-01-13 18:11:48 -0800287 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200288 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 u16 bad;
290
Brian Norris5fb15492011-05-31 16:31:21 -0700291 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700292 ofs += mtd->erasesize - mtd->writesize;
293
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100294 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200297 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Huang Shijie6a8214a2012-11-19 14:43:30 +0800299 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200302 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Brian Norriscdbec052012-01-13 18:11:48 -0800305 do {
306 if (chip->options & NAND_BUSWIDTH_16) {
307 chip->cmdfunc(mtd, NAND_CMD_READOOB,
308 chip->badblockpos & 0xFE, page);
309 bad = cpu_to_le16(chip->read_word(mtd));
310 if (chip->badblockpos & 0x1)
311 bad >>= 8;
312 else
313 bad &= 0xFF;
314 } else {
315 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
316 page);
317 bad = chip->read_byte(mtd);
318 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000319
Brian Norriscdbec052012-01-13 18:11:48 -0800320 if (likely(chip->badblockbits == 8))
321 res = bad != 0xFF;
322 else
323 res = hweight8(bad) < chip->badblockbits;
324 ofs += mtd->writesize;
325 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
326 i++;
327 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200328
Huang Shijieb0bb6902012-11-19 14:43:29 +0800329 if (getchip) {
330 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800332 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return res;
335}
336
337/**
338 * nand_default_block_markbad - [DEFAULT] mark a block bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700339 * @mtd: MTD device structure
340 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700342 * This is the default implementation, which can be overridden by a hardware
Brian Norrise2414f42012-02-06 13:44:00 -0800343 * specific driver. We try operations in the following order, according to our
344 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
345 * (1) erase the affected block, to allow OOB marker to be written cleanly
346 * (2) update in-memory BBT
347 * (3) write bad block marker to OOB area of affected block
348 * (4) update flash-based BBT
349 * Note that we retain the first error encountered in (3) or (4), finish the
350 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351*/
352static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
353{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200354 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200355 uint8_t buf[2] = { 0, 0 };
Brian Norrise2414f42012-02-06 13:44:00 -0800356 int block, res, ret = 0, i = 0;
357 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000358
Brian Norrise2414f42012-02-06 13:44:00 -0800359 if (write_oob) {
Brian Norris00918422012-01-13 18:11:47 -0800360 struct erase_info einfo;
361
362 /* Attempt erase before marking OOB */
363 memset(&einfo, 0, sizeof(einfo));
364 einfo.mtd = mtd;
365 einfo.addr = ofs;
366 einfo.len = 1 << chip->phys_erase_shift;
367 nand_erase_nand(mtd, &einfo, 0);
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400371 block = (int)(ofs >> chip->bbt_erase_shift);
Brian Norrise2414f42012-02-06 13:44:00 -0800372 /* Mark block bad in memory-based BBT */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200373 if (chip->bbt)
374 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Brian Norrise2414f42012-02-06 13:44:00 -0800376 /* Write bad block marker to OOB */
377 if (write_oob) {
Brian Norris4a89ff82011-08-30 18:45:45 -0700378 struct mtd_oob_ops ops;
Brian Norrisdf698622012-01-20 20:38:03 -0800379 loff_t wr_ofs = ofs;
Brian Norris4a89ff82011-08-30 18:45:45 -0700380
Huang Shijie6a8214a2012-11-19 14:43:30 +0800381 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000382
Brian Norris4a89ff82011-08-30 18:45:45 -0700383 ops.datbuf = NULL;
384 ops.oobbuf = buf;
Brian Norris85443312012-01-13 18:11:49 -0800385 ops.ooboffs = chip->badblockpos;
386 if (chip->options & NAND_BUSWIDTH_16) {
387 ops.ooboffs &= ~0x01;
388 ops.len = ops.ooblen = 2;
389 } else {
390 ops.len = ops.ooblen = 1;
391 }
Brian Norris23b1a992011-10-14 20:09:33 -0700392 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norrisdf698622012-01-20 20:38:03 -0800393
Brian Norrise2414f42012-02-06 13:44:00 -0800394 /* Write to first/last page(s) if necessary */
Brian Norrisdf698622012-01-20 20:38:03 -0800395 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
396 wr_ofs += mtd->erasesize - mtd->writesize;
Brian Norris02ed70b2010-07-21 16:53:47 -0700397 do {
Brian Norrise2414f42012-02-06 13:44:00 -0800398 res = nand_do_write_oob(mtd, wr_ofs, &ops);
399 if (!ret)
400 ret = res;
Brian Norris02ed70b2010-07-21 16:53:47 -0700401
Brian Norris02ed70b2010-07-21 16:53:47 -0700402 i++;
Brian Norrisdf698622012-01-20 20:38:03 -0800403 wr_ofs += mtd->writesize;
Brian Norrise2414f42012-02-06 13:44:00 -0800404 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Brian Norris02ed70b2010-07-21 16:53:47 -0700405
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300406 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200407 }
Brian Norrise2414f42012-02-06 13:44:00 -0800408
409 /* Update flash-based bad block table */
410 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
411 res = nand_update_bbt(mtd, ofs);
412 if (!ret)
413 ret = res;
414 }
415
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200416 if (!ret)
417 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300418
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000422/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700424 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700426 * Check, if the device is write protected. The function expects, that the
427 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100429static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200431 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200432
Brian Norris8b6e50c2011-05-25 14:59:01 -0700433 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200434 if (chip->options & NAND_BROKEN_XD)
435 return 0;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200438 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
439 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/**
443 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700444 * @mtd: MTD device structure
445 * @ofs: offset from device start
446 * @getchip: 0, if the chip is already selected
447 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 *
449 * Check, if the block is bad. Either by reading the bad block table or
450 * calling of the scan function.
451 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200452static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
453 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200455 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000456
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200457 if (!chip->bbt)
458 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100461 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200464/**
465 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700466 * @mtd: MTD device structure
467 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200468 *
469 * Helper function for nand_wait_ready used when needing to wait in interrupt
470 * context.
471 */
472static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
473{
474 struct nand_chip *chip = mtd->priv;
475 int i;
476
477 /* Wait for the device to get ready */
478 for (i = 0; i < timeo; i++) {
479 if (chip->dev_ready(mtd))
480 break;
481 touch_softlockup_watchdog();
482 mdelay(1);
483 }
484}
485
Brian Norris7854d3f2011-06-23 14:12:08 -0700486/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100487void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000488{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200489 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100490 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000491
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200492 /* 400ms timeout */
493 if (in_interrupt() || oops_in_progress)
494 return panic_nand_wait_ready(mtd, 400);
495
Richard Purdie8fe833c2006-03-31 02:31:14 -0800496 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700497 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000498 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200499 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800500 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700501 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000502 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800503 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000504}
David Woodhouse4b648b02006-09-25 17:05:24 +0100505EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/**
508 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700509 * @mtd: MTD device structure
510 * @command: the command to be sent
511 * @column: the column address for this command, -1 if none
512 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700514 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200515 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200517static void nand_command(struct mtd_info *mtd, unsigned int command,
518 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200520 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200521 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Brian Norris8b6e50c2011-05-25 14:59:01 -0700523 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (command == NAND_CMD_SEQIN) {
525 int readcmd;
526
Joern Engel28318772006-05-22 23:18:05 +0200527 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200529 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 readcmd = NAND_CMD_READOOB;
531 } else if (column < 256) {
532 /* First 256 bytes --> READ0 */
533 readcmd = NAND_CMD_READ0;
534 } else {
535 column -= 256;
536 readcmd = NAND_CMD_READ1;
537 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200538 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200539 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200541 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Brian Norris8b6e50c2011-05-25 14:59:01 -0700543 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200544 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
545 /* Serially input address */
546 if (column != -1) {
547 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200548 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200549 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200550 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200551 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200553 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200554 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200555 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200556 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200557 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200558 if (chip->chipsize > (32 << 20))
559 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200560 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200561 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000562
563 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700564 * Program and erase have their own busy handlers status and sequential
565 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 case NAND_CMD_PAGEPROG:
570 case NAND_CMD_ERASE1:
571 case NAND_CMD_ERASE2:
572 case NAND_CMD_SEQIN:
573 case NAND_CMD_STATUS:
574 return;
575
576 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200577 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200579 udelay(chip->chip_delay);
580 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200581 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200582 chip->cmd_ctrl(mtd,
583 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200584 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
585 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return;
587
David Woodhousee0c7d762006-05-13 18:07:53 +0100588 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * If we don't have access to the busy pin, we apply the given
592 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100593 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200594 if (!chip->dev_ready) {
595 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700599 /*
600 * Apply this short delay always to ensure that we do wait tWB in
601 * any case on any machine.
602 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100603 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000604
605 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/**
609 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700610 * @mtd: MTD device structure
611 * @command: the command to be sent
612 * @column: the column address for this command, -1 if none
613 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200615 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700616 * devices. We don't have the separate regions as we have in the small page
617 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
620 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200622 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Emulate NAND_CMD_READOOB */
625 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200626 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 command = NAND_CMD_READ0;
628 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000629
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200630 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400631 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200634 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 /* Serially input address */
637 if (column != -1) {
638 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200642 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200643 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200646 chip->cmd_ctrl(mtd, page_addr, ctrl);
647 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200648 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200650 if (chip->chipsize > (128 << 20))
651 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200652 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200655 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000656
657 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700658 * Program and erase have their own busy handlers status, sequential
659 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 case NAND_CMD_CACHEDPROG:
664 case NAND_CMD_PAGEPROG:
665 case NAND_CMD_ERASE1:
666 case NAND_CMD_ERASE2:
667 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200668 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000670 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200673 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200675 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200676 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
677 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
678 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
679 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200680 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
681 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return;
683
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200684 case NAND_CMD_RNDOUT:
685 /* No ready / busy check necessary */
686 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
687 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
688 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
689 NAND_NCE | NAND_CTRL_CHANGE);
690 return;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200693 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
694 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
695 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
696 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000697
David Woodhousee0c7d762006-05-13 18:07:53 +0100698 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000700 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700702 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100703 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200704 if (!chip->dev_ready) {
705 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000709
Brian Norris8b6e50c2011-05-25 14:59:01 -0700710 /*
711 * Apply this short delay always to ensure that we do wait tWB in
712 * any case on any machine.
713 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100714 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000715
716 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200720 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700721 * @chip: the nand chip descriptor
722 * @mtd: MTD device structure
723 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200724 *
725 * Used when in panic, no locks are taken.
726 */
727static void panic_nand_get_device(struct nand_chip *chip,
728 struct mtd_info *mtd, int new_state)
729{
Brian Norris7854d3f2011-06-23 14:12:08 -0700730 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200731 chip->controller->active = chip;
732 chip->state = new_state;
733}
734
735/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700737 * @mtd: MTD device structure
738 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 *
740 * Get the device and lock it for exclusive access
741 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200742static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800743nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800745 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200746 spinlock_t *lock = &chip->controller->lock;
747 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100748 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200749retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100750 spin_lock(lock);
751
vimal singhb8b3ee92009-07-09 20:41:22 +0530752 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200753 if (!chip->controller->active)
754 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200755
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200756 if (chip->controller->active == chip && chip->state == FL_READY) {
757 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100758 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100759 return 0;
760 }
761 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800762 if (chip->controller->active->state == FL_PM_SUSPENDED) {
763 chip->state = FL_PM_SUSPENDED;
764 spin_unlock(lock);
765 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800766 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100767 }
768 set_current_state(TASK_UNINTERRUPTIBLE);
769 add_wait_queue(wq, &wait);
770 spin_unlock(lock);
771 schedule();
772 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto retry;
774}
775
776/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700777 * panic_nand_wait - [GENERIC] wait until the command is done
778 * @mtd: MTD device structure
779 * @chip: NAND chip structure
780 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200781 *
782 * Wait for command done. This is a helper function for nand_wait used when
783 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400784 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200785 */
786static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
787 unsigned long timeo)
788{
789 int i;
790 for (i = 0; i < timeo; i++) {
791 if (chip->dev_ready) {
792 if (chip->dev_ready(mtd))
793 break;
794 } else {
795 if (chip->read_byte(mtd) & NAND_STATUS_READY)
796 break;
797 }
798 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200799 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200800}
801
802/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700803 * nand_wait - [DEFAULT] wait until the command is done
804 * @mtd: MTD device structure
805 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700807 * Wait for command done. This applies to erase and program only. Erase can
808 * take up to 400ms and program up to 20ms according to general NAND and
809 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700810 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200811static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200814 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800815 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Richard Purdie8fe833c2006-03-31 02:31:14 -0800817 led_trigger_event(nand_led_trigger, LED_FULL);
818
Brian Norris8b6e50c2011-05-25 14:59:01 -0700819 /*
820 * Apply this short delay always to ensure that we do wait tWB in any
821 * case on any machine.
822 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100823 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200825 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200827 if (in_interrupt() || oops_in_progress)
828 panic_nand_wait(mtd, chip, timeo);
829 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800830 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200831 while (time_before(jiffies, timeo)) {
832 if (chip->dev_ready) {
833 if (chip->dev_ready(mtd))
834 break;
835 } else {
836 if (chip->read_byte(mtd) & NAND_STATUS_READY)
837 break;
838 }
839 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800842 led_trigger_event(nand_led_trigger, LED_OFF);
843
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200844 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100845 /* This can happen if in case of timeout or buggy dev_ready */
846 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return status;
848}
849
850/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700851 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700852 * @mtd: mtd info
853 * @ofs: offset to start unlock from
854 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700855 * @invert: when = 0, unlock the range of blocks within the lower and
856 * upper boundary address
857 * when = 1, unlock the range of blocks outside the boundaries
858 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530859 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530861 */
862static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
863 uint64_t len, int invert)
864{
865 int ret = 0;
866 int status, page;
867 struct nand_chip *chip = mtd->priv;
868
869 /* Submit address of first page to unlock */
870 page = ofs >> chip->page_shift;
871 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
872
873 /* Submit address of last page to unlock */
874 page = (ofs + len) >> chip->page_shift;
875 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
876 (page | invert) & chip->pagemask);
877
878 /* Call wait ready function */
879 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530880 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400881 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700882 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530883 __func__, status);
884 ret = -EIO;
885 }
886
887 return ret;
888}
889
890/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700891 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700892 * @mtd: mtd info
893 * @ofs: offset to start unlock from
894 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530895 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700896 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530897 */
898int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
899{
900 int ret = 0;
901 int chipnr;
902 struct nand_chip *chip = mtd->priv;
903
Brian Norris289c0522011-07-19 10:06:09 -0700904 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530905 __func__, (unsigned long long)ofs, len);
906
907 if (check_offs_len(mtd, ofs, len))
908 ret = -EINVAL;
909
910 /* Align to last block address if size addresses end of the device */
911 if (ofs + len == mtd->size)
912 len -= mtd->erasesize;
913
Huang Shijie6a8214a2012-11-19 14:43:30 +0800914 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530915
916 /* Shift to get chip number */
917 chipnr = ofs >> chip->chip_shift;
918
919 chip->select_chip(mtd, chipnr);
920
921 /* Check, if it is write protected */
922 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700923 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530924 __func__);
925 ret = -EIO;
926 goto out;
927 }
928
929 ret = __nand_unlock(mtd, ofs, len, 0);
930
931out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800932 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530933 nand_release_device(mtd);
934
935 return ret;
936}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200937EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530938
939/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700940 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700941 * @mtd: mtd info
942 * @ofs: offset to start unlock from
943 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530944 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700945 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
946 * have this feature, but it allows only to lock all blocks, not for specified
947 * range for block. Implementing 'lock' feature by making use of 'unlock', for
948 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530949 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700950 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530951 */
952int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
953{
954 int ret = 0;
955 int chipnr, status, page;
956 struct nand_chip *chip = mtd->priv;
957
Brian Norris289c0522011-07-19 10:06:09 -0700958 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530959 __func__, (unsigned long long)ofs, len);
960
961 if (check_offs_len(mtd, ofs, len))
962 ret = -EINVAL;
963
Huang Shijie6a8214a2012-11-19 14:43:30 +0800964 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530965
966 /* Shift to get chip number */
967 chipnr = ofs >> chip->chip_shift;
968
969 chip->select_chip(mtd, chipnr);
970
971 /* Check, if it is write protected */
972 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700973 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530974 __func__);
975 status = MTD_ERASE_FAILED;
976 ret = -EIO;
977 goto out;
978 }
979
980 /* Submit address of first page to lock */
981 page = ofs >> chip->page_shift;
982 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
983
984 /* Call wait ready function */
985 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530986 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400987 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700988 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530989 __func__, status);
990 ret = -EIO;
991 goto out;
992 }
993
994 ret = __nand_unlock(mtd, ofs, len, 0x1);
995
996out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800997 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530998 nand_release_device(mtd);
999
1000 return ret;
1001}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001002EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301003
1004/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001005 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001006 * @mtd: mtd info structure
1007 * @chip: nand chip info structure
1008 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001009 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001010 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001011 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001012 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001013 */
1014static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001015 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001016{
1017 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001018 if (oob_required)
1019 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001020 return 0;
1021}
1022
1023/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001024 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001025 * @mtd: mtd info structure
1026 * @chip: nand chip info structure
1027 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001028 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001029 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001030 *
1031 * We need a special oob layout and handling even when OOB isn't used.
1032 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001033static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001034 struct nand_chip *chip, uint8_t *buf,
1035 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001036{
1037 int eccsize = chip->ecc.size;
1038 int eccbytes = chip->ecc.bytes;
1039 uint8_t *oob = chip->oob_poi;
1040 int steps, size;
1041
1042 for (steps = chip->ecc.steps; steps > 0; steps--) {
1043 chip->read_buf(mtd, buf, eccsize);
1044 buf += eccsize;
1045
1046 if (chip->ecc.prepad) {
1047 chip->read_buf(mtd, oob, chip->ecc.prepad);
1048 oob += chip->ecc.prepad;
1049 }
1050
1051 chip->read_buf(mtd, oob, eccbytes);
1052 oob += eccbytes;
1053
1054 if (chip->ecc.postpad) {
1055 chip->read_buf(mtd, oob, chip->ecc.postpad);
1056 oob += chip->ecc.postpad;
1057 }
1058 }
1059
1060 size = mtd->oobsize - (oob - chip->oob_poi);
1061 if (size)
1062 chip->read_buf(mtd, oob, size);
1063
1064 return 0;
1065}
1066
1067/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001068 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001069 * @mtd: mtd info structure
1070 * @chip: nand chip info structure
1071 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001072 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001073 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001074 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001075static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001076 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001078 int i, eccsize = chip->ecc.size;
1079 int eccbytes = chip->ecc.bytes;
1080 int eccsteps = chip->ecc.steps;
1081 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001082 uint8_t *ecc_calc = chip->buffers->ecccalc;
1083 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001084 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001085 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001086
Brian Norris1fbb9382012-05-02 10:14:55 -07001087 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001088
1089 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1090 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1091
1092 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001093 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001094
1095 eccsteps = chip->ecc.steps;
1096 p = buf;
1097
1098 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1099 int stat;
1100
1101 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001102 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001103 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001104 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001105 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001106 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1107 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001108 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001109 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001113 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001114 * @mtd: mtd info structure
1115 * @chip: nand chip info structure
1116 * @data_offs: offset of requested data within the page
1117 * @readlen: data length
1118 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001119 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001120static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1121 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001122{
1123 int start_step, end_step, num_steps;
1124 uint32_t *eccpos = chip->ecc.layout->eccpos;
1125 uint8_t *p;
1126 int data_col_addr, i, gaps = 0;
1127 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1128 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001129 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001130 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001131
Brian Norris7854d3f2011-06-23 14:12:08 -07001132 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001133 start_step = data_offs / chip->ecc.size;
1134 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1135 num_steps = end_step - start_step + 1;
1136
Brian Norris8b6e50c2011-05-25 14:59:01 -07001137 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001138 datafrag_len = num_steps * chip->ecc.size;
1139 eccfrag_len = num_steps * chip->ecc.bytes;
1140
1141 data_col_addr = start_step * chip->ecc.size;
1142 /* If we read not a page aligned data */
1143 if (data_col_addr != 0)
1144 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1145
1146 p = bufpoi + data_col_addr;
1147 chip->read_buf(mtd, p, datafrag_len);
1148
Brian Norris8b6e50c2011-05-25 14:59:01 -07001149 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001150 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1151 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1152
Brian Norris8b6e50c2011-05-25 14:59:01 -07001153 /*
1154 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001155 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001156 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001157 for (i = 0; i < eccfrag_len - 1; i++) {
1158 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1159 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1160 gaps = 1;
1161 break;
1162 }
1163 }
1164 if (gaps) {
1165 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1166 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1167 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001168 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001169 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001170 * about buswidth alignment in read_buf.
1171 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001172 index = start_step * chip->ecc.bytes;
1173
1174 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001175 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001176 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001177 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001178 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001179 aligned_len++;
1180
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001181 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1182 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001183 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1184 }
1185
1186 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001187 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001188
1189 p = bufpoi + data_col_addr;
1190 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1191 int stat;
1192
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001193 stat = chip->ecc.correct(mtd, p,
1194 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001195 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001196 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001197 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001198 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001199 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1200 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001201 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001202 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001203}
1204
1205/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001206 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001207 * @mtd: mtd info structure
1208 * @chip: nand chip info structure
1209 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001210 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001211 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001212 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001213 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001214 */
1215static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001216 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001217{
1218 int i, eccsize = chip->ecc.size;
1219 int eccbytes = chip->ecc.bytes;
1220 int eccsteps = chip->ecc.steps;
1221 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001222 uint8_t *ecc_calc = chip->buffers->ecccalc;
1223 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001224 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001225 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001226
1227 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1228 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1229 chip->read_buf(mtd, p, eccsize);
1230 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1231 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001232 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001233
1234 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001235 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001236
1237 eccsteps = chip->ecc.steps;
1238 p = buf;
1239
1240 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1241 int stat;
1242
1243 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001244 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001245 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001246 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001247 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001248 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1249 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001250 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001251 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001252}
1253
1254/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001255 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001256 * @mtd: mtd info structure
1257 * @chip: nand chip info structure
1258 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001259 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001260 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001261 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001262 * Hardware ECC for large page chips, require OOB to be read first. For this
1263 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1264 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1265 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1266 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001267 */
1268static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001269 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001270{
1271 int i, eccsize = chip->ecc.size;
1272 int eccbytes = chip->ecc.bytes;
1273 int eccsteps = chip->ecc.steps;
1274 uint8_t *p = buf;
1275 uint8_t *ecc_code = chip->buffers->ecccode;
1276 uint32_t *eccpos = chip->ecc.layout->eccpos;
1277 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001278 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001279
1280 /* Read the OOB area first */
1281 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1282 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1283 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1284
1285 for (i = 0; i < chip->ecc.total; i++)
1286 ecc_code[i] = chip->oob_poi[eccpos[i]];
1287
1288 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1289 int stat;
1290
1291 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1292 chip->read_buf(mtd, p, eccsize);
1293 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1294
1295 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001296 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001297 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001298 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001299 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001300 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1301 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001302 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001303 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001304}
1305
1306/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001307 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001308 * @mtd: mtd info structure
1309 * @chip: nand chip info structure
1310 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001311 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001312 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001314 * The hw generator calculates the error syndrome automatically. Therefore we
1315 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001316 */
1317static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001318 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001319{
1320 int i, eccsize = chip->ecc.size;
1321 int eccbytes = chip->ecc.bytes;
1322 int eccsteps = chip->ecc.steps;
1323 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001324 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001325 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001326
1327 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1328 int stat;
1329
1330 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1331 chip->read_buf(mtd, p, eccsize);
1332
1333 if (chip->ecc.prepad) {
1334 chip->read_buf(mtd, oob, chip->ecc.prepad);
1335 oob += chip->ecc.prepad;
1336 }
1337
1338 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1339 chip->read_buf(mtd, oob, eccbytes);
1340 stat = chip->ecc.correct(mtd, p, oob, NULL);
1341
Mike Dunn3f91e942012-04-25 12:06:09 -07001342 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001343 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001344 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001345 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001346 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1347 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001348
1349 oob += eccbytes;
1350
1351 if (chip->ecc.postpad) {
1352 chip->read_buf(mtd, oob, chip->ecc.postpad);
1353 oob += chip->ecc.postpad;
1354 }
1355 }
1356
1357 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001358 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001359 if (i)
1360 chip->read_buf(mtd, oob, i);
1361
Mike Dunn3f91e942012-04-25 12:06:09 -07001362 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001363}
1364
1365/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001366 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001367 * @chip: nand chip structure
1368 * @oob: oob destination address
1369 * @ops: oob ops structure
1370 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001371 */
1372static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001373 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001374{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001375 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001376
Brian Norris0612b9d2011-08-30 18:45:40 -07001377 case MTD_OPS_PLACE_OOB:
1378 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001379 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1380 return oob + len;
1381
Brian Norris0612b9d2011-08-30 18:45:40 -07001382 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001383 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001384 uint32_t boffs = 0, roffs = ops->ooboffs;
1385 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001386
Florian Fainellif8ac0412010-09-07 13:23:43 +02001387 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001388 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001389 if (unlikely(roffs)) {
1390 if (roffs >= free->length) {
1391 roffs -= free->length;
1392 continue;
1393 }
1394 boffs = free->offset + roffs;
1395 bytes = min_t(size_t, len,
1396 (free->length - roffs));
1397 roffs = 0;
1398 } else {
1399 bytes = min_t(size_t, len, free->length);
1400 boffs = free->offset;
1401 }
1402 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001403 oob += bytes;
1404 }
1405 return oob;
1406 }
1407 default:
1408 BUG();
1409 }
1410 return NULL;
1411}
1412
1413/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001414 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001415 * @mtd: MTD device structure
1416 * @from: offset to read from
1417 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001418 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001419 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001420 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001421static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1422 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001423{
Brian Norrise47f3db2012-05-02 10:14:56 -07001424 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001425 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001426 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001427 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001428 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001429 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001430 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001431 mtd->oobavail : mtd->oobsize;
1432
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001433 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001434 unsigned int max_bitflips = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001436 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001438 chipnr = (int)(from >> chip->chip_shift);
1439 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001441 realpage = (int)(from >> chip->page_shift);
1442 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001446 buf = ops->datbuf;
1447 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001448 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001449
Florian Fainellif8ac0412010-09-07 13:23:43 +02001450 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001451 bytes = min(mtd->writesize - col, readlen);
1452 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001453
Brian Norris8b6e50c2011-05-25 14:59:01 -07001454 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001455 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001456 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Brian Norrisc00a0992012-05-01 17:12:54 -07001458 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Mike Dunnedbc45402012-04-25 12:06:11 -07001460 /*
1461 * Now read the page into the buffer. Absent an error,
1462 * the read methods return max bitflips per ecc step.
1463 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001464 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001465 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001466 oob_required,
1467 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001468 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1469 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001470 ret = chip->ecc.read_subpage(mtd, chip,
1471 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001472 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001473 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001474 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001475 if (ret < 0) {
1476 if (!aligned)
1477 /* Invalidate page cache */
1478 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001479 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001480 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001481
Mike Dunnedbc45402012-04-25 12:06:11 -07001482 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1483
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001484 /* Transfer not aligned data */
1485 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001486 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001487 !(mtd->ecc_stats.failed - stats.failed) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001488 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001489 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001490 chip->pagebuf_bitflips = ret;
1491 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001492 /* Invalidate page cache */
1493 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001494 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001495 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001497
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001498 buf += bytes;
1499
1500 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001501 int toread = min(oobreadlen, max_oobsize);
1502
1503 if (toread) {
1504 oob = nand_transfer_oob(chip,
1505 oob, ops, toread);
1506 oobreadlen -= toread;
1507 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001508 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001509
1510 if (chip->options & NAND_NEED_READRDY) {
1511 /* Apply delay or wait for ready/busy pin */
1512 if (!chip->dev_ready)
1513 udelay(chip->chip_delay);
1514 else
1515 nand_wait_ready(mtd);
1516 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001517 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001518 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001519 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001520 max_bitflips = max_t(unsigned int, max_bitflips,
1521 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001524 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001525
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001526 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Brian Norris8b6e50c2011-05-25 14:59:01 -07001529 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 col = 0;
1531 /* Increment page address */
1532 realpage++;
1533
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001534 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /* Check, if we cross a chip boundary */
1536 if (!page) {
1537 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001538 chip->select_chip(mtd, -1);
1539 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001542 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001544 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001545 if (oob)
1546 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Mike Dunn3f91e942012-04-25 12:06:09 -07001548 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001549 return ret;
1550
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001551 if (mtd->ecc_stats.failed - stats.failed)
1552 return -EBADMSG;
1553
Mike Dunnedbc45402012-04-25 12:06:11 -07001554 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001555}
1556
1557/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001558 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001559 * @mtd: MTD device structure
1560 * @from: offset to read from
1561 * @len: number of bytes to read
1562 * @retlen: pointer to variable to store the number of read bytes
1563 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001564 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001565 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001566 */
1567static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1568 size_t *retlen, uint8_t *buf)
1569{
Brian Norris4a89ff82011-08-30 18:45:45 -07001570 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001571 int ret;
1572
Huang Shijie6a8214a2012-11-19 14:43:30 +08001573 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001574 ops.len = len;
1575 ops.datbuf = buf;
1576 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001577 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001578 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001579 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001580 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001585 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001586 * @mtd: mtd info structure
1587 * @chip: nand chip info structure
1588 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001589 */
1590static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001591 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001592{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001593 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001594 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001595 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001596}
1597
1598/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001599 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001600 * with syndromes
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_syndrome(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{
1608 uint8_t *buf = chip->oob_poi;
1609 int length = mtd->oobsize;
1610 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1611 int eccsize = chip->ecc.size;
1612 uint8_t *bufpoi = buf;
1613 int i, toread, sndrnd = 0, pos;
1614
1615 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1616 for (i = 0; i < chip->ecc.steps; i++) {
1617 if (sndrnd) {
1618 pos = eccsize + i * (eccsize + chunk);
1619 if (mtd->writesize > 512)
1620 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1621 else
1622 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1623 } else
1624 sndrnd = 1;
1625 toread = min_t(int, length, chunk);
1626 chip->read_buf(mtd, bufpoi, toread);
1627 bufpoi += toread;
1628 length -= toread;
1629 }
1630 if (length > 0)
1631 chip->read_buf(mtd, bufpoi, length);
1632
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001633 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001634}
1635
1636/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001637 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001638 * @mtd: mtd info structure
1639 * @chip: nand chip info structure
1640 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001641 */
1642static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1643 int page)
1644{
1645 int status = 0;
1646 const uint8_t *buf = chip->oob_poi;
1647 int length = mtd->oobsize;
1648
1649 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1650 chip->write_buf(mtd, buf, length);
1651 /* Send command to program the OOB data */
1652 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1653
1654 status = chip->waitfunc(mtd, chip);
1655
Savin Zlobec0d420f92006-06-21 11:51:20 +02001656 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001657}
1658
1659/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001660 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001661 * with syndrome - only for large page flash
1662 * @mtd: mtd info structure
1663 * @chip: nand chip info structure
1664 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001665 */
1666static int nand_write_oob_syndrome(struct mtd_info *mtd,
1667 struct nand_chip *chip, int page)
1668{
1669 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1670 int eccsize = chip->ecc.size, length = mtd->oobsize;
1671 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1672 const uint8_t *bufpoi = chip->oob_poi;
1673
1674 /*
1675 * data-ecc-data-ecc ... ecc-oob
1676 * or
1677 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1678 */
1679 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1680 pos = steps * (eccsize + chunk);
1681 steps = 0;
1682 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001683 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001684
1685 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1686 for (i = 0; i < steps; i++) {
1687 if (sndcmd) {
1688 if (mtd->writesize <= 512) {
1689 uint32_t fill = 0xFFFFFFFF;
1690
1691 len = eccsize;
1692 while (len > 0) {
1693 int num = min_t(int, len, 4);
1694 chip->write_buf(mtd, (uint8_t *)&fill,
1695 num);
1696 len -= num;
1697 }
1698 } else {
1699 pos = eccsize + i * (eccsize + chunk);
1700 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1701 }
1702 } else
1703 sndcmd = 1;
1704 len = min_t(int, length, chunk);
1705 chip->write_buf(mtd, bufpoi, len);
1706 bufpoi += len;
1707 length -= len;
1708 }
1709 if (length > 0)
1710 chip->write_buf(mtd, bufpoi, length);
1711
1712 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1713 status = chip->waitfunc(mtd, chip);
1714
1715 return status & NAND_STATUS_FAIL ? -EIO : 0;
1716}
1717
1718/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001719 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001720 * @mtd: MTD device structure
1721 * @from: offset to read from
1722 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001724 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001726static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1727 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Brian Norrisc00a0992012-05-01 17:12:54 -07001729 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001730 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001731 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001732 int readlen = ops->ooblen;
1733 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001734 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001735 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Brian Norris289c0522011-07-19 10:06:09 -07001737 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301738 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Brian Norris041e4572011-06-23 16:45:24 -07001740 stats = mtd->ecc_stats;
1741
Brian Norris0612b9d2011-08-30 18:45:40 -07001742 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001743 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001744 else
1745 len = mtd->oobsize;
1746
1747 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001748 pr_debug("%s: attempt to start read outside oob\n",
1749 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001750 return -EINVAL;
1751 }
1752
1753 /* Do not allow reads past end of device */
1754 if (unlikely(from >= mtd->size ||
1755 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1756 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001757 pr_debug("%s: attempt to read beyond end of device\n",
1758 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001759 return -EINVAL;
1760 }
Vitaly Wool70145682006-11-03 18:20:38 +03001761
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001762 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001763 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001765 /* Shift to get page */
1766 realpage = (int)(from >> chip->page_shift);
1767 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Florian Fainellif8ac0412010-09-07 13:23:43 +02001769 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001770 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001771 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001772 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001773 ret = chip->ecc.read_oob(mtd, chip, page);
1774
1775 if (ret < 0)
1776 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001777
1778 len = min(len, readlen);
1779 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001780
Brian Norris5bc7c332013-03-13 09:51:31 -07001781 if (chip->options & NAND_NEED_READRDY) {
1782 /* Apply delay or wait for ready/busy pin */
1783 if (!chip->dev_ready)
1784 udelay(chip->chip_delay);
1785 else
1786 nand_wait_ready(mtd);
1787 }
1788
Vitaly Wool70145682006-11-03 18:20:38 +03001789 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001790 if (!readlen)
1791 break;
1792
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001793 /* Increment page address */
1794 realpage++;
1795
1796 page = realpage & chip->pagemask;
1797 /* Check, if we cross a chip boundary */
1798 if (!page) {
1799 chipnr++;
1800 chip->select_chip(mtd, -1);
1801 chip->select_chip(mtd, chipnr);
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001804 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001806 ops->oobretlen = ops->ooblen - readlen;
1807
1808 if (ret < 0)
1809 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001810
1811 if (mtd->ecc_stats.failed - stats.failed)
1812 return -EBADMSG;
1813
1814 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
1817/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001818 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001819 * @mtd: MTD device structure
1820 * @from: offset to read from
1821 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001823 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001825static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1826 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001828 int ret = -ENOTSUPP;
1829
1830 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001833 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001834 pr_debug("%s: attempt to read beyond end of device\n",
1835 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return -EINVAL;
1837 }
1838
Huang Shijie6a8214a2012-11-19 14:43:30 +08001839 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Florian Fainellif8ac0412010-09-07 13:23:43 +02001841 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001842 case MTD_OPS_PLACE_OOB:
1843 case MTD_OPS_AUTO_OOB:
1844 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001845 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001846
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001847 default:
1848 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001851 if (!ops->datbuf)
1852 ret = nand_do_read_oob(mtd, from, ops);
1853 else
1854 ret = nand_do_read_ops(mtd, from, ops);
1855
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001856out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001858 return ret;
1859}
1860
1861
1862/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001863 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001864 * @mtd: mtd info structure
1865 * @chip: nand chip info structure
1866 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001867 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001868 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001869 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001870 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001871static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001872 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001873{
1874 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001875 if (oob_required)
1876 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001877
1878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001881/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001882 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001883 * @mtd: mtd info structure
1884 * @chip: nand chip info structure
1885 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001886 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001887 *
1888 * We need a special oob layout and handling even when ECC isn't checked.
1889 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001890static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001891 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001892 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001893{
1894 int eccsize = chip->ecc.size;
1895 int eccbytes = chip->ecc.bytes;
1896 uint8_t *oob = chip->oob_poi;
1897 int steps, size;
1898
1899 for (steps = chip->ecc.steps; steps > 0; steps--) {
1900 chip->write_buf(mtd, buf, eccsize);
1901 buf += eccsize;
1902
1903 if (chip->ecc.prepad) {
1904 chip->write_buf(mtd, oob, chip->ecc.prepad);
1905 oob += chip->ecc.prepad;
1906 }
1907
1908 chip->read_buf(mtd, oob, eccbytes);
1909 oob += eccbytes;
1910
1911 if (chip->ecc.postpad) {
1912 chip->write_buf(mtd, oob, chip->ecc.postpad);
1913 oob += chip->ecc.postpad;
1914 }
1915 }
1916
1917 size = mtd->oobsize - (oob - chip->oob_poi);
1918 if (size)
1919 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001920
1921 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001922}
1923/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001924 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001925 * @mtd: mtd info structure
1926 * @chip: nand chip info structure
1927 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001928 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001929 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001930static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001931 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001932{
1933 int i, eccsize = chip->ecc.size;
1934 int eccbytes = chip->ecc.bytes;
1935 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001936 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001937 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001938 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001939
Brian Norris7854d3f2011-06-23 14:12:08 -07001940 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001941 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1942 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001943
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001944 for (i = 0; i < chip->ecc.total; i++)
1945 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001946
Josh Wufdbad98d2012-06-25 18:07:45 +08001947 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001948}
1949
1950/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001951 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001952 * @mtd: mtd info structure
1953 * @chip: nand chip info structure
1954 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001955 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001956 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001957static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001958 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001959{
1960 int i, eccsize = chip->ecc.size;
1961 int eccbytes = chip->ecc.bytes;
1962 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001963 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001964 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001965 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001966
1967 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1968 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001969 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001970 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1971 }
1972
1973 for (i = 0; i < chip->ecc.total; i++)
1974 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1975
1976 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001977
1978 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001979}
1980
1981/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001982 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07001983 * @mtd: mtd info structure
1984 * @chip: nand chip info structure
1985 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001986 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001987 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001988 * The hw generator calculates the error syndrome automatically. Therefore we
1989 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001990 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001991static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001992 struct nand_chip *chip,
1993 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001994{
1995 int i, eccsize = chip->ecc.size;
1996 int eccbytes = chip->ecc.bytes;
1997 int eccsteps = chip->ecc.steps;
1998 const uint8_t *p = buf;
1999 uint8_t *oob = chip->oob_poi;
2000
2001 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2002
2003 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2004 chip->write_buf(mtd, p, eccsize);
2005
2006 if (chip->ecc.prepad) {
2007 chip->write_buf(mtd, oob, chip->ecc.prepad);
2008 oob += chip->ecc.prepad;
2009 }
2010
2011 chip->ecc.calculate(mtd, p, oob);
2012 chip->write_buf(mtd, oob, eccbytes);
2013 oob += eccbytes;
2014
2015 if (chip->ecc.postpad) {
2016 chip->write_buf(mtd, oob, chip->ecc.postpad);
2017 oob += chip->ecc.postpad;
2018 }
2019 }
2020
2021 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002022 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002023 if (i)
2024 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002025
2026 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002027}
2028
2029/**
David Woodhouse956e9442006-09-25 17:12:39 +01002030 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002031 * @mtd: MTD device structure
2032 * @chip: NAND chip descriptor
2033 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002034 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002035 * @page: page number to write
2036 * @cached: cached programming
2037 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002038 */
2039static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002040 const uint8_t *buf, int oob_required, int page,
2041 int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002042{
2043 int status;
2044
2045 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2046
David Woodhouse956e9442006-09-25 17:12:39 +01002047 if (unlikely(raw))
Josh Wufdbad98d2012-06-25 18:07:45 +08002048 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002049 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002050 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2051
2052 if (status < 0)
2053 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002054
2055 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002056 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002057 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002058 */
2059 cached = 0;
2060
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002061 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002062
2063 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002064 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002065 /*
2066 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002067 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002068 */
2069 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2070 status = chip->errstat(mtd, chip, FL_WRITING, status,
2071 page);
2072
2073 if (status & NAND_STATUS_FAIL)
2074 return -EIO;
2075 } else {
2076 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002077 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002078 }
2079
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002080 return 0;
2081}
2082
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002083/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002084 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002085 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002086 * @oob: oob data buffer
2087 * @len: oob data write length
2088 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002089 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002090static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2091 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002092{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002093 struct nand_chip *chip = mtd->priv;
2094
2095 /*
2096 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2097 * data from a previous OOB read.
2098 */
2099 memset(chip->oob_poi, 0xff, mtd->oobsize);
2100
Florian Fainellif8ac0412010-09-07 13:23:43 +02002101 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002102
Brian Norris0612b9d2011-08-30 18:45:40 -07002103 case MTD_OPS_PLACE_OOB:
2104 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002105 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2106 return oob + len;
2107
Brian Norris0612b9d2011-08-30 18:45:40 -07002108 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002109 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002110 uint32_t boffs = 0, woffs = ops->ooboffs;
2111 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002112
Florian Fainellif8ac0412010-09-07 13:23:43 +02002113 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002114 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002115 if (unlikely(woffs)) {
2116 if (woffs >= free->length) {
2117 woffs -= free->length;
2118 continue;
2119 }
2120 boffs = free->offset + woffs;
2121 bytes = min_t(size_t, len,
2122 (free->length - woffs));
2123 woffs = 0;
2124 } else {
2125 bytes = min_t(size_t, len, free->length);
2126 boffs = free->offset;
2127 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002128 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002129 oob += bytes;
2130 }
2131 return oob;
2132 }
2133 default:
2134 BUG();
2135 }
2136 return NULL;
2137}
2138
Florian Fainellif8ac0412010-09-07 13:23:43 +02002139#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002140
2141/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002142 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002143 * @mtd: MTD device structure
2144 * @to: offset to write to
2145 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002146 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002147 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002148 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002149static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2150 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002151{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002152 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002153 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002154 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002155
2156 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002157 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002158 mtd->oobavail : mtd->oobsize;
2159
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002160 uint8_t *oob = ops->oobbuf;
2161 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002162 int ret, subpage;
Brian Norrise47f3db2012-05-02 10:14:56 -07002163 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002164
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002166 if (!writelen)
2167 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002168
Brian Norris8b6e50c2011-05-25 14:59:01 -07002169 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002170 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002171 pr_notice("%s: attempt to write non page aligned data\n",
2172 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002173 return -EINVAL;
2174 }
2175
Thomas Gleixner29072b92006-09-28 15:38:36 +02002176 column = to & (mtd->writesize - 1);
2177 subpage = column || (writelen & (mtd->writesize - 1));
2178
2179 if (subpage && oob)
2180 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002181
Thomas Gleixner6a930962006-06-28 00:11:45 +02002182 chipnr = (int)(to >> chip->chip_shift);
2183 chip->select_chip(mtd, chipnr);
2184
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002185 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002186 if (nand_check_wp(mtd)) {
2187 ret = -EIO;
2188 goto err_out;
2189 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002190
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002191 realpage = (int)(to >> chip->page_shift);
2192 page = realpage & chip->pagemask;
2193 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2194
2195 /* Invalidate the page cache, when we write to the cached page */
2196 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002197 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002198 chip->pagebuf = -1;
2199
Maxim Levitsky782ce792010-02-22 20:39:36 +02002200 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002201 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2202 ret = -EINVAL;
2203 goto err_out;
2204 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002205
Florian Fainellif8ac0412010-09-07 13:23:43 +02002206 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002207 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002208 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002209 uint8_t *wbuf = buf;
2210
Brian Norris8b6e50c2011-05-25 14:59:01 -07002211 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002212 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2213 cached = 0;
2214 bytes = min_t(int, bytes - column, (int) writelen);
2215 chip->pagebuf = -1;
2216 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2217 memcpy(&chip->buffers->databuf[column], buf, bytes);
2218 wbuf = chip->buffers->databuf;
2219 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002220
Maxim Levitsky782ce792010-02-22 20:39:36 +02002221 if (unlikely(oob)) {
2222 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002223 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002224 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002225 } else {
2226 /* We still need to erase leftover OOB data */
2227 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002228 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002229
Brian Norrise47f3db2012-05-02 10:14:56 -07002230 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2231 cached, (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002232 if (ret)
2233 break;
2234
2235 writelen -= bytes;
2236 if (!writelen)
2237 break;
2238
Thomas Gleixner29072b92006-09-28 15:38:36 +02002239 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002240 buf += bytes;
2241 realpage++;
2242
2243 page = realpage & chip->pagemask;
2244 /* Check, if we cross a chip boundary */
2245 if (!page) {
2246 chipnr++;
2247 chip->select_chip(mtd, -1);
2248 chip->select_chip(mtd, chipnr);
2249 }
2250 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002251
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002252 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002253 if (unlikely(oob))
2254 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002255
2256err_out:
2257 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002258 return ret;
2259}
2260
2261/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002262 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002263 * @mtd: MTD device structure
2264 * @to: offset to write to
2265 * @len: number of bytes to write
2266 * @retlen: pointer to variable to store the number of written bytes
2267 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002268 *
2269 * NAND write with ECC. Used when performing writes in interrupt context, this
2270 * may for example be called by mtdoops when writing an oops while in panic.
2271 */
2272static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2273 size_t *retlen, const uint8_t *buf)
2274{
2275 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002276 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002277 int ret;
2278
Brian Norris8b6e50c2011-05-25 14:59:01 -07002279 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002280 panic_nand_wait(mtd, chip, 400);
2281
Brian Norris8b6e50c2011-05-25 14:59:01 -07002282 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002283 panic_nand_get_device(chip, mtd, FL_WRITING);
2284
Brian Norris4a89ff82011-08-30 18:45:45 -07002285 ops.len = len;
2286 ops.datbuf = (uint8_t *)buf;
2287 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002288 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002289
Brian Norris4a89ff82011-08-30 18:45:45 -07002290 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002291
Brian Norris4a89ff82011-08-30 18:45:45 -07002292 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002293 return ret;
2294}
2295
2296/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002297 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002298 * @mtd: MTD device structure
2299 * @to: offset to write to
2300 * @len: number of bytes to write
2301 * @retlen: pointer to variable to store the number of written bytes
2302 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002304 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002306static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002307 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Brian Norris4a89ff82011-08-30 18:45:45 -07002309 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002310 int ret;
2311
Huang Shijie6a8214a2012-11-19 14:43:30 +08002312 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002313 ops.len = len;
2314 ops.datbuf = (uint8_t *)buf;
2315 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002316 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002317 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002318 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002319 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002320 return ret;
2321}
2322
2323/**
2324 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002325 * @mtd: MTD device structure
2326 * @to: offset to write to
2327 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002329 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002330 */
2331static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2332 struct mtd_oob_ops *ops)
2333{
Adrian Hunter03736152007-01-31 17:58:29 +02002334 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002335 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Brian Norris289c0522011-07-19 10:06:09 -07002337 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302338 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Brian Norris0612b9d2011-08-30 18:45:40 -07002340 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002341 len = chip->ecc.layout->oobavail;
2342 else
2343 len = mtd->oobsize;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002346 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002347 pr_debug("%s: attempt to write past end of page\n",
2348 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return -EINVAL;
2350 }
2351
Adrian Hunter03736152007-01-31 17:58:29 +02002352 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002353 pr_debug("%s: attempt to start write outside oob\n",
2354 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002355 return -EINVAL;
2356 }
2357
Jason Liu775adc32011-02-25 13:06:18 +08002358 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002359 if (unlikely(to >= mtd->size ||
2360 ops->ooboffs + ops->ooblen >
2361 ((mtd->size >> chip->page_shift) -
2362 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002363 pr_debug("%s: attempt to write beyond end of device\n",
2364 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002365 return -EINVAL;
2366 }
2367
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002368 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002369 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002371 /* Shift to get page */
2372 page = (int)(to >> chip->page_shift);
2373
2374 /*
2375 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2376 * of my DiskOnChip 2000 test units) will clear the whole data page too
2377 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2378 * it in the doc2000 driver in August 1999. dwmw2.
2379 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002380 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002383 if (nand_check_wp(mtd)) {
2384 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002385 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002386 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002389 if (page == chip->pagebuf)
2390 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002392 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002393
Brian Norris0612b9d2011-08-30 18:45:40 -07002394 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002395 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2396 else
2397 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002398
Huang Shijieb0bb6902012-11-19 14:43:29 +08002399 chip->select_chip(mtd, -1);
2400
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002401 if (status)
2402 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Vitaly Wool70145682006-11-03 18:20:38 +03002404 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002406 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002409/**
2410 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002411 * @mtd: MTD device structure
2412 * @to: offset to write to
2413 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002414 */
2415static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2416 struct mtd_oob_ops *ops)
2417{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002418 int ret = -ENOTSUPP;
2419
2420 ops->retlen = 0;
2421
2422 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002423 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002424 pr_debug("%s: attempt to write beyond end of device\n",
2425 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002426 return -EINVAL;
2427 }
2428
Huang Shijie6a8214a2012-11-19 14:43:30 +08002429 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002430
Florian Fainellif8ac0412010-09-07 13:23:43 +02002431 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002432 case MTD_OPS_PLACE_OOB:
2433 case MTD_OPS_AUTO_OOB:
2434 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002435 break;
2436
2437 default:
2438 goto out;
2439 }
2440
2441 if (!ops->datbuf)
2442 ret = nand_do_write_oob(mtd, to, ops);
2443 else
2444 ret = nand_do_write_ops(mtd, to, ops);
2445
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002446out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002447 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return ret;
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002452 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002453 * @mtd: MTD device structure
2454 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002456 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002458static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002460 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002462 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2463 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
2466/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002468 * @mtd: MTD device structure
2469 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002471 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002473static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
David Woodhousee0c7d762006-05-13 18:07:53 +01002475 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002479 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002480 * @mtd: MTD device structure
2481 * @instr: erase instruction
2482 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002484 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002486int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2487 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Adrian Hunter69423d92008-12-10 13:37:21 +00002489 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002490 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002491 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Brian Norris289c0522011-07-19 10:06:09 -07002493 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2494 __func__, (unsigned long long)instr->addr,
2495 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302497 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002501 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002504 page = (int)(instr->addr >> chip->page_shift);
2505 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002508 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002511 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /* Check, if it is write protected */
2514 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002515 pr_debug("%s: device is write protected!\n",
2516 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 instr->state = MTD_ERASE_FAILED;
2518 goto erase_exit;
2519 }
2520
2521 /* Loop through the pages */
2522 len = instr->len;
2523
2524 instr->state = MTD_ERASING;
2525
2526 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002527 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2529 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002530 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2531 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 instr->state = MTD_ERASE_FAILED;
2533 goto erase_exit;
2534 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002535
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002536 /*
2537 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002538 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002539 */
2540 if (page <= chip->pagebuf && chip->pagebuf <
2541 (page + pages_per_block))
2542 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002544 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002545
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002546 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002548 /*
2549 * See if operation failed and additional status checks are
2550 * available
2551 */
2552 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2553 status = chip->errstat(mtd, chip, FL_ERASING,
2554 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002557 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002558 pr_debug("%s: failed erase, page 0x%08x\n",
2559 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002561 instr->fail_addr =
2562 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 goto erase_exit;
2564 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002567 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 page += pages_per_block;
2569
2570 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002571 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002573 chip->select_chip(mtd, -1);
2574 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
2576 }
2577 instr->state = MTD_ERASE_DONE;
2578
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002579erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002584 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 nand_release_device(mtd);
2586
David Woodhouse49defc02007-10-06 15:01:59 -04002587 /* Do call back function */
2588 if (!ret)
2589 mtd_erase_callback(instr);
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /* Return more or less happy */
2592 return ret;
2593}
2594
2595/**
2596 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002597 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002599 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002601static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Brian Norris289c0522011-07-19 10:06:09 -07002603 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002606 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002608 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002612 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002613 * @mtd: MTD device structure
2614 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002616static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002618 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
2621/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002622 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002623 * @mtd: MTD device structure
2624 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002626static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002628 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 int ret;
2630
Florian Fainellif8ac0412010-09-07 13:23:43 +02002631 ret = nand_block_isbad(mtd, ofs);
2632 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002633 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (ret > 0)
2635 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002636 return ret;
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002639 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
2642/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002643 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2644 * @mtd: MTD device structure
2645 * @chip: nand chip info structure
2646 * @addr: feature address.
2647 * @subfeature_param: the subfeature parameters, a four bytes array.
2648 */
2649static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2650 int addr, uint8_t *subfeature_param)
2651{
2652 int status;
2653
2654 if (!chip->onfi_version)
2655 return -EINVAL;
2656
2657 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2658 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2659 status = chip->waitfunc(mtd, chip);
2660 if (status & NAND_STATUS_FAIL)
2661 return -EIO;
2662 return 0;
2663}
2664
2665/**
2666 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2667 * @mtd: MTD device structure
2668 * @chip: nand chip info structure
2669 * @addr: feature address.
2670 * @subfeature_param: the subfeature parameters, a four bytes array.
2671 */
2672static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2673 int addr, uint8_t *subfeature_param)
2674{
2675 if (!chip->onfi_version)
2676 return -EINVAL;
2677
2678 /* clear the sub feature parameters */
2679 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2680
2681 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2682 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2683 return 0;
2684}
2685
2686/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002687 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002688 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002689 */
2690static int nand_suspend(struct mtd_info *mtd)
2691{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002692 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002693}
2694
2695/**
2696 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002698 */
2699static void nand_resume(struct mtd_info *mtd)
2700{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002701 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002702
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002703 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002704 nand_release_device(mtd);
2705 else
Brian Norrisd0370212011-07-19 10:06:08 -07002706 pr_err("%s called for a chip which is not in suspended state\n",
2707 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002708}
2709
Brian Norris8b6e50c2011-05-25 14:59:01 -07002710/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002711static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002714 if (!chip->chip_delay)
2715 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002718 if (chip->cmdfunc == NULL)
2719 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002722 if (chip->waitfunc == NULL)
2723 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002725 if (!chip->select_chip)
2726 chip->select_chip = nand_select_chip;
2727 if (!chip->read_byte)
2728 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2729 if (!chip->read_word)
2730 chip->read_word = nand_read_word;
2731 if (!chip->block_bad)
2732 chip->block_bad = nand_block_bad;
2733 if (!chip->block_markbad)
2734 chip->block_markbad = nand_default_block_markbad;
2735 if (!chip->write_buf)
2736 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2737 if (!chip->read_buf)
2738 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002739 if (!chip->scan_bbt)
2740 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002741
2742 if (!chip->controller) {
2743 chip->controller = &chip->hwcontrol;
2744 spin_lock_init(&chip->controller->lock);
2745 init_waitqueue_head(&chip->controller->wq);
2746 }
2747
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002748}
2749
Brian Norris8b6e50c2011-05-25 14:59:01 -07002750/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002751static void sanitize_string(uint8_t *s, size_t len)
2752{
2753 ssize_t i;
2754
Brian Norris8b6e50c2011-05-25 14:59:01 -07002755 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002756 s[len - 1] = 0;
2757
Brian Norris8b6e50c2011-05-25 14:59:01 -07002758 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002759 for (i = 0; i < len - 1; i++) {
2760 if (s[i] < ' ' || s[i] > 127)
2761 s[i] = '?';
2762 }
2763
Brian Norris8b6e50c2011-05-25 14:59:01 -07002764 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002765 strim(s);
2766}
2767
2768static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2769{
2770 int i;
2771 while (len--) {
2772 crc ^= *p++ << 8;
2773 for (i = 0; i < 8; i++)
2774 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2775 }
2776
2777 return crc;
2778}
2779
2780/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002781 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002782 */
2783static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002784 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002785{
2786 struct nand_onfi_params *p = &chip->onfi_params;
2787 int i;
2788 int val;
2789
Matthieu CASTET0ce82b72013-01-16 15:25:45 +01002790 /* ONFI need to be probed in 8 bits mode, and 16 bits should be selected with NAND_BUSWIDTH_AUTO */
2791 if (chip->options & NAND_BUSWIDTH_16) {
2792 pr_err("Trying ONFI probe in 16 bits mode, aborting !\n");
2793 return 0;
2794 }
Brian Norris7854d3f2011-06-23 14:12:08 -07002795 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002796 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2797 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2798 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2799 return 0;
2800
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002801 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2802 for (i = 0; i < 3; i++) {
2803 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2804 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2805 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002806 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002807 break;
2808 }
2809 }
2810
2811 if (i == 3)
2812 return 0;
2813
Brian Norris8b6e50c2011-05-25 14:59:01 -07002814 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002815 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002816 if (val & (1 << 5))
2817 chip->onfi_version = 23;
2818 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002819 chip->onfi_version = 22;
2820 else if (val & (1 << 3))
2821 chip->onfi_version = 21;
2822 else if (val & (1 << 2))
2823 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002824 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002825 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002826
2827 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002828 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002829 return 0;
2830 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002831
2832 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2833 sanitize_string(p->model, sizeof(p->model));
2834 if (!mtd->name)
2835 mtd->name = p->model;
2836 mtd->writesize = le32_to_cpu(p->byte_per_page);
2837 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2838 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTET63795752012-03-19 15:35:25 +01002839 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2840 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002841 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002842 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002843 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002844
Huang Shijied42b5de2012-02-17 11:22:37 +08002845 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002846 return 1;
2847}
2848
2849/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07002850 * nand_id_has_period - Check if an ID string has a given wraparound period
2851 * @id_data: the ID string
2852 * @arrlen: the length of the @id_data array
2853 * @period: the period of repitition
2854 *
2855 * Check if an ID string is repeated within a given sequence of bytes at
2856 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08002857 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07002858 * if the repetition has a period of @period; otherwise, returns zero.
2859 */
2860static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2861{
2862 int i, j;
2863 for (i = 0; i < period; i++)
2864 for (j = i + period; j < arrlen; j += period)
2865 if (id_data[i] != id_data[j])
2866 return 0;
2867 return 1;
2868}
2869
2870/*
2871 * nand_id_len - Get the length of an ID string returned by CMD_READID
2872 * @id_data: the ID string
2873 * @arrlen: the length of the @id_data array
2874
2875 * Returns the length of the ID string, according to known wraparound/trailing
2876 * zero patterns. If no pattern exists, returns the length of the array.
2877 */
2878static int nand_id_len(u8 *id_data, int arrlen)
2879{
2880 int last_nonzero, period;
2881
2882 /* Find last non-zero byte */
2883 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2884 if (id_data[last_nonzero])
2885 break;
2886
2887 /* All zeros */
2888 if (last_nonzero < 0)
2889 return 0;
2890
2891 /* Calculate wraparound period */
2892 for (period = 1; period < arrlen; period++)
2893 if (nand_id_has_period(id_data, arrlen, period))
2894 break;
2895
2896 /* There's a repeated pattern */
2897 if (period < arrlen)
2898 return period;
2899
2900 /* There are trailing zeros */
2901 if (last_nonzero < arrlen - 1)
2902 return last_nonzero + 1;
2903
2904 /* No pattern detected */
2905 return arrlen;
2906}
2907
2908/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002909 * Many new NAND share similar device ID codes, which represent the size of the
2910 * chip. The rest of the parameters must be decoded according to generic or
2911 * manufacturer-specific "extended ID" decoding patterns.
2912 */
2913static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2914 u8 id_data[8], int *busw)
2915{
Brian Norrise3b88bd2012-09-24 20:40:52 -07002916 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002917 /* The 3rd id byte holds MLC / multichip data */
2918 chip->cellinfo = id_data[2];
2919 /* The 4th id byte is the important one */
2920 extid = id_data[3];
2921
Brian Norrise3b88bd2012-09-24 20:40:52 -07002922 id_len = nand_id_len(id_data, 8);
2923
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002924 /*
2925 * Field definitions are in the following datasheets:
2926 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07002927 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07002928 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002929 *
Brian Norrisaf451af2012-10-09 23:26:06 -07002930 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2931 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002932 */
Brian Norrisaf451af2012-10-09 23:26:06 -07002933 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Brian Norris6924d992012-11-14 21:46:30 -08002934 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Brian Norrisaf451af2012-10-09 23:26:06 -07002935 id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002936 /* Calc pagesize */
2937 mtd->writesize = 2048 << (extid & 0x03);
2938 extid >>= 2;
2939 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07002940 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002941 case 1:
2942 mtd->oobsize = 128;
2943 break;
2944 case 2:
2945 mtd->oobsize = 218;
2946 break;
2947 case 3:
2948 mtd->oobsize = 400;
2949 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07002950 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002951 mtd->oobsize = 436;
2952 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07002953 case 5:
2954 mtd->oobsize = 512;
2955 break;
2956 case 6:
2957 default: /* Other cases are "reserved" (unknown) */
2958 mtd->oobsize = 640;
2959 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002960 }
2961 extid >>= 2;
2962 /* Calc blocksize */
2963 mtd->erasesize = (128 * 1024) <<
2964 (((extid >> 1) & 0x04) | (extid & 0x03));
2965 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07002966 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2967 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2968 unsigned int tmp;
2969
2970 /* Calc pagesize */
2971 mtd->writesize = 2048 << (extid & 0x03);
2972 extid >>= 2;
2973 /* Calc oobsize */
2974 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2975 case 0:
2976 mtd->oobsize = 128;
2977 break;
2978 case 1:
2979 mtd->oobsize = 224;
2980 break;
2981 case 2:
2982 mtd->oobsize = 448;
2983 break;
2984 case 3:
2985 mtd->oobsize = 64;
2986 break;
2987 case 4:
2988 mtd->oobsize = 32;
2989 break;
2990 case 5:
2991 mtd->oobsize = 16;
2992 break;
2993 default:
2994 mtd->oobsize = 640;
2995 break;
2996 }
2997 extid >>= 2;
2998 /* Calc blocksize */
2999 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3000 if (tmp < 0x03)
3001 mtd->erasesize = (128 * 1024) << tmp;
3002 else if (tmp == 0x03)
3003 mtd->erasesize = 768 * 1024;
3004 else
3005 mtd->erasesize = (64 * 1024) << tmp;
3006 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003007 } else {
3008 /* Calc pagesize */
3009 mtd->writesize = 1024 << (extid & 0x03);
3010 extid >>= 2;
3011 /* Calc oobsize */
3012 mtd->oobsize = (8 << (extid & 0x01)) *
3013 (mtd->writesize >> 9);
3014 extid >>= 2;
3015 /* Calc blocksize. Blocksize is multiples of 64KiB */
3016 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3017 extid >>= 2;
3018 /* Get buswidth information */
3019 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3020 }
3021}
3022
3023/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003024 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3025 * decodes a matching ID table entry and assigns the MTD size parameters for
3026 * the chip.
3027 */
3028static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3029 struct nand_flash_dev *type, u8 id_data[8],
3030 int *busw)
3031{
3032 int maf_id = id_data[0];
3033
3034 mtd->erasesize = type->erasesize;
3035 mtd->writesize = type->pagesize;
3036 mtd->oobsize = mtd->writesize / 32;
3037 *busw = type->options & NAND_BUSWIDTH_16;
3038
3039 /*
3040 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3041 * some Spansion chips have erasesize that conflicts with size
3042 * listed in nand_ids table.
3043 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3044 */
3045 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3046 && id_data[6] == 0x00 && id_data[7] == 0x00
3047 && mtd->writesize == 512) {
3048 mtd->erasesize = 128 * 1024;
3049 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3050 }
3051}
3052
3053/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003054 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3055 * heuristic patterns using various detected parameters (e.g., manufacturer,
3056 * page size, cell-type information).
3057 */
3058static void nand_decode_bbm_options(struct mtd_info *mtd,
3059 struct nand_chip *chip, u8 id_data[8])
3060{
3061 int maf_id = id_data[0];
3062
3063 /* Set the bad block position */
3064 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3065 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3066 else
3067 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3068
3069 /*
3070 * Bad block marker is stored in the last page of each block on Samsung
3071 * and Hynix MLC devices; stored in first two pages of each block on
3072 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3073 * AMD/Spansion, and Macronix. All others scan only the first page.
3074 */
3075 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3076 (maf_id == NAND_MFR_SAMSUNG ||
3077 maf_id == NAND_MFR_HYNIX))
3078 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3079 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3080 (maf_id == NAND_MFR_SAMSUNG ||
3081 maf_id == NAND_MFR_HYNIX ||
3082 maf_id == NAND_MFR_TOSHIBA ||
3083 maf_id == NAND_MFR_AMD ||
3084 maf_id == NAND_MFR_MACRONIX)) ||
3085 (mtd->writesize == 2048 &&
3086 maf_id == NAND_MFR_MICRON))
3087 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3088}
3089
Huang Shijieec6e87e2013-03-15 11:01:00 +08003090static inline bool is_full_id_nand(struct nand_flash_dev *type)
3091{
3092 return type->id_len;
3093}
3094
3095static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3096 struct nand_flash_dev *type, u8 *id_data, int *busw)
3097{
3098 if (!strncmp(type->id, id_data, type->id_len)) {
3099 mtd->writesize = type->pagesize;
3100 mtd->erasesize = type->erasesize;
3101 mtd->oobsize = type->oobsize;
3102
3103 chip->cellinfo = id_data[2];
3104 chip->chipsize = (uint64_t)type->chipsize << 20;
3105 chip->options |= type->options;
3106
3107 *busw = type->options & NAND_BUSWIDTH_16;
3108
3109 return true;
3110 }
3111 return false;
3112}
3113
Brian Norris7e74c2d2012-09-24 20:40:49 -07003114/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003115 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003116 */
3117static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003118 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003119 int busw,
3120 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003121 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003122{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003123 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003124 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003127 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Karl Beldanef89a882008-09-15 14:37:29 +02003129 /*
3130 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003131 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003132 */
3133 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003136 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003139 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003140 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Brian Norris8b6e50c2011-05-25 14:59:01 -07003142 /*
3143 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003144 * interface concerns can cause random data which looks like a
3145 * possibly credible NAND flash to appear. If the two results do
3146 * not match, ignore the device completely.
3147 */
3148
3149 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3150
Brian Norris4aef9b72012-09-24 20:40:48 -07003151 /* Read entire ID string */
3152 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003153 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003154
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003155 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003156 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07003157 "%02x,%02x against %02x,%02x\n", __func__,
3158 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003159 return ERR_PTR(-ENODEV);
3160 }
3161
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003162 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003163 type = nand_flash_ids;
3164
Huang Shijieec6e87e2013-03-15 11:01:00 +08003165 for (; type->name != NULL; type++) {
3166 if (is_full_id_nand(type)) {
3167 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3168 goto ident_done;
3169 } else if (*dev_id == type->dev_id) {
3170 break;
3171 }
3172 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003173
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003174 chip->onfi_version = 0;
3175 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003176 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003177 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003178 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003179 }
3180
David Woodhouse5e81e882010-02-26 18:32:56 +00003181 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003182 return ERR_PTR(-ENODEV);
3183
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003184 if (!mtd->name)
3185 mtd->name = type->name;
3186
Adrian Hunter69423d92008-12-10 13:37:21 +00003187 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003188
Huang Shijie12a40a52010-09-27 10:43:53 +08003189 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003190 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003191 busw = chip->init_size(mtd, chip, id_data);
3192 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003193 /* Decode parameters from extended ID */
3194 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003195 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003196 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003197 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003198 /* Get chip options */
3199 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003200
Brian Norris8b6e50c2011-05-25 14:59:01 -07003201 /*
3202 * Check if chip is not a Samsung device. Do not clear the
3203 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003204 */
3205 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3206 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3207ident_done:
3208
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003209 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003210 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003211 if (nand_manuf_ids[maf_idx].id == *maf_id)
3212 break;
3213 }
3214
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003215 if (chip->options & NAND_BUSWIDTH_AUTO) {
3216 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3217 chip->options |= busw;
3218 nand_set_defaults(chip, busw);
3219 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3220 /*
3221 * Check, if buswidth is correct. Hardware drivers should set
3222 * chip correct!
3223 */
Brian Norris9a4d4d62011-07-19 10:06:07 -07003224 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003225 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3226 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003227 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003228 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3229 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003230 return ERR_PTR(-EINVAL);
3231 }
3232
Brian Norris7e74c2d2012-09-24 20:40:49 -07003233 nand_decode_bbm_options(mtd, chip, id_data);
3234
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003235 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003236 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003237 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003238 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003239
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003240 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003241 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003242 if (chip->chipsize & 0xffffffff)
3243 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003244 else {
3245 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3246 chip->chip_shift += 32 - 1;
3247 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003248
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003249 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003250 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003251
Brian Norris8b6e50c2011-05-25 14:59:01 -07003252 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003253 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3254 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003255
Huang Shijie886bd332012-04-09 11:41:37 +08003256 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003257 " %dMiB, page size: %d, OOB size: %d\n",
Huang Shijie886bd332012-04-09 11:41:37 +08003258 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3259 chip->onfi_version ? chip->onfi_params.model : type->name,
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003260 (int)(chip->chipsize >> 20), mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003261
3262 return type;
3263}
3264
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003265/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003266 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003267 * @mtd: MTD device structure
3268 * @maxchips: number of chips to scan for
3269 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003270 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003271 * This is the first phase of the normal nand_scan() function. It reads the
3272 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003273 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003274 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003275 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003276int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3277 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003278{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003279 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003280 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003281 struct nand_flash_dev *type;
3282
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003283 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003284 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003285 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003286 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003287
3288 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003289 type = nand_get_flash_type(mtd, chip, busw,
3290 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003291
3292 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003293 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003294 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003295 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003296 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
3298
Huang Shijie07300162012-11-09 16:23:45 +08003299 chip->select_chip(mtd, -1);
3300
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003301 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003302 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003303 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003304 /* See comment in nand_get_flash_type for reset */
3305 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003307 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003309 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003310 nand_dev_id != chip->read_byte(mtd)) {
3311 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 break;
Huang Shijie07300162012-11-09 16:23:45 +08003313 }
3314 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003317 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003320 chip->numchips = i;
3321 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
David Woodhouse3b85c322006-09-25 17:06:53 +01003323 return 0;
3324}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003325EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003326
3327
3328/**
3329 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003330 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003331 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003332 * This is the second phase of the normal nand_scan() function. It fills out
3333 * all the uninitialized function pointers with the defaults and scans for a
3334 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003335 */
3336int nand_scan_tail(struct mtd_info *mtd)
3337{
3338 int i;
3339 struct nand_chip *chip = mtd->priv;
3340
Brian Norrise2414f42012-02-06 13:44:00 -08003341 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3342 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3343 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3344
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003345 if (!(chip->options & NAND_OWN_BUFFERS))
3346 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3347 if (!chip->buffers)
3348 return -ENOMEM;
3349
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003350 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003351 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003352
3353 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003354 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003355 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003356 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003357 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003359 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 break;
3361 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003362 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 break;
3364 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003365 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003367 case 128:
3368 chip->ecc.layout = &nand_oob_128;
3369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003371 pr_warn("No oob scheme defined for oobsize %d\n",
3372 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 BUG();
3374 }
3375 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003376
David Woodhouse956e9442006-09-25 17:12:39 +01003377 if (!chip->write_page)
3378 chip->write_page = nand_write_page;
3379
Huang Shijie7db03ec2012-09-13 14:57:52 +08003380 /* set for ONFI nand */
3381 if (!chip->onfi_set_features)
3382 chip->onfi_set_features = nand_onfi_set_features;
3383 if (!chip->onfi_get_features)
3384 chip->onfi_get_features = nand_onfi_get_features;
3385
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003386 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003387 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003388 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003389 */
David Woodhouse956e9442006-09-25 17:12:39 +01003390
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003391 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003392 case NAND_ECC_HW_OOB_FIRST:
3393 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3394 if (!chip->ecc.calculate || !chip->ecc.correct ||
3395 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003396 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003397 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003398 BUG();
3399 }
3400 if (!chip->ecc.read_page)
3401 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3402
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003403 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003404 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003405 if (!chip->ecc.read_page)
3406 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003407 if (!chip->ecc.write_page)
3408 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003409 if (!chip->ecc.read_page_raw)
3410 chip->ecc.read_page_raw = nand_read_page_raw;
3411 if (!chip->ecc.write_page_raw)
3412 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003413 if (!chip->ecc.read_oob)
3414 chip->ecc.read_oob = nand_read_oob_std;
3415 if (!chip->ecc.write_oob)
3416 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003417
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003418 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003419 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3420 !chip->ecc.hwctl) &&
3421 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003422 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003423 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003424 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003425 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003426 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003427 BUG();
3428 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003429 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003430 if (!chip->ecc.read_page)
3431 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003432 if (!chip->ecc.write_page)
3433 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003434 if (!chip->ecc.read_page_raw)
3435 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3436 if (!chip->ecc.write_page_raw)
3437 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003438 if (!chip->ecc.read_oob)
3439 chip->ecc.read_oob = nand_read_oob_syndrome;
3440 if (!chip->ecc.write_oob)
3441 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003442
Mike Dunne2788c92012-04-25 12:06:10 -07003443 if (mtd->writesize >= chip->ecc.size) {
3444 if (!chip->ecc.strength) {
3445 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3446 BUG();
3447 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003448 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003449 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003450 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003451 "%d byte page size, fallback to SW ECC\n",
3452 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003453 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003455 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003456 chip->ecc.calculate = nand_calculate_ecc;
3457 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003458 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003459 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003460 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003461 chip->ecc.read_page_raw = nand_read_page_raw;
3462 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003463 chip->ecc.read_oob = nand_read_oob_std;
3464 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003465 if (!chip->ecc.size)
3466 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003467 chip->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003468 chip->ecc.strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003470
Ivan Djelic193bd402011-03-11 11:05:33 +01003471 case NAND_ECC_SOFT_BCH:
3472 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003473 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003474 BUG();
3475 }
3476 chip->ecc.calculate = nand_bch_calculate_ecc;
3477 chip->ecc.correct = nand_bch_correct_data;
3478 chip->ecc.read_page = nand_read_page_swecc;
3479 chip->ecc.read_subpage = nand_read_subpage;
3480 chip->ecc.write_page = nand_write_page_swecc;
3481 chip->ecc.read_page_raw = nand_read_page_raw;
3482 chip->ecc.write_page_raw = nand_write_page_raw;
3483 chip->ecc.read_oob = nand_read_oob_std;
3484 chip->ecc.write_oob = nand_write_oob_std;
3485 /*
3486 * Board driver should supply ecc.size and ecc.bytes values to
3487 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003488 * for details. Otherwise, default to 4 bits for large page
3489 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003490 */
3491 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3492 chip->ecc.size = 512;
3493 chip->ecc.bytes = 7;
3494 }
3495 chip->ecc.priv = nand_bch_init(mtd,
3496 chip->ecc.size,
3497 chip->ecc.bytes,
3498 &chip->ecc.layout);
3499 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003500 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003501 BUG();
3502 }
Mike Dunn6a918ba2012-03-11 14:21:11 -07003503 chip->ecc.strength =
Mike Dunne2788c92012-04-25 12:06:10 -07003504 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003505 break;
3506
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003507 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003508 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003509 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003510 chip->ecc.read_page = nand_read_page_raw;
3511 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003512 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003513 chip->ecc.read_page_raw = nand_read_page_raw;
3514 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003515 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003516 chip->ecc.size = mtd->writesize;
3517 chip->ecc.bytes = 0;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003518 chip->ecc.strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003522 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003523 BUG();
3524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Brian Norris9ce244b2011-08-30 18:45:37 -07003526 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003527 if (!chip->ecc.read_oob_raw)
3528 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003529 if (!chip->ecc.write_oob_raw)
3530 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3531
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003532 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003533 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003534 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003535 */
3536 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003537 for (i = 0; chip->ecc.layout->oobfree[i].length
3538 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003539 chip->ecc.layout->oobavail +=
3540 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003541 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003542
3543 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003544 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003545 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003546 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003547 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003548 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003549 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003550 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003552 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003553
Brian Norris8b6e50c2011-05-25 14:59:01 -07003554 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003555 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3556 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003557 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003558 case 2:
3559 mtd->subpage_sft = 1;
3560 break;
3561 case 4:
3562 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003563 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003564 mtd->subpage_sft = 2;
3565 break;
3566 }
3567 }
3568 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3569
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003570 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003571 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003574 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003576 /* Large page NAND with SOFT_ECC should support subpage reads */
3577 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3578 chip->options |= NAND_SUBPAGE_READ;
3579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 /* Fill in remaining MTD driver data */
3581 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003582 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3583 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003584 mtd->_erase = nand_erase;
3585 mtd->_point = NULL;
3586 mtd->_unpoint = NULL;
3587 mtd->_read = nand_read;
3588 mtd->_write = nand_write;
3589 mtd->_panic_write = panic_nand_write;
3590 mtd->_read_oob = nand_read_oob;
3591 mtd->_write_oob = nand_write_oob;
3592 mtd->_sync = nand_sync;
3593 mtd->_lock = NULL;
3594 mtd->_unlock = NULL;
3595 mtd->_suspend = nand_suspend;
3596 mtd->_resume = nand_resume;
3597 mtd->_block_isbad = nand_block_isbad;
3598 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003599 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Mike Dunn6a918ba2012-03-11 14:21:11 -07003601 /* propagate ecc info to mtd_info */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003602 mtd->ecclayout = chip->ecc.layout;
Mike Dunn86c20722012-04-25 12:06:05 -07003603 mtd->ecc_strength = chip->ecc.strength;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003604 /*
3605 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3606 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3607 * properly set.
3608 */
3609 if (!mtd->bitflip_threshold)
3610 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003612 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003613 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
3616 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003617 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003619EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Brian Norris8b6e50c2011-05-25 14:59:01 -07003621/*
3622 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003623 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003624 * to call us from in-kernel code if the core NAND support is modular.
3625 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003626#ifdef MODULE
3627#define caller_is_module() (1)
3628#else
3629#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003630 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003631#endif
3632
3633/**
3634 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003635 * @mtd: MTD device structure
3636 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003637 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003638 * This fills out all the uninitialized function pointers with the defaults.
3639 * The flash ID is read and the mtd/chip structures are filled with the
3640 * appropriate values. The mtd->owner field must be set to the module of the
3641 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003642 */
3643int nand_scan(struct mtd_info *mtd, int maxchips)
3644{
3645 int ret;
3646
3647 /* Many callers got this wrong, so check for it for a while... */
3648 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003649 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003650 BUG();
3651 }
3652
David Woodhouse5e81e882010-02-26 18:32:56 +00003653 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003654 if (!ret)
3655 ret = nand_scan_tail(mtd);
3656 return ret;
3657}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003658EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003661 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003662 * @mtd: MTD device structure
3663 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003664void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003666 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Ivan Djelic193bd402011-03-11 11:05:33 +01003668 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3669 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3670
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003671 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Jesper Juhlfa671642005-11-07 01:01:27 -08003673 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003674 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003675 if (!(chip->options & NAND_OWN_BUFFERS))
3676 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003677
3678 /* Free bad block descriptor memory */
3679 if (chip->badblock_pattern && chip->badblock_pattern->options
3680 & NAND_BBT_DYNAMICSTRUCT)
3681 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
David Woodhousee0c7d762006-05-13 18:07:53 +01003683EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003684
3685static int __init nand_base_init(void)
3686{
3687 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3688 return 0;
3689}
3690
3691static void __exit nand_base_exit(void)
3692{
3693 led_trigger_unregister_simple(nand_led_trigger);
3694}
3695
3696module_init(nand_base_init);
3697module_exit(nand_base_exit);
3698
David Woodhousee0c7d762006-05-13 18:07:53 +01003699MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003700MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3701MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003702MODULE_DESCRIPTION("Generic NAND flash driver code");