blob: 0afe76315c384cc8b5993a35d3f70e148e780de3 [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
Brian Norris0ec56dc2015-02-28 02:02:30 -0800389 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700390 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/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200569 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
570 * @mtd: MTD device structure
571 * @timeo: Timeout in ms
572 *
573 * Wait for status ready (i.e. command done) or timeout.
574 */
575static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
576{
577 register struct nand_chip *chip = mtd->priv;
578
579 timeo = jiffies + msecs_to_jiffies(timeo);
580 do {
581 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
582 break;
583 touch_softlockup_watchdog();
584 } while (time_before(jiffies, timeo));
585};
586
587/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700589 * @mtd: MTD device structure
590 * @command: the command to be sent
591 * @column: the column address for this command, -1 if none
592 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700594 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200595 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200597static void nand_command(struct mtd_info *mtd, unsigned int command,
598 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200600 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200601 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Brian Norris8b6e50c2011-05-25 14:59:01 -0700603 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (command == NAND_CMD_SEQIN) {
605 int readcmd;
606
Joern Engel28318772006-05-22 23:18:05 +0200607 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200609 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 readcmd = NAND_CMD_READOOB;
611 } else if (column < 256) {
612 /* First 256 bytes --> READ0 */
613 readcmd = NAND_CMD_READ0;
614 } else {
615 column -= 256;
616 readcmd = NAND_CMD_READ1;
617 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200618 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200621 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Brian Norris8b6e50c2011-05-25 14:59:01 -0700623 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
625 /* Serially input address */
626 if (column != -1) {
627 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800628 if (chip->options & NAND_BUSWIDTH_16 &&
629 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200630 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200631 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200632 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200634 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200635 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200636 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200637 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200638 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 if (chip->chipsize > (32 << 20))
640 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200641 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000643
644 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700645 * Program and erase have their own busy handlers status and sequential
646 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 case NAND_CMD_PAGEPROG:
651 case NAND_CMD_ERASE1:
652 case NAND_CMD_ERASE2:
653 case NAND_CMD_SEQIN:
654 case NAND_CMD_STATUS:
655 return;
656
657 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200658 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200660 udelay(chip->chip_delay);
661 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200662 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200663 chip->cmd_ctrl(mtd,
664 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200665 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
666 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return;
668
David Woodhousee0c7d762006-05-13 18:07:53 +0100669 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000671 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * If we don't have access to the busy pin, we apply the given
673 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100674 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200675 if (!chip->dev_ready) {
676 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700680 /*
681 * Apply this short delay always to ensure that we do wait tWB in
682 * any case on any machine.
683 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100684 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000685
686 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/**
690 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700691 * @mtd: MTD device structure
692 * @command: the command to be sent
693 * @column: the column address for this command, -1 if none
694 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200696 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700697 * devices. We don't have the separate regions as we have in the small page
698 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200700static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
701 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200703 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* Emulate NAND_CMD_READOOB */
706 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200707 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 command = NAND_CMD_READ0;
709 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000710
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400712 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200715 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* Serially input address */
718 if (column != -1) {
719 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800720 if (chip->options & NAND_BUSWIDTH_16 &&
721 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200723 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200724 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200725 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200728 chip->cmd_ctrl(mtd, page_addr, ctrl);
729 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200730 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200732 if (chip->chipsize > (128 << 20))
733 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200734 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200737 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000738
739 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700740 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100741 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 case NAND_CMD_CACHEDPROG:
746 case NAND_CMD_PAGEPROG:
747 case NAND_CMD_ERASE1:
748 case NAND_CMD_ERASE2:
749 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200750 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000752 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200757 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200758 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
759 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
760 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
761 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200762 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
763 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return;
765
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200766 case NAND_CMD_RNDOUT:
767 /* No ready / busy check necessary */
768 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
769 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
770 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
771 NAND_NCE | NAND_CTRL_CHANGE);
772 return;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200775 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
776 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
777 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
778 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000779
David Woodhousee0c7d762006-05-13 18:07:53 +0100780 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000782 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100785 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200786 if (!chip->dev_ready) {
787 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000791
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 /*
793 * Apply this short delay always to ensure that we do wait tWB in
794 * any case on any machine.
795 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100796 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000797
798 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200802 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700803 * @chip: the nand chip descriptor
804 * @mtd: MTD device structure
805 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200806 *
807 * Used when in panic, no locks are taken.
808 */
809static void panic_nand_get_device(struct nand_chip *chip,
810 struct mtd_info *mtd, int new_state)
811{
Brian Norris7854d3f2011-06-23 14:12:08 -0700812 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200813 chip->controller->active = chip;
814 chip->state = new_state;
815}
816
817/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700819 * @mtd: MTD device structure
820 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 *
822 * Get the device and lock it for exclusive access
823 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200824static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800825nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800827 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200828 spinlock_t *lock = &chip->controller->lock;
829 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100830 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200831retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100832 spin_lock(lock);
833
vimal singhb8b3ee92009-07-09 20:41:22 +0530834 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200835 if (!chip->controller->active)
836 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200837
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200838 if (chip->controller->active == chip && chip->state == FL_READY) {
839 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100840 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100841 return 0;
842 }
843 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800844 if (chip->controller->active->state == FL_PM_SUSPENDED) {
845 chip->state = FL_PM_SUSPENDED;
846 spin_unlock(lock);
847 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800848 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100849 }
850 set_current_state(TASK_UNINTERRUPTIBLE);
851 add_wait_queue(wq, &wait);
852 spin_unlock(lock);
853 schedule();
854 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 goto retry;
856}
857
858/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700859 * panic_nand_wait - [GENERIC] wait until the command is done
860 * @mtd: MTD device structure
861 * @chip: NAND chip structure
862 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200863 *
864 * Wait for command done. This is a helper function for nand_wait used when
865 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400866 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200867 */
868static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
869 unsigned long timeo)
870{
871 int i;
872 for (i = 0; i < timeo; i++) {
873 if (chip->dev_ready) {
874 if (chip->dev_ready(mtd))
875 break;
876 } else {
877 if (chip->read_byte(mtd) & NAND_STATUS_READY)
878 break;
879 }
880 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200881 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200882}
883
884/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700885 * nand_wait - [DEFAULT] wait until the command is done
886 * @mtd: MTD device structure
887 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700889 * Wait for command done. This applies to erase and program only. Erase can
890 * take up to 400ms and program up to 20ms according to general NAND and
891 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700892 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200893static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200896 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800897 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Richard Purdie8fe833c2006-03-31 02:31:14 -0800899 led_trigger_event(nand_led_trigger, LED_FULL);
900
Brian Norris8b6e50c2011-05-25 14:59:01 -0700901 /*
902 * Apply this short delay always to ensure that we do wait tWB in any
903 * case on any machine.
904 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100905 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200907 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200909 if (in_interrupt() || oops_in_progress)
910 panic_nand_wait(mtd, chip, timeo);
911 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800912 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200913 while (time_before(jiffies, timeo)) {
914 if (chip->dev_ready) {
915 if (chip->dev_ready(mtd))
916 break;
917 } else {
918 if (chip->read_byte(mtd) & NAND_STATUS_READY)
919 break;
920 }
921 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800924 led_trigger_event(nand_led_trigger, LED_OFF);
925
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200926 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100927 /* This can happen if in case of timeout or buggy dev_ready */
928 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return status;
930}
931
932/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700933 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700934 * @mtd: mtd info
935 * @ofs: offset to start unlock from
936 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700937 * @invert: when = 0, unlock the range of blocks within the lower and
938 * upper boundary address
939 * when = 1, unlock the range of blocks outside the boundaries
940 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530941 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700942 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530943 */
944static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
945 uint64_t len, int invert)
946{
947 int ret = 0;
948 int status, page;
949 struct nand_chip *chip = mtd->priv;
950
951 /* Submit address of first page to unlock */
952 page = ofs >> chip->page_shift;
953 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
954
955 /* Submit address of last page to unlock */
956 page = (ofs + len) >> chip->page_shift;
957 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
958 (page | invert) & chip->pagemask);
959
960 /* Call wait ready function */
961 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530962 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400963 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700964 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530965 __func__, status);
966 ret = -EIO;
967 }
968
969 return ret;
970}
971
972/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700973 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700974 * @mtd: mtd info
975 * @ofs: offset to start unlock from
976 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530977 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700978 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530979 */
980int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
981{
982 int ret = 0;
983 int chipnr;
984 struct nand_chip *chip = mtd->priv;
985
Brian Norris289c0522011-07-19 10:06:09 -0700986 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530987 __func__, (unsigned long long)ofs, len);
988
989 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -0800990 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +0530991
992 /* Align to last block address if size addresses end of the device */
993 if (ofs + len == mtd->size)
994 len -= mtd->erasesize;
995
Huang Shijie6a8214a2012-11-19 14:43:30 +0800996 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530997
998 /* Shift to get chip number */
999 chipnr = ofs >> chip->chip_shift;
1000
1001 chip->select_chip(mtd, chipnr);
1002
White Ding57d3a9a2014-07-24 00:10:45 +08001003 /*
1004 * Reset the chip.
1005 * If we want to check the WP through READ STATUS and check the bit 7
1006 * we must reset the chip
1007 * some operation can also clear the bit 7 of status register
1008 * eg. erase/program a locked block
1009 */
1010 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1011
Vimal Singh7d70f332010-02-08 15:50:49 +05301012 /* Check, if it is write protected */
1013 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001014 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301015 __func__);
1016 ret = -EIO;
1017 goto out;
1018 }
1019
1020 ret = __nand_unlock(mtd, ofs, len, 0);
1021
1022out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001023 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301024 nand_release_device(mtd);
1025
1026 return ret;
1027}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001028EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301029
1030/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001031 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001032 * @mtd: mtd info
1033 * @ofs: offset to start unlock from
1034 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301035 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001036 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1037 * have this feature, but it allows only to lock all blocks, not for specified
1038 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1039 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301040 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001041 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301042 */
1043int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1044{
1045 int ret = 0;
1046 int chipnr, status, page;
1047 struct nand_chip *chip = mtd->priv;
1048
Brian Norris289c0522011-07-19 10:06:09 -07001049 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301050 __func__, (unsigned long long)ofs, len);
1051
1052 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001053 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301054
Huang Shijie6a8214a2012-11-19 14:43:30 +08001055 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301056
1057 /* Shift to get chip number */
1058 chipnr = ofs >> chip->chip_shift;
1059
1060 chip->select_chip(mtd, chipnr);
1061
White Ding57d3a9a2014-07-24 00:10:45 +08001062 /*
1063 * Reset the chip.
1064 * If we want to check the WP through READ STATUS and check the bit 7
1065 * we must reset the chip
1066 * some operation can also clear the bit 7 of status register
1067 * eg. erase/program a locked block
1068 */
1069 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1070
Vimal Singh7d70f332010-02-08 15:50:49 +05301071 /* Check, if it is write protected */
1072 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001073 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301074 __func__);
1075 status = MTD_ERASE_FAILED;
1076 ret = -EIO;
1077 goto out;
1078 }
1079
1080 /* Submit address of first page to lock */
1081 page = ofs >> chip->page_shift;
1082 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1083
1084 /* Call wait ready function */
1085 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301086 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001087 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001088 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301089 __func__, status);
1090 ret = -EIO;
1091 goto out;
1092 }
1093
1094 ret = __nand_unlock(mtd, ofs, len, 0x1);
1095
1096out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001097 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301098 nand_release_device(mtd);
1099
1100 return ret;
1101}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001102EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301103
1104/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001105 * nand_read_page_raw - [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 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001112 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001113 */
1114static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001115 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001116{
1117 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001118 if (oob_required)
1119 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001120 return 0;
1121}
1122
1123/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001124 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001125 * @mtd: mtd info structure
1126 * @chip: nand chip info structure
1127 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001128 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001129 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001130 *
1131 * We need a special oob layout and handling even when OOB isn't used.
1132 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001133static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001134 struct nand_chip *chip, uint8_t *buf,
1135 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001136{
1137 int eccsize = chip->ecc.size;
1138 int eccbytes = chip->ecc.bytes;
1139 uint8_t *oob = chip->oob_poi;
1140 int steps, size;
1141
1142 for (steps = chip->ecc.steps; steps > 0; steps--) {
1143 chip->read_buf(mtd, buf, eccsize);
1144 buf += eccsize;
1145
1146 if (chip->ecc.prepad) {
1147 chip->read_buf(mtd, oob, chip->ecc.prepad);
1148 oob += chip->ecc.prepad;
1149 }
1150
1151 chip->read_buf(mtd, oob, eccbytes);
1152 oob += eccbytes;
1153
1154 if (chip->ecc.postpad) {
1155 chip->read_buf(mtd, oob, chip->ecc.postpad);
1156 oob += chip->ecc.postpad;
1157 }
1158 }
1159
1160 size = mtd->oobsize - (oob - chip->oob_poi);
1161 if (size)
1162 chip->read_buf(mtd, oob, size);
1163
1164 return 0;
1165}
1166
1167/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001168 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001169 * @mtd: mtd info structure
1170 * @chip: nand chip info structure
1171 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001172 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001173 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001174 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001175static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001176 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001178 int i, eccsize = chip->ecc.size;
1179 int eccbytes = chip->ecc.bytes;
1180 int eccsteps = chip->ecc.steps;
1181 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001182 uint8_t *ecc_calc = chip->buffers->ecccalc;
1183 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001184 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001185 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001186
Brian Norris1fbb9382012-05-02 10:14:55 -07001187 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001188
1189 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1190 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1191
1192 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001193 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001194
1195 eccsteps = chip->ecc.steps;
1196 p = buf;
1197
1198 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1199 int stat;
1200
1201 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001202 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001203 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001204 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001205 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001206 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1207 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001208 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001209 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301213 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001214 * @mtd: mtd info structure
1215 * @chip: nand chip info structure
1216 * @data_offs: offset of requested data within the page
1217 * @readlen: data length
1218 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001219 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001220 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001221static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001222 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1223 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001224{
1225 int start_step, end_step, num_steps;
1226 uint32_t *eccpos = chip->ecc.layout->eccpos;
1227 uint8_t *p;
1228 int data_col_addr, i, gaps = 0;
1229 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1230 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163ca2014-03-16 04:01:07 +10301231 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001232 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001233
Brian Norris7854d3f2011-06-23 14:12:08 -07001234 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001235 start_step = data_offs / chip->ecc.size;
1236 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1237 num_steps = end_step - start_step + 1;
Ron4a4163ca2014-03-16 04:01:07 +10301238 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001239
Brian Norris8b6e50c2011-05-25 14:59:01 -07001240 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001241 datafrag_len = num_steps * chip->ecc.size;
1242 eccfrag_len = num_steps * chip->ecc.bytes;
1243
1244 data_col_addr = start_step * chip->ecc.size;
1245 /* If we read not a page aligned data */
1246 if (data_col_addr != 0)
1247 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1248
1249 p = bufpoi + data_col_addr;
1250 chip->read_buf(mtd, p, datafrag_len);
1251
Brian Norris8b6e50c2011-05-25 14:59:01 -07001252 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001253 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1254 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1255
Brian Norris8b6e50c2011-05-25 14:59:01 -07001256 /*
1257 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001258 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001259 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001260 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301261 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001262 gaps = 1;
1263 break;
1264 }
1265 }
1266 if (gaps) {
1267 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1268 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1269 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001270 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001271 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001272 * about buswidth alignment in read_buf.
1273 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001274 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001275 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001276 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001277 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001278 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001279 aligned_len++;
1280
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001281 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1282 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001283 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1284 }
1285
1286 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001287 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001288
1289 p = bufpoi + data_col_addr;
1290 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1291 int stat;
1292
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001293 stat = chip->ecc.correct(mtd, p,
1294 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001295 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001296 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001297 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001298 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001299 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1300 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001301 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001302 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001303}
1304
1305/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001306 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001307 * @mtd: mtd info structure
1308 * @chip: nand chip info structure
1309 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001310 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001311 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001312 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001313 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001314 */
1315static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001316 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001317{
1318 int i, eccsize = chip->ecc.size;
1319 int eccbytes = chip->ecc.bytes;
1320 int eccsteps = chip->ecc.steps;
1321 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001322 uint8_t *ecc_calc = chip->buffers->ecccalc;
1323 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001324 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001325 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001326
1327 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1328 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1329 chip->read_buf(mtd, p, eccsize);
1330 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1331 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001332 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001333
1334 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001335 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001336
1337 eccsteps = chip->ecc.steps;
1338 p = buf;
1339
1340 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1341 int stat;
1342
1343 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001344 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001345 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001346 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001347 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001348 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1349 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001350 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001351 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001352}
1353
1354/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001355 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001356 * @mtd: mtd info structure
1357 * @chip: nand chip info structure
1358 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001359 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001360 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001361 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001362 * Hardware ECC for large page chips, require OOB to be read first. For this
1363 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1364 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1365 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1366 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001367 */
1368static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001369 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001370{
1371 int i, eccsize = chip->ecc.size;
1372 int eccbytes = chip->ecc.bytes;
1373 int eccsteps = chip->ecc.steps;
1374 uint8_t *p = buf;
1375 uint8_t *ecc_code = chip->buffers->ecccode;
1376 uint32_t *eccpos = chip->ecc.layout->eccpos;
1377 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001378 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001379
1380 /* Read the OOB area first */
1381 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1382 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1383 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1384
1385 for (i = 0; i < chip->ecc.total; i++)
1386 ecc_code[i] = chip->oob_poi[eccpos[i]];
1387
1388 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1389 int stat;
1390
1391 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1392 chip->read_buf(mtd, p, eccsize);
1393 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1394
1395 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001396 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001397 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001398 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001399 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001400 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1401 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001402 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001403 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001404}
1405
1406/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001407 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001408 * @mtd: mtd info structure
1409 * @chip: nand chip info structure
1410 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001411 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001412 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001413 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001414 * The hw generator calculates the error syndrome automatically. Therefore we
1415 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001416 */
1417static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001418 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001419{
1420 int i, eccsize = chip->ecc.size;
1421 int eccbytes = chip->ecc.bytes;
1422 int eccsteps = chip->ecc.steps;
1423 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001424 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001425 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001426
1427 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1428 int stat;
1429
1430 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1431 chip->read_buf(mtd, p, eccsize);
1432
1433 if (chip->ecc.prepad) {
1434 chip->read_buf(mtd, oob, chip->ecc.prepad);
1435 oob += chip->ecc.prepad;
1436 }
1437
1438 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1439 chip->read_buf(mtd, oob, eccbytes);
1440 stat = chip->ecc.correct(mtd, p, oob, NULL);
1441
Mike Dunn3f91e942012-04-25 12:06:09 -07001442 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001443 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001444 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001445 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001446 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1447 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001448
1449 oob += eccbytes;
1450
1451 if (chip->ecc.postpad) {
1452 chip->read_buf(mtd, oob, chip->ecc.postpad);
1453 oob += chip->ecc.postpad;
1454 }
1455 }
1456
1457 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001458 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001459 if (i)
1460 chip->read_buf(mtd, oob, i);
1461
Mike Dunn3f91e942012-04-25 12:06:09 -07001462 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001463}
1464
1465/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001466 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001467 * @chip: nand chip structure
1468 * @oob: oob destination address
1469 * @ops: oob ops structure
1470 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001471 */
1472static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001473 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001474{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001475 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001476
Brian Norris0612b9d2011-08-30 18:45:40 -07001477 case MTD_OPS_PLACE_OOB:
1478 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001479 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1480 return oob + len;
1481
Brian Norris0612b9d2011-08-30 18:45:40 -07001482 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001483 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001484 uint32_t boffs = 0, roffs = ops->ooboffs;
1485 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001486
Florian Fainellif8ac0412010-09-07 13:23:43 +02001487 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001488 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001489 if (unlikely(roffs)) {
1490 if (roffs >= free->length) {
1491 roffs -= free->length;
1492 continue;
1493 }
1494 boffs = free->offset + roffs;
1495 bytes = min_t(size_t, len,
1496 (free->length - roffs));
1497 roffs = 0;
1498 } else {
1499 bytes = min_t(size_t, len, free->length);
1500 boffs = free->offset;
1501 }
1502 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001503 oob += bytes;
1504 }
1505 return oob;
1506 }
1507 default:
1508 BUG();
1509 }
1510 return NULL;
1511}
1512
1513/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001514 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1515 * @mtd: MTD device structure
1516 * @retry_mode: the retry mode to use
1517 *
1518 * Some vendors supply a special command to shift the Vt threshold, to be used
1519 * when there are too many bitflips in a page (i.e., ECC error). After setting
1520 * a new threshold, the host should retry reading the page.
1521 */
1522static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1523{
1524 struct nand_chip *chip = mtd->priv;
1525
1526 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1527
1528 if (retry_mode >= chip->read_retries)
1529 return -EINVAL;
1530
1531 if (!chip->setup_read_retry)
1532 return -EOPNOTSUPP;
1533
1534 return chip->setup_read_retry(mtd, retry_mode);
1535}
1536
1537/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001538 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001539 * @mtd: MTD device structure
1540 * @from: offset to read from
1541 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001542 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001543 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001544 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001545static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1546 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001547{
Brian Norrise47f3db2012-05-02 10:14:56 -07001548 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001549 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001550 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001551 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001552 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001553 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001554 mtd->oobavail : mtd->oobsize;
1555
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001556 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001557 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001558 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001559 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001560 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001562 chipnr = (int)(from >> chip->chip_shift);
1563 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001565 realpage = (int)(from >> chip->page_shift);
1566 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001568 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001570 buf = ops->datbuf;
1571 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001572 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001573
Florian Fainellif8ac0412010-09-07 13:23:43 +02001574 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001575 unsigned int ecc_failures = mtd->ecc_stats.failed;
1576
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001577 bytes = min(mtd->writesize - col, readlen);
1578 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001579
Kamal Dasu66507c72014-05-01 20:51:19 -04001580 if (!aligned)
1581 use_bufpoi = 1;
1582 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1583 use_bufpoi = !virt_addr_valid(buf);
1584 else
1585 use_bufpoi = 0;
1586
Brian Norris8b6e50c2011-05-25 14:59:01 -07001587 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001588 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001589 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1590
1591 if (use_bufpoi && aligned)
1592 pr_debug("%s: using read bounce buffer for buf@%p\n",
1593 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Brian Norrisba84fb52014-01-03 15:13:33 -08001595read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001596 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Mike Dunnedbc45402012-04-25 12:06:11 -07001598 /*
1599 * Now read the page into the buffer. Absent an error,
1600 * the read methods return max bitflips per ecc step.
1601 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001602 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001603 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001604 oob_required,
1605 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001606 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1607 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001608 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001609 col, bytes, bufpoi,
1610 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001611 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001612 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001613 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001614 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001615 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001616 /* Invalidate page cache */
1617 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001618 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001619 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001620
Mike Dunnedbc45402012-04-25 12:06:11 -07001621 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1622
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001623 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001624 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001625 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001626 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001627 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001628 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001629 chip->pagebuf_bitflips = ret;
1630 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001631 /* Invalidate page cache */
1632 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001633 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001634 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001636
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001637 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001638 int toread = min(oobreadlen, max_oobsize);
1639
1640 if (toread) {
1641 oob = nand_transfer_oob(chip,
1642 oob, ops, toread);
1643 oobreadlen -= toread;
1644 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001645 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001646
1647 if (chip->options & NAND_NEED_READRDY) {
1648 /* Apply delay or wait for ready/busy pin */
1649 if (!chip->dev_ready)
1650 udelay(chip->chip_delay);
1651 else
1652 nand_wait_ready(mtd);
1653 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001654
Brian Norrisba84fb52014-01-03 15:13:33 -08001655 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001656 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001657 retry_mode++;
1658 ret = nand_setup_read_retry(mtd,
1659 retry_mode);
1660 if (ret < 0)
1661 break;
1662
1663 /* Reset failures; retry */
1664 mtd->ecc_stats.failed = ecc_failures;
1665 goto read_retry;
1666 } else {
1667 /* No more retry modes; real failure */
1668 ecc_fail = true;
1669 }
1670 }
1671
1672 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001673 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001674 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001675 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001676 max_bitflips = max_t(unsigned int, max_bitflips,
1677 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001680 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001681
Brian Norrisba84fb52014-01-03 15:13:33 -08001682 /* Reset to retry mode 0 */
1683 if (retry_mode) {
1684 ret = nand_setup_read_retry(mtd, 0);
1685 if (ret < 0)
1686 break;
1687 retry_mode = 0;
1688 }
1689
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001690 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001691 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Brian Norris8b6e50c2011-05-25 14:59:01 -07001693 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 col = 0;
1695 /* Increment page address */
1696 realpage++;
1697
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001698 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* Check, if we cross a chip boundary */
1700 if (!page) {
1701 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001702 chip->select_chip(mtd, -1);
1703 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001706 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001708 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001709 if (oob)
1710 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Mike Dunn3f91e942012-04-25 12:06:09 -07001712 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001713 return ret;
1714
Brian Norrisb72f3df2013-12-03 11:04:14 -08001715 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001716 return -EBADMSG;
1717
Mike Dunnedbc45402012-04-25 12:06:11 -07001718 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001719}
1720
1721/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001722 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001723 * @mtd: MTD device structure
1724 * @from: offset to read from
1725 * @len: number of bytes to read
1726 * @retlen: pointer to variable to store the number of read bytes
1727 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001728 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001729 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001730 */
1731static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1732 size_t *retlen, uint8_t *buf)
1733{
Brian Norris4a89ff82011-08-30 18:45:45 -07001734 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001735 int ret;
1736
Huang Shijie6a8214a2012-11-19 14:43:30 +08001737 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001738 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001739 ops.len = len;
1740 ops.datbuf = buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08001741 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001742 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001743 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001744 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001745 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747
1748/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001749 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001750 * @mtd: mtd info structure
1751 * @chip: nand chip info structure
1752 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001753 */
1754static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001755 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001756{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001757 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001758 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001759 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001760}
1761
1762/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001763 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001764 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001765 * @mtd: mtd info structure
1766 * @chip: nand chip info structure
1767 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001768 */
1769static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001770 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001771{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001772 int length = mtd->oobsize;
1773 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1774 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001775 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001776 int i, toread, sndrnd = 0, pos;
1777
1778 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1779 for (i = 0; i < chip->ecc.steps; i++) {
1780 if (sndrnd) {
1781 pos = eccsize + i * (eccsize + chunk);
1782 if (mtd->writesize > 512)
1783 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1784 else
1785 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1786 } else
1787 sndrnd = 1;
1788 toread = min_t(int, length, chunk);
1789 chip->read_buf(mtd, bufpoi, toread);
1790 bufpoi += toread;
1791 length -= toread;
1792 }
1793 if (length > 0)
1794 chip->read_buf(mtd, bufpoi, length);
1795
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001796 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001797}
1798
1799/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001800 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001801 * @mtd: mtd info structure
1802 * @chip: nand chip info structure
1803 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001804 */
1805static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1806 int page)
1807{
1808 int status = 0;
1809 const uint8_t *buf = chip->oob_poi;
1810 int length = mtd->oobsize;
1811
1812 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1813 chip->write_buf(mtd, buf, length);
1814 /* Send command to program the OOB data */
1815 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1816
1817 status = chip->waitfunc(mtd, chip);
1818
Savin Zlobec0d420f92006-06-21 11:51:20 +02001819 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001820}
1821
1822/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001823 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001824 * with syndrome - only for large page flash
1825 * @mtd: mtd info structure
1826 * @chip: nand chip info structure
1827 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001828 */
1829static int nand_write_oob_syndrome(struct mtd_info *mtd,
1830 struct nand_chip *chip, int page)
1831{
1832 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1833 int eccsize = chip->ecc.size, length = mtd->oobsize;
1834 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1835 const uint8_t *bufpoi = chip->oob_poi;
1836
1837 /*
1838 * data-ecc-data-ecc ... ecc-oob
1839 * or
1840 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1841 */
1842 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1843 pos = steps * (eccsize + chunk);
1844 steps = 0;
1845 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001846 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001847
1848 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1849 for (i = 0; i < steps; i++) {
1850 if (sndcmd) {
1851 if (mtd->writesize <= 512) {
1852 uint32_t fill = 0xFFFFFFFF;
1853
1854 len = eccsize;
1855 while (len > 0) {
1856 int num = min_t(int, len, 4);
1857 chip->write_buf(mtd, (uint8_t *)&fill,
1858 num);
1859 len -= num;
1860 }
1861 } else {
1862 pos = eccsize + i * (eccsize + chunk);
1863 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1864 }
1865 } else
1866 sndcmd = 1;
1867 len = min_t(int, length, chunk);
1868 chip->write_buf(mtd, bufpoi, len);
1869 bufpoi += len;
1870 length -= len;
1871 }
1872 if (length > 0)
1873 chip->write_buf(mtd, bufpoi, length);
1874
1875 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1876 status = chip->waitfunc(mtd, chip);
1877
1878 return status & NAND_STATUS_FAIL ? -EIO : 0;
1879}
1880
1881/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001882 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001883 * @mtd: MTD device structure
1884 * @from: offset to read from
1885 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001887 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001889static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1890 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Brian Norrisc00a0992012-05-01 17:12:54 -07001892 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001893 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001894 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001895 int readlen = ops->ooblen;
1896 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001897 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001898 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Brian Norris289c0522011-07-19 10:06:09 -07001900 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301901 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Brian Norris041e4572011-06-23 16:45:24 -07001903 stats = mtd->ecc_stats;
1904
Brian Norris0612b9d2011-08-30 18:45:40 -07001905 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001906 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001907 else
1908 len = mtd->oobsize;
1909
1910 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001911 pr_debug("%s: attempt to start read outside oob\n",
1912 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001913 return -EINVAL;
1914 }
1915
1916 /* Do not allow reads past end of device */
1917 if (unlikely(from >= mtd->size ||
1918 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1919 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001920 pr_debug("%s: attempt to read beyond end of device\n",
1921 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001922 return -EINVAL;
1923 }
Vitaly Wool70145682006-11-03 18:20:38 +03001924
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001925 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001926 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001928 /* Shift to get page */
1929 realpage = (int)(from >> chip->page_shift);
1930 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Florian Fainellif8ac0412010-09-07 13:23:43 +02001932 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001933 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001934 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001935 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001936 ret = chip->ecc.read_oob(mtd, chip, page);
1937
1938 if (ret < 0)
1939 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001940
1941 len = min(len, readlen);
1942 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001943
Brian Norris5bc7c332013-03-13 09:51:31 -07001944 if (chip->options & NAND_NEED_READRDY) {
1945 /* Apply delay or wait for ready/busy pin */
1946 if (!chip->dev_ready)
1947 udelay(chip->chip_delay);
1948 else
1949 nand_wait_ready(mtd);
1950 }
1951
Vitaly Wool70145682006-11-03 18:20:38 +03001952 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001953 if (!readlen)
1954 break;
1955
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001956 /* Increment page address */
1957 realpage++;
1958
1959 page = realpage & chip->pagemask;
1960 /* Check, if we cross a chip boundary */
1961 if (!page) {
1962 chipnr++;
1963 chip->select_chip(mtd, -1);
1964 chip->select_chip(mtd, chipnr);
1965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001967 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001969 ops->oobretlen = ops->ooblen - readlen;
1970
1971 if (ret < 0)
1972 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001973
1974 if (mtd->ecc_stats.failed - stats.failed)
1975 return -EBADMSG;
1976
1977 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
1980/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001981 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001982 * @mtd: MTD device structure
1983 * @from: offset to read from
1984 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001986 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001988static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1989 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001991 int ret = -ENOTSUPP;
1992
1993 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001996 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001997 pr_debug("%s: attempt to read beyond end of device\n",
1998 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return -EINVAL;
2000 }
2001
Huang Shijie6a8214a2012-11-19 14:43:30 +08002002 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Florian Fainellif8ac0412010-09-07 13:23:43 +02002004 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002005 case MTD_OPS_PLACE_OOB:
2006 case MTD_OPS_AUTO_OOB:
2007 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002008 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002009
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002010 default:
2011 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002014 if (!ops->datbuf)
2015 ret = nand_do_read_oob(mtd, from, ops);
2016 else
2017 ret = nand_do_read_ops(mtd, from, ops);
2018
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002021 return ret;
2022}
2023
2024
2025/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002026 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002027 * @mtd: mtd info structure
2028 * @chip: nand chip info structure
2029 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002030 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002031 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002032 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002033 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002034static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002035 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002036{
2037 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002038 if (oob_required)
2039 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002040
2041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002044/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002045 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002046 * @mtd: mtd info structure
2047 * @chip: nand chip info structure
2048 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002049 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08002050 *
2051 * We need a special oob layout and handling even when ECC isn't checked.
2052 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002053static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002054 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002055 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08002056{
2057 int eccsize = chip->ecc.size;
2058 int eccbytes = chip->ecc.bytes;
2059 uint8_t *oob = chip->oob_poi;
2060 int steps, size;
2061
2062 for (steps = chip->ecc.steps; steps > 0; steps--) {
2063 chip->write_buf(mtd, buf, eccsize);
2064 buf += eccsize;
2065
2066 if (chip->ecc.prepad) {
2067 chip->write_buf(mtd, oob, chip->ecc.prepad);
2068 oob += chip->ecc.prepad;
2069 }
2070
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002071 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002072 oob += eccbytes;
2073
2074 if (chip->ecc.postpad) {
2075 chip->write_buf(mtd, oob, chip->ecc.postpad);
2076 oob += chip->ecc.postpad;
2077 }
2078 }
2079
2080 size = mtd->oobsize - (oob - chip->oob_poi);
2081 if (size)
2082 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002083
2084 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002085}
2086/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002087 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002088 * @mtd: mtd info structure
2089 * @chip: nand chip info structure
2090 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002091 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002092 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002093static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002094 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002095{
2096 int i, eccsize = chip->ecc.size;
2097 int eccbytes = chip->ecc.bytes;
2098 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002099 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002100 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002101 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002102
Brian Norris7854d3f2011-06-23 14:12:08 -07002103 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002104 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2105 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002107 for (i = 0; i < chip->ecc.total; i++)
2108 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002109
Josh Wufdbad98d2012-06-25 18:07:45 +08002110 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002111}
2112
2113/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002114 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002115 * @mtd: mtd info structure
2116 * @chip: nand chip info structure
2117 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002118 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002119 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002120static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002121 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002122{
2123 int i, eccsize = chip->ecc.size;
2124 int eccbytes = chip->ecc.bytes;
2125 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002126 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002127 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002128 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002129
2130 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2131 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002132 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002133 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2134 }
2135
2136 for (i = 0; i < chip->ecc.total; i++)
2137 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2138
2139 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002140
2141 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002142}
2143
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302144
2145/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002146 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302147 * @mtd: mtd info structure
2148 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002149 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302150 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002151 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302152 * @oob_required: must write chip->oob_poi to OOB
2153 */
2154static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2155 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002156 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302157 int oob_required)
2158{
2159 uint8_t *oob_buf = chip->oob_poi;
2160 uint8_t *ecc_calc = chip->buffers->ecccalc;
2161 int ecc_size = chip->ecc.size;
2162 int ecc_bytes = chip->ecc.bytes;
2163 int ecc_steps = chip->ecc.steps;
2164 uint32_t *eccpos = chip->ecc.layout->eccpos;
2165 uint32_t start_step = offset / ecc_size;
2166 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2167 int oob_bytes = mtd->oobsize / ecc_steps;
2168 int step, i;
2169
2170 for (step = 0; step < ecc_steps; step++) {
2171 /* configure controller for WRITE access */
2172 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2173
2174 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002175 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302176
2177 /* mask ECC of un-touched subpages by padding 0xFF */
2178 if ((step < start_step) || (step > end_step))
2179 memset(ecc_calc, 0xff, ecc_bytes);
2180 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002181 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302182
2183 /* mask OOB of un-touched subpages by padding 0xFF */
2184 /* if oob_required, preserve OOB metadata of written subpage */
2185 if (!oob_required || (step < start_step) || (step > end_step))
2186 memset(oob_buf, 0xff, oob_bytes);
2187
Brian Norrisd6a950802013-08-08 17:16:36 -07002188 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302189 ecc_calc += ecc_bytes;
2190 oob_buf += oob_bytes;
2191 }
2192
2193 /* copy calculated ECC for whole page to chip->buffer->oob */
2194 /* this include masked-value(0xFF) for unwritten subpages */
2195 ecc_calc = chip->buffers->ecccalc;
2196 for (i = 0; i < chip->ecc.total; i++)
2197 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2198
2199 /* write OOB buffer to NAND device */
2200 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2201
2202 return 0;
2203}
2204
2205
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002206/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002207 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002208 * @mtd: mtd info structure
2209 * @chip: nand chip info structure
2210 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002211 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002212 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002213 * The hw generator calculates the error syndrome automatically. Therefore we
2214 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002215 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002216static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002217 struct nand_chip *chip,
2218 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002219{
2220 int i, eccsize = chip->ecc.size;
2221 int eccbytes = chip->ecc.bytes;
2222 int eccsteps = chip->ecc.steps;
2223 const uint8_t *p = buf;
2224 uint8_t *oob = chip->oob_poi;
2225
2226 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2227
2228 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2229 chip->write_buf(mtd, p, eccsize);
2230
2231 if (chip->ecc.prepad) {
2232 chip->write_buf(mtd, oob, chip->ecc.prepad);
2233 oob += chip->ecc.prepad;
2234 }
2235
2236 chip->ecc.calculate(mtd, p, oob);
2237 chip->write_buf(mtd, oob, eccbytes);
2238 oob += eccbytes;
2239
2240 if (chip->ecc.postpad) {
2241 chip->write_buf(mtd, oob, chip->ecc.postpad);
2242 oob += chip->ecc.postpad;
2243 }
2244 }
2245
2246 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002247 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002248 if (i)
2249 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002250
2251 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002252}
2253
2254/**
David Woodhouse956e9442006-09-25 17:12:39 +01002255 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002256 * @mtd: MTD device structure
2257 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302258 * @offset: address offset within the page
2259 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002260 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002261 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002262 * @page: page number to write
2263 * @cached: cached programming
2264 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002265 */
2266static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302267 uint32_t offset, int data_len, const uint8_t *buf,
2268 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002269{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302270 int status, subpage;
2271
2272 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2273 chip->ecc.write_subpage)
2274 subpage = offset || (data_len < mtd->writesize);
2275 else
2276 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002277
2278 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2279
David Woodhouse956e9442006-09-25 17:12:39 +01002280 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302281 status = chip->ecc.write_page_raw(mtd, chip, buf,
2282 oob_required);
2283 else if (subpage)
2284 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2285 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002286 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002287 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2288
2289 if (status < 0)
2290 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002291
2292 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002293 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002294 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002295 */
2296 cached = 0;
2297
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002298 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002299
2300 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002301 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002302 /*
2303 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002304 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002305 */
2306 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2307 status = chip->errstat(mtd, chip, FL_WRITING, status,
2308 page);
2309
2310 if (status & NAND_STATUS_FAIL)
2311 return -EIO;
2312 } else {
2313 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002314 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002315 }
2316
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002317 return 0;
2318}
2319
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002320/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002321 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002322 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002323 * @oob: oob data buffer
2324 * @len: oob data write length
2325 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002326 */
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002327static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2328 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002329{
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002330 struct nand_chip *chip = mtd->priv;
2331
2332 /*
2333 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2334 * data from a previous OOB read.
2335 */
2336 memset(chip->oob_poi, 0xff, mtd->oobsize);
2337
Florian Fainellif8ac0412010-09-07 13:23:43 +02002338 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002339
Brian Norris0612b9d2011-08-30 18:45:40 -07002340 case MTD_OPS_PLACE_OOB:
2341 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002342 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2343 return oob + len;
2344
Brian Norris0612b9d2011-08-30 18:45:40 -07002345 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002346 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002347 uint32_t boffs = 0, woffs = ops->ooboffs;
2348 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002349
Florian Fainellif8ac0412010-09-07 13:23:43 +02002350 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002351 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002352 if (unlikely(woffs)) {
2353 if (woffs >= free->length) {
2354 woffs -= free->length;
2355 continue;
2356 }
2357 boffs = free->offset + woffs;
2358 bytes = min_t(size_t, len,
2359 (free->length - woffs));
2360 woffs = 0;
2361 } else {
2362 bytes = min_t(size_t, len, free->length);
2363 boffs = free->offset;
2364 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002365 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002366 oob += bytes;
2367 }
2368 return oob;
2369 }
2370 default:
2371 BUG();
2372 }
2373 return NULL;
2374}
2375
Florian Fainellif8ac0412010-09-07 13:23:43 +02002376#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002377
2378/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002379 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002380 * @mtd: MTD device structure
2381 * @to: offset to write to
2382 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002383 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002384 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002385 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002386static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2387 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002388{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002389 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002390 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002391 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002392
2393 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002394 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002395 mtd->oobavail : mtd->oobsize;
2396
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002397 uint8_t *oob = ops->oobbuf;
2398 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302399 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002400 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002401
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002402 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002403 if (!writelen)
2404 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002405
Brian Norris8b6e50c2011-05-25 14:59:01 -07002406 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002407 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002408 pr_notice("%s: attempt to write non page aligned data\n",
2409 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002410 return -EINVAL;
2411 }
2412
Thomas Gleixner29072b92006-09-28 15:38:36 +02002413 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002414
Thomas Gleixner6a930962006-06-28 00:11:45 +02002415 chipnr = (int)(to >> chip->chip_shift);
2416 chip->select_chip(mtd, chipnr);
2417
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002418 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002419 if (nand_check_wp(mtd)) {
2420 ret = -EIO;
2421 goto err_out;
2422 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002423
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002424 realpage = (int)(to >> chip->page_shift);
2425 page = realpage & chip->pagemask;
2426 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2427
2428 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002429 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2430 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002431 chip->pagebuf = -1;
2432
Maxim Levitsky782ce792010-02-22 20:39:36 +02002433 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002434 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2435 ret = -EINVAL;
2436 goto err_out;
2437 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002438
Florian Fainellif8ac0412010-09-07 13:23:43 +02002439 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002440 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002441 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002442 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002443 int use_bufpoi;
2444 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002445
Kamal Dasu66507c72014-05-01 20:51:19 -04002446 if (part_pagewr)
2447 use_bufpoi = 1;
2448 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2449 use_bufpoi = !virt_addr_valid(buf);
2450 else
2451 use_bufpoi = 0;
2452
2453 /* Partial page write?, or need to use bounce buffer */
2454 if (use_bufpoi) {
2455 pr_debug("%s: using write bounce buffer for buf@%p\n",
2456 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002457 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002458 if (part_pagewr)
2459 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002460 chip->pagebuf = -1;
2461 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2462 memcpy(&chip->buffers->databuf[column], buf, bytes);
2463 wbuf = chip->buffers->databuf;
2464 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002465
Maxim Levitsky782ce792010-02-22 20:39:36 +02002466 if (unlikely(oob)) {
2467 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002468 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002469 oobwritelen -= len;
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002470 } else {
2471 /* We still need to erase leftover OOB data */
2472 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002473 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302474 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2475 oob_required, page, cached,
2476 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002477 if (ret)
2478 break;
2479
2480 writelen -= bytes;
2481 if (!writelen)
2482 break;
2483
Thomas Gleixner29072b92006-09-28 15:38:36 +02002484 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002485 buf += bytes;
2486 realpage++;
2487
2488 page = realpage & chip->pagemask;
2489 /* Check, if we cross a chip boundary */
2490 if (!page) {
2491 chipnr++;
2492 chip->select_chip(mtd, -1);
2493 chip->select_chip(mtd, chipnr);
2494 }
2495 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002497 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002498 if (unlikely(oob))
2499 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002500
2501err_out:
2502 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002503 return ret;
2504}
2505
2506/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002507 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002508 * @mtd: MTD device structure
2509 * @to: offset to write to
2510 * @len: number of bytes to write
2511 * @retlen: pointer to variable to store the number of written bytes
2512 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002513 *
2514 * NAND write with ECC. Used when performing writes in interrupt context, this
2515 * may for example be called by mtdoops when writing an oops while in panic.
2516 */
2517static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2518 size_t *retlen, const uint8_t *buf)
2519{
2520 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002521 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002522 int ret;
2523
Brian Norris8b6e50c2011-05-25 14:59:01 -07002524 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002525 panic_nand_wait(mtd, chip, 400);
2526
Brian Norris8b6e50c2011-05-25 14:59:01 -07002527 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002528 panic_nand_get_device(chip, mtd, FL_WRITING);
2529
Brian Norris0ec56dc2015-02-28 02:02:30 -08002530 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002531 ops.len = len;
2532 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002533 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002534
Brian Norris4a89ff82011-08-30 18:45:45 -07002535 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002536
Brian Norris4a89ff82011-08-30 18:45:45 -07002537 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002538 return ret;
2539}
2540
2541/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002542 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002543 * @mtd: MTD device structure
2544 * @to: offset to write to
2545 * @len: number of bytes to write
2546 * @retlen: pointer to variable to store the number of written bytes
2547 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002549 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002551static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002552 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Brian Norris4a89ff82011-08-30 18:45:45 -07002554 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002555 int ret;
2556
Huang Shijie6a8214a2012-11-19 14:43:30 +08002557 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002558 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002559 ops.len = len;
2560 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae62012-07-03 16:44:14 +08002561 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002562 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002563 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002564 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002565 return ret;
2566}
2567
2568/**
2569 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002570 * @mtd: MTD device structure
2571 * @to: offset to write to
2572 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002573 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002574 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002575 */
2576static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2577 struct mtd_oob_ops *ops)
2578{
Adrian Hunter03736152007-01-31 17:58:29 +02002579 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Brian Norris289c0522011-07-19 10:06:09 -07002582 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302583 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Brian Norris0612b9d2011-08-30 18:45:40 -07002585 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002586 len = chip->ecc.layout->oobavail;
2587 else
2588 len = mtd->oobsize;
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002591 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002592 pr_debug("%s: attempt to write past end of page\n",
2593 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return -EINVAL;
2595 }
2596
Adrian Hunter03736152007-01-31 17:58:29 +02002597 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002598 pr_debug("%s: attempt to start write outside oob\n",
2599 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002600 return -EINVAL;
2601 }
2602
Jason Liu775adc3d42011-02-25 13:06:18 +08002603 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002604 if (unlikely(to >= mtd->size ||
2605 ops->ooboffs + ops->ooblen >
2606 ((mtd->size >> chip->page_shift) -
2607 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002608 pr_debug("%s: attempt to write beyond end of device\n",
2609 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002610 return -EINVAL;
2611 }
2612
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002613 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002614 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002616 /* Shift to get page */
2617 page = (int)(to >> chip->page_shift);
2618
2619 /*
2620 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2621 * of my DiskOnChip 2000 test units) will clear the whole data page too
2622 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2623 * it in the doc2000 driver in August 1999. dwmw2.
2624 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002628 if (nand_check_wp(mtd)) {
2629 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002630 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002631 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002634 if (page == chip->pagebuf)
2635 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
THOMSON, Adam (Adam)f722013e2011-06-14 16:52:38 +02002637 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002638
Brian Norris0612b9d2011-08-30 18:45:40 -07002639 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002640 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2641 else
2642 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002643
Huang Shijieb0bb6902012-11-19 14:43:29 +08002644 chip->select_chip(mtd, -1);
2645
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002646 if (status)
2647 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Vitaly Wool70145682006-11-03 18:20:38 +03002649 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002651 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002654/**
2655 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002656 * @mtd: MTD device structure
2657 * @to: offset to write to
2658 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002659 */
2660static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2661 struct mtd_oob_ops *ops)
2662{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002663 int ret = -ENOTSUPP;
2664
2665 ops->retlen = 0;
2666
2667 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002668 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002669 pr_debug("%s: attempt to write beyond end of device\n",
2670 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002671 return -EINVAL;
2672 }
2673
Huang Shijie6a8214a2012-11-19 14:43:30 +08002674 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002675
Florian Fainellif8ac0412010-09-07 13:23:43 +02002676 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002677 case MTD_OPS_PLACE_OOB:
2678 case MTD_OPS_AUTO_OOB:
2679 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002680 break;
2681
2682 default:
2683 goto out;
2684 }
2685
2686 if (!ops->datbuf)
2687 ret = nand_do_write_oob(mtd, to, ops);
2688 else
2689 ret = nand_do_write_ops(mtd, to, ops);
2690
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002691out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002692 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return ret;
2694}
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696/**
Brian Norris49c50b92014-05-06 16:02:19 -07002697 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002698 * @mtd: MTD device structure
2699 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 *
Brian Norris49c50b92014-05-06 16:02:19 -07002701 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 */
Brian Norris49c50b92014-05-06 16:02:19 -07002703static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002705 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002707 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2708 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002709
2710 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
2713/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002715 * @mtd: MTD device structure
2716 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002718 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002720static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
David Woodhousee0c7d762006-05-13 18:07:53 +01002722 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002726 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002727 * @mtd: MTD device structure
2728 * @instr: erase instruction
2729 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002731 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002733int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2734 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Adrian Hunter69423d92008-12-10 13:37:21 +00002736 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002737 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002738 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Brian Norris289c0522011-07-19 10:06:09 -07002740 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2741 __func__, (unsigned long long)instr->addr,
2742 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302744 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002748 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002751 page = (int)(instr->addr >> chip->page_shift);
2752 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002755 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002758 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /* Check, if it is write protected */
2761 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002762 pr_debug("%s: device is write protected!\n",
2763 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 instr->state = MTD_ERASE_FAILED;
2765 goto erase_exit;
2766 }
2767
2768 /* Loop through the pages */
2769 len = instr->len;
2770
2771 instr->state = MTD_ERASING;
2772
2773 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002774 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002775 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2776 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002777 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2778 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 instr->state = MTD_ERASE_FAILED;
2780 goto erase_exit;
2781 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002782
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783 /*
2784 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002785 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002786 */
2787 if (page <= chip->pagebuf && chip->pagebuf <
2788 (page + pages_per_block))
2789 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Brian Norris49c50b92014-05-06 16:02:19 -07002791 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002793 /*
2794 * See if operation failed and additional status checks are
2795 * available
2796 */
2797 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2798 status = chip->errstat(mtd, chip, FL_ERASING,
2799 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002802 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002803 pr_debug("%s: failed erase, page 0x%08x\n",
2804 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002806 instr->fail_addr =
2807 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 goto erase_exit;
2809 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002812 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 page += pages_per_block;
2814
2815 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002816 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002818 chip->select_chip(mtd, -1);
2819 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
2821 }
2822 instr->state = MTD_ERASE_DONE;
2823
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002824erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002829 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 nand_release_device(mtd);
2831
David Woodhouse49defc02007-10-06 15:01:59 -04002832 /* Do call back function */
2833 if (!ret)
2834 mtd_erase_callback(instr);
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /* Return more or less happy */
2837 return ret;
2838}
2839
2840/**
2841 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002842 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002844 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002846static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
Brian Norris289c0522011-07-19 10:06:09 -07002848 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002851 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002853 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002857 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002858 * @mtd: MTD device structure
2859 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002861static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002863 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
2866/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002867 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002868 * @mtd: MTD device structure
2869 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002871static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 int ret;
2874
Florian Fainellif8ac0412010-09-07 13:23:43 +02002875 ret = nand_block_isbad(mtd, ofs);
2876 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002877 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (ret > 0)
2879 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002880 return ret;
2881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Brian Norris5a0edb22013-07-30 17:52:58 -07002883 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
2886/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002887 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2888 * @mtd: MTD device structure
2889 * @chip: nand chip info structure
2890 * @addr: feature address.
2891 * @subfeature_param: the subfeature parameters, a four bytes array.
2892 */
2893static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2894 int addr, uint8_t *subfeature_param)
2895{
2896 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002897 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002898
David Mosbergerd914c932013-05-29 15:30:13 +03002899 if (!chip->onfi_version ||
2900 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2901 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002902 return -EINVAL;
2903
2904 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002905 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2906 chip->write_byte(mtd, subfeature_param[i]);
2907
Huang Shijie7db03ec2012-09-13 14:57:52 +08002908 status = chip->waitfunc(mtd, chip);
2909 if (status & NAND_STATUS_FAIL)
2910 return -EIO;
2911 return 0;
2912}
2913
2914/**
2915 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2916 * @mtd: MTD device structure
2917 * @chip: nand chip info structure
2918 * @addr: feature address.
2919 * @subfeature_param: the subfeature parameters, a four bytes array.
2920 */
2921static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2922 int addr, uint8_t *subfeature_param)
2923{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002924 int i;
2925
David Mosbergerd914c932013-05-29 15:30:13 +03002926 if (!chip->onfi_version ||
2927 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2928 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002929 return -EINVAL;
2930
Huang Shijie7db03ec2012-09-13 14:57:52 +08002931 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002932 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2933 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002934 return 0;
2935}
2936
2937/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002938 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002939 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002940 */
2941static int nand_suspend(struct mtd_info *mtd)
2942{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002943 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002944}
2945
2946/**
2947 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002948 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002949 */
2950static void nand_resume(struct mtd_info *mtd)
2951{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002952 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002953
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002954 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002955 nand_release_device(mtd);
2956 else
Brian Norrisd0370212011-07-19 10:06:08 -07002957 pr_err("%s called for a chip which is not in suspended state\n",
2958 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002959}
2960
Scott Branden72ea4032014-11-20 11:18:05 -08002961/**
2962 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
2963 * prevent further operations
2964 * @mtd: MTD device structure
2965 */
2966static void nand_shutdown(struct mtd_info *mtd)
2967{
2968 nand_get_device(mtd, FL_SHUTDOWN);
2969}
2970
Brian Norris8b6e50c2011-05-25 14:59:01 -07002971/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002972static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002975 if (!chip->chip_delay)
2976 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002979 if (chip->cmdfunc == NULL)
2980 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002983 if (chip->waitfunc == NULL)
2984 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002986 if (!chip->select_chip)
2987 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002988
Huang Shijie4204ccc2013-08-16 10:10:07 +08002989 /* set for ONFI nand */
2990 if (!chip->onfi_set_features)
2991 chip->onfi_set_features = nand_onfi_set_features;
2992 if (!chip->onfi_get_features)
2993 chip->onfi_get_features = nand_onfi_get_features;
2994
Brian Norris68e80782013-07-18 01:17:02 -07002995 /* If called twice, pointers that depend on busw may need to be reset */
2996 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002997 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2998 if (!chip->read_word)
2999 chip->read_word = nand_read_word;
3000 if (!chip->block_bad)
3001 chip->block_bad = nand_block_bad;
3002 if (!chip->block_markbad)
3003 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003004 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003005 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003006 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3007 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003008 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003009 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003010 if (!chip->scan_bbt)
3011 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003012
3013 if (!chip->controller) {
3014 chip->controller = &chip->hwcontrol;
3015 spin_lock_init(&chip->controller->lock);
3016 init_waitqueue_head(&chip->controller->wq);
3017 }
3018
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003019}
3020
Brian Norris8b6e50c2011-05-25 14:59:01 -07003021/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003022static void sanitize_string(uint8_t *s, size_t len)
3023{
3024 ssize_t i;
3025
Brian Norris8b6e50c2011-05-25 14:59:01 -07003026 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003027 s[len - 1] = 0;
3028
Brian Norris8b6e50c2011-05-25 14:59:01 -07003029 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003030 for (i = 0; i < len - 1; i++) {
3031 if (s[i] < ' ' || s[i] > 127)
3032 s[i] = '?';
3033 }
3034
Brian Norris8b6e50c2011-05-25 14:59:01 -07003035 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003036 strim(s);
3037}
3038
3039static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3040{
3041 int i;
3042 while (len--) {
3043 crc ^= *p++ << 8;
3044 for (i = 0; i < 8; i++)
3045 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3046 }
3047
3048 return crc;
3049}
3050
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003051/* Parse the Extended Parameter Page. */
3052static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3053 struct nand_chip *chip, struct nand_onfi_params *p)
3054{
3055 struct onfi_ext_param_page *ep;
3056 struct onfi_ext_section *s;
3057 struct onfi_ext_ecc_info *ecc;
3058 uint8_t *cursor;
3059 int ret = -EINVAL;
3060 int len;
3061 int i;
3062
3063 len = le16_to_cpu(p->ext_param_page_length) * 16;
3064 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003065 if (!ep)
3066 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003067
3068 /* Send our own NAND_CMD_PARAM. */
3069 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3070
3071 /* Use the Change Read Column command to skip the ONFI param pages. */
3072 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3073 sizeof(*p) * p->num_of_param_pages , -1);
3074
3075 /* Read out the Extended Parameter Page. */
3076 chip->read_buf(mtd, (uint8_t *)ep, len);
3077 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3078 != le16_to_cpu(ep->crc))) {
3079 pr_debug("fail in the CRC.\n");
3080 goto ext_out;
3081 }
3082
3083 /*
3084 * Check the signature.
3085 * Do not strictly follow the ONFI spec, maybe changed in future.
3086 */
3087 if (strncmp(ep->sig, "EPPS", 4)) {
3088 pr_debug("The signature is invalid.\n");
3089 goto ext_out;
3090 }
3091
3092 /* find the ECC section. */
3093 cursor = (uint8_t *)(ep + 1);
3094 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3095 s = ep->sections + i;
3096 if (s->type == ONFI_SECTION_TYPE_2)
3097 break;
3098 cursor += s->length * 16;
3099 }
3100 if (i == ONFI_EXT_SECTION_MAX) {
3101 pr_debug("We can not find the ECC section.\n");
3102 goto ext_out;
3103 }
3104
3105 /* get the info we want. */
3106 ecc = (struct onfi_ext_ecc_info *)cursor;
3107
Brian Norris4ae7d222013-09-16 18:20:21 -07003108 if (!ecc->codeword_size) {
3109 pr_debug("Invalid codeword size\n");
3110 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003111 }
3112
Brian Norris4ae7d222013-09-16 18:20:21 -07003113 chip->ecc_strength_ds = ecc->ecc_bits;
3114 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003115 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003116
3117ext_out:
3118 kfree(ep);
3119 return ret;
3120}
3121
Brian Norris8429bb32013-12-03 15:51:09 -08003122static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3123{
3124 struct nand_chip *chip = mtd->priv;
3125 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3126
3127 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3128 feature);
3129}
3130
3131/*
3132 * Configure chip properties from Micron vendor-specific ONFI table
3133 */
3134static void nand_onfi_detect_micron(struct nand_chip *chip,
3135 struct nand_onfi_params *p)
3136{
3137 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3138
3139 if (le16_to_cpu(p->vendor_revision) < 1)
3140 return;
3141
3142 chip->read_retries = micron->read_retry_options;
3143 chip->setup_read_retry = nand_setup_read_retry_micron;
3144}
3145
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003146/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003147 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003148 */
3149static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003150 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003151{
3152 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003153 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003154 int val;
3155
Brian Norris7854d3f2011-06-23 14:12:08 -07003156 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003157 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3158 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3159 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3160 return 0;
3161
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003162 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3163 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003164 for (j = 0; j < sizeof(*p); j++)
3165 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003166 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3167 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003168 break;
3169 }
3170 }
3171
Brian Norrisc7f23a72013-08-13 10:51:55 -07003172 if (i == 3) {
3173 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003174 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003175 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003176
Brian Norris8b6e50c2011-05-25 14:59:01 -07003177 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003178 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003179 if (val & (1 << 5))
3180 chip->onfi_version = 23;
3181 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003182 chip->onfi_version = 22;
3183 else if (val & (1 << 3))
3184 chip->onfi_version = 21;
3185 else if (val & (1 << 2))
3186 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003187 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003188 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003189
3190 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003191 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003192 return 0;
3193 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003194
3195 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3196 sanitize_string(p->model, sizeof(p->model));
3197 if (!mtd->name)
3198 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003199
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003200 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003201
3202 /*
3203 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3204 * (don't ask me who thought of this...). MTD assumes that these
3205 * dimensions will be power-of-2, so just truncate the remaining area.
3206 */
3207 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3208 mtd->erasesize *= mtd->writesize;
3209
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003210 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003211
3212 /* See erasesize comment */
3213 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003214 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003215 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003216
3217 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003218 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003219 else
3220 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003221
Huang Shijie10c86ba2013-05-17 11:17:26 +08003222 if (p->ecc_bits != 0xff) {
3223 chip->ecc_strength_ds = p->ecc_bits;
3224 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003225 } else if (chip->onfi_version >= 21 &&
3226 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3227
3228 /*
3229 * The nand_flash_detect_ext_param_page() uses the
3230 * Change Read Column command which maybe not supported
3231 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3232 * now. We do not replace user supplied command function.
3233 */
3234 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3235 chip->cmdfunc = nand_command_lp;
3236
3237 /* The Extended Parameter Page is supported since ONFI 2.1. */
3238 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003239 pr_warn("Failed to detect ONFI extended param page\n");
3240 } else {
3241 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003242 }
3243
Brian Norris8429bb32013-12-03 15:51:09 -08003244 if (p->jedec_id == NAND_MFR_MICRON)
3245 nand_onfi_detect_micron(chip, p);
3246
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003247 return 1;
3248}
3249
3250/*
Huang Shijie91361812014-02-21 13:39:40 +08003251 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3252 */
3253static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3254 int *busw)
3255{
3256 struct nand_jedec_params *p = &chip->jedec_params;
3257 struct jedec_ecc_info *ecc;
3258 int val;
3259 int i, j;
3260
3261 /* Try JEDEC for unknown chip or LP */
3262 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3263 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3264 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3265 chip->read_byte(mtd) != 'C')
3266 return 0;
3267
3268 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3269 for (i = 0; i < 3; i++) {
3270 for (j = 0; j < sizeof(*p); j++)
3271 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3272
3273 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3274 le16_to_cpu(p->crc))
3275 break;
3276 }
3277
3278 if (i == 3) {
3279 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3280 return 0;
3281 }
3282
3283 /* Check version */
3284 val = le16_to_cpu(p->revision);
3285 if (val & (1 << 2))
3286 chip->jedec_version = 10;
3287 else if (val & (1 << 1))
3288 chip->jedec_version = 1; /* vendor specific version */
3289
3290 if (!chip->jedec_version) {
3291 pr_info("unsupported JEDEC version: %d\n", val);
3292 return 0;
3293 }
3294
3295 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3296 sanitize_string(p->model, sizeof(p->model));
3297 if (!mtd->name)
3298 mtd->name = p->model;
3299
3300 mtd->writesize = le32_to_cpu(p->byte_per_page);
3301
3302 /* Please reference to the comment for nand_flash_detect_onfi. */
3303 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3304 mtd->erasesize *= mtd->writesize;
3305
3306 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3307
3308 /* Please reference to the comment for nand_flash_detect_onfi. */
3309 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3310 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3311 chip->bits_per_cell = p->bits_per_cell;
3312
3313 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3314 *busw = NAND_BUSWIDTH_16;
3315 else
3316 *busw = 0;
3317
3318 /* ECC info */
3319 ecc = &p->ecc_info[0];
3320
3321 if (ecc->codeword_size >= 9) {
3322 chip->ecc_strength_ds = ecc->ecc_bits;
3323 chip->ecc_step_ds = 1 << ecc->codeword_size;
3324 } else {
3325 pr_warn("Invalid codeword size\n");
3326 }
3327
3328 return 1;
3329}
3330
3331/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003332 * nand_id_has_period - Check if an ID string has a given wraparound period
3333 * @id_data: the ID string
3334 * @arrlen: the length of the @id_data array
3335 * @period: the period of repitition
3336 *
3337 * Check if an ID string is repeated within a given sequence of bytes at
3338 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003339 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003340 * if the repetition has a period of @period; otherwise, returns zero.
3341 */
3342static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3343{
3344 int i, j;
3345 for (i = 0; i < period; i++)
3346 for (j = i + period; j < arrlen; j += period)
3347 if (id_data[i] != id_data[j])
3348 return 0;
3349 return 1;
3350}
3351
3352/*
3353 * nand_id_len - Get the length of an ID string returned by CMD_READID
3354 * @id_data: the ID string
3355 * @arrlen: the length of the @id_data array
3356
3357 * Returns the length of the ID string, according to known wraparound/trailing
3358 * zero patterns. If no pattern exists, returns the length of the array.
3359 */
3360static int nand_id_len(u8 *id_data, int arrlen)
3361{
3362 int last_nonzero, period;
3363
3364 /* Find last non-zero byte */
3365 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3366 if (id_data[last_nonzero])
3367 break;
3368
3369 /* All zeros */
3370 if (last_nonzero < 0)
3371 return 0;
3372
3373 /* Calculate wraparound period */
3374 for (period = 1; period < arrlen; period++)
3375 if (nand_id_has_period(id_data, arrlen, period))
3376 break;
3377
3378 /* There's a repeated pattern */
3379 if (period < arrlen)
3380 return period;
3381
3382 /* There are trailing zeros */
3383 if (last_nonzero < arrlen - 1)
3384 return last_nonzero + 1;
3385
3386 /* No pattern detected */
3387 return arrlen;
3388}
3389
Huang Shijie7db906b2013-09-25 14:58:11 +08003390/* Extract the bits of per cell from the 3rd byte of the extended ID */
3391static int nand_get_bits_per_cell(u8 cellinfo)
3392{
3393 int bits;
3394
3395 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3396 bits >>= NAND_CI_CELLTYPE_SHIFT;
3397 return bits + 1;
3398}
3399
Brian Norrise3b88bd2012-09-24 20:40:52 -07003400/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003401 * Many new NAND share similar device ID codes, which represent the size of the
3402 * chip. The rest of the parameters must be decoded according to generic or
3403 * manufacturer-specific "extended ID" decoding patterns.
3404 */
3405static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3406 u8 id_data[8], int *busw)
3407{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003408 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003409 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003410 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003411 /* The 4th id byte is the important one */
3412 extid = id_data[3];
3413
Brian Norrise3b88bd2012-09-24 20:40:52 -07003414 id_len = nand_id_len(id_data, 8);
3415
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003416 /*
3417 * Field definitions are in the following datasheets:
3418 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003419 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003420 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003421 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003422 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3423 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003424 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003425 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003426 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003427 /* Calc pagesize */
3428 mtd->writesize = 2048 << (extid & 0x03);
3429 extid >>= 2;
3430 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003431 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003432 case 1:
3433 mtd->oobsize = 128;
3434 break;
3435 case 2:
3436 mtd->oobsize = 218;
3437 break;
3438 case 3:
3439 mtd->oobsize = 400;
3440 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003441 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003442 mtd->oobsize = 436;
3443 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003444 case 5:
3445 mtd->oobsize = 512;
3446 break;
3447 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003448 mtd->oobsize = 640;
3449 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003450 case 7:
3451 default: /* Other cases are "reserved" (unknown) */
3452 mtd->oobsize = 1024;
3453 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003454 }
3455 extid >>= 2;
3456 /* Calc blocksize */
3457 mtd->erasesize = (128 * 1024) <<
3458 (((extid >> 1) & 0x04) | (extid & 0x03));
3459 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003460 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003461 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003462 unsigned int tmp;
3463
3464 /* Calc pagesize */
3465 mtd->writesize = 2048 << (extid & 0x03);
3466 extid >>= 2;
3467 /* Calc oobsize */
3468 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3469 case 0:
3470 mtd->oobsize = 128;
3471 break;
3472 case 1:
3473 mtd->oobsize = 224;
3474 break;
3475 case 2:
3476 mtd->oobsize = 448;
3477 break;
3478 case 3:
3479 mtd->oobsize = 64;
3480 break;
3481 case 4:
3482 mtd->oobsize = 32;
3483 break;
3484 case 5:
3485 mtd->oobsize = 16;
3486 break;
3487 default:
3488 mtd->oobsize = 640;
3489 break;
3490 }
3491 extid >>= 2;
3492 /* Calc blocksize */
3493 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3494 if (tmp < 0x03)
3495 mtd->erasesize = (128 * 1024) << tmp;
3496 else if (tmp == 0x03)
3497 mtd->erasesize = 768 * 1024;
3498 else
3499 mtd->erasesize = (64 * 1024) << tmp;
3500 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003501 } else {
3502 /* Calc pagesize */
3503 mtd->writesize = 1024 << (extid & 0x03);
3504 extid >>= 2;
3505 /* Calc oobsize */
3506 mtd->oobsize = (8 << (extid & 0x01)) *
3507 (mtd->writesize >> 9);
3508 extid >>= 2;
3509 /* Calc blocksize. Blocksize is multiples of 64KiB */
3510 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3511 extid >>= 2;
3512 /* Get buswidth information */
3513 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003514
3515 /*
3516 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3517 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3518 * follows:
3519 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3520 * 110b -> 24nm
3521 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3522 */
3523 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003524 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003525 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3526 !(id_data[4] & 0x80) /* !BENAND */) {
3527 mtd->oobsize = 32 * mtd->writesize >> 9;
3528 }
3529
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003530 }
3531}
3532
3533/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003534 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3535 * decodes a matching ID table entry and assigns the MTD size parameters for
3536 * the chip.
3537 */
3538static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3539 struct nand_flash_dev *type, u8 id_data[8],
3540 int *busw)
3541{
3542 int maf_id = id_data[0];
3543
3544 mtd->erasesize = type->erasesize;
3545 mtd->writesize = type->pagesize;
3546 mtd->oobsize = mtd->writesize / 32;
3547 *busw = type->options & NAND_BUSWIDTH_16;
3548
Huang Shijie1c195e92013-09-25 14:58:12 +08003549 /* All legacy ID NAND are small-page, SLC */
3550 chip->bits_per_cell = 1;
3551
Brian Norrisf23a4812012-09-24 20:40:51 -07003552 /*
3553 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3554 * some Spansion chips have erasesize that conflicts with size
3555 * listed in nand_ids table.
3556 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3557 */
3558 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3559 && id_data[6] == 0x00 && id_data[7] == 0x00
3560 && mtd->writesize == 512) {
3561 mtd->erasesize = 128 * 1024;
3562 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3563 }
3564}
3565
3566/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003567 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3568 * heuristic patterns using various detected parameters (e.g., manufacturer,
3569 * page size, cell-type information).
3570 */
3571static void nand_decode_bbm_options(struct mtd_info *mtd,
3572 struct nand_chip *chip, u8 id_data[8])
3573{
3574 int maf_id = id_data[0];
3575
3576 /* Set the bad block position */
3577 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3578 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3579 else
3580 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3581
3582 /*
3583 * Bad block marker is stored in the last page of each block on Samsung
3584 * and Hynix MLC devices; stored in first two pages of each block on
3585 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3586 * AMD/Spansion, and Macronix. All others scan only the first page.
3587 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003588 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003589 (maf_id == NAND_MFR_SAMSUNG ||
3590 maf_id == NAND_MFR_HYNIX))
3591 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003592 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003593 (maf_id == NAND_MFR_SAMSUNG ||
3594 maf_id == NAND_MFR_HYNIX ||
3595 maf_id == NAND_MFR_TOSHIBA ||
3596 maf_id == NAND_MFR_AMD ||
3597 maf_id == NAND_MFR_MACRONIX)) ||
3598 (mtd->writesize == 2048 &&
3599 maf_id == NAND_MFR_MICRON))
3600 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3601}
3602
Huang Shijieec6e87e2013-03-15 11:01:00 +08003603static inline bool is_full_id_nand(struct nand_flash_dev *type)
3604{
3605 return type->id_len;
3606}
3607
3608static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3609 struct nand_flash_dev *type, u8 *id_data, int *busw)
3610{
3611 if (!strncmp(type->id, id_data, type->id_len)) {
3612 mtd->writesize = type->pagesize;
3613 mtd->erasesize = type->erasesize;
3614 mtd->oobsize = type->oobsize;
3615
Huang Shijie7db906b2013-09-25 14:58:11 +08003616 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003617 chip->chipsize = (uint64_t)type->chipsize << 20;
3618 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003619 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3620 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003621 chip->onfi_timing_mode_default =
3622 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003623
3624 *busw = type->options & NAND_BUSWIDTH_16;
3625
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003626 if (!mtd->name)
3627 mtd->name = type->name;
3628
Huang Shijieec6e87e2013-03-15 11:01:00 +08003629 return true;
3630 }
3631 return false;
3632}
3633
Brian Norris7e74c2d2012-09-24 20:40:49 -07003634/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003635 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003636 */
3637static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003638 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003639 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003640 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003641{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003642 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003643 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003644 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003647 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Karl Beldanef89a882008-09-15 14:37:29 +02003649 /*
3650 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003651 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003652 */
3653 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003656 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003659 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003660 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Brian Norris8b6e50c2011-05-25 14:59:01 -07003662 /*
3663 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003664 * interface concerns can cause random data which looks like a
3665 * possibly credible NAND flash to appear. If the two results do
3666 * not match, ignore the device completely.
3667 */
3668
3669 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3670
Brian Norris4aef9b72012-09-24 20:40:48 -07003671 /* Read entire ID string */
3672 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003673 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003674
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003675 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003676 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003677 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003678 return ERR_PTR(-ENODEV);
3679 }
3680
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003681 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003682 type = nand_flash_ids;
3683
Huang Shijieec6e87e2013-03-15 11:01:00 +08003684 for (; type->name != NULL; type++) {
3685 if (is_full_id_nand(type)) {
3686 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3687 goto ident_done;
3688 } else if (*dev_id == type->dev_id) {
3689 break;
3690 }
3691 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003692
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003693 chip->onfi_version = 0;
3694 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003695 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003696 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003697 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003698
3699 /* Check if the chip is JEDEC compliant */
3700 if (nand_flash_detect_jedec(mtd, chip, &busw))
3701 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003702 }
3703
David Woodhouse5e81e882010-02-26 18:32:56 +00003704 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003705 return ERR_PTR(-ENODEV);
3706
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003707 if (!mtd->name)
3708 mtd->name = type->name;
3709
Adrian Hunter69423d92008-12-10 13:37:21 +00003710 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003711
Huang Shijie12a40a52010-09-27 10:43:53 +08003712 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003713 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003714 busw = chip->init_size(mtd, chip, id_data);
3715 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003716 /* Decode parameters from extended ID */
3717 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003718 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003719 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003720 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003721 /* Get chip options */
3722 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003723
Brian Norris8b6e50c2011-05-25 14:59:01 -07003724 /*
3725 * Check if chip is not a Samsung device. Do not clear the
3726 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003727 */
3728 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3729 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3730ident_done:
3731
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003732 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003733 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003734 if (nand_manuf_ids[maf_idx].id == *maf_id)
3735 break;
3736 }
3737
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003738 if (chip->options & NAND_BUSWIDTH_AUTO) {
3739 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3740 chip->options |= busw;
3741 nand_set_defaults(chip, busw);
3742 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3743 /*
3744 * Check, if buswidth is correct. Hardware drivers should set
3745 * chip correct!
3746 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003747 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3748 *maf_id, *dev_id);
3749 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3750 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003751 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3752 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003753 return ERR_PTR(-EINVAL);
3754 }
3755
Brian Norris7e74c2d2012-09-24 20:40:49 -07003756 nand_decode_bbm_options(mtd, chip, id_data);
3757
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003758 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003759 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003760 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003761 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003762
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003763 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003764 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003765 if (chip->chipsize & 0xffffffff)
3766 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003767 else {
3768 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3769 chip->chip_shift += 32 - 1;
3770 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003771
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003772 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003773 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003774
Brian Norris8b6e50c2011-05-25 14:59:01 -07003775 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003776 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3777 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003778
Ezequiel Garcia20171642013-11-25 08:30:31 -03003779 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3780 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003781
3782 if (chip->onfi_version)
3783 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3784 chip->onfi_params.model);
3785 else if (chip->jedec_version)
3786 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3787 chip->jedec_params.model);
3788 else
3789 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3790 type->name);
3791
Rafał Miłecki3755a992014-10-21 00:01:04 +02003792 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003793 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003794 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003795 return type;
3796}
3797
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003798/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003799 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003800 * @mtd: MTD device structure
3801 * @maxchips: number of chips to scan for
3802 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003803 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003804 * This is the first phase of the normal nand_scan() function. It reads the
3805 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003806 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003807 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003808 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003809int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3810 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003811{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003812 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003813 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003814 struct nand_flash_dev *type;
3815
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003816 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003817 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003818
3819 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003820 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3821 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003822
3823 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003824 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003825 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003826 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003827 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
3829
Huang Shijie07300162012-11-09 16:23:45 +08003830 chip->select_chip(mtd, -1);
3831
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003832 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003833 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003834 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003835 /* See comment in nand_get_flash_type for reset */
3836 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003838 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003840 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003841 nand_dev_id != chip->read_byte(mtd)) {
3842 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 break;
Huang Shijie07300162012-11-09 16:23:45 +08003844 }
3845 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
3847 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003848 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003851 chip->numchips = i;
3852 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
David Woodhouse3b85c322006-09-25 17:06:53 +01003854 return 0;
3855}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003856EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003857
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03003858/*
3859 * Check if the chip configuration meet the datasheet requirements.
3860
3861 * If our configuration corrects A bits per B bytes and the minimum
3862 * required correction level is X bits per Y bytes, then we must ensure
3863 * both of the following are true:
3864 *
3865 * (1) A / B >= X / Y
3866 * (2) A >= X
3867 *
3868 * Requirement (1) ensures we can correct for the required bitflip density.
3869 * Requirement (2) ensures we can correct even when all bitflips are clumped
3870 * in the same sector.
3871 */
3872static bool nand_ecc_strength_good(struct mtd_info *mtd)
3873{
3874 struct nand_chip *chip = mtd->priv;
3875 struct nand_ecc_ctrl *ecc = &chip->ecc;
3876 int corr, ds_corr;
3877
3878 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3879 /* Not enough information */
3880 return true;
3881
3882 /*
3883 * We get the number of corrected bits per page to compare
3884 * the correction density.
3885 */
3886 corr = (mtd->writesize * ecc->strength) / ecc->size;
3887 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3888
3889 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3890}
David Woodhouse3b85c322006-09-25 17:06:53 +01003891
3892/**
3893 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003894 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003895 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003896 * This is the second phase of the normal nand_scan() function. It fills out
3897 * all the uninitialized function pointers with the defaults and scans for a
3898 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003899 */
3900int nand_scan_tail(struct mtd_info *mtd)
3901{
3902 int i;
3903 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003904 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003905 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003906
Brian Norrise2414f42012-02-06 13:44:00 -08003907 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3908 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3909 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3910
Huang Shijief02ea4e2014-01-13 14:27:12 +08003911 if (!(chip->options & NAND_OWN_BUFFERS)) {
3912 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3913 + mtd->oobsize * 3, GFP_KERNEL);
3914 if (!nbuf)
3915 return -ENOMEM;
3916 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3917 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3918 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3919
3920 chip->buffers = nbuf;
3921 } else {
3922 if (!chip->buffers)
3923 return -ENOMEM;
3924 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003925
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003926 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003927 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003928
3929 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003930 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003931 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003932 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003933 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003935 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 break;
3937 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003938 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 break;
3940 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003941 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003943 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003944 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003945 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003947 pr_warn("No oob scheme defined for oobsize %d\n",
3948 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 BUG();
3950 }
3951 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003952
David Woodhouse956e9442006-09-25 17:12:39 +01003953 if (!chip->write_page)
3954 chip->write_page = nand_write_page;
3955
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003956 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003957 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003958 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003959 */
David Woodhouse956e9442006-09-25 17:12:39 +01003960
Huang Shijie97de79e02013-10-18 14:20:53 +08003961 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003962 case NAND_ECC_HW_OOB_FIRST:
3963 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003964 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02003965 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003966 BUG();
3967 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003968 if (!ecc->read_page)
3969 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003970
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003971 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003972 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003973 if (!ecc->read_page)
3974 ecc->read_page = nand_read_page_hwecc;
3975 if (!ecc->write_page)
3976 ecc->write_page = nand_write_page_hwecc;
3977 if (!ecc->read_page_raw)
3978 ecc->read_page_raw = nand_read_page_raw;
3979 if (!ecc->write_page_raw)
3980 ecc->write_page_raw = nand_write_page_raw;
3981 if (!ecc->read_oob)
3982 ecc->read_oob = nand_read_oob_std;
3983 if (!ecc->write_oob)
3984 ecc->write_oob = nand_write_oob_std;
3985 if (!ecc->read_subpage)
3986 ecc->read_subpage = nand_read_subpage;
3987 if (!ecc->write_subpage)
3988 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003989
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003990 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003991 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3992 (!ecc->read_page ||
3993 ecc->read_page == nand_read_page_hwecc ||
3994 !ecc->write_page ||
3995 ecc->write_page == nand_write_page_hwecc)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02003996 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003997 BUG();
3998 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003999 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004000 if (!ecc->read_page)
4001 ecc->read_page = nand_read_page_syndrome;
4002 if (!ecc->write_page)
4003 ecc->write_page = nand_write_page_syndrome;
4004 if (!ecc->read_page_raw)
4005 ecc->read_page_raw = nand_read_page_raw_syndrome;
4006 if (!ecc->write_page_raw)
4007 ecc->write_page_raw = nand_write_page_raw_syndrome;
4008 if (!ecc->read_oob)
4009 ecc->read_oob = nand_read_oob_syndrome;
4010 if (!ecc->write_oob)
4011 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004012
Huang Shijie97de79e02013-10-18 14:20:53 +08004013 if (mtd->writesize >= ecc->size) {
4014 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07004015 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4016 BUG();
4017 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004018 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004019 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004020 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4021 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004022 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004024 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004025 ecc->calculate = nand_calculate_ecc;
4026 ecc->correct = nand_correct_data;
4027 ecc->read_page = nand_read_page_swecc;
4028 ecc->read_subpage = nand_read_subpage;
4029 ecc->write_page = nand_write_page_swecc;
4030 ecc->read_page_raw = nand_read_page_raw;
4031 ecc->write_page_raw = nand_write_page_raw;
4032 ecc->read_oob = nand_read_oob_std;
4033 ecc->write_oob = nand_write_oob_std;
4034 if (!ecc->size)
4035 ecc->size = 256;
4036 ecc->bytes = 3;
4037 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004039
Ivan Djelic193bd402011-03-11 11:05:33 +01004040 case NAND_ECC_SOFT_BCH:
4041 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03004042 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004043 BUG();
4044 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004045 ecc->calculate = nand_bch_calculate_ecc;
4046 ecc->correct = nand_bch_correct_data;
4047 ecc->read_page = nand_read_page_swecc;
4048 ecc->read_subpage = nand_read_subpage;
4049 ecc->write_page = nand_write_page_swecc;
4050 ecc->read_page_raw = nand_read_page_raw;
4051 ecc->write_page_raw = nand_write_page_raw;
4052 ecc->read_oob = nand_read_oob_std;
4053 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004054 /*
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004055 * Board driver should supply ecc.size and ecc.strength values
4056 * to select how many bits are correctable. Otherwise, default
4057 * to 4 bits for large page devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004058 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004059 if (!ecc->size && (mtd->oobsize >= 64)) {
4060 ecc->size = 512;
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004061 ecc->strength = 4;
Ivan Djelic193bd402011-03-11 11:05:33 +01004062 }
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004063
4064 /* See nand_bch_init() for details. */
4065 ecc->bytes = DIV_ROUND_UP(
4066 ecc->strength * fls(8 * ecc->size), 8);
Huang Shijie97de79e02013-10-18 14:20:53 +08004067 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4068 &ecc->layout);
4069 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004070 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01004071 BUG();
4072 }
4073 break;
4074
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004075 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004076 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004077 ecc->read_page = nand_read_page_raw;
4078 ecc->write_page = nand_write_page_raw;
4079 ecc->read_oob = nand_read_oob_std;
4080 ecc->read_page_raw = nand_read_page_raw;
4081 ecc->write_page_raw = nand_write_page_raw;
4082 ecc->write_oob = nand_write_oob_std;
4083 ecc->size = mtd->writesize;
4084 ecc->bytes = 0;
4085 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08004089 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004090 BUG();
4091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Brian Norris9ce244b2011-08-30 18:45:37 -07004093 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004094 if (!ecc->read_oob_raw)
4095 ecc->read_oob_raw = ecc->read_oob;
4096 if (!ecc->write_oob_raw)
4097 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004098
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004099 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004100 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07004101 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004102 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004103 ecc->layout->oobavail = 0;
4104 for (i = 0; ecc->layout->oobfree[i].length
4105 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4106 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4107 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004108
Thomas Petazzoni54c39e92014-07-02 15:16:32 +02004109 /* ECC sanity check: warn if it's too weak */
4110 if (!nand_ecc_strength_good(mtd))
4111 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4112 mtd->name);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004113
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004114 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004115 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004116 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004117 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004118 ecc->steps = mtd->writesize / ecc->size;
4119 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07004120 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004121 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004123 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004124
Brian Norris8b6e50c2011-05-25 14:59:01 -07004125 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004126 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004127 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004128 case 2:
4129 mtd->subpage_sft = 1;
4130 break;
4131 case 4:
4132 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004133 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004134 mtd->subpage_sft = 2;
4135 break;
4136 }
4137 }
4138 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4139
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004140 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004141 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004144 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004146 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304147 switch (ecc->mode) {
4148 case NAND_ECC_SOFT:
4149 case NAND_ECC_SOFT_BCH:
4150 if (chip->page_shift > 9)
4151 chip->options |= NAND_SUBPAGE_READ;
4152 break;
4153
4154 default:
4155 break;
4156 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004159 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004160 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4161 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004162 mtd->_erase = nand_erase;
4163 mtd->_point = NULL;
4164 mtd->_unpoint = NULL;
4165 mtd->_read = nand_read;
4166 mtd->_write = nand_write;
4167 mtd->_panic_write = panic_nand_write;
4168 mtd->_read_oob = nand_read_oob;
4169 mtd->_write_oob = nand_write_oob;
4170 mtd->_sync = nand_sync;
4171 mtd->_lock = NULL;
4172 mtd->_unlock = NULL;
4173 mtd->_suspend = nand_suspend;
4174 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004175 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004176 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004177 mtd->_block_isbad = nand_block_isbad;
4178 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004179 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Mike Dunn6a918ba2012-03-11 14:21:11 -07004181 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004182 mtd->ecclayout = ecc->layout;
4183 mtd->ecc_strength = ecc->strength;
4184 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004185 /*
4186 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4187 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4188 * properly set.
4189 */
4190 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004191 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004193 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004194 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004198 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004200EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Brian Norris8b6e50c2011-05-25 14:59:01 -07004202/*
4203 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004204 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004205 * to call us from in-kernel code if the core NAND support is modular.
4206 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004207#ifdef MODULE
4208#define caller_is_module() (1)
4209#else
4210#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004211 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004212#endif
4213
4214/**
4215 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004216 * @mtd: MTD device structure
4217 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004218 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004219 * This fills out all the uninitialized function pointers with the defaults.
4220 * The flash ID is read and the mtd/chip structures are filled with the
4221 * appropriate values. The mtd->owner field must be set to the module of the
4222 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004223 */
4224int nand_scan(struct mtd_info *mtd, int maxchips)
4225{
4226 int ret;
4227
4228 /* Many callers got this wrong, so check for it for a while... */
4229 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004230 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004231 BUG();
4232 }
4233
David Woodhouse5e81e882010-02-26 18:32:56 +00004234 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004235 if (!ret)
4236 ret = nand_scan_tail(mtd);
4237 return ret;
4238}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004239EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004242 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004243 * @mtd: MTD device structure
4244 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004245void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004247 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Ivan Djelic193bd402011-03-11 11:05:33 +01004249 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4250 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4251
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004252 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Jesper Juhlfa671642005-11-07 01:01:27 -08004254 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004255 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004256 if (!(chip->options & NAND_OWN_BUFFERS))
4257 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004258
4259 /* Free bad block descriptor memory */
4260 if (chip->badblock_pattern && chip->badblock_pattern->options
4261 & NAND_BBT_DYNAMICSTRUCT)
4262 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
David Woodhousee0c7d762006-05-13 18:07:53 +01004264EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004265
4266static int __init nand_base_init(void)
4267{
4268 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4269 return 0;
4270}
4271
4272static void __exit nand_base_exit(void)
4273{
4274 led_trigger_unregister_simple(nand_led_trigger);
4275}
4276
4277module_init(nand_base_init);
4278module_exit(nand_base_exit);
4279
David Woodhousee0c7d762006-05-13 18:07:53 +01004280MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004281MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4282MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004283MODULE_DESCRIPTION("Generic NAND flash driver code");