blob: b5c3768b32aef6d62fbe9f0555ca2b1f3327cc55 [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 Norris7854d3f2011-06-23 14:12:08 -07001413 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001414 * @mtd: MTD device structure
1415 * @from: offset to read from
1416 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001417 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001418 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001419 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001420static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1421 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001422{
Brian Norrise47f3db2012-05-02 10:14:56 -07001423 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001424 struct nand_chip *chip = mtd->priv;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001425 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001426 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001427 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001428 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001429 mtd->oobavail : mtd->oobsize;
1430
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001431 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001432 unsigned int max_bitflips = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001433 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001435 chipnr = (int)(from >> chip->chip_shift);
1436 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001438 realpage = (int)(from >> chip->page_shift);
1439 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001441 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001443 buf = ops->datbuf;
1444 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001445 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001446
Florian Fainellif8ac0412010-09-07 13:23:43 +02001447 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001448 unsigned int ecc_failures = mtd->ecc_stats.failed;
1449
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001450 bytes = min(mtd->writesize - col, readlen);
1451 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001452
Brian Norris8b6e50c2011-05-25 14:59:01 -07001453 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001454 if (realpage != chip->pagebuf || oob) {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001455 bufpoi = aligned ? buf : chip->buffers->databuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Brian Norrisc00a0992012-05-01 17:12:54 -07001457 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Mike Dunnedbc45402012-04-25 12:06:11 -07001459 /*
1460 * Now read the page into the buffer. Absent an error,
1461 * the read methods return max bitflips per ecc step.
1462 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001463 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001464 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001465 oob_required,
1466 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001467 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1468 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001469 ret = chip->ecc.read_subpage(mtd, chip,
1470 col, bytes, bufpoi);
David Woodhouse956e9442006-09-25 17:12:39 +01001471 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001472 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001473 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001474 if (ret < 0) {
1475 if (!aligned)
1476 /* Invalidate page cache */
1477 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001478 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001479 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001480
Mike Dunnedbc45402012-04-25 12:06:11 -07001481 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1482
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001483 /* Transfer not aligned data */
1484 if (!aligned) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001485 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001486 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001487 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001488 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001489 chip->pagebuf_bitflips = ret;
1490 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001491 /* Invalidate page cache */
1492 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001493 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001494 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001496
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001497 buf += bytes;
1498
1499 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001500 int toread = min(oobreadlen, max_oobsize);
1501
1502 if (toread) {
1503 oob = nand_transfer_oob(chip,
1504 oob, ops, toread);
1505 oobreadlen -= toread;
1506 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001507 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001508
1509 if (chip->options & NAND_NEED_READRDY) {
1510 /* Apply delay or wait for ready/busy pin */
1511 if (!chip->dev_ready)
1512 udelay(chip->chip_delay);
1513 else
1514 nand_wait_ready(mtd);
1515 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001516
1517 if (mtd->ecc_stats.failed - ecc_failures)
1518 ecc_fail = true;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001519 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001520 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001521 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001522 max_bitflips = max_t(unsigned int, max_bitflips,
1523 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001526 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001527
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001528 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Brian Norris8b6e50c2011-05-25 14:59:01 -07001531 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 col = 0;
1533 /* Increment page address */
1534 realpage++;
1535
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001536 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /* Check, if we cross a chip boundary */
1538 if (!page) {
1539 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001540 chip->select_chip(mtd, -1);
1541 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001544 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001546 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001547 if (oob)
1548 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Mike Dunn3f91e942012-04-25 12:06:09 -07001550 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001551 return ret;
1552
Brian Norrisb72f3df2013-12-03 11:04:14 -08001553 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001554 return -EBADMSG;
1555
Mike Dunnedbc45402012-04-25 12:06:11 -07001556 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001557}
1558
1559/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001560 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001561 * @mtd: MTD device structure
1562 * @from: offset to read from
1563 * @len: number of bytes to read
1564 * @retlen: pointer to variable to store the number of read bytes
1565 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001566 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001567 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001568 */
1569static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1570 size_t *retlen, uint8_t *buf)
1571{
Brian Norris4a89ff82011-08-30 18:45:45 -07001572 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001573 int ret;
1574
Huang Shijie6a8214a2012-11-19 14:43:30 +08001575 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001576 ops.len = len;
1577 ops.datbuf = buf;
1578 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001579 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001580 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001581 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001582 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001583 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001587 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001588 * @mtd: mtd info structure
1589 * @chip: nand chip info structure
1590 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001591 */
1592static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001593 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001594{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001595 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001596 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001597 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001598}
1599
1600/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001601 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001602 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001603 * @mtd: mtd info structure
1604 * @chip: nand chip info structure
1605 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001606 */
1607static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001608 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001609{
1610 uint8_t *buf = chip->oob_poi;
1611 int length = mtd->oobsize;
1612 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1613 int eccsize = chip->ecc.size;
1614 uint8_t *bufpoi = buf;
1615 int i, toread, sndrnd = 0, pos;
1616
1617 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1618 for (i = 0; i < chip->ecc.steps; i++) {
1619 if (sndrnd) {
1620 pos = eccsize + i * (eccsize + chunk);
1621 if (mtd->writesize > 512)
1622 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1623 else
1624 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1625 } else
1626 sndrnd = 1;
1627 toread = min_t(int, length, chunk);
1628 chip->read_buf(mtd, bufpoi, toread);
1629 bufpoi += toread;
1630 length -= toread;
1631 }
1632 if (length > 0)
1633 chip->read_buf(mtd, bufpoi, length);
1634
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001635 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001636}
1637
1638/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001639 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001640 * @mtd: mtd info structure
1641 * @chip: nand chip info structure
1642 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001643 */
1644static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1645 int page)
1646{
1647 int status = 0;
1648 const uint8_t *buf = chip->oob_poi;
1649 int length = mtd->oobsize;
1650
1651 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1652 chip->write_buf(mtd, buf, length);
1653 /* Send command to program the OOB data */
1654 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1655
1656 status = chip->waitfunc(mtd, chip);
1657
Savin Zlobec0d420f92006-06-21 11:51:20 +02001658 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001659}
1660
1661/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001662 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001663 * with syndrome - only for large page flash
1664 * @mtd: mtd info structure
1665 * @chip: nand chip info structure
1666 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001667 */
1668static int nand_write_oob_syndrome(struct mtd_info *mtd,
1669 struct nand_chip *chip, int page)
1670{
1671 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1672 int eccsize = chip->ecc.size, length = mtd->oobsize;
1673 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1674 const uint8_t *bufpoi = chip->oob_poi;
1675
1676 /*
1677 * data-ecc-data-ecc ... ecc-oob
1678 * or
1679 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1680 */
1681 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1682 pos = steps * (eccsize + chunk);
1683 steps = 0;
1684 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001685 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001686
1687 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1688 for (i = 0; i < steps; i++) {
1689 if (sndcmd) {
1690 if (mtd->writesize <= 512) {
1691 uint32_t fill = 0xFFFFFFFF;
1692
1693 len = eccsize;
1694 while (len > 0) {
1695 int num = min_t(int, len, 4);
1696 chip->write_buf(mtd, (uint8_t *)&fill,
1697 num);
1698 len -= num;
1699 }
1700 } else {
1701 pos = eccsize + i * (eccsize + chunk);
1702 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1703 }
1704 } else
1705 sndcmd = 1;
1706 len = min_t(int, length, chunk);
1707 chip->write_buf(mtd, bufpoi, len);
1708 bufpoi += len;
1709 length -= len;
1710 }
1711 if (length > 0)
1712 chip->write_buf(mtd, bufpoi, length);
1713
1714 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1715 status = chip->waitfunc(mtd, chip);
1716
1717 return status & NAND_STATUS_FAIL ? -EIO : 0;
1718}
1719
1720/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001721 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001722 * @mtd: MTD device structure
1723 * @from: offset to read from
1724 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001726 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001728static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1729 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Brian Norrisc00a0992012-05-01 17:12:54 -07001731 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001732 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001733 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001734 int readlen = ops->ooblen;
1735 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001736 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001737 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Brian Norris289c0522011-07-19 10:06:09 -07001739 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301740 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Brian Norris041e4572011-06-23 16:45:24 -07001742 stats = mtd->ecc_stats;
1743
Brian Norris0612b9d2011-08-30 18:45:40 -07001744 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001745 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001746 else
1747 len = mtd->oobsize;
1748
1749 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001750 pr_debug("%s: attempt to start read outside oob\n",
1751 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001752 return -EINVAL;
1753 }
1754
1755 /* Do not allow reads past end of device */
1756 if (unlikely(from >= mtd->size ||
1757 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1758 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001759 pr_debug("%s: attempt to read beyond end of device\n",
1760 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001761 return -EINVAL;
1762 }
Vitaly Wool70145682006-11-03 18:20:38 +03001763
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001764 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001765 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001767 /* Shift to get page */
1768 realpage = (int)(from >> chip->page_shift);
1769 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Florian Fainellif8ac0412010-09-07 13:23:43 +02001771 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001772 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001773 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001774 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001775 ret = chip->ecc.read_oob(mtd, chip, page);
1776
1777 if (ret < 0)
1778 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001779
1780 len = min(len, readlen);
1781 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001782
Brian Norris5bc7c332013-03-13 09:51:31 -07001783 if (chip->options & NAND_NEED_READRDY) {
1784 /* Apply delay or wait for ready/busy pin */
1785 if (!chip->dev_ready)
1786 udelay(chip->chip_delay);
1787 else
1788 nand_wait_ready(mtd);
1789 }
1790
Vitaly Wool70145682006-11-03 18:20:38 +03001791 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001792 if (!readlen)
1793 break;
1794
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001795 /* Increment page address */
1796 realpage++;
1797
1798 page = realpage & chip->pagemask;
1799 /* Check, if we cross a chip boundary */
1800 if (!page) {
1801 chipnr++;
1802 chip->select_chip(mtd, -1);
1803 chip->select_chip(mtd, chipnr);
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001806 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001808 ops->oobretlen = ops->ooblen - readlen;
1809
1810 if (ret < 0)
1811 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001812
1813 if (mtd->ecc_stats.failed - stats.failed)
1814 return -EBADMSG;
1815
1816 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
1819/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001820 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001821 * @mtd: MTD device structure
1822 * @from: offset to read from
1823 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001825 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001827static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1828 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001830 int ret = -ENOTSUPP;
1831
1832 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001835 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001836 pr_debug("%s: attempt to read beyond end of device\n",
1837 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return -EINVAL;
1839 }
1840
Huang Shijie6a8214a2012-11-19 14:43:30 +08001841 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Florian Fainellif8ac0412010-09-07 13:23:43 +02001843 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001844 case MTD_OPS_PLACE_OOB:
1845 case MTD_OPS_AUTO_OOB:
1846 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001847 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001848
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001849 default:
1850 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001853 if (!ops->datbuf)
1854 ret = nand_do_read_oob(mtd, from, ops);
1855 else
1856 ret = nand_do_read_ops(mtd, from, ops);
1857
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001858out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001860 return ret;
1861}
1862
1863
1864/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001865 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001866 * @mtd: mtd info structure
1867 * @chip: nand chip info structure
1868 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001869 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001870 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001871 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001872 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001873static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001874 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001875{
1876 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001877 if (oob_required)
1878 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001879
1880 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001883/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001884 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001885 * @mtd: mtd info structure
1886 * @chip: nand chip info structure
1887 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001888 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001889 *
1890 * We need a special oob layout and handling even when ECC isn't checked.
1891 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001892static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001893 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001894 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001895{
1896 int eccsize = chip->ecc.size;
1897 int eccbytes = chip->ecc.bytes;
1898 uint8_t *oob = chip->oob_poi;
1899 int steps, size;
1900
1901 for (steps = chip->ecc.steps; steps > 0; steps--) {
1902 chip->write_buf(mtd, buf, eccsize);
1903 buf += eccsize;
1904
1905 if (chip->ecc.prepad) {
1906 chip->write_buf(mtd, oob, chip->ecc.prepad);
1907 oob += chip->ecc.prepad;
1908 }
1909
1910 chip->read_buf(mtd, oob, eccbytes);
1911 oob += eccbytes;
1912
1913 if (chip->ecc.postpad) {
1914 chip->write_buf(mtd, oob, chip->ecc.postpad);
1915 oob += chip->ecc.postpad;
1916 }
1917 }
1918
1919 size = mtd->oobsize - (oob - chip->oob_poi);
1920 if (size)
1921 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001922
1923 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001924}
1925/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001926 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001927 * @mtd: mtd info structure
1928 * @chip: nand chip info structure
1929 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001930 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001931 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001932static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001933 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001934{
1935 int i, eccsize = chip->ecc.size;
1936 int eccbytes = chip->ecc.bytes;
1937 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001938 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001939 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001940 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001941
Brian Norris7854d3f2011-06-23 14:12:08 -07001942 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001943 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1944 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001945
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001946 for (i = 0; i < chip->ecc.total; i++)
1947 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001948
Josh Wufdbad98d2012-06-25 18:07:45 +08001949 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001950}
1951
1952/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001953 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001954 * @mtd: mtd info structure
1955 * @chip: nand chip info structure
1956 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001957 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001958 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001959static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001960 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001961{
1962 int i, eccsize = chip->ecc.size;
1963 int eccbytes = chip->ecc.bytes;
1964 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001965 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001966 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001967 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001968
1969 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1970 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001971 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001972 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1973 }
1974
1975 for (i = 0; i < chip->ecc.total; i++)
1976 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1977
1978 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001979
1980 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001981}
1982
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301983
1984/**
1985 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
1986 * @mtd: mtd info structure
1987 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07001988 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301989 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07001990 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301991 * @oob_required: must write chip->oob_poi to OOB
1992 */
1993static int nand_write_subpage_hwecc(struct mtd_info *mtd,
1994 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07001995 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301996 int oob_required)
1997{
1998 uint8_t *oob_buf = chip->oob_poi;
1999 uint8_t *ecc_calc = chip->buffers->ecccalc;
2000 int ecc_size = chip->ecc.size;
2001 int ecc_bytes = chip->ecc.bytes;
2002 int ecc_steps = chip->ecc.steps;
2003 uint32_t *eccpos = chip->ecc.layout->eccpos;
2004 uint32_t start_step = offset / ecc_size;
2005 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2006 int oob_bytes = mtd->oobsize / ecc_steps;
2007 int step, i;
2008
2009 for (step = 0; step < ecc_steps; step++) {
2010 /* configure controller for WRITE access */
2011 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2012
2013 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002014 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302015
2016 /* mask ECC of un-touched subpages by padding 0xFF */
2017 if ((step < start_step) || (step > end_step))
2018 memset(ecc_calc, 0xff, ecc_bytes);
2019 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002020 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302021
2022 /* mask OOB of un-touched subpages by padding 0xFF */
2023 /* if oob_required, preserve OOB metadata of written subpage */
2024 if (!oob_required || (step < start_step) || (step > end_step))
2025 memset(oob_buf, 0xff, oob_bytes);
2026
Brian Norrisd6a950802013-08-08 17:16:36 -07002027 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302028 ecc_calc += ecc_bytes;
2029 oob_buf += oob_bytes;
2030 }
2031
2032 /* copy calculated ECC for whole page to chip->buffer->oob */
2033 /* this include masked-value(0xFF) for unwritten subpages */
2034 ecc_calc = chip->buffers->ecccalc;
2035 for (i = 0; i < chip->ecc.total; i++)
2036 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2037
2038 /* write OOB buffer to NAND device */
2039 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2040
2041 return 0;
2042}
2043
2044
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002045/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002046 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002047 * @mtd: mtd info structure
2048 * @chip: nand chip info structure
2049 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002050 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002051 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002052 * The hw generator calculates the error syndrome automatically. Therefore we
2053 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002054 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002055static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002056 struct nand_chip *chip,
2057 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002058{
2059 int i, eccsize = chip->ecc.size;
2060 int eccbytes = chip->ecc.bytes;
2061 int eccsteps = chip->ecc.steps;
2062 const uint8_t *p = buf;
2063 uint8_t *oob = chip->oob_poi;
2064
2065 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2066
2067 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2068 chip->write_buf(mtd, p, eccsize);
2069
2070 if (chip->ecc.prepad) {
2071 chip->write_buf(mtd, oob, chip->ecc.prepad);
2072 oob += chip->ecc.prepad;
2073 }
2074
2075 chip->ecc.calculate(mtd, p, oob);
2076 chip->write_buf(mtd, oob, eccbytes);
2077 oob += eccbytes;
2078
2079 if (chip->ecc.postpad) {
2080 chip->write_buf(mtd, oob, chip->ecc.postpad);
2081 oob += chip->ecc.postpad;
2082 }
2083 }
2084
2085 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002086 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002087 if (i)
2088 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002089
2090 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002091}
2092
2093/**
David Woodhouse956e9442006-09-25 17:12:39 +01002094 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002095 * @mtd: MTD device structure
2096 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302097 * @offset: address offset within the page
2098 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002099 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002100 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002101 * @page: page number to write
2102 * @cached: cached programming
2103 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002104 */
2105static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302106 uint32_t offset, int data_len, const uint8_t *buf,
2107 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002108{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302109 int status, subpage;
2110
2111 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2112 chip->ecc.write_subpage)
2113 subpage = offset || (data_len < mtd->writesize);
2114 else
2115 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002116
2117 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2118
David Woodhouse956e9442006-09-25 17:12:39 +01002119 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302120 status = chip->ecc.write_page_raw(mtd, chip, buf,
2121 oob_required);
2122 else if (subpage)
2123 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2124 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002125 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002126 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2127
2128 if (status < 0)
2129 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002130
2131 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002132 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002133 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002134 */
2135 cached = 0;
2136
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002137 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002138
2139 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002140 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002141 /*
2142 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002143 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002144 */
2145 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2146 status = chip->errstat(mtd, chip, FL_WRITING, status,
2147 page);
2148
2149 if (status & NAND_STATUS_FAIL)
2150 return -EIO;
2151 } else {
2152 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002153 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002154 }
2155
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002156 return 0;
2157}
2158
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002159/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002160 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002161 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002162 * @oob: oob data buffer
2163 * @len: oob data write length
2164 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002166static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2167 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002168{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002169 struct nand_chip *chip = mtd->priv;
2170
2171 /*
2172 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2173 * data from a previous OOB read.
2174 */
2175 memset(chip->oob_poi, 0xff, mtd->oobsize);
2176
Florian Fainellif8ac0412010-09-07 13:23:43 +02002177 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002178
Brian Norris0612b9d2011-08-30 18:45:40 -07002179 case MTD_OPS_PLACE_OOB:
2180 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002181 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2182 return oob + len;
2183
Brian Norris0612b9d2011-08-30 18:45:40 -07002184 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002185 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002186 uint32_t boffs = 0, woffs = ops->ooboffs;
2187 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002188
Florian Fainellif8ac0412010-09-07 13:23:43 +02002189 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002190 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002191 if (unlikely(woffs)) {
2192 if (woffs >= free->length) {
2193 woffs -= free->length;
2194 continue;
2195 }
2196 boffs = free->offset + woffs;
2197 bytes = min_t(size_t, len,
2198 (free->length - woffs));
2199 woffs = 0;
2200 } else {
2201 bytes = min_t(size_t, len, free->length);
2202 boffs = free->offset;
2203 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002204 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002205 oob += bytes;
2206 }
2207 return oob;
2208 }
2209 default:
2210 BUG();
2211 }
2212 return NULL;
2213}
2214
Florian Fainellif8ac0412010-09-07 13:23:43 +02002215#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002216
2217/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002218 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002219 * @mtd: MTD device structure
2220 * @to: offset to write to
2221 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002222 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002223 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002224 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002225static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2226 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002227{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002228 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002229 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002230 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002231
2232 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002233 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002234 mtd->oobavail : mtd->oobsize;
2235
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002236 uint8_t *oob = ops->oobbuf;
2237 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302238 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002239 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002240
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002241 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002242 if (!writelen)
2243 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002244
Brian Norris8b6e50c2011-05-25 14:59:01 -07002245 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002246 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002247 pr_notice("%s: attempt to write non page aligned data\n",
2248 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249 return -EINVAL;
2250 }
2251
Thomas Gleixner29072b92006-09-28 15:38:36 +02002252 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002253
Thomas Gleixner6a930962006-06-28 00:11:45 +02002254 chipnr = (int)(to >> chip->chip_shift);
2255 chip->select_chip(mtd, chipnr);
2256
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002257 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002258 if (nand_check_wp(mtd)) {
2259 ret = -EIO;
2260 goto err_out;
2261 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002262
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002263 realpage = (int)(to >> chip->page_shift);
2264 page = realpage & chip->pagemask;
2265 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2266
2267 /* Invalidate the page cache, when we write to the cached page */
2268 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002269 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002270 chip->pagebuf = -1;
2271
Maxim Levitsky782ce792010-02-22 20:39:36 +02002272 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002273 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2274 ret = -EINVAL;
2275 goto err_out;
2276 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002277
Florian Fainellif8ac0412010-09-07 13:23:43 +02002278 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002279 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002280 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002281 uint8_t *wbuf = buf;
2282
Brian Norris8b6e50c2011-05-25 14:59:01 -07002283 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002284 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2285 cached = 0;
2286 bytes = min_t(int, bytes - column, (int) writelen);
2287 chip->pagebuf = -1;
2288 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2289 memcpy(&chip->buffers->databuf[column], buf, bytes);
2290 wbuf = chip->buffers->databuf;
2291 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002292
Maxim Levitsky782ce792010-02-22 20:39:36 +02002293 if (unlikely(oob)) {
2294 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002295 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002296 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002297 } else {
2298 /* We still need to erase leftover OOB data */
2299 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002300 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302301 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2302 oob_required, page, cached,
2303 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002304 if (ret)
2305 break;
2306
2307 writelen -= bytes;
2308 if (!writelen)
2309 break;
2310
Thomas Gleixner29072b92006-09-28 15:38:36 +02002311 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002312 buf += bytes;
2313 realpage++;
2314
2315 page = realpage & chip->pagemask;
2316 /* Check, if we cross a chip boundary */
2317 if (!page) {
2318 chipnr++;
2319 chip->select_chip(mtd, -1);
2320 chip->select_chip(mtd, chipnr);
2321 }
2322 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002323
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002324 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002325 if (unlikely(oob))
2326 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002327
2328err_out:
2329 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002330 return ret;
2331}
2332
2333/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002334 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002335 * @mtd: MTD device structure
2336 * @to: offset to write to
2337 * @len: number of bytes to write
2338 * @retlen: pointer to variable to store the number of written bytes
2339 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002340 *
2341 * NAND write with ECC. Used when performing writes in interrupt context, this
2342 * may for example be called by mtdoops when writing an oops while in panic.
2343 */
2344static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2345 size_t *retlen, const uint8_t *buf)
2346{
2347 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002348 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002349 int ret;
2350
Brian Norris8b6e50c2011-05-25 14:59:01 -07002351 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002352 panic_nand_wait(mtd, chip, 400);
2353
Brian Norris8b6e50c2011-05-25 14:59:01 -07002354 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002355 panic_nand_get_device(chip, mtd, FL_WRITING);
2356
Brian Norris4a89ff82011-08-30 18:45:45 -07002357 ops.len = len;
2358 ops.datbuf = (uint8_t *)buf;
2359 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002360 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002361
Brian Norris4a89ff82011-08-30 18:45:45 -07002362 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002363
Brian Norris4a89ff82011-08-30 18:45:45 -07002364 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002365 return ret;
2366}
2367
2368/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002369 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002370 * @mtd: MTD device structure
2371 * @to: offset to write to
2372 * @len: number of bytes to write
2373 * @retlen: pointer to variable to store the number of written bytes
2374 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002376 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002378static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002379 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Brian Norris4a89ff82011-08-30 18:45:45 -07002381 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002382 int ret;
2383
Huang Shijie6a8214a2012-11-19 14:43:30 +08002384 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002385 ops.len = len;
2386 ops.datbuf = (uint8_t *)buf;
2387 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002388 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002389 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002390 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002391 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002392 return ret;
2393}
2394
2395/**
2396 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002397 * @mtd: MTD device structure
2398 * @to: offset to write to
2399 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002400 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002401 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002402 */
2403static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2404 struct mtd_oob_ops *ops)
2405{
Adrian Hunter03736152007-01-31 17:58:29 +02002406 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002407 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Brian Norris289c0522011-07-19 10:06:09 -07002409 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302410 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Brian Norris0612b9d2011-08-30 18:45:40 -07002412 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002413 len = chip->ecc.layout->oobavail;
2414 else
2415 len = mtd->oobsize;
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002418 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002419 pr_debug("%s: attempt to write past end of page\n",
2420 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return -EINVAL;
2422 }
2423
Adrian Hunter03736152007-01-31 17:58:29 +02002424 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002425 pr_debug("%s: attempt to start write outside oob\n",
2426 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002427 return -EINVAL;
2428 }
2429
Jason Liu775adc32011-02-25 13:06:18 +08002430 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002431 if (unlikely(to >= mtd->size ||
2432 ops->ooboffs + ops->ooblen >
2433 ((mtd->size >> chip->page_shift) -
2434 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002435 pr_debug("%s: attempt to write beyond end of device\n",
2436 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002437 return -EINVAL;
2438 }
2439
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002440 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002441 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002443 /* Shift to get page */
2444 page = (int)(to >> chip->page_shift);
2445
2446 /*
2447 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2448 * of my DiskOnChip 2000 test units) will clear the whole data page too
2449 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2450 * it in the doc2000 driver in August 1999. dwmw2.
2451 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002452 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002455 if (nand_check_wp(mtd)) {
2456 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002457 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002458 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002461 if (page == chip->pagebuf)
2462 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002464 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002465
Brian Norris0612b9d2011-08-30 18:45:40 -07002466 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002467 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2468 else
2469 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002470
Huang Shijieb0bb6902012-11-19 14:43:29 +08002471 chip->select_chip(mtd, -1);
2472
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002473 if (status)
2474 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Vitaly Wool70145682006-11-03 18:20:38 +03002476 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002478 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002481/**
2482 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002483 * @mtd: MTD device structure
2484 * @to: offset to write to
2485 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002486 */
2487static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2488 struct mtd_oob_ops *ops)
2489{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002490 int ret = -ENOTSUPP;
2491
2492 ops->retlen = 0;
2493
2494 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002495 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002496 pr_debug("%s: attempt to write beyond end of device\n",
2497 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002498 return -EINVAL;
2499 }
2500
Huang Shijie6a8214a2012-11-19 14:43:30 +08002501 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002502
Florian Fainellif8ac0412010-09-07 13:23:43 +02002503 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002504 case MTD_OPS_PLACE_OOB:
2505 case MTD_OPS_AUTO_OOB:
2506 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002507 break;
2508
2509 default:
2510 goto out;
2511 }
2512
2513 if (!ops->datbuf)
2514 ret = nand_do_write_oob(mtd, to, ops);
2515 else
2516 ret = nand_do_write_ops(mtd, to, ops);
2517
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002518out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002519 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return ret;
2521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002524 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002525 * @mtd: MTD device structure
2526 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002528 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002530static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002532 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002534 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2535 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002540 * @mtd: MTD device structure
2541 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002543 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002545static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
David Woodhousee0c7d762006-05-13 18:07:53 +01002547 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002551 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002552 * @mtd: MTD device structure
2553 * @instr: erase instruction
2554 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002556 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002558int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2559 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Adrian Hunter69423d92008-12-10 13:37:21 +00002561 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002562 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002563 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Brian Norris289c0522011-07-19 10:06:09 -07002565 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2566 __func__, (unsigned long long)instr->addr,
2567 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302569 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002573 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002576 page = (int)(instr->addr >> chip->page_shift);
2577 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002583 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /* Check, if it is write protected */
2586 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002587 pr_debug("%s: device is write protected!\n",
2588 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 instr->state = MTD_ERASE_FAILED;
2590 goto erase_exit;
2591 }
2592
2593 /* Loop through the pages */
2594 len = instr->len;
2595
2596 instr->state = MTD_ERASING;
2597
2598 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002599 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002600 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2601 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002602 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2603 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 instr->state = MTD_ERASE_FAILED;
2605 goto erase_exit;
2606 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002607
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002608 /*
2609 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002610 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002611 */
2612 if (page <= chip->pagebuf && chip->pagebuf <
2613 (page + pages_per_block))
2614 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002616 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002617
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002618 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002620 /*
2621 * See if operation failed and additional status checks are
2622 * available
2623 */
2624 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2625 status = chip->errstat(mtd, chip, FL_ERASING,
2626 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002629 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002630 pr_debug("%s: failed erase, page 0x%08x\n",
2631 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002633 instr->fail_addr =
2634 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 goto erase_exit;
2636 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002639 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 page += pages_per_block;
2641
2642 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002643 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002645 chip->select_chip(mtd, -1);
2646 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648 }
2649 instr->state = MTD_ERASE_DONE;
2650
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002651erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002656 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 nand_release_device(mtd);
2658
David Woodhouse49defc02007-10-06 15:01:59 -04002659 /* Do call back function */
2660 if (!ret)
2661 mtd_erase_callback(instr);
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /* Return more or less happy */
2664 return ret;
2665}
2666
2667/**
2668 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002669 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002671 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002673static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Brian Norris289c0522011-07-19 10:06:09 -07002675 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002678 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002680 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002684 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002685 * @mtd: MTD device structure
2686 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002688static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002690 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
2693/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002694 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002695 * @mtd: MTD device structure
2696 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002698static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 int ret;
2701
Florian Fainellif8ac0412010-09-07 13:23:43 +02002702 ret = nand_block_isbad(mtd, ofs);
2703 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002704 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 if (ret > 0)
2706 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002707 return ret;
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Brian Norris5a0edb22013-07-30 17:52:58 -07002710 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
2713/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002714 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2715 * @mtd: MTD device structure
2716 * @chip: nand chip info structure
2717 * @addr: feature address.
2718 * @subfeature_param: the subfeature parameters, a four bytes array.
2719 */
2720static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2721 int addr, uint8_t *subfeature_param)
2722{
2723 int status;
2724
David Mosbergerd914c932013-05-29 15:30:13 +03002725 if (!chip->onfi_version ||
2726 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2727 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002728 return -EINVAL;
2729
2730 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2731 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2732 status = chip->waitfunc(mtd, chip);
2733 if (status & NAND_STATUS_FAIL)
2734 return -EIO;
2735 return 0;
2736}
2737
2738/**
2739 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2740 * @mtd: MTD device structure
2741 * @chip: nand chip info structure
2742 * @addr: feature address.
2743 * @subfeature_param: the subfeature parameters, a four bytes array.
2744 */
2745static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2746 int addr, uint8_t *subfeature_param)
2747{
David Mosbergerd914c932013-05-29 15:30:13 +03002748 if (!chip->onfi_version ||
2749 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2750 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002751 return -EINVAL;
2752
2753 /* clear the sub feature parameters */
2754 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2755
2756 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2757 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2758 return 0;
2759}
2760
2761/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002762 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002763 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002764 */
2765static int nand_suspend(struct mtd_info *mtd)
2766{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002767 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002768}
2769
2770/**
2771 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002772 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002773 */
2774static void nand_resume(struct mtd_info *mtd)
2775{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002776 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002777
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002778 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002779 nand_release_device(mtd);
2780 else
Brian Norrisd0370212011-07-19 10:06:08 -07002781 pr_err("%s called for a chip which is not in suspended state\n",
2782 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002783}
2784
Brian Norris8b6e50c2011-05-25 14:59:01 -07002785/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002786static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002789 if (!chip->chip_delay)
2790 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002793 if (chip->cmdfunc == NULL)
2794 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002797 if (chip->waitfunc == NULL)
2798 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002800 if (!chip->select_chip)
2801 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002802
Huang Shijie4204ccc2013-08-16 10:10:07 +08002803 /* set for ONFI nand */
2804 if (!chip->onfi_set_features)
2805 chip->onfi_set_features = nand_onfi_set_features;
2806 if (!chip->onfi_get_features)
2807 chip->onfi_get_features = nand_onfi_get_features;
2808
Brian Norris68e80782013-07-18 01:17:02 -07002809 /* If called twice, pointers that depend on busw may need to be reset */
2810 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002811 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2812 if (!chip->read_word)
2813 chip->read_word = nand_read_word;
2814 if (!chip->block_bad)
2815 chip->block_bad = nand_block_bad;
2816 if (!chip->block_markbad)
2817 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002818 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002819 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Brian Norris68e80782013-07-18 01:17:02 -07002820 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002821 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002822 if (!chip->scan_bbt)
2823 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002824
2825 if (!chip->controller) {
2826 chip->controller = &chip->hwcontrol;
2827 spin_lock_init(&chip->controller->lock);
2828 init_waitqueue_head(&chip->controller->wq);
2829 }
2830
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002831}
2832
Brian Norris8b6e50c2011-05-25 14:59:01 -07002833/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002834static void sanitize_string(uint8_t *s, size_t len)
2835{
2836 ssize_t i;
2837
Brian Norris8b6e50c2011-05-25 14:59:01 -07002838 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002839 s[len - 1] = 0;
2840
Brian Norris8b6e50c2011-05-25 14:59:01 -07002841 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002842 for (i = 0; i < len - 1; i++) {
2843 if (s[i] < ' ' || s[i] > 127)
2844 s[i] = '?';
2845 }
2846
Brian Norris8b6e50c2011-05-25 14:59:01 -07002847 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002848 strim(s);
2849}
2850
2851static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2852{
2853 int i;
2854 while (len--) {
2855 crc ^= *p++ << 8;
2856 for (i = 0; i < 8; i++)
2857 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2858 }
2859
2860 return crc;
2861}
2862
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002863/* Parse the Extended Parameter Page. */
2864static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2865 struct nand_chip *chip, struct nand_onfi_params *p)
2866{
2867 struct onfi_ext_param_page *ep;
2868 struct onfi_ext_section *s;
2869 struct onfi_ext_ecc_info *ecc;
2870 uint8_t *cursor;
2871 int ret = -EINVAL;
2872 int len;
2873 int i;
2874
2875 len = le16_to_cpu(p->ext_param_page_length) * 16;
2876 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07002877 if (!ep)
2878 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002879
2880 /* Send our own NAND_CMD_PARAM. */
2881 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2882
2883 /* Use the Change Read Column command to skip the ONFI param pages. */
2884 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
2885 sizeof(*p) * p->num_of_param_pages , -1);
2886
2887 /* Read out the Extended Parameter Page. */
2888 chip->read_buf(mtd, (uint8_t *)ep, len);
2889 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
2890 != le16_to_cpu(ep->crc))) {
2891 pr_debug("fail in the CRC.\n");
2892 goto ext_out;
2893 }
2894
2895 /*
2896 * Check the signature.
2897 * Do not strictly follow the ONFI spec, maybe changed in future.
2898 */
2899 if (strncmp(ep->sig, "EPPS", 4)) {
2900 pr_debug("The signature is invalid.\n");
2901 goto ext_out;
2902 }
2903
2904 /* find the ECC section. */
2905 cursor = (uint8_t *)(ep + 1);
2906 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
2907 s = ep->sections + i;
2908 if (s->type == ONFI_SECTION_TYPE_2)
2909 break;
2910 cursor += s->length * 16;
2911 }
2912 if (i == ONFI_EXT_SECTION_MAX) {
2913 pr_debug("We can not find the ECC section.\n");
2914 goto ext_out;
2915 }
2916
2917 /* get the info we want. */
2918 ecc = (struct onfi_ext_ecc_info *)cursor;
2919
Brian Norris4ae7d222013-09-16 18:20:21 -07002920 if (!ecc->codeword_size) {
2921 pr_debug("Invalid codeword size\n");
2922 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002923 }
2924
Brian Norris4ae7d222013-09-16 18:20:21 -07002925 chip->ecc_strength_ds = ecc->ecc_bits;
2926 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07002927 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002928
2929ext_out:
2930 kfree(ep);
2931 return ret;
2932}
2933
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002934/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002935 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002936 */
2937static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002938 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002939{
2940 struct nand_onfi_params *p = &chip->onfi_params;
2941 int i;
2942 int val;
2943
Brian Norris7854d3f2011-06-23 14:12:08 -07002944 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002945 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2946 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2947 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2948 return 0;
2949
Brian Norrisc7f23a72013-08-13 10:51:55 -07002950 /*
2951 * ONFI must be probed in 8-bit mode or with NAND_BUSWIDTH_AUTO, not
2952 * with NAND_BUSWIDTH_16
2953 */
2954 if (chip->options & NAND_BUSWIDTH_16) {
2955 pr_err("ONFI cannot be probed in 16-bit mode; aborting\n");
2956 return 0;
2957 }
2958
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002959 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2960 for (i = 0; i < 3; i++) {
2961 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2962 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2963 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002964 break;
2965 }
2966 }
2967
Brian Norrisc7f23a72013-08-13 10:51:55 -07002968 if (i == 3) {
2969 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002970 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07002971 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002972
Brian Norris8b6e50c2011-05-25 14:59:01 -07002973 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002974 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002975 if (val & (1 << 5))
2976 chip->onfi_version = 23;
2977 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002978 chip->onfi_version = 22;
2979 else if (val & (1 << 3))
2980 chip->onfi_version = 21;
2981 else if (val & (1 << 2))
2982 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002983 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002984 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002985
2986 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03002987 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002988 return 0;
2989 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002990
2991 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2992 sanitize_string(p->model, sizeof(p->model));
2993 if (!mtd->name)
2994 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07002995
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002996 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07002997
2998 /*
2999 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3000 * (don't ask me who thought of this...). MTD assumes that these
3001 * dimensions will be power-of-2, so just truncate the remaining area.
3002 */
3003 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3004 mtd->erasesize *= mtd->writesize;
3005
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003006 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003007
3008 /* See erasesize comment */
3009 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003010 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003011 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003012
3013 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003014 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003015 else
3016 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003017
Huang Shijie10c86ba2013-05-17 11:17:26 +08003018 if (p->ecc_bits != 0xff) {
3019 chip->ecc_strength_ds = p->ecc_bits;
3020 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003021 } else if (chip->onfi_version >= 21 &&
3022 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3023
3024 /*
3025 * The nand_flash_detect_ext_param_page() uses the
3026 * Change Read Column command which maybe not supported
3027 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3028 * now. We do not replace user supplied command function.
3029 */
3030 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3031 chip->cmdfunc = nand_command_lp;
3032
3033 /* The Extended Parameter Page is supported since ONFI 2.1. */
3034 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003035 pr_warn("Failed to detect ONFI extended param page\n");
3036 } else {
3037 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003038 }
3039
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003040 return 1;
3041}
3042
3043/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003044 * nand_id_has_period - Check if an ID string has a given wraparound period
3045 * @id_data: the ID string
3046 * @arrlen: the length of the @id_data array
3047 * @period: the period of repitition
3048 *
3049 * Check if an ID string is repeated within a given sequence of bytes at
3050 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003051 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003052 * if the repetition has a period of @period; otherwise, returns zero.
3053 */
3054static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3055{
3056 int i, j;
3057 for (i = 0; i < period; i++)
3058 for (j = i + period; j < arrlen; j += period)
3059 if (id_data[i] != id_data[j])
3060 return 0;
3061 return 1;
3062}
3063
3064/*
3065 * nand_id_len - Get the length of an ID string returned by CMD_READID
3066 * @id_data: the ID string
3067 * @arrlen: the length of the @id_data array
3068
3069 * Returns the length of the ID string, according to known wraparound/trailing
3070 * zero patterns. If no pattern exists, returns the length of the array.
3071 */
3072static int nand_id_len(u8 *id_data, int arrlen)
3073{
3074 int last_nonzero, period;
3075
3076 /* Find last non-zero byte */
3077 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3078 if (id_data[last_nonzero])
3079 break;
3080
3081 /* All zeros */
3082 if (last_nonzero < 0)
3083 return 0;
3084
3085 /* Calculate wraparound period */
3086 for (period = 1; period < arrlen; period++)
3087 if (nand_id_has_period(id_data, arrlen, period))
3088 break;
3089
3090 /* There's a repeated pattern */
3091 if (period < arrlen)
3092 return period;
3093
3094 /* There are trailing zeros */
3095 if (last_nonzero < arrlen - 1)
3096 return last_nonzero + 1;
3097
3098 /* No pattern detected */
3099 return arrlen;
3100}
3101
Huang Shijie7db906b2013-09-25 14:58:11 +08003102/* Extract the bits of per cell from the 3rd byte of the extended ID */
3103static int nand_get_bits_per_cell(u8 cellinfo)
3104{
3105 int bits;
3106
3107 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3108 bits >>= NAND_CI_CELLTYPE_SHIFT;
3109 return bits + 1;
3110}
3111
Brian Norrise3b88bd2012-09-24 20:40:52 -07003112/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003113 * Many new NAND share similar device ID codes, which represent the size of the
3114 * chip. The rest of the parameters must be decoded according to generic or
3115 * manufacturer-specific "extended ID" decoding patterns.
3116 */
3117static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3118 u8 id_data[8], int *busw)
3119{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003120 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003121 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003122 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003123 /* The 4th id byte is the important one */
3124 extid = id_data[3];
3125
Brian Norrise3b88bd2012-09-24 20:40:52 -07003126 id_len = nand_id_len(id_data, 8);
3127
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003128 /*
3129 * Field definitions are in the following datasheets:
3130 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003131 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003132 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003133 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003134 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3135 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003136 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003137 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003138 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003139 /* Calc pagesize */
3140 mtd->writesize = 2048 << (extid & 0x03);
3141 extid >>= 2;
3142 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003143 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003144 case 1:
3145 mtd->oobsize = 128;
3146 break;
3147 case 2:
3148 mtd->oobsize = 218;
3149 break;
3150 case 3:
3151 mtd->oobsize = 400;
3152 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003153 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003154 mtd->oobsize = 436;
3155 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003156 case 5:
3157 mtd->oobsize = 512;
3158 break;
3159 case 6:
3160 default: /* Other cases are "reserved" (unknown) */
3161 mtd->oobsize = 640;
3162 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003163 }
3164 extid >>= 2;
3165 /* Calc blocksize */
3166 mtd->erasesize = (128 * 1024) <<
3167 (((extid >> 1) & 0x04) | (extid & 0x03));
3168 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003169 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003170 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003171 unsigned int tmp;
3172
3173 /* Calc pagesize */
3174 mtd->writesize = 2048 << (extid & 0x03);
3175 extid >>= 2;
3176 /* Calc oobsize */
3177 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3178 case 0:
3179 mtd->oobsize = 128;
3180 break;
3181 case 1:
3182 mtd->oobsize = 224;
3183 break;
3184 case 2:
3185 mtd->oobsize = 448;
3186 break;
3187 case 3:
3188 mtd->oobsize = 64;
3189 break;
3190 case 4:
3191 mtd->oobsize = 32;
3192 break;
3193 case 5:
3194 mtd->oobsize = 16;
3195 break;
3196 default:
3197 mtd->oobsize = 640;
3198 break;
3199 }
3200 extid >>= 2;
3201 /* Calc blocksize */
3202 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3203 if (tmp < 0x03)
3204 mtd->erasesize = (128 * 1024) << tmp;
3205 else if (tmp == 0x03)
3206 mtd->erasesize = 768 * 1024;
3207 else
3208 mtd->erasesize = (64 * 1024) << tmp;
3209 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003210 } else {
3211 /* Calc pagesize */
3212 mtd->writesize = 1024 << (extid & 0x03);
3213 extid >>= 2;
3214 /* Calc oobsize */
3215 mtd->oobsize = (8 << (extid & 0x01)) *
3216 (mtd->writesize >> 9);
3217 extid >>= 2;
3218 /* Calc blocksize. Blocksize is multiples of 64KiB */
3219 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3220 extid >>= 2;
3221 /* Get buswidth information */
3222 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003223
3224 /*
3225 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3226 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3227 * follows:
3228 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3229 * 110b -> 24nm
3230 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3231 */
3232 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003233 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003234 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3235 !(id_data[4] & 0x80) /* !BENAND */) {
3236 mtd->oobsize = 32 * mtd->writesize >> 9;
3237 }
3238
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003239 }
3240}
3241
3242/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003243 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3244 * decodes a matching ID table entry and assigns the MTD size parameters for
3245 * the chip.
3246 */
3247static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3248 struct nand_flash_dev *type, u8 id_data[8],
3249 int *busw)
3250{
3251 int maf_id = id_data[0];
3252
3253 mtd->erasesize = type->erasesize;
3254 mtd->writesize = type->pagesize;
3255 mtd->oobsize = mtd->writesize / 32;
3256 *busw = type->options & NAND_BUSWIDTH_16;
3257
Huang Shijie1c195e92013-09-25 14:58:12 +08003258 /* All legacy ID NAND are small-page, SLC */
3259 chip->bits_per_cell = 1;
3260
Brian Norrisf23a4812012-09-24 20:40:51 -07003261 /*
3262 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3263 * some Spansion chips have erasesize that conflicts with size
3264 * listed in nand_ids table.
3265 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3266 */
3267 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3268 && id_data[6] == 0x00 && id_data[7] == 0x00
3269 && mtd->writesize == 512) {
3270 mtd->erasesize = 128 * 1024;
3271 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3272 }
3273}
3274
3275/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003276 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3277 * heuristic patterns using various detected parameters (e.g., manufacturer,
3278 * page size, cell-type information).
3279 */
3280static void nand_decode_bbm_options(struct mtd_info *mtd,
3281 struct nand_chip *chip, u8 id_data[8])
3282{
3283 int maf_id = id_data[0];
3284
3285 /* Set the bad block position */
3286 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3287 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3288 else
3289 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3290
3291 /*
3292 * Bad block marker is stored in the last page of each block on Samsung
3293 * and Hynix MLC devices; stored in first two pages of each block on
3294 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3295 * AMD/Spansion, and Macronix. All others scan only the first page.
3296 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003297 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003298 (maf_id == NAND_MFR_SAMSUNG ||
3299 maf_id == NAND_MFR_HYNIX))
3300 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003301 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003302 (maf_id == NAND_MFR_SAMSUNG ||
3303 maf_id == NAND_MFR_HYNIX ||
3304 maf_id == NAND_MFR_TOSHIBA ||
3305 maf_id == NAND_MFR_AMD ||
3306 maf_id == NAND_MFR_MACRONIX)) ||
3307 (mtd->writesize == 2048 &&
3308 maf_id == NAND_MFR_MICRON))
3309 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3310}
3311
Huang Shijieec6e87e2013-03-15 11:01:00 +08003312static inline bool is_full_id_nand(struct nand_flash_dev *type)
3313{
3314 return type->id_len;
3315}
3316
3317static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3318 struct nand_flash_dev *type, u8 *id_data, int *busw)
3319{
3320 if (!strncmp(type->id, id_data, type->id_len)) {
3321 mtd->writesize = type->pagesize;
3322 mtd->erasesize = type->erasesize;
3323 mtd->oobsize = type->oobsize;
3324
Huang Shijie7db906b2013-09-25 14:58:11 +08003325 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003326 chip->chipsize = (uint64_t)type->chipsize << 20;
3327 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003328 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3329 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003330
3331 *busw = type->options & NAND_BUSWIDTH_16;
3332
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003333 if (!mtd->name)
3334 mtd->name = type->name;
3335
Huang Shijieec6e87e2013-03-15 11:01:00 +08003336 return true;
3337 }
3338 return false;
3339}
3340
Brian Norris7e74c2d2012-09-24 20:40:49 -07003341/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003342 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003343 */
3344static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003345 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003346 int busw,
3347 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003348 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003349{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003350 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003351 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
3353 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003354 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Karl Beldanef89a882008-09-15 14:37:29 +02003356 /*
3357 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003358 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003359 */
3360 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003363 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
3365 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003366 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003367 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Brian Norris8b6e50c2011-05-25 14:59:01 -07003369 /*
3370 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003371 * interface concerns can cause random data which looks like a
3372 * possibly credible NAND flash to appear. If the two results do
3373 * not match, ignore the device completely.
3374 */
3375
3376 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3377
Brian Norris4aef9b72012-09-24 20:40:48 -07003378 /* Read entire ID string */
3379 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003380 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003381
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003382 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003383 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003384 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003385 return ERR_PTR(-ENODEV);
3386 }
3387
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003388 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003389 type = nand_flash_ids;
3390
Huang Shijieec6e87e2013-03-15 11:01:00 +08003391 for (; type->name != NULL; type++) {
3392 if (is_full_id_nand(type)) {
3393 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3394 goto ident_done;
3395 } else if (*dev_id == type->dev_id) {
3396 break;
3397 }
3398 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003399
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003400 chip->onfi_version = 0;
3401 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003402 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003403 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003404 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003405 }
3406
David Woodhouse5e81e882010-02-26 18:32:56 +00003407 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003408 return ERR_PTR(-ENODEV);
3409
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003410 if (!mtd->name)
3411 mtd->name = type->name;
3412
Adrian Hunter69423d92008-12-10 13:37:21 +00003413 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003414
Huang Shijie12a40a52010-09-27 10:43:53 +08003415 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003416 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003417 busw = chip->init_size(mtd, chip, id_data);
3418 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003419 /* Decode parameters from extended ID */
3420 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003421 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003422 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003423 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003424 /* Get chip options */
3425 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003426
Brian Norris8b6e50c2011-05-25 14:59:01 -07003427 /*
3428 * Check if chip is not a Samsung device. Do not clear the
3429 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003430 */
3431 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3432 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3433ident_done:
3434
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003435 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003436 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003437 if (nand_manuf_ids[maf_idx].id == *maf_id)
3438 break;
3439 }
3440
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003441 if (chip->options & NAND_BUSWIDTH_AUTO) {
3442 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3443 chip->options |= busw;
3444 nand_set_defaults(chip, busw);
3445 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3446 /*
3447 * Check, if buswidth is correct. Hardware drivers should set
3448 * chip correct!
3449 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003450 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3451 *maf_id, *dev_id);
3452 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3453 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003454 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3455 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003456 return ERR_PTR(-EINVAL);
3457 }
3458
Brian Norris7e74c2d2012-09-24 20:40:49 -07003459 nand_decode_bbm_options(mtd, chip, id_data);
3460
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003461 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003462 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003463 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003464 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003465
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003466 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003467 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003468 if (chip->chipsize & 0xffffffff)
3469 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003470 else {
3471 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3472 chip->chip_shift += 32 - 1;
3473 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003474
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003475 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003476 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003477
Brian Norris8b6e50c2011-05-25 14:59:01 -07003478 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003479 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3480 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003481
Ezequiel Garcia20171642013-11-25 08:30:31 -03003482 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3483 *maf_id, *dev_id);
3484 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
Huang Shijie3723e932013-09-25 14:58:14 +08003485 chip->onfi_version ? chip->onfi_params.model : type->name);
Ezequiel Garcia20171642013-11-25 08:30:31 -03003486 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003487 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3488 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003489 return type;
3490}
3491
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003492/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003493 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003494 * @mtd: MTD device structure
3495 * @maxchips: number of chips to scan for
3496 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003497 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003498 * This is the first phase of the normal nand_scan() function. It reads the
3499 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003500 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003501 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003502 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003503int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3504 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003505{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003506 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003507 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003508 struct nand_flash_dev *type;
3509
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003510 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003511 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003512 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003513 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003514
3515 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003516 type = nand_get_flash_type(mtd, chip, busw,
3517 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003518
3519 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003520 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003521 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003522 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003523 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 }
3525
Huang Shijie07300162012-11-09 16:23:45 +08003526 chip->select_chip(mtd, -1);
3527
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003528 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003529 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003530 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003531 /* See comment in nand_get_flash_type for reset */
3532 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003534 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003536 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003537 nand_dev_id != chip->read_byte(mtd)) {
3538 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 break;
Huang Shijie07300162012-11-09 16:23:45 +08003540 }
3541 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003544 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003547 chip->numchips = i;
3548 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
David Woodhouse3b85c322006-09-25 17:06:53 +01003550 return 0;
3551}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003552EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003553
3554
3555/**
3556 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003557 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003558 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003559 * This is the second phase of the normal nand_scan() function. It fills out
3560 * all the uninitialized function pointers with the defaults and scans for a
3561 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003562 */
3563int nand_scan_tail(struct mtd_info *mtd)
3564{
3565 int i;
3566 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003567 struct nand_ecc_ctrl *ecc = &chip->ecc;
David Woodhouse3b85c322006-09-25 17:06:53 +01003568
Brian Norrise2414f42012-02-06 13:44:00 -08003569 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3570 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3571 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3572
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003573 if (!(chip->options & NAND_OWN_BUFFERS))
3574 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3575 if (!chip->buffers)
3576 return -ENOMEM;
3577
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003578 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003579 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003580
3581 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003582 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003583 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003584 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003585 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003587 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 break;
3589 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003590 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 break;
3592 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003593 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003595 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003596 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003599 pr_warn("No oob scheme defined for oobsize %d\n",
3600 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 BUG();
3602 }
3603 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003604
David Woodhouse956e9442006-09-25 17:12:39 +01003605 if (!chip->write_page)
3606 chip->write_page = nand_write_page;
3607
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003608 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003609 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003610 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003611 */
David Woodhouse956e9442006-09-25 17:12:39 +01003612
Huang Shijie97de79e02013-10-18 14:20:53 +08003613 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003614 case NAND_ECC_HW_OOB_FIRST:
3615 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003616 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003617 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003618 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003619 BUG();
3620 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003621 if (!ecc->read_page)
3622 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003623
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003624 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003625 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003626 if (!ecc->read_page)
3627 ecc->read_page = nand_read_page_hwecc;
3628 if (!ecc->write_page)
3629 ecc->write_page = nand_write_page_hwecc;
3630 if (!ecc->read_page_raw)
3631 ecc->read_page_raw = nand_read_page_raw;
3632 if (!ecc->write_page_raw)
3633 ecc->write_page_raw = nand_write_page_raw;
3634 if (!ecc->read_oob)
3635 ecc->read_oob = nand_read_oob_std;
3636 if (!ecc->write_oob)
3637 ecc->write_oob = nand_write_oob_std;
3638 if (!ecc->read_subpage)
3639 ecc->read_subpage = nand_read_subpage;
3640 if (!ecc->write_subpage)
3641 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003642
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003643 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003644 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3645 (!ecc->read_page ||
3646 ecc->read_page == nand_read_page_hwecc ||
3647 !ecc->write_page ||
3648 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003649 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003650 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003651 BUG();
3652 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003653 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003654 if (!ecc->read_page)
3655 ecc->read_page = nand_read_page_syndrome;
3656 if (!ecc->write_page)
3657 ecc->write_page = nand_write_page_syndrome;
3658 if (!ecc->read_page_raw)
3659 ecc->read_page_raw = nand_read_page_raw_syndrome;
3660 if (!ecc->write_page_raw)
3661 ecc->write_page_raw = nand_write_page_raw_syndrome;
3662 if (!ecc->read_oob)
3663 ecc->read_oob = nand_read_oob_syndrome;
3664 if (!ecc->write_oob)
3665 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003666
Huang Shijie97de79e02013-10-18 14:20:53 +08003667 if (mtd->writesize >= ecc->size) {
3668 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003669 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3670 BUG();
3671 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003672 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003673 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003674 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003675 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003676 ecc->size, mtd->writesize);
3677 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003679 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003680 ecc->calculate = nand_calculate_ecc;
3681 ecc->correct = nand_correct_data;
3682 ecc->read_page = nand_read_page_swecc;
3683 ecc->read_subpage = nand_read_subpage;
3684 ecc->write_page = nand_write_page_swecc;
3685 ecc->read_page_raw = nand_read_page_raw;
3686 ecc->write_page_raw = nand_write_page_raw;
3687 ecc->read_oob = nand_read_oob_std;
3688 ecc->write_oob = nand_write_oob_std;
3689 if (!ecc->size)
3690 ecc->size = 256;
3691 ecc->bytes = 3;
3692 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003694
Ivan Djelic193bd402011-03-11 11:05:33 +01003695 case NAND_ECC_SOFT_BCH:
3696 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003697 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003698 BUG();
3699 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003700 ecc->calculate = nand_bch_calculate_ecc;
3701 ecc->correct = nand_bch_correct_data;
3702 ecc->read_page = nand_read_page_swecc;
3703 ecc->read_subpage = nand_read_subpage;
3704 ecc->write_page = nand_write_page_swecc;
3705 ecc->read_page_raw = nand_read_page_raw;
3706 ecc->write_page_raw = nand_write_page_raw;
3707 ecc->read_oob = nand_read_oob_std;
3708 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01003709 /*
3710 * Board driver should supply ecc.size and ecc.bytes values to
3711 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003712 * for details. Otherwise, default to 4 bits for large page
3713 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003714 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003715 if (!ecc->size && (mtd->oobsize >= 64)) {
3716 ecc->size = 512;
3717 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01003718 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003719 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3720 &ecc->layout);
3721 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003722 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003723 BUG();
3724 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003725 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003726 break;
3727
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003728 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003729 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003730 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08003731 ecc->read_page = nand_read_page_raw;
3732 ecc->write_page = nand_write_page_raw;
3733 ecc->read_oob = nand_read_oob_std;
3734 ecc->read_page_raw = nand_read_page_raw;
3735 ecc->write_page_raw = nand_write_page_raw;
3736 ecc->write_oob = nand_write_oob_std;
3737 ecc->size = mtd->writesize;
3738 ecc->bytes = 0;
3739 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08003743 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003744 BUG();
3745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Brian Norris9ce244b2011-08-30 18:45:37 -07003747 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08003748 if (!ecc->read_oob_raw)
3749 ecc->read_oob_raw = ecc->read_oob;
3750 if (!ecc->write_oob_raw)
3751 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003752
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003753 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003754 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003755 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003756 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003757 ecc->layout->oobavail = 0;
3758 for (i = 0; ecc->layout->oobfree[i].length
3759 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
3760 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
3761 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003762
3763 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003764 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003765 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003766 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003767 ecc->steps = mtd->writesize / ecc->size;
3768 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003769 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003770 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003772 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003773
Brian Norris8b6e50c2011-05-25 14:59:01 -07003774 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003775 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08003776 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003777 case 2:
3778 mtd->subpage_sft = 1;
3779 break;
3780 case 4:
3781 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003782 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003783 mtd->subpage_sft = 2;
3784 break;
3785 }
3786 }
3787 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3788
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003789 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003790 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003793 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003795 /* Large page NAND with SOFT_ECC should support subpage reads */
Huang Shijie97de79e02013-10-18 14:20:53 +08003796 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003797 chip->options |= NAND_SUBPAGE_READ;
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08003800 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003801 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3802 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003803 mtd->_erase = nand_erase;
3804 mtd->_point = NULL;
3805 mtd->_unpoint = NULL;
3806 mtd->_read = nand_read;
3807 mtd->_write = nand_write;
3808 mtd->_panic_write = panic_nand_write;
3809 mtd->_read_oob = nand_read_oob;
3810 mtd->_write_oob = nand_write_oob;
3811 mtd->_sync = nand_sync;
3812 mtd->_lock = NULL;
3813 mtd->_unlock = NULL;
3814 mtd->_suspend = nand_suspend;
3815 mtd->_resume = nand_resume;
3816 mtd->_block_isbad = nand_block_isbad;
3817 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003818 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Mike Dunn6a918ba2012-03-11 14:21:11 -07003820 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08003821 mtd->ecclayout = ecc->layout;
3822 mtd->ecc_strength = ecc->strength;
3823 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003824 /*
3825 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3826 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3827 * properly set.
3828 */
3829 if (!mtd->bitflip_threshold)
3830 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003832 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003833 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003837 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003839EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Brian Norris8b6e50c2011-05-25 14:59:01 -07003841/*
3842 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003843 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003844 * to call us from in-kernel code if the core NAND support is modular.
3845 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003846#ifdef MODULE
3847#define caller_is_module() (1)
3848#else
3849#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003850 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003851#endif
3852
3853/**
3854 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003855 * @mtd: MTD device structure
3856 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003857 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003858 * This fills out all the uninitialized function pointers with the defaults.
3859 * The flash ID is read and the mtd/chip structures are filled with the
3860 * appropriate values. The mtd->owner field must be set to the module of the
3861 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003862 */
3863int nand_scan(struct mtd_info *mtd, int maxchips)
3864{
3865 int ret;
3866
3867 /* Many callers got this wrong, so check for it for a while... */
3868 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003869 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003870 BUG();
3871 }
3872
David Woodhouse5e81e882010-02-26 18:32:56 +00003873 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003874 if (!ret)
3875 ret = nand_scan_tail(mtd);
3876 return ret;
3877}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003878EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003881 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003882 * @mtd: MTD device structure
3883 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003884void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003886 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Ivan Djelic193bd402011-03-11 11:05:33 +01003888 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3889 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3890
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003891 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Jesper Juhlfa671642005-11-07 01:01:27 -08003893 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003894 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003895 if (!(chip->options & NAND_OWN_BUFFERS))
3896 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003897
3898 /* Free bad block descriptor memory */
3899 if (chip->badblock_pattern && chip->badblock_pattern->options
3900 & NAND_BBT_DYNAMICSTRUCT)
3901 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
David Woodhousee0c7d762006-05-13 18:07:53 +01003903EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003904
3905static int __init nand_base_init(void)
3906{
3907 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3908 return 0;
3909}
3910
3911static void __exit nand_base_exit(void)
3912{
3913 led_trigger_unregister_simple(nand_led_trigger);
3914}
3915
3916module_init(nand_base_init);
3917module_exit(nand_base_exit);
3918
David Woodhousee0c7d762006-05-13 18:07:53 +01003919MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003920MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3921MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003922MODULE_DESCRIPTION("Generic NAND flash driver code");