blob: 11ee9d47f53ef4d506347c10433cf94548d43c93 [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
515 * (256/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 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200631 chip->cmd_ctrl(mtd, command & 0xff,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200632 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200635 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* Serially input address */
638 if (column != -1) {
639 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200640 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200643 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200647 chip->cmd_ctrl(mtd, page_addr, ctrl);
648 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200649 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200651 if (chip->chipsize > (128 << 20))
652 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200653 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200656 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000657
658 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700659 * Program and erase have their own busy handlers status, sequential
660 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 case NAND_CMD_CACHEDPROG:
665 case NAND_CMD_PAGEPROG:
666 case NAND_CMD_ERASE1:
667 case NAND_CMD_ERASE2:
668 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200669 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200674 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200676 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200677 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
678 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
679 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
680 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200681 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
682 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return;
684
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200685 case NAND_CMD_RNDOUT:
686 /* No ready / busy check necessary */
687 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
688 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
689 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
690 NAND_NCE | NAND_CTRL_CHANGE);
691 return;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200694 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
695 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
696 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
697 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000698
David Woodhousee0c7d762006-05-13 18:07:53 +0100699 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700703 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100704 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200705 if (!chip->dev_ready) {
706 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000710
Brian Norris8b6e50c2011-05-25 14:59:01 -0700711 /*
712 * Apply this short delay always to ensure that we do wait tWB in
713 * any case on any machine.
714 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100715 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000716
717 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200721 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700722 * @chip: the nand chip descriptor
723 * @mtd: MTD device structure
724 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200725 *
726 * Used when in panic, no locks are taken.
727 */
728static void panic_nand_get_device(struct nand_chip *chip,
729 struct mtd_info *mtd, int new_state)
730{
Brian Norris7854d3f2011-06-23 14:12:08 -0700731 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200732 chip->controller->active = chip;
733 chip->state = new_state;
734}
735
736/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700738 * @mtd: MTD device structure
739 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 *
741 * Get the device and lock it for exclusive access
742 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200743static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800744nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800746 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200747 spinlock_t *lock = &chip->controller->lock;
748 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200750retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100751 spin_lock(lock);
752
vimal singhb8b3ee92009-07-09 20:41:22 +0530753 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200754 if (!chip->controller->active)
755 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200756
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200757 if (chip->controller->active == chip && chip->state == FL_READY) {
758 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100759 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100760 return 0;
761 }
762 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800763 if (chip->controller->active->state == FL_PM_SUSPENDED) {
764 chip->state = FL_PM_SUSPENDED;
765 spin_unlock(lock);
766 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800767 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100768 }
769 set_current_state(TASK_UNINTERRUPTIBLE);
770 add_wait_queue(wq, &wait);
771 spin_unlock(lock);
772 schedule();
773 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto retry;
775}
776
777/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700778 * panic_nand_wait - [GENERIC] wait until the command is done
779 * @mtd: MTD device structure
780 * @chip: NAND chip structure
781 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200782 *
783 * Wait for command done. This is a helper function for nand_wait used when
784 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400785 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200786 */
787static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
788 unsigned long timeo)
789{
790 int i;
791 for (i = 0; i < timeo; i++) {
792 if (chip->dev_ready) {
793 if (chip->dev_ready(mtd))
794 break;
795 } else {
796 if (chip->read_byte(mtd) & NAND_STATUS_READY)
797 break;
798 }
799 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200800 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200801}
802
803/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700804 * nand_wait - [DEFAULT] wait until the command is done
805 * @mtd: MTD device structure
806 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700808 * Wait for command done. This applies to erase and program only. Erase can
809 * take up to 400ms and program up to 20ms according to general NAND and
810 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700811 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200812static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200815 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800816 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Richard Purdie8fe833c2006-03-31 02:31:14 -0800818 led_trigger_event(nand_led_trigger, LED_FULL);
819
Brian Norris8b6e50c2011-05-25 14:59:01 -0700820 /*
821 * Apply this short delay always to ensure that we do wait tWB in any
822 * case on any machine.
823 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100824 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200826 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200828 if (in_interrupt() || oops_in_progress)
829 panic_nand_wait(mtd, chip, timeo);
830 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800831 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200832 while (time_before(jiffies, timeo)) {
833 if (chip->dev_ready) {
834 if (chip->dev_ready(mtd))
835 break;
836 } else {
837 if (chip->read_byte(mtd) & NAND_STATUS_READY)
838 break;
839 }
840 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800843 led_trigger_event(nand_led_trigger, LED_OFF);
844
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200845 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100846 /* This can happen if in case of timeout or buggy dev_ready */
847 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return status;
849}
850
851/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700852 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700853 * @mtd: mtd info
854 * @ofs: offset to start unlock from
855 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700856 * @invert: when = 0, unlock the range of blocks within the lower and
857 * upper boundary address
858 * when = 1, unlock the range of blocks outside the boundaries
859 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530860 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700861 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530862 */
863static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
864 uint64_t len, int invert)
865{
866 int ret = 0;
867 int status, page;
868 struct nand_chip *chip = mtd->priv;
869
870 /* Submit address of first page to unlock */
871 page = ofs >> chip->page_shift;
872 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
873
874 /* Submit address of last page to unlock */
875 page = (ofs + len) >> chip->page_shift;
876 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
877 (page | invert) & chip->pagemask);
878
879 /* Call wait ready function */
880 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530881 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400882 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700883 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530884 __func__, status);
885 ret = -EIO;
886 }
887
888 return ret;
889}
890
891/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700892 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700893 * @mtd: mtd info
894 * @ofs: offset to start unlock from
895 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530896 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700897 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530898 */
899int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
900{
901 int ret = 0;
902 int chipnr;
903 struct nand_chip *chip = mtd->priv;
904
Brian Norris289c0522011-07-19 10:06:09 -0700905 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530906 __func__, (unsigned long long)ofs, len);
907
908 if (check_offs_len(mtd, ofs, len))
909 ret = -EINVAL;
910
911 /* Align to last block address if size addresses end of the device */
912 if (ofs + len == mtd->size)
913 len -= mtd->erasesize;
914
Huang Shijie6a8214a2012-11-19 14:43:30 +0800915 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530916
917 /* Shift to get chip number */
918 chipnr = ofs >> chip->chip_shift;
919
920 chip->select_chip(mtd, chipnr);
921
922 /* Check, if it is write protected */
923 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700924 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530925 __func__);
926 ret = -EIO;
927 goto out;
928 }
929
930 ret = __nand_unlock(mtd, ofs, len, 0);
931
932out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800933 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530934 nand_release_device(mtd);
935
936 return ret;
937}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200938EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530939
940/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700941 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700942 * @mtd: mtd info
943 * @ofs: offset to start unlock from
944 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530945 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700946 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
947 * have this feature, but it allows only to lock all blocks, not for specified
948 * range for block. Implementing 'lock' feature by making use of 'unlock', for
949 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530950 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700951 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530952 */
953int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
954{
955 int ret = 0;
956 int chipnr, status, page;
957 struct nand_chip *chip = mtd->priv;
958
Brian Norris289c0522011-07-19 10:06:09 -0700959 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530960 __func__, (unsigned long long)ofs, len);
961
962 if (check_offs_len(mtd, ofs, len))
963 ret = -EINVAL;
964
Huang Shijie6a8214a2012-11-19 14:43:30 +0800965 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530966
967 /* Shift to get chip number */
968 chipnr = ofs >> chip->chip_shift;
969
970 chip->select_chip(mtd, chipnr);
971
972 /* Check, if it is write protected */
973 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700974 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530975 __func__);
976 status = MTD_ERASE_FAILED;
977 ret = -EIO;
978 goto out;
979 }
980
981 /* Submit address of first page to lock */
982 page = ofs >> chip->page_shift;
983 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
984
985 /* Call wait ready function */
986 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530987 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400988 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700989 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530990 __func__, status);
991 ret = -EIO;
992 goto out;
993 }
994
995 ret = __nand_unlock(mtd, ofs, len, 0x1);
996
997out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800998 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530999 nand_release_device(mtd);
1000
1001 return ret;
1002}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001003EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301004
1005/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001006 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001007 * @mtd: mtd info structure
1008 * @chip: nand chip info structure
1009 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001010 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001011 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001012 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001013 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001014 */
1015static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001016 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001017{
1018 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001019 if (oob_required)
1020 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001021 return 0;
1022}
1023
1024/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001025 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001026 * @mtd: mtd info structure
1027 * @chip: nand chip info structure
1028 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001029 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001030 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001031 *
1032 * We need a special oob layout and handling even when OOB isn't used.
1033 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001034static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001035 struct nand_chip *chip, uint8_t *buf,
1036 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001037{
1038 int eccsize = chip->ecc.size;
1039 int eccbytes = chip->ecc.bytes;
1040 uint8_t *oob = chip->oob_poi;
1041 int steps, size;
1042
1043 for (steps = chip->ecc.steps; steps > 0; steps--) {
1044 chip->read_buf(mtd, buf, eccsize);
1045 buf += eccsize;
1046
1047 if (chip->ecc.prepad) {
1048 chip->read_buf(mtd, oob, chip->ecc.prepad);
1049 oob += chip->ecc.prepad;
1050 }
1051
1052 chip->read_buf(mtd, oob, eccbytes);
1053 oob += eccbytes;
1054
1055 if (chip->ecc.postpad) {
1056 chip->read_buf(mtd, oob, chip->ecc.postpad);
1057 oob += chip->ecc.postpad;
1058 }
1059 }
1060
1061 size = mtd->oobsize - (oob - chip->oob_poi);
1062 if (size)
1063 chip->read_buf(mtd, oob, size);
1064
1065 return 0;
1066}
1067
1068/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001069 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001070 * @mtd: mtd info structure
1071 * @chip: nand chip info structure
1072 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001073 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001074 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001075 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001076static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001077 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001079 int i, eccsize = chip->ecc.size;
1080 int eccbytes = chip->ecc.bytes;
1081 int eccsteps = chip->ecc.steps;
1082 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001083 uint8_t *ecc_calc = chip->buffers->ecccalc;
1084 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001085 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001086 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001087
Brian Norris1fbb9382012-05-02 10:14:55 -07001088 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001089
1090 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1091 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1092
1093 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001094 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001095
1096 eccsteps = chip->ecc.steps;
1097 p = buf;
1098
1099 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1100 int stat;
1101
1102 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001103 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001104 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001105 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001106 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001107 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1108 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001109 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001110 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001114 * nand_read_subpage - [REPLACEABLE] software ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001115 * @mtd: mtd info structure
1116 * @chip: nand chip info structure
1117 * @data_offs: offset of requested data within the page
1118 * @readlen: data length
1119 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001120 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001121static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1122 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001123{
1124 int start_step, end_step, num_steps;
1125 uint32_t *eccpos = chip->ecc.layout->eccpos;
1126 uint8_t *p;
1127 int data_col_addr, i, gaps = 0;
1128 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1129 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001130 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001131 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001132
Brian Norris7854d3f2011-06-23 14:12:08 -07001133 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001134 start_step = data_offs / chip->ecc.size;
1135 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1136 num_steps = end_step - start_step + 1;
1137
Brian Norris8b6e50c2011-05-25 14:59:01 -07001138 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001139 datafrag_len = num_steps * chip->ecc.size;
1140 eccfrag_len = num_steps * chip->ecc.bytes;
1141
1142 data_col_addr = start_step * chip->ecc.size;
1143 /* If we read not a page aligned data */
1144 if (data_col_addr != 0)
1145 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1146
1147 p = bufpoi + data_col_addr;
1148 chip->read_buf(mtd, p, datafrag_len);
1149
Brian Norris8b6e50c2011-05-25 14:59:01 -07001150 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001151 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1152 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1153
Brian Norris8b6e50c2011-05-25 14:59:01 -07001154 /*
1155 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001156 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001157 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001158 for (i = 0; i < eccfrag_len - 1; i++) {
1159 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1160 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1161 gaps = 1;
1162 break;
1163 }
1164 }
1165 if (gaps) {
1166 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1167 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1168 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001169 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001170 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001171 * about buswidth alignment in read_buf.
1172 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001173 index = start_step * chip->ecc.bytes;
1174
1175 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001176 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001177 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001178 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001179 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001180 aligned_len++;
1181
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001182 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1183 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001184 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1185 }
1186
1187 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001188 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001189
1190 p = bufpoi + data_col_addr;
1191 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1192 int stat;
1193
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001194 stat = chip->ecc.correct(mtd, p,
1195 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001196 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001197 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001198 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001199 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001200 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1201 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001202 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001203 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001204}
1205
1206/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001207 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001208 * @mtd: mtd info structure
1209 * @chip: nand chip info structure
1210 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001211 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001212 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001213 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001214 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001215 */
1216static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001217 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001218{
1219 int i, eccsize = chip->ecc.size;
1220 int eccbytes = chip->ecc.bytes;
1221 int eccsteps = chip->ecc.steps;
1222 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001223 uint8_t *ecc_calc = chip->buffers->ecccalc;
1224 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001225 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001226 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001227
1228 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1229 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1230 chip->read_buf(mtd, p, eccsize);
1231 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1232 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001233 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001234
1235 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001236 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001237
1238 eccsteps = chip->ecc.steps;
1239 p = buf;
1240
1241 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1242 int stat;
1243
1244 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001245 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001246 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001247 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001248 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001249 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1250 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001251 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001252 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001253}
1254
1255/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001256 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001257 * @mtd: mtd info structure
1258 * @chip: nand chip info structure
1259 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001260 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001261 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001262 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001263 * Hardware ECC for large page chips, require OOB to be read first. For this
1264 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1265 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1266 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1267 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001268 */
1269static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001270 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001271{
1272 int i, eccsize = chip->ecc.size;
1273 int eccbytes = chip->ecc.bytes;
1274 int eccsteps = chip->ecc.steps;
1275 uint8_t *p = buf;
1276 uint8_t *ecc_code = chip->buffers->ecccode;
1277 uint32_t *eccpos = chip->ecc.layout->eccpos;
1278 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001279 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001280
1281 /* Read the OOB area first */
1282 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1283 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1284 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1285
1286 for (i = 0; i < chip->ecc.total; i++)
1287 ecc_code[i] = chip->oob_poi[eccpos[i]];
1288
1289 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1290 int stat;
1291
1292 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1293 chip->read_buf(mtd, p, eccsize);
1294 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1295
1296 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001297 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001298 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001299 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001300 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001301 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1302 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001303 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001304 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001305}
1306
1307/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001308 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001309 * @mtd: mtd info structure
1310 * @chip: nand chip info structure
1311 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001312 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001313 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001314 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001315 * The hw generator calculates the error syndrome automatically. Therefore we
1316 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001317 */
1318static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001319 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001320{
1321 int i, eccsize = chip->ecc.size;
1322 int eccbytes = chip->ecc.bytes;
1323 int eccsteps = chip->ecc.steps;
1324 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001325 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001326 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001327
1328 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1329 int stat;
1330
1331 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1332 chip->read_buf(mtd, p, eccsize);
1333
1334 if (chip->ecc.prepad) {
1335 chip->read_buf(mtd, oob, chip->ecc.prepad);
1336 oob += chip->ecc.prepad;
1337 }
1338
1339 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1340 chip->read_buf(mtd, oob, eccbytes);
1341 stat = chip->ecc.correct(mtd, p, oob, NULL);
1342
Mike Dunn3f91e942012-04-25 12:06:09 -07001343 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001344 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001345 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001346 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1348 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001349
1350 oob += eccbytes;
1351
1352 if (chip->ecc.postpad) {
1353 chip->read_buf(mtd, oob, chip->ecc.postpad);
1354 oob += chip->ecc.postpad;
1355 }
1356 }
1357
1358 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001359 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001360 if (i)
1361 chip->read_buf(mtd, oob, i);
1362
Mike Dunn3f91e942012-04-25 12:06:09 -07001363 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001364}
1365
1366/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001367 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001368 * @chip: nand chip structure
1369 * @oob: oob destination address
1370 * @ops: oob ops structure
1371 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001372 */
1373static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001374 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001375{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001376 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001377
Brian Norris0612b9d2011-08-30 18:45:40 -07001378 case MTD_OPS_PLACE_OOB:
1379 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001380 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1381 return oob + len;
1382
Brian Norris0612b9d2011-08-30 18:45:40 -07001383 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001384 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001385 uint32_t boffs = 0, roffs = ops->ooboffs;
1386 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001387
Florian Fainellif8ac0412010-09-07 13:23:43 +02001388 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001389 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001390 if (unlikely(roffs)) {
1391 if (roffs >= free->length) {
1392 roffs -= free->length;
1393 continue;
1394 }
1395 boffs = free->offset + roffs;
1396 bytes = min_t(size_t, len,
1397 (free->length - roffs));
1398 roffs = 0;
1399 } else {
1400 bytes = min_t(size_t, len, free->length);
1401 boffs = free->offset;
1402 }
1403 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001404 oob += bytes;
1405 }
1406 return oob;
1407 }
1408 default:
1409 BUG();
1410 }
1411 return NULL;
1412}
1413
1414/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001415 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001416 * @mtd: MTD device structure
1417 * @from: offset to read from
1418 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001419 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001420 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001421 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001422static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1423 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001424{
Brian Norrise47f3db2012-05-02 10:14:56 -07001425 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001426 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001427 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001428 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001429 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001430 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001431 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001432 mtd->oobavail : mtd->oobsize;
1433
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001434 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001435 unsigned int max_bitflips = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001437 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001439 chipnr = (int)(from >> chip->chip_shift);
1440 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001442 realpage = (int)(from >> chip->page_shift);
1443 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001445 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001447 buf = ops->datbuf;
1448 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001449 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450
Florian Fainellif8ac0412010-09-07 13:23:43 +02001451 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001452 bytes = min(mtd->writesize - col, readlen);
1453 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001454
Brian Norris8b6e50c2011-05-25 14:59:01 -07001455 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001456 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001457 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Brian Norrisc00a0992012-05-01 17:12:54 -07001459 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Mike Dunnedbc45402012-04-25 12:06:11 -07001461 /*
1462 * Now read the page into the buffer. Absent an error,
1463 * the read methods return max bitflips per ecc step.
1464 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001465 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001466 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001467 oob_required,
1468 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001469 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1470 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001471 ret = chip->ecc.read_subpage(mtd, chip,
1472 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001473 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001474 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001475 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001476 if (ret < 0) {
1477 if (!aligned)
1478 /* Invalidate page cache */
1479 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001480 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001481 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001482
Mike Dunnedbc45402012-04-25 12:06:11 -07001483 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1484
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001485 /* Transfer not aligned data */
1486 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001487 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001488 !(mtd->ecc_stats.failed - stats.failed) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001489 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001490 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001491 chip->pagebuf_bitflips = ret;
1492 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001493 /* Invalidate page cache */
1494 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001495 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001496 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001498
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001499 buf += bytes;
1500
1501 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001502 int toread = min(oobreadlen, max_oobsize);
1503
1504 if (toread) {
1505 oob = nand_transfer_oob(chip,
1506 oob, ops, toread);
1507 oobreadlen -= toread;
1508 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001509 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001510
1511 if (chip->options & NAND_NEED_READRDY) {
1512 /* Apply delay or wait for ready/busy pin */
1513 if (!chip->dev_ready)
1514 udelay(chip->chip_delay);
1515 else
1516 nand_wait_ready(mtd);
1517 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001518 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001519 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001520 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001521 max_bitflips = max_t(unsigned int, max_bitflips,
1522 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001525 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001526
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001527 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Brian Norris8b6e50c2011-05-25 14:59:01 -07001530 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 col = 0;
1532 /* Increment page address */
1533 realpage++;
1534
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001535 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /* Check, if we cross a chip boundary */
1537 if (!page) {
1538 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001539 chip->select_chip(mtd, -1);
1540 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001543 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001545 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001546 if (oob)
1547 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Mike Dunn3f91e942012-04-25 12:06:09 -07001549 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001550 return ret;
1551
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001552 if (mtd->ecc_stats.failed - stats.failed)
1553 return -EBADMSG;
1554
Mike Dunnedbc45402012-04-25 12:06:11 -07001555 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001556}
1557
1558/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001559 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001560 * @mtd: MTD device structure
1561 * @from: offset to read from
1562 * @len: number of bytes to read
1563 * @retlen: pointer to variable to store the number of read bytes
1564 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001565 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001566 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001567 */
1568static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1569 size_t *retlen, uint8_t *buf)
1570{
Brian Norris4a89ff82011-08-30 18:45:45 -07001571 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001572 int ret;
1573
Huang Shijie6a8214a2012-11-19 14:43:30 +08001574 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001575 ops.len = len;
1576 ops.datbuf = buf;
1577 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001578 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001579 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001580 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001581 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001582 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001586 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001587 * @mtd: mtd info structure
1588 * @chip: nand chip info structure
1589 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001590 */
1591static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001592 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001593{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001594 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001595 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001596 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001597}
1598
1599/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001600 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001601 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001602 * @mtd: mtd info structure
1603 * @chip: nand chip info structure
1604 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001605 */
1606static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001607 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001608{
1609 uint8_t *buf = chip->oob_poi;
1610 int length = mtd->oobsize;
1611 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1612 int eccsize = chip->ecc.size;
1613 uint8_t *bufpoi = buf;
1614 int i, toread, sndrnd = 0, pos;
1615
1616 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1617 for (i = 0; i < chip->ecc.steps; i++) {
1618 if (sndrnd) {
1619 pos = eccsize + i * (eccsize + chunk);
1620 if (mtd->writesize > 512)
1621 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1622 else
1623 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1624 } else
1625 sndrnd = 1;
1626 toread = min_t(int, length, chunk);
1627 chip->read_buf(mtd, bufpoi, toread);
1628 bufpoi += toread;
1629 length -= toread;
1630 }
1631 if (length > 0)
1632 chip->read_buf(mtd, bufpoi, length);
1633
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001634 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001635}
1636
1637/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001638 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001639 * @mtd: mtd info structure
1640 * @chip: nand chip info structure
1641 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001642 */
1643static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1644 int page)
1645{
1646 int status = 0;
1647 const uint8_t *buf = chip->oob_poi;
1648 int length = mtd->oobsize;
1649
1650 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1651 chip->write_buf(mtd, buf, length);
1652 /* Send command to program the OOB data */
1653 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1654
1655 status = chip->waitfunc(mtd, chip);
1656
Savin Zlobec0d420f92006-06-21 11:51:20 +02001657 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001658}
1659
1660/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001661 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001662 * with syndrome - only for large page flash
1663 * @mtd: mtd info structure
1664 * @chip: nand chip info structure
1665 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001666 */
1667static int nand_write_oob_syndrome(struct mtd_info *mtd,
1668 struct nand_chip *chip, int page)
1669{
1670 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1671 int eccsize = chip->ecc.size, length = mtd->oobsize;
1672 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1673 const uint8_t *bufpoi = chip->oob_poi;
1674
1675 /*
1676 * data-ecc-data-ecc ... ecc-oob
1677 * or
1678 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1679 */
1680 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1681 pos = steps * (eccsize + chunk);
1682 steps = 0;
1683 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001684 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001685
1686 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1687 for (i = 0; i < steps; i++) {
1688 if (sndcmd) {
1689 if (mtd->writesize <= 512) {
1690 uint32_t fill = 0xFFFFFFFF;
1691
1692 len = eccsize;
1693 while (len > 0) {
1694 int num = min_t(int, len, 4);
1695 chip->write_buf(mtd, (uint8_t *)&fill,
1696 num);
1697 len -= num;
1698 }
1699 } else {
1700 pos = eccsize + i * (eccsize + chunk);
1701 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1702 }
1703 } else
1704 sndcmd = 1;
1705 len = min_t(int, length, chunk);
1706 chip->write_buf(mtd, bufpoi, len);
1707 bufpoi += len;
1708 length -= len;
1709 }
1710 if (length > 0)
1711 chip->write_buf(mtd, bufpoi, length);
1712
1713 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1714 status = chip->waitfunc(mtd, chip);
1715
1716 return status & NAND_STATUS_FAIL ? -EIO : 0;
1717}
1718
1719/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001720 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001721 * @mtd: MTD device structure
1722 * @from: offset to read from
1723 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001725 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001727static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1728 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Brian Norrisc00a0992012-05-01 17:12:54 -07001730 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001731 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001732 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001733 int readlen = ops->ooblen;
1734 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001735 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001736 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Brian Norris289c0522011-07-19 10:06:09 -07001738 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301739 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Brian Norris041e4572011-06-23 16:45:24 -07001741 stats = mtd->ecc_stats;
1742
Brian Norris0612b9d2011-08-30 18:45:40 -07001743 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001744 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001745 else
1746 len = mtd->oobsize;
1747
1748 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001749 pr_debug("%s: attempt to start read outside oob\n",
1750 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001751 return -EINVAL;
1752 }
1753
1754 /* Do not allow reads past end of device */
1755 if (unlikely(from >= mtd->size ||
1756 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1757 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001758 pr_debug("%s: attempt to read beyond end of device\n",
1759 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001760 return -EINVAL;
1761 }
Vitaly Wool70145682006-11-03 18:20:38 +03001762
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001763 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001764 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001766 /* Shift to get page */
1767 realpage = (int)(from >> chip->page_shift);
1768 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Florian Fainellif8ac0412010-09-07 13:23:43 +02001770 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001771 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001772 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001773 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001774 ret = chip->ecc.read_oob(mtd, chip, page);
1775
1776 if (ret < 0)
1777 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001778
1779 len = min(len, readlen);
1780 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001781
Brian Norris5bc7c332013-03-13 09:51:31 -07001782 if (chip->options & NAND_NEED_READRDY) {
1783 /* Apply delay or wait for ready/busy pin */
1784 if (!chip->dev_ready)
1785 udelay(chip->chip_delay);
1786 else
1787 nand_wait_ready(mtd);
1788 }
1789
Vitaly Wool70145682006-11-03 18:20:38 +03001790 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001791 if (!readlen)
1792 break;
1793
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001794 /* Increment page address */
1795 realpage++;
1796
1797 page = realpage & chip->pagemask;
1798 /* Check, if we cross a chip boundary */
1799 if (!page) {
1800 chipnr++;
1801 chip->select_chip(mtd, -1);
1802 chip->select_chip(mtd, chipnr);
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001805 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001807 ops->oobretlen = ops->ooblen - readlen;
1808
1809 if (ret < 0)
1810 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001811
1812 if (mtd->ecc_stats.failed - stats.failed)
1813 return -EBADMSG;
1814
1815 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001819 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001820 * @mtd: MTD device structure
1821 * @from: offset to read from
1822 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001824 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001826static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1827 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001829 int ret = -ENOTSUPP;
1830
1831 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001834 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001835 pr_debug("%s: attempt to read beyond end of device\n",
1836 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return -EINVAL;
1838 }
1839
Huang Shijie6a8214a2012-11-19 14:43:30 +08001840 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Florian Fainellif8ac0412010-09-07 13:23:43 +02001842 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001843 case MTD_OPS_PLACE_OOB:
1844 case MTD_OPS_AUTO_OOB:
1845 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001846 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001847
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001848 default:
1849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001852 if (!ops->datbuf)
1853 ret = nand_do_read_oob(mtd, from, ops);
1854 else
1855 ret = nand_do_read_ops(mtd, from, ops);
1856
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001857out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001859 return ret;
1860}
1861
1862
1863/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001864 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001865 * @mtd: mtd info structure
1866 * @chip: nand chip info structure
1867 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001868 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001869 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001870 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001871 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001872static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001873 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001874{
1875 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001876 if (oob_required)
1877 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001878
1879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001882/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001883 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001884 * @mtd: mtd info structure
1885 * @chip: nand chip info structure
1886 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001887 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001888 *
1889 * We need a special oob layout and handling even when ECC isn't checked.
1890 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001891static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001892 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001893 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001894{
1895 int eccsize = chip->ecc.size;
1896 int eccbytes = chip->ecc.bytes;
1897 uint8_t *oob = chip->oob_poi;
1898 int steps, size;
1899
1900 for (steps = chip->ecc.steps; steps > 0; steps--) {
1901 chip->write_buf(mtd, buf, eccsize);
1902 buf += eccsize;
1903
1904 if (chip->ecc.prepad) {
1905 chip->write_buf(mtd, oob, chip->ecc.prepad);
1906 oob += chip->ecc.prepad;
1907 }
1908
1909 chip->read_buf(mtd, oob, eccbytes);
1910 oob += eccbytes;
1911
1912 if (chip->ecc.postpad) {
1913 chip->write_buf(mtd, oob, chip->ecc.postpad);
1914 oob += chip->ecc.postpad;
1915 }
1916 }
1917
1918 size = mtd->oobsize - (oob - chip->oob_poi);
1919 if (size)
1920 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001921
1922 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001923}
1924/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001925 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001926 * @mtd: mtd info structure
1927 * @chip: nand chip info structure
1928 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001929 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001930 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001931static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001932 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001933{
1934 int i, eccsize = chip->ecc.size;
1935 int eccbytes = chip->ecc.bytes;
1936 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001937 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001938 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001939 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001940
Brian Norris7854d3f2011-06-23 14:12:08 -07001941 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001942 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1943 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 for (i = 0; i < chip->ecc.total; i++)
1946 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001947
Josh Wufdbad98d2012-06-25 18:07:45 +08001948 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001949}
1950
1951/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001952 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001953 * @mtd: mtd info structure
1954 * @chip: nand chip info structure
1955 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001956 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001957 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001958static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001959 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001960{
1961 int i, eccsize = chip->ecc.size;
1962 int eccbytes = chip->ecc.bytes;
1963 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001964 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001965 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001966 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001967
1968 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1969 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001970 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001971 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1972 }
1973
1974 for (i = 0; i < chip->ecc.total; i++)
1975 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1976
1977 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001978
1979 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001980}
1981
1982/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001983 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07001984 * @mtd: mtd info structure
1985 * @chip: nand chip info structure
1986 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001987 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001988 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001989 * The hw generator calculates the error syndrome automatically. Therefore we
1990 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001991 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001992static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001993 struct nand_chip *chip,
1994 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001995{
1996 int i, eccsize = chip->ecc.size;
1997 int eccbytes = chip->ecc.bytes;
1998 int eccsteps = chip->ecc.steps;
1999 const uint8_t *p = buf;
2000 uint8_t *oob = chip->oob_poi;
2001
2002 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2003
2004 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2005 chip->write_buf(mtd, p, eccsize);
2006
2007 if (chip->ecc.prepad) {
2008 chip->write_buf(mtd, oob, chip->ecc.prepad);
2009 oob += chip->ecc.prepad;
2010 }
2011
2012 chip->ecc.calculate(mtd, p, oob);
2013 chip->write_buf(mtd, oob, eccbytes);
2014 oob += eccbytes;
2015
2016 if (chip->ecc.postpad) {
2017 chip->write_buf(mtd, oob, chip->ecc.postpad);
2018 oob += chip->ecc.postpad;
2019 }
2020 }
2021
2022 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002023 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002024 if (i)
2025 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002026
2027 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002028}
2029
2030/**
David Woodhouse956e9442006-09-25 17:12:39 +01002031 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002032 * @mtd: MTD device structure
2033 * @chip: NAND chip descriptor
2034 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002035 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002036 * @page: page number to write
2037 * @cached: cached programming
2038 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002039 */
2040static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002041 const uint8_t *buf, int oob_required, int page,
2042 int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002043{
2044 int status;
2045
2046 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2047
David Woodhouse956e9442006-09-25 17:12:39 +01002048 if (unlikely(raw))
Josh Wufdbad98d2012-06-25 18:07:45 +08002049 status = chip->ecc.write_page_raw(mtd, chip, buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002050 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002051 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2052
2053 if (status < 0)
2054 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002055
2056 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002057 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002058 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002059 */
2060 cached = 0;
2061
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002062 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002063
2064 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002065 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002066 /*
2067 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002068 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002069 */
2070 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2071 status = chip->errstat(mtd, chip, FL_WRITING, status,
2072 page);
2073
2074 if (status & NAND_STATUS_FAIL)
2075 return -EIO;
2076 } else {
2077 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002078 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002079 }
2080
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002081 return 0;
2082}
2083
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002084/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002085 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002086 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002087 * @oob: oob data buffer
2088 * @len: oob data write length
2089 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002090 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002091static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2092 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002093{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002094 struct nand_chip *chip = mtd->priv;
2095
2096 /*
2097 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2098 * data from a previous OOB read.
2099 */
2100 memset(chip->oob_poi, 0xff, mtd->oobsize);
2101
Florian Fainellif8ac0412010-09-07 13:23:43 +02002102 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002103
Brian Norris0612b9d2011-08-30 18:45:40 -07002104 case MTD_OPS_PLACE_OOB:
2105 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002106 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2107 return oob + len;
2108
Brian Norris0612b9d2011-08-30 18:45:40 -07002109 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002110 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002111 uint32_t boffs = 0, woffs = ops->ooboffs;
2112 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002113
Florian Fainellif8ac0412010-09-07 13:23:43 +02002114 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002115 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002116 if (unlikely(woffs)) {
2117 if (woffs >= free->length) {
2118 woffs -= free->length;
2119 continue;
2120 }
2121 boffs = free->offset + woffs;
2122 bytes = min_t(size_t, len,
2123 (free->length - woffs));
2124 woffs = 0;
2125 } else {
2126 bytes = min_t(size_t, len, free->length);
2127 boffs = free->offset;
2128 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002129 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002130 oob += bytes;
2131 }
2132 return oob;
2133 }
2134 default:
2135 BUG();
2136 }
2137 return NULL;
2138}
2139
Florian Fainellif8ac0412010-09-07 13:23:43 +02002140#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002141
2142/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002143 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002144 * @mtd: MTD device structure
2145 * @to: offset to write to
2146 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002147 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002148 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002149 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002150static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2151 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002152{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002153 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002155 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002156
2157 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002158 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002159 mtd->oobavail : mtd->oobsize;
2160
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002161 uint8_t *oob = ops->oobbuf;
2162 uint8_t *buf = ops->datbuf;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002163 int ret, subpage;
Brian Norrise47f3db2012-05-02 10:14:56 -07002164 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002165
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002166 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002167 if (!writelen)
2168 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002169
Brian Norris8b6e50c2011-05-25 14:59:01 -07002170 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002171 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002172 pr_notice("%s: attempt to write non page aligned data\n",
2173 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002174 return -EINVAL;
2175 }
2176
Thomas Gleixner29072b92006-09-28 15:38:36 +02002177 column = to & (mtd->writesize - 1);
2178 subpage = column || (writelen & (mtd->writesize - 1));
2179
2180 if (subpage && oob)
2181 return -EINVAL;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002182
Thomas Gleixner6a930962006-06-28 00:11:45 +02002183 chipnr = (int)(to >> chip->chip_shift);
2184 chip->select_chip(mtd, chipnr);
2185
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002186 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002187 if (nand_check_wp(mtd)) {
2188 ret = -EIO;
2189 goto err_out;
2190 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002191
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002192 realpage = (int)(to >> chip->page_shift);
2193 page = realpage & chip->pagemask;
2194 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2195
2196 /* Invalidate the page cache, when we write to the cached page */
2197 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002198 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002199 chip->pagebuf = -1;
2200
Maxim Levitsky782ce792010-02-22 20:39:36 +02002201 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002202 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2203 ret = -EINVAL;
2204 goto err_out;
2205 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002206
Florian Fainellif8ac0412010-09-07 13:23:43 +02002207 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002208 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002209 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002210 uint8_t *wbuf = buf;
2211
Brian Norris8b6e50c2011-05-25 14:59:01 -07002212 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002213 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2214 cached = 0;
2215 bytes = min_t(int, bytes - column, (int) writelen);
2216 chip->pagebuf = -1;
2217 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2218 memcpy(&chip->buffers->databuf[column], buf, bytes);
2219 wbuf = chip->buffers->databuf;
2220 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002221
Maxim Levitsky782ce792010-02-22 20:39:36 +02002222 if (unlikely(oob)) {
2223 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002224 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002225 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002226 } else {
2227 /* We still need to erase leftover OOB data */
2228 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002229 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002230
Brian Norrise47f3db2012-05-02 10:14:56 -07002231 ret = chip->write_page(mtd, chip, wbuf, oob_required, page,
2232 cached, (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002233 if (ret)
2234 break;
2235
2236 writelen -= bytes;
2237 if (!writelen)
2238 break;
2239
Thomas Gleixner29072b92006-09-28 15:38:36 +02002240 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002241 buf += bytes;
2242 realpage++;
2243
2244 page = realpage & chip->pagemask;
2245 /* Check, if we cross a chip boundary */
2246 if (!page) {
2247 chipnr++;
2248 chip->select_chip(mtd, -1);
2249 chip->select_chip(mtd, chipnr);
2250 }
2251 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002252
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002253 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002254 if (unlikely(oob))
2255 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002256
2257err_out:
2258 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002259 return ret;
2260}
2261
2262/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002263 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002264 * @mtd: MTD device structure
2265 * @to: offset to write to
2266 * @len: number of bytes to write
2267 * @retlen: pointer to variable to store the number of written bytes
2268 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002269 *
2270 * NAND write with ECC. Used when performing writes in interrupt context, this
2271 * may for example be called by mtdoops when writing an oops while in panic.
2272 */
2273static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2274 size_t *retlen, const uint8_t *buf)
2275{
2276 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002277 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002278 int ret;
2279
Brian Norris8b6e50c2011-05-25 14:59:01 -07002280 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002281 panic_nand_wait(mtd, chip, 400);
2282
Brian Norris8b6e50c2011-05-25 14:59:01 -07002283 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002284 panic_nand_get_device(chip, mtd, FL_WRITING);
2285
Brian Norris4a89ff82011-08-30 18:45:45 -07002286 ops.len = len;
2287 ops.datbuf = (uint8_t *)buf;
2288 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002289 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002290
Brian Norris4a89ff82011-08-30 18:45:45 -07002291 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002292
Brian Norris4a89ff82011-08-30 18:45:45 -07002293 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002294 return ret;
2295}
2296
2297/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002298 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002299 * @mtd: MTD device structure
2300 * @to: offset to write to
2301 * @len: number of bytes to write
2302 * @retlen: pointer to variable to store the number of written bytes
2303 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002305 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002307static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002308 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Brian Norris4a89ff82011-08-30 18:45:45 -07002310 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002311 int ret;
2312
Huang Shijie6a8214a2012-11-19 14:43:30 +08002313 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002314 ops.len = len;
2315 ops.datbuf = (uint8_t *)buf;
2316 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002317 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002318 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002319 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002320 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321 return ret;
2322}
2323
2324/**
2325 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002326 * @mtd: MTD device structure
2327 * @to: offset to write to
2328 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002329 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002330 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002331 */
2332static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2333 struct mtd_oob_ops *ops)
2334{
Adrian Hunter03736152007-01-31 17:58:29 +02002335 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002336 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Brian Norris289c0522011-07-19 10:06:09 -07002338 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302339 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Brian Norris0612b9d2011-08-30 18:45:40 -07002341 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002342 len = chip->ecc.layout->oobavail;
2343 else
2344 len = mtd->oobsize;
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002347 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002348 pr_debug("%s: attempt to write past end of page\n",
2349 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return -EINVAL;
2351 }
2352
Adrian Hunter03736152007-01-31 17:58:29 +02002353 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002354 pr_debug("%s: attempt to start write outside oob\n",
2355 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002356 return -EINVAL;
2357 }
2358
Jason Liu775adc32011-02-25 13:06:18 +08002359 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002360 if (unlikely(to >= mtd->size ||
2361 ops->ooboffs + ops->ooblen >
2362 ((mtd->size >> chip->page_shift) -
2363 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002364 pr_debug("%s: attempt to write beyond end of device\n",
2365 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002366 return -EINVAL;
2367 }
2368
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002369 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002370 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002372 /* Shift to get page */
2373 page = (int)(to >> chip->page_shift);
2374
2375 /*
2376 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2377 * of my DiskOnChip 2000 test units) will clear the whole data page too
2378 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2379 * it in the doc2000 driver in August 1999. dwmw2.
2380 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002381 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002384 if (nand_check_wp(mtd)) {
2385 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002386 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002387 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002390 if (page == chip->pagebuf)
2391 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002393 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002394
Brian Norris0612b9d2011-08-30 18:45:40 -07002395 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002396 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2397 else
2398 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002399
Huang Shijieb0bb6902012-11-19 14:43:29 +08002400 chip->select_chip(mtd, -1);
2401
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002402 if (status)
2403 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Vitaly Wool70145682006-11-03 18:20:38 +03002405 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002407 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002410/**
2411 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002412 * @mtd: MTD device structure
2413 * @to: offset to write to
2414 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002415 */
2416static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2417 struct mtd_oob_ops *ops)
2418{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002419 int ret = -ENOTSUPP;
2420
2421 ops->retlen = 0;
2422
2423 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002424 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002425 pr_debug("%s: attempt to write beyond end of device\n",
2426 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002427 return -EINVAL;
2428 }
2429
Huang Shijie6a8214a2012-11-19 14:43:30 +08002430 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002431
Florian Fainellif8ac0412010-09-07 13:23:43 +02002432 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002433 case MTD_OPS_PLACE_OOB:
2434 case MTD_OPS_AUTO_OOB:
2435 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002436 break;
2437
2438 default:
2439 goto out;
2440 }
2441
2442 if (!ops->datbuf)
2443 ret = nand_do_write_oob(mtd, to, ops);
2444 else
2445 ret = nand_do_write_ops(mtd, to, ops);
2446
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002447out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002448 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return ret;
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002453 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002454 * @mtd: MTD device structure
2455 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002457 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002459static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002461 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002463 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2464 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
2467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002469 * @mtd: MTD device structure
2470 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002472 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002474static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
David Woodhousee0c7d762006-05-13 18:07:53 +01002476 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002480 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002481 * @mtd: MTD device structure
2482 * @instr: erase instruction
2483 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002485 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002487int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2488 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Adrian Hunter69423d92008-12-10 13:37:21 +00002490 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002491 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002492 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Brian Norris289c0522011-07-19 10:06:09 -07002494 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2495 __func__, (unsigned long long)instr->addr,
2496 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302498 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002502 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002505 page = (int)(instr->addr >> chip->page_shift);
2506 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002509 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002512 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /* Check, if it is write protected */
2515 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002516 pr_debug("%s: device is write protected!\n",
2517 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 instr->state = MTD_ERASE_FAILED;
2519 goto erase_exit;
2520 }
2521
2522 /* Loop through the pages */
2523 len = instr->len;
2524
2525 instr->state = MTD_ERASING;
2526
2527 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002528 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002529 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2530 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002531 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2532 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 instr->state = MTD_ERASE_FAILED;
2534 goto erase_exit;
2535 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002536
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002537 /*
2538 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002539 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002540 */
2541 if (page <= chip->pagebuf && chip->pagebuf <
2542 (page + pages_per_block))
2543 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002545 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002546
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002547 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002549 /*
2550 * See if operation failed and additional status checks are
2551 * available
2552 */
2553 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2554 status = chip->errstat(mtd, chip, FL_ERASING,
2555 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002558 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002559 pr_debug("%s: failed erase, page 0x%08x\n",
2560 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002562 instr->fail_addr =
2563 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 goto erase_exit;
2565 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002568 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 page += pages_per_block;
2570
2571 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002572 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002574 chip->select_chip(mtd, -1);
2575 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 }
2578 instr->state = MTD_ERASE_DONE;
2579
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002580erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002585 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 nand_release_device(mtd);
2587
David Woodhouse49defc02007-10-06 15:01:59 -04002588 /* Do call back function */
2589 if (!ret)
2590 mtd_erase_callback(instr);
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /* Return more or less happy */
2593 return ret;
2594}
2595
2596/**
2597 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002598 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002600 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002602static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Brian Norris289c0522011-07-19 10:06:09 -07002604 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002607 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002609 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002613 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002614 * @mtd: MTD device structure
2615 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002617static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002619 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
2622/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002623 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002624 * @mtd: MTD device structure
2625 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002627static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002629 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 int ret;
2631
Florian Fainellif8ac0412010-09-07 13:23:43 +02002632 ret = nand_block_isbad(mtd, ofs);
2633 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002634 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (ret > 0)
2636 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002637 return ret;
2638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002640 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002644 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2645 * @mtd: MTD device structure
2646 * @chip: nand chip info structure
2647 * @addr: feature address.
2648 * @subfeature_param: the subfeature parameters, a four bytes array.
2649 */
2650static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2651 int addr, uint8_t *subfeature_param)
2652{
2653 int status;
2654
2655 if (!chip->onfi_version)
2656 return -EINVAL;
2657
2658 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2659 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2660 status = chip->waitfunc(mtd, chip);
2661 if (status & NAND_STATUS_FAIL)
2662 return -EIO;
2663 return 0;
2664}
2665
2666/**
2667 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2668 * @mtd: MTD device structure
2669 * @chip: nand chip info structure
2670 * @addr: feature address.
2671 * @subfeature_param: the subfeature parameters, a four bytes array.
2672 */
2673static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2674 int addr, uint8_t *subfeature_param)
2675{
2676 if (!chip->onfi_version)
2677 return -EINVAL;
2678
2679 /* clear the sub feature parameters */
2680 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2681
2682 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2683 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2684 return 0;
2685}
2686
2687/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002688 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002689 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002690 */
2691static int nand_suspend(struct mtd_info *mtd)
2692{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002693 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002694}
2695
2696/**
2697 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002698 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002699 */
2700static void nand_resume(struct mtd_info *mtd)
2701{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002702 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002703
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002704 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002705 nand_release_device(mtd);
2706 else
Brian Norrisd0370212011-07-19 10:06:08 -07002707 pr_err("%s called for a chip which is not in suspended state\n",
2708 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002709}
2710
Brian Norris8b6e50c2011-05-25 14:59:01 -07002711/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002712static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002715 if (!chip->chip_delay)
2716 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002719 if (chip->cmdfunc == NULL)
2720 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723 if (chip->waitfunc == NULL)
2724 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002726 if (!chip->select_chip)
2727 chip->select_chip = nand_select_chip;
2728 if (!chip->read_byte)
2729 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2730 if (!chip->read_word)
2731 chip->read_word = nand_read_word;
2732 if (!chip->block_bad)
2733 chip->block_bad = nand_block_bad;
2734 if (!chip->block_markbad)
2735 chip->block_markbad = nand_default_block_markbad;
2736 if (!chip->write_buf)
2737 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2738 if (!chip->read_buf)
2739 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002740 if (!chip->scan_bbt)
2741 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002742
2743 if (!chip->controller) {
2744 chip->controller = &chip->hwcontrol;
2745 spin_lock_init(&chip->controller->lock);
2746 init_waitqueue_head(&chip->controller->wq);
2747 }
2748
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002749}
2750
Brian Norris8b6e50c2011-05-25 14:59:01 -07002751/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002752static void sanitize_string(uint8_t *s, size_t len)
2753{
2754 ssize_t i;
2755
Brian Norris8b6e50c2011-05-25 14:59:01 -07002756 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002757 s[len - 1] = 0;
2758
Brian Norris8b6e50c2011-05-25 14:59:01 -07002759 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002760 for (i = 0; i < len - 1; i++) {
2761 if (s[i] < ' ' || s[i] > 127)
2762 s[i] = '?';
2763 }
2764
Brian Norris8b6e50c2011-05-25 14:59:01 -07002765 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002766 strim(s);
2767}
2768
2769static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2770{
2771 int i;
2772 while (len--) {
2773 crc ^= *p++ << 8;
2774 for (i = 0; i < 8; i++)
2775 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2776 }
2777
2778 return crc;
2779}
2780
2781/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002782 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002783 */
2784static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002785 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002786{
2787 struct nand_onfi_params *p = &chip->onfi_params;
2788 int i;
2789 int val;
2790
Matthieu CASTET0ce82b72013-01-16 15:25:45 +01002791 /* ONFI need to be probed in 8 bits mode, and 16 bits should be selected with NAND_BUSWIDTH_AUTO */
2792 if (chip->options & NAND_BUSWIDTH_16) {
2793 pr_err("Trying ONFI probe in 16 bits mode, aborting !\n");
2794 return 0;
2795 }
Brian Norris7854d3f2011-06-23 14:12:08 -07002796 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002797 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2798 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2799 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2800 return 0;
2801
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002802 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2803 for (i = 0; i < 3; i++) {
2804 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2805 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2806 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002807 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002808 break;
2809 }
2810 }
2811
2812 if (i == 3)
2813 return 0;
2814
Brian Norris8b6e50c2011-05-25 14:59:01 -07002815 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002816 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002817 if (val & (1 << 5))
2818 chip->onfi_version = 23;
2819 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002820 chip->onfi_version = 22;
2821 else if (val & (1 << 3))
2822 chip->onfi_version = 21;
2823 else if (val & (1 << 2))
2824 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002825 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002826 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002827 else
2828 chip->onfi_version = 0;
2829
2830 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002831 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002832 return 0;
2833 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002834
2835 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2836 sanitize_string(p->model, sizeof(p->model));
2837 if (!mtd->name)
2838 mtd->name = p->model;
2839 mtd->writesize = le32_to_cpu(p->byte_per_page);
2840 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2841 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTET63795752012-03-19 15:35:25 +01002842 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2843 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002844 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002845 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002846 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002847
Huang Shijied42b5de2012-02-17 11:22:37 +08002848 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002849 return 1;
2850}
2851
2852/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07002853 * nand_id_has_period - Check if an ID string has a given wraparound period
2854 * @id_data: the ID string
2855 * @arrlen: the length of the @id_data array
2856 * @period: the period of repitition
2857 *
2858 * Check if an ID string is repeated within a given sequence of bytes at
2859 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08002860 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07002861 * if the repetition has a period of @period; otherwise, returns zero.
2862 */
2863static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2864{
2865 int i, j;
2866 for (i = 0; i < period; i++)
2867 for (j = i + period; j < arrlen; j += period)
2868 if (id_data[i] != id_data[j])
2869 return 0;
2870 return 1;
2871}
2872
2873/*
2874 * nand_id_len - Get the length of an ID string returned by CMD_READID
2875 * @id_data: the ID string
2876 * @arrlen: the length of the @id_data array
2877
2878 * Returns the length of the ID string, according to known wraparound/trailing
2879 * zero patterns. If no pattern exists, returns the length of the array.
2880 */
2881static int nand_id_len(u8 *id_data, int arrlen)
2882{
2883 int last_nonzero, period;
2884
2885 /* Find last non-zero byte */
2886 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2887 if (id_data[last_nonzero])
2888 break;
2889
2890 /* All zeros */
2891 if (last_nonzero < 0)
2892 return 0;
2893
2894 /* Calculate wraparound period */
2895 for (period = 1; period < arrlen; period++)
2896 if (nand_id_has_period(id_data, arrlen, period))
2897 break;
2898
2899 /* There's a repeated pattern */
2900 if (period < arrlen)
2901 return period;
2902
2903 /* There are trailing zeros */
2904 if (last_nonzero < arrlen - 1)
2905 return last_nonzero + 1;
2906
2907 /* No pattern detected */
2908 return arrlen;
2909}
2910
2911/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002912 * Many new NAND share similar device ID codes, which represent the size of the
2913 * chip. The rest of the parameters must be decoded according to generic or
2914 * manufacturer-specific "extended ID" decoding patterns.
2915 */
2916static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2917 u8 id_data[8], int *busw)
2918{
Brian Norrise3b88bd2012-09-24 20:40:52 -07002919 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002920 /* The 3rd id byte holds MLC / multichip data */
2921 chip->cellinfo = id_data[2];
2922 /* The 4th id byte is the important one */
2923 extid = id_data[3];
2924
Brian Norrise3b88bd2012-09-24 20:40:52 -07002925 id_len = nand_id_len(id_data, 8);
2926
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002927 /*
2928 * Field definitions are in the following datasheets:
2929 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07002930 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07002931 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002932 *
Brian Norrisaf451af2012-10-09 23:26:06 -07002933 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2934 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002935 */
Brian Norrisaf451af2012-10-09 23:26:06 -07002936 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Brian Norris6924d992012-11-14 21:46:30 -08002937 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Brian Norrisaf451af2012-10-09 23:26:06 -07002938 id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002939 /* Calc pagesize */
2940 mtd->writesize = 2048 << (extid & 0x03);
2941 extid >>= 2;
2942 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07002943 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002944 case 1:
2945 mtd->oobsize = 128;
2946 break;
2947 case 2:
2948 mtd->oobsize = 218;
2949 break;
2950 case 3:
2951 mtd->oobsize = 400;
2952 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07002953 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002954 mtd->oobsize = 436;
2955 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07002956 case 5:
2957 mtd->oobsize = 512;
2958 break;
2959 case 6:
2960 default: /* Other cases are "reserved" (unknown) */
2961 mtd->oobsize = 640;
2962 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002963 }
2964 extid >>= 2;
2965 /* Calc blocksize */
2966 mtd->erasesize = (128 * 1024) <<
2967 (((extid >> 1) & 0x04) | (extid & 0x03));
2968 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07002969 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
2970 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2971 unsigned int tmp;
2972
2973 /* Calc pagesize */
2974 mtd->writesize = 2048 << (extid & 0x03);
2975 extid >>= 2;
2976 /* Calc oobsize */
2977 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
2978 case 0:
2979 mtd->oobsize = 128;
2980 break;
2981 case 1:
2982 mtd->oobsize = 224;
2983 break;
2984 case 2:
2985 mtd->oobsize = 448;
2986 break;
2987 case 3:
2988 mtd->oobsize = 64;
2989 break;
2990 case 4:
2991 mtd->oobsize = 32;
2992 break;
2993 case 5:
2994 mtd->oobsize = 16;
2995 break;
2996 default:
2997 mtd->oobsize = 640;
2998 break;
2999 }
3000 extid >>= 2;
3001 /* Calc blocksize */
3002 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3003 if (tmp < 0x03)
3004 mtd->erasesize = (128 * 1024) << tmp;
3005 else if (tmp == 0x03)
3006 mtd->erasesize = 768 * 1024;
3007 else
3008 mtd->erasesize = (64 * 1024) << tmp;
3009 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003010 } else {
3011 /* Calc pagesize */
3012 mtd->writesize = 1024 << (extid & 0x03);
3013 extid >>= 2;
3014 /* Calc oobsize */
3015 mtd->oobsize = (8 << (extid & 0x01)) *
3016 (mtd->writesize >> 9);
3017 extid >>= 2;
3018 /* Calc blocksize. Blocksize is multiples of 64KiB */
3019 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3020 extid >>= 2;
3021 /* Get buswidth information */
3022 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3023 }
3024}
3025
3026/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003027 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3028 * decodes a matching ID table entry and assigns the MTD size parameters for
3029 * the chip.
3030 */
3031static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3032 struct nand_flash_dev *type, u8 id_data[8],
3033 int *busw)
3034{
3035 int maf_id = id_data[0];
3036
3037 mtd->erasesize = type->erasesize;
3038 mtd->writesize = type->pagesize;
3039 mtd->oobsize = mtd->writesize / 32;
3040 *busw = type->options & NAND_BUSWIDTH_16;
3041
3042 /*
3043 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3044 * some Spansion chips have erasesize that conflicts with size
3045 * listed in nand_ids table.
3046 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3047 */
3048 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3049 && id_data[6] == 0x00 && id_data[7] == 0x00
3050 && mtd->writesize == 512) {
3051 mtd->erasesize = 128 * 1024;
3052 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3053 }
3054}
3055
3056/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003057 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3058 * heuristic patterns using various detected parameters (e.g., manufacturer,
3059 * page size, cell-type information).
3060 */
3061static void nand_decode_bbm_options(struct mtd_info *mtd,
3062 struct nand_chip *chip, u8 id_data[8])
3063{
3064 int maf_id = id_data[0];
3065
3066 /* Set the bad block position */
3067 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3068 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3069 else
3070 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3071
3072 /*
3073 * Bad block marker is stored in the last page of each block on Samsung
3074 * and Hynix MLC devices; stored in first two pages of each block on
3075 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3076 * AMD/Spansion, and Macronix. All others scan only the first page.
3077 */
3078 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3079 (maf_id == NAND_MFR_SAMSUNG ||
3080 maf_id == NAND_MFR_HYNIX))
3081 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3082 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3083 (maf_id == NAND_MFR_SAMSUNG ||
3084 maf_id == NAND_MFR_HYNIX ||
3085 maf_id == NAND_MFR_TOSHIBA ||
3086 maf_id == NAND_MFR_AMD ||
3087 maf_id == NAND_MFR_MACRONIX)) ||
3088 (mtd->writesize == 2048 &&
3089 maf_id == NAND_MFR_MICRON))
3090 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3091}
3092
3093/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003094 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003095 */
3096static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003097 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003098 int busw,
3099 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003100 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003101{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003102 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003103 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003106 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Karl Beldanef89a882008-09-15 14:37:29 +02003108 /*
3109 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003110 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003111 */
3112 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003115 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003118 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003119 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Brian Norris8b6e50c2011-05-25 14:59:01 -07003121 /*
3122 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003123 * interface concerns can cause random data which looks like a
3124 * possibly credible NAND flash to appear. If the two results do
3125 * not match, ignore the device completely.
3126 */
3127
3128 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3129
Brian Norris4aef9b72012-09-24 20:40:48 -07003130 /* Read entire ID string */
3131 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003132 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003133
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003134 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003135 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07003136 "%02x,%02x against %02x,%02x\n", __func__,
3137 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003138 return ERR_PTR(-ENODEV);
3139 }
3140
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003141 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003142 type = nand_flash_ids;
3143
3144 for (; type->name != NULL; type++)
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003145 if (*dev_id == type->id)
Florian Fainellif8ac0412010-09-07 13:23:43 +02003146 break;
David Woodhouse5e81e882010-02-26 18:32:56 +00003147
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003148 chip->onfi_version = 0;
3149 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003150 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003151 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003152 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003153 }
3154
David Woodhouse5e81e882010-02-26 18:32:56 +00003155 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003156 return ERR_PTR(-ENODEV);
3157
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003158 if (!mtd->name)
3159 mtd->name = type->name;
3160
Adrian Hunter69423d92008-12-10 13:37:21 +00003161 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003162
Huang Shijie12a40a52010-09-27 10:43:53 +08003163 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003164 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003165 busw = chip->init_size(mtd, chip, id_data);
3166 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003167 /* Decode parameters from extended ID */
3168 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003169 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003170 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003171 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003172 /* Get chip options */
3173 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003174
Brian Norris8b6e50c2011-05-25 14:59:01 -07003175 /*
3176 * Check if chip is not a Samsung device. Do not clear the
3177 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003178 */
3179 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3180 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3181ident_done:
3182
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003183 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003184 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003185 if (nand_manuf_ids[maf_idx].id == *maf_id)
3186 break;
3187 }
3188
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003189 if (chip->options & NAND_BUSWIDTH_AUTO) {
3190 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3191 chip->options |= busw;
3192 nand_set_defaults(chip, busw);
3193 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3194 /*
3195 * Check, if buswidth is correct. Hardware drivers should set
3196 * chip correct!
3197 */
Brian Norris9a4d4d62011-07-19 10:06:07 -07003198 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003199 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3200 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003201 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003202 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3203 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003204 return ERR_PTR(-EINVAL);
3205 }
3206
Brian Norris7e74c2d2012-09-24 20:40:49 -07003207 nand_decode_bbm_options(mtd, chip, id_data);
3208
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003209 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003210 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003211 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003212 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003213
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003214 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003215 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003216 if (chip->chipsize & 0xffffffff)
3217 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003218 else {
3219 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3220 chip->chip_shift += 32 - 1;
3221 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003222
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003223 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003224 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003225
Brian Norris8b6e50c2011-05-25 14:59:01 -07003226 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003227 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3228 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003229
Huang Shijie886bd332012-04-09 11:41:37 +08003230 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003231 " %dMiB, page size: %d, OOB size: %d\n",
Huang Shijie886bd332012-04-09 11:41:37 +08003232 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3233 chip->onfi_version ? chip->onfi_params.model : type->name,
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003234 (int)(chip->chipsize >> 20), mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003235
3236 return type;
3237}
3238
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003239/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003240 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003241 * @mtd: MTD device structure
3242 * @maxchips: number of chips to scan for
3243 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003244 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003245 * This is the first phase of the normal nand_scan() function. It reads the
3246 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003247 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003248 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003249 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003250int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3251 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003252{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003253 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003254 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003255 struct nand_flash_dev *type;
3256
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003257 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003258 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003259 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003260 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003261
3262 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003263 type = nand_get_flash_type(mtd, chip, busw,
3264 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003265
3266 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003267 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003268 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003269 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003270 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272
Huang Shijie07300162012-11-09 16:23:45 +08003273 chip->select_chip(mtd, -1);
3274
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003275 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003276 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003277 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003278 /* See comment in nand_get_flash_type for reset */
3279 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003281 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003283 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003284 nand_dev_id != chip->read_byte(mtd)) {
3285 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 break;
Huang Shijie07300162012-11-09 16:23:45 +08003287 }
3288 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
3290 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003291 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003294 chip->numchips = i;
3295 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
David Woodhouse3b85c322006-09-25 17:06:53 +01003297 return 0;
3298}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003299EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003300
3301
3302/**
3303 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003304 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003305 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003306 * This is the second phase of the normal nand_scan() function. It fills out
3307 * all the uninitialized function pointers with the defaults and scans for a
3308 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003309 */
3310int nand_scan_tail(struct mtd_info *mtd)
3311{
3312 int i;
3313 struct nand_chip *chip = mtd->priv;
3314
Brian Norrise2414f42012-02-06 13:44:00 -08003315 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3316 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3317 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3318
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003319 if (!(chip->options & NAND_OWN_BUFFERS))
3320 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3321 if (!chip->buffers)
3322 return -ENOMEM;
3323
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003324 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003325 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003326
3327 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003328 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003329 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003330 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003331 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003333 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 break;
3335 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003336 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 break;
3338 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003339 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003341 case 128:
3342 chip->ecc.layout = &nand_oob_128;
3343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003345 pr_warn("No oob scheme defined for oobsize %d\n",
3346 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 BUG();
3348 }
3349 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003350
David Woodhouse956e9442006-09-25 17:12:39 +01003351 if (!chip->write_page)
3352 chip->write_page = nand_write_page;
3353
Huang Shijie7db03ec2012-09-13 14:57:52 +08003354 /* set for ONFI nand */
3355 if (!chip->onfi_set_features)
3356 chip->onfi_set_features = nand_onfi_set_features;
3357 if (!chip->onfi_get_features)
3358 chip->onfi_get_features = nand_onfi_get_features;
3359
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003360 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003361 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003362 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003363 */
David Woodhouse956e9442006-09-25 17:12:39 +01003364
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003365 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003366 case NAND_ECC_HW_OOB_FIRST:
3367 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3368 if (!chip->ecc.calculate || !chip->ecc.correct ||
3369 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003370 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003371 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003372 BUG();
3373 }
3374 if (!chip->ecc.read_page)
3375 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3376
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003377 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003378 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003379 if (!chip->ecc.read_page)
3380 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003381 if (!chip->ecc.write_page)
3382 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003383 if (!chip->ecc.read_page_raw)
3384 chip->ecc.read_page_raw = nand_read_page_raw;
3385 if (!chip->ecc.write_page_raw)
3386 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003387 if (!chip->ecc.read_oob)
3388 chip->ecc.read_oob = nand_read_oob_std;
3389 if (!chip->ecc.write_oob)
3390 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003391
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003392 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003393 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3394 !chip->ecc.hwctl) &&
3395 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003396 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003397 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003398 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003399 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003400 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003401 BUG();
3402 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003403 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003404 if (!chip->ecc.read_page)
3405 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003406 if (!chip->ecc.write_page)
3407 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003408 if (!chip->ecc.read_page_raw)
3409 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3410 if (!chip->ecc.write_page_raw)
3411 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003412 if (!chip->ecc.read_oob)
3413 chip->ecc.read_oob = nand_read_oob_syndrome;
3414 if (!chip->ecc.write_oob)
3415 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003416
Mike Dunne2788c92012-04-25 12:06:10 -07003417 if (mtd->writesize >= chip->ecc.size) {
3418 if (!chip->ecc.strength) {
3419 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3420 BUG();
3421 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003422 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003423 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003424 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003425 "%d byte page size, fallback to SW ECC\n",
3426 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003427 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003429 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003430 chip->ecc.calculate = nand_calculate_ecc;
3431 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003432 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003433 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003434 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003435 chip->ecc.read_page_raw = nand_read_page_raw;
3436 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003437 chip->ecc.read_oob = nand_read_oob_std;
3438 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003439 if (!chip->ecc.size)
3440 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003441 chip->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003442 chip->ecc.strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003444
Ivan Djelic193bd402011-03-11 11:05:33 +01003445 case NAND_ECC_SOFT_BCH:
3446 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003447 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003448 BUG();
3449 }
3450 chip->ecc.calculate = nand_bch_calculate_ecc;
3451 chip->ecc.correct = nand_bch_correct_data;
3452 chip->ecc.read_page = nand_read_page_swecc;
3453 chip->ecc.read_subpage = nand_read_subpage;
3454 chip->ecc.write_page = nand_write_page_swecc;
3455 chip->ecc.read_page_raw = nand_read_page_raw;
3456 chip->ecc.write_page_raw = nand_write_page_raw;
3457 chip->ecc.read_oob = nand_read_oob_std;
3458 chip->ecc.write_oob = nand_write_oob_std;
3459 /*
3460 * Board driver should supply ecc.size and ecc.bytes values to
3461 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003462 * for details. Otherwise, default to 4 bits for large page
3463 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003464 */
3465 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3466 chip->ecc.size = 512;
3467 chip->ecc.bytes = 7;
3468 }
3469 chip->ecc.priv = nand_bch_init(mtd,
3470 chip->ecc.size,
3471 chip->ecc.bytes,
3472 &chip->ecc.layout);
3473 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003474 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003475 BUG();
3476 }
Mike Dunn6a918ba2012-03-11 14:21:11 -07003477 chip->ecc.strength =
Mike Dunne2788c92012-04-25 12:06:10 -07003478 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003479 break;
3480
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003481 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003482 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003483 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003484 chip->ecc.read_page = nand_read_page_raw;
3485 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003486 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003487 chip->ecc.read_page_raw = nand_read_page_raw;
3488 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003489 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003490 chip->ecc.size = mtd->writesize;
3491 chip->ecc.bytes = 0;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003492 chip->ecc.strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003496 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003497 BUG();
3498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Brian Norris9ce244b2011-08-30 18:45:37 -07003500 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003501 if (!chip->ecc.read_oob_raw)
3502 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003503 if (!chip->ecc.write_oob_raw)
3504 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3505
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003506 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003507 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003508 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003509 */
3510 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003511 for (i = 0; chip->ecc.layout->oobfree[i].length
3512 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003513 chip->ecc.layout->oobavail +=
3514 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003515 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003516
3517 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003518 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003519 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003520 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003521 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003522 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003523 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003524 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003526 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003527
Brian Norris8b6e50c2011-05-25 14:59:01 -07003528 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003529 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3530 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003531 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003532 case 2:
3533 mtd->subpage_sft = 1;
3534 break;
3535 case 4:
3536 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003537 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003538 mtd->subpage_sft = 2;
3539 break;
3540 }
3541 }
3542 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3543
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003544 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003545 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003548 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003550 /* Large page NAND with SOFT_ECC should support subpage reads */
3551 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3552 chip->options |= NAND_SUBPAGE_READ;
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 /* Fill in remaining MTD driver data */
3555 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003556 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3557 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003558 mtd->_erase = nand_erase;
3559 mtd->_point = NULL;
3560 mtd->_unpoint = NULL;
3561 mtd->_read = nand_read;
3562 mtd->_write = nand_write;
3563 mtd->_panic_write = panic_nand_write;
3564 mtd->_read_oob = nand_read_oob;
3565 mtd->_write_oob = nand_write_oob;
3566 mtd->_sync = nand_sync;
3567 mtd->_lock = NULL;
3568 mtd->_unlock = NULL;
3569 mtd->_suspend = nand_suspend;
3570 mtd->_resume = nand_resume;
3571 mtd->_block_isbad = nand_block_isbad;
3572 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003573 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Mike Dunn6a918ba2012-03-11 14:21:11 -07003575 /* propagate ecc info to mtd_info */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003576 mtd->ecclayout = chip->ecc.layout;
Mike Dunn86c20722012-04-25 12:06:05 -07003577 mtd->ecc_strength = chip->ecc.strength;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003578 /*
3579 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3580 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3581 * properly set.
3582 */
3583 if (!mtd->bitflip_threshold)
3584 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003586 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003587 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003591 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003593EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Brian Norris8b6e50c2011-05-25 14:59:01 -07003595/*
3596 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003597 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003598 * to call us from in-kernel code if the core NAND support is modular.
3599 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003600#ifdef MODULE
3601#define caller_is_module() (1)
3602#else
3603#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003604 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003605#endif
3606
3607/**
3608 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003609 * @mtd: MTD device structure
3610 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003611 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003612 * This fills out all the uninitialized function pointers with the defaults.
3613 * The flash ID is read and the mtd/chip structures are filled with the
3614 * appropriate values. The mtd->owner field must be set to the module of the
3615 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003616 */
3617int nand_scan(struct mtd_info *mtd, int maxchips)
3618{
3619 int ret;
3620
3621 /* Many callers got this wrong, so check for it for a while... */
3622 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003623 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003624 BUG();
3625 }
3626
David Woodhouse5e81e882010-02-26 18:32:56 +00003627 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003628 if (!ret)
3629 ret = nand_scan_tail(mtd);
3630 return ret;
3631}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003632EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003635 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003636 * @mtd: MTD device structure
3637 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003638void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003640 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Ivan Djelic193bd402011-03-11 11:05:33 +01003642 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3643 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3644
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003645 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Jesper Juhlfa671642005-11-07 01:01:27 -08003647 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003648 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003649 if (!(chip->options & NAND_OWN_BUFFERS))
3650 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003651
3652 /* Free bad block descriptor memory */
3653 if (chip->badblock_pattern && chip->badblock_pattern->options
3654 & NAND_BBT_DYNAMICSTRUCT)
3655 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
David Woodhousee0c7d762006-05-13 18:07:53 +01003657EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003658
3659static int __init nand_base_init(void)
3660{
3661 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3662 return 0;
3663}
3664
3665static void __exit nand_base_exit(void)
3666{
3667 led_trigger_unregister_simple(nand_led_trigger);
3668}
3669
3670module_init(nand_base_init);
3671module_exit(nand_base_exit);
3672
David Woodhousee0c7d762006-05-13 18:07:53 +01003673MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003674MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3675MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003676MODULE_DESCRIPTION("Generic NAND flash driver code");