blob: 3f24b587304f035a3c43253e7cfe4144c40ee8ef [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 Norris8b6e50c2011-05-25 14:59:01 -0700160 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700162 * Default read function for 16bit buswidth with endianness conversion.
163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200165static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200167 struct nand_chip *chip = mtd->priv;
168 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700173 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700175 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177static u16 nand_read_word(struct mtd_info *mtd)
178{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200179 struct nand_chip *chip = mtd->priv;
180 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700185 * @mtd: MTD device structure
186 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
188 * Default select function for 1 chip devices.
189 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200192 struct nand_chip *chip = mtd->priv;
193
194 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200196 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200
201 default:
202 BUG();
203 }
204}
205
206/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100207 * nand_write_byte - [DEFAULT] write single byte to chip
208 * @mtd: MTD device structure
209 * @byte: value to write
210 *
211 * Default function to write a byte to I/O[7:0]
212 */
213static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
214{
215 struct nand_chip *chip = mtd->priv;
216
217 chip->write_buf(mtd, &byte, 1);
218}
219
220/**
221 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
222 * @mtd: MTD device structure
223 * @byte: value to write
224 *
225 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
226 */
227static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
228{
229 struct nand_chip *chip = mtd->priv;
230 uint16_t word = byte;
231
232 /*
233 * It's not entirely clear what should happen to I/O[15:8] when writing
234 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
235 *
236 * When the host supports a 16-bit bus width, only data is
237 * transferred at the 16-bit width. All address and command line
238 * transfers shall use only the lower 8-bits of the data bus. During
239 * command transfers, the host may place any value on the upper
240 * 8-bits of the data bus. During address transfers, the host shall
241 * set the upper 8-bits of the data bus to 00h.
242 *
243 * One user of the write_byte callback is nand_onfi_set_features. The
244 * four parameters are specified to be written to I/O[7:0], but this is
245 * neither an address nor a command transfer. Let's assume a 0 on the
246 * upper I/O lines is OK.
247 */
248 chip->write_buf(mtd, (uint8_t *)&word, 2);
249}
250
251/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700253 * @mtd: MTD device structure
254 * @buf: data buffer
255 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700257 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200259static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200261 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alexander Shiyan76413832013-04-13 09:32:13 +0400263 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000267 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700268 * @mtd: MTD device structure
269 * @buf: buffer to store date
270 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700272 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200276 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Alexander Shiyan76413832013-04-13 09:32:13 +0400278 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700283 * @mtd: MTD device structure
284 * @buf: data buffer
285 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700287 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200289static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200291 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293
Alexander Shiyan76413832013-04-13 09:32:13 +0400294 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000298 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700299 * @mtd: MTD device structure
300 * @buf: buffer to store date
301 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700303 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200305static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200307 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Alexander Shiyan76413832013-04-13 09:32:13 +0400310 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700315 * @mtd: MTD device structure
316 * @ofs: offset from device start
317 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000319 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
322{
Brian Norriscdbec052012-01-13 18:11:48 -0800323 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200324 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 u16 bad;
326
Brian Norris5fb15492011-05-31 16:31:21 -0700327 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700328 ofs += mtd->erasesize - mtd->writesize;
329
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100330 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200333 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Huang Shijie6a8214a2012-11-19 14:43:30 +0800335 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200338 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Brian Norriscdbec052012-01-13 18:11:48 -0800341 do {
342 if (chip->options & NAND_BUSWIDTH_16) {
343 chip->cmdfunc(mtd, NAND_CMD_READOOB,
344 chip->badblockpos & 0xFE, page);
345 bad = cpu_to_le16(chip->read_word(mtd));
346 if (chip->badblockpos & 0x1)
347 bad >>= 8;
348 else
349 bad &= 0xFF;
350 } else {
351 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
352 page);
353 bad = chip->read_byte(mtd);
354 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000355
Brian Norriscdbec052012-01-13 18:11:48 -0800356 if (likely(chip->badblockbits == 8))
357 res = bad != 0xFF;
358 else
359 res = hweight8(bad) < chip->badblockbits;
360 ofs += mtd->writesize;
361 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
362 i++;
363 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200364
Huang Shijieb0bb6902012-11-19 14:43:29 +0800365 if (getchip) {
366 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800368 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return res;
371}
372
373/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700374 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700375 * @mtd: MTD device structure
376 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700378 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700379 * specific driver. It provides the details for writing a bad block marker to a
380 * block.
381 */
382static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
383{
384 struct nand_chip *chip = mtd->priv;
385 struct mtd_oob_ops ops;
386 uint8_t buf[2] = { 0, 0 };
387 int ret = 0, res, i = 0;
388
389 ops.datbuf = NULL;
390 ops.oobbuf = buf;
391 ops.ooboffs = chip->badblockpos;
392 if (chip->options & NAND_BUSWIDTH_16) {
393 ops.ooboffs &= ~0x01;
394 ops.len = ops.ooblen = 2;
395 } else {
396 ops.len = ops.ooblen = 1;
397 }
398 ops.mode = MTD_OPS_PLACE_OOB;
399
400 /* Write to first/last page(s) if necessary */
401 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
402 ofs += mtd->erasesize - mtd->writesize;
403 do {
404 res = nand_do_write_oob(mtd, ofs, &ops);
405 if (!ret)
406 ret = res;
407
408 i++;
409 ofs += mtd->writesize;
410 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
411
412 return ret;
413}
414
415/**
416 * nand_block_markbad_lowlevel - mark a block bad
417 * @mtd: MTD device structure
418 * @ofs: offset from device start
419 *
420 * This function performs the generic NAND bad block marking steps (i.e., bad
421 * block table(s) and/or marker(s)). We only allow the hardware driver to
422 * specify how to write bad block markers to OOB (chip->block_markbad).
423 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700424 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800425 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700426 * (2) write bad block marker to OOB area of affected block (unless flag
427 * NAND_BBT_NO_OOB_BBM is present)
428 * (3) update the BBT
429 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800430 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700432static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200434 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700435 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000436
Brian Norrisb32843b2013-07-30 17:52:59 -0700437 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800438 struct erase_info einfo;
439
440 /* Attempt erase before marking OOB */
441 memset(&einfo, 0, sizeof(einfo));
442 einfo.mtd = mtd;
443 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300444 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800445 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800446
Brian Norrisb32843b2013-07-30 17:52:59 -0700447 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800448 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700449 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300450 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200451 }
Brian Norrise2414f42012-02-06 13:44:00 -0800452
Brian Norrisb32843b2013-07-30 17:52:59 -0700453 /* Mark block bad in BBT */
454 if (chip->bbt) {
455 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800456 if (!ret)
457 ret = res;
458 }
459
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200460 if (!ret)
461 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300462
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000466/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700468 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700470 * Check, if the device is write protected. The function expects, that the
471 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100473static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200475 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200476
Brian Norris8b6e50c2011-05-25 14:59:01 -0700477 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200478 if (chip->options & NAND_BROKEN_XD)
479 return 0;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200482 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
483 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800487 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700488 * @mtd: MTD device structure
489 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300490 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800491 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300492 */
493static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
494{
495 struct nand_chip *chip = mtd->priv;
496
497 if (!chip->bbt)
498 return 0;
499 /* Return info from the table */
500 return nand_isreserved_bbt(mtd, ofs);
501}
502
503/**
504 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
505 * @mtd: MTD device structure
506 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700507 * @getchip: 0, if the chip is already selected
508 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
510 * Check, if the block is bad. Either by reading the bad block table or
511 * calling of the scan function.
512 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200513static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
514 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200516 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000517
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200518 if (!chip->bbt)
519 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100522 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200525/**
526 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700527 * @mtd: MTD device structure
528 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200529 *
530 * Helper function for nand_wait_ready used when needing to wait in interrupt
531 * context.
532 */
533static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
534{
535 struct nand_chip *chip = mtd->priv;
536 int i;
537
538 /* Wait for the device to get ready */
539 for (i = 0; i < timeo; i++) {
540 if (chip->dev_ready(mtd))
541 break;
542 touch_softlockup_watchdog();
543 mdelay(1);
544 }
545}
546
Brian Norris7854d3f2011-06-23 14:12:08 -0700547/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100548void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000549{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200550 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100551 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000552
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200553 /* 400ms timeout */
554 if (in_interrupt() || oops_in_progress)
555 return panic_nand_wait_ready(mtd, 400);
556
Richard Purdie8fe833c2006-03-31 02:31:14 -0800557 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700558 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000559 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200560 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800561 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700562 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000563 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800564 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000565}
David Woodhouse4b648b02006-09-25 17:05:24 +0100566EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/**
569 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700570 * @mtd: MTD device structure
571 * @command: the command to be sent
572 * @column: the column address for this command, -1 if none
573 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700575 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200576 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200578static void nand_command(struct mtd_info *mtd, unsigned int command,
579 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200581 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200582 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Brian Norris8b6e50c2011-05-25 14:59:01 -0700584 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (command == NAND_CMD_SEQIN) {
586 int readcmd;
587
Joern Engel28318772006-05-22 23:18:05 +0200588 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200590 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 readcmd = NAND_CMD_READOOB;
592 } else if (column < 256) {
593 /* First 256 bytes --> READ0 */
594 readcmd = NAND_CMD_READ0;
595 } else {
596 column -= 256;
597 readcmd = NAND_CMD_READ1;
598 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200599 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200600 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Brian Norris8b6e50c2011-05-25 14:59:01 -0700604 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200605 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
606 /* Serially input address */
607 if (column != -1) {
608 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800609 if (chip->options & NAND_BUSWIDTH_16 &&
610 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200611 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200612 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200613 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200615 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200616 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200617 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200618 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200620 if (chip->chipsize > (32 << 20))
621 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200622 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200623 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000624
625 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 * Program and erase have their own busy handlers status and sequential
627 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 case NAND_CMD_PAGEPROG:
632 case NAND_CMD_ERASE1:
633 case NAND_CMD_ERASE2:
634 case NAND_CMD_SEQIN:
635 case NAND_CMD_STATUS:
636 return;
637
638 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 udelay(chip->chip_delay);
642 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200643 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200644 chip->cmd_ctrl(mtd,
645 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200646 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
647 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return;
649
David Woodhousee0c7d762006-05-13 18:07:53 +0100650 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000652 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * If we don't have access to the busy pin, we apply the given
654 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200656 if (!chip->dev_ready) {
657 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700661 /*
662 * Apply this short delay always to ensure that we do wait tWB in
663 * any case on any machine.
664 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100665 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000666
667 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670/**
671 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700672 * @mtd: MTD device structure
673 * @command: the command to be sent
674 * @column: the column address for this command, -1 if none
675 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200677 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700678 * devices. We don't have the separate regions as we have in the small page
679 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200681static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
682 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200684 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Emulate NAND_CMD_READOOB */
687 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200688 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 command = NAND_CMD_READ0;
690 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000691
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200692 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400693 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200696 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* Serially input address */
699 if (column != -1) {
700 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800701 if (chip->options & NAND_BUSWIDTH_16 &&
702 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200704 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200706 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200709 chip->cmd_ctrl(mtd, page_addr, ctrl);
710 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 if (chip->chipsize > (128 << 20))
714 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200715 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200718 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000719
720 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700721 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100722 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 case NAND_CMD_CACHEDPROG:
727 case NAND_CMD_PAGEPROG:
728 case NAND_CMD_ERASE1:
729 case NAND_CMD_ERASE2:
730 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200731 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000733 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200739 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
740 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
741 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
742 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200743 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
744 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return;
746
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200747 case NAND_CMD_RNDOUT:
748 /* No ready / busy check necessary */
749 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
750 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
751 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
752 NAND_NCE | NAND_CTRL_CHANGE);
753 return;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200756 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
757 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
758 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
759 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000760
David Woodhousee0c7d762006-05-13 18:07:53 +0100761 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000763 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700765 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100766 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 if (!chip->dev_ready) {
768 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000772
Brian Norris8b6e50c2011-05-25 14:59:01 -0700773 /*
774 * Apply this short delay always to ensure that we do wait tWB in
775 * any case on any machine.
776 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100777 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000778
779 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200783 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 * @chip: the nand chip descriptor
785 * @mtd: MTD device structure
786 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200787 *
788 * Used when in panic, no locks are taken.
789 */
790static void panic_nand_get_device(struct nand_chip *chip,
791 struct mtd_info *mtd, int new_state)
792{
Brian Norris7854d3f2011-06-23 14:12:08 -0700793 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200794 chip->controller->active = chip;
795 chip->state = new_state;
796}
797
798/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700800 * @mtd: MTD device structure
801 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
803 * Get the device and lock it for exclusive access
804 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200805static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800806nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800808 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 spinlock_t *lock = &chip->controller->lock;
810 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200812retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100813 spin_lock(lock);
814
vimal singhb8b3ee92009-07-09 20:41:22 +0530815 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200816 if (!chip->controller->active)
817 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200818
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200819 if (chip->controller->active == chip && chip->state == FL_READY) {
820 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100821 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100822 return 0;
823 }
824 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800825 if (chip->controller->active->state == FL_PM_SUSPENDED) {
826 chip->state = FL_PM_SUSPENDED;
827 spin_unlock(lock);
828 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800829 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100830 }
831 set_current_state(TASK_UNINTERRUPTIBLE);
832 add_wait_queue(wq, &wait);
833 spin_unlock(lock);
834 schedule();
835 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto retry;
837}
838
839/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700840 * panic_nand_wait - [GENERIC] wait until the command is done
841 * @mtd: MTD device structure
842 * @chip: NAND chip structure
843 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200844 *
845 * Wait for command done. This is a helper function for nand_wait used when
846 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400847 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200848 */
849static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
850 unsigned long timeo)
851{
852 int i;
853 for (i = 0; i < timeo; i++) {
854 if (chip->dev_ready) {
855 if (chip->dev_ready(mtd))
856 break;
857 } else {
858 if (chip->read_byte(mtd) & NAND_STATUS_READY)
859 break;
860 }
861 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200862 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200863}
864
865/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700866 * nand_wait - [DEFAULT] wait until the command is done
867 * @mtd: MTD device structure
868 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700870 * Wait for command done. This applies to erase and program only. Erase can
871 * take up to 400ms and program up to 20ms according to general NAND and
872 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700873 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200874static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200877 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800878 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Richard Purdie8fe833c2006-03-31 02:31:14 -0800880 led_trigger_event(nand_led_trigger, LED_FULL);
881
Brian Norris8b6e50c2011-05-25 14:59:01 -0700882 /*
883 * Apply this short delay always to ensure that we do wait tWB in any
884 * case on any machine.
885 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100886 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200888 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200890 if (in_interrupt() || oops_in_progress)
891 panic_nand_wait(mtd, chip, timeo);
892 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800893 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200894 while (time_before(jiffies, timeo)) {
895 if (chip->dev_ready) {
896 if (chip->dev_ready(mtd))
897 break;
898 } else {
899 if (chip->read_byte(mtd) & NAND_STATUS_READY)
900 break;
901 }
902 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800905 led_trigger_event(nand_led_trigger, LED_OFF);
906
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200907 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100908 /* This can happen if in case of timeout or buggy dev_ready */
909 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return status;
911}
912
913/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700914 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700915 * @mtd: mtd info
916 * @ofs: offset to start unlock from
917 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700918 * @invert: when = 0, unlock the range of blocks within the lower and
919 * upper boundary address
920 * when = 1, unlock the range of blocks outside the boundaries
921 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530922 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700923 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530924 */
925static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
926 uint64_t len, int invert)
927{
928 int ret = 0;
929 int status, page;
930 struct nand_chip *chip = mtd->priv;
931
932 /* Submit address of first page to unlock */
933 page = ofs >> chip->page_shift;
934 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
935
936 /* Submit address of last page to unlock */
937 page = (ofs + len) >> chip->page_shift;
938 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
939 (page | invert) & chip->pagemask);
940
941 /* Call wait ready function */
942 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530943 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400944 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700945 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530946 __func__, status);
947 ret = -EIO;
948 }
949
950 return ret;
951}
952
953/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700954 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700955 * @mtd: mtd info
956 * @ofs: offset to start unlock from
957 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530958 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700959 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530960 */
961int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
962{
963 int ret = 0;
964 int chipnr;
965 struct nand_chip *chip = mtd->priv;
966
Brian Norris289c0522011-07-19 10:06:09 -0700967 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530968 __func__, (unsigned long long)ofs, len);
969
970 if (check_offs_len(mtd, ofs, len))
971 ret = -EINVAL;
972
973 /* Align to last block address if size addresses end of the device */
974 if (ofs + len == mtd->size)
975 len -= mtd->erasesize;
976
Huang Shijie6a8214a2012-11-19 14:43:30 +0800977 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530978
979 /* Shift to get chip number */
980 chipnr = ofs >> chip->chip_shift;
981
982 chip->select_chip(mtd, chipnr);
983
White Ding57d3a9a2014-07-24 00:10:45 +0800984 /*
985 * Reset the chip.
986 * If we want to check the WP through READ STATUS and check the bit 7
987 * we must reset the chip
988 * some operation can also clear the bit 7 of status register
989 * eg. erase/program a locked block
990 */
991 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
992
Vimal Singh7d70f332010-02-08 15:50:49 +0530993 /* Check, if it is write protected */
994 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700995 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530996 __func__);
997 ret = -EIO;
998 goto out;
999 }
1000
1001 ret = __nand_unlock(mtd, ofs, len, 0);
1002
1003out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001004 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301005 nand_release_device(mtd);
1006
1007 return ret;
1008}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001009EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301010
1011/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001012 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001013 * @mtd: mtd info
1014 * @ofs: offset to start unlock from
1015 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301016 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001017 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1018 * have this feature, but it allows only to lock all blocks, not for specified
1019 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1020 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301021 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001022 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301023 */
1024int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1025{
1026 int ret = 0;
1027 int chipnr, status, page;
1028 struct nand_chip *chip = mtd->priv;
1029
Brian Norris289c0522011-07-19 10:06:09 -07001030 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301031 __func__, (unsigned long long)ofs, len);
1032
1033 if (check_offs_len(mtd, ofs, len))
1034 ret = -EINVAL;
1035
Huang Shijie6a8214a2012-11-19 14:43:30 +08001036 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301037
1038 /* Shift to get chip number */
1039 chipnr = ofs >> chip->chip_shift;
1040
1041 chip->select_chip(mtd, chipnr);
1042
White Ding57d3a9a2014-07-24 00:10:45 +08001043 /*
1044 * Reset the chip.
1045 * If we want to check the WP through READ STATUS and check the bit 7
1046 * we must reset the chip
1047 * some operation can also clear the bit 7 of status register
1048 * eg. erase/program a locked block
1049 */
1050 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1051
Vimal Singh7d70f332010-02-08 15:50:49 +05301052 /* Check, if it is write protected */
1053 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001054 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301055 __func__);
1056 status = MTD_ERASE_FAILED;
1057 ret = -EIO;
1058 goto out;
1059 }
1060
1061 /* Submit address of first page to lock */
1062 page = ofs >> chip->page_shift;
1063 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1064
1065 /* Call wait ready function */
1066 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301067 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001068 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001069 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301070 __func__, status);
1071 ret = -EIO;
1072 goto out;
1073 }
1074
1075 ret = __nand_unlock(mtd, ofs, len, 0x1);
1076
1077out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001078 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301079 nand_release_device(mtd);
1080
1081 return ret;
1082}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001083EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301084
1085/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001086 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001087 * @mtd: mtd info structure
1088 * @chip: nand chip info structure
1089 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001090 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001091 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001092 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001093 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001094 */
1095static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001096 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001097{
1098 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001099 if (oob_required)
1100 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001101 return 0;
1102}
1103
1104/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001105 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001106 * @mtd: mtd info structure
1107 * @chip: nand chip info structure
1108 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001109 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001110 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001111 *
1112 * We need a special oob layout and handling even when OOB isn't used.
1113 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001114static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001115 struct nand_chip *chip, uint8_t *buf,
1116 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001117{
1118 int eccsize = chip->ecc.size;
1119 int eccbytes = chip->ecc.bytes;
1120 uint8_t *oob = chip->oob_poi;
1121 int steps, size;
1122
1123 for (steps = chip->ecc.steps; steps > 0; steps--) {
1124 chip->read_buf(mtd, buf, eccsize);
1125 buf += eccsize;
1126
1127 if (chip->ecc.prepad) {
1128 chip->read_buf(mtd, oob, chip->ecc.prepad);
1129 oob += chip->ecc.prepad;
1130 }
1131
1132 chip->read_buf(mtd, oob, eccbytes);
1133 oob += eccbytes;
1134
1135 if (chip->ecc.postpad) {
1136 chip->read_buf(mtd, oob, chip->ecc.postpad);
1137 oob += chip->ecc.postpad;
1138 }
1139 }
1140
1141 size = mtd->oobsize - (oob - chip->oob_poi);
1142 if (size)
1143 chip->read_buf(mtd, oob, size);
1144
1145 return 0;
1146}
1147
1148/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001149 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001150 * @mtd: mtd info structure
1151 * @chip: nand chip info structure
1152 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001153 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001154 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001155 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001156static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001157 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001159 int i, eccsize = chip->ecc.size;
1160 int eccbytes = chip->ecc.bytes;
1161 int eccsteps = chip->ecc.steps;
1162 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001163 uint8_t *ecc_calc = chip->buffers->ecccalc;
1164 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001165 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001166 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001167
Brian Norris1fbb9382012-05-02 10:14:55 -07001168 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001169
1170 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1171 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1172
1173 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001174 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001175
1176 eccsteps = chip->ecc.steps;
1177 p = buf;
1178
1179 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1180 int stat;
1181
1182 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001183 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001184 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001185 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001186 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001187 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1188 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001189 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001190 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301194 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001195 * @mtd: mtd info structure
1196 * @chip: nand chip info structure
1197 * @data_offs: offset of requested data within the page
1198 * @readlen: data length
1199 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001200 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001201 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001202static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001203 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1204 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001205{
1206 int start_step, end_step, num_steps;
1207 uint32_t *eccpos = chip->ecc.layout->eccpos;
1208 uint8_t *p;
1209 int data_col_addr, i, gaps = 0;
1210 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1211 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301212 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001213 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001214
Brian Norris7854d3f2011-06-23 14:12:08 -07001215 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001216 start_step = data_offs / chip->ecc.size;
1217 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1218 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301219 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001220
Brian Norris8b6e50c2011-05-25 14:59:01 -07001221 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001222 datafrag_len = num_steps * chip->ecc.size;
1223 eccfrag_len = num_steps * chip->ecc.bytes;
1224
1225 data_col_addr = start_step * chip->ecc.size;
1226 /* If we read not a page aligned data */
1227 if (data_col_addr != 0)
1228 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1229
1230 p = bufpoi + data_col_addr;
1231 chip->read_buf(mtd, p, datafrag_len);
1232
Brian Norris8b6e50c2011-05-25 14:59:01 -07001233 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001234 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1235 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1236
Brian Norris8b6e50c2011-05-25 14:59:01 -07001237 /*
1238 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001239 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001240 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001241 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301242 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001243 gaps = 1;
1244 break;
1245 }
1246 }
1247 if (gaps) {
1248 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1249 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1250 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001252 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001253 * about buswidth alignment in read_buf.
1254 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001255 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001256 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001257 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001258 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001259 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001260 aligned_len++;
1261
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001262 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1263 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001264 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1265 }
1266
1267 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001268 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001269
1270 p = bufpoi + data_col_addr;
1271 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1272 int stat;
1273
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001274 stat = chip->ecc.correct(mtd, p,
1275 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001276 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001277 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001278 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001279 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001280 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1281 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001282 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001283 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001284}
1285
1286/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001287 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001288 * @mtd: mtd info structure
1289 * @chip: nand chip info structure
1290 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001291 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001292 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001293 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001294 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001295 */
1296static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001297 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001298{
1299 int i, eccsize = chip->ecc.size;
1300 int eccbytes = chip->ecc.bytes;
1301 int eccsteps = chip->ecc.steps;
1302 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001303 uint8_t *ecc_calc = chip->buffers->ecccalc;
1304 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001305 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001306 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001307
1308 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1309 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1310 chip->read_buf(mtd, p, eccsize);
1311 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1312 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001313 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001314
1315 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001316 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001317
1318 eccsteps = chip->ecc.steps;
1319 p = buf;
1320
1321 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1322 int stat;
1323
1324 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001325 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001326 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001327 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001328 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001329 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1330 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001331 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001332 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001333}
1334
1335/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001336 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001337 * @mtd: mtd info structure
1338 * @chip: nand chip info structure
1339 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001340 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001341 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001342 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001343 * Hardware ECC for large page chips, require OOB to be read first. For this
1344 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1345 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1346 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1347 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001348 */
1349static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001350 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001351{
1352 int i, eccsize = chip->ecc.size;
1353 int eccbytes = chip->ecc.bytes;
1354 int eccsteps = chip->ecc.steps;
1355 uint8_t *p = buf;
1356 uint8_t *ecc_code = chip->buffers->ecccode;
1357 uint32_t *eccpos = chip->ecc.layout->eccpos;
1358 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001359 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001360
1361 /* Read the OOB area first */
1362 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1363 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1364 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1365
1366 for (i = 0; i < chip->ecc.total; i++)
1367 ecc_code[i] = chip->oob_poi[eccpos[i]];
1368
1369 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1370 int stat;
1371
1372 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1373 chip->read_buf(mtd, p, eccsize);
1374 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1375
1376 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001377 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001378 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001379 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001380 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001381 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1382 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001383 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001384 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001385}
1386
1387/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001388 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001389 * @mtd: mtd info structure
1390 * @chip: nand chip info structure
1391 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001392 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001393 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001394 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001395 * The hw generator calculates the error syndrome automatically. Therefore we
1396 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001397 */
1398static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001399 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001400{
1401 int i, eccsize = chip->ecc.size;
1402 int eccbytes = chip->ecc.bytes;
1403 int eccsteps = chip->ecc.steps;
1404 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001405 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001406 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001407
1408 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1409 int stat;
1410
1411 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1412 chip->read_buf(mtd, p, eccsize);
1413
1414 if (chip->ecc.prepad) {
1415 chip->read_buf(mtd, oob, chip->ecc.prepad);
1416 oob += chip->ecc.prepad;
1417 }
1418
1419 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1420 chip->read_buf(mtd, oob, eccbytes);
1421 stat = chip->ecc.correct(mtd, p, oob, NULL);
1422
Mike Dunn3f91e942012-04-25 12:06:09 -07001423 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001424 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001425 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001426 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001427 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1428 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001429
1430 oob += eccbytes;
1431
1432 if (chip->ecc.postpad) {
1433 chip->read_buf(mtd, oob, chip->ecc.postpad);
1434 oob += chip->ecc.postpad;
1435 }
1436 }
1437
1438 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001439 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001440 if (i)
1441 chip->read_buf(mtd, oob, i);
1442
Mike Dunn3f91e942012-04-25 12:06:09 -07001443 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444}
1445
1446/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001447 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001448 * @chip: nand chip structure
1449 * @oob: oob destination address
1450 * @ops: oob ops structure
1451 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001452 */
1453static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001454 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001455{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001456 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001457
Brian Norris0612b9d2011-08-30 18:45:40 -07001458 case MTD_OPS_PLACE_OOB:
1459 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001460 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1461 return oob + len;
1462
Brian Norris0612b9d2011-08-30 18:45:40 -07001463 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001464 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001465 uint32_t boffs = 0, roffs = ops->ooboffs;
1466 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001467
Florian Fainellif8ac0412010-09-07 13:23:43 +02001468 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001469 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001470 if (unlikely(roffs)) {
1471 if (roffs >= free->length) {
1472 roffs -= free->length;
1473 continue;
1474 }
1475 boffs = free->offset + roffs;
1476 bytes = min_t(size_t, len,
1477 (free->length - roffs));
1478 roffs = 0;
1479 } else {
1480 bytes = min_t(size_t, len, free->length);
1481 boffs = free->offset;
1482 }
1483 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001484 oob += bytes;
1485 }
1486 return oob;
1487 }
1488 default:
1489 BUG();
1490 }
1491 return NULL;
1492}
1493
1494/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001495 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1496 * @mtd: MTD device structure
1497 * @retry_mode: the retry mode to use
1498 *
1499 * Some vendors supply a special command to shift the Vt threshold, to be used
1500 * when there are too many bitflips in a page (i.e., ECC error). After setting
1501 * a new threshold, the host should retry reading the page.
1502 */
1503static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1504{
1505 struct nand_chip *chip = mtd->priv;
1506
1507 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1508
1509 if (retry_mode >= chip->read_retries)
1510 return -EINVAL;
1511
1512 if (!chip->setup_read_retry)
1513 return -EOPNOTSUPP;
1514
1515 return chip->setup_read_retry(mtd, retry_mode);
1516}
1517
1518/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001519 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001520 * @mtd: MTD device structure
1521 * @from: offset to read from
1522 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001523 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001524 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001525 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001526static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1527 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001528{
Brian Norrise47f3db2012-05-02 10:14:56 -07001529 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001530 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001531 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001532 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001533 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001534 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001535 mtd->oobavail : mtd->oobsize;
1536
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001537 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001538 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001539 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001540 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001541 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001543 chipnr = (int)(from >> chip->chip_shift);
1544 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001546 realpage = (int)(from >> chip->page_shift);
1547 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001549 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001551 buf = ops->datbuf;
1552 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001553 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001554
Florian Fainellif8ac0412010-09-07 13:23:43 +02001555 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001556 unsigned int ecc_failures = mtd->ecc_stats.failed;
1557
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001558 bytes = min(mtd->writesize - col, readlen);
1559 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001560
Kamal Dasu66507c72014-05-01 20:51:19 -04001561 if (!aligned)
1562 use_bufpoi = 1;
1563 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1564 use_bufpoi = !virt_addr_valid(buf);
1565 else
1566 use_bufpoi = 0;
1567
Brian Norris8b6e50c2011-05-25 14:59:01 -07001568 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001569 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001570 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1571
1572 if (use_bufpoi && aligned)
1573 pr_debug("%s: using read bounce buffer for buf@%p\n",
1574 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Brian Norrisba84fb52014-01-03 15:13:33 -08001576read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001577 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Mike Dunnedbc45402012-04-25 12:06:11 -07001579 /*
1580 * Now read the page into the buffer. Absent an error,
1581 * the read methods return max bitflips per ecc step.
1582 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001583 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001584 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001585 oob_required,
1586 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001587 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1588 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001589 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001590 col, bytes, bufpoi,
1591 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001592 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001593 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001594 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001595 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001596 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001597 /* Invalidate page cache */
1598 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001599 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001600 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001601
Mike Dunnedbc45402012-04-25 12:06:11 -07001602 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1603
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001604 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001605 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001606 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001607 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001608 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001609 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001610 chip->pagebuf_bitflips = ret;
1611 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001612 /* Invalidate page cache */
1613 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001614 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001615 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001617
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001618 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001619 int toread = min(oobreadlen, max_oobsize);
1620
1621 if (toread) {
1622 oob = nand_transfer_oob(chip,
1623 oob, ops, toread);
1624 oobreadlen -= toread;
1625 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001626 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001627
1628 if (chip->options & NAND_NEED_READRDY) {
1629 /* Apply delay or wait for ready/busy pin */
1630 if (!chip->dev_ready)
1631 udelay(chip->chip_delay);
1632 else
1633 nand_wait_ready(mtd);
1634 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001635
Brian Norrisba84fb52014-01-03 15:13:33 -08001636 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001637 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001638 retry_mode++;
1639 ret = nand_setup_read_retry(mtd,
1640 retry_mode);
1641 if (ret < 0)
1642 break;
1643
1644 /* Reset failures; retry */
1645 mtd->ecc_stats.failed = ecc_failures;
1646 goto read_retry;
1647 } else {
1648 /* No more retry modes; real failure */
1649 ecc_fail = true;
1650 }
1651 }
1652
1653 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001654 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001655 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001656 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001657 max_bitflips = max_t(unsigned int, max_bitflips,
1658 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001661 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001662
Brian Norrisba84fb52014-01-03 15:13:33 -08001663 /* Reset to retry mode 0 */
1664 if (retry_mode) {
1665 ret = nand_setup_read_retry(mtd, 0);
1666 if (ret < 0)
1667 break;
1668 retry_mode = 0;
1669 }
1670
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001671 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Brian Norris8b6e50c2011-05-25 14:59:01 -07001674 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 col = 0;
1676 /* Increment page address */
1677 realpage++;
1678
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001679 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* Check, if we cross a chip boundary */
1681 if (!page) {
1682 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001683 chip->select_chip(mtd, -1);
1684 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001687 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001689 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001690 if (oob)
1691 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Mike Dunn3f91e942012-04-25 12:06:09 -07001693 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001694 return ret;
1695
Brian Norrisb72f3df2013-12-03 11:04:14 -08001696 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001697 return -EBADMSG;
1698
Mike Dunnedbc45402012-04-25 12:06:11 -07001699 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001700}
1701
1702/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001703 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001704 * @mtd: MTD device structure
1705 * @from: offset to read from
1706 * @len: number of bytes to read
1707 * @retlen: pointer to variable to store the number of read bytes
1708 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001709 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001710 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001711 */
1712static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1713 size_t *retlen, uint8_t *buf)
1714{
Brian Norris4a89ff82011-08-30 18:45:45 -07001715 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001716 int ret;
1717
Huang Shijie6a8214a2012-11-19 14:43:30 +08001718 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001719 ops.len = len;
1720 ops.datbuf = buf;
1721 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001722 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001723 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001724 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001725 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001726 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001730 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001731 * @mtd: mtd info structure
1732 * @chip: nand chip info structure
1733 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001734 */
1735static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001736 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001737{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001738 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001739 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001740 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001741}
1742
1743/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001744 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001745 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001746 * @mtd: mtd info structure
1747 * @chip: nand chip info structure
1748 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001749 */
1750static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001751 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001752{
1753 uint8_t *buf = chip->oob_poi;
1754 int length = mtd->oobsize;
1755 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1756 int eccsize = chip->ecc.size;
1757 uint8_t *bufpoi = buf;
1758 int i, toread, sndrnd = 0, pos;
1759
1760 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1761 for (i = 0; i < chip->ecc.steps; i++) {
1762 if (sndrnd) {
1763 pos = eccsize + i * (eccsize + chunk);
1764 if (mtd->writesize > 512)
1765 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1766 else
1767 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1768 } else
1769 sndrnd = 1;
1770 toread = min_t(int, length, chunk);
1771 chip->read_buf(mtd, bufpoi, toread);
1772 bufpoi += toread;
1773 length -= toread;
1774 }
1775 if (length > 0)
1776 chip->read_buf(mtd, bufpoi, length);
1777
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001778 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001779}
1780
1781/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001782 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001783 * @mtd: mtd info structure
1784 * @chip: nand chip info structure
1785 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001786 */
1787static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1788 int page)
1789{
1790 int status = 0;
1791 const uint8_t *buf = chip->oob_poi;
1792 int length = mtd->oobsize;
1793
1794 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1795 chip->write_buf(mtd, buf, length);
1796 /* Send command to program the OOB data */
1797 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1798
1799 status = chip->waitfunc(mtd, chip);
1800
Savin Zlobec0d420f92006-06-21 11:51:20 +02001801 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001802}
1803
1804/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001805 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001806 * with syndrome - only for large page flash
1807 * @mtd: mtd info structure
1808 * @chip: nand chip info structure
1809 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001810 */
1811static int nand_write_oob_syndrome(struct mtd_info *mtd,
1812 struct nand_chip *chip, int page)
1813{
1814 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1815 int eccsize = chip->ecc.size, length = mtd->oobsize;
1816 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1817 const uint8_t *bufpoi = chip->oob_poi;
1818
1819 /*
1820 * data-ecc-data-ecc ... ecc-oob
1821 * or
1822 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1823 */
1824 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1825 pos = steps * (eccsize + chunk);
1826 steps = 0;
1827 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001828 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001829
1830 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1831 for (i = 0; i < steps; i++) {
1832 if (sndcmd) {
1833 if (mtd->writesize <= 512) {
1834 uint32_t fill = 0xFFFFFFFF;
1835
1836 len = eccsize;
1837 while (len > 0) {
1838 int num = min_t(int, len, 4);
1839 chip->write_buf(mtd, (uint8_t *)&fill,
1840 num);
1841 len -= num;
1842 }
1843 } else {
1844 pos = eccsize + i * (eccsize + chunk);
1845 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1846 }
1847 } else
1848 sndcmd = 1;
1849 len = min_t(int, length, chunk);
1850 chip->write_buf(mtd, bufpoi, len);
1851 bufpoi += len;
1852 length -= len;
1853 }
1854 if (length > 0)
1855 chip->write_buf(mtd, bufpoi, length);
1856
1857 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1858 status = chip->waitfunc(mtd, chip);
1859
1860 return status & NAND_STATUS_FAIL ? -EIO : 0;
1861}
1862
1863/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001864 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001865 * @mtd: MTD device structure
1866 * @from: offset to read from
1867 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001869 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001871static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1872 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Brian Norrisc00a0992012-05-01 17:12:54 -07001874 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001875 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001876 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001877 int readlen = ops->ooblen;
1878 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001879 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001880 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Brian Norris289c0522011-07-19 10:06:09 -07001882 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301883 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Brian Norris041e4572011-06-23 16:45:24 -07001885 stats = mtd->ecc_stats;
1886
Brian Norris0612b9d2011-08-30 18:45:40 -07001887 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001888 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001889 else
1890 len = mtd->oobsize;
1891
1892 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001893 pr_debug("%s: attempt to start read outside oob\n",
1894 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001895 return -EINVAL;
1896 }
1897
1898 /* Do not allow reads past end of device */
1899 if (unlikely(from >= mtd->size ||
1900 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1901 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001902 pr_debug("%s: attempt to read beyond end of device\n",
1903 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001904 return -EINVAL;
1905 }
Vitaly Wool70145682006-11-03 18:20:38 +03001906
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001907 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001908 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001910 /* Shift to get page */
1911 realpage = (int)(from >> chip->page_shift);
1912 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Florian Fainellif8ac0412010-09-07 13:23:43 +02001914 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001915 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001916 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001917 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001918 ret = chip->ecc.read_oob(mtd, chip, page);
1919
1920 if (ret < 0)
1921 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001922
1923 len = min(len, readlen);
1924 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001925
Brian Norris5bc7c332013-03-13 09:51:31 -07001926 if (chip->options & NAND_NEED_READRDY) {
1927 /* Apply delay or wait for ready/busy pin */
1928 if (!chip->dev_ready)
1929 udelay(chip->chip_delay);
1930 else
1931 nand_wait_ready(mtd);
1932 }
1933
Vitaly Wool70145682006-11-03 18:20:38 +03001934 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001935 if (!readlen)
1936 break;
1937
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001938 /* Increment page address */
1939 realpage++;
1940
1941 page = realpage & chip->pagemask;
1942 /* Check, if we cross a chip boundary */
1943 if (!page) {
1944 chipnr++;
1945 chip->select_chip(mtd, -1);
1946 chip->select_chip(mtd, chipnr);
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001949 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001951 ops->oobretlen = ops->ooblen - readlen;
1952
1953 if (ret < 0)
1954 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001955
1956 if (mtd->ecc_stats.failed - stats.failed)
1957 return -EBADMSG;
1958
1959 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001963 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001964 * @mtd: MTD device structure
1965 * @from: offset to read from
1966 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001968 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001970static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1971 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001973 int ret = -ENOTSUPP;
1974
1975 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001978 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001979 pr_debug("%s: attempt to read beyond end of device\n",
1980 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return -EINVAL;
1982 }
1983
Huang Shijie6a8214a2012-11-19 14:43:30 +08001984 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Florian Fainellif8ac0412010-09-07 13:23:43 +02001986 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001987 case MTD_OPS_PLACE_OOB:
1988 case MTD_OPS_AUTO_OOB:
1989 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001990 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001991
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001992 default:
1993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001996 if (!ops->datbuf)
1997 ret = nand_do_read_oob(mtd, from, ops);
1998 else
1999 ret = nand_do_read_ops(mtd, from, ops);
2000
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002003 return ret;
2004}
2005
2006
2007/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002008 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002009 * @mtd: mtd info structure
2010 * @chip: nand chip info structure
2011 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002012 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002013 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002014 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002015 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002016static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002017 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002018{
2019 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002020 if (oob_required)
2021 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002022
2023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002026/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002027 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002028 * @mtd: mtd info structure
2029 * @chip: nand chip info structure
2030 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002031 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002032 *
2033 * We need a special oob layout and handling even when ECC isn't checked.
2034 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002035static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002036 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002037 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002038{
2039 int eccsize = chip->ecc.size;
2040 int eccbytes = chip->ecc.bytes;
2041 uint8_t *oob = chip->oob_poi;
2042 int steps, size;
2043
2044 for (steps = chip->ecc.steps; steps > 0; steps--) {
2045 chip->write_buf(mtd, buf, eccsize);
2046 buf += eccsize;
2047
2048 if (chip->ecc.prepad) {
2049 chip->write_buf(mtd, oob, chip->ecc.prepad);
2050 oob += chip->ecc.prepad;
2051 }
2052
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002053 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002054 oob += eccbytes;
2055
2056 if (chip->ecc.postpad) {
2057 chip->write_buf(mtd, oob, chip->ecc.postpad);
2058 oob += chip->ecc.postpad;
2059 }
2060 }
2061
2062 size = mtd->oobsize - (oob - chip->oob_poi);
2063 if (size)
2064 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002065
2066 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002067}
2068/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002069 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002070 * @mtd: mtd info structure
2071 * @chip: nand chip info structure
2072 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002073 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002074 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002075static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002076 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002077{
2078 int i, eccsize = chip->ecc.size;
2079 int eccbytes = chip->ecc.bytes;
2080 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002081 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002082 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002083 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002084
Brian Norris7854d3f2011-06-23 14:12:08 -07002085 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002086 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2087 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002088
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002089 for (i = 0; i < chip->ecc.total; i++)
2090 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002091
Josh Wufdbad98d2012-06-25 18:07:45 +08002092 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002093}
2094
2095/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002096 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002097 * @mtd: mtd info structure
2098 * @chip: nand chip info structure
2099 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002100 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002101 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002102static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002103 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002104{
2105 int i, eccsize = chip->ecc.size;
2106 int eccbytes = chip->ecc.bytes;
2107 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002108 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002109 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002110 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002111
2112 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2113 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002114 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002115 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2116 }
2117
2118 for (i = 0; i < chip->ecc.total; i++)
2119 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2120
2121 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002122
2123 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002124}
2125
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302126
2127/**
2128 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2129 * @mtd: mtd info structure
2130 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002131 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302132 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002133 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302134 * @oob_required: must write chip->oob_poi to OOB
2135 */
2136static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2137 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002138 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302139 int oob_required)
2140{
2141 uint8_t *oob_buf = chip->oob_poi;
2142 uint8_t *ecc_calc = chip->buffers->ecccalc;
2143 int ecc_size = chip->ecc.size;
2144 int ecc_bytes = chip->ecc.bytes;
2145 int ecc_steps = chip->ecc.steps;
2146 uint32_t *eccpos = chip->ecc.layout->eccpos;
2147 uint32_t start_step = offset / ecc_size;
2148 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2149 int oob_bytes = mtd->oobsize / ecc_steps;
2150 int step, i;
2151
2152 for (step = 0; step < ecc_steps; step++) {
2153 /* configure controller for WRITE access */
2154 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2155
2156 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002157 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302158
2159 /* mask ECC of un-touched subpages by padding 0xFF */
2160 if ((step < start_step) || (step > end_step))
2161 memset(ecc_calc, 0xff, ecc_bytes);
2162 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002163 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302164
2165 /* mask OOB of un-touched subpages by padding 0xFF */
2166 /* if oob_required, preserve OOB metadata of written subpage */
2167 if (!oob_required || (step < start_step) || (step > end_step))
2168 memset(oob_buf, 0xff, oob_bytes);
2169
Brian Norrisd6a950802013-08-08 17:16:36 -07002170 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302171 ecc_calc += ecc_bytes;
2172 oob_buf += oob_bytes;
2173 }
2174
2175 /* copy calculated ECC for whole page to chip->buffer->oob */
2176 /* this include masked-value(0xFF) for unwritten subpages */
2177 ecc_calc = chip->buffers->ecccalc;
2178 for (i = 0; i < chip->ecc.total; i++)
2179 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2180
2181 /* write OOB buffer to NAND device */
2182 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2183
2184 return 0;
2185}
2186
2187
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002188/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002189 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002190 * @mtd: mtd info structure
2191 * @chip: nand chip info structure
2192 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002193 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002194 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002195 * The hw generator calculates the error syndrome automatically. Therefore we
2196 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002197 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002198static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002199 struct nand_chip *chip,
2200 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002201{
2202 int i, eccsize = chip->ecc.size;
2203 int eccbytes = chip->ecc.bytes;
2204 int eccsteps = chip->ecc.steps;
2205 const uint8_t *p = buf;
2206 uint8_t *oob = chip->oob_poi;
2207
2208 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2209
2210 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2211 chip->write_buf(mtd, p, eccsize);
2212
2213 if (chip->ecc.prepad) {
2214 chip->write_buf(mtd, oob, chip->ecc.prepad);
2215 oob += chip->ecc.prepad;
2216 }
2217
2218 chip->ecc.calculate(mtd, p, oob);
2219 chip->write_buf(mtd, oob, eccbytes);
2220 oob += eccbytes;
2221
2222 if (chip->ecc.postpad) {
2223 chip->write_buf(mtd, oob, chip->ecc.postpad);
2224 oob += chip->ecc.postpad;
2225 }
2226 }
2227
2228 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002229 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002230 if (i)
2231 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002232
2233 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002234}
2235
2236/**
David Woodhouse956e9442006-09-25 17:12:39 +01002237 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002238 * @mtd: MTD device structure
2239 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302240 * @offset: address offset within the page
2241 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002242 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002243 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002244 * @page: page number to write
2245 * @cached: cached programming
2246 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002247 */
2248static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302249 uint32_t offset, int data_len, const uint8_t *buf,
2250 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002251{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302252 int status, subpage;
2253
2254 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2255 chip->ecc.write_subpage)
2256 subpage = offset || (data_len < mtd->writesize);
2257 else
2258 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002259
2260 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2261
David Woodhouse956e9442006-09-25 17:12:39 +01002262 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302263 status = chip->ecc.write_page_raw(mtd, chip, buf,
2264 oob_required);
2265 else if (subpage)
2266 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2267 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002268 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002269 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2270
2271 if (status < 0)
2272 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002273
2274 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002275 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002276 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002277 */
2278 cached = 0;
2279
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002280 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002281
2282 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002283 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002284 /*
2285 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002286 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002287 */
2288 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2289 status = chip->errstat(mtd, chip, FL_WRITING, status,
2290 page);
2291
2292 if (status & NAND_STATUS_FAIL)
2293 return -EIO;
2294 } else {
2295 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002296 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002297 }
2298
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002299 return 0;
2300}
2301
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002302/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002303 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002304 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002305 * @oob: oob data buffer
2306 * @len: oob data write length
2307 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002308 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002309static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2310 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002311{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002312 struct nand_chip *chip = mtd->priv;
2313
2314 /*
2315 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2316 * data from a previous OOB read.
2317 */
2318 memset(chip->oob_poi, 0xff, mtd->oobsize);
2319
Florian Fainellif8ac0412010-09-07 13:23:43 +02002320 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321
Brian Norris0612b9d2011-08-30 18:45:40 -07002322 case MTD_OPS_PLACE_OOB:
2323 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002324 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2325 return oob + len;
2326
Brian Norris0612b9d2011-08-30 18:45:40 -07002327 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002328 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002329 uint32_t boffs = 0, woffs = ops->ooboffs;
2330 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002331
Florian Fainellif8ac0412010-09-07 13:23:43 +02002332 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002333 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002334 if (unlikely(woffs)) {
2335 if (woffs >= free->length) {
2336 woffs -= free->length;
2337 continue;
2338 }
2339 boffs = free->offset + woffs;
2340 bytes = min_t(size_t, len,
2341 (free->length - woffs));
2342 woffs = 0;
2343 } else {
2344 bytes = min_t(size_t, len, free->length);
2345 boffs = free->offset;
2346 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002347 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002348 oob += bytes;
2349 }
2350 return oob;
2351 }
2352 default:
2353 BUG();
2354 }
2355 return NULL;
2356}
2357
Florian Fainellif8ac0412010-09-07 13:23:43 +02002358#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002359
2360/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002361 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002362 * @mtd: MTD device structure
2363 * @to: offset to write to
2364 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002365 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002366 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002367 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002368static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2369 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002370{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002371 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002372 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002373 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002374
2375 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002376 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002377 mtd->oobavail : mtd->oobsize;
2378
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002379 uint8_t *oob = ops->oobbuf;
2380 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302381 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002382 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002383
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002384 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002385 if (!writelen)
2386 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002387
Brian Norris8b6e50c2011-05-25 14:59:01 -07002388 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002389 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002390 pr_notice("%s: attempt to write non page aligned data\n",
2391 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002392 return -EINVAL;
2393 }
2394
Thomas Gleixner29072b92006-09-28 15:38:36 +02002395 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002396
Thomas Gleixner6a930962006-06-28 00:11:45 +02002397 chipnr = (int)(to >> chip->chip_shift);
2398 chip->select_chip(mtd, chipnr);
2399
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002400 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002401 if (nand_check_wp(mtd)) {
2402 ret = -EIO;
2403 goto err_out;
2404 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002405
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002406 realpage = (int)(to >> chip->page_shift);
2407 page = realpage & chip->pagemask;
2408 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2409
2410 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002411 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2412 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002413 chip->pagebuf = -1;
2414
Maxim Levitsky782ce792010-02-22 20:39:36 +02002415 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002416 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2417 ret = -EINVAL;
2418 goto err_out;
2419 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002420
Florian Fainellif8ac0412010-09-07 13:23:43 +02002421 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002422 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002423 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002424 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002425 int use_bufpoi;
2426 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002427
Kamal Dasu66507c72014-05-01 20:51:19 -04002428 if (part_pagewr)
2429 use_bufpoi = 1;
2430 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2431 use_bufpoi = !virt_addr_valid(buf);
2432 else
2433 use_bufpoi = 0;
2434
2435 /* Partial page write?, or need to use bounce buffer */
2436 if (use_bufpoi) {
2437 pr_debug("%s: using write bounce buffer for buf@%p\n",
2438 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002439 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002440 if (part_pagewr)
2441 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002442 chip->pagebuf = -1;
2443 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2444 memcpy(&chip->buffers->databuf[column], buf, bytes);
2445 wbuf = chip->buffers->databuf;
2446 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002447
Maxim Levitsky782ce792010-02-22 20:39:36 +02002448 if (unlikely(oob)) {
2449 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002450 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002451 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002452 } else {
2453 /* We still need to erase leftover OOB data */
2454 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002455 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302456 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2457 oob_required, page, cached,
2458 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002459 if (ret)
2460 break;
2461
2462 writelen -= bytes;
2463 if (!writelen)
2464 break;
2465
Thomas Gleixner29072b92006-09-28 15:38:36 +02002466 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002467 buf += bytes;
2468 realpage++;
2469
2470 page = realpage & chip->pagemask;
2471 /* Check, if we cross a chip boundary */
2472 if (!page) {
2473 chipnr++;
2474 chip->select_chip(mtd, -1);
2475 chip->select_chip(mtd, chipnr);
2476 }
2477 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002478
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002480 if (unlikely(oob))
2481 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002482
2483err_out:
2484 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002485 return ret;
2486}
2487
2488/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002489 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002490 * @mtd: MTD device structure
2491 * @to: offset to write to
2492 * @len: number of bytes to write
2493 * @retlen: pointer to variable to store the number of written bytes
2494 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002495 *
2496 * NAND write with ECC. Used when performing writes in interrupt context, this
2497 * may for example be called by mtdoops when writing an oops while in panic.
2498 */
2499static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2500 size_t *retlen, const uint8_t *buf)
2501{
2502 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002503 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002504 int ret;
2505
Brian Norris8b6e50c2011-05-25 14:59:01 -07002506 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002507 panic_nand_wait(mtd, chip, 400);
2508
Brian Norris8b6e50c2011-05-25 14:59:01 -07002509 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002510 panic_nand_get_device(chip, mtd, FL_WRITING);
2511
Brian Norris4a89ff82011-08-30 18:45:45 -07002512 ops.len = len;
2513 ops.datbuf = (uint8_t *)buf;
2514 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002515 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002516
Brian Norris4a89ff82011-08-30 18:45:45 -07002517 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002518
Brian Norris4a89ff82011-08-30 18:45:45 -07002519 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002520 return ret;
2521}
2522
2523/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002524 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002525 * @mtd: MTD device structure
2526 * @to: offset to write to
2527 * @len: number of bytes to write
2528 * @retlen: pointer to variable to store the number of written bytes
2529 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002531 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002533static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002534 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Brian Norris4a89ff82011-08-30 18:45:45 -07002536 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002537 int ret;
2538
Huang Shijie6a8214a2012-11-19 14:43:30 +08002539 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002540 ops.len = len;
2541 ops.datbuf = (uint8_t *)buf;
2542 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002543 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002544 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002545 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002546 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002547 return ret;
2548}
2549
2550/**
2551 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002552 * @mtd: MTD device structure
2553 * @to: offset to write to
2554 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002555 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002556 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002557 */
2558static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2559 struct mtd_oob_ops *ops)
2560{
Adrian Hunter03736152007-01-31 17:58:29 +02002561 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002562 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Brian Norris289c0522011-07-19 10:06:09 -07002564 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302565 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Brian Norris0612b9d2011-08-30 18:45:40 -07002567 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002568 len = chip->ecc.layout->oobavail;
2569 else
2570 len = mtd->oobsize;
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002573 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002574 pr_debug("%s: attempt to write past end of page\n",
2575 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -EINVAL;
2577 }
2578
Adrian Hunter03736152007-01-31 17:58:29 +02002579 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002580 pr_debug("%s: attempt to start write outside oob\n",
2581 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002582 return -EINVAL;
2583 }
2584
Jason Liu775adc32011-02-25 13:06:18 +08002585 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002586 if (unlikely(to >= mtd->size ||
2587 ops->ooboffs + ops->ooblen >
2588 ((mtd->size >> chip->page_shift) -
2589 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002590 pr_debug("%s: attempt to write beyond end of device\n",
2591 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002592 return -EINVAL;
2593 }
2594
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002595 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002596 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002598 /* Shift to get page */
2599 page = (int)(to >> chip->page_shift);
2600
2601 /*
2602 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2603 * of my DiskOnChip 2000 test units) will clear the whole data page too
2604 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2605 * it in the doc2000 driver in August 1999. dwmw2.
2606 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002607 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002610 if (nand_check_wp(mtd)) {
2611 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002612 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002613 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002616 if (page == chip->pagebuf)
2617 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002619 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002620
Brian Norris0612b9d2011-08-30 18:45:40 -07002621 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002622 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2623 else
2624 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002625
Huang Shijieb0bb6902012-11-19 14:43:29 +08002626 chip->select_chip(mtd, -1);
2627
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002628 if (status)
2629 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Vitaly Wool70145682006-11-03 18:20:38 +03002631 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002633 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002636/**
2637 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002638 * @mtd: MTD device structure
2639 * @to: offset to write to
2640 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002641 */
2642static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2643 struct mtd_oob_ops *ops)
2644{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002645 int ret = -ENOTSUPP;
2646
2647 ops->retlen = 0;
2648
2649 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002650 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002651 pr_debug("%s: attempt to write beyond end of device\n",
2652 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002653 return -EINVAL;
2654 }
2655
Huang Shijie6a8214a2012-11-19 14:43:30 +08002656 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002657
Florian Fainellif8ac0412010-09-07 13:23:43 +02002658 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002659 case MTD_OPS_PLACE_OOB:
2660 case MTD_OPS_AUTO_OOB:
2661 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002662 break;
2663
2664 default:
2665 goto out;
2666 }
2667
2668 if (!ops->datbuf)
2669 ret = nand_do_write_oob(mtd, to, ops);
2670 else
2671 ret = nand_do_write_ops(mtd, to, ops);
2672
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002673out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002674 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return ret;
2676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678/**
Brian Norris49c50b92014-05-06 16:02:19 -07002679 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002680 * @mtd: MTD device structure
2681 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 *
Brian Norris49c50b92014-05-06 16:02:19 -07002683 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 */
Brian Norris49c50b92014-05-06 16:02:19 -07002685static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002687 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002689 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2690 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002691
2692 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @mtd: MTD device structure
2698 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002700 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002702static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
David Woodhousee0c7d762006-05-13 18:07:53 +01002704 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002708 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002709 * @mtd: MTD device structure
2710 * @instr: erase instruction
2711 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002713 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002715int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2716 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Adrian Hunter69423d92008-12-10 13:37:21 +00002718 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002719 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002720 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Brian Norris289c0522011-07-19 10:06:09 -07002722 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2723 __func__, (unsigned long long)instr->addr,
2724 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302726 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002730 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002733 page = (int)(instr->addr >> chip->page_shift);
2734 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002737 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002740 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* Check, if it is write protected */
2743 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002744 pr_debug("%s: device is write protected!\n",
2745 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 instr->state = MTD_ERASE_FAILED;
2747 goto erase_exit;
2748 }
2749
2750 /* Loop through the pages */
2751 len = instr->len;
2752
2753 instr->state = MTD_ERASING;
2754
2755 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002756 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002757 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2758 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002759 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2760 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 instr->state = MTD_ERASE_FAILED;
2762 goto erase_exit;
2763 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002764
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002765 /*
2766 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002767 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002768 */
2769 if (page <= chip->pagebuf && chip->pagebuf <
2770 (page + pages_per_block))
2771 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Brian Norris49c50b92014-05-06 16:02:19 -07002773 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002775 /*
2776 * See if operation failed and additional status checks are
2777 * available
2778 */
2779 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2780 status = chip->errstat(mtd, chip, FL_ERASING,
2781 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002784 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002785 pr_debug("%s: failed erase, page 0x%08x\n",
2786 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002788 instr->fail_addr =
2789 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 goto erase_exit;
2791 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002794 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 page += pages_per_block;
2796
2797 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002798 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002800 chip->select_chip(mtd, -1);
2801 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803 }
2804 instr->state = MTD_ERASE_DONE;
2805
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002806erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002811 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 nand_release_device(mtd);
2813
David Woodhouse49defc02007-10-06 15:01:59 -04002814 /* Do call back function */
2815 if (!ret)
2816 mtd_erase_callback(instr);
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /* Return more or less happy */
2819 return ret;
2820}
2821
2822/**
2823 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002824 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002826 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002828static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Brian Norris289c0522011-07-19 10:06:09 -07002830 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002833 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002835 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002839 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002840 * @mtd: MTD device structure
2841 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002843static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002845 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002849 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002850 * @mtd: MTD device structure
2851 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002853static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int ret;
2856
Florian Fainellif8ac0412010-09-07 13:23:43 +02002857 ret = nand_block_isbad(mtd, ofs);
2858 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002859 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (ret > 0)
2861 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002862 return ret;
2863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Brian Norris5a0edb22013-07-30 17:52:58 -07002865 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
2868/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002869 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2870 * @mtd: MTD device structure
2871 * @chip: nand chip info structure
2872 * @addr: feature address.
2873 * @subfeature_param: the subfeature parameters, a four bytes array.
2874 */
2875static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2876 int addr, uint8_t *subfeature_param)
2877{
2878 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002879 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002880
David Mosbergerd914c932013-05-29 15:30:13 +03002881 if (!chip->onfi_version ||
2882 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2883 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002884 return -EINVAL;
2885
2886 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002887 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2888 chip->write_byte(mtd, subfeature_param[i]);
2889
Huang Shijie7db03ec2012-09-13 14:57:52 +08002890 status = chip->waitfunc(mtd, chip);
2891 if (status & NAND_STATUS_FAIL)
2892 return -EIO;
2893 return 0;
2894}
2895
2896/**
2897 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2898 * @mtd: MTD device structure
2899 * @chip: nand chip info structure
2900 * @addr: feature address.
2901 * @subfeature_param: the subfeature parameters, a four bytes array.
2902 */
2903static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2904 int addr, uint8_t *subfeature_param)
2905{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002906 int i;
2907
David Mosbergerd914c932013-05-29 15:30:13 +03002908 if (!chip->onfi_version ||
2909 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2910 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002911 return -EINVAL;
2912
2913 /* clear the sub feature parameters */
2914 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2915
2916 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002917 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2918 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002919 return 0;
2920}
2921
2922/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002923 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002924 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002925 */
2926static int nand_suspend(struct mtd_info *mtd)
2927{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002928 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002929}
2930
2931/**
2932 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002933 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002934 */
2935static void nand_resume(struct mtd_info *mtd)
2936{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002937 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002938
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002939 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002940 nand_release_device(mtd);
2941 else
Brian Norrisd0370212011-07-19 10:06:08 -07002942 pr_err("%s called for a chip which is not in suspended state\n",
2943 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002944}
2945
Scott Branden72ea4032014-11-20 11:18:05 -08002946/**
2947 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
2948 * prevent further operations
2949 * @mtd: MTD device structure
2950 */
2951static void nand_shutdown(struct mtd_info *mtd)
2952{
2953 nand_get_device(mtd, FL_SHUTDOWN);
2954}
2955
Brian Norris8b6e50c2011-05-25 14:59:01 -07002956/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002957static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002960 if (!chip->chip_delay)
2961 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002964 if (chip->cmdfunc == NULL)
2965 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002968 if (chip->waitfunc == NULL)
2969 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002971 if (!chip->select_chip)
2972 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002973
Huang Shijie4204ccc2013-08-16 10:10:07 +08002974 /* set for ONFI nand */
2975 if (!chip->onfi_set_features)
2976 chip->onfi_set_features = nand_onfi_set_features;
2977 if (!chip->onfi_get_features)
2978 chip->onfi_get_features = nand_onfi_get_features;
2979
Brian Norris68e80782013-07-18 01:17:02 -07002980 /* If called twice, pointers that depend on busw may need to be reset */
2981 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002982 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2983 if (!chip->read_word)
2984 chip->read_word = nand_read_word;
2985 if (!chip->block_bad)
2986 chip->block_bad = nand_block_bad;
2987 if (!chip->block_markbad)
2988 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002989 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002990 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002991 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2992 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07002993 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002994 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002995 if (!chip->scan_bbt)
2996 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002997
2998 if (!chip->controller) {
2999 chip->controller = &chip->hwcontrol;
3000 spin_lock_init(&chip->controller->lock);
3001 init_waitqueue_head(&chip->controller->wq);
3002 }
3003
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003004}
3005
Brian Norris8b6e50c2011-05-25 14:59:01 -07003006/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003007static void sanitize_string(uint8_t *s, size_t len)
3008{
3009 ssize_t i;
3010
Brian Norris8b6e50c2011-05-25 14:59:01 -07003011 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003012 s[len - 1] = 0;
3013
Brian Norris8b6e50c2011-05-25 14:59:01 -07003014 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003015 for (i = 0; i < len - 1; i++) {
3016 if (s[i] < ' ' || s[i] > 127)
3017 s[i] = '?';
3018 }
3019
Brian Norris8b6e50c2011-05-25 14:59:01 -07003020 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003021 strim(s);
3022}
3023
3024static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3025{
3026 int i;
3027 while (len--) {
3028 crc ^= *p++ << 8;
3029 for (i = 0; i < 8; i++)
3030 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3031 }
3032
3033 return crc;
3034}
3035
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003036/* Parse the Extended Parameter Page. */
3037static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3038 struct nand_chip *chip, struct nand_onfi_params *p)
3039{
3040 struct onfi_ext_param_page *ep;
3041 struct onfi_ext_section *s;
3042 struct onfi_ext_ecc_info *ecc;
3043 uint8_t *cursor;
3044 int ret = -EINVAL;
3045 int len;
3046 int i;
3047
3048 len = le16_to_cpu(p->ext_param_page_length) * 16;
3049 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003050 if (!ep)
3051 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003052
3053 /* Send our own NAND_CMD_PARAM. */
3054 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3055
3056 /* Use the Change Read Column command to skip the ONFI param pages. */
3057 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3058 sizeof(*p) * p->num_of_param_pages , -1);
3059
3060 /* Read out the Extended Parameter Page. */
3061 chip->read_buf(mtd, (uint8_t *)ep, len);
3062 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3063 != le16_to_cpu(ep->crc))) {
3064 pr_debug("fail in the CRC.\n");
3065 goto ext_out;
3066 }
3067
3068 /*
3069 * Check the signature.
3070 * Do not strictly follow the ONFI spec, maybe changed in future.
3071 */
3072 if (strncmp(ep->sig, "EPPS", 4)) {
3073 pr_debug("The signature is invalid.\n");
3074 goto ext_out;
3075 }
3076
3077 /* find the ECC section. */
3078 cursor = (uint8_t *)(ep + 1);
3079 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3080 s = ep->sections + i;
3081 if (s->type == ONFI_SECTION_TYPE_2)
3082 break;
3083 cursor += s->length * 16;
3084 }
3085 if (i == ONFI_EXT_SECTION_MAX) {
3086 pr_debug("We can not find the ECC section.\n");
3087 goto ext_out;
3088 }
3089
3090 /* get the info we want. */
3091 ecc = (struct onfi_ext_ecc_info *)cursor;
3092
Brian Norris4ae7d222013-09-16 18:20:21 -07003093 if (!ecc->codeword_size) {
3094 pr_debug("Invalid codeword size\n");
3095 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003096 }
3097
Brian Norris4ae7d222013-09-16 18:20:21 -07003098 chip->ecc_strength_ds = ecc->ecc_bits;
3099 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003100 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003101
3102ext_out:
3103 kfree(ep);
3104 return ret;
3105}
3106
Brian Norris8429bb32013-12-03 15:51:09 -08003107static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3108{
3109 struct nand_chip *chip = mtd->priv;
3110 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3111
3112 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3113 feature);
3114}
3115
3116/*
3117 * Configure chip properties from Micron vendor-specific ONFI table
3118 */
3119static void nand_onfi_detect_micron(struct nand_chip *chip,
3120 struct nand_onfi_params *p)
3121{
3122 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3123
3124 if (le16_to_cpu(p->vendor_revision) < 1)
3125 return;
3126
3127 chip->read_retries = micron->read_retry_options;
3128 chip->setup_read_retry = nand_setup_read_retry_micron;
3129}
3130
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003131/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003132 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003133 */
3134static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003135 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003136{
3137 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003138 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003139 int val;
3140
Brian Norris7854d3f2011-06-23 14:12:08 -07003141 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003142 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3143 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3144 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3145 return 0;
3146
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003147 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3148 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003149 for (j = 0; j < sizeof(*p); j++)
3150 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003151 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3152 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003153 break;
3154 }
3155 }
3156
Brian Norrisc7f23a72013-08-13 10:51:55 -07003157 if (i == 3) {
3158 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003159 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003160 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003161
Brian Norris8b6e50c2011-05-25 14:59:01 -07003162 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003163 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003164 if (val & (1 << 5))
3165 chip->onfi_version = 23;
3166 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003167 chip->onfi_version = 22;
3168 else if (val & (1 << 3))
3169 chip->onfi_version = 21;
3170 else if (val & (1 << 2))
3171 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003172 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003173 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003174
3175 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003176 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003177 return 0;
3178 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003179
3180 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3181 sanitize_string(p->model, sizeof(p->model));
3182 if (!mtd->name)
3183 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003184
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003185 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003186
3187 /*
3188 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3189 * (don't ask me who thought of this...). MTD assumes that these
3190 * dimensions will be power-of-2, so just truncate the remaining area.
3191 */
3192 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3193 mtd->erasesize *= mtd->writesize;
3194
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003195 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003196
3197 /* See erasesize comment */
3198 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003199 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003200 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003201
3202 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003203 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003204 else
3205 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003206
Huang Shijie10c86ba2013-05-17 11:17:26 +08003207 if (p->ecc_bits != 0xff) {
3208 chip->ecc_strength_ds = p->ecc_bits;
3209 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003210 } else if (chip->onfi_version >= 21 &&
3211 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3212
3213 /*
3214 * The nand_flash_detect_ext_param_page() uses the
3215 * Change Read Column command which maybe not supported
3216 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3217 * now. We do not replace user supplied command function.
3218 */
3219 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3220 chip->cmdfunc = nand_command_lp;
3221
3222 /* The Extended Parameter Page is supported since ONFI 2.1. */
3223 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003224 pr_warn("Failed to detect ONFI extended param page\n");
3225 } else {
3226 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003227 }
3228
Brian Norris8429bb32013-12-03 15:51:09 -08003229 if (p->jedec_id == NAND_MFR_MICRON)
3230 nand_onfi_detect_micron(chip, p);
3231
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003232 return 1;
3233}
3234
3235/*
Huang Shijie91361812014-02-21 13:39:40 +08003236 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3237 */
3238static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3239 int *busw)
3240{
3241 struct nand_jedec_params *p = &chip->jedec_params;
3242 struct jedec_ecc_info *ecc;
3243 int val;
3244 int i, j;
3245
3246 /* Try JEDEC for unknown chip or LP */
3247 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3248 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3249 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3250 chip->read_byte(mtd) != 'C')
3251 return 0;
3252
3253 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3254 for (i = 0; i < 3; i++) {
3255 for (j = 0; j < sizeof(*p); j++)
3256 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3257
3258 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3259 le16_to_cpu(p->crc))
3260 break;
3261 }
3262
3263 if (i == 3) {
3264 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3265 return 0;
3266 }
3267
3268 /* Check version */
3269 val = le16_to_cpu(p->revision);
3270 if (val & (1 << 2))
3271 chip->jedec_version = 10;
3272 else if (val & (1 << 1))
3273 chip->jedec_version = 1; /* vendor specific version */
3274
3275 if (!chip->jedec_version) {
3276 pr_info("unsupported JEDEC version: %d\n", val);
3277 return 0;
3278 }
3279
3280 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3281 sanitize_string(p->model, sizeof(p->model));
3282 if (!mtd->name)
3283 mtd->name = p->model;
3284
3285 mtd->writesize = le32_to_cpu(p->byte_per_page);
3286
3287 /* Please reference to the comment for nand_flash_detect_onfi. */
3288 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3289 mtd->erasesize *= mtd->writesize;
3290
3291 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3292
3293 /* Please reference to the comment for nand_flash_detect_onfi. */
3294 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3295 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3296 chip->bits_per_cell = p->bits_per_cell;
3297
3298 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3299 *busw = NAND_BUSWIDTH_16;
3300 else
3301 *busw = 0;
3302
3303 /* ECC info */
3304 ecc = &p->ecc_info[0];
3305
3306 if (ecc->codeword_size >= 9) {
3307 chip->ecc_strength_ds = ecc->ecc_bits;
3308 chip->ecc_step_ds = 1 << ecc->codeword_size;
3309 } else {
3310 pr_warn("Invalid codeword size\n");
3311 }
3312
3313 return 1;
3314}
3315
3316/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003317 * nand_id_has_period - Check if an ID string has a given wraparound period
3318 * @id_data: the ID string
3319 * @arrlen: the length of the @id_data array
3320 * @period: the period of repitition
3321 *
3322 * Check if an ID string is repeated within a given sequence of bytes at
3323 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003324 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003325 * if the repetition has a period of @period; otherwise, returns zero.
3326 */
3327static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3328{
3329 int i, j;
3330 for (i = 0; i < period; i++)
3331 for (j = i + period; j < arrlen; j += period)
3332 if (id_data[i] != id_data[j])
3333 return 0;
3334 return 1;
3335}
3336
3337/*
3338 * nand_id_len - Get the length of an ID string returned by CMD_READID
3339 * @id_data: the ID string
3340 * @arrlen: the length of the @id_data array
3341
3342 * Returns the length of the ID string, according to known wraparound/trailing
3343 * zero patterns. If no pattern exists, returns the length of the array.
3344 */
3345static int nand_id_len(u8 *id_data, int arrlen)
3346{
3347 int last_nonzero, period;
3348
3349 /* Find last non-zero byte */
3350 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3351 if (id_data[last_nonzero])
3352 break;
3353
3354 /* All zeros */
3355 if (last_nonzero < 0)
3356 return 0;
3357
3358 /* Calculate wraparound period */
3359 for (period = 1; period < arrlen; period++)
3360 if (nand_id_has_period(id_data, arrlen, period))
3361 break;
3362
3363 /* There's a repeated pattern */
3364 if (period < arrlen)
3365 return period;
3366
3367 /* There are trailing zeros */
3368 if (last_nonzero < arrlen - 1)
3369 return last_nonzero + 1;
3370
3371 /* No pattern detected */
3372 return arrlen;
3373}
3374
Huang Shijie7db906b2013-09-25 14:58:11 +08003375/* Extract the bits of per cell from the 3rd byte of the extended ID */
3376static int nand_get_bits_per_cell(u8 cellinfo)
3377{
3378 int bits;
3379
3380 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3381 bits >>= NAND_CI_CELLTYPE_SHIFT;
3382 return bits + 1;
3383}
3384
Brian Norrise3b88bd2012-09-24 20:40:52 -07003385/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003386 * Many new NAND share similar device ID codes, which represent the size of the
3387 * chip. The rest of the parameters must be decoded according to generic or
3388 * manufacturer-specific "extended ID" decoding patterns.
3389 */
3390static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3391 u8 id_data[8], int *busw)
3392{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003393 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003394 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003395 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003396 /* The 4th id byte is the important one */
3397 extid = id_data[3];
3398
Brian Norrise3b88bd2012-09-24 20:40:52 -07003399 id_len = nand_id_len(id_data, 8);
3400
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003401 /*
3402 * Field definitions are in the following datasheets:
3403 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003404 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003405 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003406 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003407 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3408 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003409 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003410 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003411 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003412 /* Calc pagesize */
3413 mtd->writesize = 2048 << (extid & 0x03);
3414 extid >>= 2;
3415 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003416 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003417 case 1:
3418 mtd->oobsize = 128;
3419 break;
3420 case 2:
3421 mtd->oobsize = 218;
3422 break;
3423 case 3:
3424 mtd->oobsize = 400;
3425 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003426 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003427 mtd->oobsize = 436;
3428 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003429 case 5:
3430 mtd->oobsize = 512;
3431 break;
3432 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003433 mtd->oobsize = 640;
3434 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003435 case 7:
3436 default: /* Other cases are "reserved" (unknown) */
3437 mtd->oobsize = 1024;
3438 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003439 }
3440 extid >>= 2;
3441 /* Calc blocksize */
3442 mtd->erasesize = (128 * 1024) <<
3443 (((extid >> 1) & 0x04) | (extid & 0x03));
3444 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003445 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003446 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003447 unsigned int tmp;
3448
3449 /* Calc pagesize */
3450 mtd->writesize = 2048 << (extid & 0x03);
3451 extid >>= 2;
3452 /* Calc oobsize */
3453 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3454 case 0:
3455 mtd->oobsize = 128;
3456 break;
3457 case 1:
3458 mtd->oobsize = 224;
3459 break;
3460 case 2:
3461 mtd->oobsize = 448;
3462 break;
3463 case 3:
3464 mtd->oobsize = 64;
3465 break;
3466 case 4:
3467 mtd->oobsize = 32;
3468 break;
3469 case 5:
3470 mtd->oobsize = 16;
3471 break;
3472 default:
3473 mtd->oobsize = 640;
3474 break;
3475 }
3476 extid >>= 2;
3477 /* Calc blocksize */
3478 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3479 if (tmp < 0x03)
3480 mtd->erasesize = (128 * 1024) << tmp;
3481 else if (tmp == 0x03)
3482 mtd->erasesize = 768 * 1024;
3483 else
3484 mtd->erasesize = (64 * 1024) << tmp;
3485 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003486 } else {
3487 /* Calc pagesize */
3488 mtd->writesize = 1024 << (extid & 0x03);
3489 extid >>= 2;
3490 /* Calc oobsize */
3491 mtd->oobsize = (8 << (extid & 0x01)) *
3492 (mtd->writesize >> 9);
3493 extid >>= 2;
3494 /* Calc blocksize. Blocksize is multiples of 64KiB */
3495 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3496 extid >>= 2;
3497 /* Get buswidth information */
3498 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003499
3500 /*
3501 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3502 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3503 * follows:
3504 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3505 * 110b -> 24nm
3506 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3507 */
3508 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003509 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003510 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3511 !(id_data[4] & 0x80) /* !BENAND */) {
3512 mtd->oobsize = 32 * mtd->writesize >> 9;
3513 }
3514
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003515 }
3516}
3517
3518/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003519 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3520 * decodes a matching ID table entry and assigns the MTD size parameters for
3521 * the chip.
3522 */
3523static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3524 struct nand_flash_dev *type, u8 id_data[8],
3525 int *busw)
3526{
3527 int maf_id = id_data[0];
3528
3529 mtd->erasesize = type->erasesize;
3530 mtd->writesize = type->pagesize;
3531 mtd->oobsize = mtd->writesize / 32;
3532 *busw = type->options & NAND_BUSWIDTH_16;
3533
Huang Shijie1c195e92013-09-25 14:58:12 +08003534 /* All legacy ID NAND are small-page, SLC */
3535 chip->bits_per_cell = 1;
3536
Brian Norrisf23a4812012-09-24 20:40:51 -07003537 /*
3538 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3539 * some Spansion chips have erasesize that conflicts with size
3540 * listed in nand_ids table.
3541 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3542 */
3543 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3544 && id_data[6] == 0x00 && id_data[7] == 0x00
3545 && mtd->writesize == 512) {
3546 mtd->erasesize = 128 * 1024;
3547 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3548 }
3549}
3550
3551/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003552 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3553 * heuristic patterns using various detected parameters (e.g., manufacturer,
3554 * page size, cell-type information).
3555 */
3556static void nand_decode_bbm_options(struct mtd_info *mtd,
3557 struct nand_chip *chip, u8 id_data[8])
3558{
3559 int maf_id = id_data[0];
3560
3561 /* Set the bad block position */
3562 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3563 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3564 else
3565 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3566
3567 /*
3568 * Bad block marker is stored in the last page of each block on Samsung
3569 * and Hynix MLC devices; stored in first two pages of each block on
3570 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3571 * AMD/Spansion, and Macronix. All others scan only the first page.
3572 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003573 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003574 (maf_id == NAND_MFR_SAMSUNG ||
3575 maf_id == NAND_MFR_HYNIX))
3576 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003577 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003578 (maf_id == NAND_MFR_SAMSUNG ||
3579 maf_id == NAND_MFR_HYNIX ||
3580 maf_id == NAND_MFR_TOSHIBA ||
3581 maf_id == NAND_MFR_AMD ||
3582 maf_id == NAND_MFR_MACRONIX)) ||
3583 (mtd->writesize == 2048 &&
3584 maf_id == NAND_MFR_MICRON))
3585 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3586}
3587
Huang Shijieec6e87e2013-03-15 11:01:00 +08003588static inline bool is_full_id_nand(struct nand_flash_dev *type)
3589{
3590 return type->id_len;
3591}
3592
3593static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3594 struct nand_flash_dev *type, u8 *id_data, int *busw)
3595{
3596 if (!strncmp(type->id, id_data, type->id_len)) {
3597 mtd->writesize = type->pagesize;
3598 mtd->erasesize = type->erasesize;
3599 mtd->oobsize = type->oobsize;
3600
Huang Shijie7db906b2013-09-25 14:58:11 +08003601 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003602 chip->chipsize = (uint64_t)type->chipsize << 20;
3603 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003604 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3605 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003606 chip->onfi_timing_mode_default =
3607 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003608
3609 *busw = type->options & NAND_BUSWIDTH_16;
3610
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003611 if (!mtd->name)
3612 mtd->name = type->name;
3613
Huang Shijieec6e87e2013-03-15 11:01:00 +08003614 return true;
3615 }
3616 return false;
3617}
3618
Brian Norris7e74c2d2012-09-24 20:40:49 -07003619/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003620 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003621 */
3622static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003623 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003624 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003625 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003626{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003627 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003628 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003629 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
3631 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003632 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Karl Beldanef89a882008-09-15 14:37:29 +02003634 /*
3635 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003636 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003637 */
3638 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003641 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003644 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003645 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Brian Norris8b6e50c2011-05-25 14:59:01 -07003647 /*
3648 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003649 * interface concerns can cause random data which looks like a
3650 * possibly credible NAND flash to appear. If the two results do
3651 * not match, ignore the device completely.
3652 */
3653
3654 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3655
Brian Norris4aef9b72012-09-24 20:40:48 -07003656 /* Read entire ID string */
3657 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003658 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003659
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003660 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003661 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003662 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003663 return ERR_PTR(-ENODEV);
3664 }
3665
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003666 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003667 type = nand_flash_ids;
3668
Huang Shijieec6e87e2013-03-15 11:01:00 +08003669 for (; type->name != NULL; type++) {
3670 if (is_full_id_nand(type)) {
3671 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3672 goto ident_done;
3673 } else if (*dev_id == type->dev_id) {
3674 break;
3675 }
3676 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003677
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003678 chip->onfi_version = 0;
3679 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003680 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003681 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003682 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003683
3684 /* Check if the chip is JEDEC compliant */
3685 if (nand_flash_detect_jedec(mtd, chip, &busw))
3686 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003687 }
3688
David Woodhouse5e81e882010-02-26 18:32:56 +00003689 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003690 return ERR_PTR(-ENODEV);
3691
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003692 if (!mtd->name)
3693 mtd->name = type->name;
3694
Adrian Hunter69423d92008-12-10 13:37:21 +00003695 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003696
Huang Shijie12a40a52010-09-27 10:43:53 +08003697 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003698 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003699 busw = chip->init_size(mtd, chip, id_data);
3700 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003701 /* Decode parameters from extended ID */
3702 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003703 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003704 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003705 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003706 /* Get chip options */
3707 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003708
Brian Norris8b6e50c2011-05-25 14:59:01 -07003709 /*
3710 * Check if chip is not a Samsung device. Do not clear the
3711 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003712 */
3713 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3714 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3715ident_done:
3716
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003717 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003718 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003719 if (nand_manuf_ids[maf_idx].id == *maf_id)
3720 break;
3721 }
3722
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003723 if (chip->options & NAND_BUSWIDTH_AUTO) {
3724 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3725 chip->options |= busw;
3726 nand_set_defaults(chip, busw);
3727 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3728 /*
3729 * Check, if buswidth is correct. Hardware drivers should set
3730 * chip correct!
3731 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003732 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3733 *maf_id, *dev_id);
3734 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3735 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003736 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3737 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003738 return ERR_PTR(-EINVAL);
3739 }
3740
Brian Norris7e74c2d2012-09-24 20:40:49 -07003741 nand_decode_bbm_options(mtd, chip, id_data);
3742
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003743 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003744 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003745 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003746 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003747
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003748 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003749 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003750 if (chip->chipsize & 0xffffffff)
3751 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003752 else {
3753 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3754 chip->chip_shift += 32 - 1;
3755 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003756
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003757 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003758 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003759
Brian Norris8b6e50c2011-05-25 14:59:01 -07003760 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003761 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3762 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003763
Ezequiel Garcia20171642013-11-25 08:30:31 -03003764 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3765 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003766
3767 if (chip->onfi_version)
3768 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3769 chip->onfi_params.model);
3770 else if (chip->jedec_version)
3771 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3772 chip->jedec_params.model);
3773 else
3774 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3775 type->name);
3776
Rafał Miłecki3755a992014-10-21 00:01:04 +02003777 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003778 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003779 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003780 return type;
3781}
3782
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003783/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003784 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003785 * @mtd: MTD device structure
3786 * @maxchips: number of chips to scan for
3787 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003788 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003789 * This is the first phase of the normal nand_scan() function. It reads the
3790 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003791 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003792 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003793 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003794int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3795 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003796{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003797 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003798 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003799 struct nand_flash_dev *type;
3800
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003801 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003802 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003803
3804 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003805 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3806 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003807
3808 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003809 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003810 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003811 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003812 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
3814
Huang Shijie07300162012-11-09 16:23:45 +08003815 chip->select_chip(mtd, -1);
3816
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003817 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003818 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003819 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003820 /* See comment in nand_get_flash_type for reset */
3821 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003823 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003825 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003826 nand_dev_id != chip->read_byte(mtd)) {
3827 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 break;
Huang Shijie07300162012-11-09 16:23:45 +08003829 }
3830 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 }
3832 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003833 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003836 chip->numchips = i;
3837 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
David Woodhouse3b85c322006-09-25 17:06:53 +01003839 return 0;
3840}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003841EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003842
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03003843/*
3844 * Check if the chip configuration meet the datasheet requirements.
3845
3846 * If our configuration corrects A bits per B bytes and the minimum
3847 * required correction level is X bits per Y bytes, then we must ensure
3848 * both of the following are true:
3849 *
3850 * (1) A / B >= X / Y
3851 * (2) A >= X
3852 *
3853 * Requirement (1) ensures we can correct for the required bitflip density.
3854 * Requirement (2) ensures we can correct even when all bitflips are clumped
3855 * in the same sector.
3856 */
3857static bool nand_ecc_strength_good(struct mtd_info *mtd)
3858{
3859 struct nand_chip *chip = mtd->priv;
3860 struct nand_ecc_ctrl *ecc = &chip->ecc;
3861 int corr, ds_corr;
3862
3863 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3864 /* Not enough information */
3865 return true;
3866
3867 /*
3868 * We get the number of corrected bits per page to compare
3869 * the correction density.
3870 */
3871 corr = (mtd->writesize * ecc->strength) / ecc->size;
3872 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3873
3874 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3875}
David Woodhouse3b85c322006-09-25 17:06:53 +01003876
3877/**
3878 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003879 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003880 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003881 * This is the second phase of the normal nand_scan() function. It fills out
3882 * all the uninitialized function pointers with the defaults and scans for a
3883 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003884 */
3885int nand_scan_tail(struct mtd_info *mtd)
3886{
3887 int i;
3888 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003889 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003890 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003891
Brian Norrise2414f42012-02-06 13:44:00 -08003892 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3893 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3894 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3895
Huang Shijief02ea4e2014-01-13 14:27:12 +08003896 if (!(chip->options & NAND_OWN_BUFFERS)) {
3897 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3898 + mtd->oobsize * 3, GFP_KERNEL);
3899 if (!nbuf)
3900 return -ENOMEM;
3901 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3902 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3903 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3904
3905 chip->buffers = nbuf;
3906 } else {
3907 if (!chip->buffers)
3908 return -ENOMEM;
3909 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003910
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003911 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003912 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003913
3914 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003915 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003916 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003917 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003918 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003920 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 break;
3922 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003923 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 break;
3925 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003926 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003928 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003929 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003932 pr_warn("No oob scheme defined for oobsize %d\n",
3933 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 BUG();
3935 }
3936 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003937
David Woodhouse956e9442006-09-25 17:12:39 +01003938 if (!chip->write_page)
3939 chip->write_page = nand_write_page;
3940
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003941 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003942 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003943 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003944 */
David Woodhouse956e9442006-09-25 17:12:39 +01003945
Huang Shijie97de79e02013-10-18 14:20:53 +08003946 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003947 case NAND_ECC_HW_OOB_FIRST:
3948 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003949 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02003950 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003951 BUG();
3952 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003953 if (!ecc->read_page)
3954 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003955
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003956 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003957 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003958 if (!ecc->read_page)
3959 ecc->read_page = nand_read_page_hwecc;
3960 if (!ecc->write_page)
3961 ecc->write_page = nand_write_page_hwecc;
3962 if (!ecc->read_page_raw)
3963 ecc->read_page_raw = nand_read_page_raw;
3964 if (!ecc->write_page_raw)
3965 ecc->write_page_raw = nand_write_page_raw;
3966 if (!ecc->read_oob)
3967 ecc->read_oob = nand_read_oob_std;
3968 if (!ecc->write_oob)
3969 ecc->write_oob = nand_write_oob_std;
3970 if (!ecc->read_subpage)
3971 ecc->read_subpage = nand_read_subpage;
3972 if (!ecc->write_subpage)
3973 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003974
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003975 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003976 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3977 (!ecc->read_page ||
3978 ecc->read_page == nand_read_page_hwecc ||
3979 !ecc->write_page ||
3980 ecc->write_page == nand_write_page_hwecc)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02003981 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003982 BUG();
3983 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003984 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003985 if (!ecc->read_page)
3986 ecc->read_page = nand_read_page_syndrome;
3987 if (!ecc->write_page)
3988 ecc->write_page = nand_write_page_syndrome;
3989 if (!ecc->read_page_raw)
3990 ecc->read_page_raw = nand_read_page_raw_syndrome;
3991 if (!ecc->write_page_raw)
3992 ecc->write_page_raw = nand_write_page_raw_syndrome;
3993 if (!ecc->read_oob)
3994 ecc->read_oob = nand_read_oob_syndrome;
3995 if (!ecc->write_oob)
3996 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003997
Huang Shijie97de79e02013-10-18 14:20:53 +08003998 if (mtd->writesize >= ecc->size) {
3999 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07004000 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4001 BUG();
4002 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004003 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004004 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004005 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4006 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004007 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004009 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004010 ecc->calculate = nand_calculate_ecc;
4011 ecc->correct = nand_correct_data;
4012 ecc->read_page = nand_read_page_swecc;
4013 ecc->read_subpage = nand_read_subpage;
4014 ecc->write_page = nand_write_page_swecc;
4015 ecc->read_page_raw = nand_read_page_raw;
4016 ecc->write_page_raw = nand_write_page_raw;
4017 ecc->read_oob = nand_read_oob_std;
4018 ecc->write_oob = nand_write_oob_std;
4019 if (!ecc->size)
4020 ecc->size = 256;
4021 ecc->bytes = 3;
4022 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004024
Ivan Djelic193bd402011-03-11 11:05:33 +01004025 case NAND_ECC_SOFT_BCH:
4026 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004027 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004028 BUG();
4029 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004030 ecc->calculate = nand_bch_calculate_ecc;
4031 ecc->correct = nand_bch_correct_data;
4032 ecc->read_page = nand_read_page_swecc;
4033 ecc->read_subpage = nand_read_subpage;
4034 ecc->write_page = nand_write_page_swecc;
4035 ecc->read_page_raw = nand_read_page_raw;
4036 ecc->write_page_raw = nand_write_page_raw;
4037 ecc->read_oob = nand_read_oob_std;
4038 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004039 /*
4040 * Board driver should supply ecc.size and ecc.bytes values to
4041 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07004042 * for details. Otherwise, default to 4 bits for large page
4043 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004044 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004045 if (!ecc->size && (mtd->oobsize >= 64)) {
4046 ecc->size = 512;
Jordan Friendshuh438320d2014-08-27 12:45:36 -05004047 ecc->bytes = DIV_ROUND_UP(13 * ecc->strength, 8);
Ivan Djelic193bd402011-03-11 11:05:33 +01004048 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004049 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4050 &ecc->layout);
4051 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004052 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004053 BUG();
4054 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004055 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01004056 break;
4057
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004058 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004059 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004060 ecc->read_page = nand_read_page_raw;
4061 ecc->write_page = nand_write_page_raw;
4062 ecc->read_oob = nand_read_oob_std;
4063 ecc->read_page_raw = nand_read_page_raw;
4064 ecc->write_page_raw = nand_write_page_raw;
4065 ecc->write_oob = nand_write_oob_std;
4066 ecc->size = mtd->writesize;
4067 ecc->bytes = 0;
4068 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004072 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004073 BUG();
4074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Brian Norris9ce244b2011-08-30 18:45:37 -07004076 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004077 if (!ecc->read_oob_raw)
4078 ecc->read_oob_raw = ecc->read_oob;
4079 if (!ecc->write_oob_raw)
4080 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004081
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004082 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004083 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004084 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004085 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004086 ecc->layout->oobavail = 0;
4087 for (i = 0; ecc->layout->oobfree[i].length
4088 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4089 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4090 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004091
Thomas Petazzoni54c39e92014-07-02 15:16:32 +02004092 /* ECC sanity check: warn if it's too weak */
4093 if (!nand_ecc_strength_good(mtd))
4094 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4095 mtd->name);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004096
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004097 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004098 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004099 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004100 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004101 ecc->steps = mtd->writesize / ecc->size;
4102 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004103 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004104 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004106 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004107
Brian Norris8b6e50c2011-05-25 14:59:01 -07004108 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004109 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004110 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004111 case 2:
4112 mtd->subpage_sft = 1;
4113 break;
4114 case 4:
4115 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004116 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004117 mtd->subpage_sft = 2;
4118 break;
4119 }
4120 }
4121 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4122
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004123 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004124 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004127 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004129 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304130 switch (ecc->mode) {
4131 case NAND_ECC_SOFT:
4132 case NAND_ECC_SOFT_BCH:
4133 if (chip->page_shift > 9)
4134 chip->options |= NAND_SUBPAGE_READ;
4135 break;
4136
4137 default:
4138 break;
4139 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004142 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004143 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4144 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004145 mtd->_erase = nand_erase;
4146 mtd->_point = NULL;
4147 mtd->_unpoint = NULL;
4148 mtd->_read = nand_read;
4149 mtd->_write = nand_write;
4150 mtd->_panic_write = panic_nand_write;
4151 mtd->_read_oob = nand_read_oob;
4152 mtd->_write_oob = nand_write_oob;
4153 mtd->_sync = nand_sync;
4154 mtd->_lock = NULL;
4155 mtd->_unlock = NULL;
4156 mtd->_suspend = nand_suspend;
4157 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004158 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004159 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004160 mtd->_block_isbad = nand_block_isbad;
4161 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004162 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Mike Dunn6a918ba2012-03-11 14:21:11 -07004164 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004165 mtd->ecclayout = ecc->layout;
4166 mtd->ecc_strength = ecc->strength;
4167 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004168 /*
4169 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4170 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4171 * properly set.
4172 */
4173 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004174 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004176 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004177 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004181 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004183EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Brian Norris8b6e50c2011-05-25 14:59:01 -07004185/*
4186 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004187 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004188 * to call us from in-kernel code if the core NAND support is modular.
4189 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004190#ifdef MODULE
4191#define caller_is_module() (1)
4192#else
4193#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004194 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004195#endif
4196
4197/**
4198 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004199 * @mtd: MTD device structure
4200 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004201 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004202 * This fills out all the uninitialized function pointers with the defaults.
4203 * The flash ID is read and the mtd/chip structures are filled with the
4204 * appropriate values. The mtd->owner field must be set to the module of the
4205 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004206 */
4207int nand_scan(struct mtd_info *mtd, int maxchips)
4208{
4209 int ret;
4210
4211 /* Many callers got this wrong, so check for it for a while... */
4212 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004213 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004214 BUG();
4215 }
4216
David Woodhouse5e81e882010-02-26 18:32:56 +00004217 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004218 if (!ret)
4219 ret = nand_scan_tail(mtd);
4220 return ret;
4221}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004222EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004223
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004225 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004226 * @mtd: MTD device structure
4227 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004228void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004230 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Ivan Djelic193bd402011-03-11 11:05:33 +01004232 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4233 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4234
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004235 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Jesper Juhlfa671642005-11-07 01:01:27 -08004237 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004238 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004239 if (!(chip->options & NAND_OWN_BUFFERS))
4240 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004241
4242 /* Free bad block descriptor memory */
4243 if (chip->badblock_pattern && chip->badblock_pattern->options
4244 & NAND_BBT_DYNAMICSTRUCT)
4245 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
David Woodhousee0c7d762006-05-13 18:07:53 +01004247EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004248
4249static int __init nand_base_init(void)
4250{
4251 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4252 return 0;
4253}
4254
4255static void __exit nand_base_exit(void)
4256{
4257 led_trigger_unregister_simple(nand_led_trigger);
4258}
4259
4260module_init(nand_base_init);
4261module_exit(nand_base_exit);
4262
David Woodhousee0c7d762006-05-13 18:07:53 +01004263MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004264MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4265MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004266MODULE_DESCRIPTION("Generic NAND flash driver code");