blob: b8c74cda76255fbc3876ea93208aa839dd7fb765 [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/**
207 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700208 * @mtd: MTD device structure
209 * @buf: data buffer
210 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700212 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200214static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200216 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Alexander Shiyan76413832013-04-13 09:32:13 +0400218 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000222 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700223 * @mtd: MTD device structure
224 * @buf: buffer to store date
225 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700227 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200229static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200231 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alexander Shiyan76413832013-04-13 09:32:13 +0400233 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700238 * @mtd: MTD device structure
239 * @buf: data buffer
240 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700242 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200244static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200246 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000248
Alexander Shiyan76413832013-04-13 09:32:13 +0400249 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000253 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700254 * @mtd: MTD device structure
255 * @buf: buffer to store date
256 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700258 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200260static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200262 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Alexander Shiyan76413832013-04-13 09:32:13 +0400265 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700270 * @mtd: MTD device structure
271 * @ofs: offset from device start
272 * @getchip: 0, if the chip is already selected
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000274 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
277{
Brian Norriscdbec052012-01-13 18:11:48 -0800278 int page, chipnr, res = 0, i = 0;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200279 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 u16 bad;
281
Brian Norris5fb15492011-05-31 16:31:21 -0700282 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700283 ofs += mtd->erasesize - mtd->writesize;
284
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100285 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (getchip) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200288 chipnr = (int)(ofs >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Huang Shijie6a8214a2012-11-19 14:43:30 +0800290 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200293 chip->select_chip(mtd, chipnr);
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Brian Norriscdbec052012-01-13 18:11:48 -0800296 do {
297 if (chip->options & NAND_BUSWIDTH_16) {
298 chip->cmdfunc(mtd, NAND_CMD_READOOB,
299 chip->badblockpos & 0xFE, page);
300 bad = cpu_to_le16(chip->read_word(mtd));
301 if (chip->badblockpos & 0x1)
302 bad >>= 8;
303 else
304 bad &= 0xFF;
305 } else {
306 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
307 page);
308 bad = chip->read_byte(mtd);
309 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310
Brian Norriscdbec052012-01-13 18:11:48 -0800311 if (likely(chip->badblockbits == 8))
312 res = bad != 0xFF;
313 else
314 res = hweight8(bad) < chip->badblockbits;
315 ofs += mtd->writesize;
316 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
317 i++;
318 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200319
Huang Shijieb0bb6902012-11-19 14:43:29 +0800320 if (getchip) {
321 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 nand_release_device(mtd);
Huang Shijieb0bb6902012-11-19 14:43:29 +0800323 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return res;
326}
327
328/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700329 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700330 * @mtd: MTD device structure
331 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700333 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700334 * specific driver. It provides the details for writing a bad block marker to a
335 * block.
336 */
337static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
338{
339 struct nand_chip *chip = mtd->priv;
340 struct mtd_oob_ops ops;
341 uint8_t buf[2] = { 0, 0 };
342 int ret = 0, res, i = 0;
343
344 ops.datbuf = NULL;
345 ops.oobbuf = buf;
346 ops.ooboffs = chip->badblockpos;
347 if (chip->options & NAND_BUSWIDTH_16) {
348 ops.ooboffs &= ~0x01;
349 ops.len = ops.ooblen = 2;
350 } else {
351 ops.len = ops.ooblen = 1;
352 }
353 ops.mode = MTD_OPS_PLACE_OOB;
354
355 /* Write to first/last page(s) if necessary */
356 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
357 ofs += mtd->erasesize - mtd->writesize;
358 do {
359 res = nand_do_write_oob(mtd, ofs, &ops);
360 if (!ret)
361 ret = res;
362
363 i++;
364 ofs += mtd->writesize;
365 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
366
367 return ret;
368}
369
370/**
371 * nand_block_markbad_lowlevel - mark a block bad
372 * @mtd: MTD device structure
373 * @ofs: offset from device start
374 *
375 * This function performs the generic NAND bad block marking steps (i.e., bad
376 * block table(s) and/or marker(s)). We only allow the hardware driver to
377 * specify how to write bad block markers to OOB (chip->block_markbad).
378 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700379 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800380 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700381 * (2) write bad block marker to OOB area of affected block (unless flag
382 * NAND_BBT_NO_OOB_BBM is present)
383 * (3) update the BBT
384 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800385 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700387static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200389 struct nand_chip *chip = mtd->priv;
Brian Norrisb32843b2013-07-30 17:52:59 -0700390 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000391
Brian Norrisb32843b2013-07-30 17:52:59 -0700392 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800393 struct erase_info einfo;
394
395 /* Attempt erase before marking OOB */
396 memset(&einfo, 0, sizeof(einfo));
397 einfo.mtd = mtd;
398 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300399 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800400 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800401
Brian Norrisb32843b2013-07-30 17:52:59 -0700402 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800403 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700404 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300405 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200406 }
Brian Norrise2414f42012-02-06 13:44:00 -0800407
Brian Norrisb32843b2013-07-30 17:52:59 -0700408 /* Mark block bad in BBT */
409 if (chip->bbt) {
410 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800411 if (!ret)
412 ret = res;
413 }
414
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200415 if (!ret)
416 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300417
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200418 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000421/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700423 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700425 * Check, if the device is write protected. The function expects, that the
426 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100428static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200430 struct nand_chip *chip = mtd->priv;
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200431
Brian Norris8b6e50c2011-05-25 14:59:01 -0700432 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200433 if (chip->options & NAND_BROKEN_XD)
434 return 0;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200437 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
438 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/**
442 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Brian Norris8b6e50c2011-05-25 14:59:01 -0700443 * @mtd: MTD device structure
444 * @ofs: offset from device start
445 * @getchip: 0, if the chip is already selected
446 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 * Check, if the block is bad. Either by reading the bad block table or
449 * calling of the scan function.
450 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200451static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
452 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200454 struct nand_chip *chip = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200456 if (!chip->bbt)
457 return chip->block_bad(mtd, ofs, getchip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100460 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200463/**
464 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700465 * @mtd: MTD device structure
466 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200467 *
468 * Helper function for nand_wait_ready used when needing to wait in interrupt
469 * context.
470 */
471static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
472{
473 struct nand_chip *chip = mtd->priv;
474 int i;
475
476 /* Wait for the device to get ready */
477 for (i = 0; i < timeo; i++) {
478 if (chip->dev_ready(mtd))
479 break;
480 touch_softlockup_watchdog();
481 mdelay(1);
482 }
483}
484
Brian Norris7854d3f2011-06-23 14:12:08 -0700485/* Wait for the ready pin, after a command. The timeout is caught later. */
David Woodhouse4b648b02006-09-25 17:05:24 +0100486void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000487{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200488 struct nand_chip *chip = mtd->priv;
Matthieu CASTETca6a2482012-11-22 18:31:28 +0100489 unsigned long timeo = jiffies + msecs_to_jiffies(20);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000490
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200491 /* 400ms timeout */
492 if (in_interrupt() || oops_in_progress)
493 return panic_nand_wait_ready(mtd, 400);
494
Richard Purdie8fe833c2006-03-31 02:31:14 -0800495 led_trigger_event(nand_led_trigger, LED_FULL);
Brian Norris7854d3f2011-06-23 14:12:08 -0700496 /* Wait until command is processed or timeout occurs */
Thomas Gleixner3b887752005-02-22 21:56:49 +0000497 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200498 if (chip->dev_ready(mtd))
Richard Purdie8fe833c2006-03-31 02:31:14 -0800499 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700500 touch_softlockup_watchdog();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000501 } while (time_before(jiffies, timeo));
Richard Purdie8fe833c2006-03-31 02:31:14 -0800502 led_trigger_event(nand_led_trigger, LED_OFF);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000503}
David Woodhouse4b648b02006-09-25 17:05:24 +0100504EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/**
507 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700508 * @mtd: MTD device structure
509 * @command: the command to be sent
510 * @column: the column address for this command, -1 if none
511 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700513 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200514 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200516static void nand_command(struct mtd_info *mtd, unsigned int command,
517 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200519 register struct nand_chip *chip = mtd->priv;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200520 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Brian Norris8b6e50c2011-05-25 14:59:01 -0700522 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (command == NAND_CMD_SEQIN) {
524 int readcmd;
525
Joern Engel28318772006-05-22 23:18:05 +0200526 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200528 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 readcmd = NAND_CMD_READOOB;
530 } else if (column < 256) {
531 /* First 256 bytes --> READ0 */
532 readcmd = NAND_CMD_READ0;
533 } else {
534 column -= 256;
535 readcmd = NAND_CMD_READ1;
536 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200537 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200538 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200540 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Brian Norris8b6e50c2011-05-25 14:59:01 -0700542 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200543 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
544 /* Serially input address */
545 if (column != -1) {
546 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200547 if (chip->options & NAND_BUSWIDTH_16)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200548 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200549 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200550 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200552 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200553 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200554 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200555 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200556 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200557 if (chip->chipsize > (32 << 20))
558 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200559 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200560 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000561
562 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700563 * Program and erase have their own busy handlers status and sequential
564 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case NAND_CMD_PAGEPROG:
569 case NAND_CMD_ERASE1:
570 case NAND_CMD_ERASE2:
571 case NAND_CMD_SEQIN:
572 case NAND_CMD_STATUS:
573 return;
574
575 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200576 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200578 udelay(chip->chip_delay);
579 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200580 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200581 chip->cmd_ctrl(mtd,
582 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200583 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
584 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return;
586
David Woodhousee0c7d762006-05-13 18:07:53 +0100587 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000589 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * If we don't have access to the busy pin, we apply the given
591 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100592 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200593 if (!chip->dev_ready) {
594 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700598 /*
599 * Apply this short delay always to ensure that we do wait tWB in
600 * any case on any machine.
601 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100602 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000603
604 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/**
608 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700609 * @mtd: MTD device structure
610 * @command: the command to be sent
611 * @column: the column address for this command, -1 if none
612 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200614 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700615 * devices. We don't have the separate regions as we have in the small page
616 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
619 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200621 register struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Emulate NAND_CMD_READOOB */
624 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200625 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 command = NAND_CMD_READ0;
627 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000628
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200629 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400630 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200633 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* Serially input address */
636 if (column != -1) {
637 /* Adjust columns for 16 bit buswidth */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200638 if (chip->options & NAND_BUSWIDTH_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200640 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200641 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200642 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200645 chip->cmd_ctrl(mtd, page_addr, ctrl);
646 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200647 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200649 if (chip->chipsize > (128 << 20))
650 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200651 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200654 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000655
656 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700657 * Program and erase have their own busy handlers status, sequential
658 * in, and deplete1 need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 case NAND_CMD_CACHEDPROG:
663 case NAND_CMD_PAGEPROG:
664 case NAND_CMD_ERASE1:
665 case NAND_CMD_ERASE2:
666 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200667 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000669 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200672 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200674 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200675 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
676 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
677 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
678 NAND_NCE | NAND_CTRL_CHANGE);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200679 while (!(chip->read_byte(mtd) & NAND_STATUS_READY))
680 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200683 case NAND_CMD_RNDOUT:
684 /* No ready / busy check necessary */
685 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
686 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
687 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
688 NAND_NCE | NAND_CTRL_CHANGE);
689 return;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200692 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
693 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
694 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
695 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000696
David Woodhousee0c7d762006-05-13 18:07:53 +0100697 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000699 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700701 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100702 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200703 if (!chip->dev_ready) {
704 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000708
Brian Norris8b6e50c2011-05-25 14:59:01 -0700709 /*
710 * Apply this short delay always to ensure that we do wait tWB in
711 * any case on any machine.
712 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100713 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000714
715 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200719 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700720 * @chip: the nand chip descriptor
721 * @mtd: MTD device structure
722 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200723 *
724 * Used when in panic, no locks are taken.
725 */
726static void panic_nand_get_device(struct nand_chip *chip,
727 struct mtd_info *mtd, int new_state)
728{
Brian Norris7854d3f2011-06-23 14:12:08 -0700729 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200730 chip->controller->active = chip;
731 chip->state = new_state;
732}
733
734/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700736 * @mtd: MTD device structure
737 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 *
739 * Get the device and lock it for exclusive access
740 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200741static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800742nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Huang Shijie6a8214a2012-11-19 14:43:30 +0800744 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200745 spinlock_t *lock = &chip->controller->lock;
746 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100747 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200748retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100749 spin_lock(lock);
750
vimal singhb8b3ee92009-07-09 20:41:22 +0530751 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200752 if (!chip->controller->active)
753 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200754
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200755 if (chip->controller->active == chip && chip->state == FL_READY) {
756 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100757 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100758 return 0;
759 }
760 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800761 if (chip->controller->active->state == FL_PM_SUSPENDED) {
762 chip->state = FL_PM_SUSPENDED;
763 spin_unlock(lock);
764 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800765 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100766 }
767 set_current_state(TASK_UNINTERRUPTIBLE);
768 add_wait_queue(wq, &wait);
769 spin_unlock(lock);
770 schedule();
771 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto retry;
773}
774
775/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700776 * panic_nand_wait - [GENERIC] wait until the command is done
777 * @mtd: MTD device structure
778 * @chip: NAND chip structure
779 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200780 *
781 * Wait for command done. This is a helper function for nand_wait used when
782 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400783 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200784 */
785static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
786 unsigned long timeo)
787{
788 int i;
789 for (i = 0; i < timeo; i++) {
790 if (chip->dev_ready) {
791 if (chip->dev_ready(mtd))
792 break;
793 } else {
794 if (chip->read_byte(mtd) & NAND_STATUS_READY)
795 break;
796 }
797 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200798 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200799}
800
801/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700802 * nand_wait - [DEFAULT] wait until the command is done
803 * @mtd: MTD device structure
804 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700806 * Wait for command done. This applies to erase and program only. Erase can
807 * take up to 400ms and program up to 20ms according to general NAND and
808 * SmartMedia specs.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700809 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200810static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200813 int status, state = chip->state;
Huang Shijie6d2559f2013-01-30 10:03:56 +0800814 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Richard Purdie8fe833c2006-03-31 02:31:14 -0800816 led_trigger_event(nand_led_trigger, LED_FULL);
817
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 /*
819 * Apply this short delay always to ensure that we do wait tWB in any
820 * case on any machine.
821 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100822 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200824 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200826 if (in_interrupt() || oops_in_progress)
827 panic_nand_wait(mtd, chip, timeo);
828 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800829 timeo = jiffies + msecs_to_jiffies(timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200830 while (time_before(jiffies, timeo)) {
831 if (chip->dev_ready) {
832 if (chip->dev_ready(mtd))
833 break;
834 } else {
835 if (chip->read_byte(mtd) & NAND_STATUS_READY)
836 break;
837 }
838 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800841 led_trigger_event(nand_led_trigger, LED_OFF);
842
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200843 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100844 /* This can happen if in case of timeout or buggy dev_ready */
845 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return status;
847}
848
849/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700850 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700851 * @mtd: mtd info
852 * @ofs: offset to start unlock from
853 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700854 * @invert: when = 0, unlock the range of blocks within the lower and
855 * upper boundary address
856 * when = 1, unlock the range of blocks outside the boundaries
857 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530858 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700859 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530860 */
861static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
862 uint64_t len, int invert)
863{
864 int ret = 0;
865 int status, page;
866 struct nand_chip *chip = mtd->priv;
867
868 /* Submit address of first page to unlock */
869 page = ofs >> chip->page_shift;
870 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
871
872 /* Submit address of last page to unlock */
873 page = (ofs + len) >> chip->page_shift;
874 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
875 (page | invert) & chip->pagemask);
876
877 /* Call wait ready function */
878 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530879 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400880 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700881 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530882 __func__, status);
883 ret = -EIO;
884 }
885
886 return ret;
887}
888
889/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700890 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700891 * @mtd: mtd info
892 * @ofs: offset to start unlock from
893 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530894 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700895 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530896 */
897int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
898{
899 int ret = 0;
900 int chipnr;
901 struct nand_chip *chip = mtd->priv;
902
Brian Norris289c0522011-07-19 10:06:09 -0700903 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530904 __func__, (unsigned long long)ofs, len);
905
906 if (check_offs_len(mtd, ofs, len))
907 ret = -EINVAL;
908
909 /* Align to last block address if size addresses end of the device */
910 if (ofs + len == mtd->size)
911 len -= mtd->erasesize;
912
Huang Shijie6a8214a2012-11-19 14:43:30 +0800913 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530914
915 /* Shift to get chip number */
916 chipnr = ofs >> chip->chip_shift;
917
918 chip->select_chip(mtd, chipnr);
919
920 /* Check, if it is write protected */
921 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700922 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530923 __func__);
924 ret = -EIO;
925 goto out;
926 }
927
928 ret = __nand_unlock(mtd, ofs, len, 0);
929
930out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800931 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530932 nand_release_device(mtd);
933
934 return ret;
935}
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200936EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +0530937
938/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700939 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700940 * @mtd: mtd info
941 * @ofs: offset to start unlock from
942 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530943 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700944 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
945 * have this feature, but it allows only to lock all blocks, not for specified
946 * range for block. Implementing 'lock' feature by making use of 'unlock', for
947 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +0530948 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700949 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530950 */
951int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
952{
953 int ret = 0;
954 int chipnr, status, page;
955 struct nand_chip *chip = mtd->priv;
956
Brian Norris289c0522011-07-19 10:06:09 -0700957 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530958 __func__, (unsigned long long)ofs, len);
959
960 if (check_offs_len(mtd, ofs, len))
961 ret = -EINVAL;
962
Huang Shijie6a8214a2012-11-19 14:43:30 +0800963 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530964
965 /* Shift to get chip number */
966 chipnr = ofs >> chip->chip_shift;
967
968 chip->select_chip(mtd, chipnr);
969
970 /* Check, if it is write protected */
971 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700972 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530973 __func__);
974 status = MTD_ERASE_FAILED;
975 ret = -EIO;
976 goto out;
977 }
978
979 /* Submit address of first page to lock */
980 page = ofs >> chip->page_shift;
981 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
982
983 /* Call wait ready function */
984 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530985 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400986 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700987 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530988 __func__, status);
989 ret = -EIO;
990 goto out;
991 }
992
993 ret = __nand_unlock(mtd, ofs, len, 0x1);
994
995out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800996 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +0530997 nand_release_device(mtd);
998
999 return ret;
1000}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001001EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301002
1003/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001004 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001005 * @mtd: mtd info structure
1006 * @chip: nand chip info structure
1007 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001008 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001009 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001010 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001011 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001012 */
1013static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001014 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001015{
1016 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001017 if (oob_required)
1018 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001019 return 0;
1020}
1021
1022/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001023 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001024 * @mtd: mtd info structure
1025 * @chip: nand chip info structure
1026 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001027 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001028 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001029 *
1030 * We need a special oob layout and handling even when OOB isn't used.
1031 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001032static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001033 struct nand_chip *chip, uint8_t *buf,
1034 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001035{
1036 int eccsize = chip->ecc.size;
1037 int eccbytes = chip->ecc.bytes;
1038 uint8_t *oob = chip->oob_poi;
1039 int steps, size;
1040
1041 for (steps = chip->ecc.steps; steps > 0; steps--) {
1042 chip->read_buf(mtd, buf, eccsize);
1043 buf += eccsize;
1044
1045 if (chip->ecc.prepad) {
1046 chip->read_buf(mtd, oob, chip->ecc.prepad);
1047 oob += chip->ecc.prepad;
1048 }
1049
1050 chip->read_buf(mtd, oob, eccbytes);
1051 oob += eccbytes;
1052
1053 if (chip->ecc.postpad) {
1054 chip->read_buf(mtd, oob, chip->ecc.postpad);
1055 oob += chip->ecc.postpad;
1056 }
1057 }
1058
1059 size = mtd->oobsize - (oob - chip->oob_poi);
1060 if (size)
1061 chip->read_buf(mtd, oob, size);
1062
1063 return 0;
1064}
1065
1066/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001067 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001068 * @mtd: mtd info structure
1069 * @chip: nand chip info structure
1070 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001071 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001072 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001073 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001074static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001075 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001077 int i, eccsize = chip->ecc.size;
1078 int eccbytes = chip->ecc.bytes;
1079 int eccsteps = chip->ecc.steps;
1080 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001081 uint8_t *ecc_calc = chip->buffers->ecccalc;
1082 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001083 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001084 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001085
Brian Norris1fbb9382012-05-02 10:14:55 -07001086 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001087
1088 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1089 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1090
1091 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001092 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001093
1094 eccsteps = chip->ecc.steps;
1095 p = buf;
1096
1097 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1098 int stat;
1099
1100 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001101 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001102 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001103 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001104 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001105 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1106 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001107 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001108 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301112 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001113 * @mtd: mtd info structure
1114 * @chip: nand chip info structure
1115 * @data_offs: offset of requested data within the page
1116 * @readlen: data length
1117 * @bufpoi: buffer to store read data
Alexey Korolev3d459552008-05-15 17:23:18 +01001118 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001119static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1120 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Alexey Korolev3d459552008-05-15 17:23:18 +01001121{
1122 int start_step, end_step, num_steps;
1123 uint32_t *eccpos = chip->ecc.layout->eccpos;
1124 uint8_t *p;
1125 int data_col_addr, i, gaps = 0;
1126 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1127 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001128 int index = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001129 unsigned int max_bitflips = 0;
Alexey Korolev3d459552008-05-15 17:23:18 +01001130
Brian Norris7854d3f2011-06-23 14:12:08 -07001131 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001132 start_step = data_offs / chip->ecc.size;
1133 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1134 num_steps = end_step - start_step + 1;
1135
Brian Norris8b6e50c2011-05-25 14:59:01 -07001136 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001137 datafrag_len = num_steps * chip->ecc.size;
1138 eccfrag_len = num_steps * chip->ecc.bytes;
1139
1140 data_col_addr = start_step * chip->ecc.size;
1141 /* If we read not a page aligned data */
1142 if (data_col_addr != 0)
1143 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1144
1145 p = bufpoi + data_col_addr;
1146 chip->read_buf(mtd, p, datafrag_len);
1147
Brian Norris8b6e50c2011-05-25 14:59:01 -07001148 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001149 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1150 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1151
Brian Norris8b6e50c2011-05-25 14:59:01 -07001152 /*
1153 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001154 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001155 */
Alexey Korolev3d459552008-05-15 17:23:18 +01001156 for (i = 0; i < eccfrag_len - 1; i++) {
1157 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1158 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1159 gaps = 1;
1160 break;
1161 }
1162 }
1163 if (gaps) {
1164 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1165 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1166 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001167 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001168 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001169 * about buswidth alignment in read_buf.
1170 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001171 index = start_step * chip->ecc.bytes;
1172
1173 aligned_pos = eccpos[index] & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001174 aligned_len = eccfrag_len;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001175 if (eccpos[index] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001176 aligned_len++;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001177 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001178 aligned_len++;
1179
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001180 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1181 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001182 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1183 }
1184
1185 for (i = 0; i < eccfrag_len; i++)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001186 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Alexey Korolev3d459552008-05-15 17:23:18 +01001187
1188 p = bufpoi + data_col_addr;
1189 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1190 int stat;
1191
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001192 stat = chip->ecc.correct(mtd, p,
1193 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001194 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001195 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001196 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001197 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001198 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1199 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001200 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001201 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001202}
1203
1204/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001205 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001206 * @mtd: mtd info structure
1207 * @chip: nand chip info structure
1208 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001209 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001210 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001211 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001212 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001213 */
1214static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001215 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001216{
1217 int i, eccsize = chip->ecc.size;
1218 int eccbytes = chip->ecc.bytes;
1219 int eccsteps = chip->ecc.steps;
1220 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001221 uint8_t *ecc_calc = chip->buffers->ecccalc;
1222 uint8_t *ecc_code = chip->buffers->ecccode;
Ben Dooks8b099a32007-05-28 19:17:54 +01001223 uint32_t *eccpos = chip->ecc.layout->eccpos;
Mike Dunn3f91e942012-04-25 12:06:09 -07001224 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001225
1226 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1227 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1228 chip->read_buf(mtd, p, eccsize);
1229 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1230 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001231 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001232
1233 for (i = 0; i < chip->ecc.total; i++)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001234 ecc_code[i] = chip->oob_poi[eccpos[i]];
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001235
1236 eccsteps = chip->ecc.steps;
1237 p = buf;
1238
1239 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1240 int stat;
1241
1242 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001243 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001244 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001245 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001246 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001247 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1248 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001249 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001250 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001251}
1252
1253/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001254 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001255 * @mtd: mtd info structure
1256 * @chip: nand chip info structure
1257 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001258 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001259 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001260 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001261 * Hardware ECC for large page chips, require OOB to be read first. For this
1262 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1263 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1264 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1265 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001266 */
1267static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001268 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001269{
1270 int i, eccsize = chip->ecc.size;
1271 int eccbytes = chip->ecc.bytes;
1272 int eccsteps = chip->ecc.steps;
1273 uint8_t *p = buf;
1274 uint8_t *ecc_code = chip->buffers->ecccode;
1275 uint32_t *eccpos = chip->ecc.layout->eccpos;
1276 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001277 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001278
1279 /* Read the OOB area first */
1280 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1281 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1282 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1283
1284 for (i = 0; i < chip->ecc.total; i++)
1285 ecc_code[i] = chip->oob_poi[eccpos[i]];
1286
1287 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1288 int stat;
1289
1290 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1291 chip->read_buf(mtd, p, eccsize);
1292 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1293
1294 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Mike Dunn3f91e942012-04-25 12:06:09 -07001295 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001296 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001297 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001298 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001299 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1300 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001301 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001302 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001303}
1304
1305/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001306 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001307 * @mtd: mtd info structure
1308 * @chip: nand chip info structure
1309 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001310 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001311 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001312 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001313 * The hw generator calculates the error syndrome automatically. Therefore we
1314 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001315 */
1316static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001317 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001318{
1319 int i, eccsize = chip->ecc.size;
1320 int eccbytes = chip->ecc.bytes;
1321 int eccsteps = chip->ecc.steps;
1322 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001323 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001324 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001325
1326 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1327 int stat;
1328
1329 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1330 chip->read_buf(mtd, p, eccsize);
1331
1332 if (chip->ecc.prepad) {
1333 chip->read_buf(mtd, oob, chip->ecc.prepad);
1334 oob += chip->ecc.prepad;
1335 }
1336
1337 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1338 chip->read_buf(mtd, oob, eccbytes);
1339 stat = chip->ecc.correct(mtd, p, oob, NULL);
1340
Mike Dunn3f91e942012-04-25 12:06:09 -07001341 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001342 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001343 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001344 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001345 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1346 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001347
1348 oob += eccbytes;
1349
1350 if (chip->ecc.postpad) {
1351 chip->read_buf(mtd, oob, chip->ecc.postpad);
1352 oob += chip->ecc.postpad;
1353 }
1354 }
1355
1356 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001357 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001358 if (i)
1359 chip->read_buf(mtd, oob, i);
1360
Mike Dunn3f91e942012-04-25 12:06:09 -07001361 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001362}
1363
1364/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001365 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -07001366 * @chip: nand chip structure
1367 * @oob: oob destination address
1368 * @ops: oob ops structure
1369 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001370 */
1371static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001372 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001373{
Florian Fainellif8ac0412010-09-07 13:23:43 +02001374 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001375
Brian Norris0612b9d2011-08-30 18:45:40 -07001376 case MTD_OPS_PLACE_OOB:
1377 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001378 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1379 return oob + len;
1380
Brian Norris0612b9d2011-08-30 18:45:40 -07001381 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001382 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001383 uint32_t boffs = 0, roffs = ops->ooboffs;
1384 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001385
Florian Fainellif8ac0412010-09-07 13:23:43 +02001386 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001387 /* Read request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001388 if (unlikely(roffs)) {
1389 if (roffs >= free->length) {
1390 roffs -= free->length;
1391 continue;
1392 }
1393 boffs = free->offset + roffs;
1394 bytes = min_t(size_t, len,
1395 (free->length - roffs));
1396 roffs = 0;
1397 } else {
1398 bytes = min_t(size_t, len, free->length);
1399 boffs = free->offset;
1400 }
1401 memcpy(oob, chip->oob_poi + boffs, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001402 oob += bytes;
1403 }
1404 return oob;
1405 }
1406 default:
1407 BUG();
1408 }
1409 return NULL;
1410}
1411
1412/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001413 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1414 * @mtd: MTD device structure
1415 * @retry_mode: the retry mode to use
1416 *
1417 * Some vendors supply a special command to shift the Vt threshold, to be used
1418 * when there are too many bitflips in a page (i.e., ECC error). After setting
1419 * a new threshold, the host should retry reading the page.
1420 */
1421static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1422{
1423 struct nand_chip *chip = mtd->priv;
1424
1425 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1426
1427 if (retry_mode >= chip->read_retries)
1428 return -EINVAL;
1429
1430 if (!chip->setup_read_retry)
1431 return -EOPNOTSUPP;
1432
1433 return chip->setup_read_retry(mtd, retry_mode);
1434}
1435
1436/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001437 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001438 * @mtd: MTD device structure
1439 * @from: offset to read from
1440 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001441 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001442 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001443 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001444static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1445 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001446{
Brian Norrise47f3db2012-05-02 10:14:56 -07001447 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001448 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001449 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001450 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001451 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001452 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001453 mtd->oobavail : mtd->oobsize;
1454
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001455 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001456 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001457 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001458 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001460 chipnr = (int)(from >> chip->chip_shift);
1461 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001463 realpage = (int)(from >> chip->page_shift);
1464 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001466 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001468 buf = ops->datbuf;
1469 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001470 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001471
Florian Fainellif8ac0412010-09-07 13:23:43 +02001472 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001473 unsigned int ecc_failures = mtd->ecc_stats.failed;
1474
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001475 bytes = min(mtd->writesize - col, readlen);
1476 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001477
Brian Norris8b6e50c2011-05-25 14:59:01 -07001478 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001479 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001480 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Brian Norrisba84fb52014-01-03 15:13:33 -08001482read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001483 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Mike Dunnedbc45402012-04-25 12:06:11 -07001485 /*
1486 * Now read the page into the buffer. Absent an error,
1487 * the read methods return max bitflips per ecc step.
1488 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001489 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001490 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001491 oob_required,
1492 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001493 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1494 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001495 ret = chip->ecc.read_subpage(mtd, chip,
1496 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001497 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001498 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001499 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001500 if (ret < 0) {
1501 if (!aligned)
1502 /* Invalidate page cache */
1503 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001504 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001505 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001506
Mike Dunnedbc45402012-04-25 12:06:11 -07001507 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1508
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001509 /* Transfer not aligned data */
1510 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001511 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001512 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001513 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001514 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001515 chip->pagebuf_bitflips = ret;
1516 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001517 /* Invalidate page cache */
1518 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001519 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001520 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001522
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001523 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001524 int toread = min(oobreadlen, max_oobsize);
1525
1526 if (toread) {
1527 oob = nand_transfer_oob(chip,
1528 oob, ops, toread);
1529 oobreadlen -= toread;
1530 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001531 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001532
1533 if (chip->options & NAND_NEED_READRDY) {
1534 /* Apply delay or wait for ready/busy pin */
1535 if (!chip->dev_ready)
1536 udelay(chip->chip_delay);
1537 else
1538 nand_wait_ready(mtd);
1539 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001540
Brian Norrisba84fb52014-01-03 15:13:33 -08001541 if (mtd->ecc_stats.failed - ecc_failures) {
1542 if (retry_mode + 1 <= chip->read_retries) {
1543 retry_mode++;
1544 ret = nand_setup_read_retry(mtd,
1545 retry_mode);
1546 if (ret < 0)
1547 break;
1548
1549 /* Reset failures; retry */
1550 mtd->ecc_stats.failed = ecc_failures;
1551 goto read_retry;
1552 } else {
1553 /* No more retry modes; real failure */
1554 ecc_fail = true;
1555 }
1556 }
1557
1558 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001559 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001560 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001561 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001562 max_bitflips = max_t(unsigned int, max_bitflips,
1563 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001566 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001567
Brian Norrisba84fb52014-01-03 15:13:33 -08001568 /* Reset to retry mode 0 */
1569 if (retry_mode) {
1570 ret = nand_setup_read_retry(mtd, 0);
1571 if (ret < 0)
1572 break;
1573 retry_mode = 0;
1574 }
1575
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001576 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Brian Norris8b6e50c2011-05-25 14:59:01 -07001579 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 col = 0;
1581 /* Increment page address */
1582 realpage++;
1583
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001584 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* Check, if we cross a chip boundary */
1586 if (!page) {
1587 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001588 chip->select_chip(mtd, -1);
1589 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001592 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001594 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001595 if (oob)
1596 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Mike Dunn3f91e942012-04-25 12:06:09 -07001598 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001599 return ret;
1600
Brian Norrisb72f3df2013-12-03 11:04:14 -08001601 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001602 return -EBADMSG;
1603
Mike Dunnedbc45402012-04-25 12:06:11 -07001604 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001605}
1606
1607/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001608 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001609 * @mtd: MTD device structure
1610 * @from: offset to read from
1611 * @len: number of bytes to read
1612 * @retlen: pointer to variable to store the number of read bytes
1613 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001614 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001615 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001616 */
1617static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1618 size_t *retlen, uint8_t *buf)
1619{
Brian Norris4a89ff82011-08-30 18:45:45 -07001620 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001621 int ret;
1622
Huang Shijie6a8214a2012-11-19 14:43:30 +08001623 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001624 ops.len = len;
1625 ops.datbuf = buf;
1626 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001627 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001628 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001629 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001630 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001631 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001635 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001636 * @mtd: mtd info structure
1637 * @chip: nand chip info structure
1638 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001639 */
1640static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001641 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001642{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001643 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001644 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001645 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001646}
1647
1648/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001649 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001650 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001651 * @mtd: mtd info structure
1652 * @chip: nand chip info structure
1653 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001654 */
1655static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001656 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001657{
1658 uint8_t *buf = chip->oob_poi;
1659 int length = mtd->oobsize;
1660 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1661 int eccsize = chip->ecc.size;
1662 uint8_t *bufpoi = buf;
1663 int i, toread, sndrnd = 0, pos;
1664
1665 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1666 for (i = 0; i < chip->ecc.steps; i++) {
1667 if (sndrnd) {
1668 pos = eccsize + i * (eccsize + chunk);
1669 if (mtd->writesize > 512)
1670 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1671 else
1672 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1673 } else
1674 sndrnd = 1;
1675 toread = min_t(int, length, chunk);
1676 chip->read_buf(mtd, bufpoi, toread);
1677 bufpoi += toread;
1678 length -= toread;
1679 }
1680 if (length > 0)
1681 chip->read_buf(mtd, bufpoi, length);
1682
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001683 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001684}
1685
1686/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001687 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001688 * @mtd: mtd info structure
1689 * @chip: nand chip info structure
1690 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001691 */
1692static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1693 int page)
1694{
1695 int status = 0;
1696 const uint8_t *buf = chip->oob_poi;
1697 int length = mtd->oobsize;
1698
1699 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1700 chip->write_buf(mtd, buf, length);
1701 /* Send command to program the OOB data */
1702 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1703
1704 status = chip->waitfunc(mtd, chip);
1705
Savin Zlobec0d420f92006-06-21 11:51:20 +02001706 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001707}
1708
1709/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001710 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001711 * with syndrome - only for large page flash
1712 * @mtd: mtd info structure
1713 * @chip: nand chip info structure
1714 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001715 */
1716static int nand_write_oob_syndrome(struct mtd_info *mtd,
1717 struct nand_chip *chip, int page)
1718{
1719 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1720 int eccsize = chip->ecc.size, length = mtd->oobsize;
1721 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1722 const uint8_t *bufpoi = chip->oob_poi;
1723
1724 /*
1725 * data-ecc-data-ecc ... ecc-oob
1726 * or
1727 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1728 */
1729 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1730 pos = steps * (eccsize + chunk);
1731 steps = 0;
1732 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001733 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001734
1735 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1736 for (i = 0; i < steps; i++) {
1737 if (sndcmd) {
1738 if (mtd->writesize <= 512) {
1739 uint32_t fill = 0xFFFFFFFF;
1740
1741 len = eccsize;
1742 while (len > 0) {
1743 int num = min_t(int, len, 4);
1744 chip->write_buf(mtd, (uint8_t *)&fill,
1745 num);
1746 len -= num;
1747 }
1748 } else {
1749 pos = eccsize + i * (eccsize + chunk);
1750 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1751 }
1752 } else
1753 sndcmd = 1;
1754 len = min_t(int, length, chunk);
1755 chip->write_buf(mtd, bufpoi, len);
1756 bufpoi += len;
1757 length -= len;
1758 }
1759 if (length > 0)
1760 chip->write_buf(mtd, bufpoi, length);
1761
1762 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1763 status = chip->waitfunc(mtd, chip);
1764
1765 return status & NAND_STATUS_FAIL ? -EIO : 0;
1766}
1767
1768/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001769 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001770 * @mtd: MTD device structure
1771 * @from: offset to read from
1772 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001774 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001776static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1777 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Brian Norrisc00a0992012-05-01 17:12:54 -07001779 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001780 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001781 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001782 int readlen = ops->ooblen;
1783 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001784 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001785 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Brian Norris289c0522011-07-19 10:06:09 -07001787 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301788 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Brian Norris041e4572011-06-23 16:45:24 -07001790 stats = mtd->ecc_stats;
1791
Brian Norris0612b9d2011-08-30 18:45:40 -07001792 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001793 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001794 else
1795 len = mtd->oobsize;
1796
1797 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001798 pr_debug("%s: attempt to start read outside oob\n",
1799 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001800 return -EINVAL;
1801 }
1802
1803 /* Do not allow reads past end of device */
1804 if (unlikely(from >= mtd->size ||
1805 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1806 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001807 pr_debug("%s: attempt to read beyond end of device\n",
1808 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001809 return -EINVAL;
1810 }
Vitaly Wool70145682006-11-03 18:20:38 +03001811
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001812 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001813 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001815 /* Shift to get page */
1816 realpage = (int)(from >> chip->page_shift);
1817 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Florian Fainellif8ac0412010-09-07 13:23:43 +02001819 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001820 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001821 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001822 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001823 ret = chip->ecc.read_oob(mtd, chip, page);
1824
1825 if (ret < 0)
1826 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001827
1828 len = min(len, readlen);
1829 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001830
Brian Norris5bc7c332013-03-13 09:51:31 -07001831 if (chip->options & NAND_NEED_READRDY) {
1832 /* Apply delay or wait for ready/busy pin */
1833 if (!chip->dev_ready)
1834 udelay(chip->chip_delay);
1835 else
1836 nand_wait_ready(mtd);
1837 }
1838
Vitaly Wool70145682006-11-03 18:20:38 +03001839 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001840 if (!readlen)
1841 break;
1842
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001843 /* Increment page address */
1844 realpage++;
1845
1846 page = realpage & chip->pagemask;
1847 /* Check, if we cross a chip boundary */
1848 if (!page) {
1849 chipnr++;
1850 chip->select_chip(mtd, -1);
1851 chip->select_chip(mtd, chipnr);
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001854 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001856 ops->oobretlen = ops->ooblen - readlen;
1857
1858 if (ret < 0)
1859 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001860
1861 if (mtd->ecc_stats.failed - stats.failed)
1862 return -EBADMSG;
1863
1864 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001868 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001869 * @mtd: MTD device structure
1870 * @from: offset to read from
1871 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001873 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1876 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001878 int ret = -ENOTSUPP;
1879
1880 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001883 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001884 pr_debug("%s: attempt to read beyond end of device\n",
1885 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return -EINVAL;
1887 }
1888
Huang Shijie6a8214a2012-11-19 14:43:30 +08001889 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Florian Fainellif8ac0412010-09-07 13:23:43 +02001891 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001892 case MTD_OPS_PLACE_OOB:
1893 case MTD_OPS_AUTO_OOB:
1894 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001895 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001896
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001897 default:
1898 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001901 if (!ops->datbuf)
1902 ret = nand_do_read_oob(mtd, from, ops);
1903 else
1904 ret = nand_do_read_ops(mtd, from, ops);
1905
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001906out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001908 return ret;
1909}
1910
1911
1912/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001913 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001914 * @mtd: mtd info structure
1915 * @chip: nand chip info structure
1916 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001917 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001918 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001919 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001920 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001921static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001922 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001923{
1924 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001925 if (oob_required)
1926 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001927
1928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001931/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001932 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001933 * @mtd: mtd info structure
1934 * @chip: nand chip info structure
1935 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001936 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001937 *
1938 * We need a special oob layout and handling even when ECC isn't checked.
1939 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001940static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001941 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001942 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001943{
1944 int eccsize = chip->ecc.size;
1945 int eccbytes = chip->ecc.bytes;
1946 uint8_t *oob = chip->oob_poi;
1947 int steps, size;
1948
1949 for (steps = chip->ecc.steps; steps > 0; steps--) {
1950 chip->write_buf(mtd, buf, eccsize);
1951 buf += eccsize;
1952
1953 if (chip->ecc.prepad) {
1954 chip->write_buf(mtd, oob, chip->ecc.prepad);
1955 oob += chip->ecc.prepad;
1956 }
1957
1958 chip->read_buf(mtd, oob, eccbytes);
1959 oob += eccbytes;
1960
1961 if (chip->ecc.postpad) {
1962 chip->write_buf(mtd, oob, chip->ecc.postpad);
1963 oob += chip->ecc.postpad;
1964 }
1965 }
1966
1967 size = mtd->oobsize - (oob - chip->oob_poi);
1968 if (size)
1969 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001970
1971 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001972}
1973/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001974 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001975 * @mtd: mtd info structure
1976 * @chip: nand chip info structure
1977 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001978 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001979 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001980static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001981 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001982{
1983 int i, eccsize = chip->ecc.size;
1984 int eccbytes = chip->ecc.bytes;
1985 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001986 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001987 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001988 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001989
Brian Norris7854d3f2011-06-23 14:12:08 -07001990 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001991 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1992 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001993
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001994 for (i = 0; i < chip->ecc.total; i++)
1995 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001996
Josh Wufdbad98d2012-06-25 18:07:45 +08001997 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001998}
1999
2000/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002001 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002002 * @mtd: mtd info structure
2003 * @chip: nand chip info structure
2004 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002005 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002006 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002007static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07002008 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002009{
2010 int i, eccsize = chip->ecc.size;
2011 int eccbytes = chip->ecc.bytes;
2012 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002013 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002014 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01002015 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002016
2017 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2018 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002019 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002020 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2021 }
2022
2023 for (i = 0; i < chip->ecc.total; i++)
2024 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2025
2026 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002027
2028 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002029}
2030
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302031
2032/**
2033 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2034 * @mtd: mtd info structure
2035 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002036 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302037 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002038 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302039 * @oob_required: must write chip->oob_poi to OOB
2040 */
2041static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2042 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002043 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302044 int oob_required)
2045{
2046 uint8_t *oob_buf = chip->oob_poi;
2047 uint8_t *ecc_calc = chip->buffers->ecccalc;
2048 int ecc_size = chip->ecc.size;
2049 int ecc_bytes = chip->ecc.bytes;
2050 int ecc_steps = chip->ecc.steps;
2051 uint32_t *eccpos = chip->ecc.layout->eccpos;
2052 uint32_t start_step = offset / ecc_size;
2053 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2054 int oob_bytes = mtd->oobsize / ecc_steps;
2055 int step, i;
2056
2057 for (step = 0; step < ecc_steps; step++) {
2058 /* configure controller for WRITE access */
2059 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2060
2061 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002062 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302063
2064 /* mask ECC of un-touched subpages by padding 0xFF */
2065 if ((step < start_step) || (step > end_step))
2066 memset(ecc_calc, 0xff, ecc_bytes);
2067 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002068 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302069
2070 /* mask OOB of un-touched subpages by padding 0xFF */
2071 /* if oob_required, preserve OOB metadata of written subpage */
2072 if (!oob_required || (step < start_step) || (step > end_step))
2073 memset(oob_buf, 0xff, oob_bytes);
2074
Brian Norrisd6a950802013-08-08 17:16:36 -07002075 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302076 ecc_calc += ecc_bytes;
2077 oob_buf += oob_bytes;
2078 }
2079
2080 /* copy calculated ECC for whole page to chip->buffer->oob */
2081 /* this include masked-value(0xFF) for unwritten subpages */
2082 ecc_calc = chip->buffers->ecccalc;
2083 for (i = 0; i < chip->ecc.total; i++)
2084 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2085
2086 /* write OOB buffer to NAND device */
2087 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2088
2089 return 0;
2090}
2091
2092
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002093/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002094 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002095 * @mtd: mtd info structure
2096 * @chip: nand chip info structure
2097 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002098 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002099 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002100 * The hw generator calculates the error syndrome automatically. Therefore we
2101 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002102 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002103static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002104 struct nand_chip *chip,
2105 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002106{
2107 int i, eccsize = chip->ecc.size;
2108 int eccbytes = chip->ecc.bytes;
2109 int eccsteps = chip->ecc.steps;
2110 const uint8_t *p = buf;
2111 uint8_t *oob = chip->oob_poi;
2112
2113 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2114
2115 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2116 chip->write_buf(mtd, p, eccsize);
2117
2118 if (chip->ecc.prepad) {
2119 chip->write_buf(mtd, oob, chip->ecc.prepad);
2120 oob += chip->ecc.prepad;
2121 }
2122
2123 chip->ecc.calculate(mtd, p, oob);
2124 chip->write_buf(mtd, oob, eccbytes);
2125 oob += eccbytes;
2126
2127 if (chip->ecc.postpad) {
2128 chip->write_buf(mtd, oob, chip->ecc.postpad);
2129 oob += chip->ecc.postpad;
2130 }
2131 }
2132
2133 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002134 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002135 if (i)
2136 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002137
2138 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002139}
2140
2141/**
David Woodhouse956e9442006-09-25 17:12:39 +01002142 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002143 * @mtd: MTD device structure
2144 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302145 * @offset: address offset within the page
2146 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002147 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002148 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002149 * @page: page number to write
2150 * @cached: cached programming
2151 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002152 */
2153static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302154 uint32_t offset, int data_len, const uint8_t *buf,
2155 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002156{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302157 int status, subpage;
2158
2159 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2160 chip->ecc.write_subpage)
2161 subpage = offset || (data_len < mtd->writesize);
2162 else
2163 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002164
2165 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2166
David Woodhouse956e9442006-09-25 17:12:39 +01002167 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302168 status = chip->ecc.write_page_raw(mtd, chip, buf,
2169 oob_required);
2170 else if (subpage)
2171 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2172 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002173 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002174 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2175
2176 if (status < 0)
2177 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002178
2179 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002180 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002181 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002182 */
2183 cached = 0;
2184
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002185 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002186
2187 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002188 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002189 /*
2190 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002191 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002192 */
2193 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2194 status = chip->errstat(mtd, chip, FL_WRITING, status,
2195 page);
2196
2197 if (status & NAND_STATUS_FAIL)
2198 return -EIO;
2199 } else {
2200 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002201 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002202 }
2203
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002204 return 0;
2205}
2206
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002207/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002208 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002209 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002210 * @oob: oob data buffer
2211 * @len: oob data write length
2212 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002213 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002214static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2215 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002216{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002217 struct nand_chip *chip = mtd->priv;
2218
2219 /*
2220 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2221 * data from a previous OOB read.
2222 */
2223 memset(chip->oob_poi, 0xff, mtd->oobsize);
2224
Florian Fainellif8ac0412010-09-07 13:23:43 +02002225 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002226
Brian Norris0612b9d2011-08-30 18:45:40 -07002227 case MTD_OPS_PLACE_OOB:
2228 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002229 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2230 return oob + len;
2231
Brian Norris0612b9d2011-08-30 18:45:40 -07002232 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002233 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002234 uint32_t boffs = 0, woffs = ops->ooboffs;
2235 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002236
Florian Fainellif8ac0412010-09-07 13:23:43 +02002237 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002238 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002239 if (unlikely(woffs)) {
2240 if (woffs >= free->length) {
2241 woffs -= free->length;
2242 continue;
2243 }
2244 boffs = free->offset + woffs;
2245 bytes = min_t(size_t, len,
2246 (free->length - woffs));
2247 woffs = 0;
2248 } else {
2249 bytes = min_t(size_t, len, free->length);
2250 boffs = free->offset;
2251 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002252 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002253 oob += bytes;
2254 }
2255 return oob;
2256 }
2257 default:
2258 BUG();
2259 }
2260 return NULL;
2261}
2262
Florian Fainellif8ac0412010-09-07 13:23:43 +02002263#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002264
2265/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002266 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002267 * @mtd: MTD device structure
2268 * @to: offset to write to
2269 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002271 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002272 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002273static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2274 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002275{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002276 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002277 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002278 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002279
2280 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002281 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002282 mtd->oobavail : mtd->oobsize;
2283
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002284 uint8_t *oob = ops->oobbuf;
2285 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302286 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002287 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002288
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002289 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002290 if (!writelen)
2291 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002292
Brian Norris8b6e50c2011-05-25 14:59:01 -07002293 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002294 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002295 pr_notice("%s: attempt to write non page aligned data\n",
2296 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002297 return -EINVAL;
2298 }
2299
Thomas Gleixner29072b92006-09-28 15:38:36 +02002300 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002301
Thomas Gleixner6a930962006-06-28 00:11:45 +02002302 chipnr = (int)(to >> chip->chip_shift);
2303 chip->select_chip(mtd, chipnr);
2304
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002305 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002306 if (nand_check_wp(mtd)) {
2307 ret = -EIO;
2308 goto err_out;
2309 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002310
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002311 realpage = (int)(to >> chip->page_shift);
2312 page = realpage & chip->pagemask;
2313 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2314
2315 /* Invalidate the page cache, when we write to the cached page */
2316 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002317 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002318 chip->pagebuf = -1;
2319
Maxim Levitsky782ce792010-02-22 20:39:36 +02002320 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002321 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2322 ret = -EINVAL;
2323 goto err_out;
2324 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002325
Florian Fainellif8ac0412010-09-07 13:23:43 +02002326 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002327 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002328 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002329 uint8_t *wbuf = buf;
2330
Brian Norris8b6e50c2011-05-25 14:59:01 -07002331 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002332 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2333 cached = 0;
2334 bytes = min_t(int, bytes - column, (int) writelen);
2335 chip->pagebuf = -1;
2336 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2337 memcpy(&chip->buffers->databuf[column], buf, bytes);
2338 wbuf = chip->buffers->databuf;
2339 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002340
Maxim Levitsky782ce792010-02-22 20:39:36 +02002341 if (unlikely(oob)) {
2342 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002343 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002344 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002345 } else {
2346 /* We still need to erase leftover OOB data */
2347 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002348 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302349 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2350 oob_required, page, cached,
2351 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002352 if (ret)
2353 break;
2354
2355 writelen -= bytes;
2356 if (!writelen)
2357 break;
2358
Thomas Gleixner29072b92006-09-28 15:38:36 +02002359 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002360 buf += bytes;
2361 realpage++;
2362
2363 page = realpage & chip->pagemask;
2364 /* Check, if we cross a chip boundary */
2365 if (!page) {
2366 chipnr++;
2367 chip->select_chip(mtd, -1);
2368 chip->select_chip(mtd, chipnr);
2369 }
2370 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002371
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002372 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002373 if (unlikely(oob))
2374 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002375
2376err_out:
2377 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002378 return ret;
2379}
2380
2381/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002382 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002383 * @mtd: MTD device structure
2384 * @to: offset to write to
2385 * @len: number of bytes to write
2386 * @retlen: pointer to variable to store the number of written bytes
2387 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002388 *
2389 * NAND write with ECC. Used when performing writes in interrupt context, this
2390 * may for example be called by mtdoops when writing an oops while in panic.
2391 */
2392static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2393 size_t *retlen, const uint8_t *buf)
2394{
2395 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002396 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002397 int ret;
2398
Brian Norris8b6e50c2011-05-25 14:59:01 -07002399 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002400 panic_nand_wait(mtd, chip, 400);
2401
Brian Norris8b6e50c2011-05-25 14:59:01 -07002402 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002403 panic_nand_get_device(chip, mtd, FL_WRITING);
2404
Brian Norris4a89ff82011-08-30 18:45:45 -07002405 ops.len = len;
2406 ops.datbuf = (uint8_t *)buf;
2407 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002408 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002409
Brian Norris4a89ff82011-08-30 18:45:45 -07002410 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002411
Brian Norris4a89ff82011-08-30 18:45:45 -07002412 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002413 return ret;
2414}
2415
2416/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002417 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002418 * @mtd: MTD device structure
2419 * @to: offset to write to
2420 * @len: number of bytes to write
2421 * @retlen: pointer to variable to store the number of written bytes
2422 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002424 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002426static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002427 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Brian Norris4a89ff82011-08-30 18:45:45 -07002429 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002430 int ret;
2431
Huang Shijie6a8214a2012-11-19 14:43:30 +08002432 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002433 ops.len = len;
2434 ops.datbuf = (uint8_t *)buf;
2435 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002436 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002437 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002438 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002439 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002440 return ret;
2441}
2442
2443/**
2444 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002445 * @mtd: MTD device structure
2446 * @to: offset to write to
2447 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002448 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002449 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002450 */
2451static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2452 struct mtd_oob_ops *ops)
2453{
Adrian Hunter03736152007-01-31 17:58:29 +02002454 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002455 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Brian Norris289c0522011-07-19 10:06:09 -07002457 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302458 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Brian Norris0612b9d2011-08-30 18:45:40 -07002460 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002461 len = chip->ecc.layout->oobavail;
2462 else
2463 len = mtd->oobsize;
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002466 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002467 pr_debug("%s: attempt to write past end of page\n",
2468 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return -EINVAL;
2470 }
2471
Adrian Hunter03736152007-01-31 17:58:29 +02002472 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002473 pr_debug("%s: attempt to start write outside oob\n",
2474 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002475 return -EINVAL;
2476 }
2477
Jason Liu775adc32011-02-25 13:06:18 +08002478 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002479 if (unlikely(to >= mtd->size ||
2480 ops->ooboffs + ops->ooblen >
2481 ((mtd->size >> chip->page_shift) -
2482 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002483 pr_debug("%s: attempt to write beyond end of device\n",
2484 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002485 return -EINVAL;
2486 }
2487
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002488 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002489 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002491 /* Shift to get page */
2492 page = (int)(to >> chip->page_shift);
2493
2494 /*
2495 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2496 * of my DiskOnChip 2000 test units) will clear the whole data page too
2497 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2498 * it in the doc2000 driver in August 1999. dwmw2.
2499 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002500 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002503 if (nand_check_wp(mtd)) {
2504 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002505 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002506 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002509 if (page == chip->pagebuf)
2510 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002512 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002513
Brian Norris0612b9d2011-08-30 18:45:40 -07002514 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002515 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2516 else
2517 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002518
Huang Shijieb0bb6902012-11-19 14:43:29 +08002519 chip->select_chip(mtd, -1);
2520
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002521 if (status)
2522 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Vitaly Wool70145682006-11-03 18:20:38 +03002524 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002526 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002529/**
2530 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002531 * @mtd: MTD device structure
2532 * @to: offset to write to
2533 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002534 */
2535static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2536 struct mtd_oob_ops *ops)
2537{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002538 int ret = -ENOTSUPP;
2539
2540 ops->retlen = 0;
2541
2542 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002543 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002544 pr_debug("%s: attempt to write beyond end of device\n",
2545 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002546 return -EINVAL;
2547 }
2548
Huang Shijie6a8214a2012-11-19 14:43:30 +08002549 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002550
Florian Fainellif8ac0412010-09-07 13:23:43 +02002551 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002552 case MTD_OPS_PLACE_OOB:
2553 case MTD_OPS_AUTO_OOB:
2554 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002555 break;
2556
2557 default:
2558 goto out;
2559 }
2560
2561 if (!ops->datbuf)
2562 ret = nand_do_write_oob(mtd, to, ops);
2563 else
2564 ret = nand_do_write_ops(mtd, to, ops);
2565
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002566out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002567 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return ret;
2569}
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002572 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002573 * @mtd: MTD device structure
2574 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002576 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002578static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002582 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2583 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
2586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002588 * @mtd: MTD device structure
2589 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002591 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002593static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
David Woodhousee0c7d762006-05-13 18:07:53 +01002595 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002599 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002600 * @mtd: MTD device structure
2601 * @instr: erase instruction
2602 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002604 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002606int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2607 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Adrian Hunter69423d92008-12-10 13:37:21 +00002609 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002610 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002611 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Brian Norris289c0522011-07-19 10:06:09 -07002613 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2614 __func__, (unsigned long long)instr->addr,
2615 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302617 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002621 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002624 page = (int)(instr->addr >> chip->page_shift);
2625 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002628 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002631 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Check, if it is write protected */
2634 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002635 pr_debug("%s: device is write protected!\n",
2636 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 instr->state = MTD_ERASE_FAILED;
2638 goto erase_exit;
2639 }
2640
2641 /* Loop through the pages */
2642 len = instr->len;
2643
2644 instr->state = MTD_ERASING;
2645
2646 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002647 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002648 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2649 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002650 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2651 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 instr->state = MTD_ERASE_FAILED;
2653 goto erase_exit;
2654 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002655
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002656 /*
2657 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002658 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002659 */
2660 if (page <= chip->pagebuf && chip->pagebuf <
2661 (page + pages_per_block))
2662 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002664 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002665
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002666 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002668 /*
2669 * See if operation failed and additional status checks are
2670 * available
2671 */
2672 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2673 status = chip->errstat(mtd, chip, FL_ERASING,
2674 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002677 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002678 pr_debug("%s: failed erase, page 0x%08x\n",
2679 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002681 instr->fail_addr =
2682 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 goto erase_exit;
2684 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002687 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 page += pages_per_block;
2689
2690 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002691 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002693 chip->select_chip(mtd, -1);
2694 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696 }
2697 instr->state = MTD_ERASE_DONE;
2698
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002699erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002704 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 nand_release_device(mtd);
2706
David Woodhouse49defc02007-10-06 15:01:59 -04002707 /* Do call back function */
2708 if (!ret)
2709 mtd_erase_callback(instr);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /* Return more or less happy */
2712 return ret;
2713}
2714
2715/**
2716 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002717 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002719 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002721static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Brian Norris289c0522011-07-19 10:06:09 -07002723 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002726 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002728 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002732 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002733 * @mtd: MTD device structure
2734 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002736static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002738 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
2741/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002742 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002743 * @mtd: MTD device structure
2744 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002746static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 int ret;
2749
Florian Fainellif8ac0412010-09-07 13:23:43 +02002750 ret = nand_block_isbad(mtd, ofs);
2751 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002752 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (ret > 0)
2754 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002755 return ret;
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Brian Norris5a0edb22013-07-30 17:52:58 -07002758 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
2761/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002762 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2763 * @mtd: MTD device structure
2764 * @chip: nand chip info structure
2765 * @addr: feature address.
2766 * @subfeature_param: the subfeature parameters, a four bytes array.
2767 */
2768static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2769 int addr, uint8_t *subfeature_param)
2770{
2771 int status;
2772
David Mosbergerd914c932013-05-29 15:30:13 +03002773 if (!chip->onfi_version ||
2774 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2775 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002776 return -EINVAL;
2777
2778 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2779 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2780 status = chip->waitfunc(mtd, chip);
2781 if (status & NAND_STATUS_FAIL)
2782 return -EIO;
2783 return 0;
2784}
2785
2786/**
2787 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2788 * @mtd: MTD device structure
2789 * @chip: nand chip info structure
2790 * @addr: feature address.
2791 * @subfeature_param: the subfeature parameters, a four bytes array.
2792 */
2793static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2794 int addr, uint8_t *subfeature_param)
2795{
David Mosbergerd914c932013-05-29 15:30:13 +03002796 if (!chip->onfi_version ||
2797 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2798 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002799 return -EINVAL;
2800
2801 /* clear the sub feature parameters */
2802 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2803
2804 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2805 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2806 return 0;
2807}
2808
2809/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002810 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002811 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002812 */
2813static int nand_suspend(struct mtd_info *mtd)
2814{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002815 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002816}
2817
2818/**
2819 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002820 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002821 */
2822static void nand_resume(struct mtd_info *mtd)
2823{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002824 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002825
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002826 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002827 nand_release_device(mtd);
2828 else
Brian Norrisd0370212011-07-19 10:06:08 -07002829 pr_err("%s called for a chip which is not in suspended state\n",
2830 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002831}
2832
Brian Norris8b6e50c2011-05-25 14:59:01 -07002833/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002834static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002835{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002837 if (!chip->chip_delay)
2838 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002841 if (chip->cmdfunc == NULL)
2842 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002845 if (chip->waitfunc == NULL)
2846 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002848 if (!chip->select_chip)
2849 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002850
Huang Shijie4204ccc2013-08-16 10:10:07 +08002851 /* set for ONFI nand */
2852 if (!chip->onfi_set_features)
2853 chip->onfi_set_features = nand_onfi_set_features;
2854 if (!chip->onfi_get_features)
2855 chip->onfi_get_features = nand_onfi_get_features;
2856
Brian Norris68e80782013-07-18 01:17:02 -07002857 /* If called twice, pointers that depend on busw may need to be reset */
2858 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002859 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2860 if (!chip->read_word)
2861 chip->read_word = nand_read_word;
2862 if (!chip->block_bad)
2863 chip->block_bad = nand_block_bad;
2864 if (!chip->block_markbad)
2865 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002866 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002867 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Brian Norris68e80782013-07-18 01:17:02 -07002868 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002869 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002870 if (!chip->scan_bbt)
2871 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002872
2873 if (!chip->controller) {
2874 chip->controller = &chip->hwcontrol;
2875 spin_lock_init(&chip->controller->lock);
2876 init_waitqueue_head(&chip->controller->wq);
2877 }
2878
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002879}
2880
Brian Norris8b6e50c2011-05-25 14:59:01 -07002881/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002882static void sanitize_string(uint8_t *s, size_t len)
2883{
2884 ssize_t i;
2885
Brian Norris8b6e50c2011-05-25 14:59:01 -07002886 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002887 s[len - 1] = 0;
2888
Brian Norris8b6e50c2011-05-25 14:59:01 -07002889 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002890 for (i = 0; i < len - 1; i++) {
2891 if (s[i] < ' ' || s[i] > 127)
2892 s[i] = '?';
2893 }
2894
Brian Norris8b6e50c2011-05-25 14:59:01 -07002895 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002896 strim(s);
2897}
2898
2899static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2900{
2901 int i;
2902 while (len--) {
2903 crc ^= *p++ << 8;
2904 for (i = 0; i < 8; i++)
2905 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2906 }
2907
2908 return crc;
2909}
2910
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002911/* Parse the Extended Parameter Page. */
2912static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2913 struct nand_chip *chip, struct nand_onfi_params *p)
2914{
2915 struct onfi_ext_param_page *ep;
2916 struct onfi_ext_section *s;
2917 struct onfi_ext_ecc_info *ecc;
2918 uint8_t *cursor;
2919 int ret = -EINVAL;
2920 int len;
2921 int i;
2922
2923 len = le16_to_cpu(p->ext_param_page_length) * 16;
2924 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07002925 if (!ep)
2926 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002927
2928 /* Send our own NAND_CMD_PARAM. */
2929 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2930
2931 /* Use the Change Read Column command to skip the ONFI param pages. */
2932 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
2933 sizeof(*p) * p->num_of_param_pages , -1);
2934
2935 /* Read out the Extended Parameter Page. */
2936 chip->read_buf(mtd, (uint8_t *)ep, len);
2937 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
2938 != le16_to_cpu(ep->crc))) {
2939 pr_debug("fail in the CRC.\n");
2940 goto ext_out;
2941 }
2942
2943 /*
2944 * Check the signature.
2945 * Do not strictly follow the ONFI spec, maybe changed in future.
2946 */
2947 if (strncmp(ep->sig, "EPPS", 4)) {
2948 pr_debug("The signature is invalid.\n");
2949 goto ext_out;
2950 }
2951
2952 /* find the ECC section. */
2953 cursor = (uint8_t *)(ep + 1);
2954 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
2955 s = ep->sections + i;
2956 if (s->type == ONFI_SECTION_TYPE_2)
2957 break;
2958 cursor += s->length * 16;
2959 }
2960 if (i == ONFI_EXT_SECTION_MAX) {
2961 pr_debug("We can not find the ECC section.\n");
2962 goto ext_out;
2963 }
2964
2965 /* get the info we want. */
2966 ecc = (struct onfi_ext_ecc_info *)cursor;
2967
Brian Norris4ae7d222013-09-16 18:20:21 -07002968 if (!ecc->codeword_size) {
2969 pr_debug("Invalid codeword size\n");
2970 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002971 }
2972
Brian Norris4ae7d222013-09-16 18:20:21 -07002973 chip->ecc_strength_ds = ecc->ecc_bits;
2974 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07002975 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002976
2977ext_out:
2978 kfree(ep);
2979 return ret;
2980}
2981
Brian Norris8429bb32013-12-03 15:51:09 -08002982static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
2983{
2984 struct nand_chip *chip = mtd->priv;
2985 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
2986
2987 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
2988 feature);
2989}
2990
2991/*
2992 * Configure chip properties from Micron vendor-specific ONFI table
2993 */
2994static void nand_onfi_detect_micron(struct nand_chip *chip,
2995 struct nand_onfi_params *p)
2996{
2997 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
2998
2999 if (le16_to_cpu(p->vendor_revision) < 1)
3000 return;
3001
3002 chip->read_retries = micron->read_retry_options;
3003 chip->setup_read_retry = nand_setup_read_retry_micron;
3004}
3005
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003006/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003007 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003008 */
3009static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003010 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003011{
3012 struct nand_onfi_params *p = &chip->onfi_params;
3013 int i;
3014 int val;
3015
Brian Norris7854d3f2011-06-23 14:12:08 -07003016 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003017 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3018 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3019 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3020 return 0;
3021
Brian Norrisc7f23a72013-08-13 10:51:55 -07003022 /*
3023 * ONFI must be probed in 8-bit mode or with NAND_BUSWIDTH_AUTO, not
3024 * with NAND_BUSWIDTH_16
3025 */
3026 if (chip->options & NAND_BUSWIDTH_16) {
3027 pr_err("ONFI cannot be probed in 16-bit mode; aborting\n");
3028 return 0;
3029 }
3030
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003031 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3032 for (i = 0; i < 3; i++) {
3033 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
3034 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3035 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003036 break;
3037 }
3038 }
3039
Brian Norrisc7f23a72013-08-13 10:51:55 -07003040 if (i == 3) {
3041 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003042 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003043 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003044
Brian Norris8b6e50c2011-05-25 14:59:01 -07003045 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003046 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003047 if (val & (1 << 5))
3048 chip->onfi_version = 23;
3049 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003050 chip->onfi_version = 22;
3051 else if (val & (1 << 3))
3052 chip->onfi_version = 21;
3053 else if (val & (1 << 2))
3054 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003055 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003056 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003057
3058 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003059 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003060 return 0;
3061 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003062
3063 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3064 sanitize_string(p->model, sizeof(p->model));
3065 if (!mtd->name)
3066 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003067
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003068 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003069
3070 /*
3071 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3072 * (don't ask me who thought of this...). MTD assumes that these
3073 * dimensions will be power-of-2, so just truncate the remaining area.
3074 */
3075 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3076 mtd->erasesize *= mtd->writesize;
3077
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003078 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003079
3080 /* See erasesize comment */
3081 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003082 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003083 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003084
3085 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003086 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003087 else
3088 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003089
Huang Shijie10c86ba2013-05-17 11:17:26 +08003090 if (p->ecc_bits != 0xff) {
3091 chip->ecc_strength_ds = p->ecc_bits;
3092 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003093 } else if (chip->onfi_version >= 21 &&
3094 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3095
3096 /*
3097 * The nand_flash_detect_ext_param_page() uses the
3098 * Change Read Column command which maybe not supported
3099 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3100 * now. We do not replace user supplied command function.
3101 */
3102 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3103 chip->cmdfunc = nand_command_lp;
3104
3105 /* The Extended Parameter Page is supported since ONFI 2.1. */
3106 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003107 pr_warn("Failed to detect ONFI extended param page\n");
3108 } else {
3109 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003110 }
3111
Brian Norris8429bb32013-12-03 15:51:09 -08003112 if (p->jedec_id == NAND_MFR_MICRON)
3113 nand_onfi_detect_micron(chip, p);
3114
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003115 return 1;
3116}
3117
3118/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003119 * nand_id_has_period - Check if an ID string has a given wraparound period
3120 * @id_data: the ID string
3121 * @arrlen: the length of the @id_data array
3122 * @period: the period of repitition
3123 *
3124 * Check if an ID string is repeated within a given sequence of bytes at
3125 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003126 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003127 * if the repetition has a period of @period; otherwise, returns zero.
3128 */
3129static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3130{
3131 int i, j;
3132 for (i = 0; i < period; i++)
3133 for (j = i + period; j < arrlen; j += period)
3134 if (id_data[i] != id_data[j])
3135 return 0;
3136 return 1;
3137}
3138
3139/*
3140 * nand_id_len - Get the length of an ID string returned by CMD_READID
3141 * @id_data: the ID string
3142 * @arrlen: the length of the @id_data array
3143
3144 * Returns the length of the ID string, according to known wraparound/trailing
3145 * zero patterns. If no pattern exists, returns the length of the array.
3146 */
3147static int nand_id_len(u8 *id_data, int arrlen)
3148{
3149 int last_nonzero, period;
3150
3151 /* Find last non-zero byte */
3152 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3153 if (id_data[last_nonzero])
3154 break;
3155
3156 /* All zeros */
3157 if (last_nonzero < 0)
3158 return 0;
3159
3160 /* Calculate wraparound period */
3161 for (period = 1; period < arrlen; period++)
3162 if (nand_id_has_period(id_data, arrlen, period))
3163 break;
3164
3165 /* There's a repeated pattern */
3166 if (period < arrlen)
3167 return period;
3168
3169 /* There are trailing zeros */
3170 if (last_nonzero < arrlen - 1)
3171 return last_nonzero + 1;
3172
3173 /* No pattern detected */
3174 return arrlen;
3175}
3176
Huang Shijie7db906b2013-09-25 14:58:11 +08003177/* Extract the bits of per cell from the 3rd byte of the extended ID */
3178static int nand_get_bits_per_cell(u8 cellinfo)
3179{
3180 int bits;
3181
3182 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3183 bits >>= NAND_CI_CELLTYPE_SHIFT;
3184 return bits + 1;
3185}
3186
Brian Norrise3b88bd2012-09-24 20:40:52 -07003187/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003188 * Many new NAND share similar device ID codes, which represent the size of the
3189 * chip. The rest of the parameters must be decoded according to generic or
3190 * manufacturer-specific "extended ID" decoding patterns.
3191 */
3192static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3193 u8 id_data[8], int *busw)
3194{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003195 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003196 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003197 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003198 /* The 4th id byte is the important one */
3199 extid = id_data[3];
3200
Brian Norrise3b88bd2012-09-24 20:40:52 -07003201 id_len = nand_id_len(id_data, 8);
3202
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003203 /*
3204 * Field definitions are in the following datasheets:
3205 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003206 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003207 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003208 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003209 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3210 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003211 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003212 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003213 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003214 /* Calc pagesize */
3215 mtd->writesize = 2048 << (extid & 0x03);
3216 extid >>= 2;
3217 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003218 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003219 case 1:
3220 mtd->oobsize = 128;
3221 break;
3222 case 2:
3223 mtd->oobsize = 218;
3224 break;
3225 case 3:
3226 mtd->oobsize = 400;
3227 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003228 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003229 mtd->oobsize = 436;
3230 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003231 case 5:
3232 mtd->oobsize = 512;
3233 break;
3234 case 6:
3235 default: /* Other cases are "reserved" (unknown) */
3236 mtd->oobsize = 640;
3237 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003238 }
3239 extid >>= 2;
3240 /* Calc blocksize */
3241 mtd->erasesize = (128 * 1024) <<
3242 (((extid >> 1) & 0x04) | (extid & 0x03));
3243 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003244 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003245 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003246 unsigned int tmp;
3247
3248 /* Calc pagesize */
3249 mtd->writesize = 2048 << (extid & 0x03);
3250 extid >>= 2;
3251 /* Calc oobsize */
3252 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3253 case 0:
3254 mtd->oobsize = 128;
3255 break;
3256 case 1:
3257 mtd->oobsize = 224;
3258 break;
3259 case 2:
3260 mtd->oobsize = 448;
3261 break;
3262 case 3:
3263 mtd->oobsize = 64;
3264 break;
3265 case 4:
3266 mtd->oobsize = 32;
3267 break;
3268 case 5:
3269 mtd->oobsize = 16;
3270 break;
3271 default:
3272 mtd->oobsize = 640;
3273 break;
3274 }
3275 extid >>= 2;
3276 /* Calc blocksize */
3277 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3278 if (tmp < 0x03)
3279 mtd->erasesize = (128 * 1024) << tmp;
3280 else if (tmp == 0x03)
3281 mtd->erasesize = 768 * 1024;
3282 else
3283 mtd->erasesize = (64 * 1024) << tmp;
3284 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003285 } else {
3286 /* Calc pagesize */
3287 mtd->writesize = 1024 << (extid & 0x03);
3288 extid >>= 2;
3289 /* Calc oobsize */
3290 mtd->oobsize = (8 << (extid & 0x01)) *
3291 (mtd->writesize >> 9);
3292 extid >>= 2;
3293 /* Calc blocksize. Blocksize is multiples of 64KiB */
3294 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3295 extid >>= 2;
3296 /* Get buswidth information */
3297 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003298
3299 /*
3300 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3301 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3302 * follows:
3303 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3304 * 110b -> 24nm
3305 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3306 */
3307 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003308 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003309 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3310 !(id_data[4] & 0x80) /* !BENAND */) {
3311 mtd->oobsize = 32 * mtd->writesize >> 9;
3312 }
3313
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003314 }
3315}
3316
3317/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003318 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3319 * decodes a matching ID table entry and assigns the MTD size parameters for
3320 * the chip.
3321 */
3322static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3323 struct nand_flash_dev *type, u8 id_data[8],
3324 int *busw)
3325{
3326 int maf_id = id_data[0];
3327
3328 mtd->erasesize = type->erasesize;
3329 mtd->writesize = type->pagesize;
3330 mtd->oobsize = mtd->writesize / 32;
3331 *busw = type->options & NAND_BUSWIDTH_16;
3332
Huang Shijie1c195e92013-09-25 14:58:12 +08003333 /* All legacy ID NAND are small-page, SLC */
3334 chip->bits_per_cell = 1;
3335
Brian Norrisf23a4812012-09-24 20:40:51 -07003336 /*
3337 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3338 * some Spansion chips have erasesize that conflicts with size
3339 * listed in nand_ids table.
3340 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3341 */
3342 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3343 && id_data[6] == 0x00 && id_data[7] == 0x00
3344 && mtd->writesize == 512) {
3345 mtd->erasesize = 128 * 1024;
3346 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3347 }
3348}
3349
3350/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003351 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3352 * heuristic patterns using various detected parameters (e.g., manufacturer,
3353 * page size, cell-type information).
3354 */
3355static void nand_decode_bbm_options(struct mtd_info *mtd,
3356 struct nand_chip *chip, u8 id_data[8])
3357{
3358 int maf_id = id_data[0];
3359
3360 /* Set the bad block position */
3361 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3362 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3363 else
3364 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3365
3366 /*
3367 * Bad block marker is stored in the last page of each block on Samsung
3368 * and Hynix MLC devices; stored in first two pages of each block on
3369 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3370 * AMD/Spansion, and Macronix. All others scan only the first page.
3371 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003372 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003373 (maf_id == NAND_MFR_SAMSUNG ||
3374 maf_id == NAND_MFR_HYNIX))
3375 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003376 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003377 (maf_id == NAND_MFR_SAMSUNG ||
3378 maf_id == NAND_MFR_HYNIX ||
3379 maf_id == NAND_MFR_TOSHIBA ||
3380 maf_id == NAND_MFR_AMD ||
3381 maf_id == NAND_MFR_MACRONIX)) ||
3382 (mtd->writesize == 2048 &&
3383 maf_id == NAND_MFR_MICRON))
3384 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3385}
3386
Huang Shijieec6e87e2013-03-15 11:01:00 +08003387static inline bool is_full_id_nand(struct nand_flash_dev *type)
3388{
3389 return type->id_len;
3390}
3391
3392static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3393 struct nand_flash_dev *type, u8 *id_data, int *busw)
3394{
3395 if (!strncmp(type->id, id_data, type->id_len)) {
3396 mtd->writesize = type->pagesize;
3397 mtd->erasesize = type->erasesize;
3398 mtd->oobsize = type->oobsize;
3399
Huang Shijie7db906b2013-09-25 14:58:11 +08003400 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003401 chip->chipsize = (uint64_t)type->chipsize << 20;
3402 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003403 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3404 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003405
3406 *busw = type->options & NAND_BUSWIDTH_16;
3407
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003408 if (!mtd->name)
3409 mtd->name = type->name;
3410
Huang Shijieec6e87e2013-03-15 11:01:00 +08003411 return true;
3412 }
3413 return false;
3414}
3415
Brian Norris7e74c2d2012-09-24 20:40:49 -07003416/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003417 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003418 */
3419static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003420 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003421 int busw,
3422 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003423 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003424{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003425 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003426 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003429 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Karl Beldanef89a882008-09-15 14:37:29 +02003431 /*
3432 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003433 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003434 */
3435 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003438 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003441 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003442 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Brian Norris8b6e50c2011-05-25 14:59:01 -07003444 /*
3445 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003446 * interface concerns can cause random data which looks like a
3447 * possibly credible NAND flash to appear. If the two results do
3448 * not match, ignore the device completely.
3449 */
3450
3451 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3452
Brian Norris4aef9b72012-09-24 20:40:48 -07003453 /* Read entire ID string */
3454 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003455 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003456
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003457 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003458 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003459 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003460 return ERR_PTR(-ENODEV);
3461 }
3462
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003463 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003464 type = nand_flash_ids;
3465
Huang Shijieec6e87e2013-03-15 11:01:00 +08003466 for (; type->name != NULL; type++) {
3467 if (is_full_id_nand(type)) {
3468 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3469 goto ident_done;
3470 } else if (*dev_id == type->dev_id) {
3471 break;
3472 }
3473 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003474
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003475 chip->onfi_version = 0;
3476 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003477 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003478 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003479 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003480 }
3481
David Woodhouse5e81e882010-02-26 18:32:56 +00003482 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003483 return ERR_PTR(-ENODEV);
3484
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003485 if (!mtd->name)
3486 mtd->name = type->name;
3487
Adrian Hunter69423d92008-12-10 13:37:21 +00003488 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003489
Huang Shijie12a40a52010-09-27 10:43:53 +08003490 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003491 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003492 busw = chip->init_size(mtd, chip, id_data);
3493 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003494 /* Decode parameters from extended ID */
3495 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003496 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003497 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003498 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003499 /* Get chip options */
3500 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003501
Brian Norris8b6e50c2011-05-25 14:59:01 -07003502 /*
3503 * Check if chip is not a Samsung device. Do not clear the
3504 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003505 */
3506 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3507 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3508ident_done:
3509
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003510 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003511 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003512 if (nand_manuf_ids[maf_idx].id == *maf_id)
3513 break;
3514 }
3515
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003516 if (chip->options & NAND_BUSWIDTH_AUTO) {
3517 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3518 chip->options |= busw;
3519 nand_set_defaults(chip, busw);
3520 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3521 /*
3522 * Check, if buswidth is correct. Hardware drivers should set
3523 * chip correct!
3524 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003525 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3526 *maf_id, *dev_id);
3527 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3528 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003529 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3530 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003531 return ERR_PTR(-EINVAL);
3532 }
3533
Brian Norris7e74c2d2012-09-24 20:40:49 -07003534 nand_decode_bbm_options(mtd, chip, id_data);
3535
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003536 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003537 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003538 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003539 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003540
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003541 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003542 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003543 if (chip->chipsize & 0xffffffff)
3544 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003545 else {
3546 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3547 chip->chip_shift += 32 - 1;
3548 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003549
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003550 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003551 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003552
Brian Norris8b6e50c2011-05-25 14:59:01 -07003553 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003554 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3555 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003556
Ezequiel Garcia20171642013-11-25 08:30:31 -03003557 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3558 *maf_id, *dev_id);
3559 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
Huang Shijie3723e932013-09-25 14:58:14 +08003560 chip->onfi_version ? chip->onfi_params.model : type->name);
Ezequiel Garcia20171642013-11-25 08:30:31 -03003561 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003562 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3563 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003564 return type;
3565}
3566
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003567/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003568 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003569 * @mtd: MTD device structure
3570 * @maxchips: number of chips to scan for
3571 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003572 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003573 * This is the first phase of the normal nand_scan() function. It reads the
3574 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003575 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003576 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003577 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003578int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3579 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003580{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003581 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003582 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003583 struct nand_flash_dev *type;
3584
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003585 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003586 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003587 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003588 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003589
3590 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003591 type = nand_get_flash_type(mtd, chip, busw,
3592 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003593
3594 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003595 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003596 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003597 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003598 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
3600
Huang Shijie07300162012-11-09 16:23:45 +08003601 chip->select_chip(mtd, -1);
3602
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003603 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003604 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003605 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003606 /* See comment in nand_get_flash_type for reset */
3607 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003609 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003611 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003612 nand_dev_id != chip->read_byte(mtd)) {
3613 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 break;
Huang Shijie07300162012-11-09 16:23:45 +08003615 }
3616 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 }
3618 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003619 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003622 chip->numchips = i;
3623 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
David Woodhouse3b85c322006-09-25 17:06:53 +01003625 return 0;
3626}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003627EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003628
3629
3630/**
3631 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003632 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003633 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003634 * This is the second phase of the normal nand_scan() function. It fills out
3635 * all the uninitialized function pointers with the defaults and scans for a
3636 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003637 */
3638int nand_scan_tail(struct mtd_info *mtd)
3639{
3640 int i;
3641 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003642 struct nand_ecc_ctrl *ecc = &chip->ecc;
David Woodhouse3b85c322006-09-25 17:06:53 +01003643
Brian Norrise2414f42012-02-06 13:44:00 -08003644 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3645 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3646 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3647
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003648 if (!(chip->options & NAND_OWN_BUFFERS))
3649 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3650 if (!chip->buffers)
3651 return -ENOMEM;
3652
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003653 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003654 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003655
3656 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003657 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003658 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003659 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003660 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003662 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
3664 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003665 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 break;
3667 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003668 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003670 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003671 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003674 pr_warn("No oob scheme defined for oobsize %d\n",
3675 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 BUG();
3677 }
3678 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003679
David Woodhouse956e9442006-09-25 17:12:39 +01003680 if (!chip->write_page)
3681 chip->write_page = nand_write_page;
3682
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003683 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003684 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003685 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003686 */
David Woodhouse956e9442006-09-25 17:12:39 +01003687
Huang Shijie97de79e02013-10-18 14:20:53 +08003688 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003689 case NAND_ECC_HW_OOB_FIRST:
3690 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003691 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003692 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003693 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003694 BUG();
3695 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003696 if (!ecc->read_page)
3697 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003698
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003699 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003700 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003701 if (!ecc->read_page)
3702 ecc->read_page = nand_read_page_hwecc;
3703 if (!ecc->write_page)
3704 ecc->write_page = nand_write_page_hwecc;
3705 if (!ecc->read_page_raw)
3706 ecc->read_page_raw = nand_read_page_raw;
3707 if (!ecc->write_page_raw)
3708 ecc->write_page_raw = nand_write_page_raw;
3709 if (!ecc->read_oob)
3710 ecc->read_oob = nand_read_oob_std;
3711 if (!ecc->write_oob)
3712 ecc->write_oob = nand_write_oob_std;
3713 if (!ecc->read_subpage)
3714 ecc->read_subpage = nand_read_subpage;
3715 if (!ecc->write_subpage)
3716 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003717
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003718 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003719 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3720 (!ecc->read_page ||
3721 ecc->read_page == nand_read_page_hwecc ||
3722 !ecc->write_page ||
3723 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003724 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003725 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003726 BUG();
3727 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003728 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003729 if (!ecc->read_page)
3730 ecc->read_page = nand_read_page_syndrome;
3731 if (!ecc->write_page)
3732 ecc->write_page = nand_write_page_syndrome;
3733 if (!ecc->read_page_raw)
3734 ecc->read_page_raw = nand_read_page_raw_syndrome;
3735 if (!ecc->write_page_raw)
3736 ecc->write_page_raw = nand_write_page_raw_syndrome;
3737 if (!ecc->read_oob)
3738 ecc->read_oob = nand_read_oob_syndrome;
3739 if (!ecc->write_oob)
3740 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003741
Huang Shijie97de79e02013-10-18 14:20:53 +08003742 if (mtd->writesize >= ecc->size) {
3743 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003744 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3745 BUG();
3746 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003747 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003748 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003749 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003750 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003751 ecc->size, mtd->writesize);
3752 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003754 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003755 ecc->calculate = nand_calculate_ecc;
3756 ecc->correct = nand_correct_data;
3757 ecc->read_page = nand_read_page_swecc;
3758 ecc->read_subpage = nand_read_subpage;
3759 ecc->write_page = nand_write_page_swecc;
3760 ecc->read_page_raw = nand_read_page_raw;
3761 ecc->write_page_raw = nand_write_page_raw;
3762 ecc->read_oob = nand_read_oob_std;
3763 ecc->write_oob = nand_write_oob_std;
3764 if (!ecc->size)
3765 ecc->size = 256;
3766 ecc->bytes = 3;
3767 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003769
Ivan Djelic193bd402011-03-11 11:05:33 +01003770 case NAND_ECC_SOFT_BCH:
3771 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003772 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003773 BUG();
3774 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003775 ecc->calculate = nand_bch_calculate_ecc;
3776 ecc->correct = nand_bch_correct_data;
3777 ecc->read_page = nand_read_page_swecc;
3778 ecc->read_subpage = nand_read_subpage;
3779 ecc->write_page = nand_write_page_swecc;
3780 ecc->read_page_raw = nand_read_page_raw;
3781 ecc->write_page_raw = nand_write_page_raw;
3782 ecc->read_oob = nand_read_oob_std;
3783 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01003784 /*
3785 * Board driver should supply ecc.size and ecc.bytes values to
3786 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003787 * for details. Otherwise, default to 4 bits for large page
3788 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003789 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003790 if (!ecc->size && (mtd->oobsize >= 64)) {
3791 ecc->size = 512;
3792 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01003793 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003794 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3795 &ecc->layout);
3796 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003797 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003798 BUG();
3799 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003800 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003801 break;
3802
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003803 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003804 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003805 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08003806 ecc->read_page = nand_read_page_raw;
3807 ecc->write_page = nand_write_page_raw;
3808 ecc->read_oob = nand_read_oob_std;
3809 ecc->read_page_raw = nand_read_page_raw;
3810 ecc->write_page_raw = nand_write_page_raw;
3811 ecc->write_oob = nand_write_oob_std;
3812 ecc->size = mtd->writesize;
3813 ecc->bytes = 0;
3814 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08003818 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003819 BUG();
3820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Brian Norris9ce244b2011-08-30 18:45:37 -07003822 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08003823 if (!ecc->read_oob_raw)
3824 ecc->read_oob_raw = ecc->read_oob;
3825 if (!ecc->write_oob_raw)
3826 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003827
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003828 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003829 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003830 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003831 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003832 ecc->layout->oobavail = 0;
3833 for (i = 0; ecc->layout->oobfree[i].length
3834 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
3835 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
3836 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003837
3838 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003839 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003840 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003841 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003842 ecc->steps = mtd->writesize / ecc->size;
3843 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003844 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003845 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003847 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003848
Brian Norris8b6e50c2011-05-25 14:59:01 -07003849 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003850 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08003851 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003852 case 2:
3853 mtd->subpage_sft = 1;
3854 break;
3855 case 4:
3856 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003857 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003858 mtd->subpage_sft = 2;
3859 break;
3860 }
3861 }
3862 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3863
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003864 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003865 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003868 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003870 /* Large page NAND with SOFT_ECC should support subpage reads */
Huang Shijie97de79e02013-10-18 14:20:53 +08003871 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003872 chip->options |= NAND_SUBPAGE_READ;
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08003875 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003876 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3877 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003878 mtd->_erase = nand_erase;
3879 mtd->_point = NULL;
3880 mtd->_unpoint = NULL;
3881 mtd->_read = nand_read;
3882 mtd->_write = nand_write;
3883 mtd->_panic_write = panic_nand_write;
3884 mtd->_read_oob = nand_read_oob;
3885 mtd->_write_oob = nand_write_oob;
3886 mtd->_sync = nand_sync;
3887 mtd->_lock = NULL;
3888 mtd->_unlock = NULL;
3889 mtd->_suspend = nand_suspend;
3890 mtd->_resume = nand_resume;
3891 mtd->_block_isbad = nand_block_isbad;
3892 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003893 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Mike Dunn6a918ba2012-03-11 14:21:11 -07003895 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08003896 mtd->ecclayout = ecc->layout;
3897 mtd->ecc_strength = ecc->strength;
3898 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003899 /*
3900 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3901 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3902 * properly set.
3903 */
3904 if (!mtd->bitflip_threshold)
3905 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003907 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003908 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003912 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003914EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Brian Norris8b6e50c2011-05-25 14:59:01 -07003916/*
3917 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003918 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003919 * to call us from in-kernel code if the core NAND support is modular.
3920 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003921#ifdef MODULE
3922#define caller_is_module() (1)
3923#else
3924#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003925 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003926#endif
3927
3928/**
3929 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003930 * @mtd: MTD device structure
3931 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003932 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003933 * This fills out all the uninitialized function pointers with the defaults.
3934 * The flash ID is read and the mtd/chip structures are filled with the
3935 * appropriate values. The mtd->owner field must be set to the module of the
3936 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003937 */
3938int nand_scan(struct mtd_info *mtd, int maxchips)
3939{
3940 int ret;
3941
3942 /* Many callers got this wrong, so check for it for a while... */
3943 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003944 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003945 BUG();
3946 }
3947
David Woodhouse5e81e882010-02-26 18:32:56 +00003948 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003949 if (!ret)
3950 ret = nand_scan_tail(mtd);
3951 return ret;
3952}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003953EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003956 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003957 * @mtd: MTD device structure
3958 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003959void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003961 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Ivan Djelic193bd402011-03-11 11:05:33 +01003963 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3964 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3965
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003966 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Jesper Juhlfa671642005-11-07 01:01:27 -08003968 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003969 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003970 if (!(chip->options & NAND_OWN_BUFFERS))
3971 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003972
3973 /* Free bad block descriptor memory */
3974 if (chip->badblock_pattern && chip->badblock_pattern->options
3975 & NAND_BBT_DYNAMICSTRUCT)
3976 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
David Woodhousee0c7d762006-05-13 18:07:53 +01003978EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003979
3980static int __init nand_base_init(void)
3981{
3982 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3983 return 0;
3984}
3985
3986static void __exit nand_base_exit(void)
3987{
3988 led_trigger_unregister_simple(nand_led_trigger);
3989}
3990
3991module_init(nand_base_init);
3992module_exit(nand_base_exit);
3993
David Woodhousee0c7d762006-05-13 18:07:53 +01003994MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003995MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3996MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003997MODULE_DESCRIPTION("Generic NAND flash driver code");