blob: 0c1593be69e3ce7affb0986c5ece774252a7873f [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
Ezequiel Garcia20171642013-11-25 08:30:31 -030032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
David Woodhouse552d9202006-05-14 01:20:46 +010034#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
36#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020037#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sched.h>
39#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040040#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010045#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/interrupt.h>
47#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080048#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020049#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020053static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .eccbytes = 3,
55 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020056 .oobfree = {
57 {.offset = 3,
58 .length = 2},
59 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020060 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020063static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .eccbytes = 6,
65 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020066 .oobfree = {
67 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020068 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020071static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .eccbytes = 24,
73 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010074 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020077 .oobfree = {
78 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020079 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
Thomas Gleixner81ec5362007-12-12 17:27:03 +010082static struct nand_ecclayout nand_oob_128 = {
83 .eccbytes = 48,
84 .eccpos = {
85 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
88 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
91 .oobfree = {
92 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020093 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010094};
95
Huang Shijie6a8214a2012-11-19 14:43:30 +080096static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020098static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
99 struct mtd_oob_ops *ops);
100
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200101/*
Joe Perches8e87d782008-02-03 17:22:34 +0200102 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200103 * compiled away when LED support is disabled.
104 */
105DEFINE_LED_TRIGGER(nand_led_trigger);
106
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530107static int check_offs_len(struct mtd_info *mtd,
108 loff_t ofs, uint64_t len)
109{
110 struct nand_chip *chip = mtd->priv;
111 int ret = 0;
112
113 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300114 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700115 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530116 ret = -EINVAL;
117 }
118
119 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300120 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700121 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530122 ret = -EINVAL;
123 }
124
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530125 return ret;
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/**
129 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700130 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000131 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800132 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100134static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200136 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200138 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200139 spin_lock(&chip->controller->lock);
140 chip->controller->active = NULL;
141 chip->state = FL_READY;
142 wake_up(&chip->controller->wq);
143 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146/**
147 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700148 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700150 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200152static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200154 struct nand_chip *chip = mtd->priv;
155 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/**
Masanari Iida064a7692012-11-09 23:20:58 +0900159 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700160 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700161 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700163 * Default read function for 16bit buswidth with endianness conversion.
164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200166static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200168 struct nand_chip *chip = mtd->priv;
169 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700174 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700176 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178static u16 nand_read_word(struct mtd_info *mtd)
179{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200180 struct nand_chip *chip = mtd->priv;
181 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700186 * @mtd: MTD device structure
187 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
189 * Default select function for 1 chip devices.
190 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200191static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200193 struct nand_chip *chip = mtd->priv;
194
195 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200197 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201
202 default:
203 BUG();
204 }
205}
206
207/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100208 * nand_write_byte - [DEFAULT] write single byte to chip
209 * @mtd: MTD device structure
210 * @byte: value to write
211 *
212 * Default function to write a byte to I/O[7:0]
213 */
214static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
215{
216 struct nand_chip *chip = mtd->priv;
217
218 chip->write_buf(mtd, &byte, 1);
219}
220
221/**
222 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
223 * @mtd: MTD device structure
224 * @byte: value to write
225 *
226 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
227 */
228static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
229{
230 struct nand_chip *chip = mtd->priv;
231 uint16_t word = byte;
232
233 /*
234 * It's not entirely clear what should happen to I/O[15:8] when writing
235 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
236 *
237 * When the host supports a 16-bit bus width, only data is
238 * transferred at the 16-bit width. All address and command line
239 * transfers shall use only the lower 8-bits of the data bus. During
240 * command transfers, the host may place any value on the upper
241 * 8-bits of the data bus. During address transfers, the host shall
242 * set the upper 8-bits of the data bus to 00h.
243 *
244 * One user of the write_byte callback is nand_onfi_set_features. The
245 * four parameters are specified to be written to I/O[7:0], but this is
246 * neither an address nor a command transfer. Let's assume a 0 on the
247 * upper I/O lines is OK.
248 */
249 chip->write_buf(mtd, (uint8_t *)&word, 2);
250}
251
252/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700254 * @mtd: MTD device structure
255 * @buf: data buffer
256 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700258 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200260static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Alexander Shiyan76413832013-04-13 09:32:13 +0400264 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000268 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700269 * @mtd: MTD device structure
270 * @buf: buffer to store date
271 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700273 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200275static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200277 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Alexander Shiyan76413832013-04-13 09:32:13 +0400279 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700284 * @mtd: MTD device structure
285 * @buf: data buffer
286 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700288 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200290static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200292 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000294
Alexander Shiyan76413832013-04-13 09:32:13 +0400295 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000299 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700300 * @mtd: MTD device structure
301 * @buf: buffer to store date
302 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700304 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200306static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200308 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alexander Shiyan76413832013-04-13 09:32:13 +0400311 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700316 * @mtd: MTD device structure
317 * @ofs: offset from device start
318 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000320 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
322static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
323{
Brian Norriscdbec052012-01-13 18:11:48 -0800324 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200325 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u16 bad;
327
Brian Norris5fb15492011-05-31 16:31:21 -0700328 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700329 ofs += mtd->erasesize - mtd->writesize;
330
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100331 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200334 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Huang Shijie6a8214a2012-11-19 14:43:30 +0800336 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200339 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Brian Norriscdbec052012-01-13 18:11:48 -0800342 do {
343 if (chip->options & NAND_BUSWIDTH_16) {
344 chip->cmdfunc(mtd, NAND_CMD_READOOB,
345 chip->badblockpos & 0xFE, page);
346 bad = cpu_to_le16(chip->read_word(mtd));
347 if (chip->badblockpos & 0x1)
348 bad >>= 8;
349 else
350 bad &= 0xFF;
351 } else {
352 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
353 page);
354 bad = chip->read_byte(mtd);
355 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000356
Brian Norriscdbec052012-01-13 18:11:48 -0800357 if (likely(chip->badblockbits == 8))
358 res = bad != 0xFF;
359 else
360 res = hweight8(bad) < chip->badblockbits;
361 ofs += mtd->writesize;
362 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
363 i++;
364 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200365
Huang Shijieb0bb6902012-11-19 14:43:29 +0800366 if (getchip) {
367 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800369 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return res;
372}
373
374/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700375 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700376 * @mtd: MTD device structure
377 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700379 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700380 * specific driver. It provides the details for writing a bad block marker to a
381 * block.
382 */
383static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
384{
385 struct nand_chip *chip = mtd->priv;
386 struct mtd_oob_ops ops;
387 uint8_t buf[2] = { 0, 0 };
388 int ret = 0, res, i = 0;
389
390 ops.datbuf = NULL;
391 ops.oobbuf = buf;
392 ops.ooboffs = chip->badblockpos;
393 if (chip->options & NAND_BUSWIDTH_16) {
394 ops.ooboffs &= ~0x01;
395 ops.len = ops.ooblen = 2;
396 } else {
397 ops.len = ops.ooblen = 1;
398 }
399 ops.mode = MTD_OPS_PLACE_OOB;
400
401 /* Write to first/last page(s) if necessary */
402 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
403 ofs += mtd->erasesize - mtd->writesize;
404 do {
405 res = nand_do_write_oob(mtd, ofs, &ops);
406 if (!ret)
407 ret = res;
408
409 i++;
410 ofs += mtd->writesize;
411 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
412
413 return ret;
414}
415
416/**
417 * nand_block_markbad_lowlevel - mark a block bad
418 * @mtd: MTD device structure
419 * @ofs: offset from device start
420 *
421 * This function performs the generic NAND bad block marking steps (i.e., bad
422 * block table(s) and/or marker(s)). We only allow the hardware driver to
423 * specify how to write bad block markers to OOB (chip->block_markbad).
424 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700425 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800426 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700427 * (2) write bad block marker to OOB area of affected block (unless flag
428 * NAND_BBT_NO_OOB_BBM is present)
429 * (3) update the BBT
430 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800431 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700433static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200435 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700436 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000437
Brian Norrisb32843b2013-07-30 17:52:59 -0700438 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800439 struct erase_info einfo;
440
441 /* Attempt erase before marking OOB */
442 memset(&einfo, 0, sizeof(einfo));
443 einfo.mtd = mtd;
444 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300445 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800446 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800447
Brian Norrisb32843b2013-07-30 17:52:59 -0700448 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800449 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700450 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300451 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200452 }
Brian Norrise2414f42012-02-06 13:44:00 -0800453
Brian Norrisb32843b2013-07-30 17:52:59 -0700454 /* Mark block bad in BBT */
455 if (chip->bbt) {
456 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800457 if (!ret)
458 ret = res;
459 }
460
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200461 if (!ret)
462 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300463
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700469 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700471 * Check, if the device is write protected. The function expects, that the
472 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100474static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200476 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200477
Brian Norris8b6e50c2011-05-25 14:59:01 -0700478 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200479 if (chip->options & NAND_BROKEN_XD)
480 return 0;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200483 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
484 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487/**
488 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 * @mtd: MTD device structure
490 * @ofs: offset from device start
491 * @getchip: 0, if the chip is already selected
492 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *
494 * Check, if the block is bad. Either by reading the bad block table or
495 * calling of the scan function.
496 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200497static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
498 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200500 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000501
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200502 if (!chip->bbt)
503 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100506 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200509/**
510 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700511 * @mtd: MTD device structure
512 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200513 *
514 * Helper function for nand_wait_ready used when needing to wait in interrupt
515 * context.
516 */
517static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
518{
519 struct nand_chip *chip = mtd->priv;
520 int i;
521
522 /* Wait for the device to get ready */
523 for (i = 0; i < timeo; i++) {
524 if (chip->dev_ready(mtd))
525 break;
526 touch_softlockup_watchdog();
527 mdelay(1);
528 }
529}
530
Brian Norris7854d3f2011-06-23 14:12:08 -0700531/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100532void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000533{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200534 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100535 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000536
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200537 /* 400ms timeout */
538 if (in_interrupt() || oops_in_progress)
539 return panic_nand_wait_ready(mtd, 400);
540
Richard Purdie8fe833c2006-03-31 02:31:14 -0800541 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700542 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000543 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200544 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800545 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700546 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000547 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800548 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000549}
David Woodhouse4b648b02006-09-25 17:05:24 +0100550EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/**
553 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700554 * @mtd: MTD device structure
555 * @command: the command to be sent
556 * @column: the column address for this command, -1 if none
557 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700559 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200560 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200562static void nand_command(struct mtd_info *mtd, unsigned int command,
563 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200565 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200566 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Brian Norris8b6e50c2011-05-25 14:59:01 -0700568 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (command == NAND_CMD_SEQIN) {
570 int readcmd;
571
Joern Engel28318772006-05-22 23:18:05 +0200572 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200574 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 readcmd = NAND_CMD_READOOB;
576 } else if (column < 256) {
577 /* First 256 bytes --> READ0 */
578 readcmd = NAND_CMD_READ0;
579 } else {
580 column -= 256;
581 readcmd = NAND_CMD_READ1;
582 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200583 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200584 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200586 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Brian Norris8b6e50c2011-05-25 14:59:01 -0700588 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200589 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
590 /* Serially input address */
591 if (column != -1) {
592 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800593 if (chip->options & NAND_BUSWIDTH_16 &&
594 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200595 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200596 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200597 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200599 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200600 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200601 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200603 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200604 if (chip->chipsize > (32 << 20))
605 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200606 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200607 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608
609 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700610 * Program and erase have their own busy handlers status and sequential
611 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 case NAND_CMD_PAGEPROG:
616 case NAND_CMD_ERASE1:
617 case NAND_CMD_ERASE2:
618 case NAND_CMD_SEQIN:
619 case NAND_CMD_STATUS:
620 return;
621
622 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200623 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200625 udelay(chip->chip_delay);
626 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200627 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200628 chip->cmd_ctrl(mtd,
629 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200630 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
631 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return;
633
David Woodhousee0c7d762006-05-13 18:07:53 +0100634 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000636 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * If we don't have access to the busy pin, we apply the given
638 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100639 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200640 if (!chip->dev_ready) {
641 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700645 /*
646 * Apply this short delay always to ensure that we do wait tWB in
647 * any case on any machine.
648 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100649 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000650
651 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/**
655 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700656 * @mtd: MTD device structure
657 * @command: the command to be sent
658 * @column: the column address for this command, -1 if none
659 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200661 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700662 * devices. We don't have the separate regions as we have in the small page
663 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200665static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
666 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200668 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* Emulate NAND_CMD_READOOB */
671 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200672 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 command = NAND_CMD_READ0;
674 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000675
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200676 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400677 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200680 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* Serially input address */
683 if (column != -1) {
684 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800685 if (chip->options & NAND_BUSWIDTH_16 &&
686 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200688 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200689 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200690 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200693 chip->cmd_ctrl(mtd, page_addr, ctrl);
694 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200695 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200697 if (chip->chipsize > (128 << 20))
698 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200699 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200702 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000703
704 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700705 * Program and erase have their own busy handlers status, sequential
706 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 case NAND_CMD_CACHEDPROG:
711 case NAND_CMD_PAGEPROG:
712 case NAND_CMD_ERASE1:
713 case NAND_CMD_ERASE2:
714 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200715 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200720 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200722 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200723 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
724 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
725 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
726 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200727 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
728 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return;
730
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200731 case NAND_CMD_RNDOUT:
732 /* No ready / busy check necessary */
733 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
734 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
735 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
736 NAND_NCE | NAND_CTRL_CHANGE);
737 return;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200740 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
741 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
742 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
743 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000744
David Woodhousee0c7d762006-05-13 18:07:53 +0100745 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000747 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700749 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100750 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200751 if (!chip->dev_ready) {
752 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000756
Brian Norris8b6e50c2011-05-25 14:59:01 -0700757 /*
758 * Apply this short delay always to ensure that we do wait tWB in
759 * any case on any machine.
760 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100761 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000762
763 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200767 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700768 * @chip: the nand chip descriptor
769 * @mtd: MTD device structure
770 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200771 *
772 * Used when in panic, no locks are taken.
773 */
774static void panic_nand_get_device(struct nand_chip *chip,
775 struct mtd_info *mtd, int new_state)
776{
Brian Norris7854d3f2011-06-23 14:12:08 -0700777 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200778 chip->controller->active = chip;
779 chip->state = new_state;
780}
781
782/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 * @mtd: MTD device structure
785 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
787 * Get the device and lock it for exclusive access
788 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200789static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800790nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800792 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200793 spinlock_t *lock = &chip->controller->lock;
794 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100795 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200796retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100797 spin_lock(lock);
798
vimal singhb8b3ee92009-07-09 20:41:22 +0530799 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200800 if (!chip->controller->active)
801 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200802
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200803 if (chip->controller->active == chip && chip->state == FL_READY) {
804 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100805 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100806 return 0;
807 }
808 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800809 if (chip->controller->active->state == FL_PM_SUSPENDED) {
810 chip->state = FL_PM_SUSPENDED;
811 spin_unlock(lock);
812 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800813 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100814 }
815 set_current_state(TASK_UNINTERRUPTIBLE);
816 add_wait_queue(wq, &wait);
817 spin_unlock(lock);
818 schedule();
819 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto retry;
821}
822
823/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700824 * panic_nand_wait - [GENERIC] wait until the command is done
825 * @mtd: MTD device structure
826 * @chip: NAND chip structure
827 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200828 *
829 * Wait for command done. This is a helper function for nand_wait used when
830 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400831 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200832 */
833static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
834 unsigned long timeo)
835{
836 int i;
837 for (i = 0; i < timeo; i++) {
838 if (chip->dev_ready) {
839 if (chip->dev_ready(mtd))
840 break;
841 } else {
842 if (chip->read_byte(mtd) & NAND_STATUS_READY)
843 break;
844 }
845 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200846 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200847}
848
849/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700850 * nand_wait - [DEFAULT] wait until the command is done
851 * @mtd: MTD device structure
852 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700854 * Wait for command done. This applies to erase and program only. Erase can
855 * take up to 400ms and program up to 20ms according to general NAND and
856 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700857 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200858static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200861 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800862 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Richard Purdie8fe833c2006-03-31 02:31:14 -0800864 led_trigger_event(nand_led_trigger, LED_FULL);
865
Brian Norris8b6e50c2011-05-25 14:59:01 -0700866 /*
867 * Apply this short delay always to ensure that we do wait tWB in any
868 * case on any machine.
869 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100870 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200872 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200874 if (in_interrupt() || oops_in_progress)
875 panic_nand_wait(mtd, chip, timeo);
876 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800877 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200878 while (time_before(jiffies, timeo)) {
879 if (chip->dev_ready) {
880 if (chip->dev_ready(mtd))
881 break;
882 } else {
883 if (chip->read_byte(mtd) & NAND_STATUS_READY)
884 break;
885 }
886 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800889 led_trigger_event(nand_led_trigger, LED_OFF);
890
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200891 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100892 /* This can happen if in case of timeout or buggy dev_ready */
893 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return status;
895}
896
897/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700898 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700899 * @mtd: mtd info
900 * @ofs: offset to start unlock from
901 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700902 * @invert: when = 0, unlock the range of blocks within the lower and
903 * upper boundary address
904 * when = 1, unlock the range of blocks outside the boundaries
905 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530906 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700907 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530908 */
909static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
910 uint64_t len, int invert)
911{
912 int ret = 0;
913 int status, page;
914 struct nand_chip *chip = mtd->priv;
915
916 /* Submit address of first page to unlock */
917 page = ofs >> chip->page_shift;
918 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
919
920 /* Submit address of last page to unlock */
921 page = (ofs + len) >> chip->page_shift;
922 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
923 (page | invert) & chip->pagemask);
924
925 /* Call wait ready function */
926 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530927 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400928 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700929 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530930 __func__, status);
931 ret = -EIO;
932 }
933
934 return ret;
935}
936
937/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700938 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700939 * @mtd: mtd info
940 * @ofs: offset to start unlock from
941 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530942 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700943 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530944 */
945int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
946{
947 int ret = 0;
948 int chipnr;
949 struct nand_chip *chip = mtd->priv;
950
Brian Norris289c0522011-07-19 10:06:09 -0700951 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530952 __func__, (unsigned long long)ofs, len);
953
954 if (check_offs_len(mtd, ofs, len))
955 ret = -EINVAL;
956
957 /* Align to last block address if size addresses end of the device */
958 if (ofs + len == mtd->size)
959 len -= mtd->erasesize;
960
Huang Shijie6a8214a2012-11-19 14:43:30 +0800961 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530962
963 /* Shift to get chip number */
964 chipnr = ofs >> chip->chip_shift;
965
966 chip->select_chip(mtd, chipnr);
967
968 /* Check, if it is write protected */
969 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700970 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530971 __func__);
972 ret = -EIO;
973 goto out;
974 }
975
976 ret = __nand_unlock(mtd, ofs, len, 0);
977
978out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800979 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530980 nand_release_device(mtd);
981
982 return ret;
983}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200984EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530985
986/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700987 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700988 * @mtd: mtd info
989 * @ofs: offset to start unlock from
990 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530991 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700992 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
993 * have this feature, but it allows only to lock all blocks, not for specified
994 * range for block. Implementing 'lock' feature by making use of 'unlock', for
995 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530996 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700997 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530998 */
999int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1000{
1001 int ret = 0;
1002 int chipnr, status, page;
1003 struct nand_chip *chip = mtd->priv;
1004
Brian Norris289c0522011-07-19 10:06:09 -07001005 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301006 __func__, (unsigned long long)ofs, len);
1007
1008 if (check_offs_len(mtd, ofs, len))
1009 ret = -EINVAL;
1010
Huang Shijie6a8214a2012-11-19 14:43:30 +08001011 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301012
1013 /* Shift to get chip number */
1014 chipnr = ofs >> chip->chip_shift;
1015
1016 chip->select_chip(mtd, chipnr);
1017
1018 /* Check, if it is write protected */
1019 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001020 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301021 __func__);
1022 status = MTD_ERASE_FAILED;
1023 ret = -EIO;
1024 goto out;
1025 }
1026
1027 /* Submit address of first page to lock */
1028 page = ofs >> chip->page_shift;
1029 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1030
1031 /* Call wait ready function */
1032 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301033 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001034 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001035 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301036 __func__, status);
1037 ret = -EIO;
1038 goto out;
1039 }
1040
1041 ret = __nand_unlock(mtd, ofs, len, 0x1);
1042
1043out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001044 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301045 nand_release_device(mtd);
1046
1047 return ret;
1048}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001049EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301050
1051/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001052 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001053 * @mtd: mtd info structure
1054 * @chip: nand chip info structure
1055 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001056 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001057 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001058 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001059 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001060 */
1061static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001062 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001063{
1064 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001065 if (oob_required)
1066 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001067 return 0;
1068}
1069
1070/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001071 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001072 * @mtd: mtd info structure
1073 * @chip: nand chip info structure
1074 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001075 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001076 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001077 *
1078 * We need a special oob layout and handling even when OOB isn't used.
1079 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001080static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001081 struct nand_chip *chip, uint8_t *buf,
1082 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001083{
1084 int eccsize = chip->ecc.size;
1085 int eccbytes = chip->ecc.bytes;
1086 uint8_t *oob = chip->oob_poi;
1087 int steps, size;
1088
1089 for (steps = chip->ecc.steps; steps > 0; steps--) {
1090 chip->read_buf(mtd, buf, eccsize);
1091 buf += eccsize;
1092
1093 if (chip->ecc.prepad) {
1094 chip->read_buf(mtd, oob, chip->ecc.prepad);
1095 oob += chip->ecc.prepad;
1096 }
1097
1098 chip->read_buf(mtd, oob, eccbytes);
1099 oob += eccbytes;
1100
1101 if (chip->ecc.postpad) {
1102 chip->read_buf(mtd, oob, chip->ecc.postpad);
1103 oob += chip->ecc.postpad;
1104 }
1105 }
1106
1107 size = mtd->oobsize - (oob - chip->oob_poi);
1108 if (size)
1109 chip->read_buf(mtd, oob, size);
1110
1111 return 0;
1112}
1113
1114/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001115 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001116 * @mtd: mtd info structure
1117 * @chip: nand chip info structure
1118 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001119 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001120 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001121 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001122static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001123 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001125 int i, eccsize = chip->ecc.size;
1126 int eccbytes = chip->ecc.bytes;
1127 int eccsteps = chip->ecc.steps;
1128 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001129 uint8_t *ecc_calc = chip->buffers->ecccalc;
1130 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001131 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001132 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001133
Brian Norris1fbb9382012-05-02 10:14:55 -07001134 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001135
1136 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1137 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1138
1139 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001140 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001141
1142 eccsteps = chip->ecc.steps;
1143 p = buf;
1144
1145 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1146 int stat;
1147
1148 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001149 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001150 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001151 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001152 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001153 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1154 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001155 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001156 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301160 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001161 * @mtd: mtd info structure
1162 * @chip: nand chip info structure
1163 * @data_offs: offset of requested data within the page
1164 * @readlen: data length
1165 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001166 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001167 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001168static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001169 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1170 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001171{
1172 int start_step, end_step, num_steps;
1173 uint32_t *eccpos = chip->ecc.layout->eccpos;
1174 uint8_t *p;
1175 int data_col_addr, i, gaps = 0;
1176 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1177 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301178 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001179 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001180
Brian Norris7854d3f2011-06-23 14:12:08 -07001181 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001182 start_step = data_offs / chip->ecc.size;
1183 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1184 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301185 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001186
Brian Norris8b6e50c2011-05-25 14:59:01 -07001187 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001188 datafrag_len = num_steps * chip->ecc.size;
1189 eccfrag_len = num_steps * chip->ecc.bytes;
1190
1191 data_col_addr = start_step * chip->ecc.size;
1192 /* If we read not a page aligned data */
1193 if (data_col_addr != 0)
1194 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1195
1196 p = bufpoi + data_col_addr;
1197 chip->read_buf(mtd, p, datafrag_len);
1198
Brian Norris8b6e50c2011-05-25 14:59:01 -07001199 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001200 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1201 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1202
Brian Norris8b6e50c2011-05-25 14:59:01 -07001203 /*
1204 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001205 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001206 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001207 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301208 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001209 gaps = 1;
1210 break;
1211 }
1212 }
1213 if (gaps) {
1214 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1215 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1216 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001217 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001218 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001219 * about buswidth alignment in read_buf.
1220 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001221 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001222 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001223 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001224 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001225 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001226 aligned_len++;
1227
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001228 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1229 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001230 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1231 }
1232
1233 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001234 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001235
1236 p = bufpoi + data_col_addr;
1237 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1238 int stat;
1239
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001240 stat = chip->ecc.correct(mtd, p,
1241 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001242 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001243 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001244 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001245 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001246 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1247 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001248 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001249 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001250}
1251
1252/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001253 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001254 * @mtd: mtd info structure
1255 * @chip: nand chip info structure
1256 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001257 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001258 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001259 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001260 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001261 */
1262static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001263 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001264{
1265 int i, eccsize = chip->ecc.size;
1266 int eccbytes = chip->ecc.bytes;
1267 int eccsteps = chip->ecc.steps;
1268 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001269 uint8_t *ecc_calc = chip->buffers->ecccalc;
1270 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001271 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001272 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001273
1274 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1275 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1276 chip->read_buf(mtd, p, eccsize);
1277 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1278 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001279 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001280
1281 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001282 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001283
1284 eccsteps = chip->ecc.steps;
1285 p = buf;
1286
1287 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1288 int stat;
1289
1290 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001291 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001292 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001293 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001294 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001295 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1296 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001297 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001298 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001299}
1300
1301/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001302 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001303 * @mtd: mtd info structure
1304 * @chip: nand chip info structure
1305 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001306 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001307 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001308 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001309 * Hardware ECC for large page chips, require OOB to be read first. For this
1310 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1311 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1312 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1313 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001314 */
1315static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001316 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001317{
1318 int i, eccsize = chip->ecc.size;
1319 int eccbytes = chip->ecc.bytes;
1320 int eccsteps = chip->ecc.steps;
1321 uint8_t *p = buf;
1322 uint8_t *ecc_code = chip->buffers->ecccode;
1323 uint32_t *eccpos = chip->ecc.layout->eccpos;
1324 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001325 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001326
1327 /* Read the OOB area first */
1328 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1329 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1330 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1331
1332 for (i = 0; i < chip->ecc.total; i++)
1333 ecc_code[i] = chip->oob_poi[eccpos[i]];
1334
1335 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1336 int stat;
1337
1338 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1339 chip->read_buf(mtd, p, eccsize);
1340 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1341
1342 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001343 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001344 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001345 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001346 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1348 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001349 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001350 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001351}
1352
1353/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001354 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001355 * @mtd: mtd info structure
1356 * @chip: nand chip info structure
1357 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001358 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001359 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001360 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 * The hw generator calculates the error syndrome automatically. Therefore we
1362 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001363 */
1364static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001365 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001366{
1367 int i, eccsize = chip->ecc.size;
1368 int eccbytes = chip->ecc.bytes;
1369 int eccsteps = chip->ecc.steps;
1370 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001371 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001372 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001373
1374 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1375 int stat;
1376
1377 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1378 chip->read_buf(mtd, p, eccsize);
1379
1380 if (chip->ecc.prepad) {
1381 chip->read_buf(mtd, oob, chip->ecc.prepad);
1382 oob += chip->ecc.prepad;
1383 }
1384
1385 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1386 chip->read_buf(mtd, oob, eccbytes);
1387 stat = chip->ecc.correct(mtd, p, oob, NULL);
1388
Mike Dunn3f91e942012-04-25 12:06:09 -07001389 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001390 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001391 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001392 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001393 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1394 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001395
1396 oob += eccbytes;
1397
1398 if (chip->ecc.postpad) {
1399 chip->read_buf(mtd, oob, chip->ecc.postpad);
1400 oob += chip->ecc.postpad;
1401 }
1402 }
1403
1404 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001405 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001406 if (i)
1407 chip->read_buf(mtd, oob, i);
1408
Mike Dunn3f91e942012-04-25 12:06:09 -07001409 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001410}
1411
1412/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001413 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001414 * @chip: nand chip structure
1415 * @oob: oob destination address
1416 * @ops: oob ops structure
1417 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001418 */
1419static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001420 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001421{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001422 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001423
Brian Norris0612b9d2011-08-30 18:45:40 -07001424 case MTD_OPS_PLACE_OOB:
1425 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001426 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1427 return oob + len;
1428
Brian Norris0612b9d2011-08-30 18:45:40 -07001429 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001430 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001431 uint32_t boffs = 0, roffs = ops->ooboffs;
1432 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001433
Florian Fainellif8ac0412010-09-07 13:23:43 +02001434 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001435 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001436 if (unlikely(roffs)) {
1437 if (roffs >= free->length) {
1438 roffs -= free->length;
1439 continue;
1440 }
1441 boffs = free->offset + roffs;
1442 bytes = min_t(size_t, len,
1443 (free->length - roffs));
1444 roffs = 0;
1445 } else {
1446 bytes = min_t(size_t, len, free->length);
1447 boffs = free->offset;
1448 }
1449 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450 oob += bytes;
1451 }
1452 return oob;
1453 }
1454 default:
1455 BUG();
1456 }
1457 return NULL;
1458}
1459
1460/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001461 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1462 * @mtd: MTD device structure
1463 * @retry_mode: the retry mode to use
1464 *
1465 * Some vendors supply a special command to shift the Vt threshold, to be used
1466 * when there are too many bitflips in a page (i.e., ECC error). After setting
1467 * a new threshold, the host should retry reading the page.
1468 */
1469static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1470{
1471 struct nand_chip *chip = mtd->priv;
1472
1473 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1474
1475 if (retry_mode >= chip->read_retries)
1476 return -EINVAL;
1477
1478 if (!chip->setup_read_retry)
1479 return -EOPNOTSUPP;
1480
1481 return chip->setup_read_retry(mtd, retry_mode);
1482}
1483
1484/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001485 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001486 * @mtd: MTD device structure
1487 * @from: offset to read from
1488 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001489 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001490 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001491 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001492static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1493 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001494{
Brian Norrise47f3db2012-05-02 10:14:56 -07001495 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001496 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001497 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001498 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001499 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001500 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001501 mtd->oobavail : mtd->oobsize;
1502
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001503 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001504 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001505 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001506 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001507 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001509 chipnr = (int)(from >> chip->chip_shift);
1510 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001512 realpage = (int)(from >> chip->page_shift);
1513 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001515 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001517 buf = ops->datbuf;
1518 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001519 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001520
Florian Fainellif8ac0412010-09-07 13:23:43 +02001521 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001522 unsigned int ecc_failures = mtd->ecc_stats.failed;
1523
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001524 bytes = min(mtd->writesize - col, readlen);
1525 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001526
Kamal Dasu66507c72014-05-01 20:51:19 -04001527 if (!aligned)
1528 use_bufpoi = 1;
1529 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1530 use_bufpoi = !virt_addr_valid(buf);
1531 else
1532 use_bufpoi = 0;
1533
Brian Norris8b6e50c2011-05-25 14:59:01 -07001534 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001535 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001536 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1537
1538 if (use_bufpoi && aligned)
1539 pr_debug("%s: using read bounce buffer for buf@%p\n",
1540 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Brian Norrisba84fb52014-01-03 15:13:33 -08001542read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001543 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Mike Dunnedbc45402012-04-25 12:06:11 -07001545 /*
1546 * Now read the page into the buffer. Absent an error,
1547 * the read methods return max bitflips per ecc step.
1548 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001549 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001550 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001551 oob_required,
1552 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001553 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1554 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001555 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001556 col, bytes, bufpoi,
1557 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001558 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001559 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001560 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001561 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001562 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001563 /* Invalidate page cache */
1564 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001565 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001566 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001567
Mike Dunnedbc45402012-04-25 12:06:11 -07001568 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1569
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001570 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001571 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001572 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001573 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001574 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001575 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001576 chip->pagebuf_bitflips = ret;
1577 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001578 /* Invalidate page cache */
1579 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001580 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001581 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001583
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001584 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001585 int toread = min(oobreadlen, max_oobsize);
1586
1587 if (toread) {
1588 oob = nand_transfer_oob(chip,
1589 oob, ops, toread);
1590 oobreadlen -= toread;
1591 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001592 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001593
1594 if (chip->options & NAND_NEED_READRDY) {
1595 /* Apply delay or wait for ready/busy pin */
1596 if (!chip->dev_ready)
1597 udelay(chip->chip_delay);
1598 else
1599 nand_wait_ready(mtd);
1600 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001601
Brian Norrisba84fb52014-01-03 15:13:33 -08001602 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001603 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001604 retry_mode++;
1605 ret = nand_setup_read_retry(mtd,
1606 retry_mode);
1607 if (ret < 0)
1608 break;
1609
1610 /* Reset failures; retry */
1611 mtd->ecc_stats.failed = ecc_failures;
1612 goto read_retry;
1613 } else {
1614 /* No more retry modes; real failure */
1615 ecc_fail = true;
1616 }
1617 }
1618
1619 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001620 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001621 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001622 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001623 max_bitflips = max_t(unsigned int, max_bitflips,
1624 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001627 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001628
Brian Norrisba84fb52014-01-03 15:13:33 -08001629 /* Reset to retry mode 0 */
1630 if (retry_mode) {
1631 ret = nand_setup_read_retry(mtd, 0);
1632 if (ret < 0)
1633 break;
1634 retry_mode = 0;
1635 }
1636
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001637 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Brian Norris8b6e50c2011-05-25 14:59:01 -07001640 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 col = 0;
1642 /* Increment page address */
1643 realpage++;
1644
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001645 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* Check, if we cross a chip boundary */
1647 if (!page) {
1648 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001649 chip->select_chip(mtd, -1);
1650 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001653 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001655 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001656 if (oob)
1657 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Mike Dunn3f91e942012-04-25 12:06:09 -07001659 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001660 return ret;
1661
Brian Norrisb72f3df2013-12-03 11:04:14 -08001662 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001663 return -EBADMSG;
1664
Mike Dunnedbc45402012-04-25 12:06:11 -07001665 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001666}
1667
1668/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001669 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001670 * @mtd: MTD device structure
1671 * @from: offset to read from
1672 * @len: number of bytes to read
1673 * @retlen: pointer to variable to store the number of read bytes
1674 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001675 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001676 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001677 */
1678static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1679 size_t *retlen, uint8_t *buf)
1680{
Brian Norris4a89ff82011-08-30 18:45:45 -07001681 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001682 int ret;
1683
Huang Shijie6a8214a2012-11-19 14:43:30 +08001684 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001685 ops.len = len;
1686 ops.datbuf = buf;
1687 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001688 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001689 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001690 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001691 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001696 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001697 * @mtd: mtd info structure
1698 * @chip: nand chip info structure
1699 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001700 */
1701static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001702 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001703{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001704 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001705 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001706 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001707}
1708
1709/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001710 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001711 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001712 * @mtd: mtd info structure
1713 * @chip: nand chip info structure
1714 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001715 */
1716static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001717 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001718{
1719 uint8_t *buf = chip->oob_poi;
1720 int length = mtd->oobsize;
1721 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1722 int eccsize = chip->ecc.size;
1723 uint8_t *bufpoi = buf;
1724 int i, toread, sndrnd = 0, pos;
1725
1726 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1727 for (i = 0; i < chip->ecc.steps; i++) {
1728 if (sndrnd) {
1729 pos = eccsize + i * (eccsize + chunk);
1730 if (mtd->writesize > 512)
1731 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1732 else
1733 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1734 } else
1735 sndrnd = 1;
1736 toread = min_t(int, length, chunk);
1737 chip->read_buf(mtd, bufpoi, toread);
1738 bufpoi += toread;
1739 length -= toread;
1740 }
1741 if (length > 0)
1742 chip->read_buf(mtd, bufpoi, length);
1743
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001744 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001745}
1746
1747/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001748 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001749 * @mtd: mtd info structure
1750 * @chip: nand chip info structure
1751 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001752 */
1753static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1754 int page)
1755{
1756 int status = 0;
1757 const uint8_t *buf = chip->oob_poi;
1758 int length = mtd->oobsize;
1759
1760 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1761 chip->write_buf(mtd, buf, length);
1762 /* Send command to program the OOB data */
1763 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1764
1765 status = chip->waitfunc(mtd, chip);
1766
Savin Zlobec0d420f92006-06-21 11:51:20 +02001767 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001768}
1769
1770/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001771 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001772 * with syndrome - only for large page flash
1773 * @mtd: mtd info structure
1774 * @chip: nand chip info structure
1775 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001776 */
1777static int nand_write_oob_syndrome(struct mtd_info *mtd,
1778 struct nand_chip *chip, int page)
1779{
1780 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1781 int eccsize = chip->ecc.size, length = mtd->oobsize;
1782 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1783 const uint8_t *bufpoi = chip->oob_poi;
1784
1785 /*
1786 * data-ecc-data-ecc ... ecc-oob
1787 * or
1788 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1789 */
1790 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1791 pos = steps * (eccsize + chunk);
1792 steps = 0;
1793 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001794 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001795
1796 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1797 for (i = 0; i < steps; i++) {
1798 if (sndcmd) {
1799 if (mtd->writesize <= 512) {
1800 uint32_t fill = 0xFFFFFFFF;
1801
1802 len = eccsize;
1803 while (len > 0) {
1804 int num = min_t(int, len, 4);
1805 chip->write_buf(mtd, (uint8_t *)&fill,
1806 num);
1807 len -= num;
1808 }
1809 } else {
1810 pos = eccsize + i * (eccsize + chunk);
1811 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1812 }
1813 } else
1814 sndcmd = 1;
1815 len = min_t(int, length, chunk);
1816 chip->write_buf(mtd, bufpoi, len);
1817 bufpoi += len;
1818 length -= len;
1819 }
1820 if (length > 0)
1821 chip->write_buf(mtd, bufpoi, length);
1822
1823 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1824 status = chip->waitfunc(mtd, chip);
1825
1826 return status & NAND_STATUS_FAIL ? -EIO : 0;
1827}
1828
1829/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001830 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001831 * @mtd: MTD device structure
1832 * @from: offset to read from
1833 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001835 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001837static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1838 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Brian Norrisc00a0992012-05-01 17:12:54 -07001840 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001841 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001842 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001843 int readlen = ops->ooblen;
1844 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001845 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001846 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Brian Norris289c0522011-07-19 10:06:09 -07001848 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301849 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Brian Norris041e4572011-06-23 16:45:24 -07001851 stats = mtd->ecc_stats;
1852
Brian Norris0612b9d2011-08-30 18:45:40 -07001853 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001854 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001855 else
1856 len = mtd->oobsize;
1857
1858 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001859 pr_debug("%s: attempt to start read outside oob\n",
1860 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001861 return -EINVAL;
1862 }
1863
1864 /* Do not allow reads past end of device */
1865 if (unlikely(from >= mtd->size ||
1866 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1867 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001868 pr_debug("%s: attempt to read beyond end of device\n",
1869 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001870 return -EINVAL;
1871 }
Vitaly Wool70145682006-11-03 18:20:38 +03001872
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001873 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001874 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001876 /* Shift to get page */
1877 realpage = (int)(from >> chip->page_shift);
1878 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Florian Fainellif8ac0412010-09-07 13:23:43 +02001880 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001881 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001882 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001883 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001884 ret = chip->ecc.read_oob(mtd, chip, page);
1885
1886 if (ret < 0)
1887 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001888
1889 len = min(len, readlen);
1890 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001891
Brian Norris5bc7c332013-03-13 09:51:31 -07001892 if (chip->options & NAND_NEED_READRDY) {
1893 /* Apply delay or wait for ready/busy pin */
1894 if (!chip->dev_ready)
1895 udelay(chip->chip_delay);
1896 else
1897 nand_wait_ready(mtd);
1898 }
1899
Vitaly Wool70145682006-11-03 18:20:38 +03001900 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001901 if (!readlen)
1902 break;
1903
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001904 /* Increment page address */
1905 realpage++;
1906
1907 page = realpage & chip->pagemask;
1908 /* Check, if we cross a chip boundary */
1909 if (!page) {
1910 chipnr++;
1911 chip->select_chip(mtd, -1);
1912 chip->select_chip(mtd, chipnr);
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001915 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001917 ops->oobretlen = ops->ooblen - readlen;
1918
1919 if (ret < 0)
1920 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001921
1922 if (mtd->ecc_stats.failed - stats.failed)
1923 return -EBADMSG;
1924
1925 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
1928/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001929 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001930 * @mtd: MTD device structure
1931 * @from: offset to read from
1932 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001934 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001936static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1937 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001939 int ret = -ENOTSUPP;
1940
1941 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001944 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001945 pr_debug("%s: attempt to read beyond end of device\n",
1946 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return -EINVAL;
1948 }
1949
Huang Shijie6a8214a2012-11-19 14:43:30 +08001950 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Florian Fainellif8ac0412010-09-07 13:23:43 +02001952 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001953 case MTD_OPS_PLACE_OOB:
1954 case MTD_OPS_AUTO_OOB:
1955 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001956 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001957
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001958 default:
1959 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001962 if (!ops->datbuf)
1963 ret = nand_do_read_oob(mtd, from, ops);
1964 else
1965 ret = nand_do_read_ops(mtd, from, ops);
1966
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001967out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001969 return ret;
1970}
1971
1972
1973/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001974 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001975 * @mtd: mtd info structure
1976 * @chip: nand chip info structure
1977 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001978 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001979 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001980 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001981 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001982static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001983 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001984{
1985 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001986 if (oob_required)
1987 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001988
1989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001992/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001993 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001994 * @mtd: mtd info structure
1995 * @chip: nand chip info structure
1996 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001997 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001998 *
1999 * We need a special oob layout and handling even when ECC isn't checked.
2000 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002001static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002002 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002003 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002004{
2005 int eccsize = chip->ecc.size;
2006 int eccbytes = chip->ecc.bytes;
2007 uint8_t *oob = chip->oob_poi;
2008 int steps, size;
2009
2010 for (steps = chip->ecc.steps; steps > 0; steps--) {
2011 chip->write_buf(mtd, buf, eccsize);
2012 buf += eccsize;
2013
2014 if (chip->ecc.prepad) {
2015 chip->write_buf(mtd, oob, chip->ecc.prepad);
2016 oob += chip->ecc.prepad;
2017 }
2018
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002019 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002020 oob += eccbytes;
2021
2022 if (chip->ecc.postpad) {
2023 chip->write_buf(mtd, oob, chip->ecc.postpad);
2024 oob += chip->ecc.postpad;
2025 }
2026 }
2027
2028 size = mtd->oobsize - (oob - chip->oob_poi);
2029 if (size)
2030 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002031
2032 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002033}
2034/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002035 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002036 * @mtd: mtd info structure
2037 * @chip: nand chip info structure
2038 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002039 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002040 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002041static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002042 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002043{
2044 int i, eccsize = chip->ecc.size;
2045 int eccbytes = chip->ecc.bytes;
2046 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002047 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002048 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002049 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002050
Brian Norris7854d3f2011-06-23 14:12:08 -07002051 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002052 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2053 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002054
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002055 for (i = 0; i < chip->ecc.total; i++)
2056 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002057
Josh Wufdbad98d2012-06-25 18:07:45 +08002058 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002059}
2060
2061/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002062 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002063 * @mtd: mtd info structure
2064 * @chip: nand chip info structure
2065 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002066 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002067 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002068static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002069 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002070{
2071 int i, eccsize = chip->ecc.size;
2072 int eccbytes = chip->ecc.bytes;
2073 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002074 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002075 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002076 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002077
2078 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2079 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002080 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002081 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2082 }
2083
2084 for (i = 0; i < chip->ecc.total; i++)
2085 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2086
2087 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002088
2089 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002090}
2091
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302092
2093/**
2094 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2095 * @mtd: mtd info structure
2096 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002097 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302098 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002099 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302100 * @oob_required: must write chip->oob_poi to OOB
2101 */
2102static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2103 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002104 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302105 int oob_required)
2106{
2107 uint8_t *oob_buf = chip->oob_poi;
2108 uint8_t *ecc_calc = chip->buffers->ecccalc;
2109 int ecc_size = chip->ecc.size;
2110 int ecc_bytes = chip->ecc.bytes;
2111 int ecc_steps = chip->ecc.steps;
2112 uint32_t *eccpos = chip->ecc.layout->eccpos;
2113 uint32_t start_step = offset / ecc_size;
2114 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2115 int oob_bytes = mtd->oobsize / ecc_steps;
2116 int step, i;
2117
2118 for (step = 0; step < ecc_steps; step++) {
2119 /* configure controller for WRITE access */
2120 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2121
2122 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002123 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302124
2125 /* mask ECC of un-touched subpages by padding 0xFF */
2126 if ((step < start_step) || (step > end_step))
2127 memset(ecc_calc, 0xff, ecc_bytes);
2128 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002129 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302130
2131 /* mask OOB of un-touched subpages by padding 0xFF */
2132 /* if oob_required, preserve OOB metadata of written subpage */
2133 if (!oob_required || (step < start_step) || (step > end_step))
2134 memset(oob_buf, 0xff, oob_bytes);
2135
Brian Norrisd6a950802013-08-08 17:16:36 -07002136 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302137 ecc_calc += ecc_bytes;
2138 oob_buf += oob_bytes;
2139 }
2140
2141 /* copy calculated ECC for whole page to chip->buffer->oob */
2142 /* this include masked-value(0xFF) for unwritten subpages */
2143 ecc_calc = chip->buffers->ecccalc;
2144 for (i = 0; i < chip->ecc.total; i++)
2145 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2146
2147 /* write OOB buffer to NAND device */
2148 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2149
2150 return 0;
2151}
2152
2153
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002155 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002156 * @mtd: mtd info structure
2157 * @chip: nand chip info structure
2158 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002159 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002160 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002161 * The hw generator calculates the error syndrome automatically. Therefore we
2162 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002163 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002164static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002165 struct nand_chip *chip,
2166 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002167{
2168 int i, eccsize = chip->ecc.size;
2169 int eccbytes = chip->ecc.bytes;
2170 int eccsteps = chip->ecc.steps;
2171 const uint8_t *p = buf;
2172 uint8_t *oob = chip->oob_poi;
2173
2174 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2175
2176 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2177 chip->write_buf(mtd, p, eccsize);
2178
2179 if (chip->ecc.prepad) {
2180 chip->write_buf(mtd, oob, chip->ecc.prepad);
2181 oob += chip->ecc.prepad;
2182 }
2183
2184 chip->ecc.calculate(mtd, p, oob);
2185 chip->write_buf(mtd, oob, eccbytes);
2186 oob += eccbytes;
2187
2188 if (chip->ecc.postpad) {
2189 chip->write_buf(mtd, oob, chip->ecc.postpad);
2190 oob += chip->ecc.postpad;
2191 }
2192 }
2193
2194 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002195 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002196 if (i)
2197 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002198
2199 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002200}
2201
2202/**
David Woodhouse956e9442006-09-25 17:12:39 +01002203 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002204 * @mtd: MTD device structure
2205 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302206 * @offset: address offset within the page
2207 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002208 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002209 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002210 * @page: page number to write
2211 * @cached: cached programming
2212 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002213 */
2214static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302215 uint32_t offset, int data_len, const uint8_t *buf,
2216 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002217{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302218 int status, subpage;
2219
2220 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2221 chip->ecc.write_subpage)
2222 subpage = offset || (data_len < mtd->writesize);
2223 else
2224 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002225
2226 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2227
David Woodhouse956e9442006-09-25 17:12:39 +01002228 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302229 status = chip->ecc.write_page_raw(mtd, chip, buf,
2230 oob_required);
2231 else if (subpage)
2232 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2233 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002234 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002235 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2236
2237 if (status < 0)
2238 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002239
2240 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002241 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002242 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002243 */
2244 cached = 0;
2245
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002246 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002247
2248 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002249 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002250 /*
2251 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002252 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002253 */
2254 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2255 status = chip->errstat(mtd, chip, FL_WRITING, status,
2256 page);
2257
2258 if (status & NAND_STATUS_FAIL)
2259 return -EIO;
2260 } else {
2261 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002262 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002263 }
2264
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002265 return 0;
2266}
2267
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002268/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002269 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002270 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002271 * @oob: oob data buffer
2272 * @len: oob data write length
2273 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002274 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002275static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2276 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002277{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002278 struct nand_chip *chip = mtd->priv;
2279
2280 /*
2281 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2282 * data from a previous OOB read.
2283 */
2284 memset(chip->oob_poi, 0xff, mtd->oobsize);
2285
Florian Fainellif8ac0412010-09-07 13:23:43 +02002286 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002287
Brian Norris0612b9d2011-08-30 18:45:40 -07002288 case MTD_OPS_PLACE_OOB:
2289 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002290 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2291 return oob + len;
2292
Brian Norris0612b9d2011-08-30 18:45:40 -07002293 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002294 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002295 uint32_t boffs = 0, woffs = ops->ooboffs;
2296 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002297
Florian Fainellif8ac0412010-09-07 13:23:43 +02002298 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002299 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002300 if (unlikely(woffs)) {
2301 if (woffs >= free->length) {
2302 woffs -= free->length;
2303 continue;
2304 }
2305 boffs = free->offset + woffs;
2306 bytes = min_t(size_t, len,
2307 (free->length - woffs));
2308 woffs = 0;
2309 } else {
2310 bytes = min_t(size_t, len, free->length);
2311 boffs = free->offset;
2312 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002313 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002314 oob += bytes;
2315 }
2316 return oob;
2317 }
2318 default:
2319 BUG();
2320 }
2321 return NULL;
2322}
2323
Florian Fainellif8ac0412010-09-07 13:23:43 +02002324#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002325
2326/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002327 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002328 * @mtd: MTD device structure
2329 * @to: offset to write to
2330 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002331 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002332 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002333 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002334static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2335 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002336{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002337 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002338 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002339 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002340
2341 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002342 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002343 mtd->oobavail : mtd->oobsize;
2344
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002345 uint8_t *oob = ops->oobbuf;
2346 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302347 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002348 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002349
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002350 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002351 if (!writelen)
2352 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002353
Brian Norris8b6e50c2011-05-25 14:59:01 -07002354 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002355 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002356 pr_notice("%s: attempt to write non page aligned data\n",
2357 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002358 return -EINVAL;
2359 }
2360
Thomas Gleixner29072b92006-09-28 15:38:36 +02002361 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002362
Thomas Gleixner6a930962006-06-28 00:11:45 +02002363 chipnr = (int)(to >> chip->chip_shift);
2364 chip->select_chip(mtd, chipnr);
2365
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002366 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002367 if (nand_check_wp(mtd)) {
2368 ret = -EIO;
2369 goto err_out;
2370 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002371
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002372 realpage = (int)(to >> chip->page_shift);
2373 page = realpage & chip->pagemask;
2374 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2375
2376 /* Invalidate the page cache, when we write to the cached page */
2377 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002378 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002379 chip->pagebuf = -1;
2380
Maxim Levitsky782ce792010-02-22 20:39:36 +02002381 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002382 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2383 ret = -EINVAL;
2384 goto err_out;
2385 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002386
Florian Fainellif8ac0412010-09-07 13:23:43 +02002387 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002388 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002389 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002390 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002391 int use_bufpoi;
2392 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002393
Kamal Dasu66507c72014-05-01 20:51:19 -04002394 if (part_pagewr)
2395 use_bufpoi = 1;
2396 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2397 use_bufpoi = !virt_addr_valid(buf);
2398 else
2399 use_bufpoi = 0;
2400
2401 /* Partial page write?, or need to use bounce buffer */
2402 if (use_bufpoi) {
2403 pr_debug("%s: using write bounce buffer for buf@%p\n",
2404 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002405 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002406 if (part_pagewr)
2407 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002408 chip->pagebuf = -1;
2409 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2410 memcpy(&chip->buffers->databuf[column], buf, bytes);
2411 wbuf = chip->buffers->databuf;
2412 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002413
Maxim Levitsky782ce792010-02-22 20:39:36 +02002414 if (unlikely(oob)) {
2415 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002416 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002417 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002418 } else {
2419 /* We still need to erase leftover OOB data */
2420 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002421 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302422 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2423 oob_required, page, cached,
2424 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002425 if (ret)
2426 break;
2427
2428 writelen -= bytes;
2429 if (!writelen)
2430 break;
2431
Thomas Gleixner29072b92006-09-28 15:38:36 +02002432 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002433 buf += bytes;
2434 realpage++;
2435
2436 page = realpage & chip->pagemask;
2437 /* Check, if we cross a chip boundary */
2438 if (!page) {
2439 chipnr++;
2440 chip->select_chip(mtd, -1);
2441 chip->select_chip(mtd, chipnr);
2442 }
2443 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002444
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002445 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002446 if (unlikely(oob))
2447 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002448
2449err_out:
2450 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002451 return ret;
2452}
2453
2454/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002455 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002456 * @mtd: MTD device structure
2457 * @to: offset to write to
2458 * @len: number of bytes to write
2459 * @retlen: pointer to variable to store the number of written bytes
2460 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002461 *
2462 * NAND write with ECC. Used when performing writes in interrupt context, this
2463 * may for example be called by mtdoops when writing an oops while in panic.
2464 */
2465static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2466 size_t *retlen, const uint8_t *buf)
2467{
2468 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002469 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002470 int ret;
2471
Brian Norris8b6e50c2011-05-25 14:59:01 -07002472 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002473 panic_nand_wait(mtd, chip, 400);
2474
Brian Norris8b6e50c2011-05-25 14:59:01 -07002475 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002476 panic_nand_get_device(chip, mtd, FL_WRITING);
2477
Brian Norris4a89ff82011-08-30 18:45:45 -07002478 ops.len = len;
2479 ops.datbuf = (uint8_t *)buf;
2480 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002481 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002482
Brian Norris4a89ff82011-08-30 18:45:45 -07002483 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002484
Brian Norris4a89ff82011-08-30 18:45:45 -07002485 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002486 return ret;
2487}
2488
2489/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002490 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002491 * @mtd: MTD device structure
2492 * @to: offset to write to
2493 * @len: number of bytes to write
2494 * @retlen: pointer to variable to store the number of written bytes
2495 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002497 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002499static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002500 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
Brian Norris4a89ff82011-08-30 18:45:45 -07002502 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002503 int ret;
2504
Huang Shijie6a8214a2012-11-19 14:43:30 +08002505 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002506 ops.len = len;
2507 ops.datbuf = (uint8_t *)buf;
2508 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002509 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002510 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002511 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002512 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002513 return ret;
2514}
2515
2516/**
2517 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002518 * @mtd: MTD device structure
2519 * @to: offset to write to
2520 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002522 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002523 */
2524static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2525 struct mtd_oob_ops *ops)
2526{
Adrian Hunter03736152007-01-31 17:58:29 +02002527 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002528 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Brian Norris289c0522011-07-19 10:06:09 -07002530 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302531 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Brian Norris0612b9d2011-08-30 18:45:40 -07002533 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002534 len = chip->ecc.layout->oobavail;
2535 else
2536 len = mtd->oobsize;
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002539 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002540 pr_debug("%s: attempt to write past end of page\n",
2541 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return -EINVAL;
2543 }
2544
Adrian Hunter03736152007-01-31 17:58:29 +02002545 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002546 pr_debug("%s: attempt to start write outside oob\n",
2547 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002548 return -EINVAL;
2549 }
2550
Jason Liu775adc32011-02-25 13:06:18 +08002551 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002552 if (unlikely(to >= mtd->size ||
2553 ops->ooboffs + ops->ooblen >
2554 ((mtd->size >> chip->page_shift) -
2555 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002556 pr_debug("%s: attempt to write beyond end of device\n",
2557 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002558 return -EINVAL;
2559 }
2560
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002561 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002562 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002564 /* Shift to get page */
2565 page = (int)(to >> chip->page_shift);
2566
2567 /*
2568 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2569 * of my DiskOnChip 2000 test units) will clear the whole data page too
2570 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2571 * it in the doc2000 driver in August 1999. dwmw2.
2572 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002573 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002576 if (nand_check_wp(mtd)) {
2577 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002578 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002579 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002582 if (page == chip->pagebuf)
2583 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002585 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002586
Brian Norris0612b9d2011-08-30 18:45:40 -07002587 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002588 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2589 else
2590 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002591
Huang Shijieb0bb6902012-11-19 14:43:29 +08002592 chip->select_chip(mtd, -1);
2593
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002594 if (status)
2595 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Vitaly Wool70145682006-11-03 18:20:38 +03002597 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002599 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002602/**
2603 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002604 * @mtd: MTD device structure
2605 * @to: offset to write to
2606 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002607 */
2608static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2609 struct mtd_oob_ops *ops)
2610{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002611 int ret = -ENOTSUPP;
2612
2613 ops->retlen = 0;
2614
2615 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002616 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002617 pr_debug("%s: attempt to write beyond end of device\n",
2618 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002619 return -EINVAL;
2620 }
2621
Huang Shijie6a8214a2012-11-19 14:43:30 +08002622 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002623
Florian Fainellif8ac0412010-09-07 13:23:43 +02002624 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002625 case MTD_OPS_PLACE_OOB:
2626 case MTD_OPS_AUTO_OOB:
2627 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002628 break;
2629
2630 default:
2631 goto out;
2632 }
2633
2634 if (!ops->datbuf)
2635 ret = nand_do_write_oob(mtd, to, ops);
2636 else
2637 ret = nand_do_write_ops(mtd, to, ops);
2638
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002639out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002640 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return ret;
2642}
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644/**
Brian Norris49c50b92014-05-06 16:02:19 -07002645 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002646 * @mtd: MTD device structure
2647 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
Brian Norris49c50b92014-05-06 16:02:19 -07002649 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 */
Brian Norris49c50b92014-05-06 16:02:19 -07002651static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002653 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2656 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002657
2658 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
2661/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002663 * @mtd: MTD device structure
2664 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002666 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002668static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
David Woodhousee0c7d762006-05-13 18:07:53 +01002670 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002674 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002675 * @mtd: MTD device structure
2676 * @instr: erase instruction
2677 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002679 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002681int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2682 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Adrian Hunter69423d92008-12-10 13:37:21 +00002684 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002685 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002686 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Brian Norris289c0522011-07-19 10:06:09 -07002688 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2689 __func__, (unsigned long long)instr->addr,
2690 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302692 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002696 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002699 page = (int)(instr->addr >> chip->page_shift);
2700 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002703 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002706 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /* Check, if it is write protected */
2709 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002710 pr_debug("%s: device is write protected!\n",
2711 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 instr->state = MTD_ERASE_FAILED;
2713 goto erase_exit;
2714 }
2715
2716 /* Loop through the pages */
2717 len = instr->len;
2718
2719 instr->state = MTD_ERASING;
2720
2721 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002722 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2724 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002725 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2726 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 instr->state = MTD_ERASE_FAILED;
2728 goto erase_exit;
2729 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002730
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002731 /*
2732 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002733 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002734 */
2735 if (page <= chip->pagebuf && chip->pagebuf <
2736 (page + pages_per_block))
2737 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Brian Norris49c50b92014-05-06 16:02:19 -07002739 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002741 /*
2742 * See if operation failed and additional status checks are
2743 * available
2744 */
2745 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2746 status = chip->errstat(mtd, chip, FL_ERASING,
2747 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002750 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002751 pr_debug("%s: failed erase, page 0x%08x\n",
2752 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002754 instr->fail_addr =
2755 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 goto erase_exit;
2757 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002760 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 page += pages_per_block;
2762
2763 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002764 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002766 chip->select_chip(mtd, -1);
2767 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769 }
2770 instr->state = MTD_ERASE_DONE;
2771
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002772erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002777 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 nand_release_device(mtd);
2779
David Woodhouse49defc02007-10-06 15:01:59 -04002780 /* Do call back function */
2781 if (!ret)
2782 mtd_erase_callback(instr);
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 /* Return more or less happy */
2785 return ret;
2786}
2787
2788/**
2789 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002790 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002792 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002794static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Brian Norris289c0522011-07-19 10:06:09 -07002796 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002799 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002801 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002805 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002806 * @mtd: MTD device structure
2807 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002809static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002811 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002815 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002816 * @mtd: MTD device structure
2817 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002819static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 int ret;
2822
Florian Fainellif8ac0412010-09-07 13:23:43 +02002823 ret = nand_block_isbad(mtd, ofs);
2824 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002825 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (ret > 0)
2827 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002828 return ret;
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Brian Norris5a0edb22013-07-30 17:52:58 -07002831 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
2834/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002835 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2836 * @mtd: MTD device structure
2837 * @chip: nand chip info structure
2838 * @addr: feature address.
2839 * @subfeature_param: the subfeature parameters, a four bytes array.
2840 */
2841static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2842 int addr, uint8_t *subfeature_param)
2843{
2844 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002845 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002846
David Mosbergerd914c932013-05-29 15:30:13 +03002847 if (!chip->onfi_version ||
2848 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2849 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002850 return -EINVAL;
2851
2852 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002853 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2854 chip->write_byte(mtd, subfeature_param[i]);
2855
Huang Shijie7db03ec2012-09-13 14:57:52 +08002856 status = chip->waitfunc(mtd, chip);
2857 if (status & NAND_STATUS_FAIL)
2858 return -EIO;
2859 return 0;
2860}
2861
2862/**
2863 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2864 * @mtd: MTD device structure
2865 * @chip: nand chip info structure
2866 * @addr: feature address.
2867 * @subfeature_param: the subfeature parameters, a four bytes array.
2868 */
2869static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2870 int addr, uint8_t *subfeature_param)
2871{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002872 int i;
2873
David Mosbergerd914c932013-05-29 15:30:13 +03002874 if (!chip->onfi_version ||
2875 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2876 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002877 return -EINVAL;
2878
2879 /* clear the sub feature parameters */
2880 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2881
2882 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002883 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2884 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002885 return 0;
2886}
2887
2888/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002889 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002890 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002891 */
2892static int nand_suspend(struct mtd_info *mtd)
2893{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002894 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002895}
2896
2897/**
2898 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002899 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002900 */
2901static void nand_resume(struct mtd_info *mtd)
2902{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002903 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002904
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002905 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002906 nand_release_device(mtd);
2907 else
Brian Norrisd0370212011-07-19 10:06:08 -07002908 pr_err("%s called for a chip which is not in suspended state\n",
2909 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002910}
2911
Brian Norris8b6e50c2011-05-25 14:59:01 -07002912/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002913static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002916 if (!chip->chip_delay)
2917 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002920 if (chip->cmdfunc == NULL)
2921 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002924 if (chip->waitfunc == NULL)
2925 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002927 if (!chip->select_chip)
2928 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002929
Huang Shijie4204ccc2013-08-16 10:10:07 +08002930 /* set for ONFI nand */
2931 if (!chip->onfi_set_features)
2932 chip->onfi_set_features = nand_onfi_set_features;
2933 if (!chip->onfi_get_features)
2934 chip->onfi_get_features = nand_onfi_get_features;
2935
Brian Norris68e80782013-07-18 01:17:02 -07002936 /* If called twice, pointers that depend on busw may need to be reset */
2937 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002938 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2939 if (!chip->read_word)
2940 chip->read_word = nand_read_word;
2941 if (!chip->block_bad)
2942 chip->block_bad = nand_block_bad;
2943 if (!chip->block_markbad)
2944 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002945 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002946 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002947 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2948 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07002949 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002950 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002951 if (!chip->scan_bbt)
2952 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002953
2954 if (!chip->controller) {
2955 chip->controller = &chip->hwcontrol;
2956 spin_lock_init(&chip->controller->lock);
2957 init_waitqueue_head(&chip->controller->wq);
2958 }
2959
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002960}
2961
Brian Norris8b6e50c2011-05-25 14:59:01 -07002962/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002963static void sanitize_string(uint8_t *s, size_t len)
2964{
2965 ssize_t i;
2966
Brian Norris8b6e50c2011-05-25 14:59:01 -07002967 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002968 s[len - 1] = 0;
2969
Brian Norris8b6e50c2011-05-25 14:59:01 -07002970 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002971 for (i = 0; i < len - 1; i++) {
2972 if (s[i] < ' ' || s[i] > 127)
2973 s[i] = '?';
2974 }
2975
Brian Norris8b6e50c2011-05-25 14:59:01 -07002976 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002977 strim(s);
2978}
2979
2980static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2981{
2982 int i;
2983 while (len--) {
2984 crc ^= *p++ << 8;
2985 for (i = 0; i < 8; i++)
2986 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2987 }
2988
2989 return crc;
2990}
2991
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002992/* Parse the Extended Parameter Page. */
2993static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2994 struct nand_chip *chip, struct nand_onfi_params *p)
2995{
2996 struct onfi_ext_param_page *ep;
2997 struct onfi_ext_section *s;
2998 struct onfi_ext_ecc_info *ecc;
2999 uint8_t *cursor;
3000 int ret = -EINVAL;
3001 int len;
3002 int i;
3003
3004 len = le16_to_cpu(p->ext_param_page_length) * 16;
3005 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003006 if (!ep)
3007 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003008
3009 /* Send our own NAND_CMD_PARAM. */
3010 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3011
3012 /* Use the Change Read Column command to skip the ONFI param pages. */
3013 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3014 sizeof(*p) * p->num_of_param_pages , -1);
3015
3016 /* Read out the Extended Parameter Page. */
3017 chip->read_buf(mtd, (uint8_t *)ep, len);
3018 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3019 != le16_to_cpu(ep->crc))) {
3020 pr_debug("fail in the CRC.\n");
3021 goto ext_out;
3022 }
3023
3024 /*
3025 * Check the signature.
3026 * Do not strictly follow the ONFI spec, maybe changed in future.
3027 */
3028 if (strncmp(ep->sig, "EPPS", 4)) {
3029 pr_debug("The signature is invalid.\n");
3030 goto ext_out;
3031 }
3032
3033 /* find the ECC section. */
3034 cursor = (uint8_t *)(ep + 1);
3035 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3036 s = ep->sections + i;
3037 if (s->type == ONFI_SECTION_TYPE_2)
3038 break;
3039 cursor += s->length * 16;
3040 }
3041 if (i == ONFI_EXT_SECTION_MAX) {
3042 pr_debug("We can not find the ECC section.\n");
3043 goto ext_out;
3044 }
3045
3046 /* get the info we want. */
3047 ecc = (struct onfi_ext_ecc_info *)cursor;
3048
Brian Norris4ae7d222013-09-16 18:20:21 -07003049 if (!ecc->codeword_size) {
3050 pr_debug("Invalid codeword size\n");
3051 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003052 }
3053
Brian Norris4ae7d222013-09-16 18:20:21 -07003054 chip->ecc_strength_ds = ecc->ecc_bits;
3055 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003056 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003057
3058ext_out:
3059 kfree(ep);
3060 return ret;
3061}
3062
Brian Norris8429bb32013-12-03 15:51:09 -08003063static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3064{
3065 struct nand_chip *chip = mtd->priv;
3066 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3067
3068 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3069 feature);
3070}
3071
3072/*
3073 * Configure chip properties from Micron vendor-specific ONFI table
3074 */
3075static void nand_onfi_detect_micron(struct nand_chip *chip,
3076 struct nand_onfi_params *p)
3077{
3078 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3079
3080 if (le16_to_cpu(p->vendor_revision) < 1)
3081 return;
3082
3083 chip->read_retries = micron->read_retry_options;
3084 chip->setup_read_retry = nand_setup_read_retry_micron;
3085}
3086
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003087/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003088 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003089 */
3090static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003091 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003092{
3093 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003094 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003095 int val;
3096
Brian Norris7854d3f2011-06-23 14:12:08 -07003097 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003098 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3099 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3100 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3101 return 0;
3102
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003103 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3104 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003105 for (j = 0; j < sizeof(*p); j++)
3106 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003107 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3108 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003109 break;
3110 }
3111 }
3112
Brian Norrisc7f23a72013-08-13 10:51:55 -07003113 if (i == 3) {
3114 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003115 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003116 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003117
Brian Norris8b6e50c2011-05-25 14:59:01 -07003118 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003119 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003120 if (val & (1 << 5))
3121 chip->onfi_version = 23;
3122 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003123 chip->onfi_version = 22;
3124 else if (val & (1 << 3))
3125 chip->onfi_version = 21;
3126 else if (val & (1 << 2))
3127 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003128 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003129 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003130
3131 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003132 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003133 return 0;
3134 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003135
3136 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3137 sanitize_string(p->model, sizeof(p->model));
3138 if (!mtd->name)
3139 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003140
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003141 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003142
3143 /*
3144 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3145 * (don't ask me who thought of this...). MTD assumes that these
3146 * dimensions will be power-of-2, so just truncate the remaining area.
3147 */
3148 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3149 mtd->erasesize *= mtd->writesize;
3150
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003151 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003152
3153 /* See erasesize comment */
3154 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003155 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003156 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003157
3158 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003159 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003160 else
3161 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003162
Huang Shijie10c86ba2013-05-17 11:17:26 +08003163 if (p->ecc_bits != 0xff) {
3164 chip->ecc_strength_ds = p->ecc_bits;
3165 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003166 } else if (chip->onfi_version >= 21 &&
3167 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3168
3169 /*
3170 * The nand_flash_detect_ext_param_page() uses the
3171 * Change Read Column command which maybe not supported
3172 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3173 * now. We do not replace user supplied command function.
3174 */
3175 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3176 chip->cmdfunc = nand_command_lp;
3177
3178 /* The Extended Parameter Page is supported since ONFI 2.1. */
3179 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003180 pr_warn("Failed to detect ONFI extended param page\n");
3181 } else {
3182 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003183 }
3184
Brian Norris8429bb32013-12-03 15:51:09 -08003185 if (p->jedec_id == NAND_MFR_MICRON)
3186 nand_onfi_detect_micron(chip, p);
3187
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003188 return 1;
3189}
3190
3191/*
Huang Shijie91361812014-02-21 13:39:40 +08003192 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3193 */
3194static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3195 int *busw)
3196{
3197 struct nand_jedec_params *p = &chip->jedec_params;
3198 struct jedec_ecc_info *ecc;
3199 int val;
3200 int i, j;
3201
3202 /* Try JEDEC for unknown chip or LP */
3203 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3204 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3205 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3206 chip->read_byte(mtd) != 'C')
3207 return 0;
3208
3209 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3210 for (i = 0; i < 3; i++) {
3211 for (j = 0; j < sizeof(*p); j++)
3212 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3213
3214 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3215 le16_to_cpu(p->crc))
3216 break;
3217 }
3218
3219 if (i == 3) {
3220 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3221 return 0;
3222 }
3223
3224 /* Check version */
3225 val = le16_to_cpu(p->revision);
3226 if (val & (1 << 2))
3227 chip->jedec_version = 10;
3228 else if (val & (1 << 1))
3229 chip->jedec_version = 1; /* vendor specific version */
3230
3231 if (!chip->jedec_version) {
3232 pr_info("unsupported JEDEC version: %d\n", val);
3233 return 0;
3234 }
3235
3236 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3237 sanitize_string(p->model, sizeof(p->model));
3238 if (!mtd->name)
3239 mtd->name = p->model;
3240
3241 mtd->writesize = le32_to_cpu(p->byte_per_page);
3242
3243 /* Please reference to the comment for nand_flash_detect_onfi. */
3244 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3245 mtd->erasesize *= mtd->writesize;
3246
3247 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3248
3249 /* Please reference to the comment for nand_flash_detect_onfi. */
3250 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3251 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3252 chip->bits_per_cell = p->bits_per_cell;
3253
3254 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3255 *busw = NAND_BUSWIDTH_16;
3256 else
3257 *busw = 0;
3258
3259 /* ECC info */
3260 ecc = &p->ecc_info[0];
3261
3262 if (ecc->codeword_size >= 9) {
3263 chip->ecc_strength_ds = ecc->ecc_bits;
3264 chip->ecc_step_ds = 1 << ecc->codeword_size;
3265 } else {
3266 pr_warn("Invalid codeword size\n");
3267 }
3268
3269 return 1;
3270}
3271
3272/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003273 * nand_id_has_period - Check if an ID string has a given wraparound period
3274 * @id_data: the ID string
3275 * @arrlen: the length of the @id_data array
3276 * @period: the period of repitition
3277 *
3278 * Check if an ID string is repeated within a given sequence of bytes at
3279 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003280 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003281 * if the repetition has a period of @period; otherwise, returns zero.
3282 */
3283static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3284{
3285 int i, j;
3286 for (i = 0; i < period; i++)
3287 for (j = i + period; j < arrlen; j += period)
3288 if (id_data[i] != id_data[j])
3289 return 0;
3290 return 1;
3291}
3292
3293/*
3294 * nand_id_len - Get the length of an ID string returned by CMD_READID
3295 * @id_data: the ID string
3296 * @arrlen: the length of the @id_data array
3297
3298 * Returns the length of the ID string, according to known wraparound/trailing
3299 * zero patterns. If no pattern exists, returns the length of the array.
3300 */
3301static int nand_id_len(u8 *id_data, int arrlen)
3302{
3303 int last_nonzero, period;
3304
3305 /* Find last non-zero byte */
3306 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3307 if (id_data[last_nonzero])
3308 break;
3309
3310 /* All zeros */
3311 if (last_nonzero < 0)
3312 return 0;
3313
3314 /* Calculate wraparound period */
3315 for (period = 1; period < arrlen; period++)
3316 if (nand_id_has_period(id_data, arrlen, period))
3317 break;
3318
3319 /* There's a repeated pattern */
3320 if (period < arrlen)
3321 return period;
3322
3323 /* There are trailing zeros */
3324 if (last_nonzero < arrlen - 1)
3325 return last_nonzero + 1;
3326
3327 /* No pattern detected */
3328 return arrlen;
3329}
3330
Huang Shijie7db906b2013-09-25 14:58:11 +08003331/* Extract the bits of per cell from the 3rd byte of the extended ID */
3332static int nand_get_bits_per_cell(u8 cellinfo)
3333{
3334 int bits;
3335
3336 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3337 bits >>= NAND_CI_CELLTYPE_SHIFT;
3338 return bits + 1;
3339}
3340
Brian Norrise3b88bd2012-09-24 20:40:52 -07003341/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003342 * Many new NAND share similar device ID codes, which represent the size of the
3343 * chip. The rest of the parameters must be decoded according to generic or
3344 * manufacturer-specific "extended ID" decoding patterns.
3345 */
3346static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3347 u8 id_data[8], int *busw)
3348{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003349 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003350 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003351 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003352 /* The 4th id byte is the important one */
3353 extid = id_data[3];
3354
Brian Norrise3b88bd2012-09-24 20:40:52 -07003355 id_len = nand_id_len(id_data, 8);
3356
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003357 /*
3358 * Field definitions are in the following datasheets:
3359 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003360 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003361 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003362 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003363 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3364 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003365 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003366 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003367 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003368 /* Calc pagesize */
3369 mtd->writesize = 2048 << (extid & 0x03);
3370 extid >>= 2;
3371 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003372 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003373 case 1:
3374 mtd->oobsize = 128;
3375 break;
3376 case 2:
3377 mtd->oobsize = 218;
3378 break;
3379 case 3:
3380 mtd->oobsize = 400;
3381 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003382 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003383 mtd->oobsize = 436;
3384 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003385 case 5:
3386 mtd->oobsize = 512;
3387 break;
3388 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003389 mtd->oobsize = 640;
3390 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003391 case 7:
3392 default: /* Other cases are "reserved" (unknown) */
3393 mtd->oobsize = 1024;
3394 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003395 }
3396 extid >>= 2;
3397 /* Calc blocksize */
3398 mtd->erasesize = (128 * 1024) <<
3399 (((extid >> 1) & 0x04) | (extid & 0x03));
3400 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003401 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003402 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003403 unsigned int tmp;
3404
3405 /* Calc pagesize */
3406 mtd->writesize = 2048 << (extid & 0x03);
3407 extid >>= 2;
3408 /* Calc oobsize */
3409 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3410 case 0:
3411 mtd->oobsize = 128;
3412 break;
3413 case 1:
3414 mtd->oobsize = 224;
3415 break;
3416 case 2:
3417 mtd->oobsize = 448;
3418 break;
3419 case 3:
3420 mtd->oobsize = 64;
3421 break;
3422 case 4:
3423 mtd->oobsize = 32;
3424 break;
3425 case 5:
3426 mtd->oobsize = 16;
3427 break;
3428 default:
3429 mtd->oobsize = 640;
3430 break;
3431 }
3432 extid >>= 2;
3433 /* Calc blocksize */
3434 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3435 if (tmp < 0x03)
3436 mtd->erasesize = (128 * 1024) << tmp;
3437 else if (tmp == 0x03)
3438 mtd->erasesize = 768 * 1024;
3439 else
3440 mtd->erasesize = (64 * 1024) << tmp;
3441 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003442 } else {
3443 /* Calc pagesize */
3444 mtd->writesize = 1024 << (extid & 0x03);
3445 extid >>= 2;
3446 /* Calc oobsize */
3447 mtd->oobsize = (8 << (extid & 0x01)) *
3448 (mtd->writesize >> 9);
3449 extid >>= 2;
3450 /* Calc blocksize. Blocksize is multiples of 64KiB */
3451 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3452 extid >>= 2;
3453 /* Get buswidth information */
3454 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003455
3456 /*
3457 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3458 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3459 * follows:
3460 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3461 * 110b -> 24nm
3462 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3463 */
3464 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003465 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003466 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3467 !(id_data[4] & 0x80) /* !BENAND */) {
3468 mtd->oobsize = 32 * mtd->writesize >> 9;
3469 }
3470
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003471 }
3472}
3473
3474/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003475 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3476 * decodes a matching ID table entry and assigns the MTD size parameters for
3477 * the chip.
3478 */
3479static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3480 struct nand_flash_dev *type, u8 id_data[8],
3481 int *busw)
3482{
3483 int maf_id = id_data[0];
3484
3485 mtd->erasesize = type->erasesize;
3486 mtd->writesize = type->pagesize;
3487 mtd->oobsize = mtd->writesize / 32;
3488 *busw = type->options & NAND_BUSWIDTH_16;
3489
Huang Shijie1c195e92013-09-25 14:58:12 +08003490 /* All legacy ID NAND are small-page, SLC */
3491 chip->bits_per_cell = 1;
3492
Brian Norrisf23a4812012-09-24 20:40:51 -07003493 /*
3494 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3495 * some Spansion chips have erasesize that conflicts with size
3496 * listed in nand_ids table.
3497 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3498 */
3499 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3500 && id_data[6] == 0x00 && id_data[7] == 0x00
3501 && mtd->writesize == 512) {
3502 mtd->erasesize = 128 * 1024;
3503 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3504 }
3505}
3506
3507/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003508 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3509 * heuristic patterns using various detected parameters (e.g., manufacturer,
3510 * page size, cell-type information).
3511 */
3512static void nand_decode_bbm_options(struct mtd_info *mtd,
3513 struct nand_chip *chip, u8 id_data[8])
3514{
3515 int maf_id = id_data[0];
3516
3517 /* Set the bad block position */
3518 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3519 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3520 else
3521 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3522
3523 /*
3524 * Bad block marker is stored in the last page of each block on Samsung
3525 * and Hynix MLC devices; stored in first two pages of each block on
3526 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3527 * AMD/Spansion, and Macronix. All others scan only the first page.
3528 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003529 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003530 (maf_id == NAND_MFR_SAMSUNG ||
3531 maf_id == NAND_MFR_HYNIX))
3532 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003533 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003534 (maf_id == NAND_MFR_SAMSUNG ||
3535 maf_id == NAND_MFR_HYNIX ||
3536 maf_id == NAND_MFR_TOSHIBA ||
3537 maf_id == NAND_MFR_AMD ||
3538 maf_id == NAND_MFR_MACRONIX)) ||
3539 (mtd->writesize == 2048 &&
3540 maf_id == NAND_MFR_MICRON))
3541 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3542}
3543
Huang Shijieec6e87e2013-03-15 11:01:00 +08003544static inline bool is_full_id_nand(struct nand_flash_dev *type)
3545{
3546 return type->id_len;
3547}
3548
3549static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3550 struct nand_flash_dev *type, u8 *id_data, int *busw)
3551{
3552 if (!strncmp(type->id, id_data, type->id_len)) {
3553 mtd->writesize = type->pagesize;
3554 mtd->erasesize = type->erasesize;
3555 mtd->oobsize = type->oobsize;
3556
Huang Shijie7db906b2013-09-25 14:58:11 +08003557 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003558 chip->chipsize = (uint64_t)type->chipsize << 20;
3559 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003560 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3561 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003562
3563 *busw = type->options & NAND_BUSWIDTH_16;
3564
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003565 if (!mtd->name)
3566 mtd->name = type->name;
3567
Huang Shijieec6e87e2013-03-15 11:01:00 +08003568 return true;
3569 }
3570 return false;
3571}
3572
Brian Norris7e74c2d2012-09-24 20:40:49 -07003573/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003574 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003575 */
3576static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003577 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003578 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003579 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003580{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003581 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003582 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003583 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003586 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Karl Beldanef89a882008-09-15 14:37:29 +02003588 /*
3589 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003590 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003591 */
3592 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003595 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003598 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003599 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Brian Norris8b6e50c2011-05-25 14:59:01 -07003601 /*
3602 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003603 * interface concerns can cause random data which looks like a
3604 * possibly credible NAND flash to appear. If the two results do
3605 * not match, ignore the device completely.
3606 */
3607
3608 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3609
Brian Norris4aef9b72012-09-24 20:40:48 -07003610 /* Read entire ID string */
3611 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003612 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003613
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003614 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003615 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003616 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003617 return ERR_PTR(-ENODEV);
3618 }
3619
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003620 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003621 type = nand_flash_ids;
3622
Huang Shijieec6e87e2013-03-15 11:01:00 +08003623 for (; type->name != NULL; type++) {
3624 if (is_full_id_nand(type)) {
3625 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3626 goto ident_done;
3627 } else if (*dev_id == type->dev_id) {
3628 break;
3629 }
3630 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003631
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003632 chip->onfi_version = 0;
3633 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003634 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003635 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003636 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003637
3638 /* Check if the chip is JEDEC compliant */
3639 if (nand_flash_detect_jedec(mtd, chip, &busw))
3640 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003641 }
3642
David Woodhouse5e81e882010-02-26 18:32:56 +00003643 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003644 return ERR_PTR(-ENODEV);
3645
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003646 if (!mtd->name)
3647 mtd->name = type->name;
3648
Adrian Hunter69423d92008-12-10 13:37:21 +00003649 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003650
Huang Shijie12a40a52010-09-27 10:43:53 +08003651 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003652 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003653 busw = chip->init_size(mtd, chip, id_data);
3654 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003655 /* Decode parameters from extended ID */
3656 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003657 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003658 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003659 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003660 /* Get chip options */
3661 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003662
Brian Norris8b6e50c2011-05-25 14:59:01 -07003663 /*
3664 * Check if chip is not a Samsung device. Do not clear the
3665 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003666 */
3667 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3668 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3669ident_done:
3670
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003671 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003672 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003673 if (nand_manuf_ids[maf_idx].id == *maf_id)
3674 break;
3675 }
3676
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003677 if (chip->options & NAND_BUSWIDTH_AUTO) {
3678 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3679 chip->options |= busw;
3680 nand_set_defaults(chip, busw);
3681 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3682 /*
3683 * Check, if buswidth is correct. Hardware drivers should set
3684 * chip correct!
3685 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003686 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3687 *maf_id, *dev_id);
3688 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3689 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003690 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3691 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003692 return ERR_PTR(-EINVAL);
3693 }
3694
Brian Norris7e74c2d2012-09-24 20:40:49 -07003695 nand_decode_bbm_options(mtd, chip, id_data);
3696
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003697 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003698 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003699 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003700 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003701
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003702 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003703 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003704 if (chip->chipsize & 0xffffffff)
3705 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003706 else {
3707 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3708 chip->chip_shift += 32 - 1;
3709 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003710
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003711 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003712 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003713
Brian Norris8b6e50c2011-05-25 14:59:01 -07003714 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003715 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3716 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003717
Ezequiel Garcia20171642013-11-25 08:30:31 -03003718 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3719 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003720
3721 if (chip->onfi_version)
3722 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3723 chip->onfi_params.model);
3724 else if (chip->jedec_version)
3725 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3726 chip->jedec_params.model);
3727 else
3728 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3729 type->name);
3730
Ezequiel Garcia20171642013-11-25 08:30:31 -03003731 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003732 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3733 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003734 return type;
3735}
3736
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003737/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003738 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003739 * @mtd: MTD device structure
3740 * @maxchips: number of chips to scan for
3741 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003742 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003743 * This is the first phase of the normal nand_scan() function. It reads the
3744 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003745 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003746 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003747 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003748int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3749 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003750{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003751 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003752 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003753 struct nand_flash_dev *type;
3754
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003755 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003756 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003757
3758 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003759 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3760 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003761
3762 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003763 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003764 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003765 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003766 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
3768
Huang Shijie07300162012-11-09 16:23:45 +08003769 chip->select_chip(mtd, -1);
3770
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003771 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003772 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003773 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003774 /* See comment in nand_get_flash_type for reset */
3775 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003777 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003779 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003780 nand_dev_id != chip->read_byte(mtd)) {
3781 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 break;
Huang Shijie07300162012-11-09 16:23:45 +08003783 }
3784 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003787 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003790 chip->numchips = i;
3791 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
David Woodhouse3b85c322006-09-25 17:06:53 +01003793 return 0;
3794}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003795EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003796
3797
3798/**
3799 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003800 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003801 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003802 * This is the second phase of the normal nand_scan() function. It fills out
3803 * all the uninitialized function pointers with the defaults and scans for a
3804 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003805 */
3806int nand_scan_tail(struct mtd_info *mtd)
3807{
3808 int i;
3809 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003810 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003811 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003812
Brian Norrise2414f42012-02-06 13:44:00 -08003813 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3814 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3815 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3816
Huang Shijief02ea4e2014-01-13 14:27:12 +08003817 if (!(chip->options & NAND_OWN_BUFFERS)) {
3818 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3819 + mtd->oobsize * 3, GFP_KERNEL);
3820 if (!nbuf)
3821 return -ENOMEM;
3822 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3823 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3824 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3825
3826 chip->buffers = nbuf;
3827 } else {
3828 if (!chip->buffers)
3829 return -ENOMEM;
3830 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003831
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003832 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003833 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003834
3835 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003836 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003837 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003838 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003839 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003841 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 break;
3843 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003844 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 break;
3846 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003847 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003849 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003850 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003853 pr_warn("No oob scheme defined for oobsize %d\n",
3854 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 BUG();
3856 }
3857 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003858
David Woodhouse956e9442006-09-25 17:12:39 +01003859 if (!chip->write_page)
3860 chip->write_page = nand_write_page;
3861
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003862 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003863 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003864 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003865 */
David Woodhouse956e9442006-09-25 17:12:39 +01003866
Huang Shijie97de79e02013-10-18 14:20:53 +08003867 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003868 case NAND_ECC_HW_OOB_FIRST:
3869 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003870 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003871 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003872 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003873 BUG();
3874 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003875 if (!ecc->read_page)
3876 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003877
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003878 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003879 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003880 if (!ecc->read_page)
3881 ecc->read_page = nand_read_page_hwecc;
3882 if (!ecc->write_page)
3883 ecc->write_page = nand_write_page_hwecc;
3884 if (!ecc->read_page_raw)
3885 ecc->read_page_raw = nand_read_page_raw;
3886 if (!ecc->write_page_raw)
3887 ecc->write_page_raw = nand_write_page_raw;
3888 if (!ecc->read_oob)
3889 ecc->read_oob = nand_read_oob_std;
3890 if (!ecc->write_oob)
3891 ecc->write_oob = nand_write_oob_std;
3892 if (!ecc->read_subpage)
3893 ecc->read_subpage = nand_read_subpage;
3894 if (!ecc->write_subpage)
3895 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003896
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003897 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003898 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3899 (!ecc->read_page ||
3900 ecc->read_page == nand_read_page_hwecc ||
3901 !ecc->write_page ||
3902 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003903 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003904 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003905 BUG();
3906 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003907 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003908 if (!ecc->read_page)
3909 ecc->read_page = nand_read_page_syndrome;
3910 if (!ecc->write_page)
3911 ecc->write_page = nand_write_page_syndrome;
3912 if (!ecc->read_page_raw)
3913 ecc->read_page_raw = nand_read_page_raw_syndrome;
3914 if (!ecc->write_page_raw)
3915 ecc->write_page_raw = nand_write_page_raw_syndrome;
3916 if (!ecc->read_oob)
3917 ecc->read_oob = nand_read_oob_syndrome;
3918 if (!ecc->write_oob)
3919 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003920
Huang Shijie97de79e02013-10-18 14:20:53 +08003921 if (mtd->writesize >= ecc->size) {
3922 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003923 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3924 BUG();
3925 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003926 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003927 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003928 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003929 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003930 ecc->size, mtd->writesize);
3931 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003933 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003934 ecc->calculate = nand_calculate_ecc;
3935 ecc->correct = nand_correct_data;
3936 ecc->read_page = nand_read_page_swecc;
3937 ecc->read_subpage = nand_read_subpage;
3938 ecc->write_page = nand_write_page_swecc;
3939 ecc->read_page_raw = nand_read_page_raw;
3940 ecc->write_page_raw = nand_write_page_raw;
3941 ecc->read_oob = nand_read_oob_std;
3942 ecc->write_oob = nand_write_oob_std;
3943 if (!ecc->size)
3944 ecc->size = 256;
3945 ecc->bytes = 3;
3946 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003948
Ivan Djelic193bd402011-03-11 11:05:33 +01003949 case NAND_ECC_SOFT_BCH:
3950 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03003951 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003952 BUG();
3953 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003954 ecc->calculate = nand_bch_calculate_ecc;
3955 ecc->correct = nand_bch_correct_data;
3956 ecc->read_page = nand_read_page_swecc;
3957 ecc->read_subpage = nand_read_subpage;
3958 ecc->write_page = nand_write_page_swecc;
3959 ecc->read_page_raw = nand_read_page_raw;
3960 ecc->write_page_raw = nand_write_page_raw;
3961 ecc->read_oob = nand_read_oob_std;
3962 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01003963 /*
3964 * Board driver should supply ecc.size and ecc.bytes values to
3965 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003966 * for details. Otherwise, default to 4 bits for large page
3967 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003968 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003969 if (!ecc->size && (mtd->oobsize >= 64)) {
3970 ecc->size = 512;
3971 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01003972 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003973 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3974 &ecc->layout);
3975 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003976 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003977 BUG();
3978 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003979 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003980 break;
3981
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003982 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003983 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003984 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08003985 ecc->read_page = nand_read_page_raw;
3986 ecc->write_page = nand_write_page_raw;
3987 ecc->read_oob = nand_read_oob_std;
3988 ecc->read_page_raw = nand_read_page_raw;
3989 ecc->write_page_raw = nand_write_page_raw;
3990 ecc->write_oob = nand_write_oob_std;
3991 ecc->size = mtd->writesize;
3992 ecc->bytes = 0;
3993 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08003997 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003998 BUG();
3999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Brian Norris9ce244b2011-08-30 18:45:37 -07004001 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004002 if (!ecc->read_oob_raw)
4003 ecc->read_oob_raw = ecc->read_oob;
4004 if (!ecc->write_oob_raw)
4005 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004006
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004007 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004008 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004009 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004010 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004011 ecc->layout->oobavail = 0;
4012 for (i = 0; ecc->layout->oobfree[i].length
4013 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4014 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4015 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004016
4017 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004018 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004019 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004020 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004021 ecc->steps = mtd->writesize / ecc->size;
4022 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004023 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004024 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004026 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004027
Brian Norris8b6e50c2011-05-25 14:59:01 -07004028 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004029 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004030 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004031 case 2:
4032 mtd->subpage_sft = 1;
4033 break;
4034 case 4:
4035 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004036 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004037 mtd->subpage_sft = 2;
4038 break;
4039 }
4040 }
4041 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4042
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004043 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004044 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004047 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004049 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304050 switch (ecc->mode) {
4051 case NAND_ECC_SOFT:
4052 case NAND_ECC_SOFT_BCH:
4053 if (chip->page_shift > 9)
4054 chip->options |= NAND_SUBPAGE_READ;
4055 break;
4056
4057 default:
4058 break;
4059 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004062 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004063 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4064 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004065 mtd->_erase = nand_erase;
4066 mtd->_point = NULL;
4067 mtd->_unpoint = NULL;
4068 mtd->_read = nand_read;
4069 mtd->_write = nand_write;
4070 mtd->_panic_write = panic_nand_write;
4071 mtd->_read_oob = nand_read_oob;
4072 mtd->_write_oob = nand_write_oob;
4073 mtd->_sync = nand_sync;
4074 mtd->_lock = NULL;
4075 mtd->_unlock = NULL;
4076 mtd->_suspend = nand_suspend;
4077 mtd->_resume = nand_resume;
4078 mtd->_block_isbad = nand_block_isbad;
4079 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004080 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Mike Dunn6a918ba2012-03-11 14:21:11 -07004082 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004083 mtd->ecclayout = ecc->layout;
4084 mtd->ecc_strength = ecc->strength;
4085 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004086 /*
4087 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4088 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4089 * properly set.
4090 */
4091 if (!mtd->bitflip_threshold)
4092 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004094 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004095 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004099 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004101EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
Brian Norris8b6e50c2011-05-25 14:59:01 -07004103/*
4104 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004105 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004106 * to call us from in-kernel code if the core NAND support is modular.
4107 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004108#ifdef MODULE
4109#define caller_is_module() (1)
4110#else
4111#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004112 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004113#endif
4114
4115/**
4116 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004117 * @mtd: MTD device structure
4118 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004119 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004120 * This fills out all the uninitialized function pointers with the defaults.
4121 * The flash ID is read and the mtd/chip structures are filled with the
4122 * appropriate values. The mtd->owner field must be set to the module of the
4123 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004124 */
4125int nand_scan(struct mtd_info *mtd, int maxchips)
4126{
4127 int ret;
4128
4129 /* Many callers got this wrong, so check for it for a while... */
4130 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004131 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004132 BUG();
4133 }
4134
David Woodhouse5e81e882010-02-26 18:32:56 +00004135 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004136 if (!ret)
4137 ret = nand_scan_tail(mtd);
4138 return ret;
4139}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004140EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004143 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004144 * @mtd: MTD device structure
4145 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004146void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004148 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Ivan Djelic193bd402011-03-11 11:05:33 +01004150 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4151 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4152
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004153 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Jesper Juhlfa671642005-11-07 01:01:27 -08004155 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004156 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004157 if (!(chip->options & NAND_OWN_BUFFERS))
4158 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004159
4160 /* Free bad block descriptor memory */
4161 if (chip->badblock_pattern && chip->badblock_pattern->options
4162 & NAND_BBT_DYNAMICSTRUCT)
4163 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
David Woodhousee0c7d762006-05-13 18:07:53 +01004165EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004166
4167static int __init nand_base_init(void)
4168{
4169 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4170 return 0;
4171}
4172
4173static void __exit nand_base_exit(void)
4174{
4175 led_trigger_unregister_simple(nand_led_trigger);
4176}
4177
4178module_init(nand_base_init);
4179module_exit(nand_base_exit);
4180
David Woodhousee0c7d762006-05-13 18:07:53 +01004181MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004182MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4183MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004184MODULE_DESCRIPTION("Generic NAND flash driver code");