blob: 59eba5d2c68574fae787d36d2bd9e5ed29eb9f25 [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>
40#include <linux/types.h>
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010044#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/interrupt.h>
46#include <linux/bitops.h>
Richard Purdie8fe833c2006-03-31 02:31:14 -080047#include <linux/leds.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020048#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/mtd/partitions.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020052static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .eccbytes = 3,
54 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020055 .oobfree = {
56 {.offset = 3,
57 .length = 2},
58 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020059 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020062static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .eccbytes = 6,
64 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020065 .oobfree = {
66 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020067 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020070static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .eccbytes = 24,
72 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010073 40, 41, 42, 43, 44, 45, 46, 47,
74 48, 49, 50, 51, 52, 53, 54, 55,
75 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020076 .oobfree = {
77 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020078 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Thomas Gleixner81ec5362007-12-12 17:27:03 +010081static struct nand_ecclayout nand_oob_128 = {
82 .eccbytes = 48,
83 .eccpos = {
84 80, 81, 82, 83, 84, 85, 86, 87,
85 88, 89, 90, 91, 92, 93, 94, 95,
86 96, 97, 98, 99, 100, 101, 102, 103,
87 104, 105, 106, 107, 108, 109, 110, 111,
88 112, 113, 114, 115, 116, 117, 118, 119,
89 120, 121, 122, 123, 124, 125, 126, 127},
90 .oobfree = {
91 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020092 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010093};
94
Huang Shijie6a8214a2012-11-19 14:43:30 +080095static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020097static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
98 struct mtd_oob_ops *ops);
99
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200100/*
Joe Perches8e87d782008-02-03 17:22:34 +0200101 * For devices which display every fart in the system on a separate LED. Is
Thomas Gleixnerd470a972006-05-23 23:48:57 +0200102 * compiled away when LED support is disabled.
103 */
104DEFINE_LED_TRIGGER(nand_led_trigger);
105
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530106static int check_offs_len(struct mtd_info *mtd,
107 loff_t ofs, uint64_t len)
108{
109 struct nand_chip *chip = mtd->priv;
110 int ret = 0;
111
112 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300113 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700114 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530115 ret = -EINVAL;
116 }
117
118 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300119 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700120 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530121 ret = -EINVAL;
122 }
123
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530124 return ret;
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/**
128 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700129 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000130 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800131 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100133static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200135 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200137 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200138 spin_lock(&chip->controller->lock);
139 chip->controller->active = NULL;
140 chip->state = FL_READY;
141 wake_up(&chip->controller->wq);
142 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/**
146 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700147 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700149 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200151static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200153 struct nand_chip *chip = mtd->priv;
154 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/**
Masanari Iida064a7692012-11-09 23:20:58 +0900158 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris7854d3f2011-06-23 14:12:08 -0700159 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700160 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700162 * Default read function for 16bit buswidth with endianness conversion.
163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200165static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200167 struct nand_chip *chip = mtd->priv;
168 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700173 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700175 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177static u16 nand_read_word(struct mtd_info *mtd)
178{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200179 struct nand_chip *chip = mtd->priv;
180 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700185 * @mtd: MTD device structure
186 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
188 * Default select function for 1 chip devices.
189 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200190static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200192 struct nand_chip *chip = mtd->priv;
193
194 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200196 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200
201 default:
202 BUG();
203 }
204}
205
206/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100207 * nand_write_byte - [DEFAULT] write single byte to chip
208 * @mtd: MTD device structure
209 * @byte: value to write
210 *
211 * Default function to write a byte to I/O[7:0]
212 */
213static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
214{
215 struct nand_chip *chip = mtd->priv;
216
217 chip->write_buf(mtd, &byte, 1);
218}
219
220/**
221 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
222 * @mtd: MTD device structure
223 * @byte: value to write
224 *
225 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
226 */
227static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
228{
229 struct nand_chip *chip = mtd->priv;
230 uint16_t word = byte;
231
232 /*
233 * It's not entirely clear what should happen to I/O[15:8] when writing
234 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
235 *
236 * When the host supports a 16-bit bus width, only data is
237 * transferred at the 16-bit width. All address and command line
238 * transfers shall use only the lower 8-bits of the data bus. During
239 * command transfers, the host may place any value on the upper
240 * 8-bits of the data bus. During address transfers, the host shall
241 * set the upper 8-bits of the data bus to 00h.
242 *
243 * One user of the write_byte callback is nand_onfi_set_features. The
244 * four parameters are specified to be written to I/O[7:0], but this is
245 * neither an address nor a command transfer. Let's assume a 0 on the
246 * upper I/O lines is OK.
247 */
248 chip->write_buf(mtd, (uint8_t *)&word, 2);
249}
250
251/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700253 * @mtd: MTD device structure
254 * @buf: data buffer
255 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700257 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200259static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200261 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Alexander Shiyan76413832013-04-13 09:32:13 +0400263 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000267 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700268 * @mtd: MTD device structure
269 * @buf: buffer to store date
270 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700272 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200274static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200276 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Alexander Shiyan76413832013-04-13 09:32:13 +0400278 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700283 * @mtd: MTD device structure
284 * @buf: data buffer
285 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700287 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200289static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200291 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293
Alexander Shiyan76413832013-04-13 09:32:13 +0400294 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000298 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700299 * @mtd: MTD device structure
300 * @buf: buffer to store date
301 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700303 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200305static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200307 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Alexander Shiyan76413832013-04-13 09:32:13 +0400310 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700315 * @mtd: MTD device structure
316 * @ofs: offset from device start
317 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000319 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
322{
Brian Norriscdbec052012-01-13 18:11:48 -0800323 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200324 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 u16 bad;
326
Brian Norris5fb15492011-05-31 16:31:21 -0700327 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700328 ofs += mtd->erasesize - mtd->writesize;
329
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100330 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200333 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Huang Shijie6a8214a2012-11-19 14:43:30 +0800335 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200338 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Brian Norriscdbec052012-01-13 18:11:48 -0800341 do {
342 if (chip->options & NAND_BUSWIDTH_16) {
343 chip->cmdfunc(mtd, NAND_CMD_READOOB,
344 chip->badblockpos & 0xFE, page);
345 bad = cpu_to_le16(chip->read_word(mtd));
346 if (chip->badblockpos & 0x1)
347 bad >>= 8;
348 else
349 bad &= 0xFF;
350 } else {
351 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
352 page);
353 bad = chip->read_byte(mtd);
354 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000355
Brian Norriscdbec052012-01-13 18:11:48 -0800356 if (likely(chip->badblockbits == 8))
357 res = bad != 0xFF;
358 else
359 res = hweight8(bad) < chip->badblockbits;
360 ofs += mtd->writesize;
361 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
362 i++;
363 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200364
Huang Shijieb0bb6902012-11-19 14:43:29 +0800365 if (getchip) {
366 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800368 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return res;
371}
372
373/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700374 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700375 * @mtd: MTD device structure
376 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700378 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700379 * specific driver. It provides the details for writing a bad block marker to a
380 * block.
381 */
382static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
383{
384 struct nand_chip *chip = mtd->priv;
385 struct mtd_oob_ops ops;
386 uint8_t buf[2] = { 0, 0 };
387 int ret = 0, res, i = 0;
388
389 ops.datbuf = NULL;
390 ops.oobbuf = buf;
391 ops.ooboffs = chip->badblockpos;
392 if (chip->options & NAND_BUSWIDTH_16) {
393 ops.ooboffs &= ~0x01;
394 ops.len = ops.ooblen = 2;
395 } else {
396 ops.len = ops.ooblen = 1;
397 }
398 ops.mode = MTD_OPS_PLACE_OOB;
399
400 /* Write to first/last page(s) if necessary */
401 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
402 ofs += mtd->erasesize - mtd->writesize;
403 do {
404 res = nand_do_write_oob(mtd, ofs, &ops);
405 if (!ret)
406 ret = res;
407
408 i++;
409 ofs += mtd->writesize;
410 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
411
412 return ret;
413}
414
415/**
416 * nand_block_markbad_lowlevel - mark a block bad
417 * @mtd: MTD device structure
418 * @ofs: offset from device start
419 *
420 * This function performs the generic NAND bad block marking steps (i.e., bad
421 * block table(s) and/or marker(s)). We only allow the hardware driver to
422 * specify how to write bad block markers to OOB (chip->block_markbad).
423 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700424 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800425 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700426 * (2) write bad block marker to OOB area of affected block (unless flag
427 * NAND_BBT_NO_OOB_BBM is present)
428 * (3) update the BBT
429 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800430 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700432static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200434 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700435 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000436
Brian Norrisb32843b2013-07-30 17:52:59 -0700437 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800438 struct erase_info einfo;
439
440 /* Attempt erase before marking OOB */
441 memset(&einfo, 0, sizeof(einfo));
442 einfo.mtd = mtd;
443 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300444 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800445 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800446
Brian Norrisb32843b2013-07-30 17:52:59 -0700447 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800448 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700449 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300450 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200451 }
Brian Norrise2414f42012-02-06 13:44:00 -0800452
Brian Norrisb32843b2013-07-30 17:52:59 -0700453 /* Mark block bad in BBT */
454 if (chip->bbt) {
455 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800456 if (!ret)
457 ret = res;
458 }
459
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200460 if (!ret)
461 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300462
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200463 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000466/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700468 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700470 * Check, if the device is write protected. The function expects, that the
471 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100473static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200475 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200476
Brian Norris8b6e50c2011-05-25 14:59:01 -0700477 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200478 if (chip->options & NAND_BROKEN_XD)
479 return 0;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200482 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
483 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/**
487 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700488 * @mtd: MTD device structure
489 * @ofs: offset from device start
490 * @getchip: 0, if the chip is already selected
491 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *
493 * Check, if the block is bad. Either by reading the bad block table or
494 * calling of the scan function.
495 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200496static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
497 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200499 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000500
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200501 if (!chip->bbt)
502 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100505 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200508/**
509 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700510 * @mtd: MTD device structure
511 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200512 *
513 * Helper function for nand_wait_ready used when needing to wait in interrupt
514 * context.
515 */
516static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
517{
518 struct nand_chip *chip = mtd->priv;
519 int i;
520
521 /* Wait for the device to get ready */
522 for (i = 0; i < timeo; i++) {
523 if (chip->dev_ready(mtd))
524 break;
525 touch_softlockup_watchdog();
526 mdelay(1);
527 }
528}
529
Brian Norris7854d3f2011-06-23 14:12:08 -0700530/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100531void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000532{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200533 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100534 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000535
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200536 /* 400ms timeout */
537 if (in_interrupt() || oops_in_progress)
538 return panic_nand_wait_ready(mtd, 400);
539
Richard Purdie8fe833c2006-03-31 02:31:14 -0800540 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700541 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000542 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200543 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800544 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700545 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000546 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800547 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000548}
David Woodhouse4b648b02006-09-25 17:05:24 +0100549EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
552 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700553 * @mtd: MTD device structure
554 * @command: the command to be sent
555 * @column: the column address for this command, -1 if none
556 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700558 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200559 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200561static void nand_command(struct mtd_info *mtd, unsigned int command,
562 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200564 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200565 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Brian Norris8b6e50c2011-05-25 14:59:01 -0700567 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (command == NAND_CMD_SEQIN) {
569 int readcmd;
570
Joern Engel28318772006-05-22 23:18:05 +0200571 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200573 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 readcmd = NAND_CMD_READOOB;
575 } else if (column < 256) {
576 /* First 256 bytes --> READ0 */
577 readcmd = NAND_CMD_READ0;
578 } else {
579 column -= 256;
580 readcmd = NAND_CMD_READ1;
581 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200582 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200583 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200585 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Brian Norris8b6e50c2011-05-25 14:59:01 -0700587 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200588 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
589 /* Serially input address */
590 if (column != -1) {
591 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200592 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200593 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200594 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200595 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200597 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200598 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200599 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200600 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200601 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 if (chip->chipsize > (32 << 20))
603 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200604 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200605 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
607 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700608 * Program and erase have their own busy handlers status and sequential
609 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 case NAND_CMD_PAGEPROG:
614 case NAND_CMD_ERASE1:
615 case NAND_CMD_ERASE2:
616 case NAND_CMD_SEQIN:
617 case NAND_CMD_STATUS:
618 return;
619
620 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200621 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200623 udelay(chip->chip_delay);
624 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200625 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200626 chip->cmd_ctrl(mtd,
627 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200628 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
629 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return;
631
David Woodhousee0c7d762006-05-13 18:07:53 +0100632 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000634 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * If we don't have access to the busy pin, we apply the given
636 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100637 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 if (!chip->dev_ready) {
639 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700643 /*
644 * Apply this short delay always to ensure that we do wait tWB in
645 * any case on any machine.
646 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100647 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000648
649 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/**
653 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700654 * @mtd: MTD device structure
655 * @command: the command to be sent
656 * @column: the column address for this command, -1 if none
657 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200659 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700660 * devices. We don't have the separate regions as we have in the small page
661 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200663static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
664 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200666 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* Emulate NAND_CMD_READOOB */
669 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200670 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 command = NAND_CMD_READ0;
672 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000673
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200674 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400675 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200678 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /* Serially input address */
681 if (column != -1) {
682 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200683 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200685 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200686 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200687 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200690 chip->cmd_ctrl(mtd, page_addr, ctrl);
691 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200692 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200694 if (chip->chipsize > (128 << 20))
695 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200696 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200699 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000700
701 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700702 * Program and erase have their own busy handlers status, sequential
703 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case NAND_CMD_CACHEDPROG:
708 case NAND_CMD_PAGEPROG:
709 case NAND_CMD_ERASE1:
710 case NAND_CMD_ERASE2:
711 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200712 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000714 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200717 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200719 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200720 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
721 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
722 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
723 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200724 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
725 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return;
727
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200728 case NAND_CMD_RNDOUT:
729 /* No ready / busy check necessary */
730 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
731 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
732 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
733 NAND_NCE | NAND_CTRL_CHANGE);
734 return;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200737 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
738 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
739 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
740 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000741
David Woodhousee0c7d762006-05-13 18:07:53 +0100742 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700746 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100747 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200748 if (!chip->dev_ready) {
749 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000753
Brian Norris8b6e50c2011-05-25 14:59:01 -0700754 /*
755 * Apply this short delay always to ensure that we do wait tWB in
756 * any case on any machine.
757 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100758 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000759
760 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200764 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700765 * @chip: the nand chip descriptor
766 * @mtd: MTD device structure
767 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200768 *
769 * Used when in panic, no locks are taken.
770 */
771static void panic_nand_get_device(struct nand_chip *chip,
772 struct mtd_info *mtd, int new_state)
773{
Brian Norris7854d3f2011-06-23 14:12:08 -0700774 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200775 chip->controller->active = chip;
776 chip->state = new_state;
777}
778
779/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700781 * @mtd: MTD device structure
782 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
784 * Get the device and lock it for exclusive access
785 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200786static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800787nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800789 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200790 spinlock_t *lock = &chip->controller->lock;
791 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100792 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200793retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100794 spin_lock(lock);
795
vimal singhb8b3ee92009-07-09 20:41:22 +0530796 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200797 if (!chip->controller->active)
798 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200799
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200800 if (chip->controller->active == chip && chip->state == FL_READY) {
801 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100802 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100803 return 0;
804 }
805 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800806 if (chip->controller->active->state == FL_PM_SUSPENDED) {
807 chip->state = FL_PM_SUSPENDED;
808 spin_unlock(lock);
809 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800810 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100811 }
812 set_current_state(TASK_UNINTERRUPTIBLE);
813 add_wait_queue(wq, &wait);
814 spin_unlock(lock);
815 schedule();
816 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto retry;
818}
819
820/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700821 * panic_nand_wait - [GENERIC] wait until the command is done
822 * @mtd: MTD device structure
823 * @chip: NAND chip structure
824 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200825 *
826 * Wait for command done. This is a helper function for nand_wait used when
827 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400828 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200829 */
830static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
831 unsigned long timeo)
832{
833 int i;
834 for (i = 0; i < timeo; i++) {
835 if (chip->dev_ready) {
836 if (chip->dev_ready(mtd))
837 break;
838 } else {
839 if (chip->read_byte(mtd) & NAND_STATUS_READY)
840 break;
841 }
842 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200843 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200844}
845
846/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700847 * nand_wait - [DEFAULT] wait until the command is done
848 * @mtd: MTD device structure
849 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700851 * Wait for command done. This applies to erase and program only. Erase can
852 * take up to 400ms and program up to 20ms according to general NAND and
853 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700854 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200855static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200858 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800859 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Richard Purdie8fe833c2006-03-31 02:31:14 -0800861 led_trigger_event(nand_led_trigger, LED_FULL);
862
Brian Norris8b6e50c2011-05-25 14:59:01 -0700863 /*
864 * Apply this short delay always to ensure that we do wait tWB in any
865 * case on any machine.
866 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100867 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200869 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200871 if (in_interrupt() || oops_in_progress)
872 panic_nand_wait(mtd, chip, timeo);
873 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800874 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200875 while (time_before(jiffies, timeo)) {
876 if (chip->dev_ready) {
877 if (chip->dev_ready(mtd))
878 break;
879 } else {
880 if (chip->read_byte(mtd) & NAND_STATUS_READY)
881 break;
882 }
883 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800886 led_trigger_event(nand_led_trigger, LED_OFF);
887
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200888 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100889 /* This can happen if in case of timeout or buggy dev_ready */
890 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return status;
892}
893
894/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700895 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700896 * @mtd: mtd info
897 * @ofs: offset to start unlock from
898 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700899 * @invert: when = 0, unlock the range of blocks within the lower and
900 * upper boundary address
901 * when = 1, unlock the range of blocks outside the boundaries
902 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530903 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700904 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530905 */
906static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
907 uint64_t len, int invert)
908{
909 int ret = 0;
910 int status, page;
911 struct nand_chip *chip = mtd->priv;
912
913 /* Submit address of first page to unlock */
914 page = ofs >> chip->page_shift;
915 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
916
917 /* Submit address of last page to unlock */
918 page = (ofs + len) >> chip->page_shift;
919 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
920 (page | invert) & chip->pagemask);
921
922 /* Call wait ready function */
923 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530924 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400925 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700926 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530927 __func__, status);
928 ret = -EIO;
929 }
930
931 return ret;
932}
933
934/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700935 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700936 * @mtd: mtd info
937 * @ofs: offset to start unlock from
938 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530939 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530941 */
942int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
943{
944 int ret = 0;
945 int chipnr;
946 struct nand_chip *chip = mtd->priv;
947
Brian Norris289c0522011-07-19 10:06:09 -0700948 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530949 __func__, (unsigned long long)ofs, len);
950
951 if (check_offs_len(mtd, ofs, len))
952 ret = -EINVAL;
953
954 /* Align to last block address if size addresses end of the device */
955 if (ofs + len == mtd->size)
956 len -= mtd->erasesize;
957
Huang Shijie6a8214a2012-11-19 14:43:30 +0800958 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530959
960 /* Shift to get chip number */
961 chipnr = ofs >> chip->chip_shift;
962
963 chip->select_chip(mtd, chipnr);
964
965 /* Check, if it is write protected */
966 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700967 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530968 __func__);
969 ret = -EIO;
970 goto out;
971 }
972
973 ret = __nand_unlock(mtd, ofs, len, 0);
974
975out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800976 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530977 nand_release_device(mtd);
978
979 return ret;
980}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200981EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530982
983/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700984 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700985 * @mtd: mtd info
986 * @ofs: offset to start unlock from
987 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530988 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700989 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
990 * have this feature, but it allows only to lock all blocks, not for specified
991 * range for block. Implementing 'lock' feature by making use of 'unlock', for
992 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530993 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700994 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530995 */
996int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
997{
998 int ret = 0;
999 int chipnr, status, page;
1000 struct nand_chip *chip = mtd->priv;
1001
Brian Norris289c0522011-07-19 10:06:09 -07001002 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301003 __func__, (unsigned long long)ofs, len);
1004
1005 if (check_offs_len(mtd, ofs, len))
1006 ret = -EINVAL;
1007
Huang Shijie6a8214a2012-11-19 14:43:30 +08001008 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301009
1010 /* Shift to get chip number */
1011 chipnr = ofs >> chip->chip_shift;
1012
1013 chip->select_chip(mtd, chipnr);
1014
1015 /* Check, if it is write protected */
1016 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001017 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301018 __func__);
1019 status = MTD_ERASE_FAILED;
1020 ret = -EIO;
1021 goto out;
1022 }
1023
1024 /* Submit address of first page to lock */
1025 page = ofs >> chip->page_shift;
1026 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1027
1028 /* Call wait ready function */
1029 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301030 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001031 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001032 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301033 __func__, status);
1034 ret = -EIO;
1035 goto out;
1036 }
1037
1038 ret = __nand_unlock(mtd, ofs, len, 0x1);
1039
1040out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001041 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301042 nand_release_device(mtd);
1043
1044 return ret;
1045}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001046EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301047
1048/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001049 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001050 * @mtd: mtd info structure
1051 * @chip: nand chip info structure
1052 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001053 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001054 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001055 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001056 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001057 */
1058static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001059 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001060{
1061 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001062 if (oob_required)
1063 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001064 return 0;
1065}
1066
1067/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001068 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001069 * @mtd: mtd info structure
1070 * @chip: nand chip info structure
1071 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001072 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001073 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001074 *
1075 * We need a special oob layout and handling even when OOB isn't used.
1076 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001077static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001078 struct nand_chip *chip, uint8_t *buf,
1079 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001080{
1081 int eccsize = chip->ecc.size;
1082 int eccbytes = chip->ecc.bytes;
1083 uint8_t *oob = chip->oob_poi;
1084 int steps, size;
1085
1086 for (steps = chip->ecc.steps; steps > 0; steps--) {
1087 chip->read_buf(mtd, buf, eccsize);
1088 buf += eccsize;
1089
1090 if (chip->ecc.prepad) {
1091 chip->read_buf(mtd, oob, chip->ecc.prepad);
1092 oob += chip->ecc.prepad;
1093 }
1094
1095 chip->read_buf(mtd, oob, eccbytes);
1096 oob += eccbytes;
1097
1098 if (chip->ecc.postpad) {
1099 chip->read_buf(mtd, oob, chip->ecc.postpad);
1100 oob += chip->ecc.postpad;
1101 }
1102 }
1103
1104 size = mtd->oobsize - (oob - chip->oob_poi);
1105 if (size)
1106 chip->read_buf(mtd, oob, size);
1107
1108 return 0;
1109}
1110
1111/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001112 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001113 * @mtd: mtd info structure
1114 * @chip: nand chip info structure
1115 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001116 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001117 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001118 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001119static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001120 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001122 int i, eccsize = chip->ecc.size;
1123 int eccbytes = chip->ecc.bytes;
1124 int eccsteps = chip->ecc.steps;
1125 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001126 uint8_t *ecc_calc = chip->buffers->ecccalc;
1127 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001128 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001129 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001130
Brian Norris1fbb9382012-05-02 10:14:55 -07001131 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001132
1133 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1134 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1135
1136 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001137 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001138
1139 eccsteps = chip->ecc.steps;
1140 p = buf;
1141
1142 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1143 int stat;
1144
1145 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001146 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001147 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001148 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001149 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001150 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1151 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001152 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001153 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301157 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001158 * @mtd: mtd info structure
1159 * @chip: nand chip info structure
1160 * @data_offs: offset of requested data within the page
1161 * @readlen: data length
1162 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001163 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001164static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1165 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001166{
1167 int start_step, end_step, num_steps;
1168 uint32_t *eccpos = chip->ecc.layout->eccpos;
1169 uint8_t *p;
1170 int data_col_addr, i, gaps = 0;
1171 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1172 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001173 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001174 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001175
Brian Norris7854d3f2011-06-23 14:12:08 -07001176 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001177 start_step = data_offs / chip->ecc.size;
1178 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1179 num_steps = end_step - start_step + 1;
1180
Brian Norris8b6e50c2011-05-25 14:59:01 -07001181 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001182 datafrag_len = num_steps * chip->ecc.size;
1183 eccfrag_len = num_steps * chip->ecc.bytes;
1184
1185 data_col_addr = start_step * chip->ecc.size;
1186 /* If we read not a page aligned data */
1187 if (data_col_addr != 0)
1188 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1189
1190 p = bufpoi + data_col_addr;
1191 chip->read_buf(mtd, p, datafrag_len);
1192
Brian Norris8b6e50c2011-05-25 14:59:01 -07001193 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001194 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1195 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1196
Brian Norris8b6e50c2011-05-25 14:59:01 -07001197 /*
1198 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001199 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001200 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001201 for (i = 0; i < eccfrag_len - 1; i++) {
1202 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1203 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1204 gaps = 1;
1205 break;
1206 }
1207 }
1208 if (gaps) {
1209 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1210 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1211 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001212 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001213 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001214 * about buswidth alignment in read_buf.
1215 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001216 index = start_step * chip->ecc.bytes;
1217
1218 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001219 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001220 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001221 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001222 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001223 aligned_len++;
1224
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001225 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1226 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001227 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1228 }
1229
1230 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001231 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001232
1233 p = bufpoi + data_col_addr;
1234 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1235 int stat;
1236
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001237 stat = chip->ecc.correct(mtd, p,
1238 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001239 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001240 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001241 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001242 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001243 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1244 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001245 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001246 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001247}
1248
1249/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001250 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 * @mtd: mtd info structure
1252 * @chip: nand chip info structure
1253 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001254 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001255 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001256 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001257 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001258 */
1259static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001260 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001261{
1262 int i, eccsize = chip->ecc.size;
1263 int eccbytes = chip->ecc.bytes;
1264 int eccsteps = chip->ecc.steps;
1265 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001266 uint8_t *ecc_calc = chip->buffers->ecccalc;
1267 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001268 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001269 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001270
1271 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1272 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1273 chip->read_buf(mtd, p, eccsize);
1274 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1275 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001276 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001277
1278 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001279 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001280
1281 eccsteps = chip->ecc.steps;
1282 p = buf;
1283
1284 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1285 int stat;
1286
1287 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001288 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001289 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001290 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001291 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001292 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1293 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001294 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001295 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001296}
1297
1298/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001299 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001300 * @mtd: mtd info structure
1301 * @chip: nand chip info structure
1302 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001303 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001304 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001305 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001306 * Hardware ECC for large page chips, require OOB to be read first. For this
1307 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1308 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1309 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1310 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001311 */
1312static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001313 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001314{
1315 int i, eccsize = chip->ecc.size;
1316 int eccbytes = chip->ecc.bytes;
1317 int eccsteps = chip->ecc.steps;
1318 uint8_t *p = buf;
1319 uint8_t *ecc_code = chip->buffers->ecccode;
1320 uint32_t *eccpos = chip->ecc.layout->eccpos;
1321 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001322 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001323
1324 /* Read the OOB area first */
1325 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1326 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1327 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1328
1329 for (i = 0; i < chip->ecc.total; i++)
1330 ecc_code[i] = chip->oob_poi[eccpos[i]];
1331
1332 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1333 int stat;
1334
1335 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1336 chip->read_buf(mtd, p, eccsize);
1337 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1338
1339 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001340 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001341 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001342 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001343 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001344 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1345 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001346 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001347 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001348}
1349
1350/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001351 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001352 * @mtd: mtd info structure
1353 * @chip: nand chip info structure
1354 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001355 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001356 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001357 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001358 * The hw generator calculates the error syndrome automatically. Therefore we
1359 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001360 */
1361static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001362 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001363{
1364 int i, eccsize = chip->ecc.size;
1365 int eccbytes = chip->ecc.bytes;
1366 int eccsteps = chip->ecc.steps;
1367 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001368 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001369 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001370
1371 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1372 int stat;
1373
1374 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1375 chip->read_buf(mtd, p, eccsize);
1376
1377 if (chip->ecc.prepad) {
1378 chip->read_buf(mtd, oob, chip->ecc.prepad);
1379 oob += chip->ecc.prepad;
1380 }
1381
1382 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1383 chip->read_buf(mtd, oob, eccbytes);
1384 stat = chip->ecc.correct(mtd, p, oob, NULL);
1385
Mike Dunn3f91e942012-04-25 12:06:09 -07001386 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001387 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001388 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001389 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001390 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1391 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001392
1393 oob += eccbytes;
1394
1395 if (chip->ecc.postpad) {
1396 chip->read_buf(mtd, oob, chip->ecc.postpad);
1397 oob += chip->ecc.postpad;
1398 }
1399 }
1400
1401 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001402 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001403 if (i)
1404 chip->read_buf(mtd, oob, i);
1405
Mike Dunn3f91e942012-04-25 12:06:09 -07001406 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001407}
1408
1409/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001410 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001411 * @chip: nand chip structure
1412 * @oob: oob destination address
1413 * @ops: oob ops structure
1414 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001415 */
1416static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001417 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001418{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001419 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001420
Brian Norris0612b9d2011-08-30 18:45:40 -07001421 case MTD_OPS_PLACE_OOB:
1422 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001423 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1424 return oob + len;
1425
Brian Norris0612b9d2011-08-30 18:45:40 -07001426 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001427 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001428 uint32_t boffs = 0, roffs = ops->ooboffs;
1429 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001430
Florian Fainellif8ac0412010-09-07 13:23:43 +02001431 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001432 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001433 if (unlikely(roffs)) {
1434 if (roffs >= free->length) {
1435 roffs -= free->length;
1436 continue;
1437 }
1438 boffs = free->offset + roffs;
1439 bytes = min_t(size_t, len,
1440 (free->length - roffs));
1441 roffs = 0;
1442 } else {
1443 bytes = min_t(size_t, len, free->length);
1444 boffs = free->offset;
1445 }
1446 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001447 oob += bytes;
1448 }
1449 return oob;
1450 }
1451 default:
1452 BUG();
1453 }
1454 return NULL;
1455}
1456
1457/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001458 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1459 * @mtd: MTD device structure
1460 * @retry_mode: the retry mode to use
1461 *
1462 * Some vendors supply a special command to shift the Vt threshold, to be used
1463 * when there are too many bitflips in a page (i.e., ECC error). After setting
1464 * a new threshold, the host should retry reading the page.
1465 */
1466static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1467{
1468 struct nand_chip *chip = mtd->priv;
1469
1470 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1471
1472 if (retry_mode >= chip->read_retries)
1473 return -EINVAL;
1474
1475 if (!chip->setup_read_retry)
1476 return -EOPNOTSUPP;
1477
1478 return chip->setup_read_retry(mtd, retry_mode);
1479}
1480
1481/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001482 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001483 * @mtd: MTD device structure
1484 * @from: offset to read from
1485 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001486 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001487 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001488 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001489static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1490 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001491{
Brian Norrise47f3db2012-05-02 10:14:56 -07001492 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001493 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001494 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001495 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001496 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001497 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001498 mtd->oobavail : mtd->oobsize;
1499
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001500 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001501 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001502 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001503 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001505 chipnr = (int)(from >> chip->chip_shift);
1506 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001508 realpage = (int)(from >> chip->page_shift);
1509 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001511 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001513 buf = ops->datbuf;
1514 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001515 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001516
Florian Fainellif8ac0412010-09-07 13:23:43 +02001517 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001518 unsigned int ecc_failures = mtd->ecc_stats.failed;
1519
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001520 bytes = min(mtd->writesize - col, readlen);
1521 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001522
Brian Norris8b6e50c2011-05-25 14:59:01 -07001523 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001524 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001525 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Brian Norrisba84fb52014-01-03 15:13:33 -08001527read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001528 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Mike Dunnedbc45402012-04-25 12:06:11 -07001530 /*
1531 * Now read the page into the buffer. Absent an error,
1532 * the read methods return max bitflips per ecc step.
1533 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001534 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001535 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001536 oob_required,
1537 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001538 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1539 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001540 ret = chip->ecc.read_subpage(mtd, chip,
1541 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001542 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001543 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001544 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001545 if (ret < 0) {
1546 if (!aligned)
1547 /* Invalidate page cache */
1548 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001549 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001550 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001551
Mike Dunnedbc45402012-04-25 12:06:11 -07001552 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1553
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001554 /* Transfer not aligned data */
1555 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001556 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001557 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001558 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001559 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001560 chip->pagebuf_bitflips = ret;
1561 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001562 /* Invalidate page cache */
1563 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001564 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001565 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001567
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001568 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001569 int toread = min(oobreadlen, max_oobsize);
1570
1571 if (toread) {
1572 oob = nand_transfer_oob(chip,
1573 oob, ops, toread);
1574 oobreadlen -= toread;
1575 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001576 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001577
1578 if (chip->options & NAND_NEED_READRDY) {
1579 /* Apply delay or wait for ready/busy pin */
1580 if (!chip->dev_ready)
1581 udelay(chip->chip_delay);
1582 else
1583 nand_wait_ready(mtd);
1584 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001585
Brian Norrisba84fb52014-01-03 15:13:33 -08001586 if (mtd->ecc_stats.failed - ecc_failures) {
1587 if (retry_mode + 1 <= chip->read_retries) {
1588 retry_mode++;
1589 ret = nand_setup_read_retry(mtd,
1590 retry_mode);
1591 if (ret < 0)
1592 break;
1593
1594 /* Reset failures; retry */
1595 mtd->ecc_stats.failed = ecc_failures;
1596 goto read_retry;
1597 } else {
1598 /* No more retry modes; real failure */
1599 ecc_fail = true;
1600 }
1601 }
1602
1603 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001604 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001605 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001606 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001607 max_bitflips = max_t(unsigned int, max_bitflips,
1608 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001611 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001612
Brian Norrisba84fb52014-01-03 15:13:33 -08001613 /* Reset to retry mode 0 */
1614 if (retry_mode) {
1615 ret = nand_setup_read_retry(mtd, 0);
1616 if (ret < 0)
1617 break;
1618 retry_mode = 0;
1619 }
1620
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001621 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Brian Norris8b6e50c2011-05-25 14:59:01 -07001624 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 col = 0;
1626 /* Increment page address */
1627 realpage++;
1628
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001629 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /* Check, if we cross a chip boundary */
1631 if (!page) {
1632 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001633 chip->select_chip(mtd, -1);
1634 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001637 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001639 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001640 if (oob)
1641 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Mike Dunn3f91e942012-04-25 12:06:09 -07001643 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001644 return ret;
1645
Brian Norrisb72f3df2013-12-03 11:04:14 -08001646 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001647 return -EBADMSG;
1648
Mike Dunnedbc45402012-04-25 12:06:11 -07001649 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001650}
1651
1652/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001653 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001654 * @mtd: MTD device structure
1655 * @from: offset to read from
1656 * @len: number of bytes to read
1657 * @retlen: pointer to variable to store the number of read bytes
1658 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001659 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001660 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001661 */
1662static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1663 size_t *retlen, uint8_t *buf)
1664{
Brian Norris4a89ff82011-08-30 18:45:45 -07001665 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001666 int ret;
1667
Huang Shijie6a8214a2012-11-19 14:43:30 +08001668 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001669 ops.len = len;
1670 ops.datbuf = buf;
1671 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001672 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001673 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001674 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001675 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001680 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001681 * @mtd: mtd info structure
1682 * @chip: nand chip info structure
1683 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001684 */
1685static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001686 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001687{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001688 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001689 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001690 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001691}
1692
1693/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001694 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001695 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001696 * @mtd: mtd info structure
1697 * @chip: nand chip info structure
1698 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001699 */
1700static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001701 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001702{
1703 uint8_t *buf = chip->oob_poi;
1704 int length = mtd->oobsize;
1705 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1706 int eccsize = chip->ecc.size;
1707 uint8_t *bufpoi = buf;
1708 int i, toread, sndrnd = 0, pos;
1709
1710 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1711 for (i = 0; i < chip->ecc.steps; i++) {
1712 if (sndrnd) {
1713 pos = eccsize + i * (eccsize + chunk);
1714 if (mtd->writesize > 512)
1715 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1716 else
1717 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1718 } else
1719 sndrnd = 1;
1720 toread = min_t(int, length, chunk);
1721 chip->read_buf(mtd, bufpoi, toread);
1722 bufpoi += toread;
1723 length -= toread;
1724 }
1725 if (length > 0)
1726 chip->read_buf(mtd, bufpoi, length);
1727
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001728 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001729}
1730
1731/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001732 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001733 * @mtd: mtd info structure
1734 * @chip: nand chip info structure
1735 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001736 */
1737static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1738 int page)
1739{
1740 int status = 0;
1741 const uint8_t *buf = chip->oob_poi;
1742 int length = mtd->oobsize;
1743
1744 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1745 chip->write_buf(mtd, buf, length);
1746 /* Send command to program the OOB data */
1747 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1748
1749 status = chip->waitfunc(mtd, chip);
1750
Savin Zlobec0d420f92006-06-21 11:51:20 +02001751 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001752}
1753
1754/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001755 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001756 * with syndrome - only for large page flash
1757 * @mtd: mtd info structure
1758 * @chip: nand chip info structure
1759 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001760 */
1761static int nand_write_oob_syndrome(struct mtd_info *mtd,
1762 struct nand_chip *chip, int page)
1763{
1764 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1765 int eccsize = chip->ecc.size, length = mtd->oobsize;
1766 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1767 const uint8_t *bufpoi = chip->oob_poi;
1768
1769 /*
1770 * data-ecc-data-ecc ... ecc-oob
1771 * or
1772 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1773 */
1774 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1775 pos = steps * (eccsize + chunk);
1776 steps = 0;
1777 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001778 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001779
1780 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1781 for (i = 0; i < steps; i++) {
1782 if (sndcmd) {
1783 if (mtd->writesize <= 512) {
1784 uint32_t fill = 0xFFFFFFFF;
1785
1786 len = eccsize;
1787 while (len > 0) {
1788 int num = min_t(int, len, 4);
1789 chip->write_buf(mtd, (uint8_t *)&fill,
1790 num);
1791 len -= num;
1792 }
1793 } else {
1794 pos = eccsize + i * (eccsize + chunk);
1795 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1796 }
1797 } else
1798 sndcmd = 1;
1799 len = min_t(int, length, chunk);
1800 chip->write_buf(mtd, bufpoi, len);
1801 bufpoi += len;
1802 length -= len;
1803 }
1804 if (length > 0)
1805 chip->write_buf(mtd, bufpoi, length);
1806
1807 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1808 status = chip->waitfunc(mtd, chip);
1809
1810 return status & NAND_STATUS_FAIL ? -EIO : 0;
1811}
1812
1813/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001814 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001815 * @mtd: MTD device structure
1816 * @from: offset to read from
1817 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001819 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001821static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1822 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Brian Norrisc00a0992012-05-01 17:12:54 -07001824 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001825 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001826 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001827 int readlen = ops->ooblen;
1828 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001829 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001830 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Brian Norris289c0522011-07-19 10:06:09 -07001832 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301833 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Brian Norris041e4572011-06-23 16:45:24 -07001835 stats = mtd->ecc_stats;
1836
Brian Norris0612b9d2011-08-30 18:45:40 -07001837 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001838 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001839 else
1840 len = mtd->oobsize;
1841
1842 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001843 pr_debug("%s: attempt to start read outside oob\n",
1844 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001845 return -EINVAL;
1846 }
1847
1848 /* Do not allow reads past end of device */
1849 if (unlikely(from >= mtd->size ||
1850 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1851 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001852 pr_debug("%s: attempt to read beyond end of device\n",
1853 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001854 return -EINVAL;
1855 }
Vitaly Wool70145682006-11-03 18:20:38 +03001856
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001857 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001858 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001860 /* Shift to get page */
1861 realpage = (int)(from >> chip->page_shift);
1862 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Florian Fainellif8ac0412010-09-07 13:23:43 +02001864 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001865 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001866 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001867 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001868 ret = chip->ecc.read_oob(mtd, chip, page);
1869
1870 if (ret < 0)
1871 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001872
1873 len = min(len, readlen);
1874 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875
Brian Norris5bc7c332013-03-13 09:51:31 -07001876 if (chip->options & NAND_NEED_READRDY) {
1877 /* Apply delay or wait for ready/busy pin */
1878 if (!chip->dev_ready)
1879 udelay(chip->chip_delay);
1880 else
1881 nand_wait_ready(mtd);
1882 }
1883
Vitaly Wool70145682006-11-03 18:20:38 +03001884 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001885 if (!readlen)
1886 break;
1887
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001888 /* Increment page address */
1889 realpage++;
1890
1891 page = realpage & chip->pagemask;
1892 /* Check, if we cross a chip boundary */
1893 if (!page) {
1894 chipnr++;
1895 chip->select_chip(mtd, -1);
1896 chip->select_chip(mtd, chipnr);
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001899 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001901 ops->oobretlen = ops->ooblen - readlen;
1902
1903 if (ret < 0)
1904 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001905
1906 if (mtd->ecc_stats.failed - stats.failed)
1907 return -EBADMSG;
1908
1909 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
1912/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001913 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001914 * @mtd: MTD device structure
1915 * @from: offset to read from
1916 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001918 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001920static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1921 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923 int ret = -ENOTSUPP;
1924
1925 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001928 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001929 pr_debug("%s: attempt to read beyond end of device\n",
1930 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return -EINVAL;
1932 }
1933
Huang Shijie6a8214a2012-11-19 14:43:30 +08001934 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Florian Fainellif8ac0412010-09-07 13:23:43 +02001936 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001937 case MTD_OPS_PLACE_OOB:
1938 case MTD_OPS_AUTO_OOB:
1939 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001940 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001941
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001942 default:
1943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946 if (!ops->datbuf)
1947 ret = nand_do_read_oob(mtd, from, ops);
1948 else
1949 ret = nand_do_read_ops(mtd, from, ops);
1950
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001951out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001953 return ret;
1954}
1955
1956
1957/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001958 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001959 * @mtd: mtd info structure
1960 * @chip: nand chip info structure
1961 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001962 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001963 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001964 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001965 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001966static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001967 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968{
1969 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001970 if (oob_required)
1971 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001972
1973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001976/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001977 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001978 * @mtd: mtd info structure
1979 * @chip: nand chip info structure
1980 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001981 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001982 *
1983 * We need a special oob layout and handling even when ECC isn't checked.
1984 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001985static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001986 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001987 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001988{
1989 int eccsize = chip->ecc.size;
1990 int eccbytes = chip->ecc.bytes;
1991 uint8_t *oob = chip->oob_poi;
1992 int steps, size;
1993
1994 for (steps = chip->ecc.steps; steps > 0; steps--) {
1995 chip->write_buf(mtd, buf, eccsize);
1996 buf += eccsize;
1997
1998 if (chip->ecc.prepad) {
1999 chip->write_buf(mtd, oob, chip->ecc.prepad);
2000 oob += chip->ecc.prepad;
2001 }
2002
2003 chip->read_buf(mtd, oob, eccbytes);
2004 oob += eccbytes;
2005
2006 if (chip->ecc.postpad) {
2007 chip->write_buf(mtd, oob, chip->ecc.postpad);
2008 oob += chip->ecc.postpad;
2009 }
2010 }
2011
2012 size = mtd->oobsize - (oob - chip->oob_poi);
2013 if (size)
2014 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002015
2016 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002017}
2018/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002019 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002020 * @mtd: mtd info structure
2021 * @chip: nand chip info structure
2022 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002023 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002024 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002025static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002026 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002027{
2028 int i, eccsize = chip->ecc.size;
2029 int eccbytes = chip->ecc.bytes;
2030 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002031 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002032 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002033 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002034
Brian Norris7854d3f2011-06-23 14:12:08 -07002035 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002036 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2037 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002038
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002039 for (i = 0; i < chip->ecc.total; i++)
2040 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002041
Josh Wufdbad98d2012-06-25 18:07:45 +08002042 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002043}
2044
2045/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002046 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002047 * @mtd: mtd info structure
2048 * @chip: nand chip info structure
2049 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002050 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002051 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002052static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002053 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002054{
2055 int i, eccsize = chip->ecc.size;
2056 int eccbytes = chip->ecc.bytes;
2057 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002058 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002059 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002060 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002061
2062 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2063 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002064 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002065 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2066 }
2067
2068 for (i = 0; i < chip->ecc.total; i++)
2069 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2070
2071 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002072
2073 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002074}
2075
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302076
2077/**
2078 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2079 * @mtd: mtd info structure
2080 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002081 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302082 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002083 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302084 * @oob_required: must write chip->oob_poi to OOB
2085 */
2086static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2087 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002088 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302089 int oob_required)
2090{
2091 uint8_t *oob_buf = chip->oob_poi;
2092 uint8_t *ecc_calc = chip->buffers->ecccalc;
2093 int ecc_size = chip->ecc.size;
2094 int ecc_bytes = chip->ecc.bytes;
2095 int ecc_steps = chip->ecc.steps;
2096 uint32_t *eccpos = chip->ecc.layout->eccpos;
2097 uint32_t start_step = offset / ecc_size;
2098 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2099 int oob_bytes = mtd->oobsize / ecc_steps;
2100 int step, i;
2101
2102 for (step = 0; step < ecc_steps; step++) {
2103 /* configure controller for WRITE access */
2104 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2105
2106 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002107 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302108
2109 /* mask ECC of un-touched subpages by padding 0xFF */
2110 if ((step < start_step) || (step > end_step))
2111 memset(ecc_calc, 0xff, ecc_bytes);
2112 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002113 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302114
2115 /* mask OOB of un-touched subpages by padding 0xFF */
2116 /* if oob_required, preserve OOB metadata of written subpage */
2117 if (!oob_required || (step < start_step) || (step > end_step))
2118 memset(oob_buf, 0xff, oob_bytes);
2119
Brian Norrisd6a950802013-08-08 17:16:36 -07002120 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302121 ecc_calc += ecc_bytes;
2122 oob_buf += oob_bytes;
2123 }
2124
2125 /* copy calculated ECC for whole page to chip->buffer->oob */
2126 /* this include masked-value(0xFF) for unwritten subpages */
2127 ecc_calc = chip->buffers->ecccalc;
2128 for (i = 0; i < chip->ecc.total; i++)
2129 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2130
2131 /* write OOB buffer to NAND device */
2132 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2133
2134 return 0;
2135}
2136
2137
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002138/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002139 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002140 * @mtd: mtd info structure
2141 * @chip: nand chip info structure
2142 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002143 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002144 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002145 * The hw generator calculates the error syndrome automatically. Therefore we
2146 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002147 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002148static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002149 struct nand_chip *chip,
2150 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002151{
2152 int i, eccsize = chip->ecc.size;
2153 int eccbytes = chip->ecc.bytes;
2154 int eccsteps = chip->ecc.steps;
2155 const uint8_t *p = buf;
2156 uint8_t *oob = chip->oob_poi;
2157
2158 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2159
2160 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2161 chip->write_buf(mtd, p, eccsize);
2162
2163 if (chip->ecc.prepad) {
2164 chip->write_buf(mtd, oob, chip->ecc.prepad);
2165 oob += chip->ecc.prepad;
2166 }
2167
2168 chip->ecc.calculate(mtd, p, oob);
2169 chip->write_buf(mtd, oob, eccbytes);
2170 oob += eccbytes;
2171
2172 if (chip->ecc.postpad) {
2173 chip->write_buf(mtd, oob, chip->ecc.postpad);
2174 oob += chip->ecc.postpad;
2175 }
2176 }
2177
2178 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002179 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002180 if (i)
2181 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002182
2183 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002184}
2185
2186/**
David Woodhouse956e9442006-09-25 17:12:39 +01002187 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002188 * @mtd: MTD device structure
2189 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302190 * @offset: address offset within the page
2191 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002192 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002193 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002194 * @page: page number to write
2195 * @cached: cached programming
2196 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002197 */
2198static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302199 uint32_t offset, int data_len, const uint8_t *buf,
2200 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002201{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302202 int status, subpage;
2203
2204 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2205 chip->ecc.write_subpage)
2206 subpage = offset || (data_len < mtd->writesize);
2207 else
2208 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002209
2210 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2211
David Woodhouse956e9442006-09-25 17:12:39 +01002212 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302213 status = chip->ecc.write_page_raw(mtd, chip, buf,
2214 oob_required);
2215 else if (subpage)
2216 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2217 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002218 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002219 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2220
2221 if (status < 0)
2222 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002223
2224 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002225 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002226 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002227 */
2228 cached = 0;
2229
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002230 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002231
2232 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002233 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002234 /*
2235 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002236 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237 */
2238 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2239 status = chip->errstat(mtd, chip, FL_WRITING, status,
2240 page);
2241
2242 if (status & NAND_STATUS_FAIL)
2243 return -EIO;
2244 } else {
2245 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002246 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002247 }
2248
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249 return 0;
2250}
2251
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002252/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002253 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002254 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002255 * @oob: oob data buffer
2256 * @len: oob data write length
2257 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002258 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002259static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2260 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002261{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002262 struct nand_chip *chip = mtd->priv;
2263
2264 /*
2265 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2266 * data from a previous OOB read.
2267 */
2268 memset(chip->oob_poi, 0xff, mtd->oobsize);
2269
Florian Fainellif8ac0412010-09-07 13:23:43 +02002270 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002271
Brian Norris0612b9d2011-08-30 18:45:40 -07002272 case MTD_OPS_PLACE_OOB:
2273 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002274 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2275 return oob + len;
2276
Brian Norris0612b9d2011-08-30 18:45:40 -07002277 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002278 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002279 uint32_t boffs = 0, woffs = ops->ooboffs;
2280 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002281
Florian Fainellif8ac0412010-09-07 13:23:43 +02002282 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002283 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002284 if (unlikely(woffs)) {
2285 if (woffs >= free->length) {
2286 woffs -= free->length;
2287 continue;
2288 }
2289 boffs = free->offset + woffs;
2290 bytes = min_t(size_t, len,
2291 (free->length - woffs));
2292 woffs = 0;
2293 } else {
2294 bytes = min_t(size_t, len, free->length);
2295 boffs = free->offset;
2296 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002297 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002298 oob += bytes;
2299 }
2300 return oob;
2301 }
2302 default:
2303 BUG();
2304 }
2305 return NULL;
2306}
2307
Florian Fainellif8ac0412010-09-07 13:23:43 +02002308#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309
2310/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002311 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002312 * @mtd: MTD device structure
2313 * @to: offset to write to
2314 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002315 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002316 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002317 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002318static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2319 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002320{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002321 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002322 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002323 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002324
2325 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002326 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002327 mtd->oobavail : mtd->oobsize;
2328
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002329 uint8_t *oob = ops->oobbuf;
2330 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302331 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002332 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002333
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002334 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002335 if (!writelen)
2336 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002337
Brian Norris8b6e50c2011-05-25 14:59:01 -07002338 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002339 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002340 pr_notice("%s: attempt to write non page aligned data\n",
2341 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002342 return -EINVAL;
2343 }
2344
Thomas Gleixner29072b92006-09-28 15:38:36 +02002345 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002346
Thomas Gleixner6a930962006-06-28 00:11:45 +02002347 chipnr = (int)(to >> chip->chip_shift);
2348 chip->select_chip(mtd, chipnr);
2349
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002350 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002351 if (nand_check_wp(mtd)) {
2352 ret = -EIO;
2353 goto err_out;
2354 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002355
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002356 realpage = (int)(to >> chip->page_shift);
2357 page = realpage & chip->pagemask;
2358 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2359
2360 /* Invalidate the page cache, when we write to the cached page */
2361 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002362 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002363 chip->pagebuf = -1;
2364
Maxim Levitsky782ce792010-02-22 20:39:36 +02002365 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002366 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2367 ret = -EINVAL;
2368 goto err_out;
2369 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002370
Florian Fainellif8ac0412010-09-07 13:23:43 +02002371 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002372 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002373 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002374 uint8_t *wbuf = buf;
2375
Brian Norris8b6e50c2011-05-25 14:59:01 -07002376 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002377 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2378 cached = 0;
2379 bytes = min_t(int, bytes - column, (int) writelen);
2380 chip->pagebuf = -1;
2381 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2382 memcpy(&chip->buffers->databuf[column], buf, bytes);
2383 wbuf = chip->buffers->databuf;
2384 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002385
Maxim Levitsky782ce792010-02-22 20:39:36 +02002386 if (unlikely(oob)) {
2387 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002388 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002389 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002390 } else {
2391 /* We still need to erase leftover OOB data */
2392 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002393 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302394 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2395 oob_required, page, cached,
2396 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002397 if (ret)
2398 break;
2399
2400 writelen -= bytes;
2401 if (!writelen)
2402 break;
2403
Thomas Gleixner29072b92006-09-28 15:38:36 +02002404 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002405 buf += bytes;
2406 realpage++;
2407
2408 page = realpage & chip->pagemask;
2409 /* Check, if we cross a chip boundary */
2410 if (!page) {
2411 chipnr++;
2412 chip->select_chip(mtd, -1);
2413 chip->select_chip(mtd, chipnr);
2414 }
2415 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002416
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002417 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002418 if (unlikely(oob))
2419 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002420
2421err_out:
2422 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002423 return ret;
2424}
2425
2426/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002427 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002428 * @mtd: MTD device structure
2429 * @to: offset to write to
2430 * @len: number of bytes to write
2431 * @retlen: pointer to variable to store the number of written bytes
2432 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002433 *
2434 * NAND write with ECC. Used when performing writes in interrupt context, this
2435 * may for example be called by mtdoops when writing an oops while in panic.
2436 */
2437static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2438 size_t *retlen, const uint8_t *buf)
2439{
2440 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002441 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002442 int ret;
2443
Brian Norris8b6e50c2011-05-25 14:59:01 -07002444 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002445 panic_nand_wait(mtd, chip, 400);
2446
Brian Norris8b6e50c2011-05-25 14:59:01 -07002447 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002448 panic_nand_get_device(chip, mtd, FL_WRITING);
2449
Brian Norris4a89ff82011-08-30 18:45:45 -07002450 ops.len = len;
2451 ops.datbuf = (uint8_t *)buf;
2452 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002453 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002454
Brian Norris4a89ff82011-08-30 18:45:45 -07002455 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002456
Brian Norris4a89ff82011-08-30 18:45:45 -07002457 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002458 return ret;
2459}
2460
2461/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002462 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002463 * @mtd: MTD device structure
2464 * @to: offset to write to
2465 * @len: number of bytes to write
2466 * @retlen: pointer to variable to store the number of written bytes
2467 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002469 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002471static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002472 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Brian Norris4a89ff82011-08-30 18:45:45 -07002474 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002475 int ret;
2476
Huang Shijie6a8214a2012-11-19 14:43:30 +08002477 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002478 ops.len = len;
2479 ops.datbuf = (uint8_t *)buf;
2480 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002481 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002482 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002483 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002484 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002485 return ret;
2486}
2487
2488/**
2489 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002490 * @mtd: MTD device structure
2491 * @to: offset to write to
2492 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002493 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002494 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002495 */
2496static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2497 struct mtd_oob_ops *ops)
2498{
Adrian Hunter03736152007-01-31 17:58:29 +02002499 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002500 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Brian Norris289c0522011-07-19 10:06:09 -07002502 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302503 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Brian Norris0612b9d2011-08-30 18:45:40 -07002505 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002506 len = chip->ecc.layout->oobavail;
2507 else
2508 len = mtd->oobsize;
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002511 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002512 pr_debug("%s: attempt to write past end of page\n",
2513 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return -EINVAL;
2515 }
2516
Adrian Hunter03736152007-01-31 17:58:29 +02002517 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002518 pr_debug("%s: attempt to start write outside oob\n",
2519 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002520 return -EINVAL;
2521 }
2522
Jason Liu775adc32011-02-25 13:06:18 +08002523 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002524 if (unlikely(to >= mtd->size ||
2525 ops->ooboffs + ops->ooblen >
2526 ((mtd->size >> chip->page_shift) -
2527 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002528 pr_debug("%s: attempt to write beyond end of device\n",
2529 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002530 return -EINVAL;
2531 }
2532
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002533 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002534 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002536 /* Shift to get page */
2537 page = (int)(to >> chip->page_shift);
2538
2539 /*
2540 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2541 * of my DiskOnChip 2000 test units) will clear the whole data page too
2542 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2543 * it in the doc2000 driver in August 1999. dwmw2.
2544 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002545 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002548 if (nand_check_wp(mtd)) {
2549 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002550 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002551 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002554 if (page == chip->pagebuf)
2555 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002557 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002558
Brian Norris0612b9d2011-08-30 18:45:40 -07002559 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002560 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2561 else
2562 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002563
Huang Shijieb0bb6902012-11-19 14:43:29 +08002564 chip->select_chip(mtd, -1);
2565
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002566 if (status)
2567 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Vitaly Wool70145682006-11-03 18:20:38 +03002569 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002571 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002574/**
2575 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002576 * @mtd: MTD device structure
2577 * @to: offset to write to
2578 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002579 */
2580static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2581 struct mtd_oob_ops *ops)
2582{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002583 int ret = -ENOTSUPP;
2584
2585 ops->retlen = 0;
2586
2587 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002588 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002589 pr_debug("%s: attempt to write beyond end of device\n",
2590 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002591 return -EINVAL;
2592 }
2593
Huang Shijie6a8214a2012-11-19 14:43:30 +08002594 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002595
Florian Fainellif8ac0412010-09-07 13:23:43 +02002596 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002597 case MTD_OPS_PLACE_OOB:
2598 case MTD_OPS_AUTO_OOB:
2599 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002600 break;
2601
2602 default:
2603 goto out;
2604 }
2605
2606 if (!ops->datbuf)
2607 ret = nand_do_write_oob(mtd, to, ops);
2608 else
2609 ret = nand_do_write_ops(mtd, to, ops);
2610
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002611out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002612 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return ret;
2614}
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002617 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002618 * @mtd: MTD device structure
2619 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002621 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002623static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002625 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002627 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2628 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
2631/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002633 * @mtd: MTD device structure
2634 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002636 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002638static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
David Woodhousee0c7d762006-05-13 18:07:53 +01002640 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002644 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002645 * @mtd: MTD device structure
2646 * @instr: erase instruction
2647 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002649 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002651int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2652 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Adrian Hunter69423d92008-12-10 13:37:21 +00002654 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002655 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002656 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Brian Norris289c0522011-07-19 10:06:09 -07002658 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2659 __func__, (unsigned long long)instr->addr,
2660 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302662 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002666 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002669 page = (int)(instr->addr >> chip->page_shift);
2670 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002673 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002676 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 /* Check, if it is write protected */
2679 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002680 pr_debug("%s: device is write protected!\n",
2681 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 instr->state = MTD_ERASE_FAILED;
2683 goto erase_exit;
2684 }
2685
2686 /* Loop through the pages */
2687 len = instr->len;
2688
2689 instr->state = MTD_ERASING;
2690
2691 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002692 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002693 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2694 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002695 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2696 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 instr->state = MTD_ERASE_FAILED;
2698 goto erase_exit;
2699 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002700
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002701 /*
2702 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002703 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002704 */
2705 if (page <= chip->pagebuf && chip->pagebuf <
2706 (page + pages_per_block))
2707 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002709 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002710
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002711 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002713 /*
2714 * See if operation failed and additional status checks are
2715 * available
2716 */
2717 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2718 status = chip->errstat(mtd, chip, FL_ERASING,
2719 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002722 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002723 pr_debug("%s: failed erase, page 0x%08x\n",
2724 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002726 instr->fail_addr =
2727 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 goto erase_exit;
2729 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002732 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 page += pages_per_block;
2734
2735 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002736 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002738 chip->select_chip(mtd, -1);
2739 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 }
2742 instr->state = MTD_ERASE_DONE;
2743
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002744erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002749 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 nand_release_device(mtd);
2751
David Woodhouse49defc02007-10-06 15:01:59 -04002752 /* Do call back function */
2753 if (!ret)
2754 mtd_erase_callback(instr);
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 /* Return more or less happy */
2757 return ret;
2758}
2759
2760/**
2761 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002762 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002764 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002766static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Brian Norris289c0522011-07-19 10:06:09 -07002768 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002771 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002773 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002777 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002778 * @mtd: MTD device structure
2779 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002781static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002787 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002788 * @mtd: MTD device structure
2789 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002791static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 int ret;
2794
Florian Fainellif8ac0412010-09-07 13:23:43 +02002795 ret = nand_block_isbad(mtd, ofs);
2796 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002797 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (ret > 0)
2799 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002800 return ret;
2801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Brian Norris5a0edb22013-07-30 17:52:58 -07002803 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
2806/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002807 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2808 * @mtd: MTD device structure
2809 * @chip: nand chip info structure
2810 * @addr: feature address.
2811 * @subfeature_param: the subfeature parameters, a four bytes array.
2812 */
2813static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2814 int addr, uint8_t *subfeature_param)
2815{
2816 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002817 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002818
David Mosbergerd914c932013-05-29 15:30:13 +03002819 if (!chip->onfi_version ||
2820 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2821 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002822 return -EINVAL;
2823
2824 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002825 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2826 chip->write_byte(mtd, subfeature_param[i]);
2827
Huang Shijie7db03ec2012-09-13 14:57:52 +08002828 status = chip->waitfunc(mtd, chip);
2829 if (status & NAND_STATUS_FAIL)
2830 return -EIO;
2831 return 0;
2832}
2833
2834/**
2835 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2836 * @mtd: MTD device structure
2837 * @chip: nand chip info structure
2838 * @addr: feature address.
2839 * @subfeature_param: the subfeature parameters, a four bytes array.
2840 */
2841static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2842 int addr, uint8_t *subfeature_param)
2843{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002844 int i;
2845
David Mosbergerd914c932013-05-29 15:30:13 +03002846 if (!chip->onfi_version ||
2847 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2848 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002849 return -EINVAL;
2850
2851 /* clear the sub feature parameters */
2852 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2853
2854 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002855 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2856 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002857 return 0;
2858}
2859
2860/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002861 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002862 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002863 */
2864static int nand_suspend(struct mtd_info *mtd)
2865{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002866 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002867}
2868
2869/**
2870 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002871 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002872 */
2873static void nand_resume(struct mtd_info *mtd)
2874{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002875 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002876
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002877 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002878 nand_release_device(mtd);
2879 else
Brian Norrisd0370212011-07-19 10:06:08 -07002880 pr_err("%s called for a chip which is not in suspended state\n",
2881 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002882}
2883
Brian Norris8b6e50c2011-05-25 14:59:01 -07002884/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002885static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002886{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002888 if (!chip->chip_delay)
2889 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002892 if (chip->cmdfunc == NULL)
2893 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002896 if (chip->waitfunc == NULL)
2897 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002899 if (!chip->select_chip)
2900 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002901
Huang Shijie4204ccc2013-08-16 10:10:07 +08002902 /* set for ONFI nand */
2903 if (!chip->onfi_set_features)
2904 chip->onfi_set_features = nand_onfi_set_features;
2905 if (!chip->onfi_get_features)
2906 chip->onfi_get_features = nand_onfi_get_features;
2907
Brian Norris68e80782013-07-18 01:17:02 -07002908 /* If called twice, pointers that depend on busw may need to be reset */
2909 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002910 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2911 if (!chip->read_word)
2912 chip->read_word = nand_read_word;
2913 if (!chip->block_bad)
2914 chip->block_bad = nand_block_bad;
2915 if (!chip->block_markbad)
2916 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002917 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002918 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002919 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2920 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07002921 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002922 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002923 if (!chip->scan_bbt)
2924 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002925
2926 if (!chip->controller) {
2927 chip->controller = &chip->hwcontrol;
2928 spin_lock_init(&chip->controller->lock);
2929 init_waitqueue_head(&chip->controller->wq);
2930 }
2931
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002932}
2933
Brian Norris8b6e50c2011-05-25 14:59:01 -07002934/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002935static void sanitize_string(uint8_t *s, size_t len)
2936{
2937 ssize_t i;
2938
Brian Norris8b6e50c2011-05-25 14:59:01 -07002939 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002940 s[len - 1] = 0;
2941
Brian Norris8b6e50c2011-05-25 14:59:01 -07002942 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002943 for (i = 0; i < len - 1; i++) {
2944 if (s[i] < ' ' || s[i] > 127)
2945 s[i] = '?';
2946 }
2947
Brian Norris8b6e50c2011-05-25 14:59:01 -07002948 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002949 strim(s);
2950}
2951
2952static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2953{
2954 int i;
2955 while (len--) {
2956 crc ^= *p++ << 8;
2957 for (i = 0; i < 8; i++)
2958 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2959 }
2960
2961 return crc;
2962}
2963
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002964/* Parse the Extended Parameter Page. */
2965static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2966 struct nand_chip *chip, struct nand_onfi_params *p)
2967{
2968 struct onfi_ext_param_page *ep;
2969 struct onfi_ext_section *s;
2970 struct onfi_ext_ecc_info *ecc;
2971 uint8_t *cursor;
2972 int ret = -EINVAL;
2973 int len;
2974 int i;
2975
2976 len = le16_to_cpu(p->ext_param_page_length) * 16;
2977 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07002978 if (!ep)
2979 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002980
2981 /* Send our own NAND_CMD_PARAM. */
2982 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2983
2984 /* Use the Change Read Column command to skip the ONFI param pages. */
2985 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
2986 sizeof(*p) * p->num_of_param_pages , -1);
2987
2988 /* Read out the Extended Parameter Page. */
2989 chip->read_buf(mtd, (uint8_t *)ep, len);
2990 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
2991 != le16_to_cpu(ep->crc))) {
2992 pr_debug("fail in the CRC.\n");
2993 goto ext_out;
2994 }
2995
2996 /*
2997 * Check the signature.
2998 * Do not strictly follow the ONFI spec, maybe changed in future.
2999 */
3000 if (strncmp(ep->sig, "EPPS", 4)) {
3001 pr_debug("The signature is invalid.\n");
3002 goto ext_out;
3003 }
3004
3005 /* find the ECC section. */
3006 cursor = (uint8_t *)(ep + 1);
3007 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3008 s = ep->sections + i;
3009 if (s->type == ONFI_SECTION_TYPE_2)
3010 break;
3011 cursor += s->length * 16;
3012 }
3013 if (i == ONFI_EXT_SECTION_MAX) {
3014 pr_debug("We can not find the ECC section.\n");
3015 goto ext_out;
3016 }
3017
3018 /* get the info we want. */
3019 ecc = (struct onfi_ext_ecc_info *)cursor;
3020
Brian Norris4ae7d222013-09-16 18:20:21 -07003021 if (!ecc->codeword_size) {
3022 pr_debug("Invalid codeword size\n");
3023 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003024 }
3025
Brian Norris4ae7d222013-09-16 18:20:21 -07003026 chip->ecc_strength_ds = ecc->ecc_bits;
3027 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003028 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003029
3030ext_out:
3031 kfree(ep);
3032 return ret;
3033}
3034
Brian Norris8429bb32013-12-03 15:51:09 -08003035static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3036{
3037 struct nand_chip *chip = mtd->priv;
3038 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3039
3040 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3041 feature);
3042}
3043
3044/*
3045 * Configure chip properties from Micron vendor-specific ONFI table
3046 */
3047static void nand_onfi_detect_micron(struct nand_chip *chip,
3048 struct nand_onfi_params *p)
3049{
3050 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3051
3052 if (le16_to_cpu(p->vendor_revision) < 1)
3053 return;
3054
3055 chip->read_retries = micron->read_retry_options;
3056 chip->setup_read_retry = nand_setup_read_retry_micron;
3057}
3058
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003059/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003060 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003061 */
3062static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003063 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003064{
3065 struct nand_onfi_params *p = &chip->onfi_params;
3066 int i;
3067 int val;
3068
Brian Norris7854d3f2011-06-23 14:12:08 -07003069 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003070 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3071 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3072 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3073 return 0;
3074
Brian Norrisc7f23a72013-08-13 10:51:55 -07003075 /*
3076 * ONFI must be probed in 8-bit mode or with NAND_BUSWIDTH_AUTO, not
3077 * with NAND_BUSWIDTH_16
3078 */
3079 if (chip->options & NAND_BUSWIDTH_16) {
3080 pr_err("ONFI cannot be probed in 16-bit mode; aborting\n");
3081 return 0;
3082 }
3083
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003084 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3085 for (i = 0; i < 3; i++) {
3086 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
3087 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3088 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003089 break;
3090 }
3091 }
3092
Brian Norrisc7f23a72013-08-13 10:51:55 -07003093 if (i == 3) {
3094 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003095 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003096 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003097
Brian Norris8b6e50c2011-05-25 14:59:01 -07003098 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003099 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003100 if (val & (1 << 5))
3101 chip->onfi_version = 23;
3102 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003103 chip->onfi_version = 22;
3104 else if (val & (1 << 3))
3105 chip->onfi_version = 21;
3106 else if (val & (1 << 2))
3107 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003108 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003109 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003110
3111 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003112 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003113 return 0;
3114 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003115
3116 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3117 sanitize_string(p->model, sizeof(p->model));
3118 if (!mtd->name)
3119 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003120
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003121 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003122
3123 /*
3124 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3125 * (don't ask me who thought of this...). MTD assumes that these
3126 * dimensions will be power-of-2, so just truncate the remaining area.
3127 */
3128 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3129 mtd->erasesize *= mtd->writesize;
3130
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003131 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003132
3133 /* See erasesize comment */
3134 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003135 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003136 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003137
3138 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003139 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003140 else
3141 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003142
Huang Shijie10c86ba2013-05-17 11:17:26 +08003143 if (p->ecc_bits != 0xff) {
3144 chip->ecc_strength_ds = p->ecc_bits;
3145 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003146 } else if (chip->onfi_version >= 21 &&
3147 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3148
3149 /*
3150 * The nand_flash_detect_ext_param_page() uses the
3151 * Change Read Column command which maybe not supported
3152 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3153 * now. We do not replace user supplied command function.
3154 */
3155 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3156 chip->cmdfunc = nand_command_lp;
3157
3158 /* The Extended Parameter Page is supported since ONFI 2.1. */
3159 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003160 pr_warn("Failed to detect ONFI extended param page\n");
3161 } else {
3162 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003163 }
3164
Brian Norris8429bb32013-12-03 15:51:09 -08003165 if (p->jedec_id == NAND_MFR_MICRON)
3166 nand_onfi_detect_micron(chip, p);
3167
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003168 return 1;
3169}
3170
3171/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003172 * nand_id_has_period - Check if an ID string has a given wraparound period
3173 * @id_data: the ID string
3174 * @arrlen: the length of the @id_data array
3175 * @period: the period of repitition
3176 *
3177 * Check if an ID string is repeated within a given sequence of bytes at
3178 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003179 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003180 * if the repetition has a period of @period; otherwise, returns zero.
3181 */
3182static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3183{
3184 int i, j;
3185 for (i = 0; i < period; i++)
3186 for (j = i + period; j < arrlen; j += period)
3187 if (id_data[i] != id_data[j])
3188 return 0;
3189 return 1;
3190}
3191
3192/*
3193 * nand_id_len - Get the length of an ID string returned by CMD_READID
3194 * @id_data: the ID string
3195 * @arrlen: the length of the @id_data array
3196
3197 * Returns the length of the ID string, according to known wraparound/trailing
3198 * zero patterns. If no pattern exists, returns the length of the array.
3199 */
3200static int nand_id_len(u8 *id_data, int arrlen)
3201{
3202 int last_nonzero, period;
3203
3204 /* Find last non-zero byte */
3205 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3206 if (id_data[last_nonzero])
3207 break;
3208
3209 /* All zeros */
3210 if (last_nonzero < 0)
3211 return 0;
3212
3213 /* Calculate wraparound period */
3214 for (period = 1; period < arrlen; period++)
3215 if (nand_id_has_period(id_data, arrlen, period))
3216 break;
3217
3218 /* There's a repeated pattern */
3219 if (period < arrlen)
3220 return period;
3221
3222 /* There are trailing zeros */
3223 if (last_nonzero < arrlen - 1)
3224 return last_nonzero + 1;
3225
3226 /* No pattern detected */
3227 return arrlen;
3228}
3229
Huang Shijie7db906b2013-09-25 14:58:11 +08003230/* Extract the bits of per cell from the 3rd byte of the extended ID */
3231static int nand_get_bits_per_cell(u8 cellinfo)
3232{
3233 int bits;
3234
3235 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3236 bits >>= NAND_CI_CELLTYPE_SHIFT;
3237 return bits + 1;
3238}
3239
Brian Norrise3b88bd2012-09-24 20:40:52 -07003240/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003241 * Many new NAND share similar device ID codes, which represent the size of the
3242 * chip. The rest of the parameters must be decoded according to generic or
3243 * manufacturer-specific "extended ID" decoding patterns.
3244 */
3245static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3246 u8 id_data[8], int *busw)
3247{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003248 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003249 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003250 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003251 /* The 4th id byte is the important one */
3252 extid = id_data[3];
3253
Brian Norrise3b88bd2012-09-24 20:40:52 -07003254 id_len = nand_id_len(id_data, 8);
3255
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003256 /*
3257 * Field definitions are in the following datasheets:
3258 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003259 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003260 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003261 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003262 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3263 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003264 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003265 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003266 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003267 /* Calc pagesize */
3268 mtd->writesize = 2048 << (extid & 0x03);
3269 extid >>= 2;
3270 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003271 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003272 case 1:
3273 mtd->oobsize = 128;
3274 break;
3275 case 2:
3276 mtd->oobsize = 218;
3277 break;
3278 case 3:
3279 mtd->oobsize = 400;
3280 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003281 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003282 mtd->oobsize = 436;
3283 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003284 case 5:
3285 mtd->oobsize = 512;
3286 break;
3287 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003288 mtd->oobsize = 640;
3289 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003290 case 7:
3291 default: /* Other cases are "reserved" (unknown) */
3292 mtd->oobsize = 1024;
3293 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003294 }
3295 extid >>= 2;
3296 /* Calc blocksize */
3297 mtd->erasesize = (128 * 1024) <<
3298 (((extid >> 1) & 0x04) | (extid & 0x03));
3299 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003300 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003301 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003302 unsigned int tmp;
3303
3304 /* Calc pagesize */
3305 mtd->writesize = 2048 << (extid & 0x03);
3306 extid >>= 2;
3307 /* Calc oobsize */
3308 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3309 case 0:
3310 mtd->oobsize = 128;
3311 break;
3312 case 1:
3313 mtd->oobsize = 224;
3314 break;
3315 case 2:
3316 mtd->oobsize = 448;
3317 break;
3318 case 3:
3319 mtd->oobsize = 64;
3320 break;
3321 case 4:
3322 mtd->oobsize = 32;
3323 break;
3324 case 5:
3325 mtd->oobsize = 16;
3326 break;
3327 default:
3328 mtd->oobsize = 640;
3329 break;
3330 }
3331 extid >>= 2;
3332 /* Calc blocksize */
3333 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3334 if (tmp < 0x03)
3335 mtd->erasesize = (128 * 1024) << tmp;
3336 else if (tmp == 0x03)
3337 mtd->erasesize = 768 * 1024;
3338 else
3339 mtd->erasesize = (64 * 1024) << tmp;
3340 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003341 } else {
3342 /* Calc pagesize */
3343 mtd->writesize = 1024 << (extid & 0x03);
3344 extid >>= 2;
3345 /* Calc oobsize */
3346 mtd->oobsize = (8 << (extid & 0x01)) *
3347 (mtd->writesize >> 9);
3348 extid >>= 2;
3349 /* Calc blocksize. Blocksize is multiples of 64KiB */
3350 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3351 extid >>= 2;
3352 /* Get buswidth information */
3353 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003354
3355 /*
3356 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3357 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3358 * follows:
3359 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3360 * 110b -> 24nm
3361 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3362 */
3363 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003364 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003365 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3366 !(id_data[4] & 0x80) /* !BENAND */) {
3367 mtd->oobsize = 32 * mtd->writesize >> 9;
3368 }
3369
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003370 }
3371}
3372
3373/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003374 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3375 * decodes a matching ID table entry and assigns the MTD size parameters for
3376 * the chip.
3377 */
3378static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3379 struct nand_flash_dev *type, u8 id_data[8],
3380 int *busw)
3381{
3382 int maf_id = id_data[0];
3383
3384 mtd->erasesize = type->erasesize;
3385 mtd->writesize = type->pagesize;
3386 mtd->oobsize = mtd->writesize / 32;
3387 *busw = type->options & NAND_BUSWIDTH_16;
3388
Huang Shijie1c195e92013-09-25 14:58:12 +08003389 /* All legacy ID NAND are small-page, SLC */
3390 chip->bits_per_cell = 1;
3391
Brian Norrisf23a4812012-09-24 20:40:51 -07003392 /*
3393 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3394 * some Spansion chips have erasesize that conflicts with size
3395 * listed in nand_ids table.
3396 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3397 */
3398 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3399 && id_data[6] == 0x00 && id_data[7] == 0x00
3400 && mtd->writesize == 512) {
3401 mtd->erasesize = 128 * 1024;
3402 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3403 }
3404}
3405
3406/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003407 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3408 * heuristic patterns using various detected parameters (e.g., manufacturer,
3409 * page size, cell-type information).
3410 */
3411static void nand_decode_bbm_options(struct mtd_info *mtd,
3412 struct nand_chip *chip, u8 id_data[8])
3413{
3414 int maf_id = id_data[0];
3415
3416 /* Set the bad block position */
3417 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3418 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3419 else
3420 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3421
3422 /*
3423 * Bad block marker is stored in the last page of each block on Samsung
3424 * and Hynix MLC devices; stored in first two pages of each block on
3425 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3426 * AMD/Spansion, and Macronix. All others scan only the first page.
3427 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003428 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003429 (maf_id == NAND_MFR_SAMSUNG ||
3430 maf_id == NAND_MFR_HYNIX))
3431 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003432 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003433 (maf_id == NAND_MFR_SAMSUNG ||
3434 maf_id == NAND_MFR_HYNIX ||
3435 maf_id == NAND_MFR_TOSHIBA ||
3436 maf_id == NAND_MFR_AMD ||
3437 maf_id == NAND_MFR_MACRONIX)) ||
3438 (mtd->writesize == 2048 &&
3439 maf_id == NAND_MFR_MICRON))
3440 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3441}
3442
Huang Shijieec6e87e2013-03-15 11:01:00 +08003443static inline bool is_full_id_nand(struct nand_flash_dev *type)
3444{
3445 return type->id_len;
3446}
3447
3448static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3449 struct nand_flash_dev *type, u8 *id_data, int *busw)
3450{
3451 if (!strncmp(type->id, id_data, type->id_len)) {
3452 mtd->writesize = type->pagesize;
3453 mtd->erasesize = type->erasesize;
3454 mtd->oobsize = type->oobsize;
3455
Huang Shijie7db906b2013-09-25 14:58:11 +08003456 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003457 chip->chipsize = (uint64_t)type->chipsize << 20;
3458 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003459 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3460 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003461
3462 *busw = type->options & NAND_BUSWIDTH_16;
3463
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003464 if (!mtd->name)
3465 mtd->name = type->name;
3466
Huang Shijieec6e87e2013-03-15 11:01:00 +08003467 return true;
3468 }
3469 return false;
3470}
3471
Brian Norris7e74c2d2012-09-24 20:40:49 -07003472/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003473 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003474 */
3475static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003476 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003477 int busw,
3478 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003479 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003480{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003481 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003482 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003485 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Karl Beldanef89a882008-09-15 14:37:29 +02003487 /*
3488 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003489 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003490 */
3491 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003494 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003497 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003498 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Brian Norris8b6e50c2011-05-25 14:59:01 -07003500 /*
3501 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003502 * interface concerns can cause random data which looks like a
3503 * possibly credible NAND flash to appear. If the two results do
3504 * not match, ignore the device completely.
3505 */
3506
3507 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3508
Brian Norris4aef9b72012-09-24 20:40:48 -07003509 /* Read entire ID string */
3510 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003511 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003512
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003513 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003514 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003515 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003516 return ERR_PTR(-ENODEV);
3517 }
3518
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003519 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003520 type = nand_flash_ids;
3521
Huang Shijieec6e87e2013-03-15 11:01:00 +08003522 for (; type->name != NULL; type++) {
3523 if (is_full_id_nand(type)) {
3524 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3525 goto ident_done;
3526 } else if (*dev_id == type->dev_id) {
3527 break;
3528 }
3529 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003530
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003531 chip->onfi_version = 0;
3532 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003533 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003534 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003535 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003536 }
3537
David Woodhouse5e81e882010-02-26 18:32:56 +00003538 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003539 return ERR_PTR(-ENODEV);
3540
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003541 if (!mtd->name)
3542 mtd->name = type->name;
3543
Adrian Hunter69423d92008-12-10 13:37:21 +00003544 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003545
Huang Shijie12a40a52010-09-27 10:43:53 +08003546 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003547 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003548 busw = chip->init_size(mtd, chip, id_data);
3549 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003550 /* Decode parameters from extended ID */
3551 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003552 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003553 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003554 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003555 /* Get chip options */
3556 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003557
Brian Norris8b6e50c2011-05-25 14:59:01 -07003558 /*
3559 * Check if chip is not a Samsung device. Do not clear the
3560 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003561 */
3562 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3563 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3564ident_done:
3565
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003566 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003567 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003568 if (nand_manuf_ids[maf_idx].id == *maf_id)
3569 break;
3570 }
3571
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003572 if (chip->options & NAND_BUSWIDTH_AUTO) {
3573 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3574 chip->options |= busw;
3575 nand_set_defaults(chip, busw);
3576 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3577 /*
3578 * Check, if buswidth is correct. Hardware drivers should set
3579 * chip correct!
3580 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003581 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3582 *maf_id, *dev_id);
3583 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3584 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003585 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3586 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003587 return ERR_PTR(-EINVAL);
3588 }
3589
Brian Norris7e74c2d2012-09-24 20:40:49 -07003590 nand_decode_bbm_options(mtd, chip, id_data);
3591
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003592 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003593 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003594 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003595 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003596
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003597 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003598 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003599 if (chip->chipsize & 0xffffffff)
3600 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003601 else {
3602 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3603 chip->chip_shift += 32 - 1;
3604 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003605
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003606 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003607 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003608
Brian Norris8b6e50c2011-05-25 14:59:01 -07003609 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003610 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3611 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003612
Ezequiel Garcia20171642013-11-25 08:30:31 -03003613 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3614 *maf_id, *dev_id);
3615 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
Huang Shijie3723e932013-09-25 14:58:14 +08003616 chip->onfi_version ? chip->onfi_params.model : type->name);
Ezequiel Garcia20171642013-11-25 08:30:31 -03003617 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003618 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3619 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003620 return type;
3621}
3622
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003623/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003624 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003625 * @mtd: MTD device structure
3626 * @maxchips: number of chips to scan for
3627 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003628 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003629 * This is the first phase of the normal nand_scan() function. It reads the
3630 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003631 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003632 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003633 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003634int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3635 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003636{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003637 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003638 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003639 struct nand_flash_dev *type;
3640
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003641 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003642 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003643 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003644 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003645
3646 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003647 type = nand_get_flash_type(mtd, chip, busw,
3648 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003649
3650 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003651 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003652 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003653 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003654 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 }
3656
Huang Shijie07300162012-11-09 16:23:45 +08003657 chip->select_chip(mtd, -1);
3658
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003659 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003660 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003661 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003662 /* See comment in nand_get_flash_type for reset */
3663 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003665 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003667 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003668 nand_dev_id != chip->read_byte(mtd)) {
3669 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 break;
Huang Shijie07300162012-11-09 16:23:45 +08003671 }
3672 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003675 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003678 chip->numchips = i;
3679 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
David Woodhouse3b85c322006-09-25 17:06:53 +01003681 return 0;
3682}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003683EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003684
3685
3686/**
3687 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003688 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003689 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003690 * This is the second phase of the normal nand_scan() function. It fills out
3691 * all the uninitialized function pointers with the defaults and scans for a
3692 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003693 */
3694int nand_scan_tail(struct mtd_info *mtd)
3695{
3696 int i;
3697 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003698 struct nand_ecc_ctrl *ecc = &chip->ecc;
David Woodhouse3b85c322006-09-25 17:06:53 +01003699
Brian Norrise2414f42012-02-06 13:44:00 -08003700 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3701 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3702 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3703
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003704 if (!(chip->options & NAND_OWN_BUFFERS))
3705 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3706 if (!chip->buffers)
3707 return -ENOMEM;
3708
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003709 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003710 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003711
3712 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003713 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003714 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003715 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003716 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003718 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 break;
3720 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003721 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 break;
3723 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003724 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003726 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003727 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003730 pr_warn("No oob scheme defined for oobsize %d\n",
3731 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 BUG();
3733 }
3734 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003735
David Woodhouse956e9442006-09-25 17:12:39 +01003736 if (!chip->write_page)
3737 chip->write_page = nand_write_page;
3738
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003739 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003740 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003741 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003742 */
David Woodhouse956e9442006-09-25 17:12:39 +01003743
Huang Shijie97de79e02013-10-18 14:20:53 +08003744 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003745 case NAND_ECC_HW_OOB_FIRST:
3746 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003747 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003748 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003749 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003750 BUG();
3751 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003752 if (!ecc->read_page)
3753 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003754
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003755 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003756 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003757 if (!ecc->read_page)
3758 ecc->read_page = nand_read_page_hwecc;
3759 if (!ecc->write_page)
3760 ecc->write_page = nand_write_page_hwecc;
3761 if (!ecc->read_page_raw)
3762 ecc->read_page_raw = nand_read_page_raw;
3763 if (!ecc->write_page_raw)
3764 ecc->write_page_raw = nand_write_page_raw;
3765 if (!ecc->read_oob)
3766 ecc->read_oob = nand_read_oob_std;
3767 if (!ecc->write_oob)
3768 ecc->write_oob = nand_write_oob_std;
3769 if (!ecc->read_subpage)
3770 ecc->read_subpage = nand_read_subpage;
3771 if (!ecc->write_subpage)
3772 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003773
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003774 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003775 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3776 (!ecc->read_page ||
3777 ecc->read_page == nand_read_page_hwecc ||
3778 !ecc->write_page ||
3779 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003780 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003781 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003782 BUG();
3783 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003784 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003785 if (!ecc->read_page)
3786 ecc->read_page = nand_read_page_syndrome;
3787 if (!ecc->write_page)
3788 ecc->write_page = nand_write_page_syndrome;
3789 if (!ecc->read_page_raw)
3790 ecc->read_page_raw = nand_read_page_raw_syndrome;
3791 if (!ecc->write_page_raw)
3792 ecc->write_page_raw = nand_write_page_raw_syndrome;
3793 if (!ecc->read_oob)
3794 ecc->read_oob = nand_read_oob_syndrome;
3795 if (!ecc->write_oob)
3796 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003797
Huang Shijie97de79e02013-10-18 14:20:53 +08003798 if (mtd->writesize >= ecc->size) {
3799 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003800 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3801 BUG();
3802 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003803 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003804 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003805 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003806 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003807 ecc->size, mtd->writesize);
3808 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003810 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003811 ecc->calculate = nand_calculate_ecc;
3812 ecc->correct = nand_correct_data;
3813 ecc->read_page = nand_read_page_swecc;
3814 ecc->read_subpage = nand_read_subpage;
3815 ecc->write_page = nand_write_page_swecc;
3816 ecc->read_page_raw = nand_read_page_raw;
3817 ecc->write_page_raw = nand_write_page_raw;
3818 ecc->read_oob = nand_read_oob_std;
3819 ecc->write_oob = nand_write_oob_std;
3820 if (!ecc->size)
3821 ecc->size = 256;
3822 ecc->bytes = 3;
3823 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003825
Ivan Djelic193bd402011-03-11 11:05:33 +01003826 case NAND_ECC_SOFT_BCH:
3827 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003828 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003829 BUG();
3830 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003831 ecc->calculate = nand_bch_calculate_ecc;
3832 ecc->correct = nand_bch_correct_data;
3833 ecc->read_page = nand_read_page_swecc;
3834 ecc->read_subpage = nand_read_subpage;
3835 ecc->write_page = nand_write_page_swecc;
3836 ecc->read_page_raw = nand_read_page_raw;
3837 ecc->write_page_raw = nand_write_page_raw;
3838 ecc->read_oob = nand_read_oob_std;
3839 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01003840 /*
3841 * Board driver should supply ecc.size and ecc.bytes values to
3842 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003843 * for details. Otherwise, default to 4 bits for large page
3844 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003845 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003846 if (!ecc->size && (mtd->oobsize >= 64)) {
3847 ecc->size = 512;
3848 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01003849 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003850 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3851 &ecc->layout);
3852 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003853 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003854 BUG();
3855 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003856 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003857 break;
3858
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003859 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003860 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003861 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08003862 ecc->read_page = nand_read_page_raw;
3863 ecc->write_page = nand_write_page_raw;
3864 ecc->read_oob = nand_read_oob_std;
3865 ecc->read_page_raw = nand_read_page_raw;
3866 ecc->write_page_raw = nand_write_page_raw;
3867 ecc->write_oob = nand_write_oob_std;
3868 ecc->size = mtd->writesize;
3869 ecc->bytes = 0;
3870 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08003874 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003875 BUG();
3876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Brian Norris9ce244b2011-08-30 18:45:37 -07003878 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08003879 if (!ecc->read_oob_raw)
3880 ecc->read_oob_raw = ecc->read_oob;
3881 if (!ecc->write_oob_raw)
3882 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003883
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003884 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003885 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003886 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003887 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003888 ecc->layout->oobavail = 0;
3889 for (i = 0; ecc->layout->oobfree[i].length
3890 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
3891 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
3892 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003893
3894 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003895 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003896 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003897 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003898 ecc->steps = mtd->writesize / ecc->size;
3899 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003900 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003901 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003903 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003904
Brian Norris8b6e50c2011-05-25 14:59:01 -07003905 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003906 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08003907 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003908 case 2:
3909 mtd->subpage_sft = 1;
3910 break;
3911 case 4:
3912 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003913 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003914 mtd->subpage_sft = 2;
3915 break;
3916 }
3917 }
3918 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3919
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003920 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003921 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003924 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003926 /* Large page NAND with SOFT_ECC should support subpage reads */
Huang Shijie97de79e02013-10-18 14:20:53 +08003927 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003928 chip->options |= NAND_SUBPAGE_READ;
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08003931 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003932 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3933 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003934 mtd->_erase = nand_erase;
3935 mtd->_point = NULL;
3936 mtd->_unpoint = NULL;
3937 mtd->_read = nand_read;
3938 mtd->_write = nand_write;
3939 mtd->_panic_write = panic_nand_write;
3940 mtd->_read_oob = nand_read_oob;
3941 mtd->_write_oob = nand_write_oob;
3942 mtd->_sync = nand_sync;
3943 mtd->_lock = NULL;
3944 mtd->_unlock = NULL;
3945 mtd->_suspend = nand_suspend;
3946 mtd->_resume = nand_resume;
3947 mtd->_block_isbad = nand_block_isbad;
3948 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003949 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Mike Dunn6a918ba2012-03-11 14:21:11 -07003951 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08003952 mtd->ecclayout = ecc->layout;
3953 mtd->ecc_strength = ecc->strength;
3954 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003955 /*
3956 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3957 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3958 * properly set.
3959 */
3960 if (!mtd->bitflip_threshold)
3961 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003963 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003964 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003965 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
3967 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003968 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003970EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Brian Norris8b6e50c2011-05-25 14:59:01 -07003972/*
3973 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003974 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003975 * to call us from in-kernel code if the core NAND support is modular.
3976 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003977#ifdef MODULE
3978#define caller_is_module() (1)
3979#else
3980#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003981 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003982#endif
3983
3984/**
3985 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003986 * @mtd: MTD device structure
3987 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003988 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003989 * This fills out all the uninitialized function pointers with the defaults.
3990 * The flash ID is read and the mtd/chip structures are filled with the
3991 * appropriate values. The mtd->owner field must be set to the module of the
3992 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003993 */
3994int nand_scan(struct mtd_info *mtd, int maxchips)
3995{
3996 int ret;
3997
3998 /* Many callers got this wrong, so check for it for a while... */
3999 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004000 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004001 BUG();
4002 }
4003
David Woodhouse5e81e882010-02-26 18:32:56 +00004004 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004005 if (!ret)
4006 ret = nand_scan_tail(mtd);
4007 return ret;
4008}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004009EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004012 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004013 * @mtd: MTD device structure
4014 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004015void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004017 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Ivan Djelic193bd402011-03-11 11:05:33 +01004019 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4020 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4021
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004022 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Jesper Juhlfa671642005-11-07 01:01:27 -08004024 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004025 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004026 if (!(chip->options & NAND_OWN_BUFFERS))
4027 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004028
4029 /* Free bad block descriptor memory */
4030 if (chip->badblock_pattern && chip->badblock_pattern->options
4031 & NAND_BBT_DYNAMICSTRUCT)
4032 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
David Woodhousee0c7d762006-05-13 18:07:53 +01004034EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004035
4036static int __init nand_base_init(void)
4037{
4038 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4039 return 0;
4040}
4041
4042static void __exit nand_base_exit(void)
4043{
4044 led_trigger_unregister_simple(nand_led_trigger);
4045}
4046
4047module_init(nand_base_init);
4048module_exit(nand_base_exit);
4049
David Woodhousee0c7d762006-05-13 18:07:53 +01004050MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004051MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4052MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004053MODULE_DESCRIPTION("Generic NAND flash driver code");