blob: 404e83d7e3dad766c1d67c13dc19c398460af619 [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 struct mtd_ecc_stats stats;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001426 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001427 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001428 uint32_t oobreadlen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07001429 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001430 mtd->oobavail : mtd->oobsize;
1431
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001432 uint8_t *bufpoi, *oob, *buf;
Mike Dunnedbc45402012-04-25 12:06:11 -07001433 unsigned int max_bitflips = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001435 stats = mtd->ecc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001437 chipnr = (int)(from >> chip->chip_shift);
1438 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001440 realpage = (int)(from >> chip->page_shift);
1441 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001443 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001445 buf = ops->datbuf;
1446 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001447 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001448
Florian Fainellif8ac0412010-09-07 13:23:43 +02001449 while (1) {
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 Norris6d77b9d2011-09-07 13:13:40 -07001486 !(mtd->ecc_stats.failed - stats.failed) &&
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 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001516 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001517 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001518 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001519 max_bitflips = max_t(unsigned int, max_bitflips,
1520 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001523 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001524
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001525 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Brian Norris8b6e50c2011-05-25 14:59:01 -07001528 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 col = 0;
1530 /* Increment page address */
1531 realpage++;
1532
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001533 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* Check, if we cross a chip boundary */
1535 if (!page) {
1536 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001537 chip->select_chip(mtd, -1);
1538 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001541 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001543 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001544 if (oob)
1545 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Mike Dunn3f91e942012-04-25 12:06:09 -07001547 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001548 return ret;
1549
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001550 if (mtd->ecc_stats.failed - stats.failed)
1551 return -EBADMSG;
1552
Mike Dunnedbc45402012-04-25 12:06:11 -07001553 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001554}
1555
1556/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001557 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001558 * @mtd: MTD device structure
1559 * @from: offset to read from
1560 * @len: number of bytes to read
1561 * @retlen: pointer to variable to store the number of read bytes
1562 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001563 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001564 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001565 */
1566static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1567 size_t *retlen, uint8_t *buf)
1568{
Brian Norris4a89ff82011-08-30 18:45:45 -07001569 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001570 int ret;
1571
Huang Shijie6a8214a2012-11-19 14:43:30 +08001572 nand_get_device(mtd, FL_READING);
Brian Norris4a89ff82011-08-30 18:45:45 -07001573 ops.len = len;
1574 ops.datbuf = buf;
1575 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08001576 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001577 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001578 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001579 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001584 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001585 * @mtd: mtd info structure
1586 * @chip: nand chip info structure
1587 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001588 */
1589static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001590 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001591{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001592 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001593 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001594 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001595}
1596
1597/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001598 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001599 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001600 * @mtd: mtd info structure
1601 * @chip: nand chip info structure
1602 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001603 */
1604static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001605 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001606{
1607 uint8_t *buf = chip->oob_poi;
1608 int length = mtd->oobsize;
1609 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1610 int eccsize = chip->ecc.size;
1611 uint8_t *bufpoi = buf;
1612 int i, toread, sndrnd = 0, pos;
1613
1614 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1615 for (i = 0; i < chip->ecc.steps; i++) {
1616 if (sndrnd) {
1617 pos = eccsize + i * (eccsize + chunk);
1618 if (mtd->writesize > 512)
1619 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1620 else
1621 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1622 } else
1623 sndrnd = 1;
1624 toread = min_t(int, length, chunk);
1625 chip->read_buf(mtd, bufpoi, toread);
1626 bufpoi += toread;
1627 length -= toread;
1628 }
1629 if (length > 0)
1630 chip->read_buf(mtd, bufpoi, length);
1631
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001632 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001633}
1634
1635/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001636 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001637 * @mtd: mtd info structure
1638 * @chip: nand chip info structure
1639 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001640 */
1641static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1642 int page)
1643{
1644 int status = 0;
1645 const uint8_t *buf = chip->oob_poi;
1646 int length = mtd->oobsize;
1647
1648 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1649 chip->write_buf(mtd, buf, length);
1650 /* Send command to program the OOB data */
1651 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1652
1653 status = chip->waitfunc(mtd, chip);
1654
Savin Zlobec0d420f92006-06-21 11:51:20 +02001655 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001656}
1657
1658/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001659 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001660 * with syndrome - only for large page flash
1661 * @mtd: mtd info structure
1662 * @chip: nand chip info structure
1663 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001664 */
1665static int nand_write_oob_syndrome(struct mtd_info *mtd,
1666 struct nand_chip *chip, int page)
1667{
1668 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1669 int eccsize = chip->ecc.size, length = mtd->oobsize;
1670 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1671 const uint8_t *bufpoi = chip->oob_poi;
1672
1673 /*
1674 * data-ecc-data-ecc ... ecc-oob
1675 * or
1676 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1677 */
1678 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1679 pos = steps * (eccsize + chunk);
1680 steps = 0;
1681 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001682 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001683
1684 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1685 for (i = 0; i < steps; i++) {
1686 if (sndcmd) {
1687 if (mtd->writesize <= 512) {
1688 uint32_t fill = 0xFFFFFFFF;
1689
1690 len = eccsize;
1691 while (len > 0) {
1692 int num = min_t(int, len, 4);
1693 chip->write_buf(mtd, (uint8_t *)&fill,
1694 num);
1695 len -= num;
1696 }
1697 } else {
1698 pos = eccsize + i * (eccsize + chunk);
1699 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1700 }
1701 } else
1702 sndcmd = 1;
1703 len = min_t(int, length, chunk);
1704 chip->write_buf(mtd, bufpoi, len);
1705 bufpoi += len;
1706 length -= len;
1707 }
1708 if (length > 0)
1709 chip->write_buf(mtd, bufpoi, length);
1710
1711 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1712 status = chip->waitfunc(mtd, chip);
1713
1714 return status & NAND_STATUS_FAIL ? -EIO : 0;
1715}
1716
1717/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001718 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001719 * @mtd: MTD device structure
1720 * @from: offset to read from
1721 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001723 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001725static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1726 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Brian Norrisc00a0992012-05-01 17:12:54 -07001728 int page, realpage, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001729 struct nand_chip *chip = mtd->priv;
Brian Norris041e4572011-06-23 16:45:24 -07001730 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03001731 int readlen = ops->ooblen;
1732 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001733 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001734 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Brian Norris289c0522011-07-19 10:06:09 -07001736 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05301737 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Brian Norris041e4572011-06-23 16:45:24 -07001739 stats = mtd->ecc_stats;
1740
Brian Norris0612b9d2011-08-30 18:45:40 -07001741 if (ops->mode == MTD_OPS_AUTO_OOB)
Vitaly Wool70145682006-11-03 18:20:38 +03001742 len = chip->ecc.layout->oobavail;
Adrian Hunter03736152007-01-31 17:58:29 +02001743 else
1744 len = mtd->oobsize;
1745
1746 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001747 pr_debug("%s: attempt to start read outside oob\n",
1748 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001749 return -EINVAL;
1750 }
1751
1752 /* Do not allow reads past end of device */
1753 if (unlikely(from >= mtd->size ||
1754 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1755 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07001756 pr_debug("%s: attempt to read beyond end of device\n",
1757 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02001758 return -EINVAL;
1759 }
Vitaly Wool70145682006-11-03 18:20:38 +03001760
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001761 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001762 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001764 /* Shift to get page */
1765 realpage = (int)(from >> chip->page_shift);
1766 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Florian Fainellif8ac0412010-09-07 13:23:43 +02001768 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001769 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001770 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07001771 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001772 ret = chip->ecc.read_oob(mtd, chip, page);
1773
1774 if (ret < 0)
1775 break;
Vitaly Wool70145682006-11-03 18:20:38 +03001776
1777 len = min(len, readlen);
1778 buf = nand_transfer_oob(chip, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001779
Brian Norris5bc7c332013-03-13 09:51:31 -07001780 if (chip->options & NAND_NEED_READRDY) {
1781 /* Apply delay or wait for ready/busy pin */
1782 if (!chip->dev_ready)
1783 udelay(chip->chip_delay);
1784 else
1785 nand_wait_ready(mtd);
1786 }
1787
Vitaly Wool70145682006-11-03 18:20:38 +03001788 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02001789 if (!readlen)
1790 break;
1791
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02001792 /* Increment page address */
1793 realpage++;
1794
1795 page = realpage & chip->pagemask;
1796 /* Check, if we cross a chip boundary */
1797 if (!page) {
1798 chipnr++;
1799 chip->select_chip(mtd, -1);
1800 chip->select_chip(mtd, chipnr);
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001803 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03001805 ops->oobretlen = ops->ooblen - readlen;
1806
1807 if (ret < 0)
1808 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07001809
1810 if (mtd->ecc_stats.failed - stats.failed)
1811 return -EBADMSG;
1812
1813 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001817 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07001818 * @mtd: MTD device structure
1819 * @from: offset to read from
1820 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001822 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001824static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1825 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001827 int ret = -ENOTSUPP;
1828
1829 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03001832 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07001833 pr_debug("%s: attempt to read beyond end of device\n",
1834 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return -EINVAL;
1836 }
1837
Huang Shijie6a8214a2012-11-19 14:43:30 +08001838 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Florian Fainellif8ac0412010-09-07 13:23:43 +02001840 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07001841 case MTD_OPS_PLACE_OOB:
1842 case MTD_OPS_AUTO_OOB:
1843 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001844 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001845
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001846 default:
1847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001850 if (!ops->datbuf)
1851 ret = nand_do_read_oob(mtd, from, ops);
1852 else
1853 ret = nand_do_read_ops(mtd, from, ops);
1854
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001855out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001857 return ret;
1858}
1859
1860
1861/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001862 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001863 * @mtd: mtd info structure
1864 * @chip: nand chip info structure
1865 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001866 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001867 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001868 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001869 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001870static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001871 const uint8_t *buf, int oob_required)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001872{
1873 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001874 if (oob_required)
1875 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001876
1877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001880/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001881 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001882 * @mtd: mtd info structure
1883 * @chip: nand chip info structure
1884 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001885 * @oob_required: must write chip->oob_poi to OOB
David Brownell52ff49d2009-03-04 12:01:36 -08001886 *
1887 * We need a special oob layout and handling even when ECC isn't checked.
1888 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001889static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001890 struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001891 const uint8_t *buf, int oob_required)
David Brownell52ff49d2009-03-04 12:01:36 -08001892{
1893 int eccsize = chip->ecc.size;
1894 int eccbytes = chip->ecc.bytes;
1895 uint8_t *oob = chip->oob_poi;
1896 int steps, size;
1897
1898 for (steps = chip->ecc.steps; steps > 0; steps--) {
1899 chip->write_buf(mtd, buf, eccsize);
1900 buf += eccsize;
1901
1902 if (chip->ecc.prepad) {
1903 chip->write_buf(mtd, oob, chip->ecc.prepad);
1904 oob += chip->ecc.prepad;
1905 }
1906
1907 chip->read_buf(mtd, oob, eccbytes);
1908 oob += eccbytes;
1909
1910 if (chip->ecc.postpad) {
1911 chip->write_buf(mtd, oob, chip->ecc.postpad);
1912 oob += chip->ecc.postpad;
1913 }
1914 }
1915
1916 size = mtd->oobsize - (oob - chip->oob_poi);
1917 if (size)
1918 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08001919
1920 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08001921}
1922/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001923 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001924 * @mtd: mtd info structure
1925 * @chip: nand chip info structure
1926 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001927 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001928 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001929static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001930 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001931{
1932 int i, eccsize = chip->ecc.size;
1933 int eccbytes = chip->ecc.bytes;
1934 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001935 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001936 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001937 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001938
Brian Norris7854d3f2011-06-23 14:12:08 -07001939 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001940 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1941 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001942
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001943 for (i = 0; i < chip->ecc.total; i++)
1944 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001945
Josh Wufdbad98d2012-06-25 18:07:45 +08001946 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001947}
1948
1949/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001950 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001951 * @mtd: mtd info structure
1952 * @chip: nand chip info structure
1953 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07001954 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001955 */
Josh Wufdbad98d2012-06-25 18:07:45 +08001956static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001957 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001958{
1959 int i, eccsize = chip->ecc.size;
1960 int eccbytes = chip->ecc.bytes;
1961 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001962 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001963 const uint8_t *p = buf;
Ben Dooks8b099a32007-05-28 19:17:54 +01001964 uint32_t *eccpos = chip->ecc.layout->eccpos;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001965
1966 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1967 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01001968 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001969 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1970 }
1971
1972 for (i = 0; i < chip->ecc.total; i++)
1973 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1974
1975 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001976
1977 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001978}
1979
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301980
1981/**
1982 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
1983 * @mtd: mtd info structure
1984 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07001985 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301986 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07001987 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301988 * @oob_required: must write chip->oob_poi to OOB
1989 */
1990static int nand_write_subpage_hwecc(struct mtd_info *mtd,
1991 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07001992 uint32_t data_len, const uint8_t *buf,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301993 int oob_required)
1994{
1995 uint8_t *oob_buf = chip->oob_poi;
1996 uint8_t *ecc_calc = chip->buffers->ecccalc;
1997 int ecc_size = chip->ecc.size;
1998 int ecc_bytes = chip->ecc.bytes;
1999 int ecc_steps = chip->ecc.steps;
2000 uint32_t *eccpos = chip->ecc.layout->eccpos;
2001 uint32_t start_step = offset / ecc_size;
2002 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2003 int oob_bytes = mtd->oobsize / ecc_steps;
2004 int step, i;
2005
2006 for (step = 0; step < ecc_steps; step++) {
2007 /* configure controller for WRITE access */
2008 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2009
2010 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002011 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302012
2013 /* mask ECC of un-touched subpages by padding 0xFF */
2014 if ((step < start_step) || (step > end_step))
2015 memset(ecc_calc, 0xff, ecc_bytes);
2016 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002017 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302018
2019 /* mask OOB of un-touched subpages by padding 0xFF */
2020 /* if oob_required, preserve OOB metadata of written subpage */
2021 if (!oob_required || (step < start_step) || (step > end_step))
2022 memset(oob_buf, 0xff, oob_bytes);
2023
Brian Norrisd6a950802013-08-08 17:16:36 -07002024 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302025 ecc_calc += ecc_bytes;
2026 oob_buf += oob_bytes;
2027 }
2028
2029 /* copy calculated ECC for whole page to chip->buffer->oob */
2030 /* this include masked-value(0xFF) for unwritten subpages */
2031 ecc_calc = chip->buffers->ecccalc;
2032 for (i = 0; i < chip->ecc.total; i++)
2033 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2034
2035 /* write OOB buffer to NAND device */
2036 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2037
2038 return 0;
2039}
2040
2041
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002042/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002043 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002044 * @mtd: mtd info structure
2045 * @chip: nand chip info structure
2046 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002047 * @oob_required: must write chip->oob_poi to OOB
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002048 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002049 * The hw generator calculates the error syndrome automatically. Therefore we
2050 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002051 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002052static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002053 struct nand_chip *chip,
2054 const uint8_t *buf, int oob_required)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002055{
2056 int i, eccsize = chip->ecc.size;
2057 int eccbytes = chip->ecc.bytes;
2058 int eccsteps = chip->ecc.steps;
2059 const uint8_t *p = buf;
2060 uint8_t *oob = chip->oob_poi;
2061
2062 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2063
2064 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2065 chip->write_buf(mtd, p, eccsize);
2066
2067 if (chip->ecc.prepad) {
2068 chip->write_buf(mtd, oob, chip->ecc.prepad);
2069 oob += chip->ecc.prepad;
2070 }
2071
2072 chip->ecc.calculate(mtd, p, oob);
2073 chip->write_buf(mtd, oob, eccbytes);
2074 oob += eccbytes;
2075
2076 if (chip->ecc.postpad) {
2077 chip->write_buf(mtd, oob, chip->ecc.postpad);
2078 oob += chip->ecc.postpad;
2079 }
2080 }
2081
2082 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002083 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002084 if (i)
2085 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002086
2087 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002088}
2089
2090/**
David Woodhouse956e9442006-09-25 17:12:39 +01002091 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002092 * @mtd: MTD device structure
2093 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302094 * @offset: address offset within the page
2095 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002096 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002097 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002098 * @page: page number to write
2099 * @cached: cached programming
2100 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002101 */
2102static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302103 uint32_t offset, int data_len, const uint8_t *buf,
2104 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002105{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302106 int status, subpage;
2107
2108 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2109 chip->ecc.write_subpage)
2110 subpage = offset || (data_len < mtd->writesize);
2111 else
2112 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002113
2114 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2115
David Woodhouse956e9442006-09-25 17:12:39 +01002116 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302117 status = chip->ecc.write_page_raw(mtd, chip, buf,
2118 oob_required);
2119 else if (subpage)
2120 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2121 buf, oob_required);
David Woodhouse956e9442006-09-25 17:12:39 +01002122 else
Josh Wufdbad98d2012-06-25 18:07:45 +08002123 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2124
2125 if (status < 0)
2126 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002127
2128 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002129 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002130 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002131 */
2132 cached = 0;
2133
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002134 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002135
2136 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002137 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002138 /*
2139 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002140 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002141 */
2142 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2143 status = chip->errstat(mtd, chip, FL_WRITING, status,
2144 page);
2145
2146 if (status & NAND_STATUS_FAIL)
2147 return -EIO;
2148 } else {
2149 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002150 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002151 }
2152
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002153 return 0;
2154}
2155
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002156/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002157 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002158 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002159 * @oob: oob data buffer
2160 * @len: oob data write length
2161 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002162 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002163static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2164 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002165{
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002166 struct nand_chip *chip = mtd->priv;
2167
2168 /*
2169 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2170 * data from a previous OOB read.
2171 */
2172 memset(chip->oob_poi, 0xff, mtd->oobsize);
2173
Florian Fainellif8ac0412010-09-07 13:23:43 +02002174 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002175
Brian Norris0612b9d2011-08-30 18:45:40 -07002176 case MTD_OPS_PLACE_OOB:
2177 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002178 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2179 return oob + len;
2180
Brian Norris0612b9d2011-08-30 18:45:40 -07002181 case MTD_OPS_AUTO_OOB: {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002182 struct nand_oobfree *free = chip->ecc.layout->oobfree;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002183 uint32_t boffs = 0, woffs = ops->ooboffs;
2184 size_t bytes = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002185
Florian Fainellif8ac0412010-09-07 13:23:43 +02002186 for (; free->length && len; free++, len -= bytes) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002187 /* Write request not from offset 0? */
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002188 if (unlikely(woffs)) {
2189 if (woffs >= free->length) {
2190 woffs -= free->length;
2191 continue;
2192 }
2193 boffs = free->offset + woffs;
2194 bytes = min_t(size_t, len,
2195 (free->length - woffs));
2196 woffs = 0;
2197 } else {
2198 bytes = min_t(size_t, len, free->length);
2199 boffs = free->offset;
2200 }
Vitaly Wool8b0036e2006-07-11 09:11:25 +02002201 memcpy(chip->oob_poi + boffs, oob, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002202 oob += bytes;
2203 }
2204 return oob;
2205 }
2206 default:
2207 BUG();
2208 }
2209 return NULL;
2210}
2211
Florian Fainellif8ac0412010-09-07 13:23:43 +02002212#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002213
2214/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002215 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002216 * @mtd: MTD device structure
2217 * @to: offset to write to
2218 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002219 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002220 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002221 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002222static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2223 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002224{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002225 int chipnr, realpage, page, blockmask, column;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002226 struct nand_chip *chip = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002227 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002228
2229 uint32_t oobwritelen = ops->ooblen;
Brian Norris0612b9d2011-08-30 18:45:40 -07002230 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Maxim Levitsky782ce792010-02-22 20:39:36 +02002231 mtd->oobavail : mtd->oobsize;
2232
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002233 uint8_t *oob = ops->oobbuf;
2234 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302235 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002236 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002238 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002239 if (!writelen)
2240 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002241
Brian Norris8b6e50c2011-05-25 14:59:01 -07002242 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002243 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002244 pr_notice("%s: attempt to write non page aligned data\n",
2245 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002246 return -EINVAL;
2247 }
2248
Thomas Gleixner29072b92006-09-28 15:38:36 +02002249 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002250
Thomas Gleixner6a930962006-06-28 00:11:45 +02002251 chipnr = (int)(to >> chip->chip_shift);
2252 chip->select_chip(mtd, chipnr);
2253
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002254 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002255 if (nand_check_wp(mtd)) {
2256 ret = -EIO;
2257 goto err_out;
2258 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002259
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002260 realpage = (int)(to >> chip->page_shift);
2261 page = realpage & chip->pagemask;
2262 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2263
2264 /* Invalidate the page cache, when we write to the cached page */
2265 if (to <= (chip->pagebuf << chip->page_shift) &&
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002266 (chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002267 chip->pagebuf = -1;
2268
Maxim Levitsky782ce792010-02-22 20:39:36 +02002269 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002270 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2271 ret = -EINVAL;
2272 goto err_out;
2273 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002274
Florian Fainellif8ac0412010-09-07 13:23:43 +02002275 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002276 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002277 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002278 uint8_t *wbuf = buf;
2279
Brian Norris8b6e50c2011-05-25 14:59:01 -07002280 /* Partial page write? */
Thomas Gleixner29072b92006-09-28 15:38:36 +02002281 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2282 cached = 0;
2283 bytes = min_t(int, bytes - column, (int) writelen);
2284 chip->pagebuf = -1;
2285 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2286 memcpy(&chip->buffers->databuf[column], buf, bytes);
2287 wbuf = chip->buffers->databuf;
2288 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002289
Maxim Levitsky782ce792010-02-22 20:39:36 +02002290 if (unlikely(oob)) {
2291 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002292 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002293 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002294 } else {
2295 /* We still need to erase leftover OOB data */
2296 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002297 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302298 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2299 oob_required, page, cached,
2300 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002301 if (ret)
2302 break;
2303
2304 writelen -= bytes;
2305 if (!writelen)
2306 break;
2307
Thomas Gleixner29072b92006-09-28 15:38:36 +02002308 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002309 buf += bytes;
2310 realpage++;
2311
2312 page = realpage & chip->pagemask;
2313 /* Check, if we cross a chip boundary */
2314 if (!page) {
2315 chipnr++;
2316 chip->select_chip(mtd, -1);
2317 chip->select_chip(mtd, chipnr);
2318 }
2319 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002320
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002321 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002322 if (unlikely(oob))
2323 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002324
2325err_out:
2326 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002327 return ret;
2328}
2329
2330/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002331 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002332 * @mtd: MTD device structure
2333 * @to: offset to write to
2334 * @len: number of bytes to write
2335 * @retlen: pointer to variable to store the number of written bytes
2336 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002337 *
2338 * NAND write with ECC. Used when performing writes in interrupt context, this
2339 * may for example be called by mtdoops when writing an oops while in panic.
2340 */
2341static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2342 size_t *retlen, const uint8_t *buf)
2343{
2344 struct nand_chip *chip = mtd->priv;
Brian Norris4a89ff82011-08-30 18:45:45 -07002345 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002346 int ret;
2347
Brian Norris8b6e50c2011-05-25 14:59:01 -07002348 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002349 panic_nand_wait(mtd, chip, 400);
2350
Brian Norris8b6e50c2011-05-25 14:59:01 -07002351 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002352 panic_nand_get_device(chip, mtd, FL_WRITING);
2353
Brian Norris4a89ff82011-08-30 18:45:45 -07002354 ops.len = len;
2355 ops.datbuf = (uint8_t *)buf;
2356 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002357 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002358
Brian Norris4a89ff82011-08-30 18:45:45 -07002359 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002360
Brian Norris4a89ff82011-08-30 18:45:45 -07002361 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002362 return ret;
2363}
2364
2365/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002366 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002367 * @mtd: MTD device structure
2368 * @to: offset to write to
2369 * @len: number of bytes to write
2370 * @retlen: pointer to variable to store the number of written bytes
2371 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002373 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002375static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002376 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Brian Norris4a89ff82011-08-30 18:45:45 -07002378 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002379 int ret;
2380
Huang Shijie6a8214a2012-11-19 14:43:30 +08002381 nand_get_device(mtd, FL_WRITING);
Brian Norris4a89ff82011-08-30 18:45:45 -07002382 ops.len = len;
2383 ops.datbuf = (uint8_t *)buf;
2384 ops.oobbuf = NULL;
Huang Shijie11041ae2012-07-03 16:44:14 +08002385 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002386 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002387 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002388 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002389 return ret;
2390}
2391
2392/**
2393 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002394 * @mtd: MTD device structure
2395 * @to: offset to write to
2396 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002397 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002398 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002399 */
2400static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2401 struct mtd_oob_ops *ops)
2402{
Adrian Hunter03736152007-01-31 17:58:29 +02002403 int chipnr, page, status, len;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002404 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Brian Norris289c0522011-07-19 10:06:09 -07002406 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302407 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Brian Norris0612b9d2011-08-30 18:45:40 -07002409 if (ops->mode == MTD_OPS_AUTO_OOB)
Adrian Hunter03736152007-01-31 17:58:29 +02002410 len = chip->ecc.layout->oobavail;
2411 else
2412 len = mtd->oobsize;
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002415 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002416 pr_debug("%s: attempt to write past end of page\n",
2417 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -EINVAL;
2419 }
2420
Adrian Hunter03736152007-01-31 17:58:29 +02002421 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002422 pr_debug("%s: attempt to start write outside oob\n",
2423 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002424 return -EINVAL;
2425 }
2426
Jason Liu775adc32011-02-25 13:06:18 +08002427 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002428 if (unlikely(to >= mtd->size ||
2429 ops->ooboffs + ops->ooblen >
2430 ((mtd->size >> chip->page_shift) -
2431 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002432 pr_debug("%s: attempt to write beyond end of device\n",
2433 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002434 return -EINVAL;
2435 }
2436
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002437 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002438 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002440 /* Shift to get page */
2441 page = (int)(to >> chip->page_shift);
2442
2443 /*
2444 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2445 * of my DiskOnChip 2000 test units) will clear the whole data page too
2446 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2447 * it in the doc2000 driver in August 1999. dwmw2.
2448 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002449 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002452 if (nand_check_wp(mtd)) {
2453 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002454 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002455 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002458 if (page == chip->pagebuf)
2459 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002461 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002462
Brian Norris0612b9d2011-08-30 18:45:40 -07002463 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002464 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2465 else
2466 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002467
Huang Shijieb0bb6902012-11-19 14:43:29 +08002468 chip->select_chip(mtd, -1);
2469
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002470 if (status)
2471 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Vitaly Wool70145682006-11-03 18:20:38 +03002473 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002475 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002478/**
2479 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002480 * @mtd: MTD device structure
2481 * @to: offset to write to
2482 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002483 */
2484static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2485 struct mtd_oob_ops *ops)
2486{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002487 int ret = -ENOTSUPP;
2488
2489 ops->retlen = 0;
2490
2491 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002492 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002493 pr_debug("%s: attempt to write beyond end of device\n",
2494 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002495 return -EINVAL;
2496 }
2497
Huang Shijie6a8214a2012-11-19 14:43:30 +08002498 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002499
Florian Fainellif8ac0412010-09-07 13:23:43 +02002500 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002501 case MTD_OPS_PLACE_OOB:
2502 case MTD_OPS_AUTO_OOB:
2503 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002504 break;
2505
2506 default:
2507 goto out;
2508 }
2509
2510 if (!ops->datbuf)
2511 ret = nand_do_write_oob(mtd, to, ops);
2512 else
2513 ret = nand_do_write_ops(mtd, to, ops);
2514
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002515out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002516 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return ret;
2518}
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002521 * single_erase_cmd - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002522 * @mtd: MTD device structure
2523 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002525 * Standard erase command for NAND chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002527static void single_erase_cmd(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002529 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002531 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2532 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
2534
2535/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002537 * @mtd: MTD device structure
2538 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002540 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002542static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
David Woodhousee0c7d762006-05-13 18:07:53 +01002544 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002548 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002549 * @mtd: MTD device structure
2550 * @instr: erase instruction
2551 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002553 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002555int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2556 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Adrian Hunter69423d92008-12-10 13:37:21 +00002558 int page, status, pages_per_block, ret, chipnr;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002559 struct nand_chip *chip = mtd->priv;
Adrian Hunter69423d92008-12-10 13:37:21 +00002560 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Brian Norris289c0522011-07-19 10:06:09 -07002562 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2563 __func__, (unsigned long long)instr->addr,
2564 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302566 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002570 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002573 page = (int)(instr->addr >> chip->page_shift);
2574 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002577 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002580 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 /* Check, if it is write protected */
2583 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002584 pr_debug("%s: device is write protected!\n",
2585 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 instr->state = MTD_ERASE_FAILED;
2587 goto erase_exit;
2588 }
2589
2590 /* Loop through the pages */
2591 len = instr->len;
2592
2593 instr->state = MTD_ERASING;
2594
2595 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002596 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002597 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2598 chip->page_shift, 0, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002599 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2600 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 instr->state = MTD_ERASE_FAILED;
2602 goto erase_exit;
2603 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002604
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002605 /*
2606 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002607 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002608 */
2609 if (page <= chip->pagebuf && chip->pagebuf <
2610 (page + pages_per_block))
2611 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002613 chip->erase_cmd(mtd, page & chip->pagemask);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002614
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002615 status = chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002617 /*
2618 * See if operation failed and additional status checks are
2619 * available
2620 */
2621 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2622 status = chip->errstat(mtd, chip, FL_ERASING,
2623 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002626 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002627 pr_debug("%s: failed erase, page 0x%08x\n",
2628 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002630 instr->fail_addr =
2631 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 goto erase_exit;
2633 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002636 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 page += pages_per_block;
2638
2639 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002640 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002642 chip->select_chip(mtd, -1);
2643 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 }
2646 instr->state = MTD_ERASE_DONE;
2647
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002648erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002653 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 nand_release_device(mtd);
2655
David Woodhouse49defc02007-10-06 15:01:59 -04002656 /* Do call back function */
2657 if (!ret)
2658 mtd_erase_callback(instr);
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /* Return more or less happy */
2661 return ret;
2662}
2663
2664/**
2665 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002666 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002668 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002670static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Brian Norris289c0522011-07-19 10:06:09 -07002672 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002675 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002677 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002681 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002682 * @mtd: MTD device structure
2683 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002685static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002687 return nand_block_checkbad(mtd, offs, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
2690/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002691 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002692 * @mtd: MTD device structure
2693 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002695static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 int ret;
2698
Florian Fainellif8ac0412010-09-07 13:23:43 +02002699 ret = nand_block_isbad(mtd, ofs);
2700 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07002701 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (ret > 0)
2703 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01002704 return ret;
2705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Brian Norris5a0edb22013-07-30 17:52:58 -07002707 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08002711 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2712 * @mtd: MTD device structure
2713 * @chip: nand chip info structure
2714 * @addr: feature address.
2715 * @subfeature_param: the subfeature parameters, a four bytes array.
2716 */
2717static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2718 int addr, uint8_t *subfeature_param)
2719{
2720 int status;
2721
David Mosbergerd914c932013-05-29 15:30:13 +03002722 if (!chip->onfi_version ||
2723 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2724 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002725 return -EINVAL;
2726
2727 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2728 chip->write_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2729 status = chip->waitfunc(mtd, chip);
2730 if (status & NAND_STATUS_FAIL)
2731 return -EIO;
2732 return 0;
2733}
2734
2735/**
2736 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2737 * @mtd: MTD device structure
2738 * @chip: nand chip info structure
2739 * @addr: feature address.
2740 * @subfeature_param: the subfeature parameters, a four bytes array.
2741 */
2742static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2743 int addr, uint8_t *subfeature_param)
2744{
David Mosbergerd914c932013-05-29 15:30:13 +03002745 if (!chip->onfi_version ||
2746 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2747 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08002748 return -EINVAL;
2749
2750 /* clear the sub feature parameters */
2751 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
2752
2753 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
2754 chip->read_buf(mtd, subfeature_param, ONFI_SUBFEATURE_PARAM_LEN);
2755 return 0;
2756}
2757
2758/**
Vitaly Wool962034f2005-09-15 14:58:53 +01002759 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002760 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002761 */
2762static int nand_suspend(struct mtd_info *mtd)
2763{
Huang Shijie6a8214a2012-11-19 14:43:30 +08002764 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01002765}
2766
2767/**
2768 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07002769 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01002770 */
2771static void nand_resume(struct mtd_info *mtd)
2772{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002773 struct nand_chip *chip = mtd->priv;
Vitaly Wool962034f2005-09-15 14:58:53 +01002774
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002775 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01002776 nand_release_device(mtd);
2777 else
Brian Norrisd0370212011-07-19 10:06:08 -07002778 pr_err("%s called for a chip which is not in suspended state\n",
2779 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01002780}
2781
Brian Norris8b6e50c2011-05-25 14:59:01 -07002782/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002783static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002786 if (!chip->chip_delay)
2787 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002790 if (chip->cmdfunc == NULL)
2791 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002794 if (chip->waitfunc == NULL)
2795 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002797 if (!chip->select_chip)
2798 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07002799
Huang Shijie4204ccc2013-08-16 10:10:07 +08002800 /* set for ONFI nand */
2801 if (!chip->onfi_set_features)
2802 chip->onfi_set_features = nand_onfi_set_features;
2803 if (!chip->onfi_get_features)
2804 chip->onfi_get_features = nand_onfi_get_features;
2805
Brian Norris68e80782013-07-18 01:17:02 -07002806 /* If called twice, pointers that depend on busw may need to be reset */
2807 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002808 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2809 if (!chip->read_word)
2810 chip->read_word = nand_read_word;
2811 if (!chip->block_bad)
2812 chip->block_bad = nand_block_bad;
2813 if (!chip->block_markbad)
2814 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07002815 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002816 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Brian Norris68e80782013-07-18 01:17:02 -07002817 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002818 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002819 if (!chip->scan_bbt)
2820 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002821
2822 if (!chip->controller) {
2823 chip->controller = &chip->hwcontrol;
2824 spin_lock_init(&chip->controller->lock);
2825 init_waitqueue_head(&chip->controller->wq);
2826 }
2827
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02002828}
2829
Brian Norris8b6e50c2011-05-25 14:59:01 -07002830/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002831static void sanitize_string(uint8_t *s, size_t len)
2832{
2833 ssize_t i;
2834
Brian Norris8b6e50c2011-05-25 14:59:01 -07002835 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002836 s[len - 1] = 0;
2837
Brian Norris8b6e50c2011-05-25 14:59:01 -07002838 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002839 for (i = 0; i < len - 1; i++) {
2840 if (s[i] < ' ' || s[i] > 127)
2841 s[i] = '?';
2842 }
2843
Brian Norris8b6e50c2011-05-25 14:59:01 -07002844 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002845 strim(s);
2846}
2847
2848static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2849{
2850 int i;
2851 while (len--) {
2852 crc ^= *p++ << 8;
2853 for (i = 0; i < 8; i++)
2854 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2855 }
2856
2857 return crc;
2858}
2859
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002860/* Parse the Extended Parameter Page. */
2861static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
2862 struct nand_chip *chip, struct nand_onfi_params *p)
2863{
2864 struct onfi_ext_param_page *ep;
2865 struct onfi_ext_section *s;
2866 struct onfi_ext_ecc_info *ecc;
2867 uint8_t *cursor;
2868 int ret = -EINVAL;
2869 int len;
2870 int i;
2871
2872 len = le16_to_cpu(p->ext_param_page_length) * 16;
2873 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07002874 if (!ep)
2875 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002876
2877 /* Send our own NAND_CMD_PARAM. */
2878 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2879
2880 /* Use the Change Read Column command to skip the ONFI param pages. */
2881 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
2882 sizeof(*p) * p->num_of_param_pages , -1);
2883
2884 /* Read out the Extended Parameter Page. */
2885 chip->read_buf(mtd, (uint8_t *)ep, len);
2886 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
2887 != le16_to_cpu(ep->crc))) {
2888 pr_debug("fail in the CRC.\n");
2889 goto ext_out;
2890 }
2891
2892 /*
2893 * Check the signature.
2894 * Do not strictly follow the ONFI spec, maybe changed in future.
2895 */
2896 if (strncmp(ep->sig, "EPPS", 4)) {
2897 pr_debug("The signature is invalid.\n");
2898 goto ext_out;
2899 }
2900
2901 /* find the ECC section. */
2902 cursor = (uint8_t *)(ep + 1);
2903 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
2904 s = ep->sections + i;
2905 if (s->type == ONFI_SECTION_TYPE_2)
2906 break;
2907 cursor += s->length * 16;
2908 }
2909 if (i == ONFI_EXT_SECTION_MAX) {
2910 pr_debug("We can not find the ECC section.\n");
2911 goto ext_out;
2912 }
2913
2914 /* get the info we want. */
2915 ecc = (struct onfi_ext_ecc_info *)cursor;
2916
Brian Norris4ae7d222013-09-16 18:20:21 -07002917 if (!ecc->codeword_size) {
2918 pr_debug("Invalid codeword size\n");
2919 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002920 }
2921
Brian Norris4ae7d222013-09-16 18:20:21 -07002922 chip->ecc_strength_ds = ecc->ecc_bits;
2923 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07002924 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08002925
2926ext_out:
2927 kfree(ep);
2928 return ret;
2929}
2930
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02002931/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07002932 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002933 */
2934static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02002935 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002936{
2937 struct nand_onfi_params *p = &chip->onfi_params;
2938 int i;
2939 int val;
2940
Brian Norris7854d3f2011-06-23 14:12:08 -07002941 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002942 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2943 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2944 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2945 return 0;
2946
Brian Norrisc7f23a72013-08-13 10:51:55 -07002947 /*
2948 * ONFI must be probed in 8-bit mode or with NAND_BUSWIDTH_AUTO, not
2949 * with NAND_BUSWIDTH_16
2950 */
2951 if (chip->options & NAND_BUSWIDTH_16) {
2952 pr_err("ONFI cannot be probed in 16-bit mode; aborting\n");
2953 return 0;
2954 }
2955
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002956 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2957 for (i = 0; i < 3; i++) {
2958 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2959 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2960 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002961 break;
2962 }
2963 }
2964
Brian Norrisc7f23a72013-08-13 10:51:55 -07002965 if (i == 3) {
2966 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002967 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07002968 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002969
Brian Norris8b6e50c2011-05-25 14:59:01 -07002970 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002971 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002972 if (val & (1 << 5))
2973 chip->onfi_version = 23;
2974 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002975 chip->onfi_version = 22;
2976 else if (val & (1 << 3))
2977 chip->onfi_version = 21;
2978 else if (val & (1 << 2))
2979 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002980 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002981 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08002982
2983 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03002984 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08002985 return 0;
2986 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002987
2988 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
2989 sanitize_string(p->model, sizeof(p->model));
2990 if (!mtd->name)
2991 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07002992
Florian Fainelli6fb277b2010-09-01 22:28:59 +02002993 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07002994
2995 /*
2996 * pages_per_block and blocks_per_lun may not be a power-of-2 size
2997 * (don't ask me who thought of this...). MTD assumes that these
2998 * dimensions will be power-of-2, so just truncate the remaining area.
2999 */
3000 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3001 mtd->erasesize *= mtd->writesize;
3002
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003003 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003004
3005 /* See erasesize comment */
3006 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003007 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003008 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003009
3010 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003011 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003012 else
3013 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003014
Huang Shijie10c86ba2013-05-17 11:17:26 +08003015 if (p->ecc_bits != 0xff) {
3016 chip->ecc_strength_ds = p->ecc_bits;
3017 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003018 } else if (chip->onfi_version >= 21 &&
3019 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3020
3021 /*
3022 * The nand_flash_detect_ext_param_page() uses the
3023 * Change Read Column command which maybe not supported
3024 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3025 * now. We do not replace user supplied command function.
3026 */
3027 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3028 chip->cmdfunc = nand_command_lp;
3029
3030 /* The Extended Parameter Page is supported since ONFI 2.1. */
3031 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003032 pr_warn("Failed to detect ONFI extended param page\n");
3033 } else {
3034 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003035 }
3036
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003037 return 1;
3038}
3039
3040/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003041 * nand_id_has_period - Check if an ID string has a given wraparound period
3042 * @id_data: the ID string
3043 * @arrlen: the length of the @id_data array
3044 * @period: the period of repitition
3045 *
3046 * Check if an ID string is repeated within a given sequence of bytes at
3047 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003048 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003049 * if the repetition has a period of @period; otherwise, returns zero.
3050 */
3051static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3052{
3053 int i, j;
3054 for (i = 0; i < period; i++)
3055 for (j = i + period; j < arrlen; j += period)
3056 if (id_data[i] != id_data[j])
3057 return 0;
3058 return 1;
3059}
3060
3061/*
3062 * nand_id_len - Get the length of an ID string returned by CMD_READID
3063 * @id_data: the ID string
3064 * @arrlen: the length of the @id_data array
3065
3066 * Returns the length of the ID string, according to known wraparound/trailing
3067 * zero patterns. If no pattern exists, returns the length of the array.
3068 */
3069static int nand_id_len(u8 *id_data, int arrlen)
3070{
3071 int last_nonzero, period;
3072
3073 /* Find last non-zero byte */
3074 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3075 if (id_data[last_nonzero])
3076 break;
3077
3078 /* All zeros */
3079 if (last_nonzero < 0)
3080 return 0;
3081
3082 /* Calculate wraparound period */
3083 for (period = 1; period < arrlen; period++)
3084 if (nand_id_has_period(id_data, arrlen, period))
3085 break;
3086
3087 /* There's a repeated pattern */
3088 if (period < arrlen)
3089 return period;
3090
3091 /* There are trailing zeros */
3092 if (last_nonzero < arrlen - 1)
3093 return last_nonzero + 1;
3094
3095 /* No pattern detected */
3096 return arrlen;
3097}
3098
Huang Shijie7db906b2013-09-25 14:58:11 +08003099/* Extract the bits of per cell from the 3rd byte of the extended ID */
3100static int nand_get_bits_per_cell(u8 cellinfo)
3101{
3102 int bits;
3103
3104 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3105 bits >>= NAND_CI_CELLTYPE_SHIFT;
3106 return bits + 1;
3107}
3108
Brian Norrise3b88bd2012-09-24 20:40:52 -07003109/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003110 * Many new NAND share similar device ID codes, which represent the size of the
3111 * chip. The rest of the parameters must be decoded according to generic or
3112 * manufacturer-specific "extended ID" decoding patterns.
3113 */
3114static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3115 u8 id_data[8], int *busw)
3116{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003117 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003118 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003119 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003120 /* The 4th id byte is the important one */
3121 extid = id_data[3];
3122
Brian Norrise3b88bd2012-09-24 20:40:52 -07003123 id_len = nand_id_len(id_data, 8);
3124
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003125 /*
3126 * Field definitions are in the following datasheets:
3127 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003128 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003129 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003130 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003131 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3132 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003133 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003134 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003135 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003136 /* Calc pagesize */
3137 mtd->writesize = 2048 << (extid & 0x03);
3138 extid >>= 2;
3139 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003140 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003141 case 1:
3142 mtd->oobsize = 128;
3143 break;
3144 case 2:
3145 mtd->oobsize = 218;
3146 break;
3147 case 3:
3148 mtd->oobsize = 400;
3149 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003150 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003151 mtd->oobsize = 436;
3152 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003153 case 5:
3154 mtd->oobsize = 512;
3155 break;
3156 case 6:
3157 default: /* Other cases are "reserved" (unknown) */
3158 mtd->oobsize = 640;
3159 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003160 }
3161 extid >>= 2;
3162 /* Calc blocksize */
3163 mtd->erasesize = (128 * 1024) <<
3164 (((extid >> 1) & 0x04) | (extid & 0x03));
3165 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003166 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003167 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003168 unsigned int tmp;
3169
3170 /* Calc pagesize */
3171 mtd->writesize = 2048 << (extid & 0x03);
3172 extid >>= 2;
3173 /* Calc oobsize */
3174 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3175 case 0:
3176 mtd->oobsize = 128;
3177 break;
3178 case 1:
3179 mtd->oobsize = 224;
3180 break;
3181 case 2:
3182 mtd->oobsize = 448;
3183 break;
3184 case 3:
3185 mtd->oobsize = 64;
3186 break;
3187 case 4:
3188 mtd->oobsize = 32;
3189 break;
3190 case 5:
3191 mtd->oobsize = 16;
3192 break;
3193 default:
3194 mtd->oobsize = 640;
3195 break;
3196 }
3197 extid >>= 2;
3198 /* Calc blocksize */
3199 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3200 if (tmp < 0x03)
3201 mtd->erasesize = (128 * 1024) << tmp;
3202 else if (tmp == 0x03)
3203 mtd->erasesize = 768 * 1024;
3204 else
3205 mtd->erasesize = (64 * 1024) << tmp;
3206 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003207 } else {
3208 /* Calc pagesize */
3209 mtd->writesize = 1024 << (extid & 0x03);
3210 extid >>= 2;
3211 /* Calc oobsize */
3212 mtd->oobsize = (8 << (extid & 0x01)) *
3213 (mtd->writesize >> 9);
3214 extid >>= 2;
3215 /* Calc blocksize. Blocksize is multiples of 64KiB */
3216 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3217 extid >>= 2;
3218 /* Get buswidth information */
3219 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003220
3221 /*
3222 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3223 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3224 * follows:
3225 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3226 * 110b -> 24nm
3227 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3228 */
3229 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003230 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003231 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3232 !(id_data[4] & 0x80) /* !BENAND */) {
3233 mtd->oobsize = 32 * mtd->writesize >> 9;
3234 }
3235
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003236 }
3237}
3238
3239/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003240 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3241 * decodes a matching ID table entry and assigns the MTD size parameters for
3242 * the chip.
3243 */
3244static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3245 struct nand_flash_dev *type, u8 id_data[8],
3246 int *busw)
3247{
3248 int maf_id = id_data[0];
3249
3250 mtd->erasesize = type->erasesize;
3251 mtd->writesize = type->pagesize;
3252 mtd->oobsize = mtd->writesize / 32;
3253 *busw = type->options & NAND_BUSWIDTH_16;
3254
Huang Shijie1c195e92013-09-25 14:58:12 +08003255 /* All legacy ID NAND are small-page, SLC */
3256 chip->bits_per_cell = 1;
3257
Brian Norrisf23a4812012-09-24 20:40:51 -07003258 /*
3259 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3260 * some Spansion chips have erasesize that conflicts with size
3261 * listed in nand_ids table.
3262 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3263 */
3264 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3265 && id_data[6] == 0x00 && id_data[7] == 0x00
3266 && mtd->writesize == 512) {
3267 mtd->erasesize = 128 * 1024;
3268 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3269 }
3270}
3271
3272/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003273 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3274 * heuristic patterns using various detected parameters (e.g., manufacturer,
3275 * page size, cell-type information).
3276 */
3277static void nand_decode_bbm_options(struct mtd_info *mtd,
3278 struct nand_chip *chip, u8 id_data[8])
3279{
3280 int maf_id = id_data[0];
3281
3282 /* Set the bad block position */
3283 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3284 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3285 else
3286 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3287
3288 /*
3289 * Bad block marker is stored in the last page of each block on Samsung
3290 * and Hynix MLC devices; stored in first two pages of each block on
3291 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3292 * AMD/Spansion, and Macronix. All others scan only the first page.
3293 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003294 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003295 (maf_id == NAND_MFR_SAMSUNG ||
3296 maf_id == NAND_MFR_HYNIX))
3297 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003298 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003299 (maf_id == NAND_MFR_SAMSUNG ||
3300 maf_id == NAND_MFR_HYNIX ||
3301 maf_id == NAND_MFR_TOSHIBA ||
3302 maf_id == NAND_MFR_AMD ||
3303 maf_id == NAND_MFR_MACRONIX)) ||
3304 (mtd->writesize == 2048 &&
3305 maf_id == NAND_MFR_MICRON))
3306 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3307}
3308
Huang Shijieec6e87e2013-03-15 11:01:00 +08003309static inline bool is_full_id_nand(struct nand_flash_dev *type)
3310{
3311 return type->id_len;
3312}
3313
3314static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3315 struct nand_flash_dev *type, u8 *id_data, int *busw)
3316{
3317 if (!strncmp(type->id, id_data, type->id_len)) {
3318 mtd->writesize = type->pagesize;
3319 mtd->erasesize = type->erasesize;
3320 mtd->oobsize = type->oobsize;
3321
Huang Shijie7db906b2013-09-25 14:58:11 +08003322 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003323 chip->chipsize = (uint64_t)type->chipsize << 20;
3324 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003325 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3326 chip->ecc_step_ds = NAND_ECC_STEP(type);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003327
3328 *busw = type->options & NAND_BUSWIDTH_16;
3329
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003330 if (!mtd->name)
3331 mtd->name = type->name;
3332
Huang Shijieec6e87e2013-03-15 11:01:00 +08003333 return true;
3334 }
3335 return false;
3336}
3337
Brian Norris7e74c2d2012-09-24 20:40:49 -07003338/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003339 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003340 */
3341static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003342 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003343 int busw,
3344 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003345 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003346{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003347 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003348 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003351 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Karl Beldanef89a882008-09-15 14:37:29 +02003353 /*
3354 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003355 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003356 */
3357 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003360 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003363 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003364 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Brian Norris8b6e50c2011-05-25 14:59:01 -07003366 /*
3367 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003368 * interface concerns can cause random data which looks like a
3369 * possibly credible NAND flash to appear. If the two results do
3370 * not match, ignore the device completely.
3371 */
3372
3373 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3374
Brian Norris4aef9b72012-09-24 20:40:48 -07003375 /* Read entire ID string */
3376 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003377 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003378
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003379 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003380 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003381 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003382 return ERR_PTR(-ENODEV);
3383 }
3384
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003385 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003386 type = nand_flash_ids;
3387
Huang Shijieec6e87e2013-03-15 11:01:00 +08003388 for (; type->name != NULL; type++) {
3389 if (is_full_id_nand(type)) {
3390 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3391 goto ident_done;
3392 } else if (*dev_id == type->dev_id) {
3393 break;
3394 }
3395 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003396
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003397 chip->onfi_version = 0;
3398 if (!type->name || !type->pagesize) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003399 /* Check is chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003400 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003401 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003402 }
3403
David Woodhouse5e81e882010-02-26 18:32:56 +00003404 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003405 return ERR_PTR(-ENODEV);
3406
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003407 if (!mtd->name)
3408 mtd->name = type->name;
3409
Adrian Hunter69423d92008-12-10 13:37:21 +00003410 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003411
Huang Shijie12a40a52010-09-27 10:43:53 +08003412 if (!type->pagesize && chip->init_size) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003413 /* Set the pagesize, oobsize, erasesize by the driver */
Huang Shijie12a40a52010-09-27 10:43:53 +08003414 busw = chip->init_size(mtd, chip, id_data);
3415 } else if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003416 /* Decode parameters from extended ID */
3417 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003418 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003419 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003420 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003421 /* Get chip options */
3422 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003423
Brian Norris8b6e50c2011-05-25 14:59:01 -07003424 /*
3425 * Check if chip is not a Samsung device. Do not clear the
3426 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003427 */
3428 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3429 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3430ident_done:
3431
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003432 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003433 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003434 if (nand_manuf_ids[maf_idx].id == *maf_id)
3435 break;
3436 }
3437
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003438 if (chip->options & NAND_BUSWIDTH_AUTO) {
3439 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3440 chip->options |= busw;
3441 nand_set_defaults(chip, busw);
3442 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3443 /*
3444 * Check, if buswidth is correct. Hardware drivers should set
3445 * chip correct!
3446 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003447 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3448 *maf_id, *dev_id);
3449 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3450 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003451 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3452 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003453 return ERR_PTR(-EINVAL);
3454 }
3455
Brian Norris7e74c2d2012-09-24 20:40:49 -07003456 nand_decode_bbm_options(mtd, chip, id_data);
3457
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003458 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003459 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003460 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003461 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003462
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003463 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003464 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003465 if (chip->chipsize & 0xffffffff)
3466 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003467 else {
3468 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3469 chip->chip_shift += 32 - 1;
3470 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003471
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003472 chip->badblockbits = 8;
Artem Bityutskiy14c65782013-03-04 14:21:34 +02003473 chip->erase_cmd = single_erase_cmd;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003474
Brian Norris8b6e50c2011-05-25 14:59:01 -07003475 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003476 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3477 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003478
Ezequiel Garcia20171642013-11-25 08:30:31 -03003479 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3480 *maf_id, *dev_id);
3481 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
Huang Shijie3723e932013-09-25 14:58:14 +08003482 chip->onfi_version ? chip->onfi_params.model : type->name);
Ezequiel Garcia20171642013-11-25 08:30:31 -03003483 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003484 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3485 mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003486 return type;
3487}
3488
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003489/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003490 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003491 * @mtd: MTD device structure
3492 * @maxchips: number of chips to scan for
3493 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003494 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003495 * This is the first phase of the normal nand_scan() function. It reads the
3496 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003497 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003498 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003499 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003500int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3501 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003502{
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003503 int i, busw, nand_maf_id, nand_dev_id;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003504 struct nand_chip *chip = mtd->priv;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003505 struct nand_flash_dev *type;
3506
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003507 /* Get buswidth to select the correct functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003508 busw = chip->options & NAND_BUSWIDTH_16;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003509 /* Set the default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003510 nand_set_defaults(chip, busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003511
3512 /* Read the flash type */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003513 type = nand_get_flash_type(mtd, chip, busw,
3514 &nand_maf_id, &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003515
3516 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00003517 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07003518 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003519 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003520 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
3522
Huang Shijie07300162012-11-09 16:23:45 +08003523 chip->select_chip(mtd, -1);
3524
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003525 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01003526 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003527 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02003528 /* See comment in nand_get_flash_type for reset */
3529 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003531 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003533 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08003534 nand_dev_id != chip->read_byte(mtd)) {
3535 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 break;
Huang Shijie07300162012-11-09 16:23:45 +08003537 }
3538 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
3540 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03003541 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003544 chip->numchips = i;
3545 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
David Woodhouse3b85c322006-09-25 17:06:53 +01003547 return 0;
3548}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003549EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01003550
3551
3552/**
3553 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003554 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01003555 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003556 * This is the second phase of the normal nand_scan() function. It fills out
3557 * all the uninitialized function pointers with the defaults and scans for a
3558 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01003559 */
3560int nand_scan_tail(struct mtd_info *mtd)
3561{
3562 int i;
3563 struct nand_chip *chip = mtd->priv;
Huang Shijie97de79e02013-10-18 14:20:53 +08003564 struct nand_ecc_ctrl *ecc = &chip->ecc;
David Woodhouse3b85c322006-09-25 17:06:53 +01003565
Brian Norrise2414f42012-02-06 13:44:00 -08003566 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3567 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3568 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3569
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003570 if (!(chip->options & NAND_OWN_BUFFERS))
3571 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
3572 if (!chip->buffers)
3573 return -ENOMEM;
3574
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01003575 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01003576 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003577
3578 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003579 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003580 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003581 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003582 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08003584 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 break;
3586 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08003587 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 break;
3589 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08003590 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003592 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08003593 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 default:
Brian Norrisd0370212011-07-19 10:06:08 -07003596 pr_warn("No oob scheme defined for oobsize %d\n",
3597 mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 BUG();
3599 }
3600 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003601
David Woodhouse956e9442006-09-25 17:12:39 +01003602 if (!chip->write_page)
3603 chip->write_page = nand_write_page;
3604
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003605 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003606 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003607 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01003608 */
David Woodhouse956e9442006-09-25 17:12:39 +01003609
Huang Shijie97de79e02013-10-18 14:20:53 +08003610 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003611 case NAND_ECC_HW_OOB_FIRST:
3612 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08003613 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003614 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003615 "hardware ECC not possible\n");
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003616 BUG();
3617 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003618 if (!ecc->read_page)
3619 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07003620
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003621 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07003622 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003623 if (!ecc->read_page)
3624 ecc->read_page = nand_read_page_hwecc;
3625 if (!ecc->write_page)
3626 ecc->write_page = nand_write_page_hwecc;
3627 if (!ecc->read_page_raw)
3628 ecc->read_page_raw = nand_read_page_raw;
3629 if (!ecc->write_page_raw)
3630 ecc->write_page_raw = nand_write_page_raw;
3631 if (!ecc->read_oob)
3632 ecc->read_oob = nand_read_oob_std;
3633 if (!ecc->write_oob)
3634 ecc->write_oob = nand_write_oob_std;
3635 if (!ecc->read_subpage)
3636 ecc->read_subpage = nand_read_subpage;
3637 if (!ecc->write_subpage)
3638 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003639
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003640 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08003641 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3642 (!ecc->read_page ||
3643 ecc->read_page == nand_read_page_hwecc ||
3644 !ecc->write_page ||
3645 ecc->write_page == nand_write_page_hwecc)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003646 pr_warn("No ECC functions supplied; "
Brian Norrisd0370212011-07-19 10:06:08 -07003647 "hardware ECC not possible\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003648 BUG();
3649 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07003650 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08003651 if (!ecc->read_page)
3652 ecc->read_page = nand_read_page_syndrome;
3653 if (!ecc->write_page)
3654 ecc->write_page = nand_write_page_syndrome;
3655 if (!ecc->read_page_raw)
3656 ecc->read_page_raw = nand_read_page_raw_syndrome;
3657 if (!ecc->write_page_raw)
3658 ecc->write_page_raw = nand_write_page_raw_syndrome;
3659 if (!ecc->read_oob)
3660 ecc->read_oob = nand_read_oob_syndrome;
3661 if (!ecc->write_oob)
3662 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02003663
Huang Shijie97de79e02013-10-18 14:20:53 +08003664 if (mtd->writesize >= ecc->size) {
3665 if (!ecc->strength) {
Mike Dunne2788c92012-04-25 12:06:10 -07003666 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3667 BUG();
3668 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003669 break;
Mike Dunne2788c92012-04-25 12:06:10 -07003670 }
Brian Norris9a4d4d62011-07-19 10:06:07 -07003671 pr_warn("%d byte HW ECC not possible on "
Brian Norrisd0370212011-07-19 10:06:08 -07003672 "%d byte page size, fallback to SW ECC\n",
Huang Shijie97de79e02013-10-18 14:20:53 +08003673 ecc->size, mtd->writesize);
3674 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003676 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08003677 ecc->calculate = nand_calculate_ecc;
3678 ecc->correct = nand_correct_data;
3679 ecc->read_page = nand_read_page_swecc;
3680 ecc->read_subpage = nand_read_subpage;
3681 ecc->write_page = nand_write_page_swecc;
3682 ecc->read_page_raw = nand_read_page_raw;
3683 ecc->write_page_raw = nand_write_page_raw;
3684 ecc->read_oob = nand_read_oob_std;
3685 ecc->write_oob = nand_write_oob_std;
3686 if (!ecc->size)
3687 ecc->size = 256;
3688 ecc->bytes = 3;
3689 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003691
Ivan Djelic193bd402011-03-11 11:05:33 +01003692 case NAND_ECC_SOFT_BCH:
3693 if (!mtd_nand_has_bch()) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003694 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003695 BUG();
3696 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003697 ecc->calculate = nand_bch_calculate_ecc;
3698 ecc->correct = nand_bch_correct_data;
3699 ecc->read_page = nand_read_page_swecc;
3700 ecc->read_subpage = nand_read_subpage;
3701 ecc->write_page = nand_write_page_swecc;
3702 ecc->read_page_raw = nand_read_page_raw;
3703 ecc->write_page_raw = nand_write_page_raw;
3704 ecc->read_oob = nand_read_oob_std;
3705 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01003706 /*
3707 * Board driver should supply ecc.size and ecc.bytes values to
3708 * select how many bits are correctable; see nand_bch_init()
Brian Norris8b6e50c2011-05-25 14:59:01 -07003709 * for details. Otherwise, default to 4 bits for large page
3710 * devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01003711 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003712 if (!ecc->size && (mtd->oobsize >= 64)) {
3713 ecc->size = 512;
3714 ecc->bytes = 7;
Ivan Djelic193bd402011-03-11 11:05:33 +01003715 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003716 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
3717 &ecc->layout);
3718 if (!ecc->priv) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003719 pr_warn("BCH ECC initialization failed!\n");
Ivan Djelic193bd402011-03-11 11:05:33 +01003720 BUG();
3721 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003722 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Ivan Djelic193bd402011-03-11 11:05:33 +01003723 break;
3724
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003725 case NAND_ECC_NONE:
Brian Norris9a4d4d62011-07-19 10:06:07 -07003726 pr_warn("NAND_ECC_NONE selected by board driver. "
Brian Norrisd0370212011-07-19 10:06:08 -07003727 "This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08003728 ecc->read_page = nand_read_page_raw;
3729 ecc->write_page = nand_write_page_raw;
3730 ecc->read_oob = nand_read_oob_std;
3731 ecc->read_page_raw = nand_read_page_raw;
3732 ecc->write_page_raw = nand_write_page_raw;
3733 ecc->write_oob = nand_write_oob_std;
3734 ecc->size = mtd->writesize;
3735 ecc->bytes = 0;
3736 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 break;
David Woodhouse956e9442006-09-25 17:12:39 +01003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 default:
Huang Shijie97de79e02013-10-18 14:20:53 +08003740 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003741 BUG();
3742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Brian Norris9ce244b2011-08-30 18:45:37 -07003744 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08003745 if (!ecc->read_oob_raw)
3746 ecc->read_oob_raw = ecc->read_oob;
3747 if (!ecc->write_oob_raw)
3748 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07003749
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003750 /*
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003751 * The number of bytes available for a client to place data into
Brian Norris8b6e50c2011-05-25 14:59:01 -07003752 * the out of band area.
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003753 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003754 ecc->layout->oobavail = 0;
3755 for (i = 0; ecc->layout->oobfree[i].length
3756 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
3757 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
3758 mtd->oobavail = ecc->layout->oobavail;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02003759
3760 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003761 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07003762 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003763 */
Huang Shijie97de79e02013-10-18 14:20:53 +08003764 ecc->steps = mtd->writesize / ecc->size;
3765 if (ecc->steps * ecc->size != mtd->writesize) {
Brian Norris9a4d4d62011-07-19 10:06:07 -07003766 pr_warn("Invalid ECC parameters\n");
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02003767 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
Huang Shijie97de79e02013-10-18 14:20:53 +08003769 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003770
Brian Norris8b6e50c2011-05-25 14:59:01 -07003771 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003772 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08003773 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02003774 case 2:
3775 mtd->subpage_sft = 1;
3776 break;
3777 case 4:
3778 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01003779 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02003780 mtd->subpage_sft = 2;
3781 break;
3782 }
3783 }
3784 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
3785
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02003786 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003787 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003790 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003792 /* Large page NAND with SOFT_ECC should support subpage reads */
Huang Shijie97de79e02013-10-18 14:20:53 +08003793 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05003794 chip->options |= NAND_SUBPAGE_READ;
3795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08003797 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02003798 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3799 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02003800 mtd->_erase = nand_erase;
3801 mtd->_point = NULL;
3802 mtd->_unpoint = NULL;
3803 mtd->_read = nand_read;
3804 mtd->_write = nand_write;
3805 mtd->_panic_write = panic_nand_write;
3806 mtd->_read_oob = nand_read_oob;
3807 mtd->_write_oob = nand_write_oob;
3808 mtd->_sync = nand_sync;
3809 mtd->_lock = NULL;
3810 mtd->_unlock = NULL;
3811 mtd->_suspend = nand_suspend;
3812 mtd->_resume = nand_resume;
3813 mtd->_block_isbad = nand_block_isbad;
3814 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01003815 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Mike Dunn6a918ba2012-03-11 14:21:11 -07003817 /* propagate ecc info to mtd_info */
Huang Shijie97de79e02013-10-18 14:20:53 +08003818 mtd->ecclayout = ecc->layout;
3819 mtd->ecc_strength = ecc->strength;
3820 mtd->ecc_step_size = ecc->size;
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03003821 /*
3822 * Initialize bitflip_threshold to its default prior scan_bbt() call.
3823 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
3824 * properly set.
3825 */
3826 if (!mtd->bitflip_threshold)
3827 mtd->bitflip_threshold = mtd->ecc_strength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003829 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003830 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00003831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003834 return chip->scan_bbt(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003836EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Brian Norris8b6e50c2011-05-25 14:59:01 -07003838/*
3839 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003840 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07003841 * to call us from in-kernel code if the core NAND support is modular.
3842 */
David Woodhouse3b85c322006-09-25 17:06:53 +01003843#ifdef MODULE
3844#define caller_is_module() (1)
3845#else
3846#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06003847 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01003848#endif
3849
3850/**
3851 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003852 * @mtd: MTD device structure
3853 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01003854 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003855 * This fills out all the uninitialized function pointers with the defaults.
3856 * The flash ID is read and the mtd/chip structures are filled with the
3857 * appropriate values. The mtd->owner field must be set to the module of the
3858 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01003859 */
3860int nand_scan(struct mtd_info *mtd, int maxchips)
3861{
3862 int ret;
3863
3864 /* Many callers got this wrong, so check for it for a while... */
3865 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07003866 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01003867 BUG();
3868 }
3869
David Woodhouse5e81e882010-02-26 18:32:56 +00003870 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01003871 if (!ret)
3872 ret = nand_scan_tail(mtd);
3873 return ret;
3874}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003875EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01003876
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00003878 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003879 * @mtd: MTD device structure
3880 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003881void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882{
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003883 struct nand_chip *chip = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Ivan Djelic193bd402011-03-11 11:05:33 +01003885 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3886 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3887
Jamie Iles5ffcaf32011-05-23 10:22:46 +01003888 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Jesper Juhlfa671642005-11-07 01:01:27 -08003890 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003891 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01003892 if (!(chip->options & NAND_OWN_BUFFERS))
3893 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07003894
3895 /* Free bad block descriptor memory */
3896 if (chip->badblock_pattern && chip->badblock_pattern->options
3897 & NAND_BBT_DYNAMICSTRUCT)
3898 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
David Woodhousee0c7d762006-05-13 18:07:53 +01003900EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08003901
3902static int __init nand_base_init(void)
3903{
3904 led_trigger_register_simple("nand-disk", &nand_led_trigger);
3905 return 0;
3906}
3907
3908static void __exit nand_base_exit(void)
3909{
3910 led_trigger_unregister_simple(nand_led_trigger);
3911}
3912
3913module_init(nand_base_init);
3914module_exit(nand_base_exit);
3915
David Woodhousee0c7d762006-05-13 18:07:53 +01003916MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003917MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
3918MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01003919MODULE_DESCRIPTION("Generic NAND flash driver code");