blob: 1cbacffb51063d79d084d7ab9d12547bc8c57905 [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{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200214 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Alexander Shiyan76413832013-04-13 09:32:13 +0400216 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000220 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700221 * @mtd: MTD device structure
222 * @buf: buffer to store date
223 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700225 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200227static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200229 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Alexander Shiyan76413832013-04-13 09:32:13 +0400231 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700236 * @mtd: MTD device structure
237 * @buf: data buffer
238 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700240 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200242static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200244 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246
Alexander Shiyan76413832013-04-13 09:32:13 +0400247 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000251 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700252 * @mtd: MTD device structure
253 * @buf: buffer to store date
254 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700256 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200258static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200260 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alexander Shiyan76413832013-04-13 09:32:13 +0400263 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700268 * @mtd: MTD device structure
269 * @ofs: offset from device start
270 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000272 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
274static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
275{
Brian Norriscdbec052012-01-13 18:11:48 -0800276 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 u16 bad;
279
Brian Norris5fb15492011-05-31 16:31:21 -0700280 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700281 ofs += mtd->erasesize - mtd->writesize;
282
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100283 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200286 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Huang Shijie6a8214a2012-11-19 14:43:30 +0800288 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200291 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Brian Norriscdbec052012-01-13 18:11:48 -0800294 do {
295 if (chip->options & NAND_BUSWIDTH_16) {
296 chip->cmdfunc(mtd, NAND_CMD_READOOB,
297 chip->badblockpos & 0xFE, page);
298 bad = cpu_to_le16(chip->read_word(mtd));
299 if (chip->badblockpos & 0x1)
300 bad >>= 8;
301 else
302 bad &= 0xFF;
303 } else {
304 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
305 page);
306 bad = chip->read_byte(mtd);
307 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000308
Brian Norriscdbec052012-01-13 18:11:48 -0800309 if (likely(chip->badblockbits == 8))
310 res = bad != 0xFF;
311 else
312 res = hweight8(bad) < chip->badblockbits;
313 ofs += mtd->writesize;
314 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
315 i++;
316 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200317
Huang Shijieb0bb6902012-11-19 14:43:29 +0800318 if (getchip) {
319 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800321 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return res;
324}
325
326/**
327 * nand_default_block_markbad - [DEFAULT] mark a block bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700328 * @mtd: MTD device structure
329 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700331 * This is the default implementation, which can be overridden by a hardware
Brian Norrise2414f42012-02-06 13:44:00 -0800332 * specific driver. We try operations in the following order, according to our
333 * bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
334 * (1) erase the affected block, to allow OOB marker to be written cleanly
335 * (2) update in-memory BBT
336 * (3) write bad block marker to OOB area of affected block
337 * (4) update flash-based BBT
338 * Note that we retain the first error encountered in (3) or (4), finish the
339 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340*/
341static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
342{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200343 struct nand_chip *chip = mtd->priv;
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200344 uint8_t buf[2] = { 0, 0 };
Brian Norrise2414f42012-02-06 13:44:00 -0800345 int block, res, ret = 0, i = 0;
346 int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000347
Brian Norrise2414f42012-02-06 13:44:00 -0800348 if (write_oob) {
Brian Norris00918422012-01-13 18:11:47 -0800349 struct erase_info einfo;
350
351 /* Attempt erase before marking OOB */
352 memset(&einfo, 0, sizeof(einfo));
353 einfo.mtd = mtd;
354 einfo.addr = ofs;
355 einfo.len = 1 << chip->phys_erase_shift;
356 nand_erase_nand(mtd, &einfo, 0);
357 }
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Get block number */
Andre Renaud4226b512007-04-17 13:50:59 -0400360 block = (int)(ofs >> chip->bbt_erase_shift);
Brian Norrise2414f42012-02-06 13:44:00 -0800361 /* Mark block bad in memory-based BBT */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200362 if (chip->bbt)
363 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Brian Norrise2414f42012-02-06 13:44:00 -0800365 /* Write bad block marker to OOB */
366 if (write_oob) {
Brian Norris4a89ff82011-08-30 18:45:45 -0700367 struct mtd_oob_ops ops;
Brian Norrisdf698622012-01-20 20:38:03 -0800368 loff_t wr_ofs = ofs;
Brian Norris4a89ff82011-08-30 18:45:45 -0700369
Huang Shijie6a8214a2012-11-19 14:43:30 +0800370 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000371
Brian Norris4a89ff82011-08-30 18:45:45 -0700372 ops.datbuf = NULL;
373 ops.oobbuf = buf;
Brian Norris85443312012-01-13 18:11:49 -0800374 ops.ooboffs = chip->badblockpos;
375 if (chip->options & NAND_BUSWIDTH_16) {
376 ops.ooboffs &= ~0x01;
377 ops.len = ops.ooblen = 2;
378 } else {
379 ops.len = ops.ooblen = 1;
380 }
Brian Norris23b1a992011-10-14 20:09:33 -0700381 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norrisdf698622012-01-20 20:38:03 -0800382
Brian Norrise2414f42012-02-06 13:44:00 -0800383 /* Write to first/last page(s) if necessary */
Brian Norrisdf698622012-01-20 20:38:03 -0800384 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
385 wr_ofs += mtd->erasesize - mtd->writesize;
Brian Norris02ed70b2010-07-21 16:53:47 -0700386 do {
Brian Norrise2414f42012-02-06 13:44:00 -0800387 res = nand_do_write_oob(mtd, wr_ofs, &ops);
388 if (!ret)
389 ret = res;
Brian Norris02ed70b2010-07-21 16:53:47 -0700390
Brian Norris02ed70b2010-07-21 16:53:47 -0700391 i++;
Brian Norrisdf698622012-01-20 20:38:03 -0800392 wr_ofs += mtd->writesize;
Brian Norrise2414f42012-02-06 13:44:00 -0800393 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
Brian Norris02ed70b2010-07-21 16:53:47 -0700394
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300395 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200396 }
Brian Norrise2414f42012-02-06 13:44:00 -0800397
398 /* Update flash-based bad block table */
399 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
400 res = nand_update_bbt(mtd, ofs);
401 if (!ret)
402 ret = res;
403 }
404
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200405 if (!ret)
406 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300407
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000411/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700413 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700415 * Check, if the device is write protected. The function expects, that the
416 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100418static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200420 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200421
Brian Norris8b6e50c2011-05-25 14:59:01 -0700422 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200423 if (chip->options & NAND_BROKEN_XD)
424 return 0;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200427 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
428 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431/**
432 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700433 * @mtd: MTD device structure
434 * @ofs: offset from device start
435 * @getchip: 0, if the chip is already selected
436 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * Check, if the block is bad. Either by reading the bad block table or
439 * calling of the scan function.
440 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200441static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
442 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200444 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000445
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200446 if (!chip->bbt)
447 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100450 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200453/**
454 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700455 * @mtd: MTD device structure
456 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200457 *
458 * Helper function for nand_wait_ready used when needing to wait in interrupt
459 * context.
460 */
461static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
462{
463 struct nand_chip *chip = mtd->priv;
464 int i;
465
466 /* Wait for the device to get ready */
467 for (i = 0; i < timeo; i++) {
468 if (chip->dev_ready(mtd))
469 break;
470 touch_softlockup_watchdog();
471 mdelay(1);
472 }
473}
474
Brian Norris7854d3f2011-06-23 14:12:08 -0700475/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100476void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000477{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200478 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100479 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000480
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200481 /* 400ms timeout */
482 if (in_interrupt() || oops_in_progress)
483 return panic_nand_wait_ready(mtd, 400);
484
Richard Purdie8fe833c2006-03-31 02:31:14 -0800485 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700486 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000487 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800489 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700490 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000491 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800492 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000493}
David Woodhouse4b648b02006-09-25 17:05:24 +0100494EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/**
497 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700498 * @mtd: MTD device structure
499 * @command: the command to be sent
500 * @column: the column address for this command, -1 if none
501 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700503 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200504 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200506static void nand_command(struct mtd_info *mtd, unsigned int command,
507 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200509 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200510 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Brian Norris8b6e50c2011-05-25 14:59:01 -0700512 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (command == NAND_CMD_SEQIN) {
514 int readcmd;
515
Joern Engel28318772006-05-22 23:18:05 +0200516 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200518 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 readcmd = NAND_CMD_READOOB;
520 } else if (column < 256) {
521 /* First 256 bytes --> READ0 */
522 readcmd = NAND_CMD_READ0;
523 } else {
524 column -= 256;
525 readcmd = NAND_CMD_READ1;
526 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200527 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200528 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200530 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Brian Norris8b6e50c2011-05-25 14:59:01 -0700532 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200533 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
534 /* Serially input address */
535 if (column != -1) {
536 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200537 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200538 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200539 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200540 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200542 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200543 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200544 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200545 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200546 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200547 if (chip->chipsize > (32 << 20))
548 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200549 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200550 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000551
552 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700553 * Program and erase have their own busy handlers status and sequential
554 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 case NAND_CMD_PAGEPROG:
559 case NAND_CMD_ERASE1:
560 case NAND_CMD_ERASE2:
561 case NAND_CMD_SEQIN:
562 case NAND_CMD_STATUS:
563 return;
564
565 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200566 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200568 udelay(chip->chip_delay);
569 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200570 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200571 chip->cmd_ctrl(mtd,
572 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200573 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
574 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return;
576
David Woodhousee0c7d762006-05-13 18:07:53 +0100577 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000579 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * If we don't have access to the busy pin, we apply the given
581 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100582 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200583 if (!chip->dev_ready) {
584 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700588 /*
589 * Apply this short delay always to ensure that we do wait tWB in
590 * any case on any machine.
591 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100592 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000593
594 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/**
598 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700599 * @mtd: MTD device structure
600 * @command: the command to be sent
601 * @column: the column address for this command, -1 if none
602 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200604 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700605 * devices. We don't have the separate regions as we have in the small page
606 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200608static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
609 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200611 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* Emulate NAND_CMD_READOOB */
614 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200615 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 command = NAND_CMD_READ0;
617 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000618
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400620 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* Serially input address */
626 if (column != -1) {
627 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200628 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200630 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200631 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200632 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200635 chip->cmd_ctrl(mtd, page_addr, ctrl);
636 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200637 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 if (chip->chipsize > (128 << 20))
640 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200641 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200644 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000645
646 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700647 * Program and erase have their own busy handlers status, sequential
648 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 case NAND_CMD_CACHEDPROG:
653 case NAND_CMD_PAGEPROG:
654 case NAND_CMD_ERASE1:
655 case NAND_CMD_ERASE2:
656 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200657 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000659 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200662 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200664 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200665 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
666 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
667 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
668 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200669 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
670 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return;
672
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200673 case NAND_CMD_RNDOUT:
674 /* No ready / busy check necessary */
675 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
676 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
677 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
678 NAND_NCE | NAND_CTRL_CHANGE);
679 return;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200682 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
683 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
684 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
685 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000686
David Woodhousee0c7d762006-05-13 18:07:53 +0100687 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000689 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700691 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200693 if (!chip->dev_ready) {
694 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000698
Brian Norris8b6e50c2011-05-25 14:59:01 -0700699 /*
700 * Apply this short delay always to ensure that we do wait tWB in
701 * any case on any machine.
702 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100703 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000704
705 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200709 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700710 * @chip: the nand chip descriptor
711 * @mtd: MTD device structure
712 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200713 *
714 * Used when in panic, no locks are taken.
715 */
716static void panic_nand_get_device(struct nand_chip *chip,
717 struct mtd_info *mtd, int new_state)
718{
Brian Norris7854d3f2011-06-23 14:12:08 -0700719 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200720 chip->controller->active = chip;
721 chip->state = new_state;
722}
723
724/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700726 * @mtd: MTD device structure
727 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *
729 * Get the device and lock it for exclusive access
730 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200731static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800732nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800734 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200735 spinlock_t *lock = &chip->controller->lock;
736 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100737 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200738retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100739 spin_lock(lock);
740
vimal singhb8b3ee92009-07-09 20:41:22 +0530741 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200742 if (!chip->controller->active)
743 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200744
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200745 if (chip->controller->active == chip && chip->state == FL_READY) {
746 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100747 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100748 return 0;
749 }
750 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800751 if (chip->controller->active->state == FL_PM_SUSPENDED) {
752 chip->state = FL_PM_SUSPENDED;
753 spin_unlock(lock);
754 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800755 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100756 }
757 set_current_state(TASK_UNINTERRUPTIBLE);
758 add_wait_queue(wq, &wait);
759 spin_unlock(lock);
760 schedule();
761 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto retry;
763}
764
765/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 * panic_nand_wait - [GENERIC] wait until the command is done
767 * @mtd: MTD device structure
768 * @chip: NAND chip structure
769 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200770 *
771 * Wait for command done. This is a helper function for nand_wait used when
772 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400773 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200774 */
775static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
776 unsigned long timeo)
777{
778 int i;
779 for (i = 0; i < timeo; i++) {
780 if (chip->dev_ready) {
781 if (chip->dev_ready(mtd))
782 break;
783 } else {
784 if (chip->read_byte(mtd) & NAND_STATUS_READY)
785 break;
786 }
787 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200788 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200789}
790
791/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 * nand_wait - [DEFAULT] wait until the command is done
793 * @mtd: MTD device structure
794 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700796 * Wait for command done. This applies to erase and program only. Erase can
797 * take up to 400ms and program up to 20ms according to general NAND and
798 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700799 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200800static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200803 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800804 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Richard Purdie8fe833c2006-03-31 02:31:14 -0800806 led_trigger_event(nand_led_trigger, LED_FULL);
807
Brian Norris8b6e50c2011-05-25 14:59:01 -0700808 /*
809 * Apply this short delay always to ensure that we do wait tWB in any
810 * case on any machine.
811 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100812 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200814 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200816 if (in_interrupt() || oops_in_progress)
817 panic_nand_wait(mtd, chip, timeo);
818 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800819 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200820 while (time_before(jiffies, timeo)) {
821 if (chip->dev_ready) {
822 if (chip->dev_ready(mtd))
823 break;
824 } else {
825 if (chip->read_byte(mtd) & NAND_STATUS_READY)
826 break;
827 }
828 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800831 led_trigger_event(nand_led_trigger, LED_OFF);
832
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200833 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100834 /* This can happen if in case of timeout or buggy dev_ready */
835 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return status;
837}
838
839/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700840 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700841 * @mtd: mtd info
842 * @ofs: offset to start unlock from
843 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700844 * @invert: when = 0, unlock the range of blocks within the lower and
845 * upper boundary address
846 * when = 1, unlock the range of blocks outside the boundaries
847 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530848 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700849 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530850 */
851static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
852 uint64_t len, int invert)
853{
854 int ret = 0;
855 int status, page;
856 struct nand_chip *chip = mtd->priv;
857
858 /* Submit address of first page to unlock */
859 page = ofs >> chip->page_shift;
860 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
861
862 /* Submit address of last page to unlock */
863 page = (ofs + len) >> chip->page_shift;
864 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
865 (page | invert) & chip->pagemask);
866
867 /* Call wait ready function */
868 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530869 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400870 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700871 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530872 __func__, status);
873 ret = -EIO;
874 }
875
876 return ret;
877}
878
879/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700880 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700881 * @mtd: mtd info
882 * @ofs: offset to start unlock from
883 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530884 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700885 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530886 */
887int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
888{
889 int ret = 0;
890 int chipnr;
891 struct nand_chip *chip = mtd->priv;
892
Brian Norris289c0522011-07-19 10:06:09 -0700893 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530894 __func__, (unsigned long long)ofs, len);
895
896 if (check_offs_len(mtd, ofs, len))
897 ret = -EINVAL;
898
899 /* Align to last block address if size addresses end of the device */
900 if (ofs + len == mtd->size)
901 len -= mtd->erasesize;
902
Huang Shijie6a8214a2012-11-19 14:43:30 +0800903 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530904
905 /* Shift to get chip number */
906 chipnr = ofs >> chip->chip_shift;
907
908 chip->select_chip(mtd, chipnr);
909
910 /* Check, if it is write protected */
911 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700912 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530913 __func__);
914 ret = -EIO;
915 goto out;
916 }
917
918 ret = __nand_unlock(mtd, ofs, len, 0);
919
920out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800921 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530922 nand_release_device(mtd);
923
924 return ret;
925}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200926EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530927
928/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700929 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700930 * @mtd: mtd info
931 * @ofs: offset to start unlock from
932 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530933 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700934 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
935 * have this feature, but it allows only to lock all blocks, not for specified
936 * range for block. Implementing 'lock' feature by making use of 'unlock', for
937 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530938 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700939 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530940 */
941int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
942{
943 int ret = 0;
944 int chipnr, status, page;
945 struct nand_chip *chip = mtd->priv;
946
Brian Norris289c0522011-07-19 10:06:09 -0700947 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530948 __func__, (unsigned long long)ofs, len);
949
950 if (check_offs_len(mtd, ofs, len))
951 ret = -EINVAL;
952
Huang Shijie6a8214a2012-11-19 14:43:30 +0800953 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530954
955 /* Shift to get chip number */
956 chipnr = ofs >> chip->chip_shift;
957
958 chip->select_chip(mtd, chipnr);
959
960 /* Check, if it is write protected */
961 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700962 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530963 __func__);
964 status = MTD_ERASE_FAILED;
965 ret = -EIO;
966 goto out;
967 }
968
969 /* Submit address of first page to lock */
970 page = ofs >> chip->page_shift;
971 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
972
973 /* Call wait ready function */
974 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530975 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400976 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700977 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530978 __func__, status);
979 ret = -EIO;
980 goto out;
981 }
982
983 ret = __nand_unlock(mtd, ofs, len, 0x1);
984
985out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800986 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530987 nand_release_device(mtd);
988
989 return ret;
990}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200991EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530992
993/**
Brian Norris7854d3f2011-06-23 14:12:08 -0700994 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -0700995 * @mtd: mtd info structure
996 * @chip: nand chip info structure
997 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -0700998 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -0700999 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001000 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001001 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001002 */
1003static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001004 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001005{
1006 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001007 if (oob_required)
1008 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001009 return 0;
1010}
1011
1012/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001013 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001014 * @mtd: mtd info structure
1015 * @chip: nand chip info structure
1016 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001017 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001018 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001019 *
1020 * We need a special oob layout and handling even when OOB isn't used.
1021 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001022static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001023 struct nand_chip *chip, uint8_t *buf,
1024 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001025{
1026 int eccsize = chip->ecc.size;
1027 int eccbytes = chip->ecc.bytes;
1028 uint8_t *oob = chip->oob_poi;
1029 int steps, size;
1030
1031 for (steps = chip->ecc.steps; steps > 0; steps--) {
1032 chip->read_buf(mtd, buf, eccsize);
1033 buf += eccsize;
1034
1035 if (chip->ecc.prepad) {
1036 chip->read_buf(mtd, oob, chip->ecc.prepad);
1037 oob += chip->ecc.prepad;
1038 }
1039
1040 chip->read_buf(mtd, oob, eccbytes);
1041 oob += eccbytes;
1042
1043 if (chip->ecc.postpad) {
1044 chip->read_buf(mtd, oob, chip->ecc.postpad);
1045 oob += chip->ecc.postpad;
1046 }
1047 }
1048
1049 size = mtd->oobsize - (oob - chip->oob_poi);
1050 if (size)
1051 chip->read_buf(mtd, oob, size);
1052
1053 return 0;
1054}
1055
1056/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001057 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001058 * @mtd: mtd info structure
1059 * @chip: nand chip info structure
1060 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001061 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001062 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001063 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001064static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001065 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001067 int i, eccsize = chip->ecc.size;
1068 int eccbytes = chip->ecc.bytes;
1069 int eccsteps = chip->ecc.steps;
1070 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001071 uint8_t *ecc_calc = chip->buffers->ecccalc;
1072 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001073 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001074 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001075
Brian Norris1fbb9382012-05-02 10:14:55 -07001076 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001077
1078 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1079 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1080
1081 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001082 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001083
1084 eccsteps = chip->ecc.steps;
1085 p = buf;
1086
1087 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1088 int stat;
1089
1090 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001091 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001092 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001093 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001094 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001095 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1096 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001097 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001098 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301102 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001103 * @mtd: mtd info structure
1104 * @chip: nand chip info structure
1105 * @data_offs: offset of requested data within the page
1106 * @readlen: data length
1107 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001108 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001109static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1110 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001111{
1112 int start_step, end_step, num_steps;
1113 uint32_t *eccpos = chip->ecc.layout->eccpos;
1114 uint8_t *p;
1115 int data_col_addr, i, gaps = 0;
1116 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1117 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001118 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001119 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001120
Brian Norris7854d3f2011-06-23 14:12:08 -07001121 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001122 start_step = data_offs / chip->ecc.size;
1123 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1124 num_steps = end_step - start_step + 1;
1125
Brian Norris8b6e50c2011-05-25 14:59:01 -07001126 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001127 datafrag_len = num_steps * chip->ecc.size;
1128 eccfrag_len = num_steps * chip->ecc.bytes;
1129
1130 data_col_addr = start_step * chip->ecc.size;
1131 /* If we read not a page aligned data */
1132 if (data_col_addr != 0)
1133 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1134
1135 p = bufpoi + data_col_addr;
1136 chip->read_buf(mtd, p, datafrag_len);
1137
Brian Norris8b6e50c2011-05-25 14:59:01 -07001138 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001139 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1140 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1141
Brian Norris8b6e50c2011-05-25 14:59:01 -07001142 /*
1143 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001144 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001145 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001146 for (i = 0; i < eccfrag_len - 1; i++) {
1147 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1148 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1149 gaps = 1;
1150 break;
1151 }
1152 }
1153 if (gaps) {
1154 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1155 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1156 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001157 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001158 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001159 * about buswidth alignment in read_buf.
1160 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001161 index = start_step * chip->ecc.bytes;
1162
1163 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001164 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001165 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001166 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001167 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001168 aligned_len++;
1169
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001170 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1171 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001172 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1173 }
1174
1175 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001176 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001177
1178 p = bufpoi + data_col_addr;
1179 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1180 int stat;
1181
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001182 stat = chip->ecc.correct(mtd, p,
1183 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001184 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001185 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001186 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001187 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001188 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1189 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001190 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001191 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001192}
1193
1194/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001195 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001196 * @mtd: mtd info structure
1197 * @chip: nand chip info structure
1198 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001199 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001200 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001201 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001202 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001203 */
1204static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001205 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001206{
1207 int i, eccsize = chip->ecc.size;
1208 int eccbytes = chip->ecc.bytes;
1209 int eccsteps = chip->ecc.steps;
1210 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001211 uint8_t *ecc_calc = chip->buffers->ecccalc;
1212 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001213 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001214 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001215
1216 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1217 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1218 chip->read_buf(mtd, p, eccsize);
1219 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1220 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001221 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001222
1223 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001224 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001225
1226 eccsteps = chip->ecc.steps;
1227 p = buf;
1228
1229 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1230 int stat;
1231
1232 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001233 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001234 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001235 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001236 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001237 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1238 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001239 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001240 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001241}
1242
1243/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001244 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001245 * @mtd: mtd info structure
1246 * @chip: nand chip info structure
1247 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001248 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001249 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001250 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 * Hardware ECC for large page chips, require OOB to be read first. For this
1252 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1253 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1254 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1255 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001256 */
1257static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001258 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001259{
1260 int i, eccsize = chip->ecc.size;
1261 int eccbytes = chip->ecc.bytes;
1262 int eccsteps = chip->ecc.steps;
1263 uint8_t *p = buf;
1264 uint8_t *ecc_code = chip->buffers->ecccode;
1265 uint32_t *eccpos = chip->ecc.layout->eccpos;
1266 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001267 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001268
1269 /* Read the OOB area first */
1270 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1271 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1272 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1273
1274 for (i = 0; i < chip->ecc.total; i++)
1275 ecc_code[i] = chip->oob_poi[eccpos[i]];
1276
1277 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1278 int stat;
1279
1280 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1281 chip->read_buf(mtd, p, eccsize);
1282 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1283
1284 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001285 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001286 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001287 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001288 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001289 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1290 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001291 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001292 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001293}
1294
1295/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001296 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001297 * @mtd: mtd info structure
1298 * @chip: nand chip info structure
1299 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001300 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001301 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001302 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001303 * The hw generator calculates the error syndrome automatically. Therefore we
1304 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001305 */
1306static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001307 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001308{
1309 int i, eccsize = chip->ecc.size;
1310 int eccbytes = chip->ecc.bytes;
1311 int eccsteps = chip->ecc.steps;
1312 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001313 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001314 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001315
1316 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1317 int stat;
1318
1319 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1320 chip->read_buf(mtd, p, eccsize);
1321
1322 if (chip->ecc.prepad) {
1323 chip->read_buf(mtd, oob, chip->ecc.prepad);
1324 oob += chip->ecc.prepad;
1325 }
1326
1327 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1328 chip->read_buf(mtd, oob, eccbytes);
1329 stat = chip->ecc.correct(mtd, p, oob, NULL);
1330
Mike Dunn3f91e942012-04-25 12:06:09 -07001331 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001332 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001333 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001334 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001335 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1336 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001337
1338 oob += eccbytes;
1339
1340 if (chip->ecc.postpad) {
1341 chip->read_buf(mtd, oob, chip->ecc.postpad);
1342 oob += chip->ecc.postpad;
1343 }
1344 }
1345
1346 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001347 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001348 if (i)
1349 chip->read_buf(mtd, oob, i);
1350
Mike Dunn3f91e942012-04-25 12:06:09 -07001351 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001352}
1353
1354/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001355 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001356 * @chip: nand chip structure
1357 * @oob: oob destination address
1358 * @ops: oob ops structure
1359 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001360 */
1361static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001362 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001363{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001364 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001365
Brian Norris0612b9d2011-08-30 18:45:40 -07001366 case MTD_OPS_PLACE_OOB:
1367 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001368 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1369 return oob + len;
1370
Brian Norris0612b9d2011-08-30 18:45:40 -07001371 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001372 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001373 uint32_t boffs = 0, roffs = ops->ooboffs;
1374 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001375
Florian Fainellif8ac0412010-09-07 13:23:43 +02001376 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001377 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001378 if (unlikely(roffs)) {
1379 if (roffs >= free->length) {
1380 roffs -= free->length;
1381 continue;
1382 }
1383 boffs = free->offset + roffs;
1384 bytes = min_t(size_t, len,
1385 (free->length - roffs));
1386 roffs = 0;
1387 } else {
1388 bytes = min_t(size_t, len, free->length);
1389 boffs = free->offset;
1390 }
1391 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001392 oob += bytes;
1393 }
1394 return oob;
1395 }
1396 default:
1397 BUG();
1398 }
1399 return NULL;
1400}
1401
1402/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001403 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001404 * @mtd: MTD device structure
1405 * @from: offset to read from
1406 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001407 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001408 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001409 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001410static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1411 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001412{
Brian Norrise47f3db2012-05-02 10:14:56 -07001413 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001414 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001415 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001416 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001417 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001418 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001419 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001420 mtd->oobavail : mtd->oobsize;
1421
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001422 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001423 unsigned int max_bitflips = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001425 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001427 chipnr = (int)(from >> chip->chip_shift);
1428 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001430 realpage = (int)(from >> chip->page_shift);
1431 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001433 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001435 buf = ops->datbuf;
1436 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001437 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001438
Florian Fainellif8ac0412010-09-07 13:23:43 +02001439 while (1) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001440 bytes = min(mtd->writesize - col, readlen);
1441 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001442
Brian Norris8b6e50c2011-05-25 14:59:01 -07001443 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001444 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001445 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Brian Norrisc00a0992012-05-01 17:12:54 -07001447 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Mike Dunnedbc45402012-04-25 12:06:11 -07001449 /*
1450 * Now read the page into the buffer. Absent an error,
1451 * the read methods return max bitflips per ecc step.
1452 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001453 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001454 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001455 oob_required,
1456 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001457 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1458 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001459 ret = chip->ecc.read_subpage(mtd, chip,
1460 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001461 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001462 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001463 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001464 if (ret < 0) {
1465 if (!aligned)
1466 /* Invalidate page cache */
1467 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001468 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001469 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001470
Mike Dunnedbc45402012-04-25 12:06:11 -07001471 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1472
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001473 /* Transfer not aligned data */
1474 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001475 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norris6d77b9d2011-09-07 13:13:40 -07001476 !(mtd->ecc_stats.failed - stats.failed) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001477 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001478 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001479 chip->pagebuf_bitflips = ret;
1480 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001481 /* Invalidate page cache */
1482 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001483 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001484 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001486
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001487 buf += bytes;
1488
1489 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001490 int toread = min(oobreadlen, max_oobsize);
1491
1492 if (toread) {
1493 oob = nand_transfer_oob(chip,
1494 oob, ops, toread);
1495 oobreadlen -= toread;
1496 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001497 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001498
1499 if (chip->options & NAND_NEED_READRDY) {
1500 /* Apply delay or wait for ready/busy pin */
1501 if (!chip->dev_ready)
1502 udelay(chip->chip_delay);
1503 else
1504 nand_wait_ready(mtd);
1505 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001506 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001507 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001508 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001509 max_bitflips = max_t(unsigned int, max_bitflips,
1510 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001513 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001514
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001515 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Brian Norris8b6e50c2011-05-25 14:59:01 -07001518 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 col = 0;
1520 /* Increment page address */
1521 realpage++;
1522
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001523 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* Check, if we cross a chip boundary */
1525 if (!page) {
1526 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001527 chip->select_chip(mtd, -1);
1528 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001531 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001533 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001534 if (oob)
1535 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Mike Dunn3f91e942012-04-25 12:06:09 -07001537 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001538 return ret;
1539
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001540 if (mtd->ecc_stats.failed - stats.failed)
1541 return -EBADMSG;
1542
Mike Dunnedbc45402012-04-25 12:06:11 -07001543 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001544}
1545
1546/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001547 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001548 * @mtd: MTD device structure
1549 * @from: offset to read from
1550 * @len: number of bytes to read
1551 * @retlen: pointer to variable to store the number of read bytes
1552 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001553 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001554 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001555 */
1556static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1557 size_t *retlen, uint8_t *buf)
1558{
Brian Norris4a89ff82011-08-30 18:45:45 -07001559 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001560 int ret;
1561
Huang Shijie6a8214a2012-11-19 14:43:30 +08001562 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001563 ops.len = len;
1564 ops.datbuf = buf;
1565 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001566 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001567 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001568 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001569 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001570 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001574 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001575 * @mtd: mtd info structure
1576 * @chip: nand chip info structure
1577 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001578 */
1579static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001580 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001581{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001582 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001583 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001584 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001585}
1586
1587/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001588 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001589 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001590 * @mtd: mtd info structure
1591 * @chip: nand chip info structure
1592 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001593 */
1594static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001595 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001596{
1597 uint8_t *buf = chip->oob_poi;
1598 int length = mtd->oobsize;
1599 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1600 int eccsize = chip->ecc.size;
1601 uint8_t *bufpoi = buf;
1602 int i, toread, sndrnd = 0, pos;
1603
1604 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1605 for (i = 0; i < chip->ecc.steps; i++) {
1606 if (sndrnd) {
1607 pos = eccsize + i * (eccsize + chunk);
1608 if (mtd->writesize > 512)
1609 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1610 else
1611 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1612 } else
1613 sndrnd = 1;
1614 toread = min_t(int, length, chunk);
1615 chip->read_buf(mtd, bufpoi, toread);
1616 bufpoi += toread;
1617 length -= toread;
1618 }
1619 if (length > 0)
1620 chip->read_buf(mtd, bufpoi, length);
1621
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001622 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001623}
1624
1625/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001626 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001627 * @mtd: mtd info structure
1628 * @chip: nand chip info structure
1629 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001630 */
1631static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1632 int page)
1633{
1634 int status = 0;
1635 const uint8_t *buf = chip->oob_poi;
1636 int length = mtd->oobsize;
1637
1638 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1639 chip->write_buf(mtd, buf, length);
1640 /* Send command to program the OOB data */
1641 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1642
1643 status = chip->waitfunc(mtd, chip);
1644
Savin Zlobec0d420f92006-06-21 11:51:20 +02001645 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001646}
1647
1648/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001649 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001650 * with syndrome - only for large page flash
1651 * @mtd: mtd info structure
1652 * @chip: nand chip info structure
1653 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001654 */
1655static int nand_write_oob_syndrome(struct mtd_info *mtd,
1656 struct nand_chip *chip, int page)
1657{
1658 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1659 int eccsize = chip->ecc.size, length = mtd->oobsize;
1660 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1661 const uint8_t *bufpoi = chip->oob_poi;
1662
1663 /*
1664 * data-ecc-data-ecc ... ecc-oob
1665 * or
1666 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1667 */
1668 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1669 pos = steps * (eccsize + chunk);
1670 steps = 0;
1671 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001672 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001673
1674 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1675 for (i = 0; i < steps; i++) {
1676 if (sndcmd) {
1677 if (mtd->writesize <= 512) {
1678 uint32_t fill = 0xFFFFFFFF;
1679
1680 len = eccsize;
1681 while (len > 0) {
1682 int num = min_t(int, len, 4);
1683 chip->write_buf(mtd, (uint8_t *)&fill,
1684 num);
1685 len -= num;
1686 }
1687 } else {
1688 pos = eccsize + i * (eccsize + chunk);
1689 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1690 }
1691 } else
1692 sndcmd = 1;
1693 len = min_t(int, length, chunk);
1694 chip->write_buf(mtd, bufpoi, len);
1695 bufpoi += len;
1696 length -= len;
1697 }
1698 if (length > 0)
1699 chip->write_buf(mtd, bufpoi, length);
1700
1701 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1702 status = chip->waitfunc(mtd, chip);
1703
1704 return status & NAND_STATUS_FAIL ? -EIO : 0;
1705}
1706
1707/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001708 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001709 * @mtd: MTD device structure
1710 * @from: offset to read from
1711 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001713 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001715static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1716 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Brian Norrisc00a0992012-05-01 17:12:54 -07001718 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001719 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001720 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001721 int readlen = ops->ooblen;
1722 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001723 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001724 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Brian Norris289c0522011-07-19 10:06:09 -07001726 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301727 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Brian Norris041e4572011-06-23 16:45:24 -07001729 stats = mtd->ecc_stats;
1730
Brian Norris0612b9d2011-08-30 18:45:40 -07001731 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001732 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001733 else
1734 len = mtd->oobsize;
1735
1736 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001737 pr_debug("%s: attempt to start read outside oob\n",
1738 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001739 return -EINVAL;
1740 }
1741
1742 /* Do not allow reads past end of device */
1743 if (unlikely(from >= mtd->size ||
1744 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1745 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001746 pr_debug("%s: attempt to read beyond end of device\n",
1747 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001748 return -EINVAL;
1749 }
Vitaly Wool70145682006-11-03 18:20:38 +03001750
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001751 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001752 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001754 /* Shift to get page */
1755 realpage = (int)(from >> chip->page_shift);
1756 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Florian Fainellif8ac0412010-09-07 13:23:43 +02001758 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001759 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001760 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001761 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001762 ret = chip->ecc.read_oob(mtd, chip, page);
1763
1764 if (ret < 0)
1765 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001766
1767 len = min(len, readlen);
1768 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001769
Brian Norris5bc7c332013-03-13 09:51:31 -07001770 if (chip->options & NAND_NEED_READRDY) {
1771 /* Apply delay or wait for ready/busy pin */
1772 if (!chip->dev_ready)
1773 udelay(chip->chip_delay);
1774 else
1775 nand_wait_ready(mtd);
1776 }
1777
Vitaly Wool70145682006-11-03 18:20:38 +03001778 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001779 if (!readlen)
1780 break;
1781
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001782 /* Increment page address */
1783 realpage++;
1784
1785 page = realpage & chip->pagemask;
1786 /* Check, if we cross a chip boundary */
1787 if (!page) {
1788 chipnr++;
1789 chip->select_chip(mtd, -1);
1790 chip->select_chip(mtd, chipnr);
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001793 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001795 ops->oobretlen = ops->ooblen - readlen;
1796
1797 if (ret < 0)
1798 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001799
1800 if (mtd->ecc_stats.failed - stats.failed)
1801 return -EBADMSG;
1802
1803 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001807 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001808 * @mtd: MTD device structure
1809 * @from: offset to read from
1810 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001812 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001814static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1815 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001817 int ret = -ENOTSUPP;
1818
1819 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001822 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001823 pr_debug("%s: attempt to read beyond end of device\n",
1824 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return -EINVAL;
1826 }
1827
Huang Shijie6a8214a2012-11-19 14:43:30 +08001828 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Florian Fainellif8ac0412010-09-07 13:23:43 +02001830 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001831 case MTD_OPS_PLACE_OOB:
1832 case MTD_OPS_AUTO_OOB:
1833 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001834 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001835
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001836 default:
1837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001840 if (!ops->datbuf)
1841 ret = nand_do_read_oob(mtd, from, ops);
1842 else
1843 ret = nand_do_read_ops(mtd, from, ops);
1844
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001845out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001847 return ret;
1848}
1849
1850
1851/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001852 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001853 * @mtd: mtd info structure
1854 * @chip: nand chip info structure
1855 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001856 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001857 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001858 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001859 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001860static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001861 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001862{
1863 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001864 if (oob_required)
1865 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001866
1867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001870/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001871 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001872 * @mtd: mtd info structure
1873 * @chip: nand chip info structure
1874 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001875 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001876 *
1877 * We need a special oob layout and handling even when ECC isn't checked.
1878 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001879static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001880 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001881 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001882{
1883 int eccsize = chip->ecc.size;
1884 int eccbytes = chip->ecc.bytes;
1885 uint8_t *oob = chip->oob_poi;
1886 int steps, size;
1887
1888 for (steps = chip->ecc.steps; steps > 0; steps--) {
1889 chip->write_buf(mtd, buf, eccsize);
1890 buf += eccsize;
1891
1892 if (chip->ecc.prepad) {
1893 chip->write_buf(mtd, oob, chip->ecc.prepad);
1894 oob += chip->ecc.prepad;
1895 }
1896
1897 chip->read_buf(mtd, oob, eccbytes);
1898 oob += eccbytes;
1899
1900 if (chip->ecc.postpad) {
1901 chip->write_buf(mtd, oob, chip->ecc.postpad);
1902 oob += chip->ecc.postpad;
1903 }
1904 }
1905
1906 size = mtd->oobsize - (oob - chip->oob_poi);
1907 if (size)
1908 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001909
1910 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001911}
1912/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001913 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001914 * @mtd: mtd info structure
1915 * @chip: nand chip info structure
1916 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001917 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001918 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001919static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001920 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001921{
1922 int i, eccsize = chip->ecc.size;
1923 int eccbytes = chip->ecc.bytes;
1924 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001925 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001926 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001927 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001928
Brian Norris7854d3f2011-06-23 14:12:08 -07001929 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001930 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1931 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001932
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001933 for (i = 0; i < chip->ecc.total; i++)
1934 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001935
Josh Wufdbad98d2012-06-25 18:07:45 +08001936 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001937}
1938
1939/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001940 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001941 * @mtd: mtd info structure
1942 * @chip: nand chip info structure
1943 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001944 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001945 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001946static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001947 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001948{
1949 int i, eccsize = chip->ecc.size;
1950 int eccbytes = chip->ecc.bytes;
1951 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001952 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001953 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001954 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001955
1956 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1957 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001958 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001959 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1960 }
1961
1962 for (i = 0; i < chip->ecc.total; i++)
1963 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1964
1965 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001966
1967 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001968}
1969
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301970
1971/**
1972 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
1973 * @mtd: mtd info structure
1974 * @chip: nand chip info structure
1975 * @column: column address of subpage within the page
1976 * @data_len: data length
1977 * @oob_required: must write chip->oob_poi to OOB
1978 */
1979static int nand_write_subpage_hwecc(struct mtd_info *mtd,
1980 struct nand_chip *chip, uint32_t offset,
1981 uint32_t data_len, const uint8_t *data_buf,
1982 int oob_required)
1983{
1984 uint8_t *oob_buf = chip->oob_poi;
1985 uint8_t *ecc_calc = chip->buffers->ecccalc;
1986 int ecc_size = chip->ecc.size;
1987 int ecc_bytes = chip->ecc.bytes;
1988 int ecc_steps = chip->ecc.steps;
1989 uint32_t *eccpos = chip->ecc.layout->eccpos;
1990 uint32_t start_step = offset / ecc_size;
1991 uint32_t end_step = (offset + data_len - 1) / ecc_size;
1992 int oob_bytes = mtd->oobsize / ecc_steps;
1993 int step, i;
1994
1995 for (step = 0; step < ecc_steps; step++) {
1996 /* configure controller for WRITE access */
1997 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1998
1999 /* write data (untouched subpages already masked by 0xFF) */
2000 chip->write_buf(mtd, data_buf, ecc_size);
2001
2002 /* mask ECC of un-touched subpages by padding 0xFF */
2003 if ((step < start_step) || (step > end_step))
2004 memset(ecc_calc, 0xff, ecc_bytes);
2005 else
2006 chip->ecc.calculate(mtd, data_buf, ecc_calc);
2007
2008 /* mask OOB of un-touched subpages by padding 0xFF */
2009 /* if oob_required, preserve OOB metadata of written subpage */
2010 if (!oob_required || (step < start_step) || (step > end_step))
2011 memset(oob_buf, 0xff, oob_bytes);
2012
2013 data_buf += ecc_size;
2014 ecc_calc += ecc_bytes;
2015 oob_buf += oob_bytes;
2016 }
2017
2018 /* copy calculated ECC for whole page to chip->buffer->oob */
2019 /* this include masked-value(0xFF) for unwritten subpages */
2020 ecc_calc = chip->buffers->ecccalc;
2021 for (i = 0; i < chip->ecc.total; i++)
2022 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2023
2024 /* write OOB buffer to NAND device */
2025 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2026
2027 return 0;
2028}
2029
2030
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002031/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002032 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002033 * @mtd: mtd info structure
2034 * @chip: nand chip info structure
2035 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002036 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002037 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002038 * The hw generator calculates the error syndrome automatically. Therefore we
2039 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002040 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002041static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002042 struct nand_chip *chip,
2043 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002044{
2045 int i, eccsize = chip->ecc.size;
2046 int eccbytes = chip->ecc.bytes;
2047 int eccsteps = chip->ecc.steps;
2048 const uint8_t *p = buf;
2049 uint8_t *oob = chip->oob_poi;
2050
2051 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2052
2053 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2054 chip->write_buf(mtd, p, eccsize);
2055
2056 if (chip->ecc.prepad) {
2057 chip->write_buf(mtd, oob, chip->ecc.prepad);
2058 oob += chip->ecc.prepad;
2059 }
2060
2061 chip->ecc.calculate(mtd, p, oob);
2062 chip->write_buf(mtd, oob, eccbytes);
2063 oob += eccbytes;
2064
2065 if (chip->ecc.postpad) {
2066 chip->write_buf(mtd, oob, chip->ecc.postpad);
2067 oob += chip->ecc.postpad;
2068 }
2069 }
2070
2071 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002072 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002073 if (i)
2074 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002075
2076 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002077}
2078
2079/**
David Woodhouse956e9442006-09-25 17:12:39 +01002080 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002081 * @mtd: MTD device structure
2082 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302083 * @offset: address offset within the page
2084 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002085 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002086 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002087 * @page: page number to write
2088 * @cached: cached programming
2089 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002090 */
2091static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302092 uint32_t offset, int data_len, const uint8_t *buf,
2093 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002094{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302095 int status, subpage;
2096
2097 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2098 chip->ecc.write_subpage)
2099 subpage = offset || (data_len < mtd->writesize);
2100 else
2101 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002102
2103 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2104
David Woodhouse956e9442006-09-25 17:12:39 +01002105 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302106 status = chip->ecc.write_page_raw(mtd, chip, buf,
2107 oob_required);
2108 else if (subpage)
2109 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2110 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002111 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002112 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2113
2114 if (status < 0)
2115 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002116
2117 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002118 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002119 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002120 */
2121 cached = 0;
2122
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002123 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002124
2125 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002126 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002127 /*
2128 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002129 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002130 */
2131 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2132 status = chip->errstat(mtd, chip, FL_WRITING, status,
2133 page);
2134
2135 if (status & NAND_STATUS_FAIL)
2136 return -EIO;
2137 } else {
2138 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002139 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002140 }
2141
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002142 return 0;
2143}
2144
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002145/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002146 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002147 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002148 * @oob: oob data buffer
2149 * @len: oob data write length
2150 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002151 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002152static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2153 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002154{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002155 struct nand_chip *chip = mtd->priv;
2156
2157 /*
2158 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2159 * data from a previous OOB read.
2160 */
2161 memset(chip->oob_poi, 0xff, mtd->oobsize);
2162
Florian Fainellif8ac0412010-09-07 13:23:43 +02002163 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002164
Brian Norris0612b9d2011-08-30 18:45:40 -07002165 case MTD_OPS_PLACE_OOB:
2166 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002167 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2168 return oob + len;
2169
Brian Norris0612b9d2011-08-30 18:45:40 -07002170 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002171 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002172 uint32_t boffs = 0, woffs = ops->ooboffs;
2173 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002174
Florian Fainellif8ac0412010-09-07 13:23:43 +02002175 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002176 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002177 if (unlikely(woffs)) {
2178 if (woffs >= free->length) {
2179 woffs -= free->length;
2180 continue;
2181 }
2182 boffs = free->offset + woffs;
2183 bytes = min_t(size_t, len,
2184 (free->length - woffs));
2185 woffs = 0;
2186 } else {
2187 bytes = min_t(size_t, len, free->length);
2188 boffs = free->offset;
2189 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002190 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002191 oob += bytes;
2192 }
2193 return oob;
2194 }
2195 default:
2196 BUG();
2197 }
2198 return NULL;
2199}
2200
Florian Fainellif8ac0412010-09-07 13:23:43 +02002201#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002202
2203/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002204 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002205 * @mtd: MTD device structure
2206 * @to: offset to write to
2207 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002208 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002209 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002210 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002211static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2212 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002213{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002214 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002215 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002216 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002217
2218 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002219 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002220 mtd->oobavail : mtd->oobsize;
2221
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002222 uint8_t *oob = ops->oobbuf;
2223 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302224 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002225 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002226
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002227 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002228 if (!writelen)
2229 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002230
Brian Norris8b6e50c2011-05-25 14:59:01 -07002231 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002232 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002233 pr_notice("%s: attempt to write non page aligned data\n",
2234 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002235 return -EINVAL;
2236 }
2237
Thomas Gleixner29072b92006-09-28 15:38:36 +02002238 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002239
Thomas Gleixner6a930962006-06-28 00:11:45 +02002240 chipnr = (int)(to >> chip->chip_shift);
2241 chip->select_chip(mtd, chipnr);
2242
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002243 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002244 if (nand_check_wp(mtd)) {
2245 ret = -EIO;
2246 goto err_out;
2247 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002248
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249 realpage = (int)(to >> chip->page_shift);
2250 page = realpage & chip->pagemask;
2251 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2252
2253 /* Invalidate the page cache, when we write to the cached page */
2254 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002255 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002256 chip->pagebuf = -1;
2257
Maxim Levitsky782ce792010-02-22 20:39:36 +02002258 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002259 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2260 ret = -EINVAL;
2261 goto err_out;
2262 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002263
Florian Fainellif8ac0412010-09-07 13:23:43 +02002264 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002265 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002266 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002267 uint8_t *wbuf = buf;
2268
Brian Norris8b6e50c2011-05-25 14:59:01 -07002269 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002270 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2271 cached = 0;
2272 bytes = min_t(int, bytes - column, (int) writelen);
2273 chip->pagebuf = -1;
2274 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2275 memcpy(&chip->buffers->databuf[column], buf, bytes);
2276 wbuf = chip->buffers->databuf;
2277 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002278
Maxim Levitsky782ce792010-02-22 20:39:36 +02002279 if (unlikely(oob)) {
2280 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002281 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002282 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002283 } else {
2284 /* We still need to erase leftover OOB data */
2285 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002286 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302287 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2288 oob_required, page, cached,
2289 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002290 if (ret)
2291 break;
2292
2293 writelen -= bytes;
2294 if (!writelen)
2295 break;
2296
Thomas Gleixner29072b92006-09-28 15:38:36 +02002297 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002298 buf += bytes;
2299 realpage++;
2300
2301 page = realpage & chip->pagemask;
2302 /* Check, if we cross a chip boundary */
2303 if (!page) {
2304 chipnr++;
2305 chip->select_chip(mtd, -1);
2306 chip->select_chip(mtd, chipnr);
2307 }
2308 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002309
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002310 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002311 if (unlikely(oob))
2312 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002313
2314err_out:
2315 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002316 return ret;
2317}
2318
2319/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002320 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002321 * @mtd: MTD device structure
2322 * @to: offset to write to
2323 * @len: number of bytes to write
2324 * @retlen: pointer to variable to store the number of written bytes
2325 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002326 *
2327 * NAND write with ECC. Used when performing writes in interrupt context, this
2328 * may for example be called by mtdoops when writing an oops while in panic.
2329 */
2330static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2331 size_t *retlen, const uint8_t *buf)
2332{
2333 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002334 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002335 int ret;
2336
Brian Norris8b6e50c2011-05-25 14:59:01 -07002337 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002338 panic_nand_wait(mtd, chip, 400);
2339
Brian Norris8b6e50c2011-05-25 14:59:01 -07002340 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002341 panic_nand_get_device(chip, mtd, FL_WRITING);
2342
Brian Norris4a89ff82011-08-30 18:45:45 -07002343 ops.len = len;
2344 ops.datbuf = (uint8_t *)buf;
2345 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002346 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002347
Brian Norris4a89ff82011-08-30 18:45:45 -07002348 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002349
Brian Norris4a89ff82011-08-30 18:45:45 -07002350 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002351 return ret;
2352}
2353
2354/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002355 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002356 * @mtd: MTD device structure
2357 * @to: offset to write to
2358 * @len: number of bytes to write
2359 * @retlen: pointer to variable to store the number of written bytes
2360 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002362 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002364static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002365 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Brian Norris4a89ff82011-08-30 18:45:45 -07002367 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002368 int ret;
2369
Huang Shijie6a8214a2012-11-19 14:43:30 +08002370 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002371 ops.len = len;
2372 ops.datbuf = (uint8_t *)buf;
2373 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002374 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002375 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002376 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002377 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002378 return ret;
2379}
2380
2381/**
2382 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002383 * @mtd: MTD device structure
2384 * @to: offset to write to
2385 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002386 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002387 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002388 */
2389static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2390 struct mtd_oob_ops *ops)
2391{
Adrian Hunter03736152007-01-31 17:58:29 +02002392 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002393 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Brian Norris289c0522011-07-19 10:06:09 -07002395 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302396 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Brian Norris0612b9d2011-08-30 18:45:40 -07002398 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002399 len = chip->ecc.layout->oobavail;
2400 else
2401 len = mtd->oobsize;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002404 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002405 pr_debug("%s: attempt to write past end of page\n",
2406 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return -EINVAL;
2408 }
2409
Adrian Hunter03736152007-01-31 17:58:29 +02002410 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002411 pr_debug("%s: attempt to start write outside oob\n",
2412 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002413 return -EINVAL;
2414 }
2415
Jason Liu775adc32011-02-25 13:06:18 +08002416 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002417 if (unlikely(to >= mtd->size ||
2418 ops->ooboffs + ops->ooblen >
2419 ((mtd->size >> chip->page_shift) -
2420 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002421 pr_debug("%s: attempt to write beyond end of device\n",
2422 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002423 return -EINVAL;
2424 }
2425
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002426 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002427 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002429 /* Shift to get page */
2430 page = (int)(to >> chip->page_shift);
2431
2432 /*
2433 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2434 * of my DiskOnChip 2000 test units) will clear the whole data page too
2435 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2436 * it in the doc2000 driver in August 1999. dwmw2.
2437 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002438 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002441 if (nand_check_wp(mtd)) {
2442 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002443 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002444 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002447 if (page == chip->pagebuf)
2448 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002450 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002451
Brian Norris0612b9d2011-08-30 18:45:40 -07002452 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002453 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2454 else
2455 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002456
Huang Shijieb0bb6902012-11-19 14:43:29 +08002457 chip->select_chip(mtd, -1);
2458
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002459 if (status)
2460 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Vitaly Wool70145682006-11-03 18:20:38 +03002462 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002464 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002467/**
2468 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002469 * @mtd: MTD device structure
2470 * @to: offset to write to
2471 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002472 */
2473static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2474 struct mtd_oob_ops *ops)
2475{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002476 int ret = -ENOTSUPP;
2477
2478 ops->retlen = 0;
2479
2480 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002481 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002482 pr_debug("%s: attempt to write beyond end of device\n",
2483 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002484 return -EINVAL;
2485 }
2486
Huang Shijie6a8214a2012-11-19 14:43:30 +08002487 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002488
Florian Fainellif8ac0412010-09-07 13:23:43 +02002489 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002490 case MTD_OPS_PLACE_OOB:
2491 case MTD_OPS_AUTO_OOB:
2492 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002493 break;
2494
2495 default:
2496 goto out;
2497 }
2498
2499 if (!ops->datbuf)
2500 ret = nand_do_write_oob(mtd, to, ops);
2501 else
2502 ret = nand_do_write_ops(mtd, to, ops);
2503
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002504out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002505 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return ret;
2507}
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002510 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002511 * @mtd: MTD device structure
2512 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002514 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002516static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002518 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002520 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2521 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
2524/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002526 * @mtd: MTD device structure
2527 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002529 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002531static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
David Woodhousee0c7d762006-05-13 18:07:53 +01002533 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002537 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002538 * @mtd: MTD device structure
2539 * @instr: erase instruction
2540 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002542 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002544int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2545 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Adrian Hunter69423d92008-12-10 13:37:21 +00002547 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002548 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002549 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Brian Norris289c0522011-07-19 10:06:09 -07002551 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2552 __func__, (unsigned long long)instr->addr,
2553 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302555 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002559 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002562 page = (int)(instr->addr >> chip->page_shift);
2563 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002566 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002569 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 /* Check, if it is write protected */
2572 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002573 pr_debug("%s: device is write protected!\n",
2574 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 instr->state = MTD_ERASE_FAILED;
2576 goto erase_exit;
2577 }
2578
2579 /* Loop through the pages */
2580 len = instr->len;
2581
2582 instr->state = MTD_ERASING;
2583
2584 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002585 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002586 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2587 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002588 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2589 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 instr->state = MTD_ERASE_FAILED;
2591 goto erase_exit;
2592 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002593
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002594 /*
2595 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002596 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002597 */
2598 if (page <= chip->pagebuf && chip->pagebuf <
2599 (page + pages_per_block))
2600 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002602 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002603
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002604 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002606 /*
2607 * See if operation failed and additional status checks are
2608 * available
2609 */
2610 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2611 status = chip->errstat(mtd, chip, FL_ERASING,
2612 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002615 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002616 pr_debug("%s: failed erase, page 0x%08x\n",
2617 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002619 instr->fail_addr =
2620 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 goto erase_exit;
2622 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /* Increment page address and decrement length */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 len -= (1 << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 page += pages_per_block;
2627
2628 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002629 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002631 chip->select_chip(mtd, -1);
2632 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
2634 }
2635 instr->state = MTD_ERASE_DONE;
2636
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002637erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002642 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 nand_release_device(mtd);
2644
David Woodhouse49defc02007-10-06 15:01:59 -04002645 /* Do call back function */
2646 if (!ret)
2647 mtd_erase_callback(instr);
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /* Return more or less happy */
2650 return ret;
2651}
2652
2653/**
2654 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002655 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002657 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002659static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Brian Norris289c0522011-07-19 10:06:09 -07002661 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002664 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002666 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002670 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002671 * @mtd: MTD device structure
2672 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002674static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002676 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002680 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002681 * @mtd: MTD device structure
2682 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002684static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002686 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 int ret;
2688
Florian Fainellif8ac0412010-09-07 13:23:43 +02002689 ret = nand_block_isbad(mtd, ofs);
2690 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002691 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (ret > 0)
2693 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002694 return ret;
2695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002697 return chip->block_markbad(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002701 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2702 * @mtd: MTD device structure
2703 * @chip: nand chip info structure
2704 * @addr: feature address.
2705 * @subfeature_param: the subfeature parameters, a four bytes array.
2706 */
2707static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2708 int addr, uint8_t *subfeature_param)
2709{
2710 int status;
2711
David Mosbergerd914c932013-05-29 15:30:13 +03002712 if (!chip->onfi_version ||
2713 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2714 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002715 return -EINVAL;
2716
2717 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2718 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2719 status = chip->waitfunc(mtd, chip);
2720 if (status & NAND_STATUS_FAIL)
2721 return -EIO;
2722 return 0;
2723}
2724
2725/**
2726 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2727 * @mtd: MTD device structure
2728 * @chip: nand chip info structure
2729 * @addr: feature address.
2730 * @subfeature_param: the subfeature parameters, a four bytes array.
2731 */
2732static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2733 int addr, uint8_t *subfeature_param)
2734{
David Mosbergerd914c932013-05-29 15:30:13 +03002735 if (!chip->onfi_version ||
2736 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2737 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002738 return -EINVAL;
2739
2740 /* clear the sub feature parameters */
2741 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2742
2743 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2744 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2745 return 0;
2746}
2747
2748/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002749 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002750 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002751 */
2752static int nand_suspend(struct mtd_info *mtd)
2753{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002754 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002755}
2756
2757/**
2758 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002759 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002760 */
2761static void nand_resume(struct mtd_info *mtd)
2762{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002763 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002764
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002765 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002766 nand_release_device(mtd);
2767 else
Brian Norrisd0370212011-07-19 10:06:08 -07002768 pr_err("%s called for a chip which is not in suspended state\n",
2769 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002770}
2771
Brian Norris8b6e50c2011-05-25 14:59:01 -07002772/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002773static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002776 if (!chip->chip_delay)
2777 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002780 if (chip->cmdfunc == NULL)
2781 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002784 if (chip->waitfunc == NULL)
2785 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002787 if (!chip->select_chip)
2788 chip->select_chip = nand_select_chip;
2789 if (!chip->read_byte)
2790 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2791 if (!chip->read_word)
2792 chip->read_word = nand_read_word;
2793 if (!chip->block_bad)
2794 chip->block_bad = nand_block_bad;
2795 if (!chip->block_markbad)
2796 chip->block_markbad = nand_default_block_markbad;
2797 if (!chip->write_buf)
2798 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2799 if (!chip->read_buf)
2800 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002801 if (!chip->scan_bbt)
2802 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002803
2804 if (!chip->controller) {
2805 chip->controller = &chip->hwcontrol;
2806 spin_lock_init(&chip->controller->lock);
2807 init_waitqueue_head(&chip->controller->wq);
2808 }
2809
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002810}
2811
Brian Norris8b6e50c2011-05-25 14:59:01 -07002812/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002813static void sanitize_string(uint8_t *s, size_t len)
2814{
2815 ssize_t i;
2816
Brian Norris8b6e50c2011-05-25 14:59:01 -07002817 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002818 s[len - 1] = 0;
2819
Brian Norris8b6e50c2011-05-25 14:59:01 -07002820 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002821 for (i = 0; i < len - 1; i++) {
2822 if (s[i] < ' ' || s[i] > 127)
2823 s[i] = '?';
2824 }
2825
Brian Norris8b6e50c2011-05-25 14:59:01 -07002826 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002827 strim(s);
2828}
2829
2830static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2831{
2832 int i;
2833 while (len--) {
2834 crc ^= *p++ << 8;
2835 for (i = 0; i < 8; i++)
2836 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2837 }
2838
2839 return crc;
2840}
2841
2842/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002843 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002844 */
2845static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002846 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002847{
2848 struct nand_onfi_params *p = &chip->onfi_params;
2849 int i;
2850 int val;
2851
Matthieu CASTET0ce82b72013-01-16 15:25:45 +01002852 /* ONFI need to be probed in 8 bits mode, and 16 bits should be selected with NAND_BUSWIDTH_AUTO */
2853 if (chip->options & NAND_BUSWIDTH_16) {
2854 pr_err("Trying ONFI probe in 16 bits mode, aborting !\n");
2855 return 0;
2856 }
Brian Norris7854d3f2011-06-23 14:12:08 -07002857 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002858 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2859 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2860 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2861 return 0;
2862
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002863 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2864 for (i = 0; i < 3; i++) {
2865 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2866 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2867 le16_to_cpu(p->crc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07002868 pr_info("ONFI param page %d valid\n", i);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002869 break;
2870 }
2871 }
2872
2873 if (i == 3)
2874 return 0;
2875
Brian Norris8b6e50c2011-05-25 14:59:01 -07002876 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002877 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002878 if (val & (1 << 5))
2879 chip->onfi_version = 23;
2880 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002881 chip->onfi_version = 22;
2882 else if (val & (1 << 3))
2883 chip->onfi_version = 21;
2884 else if (val & (1 << 2))
2885 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002886 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002887 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002888
2889 if (!chip->onfi_version) {
Brian Norrisd0370212011-07-19 10:06:08 -07002890 pr_info("%s: unsupported ONFI version: %d\n", __func__, val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002891 return 0;
2892 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002893
2894 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2895 sanitize_string(p->model, sizeof(p->model));
2896 if (!mtd->name)
2897 mtd->name = p->model;
2898 mtd->writesize = le32_to_cpu(p->byte_per_page);
2899 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2900 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Matthieu CASTET63795752012-03-19 15:35:25 +01002901 chip->chipsize = le32_to_cpu(p->blocks_per_lun);
2902 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002903 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002904 if (le16_to_cpu(p->features) & 1)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002905 *busw = NAND_BUSWIDTH_16;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002906
Huang Shijied42b5de2012-02-17 11:22:37 +08002907 pr_info("ONFI flash detected\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002908 return 1;
2909}
2910
2911/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07002912 * nand_id_has_period - Check if an ID string has a given wraparound period
2913 * @id_data: the ID string
2914 * @arrlen: the length of the @id_data array
2915 * @period: the period of repitition
2916 *
2917 * Check if an ID string is repeated within a given sequence of bytes at
2918 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08002919 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07002920 * if the repetition has a period of @period; otherwise, returns zero.
2921 */
2922static int nand_id_has_period(u8 *id_data, int arrlen, int period)
2923{
2924 int i, j;
2925 for (i = 0; i < period; i++)
2926 for (j = i + period; j < arrlen; j += period)
2927 if (id_data[i] != id_data[j])
2928 return 0;
2929 return 1;
2930}
2931
2932/*
2933 * nand_id_len - Get the length of an ID string returned by CMD_READID
2934 * @id_data: the ID string
2935 * @arrlen: the length of the @id_data array
2936
2937 * Returns the length of the ID string, according to known wraparound/trailing
2938 * zero patterns. If no pattern exists, returns the length of the array.
2939 */
2940static int nand_id_len(u8 *id_data, int arrlen)
2941{
2942 int last_nonzero, period;
2943
2944 /* Find last non-zero byte */
2945 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2946 if (id_data[last_nonzero])
2947 break;
2948
2949 /* All zeros */
2950 if (last_nonzero < 0)
2951 return 0;
2952
2953 /* Calculate wraparound period */
2954 for (period = 1; period < arrlen; period++)
2955 if (nand_id_has_period(id_data, arrlen, period))
2956 break;
2957
2958 /* There's a repeated pattern */
2959 if (period < arrlen)
2960 return period;
2961
2962 /* There are trailing zeros */
2963 if (last_nonzero < arrlen - 1)
2964 return last_nonzero + 1;
2965
2966 /* No pattern detected */
2967 return arrlen;
2968}
2969
2970/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002971 * Many new NAND share similar device ID codes, which represent the size of the
2972 * chip. The rest of the parameters must be decoded according to generic or
2973 * manufacturer-specific "extended ID" decoding patterns.
2974 */
2975static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
2976 u8 id_data[8], int *busw)
2977{
Brian Norrise3b88bd2012-09-24 20:40:52 -07002978 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002979 /* The 3rd id byte holds MLC / multichip data */
2980 chip->cellinfo = id_data[2];
2981 /* The 4th id byte is the important one */
2982 extid = id_data[3];
2983
Brian Norrise3b88bd2012-09-24 20:40:52 -07002984 id_len = nand_id_len(id_data, 8);
2985
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002986 /*
2987 * Field definitions are in the following datasheets:
2988 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07002989 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07002990 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002991 *
Brian Norrisaf451af2012-10-09 23:26:06 -07002992 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
2993 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002994 */
Brian Norrisaf451af2012-10-09 23:26:06 -07002995 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Brian Norris6924d992012-11-14 21:46:30 -08002996 (chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
Brian Norrisaf451af2012-10-09 23:26:06 -07002997 id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07002998 /* Calc pagesize */
2999 mtd->writesize = 2048 << (extid & 0x03);
3000 extid >>= 2;
3001 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003002 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003003 case 1:
3004 mtd->oobsize = 128;
3005 break;
3006 case 2:
3007 mtd->oobsize = 218;
3008 break;
3009 case 3:
3010 mtd->oobsize = 400;
3011 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003012 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003013 mtd->oobsize = 436;
3014 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003015 case 5:
3016 mtd->oobsize = 512;
3017 break;
3018 case 6:
3019 default: /* Other cases are "reserved" (unknown) */
3020 mtd->oobsize = 640;
3021 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003022 }
3023 extid >>= 2;
3024 /* Calc blocksize */
3025 mtd->erasesize = (128 * 1024) <<
3026 (((extid >> 1) & 0x04) | (extid & 0x03));
3027 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003028 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3029 (chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
3030 unsigned int tmp;
3031
3032 /* Calc pagesize */
3033 mtd->writesize = 2048 << (extid & 0x03);
3034 extid >>= 2;
3035 /* Calc oobsize */
3036 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3037 case 0:
3038 mtd->oobsize = 128;
3039 break;
3040 case 1:
3041 mtd->oobsize = 224;
3042 break;
3043 case 2:
3044 mtd->oobsize = 448;
3045 break;
3046 case 3:
3047 mtd->oobsize = 64;
3048 break;
3049 case 4:
3050 mtd->oobsize = 32;
3051 break;
3052 case 5:
3053 mtd->oobsize = 16;
3054 break;
3055 default:
3056 mtd->oobsize = 640;
3057 break;
3058 }
3059 extid >>= 2;
3060 /* Calc blocksize */
3061 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3062 if (tmp < 0x03)
3063 mtd->erasesize = (128 * 1024) << tmp;
3064 else if (tmp == 0x03)
3065 mtd->erasesize = 768 * 1024;
3066 else
3067 mtd->erasesize = (64 * 1024) << tmp;
3068 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003069 } else {
3070 /* Calc pagesize */
3071 mtd->writesize = 1024 << (extid & 0x03);
3072 extid >>= 2;
3073 /* Calc oobsize */
3074 mtd->oobsize = (8 << (extid & 0x01)) *
3075 (mtd->writesize >> 9);
3076 extid >>= 2;
3077 /* Calc blocksize. Blocksize is multiples of 64KiB */
3078 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3079 extid >>= 2;
3080 /* Get buswidth information */
3081 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3082 }
3083}
3084
3085/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003086 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3087 * decodes a matching ID table entry and assigns the MTD size parameters for
3088 * the chip.
3089 */
3090static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3091 struct nand_flash_dev *type, u8 id_data[8],
3092 int *busw)
3093{
3094 int maf_id = id_data[0];
3095
3096 mtd->erasesize = type->erasesize;
3097 mtd->writesize = type->pagesize;
3098 mtd->oobsize = mtd->writesize / 32;
3099 *busw = type->options & NAND_BUSWIDTH_16;
3100
3101 /*
3102 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3103 * some Spansion chips have erasesize that conflicts with size
3104 * listed in nand_ids table.
3105 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3106 */
3107 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3108 && id_data[6] == 0x00 && id_data[7] == 0x00
3109 && mtd->writesize == 512) {
3110 mtd->erasesize = 128 * 1024;
3111 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3112 }
3113}
3114
3115/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003116 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3117 * heuristic patterns using various detected parameters (e.g., manufacturer,
3118 * page size, cell-type information).
3119 */
3120static void nand_decode_bbm_options(struct mtd_info *mtd,
3121 struct nand_chip *chip, u8 id_data[8])
3122{
3123 int maf_id = id_data[0];
3124
3125 /* Set the bad block position */
3126 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3127 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3128 else
3129 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3130
3131 /*
3132 * Bad block marker is stored in the last page of each block on Samsung
3133 * and Hynix MLC devices; stored in first two pages of each block on
3134 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3135 * AMD/Spansion, and Macronix. All others scan only the first page.
3136 */
3137 if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3138 (maf_id == NAND_MFR_SAMSUNG ||
3139 maf_id == NAND_MFR_HYNIX))
3140 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3141 else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
3142 (maf_id == NAND_MFR_SAMSUNG ||
3143 maf_id == NAND_MFR_HYNIX ||
3144 maf_id == NAND_MFR_TOSHIBA ||
3145 maf_id == NAND_MFR_AMD ||
3146 maf_id == NAND_MFR_MACRONIX)) ||
3147 (mtd->writesize == 2048 &&
3148 maf_id == NAND_MFR_MICRON))
3149 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3150}
3151
Huang Shijieec6e87e2013-03-15 11:01:00 +08003152static inline bool is_full_id_nand(struct nand_flash_dev *type)
3153{
3154 return type->id_len;
3155}
3156
3157static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3158 struct nand_flash_dev *type, u8 *id_data, int *busw)
3159{
3160 if (!strncmp(type->id, id_data, type->id_len)) {
3161 mtd->writesize = type->pagesize;
3162 mtd->erasesize = type->erasesize;
3163 mtd->oobsize = type->oobsize;
3164
3165 chip->cellinfo = id_data[2];
3166 chip->chipsize = (uint64_t)type->chipsize << 20;
3167 chip->options |= type->options;
3168
3169 *busw = type->options & NAND_BUSWIDTH_16;
3170
3171 return true;
3172 }
3173 return false;
3174}
3175
Brian Norris7e74c2d2012-09-24 20:40:49 -07003176/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003177 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003178 */
3179static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003180 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003181 int busw,
3182 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003183 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003184{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003185 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003186 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
3188 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003189 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Karl Beldanef89a882008-09-15 14:37:29 +02003191 /*
3192 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003193 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003194 */
3195 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003198 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003201 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003202 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Brian Norris8b6e50c2011-05-25 14:59:01 -07003204 /*
3205 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003206 * interface concerns can cause random data which looks like a
3207 * possibly credible NAND flash to appear. If the two results do
3208 * not match, ignore the device completely.
3209 */
3210
3211 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3212
Brian Norris4aef9b72012-09-24 20:40:48 -07003213 /* Read entire ID string */
3214 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003215 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003216
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003217 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003218 pr_info("%s: second ID read did not match "
Brian Norrisd0370212011-07-19 10:06:08 -07003219 "%02x,%02x against %02x,%02x\n", __func__,
3220 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003221 return ERR_PTR(-ENODEV);
3222 }
3223
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003224 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003225 type = nand_flash_ids;
3226
Huang Shijieec6e87e2013-03-15 11:01:00 +08003227 for (; type->name != NULL; type++) {
3228 if (is_full_id_nand(type)) {
3229 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3230 goto ident_done;
3231 } else if (*dev_id == type->dev_id) {
3232 break;
3233 }
3234 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003235
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003236 chip->onfi_version = 0;
3237 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003238 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003239 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003240 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003241 }
3242
David Woodhouse5e81e882010-02-26 18:32:56 +00003243 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003244 return ERR_PTR(-ENODEV);
3245
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003246 if (!mtd->name)
3247 mtd->name = type->name;
3248
Adrian Hunter69423d92008-12-10 13:37:21 +00003249 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003250
Huang Shijie12a40a52010-09-27 10:43:53 +08003251 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003252 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003253 busw = chip->init_size(mtd, chip, id_data);
3254 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003255 /* Decode parameters from extended ID */
3256 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003257 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003258 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003259 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003260 /* Get chip options */
3261 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003262
Brian Norris8b6e50c2011-05-25 14:59:01 -07003263 /*
3264 * Check if chip is not a Samsung device. Do not clear the
3265 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003266 */
3267 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3268 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3269ident_done:
3270
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003271 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003272 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003273 if (nand_manuf_ids[maf_idx].id == *maf_id)
3274 break;
3275 }
3276
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003277 if (chip->options & NAND_BUSWIDTH_AUTO) {
3278 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3279 chip->options |= busw;
3280 nand_set_defaults(chip, busw);
3281 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3282 /*
3283 * Check, if buswidth is correct. Hardware drivers should set
3284 * chip correct!
3285 */
Brian Norris9a4d4d62011-07-19 10:06:07 -07003286 pr_info("NAND device: Manufacturer ID:"
Brian Norrisd0370212011-07-19 10:06:08 -07003287 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3288 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
Brian Norris9a4d4d62011-07-19 10:06:07 -07003289 pr_warn("NAND bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003290 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3291 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003292 return ERR_PTR(-EINVAL);
3293 }
3294
Brian Norris7e74c2d2012-09-24 20:40:49 -07003295 nand_decode_bbm_options(mtd, chip, id_data);
3296
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003297 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003298 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003299 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003300 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003301
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003302 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003303 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003304 if (chip->chipsize & 0xffffffff)
3305 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003306 else {
3307 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3308 chip->chip_shift += 32 - 1;
3309 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003310
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003311 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003312 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003313
Brian Norris8b6e50c2011-05-25 14:59:01 -07003314 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003315 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3316 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003317
Huang Shijie886bd332012-04-09 11:41:37 +08003318 pr_info("NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003319 " %dMiB, page size: %d, OOB size: %d\n",
Huang Shijie886bd332012-04-09 11:41:37 +08003320 *maf_id, *dev_id, nand_manuf_ids[maf_idx].name,
3321 chip->onfi_version ? chip->onfi_params.model : type->name,
Matthieu CASTET2fd71a22012-11-22 18:33:40 +01003322 (int)(chip->chipsize >> 20), mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003323
3324 return type;
3325}
3326
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003327/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003328 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003329 * @mtd: MTD device structure
3330 * @maxchips: number of chips to scan for
3331 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003332 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003333 * This is the first phase of the normal nand_scan() function. It reads the
3334 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003335 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003336 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003337 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003338int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3339 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003340{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003341 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003342 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003343 struct nand_flash_dev *type;
3344
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003345 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003346 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003347 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003348 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003349
3350 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003351 type = nand_get_flash_type(mtd, chip, busw,
3352 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003353
3354 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003355 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003356 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003357 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003358 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
3360
Huang Shijie07300162012-11-09 16:23:45 +08003361 chip->select_chip(mtd, -1);
3362
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003363 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003364 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003365 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003366 /* See comment in nand_get_flash_type for reset */
3367 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003369 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003371 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003372 nand_dev_id != chip->read_byte(mtd)) {
3373 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 break;
Huang Shijie07300162012-11-09 16:23:45 +08003375 }
3376 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378 if (i > 1)
Brian Norris9a4d4d62011-07-19 10:06:07 -07003379 pr_info("%d NAND chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003382 chip->numchips = i;
3383 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
David Woodhouse3b85c322006-09-25 17:06:53 +01003385 return 0;
3386}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003387EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003388
3389
3390/**
3391 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003392 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003393 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003394 * This is the second phase of the normal nand_scan() function. It fills out
3395 * all the uninitialized function pointers with the defaults and scans for a
3396 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003397 */
3398int nand_scan_tail(struct mtd_info *mtd)
3399{
3400 int i;
3401 struct nand_chip *chip = mtd->priv;
3402
Brian Norrise2414f42012-02-06 13:44:00 -08003403 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3404 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3405 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3406
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003407 if (!(chip->options & NAND_OWN_BUFFERS))
3408 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3409 if (!chip->buffers)
3410 return -ENOMEM;
3411
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003412 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003413 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003414
3415 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003416 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003417 */
Ivan Djelic193bd402011-03-11 11:05:33 +01003418 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003419 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 case 8:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003421 chip->ecc.layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 break;
3423 case 16:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003424 chip->ecc.layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 break;
3426 case 64:
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003427 chip->ecc.layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003429 case 128:
3430 chip->ecc.layout = &nand_oob_128;
3431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003433 pr_warn("No oob scheme defined for oobsize %d\n",
3434 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 BUG();
3436 }
3437 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003438
David Woodhouse956e9442006-09-25 17:12:39 +01003439 if (!chip->write_page)
3440 chip->write_page = nand_write_page;
3441
Huang Shijie7db03ec2012-09-13 14:57:52 +08003442 /* set for ONFI nand */
3443 if (!chip->onfi_set_features)
3444 chip->onfi_set_features = nand_onfi_set_features;
3445 if (!chip->onfi_get_features)
3446 chip->onfi_get_features = nand_onfi_get_features;
3447
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003448 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003449 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003450 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003451 */
David Woodhouse956e9442006-09-25 17:12:39 +01003452
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003453 switch (chip->ecc.mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003454 case NAND_ECC_HW_OOB_FIRST:
3455 /* Similar to NAND_ECC_HW, but a separate read_page handle */
3456 if (!chip->ecc.calculate || !chip->ecc.correct ||
3457 !chip->ecc.hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003458 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003459 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003460 BUG();
3461 }
3462 if (!chip->ecc.read_page)
3463 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
3464
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003465 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003466 /* Use standard hwecc read page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003467 if (!chip->ecc.read_page)
3468 chip->ecc.read_page = nand_read_page_hwecc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003469 if (!chip->ecc.write_page)
3470 chip->ecc.write_page = nand_write_page_hwecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003471 if (!chip->ecc.read_page_raw)
3472 chip->ecc.read_page_raw = nand_read_page_raw;
3473 if (!chip->ecc.write_page_raw)
3474 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003475 if (!chip->ecc.read_oob)
3476 chip->ecc.read_oob = nand_read_oob_std;
3477 if (!chip->ecc.write_oob)
3478 chip->ecc.write_oob = nand_write_oob_std;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05303479 if (!chip->ecc.read_subpage)
3480 chip->ecc.read_subpage = nand_read_subpage;
3481 if (!chip->ecc.write_subpage)
3482 chip->ecc.write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003483
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003484 case NAND_ECC_HW_SYNDROME:
Scott Wood78b65172007-12-13 11:15:28 -06003485 if ((!chip->ecc.calculate || !chip->ecc.correct ||
3486 !chip->ecc.hwctl) &&
3487 (!chip->ecc.read_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003488 chip->ecc.read_page == nand_read_page_hwecc ||
Scott Wood78b65172007-12-13 11:15:28 -06003489 !chip->ecc.write_page ||
Scott Wood1c45f602008-01-16 10:36:03 -06003490 chip->ecc.write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003491 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003492 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003493 BUG();
3494 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003495 /* Use standard syndrome read/write page function? */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003496 if (!chip->ecc.read_page)
3497 chip->ecc.read_page = nand_read_page_syndrome;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003498 if (!chip->ecc.write_page)
3499 chip->ecc.write_page = nand_write_page_syndrome;
David Brownell52ff49d2009-03-04 12:01:36 -08003500 if (!chip->ecc.read_page_raw)
3501 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
3502 if (!chip->ecc.write_page_raw)
3503 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003504 if (!chip->ecc.read_oob)
3505 chip->ecc.read_oob = nand_read_oob_syndrome;
3506 if (!chip->ecc.write_oob)
3507 chip->ecc.write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003508
Mike Dunne2788c92012-04-25 12:06:10 -07003509 if (mtd->writesize >= chip->ecc.size) {
3510 if (!chip->ecc.strength) {
3511 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3512 BUG();
3513 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003514 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003515 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003516 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003517 "%d byte page size, fallback to SW ECC\n",
3518 chip->ecc.size, mtd->writesize);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003519 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003521 case NAND_ECC_SOFT:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003522 chip->ecc.calculate = nand_calculate_ecc;
3523 chip->ecc.correct = nand_correct_data;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003524 chip->ecc.read_page = nand_read_page_swecc;
Alexey Korolev3d459552008-05-15 17:23:18 +01003525 chip->ecc.read_subpage = nand_read_subpage;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003526 chip->ecc.write_page = nand_write_page_swecc;
David Brownell52ff49d2009-03-04 12:01:36 -08003527 chip->ecc.read_page_raw = nand_read_page_raw;
3528 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003529 chip->ecc.read_oob = nand_read_oob_std;
3530 chip->ecc.write_oob = nand_write_oob_std;
Singh, Vimal9a732902008-12-12 00:10:57 +00003531 if (!chip->ecc.size)
3532 chip->ecc.size = 256;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003533 chip->ecc.bytes = 3;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003534 chip->ecc.strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003536
Ivan Djelic193bd402011-03-11 11:05:33 +01003537 case NAND_ECC_SOFT_BCH:
3538 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003539 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003540 BUG();
3541 }
3542 chip->ecc.calculate = nand_bch_calculate_ecc;
3543 chip->ecc.correct = nand_bch_correct_data;
3544 chip->ecc.read_page = nand_read_page_swecc;
3545 chip->ecc.read_subpage = nand_read_subpage;
3546 chip->ecc.write_page = nand_write_page_swecc;
3547 chip->ecc.read_page_raw = nand_read_page_raw;
3548 chip->ecc.write_page_raw = nand_write_page_raw;
3549 chip->ecc.read_oob = nand_read_oob_std;
3550 chip->ecc.write_oob = nand_write_oob_std;
3551 /*
3552 * Board driver should supply ecc.size and ecc.bytes values to
3553 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003554 * for details. Otherwise, default to 4 bits for large page
3555 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003556 */
3557 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
3558 chip->ecc.size = 512;
3559 chip->ecc.bytes = 7;
3560 }
3561 chip->ecc.priv = nand_bch_init(mtd,
3562 chip->ecc.size,
3563 chip->ecc.bytes,
3564 &chip->ecc.layout);
3565 if (!chip->ecc.priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003566 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003567 BUG();
3568 }
Mike Dunn6a918ba2012-03-11 14:21:11 -07003569 chip->ecc.strength =
Mike Dunne2788c92012-04-25 12:06:10 -07003570 chip->ecc.bytes * 8 / fls(8 * chip->ecc.size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003571 break;
3572
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003573 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003574 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003575 "This is not recommended!\n");
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02003576 chip->ecc.read_page = nand_read_page_raw;
3577 chip->ecc.write_page = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003578 chip->ecc.read_oob = nand_read_oob_std;
David Brownell52ff49d2009-03-04 12:01:36 -08003579 chip->ecc.read_page_raw = nand_read_page_raw;
3580 chip->ecc.write_page_raw = nand_write_page_raw;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02003581 chip->ecc.write_oob = nand_write_oob_std;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003582 chip->ecc.size = mtd->writesize;
3583 chip->ecc.bytes = 0;
Mike Dunn6a918ba2012-03-11 14:21:11 -07003584 chip->ecc.strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003588 pr_warn("Invalid NAND_ECC_MODE %d\n", chip->ecc.mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003589 BUG();
3590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Brian Norris9ce244b2011-08-30 18:45:37 -07003592 /* For many systems, the standard OOB write also works for raw */
Brian Norrisc46f6482011-08-30 18:45:38 -07003593 if (!chip->ecc.read_oob_raw)
3594 chip->ecc.read_oob_raw = chip->ecc.read_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003595 if (!chip->ecc.write_oob_raw)
3596 chip->ecc.write_oob_raw = chip->ecc.write_oob;
3597
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003598 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003599 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003600 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003601 */
3602 chip->ecc.layout->oobavail = 0;
David Brownell81d19b02009-04-21 19:51:20 -07003603 for (i = 0; chip->ecc.layout->oobfree[i].length
3604 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003605 chip->ecc.layout->oobavail +=
3606 chip->ecc.layout->oobfree[i].length;
Vitaly Wool1f922672007-03-06 16:56:34 +03003607 mtd->oobavail = chip->ecc.layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003608
3609 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003610 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003611 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003612 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003613 chip->ecc.steps = mtd->writesize / chip->ecc.size;
Florian Fainellif8ac0412010-09-07 13:23:43 +02003614 if (chip->ecc.steps * chip->ecc.size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003615 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003616 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003618 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003619
Brian Norris8b6e50c2011-05-25 14:59:01 -07003620 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Thomas Gleixner29072b92006-09-28 15:38:36 +02003621 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
3622 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
Florian Fainellif8ac0412010-09-07 13:23:43 +02003623 switch (chip->ecc.steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003624 case 2:
3625 mtd->subpage_sft = 1;
3626 break;
3627 case 4:
3628 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003629 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003630 mtd->subpage_sft = 2;
3631 break;
3632 }
3633 }
3634 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3635
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003636 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003637 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003640 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003642 /* Large page NAND with SOFT_ECC should support subpage reads */
3643 if ((chip->ecc.mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
3644 chip->options |= NAND_SUBPAGE_READ;
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 /* Fill in remaining MTD driver data */
3647 mtd->type = MTD_NANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003648 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3649 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003650 mtd->_erase = nand_erase;
3651 mtd->_point = NULL;
3652 mtd->_unpoint = NULL;
3653 mtd->_read = nand_read;
3654 mtd->_write = nand_write;
3655 mtd->_panic_write = panic_nand_write;
3656 mtd->_read_oob = nand_read_oob;
3657 mtd->_write_oob = nand_write_oob;
3658 mtd->_sync = nand_sync;
3659 mtd->_lock = NULL;
3660 mtd->_unlock = NULL;
3661 mtd->_suspend = nand_suspend;
3662 mtd->_resume = nand_resume;
3663 mtd->_block_isbad = nand_block_isbad;
3664 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003665 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Mike Dunn6a918ba2012-03-11 14:21:11 -07003667 /* propagate ecc info to mtd_info */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003668 mtd->ecclayout = chip->ecc.layout;
Mike Dunn86c20722012-04-25 12:06:05 -07003669 mtd->ecc_strength = chip->ecc.strength;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003670 /*
3671 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3672 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3673 * properly set.
3674 */
3675 if (!mtd->bitflip_threshold)
3676 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003678 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003679 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003683 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003685EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Brian Norris8b6e50c2011-05-25 14:59:01 -07003687/*
3688 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003689 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003690 * to call us from in-kernel code if the core NAND support is modular.
3691 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003692#ifdef MODULE
3693#define caller_is_module() (1)
3694#else
3695#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003696 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003697#endif
3698
3699/**
3700 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003701 * @mtd: MTD device structure
3702 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003703 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003704 * This fills out all the uninitialized function pointers with the defaults.
3705 * The flash ID is read and the mtd/chip structures are filled with the
3706 * appropriate values. The mtd->owner field must be set to the module of the
3707 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003708 */
3709int nand_scan(struct mtd_info *mtd, int maxchips)
3710{
3711 int ret;
3712
3713 /* Many callers got this wrong, so check for it for a while... */
3714 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003715 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003716 BUG();
3717 }
3718
David Woodhouse5e81e882010-02-26 18:32:56 +00003719 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003720 if (!ret)
3721 ret = nand_scan_tail(mtd);
3722 return ret;
3723}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003724EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003727 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003728 * @mtd: MTD device structure
3729 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003730void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003732 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Ivan Djelic193bd402011-03-11 11:05:33 +01003734 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3735 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3736
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003737 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Jesper Juhlfa671642005-11-07 01:01:27 -08003739 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003740 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003741 if (!(chip->options & NAND_OWN_BUFFERS))
3742 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003743
3744 /* Free bad block descriptor memory */
3745 if (chip->badblock_pattern && chip->badblock_pattern->options
3746 & NAND_BBT_DYNAMICSTRUCT)
3747 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
David Woodhousee0c7d762006-05-13 18:07:53 +01003749EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003750
3751static int __init nand_base_init(void)
3752{
3753 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3754 return 0;
3755}
3756
3757static void __exit nand_base_exit(void)
3758{
3759 led_trigger_unregister_simple(nand_led_trigger);
3760}
3761
3762module_init(nand_base_init);
3763module_exit(nand_base_exit);
3764
David Woodhousee0c7d762006-05-13 18:07:53 +01003765MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003766MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3767MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003768MODULE_DESCRIPTION("Generic NAND flash driver code");