blob: 0c505dd1f522f6ba7b5880298eb5f7050b86d989 [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
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300491 *
492 * Check if the block is mark as reserved.
493 */
494static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
495{
496 struct nand_chip *chip = mtd->priv;
497
498 if (!chip->bbt)
499 return 0;
500 /* Return info from the table */
501 return nand_isreserved_bbt(mtd, ofs);
502}
503
504/**
505 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
506 * @mtd: MTD device structure
507 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700508 * @getchip: 0, if the chip is already selected
509 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 *
511 * Check, if the block is bad. Either by reading the bad block table or
512 * calling of the scan function.
513 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200514static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
515 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200517 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000518
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200519 if (!chip->bbt)
520 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100523 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200526/**
527 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700528 * @mtd: MTD device structure
529 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200530 *
531 * Helper function for nand_wait_ready used when needing to wait in interrupt
532 * context.
533 */
534static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
535{
536 struct nand_chip *chip = mtd->priv;
537 int i;
538
539 /* Wait for the device to get ready */
540 for (i = 0; i < timeo; i++) {
541 if (chip->dev_ready(mtd))
542 break;
543 touch_softlockup_watchdog();
544 mdelay(1);
545 }
546}
547
Brian Norris7854d3f2011-06-23 14:12:08 -0700548/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100549void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000550{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200551 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100552 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000553
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200554 /* 400ms timeout */
555 if (in_interrupt() || oops_in_progress)
556 return panic_nand_wait_ready(mtd, 400);
557
Richard Purdie8fe833c2006-03-31 02:31:14 -0800558 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700559 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000560 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200561 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800562 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700563 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000564 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800565 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000566}
David Woodhouse4b648b02006-09-25 17:05:24 +0100567EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/**
570 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700571 * @mtd: MTD device structure
572 * @command: the command to be sent
573 * @column: the column address for this command, -1 if none
574 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700576 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200577 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200579static void nand_command(struct mtd_info *mtd, unsigned int command,
580 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200583 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Brian Norris8b6e50c2011-05-25 14:59:01 -0700585 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (command == NAND_CMD_SEQIN) {
587 int readcmd;
588
Joern Engel28318772006-05-22 23:18:05 +0200589 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200591 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 readcmd = NAND_CMD_READOOB;
593 } else if (column < 256) {
594 /* First 256 bytes --> READ0 */
595 readcmd = NAND_CMD_READ0;
596 } else {
597 column -= 256;
598 readcmd = NAND_CMD_READ1;
599 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200600 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200601 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200603 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Brian Norris8b6e50c2011-05-25 14:59:01 -0700605 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200606 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
607 /* Serially input address */
608 if (column != -1) {
609 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800610 if (chip->options & NAND_BUSWIDTH_16 &&
611 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200612 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200613 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200614 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200616 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200617 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200619 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200620 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200621 if (chip->chipsize > (32 << 20))
622 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200624 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000625
626 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700627 * Program and erase have their own busy handlers status and sequential
628 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 case NAND_CMD_PAGEPROG:
633 case NAND_CMD_ERASE1:
634 case NAND_CMD_ERASE2:
635 case NAND_CMD_SEQIN:
636 case NAND_CMD_STATUS:
637 return;
638
639 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200640 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 udelay(chip->chip_delay);
643 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200644 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200645 chip->cmd_ctrl(mtd,
646 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200647 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
648 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return;
650
David Woodhousee0c7d762006-05-13 18:07:53 +0100651 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000653 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * If we don't have access to the busy pin, we apply the given
655 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100656 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200657 if (!chip->dev_ready) {
658 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700662 /*
663 * Apply this short delay always to ensure that we do wait tWB in
664 * any case on any machine.
665 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100666 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000667
668 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/**
672 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700673 * @mtd: MTD device structure
674 * @command: the command to be sent
675 * @column: the column address for this command, -1 if none
676 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200678 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700679 * devices. We don't have the separate regions as we have in the small page
680 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200682static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
683 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* Emulate NAND_CMD_READOOB */
688 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200689 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 command = NAND_CMD_READ0;
691 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000692
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200693 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400694 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200697 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* Serially input address */
700 if (column != -1) {
701 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800702 if (chip->options & NAND_BUSWIDTH_16 &&
703 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200705 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200706 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200707 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200710 chip->cmd_ctrl(mtd, page_addr, ctrl);
711 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200712 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200714 if (chip->chipsize > (128 << 20))
715 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200716 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200719 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000720
721 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700722 * Program and erase have their own busy handlers status, sequential
723 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 case NAND_CMD_CACHEDPROG:
728 case NAND_CMD_PAGEPROG:
729 case NAND_CMD_ERASE1:
730 case NAND_CMD_ERASE2:
731 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200732 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000734 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200737 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200739 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200740 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
741 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
742 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
743 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200744 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
745 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return;
747
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200748 case NAND_CMD_RNDOUT:
749 /* No ready / busy check necessary */
750 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
751 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
752 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
753 NAND_NCE | NAND_CTRL_CHANGE);
754 return;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200757 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
758 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
759 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
760 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000761
David Woodhousee0c7d762006-05-13 18:07:53 +0100762 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100767 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200768 if (!chip->dev_ready) {
769 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000773
Brian Norris8b6e50c2011-05-25 14:59:01 -0700774 /*
775 * Apply this short delay always to ensure that we do wait tWB in
776 * any case on any machine.
777 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100778 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000779
780 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200784 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700785 * @chip: the nand chip descriptor
786 * @mtd: MTD device structure
787 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200788 *
789 * Used when in panic, no locks are taken.
790 */
791static void panic_nand_get_device(struct nand_chip *chip,
792 struct mtd_info *mtd, int new_state)
793{
Brian Norris7854d3f2011-06-23 14:12:08 -0700794 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200795 chip->controller->active = chip;
796 chip->state = new_state;
797}
798
799/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700801 * @mtd: MTD device structure
802 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
804 * Get the device and lock it for exclusive access
805 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200806static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800807nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800809 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200810 spinlock_t *lock = &chip->controller->lock;
811 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100812 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200813retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100814 spin_lock(lock);
815
vimal singhb8b3ee92009-07-09 20:41:22 +0530816 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200817 if (!chip->controller->active)
818 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200819
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200820 if (chip->controller->active == chip && chip->state == FL_READY) {
821 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100822 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100823 return 0;
824 }
825 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800826 if (chip->controller->active->state == FL_PM_SUSPENDED) {
827 chip->state = FL_PM_SUSPENDED;
828 spin_unlock(lock);
829 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800830 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100831 }
832 set_current_state(TASK_UNINTERRUPTIBLE);
833 add_wait_queue(wq, &wait);
834 spin_unlock(lock);
835 schedule();
836 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto retry;
838}
839
840/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700841 * panic_nand_wait - [GENERIC] wait until the command is done
842 * @mtd: MTD device structure
843 * @chip: NAND chip structure
844 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200845 *
846 * Wait for command done. This is a helper function for nand_wait used when
847 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400848 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200849 */
850static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
851 unsigned long timeo)
852{
853 int i;
854 for (i = 0; i < timeo; i++) {
855 if (chip->dev_ready) {
856 if (chip->dev_ready(mtd))
857 break;
858 } else {
859 if (chip->read_byte(mtd) & NAND_STATUS_READY)
860 break;
861 }
862 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200863 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200864}
865
866/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700867 * nand_wait - [DEFAULT] wait until the command is done
868 * @mtd: MTD device structure
869 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700871 * Wait for command done. This applies to erase and program only. Erase can
872 * take up to 400ms and program up to 20ms according to general NAND and
873 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700874 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200875static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200878 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800879 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Richard Purdie8fe833c2006-03-31 02:31:14 -0800881 led_trigger_event(nand_led_trigger, LED_FULL);
882
Brian Norris8b6e50c2011-05-25 14:59:01 -0700883 /*
884 * Apply this short delay always to ensure that we do wait tWB in any
885 * case on any machine.
886 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100887 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200889 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200891 if (in_interrupt() || oops_in_progress)
892 panic_nand_wait(mtd, chip, timeo);
893 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800894 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200895 while (time_before(jiffies, timeo)) {
896 if (chip->dev_ready) {
897 if (chip->dev_ready(mtd))
898 break;
899 } else {
900 if (chip->read_byte(mtd) & NAND_STATUS_READY)
901 break;
902 }
903 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800906 led_trigger_event(nand_led_trigger, LED_OFF);
907
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200908 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100909 /* This can happen if in case of timeout or buggy dev_ready */
910 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return status;
912}
913
914/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700915 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700916 * @mtd: mtd info
917 * @ofs: offset to start unlock from
918 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700919 * @invert: when = 0, unlock the range of blocks within the lower and
920 * upper boundary address
921 * when = 1, unlock the range of blocks outside the boundaries
922 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530923 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700924 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530925 */
926static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
927 uint64_t len, int invert)
928{
929 int ret = 0;
930 int status, page;
931 struct nand_chip *chip = mtd->priv;
932
933 /* Submit address of first page to unlock */
934 page = ofs >> chip->page_shift;
935 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
936
937 /* Submit address of last page to unlock */
938 page = (ofs + len) >> chip->page_shift;
939 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
940 (page | invert) & chip->pagemask);
941
942 /* Call wait ready function */
943 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530944 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400945 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700946 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530947 __func__, status);
948 ret = -EIO;
949 }
950
951 return ret;
952}
953
954/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700955 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700956 * @mtd: mtd info
957 * @ofs: offset to start unlock from
958 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530959 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700960 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530961 */
962int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
963{
964 int ret = 0;
965 int chipnr;
966 struct nand_chip *chip = mtd->priv;
967
Brian Norris289c0522011-07-19 10:06:09 -0700968 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530969 __func__, (unsigned long long)ofs, len);
970
971 if (check_offs_len(mtd, ofs, len))
972 ret = -EINVAL;
973
974 /* Align to last block address if size addresses end of the device */
975 if (ofs + len == mtd->size)
976 len -= mtd->erasesize;
977
Huang Shijie6a8214a2012-11-19 14:43:30 +0800978 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530979
980 /* Shift to get chip number */
981 chipnr = ofs >> chip->chip_shift;
982
983 chip->select_chip(mtd, chipnr);
984
985 /* Check, if it is write protected */
986 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700987 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530988 __func__);
989 ret = -EIO;
990 goto out;
991 }
992
993 ret = __nand_unlock(mtd, ofs, len, 0);
994
995out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800996 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530997 nand_release_device(mtd);
998
999 return ret;
1000}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001001EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301002
1003/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001004 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001005 * @mtd: mtd info
1006 * @ofs: offset to start unlock from
1007 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301008 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001009 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1010 * have this feature, but it allows only to lock all blocks, not for specified
1011 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1012 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301013 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001014 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301015 */
1016int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1017{
1018 int ret = 0;
1019 int chipnr, status, page;
1020 struct nand_chip *chip = mtd->priv;
1021
Brian Norris289c0522011-07-19 10:06:09 -07001022 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301023 __func__, (unsigned long long)ofs, len);
1024
1025 if (check_offs_len(mtd, ofs, len))
1026 ret = -EINVAL;
1027
Huang Shijie6a8214a2012-11-19 14:43:30 +08001028 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301029
1030 /* Shift to get chip number */
1031 chipnr = ofs >> chip->chip_shift;
1032
1033 chip->select_chip(mtd, chipnr);
1034
1035 /* Check, if it is write protected */
1036 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001037 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301038 __func__);
1039 status = MTD_ERASE_FAILED;
1040 ret = -EIO;
1041 goto out;
1042 }
1043
1044 /* Submit address of first page to lock */
1045 page = ofs >> chip->page_shift;
1046 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1047
1048 /* Call wait ready function */
1049 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301050 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001051 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001052 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301053 __func__, status);
1054 ret = -EIO;
1055 goto out;
1056 }
1057
1058 ret = __nand_unlock(mtd, ofs, len, 0x1);
1059
1060out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001061 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301062 nand_release_device(mtd);
1063
1064 return ret;
1065}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001066EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301067
1068/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001069 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001070 * @mtd: mtd info structure
1071 * @chip: nand chip info structure
1072 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001073 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001074 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001075 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001076 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001077 */
1078static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001079 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001080{
1081 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001082 if (oob_required)
1083 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001084 return 0;
1085}
1086
1087/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001088 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001089 * @mtd: mtd info structure
1090 * @chip: nand chip info structure
1091 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001092 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001093 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001094 *
1095 * We need a special oob layout and handling even when OOB isn't used.
1096 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001097static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001098 struct nand_chip *chip, uint8_t *buf,
1099 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001100{
1101 int eccsize = chip->ecc.size;
1102 int eccbytes = chip->ecc.bytes;
1103 uint8_t *oob = chip->oob_poi;
1104 int steps, size;
1105
1106 for (steps = chip->ecc.steps; steps > 0; steps--) {
1107 chip->read_buf(mtd, buf, eccsize);
1108 buf += eccsize;
1109
1110 if (chip->ecc.prepad) {
1111 chip->read_buf(mtd, oob, chip->ecc.prepad);
1112 oob += chip->ecc.prepad;
1113 }
1114
1115 chip->read_buf(mtd, oob, eccbytes);
1116 oob += eccbytes;
1117
1118 if (chip->ecc.postpad) {
1119 chip->read_buf(mtd, oob, chip->ecc.postpad);
1120 oob += chip->ecc.postpad;
1121 }
1122 }
1123
1124 size = mtd->oobsize - (oob - chip->oob_poi);
1125 if (size)
1126 chip->read_buf(mtd, oob, size);
1127
1128 return 0;
1129}
1130
1131/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001132 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001133 * @mtd: mtd info structure
1134 * @chip: nand chip info structure
1135 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001136 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001137 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001138 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001139static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001140 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001142 int i, eccsize = chip->ecc.size;
1143 int eccbytes = chip->ecc.bytes;
1144 int eccsteps = chip->ecc.steps;
1145 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001146 uint8_t *ecc_calc = chip->buffers->ecccalc;
1147 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001148 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001149 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001150
Brian Norris1fbb9382012-05-02 10:14:55 -07001151 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001152
1153 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1154 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1155
1156 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001157 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001158
1159 eccsteps = chip->ecc.steps;
1160 p = buf;
1161
1162 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1163 int stat;
1164
1165 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001166 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001167 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001168 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001169 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001170 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1171 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001172 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001173 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301177 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001178 * @mtd: mtd info structure
1179 * @chip: nand chip info structure
1180 * @data_offs: offset of requested data within the page
1181 * @readlen: data length
1182 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001183 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001184 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001185static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001186 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1187 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001188{
1189 int start_step, end_step, num_steps;
1190 uint32_t *eccpos = chip->ecc.layout->eccpos;
1191 uint8_t *p;
1192 int data_col_addr, i, gaps = 0;
1193 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1194 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301195 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001196 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001197
Brian Norris7854d3f2011-06-23 14:12:08 -07001198 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001199 start_step = data_offs / chip->ecc.size;
1200 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1201 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301202 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001203
Brian Norris8b6e50c2011-05-25 14:59:01 -07001204 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001205 datafrag_len = num_steps * chip->ecc.size;
1206 eccfrag_len = num_steps * chip->ecc.bytes;
1207
1208 data_col_addr = start_step * chip->ecc.size;
1209 /* If we read not a page aligned data */
1210 if (data_col_addr != 0)
1211 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1212
1213 p = bufpoi + data_col_addr;
1214 chip->read_buf(mtd, p, datafrag_len);
1215
Brian Norris8b6e50c2011-05-25 14:59:01 -07001216 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001217 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1218 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1219
Brian Norris8b6e50c2011-05-25 14:59:01 -07001220 /*
1221 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001222 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001223 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001224 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301225 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001226 gaps = 1;
1227 break;
1228 }
1229 }
1230 if (gaps) {
1231 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1232 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1233 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001234 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001235 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001236 * about buswidth alignment in read_buf.
1237 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001238 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001239 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001240 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001241 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001242 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001243 aligned_len++;
1244
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001245 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1246 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001247 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1248 }
1249
1250 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001251 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001252
1253 p = bufpoi + data_col_addr;
1254 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1255 int stat;
1256
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001257 stat = chip->ecc.correct(mtd, p,
1258 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001259 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001260 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001261 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001262 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001263 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1264 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001265 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001266 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001267}
1268
1269/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001270 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001271 * @mtd: mtd info structure
1272 * @chip: nand chip info structure
1273 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001274 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001275 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001276 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001277 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001278 */
1279static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001280 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001281{
1282 int i, eccsize = chip->ecc.size;
1283 int eccbytes = chip->ecc.bytes;
1284 int eccsteps = chip->ecc.steps;
1285 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001286 uint8_t *ecc_calc = chip->buffers->ecccalc;
1287 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001288 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001289 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001290
1291 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1292 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1293 chip->read_buf(mtd, p, eccsize);
1294 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1295 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001296 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001297
1298 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001299 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001300
1301 eccsteps = chip->ecc.steps;
1302 p = buf;
1303
1304 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1305 int stat;
1306
1307 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001308 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001309 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001310 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001311 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001312 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1313 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001314 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001315 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001316}
1317
1318/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001319 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001320 * @mtd: mtd info structure
1321 * @chip: nand chip info structure
1322 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001323 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001324 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001325 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001326 * Hardware ECC for large page chips, require OOB to be read first. For this
1327 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1328 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1329 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1330 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001331 */
1332static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001333 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001334{
1335 int i, eccsize = chip->ecc.size;
1336 int eccbytes = chip->ecc.bytes;
1337 int eccsteps = chip->ecc.steps;
1338 uint8_t *p = buf;
1339 uint8_t *ecc_code = chip->buffers->ecccode;
1340 uint32_t *eccpos = chip->ecc.layout->eccpos;
1341 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001342 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001343
1344 /* Read the OOB area first */
1345 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1346 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1347 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1348
1349 for (i = 0; i < chip->ecc.total; i++)
1350 ecc_code[i] = chip->oob_poi[eccpos[i]];
1351
1352 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1353 int stat;
1354
1355 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1356 chip->read_buf(mtd, p, eccsize);
1357 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1358
1359 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001360 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001361 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001362 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001363 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001364 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1365 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001366 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001367 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001368}
1369
1370/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001371 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001372 * @mtd: mtd info structure
1373 * @chip: nand chip info structure
1374 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001375 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001376 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001377 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001378 * The hw generator calculates the error syndrome automatically. Therefore we
1379 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001380 */
1381static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001382 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001383{
1384 int i, eccsize = chip->ecc.size;
1385 int eccbytes = chip->ecc.bytes;
1386 int eccsteps = chip->ecc.steps;
1387 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001388 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001389 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001390
1391 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1392 int stat;
1393
1394 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1395 chip->read_buf(mtd, p, eccsize);
1396
1397 if (chip->ecc.prepad) {
1398 chip->read_buf(mtd, oob, chip->ecc.prepad);
1399 oob += chip->ecc.prepad;
1400 }
1401
1402 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1403 chip->read_buf(mtd, oob, eccbytes);
1404 stat = chip->ecc.correct(mtd, p, oob, NULL);
1405
Mike Dunn3f91e942012-04-25 12:06:09 -07001406 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001407 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001408 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001409 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001410 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1411 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001412
1413 oob += eccbytes;
1414
1415 if (chip->ecc.postpad) {
1416 chip->read_buf(mtd, oob, chip->ecc.postpad);
1417 oob += chip->ecc.postpad;
1418 }
1419 }
1420
1421 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001422 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001423 if (i)
1424 chip->read_buf(mtd, oob, i);
1425
Mike Dunn3f91e942012-04-25 12:06:09 -07001426 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001427}
1428
1429/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001430 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001431 * @chip: nand chip structure
1432 * @oob: oob destination address
1433 * @ops: oob ops structure
1434 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001435 */
1436static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001437 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001438{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001439 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001440
Brian Norris0612b9d2011-08-30 18:45:40 -07001441 case MTD_OPS_PLACE_OOB:
1442 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001443 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1444 return oob + len;
1445
Brian Norris0612b9d2011-08-30 18:45:40 -07001446 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001447 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001448 uint32_t boffs = 0, roffs = ops->ooboffs;
1449 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450
Florian Fainellif8ac0412010-09-07 13:23:43 +02001451 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001452 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001453 if (unlikely(roffs)) {
1454 if (roffs >= free->length) {
1455 roffs -= free->length;
1456 continue;
1457 }
1458 boffs = free->offset + roffs;
1459 bytes = min_t(size_t, len,
1460 (free->length - roffs));
1461 roffs = 0;
1462 } else {
1463 bytes = min_t(size_t, len, free->length);
1464 boffs = free->offset;
1465 }
1466 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001467 oob += bytes;
1468 }
1469 return oob;
1470 }
1471 default:
1472 BUG();
1473 }
1474 return NULL;
1475}
1476
1477/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001478 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1479 * @mtd: MTD device structure
1480 * @retry_mode: the retry mode to use
1481 *
1482 * Some vendors supply a special command to shift the Vt threshold, to be used
1483 * when there are too many bitflips in a page (i.e., ECC error). After setting
1484 * a new threshold, the host should retry reading the page.
1485 */
1486static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1487{
1488 struct nand_chip *chip = mtd->priv;
1489
1490 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1491
1492 if (retry_mode >= chip->read_retries)
1493 return -EINVAL;
1494
1495 if (!chip->setup_read_retry)
1496 return -EOPNOTSUPP;
1497
1498 return chip->setup_read_retry(mtd, retry_mode);
1499}
1500
1501/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001502 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001503 * @mtd: MTD device structure
1504 * @from: offset to read from
1505 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001506 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001507 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001508 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001509static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1510 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001511{
Brian Norrise47f3db2012-05-02 10:14:56 -07001512 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001513 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001514 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001515 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001516 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001517 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001518 mtd->oobavail : mtd->oobsize;
1519
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001520 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001521 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001522 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001523 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001524 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001526 chipnr = (int)(from >> chip->chip_shift);
1527 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001529 realpage = (int)(from >> chip->page_shift);
1530 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001532 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001534 buf = ops->datbuf;
1535 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001536 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001537
Florian Fainellif8ac0412010-09-07 13:23:43 +02001538 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001539 unsigned int ecc_failures = mtd->ecc_stats.failed;
1540
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001541 bytes = min(mtd->writesize - col, readlen);
1542 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001543
Kamal Dasu66507c72014-05-01 20:51:19 -04001544 if (!aligned)
1545 use_bufpoi = 1;
1546 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1547 use_bufpoi = !virt_addr_valid(buf);
1548 else
1549 use_bufpoi = 0;
1550
Brian Norris8b6e50c2011-05-25 14:59:01 -07001551 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001552 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001553 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1554
1555 if (use_bufpoi && aligned)
1556 pr_debug("%s: using read bounce buffer for buf@%p\n",
1557 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Brian Norrisba84fb52014-01-03 15:13:33 -08001559read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001560 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Mike Dunnedbc45402012-04-25 12:06:11 -07001562 /*
1563 * Now read the page into the buffer. Absent an error,
1564 * the read methods return max bitflips per ecc step.
1565 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001566 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001567 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001568 oob_required,
1569 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001570 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1571 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001572 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001573 col, bytes, bufpoi,
1574 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001575 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001576 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001577 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001578 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001579 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001580 /* Invalidate page cache */
1581 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001582 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001583 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001584
Mike Dunnedbc45402012-04-25 12:06:11 -07001585 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1586
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001587 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001588 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001589 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001590 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001591 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001592 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001593 chip->pagebuf_bitflips = ret;
1594 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001595 /* Invalidate page cache */
1596 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001597 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001598 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001600
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001601 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001602 int toread = min(oobreadlen, max_oobsize);
1603
1604 if (toread) {
1605 oob = nand_transfer_oob(chip,
1606 oob, ops, toread);
1607 oobreadlen -= toread;
1608 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001609 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001610
1611 if (chip->options & NAND_NEED_READRDY) {
1612 /* Apply delay or wait for ready/busy pin */
1613 if (!chip->dev_ready)
1614 udelay(chip->chip_delay);
1615 else
1616 nand_wait_ready(mtd);
1617 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001618
Brian Norrisba84fb52014-01-03 15:13:33 -08001619 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001620 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001621 retry_mode++;
1622 ret = nand_setup_read_retry(mtd,
1623 retry_mode);
1624 if (ret < 0)
1625 break;
1626
1627 /* Reset failures; retry */
1628 mtd->ecc_stats.failed = ecc_failures;
1629 goto read_retry;
1630 } else {
1631 /* No more retry modes; real failure */
1632 ecc_fail = true;
1633 }
1634 }
1635
1636 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001637 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001638 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001639 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001640 max_bitflips = max_t(unsigned int, max_bitflips,
1641 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001644 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001645
Brian Norrisba84fb52014-01-03 15:13:33 -08001646 /* Reset to retry mode 0 */
1647 if (retry_mode) {
1648 ret = nand_setup_read_retry(mtd, 0);
1649 if (ret < 0)
1650 break;
1651 retry_mode = 0;
1652 }
1653
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001654 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Brian Norris8b6e50c2011-05-25 14:59:01 -07001657 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 col = 0;
1659 /* Increment page address */
1660 realpage++;
1661
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001662 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* Check, if we cross a chip boundary */
1664 if (!page) {
1665 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001666 chip->select_chip(mtd, -1);
1667 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001670 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001672 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001673 if (oob)
1674 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Mike Dunn3f91e942012-04-25 12:06:09 -07001676 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001677 return ret;
1678
Brian Norrisb72f3df2013-12-03 11:04:14 -08001679 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001680 return -EBADMSG;
1681
Mike Dunnedbc45402012-04-25 12:06:11 -07001682 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001683}
1684
1685/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001686 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001687 * @mtd: MTD device structure
1688 * @from: offset to read from
1689 * @len: number of bytes to read
1690 * @retlen: pointer to variable to store the number of read bytes
1691 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001692 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001693 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001694 */
1695static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1696 size_t *retlen, uint8_t *buf)
1697{
Brian Norris4a89ff82011-08-30 18:45:45 -07001698 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001699 int ret;
1700
Huang Shijie6a8214a2012-11-19 14:43:30 +08001701 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001702 ops.len = len;
1703 ops.datbuf = buf;
1704 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001705 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001706 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001707 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001708 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001713 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001714 * @mtd: mtd info structure
1715 * @chip: nand chip info structure
1716 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001717 */
1718static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001719 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001720{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001721 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001722 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001723 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001724}
1725
1726/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001727 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001728 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001729 * @mtd: mtd info structure
1730 * @chip: nand chip info structure
1731 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001732 */
1733static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001734 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001735{
1736 uint8_t *buf = chip->oob_poi;
1737 int length = mtd->oobsize;
1738 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1739 int eccsize = chip->ecc.size;
1740 uint8_t *bufpoi = buf;
1741 int i, toread, sndrnd = 0, pos;
1742
1743 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1744 for (i = 0; i < chip->ecc.steps; i++) {
1745 if (sndrnd) {
1746 pos = eccsize + i * (eccsize + chunk);
1747 if (mtd->writesize > 512)
1748 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1749 else
1750 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1751 } else
1752 sndrnd = 1;
1753 toread = min_t(int, length, chunk);
1754 chip->read_buf(mtd, bufpoi, toread);
1755 bufpoi += toread;
1756 length -= toread;
1757 }
1758 if (length > 0)
1759 chip->read_buf(mtd, bufpoi, length);
1760
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001761 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001762}
1763
1764/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001765 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001766 * @mtd: mtd info structure
1767 * @chip: nand chip info structure
1768 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001769 */
1770static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1771 int page)
1772{
1773 int status = 0;
1774 const uint8_t *buf = chip->oob_poi;
1775 int length = mtd->oobsize;
1776
1777 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1778 chip->write_buf(mtd, buf, length);
1779 /* Send command to program the OOB data */
1780 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1781
1782 status = chip->waitfunc(mtd, chip);
1783
Savin Zlobec0d420f92006-06-21 11:51:20 +02001784 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001785}
1786
1787/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001788 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001789 * with syndrome - only for large page flash
1790 * @mtd: mtd info structure
1791 * @chip: nand chip info structure
1792 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001793 */
1794static int nand_write_oob_syndrome(struct mtd_info *mtd,
1795 struct nand_chip *chip, int page)
1796{
1797 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1798 int eccsize = chip->ecc.size, length = mtd->oobsize;
1799 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1800 const uint8_t *bufpoi = chip->oob_poi;
1801
1802 /*
1803 * data-ecc-data-ecc ... ecc-oob
1804 * or
1805 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1806 */
1807 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1808 pos = steps * (eccsize + chunk);
1809 steps = 0;
1810 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001811 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001812
1813 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1814 for (i = 0; i < steps; i++) {
1815 if (sndcmd) {
1816 if (mtd->writesize <= 512) {
1817 uint32_t fill = 0xFFFFFFFF;
1818
1819 len = eccsize;
1820 while (len > 0) {
1821 int num = min_t(int, len, 4);
1822 chip->write_buf(mtd, (uint8_t *)&fill,
1823 num);
1824 len -= num;
1825 }
1826 } else {
1827 pos = eccsize + i * (eccsize + chunk);
1828 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1829 }
1830 } else
1831 sndcmd = 1;
1832 len = min_t(int, length, chunk);
1833 chip->write_buf(mtd, bufpoi, len);
1834 bufpoi += len;
1835 length -= len;
1836 }
1837 if (length > 0)
1838 chip->write_buf(mtd, bufpoi, length);
1839
1840 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1841 status = chip->waitfunc(mtd, chip);
1842
1843 return status & NAND_STATUS_FAIL ? -EIO : 0;
1844}
1845
1846/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001847 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001848 * @mtd: MTD device structure
1849 * @from: offset to read from
1850 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001852 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001854static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1855 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Brian Norrisc00a0992012-05-01 17:12:54 -07001857 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001858 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001859 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001860 int readlen = ops->ooblen;
1861 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001862 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001863 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Brian Norris289c0522011-07-19 10:06:09 -07001865 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301866 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Brian Norris041e4572011-06-23 16:45:24 -07001868 stats = mtd->ecc_stats;
1869
Brian Norris0612b9d2011-08-30 18:45:40 -07001870 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001871 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001872 else
1873 len = mtd->oobsize;
1874
1875 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001876 pr_debug("%s: attempt to start read outside oob\n",
1877 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001878 return -EINVAL;
1879 }
1880
1881 /* Do not allow reads past end of device */
1882 if (unlikely(from >= mtd->size ||
1883 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1884 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001885 pr_debug("%s: attempt to read beyond end of device\n",
1886 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001887 return -EINVAL;
1888 }
Vitaly Wool70145682006-11-03 18:20:38 +03001889
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001890 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001891 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001893 /* Shift to get page */
1894 realpage = (int)(from >> chip->page_shift);
1895 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Florian Fainellif8ac0412010-09-07 13:23:43 +02001897 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001898 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001899 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001900 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001901 ret = chip->ecc.read_oob(mtd, chip, page);
1902
1903 if (ret < 0)
1904 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001905
1906 len = min(len, readlen);
1907 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001908
Brian Norris5bc7c332013-03-13 09:51:31 -07001909 if (chip->options & NAND_NEED_READRDY) {
1910 /* Apply delay or wait for ready/busy pin */
1911 if (!chip->dev_ready)
1912 udelay(chip->chip_delay);
1913 else
1914 nand_wait_ready(mtd);
1915 }
1916
Vitaly Wool70145682006-11-03 18:20:38 +03001917 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001918 if (!readlen)
1919 break;
1920
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001921 /* Increment page address */
1922 realpage++;
1923
1924 page = realpage & chip->pagemask;
1925 /* Check, if we cross a chip boundary */
1926 if (!page) {
1927 chipnr++;
1928 chip->select_chip(mtd, -1);
1929 chip->select_chip(mtd, chipnr);
1930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001932 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001934 ops->oobretlen = ops->ooblen - readlen;
1935
1936 if (ret < 0)
1937 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001938
1939 if (mtd->ecc_stats.failed - stats.failed)
1940 return -EBADMSG;
1941
1942 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001947 * @mtd: MTD device structure
1948 * @from: offset to read from
1949 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001951 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001953static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1954 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001956 int ret = -ENOTSUPP;
1957
1958 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001961 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001962 pr_debug("%s: attempt to read beyond end of device\n",
1963 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return -EINVAL;
1965 }
1966
Huang Shijie6a8214a2012-11-19 14:43:30 +08001967 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Florian Fainellif8ac0412010-09-07 13:23:43 +02001969 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001970 case MTD_OPS_PLACE_OOB:
1971 case MTD_OPS_AUTO_OOB:
1972 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001973 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001974
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001975 default:
1976 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001979 if (!ops->datbuf)
1980 ret = nand_do_read_oob(mtd, from, ops);
1981 else
1982 ret = nand_do_read_ops(mtd, from, ops);
1983
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001986 return ret;
1987}
1988
1989
1990/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001991 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001992 * @mtd: mtd info structure
1993 * @chip: nand chip info structure
1994 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001995 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001996 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001997 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001998 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001999static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002000 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002001{
2002 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002003 if (oob_required)
2004 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002005
2006 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002009/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002010 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002011 * @mtd: mtd info structure
2012 * @chip: nand chip info structure
2013 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002014 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002015 *
2016 * We need a special oob layout and handling even when ECC isn't checked.
2017 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002018static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002019 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002020 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002021{
2022 int eccsize = chip->ecc.size;
2023 int eccbytes = chip->ecc.bytes;
2024 uint8_t *oob = chip->oob_poi;
2025 int steps, size;
2026
2027 for (steps = chip->ecc.steps; steps > 0; steps--) {
2028 chip->write_buf(mtd, buf, eccsize);
2029 buf += eccsize;
2030
2031 if (chip->ecc.prepad) {
2032 chip->write_buf(mtd, oob, chip->ecc.prepad);
2033 oob += chip->ecc.prepad;
2034 }
2035
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002036 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002037 oob += eccbytes;
2038
2039 if (chip->ecc.postpad) {
2040 chip->write_buf(mtd, oob, chip->ecc.postpad);
2041 oob += chip->ecc.postpad;
2042 }
2043 }
2044
2045 size = mtd->oobsize - (oob - chip->oob_poi);
2046 if (size)
2047 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002048
2049 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002050}
2051/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002052 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002053 * @mtd: mtd info structure
2054 * @chip: nand chip info structure
2055 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002056 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002057 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002058static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002059 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002060{
2061 int i, eccsize = chip->ecc.size;
2062 int eccbytes = chip->ecc.bytes;
2063 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002064 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002065 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002066 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002067
Brian Norris7854d3f2011-06-23 14:12:08 -07002068 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002069 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2070 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002071
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002072 for (i = 0; i < chip->ecc.total; i++)
2073 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002074
Josh Wufdbad98d2012-06-25 18:07:45 +08002075 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002076}
2077
2078/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002079 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002080 * @mtd: mtd info structure
2081 * @chip: nand chip info structure
2082 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002083 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002084 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002085static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002086 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002087{
2088 int i, eccsize = chip->ecc.size;
2089 int eccbytes = chip->ecc.bytes;
2090 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002091 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002092 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002093 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002094
2095 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2096 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002097 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002098 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2099 }
2100
2101 for (i = 0; i < chip->ecc.total; i++)
2102 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2103
2104 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002105
2106 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002107}
2108
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302109
2110/**
2111 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2112 * @mtd: mtd info structure
2113 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002114 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302115 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002116 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302117 * @oob_required: must write chip->oob_poi to OOB
2118 */
2119static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2120 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002121 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302122 int oob_required)
2123{
2124 uint8_t *oob_buf = chip->oob_poi;
2125 uint8_t *ecc_calc = chip->buffers->ecccalc;
2126 int ecc_size = chip->ecc.size;
2127 int ecc_bytes = chip->ecc.bytes;
2128 int ecc_steps = chip->ecc.steps;
2129 uint32_t *eccpos = chip->ecc.layout->eccpos;
2130 uint32_t start_step = offset / ecc_size;
2131 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2132 int oob_bytes = mtd->oobsize / ecc_steps;
2133 int step, i;
2134
2135 for (step = 0; step < ecc_steps; step++) {
2136 /* configure controller for WRITE access */
2137 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2138
2139 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002140 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302141
2142 /* mask ECC of un-touched subpages by padding 0xFF */
2143 if ((step < start_step) || (step > end_step))
2144 memset(ecc_calc, 0xff, ecc_bytes);
2145 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002146 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302147
2148 /* mask OOB of un-touched subpages by padding 0xFF */
2149 /* if oob_required, preserve OOB metadata of written subpage */
2150 if (!oob_required || (step < start_step) || (step > end_step))
2151 memset(oob_buf, 0xff, oob_bytes);
2152
Brian Norrisd6a950802013-08-08 17:16:36 -07002153 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302154 ecc_calc += ecc_bytes;
2155 oob_buf += oob_bytes;
2156 }
2157
2158 /* copy calculated ECC for whole page to chip->buffer->oob */
2159 /* this include masked-value(0xFF) for unwritten subpages */
2160 ecc_calc = chip->buffers->ecccalc;
2161 for (i = 0; i < chip->ecc.total; i++)
2162 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2163
2164 /* write OOB buffer to NAND device */
2165 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2166
2167 return 0;
2168}
2169
2170
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002171/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002172 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002173 * @mtd: mtd info structure
2174 * @chip: nand chip info structure
2175 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002176 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002177 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002178 * The hw generator calculates the error syndrome automatically. Therefore we
2179 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002180 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002181static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002182 struct nand_chip *chip,
2183 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002184{
2185 int i, eccsize = chip->ecc.size;
2186 int eccbytes = chip->ecc.bytes;
2187 int eccsteps = chip->ecc.steps;
2188 const uint8_t *p = buf;
2189 uint8_t *oob = chip->oob_poi;
2190
2191 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2192
2193 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2194 chip->write_buf(mtd, p, eccsize);
2195
2196 if (chip->ecc.prepad) {
2197 chip->write_buf(mtd, oob, chip->ecc.prepad);
2198 oob += chip->ecc.prepad;
2199 }
2200
2201 chip->ecc.calculate(mtd, p, oob);
2202 chip->write_buf(mtd, oob, eccbytes);
2203 oob += eccbytes;
2204
2205 if (chip->ecc.postpad) {
2206 chip->write_buf(mtd, oob, chip->ecc.postpad);
2207 oob += chip->ecc.postpad;
2208 }
2209 }
2210
2211 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002212 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002213 if (i)
2214 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002215
2216 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002217}
2218
2219/**
David Woodhouse956e9442006-09-25 17:12:39 +01002220 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002221 * @mtd: MTD device structure
2222 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302223 * @offset: address offset within the page
2224 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002225 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002226 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002227 * @page: page number to write
2228 * @cached: cached programming
2229 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002230 */
2231static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302232 uint32_t offset, int data_len, const uint8_t *buf,
2233 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002234{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302235 int status, subpage;
2236
2237 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2238 chip->ecc.write_subpage)
2239 subpage = offset || (data_len < mtd->writesize);
2240 else
2241 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002242
2243 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2244
David Woodhouse956e9442006-09-25 17:12:39 +01002245 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302246 status = chip->ecc.write_page_raw(mtd, chip, buf,
2247 oob_required);
2248 else if (subpage)
2249 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2250 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002251 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002252 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2253
2254 if (status < 0)
2255 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002256
2257 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002258 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002259 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002260 */
2261 cached = 0;
2262
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002263 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002264
2265 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002266 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002267 /*
2268 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002269 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270 */
2271 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2272 status = chip->errstat(mtd, chip, FL_WRITING, status,
2273 page);
2274
2275 if (status & NAND_STATUS_FAIL)
2276 return -EIO;
2277 } else {
2278 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002279 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002280 }
2281
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002282 return 0;
2283}
2284
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002285/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002286 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002287 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002288 * @oob: oob data buffer
2289 * @len: oob data write length
2290 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002291 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002292static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2293 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002294{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002295 struct nand_chip *chip = mtd->priv;
2296
2297 /*
2298 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2299 * data from a previous OOB read.
2300 */
2301 memset(chip->oob_poi, 0xff, mtd->oobsize);
2302
Florian Fainellif8ac0412010-09-07 13:23:43 +02002303 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002304
Brian Norris0612b9d2011-08-30 18:45:40 -07002305 case MTD_OPS_PLACE_OOB:
2306 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002307 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2308 return oob + len;
2309
Brian Norris0612b9d2011-08-30 18:45:40 -07002310 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002311 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002312 uint32_t boffs = 0, woffs = ops->ooboffs;
2313 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002314
Florian Fainellif8ac0412010-09-07 13:23:43 +02002315 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002316 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002317 if (unlikely(woffs)) {
2318 if (woffs >= free->length) {
2319 woffs -= free->length;
2320 continue;
2321 }
2322 boffs = free->offset + woffs;
2323 bytes = min_t(size_t, len,
2324 (free->length - woffs));
2325 woffs = 0;
2326 } else {
2327 bytes = min_t(size_t, len, free->length);
2328 boffs = free->offset;
2329 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002330 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002331 oob += bytes;
2332 }
2333 return oob;
2334 }
2335 default:
2336 BUG();
2337 }
2338 return NULL;
2339}
2340
Florian Fainellif8ac0412010-09-07 13:23:43 +02002341#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002342
2343/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002344 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002345 * @mtd: MTD device structure
2346 * @to: offset to write to
2347 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002348 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002349 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002350 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002351static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2352 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002353{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002354 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002355 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002356 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002357
2358 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002359 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002360 mtd->oobavail : mtd->oobsize;
2361
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002362 uint8_t *oob = ops->oobbuf;
2363 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302364 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002365 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002366
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002367 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002368 if (!writelen)
2369 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002370
Brian Norris8b6e50c2011-05-25 14:59:01 -07002371 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002372 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002373 pr_notice("%s: attempt to write non page aligned data\n",
2374 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002375 return -EINVAL;
2376 }
2377
Thomas Gleixner29072b92006-09-28 15:38:36 +02002378 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002379
Thomas Gleixner6a930962006-06-28 00:11:45 +02002380 chipnr = (int)(to >> chip->chip_shift);
2381 chip->select_chip(mtd, chipnr);
2382
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002383 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002384 if (nand_check_wp(mtd)) {
2385 ret = -EIO;
2386 goto err_out;
2387 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002388
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002389 realpage = (int)(to >> chip->page_shift);
2390 page = realpage & chip->pagemask;
2391 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2392
2393 /* Invalidate the page cache, when we write to the cached page */
2394 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002395 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002396 chip->pagebuf = -1;
2397
Maxim Levitsky782ce792010-02-22 20:39:36 +02002398 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002399 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2400 ret = -EINVAL;
2401 goto err_out;
2402 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002403
Florian Fainellif8ac0412010-09-07 13:23:43 +02002404 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002405 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002407 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002408 int use_bufpoi;
2409 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002410
Kamal Dasu66507c72014-05-01 20:51:19 -04002411 if (part_pagewr)
2412 use_bufpoi = 1;
2413 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2414 use_bufpoi = !virt_addr_valid(buf);
2415 else
2416 use_bufpoi = 0;
2417
2418 /* Partial page write?, or need to use bounce buffer */
2419 if (use_bufpoi) {
2420 pr_debug("%s: using write bounce buffer for buf@%p\n",
2421 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002422 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002423 if (part_pagewr)
2424 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002425 chip->pagebuf = -1;
2426 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2427 memcpy(&chip->buffers->databuf[column], buf, bytes);
2428 wbuf = chip->buffers->databuf;
2429 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002430
Maxim Levitsky782ce792010-02-22 20:39:36 +02002431 if (unlikely(oob)) {
2432 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002433 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002434 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002435 } else {
2436 /* We still need to erase leftover OOB data */
2437 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002438 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302439 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2440 oob_required, page, cached,
2441 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002442 if (ret)
2443 break;
2444
2445 writelen -= bytes;
2446 if (!writelen)
2447 break;
2448
Thomas Gleixner29072b92006-09-28 15:38:36 +02002449 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002450 buf += bytes;
2451 realpage++;
2452
2453 page = realpage & chip->pagemask;
2454 /* Check, if we cross a chip boundary */
2455 if (!page) {
2456 chipnr++;
2457 chip->select_chip(mtd, -1);
2458 chip->select_chip(mtd, chipnr);
2459 }
2460 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002461
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002463 if (unlikely(oob))
2464 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002465
2466err_out:
2467 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002468 return ret;
2469}
2470
2471/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002472 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002473 * @mtd: MTD device structure
2474 * @to: offset to write to
2475 * @len: number of bytes to write
2476 * @retlen: pointer to variable to store the number of written bytes
2477 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002478 *
2479 * NAND write with ECC. Used when performing writes in interrupt context, this
2480 * may for example be called by mtdoops when writing an oops while in panic.
2481 */
2482static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2483 size_t *retlen, const uint8_t *buf)
2484{
2485 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002486 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002487 int ret;
2488
Brian Norris8b6e50c2011-05-25 14:59:01 -07002489 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002490 panic_nand_wait(mtd, chip, 400);
2491
Brian Norris8b6e50c2011-05-25 14:59:01 -07002492 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002493 panic_nand_get_device(chip, mtd, FL_WRITING);
2494
Brian Norris4a89ff82011-08-30 18:45:45 -07002495 ops.len = len;
2496 ops.datbuf = (uint8_t *)buf;
2497 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002498 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002499
Brian Norris4a89ff82011-08-30 18:45:45 -07002500 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002501
Brian Norris4a89ff82011-08-30 18:45:45 -07002502 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002503 return ret;
2504}
2505
2506/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002507 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002508 * @mtd: MTD device structure
2509 * @to: offset to write to
2510 * @len: number of bytes to write
2511 * @retlen: pointer to variable to store the number of written bytes
2512 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002514 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002516static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002517 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Brian Norris4a89ff82011-08-30 18:45:45 -07002519 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002520 int ret;
2521
Huang Shijie6a8214a2012-11-19 14:43:30 +08002522 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002523 ops.len = len;
2524 ops.datbuf = (uint8_t *)buf;
2525 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002526 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002527 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002528 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002529 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002530 return ret;
2531}
2532
2533/**
2534 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002535 * @mtd: MTD device structure
2536 * @to: offset to write to
2537 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002538 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002539 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002540 */
2541static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2542 struct mtd_oob_ops *ops)
2543{
Adrian Hunter03736152007-01-31 17:58:29 +02002544 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002545 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Brian Norris289c0522011-07-19 10:06:09 -07002547 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302548 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Brian Norris0612b9d2011-08-30 18:45:40 -07002550 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002551 len = chip->ecc.layout->oobavail;
2552 else
2553 len = mtd->oobsize;
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002556 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002557 pr_debug("%s: attempt to write past end of page\n",
2558 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return -EINVAL;
2560 }
2561
Adrian Hunter03736152007-01-31 17:58:29 +02002562 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002563 pr_debug("%s: attempt to start write outside oob\n",
2564 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002565 return -EINVAL;
2566 }
2567
Jason Liu775adc32011-02-25 13:06:18 +08002568 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002569 if (unlikely(to >= mtd->size ||
2570 ops->ooboffs + ops->ooblen >
2571 ((mtd->size >> chip->page_shift) -
2572 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002573 pr_debug("%s: attempt to write beyond end of device\n",
2574 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002575 return -EINVAL;
2576 }
2577
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002578 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002579 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002581 /* Shift to get page */
2582 page = (int)(to >> chip->page_shift);
2583
2584 /*
2585 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2586 * of my DiskOnChip 2000 test units) will clear the whole data page too
2587 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2588 * it in the doc2000 driver in August 1999. dwmw2.
2589 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002590 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002593 if (nand_check_wp(mtd)) {
2594 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002595 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002596 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002599 if (page == chip->pagebuf)
2600 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002602 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002603
Brian Norris0612b9d2011-08-30 18:45:40 -07002604 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002605 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2606 else
2607 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002608
Huang Shijieb0bb6902012-11-19 14:43:29 +08002609 chip->select_chip(mtd, -1);
2610
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002611 if (status)
2612 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Vitaly Wool70145682006-11-03 18:20:38 +03002614 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002616 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002619/**
2620 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002621 * @mtd: MTD device structure
2622 * @to: offset to write to
2623 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002624 */
2625static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2626 struct mtd_oob_ops *ops)
2627{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002628 int ret = -ENOTSUPP;
2629
2630 ops->retlen = 0;
2631
2632 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002633 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002634 pr_debug("%s: attempt to write beyond end of device\n",
2635 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002636 return -EINVAL;
2637 }
2638
Huang Shijie6a8214a2012-11-19 14:43:30 +08002639 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002640
Florian Fainellif8ac0412010-09-07 13:23:43 +02002641 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002642 case MTD_OPS_PLACE_OOB:
2643 case MTD_OPS_AUTO_OOB:
2644 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002645 break;
2646
2647 default:
2648 goto out;
2649 }
2650
2651 if (!ops->datbuf)
2652 ret = nand_do_write_oob(mtd, to, ops);
2653 else
2654 ret = nand_do_write_ops(mtd, to, ops);
2655
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002656out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002657 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return ret;
2659}
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661/**
Brian Norris49c50b92014-05-06 16:02:19 -07002662 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002663 * @mtd: MTD device structure
2664 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 *
Brian Norris49c50b92014-05-06 16:02:19 -07002666 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 */
Brian Norris49c50b92014-05-06 16:02:19 -07002668static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002670 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002672 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2673 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002674
2675 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
2678/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002680 * @mtd: MTD device structure
2681 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002683 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002685static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
David Woodhousee0c7d762006-05-13 18:07:53 +01002687 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002691 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002692 * @mtd: MTD device structure
2693 * @instr: erase instruction
2694 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002696 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002698int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2699 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Adrian Hunter69423d92008-12-10 13:37:21 +00002701 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002702 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002703 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Brian Norris289c0522011-07-19 10:06:09 -07002705 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2706 __func__, (unsigned long long)instr->addr,
2707 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302709 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002713 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002716 page = (int)(instr->addr >> chip->page_shift);
2717 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002720 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002723 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* Check, if it is write protected */
2726 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002727 pr_debug("%s: device is write protected!\n",
2728 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 instr->state = MTD_ERASE_FAILED;
2730 goto erase_exit;
2731 }
2732
2733 /* Loop through the pages */
2734 len = instr->len;
2735
2736 instr->state = MTD_ERASING;
2737
2738 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002739 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002740 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2741 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002742 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2743 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 instr->state = MTD_ERASE_FAILED;
2745 goto erase_exit;
2746 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002747
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002748 /*
2749 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002750 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002751 */
2752 if (page <= chip->pagebuf && chip->pagebuf <
2753 (page + pages_per_block))
2754 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Brian Norris49c50b92014-05-06 16:02:19 -07002756 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002758 /*
2759 * See if operation failed and additional status checks are
2760 * available
2761 */
2762 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2763 status = chip->errstat(mtd, chip, FL_ERASING,
2764 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002767 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002768 pr_debug("%s: failed erase, page 0x%08x\n",
2769 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002771 instr->fail_addr =
2772 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 goto erase_exit;
2774 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002777 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 page += pages_per_block;
2779
2780 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002781 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783 chip->select_chip(mtd, -1);
2784 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786 }
2787 instr->state = MTD_ERASE_DONE;
2788
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002789erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
2791 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002794 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 nand_release_device(mtd);
2796
David Woodhouse49defc02007-10-06 15:01:59 -04002797 /* Do call back function */
2798 if (!ret)
2799 mtd_erase_callback(instr);
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* Return more or less happy */
2802 return ret;
2803}
2804
2805/**
2806 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002807 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002809 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002811static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Brian Norris289c0522011-07-19 10:06:09 -07002813 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002816 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002818 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002822 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002823 * @mtd: MTD device structure
2824 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002826static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002828 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002832 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002833 * @mtd: MTD device structure
2834 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002836static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 int ret;
2839
Florian Fainellif8ac0412010-09-07 13:23:43 +02002840 ret = nand_block_isbad(mtd, ofs);
2841 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002842 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (ret > 0)
2844 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002845 return ret;
2846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Brian Norris5a0edb22013-07-30 17:52:58 -07002848 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
2851/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002852 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2853 * @mtd: MTD device structure
2854 * @chip: nand chip info structure
2855 * @addr: feature address.
2856 * @subfeature_param: the subfeature parameters, a four bytes array.
2857 */
2858static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2859 int addr, uint8_t *subfeature_param)
2860{
2861 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002862 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002863
David Mosbergerd914c932013-05-29 15:30:13 +03002864 if (!chip->onfi_version ||
2865 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2866 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002867 return -EINVAL;
2868
2869 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002870 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2871 chip->write_byte(mtd, subfeature_param[i]);
2872
Huang Shijie7db03ec2012-09-13 14:57:52 +08002873 status = chip->waitfunc(mtd, chip);
2874 if (status & NAND_STATUS_FAIL)
2875 return -EIO;
2876 return 0;
2877}
2878
2879/**
2880 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2881 * @mtd: MTD device structure
2882 * @chip: nand chip info structure
2883 * @addr: feature address.
2884 * @subfeature_param: the subfeature parameters, a four bytes array.
2885 */
2886static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2887 int addr, uint8_t *subfeature_param)
2888{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002889 int i;
2890
David Mosbergerd914c932013-05-29 15:30:13 +03002891 if (!chip->onfi_version ||
2892 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2893 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002894 return -EINVAL;
2895
2896 /* clear the sub feature parameters */
2897 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2898
2899 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002900 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2901 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002902 return 0;
2903}
2904
2905/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002906 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002907 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002908 */
2909static int nand_suspend(struct mtd_info *mtd)
2910{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002911 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002912}
2913
2914/**
2915 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002916 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002917 */
2918static void nand_resume(struct mtd_info *mtd)
2919{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002920 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002921
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002922 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002923 nand_release_device(mtd);
2924 else
Brian Norrisd0370212011-07-19 10:06:08 -07002925 pr_err("%s called for a chip which is not in suspended state\n",
2926 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002927}
2928
Brian Norris8b6e50c2011-05-25 14:59:01 -07002929/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002930static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002933 if (!chip->chip_delay)
2934 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002937 if (chip->cmdfunc == NULL)
2938 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002941 if (chip->waitfunc == NULL)
2942 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002944 if (!chip->select_chip)
2945 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002946
Huang Shijie4204ccc2013-08-16 10:10:07 +08002947 /* set for ONFI nand */
2948 if (!chip->onfi_set_features)
2949 chip->onfi_set_features = nand_onfi_set_features;
2950 if (!chip->onfi_get_features)
2951 chip->onfi_get_features = nand_onfi_get_features;
2952
Brian Norris68e80782013-07-18 01:17:02 -07002953 /* If called twice, pointers that depend on busw may need to be reset */
2954 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002955 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2956 if (!chip->read_word)
2957 chip->read_word = nand_read_word;
2958 if (!chip->block_bad)
2959 chip->block_bad = nand_block_bad;
2960 if (!chip->block_markbad)
2961 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002962 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002963 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002964 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2965 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07002966 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002967 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002968 if (!chip->scan_bbt)
2969 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002970
2971 if (!chip->controller) {
2972 chip->controller = &chip->hwcontrol;
2973 spin_lock_init(&chip->controller->lock);
2974 init_waitqueue_head(&chip->controller->wq);
2975 }
2976
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002977}
2978
Brian Norris8b6e50c2011-05-25 14:59:01 -07002979/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002980static void sanitize_string(uint8_t *s, size_t len)
2981{
2982 ssize_t i;
2983
Brian Norris8b6e50c2011-05-25 14:59:01 -07002984 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002985 s[len - 1] = 0;
2986
Brian Norris8b6e50c2011-05-25 14:59:01 -07002987 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002988 for (i = 0; i < len - 1; i++) {
2989 if (s[i] < ' ' || s[i] > 127)
2990 s[i] = '?';
2991 }
2992
Brian Norris8b6e50c2011-05-25 14:59:01 -07002993 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002994 strim(s);
2995}
2996
2997static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2998{
2999 int i;
3000 while (len--) {
3001 crc ^= *p++ << 8;
3002 for (i = 0; i < 8; i++)
3003 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3004 }
3005
3006 return crc;
3007}
3008
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003009/* Parse the Extended Parameter Page. */
3010static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3011 struct nand_chip *chip, struct nand_onfi_params *p)
3012{
3013 struct onfi_ext_param_page *ep;
3014 struct onfi_ext_section *s;
3015 struct onfi_ext_ecc_info *ecc;
3016 uint8_t *cursor;
3017 int ret = -EINVAL;
3018 int len;
3019 int i;
3020
3021 len = le16_to_cpu(p->ext_param_page_length) * 16;
3022 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003023 if (!ep)
3024 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003025
3026 /* Send our own NAND_CMD_PARAM. */
3027 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3028
3029 /* Use the Change Read Column command to skip the ONFI param pages. */
3030 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3031 sizeof(*p) * p->num_of_param_pages , -1);
3032
3033 /* Read out the Extended Parameter Page. */
3034 chip->read_buf(mtd, (uint8_t *)ep, len);
3035 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3036 != le16_to_cpu(ep->crc))) {
3037 pr_debug("fail in the CRC.\n");
3038 goto ext_out;
3039 }
3040
3041 /*
3042 * Check the signature.
3043 * Do not strictly follow the ONFI spec, maybe changed in future.
3044 */
3045 if (strncmp(ep->sig, "EPPS", 4)) {
3046 pr_debug("The signature is invalid.\n");
3047 goto ext_out;
3048 }
3049
3050 /* find the ECC section. */
3051 cursor = (uint8_t *)(ep + 1);
3052 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3053 s = ep->sections + i;
3054 if (s->type == ONFI_SECTION_TYPE_2)
3055 break;
3056 cursor += s->length * 16;
3057 }
3058 if (i == ONFI_EXT_SECTION_MAX) {
3059 pr_debug("We can not find the ECC section.\n");
3060 goto ext_out;
3061 }
3062
3063 /* get the info we want. */
3064 ecc = (struct onfi_ext_ecc_info *)cursor;
3065
Brian Norris4ae7d222013-09-16 18:20:21 -07003066 if (!ecc->codeword_size) {
3067 pr_debug("Invalid codeword size\n");
3068 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003069 }
3070
Brian Norris4ae7d222013-09-16 18:20:21 -07003071 chip->ecc_strength_ds = ecc->ecc_bits;
3072 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003073 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003074
3075ext_out:
3076 kfree(ep);
3077 return ret;
3078}
3079
Brian Norris8429bb32013-12-03 15:51:09 -08003080static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3081{
3082 struct nand_chip *chip = mtd->priv;
3083 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3084
3085 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3086 feature);
3087}
3088
3089/*
3090 * Configure chip properties from Micron vendor-specific ONFI table
3091 */
3092static void nand_onfi_detect_micron(struct nand_chip *chip,
3093 struct nand_onfi_params *p)
3094{
3095 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3096
3097 if (le16_to_cpu(p->vendor_revision) < 1)
3098 return;
3099
3100 chip->read_retries = micron->read_retry_options;
3101 chip->setup_read_retry = nand_setup_read_retry_micron;
3102}
3103
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003104/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003105 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003106 */
3107static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003108 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003109{
3110 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003111 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003112 int val;
3113
Brian Norris7854d3f2011-06-23 14:12:08 -07003114 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003115 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3116 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3117 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3118 return 0;
3119
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003120 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3121 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003122 for (j = 0; j < sizeof(*p); j++)
3123 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003124 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3125 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003126 break;
3127 }
3128 }
3129
Brian Norrisc7f23a72013-08-13 10:51:55 -07003130 if (i == 3) {
3131 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003132 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003133 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003134
Brian Norris8b6e50c2011-05-25 14:59:01 -07003135 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003136 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003137 if (val & (1 << 5))
3138 chip->onfi_version = 23;
3139 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003140 chip->onfi_version = 22;
3141 else if (val & (1 << 3))
3142 chip->onfi_version = 21;
3143 else if (val & (1 << 2))
3144 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003145 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003146 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003147
3148 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003149 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003150 return 0;
3151 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003152
3153 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3154 sanitize_string(p->model, sizeof(p->model));
3155 if (!mtd->name)
3156 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003157
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003158 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003159
3160 /*
3161 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3162 * (don't ask me who thought of this...). MTD assumes that these
3163 * dimensions will be power-of-2, so just truncate the remaining area.
3164 */
3165 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3166 mtd->erasesize *= mtd->writesize;
3167
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003168 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003169
3170 /* See erasesize comment */
3171 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003172 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003173 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003174
3175 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003176 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003177 else
3178 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003179
Huang Shijie10c86ba2013-05-17 11:17:26 +08003180 if (p->ecc_bits != 0xff) {
3181 chip->ecc_strength_ds = p->ecc_bits;
3182 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003183 } else if (chip->onfi_version >= 21 &&
3184 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3185
3186 /*
3187 * The nand_flash_detect_ext_param_page() uses the
3188 * Change Read Column command which maybe not supported
3189 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3190 * now. We do not replace user supplied command function.
3191 */
3192 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3193 chip->cmdfunc = nand_command_lp;
3194
3195 /* The Extended Parameter Page is supported since ONFI 2.1. */
3196 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003197 pr_warn("Failed to detect ONFI extended param page\n");
3198 } else {
3199 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003200 }
3201
Brian Norris8429bb32013-12-03 15:51:09 -08003202 if (p->jedec_id == NAND_MFR_MICRON)
3203 nand_onfi_detect_micron(chip, p);
3204
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003205 return 1;
3206}
3207
3208/*
Huang Shijie91361812014-02-21 13:39:40 +08003209 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3210 */
3211static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3212 int *busw)
3213{
3214 struct nand_jedec_params *p = &chip->jedec_params;
3215 struct jedec_ecc_info *ecc;
3216 int val;
3217 int i, j;
3218
3219 /* Try JEDEC for unknown chip or LP */
3220 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3221 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3222 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3223 chip->read_byte(mtd) != 'C')
3224 return 0;
3225
3226 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3227 for (i = 0; i < 3; i++) {
3228 for (j = 0; j < sizeof(*p); j++)
3229 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3230
3231 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3232 le16_to_cpu(p->crc))
3233 break;
3234 }
3235
3236 if (i == 3) {
3237 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3238 return 0;
3239 }
3240
3241 /* Check version */
3242 val = le16_to_cpu(p->revision);
3243 if (val & (1 << 2))
3244 chip->jedec_version = 10;
3245 else if (val & (1 << 1))
3246 chip->jedec_version = 1; /* vendor specific version */
3247
3248 if (!chip->jedec_version) {
3249 pr_info("unsupported JEDEC version: %d\n", val);
3250 return 0;
3251 }
3252
3253 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3254 sanitize_string(p->model, sizeof(p->model));
3255 if (!mtd->name)
3256 mtd->name = p->model;
3257
3258 mtd->writesize = le32_to_cpu(p->byte_per_page);
3259
3260 /* Please reference to the comment for nand_flash_detect_onfi. */
3261 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3262 mtd->erasesize *= mtd->writesize;
3263
3264 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3265
3266 /* Please reference to the comment for nand_flash_detect_onfi. */
3267 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3268 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3269 chip->bits_per_cell = p->bits_per_cell;
3270
3271 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3272 *busw = NAND_BUSWIDTH_16;
3273 else
3274 *busw = 0;
3275
3276 /* ECC info */
3277 ecc = &p->ecc_info[0];
3278
3279 if (ecc->codeword_size >= 9) {
3280 chip->ecc_strength_ds = ecc->ecc_bits;
3281 chip->ecc_step_ds = 1 << ecc->codeword_size;
3282 } else {
3283 pr_warn("Invalid codeword size\n");
3284 }
3285
3286 return 1;
3287}
3288
3289/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003290 * nand_id_has_period - Check if an ID string has a given wraparound period
3291 * @id_data: the ID string
3292 * @arrlen: the length of the @id_data array
3293 * @period: the period of repitition
3294 *
3295 * Check if an ID string is repeated within a given sequence of bytes at
3296 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003297 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003298 * if the repetition has a period of @period; otherwise, returns zero.
3299 */
3300static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3301{
3302 int i, j;
3303 for (i = 0; i < period; i++)
3304 for (j = i + period; j < arrlen; j += period)
3305 if (id_data[i] != id_data[j])
3306 return 0;
3307 return 1;
3308}
3309
3310/*
3311 * nand_id_len - Get the length of an ID string returned by CMD_READID
3312 * @id_data: the ID string
3313 * @arrlen: the length of the @id_data array
3314
3315 * Returns the length of the ID string, according to known wraparound/trailing
3316 * zero patterns. If no pattern exists, returns the length of the array.
3317 */
3318static int nand_id_len(u8 *id_data, int arrlen)
3319{
3320 int last_nonzero, period;
3321
3322 /* Find last non-zero byte */
3323 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3324 if (id_data[last_nonzero])
3325 break;
3326
3327 /* All zeros */
3328 if (last_nonzero < 0)
3329 return 0;
3330
3331 /* Calculate wraparound period */
3332 for (period = 1; period < arrlen; period++)
3333 if (nand_id_has_period(id_data, arrlen, period))
3334 break;
3335
3336 /* There's a repeated pattern */
3337 if (period < arrlen)
3338 return period;
3339
3340 /* There are trailing zeros */
3341 if (last_nonzero < arrlen - 1)
3342 return last_nonzero + 1;
3343
3344 /* No pattern detected */
3345 return arrlen;
3346}
3347
Huang Shijie7db906b2013-09-25 14:58:11 +08003348/* Extract the bits of per cell from the 3rd byte of the extended ID */
3349static int nand_get_bits_per_cell(u8 cellinfo)
3350{
3351 int bits;
3352
3353 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3354 bits >>= NAND_CI_CELLTYPE_SHIFT;
3355 return bits + 1;
3356}
3357
Brian Norrise3b88bd2012-09-24 20:40:52 -07003358/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003359 * Many new NAND share similar device ID codes, which represent the size of the
3360 * chip. The rest of the parameters must be decoded according to generic or
3361 * manufacturer-specific "extended ID" decoding patterns.
3362 */
3363static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3364 u8 id_data[8], int *busw)
3365{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003366 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003367 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003368 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003369 /* The 4th id byte is the important one */
3370 extid = id_data[3];
3371
Brian Norrise3b88bd2012-09-24 20:40:52 -07003372 id_len = nand_id_len(id_data, 8);
3373
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003374 /*
3375 * Field definitions are in the following datasheets:
3376 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003377 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003378 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003379 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003380 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3381 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003382 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003383 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003384 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003385 /* Calc pagesize */
3386 mtd->writesize = 2048 << (extid & 0x03);
3387 extid >>= 2;
3388 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003389 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003390 case 1:
3391 mtd->oobsize = 128;
3392 break;
3393 case 2:
3394 mtd->oobsize = 218;
3395 break;
3396 case 3:
3397 mtd->oobsize = 400;
3398 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003399 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003400 mtd->oobsize = 436;
3401 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003402 case 5:
3403 mtd->oobsize = 512;
3404 break;
3405 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003406 mtd->oobsize = 640;
3407 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003408 case 7:
3409 default: /* Other cases are "reserved" (unknown) */
3410 mtd->oobsize = 1024;
3411 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003412 }
3413 extid >>= 2;
3414 /* Calc blocksize */
3415 mtd->erasesize = (128 * 1024) <<
3416 (((extid >> 1) & 0x04) | (extid & 0x03));
3417 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003418 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003419 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003420 unsigned int tmp;
3421
3422 /* Calc pagesize */
3423 mtd->writesize = 2048 << (extid & 0x03);
3424 extid >>= 2;
3425 /* Calc oobsize */
3426 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3427 case 0:
3428 mtd->oobsize = 128;
3429 break;
3430 case 1:
3431 mtd->oobsize = 224;
3432 break;
3433 case 2:
3434 mtd->oobsize = 448;
3435 break;
3436 case 3:
3437 mtd->oobsize = 64;
3438 break;
3439 case 4:
3440 mtd->oobsize = 32;
3441 break;
3442 case 5:
3443 mtd->oobsize = 16;
3444 break;
3445 default:
3446 mtd->oobsize = 640;
3447 break;
3448 }
3449 extid >>= 2;
3450 /* Calc blocksize */
3451 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3452 if (tmp < 0x03)
3453 mtd->erasesize = (128 * 1024) << tmp;
3454 else if (tmp == 0x03)
3455 mtd->erasesize = 768 * 1024;
3456 else
3457 mtd->erasesize = (64 * 1024) << tmp;
3458 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003459 } else {
3460 /* Calc pagesize */
3461 mtd->writesize = 1024 << (extid & 0x03);
3462 extid >>= 2;
3463 /* Calc oobsize */
3464 mtd->oobsize = (8 << (extid & 0x01)) *
3465 (mtd->writesize >> 9);
3466 extid >>= 2;
3467 /* Calc blocksize. Blocksize is multiples of 64KiB */
3468 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3469 extid >>= 2;
3470 /* Get buswidth information */
3471 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003472
3473 /*
3474 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3475 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3476 * follows:
3477 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3478 * 110b -> 24nm
3479 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3480 */
3481 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003482 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003483 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3484 !(id_data[4] & 0x80) /* !BENAND */) {
3485 mtd->oobsize = 32 * mtd->writesize >> 9;
3486 }
3487
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003488 }
3489}
3490
3491/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003492 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3493 * decodes a matching ID table entry and assigns the MTD size parameters for
3494 * the chip.
3495 */
3496static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3497 struct nand_flash_dev *type, u8 id_data[8],
3498 int *busw)
3499{
3500 int maf_id = id_data[0];
3501
3502 mtd->erasesize = type->erasesize;
3503 mtd->writesize = type->pagesize;
3504 mtd->oobsize = mtd->writesize / 32;
3505 *busw = type->options & NAND_BUSWIDTH_16;
3506
Huang Shijie1c195e92013-09-25 14:58:12 +08003507 /* All legacy ID NAND are small-page, SLC */
3508 chip->bits_per_cell = 1;
3509
Brian Norrisf23a4812012-09-24 20:40:51 -07003510 /*
3511 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3512 * some Spansion chips have erasesize that conflicts with size
3513 * listed in nand_ids table.
3514 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3515 */
3516 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3517 && id_data[6] == 0x00 && id_data[7] == 0x00
3518 && mtd->writesize == 512) {
3519 mtd->erasesize = 128 * 1024;
3520 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3521 }
3522}
3523
3524/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003525 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3526 * heuristic patterns using various detected parameters (e.g., manufacturer,
3527 * page size, cell-type information).
3528 */
3529static void nand_decode_bbm_options(struct mtd_info *mtd,
3530 struct nand_chip *chip, u8 id_data[8])
3531{
3532 int maf_id = id_data[0];
3533
3534 /* Set the bad block position */
3535 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3536 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3537 else
3538 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3539
3540 /*
3541 * Bad block marker is stored in the last page of each block on Samsung
3542 * and Hynix MLC devices; stored in first two pages of each block on
3543 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3544 * AMD/Spansion, and Macronix. All others scan only the first page.
3545 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003546 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003547 (maf_id == NAND_MFR_SAMSUNG ||
3548 maf_id == NAND_MFR_HYNIX))
3549 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003550 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003551 (maf_id == NAND_MFR_SAMSUNG ||
3552 maf_id == NAND_MFR_HYNIX ||
3553 maf_id == NAND_MFR_TOSHIBA ||
3554 maf_id == NAND_MFR_AMD ||
3555 maf_id == NAND_MFR_MACRONIX)) ||
3556 (mtd->writesize == 2048 &&
3557 maf_id == NAND_MFR_MICRON))
3558 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3559}
3560
Huang Shijieec6e87e2013-03-15 11:01:00 +08003561static inline bool is_full_id_nand(struct nand_flash_dev *type)
3562{
3563 return type->id_len;
3564}
3565
3566static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3567 struct nand_flash_dev *type, u8 *id_data, int *busw)
3568{
3569 if (!strncmp(type->id, id_data, type->id_len)) {
3570 mtd->writesize = type->pagesize;
3571 mtd->erasesize = type->erasesize;
3572 mtd->oobsize = type->oobsize;
3573
Huang Shijie7db906b2013-09-25 14:58:11 +08003574 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003575 chip->chipsize = (uint64_t)type->chipsize << 20;
3576 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003577 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3578 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003579
3580 *busw = type->options & NAND_BUSWIDTH_16;
3581
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003582 if (!mtd->name)
3583 mtd->name = type->name;
3584
Huang Shijieec6e87e2013-03-15 11:01:00 +08003585 return true;
3586 }
3587 return false;
3588}
3589
Brian Norris7e74c2d2012-09-24 20:40:49 -07003590/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003591 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003592 */
3593static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003594 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003595 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003596 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003597{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003598 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003599 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003600 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003603 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Karl Beldanef89a882008-09-15 14:37:29 +02003605 /*
3606 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003607 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003608 */
3609 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003612 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003615 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003616 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Brian Norris8b6e50c2011-05-25 14:59:01 -07003618 /*
3619 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003620 * interface concerns can cause random data which looks like a
3621 * possibly credible NAND flash to appear. If the two results do
3622 * not match, ignore the device completely.
3623 */
3624
3625 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3626
Brian Norris4aef9b72012-09-24 20:40:48 -07003627 /* Read entire ID string */
3628 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003629 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003630
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003631 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003632 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003633 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003634 return ERR_PTR(-ENODEV);
3635 }
3636
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003637 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003638 type = nand_flash_ids;
3639
Huang Shijieec6e87e2013-03-15 11:01:00 +08003640 for (; type->name != NULL; type++) {
3641 if (is_full_id_nand(type)) {
3642 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3643 goto ident_done;
3644 } else if (*dev_id == type->dev_id) {
3645 break;
3646 }
3647 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003648
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003649 chip->onfi_version = 0;
3650 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003651 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003652 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003653 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003654
3655 /* Check if the chip is JEDEC compliant */
3656 if (nand_flash_detect_jedec(mtd, chip, &busw))
3657 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003658 }
3659
David Woodhouse5e81e882010-02-26 18:32:56 +00003660 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003661 return ERR_PTR(-ENODEV);
3662
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003663 if (!mtd->name)
3664 mtd->name = type->name;
3665
Adrian Hunter69423d92008-12-10 13:37:21 +00003666 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003667
Huang Shijie12a40a52010-09-27 10:43:53 +08003668 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003669 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003670 busw = chip->init_size(mtd, chip, id_data);
3671 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003672 /* Decode parameters from extended ID */
3673 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003674 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003675 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003676 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003677 /* Get chip options */
3678 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003679
Brian Norris8b6e50c2011-05-25 14:59:01 -07003680 /*
3681 * Check if chip is not a Samsung device. Do not clear the
3682 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003683 */
3684 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3685 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3686ident_done:
3687
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003688 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003689 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003690 if (nand_manuf_ids[maf_idx].id == *maf_id)
3691 break;
3692 }
3693
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003694 if (chip->options & NAND_BUSWIDTH_AUTO) {
3695 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3696 chip->options |= busw;
3697 nand_set_defaults(chip, busw);
3698 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3699 /*
3700 * Check, if buswidth is correct. Hardware drivers should set
3701 * chip correct!
3702 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003703 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3704 *maf_id, *dev_id);
3705 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3706 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003707 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3708 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003709 return ERR_PTR(-EINVAL);
3710 }
3711
Brian Norris7e74c2d2012-09-24 20:40:49 -07003712 nand_decode_bbm_options(mtd, chip, id_data);
3713
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003714 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003715 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003716 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003717 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003718
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003719 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003720 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003721 if (chip->chipsize & 0xffffffff)
3722 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003723 else {
3724 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3725 chip->chip_shift += 32 - 1;
3726 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003727
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003728 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003729 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003730
Brian Norris8b6e50c2011-05-25 14:59:01 -07003731 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003732 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3733 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003734
Ezequiel Garcia20171642013-11-25 08:30:31 -03003735 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3736 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003737
3738 if (chip->onfi_version)
3739 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3740 chip->onfi_params.model);
3741 else if (chip->jedec_version)
3742 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3743 chip->jedec_params.model);
3744 else
3745 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3746 type->name);
3747
Ezequiel Garcia20171642013-11-25 08:30:31 -03003748 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003749 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3750 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003751 return type;
3752}
3753
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003754/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003755 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003756 * @mtd: MTD device structure
3757 * @maxchips: number of chips to scan for
3758 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003759 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003760 * This is the first phase of the normal nand_scan() function. It reads the
3761 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003762 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003763 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003764 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003765int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3766 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003767{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003768 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003769 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003770 struct nand_flash_dev *type;
3771
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003772 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003773 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003774
3775 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003776 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3777 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003778
3779 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003780 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003781 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003782 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003783 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 }
3785
Huang Shijie07300162012-11-09 16:23:45 +08003786 chip->select_chip(mtd, -1);
3787
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003788 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003789 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003790 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003791 /* See comment in nand_get_flash_type for reset */
3792 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003794 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003796 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003797 nand_dev_id != chip->read_byte(mtd)) {
3798 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 break;
Huang Shijie07300162012-11-09 16:23:45 +08003800 }
3801 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003804 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003807 chip->numchips = i;
3808 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
David Woodhouse3b85c322006-09-25 17:06:53 +01003810 return 0;
3811}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003812EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003813
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03003814/*
3815 * Check if the chip configuration meet the datasheet requirements.
3816
3817 * If our configuration corrects A bits per B bytes and the minimum
3818 * required correction level is X bits per Y bytes, then we must ensure
3819 * both of the following are true:
3820 *
3821 * (1) A / B >= X / Y
3822 * (2) A >= X
3823 *
3824 * Requirement (1) ensures we can correct for the required bitflip density.
3825 * Requirement (2) ensures we can correct even when all bitflips are clumped
3826 * in the same sector.
3827 */
3828static bool nand_ecc_strength_good(struct mtd_info *mtd)
3829{
3830 struct nand_chip *chip = mtd->priv;
3831 struct nand_ecc_ctrl *ecc = &chip->ecc;
3832 int corr, ds_corr;
3833
3834 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3835 /* Not enough information */
3836 return true;
3837
3838 /*
3839 * We get the number of corrected bits per page to compare
3840 * the correction density.
3841 */
3842 corr = (mtd->writesize * ecc->strength) / ecc->size;
3843 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3844
3845 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3846}
David Woodhouse3b85c322006-09-25 17:06:53 +01003847
3848/**
3849 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003850 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003851 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003852 * This is the second phase of the normal nand_scan() function. It fills out
3853 * all the uninitialized function pointers with the defaults and scans for a
3854 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003855 */
3856int nand_scan_tail(struct mtd_info *mtd)
3857{
3858 int i;
3859 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003860 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003861 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003862
Brian Norrise2414f42012-02-06 13:44:00 -08003863 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3864 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3865 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3866
Huang Shijief02ea4e2014-01-13 14:27:12 +08003867 if (!(chip->options & NAND_OWN_BUFFERS)) {
3868 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3869 + mtd->oobsize * 3, GFP_KERNEL);
3870 if (!nbuf)
3871 return -ENOMEM;
3872 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3873 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3874 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3875
3876 chip->buffers = nbuf;
3877 } else {
3878 if (!chip->buffers)
3879 return -ENOMEM;
3880 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003881
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003882 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003883 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003884
3885 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003886 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003887 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003888 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003889 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003891 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 break;
3893 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003894 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 break;
3896 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003897 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003899 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003900 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003903 pr_warn("No oob scheme defined for oobsize %d\n",
3904 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 BUG();
3906 }
3907 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003908
David Woodhouse956e9442006-09-25 17:12:39 +01003909 if (!chip->write_page)
3910 chip->write_page = nand_write_page;
3911
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003912 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003913 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003914 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003915 */
David Woodhouse956e9442006-09-25 17:12:39 +01003916
Huang Shijie97de79e02013-10-18 14:20:53 +08003917 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003918 case NAND_ECC_HW_OOB_FIRST:
3919 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003920 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003921 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003922 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003923 BUG();
3924 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003925 if (!ecc->read_page)
3926 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003927
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003928 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003929 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003930 if (!ecc->read_page)
3931 ecc->read_page = nand_read_page_hwecc;
3932 if (!ecc->write_page)
3933 ecc->write_page = nand_write_page_hwecc;
3934 if (!ecc->read_page_raw)
3935 ecc->read_page_raw = nand_read_page_raw;
3936 if (!ecc->write_page_raw)
3937 ecc->write_page_raw = nand_write_page_raw;
3938 if (!ecc->read_oob)
3939 ecc->read_oob = nand_read_oob_std;
3940 if (!ecc->write_oob)
3941 ecc->write_oob = nand_write_oob_std;
3942 if (!ecc->read_subpage)
3943 ecc->read_subpage = nand_read_subpage;
3944 if (!ecc->write_subpage)
3945 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003946
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003947 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003948 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3949 (!ecc->read_page ||
3950 ecc->read_page == nand_read_page_hwecc ||
3951 !ecc->write_page ||
3952 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003953 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003954 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003955 BUG();
3956 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003957 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003958 if (!ecc->read_page)
3959 ecc->read_page = nand_read_page_syndrome;
3960 if (!ecc->write_page)
3961 ecc->write_page = nand_write_page_syndrome;
3962 if (!ecc->read_page_raw)
3963 ecc->read_page_raw = nand_read_page_raw_syndrome;
3964 if (!ecc->write_page_raw)
3965 ecc->write_page_raw = nand_write_page_raw_syndrome;
3966 if (!ecc->read_oob)
3967 ecc->read_oob = nand_read_oob_syndrome;
3968 if (!ecc->write_oob)
3969 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003970
Huang Shijie97de79e02013-10-18 14:20:53 +08003971 if (mtd->writesize >= ecc->size) {
3972 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003973 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3974 BUG();
3975 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003976 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003977 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003978 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003979 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003980 ecc->size, mtd->writesize);
3981 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003983 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003984 ecc->calculate = nand_calculate_ecc;
3985 ecc->correct = nand_correct_data;
3986 ecc->read_page = nand_read_page_swecc;
3987 ecc->read_subpage = nand_read_subpage;
3988 ecc->write_page = nand_write_page_swecc;
3989 ecc->read_page_raw = nand_read_page_raw;
3990 ecc->write_page_raw = nand_write_page_raw;
3991 ecc->read_oob = nand_read_oob_std;
3992 ecc->write_oob = nand_write_oob_std;
3993 if (!ecc->size)
3994 ecc->size = 256;
3995 ecc->bytes = 3;
3996 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003998
Ivan Djelic193bd402011-03-11 11:05:33 +01003999 case NAND_ECC_SOFT_BCH:
4000 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004001 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004002 BUG();
4003 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004004 ecc->calculate = nand_bch_calculate_ecc;
4005 ecc->correct = nand_bch_correct_data;
4006 ecc->read_page = nand_read_page_swecc;
4007 ecc->read_subpage = nand_read_subpage;
4008 ecc->write_page = nand_write_page_swecc;
4009 ecc->read_page_raw = nand_read_page_raw;
4010 ecc->write_page_raw = nand_write_page_raw;
4011 ecc->read_oob = nand_read_oob_std;
4012 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004013 /*
4014 * Board driver should supply ecc.size and ecc.bytes values to
4015 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07004016 * for details. Otherwise, default to 4 bits for large page
4017 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004018 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004019 if (!ecc->size && (mtd->oobsize >= 64)) {
4020 ecc->size = 512;
4021 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01004022 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004023 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4024 &ecc->layout);
4025 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004026 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004027 BUG();
4028 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004029 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01004030 break;
4031
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004032 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07004033 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07004034 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004035 ecc->read_page = nand_read_page_raw;
4036 ecc->write_page = nand_write_page_raw;
4037 ecc->read_oob = nand_read_oob_std;
4038 ecc->read_page_raw = nand_read_page_raw;
4039 ecc->write_page_raw = nand_write_page_raw;
4040 ecc->write_oob = nand_write_oob_std;
4041 ecc->size = mtd->writesize;
4042 ecc->bytes = 0;
4043 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004047 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004048 BUG();
4049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Brian Norris9ce244b2011-08-30 18:45:37 -07004051 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004052 if (!ecc->read_oob_raw)
4053 ecc->read_oob_raw = ecc->read_oob;
4054 if (!ecc->write_oob_raw)
4055 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004056
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004057 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004058 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004059 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004060 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004061 ecc->layout->oobavail = 0;
4062 for (i = 0; ecc->layout->oobfree[i].length
4063 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4064 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4065 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004066
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004067 /* ECC sanity check: warn noisily if it's too weak */
4068 WARN_ON(!nand_ecc_strength_good(mtd));
4069
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004070 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004071 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004072 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004073 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004074 ecc->steps = mtd->writesize / ecc->size;
4075 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004076 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004077 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004079 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004080
Brian Norris8b6e50c2011-05-25 14:59:01 -07004081 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004082 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004083 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004084 case 2:
4085 mtd->subpage_sft = 1;
4086 break;
4087 case 4:
4088 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004089 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004090 mtd->subpage_sft = 2;
4091 break;
4092 }
4093 }
4094 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4095
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004096 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004097 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004100 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004102 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304103 switch (ecc->mode) {
4104 case NAND_ECC_SOFT:
4105 case NAND_ECC_SOFT_BCH:
4106 if (chip->page_shift > 9)
4107 chip->options |= NAND_SUBPAGE_READ;
4108 break;
4109
4110 default:
4111 break;
4112 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004115 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004116 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4117 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004118 mtd->_erase = nand_erase;
4119 mtd->_point = NULL;
4120 mtd->_unpoint = NULL;
4121 mtd->_read = nand_read;
4122 mtd->_write = nand_write;
4123 mtd->_panic_write = panic_nand_write;
4124 mtd->_read_oob = nand_read_oob;
4125 mtd->_write_oob = nand_write_oob;
4126 mtd->_sync = nand_sync;
4127 mtd->_lock = NULL;
4128 mtd->_unlock = NULL;
4129 mtd->_suspend = nand_suspend;
4130 mtd->_resume = nand_resume;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004131 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004132 mtd->_block_isbad = nand_block_isbad;
4133 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004134 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Mike Dunn6a918ba2012-03-11 14:21:11 -07004136 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004137 mtd->ecclayout = ecc->layout;
4138 mtd->ecc_strength = ecc->strength;
4139 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004140 /*
4141 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4142 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4143 * properly set.
4144 */
4145 if (!mtd->bitflip_threshold)
4146 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004148 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004149 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004153 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004155EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Brian Norris8b6e50c2011-05-25 14:59:01 -07004157/*
4158 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004159 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004160 * to call us from in-kernel code if the core NAND support is modular.
4161 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004162#ifdef MODULE
4163#define caller_is_module() (1)
4164#else
4165#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004166 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004167#endif
4168
4169/**
4170 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004171 * @mtd: MTD device structure
4172 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004173 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004174 * This fills out all the uninitialized function pointers with the defaults.
4175 * The flash ID is read and the mtd/chip structures are filled with the
4176 * appropriate values. The mtd->owner field must be set to the module of the
4177 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004178 */
4179int nand_scan(struct mtd_info *mtd, int maxchips)
4180{
4181 int ret;
4182
4183 /* Many callers got this wrong, so check for it for a while... */
4184 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004185 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004186 BUG();
4187 }
4188
David Woodhouse5e81e882010-02-26 18:32:56 +00004189 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004190 if (!ret)
4191 ret = nand_scan_tail(mtd);
4192 return ret;
4193}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004194EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004197 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004198 * @mtd: MTD device structure
4199 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004200void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004202 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Ivan Djelic193bd402011-03-11 11:05:33 +01004204 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4205 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4206
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004207 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Jesper Juhlfa671642005-11-07 01:01:27 -08004209 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004210 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004211 if (!(chip->options & NAND_OWN_BUFFERS))
4212 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004213
4214 /* Free bad block descriptor memory */
4215 if (chip->badblock_pattern && chip->badblock_pattern->options
4216 & NAND_BBT_DYNAMICSTRUCT)
4217 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
David Woodhousee0c7d762006-05-13 18:07:53 +01004219EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004220
4221static int __init nand_base_init(void)
4222{
4223 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4224 return 0;
4225}
4226
4227static void __exit nand_base_exit(void)
4228{
4229 led_trigger_unregister_simple(nand_led_trigger);
4230}
4231
4232module_init(nand_base_init);
4233module_exit(nand_base_exit);
4234
David Woodhousee0c7d762006-05-13 18:07:53 +01004235MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004236MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4237MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004238MODULE_DESCRIPTION("Generic NAND flash driver code");