blob: 7853b9b0a05ed8b8b8cd11adf641b3e4e7b1c6a0 [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 */
Brian Norris3dad2342014-01-29 14:08:12 -0800592 if (chip->options & NAND_BUSWIDTH_16 &&
593 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200594 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200595 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200596 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200598 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200599 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200600 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200601 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200602 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200603 if (chip->chipsize > (32 << 20))
604 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200605 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200606 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000607
608 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700609 * Program and erase have their own busy handlers status and sequential
610 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 case NAND_CMD_PAGEPROG:
615 case NAND_CMD_ERASE1:
616 case NAND_CMD_ERASE2:
617 case NAND_CMD_SEQIN:
618 case NAND_CMD_STATUS:
619 return;
620
621 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200622 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200624 udelay(chip->chip_delay);
625 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200626 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200627 chip->cmd_ctrl(mtd,
628 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200629 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
630 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return;
632
David Woodhousee0c7d762006-05-13 18:07:53 +0100633 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000635 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * If we don't have access to the busy pin, we apply the given
637 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100638 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 if (!chip->dev_ready) {
640 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700644 /*
645 * Apply this short delay always to ensure that we do wait tWB in
646 * any case on any machine.
647 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100648 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000649
650 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653/**
654 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700655 * @mtd: MTD device structure
656 * @command: the command to be sent
657 * @column: the column address for this command, -1 if none
658 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200660 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700661 * devices. We don't have the separate regions as we have in the small page
662 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200664static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
665 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200667 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Emulate NAND_CMD_READOOB */
670 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200671 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 command = NAND_CMD_READ0;
673 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000674
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200675 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400676 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200679 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 /* Serially input address */
682 if (column != -1) {
683 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800684 if (chip->options & NAND_BUSWIDTH_16 &&
685 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200687 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200688 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200689 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200692 chip->cmd_ctrl(mtd, page_addr, ctrl);
693 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200694 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200696 if (chip->chipsize > (128 << 20))
697 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200698 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200701 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000702
703 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700704 * Program and erase have their own busy handlers status, sequential
705 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 case NAND_CMD_CACHEDPROG:
710 case NAND_CMD_PAGEPROG:
711 case NAND_CMD_ERASE1:
712 case NAND_CMD_ERASE2:
713 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200714 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000716 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200719 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200721 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200722 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
723 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
724 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
725 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200726 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
727 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return;
729
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200730 case NAND_CMD_RNDOUT:
731 /* No ready / busy check necessary */
732 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
733 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
734 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
735 NAND_NCE | NAND_CTRL_CHANGE);
736 return;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200739 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
740 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
741 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
742 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743
David Woodhousee0c7d762006-05-13 18:07:53 +0100744 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000746 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200750 if (!chip->dev_ready) {
751 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000755
Brian Norris8b6e50c2011-05-25 14:59:01 -0700756 /*
757 * Apply this short delay always to ensure that we do wait tWB in
758 * any case on any machine.
759 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100760 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000761
762 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200766 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700767 * @chip: the nand chip descriptor
768 * @mtd: MTD device structure
769 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200770 *
771 * Used when in panic, no locks are taken.
772 */
773static void panic_nand_get_device(struct nand_chip *chip,
774 struct mtd_info *mtd, int new_state)
775{
Brian Norris7854d3f2011-06-23 14:12:08 -0700776 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200777 chip->controller->active = chip;
778 chip->state = new_state;
779}
780
781/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700783 * @mtd: MTD device structure
784 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 *
786 * Get the device and lock it for exclusive access
787 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200788static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800789nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800791 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200792 spinlock_t *lock = &chip->controller->lock;
793 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100794 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200795retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100796 spin_lock(lock);
797
vimal singhb8b3ee92009-07-09 20:41:22 +0530798 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200799 if (!chip->controller->active)
800 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200801
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200802 if (chip->controller->active == chip && chip->state == FL_READY) {
803 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100804 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100805 return 0;
806 }
807 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800808 if (chip->controller->active->state == FL_PM_SUSPENDED) {
809 chip->state = FL_PM_SUSPENDED;
810 spin_unlock(lock);
811 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800812 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100813 }
814 set_current_state(TASK_UNINTERRUPTIBLE);
815 add_wait_queue(wq, &wait);
816 spin_unlock(lock);
817 schedule();
818 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto retry;
820}
821
822/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700823 * panic_nand_wait - [GENERIC] wait until the command is done
824 * @mtd: MTD device structure
825 * @chip: NAND chip structure
826 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200827 *
828 * Wait for command done. This is a helper function for nand_wait used when
829 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400830 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200831 */
832static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
833 unsigned long timeo)
834{
835 int i;
836 for (i = 0; i < timeo; i++) {
837 if (chip->dev_ready) {
838 if (chip->dev_ready(mtd))
839 break;
840 } else {
841 if (chip->read_byte(mtd) & NAND_STATUS_READY)
842 break;
843 }
844 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200845 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200846}
847
848/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700849 * nand_wait - [DEFAULT] wait until the command is done
850 * @mtd: MTD device structure
851 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700853 * Wait for command done. This applies to erase and program only. Erase can
854 * take up to 400ms and program up to 20ms according to general NAND and
855 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700856 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200857static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200860 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800861 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Richard Purdie8fe833c2006-03-31 02:31:14 -0800863 led_trigger_event(nand_led_trigger, LED_FULL);
864
Brian Norris8b6e50c2011-05-25 14:59:01 -0700865 /*
866 * Apply this short delay always to ensure that we do wait tWB in any
867 * case on any machine.
868 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100869 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200871 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200873 if (in_interrupt() || oops_in_progress)
874 panic_nand_wait(mtd, chip, timeo);
875 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800876 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200877 while (time_before(jiffies, timeo)) {
878 if (chip->dev_ready) {
879 if (chip->dev_ready(mtd))
880 break;
881 } else {
882 if (chip->read_byte(mtd) & NAND_STATUS_READY)
883 break;
884 }
885 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800888 led_trigger_event(nand_led_trigger, LED_OFF);
889
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200890 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100891 /* This can happen if in case of timeout or buggy dev_ready */
892 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return status;
894}
895
896/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700897 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700898 * @mtd: mtd info
899 * @ofs: offset to start unlock from
900 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700901 * @invert: when = 0, unlock the range of blocks within the lower and
902 * upper boundary address
903 * when = 1, unlock the range of blocks outside the boundaries
904 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530905 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700906 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530907 */
908static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
909 uint64_t len, int invert)
910{
911 int ret = 0;
912 int status, page;
913 struct nand_chip *chip = mtd->priv;
914
915 /* Submit address of first page to unlock */
916 page = ofs >> chip->page_shift;
917 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
918
919 /* Submit address of last page to unlock */
920 page = (ofs + len) >> chip->page_shift;
921 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
922 (page | invert) & chip->pagemask);
923
924 /* Call wait ready function */
925 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530926 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400927 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700928 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530929 __func__, status);
930 ret = -EIO;
931 }
932
933 return ret;
934}
935
936/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700937 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700938 * @mtd: mtd info
939 * @ofs: offset to start unlock from
940 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530941 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700942 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530943 */
944int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
945{
946 int ret = 0;
947 int chipnr;
948 struct nand_chip *chip = mtd->priv;
949
Brian Norris289c0522011-07-19 10:06:09 -0700950 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530951 __func__, (unsigned long long)ofs, len);
952
953 if (check_offs_len(mtd, ofs, len))
954 ret = -EINVAL;
955
956 /* Align to last block address if size addresses end of the device */
957 if (ofs + len == mtd->size)
958 len -= mtd->erasesize;
959
Huang Shijie6a8214a2012-11-19 14:43:30 +0800960 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530961
962 /* Shift to get chip number */
963 chipnr = ofs >> chip->chip_shift;
964
965 chip->select_chip(mtd, chipnr);
966
967 /* Check, if it is write protected */
968 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700969 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530970 __func__);
971 ret = -EIO;
972 goto out;
973 }
974
975 ret = __nand_unlock(mtd, ofs, len, 0);
976
977out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800978 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530979 nand_release_device(mtd);
980
981 return ret;
982}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200983EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530984
985/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700986 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700987 * @mtd: mtd info
988 * @ofs: offset to start unlock from
989 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530990 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700991 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
992 * have this feature, but it allows only to lock all blocks, not for specified
993 * range for block. Implementing 'lock' feature by making use of 'unlock', for
994 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530995 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700996 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530997 */
998int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
999{
1000 int ret = 0;
1001 int chipnr, status, page;
1002 struct nand_chip *chip = mtd->priv;
1003
Brian Norris289c0522011-07-19 10:06:09 -07001004 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301005 __func__, (unsigned long long)ofs, len);
1006
1007 if (check_offs_len(mtd, ofs, len))
1008 ret = -EINVAL;
1009
Huang Shijie6a8214a2012-11-19 14:43:30 +08001010 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301011
1012 /* Shift to get chip number */
1013 chipnr = ofs >> chip->chip_shift;
1014
1015 chip->select_chip(mtd, chipnr);
1016
1017 /* Check, if it is write protected */
1018 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001019 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301020 __func__);
1021 status = MTD_ERASE_FAILED;
1022 ret = -EIO;
1023 goto out;
1024 }
1025
1026 /* Submit address of first page to lock */
1027 page = ofs >> chip->page_shift;
1028 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1029
1030 /* Call wait ready function */
1031 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301032 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001033 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001034 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301035 __func__, status);
1036 ret = -EIO;
1037 goto out;
1038 }
1039
1040 ret = __nand_unlock(mtd, ofs, len, 0x1);
1041
1042out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001043 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301044 nand_release_device(mtd);
1045
1046 return ret;
1047}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001048EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301049
1050/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001051 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001052 * @mtd: mtd info structure
1053 * @chip: nand chip info structure
1054 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001055 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001056 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001057 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001058 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001059 */
1060static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001061 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001062{
1063 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001064 if (oob_required)
1065 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001066 return 0;
1067}
1068
1069/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001070 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001071 * @mtd: mtd info structure
1072 * @chip: nand chip info structure
1073 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001074 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001075 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001076 *
1077 * We need a special oob layout and handling even when OOB isn't used.
1078 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001079static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001080 struct nand_chip *chip, uint8_t *buf,
1081 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001082{
1083 int eccsize = chip->ecc.size;
1084 int eccbytes = chip->ecc.bytes;
1085 uint8_t *oob = chip->oob_poi;
1086 int steps, size;
1087
1088 for (steps = chip->ecc.steps; steps > 0; steps--) {
1089 chip->read_buf(mtd, buf, eccsize);
1090 buf += eccsize;
1091
1092 if (chip->ecc.prepad) {
1093 chip->read_buf(mtd, oob, chip->ecc.prepad);
1094 oob += chip->ecc.prepad;
1095 }
1096
1097 chip->read_buf(mtd, oob, eccbytes);
1098 oob += eccbytes;
1099
1100 if (chip->ecc.postpad) {
1101 chip->read_buf(mtd, oob, chip->ecc.postpad);
1102 oob += chip->ecc.postpad;
1103 }
1104 }
1105
1106 size = mtd->oobsize - (oob - chip->oob_poi);
1107 if (size)
1108 chip->read_buf(mtd, oob, size);
1109
1110 return 0;
1111}
1112
1113/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001114 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001115 * @mtd: mtd info structure
1116 * @chip: nand chip info structure
1117 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001118 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001119 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001120 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001121static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001122 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001124 int i, eccsize = chip->ecc.size;
1125 int eccbytes = chip->ecc.bytes;
1126 int eccsteps = chip->ecc.steps;
1127 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001128 uint8_t *ecc_calc = chip->buffers->ecccalc;
1129 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001130 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001131 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001132
Brian Norris1fbb9382012-05-02 10:14:55 -07001133 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001134
1135 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1136 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1137
1138 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001139 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001140
1141 eccsteps = chip->ecc.steps;
1142 p = buf;
1143
1144 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1145 int stat;
1146
1147 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001148 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001149 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001150 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001151 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001152 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1153 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001154 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001155 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301159 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001160 * @mtd: mtd info structure
1161 * @chip: nand chip info structure
1162 * @data_offs: offset of requested data within the page
1163 * @readlen: data length
1164 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001165 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001166 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001167static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001168 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1169 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001170{
1171 int start_step, end_step, num_steps;
1172 uint32_t *eccpos = chip->ecc.layout->eccpos;
1173 uint8_t *p;
1174 int data_col_addr, i, gaps = 0;
1175 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1176 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Ron4a4163c2014-03-16 04:01:07 +10301177 int index;
Mike Dunn3f91e942012-04-25 12:06:09 -07001178 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001179
Brian Norris7854d3f2011-06-23 14:12:08 -07001180 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001181 start_step = data_offs / chip->ecc.size;
1182 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1183 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301184 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001185
Brian Norris8b6e50c2011-05-25 14:59:01 -07001186 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001187 datafrag_len = num_steps * chip->ecc.size;
1188 eccfrag_len = num_steps * chip->ecc.bytes;
1189
1190 data_col_addr = start_step * chip->ecc.size;
1191 /* If we read not a page aligned data */
1192 if (data_col_addr != 0)
1193 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1194
1195 p = bufpoi + data_col_addr;
1196 chip->read_buf(mtd, p, datafrag_len);
1197
Brian Norris8b6e50c2011-05-25 14:59:01 -07001198 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001199 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1200 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1201
Brian Norris8b6e50c2011-05-25 14:59:01 -07001202 /*
1203 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001204 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001205 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001206 for (i = 0; i < eccfrag_len - 1; i++) {
Ron47570bb12014-03-16 04:01:08 +10301207 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001208 gaps = 1;
1209 break;
1210 }
1211 }
1212 if (gaps) {
1213 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1214 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1215 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001216 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001217 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001218 * about buswidth alignment in read_buf.
1219 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001220 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001221 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001222 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001223 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001224 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001225 aligned_len++;
1226
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001227 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1228 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001229 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1230 }
1231
1232 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001233 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001234
1235 p = bufpoi + data_col_addr;
1236 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1237 int stat;
1238
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001239 stat = chip->ecc.correct(mtd, p,
1240 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001241 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001242 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001243 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001244 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001245 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1246 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001247 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001248 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001249}
1250
1251/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001252 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001253 * @mtd: mtd info structure
1254 * @chip: nand chip info structure
1255 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001256 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001257 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001258 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001259 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001260 */
1261static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001262 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001263{
1264 int i, eccsize = chip->ecc.size;
1265 int eccbytes = chip->ecc.bytes;
1266 int eccsteps = chip->ecc.steps;
1267 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001268 uint8_t *ecc_calc = chip->buffers->ecccalc;
1269 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001270 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001271 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001272
1273 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1274 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1275 chip->read_buf(mtd, p, eccsize);
1276 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1277 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001278 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001279
1280 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001281 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001282
1283 eccsteps = chip->ecc.steps;
1284 p = buf;
1285
1286 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1287 int stat;
1288
1289 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001290 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001291 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001292 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001293 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001294 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1295 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001296 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001297 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001298}
1299
1300/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001301 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001302 * @mtd: mtd info structure
1303 * @chip: nand chip info structure
1304 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001305 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001306 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001307 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001308 * Hardware ECC for large page chips, require OOB to be read first. For this
1309 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1310 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1311 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1312 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001313 */
1314static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001315 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001316{
1317 int i, eccsize = chip->ecc.size;
1318 int eccbytes = chip->ecc.bytes;
1319 int eccsteps = chip->ecc.steps;
1320 uint8_t *p = buf;
1321 uint8_t *ecc_code = chip->buffers->ecccode;
1322 uint32_t *eccpos = chip->ecc.layout->eccpos;
1323 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001324 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001325
1326 /* Read the OOB area first */
1327 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1328 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1329 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1330
1331 for (i = 0; i < chip->ecc.total; i++)
1332 ecc_code[i] = chip->oob_poi[eccpos[i]];
1333
1334 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1335 int stat;
1336
1337 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1338 chip->read_buf(mtd, p, eccsize);
1339 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1340
1341 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001342 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001343 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001344 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001345 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001346 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1347 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001348 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001349 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001350}
1351
1352/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001353 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001354 * @mtd: mtd info structure
1355 * @chip: nand chip info structure
1356 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001357 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001358 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001360 * The hw generator calculates the error syndrome automatically. Therefore we
1361 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001362 */
1363static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001364 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001365{
1366 int i, eccsize = chip->ecc.size;
1367 int eccbytes = chip->ecc.bytes;
1368 int eccsteps = chip->ecc.steps;
1369 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001370 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001371 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001372
1373 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1374 int stat;
1375
1376 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1377 chip->read_buf(mtd, p, eccsize);
1378
1379 if (chip->ecc.prepad) {
1380 chip->read_buf(mtd, oob, chip->ecc.prepad);
1381 oob += chip->ecc.prepad;
1382 }
1383
1384 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1385 chip->read_buf(mtd, oob, eccbytes);
1386 stat = chip->ecc.correct(mtd, p, oob, NULL);
1387
Mike Dunn3f91e942012-04-25 12:06:09 -07001388 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001389 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001390 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001391 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001392 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1393 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001394
1395 oob += eccbytes;
1396
1397 if (chip->ecc.postpad) {
1398 chip->read_buf(mtd, oob, chip->ecc.postpad);
1399 oob += chip->ecc.postpad;
1400 }
1401 }
1402
1403 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001404 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001405 if (i)
1406 chip->read_buf(mtd, oob, i);
1407
Mike Dunn3f91e942012-04-25 12:06:09 -07001408 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001409}
1410
1411/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001412 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001413 * @chip: nand chip structure
1414 * @oob: oob destination address
1415 * @ops: oob ops structure
1416 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001417 */
1418static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001419 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001420{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001421 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001422
Brian Norris0612b9d2011-08-30 18:45:40 -07001423 case MTD_OPS_PLACE_OOB:
1424 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001425 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1426 return oob + len;
1427
Brian Norris0612b9d2011-08-30 18:45:40 -07001428 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001429 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001430 uint32_t boffs = 0, roffs = ops->ooboffs;
1431 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001432
Florian Fainellif8ac0412010-09-07 13:23:43 +02001433 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001434 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001435 if (unlikely(roffs)) {
1436 if (roffs >= free->length) {
1437 roffs -= free->length;
1438 continue;
1439 }
1440 boffs = free->offset + roffs;
1441 bytes = min_t(size_t, len,
1442 (free->length - roffs));
1443 roffs = 0;
1444 } else {
1445 bytes = min_t(size_t, len, free->length);
1446 boffs = free->offset;
1447 }
1448 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001449 oob += bytes;
1450 }
1451 return oob;
1452 }
1453 default:
1454 BUG();
1455 }
1456 return NULL;
1457}
1458
1459/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001460 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1461 * @mtd: MTD device structure
1462 * @retry_mode: the retry mode to use
1463 *
1464 * Some vendors supply a special command to shift the Vt threshold, to be used
1465 * when there are too many bitflips in a page (i.e., ECC error). After setting
1466 * a new threshold, the host should retry reading the page.
1467 */
1468static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1469{
1470 struct nand_chip *chip = mtd->priv;
1471
1472 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1473
1474 if (retry_mode >= chip->read_retries)
1475 return -EINVAL;
1476
1477 if (!chip->setup_read_retry)
1478 return -EOPNOTSUPP;
1479
1480 return chip->setup_read_retry(mtd, retry_mode);
1481}
1482
1483/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001484 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001485 * @mtd: MTD device structure
1486 * @from: offset to read from
1487 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001488 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001489 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001490 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001491static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1492 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001493{
Brian Norrise47f3db2012-05-02 10:14:56 -07001494 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001495 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001496 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001497 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001498 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001499 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001500 mtd->oobavail : mtd->oobsize;
1501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001502 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001503 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001504 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001505 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001507 chipnr = (int)(from >> chip->chip_shift);
1508 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001510 realpage = (int)(from >> chip->page_shift);
1511 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001513 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001515 buf = ops->datbuf;
1516 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001517 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001518
Florian Fainellif8ac0412010-09-07 13:23:43 +02001519 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001520 unsigned int ecc_failures = mtd->ecc_stats.failed;
1521
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001522 bytes = min(mtd->writesize - col, readlen);
1523 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001524
Brian Norris8b6e50c2011-05-25 14:59:01 -07001525 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001526 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001527 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Brian Norrisba84fb52014-01-03 15:13:33 -08001529read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001530 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Mike Dunnedbc45402012-04-25 12:06:11 -07001532 /*
1533 * Now read the page into the buffer. Absent an error,
1534 * the read methods return max bitflips per ecc step.
1535 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001536 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001537 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001538 oob_required,
1539 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001540 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1541 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001542 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001543 col, bytes, bufpoi,
1544 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001545 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001546 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001547 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001548 if (ret < 0) {
1549 if (!aligned)
1550 /* Invalidate page cache */
1551 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001552 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001553 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001554
Mike Dunnedbc45402012-04-25 12:06:11 -07001555 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1556
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001557 /* Transfer not aligned data */
1558 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001559 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001560 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001561 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001562 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001563 chip->pagebuf_bitflips = ret;
1564 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001565 /* Invalidate page cache */
1566 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001567 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001568 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001570
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001571 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001572 int toread = min(oobreadlen, max_oobsize);
1573
1574 if (toread) {
1575 oob = nand_transfer_oob(chip,
1576 oob, ops, toread);
1577 oobreadlen -= toread;
1578 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001579 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001580
1581 if (chip->options & NAND_NEED_READRDY) {
1582 /* Apply delay or wait for ready/busy pin */
1583 if (!chip->dev_ready)
1584 udelay(chip->chip_delay);
1585 else
1586 nand_wait_ready(mtd);
1587 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001588
Brian Norrisba84fb52014-01-03 15:13:33 -08001589 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001590 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001591 retry_mode++;
1592 ret = nand_setup_read_retry(mtd,
1593 retry_mode);
1594 if (ret < 0)
1595 break;
1596
1597 /* Reset failures; retry */
1598 mtd->ecc_stats.failed = ecc_failures;
1599 goto read_retry;
1600 } else {
1601 /* No more retry modes; real failure */
1602 ecc_fail = true;
1603 }
1604 }
1605
1606 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001607 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001608 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001609 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001610 max_bitflips = max_t(unsigned int, max_bitflips,
1611 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001614 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001615
Brian Norrisba84fb52014-01-03 15:13:33 -08001616 /* Reset to retry mode 0 */
1617 if (retry_mode) {
1618 ret = nand_setup_read_retry(mtd, 0);
1619 if (ret < 0)
1620 break;
1621 retry_mode = 0;
1622 }
1623
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001624 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Brian Norris8b6e50c2011-05-25 14:59:01 -07001627 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 col = 0;
1629 /* Increment page address */
1630 realpage++;
1631
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001632 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* Check, if we cross a chip boundary */
1634 if (!page) {
1635 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001636 chip->select_chip(mtd, -1);
1637 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001640 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001642 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001643 if (oob)
1644 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Mike Dunn3f91e942012-04-25 12:06:09 -07001646 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001647 return ret;
1648
Brian Norrisb72f3df2013-12-03 11:04:14 -08001649 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001650 return -EBADMSG;
1651
Mike Dunnedbc45402012-04-25 12:06:11 -07001652 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001653}
1654
1655/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001656 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001657 * @mtd: MTD device structure
1658 * @from: offset to read from
1659 * @len: number of bytes to read
1660 * @retlen: pointer to variable to store the number of read bytes
1661 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001662 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001663 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001664 */
1665static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1666 size_t *retlen, uint8_t *buf)
1667{
Brian Norris4a89ff82011-08-30 18:45:45 -07001668 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001669 int ret;
1670
Huang Shijie6a8214a2012-11-19 14:43:30 +08001671 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001672 ops.len = len;
1673 ops.datbuf = buf;
1674 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001675 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001676 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001677 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001678 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001679 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001683 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001684 * @mtd: mtd info structure
1685 * @chip: nand chip info structure
1686 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001687 */
1688static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001689 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001690{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001691 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001692 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001693 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001694}
1695
1696/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001697 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001698 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001699 * @mtd: mtd info structure
1700 * @chip: nand chip info structure
1701 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001702 */
1703static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001704 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001705{
1706 uint8_t *buf = chip->oob_poi;
1707 int length = mtd->oobsize;
1708 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1709 int eccsize = chip->ecc.size;
1710 uint8_t *bufpoi = buf;
1711 int i, toread, sndrnd = 0, pos;
1712
1713 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1714 for (i = 0; i < chip->ecc.steps; i++) {
1715 if (sndrnd) {
1716 pos = eccsize + i * (eccsize + chunk);
1717 if (mtd->writesize > 512)
1718 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1719 else
1720 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1721 } else
1722 sndrnd = 1;
1723 toread = min_t(int, length, chunk);
1724 chip->read_buf(mtd, bufpoi, toread);
1725 bufpoi += toread;
1726 length -= toread;
1727 }
1728 if (length > 0)
1729 chip->read_buf(mtd, bufpoi, length);
1730
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001731 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001732}
1733
1734/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001735 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001736 * @mtd: mtd info structure
1737 * @chip: nand chip info structure
1738 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001739 */
1740static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1741 int page)
1742{
1743 int status = 0;
1744 const uint8_t *buf = chip->oob_poi;
1745 int length = mtd->oobsize;
1746
1747 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1748 chip->write_buf(mtd, buf, length);
1749 /* Send command to program the OOB data */
1750 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1751
1752 status = chip->waitfunc(mtd, chip);
1753
Savin Zlobec0d420f92006-06-21 11:51:20 +02001754 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001755}
1756
1757/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001758 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001759 * with syndrome - only for large page flash
1760 * @mtd: mtd info structure
1761 * @chip: nand chip info structure
1762 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001763 */
1764static int nand_write_oob_syndrome(struct mtd_info *mtd,
1765 struct nand_chip *chip, int page)
1766{
1767 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1768 int eccsize = chip->ecc.size, length = mtd->oobsize;
1769 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1770 const uint8_t *bufpoi = chip->oob_poi;
1771
1772 /*
1773 * data-ecc-data-ecc ... ecc-oob
1774 * or
1775 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1776 */
1777 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1778 pos = steps * (eccsize + chunk);
1779 steps = 0;
1780 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001781 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001782
1783 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1784 for (i = 0; i < steps; i++) {
1785 if (sndcmd) {
1786 if (mtd->writesize <= 512) {
1787 uint32_t fill = 0xFFFFFFFF;
1788
1789 len = eccsize;
1790 while (len > 0) {
1791 int num = min_t(int, len, 4);
1792 chip->write_buf(mtd, (uint8_t *)&fill,
1793 num);
1794 len -= num;
1795 }
1796 } else {
1797 pos = eccsize + i * (eccsize + chunk);
1798 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1799 }
1800 } else
1801 sndcmd = 1;
1802 len = min_t(int, length, chunk);
1803 chip->write_buf(mtd, bufpoi, len);
1804 bufpoi += len;
1805 length -= len;
1806 }
1807 if (length > 0)
1808 chip->write_buf(mtd, bufpoi, length);
1809
1810 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1811 status = chip->waitfunc(mtd, chip);
1812
1813 return status & NAND_STATUS_FAIL ? -EIO : 0;
1814}
1815
1816/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001817 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001818 * @mtd: MTD device structure
1819 * @from: offset to read from
1820 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001822 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001824static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1825 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Brian Norrisc00a0992012-05-01 17:12:54 -07001827 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001828 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001829 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001830 int readlen = ops->ooblen;
1831 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001832 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001833 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Brian Norris289c0522011-07-19 10:06:09 -07001835 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301836 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Brian Norris041e4572011-06-23 16:45:24 -07001838 stats = mtd->ecc_stats;
1839
Brian Norris0612b9d2011-08-30 18:45:40 -07001840 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001841 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001842 else
1843 len = mtd->oobsize;
1844
1845 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001846 pr_debug("%s: attempt to start read outside oob\n",
1847 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001848 return -EINVAL;
1849 }
1850
1851 /* Do not allow reads past end of device */
1852 if (unlikely(from >= mtd->size ||
1853 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1854 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001855 pr_debug("%s: attempt to read beyond end of device\n",
1856 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001857 return -EINVAL;
1858 }
Vitaly Wool70145682006-11-03 18:20:38 +03001859
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001860 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001861 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001863 /* Shift to get page */
1864 realpage = (int)(from >> chip->page_shift);
1865 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Florian Fainellif8ac0412010-09-07 13:23:43 +02001867 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001868 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001869 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001870 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001871 ret = chip->ecc.read_oob(mtd, chip, page);
1872
1873 if (ret < 0)
1874 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001875
1876 len = min(len, readlen);
1877 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001878
Brian Norris5bc7c332013-03-13 09:51:31 -07001879 if (chip->options & NAND_NEED_READRDY) {
1880 /* Apply delay or wait for ready/busy pin */
1881 if (!chip->dev_ready)
1882 udelay(chip->chip_delay);
1883 else
1884 nand_wait_ready(mtd);
1885 }
1886
Vitaly Wool70145682006-11-03 18:20:38 +03001887 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001888 if (!readlen)
1889 break;
1890
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001891 /* Increment page address */
1892 realpage++;
1893
1894 page = realpage & chip->pagemask;
1895 /* Check, if we cross a chip boundary */
1896 if (!page) {
1897 chipnr++;
1898 chip->select_chip(mtd, -1);
1899 chip->select_chip(mtd, chipnr);
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001902 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001904 ops->oobretlen = ops->ooblen - readlen;
1905
1906 if (ret < 0)
1907 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001908
1909 if (mtd->ecc_stats.failed - stats.failed)
1910 return -EBADMSG;
1911
1912 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001916 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001917 * @mtd: MTD device structure
1918 * @from: offset to read from
1919 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001921 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1924 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001926 int ret = -ENOTSUPP;
1927
1928 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001931 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001932 pr_debug("%s: attempt to read beyond end of device\n",
1933 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EINVAL;
1935 }
1936
Huang Shijie6a8214a2012-11-19 14:43:30 +08001937 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Florian Fainellif8ac0412010-09-07 13:23:43 +02001939 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001940 case MTD_OPS_PLACE_OOB:
1941 case MTD_OPS_AUTO_OOB:
1942 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001943 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001944
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001945 default:
1946 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001949 if (!ops->datbuf)
1950 ret = nand_do_read_oob(mtd, from, ops);
1951 else
1952 ret = nand_do_read_ops(mtd, from, ops);
1953
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001954out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001956 return ret;
1957}
1958
1959
1960/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001961 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001962 * @mtd: mtd info structure
1963 * @chip: nand chip info structure
1964 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001965 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001966 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001967 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001968 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001969static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001970 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001971{
1972 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001973 if (oob_required)
1974 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001975
1976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001979/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001980 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001981 * @mtd: mtd info structure
1982 * @chip: nand chip info structure
1983 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001984 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001985 *
1986 * We need a special oob layout and handling even when ECC isn't checked.
1987 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001988static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001989 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001990 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001991{
1992 int eccsize = chip->ecc.size;
1993 int eccbytes = chip->ecc.bytes;
1994 uint8_t *oob = chip->oob_poi;
1995 int steps, size;
1996
1997 for (steps = chip->ecc.steps; steps > 0; steps--) {
1998 chip->write_buf(mtd, buf, eccsize);
1999 buf += eccsize;
2000
2001 if (chip->ecc.prepad) {
2002 chip->write_buf(mtd, oob, chip->ecc.prepad);
2003 oob += chip->ecc.prepad;
2004 }
2005
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002006 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002007 oob += eccbytes;
2008
2009 if (chip->ecc.postpad) {
2010 chip->write_buf(mtd, oob, chip->ecc.postpad);
2011 oob += chip->ecc.postpad;
2012 }
2013 }
2014
2015 size = mtd->oobsize - (oob - chip->oob_poi);
2016 if (size)
2017 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002018
2019 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002020}
2021/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002022 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002023 * @mtd: mtd info structure
2024 * @chip: nand chip info structure
2025 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002026 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002027 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002028static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002029 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002030{
2031 int i, eccsize = chip->ecc.size;
2032 int eccbytes = chip->ecc.bytes;
2033 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002034 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002035 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002036 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002037
Brian Norris7854d3f2011-06-23 14:12:08 -07002038 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002039 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2040 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002041
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002042 for (i = 0; i < chip->ecc.total; i++)
2043 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002044
Josh Wufdbad98d2012-06-25 18:07:45 +08002045 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002046}
2047
2048/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002049 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002050 * @mtd: mtd info structure
2051 * @chip: nand chip info structure
2052 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002053 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002054 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002055static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002056 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002057{
2058 int i, eccsize = chip->ecc.size;
2059 int eccbytes = chip->ecc.bytes;
2060 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002061 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002062 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002063 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002064
2065 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2066 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002067 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002068 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2069 }
2070
2071 for (i = 0; i < chip->ecc.total; i++)
2072 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2073
2074 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002075
2076 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002077}
2078
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302079
2080/**
2081 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2082 * @mtd: mtd info structure
2083 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002084 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302085 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002086 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302087 * @oob_required: must write chip->oob_poi to OOB
2088 */
2089static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2090 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002091 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302092 int oob_required)
2093{
2094 uint8_t *oob_buf = chip->oob_poi;
2095 uint8_t *ecc_calc = chip->buffers->ecccalc;
2096 int ecc_size = chip->ecc.size;
2097 int ecc_bytes = chip->ecc.bytes;
2098 int ecc_steps = chip->ecc.steps;
2099 uint32_t *eccpos = chip->ecc.layout->eccpos;
2100 uint32_t start_step = offset / ecc_size;
2101 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2102 int oob_bytes = mtd->oobsize / ecc_steps;
2103 int step, i;
2104
2105 for (step = 0; step < ecc_steps; step++) {
2106 /* configure controller for WRITE access */
2107 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2108
2109 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002110 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302111
2112 /* mask ECC of un-touched subpages by padding 0xFF */
2113 if ((step < start_step) || (step > end_step))
2114 memset(ecc_calc, 0xff, ecc_bytes);
2115 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002116 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302117
2118 /* mask OOB of un-touched subpages by padding 0xFF */
2119 /* if oob_required, preserve OOB metadata of written subpage */
2120 if (!oob_required || (step < start_step) || (step > end_step))
2121 memset(oob_buf, 0xff, oob_bytes);
2122
Brian Norrisd6a950802013-08-08 17:16:36 -07002123 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302124 ecc_calc += ecc_bytes;
2125 oob_buf += oob_bytes;
2126 }
2127
2128 /* copy calculated ECC for whole page to chip->buffer->oob */
2129 /* this include masked-value(0xFF) for unwritten subpages */
2130 ecc_calc = chip->buffers->ecccalc;
2131 for (i = 0; i < chip->ecc.total; i++)
2132 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2133
2134 /* write OOB buffer to NAND device */
2135 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2136
2137 return 0;
2138}
2139
2140
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002141/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002142 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002143 * @mtd: mtd info structure
2144 * @chip: nand chip info structure
2145 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002146 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002147 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002148 * The hw generator calculates the error syndrome automatically. Therefore we
2149 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002150 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002151static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002152 struct nand_chip *chip,
2153 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154{
2155 int i, eccsize = chip->ecc.size;
2156 int eccbytes = chip->ecc.bytes;
2157 int eccsteps = chip->ecc.steps;
2158 const uint8_t *p = buf;
2159 uint8_t *oob = chip->oob_poi;
2160
2161 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2162
2163 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2164 chip->write_buf(mtd, p, eccsize);
2165
2166 if (chip->ecc.prepad) {
2167 chip->write_buf(mtd, oob, chip->ecc.prepad);
2168 oob += chip->ecc.prepad;
2169 }
2170
2171 chip->ecc.calculate(mtd, p, oob);
2172 chip->write_buf(mtd, oob, eccbytes);
2173 oob += eccbytes;
2174
2175 if (chip->ecc.postpad) {
2176 chip->write_buf(mtd, oob, chip->ecc.postpad);
2177 oob += chip->ecc.postpad;
2178 }
2179 }
2180
2181 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002182 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002183 if (i)
2184 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002185
2186 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002187}
2188
2189/**
David Woodhouse956e9442006-09-25 17:12:39 +01002190 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002191 * @mtd: MTD device structure
2192 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302193 * @offset: address offset within the page
2194 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002195 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002196 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002197 * @page: page number to write
2198 * @cached: cached programming
2199 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002200 */
2201static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302202 uint32_t offset, int data_len, const uint8_t *buf,
2203 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002204{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302205 int status, subpage;
2206
2207 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2208 chip->ecc.write_subpage)
2209 subpage = offset || (data_len < mtd->writesize);
2210 else
2211 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002212
2213 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2214
David Woodhouse956e9442006-09-25 17:12:39 +01002215 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302216 status = chip->ecc.write_page_raw(mtd, chip, buf,
2217 oob_required);
2218 else if (subpage)
2219 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2220 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002221 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002222 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2223
2224 if (status < 0)
2225 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002226
2227 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002228 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002229 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002230 */
2231 cached = 0;
2232
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002233 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002234
2235 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002236 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237 /*
2238 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002239 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002240 */
2241 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2242 status = chip->errstat(mtd, chip, FL_WRITING, status,
2243 page);
2244
2245 if (status & NAND_STATUS_FAIL)
2246 return -EIO;
2247 } else {
2248 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002249 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002250 }
2251
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002252 return 0;
2253}
2254
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002255/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002256 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002257 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002258 * @oob: oob data buffer
2259 * @len: oob data write length
2260 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002261 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002262static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2263 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002264{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002265 struct nand_chip *chip = mtd->priv;
2266
2267 /*
2268 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2269 * data from a previous OOB read.
2270 */
2271 memset(chip->oob_poi, 0xff, mtd->oobsize);
2272
Florian Fainellif8ac0412010-09-07 13:23:43 +02002273 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002274
Brian Norris0612b9d2011-08-30 18:45:40 -07002275 case MTD_OPS_PLACE_OOB:
2276 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002277 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2278 return oob + len;
2279
Brian Norris0612b9d2011-08-30 18:45:40 -07002280 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002281 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002282 uint32_t boffs = 0, woffs = ops->ooboffs;
2283 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002284
Florian Fainellif8ac0412010-09-07 13:23:43 +02002285 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002286 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002287 if (unlikely(woffs)) {
2288 if (woffs >= free->length) {
2289 woffs -= free->length;
2290 continue;
2291 }
2292 boffs = free->offset + woffs;
2293 bytes = min_t(size_t, len,
2294 (free->length - woffs));
2295 woffs = 0;
2296 } else {
2297 bytes = min_t(size_t, len, free->length);
2298 boffs = free->offset;
2299 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002300 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002301 oob += bytes;
2302 }
2303 return oob;
2304 }
2305 default:
2306 BUG();
2307 }
2308 return NULL;
2309}
2310
Florian Fainellif8ac0412010-09-07 13:23:43 +02002311#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002312
2313/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002314 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002315 * @mtd: MTD device structure
2316 * @to: offset to write to
2317 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002318 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002319 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002320 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2322 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002323{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002324 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002325 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002326 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002327
2328 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002329 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002330 mtd->oobavail : mtd->oobsize;
2331
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002332 uint8_t *oob = ops->oobbuf;
2333 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302334 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002335 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002336
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002337 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002338 if (!writelen)
2339 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002340
Brian Norris8b6e50c2011-05-25 14:59:01 -07002341 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002342 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002343 pr_notice("%s: attempt to write non page aligned data\n",
2344 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002345 return -EINVAL;
2346 }
2347
Thomas Gleixner29072b92006-09-28 15:38:36 +02002348 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002349
Thomas Gleixner6a930962006-06-28 00:11:45 +02002350 chipnr = (int)(to >> chip->chip_shift);
2351 chip->select_chip(mtd, chipnr);
2352
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002353 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002354 if (nand_check_wp(mtd)) {
2355 ret = -EIO;
2356 goto err_out;
2357 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002358
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002359 realpage = (int)(to >> chip->page_shift);
2360 page = realpage & chip->pagemask;
2361 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2362
2363 /* Invalidate the page cache, when we write to the cached page */
2364 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002365 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002366 chip->pagebuf = -1;
2367
Maxim Levitsky782ce792010-02-22 20:39:36 +02002368 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002369 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2370 ret = -EINVAL;
2371 goto err_out;
2372 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002373
Florian Fainellif8ac0412010-09-07 13:23:43 +02002374 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002375 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002376 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002377 uint8_t *wbuf = buf;
2378
Brian Norris8b6e50c2011-05-25 14:59:01 -07002379 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002380 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2381 cached = 0;
2382 bytes = min_t(int, bytes - column, (int) writelen);
2383 chip->pagebuf = -1;
2384 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2385 memcpy(&chip->buffers->databuf[column], buf, bytes);
2386 wbuf = chip->buffers->databuf;
2387 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002388
Maxim Levitsky782ce792010-02-22 20:39:36 +02002389 if (unlikely(oob)) {
2390 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002391 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002392 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002393 } else {
2394 /* We still need to erase leftover OOB data */
2395 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002396 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302397 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2398 oob_required, page, cached,
2399 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002400 if (ret)
2401 break;
2402
2403 writelen -= bytes;
2404 if (!writelen)
2405 break;
2406
Thomas Gleixner29072b92006-09-28 15:38:36 +02002407 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002408 buf += bytes;
2409 realpage++;
2410
2411 page = realpage & chip->pagemask;
2412 /* Check, if we cross a chip boundary */
2413 if (!page) {
2414 chipnr++;
2415 chip->select_chip(mtd, -1);
2416 chip->select_chip(mtd, chipnr);
2417 }
2418 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002419
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002420 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002421 if (unlikely(oob))
2422 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002423
2424err_out:
2425 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002426 return ret;
2427}
2428
2429/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002430 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002431 * @mtd: MTD device structure
2432 * @to: offset to write to
2433 * @len: number of bytes to write
2434 * @retlen: pointer to variable to store the number of written bytes
2435 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002436 *
2437 * NAND write with ECC. Used when performing writes in interrupt context, this
2438 * may for example be called by mtdoops when writing an oops while in panic.
2439 */
2440static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2441 size_t *retlen, const uint8_t *buf)
2442{
2443 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002444 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002445 int ret;
2446
Brian Norris8b6e50c2011-05-25 14:59:01 -07002447 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002448 panic_nand_wait(mtd, chip, 400);
2449
Brian Norris8b6e50c2011-05-25 14:59:01 -07002450 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002451 panic_nand_get_device(chip, mtd, FL_WRITING);
2452
Brian Norris4a89ff82011-08-30 18:45:45 -07002453 ops.len = len;
2454 ops.datbuf = (uint8_t *)buf;
2455 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002456 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002457
Brian Norris4a89ff82011-08-30 18:45:45 -07002458 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002459
Brian Norris4a89ff82011-08-30 18:45:45 -07002460 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002461 return ret;
2462}
2463
2464/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002465 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002466 * @mtd: MTD device structure
2467 * @to: offset to write to
2468 * @len: number of bytes to write
2469 * @retlen: pointer to variable to store the number of written bytes
2470 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002472 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002474static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002475 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Brian Norris4a89ff82011-08-30 18:45:45 -07002477 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002478 int ret;
2479
Huang Shijie6a8214a2012-11-19 14:43:30 +08002480 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002481 ops.len = len;
2482 ops.datbuf = (uint8_t *)buf;
2483 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002484 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002485 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002486 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002487 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002488 return ret;
2489}
2490
2491/**
2492 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002493 * @mtd: MTD device structure
2494 * @to: offset to write to
2495 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002496 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002497 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002498 */
2499static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2500 struct mtd_oob_ops *ops)
2501{
Adrian Hunter03736152007-01-31 17:58:29 +02002502 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002503 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Brian Norris289c0522011-07-19 10:06:09 -07002505 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302506 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Brian Norris0612b9d2011-08-30 18:45:40 -07002508 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002509 len = chip->ecc.layout->oobavail;
2510 else
2511 len = mtd->oobsize;
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002514 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002515 pr_debug("%s: attempt to write past end of page\n",
2516 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return -EINVAL;
2518 }
2519
Adrian Hunter03736152007-01-31 17:58:29 +02002520 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002521 pr_debug("%s: attempt to start write outside oob\n",
2522 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002523 return -EINVAL;
2524 }
2525
Jason Liu775adc32011-02-25 13:06:18 +08002526 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002527 if (unlikely(to >= mtd->size ||
2528 ops->ooboffs + ops->ooblen >
2529 ((mtd->size >> chip->page_shift) -
2530 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002531 pr_debug("%s: attempt to write beyond end of device\n",
2532 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002533 return -EINVAL;
2534 }
2535
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002536 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002537 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002539 /* Shift to get page */
2540 page = (int)(to >> chip->page_shift);
2541
2542 /*
2543 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2544 * of my DiskOnChip 2000 test units) will clear the whole data page too
2545 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2546 * it in the doc2000 driver in August 1999. dwmw2.
2547 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002548 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002551 if (nand_check_wp(mtd)) {
2552 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002553 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002554 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002557 if (page == chip->pagebuf)
2558 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002560 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002561
Brian Norris0612b9d2011-08-30 18:45:40 -07002562 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002563 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2564 else
2565 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002566
Huang Shijieb0bb6902012-11-19 14:43:29 +08002567 chip->select_chip(mtd, -1);
2568
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002569 if (status)
2570 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Vitaly Wool70145682006-11-03 18:20:38 +03002572 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002574 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002577/**
2578 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002579 * @mtd: MTD device structure
2580 * @to: offset to write to
2581 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002582 */
2583static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2584 struct mtd_oob_ops *ops)
2585{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002586 int ret = -ENOTSUPP;
2587
2588 ops->retlen = 0;
2589
2590 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002591 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002592 pr_debug("%s: attempt to write beyond end of device\n",
2593 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002594 return -EINVAL;
2595 }
2596
Huang Shijie6a8214a2012-11-19 14:43:30 +08002597 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002598
Florian Fainellif8ac0412010-09-07 13:23:43 +02002599 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002600 case MTD_OPS_PLACE_OOB:
2601 case MTD_OPS_AUTO_OOB:
2602 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002603 break;
2604
2605 default:
2606 goto out;
2607 }
2608
2609 if (!ops->datbuf)
2610 ret = nand_do_write_oob(mtd, to, ops);
2611 else
2612 ret = nand_do_write_ops(mtd, to, ops);
2613
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002614out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002615 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return ret;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/**
Brian Norris49c50b92014-05-06 16:02:19 -07002620 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002621 * @mtd: MTD device structure
2622 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 *
Brian Norris49c50b92014-05-06 16:02:19 -07002624 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 */
Brian Norris49c50b92014-05-06 16:02:19 -07002626static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002628 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002630 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2631 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002632
2633 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
2636/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002638 * @mtd: MTD device structure
2639 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002641 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002643static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
David Woodhousee0c7d762006-05-13 18:07:53 +01002645 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002649 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002650 * @mtd: MTD device structure
2651 * @instr: erase instruction
2652 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002656int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2657 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Adrian Hunter69423d92008-12-10 13:37:21 +00002659 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002660 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002661 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Brian Norris289c0522011-07-19 10:06:09 -07002663 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2664 __func__, (unsigned long long)instr->addr,
2665 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302667 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002671 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002674 page = (int)(instr->addr >> chip->page_shift);
2675 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002678 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002681 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /* Check, if it is write protected */
2684 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002685 pr_debug("%s: device is write protected!\n",
2686 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 instr->state = MTD_ERASE_FAILED;
2688 goto erase_exit;
2689 }
2690
2691 /* Loop through the pages */
2692 len = instr->len;
2693
2694 instr->state = MTD_ERASING;
2695
2696 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002697 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002698 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2699 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002700 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2701 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 instr->state = MTD_ERASE_FAILED;
2703 goto erase_exit;
2704 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002705
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002706 /*
2707 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002708 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002709 */
2710 if (page <= chip->pagebuf && chip->pagebuf <
2711 (page + pages_per_block))
2712 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Brian Norris49c50b92014-05-06 16:02:19 -07002714 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002716 /*
2717 * See if operation failed and additional status checks are
2718 * available
2719 */
2720 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2721 status = chip->errstat(mtd, chip, FL_ERASING,
2722 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002725 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002726 pr_debug("%s: failed erase, page 0x%08x\n",
2727 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002729 instr->fail_addr =
2730 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 goto erase_exit;
2732 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002735 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 page += pages_per_block;
2737
2738 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002739 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002741 chip->select_chip(mtd, -1);
2742 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 }
2745 instr->state = MTD_ERASE_DONE;
2746
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002747erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002752 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 nand_release_device(mtd);
2754
David Woodhouse49defc02007-10-06 15:01:59 -04002755 /* Do call back function */
2756 if (!ret)
2757 mtd_erase_callback(instr);
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /* Return more or less happy */
2760 return ret;
2761}
2762
2763/**
2764 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002765 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002767 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002769static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Brian Norris289c0522011-07-19 10:06:09 -07002771 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002774 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002776 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002780 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002781 * @mtd: MTD device structure
2782 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002784static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002786 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002790 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002791 * @mtd: MTD device structure
2792 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002794static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 int ret;
2797
Florian Fainellif8ac0412010-09-07 13:23:43 +02002798 ret = nand_block_isbad(mtd, ofs);
2799 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002800 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (ret > 0)
2802 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002803 return ret;
2804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Brian Norris5a0edb22013-07-30 17:52:58 -07002806 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
2809/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002810 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2811 * @mtd: MTD device structure
2812 * @chip: nand chip info structure
2813 * @addr: feature address.
2814 * @subfeature_param: the subfeature parameters, a four bytes array.
2815 */
2816static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2817 int addr, uint8_t *subfeature_param)
2818{
2819 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002820 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08002821
David Mosbergerd914c932013-05-29 15:30:13 +03002822 if (!chip->onfi_version ||
2823 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2824 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002825 return -EINVAL;
2826
2827 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002828 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2829 chip->write_byte(mtd, subfeature_param[i]);
2830
Huang Shijie7db03ec2012-09-13 14:57:52 +08002831 status = chip->waitfunc(mtd, chip);
2832 if (status & NAND_STATUS_FAIL)
2833 return -EIO;
2834 return 0;
2835}
2836
2837/**
2838 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2839 * @mtd: MTD device structure
2840 * @chip: nand chip info structure
2841 * @addr: feature address.
2842 * @subfeature_param: the subfeature parameters, a four bytes array.
2843 */
2844static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2845 int addr, uint8_t *subfeature_param)
2846{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002847 int i;
2848
David Mosbergerd914c932013-05-29 15:30:13 +03002849 if (!chip->onfi_version ||
2850 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2851 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002852 return -EINVAL;
2853
2854 /* clear the sub feature parameters */
2855 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2856
2857 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002858 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2859 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08002860 return 0;
2861}
2862
2863/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002864 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002865 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002866 */
2867static int nand_suspend(struct mtd_info *mtd)
2868{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002869 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002870}
2871
2872/**
2873 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002874 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002875 */
2876static void nand_resume(struct mtd_info *mtd)
2877{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002878 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002879
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002880 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002881 nand_release_device(mtd);
2882 else
Brian Norrisd0370212011-07-19 10:06:08 -07002883 pr_err("%s called for a chip which is not in suspended state\n",
2884 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002885}
2886
Brian Norris8b6e50c2011-05-25 14:59:01 -07002887/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002888static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002891 if (!chip->chip_delay)
2892 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002895 if (chip->cmdfunc == NULL)
2896 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002899 if (chip->waitfunc == NULL)
2900 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002902 if (!chip->select_chip)
2903 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002904
Huang Shijie4204ccc2013-08-16 10:10:07 +08002905 /* set for ONFI nand */
2906 if (!chip->onfi_set_features)
2907 chip->onfi_set_features = nand_onfi_set_features;
2908 if (!chip->onfi_get_features)
2909 chip->onfi_get_features = nand_onfi_get_features;
2910
Brian Norris68e80782013-07-18 01:17:02 -07002911 /* If called twice, pointers that depend on busw may need to be reset */
2912 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002913 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2914 if (!chip->read_word)
2915 chip->read_word = nand_read_word;
2916 if (!chip->block_bad)
2917 chip->block_bad = nand_block_bad;
2918 if (!chip->block_markbad)
2919 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002920 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002921 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01002922 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2923 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07002924 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002925 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002926 if (!chip->scan_bbt)
2927 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002928
2929 if (!chip->controller) {
2930 chip->controller = &chip->hwcontrol;
2931 spin_lock_init(&chip->controller->lock);
2932 init_waitqueue_head(&chip->controller->wq);
2933 }
2934
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002935}
2936
Brian Norris8b6e50c2011-05-25 14:59:01 -07002937/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002938static void sanitize_string(uint8_t *s, size_t len)
2939{
2940 ssize_t i;
2941
Brian Norris8b6e50c2011-05-25 14:59:01 -07002942 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002943 s[len - 1] = 0;
2944
Brian Norris8b6e50c2011-05-25 14:59:01 -07002945 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002946 for (i = 0; i < len - 1; i++) {
2947 if (s[i] < ' ' || s[i] > 127)
2948 s[i] = '?';
2949 }
2950
Brian Norris8b6e50c2011-05-25 14:59:01 -07002951 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002952 strim(s);
2953}
2954
2955static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2956{
2957 int i;
2958 while (len--) {
2959 crc ^= *p++ << 8;
2960 for (i = 0; i < 8; i++)
2961 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2962 }
2963
2964 return crc;
2965}
2966
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002967/* Parse the Extended Parameter Page. */
2968static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2969 struct nand_chip *chip, struct nand_onfi_params *p)
2970{
2971 struct onfi_ext_param_page *ep;
2972 struct onfi_ext_section *s;
2973 struct onfi_ext_ecc_info *ecc;
2974 uint8_t *cursor;
2975 int ret = -EINVAL;
2976 int len;
2977 int i;
2978
2979 len = le16_to_cpu(p->ext_param_page_length) * 16;
2980 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07002981 if (!ep)
2982 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002983
2984 /* Send our own NAND_CMD_PARAM. */
2985 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2986
2987 /* Use the Change Read Column command to skip the ONFI param pages. */
2988 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
2989 sizeof(*p) * p->num_of_param_pages , -1);
2990
2991 /* Read out the Extended Parameter Page. */
2992 chip->read_buf(mtd, (uint8_t *)ep, len);
2993 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
2994 != le16_to_cpu(ep->crc))) {
2995 pr_debug("fail in the CRC.\n");
2996 goto ext_out;
2997 }
2998
2999 /*
3000 * Check the signature.
3001 * Do not strictly follow the ONFI spec, maybe changed in future.
3002 */
3003 if (strncmp(ep->sig, "EPPS", 4)) {
3004 pr_debug("The signature is invalid.\n");
3005 goto ext_out;
3006 }
3007
3008 /* find the ECC section. */
3009 cursor = (uint8_t *)(ep + 1);
3010 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3011 s = ep->sections + i;
3012 if (s->type == ONFI_SECTION_TYPE_2)
3013 break;
3014 cursor += s->length * 16;
3015 }
3016 if (i == ONFI_EXT_SECTION_MAX) {
3017 pr_debug("We can not find the ECC section.\n");
3018 goto ext_out;
3019 }
3020
3021 /* get the info we want. */
3022 ecc = (struct onfi_ext_ecc_info *)cursor;
3023
Brian Norris4ae7d222013-09-16 18:20:21 -07003024 if (!ecc->codeword_size) {
3025 pr_debug("Invalid codeword size\n");
3026 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003027 }
3028
Brian Norris4ae7d222013-09-16 18:20:21 -07003029 chip->ecc_strength_ds = ecc->ecc_bits;
3030 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003031 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003032
3033ext_out:
3034 kfree(ep);
3035 return ret;
3036}
3037
Brian Norris8429bb32013-12-03 15:51:09 -08003038static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3039{
3040 struct nand_chip *chip = mtd->priv;
3041 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3042
3043 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3044 feature);
3045}
3046
3047/*
3048 * Configure chip properties from Micron vendor-specific ONFI table
3049 */
3050static void nand_onfi_detect_micron(struct nand_chip *chip,
3051 struct nand_onfi_params *p)
3052{
3053 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3054
3055 if (le16_to_cpu(p->vendor_revision) < 1)
3056 return;
3057
3058 chip->read_retries = micron->read_retry_options;
3059 chip->setup_read_retry = nand_setup_read_retry_micron;
3060}
3061
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003062/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003063 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003064 */
3065static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003066 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003067{
3068 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003069 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003070 int val;
3071
Brian Norris7854d3f2011-06-23 14:12:08 -07003072 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003073 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3074 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3075 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3076 return 0;
3077
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003078 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3079 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003080 for (j = 0; j < sizeof(*p); j++)
3081 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003082 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3083 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003084 break;
3085 }
3086 }
3087
Brian Norrisc7f23a72013-08-13 10:51:55 -07003088 if (i == 3) {
3089 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003090 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003091 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003092
Brian Norris8b6e50c2011-05-25 14:59:01 -07003093 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003094 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003095 if (val & (1 << 5))
3096 chip->onfi_version = 23;
3097 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003098 chip->onfi_version = 22;
3099 else if (val & (1 << 3))
3100 chip->onfi_version = 21;
3101 else if (val & (1 << 2))
3102 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003103 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003104 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003105
3106 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003107 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003108 return 0;
3109 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003110
3111 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3112 sanitize_string(p->model, sizeof(p->model));
3113 if (!mtd->name)
3114 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003115
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003116 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003117
3118 /*
3119 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3120 * (don't ask me who thought of this...). MTD assumes that these
3121 * dimensions will be power-of-2, so just truncate the remaining area.
3122 */
3123 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3124 mtd->erasesize *= mtd->writesize;
3125
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003126 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003127
3128 /* See erasesize comment */
3129 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003130 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003131 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003132
3133 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003134 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003135 else
3136 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003137
Huang Shijie10c86ba2013-05-17 11:17:26 +08003138 if (p->ecc_bits != 0xff) {
3139 chip->ecc_strength_ds = p->ecc_bits;
3140 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003141 } else if (chip->onfi_version >= 21 &&
3142 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3143
3144 /*
3145 * The nand_flash_detect_ext_param_page() uses the
3146 * Change Read Column command which maybe not supported
3147 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3148 * now. We do not replace user supplied command function.
3149 */
3150 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3151 chip->cmdfunc = nand_command_lp;
3152
3153 /* The Extended Parameter Page is supported since ONFI 2.1. */
3154 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003155 pr_warn("Failed to detect ONFI extended param page\n");
3156 } else {
3157 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003158 }
3159
Brian Norris8429bb32013-12-03 15:51:09 -08003160 if (p->jedec_id == NAND_MFR_MICRON)
3161 nand_onfi_detect_micron(chip, p);
3162
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003163 return 1;
3164}
3165
3166/*
Huang Shijie91361812014-02-21 13:39:40 +08003167 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3168 */
3169static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3170 int *busw)
3171{
3172 struct nand_jedec_params *p = &chip->jedec_params;
3173 struct jedec_ecc_info *ecc;
3174 int val;
3175 int i, j;
3176
3177 /* Try JEDEC for unknown chip or LP */
3178 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3179 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3180 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3181 chip->read_byte(mtd) != 'C')
3182 return 0;
3183
3184 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3185 for (i = 0; i < 3; i++) {
3186 for (j = 0; j < sizeof(*p); j++)
3187 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3188
3189 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3190 le16_to_cpu(p->crc))
3191 break;
3192 }
3193
3194 if (i == 3) {
3195 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3196 return 0;
3197 }
3198
3199 /* Check version */
3200 val = le16_to_cpu(p->revision);
3201 if (val & (1 << 2))
3202 chip->jedec_version = 10;
3203 else if (val & (1 << 1))
3204 chip->jedec_version = 1; /* vendor specific version */
3205
3206 if (!chip->jedec_version) {
3207 pr_info("unsupported JEDEC version: %d\n", val);
3208 return 0;
3209 }
3210
3211 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3212 sanitize_string(p->model, sizeof(p->model));
3213 if (!mtd->name)
3214 mtd->name = p->model;
3215
3216 mtd->writesize = le32_to_cpu(p->byte_per_page);
3217
3218 /* Please reference to the comment for nand_flash_detect_onfi. */
3219 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3220 mtd->erasesize *= mtd->writesize;
3221
3222 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3223
3224 /* Please reference to the comment for nand_flash_detect_onfi. */
3225 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3226 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3227 chip->bits_per_cell = p->bits_per_cell;
3228
3229 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3230 *busw = NAND_BUSWIDTH_16;
3231 else
3232 *busw = 0;
3233
3234 /* ECC info */
3235 ecc = &p->ecc_info[0];
3236
3237 if (ecc->codeword_size >= 9) {
3238 chip->ecc_strength_ds = ecc->ecc_bits;
3239 chip->ecc_step_ds = 1 << ecc->codeword_size;
3240 } else {
3241 pr_warn("Invalid codeword size\n");
3242 }
3243
3244 return 1;
3245}
3246
3247/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003248 * nand_id_has_period - Check if an ID string has a given wraparound period
3249 * @id_data: the ID string
3250 * @arrlen: the length of the @id_data array
3251 * @period: the period of repitition
3252 *
3253 * Check if an ID string is repeated within a given sequence of bytes at
3254 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003255 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003256 * if the repetition has a period of @period; otherwise, returns zero.
3257 */
3258static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3259{
3260 int i, j;
3261 for (i = 0; i < period; i++)
3262 for (j = i + period; j < arrlen; j += period)
3263 if (id_data[i] != id_data[j])
3264 return 0;
3265 return 1;
3266}
3267
3268/*
3269 * nand_id_len - Get the length of an ID string returned by CMD_READID
3270 * @id_data: the ID string
3271 * @arrlen: the length of the @id_data array
3272
3273 * Returns the length of the ID string, according to known wraparound/trailing
3274 * zero patterns. If no pattern exists, returns the length of the array.
3275 */
3276static int nand_id_len(u8 *id_data, int arrlen)
3277{
3278 int last_nonzero, period;
3279
3280 /* Find last non-zero byte */
3281 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3282 if (id_data[last_nonzero])
3283 break;
3284
3285 /* All zeros */
3286 if (last_nonzero < 0)
3287 return 0;
3288
3289 /* Calculate wraparound period */
3290 for (period = 1; period < arrlen; period++)
3291 if (nand_id_has_period(id_data, arrlen, period))
3292 break;
3293
3294 /* There's a repeated pattern */
3295 if (period < arrlen)
3296 return period;
3297
3298 /* There are trailing zeros */
3299 if (last_nonzero < arrlen - 1)
3300 return last_nonzero + 1;
3301
3302 /* No pattern detected */
3303 return arrlen;
3304}
3305
Huang Shijie7db906b2013-09-25 14:58:11 +08003306/* Extract the bits of per cell from the 3rd byte of the extended ID */
3307static int nand_get_bits_per_cell(u8 cellinfo)
3308{
3309 int bits;
3310
3311 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3312 bits >>= NAND_CI_CELLTYPE_SHIFT;
3313 return bits + 1;
3314}
3315
Brian Norrise3b88bd2012-09-24 20:40:52 -07003316/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003317 * Many new NAND share similar device ID codes, which represent the size of the
3318 * chip. The rest of the parameters must be decoded according to generic or
3319 * manufacturer-specific "extended ID" decoding patterns.
3320 */
3321static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3322 u8 id_data[8], int *busw)
3323{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003324 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003325 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003326 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003327 /* The 4th id byte is the important one */
3328 extid = id_data[3];
3329
Brian Norrise3b88bd2012-09-24 20:40:52 -07003330 id_len = nand_id_len(id_data, 8);
3331
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003332 /*
3333 * Field definitions are in the following datasheets:
3334 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003335 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003336 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003337 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003338 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3339 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003340 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003341 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003342 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003343 /* Calc pagesize */
3344 mtd->writesize = 2048 << (extid & 0x03);
3345 extid >>= 2;
3346 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003347 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003348 case 1:
3349 mtd->oobsize = 128;
3350 break;
3351 case 2:
3352 mtd->oobsize = 218;
3353 break;
3354 case 3:
3355 mtd->oobsize = 400;
3356 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003357 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003358 mtd->oobsize = 436;
3359 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003360 case 5:
3361 mtd->oobsize = 512;
3362 break;
3363 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003364 mtd->oobsize = 640;
3365 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003366 case 7:
3367 default: /* Other cases are "reserved" (unknown) */
3368 mtd->oobsize = 1024;
3369 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003370 }
3371 extid >>= 2;
3372 /* Calc blocksize */
3373 mtd->erasesize = (128 * 1024) <<
3374 (((extid >> 1) & 0x04) | (extid & 0x03));
3375 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003376 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003377 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003378 unsigned int tmp;
3379
3380 /* Calc pagesize */
3381 mtd->writesize = 2048 << (extid & 0x03);
3382 extid >>= 2;
3383 /* Calc oobsize */
3384 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3385 case 0:
3386 mtd->oobsize = 128;
3387 break;
3388 case 1:
3389 mtd->oobsize = 224;
3390 break;
3391 case 2:
3392 mtd->oobsize = 448;
3393 break;
3394 case 3:
3395 mtd->oobsize = 64;
3396 break;
3397 case 4:
3398 mtd->oobsize = 32;
3399 break;
3400 case 5:
3401 mtd->oobsize = 16;
3402 break;
3403 default:
3404 mtd->oobsize = 640;
3405 break;
3406 }
3407 extid >>= 2;
3408 /* Calc blocksize */
3409 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3410 if (tmp < 0x03)
3411 mtd->erasesize = (128 * 1024) << tmp;
3412 else if (tmp == 0x03)
3413 mtd->erasesize = 768 * 1024;
3414 else
3415 mtd->erasesize = (64 * 1024) << tmp;
3416 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003417 } else {
3418 /* Calc pagesize */
3419 mtd->writesize = 1024 << (extid & 0x03);
3420 extid >>= 2;
3421 /* Calc oobsize */
3422 mtd->oobsize = (8 << (extid & 0x01)) *
3423 (mtd->writesize >> 9);
3424 extid >>= 2;
3425 /* Calc blocksize. Blocksize is multiples of 64KiB */
3426 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3427 extid >>= 2;
3428 /* Get buswidth information */
3429 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003430
3431 /*
3432 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3433 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3434 * follows:
3435 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3436 * 110b -> 24nm
3437 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3438 */
3439 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003440 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003441 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3442 !(id_data[4] & 0x80) /* !BENAND */) {
3443 mtd->oobsize = 32 * mtd->writesize >> 9;
3444 }
3445
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003446 }
3447}
3448
3449/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003450 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3451 * decodes a matching ID table entry and assigns the MTD size parameters for
3452 * the chip.
3453 */
3454static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3455 struct nand_flash_dev *type, u8 id_data[8],
3456 int *busw)
3457{
3458 int maf_id = id_data[0];
3459
3460 mtd->erasesize = type->erasesize;
3461 mtd->writesize = type->pagesize;
3462 mtd->oobsize = mtd->writesize / 32;
3463 *busw = type->options & NAND_BUSWIDTH_16;
3464
Huang Shijie1c195e92013-09-25 14:58:12 +08003465 /* All legacy ID NAND are small-page, SLC */
3466 chip->bits_per_cell = 1;
3467
Brian Norrisf23a4812012-09-24 20:40:51 -07003468 /*
3469 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3470 * some Spansion chips have erasesize that conflicts with size
3471 * listed in nand_ids table.
3472 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3473 */
3474 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3475 && id_data[6] == 0x00 && id_data[7] == 0x00
3476 && mtd->writesize == 512) {
3477 mtd->erasesize = 128 * 1024;
3478 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3479 }
3480}
3481
3482/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003483 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3484 * heuristic patterns using various detected parameters (e.g., manufacturer,
3485 * page size, cell-type information).
3486 */
3487static void nand_decode_bbm_options(struct mtd_info *mtd,
3488 struct nand_chip *chip, u8 id_data[8])
3489{
3490 int maf_id = id_data[0];
3491
3492 /* Set the bad block position */
3493 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3494 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3495 else
3496 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3497
3498 /*
3499 * Bad block marker is stored in the last page of each block on Samsung
3500 * and Hynix MLC devices; stored in first two pages of each block on
3501 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3502 * AMD/Spansion, and Macronix. All others scan only the first page.
3503 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003504 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003505 (maf_id == NAND_MFR_SAMSUNG ||
3506 maf_id == NAND_MFR_HYNIX))
3507 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003508 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003509 (maf_id == NAND_MFR_SAMSUNG ||
3510 maf_id == NAND_MFR_HYNIX ||
3511 maf_id == NAND_MFR_TOSHIBA ||
3512 maf_id == NAND_MFR_AMD ||
3513 maf_id == NAND_MFR_MACRONIX)) ||
3514 (mtd->writesize == 2048 &&
3515 maf_id == NAND_MFR_MICRON))
3516 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3517}
3518
Huang Shijieec6e87e2013-03-15 11:01:00 +08003519static inline bool is_full_id_nand(struct nand_flash_dev *type)
3520{
3521 return type->id_len;
3522}
3523
3524static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3525 struct nand_flash_dev *type, u8 *id_data, int *busw)
3526{
3527 if (!strncmp(type->id, id_data, type->id_len)) {
3528 mtd->writesize = type->pagesize;
3529 mtd->erasesize = type->erasesize;
3530 mtd->oobsize = type->oobsize;
3531
Huang Shijie7db906b2013-09-25 14:58:11 +08003532 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003533 chip->chipsize = (uint64_t)type->chipsize << 20;
3534 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003535 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3536 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003537
3538 *busw = type->options & NAND_BUSWIDTH_16;
3539
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003540 if (!mtd->name)
3541 mtd->name = type->name;
3542
Huang Shijieec6e87e2013-03-15 11:01:00 +08003543 return true;
3544 }
3545 return false;
3546}
3547
Brian Norris7e74c2d2012-09-24 20:40:49 -07003548/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003549 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003550 */
3551static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003552 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003553 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003554 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003555{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003556 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003557 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003558 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003561 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Karl Beldanef89a882008-09-15 14:37:29 +02003563 /*
3564 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003565 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003566 */
3567 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003570 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003573 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003574 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Brian Norris8b6e50c2011-05-25 14:59:01 -07003576 /*
3577 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003578 * interface concerns can cause random data which looks like a
3579 * possibly credible NAND flash to appear. If the two results do
3580 * not match, ignore the device completely.
3581 */
3582
3583 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3584
Brian Norris4aef9b72012-09-24 20:40:48 -07003585 /* Read entire ID string */
3586 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003587 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003588
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003589 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003590 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003591 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003592 return ERR_PTR(-ENODEV);
3593 }
3594
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003595 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003596 type = nand_flash_ids;
3597
Huang Shijieec6e87e2013-03-15 11:01:00 +08003598 for (; type->name != NULL; type++) {
3599 if (is_full_id_nand(type)) {
3600 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3601 goto ident_done;
3602 } else if (*dev_id == type->dev_id) {
3603 break;
3604 }
3605 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003606
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003607 chip->onfi_version = 0;
3608 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003609 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003610 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003611 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003612
3613 /* Check if the chip is JEDEC compliant */
3614 if (nand_flash_detect_jedec(mtd, chip, &busw))
3615 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003616 }
3617
David Woodhouse5e81e882010-02-26 18:32:56 +00003618 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003619 return ERR_PTR(-ENODEV);
3620
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003621 if (!mtd->name)
3622 mtd->name = type->name;
3623
Adrian Hunter69423d92008-12-10 13:37:21 +00003624 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003625
Huang Shijie12a40a52010-09-27 10:43:53 +08003626 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003627 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003628 busw = chip->init_size(mtd, chip, id_data);
3629 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003630 /* Decode parameters from extended ID */
3631 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003632 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003633 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003634 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003635 /* Get chip options */
3636 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003637
Brian Norris8b6e50c2011-05-25 14:59:01 -07003638 /*
3639 * Check if chip is not a Samsung device. Do not clear the
3640 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003641 */
3642 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3643 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3644ident_done:
3645
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003646 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003647 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003648 if (nand_manuf_ids[maf_idx].id == *maf_id)
3649 break;
3650 }
3651
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003652 if (chip->options & NAND_BUSWIDTH_AUTO) {
3653 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3654 chip->options |= busw;
3655 nand_set_defaults(chip, busw);
3656 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3657 /*
3658 * Check, if buswidth is correct. Hardware drivers should set
3659 * chip correct!
3660 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003661 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3662 *maf_id, *dev_id);
3663 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3664 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003665 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3666 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003667 return ERR_PTR(-EINVAL);
3668 }
3669
Brian Norris7e74c2d2012-09-24 20:40:49 -07003670 nand_decode_bbm_options(mtd, chip, id_data);
3671
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003672 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003673 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003674 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003675 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003676
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003677 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003678 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003679 if (chip->chipsize & 0xffffffff)
3680 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003681 else {
3682 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3683 chip->chip_shift += 32 - 1;
3684 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003685
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003686 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003687 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003688
Brian Norris8b6e50c2011-05-25 14:59:01 -07003689 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003690 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3691 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003692
Ezequiel Garcia20171642013-11-25 08:30:31 -03003693 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3694 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003695
3696 if (chip->onfi_version)
3697 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3698 chip->onfi_params.model);
3699 else if (chip->jedec_version)
3700 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3701 chip->jedec_params.model);
3702 else
3703 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3704 type->name);
3705
Ezequiel Garcia20171642013-11-25 08:30:31 -03003706 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003707 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3708 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003709 return type;
3710}
3711
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003712/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003713 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003714 * @mtd: MTD device structure
3715 * @maxchips: number of chips to scan for
3716 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003717 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003718 * This is the first phase of the normal nand_scan() function. It reads the
3719 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003720 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003721 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003722 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003723int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3724 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003725{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003726 int i, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003727 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003728 struct nand_flash_dev *type;
3729
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003730 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003731 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003732
3733 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003734 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3735 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003736
3737 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003738 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003739 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003740 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003741 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
Huang Shijie07300162012-11-09 16:23:45 +08003744 chip->select_chip(mtd, -1);
3745
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003746 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003747 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003748 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003749 /* See comment in nand_get_flash_type for reset */
3750 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003752 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003754 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003755 nand_dev_id != chip->read_byte(mtd)) {
3756 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 break;
Huang Shijie07300162012-11-09 16:23:45 +08003758 }
3759 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 }
3761 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003762 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003765 chip->numchips = i;
3766 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
David Woodhouse3b85c322006-09-25 17:06:53 +01003768 return 0;
3769}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003770EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003771
3772
3773/**
3774 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003775 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003776 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003777 * This is the second phase of the normal nand_scan() function. It fills out
3778 * all the uninitialized function pointers with the defaults and scans for a
3779 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003780 */
3781int nand_scan_tail(struct mtd_info *mtd)
3782{
3783 int i;
3784 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003785 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08003786 struct nand_buffers *nbuf;
David Woodhouse3b85c322006-09-25 17:06:53 +01003787
Brian Norrise2414f42012-02-06 13:44:00 -08003788 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3789 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3790 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3791
Huang Shijief02ea4e2014-01-13 14:27:12 +08003792 if (!(chip->options & NAND_OWN_BUFFERS)) {
3793 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3794 + mtd->oobsize * 3, GFP_KERNEL);
3795 if (!nbuf)
3796 return -ENOMEM;
3797 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3798 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3799 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3800
3801 chip->buffers = nbuf;
3802 } else {
3803 if (!chip->buffers)
3804 return -ENOMEM;
3805 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003806
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003807 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003808 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003809
3810 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003811 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003812 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003813 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003814 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003816 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 break;
3818 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003819 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 break;
3821 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003822 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003824 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003825 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003826 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003828 pr_warn("No oob scheme defined for oobsize %d\n",
3829 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 BUG();
3831 }
3832 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003833
David Woodhouse956e9442006-09-25 17:12:39 +01003834 if (!chip->write_page)
3835 chip->write_page = nand_write_page;
3836
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003837 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003838 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003839 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003840 */
David Woodhouse956e9442006-09-25 17:12:39 +01003841
Huang Shijie97de79e02013-10-18 14:20:53 +08003842 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003843 case NAND_ECC_HW_OOB_FIRST:
3844 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003845 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003846 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003847 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003848 BUG();
3849 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003850 if (!ecc->read_page)
3851 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003852
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003853 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003854 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003855 if (!ecc->read_page)
3856 ecc->read_page = nand_read_page_hwecc;
3857 if (!ecc->write_page)
3858 ecc->write_page = nand_write_page_hwecc;
3859 if (!ecc->read_page_raw)
3860 ecc->read_page_raw = nand_read_page_raw;
3861 if (!ecc->write_page_raw)
3862 ecc->write_page_raw = nand_write_page_raw;
3863 if (!ecc->read_oob)
3864 ecc->read_oob = nand_read_oob_std;
3865 if (!ecc->write_oob)
3866 ecc->write_oob = nand_write_oob_std;
3867 if (!ecc->read_subpage)
3868 ecc->read_subpage = nand_read_subpage;
3869 if (!ecc->write_subpage)
3870 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003871
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003872 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003873 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3874 (!ecc->read_page ||
3875 ecc->read_page == nand_read_page_hwecc ||
3876 !ecc->write_page ||
3877 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003878 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003879 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003880 BUG();
3881 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003882 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003883 if (!ecc->read_page)
3884 ecc->read_page = nand_read_page_syndrome;
3885 if (!ecc->write_page)
3886 ecc->write_page = nand_write_page_syndrome;
3887 if (!ecc->read_page_raw)
3888 ecc->read_page_raw = nand_read_page_raw_syndrome;
3889 if (!ecc->write_page_raw)
3890 ecc->write_page_raw = nand_write_page_raw_syndrome;
3891 if (!ecc->read_oob)
3892 ecc->read_oob = nand_read_oob_syndrome;
3893 if (!ecc->write_oob)
3894 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003895
Huang Shijie97de79e02013-10-18 14:20:53 +08003896 if (mtd->writesize >= ecc->size) {
3897 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003898 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3899 BUG();
3900 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003901 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003902 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003903 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003904 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003905 ecc->size, mtd->writesize);
3906 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003908 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003909 ecc->calculate = nand_calculate_ecc;
3910 ecc->correct = nand_correct_data;
3911 ecc->read_page = nand_read_page_swecc;
3912 ecc->read_subpage = nand_read_subpage;
3913 ecc->write_page = nand_write_page_swecc;
3914 ecc->read_page_raw = nand_read_page_raw;
3915 ecc->write_page_raw = nand_write_page_raw;
3916 ecc->read_oob = nand_read_oob_std;
3917 ecc->write_oob = nand_write_oob_std;
3918 if (!ecc->size)
3919 ecc->size = 256;
3920 ecc->bytes = 3;
3921 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003923
Ivan Djelic193bd402011-03-11 11:05:33 +01003924 case NAND_ECC_SOFT_BCH:
3925 if (!mtd_nand_has_bch()) {
Erico Nunes148256f2014-03-11 01:31:26 -03003926 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003927 BUG();
3928 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003929 ecc->calculate = nand_bch_calculate_ecc;
3930 ecc->correct = nand_bch_correct_data;
3931 ecc->read_page = nand_read_page_swecc;
3932 ecc->read_subpage = nand_read_subpage;
3933 ecc->write_page = nand_write_page_swecc;
3934 ecc->read_page_raw = nand_read_page_raw;
3935 ecc->write_page_raw = nand_write_page_raw;
3936 ecc->read_oob = nand_read_oob_std;
3937 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01003938 /*
3939 * Board driver should supply ecc.size and ecc.bytes values to
3940 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003941 * for details. Otherwise, default to 4 bits for large page
3942 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003943 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003944 if (!ecc->size && (mtd->oobsize >= 64)) {
3945 ecc->size = 512;
3946 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01003947 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003948 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3949 &ecc->layout);
3950 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003951 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003952 BUG();
3953 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003954 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003955 break;
3956
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003957 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003958 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003959 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08003960 ecc->read_page = nand_read_page_raw;
3961 ecc->write_page = nand_write_page_raw;
3962 ecc->read_oob = nand_read_oob_std;
3963 ecc->read_page_raw = nand_read_page_raw;
3964 ecc->write_page_raw = nand_write_page_raw;
3965 ecc->write_oob = nand_write_oob_std;
3966 ecc->size = mtd->writesize;
3967 ecc->bytes = 0;
3968 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08003972 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003973 BUG();
3974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
Brian Norris9ce244b2011-08-30 18:45:37 -07003976 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08003977 if (!ecc->read_oob_raw)
3978 ecc->read_oob_raw = ecc->read_oob;
3979 if (!ecc->write_oob_raw)
3980 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003981
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003982 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003983 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003984 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003985 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003986 ecc->layout->oobavail = 0;
3987 for (i = 0; ecc->layout->oobfree[i].length
3988 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
3989 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
3990 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003991
3992 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003993 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003994 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003995 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003996 ecc->steps = mtd->writesize / ecc->size;
3997 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003998 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003999 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004001 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004002
Brian Norris8b6e50c2011-05-25 14:59:01 -07004003 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004004 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004005 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004006 case 2:
4007 mtd->subpage_sft = 1;
4008 break;
4009 case 4:
4010 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004011 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004012 mtd->subpage_sft = 2;
4013 break;
4014 }
4015 }
4016 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4017
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004018 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004019 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004022 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004024 /* Large page NAND with SOFT_ECC should support subpage reads */
Huang Shijie97de79e02013-10-18 14:20:53 +08004025 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004026 chip->options |= NAND_SUBPAGE_READ;
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004029 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004030 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4031 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004032 mtd->_erase = nand_erase;
4033 mtd->_point = NULL;
4034 mtd->_unpoint = NULL;
4035 mtd->_read = nand_read;
4036 mtd->_write = nand_write;
4037 mtd->_panic_write = panic_nand_write;
4038 mtd->_read_oob = nand_read_oob;
4039 mtd->_write_oob = nand_write_oob;
4040 mtd->_sync = nand_sync;
4041 mtd->_lock = NULL;
4042 mtd->_unlock = NULL;
4043 mtd->_suspend = nand_suspend;
4044 mtd->_resume = nand_resume;
4045 mtd->_block_isbad = nand_block_isbad;
4046 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004047 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Mike Dunn6a918ba2012-03-11 14:21:11 -07004049 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08004050 mtd->ecclayout = ecc->layout;
4051 mtd->ecc_strength = ecc->strength;
4052 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004053 /*
4054 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4055 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4056 * properly set.
4057 */
4058 if (!mtd->bitflip_threshold)
4059 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004061 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004062 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
4065 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004066 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004068EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Brian Norris8b6e50c2011-05-25 14:59:01 -07004070/*
4071 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004072 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004073 * to call us from in-kernel code if the core NAND support is modular.
4074 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004075#ifdef MODULE
4076#define caller_is_module() (1)
4077#else
4078#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004079 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004080#endif
4081
4082/**
4083 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004084 * @mtd: MTD device structure
4085 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004086 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004087 * This fills out all the uninitialized function pointers with the defaults.
4088 * The flash ID is read and the mtd/chip structures are filled with the
4089 * appropriate values. The mtd->owner field must be set to the module of the
4090 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004091 */
4092int nand_scan(struct mtd_info *mtd, int maxchips)
4093{
4094 int ret;
4095
4096 /* Many callers got this wrong, so check for it for a while... */
4097 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004098 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004099 BUG();
4100 }
4101
David Woodhouse5e81e882010-02-26 18:32:56 +00004102 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004103 if (!ret)
4104 ret = nand_scan_tail(mtd);
4105 return ret;
4106}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004107EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004110 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004111 * @mtd: MTD device structure
4112 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004113void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004115 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Ivan Djelic193bd402011-03-11 11:05:33 +01004117 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4118 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4119
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004120 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Jesper Juhlfa671642005-11-07 01:01:27 -08004122 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004123 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004124 if (!(chip->options & NAND_OWN_BUFFERS))
4125 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004126
4127 /* Free bad block descriptor memory */
4128 if (chip->badblock_pattern && chip->badblock_pattern->options
4129 & NAND_BBT_DYNAMICSTRUCT)
4130 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
David Woodhousee0c7d762006-05-13 18:07:53 +01004132EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004133
4134static int __init nand_base_init(void)
4135{
4136 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4137 return 0;
4138}
4139
4140static void __exit nand_base_exit(void)
4141{
4142 led_trigger_unregister_simple(nand_led_trigger);
4143}
4144
4145module_init(nand_base_init);
4146module_exit(nand_base_exit);
4147
David Woodhousee0c7d762006-05-13 18:07:53 +01004148MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004149MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4150MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004151MODULE_DESCRIPTION("Generic NAND flash driver code");