blob: 24ed4450df570ce714856e8edc98a5bc24ab1f23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Additional technical information is available on
maximilian attems8b2b4032007-07-28 13:07:16 +02007 * http://www.linux-mtd.infradead.org/doc/nand.html
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020012 * Credits:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * David Woodhouse for adding multichip support
14 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +020018 * TODO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Brian Norris7854d3f2011-06-23 14:12:08 -070021 * if we have HW ECC support.
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +030022 * BBT table is not serialized, has to be fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Ezequiel Garcia20171642013-11-25 08:30:31 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
David Woodhouse552d9202006-05-14 01:20:46 +010032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/delay.h>
34#include <linux/errno.h>
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +020035#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sched.h>
37#include <linux/slab.h>
Kamal Dasu66507c72014-05-01 20:51:19 -040038#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/types.h>
40#include <linux/mtd/mtd.h>
41#include <linux/mtd/nand.h>
42#include <linux/mtd/nand_ecc.h>
Ivan Djelic193bd402011-03-11 11:05:33 +010043#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/interrupt.h>
45#include <linux/bitops.h>
Florian Fainelli7351d3a2010-09-07 13:23:45 +020046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mtd/partitions.h>
Brian Norris5844fee2015-01-23 00:22:27 -080048#include <linux/of_mtd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Define default oob placement schemes for large and small page devices */
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020051static struct nand_ecclayout nand_oob_8 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .eccbytes = 3,
53 .eccpos = {0, 1, 2},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020054 .oobfree = {
55 {.offset = 3,
56 .length = 2},
57 {.offset = 6,
Florian Fainellif8ac0412010-09-07 13:23:43 +020058 .length = 2} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020061static struct nand_ecclayout nand_oob_16 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .eccbytes = 6,
63 .eccpos = {0, 1, 2, 3, 6, 7},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020064 .oobfree = {
65 {.offset = 8,
Florian Fainellif8ac0412010-09-07 13:23:43 +020066 . length = 8} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069static struct nand_ecclayout nand_oob_64 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .eccbytes = 24,
71 .eccpos = {
David Woodhousee0c7d762006-05-13 18:07:53 +010072 40, 41, 42, 43, 44, 45, 46, 47,
73 48, 49, 50, 51, 52, 53, 54, 55,
74 56, 57, 58, 59, 60, 61, 62, 63},
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020075 .oobfree = {
76 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020077 .length = 38} }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Thomas Gleixner81ec5362007-12-12 17:27:03 +010080static struct nand_ecclayout nand_oob_128 = {
81 .eccbytes = 48,
82 .eccpos = {
83 80, 81, 82, 83, 84, 85, 86, 87,
84 88, 89, 90, 91, 92, 93, 94, 95,
85 96, 97, 98, 99, 100, 101, 102, 103,
86 104, 105, 106, 107, 108, 109, 110, 111,
87 112, 113, 114, 115, 116, 117, 118, 119,
88 120, 121, 122, 123, 124, 125, 126, 127},
89 .oobfree = {
90 {.offset = 2,
Florian Fainellif8ac0412010-09-07 13:23:43 +020091 .length = 78} }
Thomas Gleixner81ec5362007-12-12 17:27:03 +010092};
93
Huang Shijie6a8214a2012-11-19 14:43:30 +080094static int nand_get_device(struct mtd_info *mtd, int new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020096static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
97 struct mtd_oob_ops *ops);
98
Vimal Singh6fe5a6a2010-02-03 14:12:24 +053099static int check_offs_len(struct mtd_info *mtd,
100 loff_t ofs, uint64_t len)
101{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100102 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530103 int ret = 0;
104
105 /* Start address must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300106 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700107 pr_debug("%s: unaligned address\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530108 ret = -EINVAL;
109 }
110
111 /* Length must align on block boundary */
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300112 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
Brian Norris289c0522011-07-19 10:06:09 -0700113 pr_debug("%s: length not block aligned\n", __func__);
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530114 ret = -EINVAL;
115 }
116
Vimal Singh6fe5a6a2010-02-03 14:12:24 +0530117 return ret;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/**
121 * nand_release_device - [GENERIC] release chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700122 * @mtd: MTD device structure
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000123 *
Huang Shijieb0bb6902012-11-19 14:43:29 +0800124 * Release chip lock and wake up anyone waiting on the device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100126static void nand_release_device(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100128 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200130 /* Release the controller and the chip */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200131 spin_lock(&chip->controller->lock);
132 chip->controller->active = NULL;
133 chip->state = FL_READY;
134 wake_up(&chip->controller->wq);
135 spin_unlock(&chip->controller->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138/**
139 * nand_read_byte - [DEFAULT] read one byte from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700140 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700142 * Default read function for 8bit buswidth
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200144static uint8_t nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100146 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200147 return readb(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150/**
Masanari Iida064a7692012-11-09 23:20:58 +0900151 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700152 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700154 * Default read function for 16bit buswidth with endianness conversion.
155 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200157static uint8_t nand_read_byte16(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100159 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200160 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * nand_read_word - [DEFAULT] read one word from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700165 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700167 * Default read function for 16bit buswidth without endianness conversion.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
169static u16 nand_read_word(struct mtd_info *mtd)
170{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100171 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200172 return readw(chip->IO_ADDR_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * nand_select_chip - [DEFAULT] control CE line
Brian Norris8b6e50c2011-05-25 14:59:01 -0700177 * @mtd: MTD device structure
178 * @chipnr: chipnumber to select, -1 for deselect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 *
180 * Default select function for 1 chip devices.
181 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200182static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100184 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200185
186 switch (chipnr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 case -1:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200188 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 break;
192
193 default:
194 BUG();
195 }
196}
197
198/**
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100199 * nand_write_byte - [DEFAULT] write single byte to chip
200 * @mtd: MTD device structure
201 * @byte: value to write
202 *
203 * Default function to write a byte to I/O[7:0]
204 */
205static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
206{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100207 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100208
209 chip->write_buf(mtd, &byte, 1);
210}
211
212/**
213 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
214 * @mtd: MTD device structure
215 * @byte: value to write
216 *
217 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
218 */
219static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
220{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100221 struct nand_chip *chip = mtd_to_nand(mtd);
Uwe Kleine-König05f78352013-12-05 22:22:04 +0100222 uint16_t word = byte;
223
224 /*
225 * It's not entirely clear what should happen to I/O[15:8] when writing
226 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
227 *
228 * When the host supports a 16-bit bus width, only data is
229 * transferred at the 16-bit width. All address and command line
230 * transfers shall use only the lower 8-bits of the data bus. During
231 * command transfers, the host may place any value on the upper
232 * 8-bits of the data bus. During address transfers, the host shall
233 * set the upper 8-bits of the data bus to 00h.
234 *
235 * One user of the write_byte callback is nand_onfi_set_features. The
236 * four parameters are specified to be written to I/O[7:0], but this is
237 * neither an address nor a command transfer. Let's assume a 0 on the
238 * upper I/O lines is OK.
239 */
240 chip->write_buf(mtd, (uint8_t *)&word, 2);
241}
242
243/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 * nand_write_buf - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700245 * @mtd: MTD device structure
246 * @buf: data buffer
247 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700249 * Default write function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200251static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100253 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Alexander Shiyan76413832013-04-13 09:32:13 +0400255 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000259 * nand_read_buf - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700260 * @mtd: MTD device structure
261 * @buf: buffer to store date
262 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700264 * Default read function for 8bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200266static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100268 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Alexander Shiyan76413832013-04-13 09:32:13 +0400270 ioread8_rep(chip->IO_ADDR_R, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * nand_write_buf16 - [DEFAULT] write buffer to chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700275 * @mtd: MTD device structure
276 * @buf: data buffer
277 * @len: number of bytes to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700279 * Default write function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200281static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100283 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 u16 *p = (u16 *) buf;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000285
Alexander Shiyan76413832013-04-13 09:32:13 +0400286 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000290 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700291 * @mtd: MTD device structure
292 * @buf: buffer to store date
293 * @len: number of bytes to read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700295 * Default read function for 16bit buswidth.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Thomas Gleixner58dd8f2b2006-05-23 11:52:35 +0200297static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100299 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 u16 *p = (u16 *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Alexander Shiyan76413832013-04-13 09:32:13 +0400302 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Brian Norris8b6e50c2011-05-25 14:59:01 -0700307 * @mtd: MTD device structure
308 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310 * Check, if the block is bad.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530312static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Archit Taneja9f3e0422016-02-03 14:29:49 +0530314 int page, res = 0, i = 0;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100315 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 u16 bad;
317
Brian Norris5fb15492011-05-31 16:31:21 -0700318 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700319 ofs += mtd->erasesize - mtd->writesize;
320
Thomas Knobloch1a12f462007-05-03 07:39:37 +0100321 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
322
Brian Norriscdbec052012-01-13 18:11:48 -0800323 do {
324 if (chip->options & NAND_BUSWIDTH_16) {
325 chip->cmdfunc(mtd, NAND_CMD_READOOB,
326 chip->badblockpos & 0xFE, page);
327 bad = cpu_to_le16(chip->read_word(mtd));
328 if (chip->badblockpos & 0x1)
329 bad >>= 8;
330 else
331 bad &= 0xFF;
332 } else {
333 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
334 page);
335 bad = chip->read_byte(mtd);
336 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000337
Brian Norriscdbec052012-01-13 18:11:48 -0800338 if (likely(chip->badblockbits == 8))
339 res = bad != 0xFF;
340 else
341 res = hweight8(bad) < chip->badblockbits;
342 ofs += mtd->writesize;
343 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
344 i++;
345 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Maxim Levitskye0b58d02010-02-22 20:39:38 +0200346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return res;
348}
349
350/**
Brian Norris5a0edb22013-07-30 17:52:58 -0700351 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Brian Norris8b6e50c2011-05-25 14:59:01 -0700352 * @mtd: MTD device structure
353 * @ofs: offset from device start
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700355 * This is the default implementation, which can be overridden by a hardware
Brian Norris5a0edb22013-07-30 17:52:58 -0700356 * specific driver. It provides the details for writing a bad block marker to a
357 * block.
358 */
359static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
360{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100361 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris5a0edb22013-07-30 17:52:58 -0700362 struct mtd_oob_ops ops;
363 uint8_t buf[2] = { 0, 0 };
364 int ret = 0, res, i = 0;
365
Brian Norris0ec56dc2015-02-28 02:02:30 -0800366 memset(&ops, 0, sizeof(ops));
Brian Norris5a0edb22013-07-30 17:52:58 -0700367 ops.oobbuf = buf;
368 ops.ooboffs = chip->badblockpos;
369 if (chip->options & NAND_BUSWIDTH_16) {
370 ops.ooboffs &= ~0x01;
371 ops.len = ops.ooblen = 2;
372 } else {
373 ops.len = ops.ooblen = 1;
374 }
375 ops.mode = MTD_OPS_PLACE_OOB;
376
377 /* Write to first/last page(s) if necessary */
378 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
379 ofs += mtd->erasesize - mtd->writesize;
380 do {
381 res = nand_do_write_oob(mtd, ofs, &ops);
382 if (!ret)
383 ret = res;
384
385 i++;
386 ofs += mtd->writesize;
387 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
388
389 return ret;
390}
391
392/**
393 * nand_block_markbad_lowlevel - mark a block bad
394 * @mtd: MTD device structure
395 * @ofs: offset from device start
396 *
397 * This function performs the generic NAND bad block marking steps (i.e., bad
398 * block table(s) and/or marker(s)). We only allow the hardware driver to
399 * specify how to write bad block markers to OOB (chip->block_markbad).
400 *
Brian Norrisb32843b2013-07-30 17:52:59 -0700401 * We try operations in the following order:
Brian Norrise2414f42012-02-06 13:44:00 -0800402 * (1) erase the affected block, to allow OOB marker to be written cleanly
Brian Norrisb32843b2013-07-30 17:52:59 -0700403 * (2) write bad block marker to OOB area of affected block (unless flag
404 * NAND_BBT_NO_OOB_BBM is present)
405 * (3) update the BBT
406 * Note that we retain the first error encountered in (2) or (3), finish the
Brian Norrise2414f42012-02-06 13:44:00 -0800407 * procedures, and dump the error in the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408*/
Brian Norris5a0edb22013-07-30 17:52:58 -0700409static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100411 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisb32843b2013-07-30 17:52:59 -0700412 int res, ret = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000413
Brian Norrisb32843b2013-07-30 17:52:59 -0700414 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Brian Norris00918422012-01-13 18:11:47 -0800415 struct erase_info einfo;
416
417 /* Attempt erase before marking OOB */
418 memset(&einfo, 0, sizeof(einfo));
419 einfo.mtd = mtd;
420 einfo.addr = ofs;
Dan Carpenterdaae74c2013-08-09 12:49:05 +0300421 einfo.len = 1ULL << chip->phys_erase_shift;
Brian Norris00918422012-01-13 18:11:47 -0800422 nand_erase_nand(mtd, &einfo, 0);
Brian Norris00918422012-01-13 18:11:47 -0800423
Brian Norrisb32843b2013-07-30 17:52:59 -0700424 /* Write bad block marker to OOB */
Huang Shijie6a8214a2012-11-19 14:43:30 +0800425 nand_get_device(mtd, FL_WRITING);
Brian Norris5a0edb22013-07-30 17:52:58 -0700426 ret = chip->block_markbad(mtd, ofs);
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300427 nand_release_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200428 }
Brian Norrise2414f42012-02-06 13:44:00 -0800429
Brian Norrisb32843b2013-07-30 17:52:59 -0700430 /* Mark block bad in BBT */
431 if (chip->bbt) {
432 res = nand_markbad_bbt(mtd, ofs);
Brian Norrise2414f42012-02-06 13:44:00 -0800433 if (!ret)
434 ret = res;
435 }
436
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200437 if (!ret)
438 mtd->ecc_stats.badblocks++;
Artem Bityutskiyc0b8ba72007-07-23 16:06:50 +0300439
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200440 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000443/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * nand_check_wp - [GENERIC] check if the chip is write protected
Brian Norris8b6e50c2011-05-25 14:59:01 -0700445 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700447 * Check, if the device is write protected. The function expects, that the
448 * device is already selected.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100450static int nand_check_wp(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100452 struct nand_chip *chip = mtd_to_nand(mtd);
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200453
Brian Norris8b6e50c2011-05-25 14:59:01 -0700454 /* Broken xD cards report WP despite being writable */
Maxim Levitsky93edbad2010-02-22 20:39:40 +0200455 if (chip->options & NAND_BROKEN_XD)
456 return 0;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* Check the WP bit */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200459 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
460 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/**
Gu Zhengc30e1f72014-09-03 17:49:10 +0800464 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700465 * @mtd: MTD device structure
466 * @ofs: offset from device start
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300467 *
Gu Zhengc30e1f72014-09-03 17:49:10 +0800468 * Check if the block is marked as reserved.
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300469 */
470static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
471{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100472 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300473
474 if (!chip->bbt)
475 return 0;
476 /* Return info from the table */
477 return nand_isreserved_bbt(mtd, ofs);
478}
479
480/**
481 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
482 * @mtd: MTD device structure
483 * @ofs: offset from device start
Brian Norris8b6e50c2011-05-25 14:59:01 -0700484 * @allowbbt: 1, if its allowed to access the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *
486 * Check, if the block is bad. Either by reading the bad block table or
487 * calling of the scan function.
488 */
Archit Taneja9f3e0422016-02-03 14:29:49 +0530489static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100491 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000492
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200493 if (!chip->bbt)
Archit Taneja9f3e0422016-02-03 14:29:49 +0530494 return chip->block_bad(mtd, ofs);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Return info from the table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100497 return nand_isbad_bbt(mtd, ofs, allowbbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200500/**
501 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700502 * @mtd: MTD device structure
503 * @timeo: Timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200504 *
505 * Helper function for nand_wait_ready used when needing to wait in interrupt
506 * context.
507 */
508static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
509{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100510 struct nand_chip *chip = mtd_to_nand(mtd);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200511 int i;
512
513 /* Wait for the device to get ready */
514 for (i = 0; i < timeo; i++) {
515 if (chip->dev_ready(mtd))
516 break;
517 touch_softlockup_watchdog();
518 mdelay(1);
519 }
520}
521
Alex Smithb70af9b2015-10-06 14:52:07 +0100522/**
523 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
524 * @mtd: MTD device structure
525 *
526 * Wait for the ready pin after a command, and warn if a timeout occurs.
527 */
David Woodhouse4b648b02006-09-25 17:05:24 +0100528void nand_wait_ready(struct mtd_info *mtd)
Thomas Gleixner3b887752005-02-22 21:56:49 +0000529{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100530 struct nand_chip *chip = mtd_to_nand(mtd);
Alex Smithb70af9b2015-10-06 14:52:07 +0100531 unsigned long timeo = 400;
Thomas Gleixner3b887752005-02-22 21:56:49 +0000532
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200533 if (in_interrupt() || oops_in_progress)
Alex Smithb70af9b2015-10-06 14:52:07 +0100534 return panic_nand_wait_ready(mtd, timeo);
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200535
Brian Norris7854d3f2011-06-23 14:12:08 -0700536 /* Wait until command is processed or timeout occurs */
Alex Smithb70af9b2015-10-06 14:52:07 +0100537 timeo = jiffies + msecs_to_jiffies(timeo);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000538 do {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200539 if (chip->dev_ready(mtd))
Ezequiel Garcia4c7e0542016-04-12 17:46:41 -0300540 return;
Alex Smithb70af9b2015-10-06 14:52:07 +0100541 cond_resched();
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000542 } while (time_before(jiffies, timeo));
Alex Smithb70af9b2015-10-06 14:52:07 +0100543
Brian Norris9ebfdf52016-03-04 17:19:23 -0800544 if (!chip->dev_ready(mtd))
545 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
Thomas Gleixner3b887752005-02-22 21:56:49 +0000546}
David Woodhouse4b648b02006-09-25 17:05:24 +0100547EXPORT_SYMBOL_GPL(nand_wait_ready);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Roger Quadros60c70d62015-02-23 17:26:39 +0200550 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
551 * @mtd: MTD device structure
552 * @timeo: Timeout in ms
553 *
554 * Wait for status ready (i.e. command done) or timeout.
555 */
556static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
557{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100558 register struct nand_chip *chip = mtd_to_nand(mtd);
Roger Quadros60c70d62015-02-23 17:26:39 +0200559
560 timeo = jiffies + msecs_to_jiffies(timeo);
561 do {
562 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
563 break;
564 touch_softlockup_watchdog();
565 } while (time_before(jiffies, timeo));
566};
567
568/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 * nand_command - [DEFAULT] Send command to NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700570 * @mtd: MTD device structure
571 * @command: the command to be sent
572 * @column: the column address for this command, -1 if none
573 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700575 * Send command to NAND device. This function is used for small page devices
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200576 * (512 Bytes per page).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200578static void nand_command(struct mtd_info *mtd, unsigned int command,
579 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100581 register struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200582 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Brian Norris8b6e50c2011-05-25 14:59:01 -0700584 /* Write out the command to the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (command == NAND_CMD_SEQIN) {
586 int readcmd;
587
Joern Engel28318772006-05-22 23:18:05 +0200588 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200590 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 readcmd = NAND_CMD_READOOB;
592 } else if (column < 256) {
593 /* First 256 bytes --> READ0 */
594 readcmd = NAND_CMD_READ0;
595 } else {
596 column -= 256;
597 readcmd = NAND_CMD_READ1;
598 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200599 chip->cmd_ctrl(mtd, readcmd, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200600 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200602 chip->cmd_ctrl(mtd, command, ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Brian Norris8b6e50c2011-05-25 14:59:01 -0700604 /* Address cycle, when necessary */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200605 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
606 /* Serially input address */
607 if (column != -1) {
608 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800609 if (chip->options & NAND_BUSWIDTH_16 &&
610 !nand_opcode_8bits(command))
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200611 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200612 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200613 ctrl &= ~NAND_CTRL_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200615 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200616 chip->cmd_ctrl(mtd, page_addr, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200617 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200618 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 /* One more address cycle for devices > 32MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200620 if (chip->chipsize > (32 << 20))
621 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200622 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200623 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000624
625 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 * Program and erase have their own busy handlers status and sequential
627 * in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 case NAND_CMD_PAGEPROG:
632 case NAND_CMD_ERASE1:
633 case NAND_CMD_ERASE2:
634 case NAND_CMD_SEQIN:
635 case NAND_CMD_STATUS:
636 return;
637
638 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200639 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200641 udelay(chip->chip_delay);
642 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200643 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200644 chip->cmd_ctrl(mtd,
645 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200646 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
647 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return;
649
David Woodhousee0c7d762006-05-13 18:07:53 +0100650 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000652 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * If we don't have access to the busy pin, we apply the given
654 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100655 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200656 if (!chip->dev_ready) {
657 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700661 /*
662 * Apply this short delay always to ensure that we do wait tWB in
663 * any case on any machine.
664 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100665 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000666
667 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670/**
671 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700672 * @mtd: MTD device structure
673 * @command: the command to be sent
674 * @column: the column address for this command, -1 if none
675 * @page_addr: the page address for this command, -1 if none
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 *
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200677 * Send command to NAND device. This is the version for the new large page
Brian Norris7854d3f2011-06-23 14:12:08 -0700678 * devices. We don't have the separate regions as we have in the small page
679 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200681static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
682 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100684 register struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Emulate NAND_CMD_READOOB */
687 if (command == NAND_CMD_READOOB) {
Joern Engel28318772006-05-22 23:18:05 +0200688 column += mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 command = NAND_CMD_READ0;
690 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000691
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200692 /* Command latch cycle */
Alexander Shiyanfb066ad2013-02-28 12:02:19 +0400693 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200696 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* Serially input address */
699 if (column != -1) {
700 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800701 if (chip->options & NAND_BUSWIDTH_16 &&
702 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 column >>= 1;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200704 chip->cmd_ctrl(mtd, column, ctrl);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200705 ctrl &= ~NAND_CTRL_CHANGE;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200706 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (page_addr != -1) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200709 chip->cmd_ctrl(mtd, page_addr, ctrl);
710 chip->cmd_ctrl(mtd, page_addr >> 8,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200711 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* One more address cycle for devices > 128MiB */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200713 if (chip->chipsize > (128 << 20))
714 chip->cmd_ctrl(mtd, page_addr >> 16,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200715 NAND_NCE | NAND_ALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200718 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000719
720 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700721 * Program and erase have their own busy handlers status, sequential
Gerhard Sittig7a442f12014-03-29 14:36:22 +0100722 * in and status need no delay.
David A. Marlin30f464b2005-01-17 18:35:25 +0000723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 switch (command) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 case NAND_CMD_CACHEDPROG:
727 case NAND_CMD_PAGEPROG:
728 case NAND_CMD_ERASE1:
729 case NAND_CMD_ERASE2:
730 case NAND_CMD_SEQIN:
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200731 case NAND_CMD_RNDIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case NAND_CMD_STATUS:
David A. Marlin30f464b2005-01-17 18:35:25 +0000733 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 case NAND_CMD_RESET:
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200736 if (chip->dev_ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200738 udelay(chip->chip_delay);
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200739 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
740 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
741 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
742 NAND_NCE | NAND_CTRL_CHANGE);
Roger Quadros60c70d62015-02-23 17:26:39 +0200743 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
744 nand_wait_status_ready(mtd, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return;
746
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200747 case NAND_CMD_RNDOUT:
748 /* No ready / busy check necessary */
749 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
750 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
751 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
752 NAND_NCE | NAND_CTRL_CHANGE);
753 return;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 case NAND_CMD_READ0:
Thomas Gleixner12efdde2006-05-24 22:57:09 +0200756 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
757 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
758 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
759 NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000760
David Woodhousee0c7d762006-05-13 18:07:53 +0100761 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000763 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * If we don't have access to the busy pin, we apply the given
Brian Norris8b6e50c2011-05-25 14:59:01 -0700765 * command delay.
David Woodhousee0c7d762006-05-13 18:07:53 +0100766 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200767 if (!chip->dev_ready) {
768 udelay(chip->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Thomas Gleixner3b887752005-02-22 21:56:49 +0000772
Brian Norris8b6e50c2011-05-25 14:59:01 -0700773 /*
774 * Apply this short delay always to ensure that we do wait tWB in
775 * any case on any machine.
776 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100777 ndelay(100);
Thomas Gleixner3b887752005-02-22 21:56:49 +0000778
779 nand_wait_ready(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200783 * panic_nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700784 * @chip: the nand chip descriptor
785 * @mtd: MTD device structure
786 * @new_state: the state which is requested
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200787 *
788 * Used when in panic, no locks are taken.
789 */
790static void panic_nand_get_device(struct nand_chip *chip,
791 struct mtd_info *mtd, int new_state)
792{
Brian Norris7854d3f2011-06-23 14:12:08 -0700793 /* Hardware controller shared among independent devices */
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200794 chip->controller->active = chip;
795 chip->state = new_state;
796}
797
798/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * nand_get_device - [GENERIC] Get chip for selected access
Brian Norris8b6e50c2011-05-25 14:59:01 -0700800 * @mtd: MTD device structure
801 * @new_state: the state which is requested
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
803 * Get the device and lock it for exclusive access
804 */
Thomas Gleixner2c0a2be2006-05-23 11:50:56 +0200805static int
Huang Shijie6a8214a2012-11-19 14:43:30 +0800806nand_get_device(struct mtd_info *mtd, int new_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100808 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200809 spinlock_t *lock = &chip->controller->lock;
810 wait_queue_head_t *wq = &chip->controller->wq;
David Woodhousee0c7d762006-05-13 18:07:53 +0100811 DECLARE_WAITQUEUE(wait, current);
Florian Fainelli7351d3a2010-09-07 13:23:45 +0200812retry:
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100813 spin_lock(lock);
814
vimal singhb8b3ee92009-07-09 20:41:22 +0530815 /* Hardware controller shared among independent devices */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200816 if (!chip->controller->active)
817 chip->controller->active = chip;
Thomas Gleixnera36ed292006-05-23 11:37:03 +0200818
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200819 if (chip->controller->active == chip && chip->state == FL_READY) {
820 chip->state = new_state;
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100821 spin_unlock(lock);
Vitaly Wool962034f2005-09-15 14:58:53 +0100822 return 0;
823 }
824 if (new_state == FL_PM_SUSPENDED) {
Li Yang6b0d9a82009-11-17 14:45:49 -0800825 if (chip->controller->active->state == FL_PM_SUSPENDED) {
826 chip->state = FL_PM_SUSPENDED;
827 spin_unlock(lock);
828 return 0;
Li Yang6b0d9a82009-11-17 14:45:49 -0800829 }
Thomas Gleixner0dfc6242005-05-31 20:39:20 +0100830 }
831 set_current_state(TASK_UNINTERRUPTIBLE);
832 add_wait_queue(wq, &wait);
833 spin_unlock(lock);
834 schedule();
835 remove_wait_queue(wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto retry;
837}
838
839/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700840 * panic_nand_wait - [GENERIC] wait until the command is done
841 * @mtd: MTD device structure
842 * @chip: NAND chip structure
843 * @timeo: timeout
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200844 *
845 * Wait for command done. This is a helper function for nand_wait used when
846 * we are in interrupt context. May happen when in panic and trying to write
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400847 * an oops through mtdoops.
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200848 */
849static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
850 unsigned long timeo)
851{
852 int i;
853 for (i = 0; i < timeo; i++) {
854 if (chip->dev_ready) {
855 if (chip->dev_ready(mtd))
856 break;
857 } else {
858 if (chip->read_byte(mtd) & NAND_STATUS_READY)
859 break;
860 }
861 mdelay(1);
Florian Fainellif8ac0412010-09-07 13:23:43 +0200862 }
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200863}
864
865/**
Brian Norris8b6e50c2011-05-25 14:59:01 -0700866 * nand_wait - [DEFAULT] wait until the command is done
867 * @mtd: MTD device structure
868 * @chip: NAND chip structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 *
Alex Smithb70af9b2015-10-06 14:52:07 +0100870 * Wait for command done. This applies to erase and program only.
Randy Dunlap844d3b42006-06-28 21:48:27 -0700871 */
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200872static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874
Alex Smithb70af9b2015-10-06 14:52:07 +0100875 int status;
876 unsigned long timeo = 400;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Brian Norris8b6e50c2011-05-25 14:59:01 -0700878 /*
879 * Apply this short delay always to ensure that we do wait tWB in any
880 * case on any machine.
881 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100882 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Artem Bityutskiy14c65782013-03-04 14:21:34 +0200884 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200886 if (in_interrupt() || oops_in_progress)
887 panic_nand_wait(mtd, chip, timeo);
888 else {
Huang Shijie6d2559f2013-01-30 10:03:56 +0800889 timeo = jiffies + msecs_to_jiffies(timeo);
Alex Smithb70af9b2015-10-06 14:52:07 +0100890 do {
Simon Kagstrom2af7c652009-10-05 15:55:52 +0200891 if (chip->dev_ready) {
892 if (chip->dev_ready(mtd))
893 break;
894 } else {
895 if (chip->read_byte(mtd) & NAND_STATUS_READY)
896 break;
897 }
898 cond_resched();
Alex Smithb70af9b2015-10-06 14:52:07 +0100899 } while (time_before(jiffies, timeo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Richard Purdie8fe833c2006-03-31 02:31:14 -0800901
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +0200902 status = (int)chip->read_byte(mtd);
Matthieu CASTETf251b8d2012-11-05 15:00:44 +0100903 /* This can happen if in case of timeout or buggy dev_ready */
904 WARN_ON(!(status & NAND_STATUS_READY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return status;
906}
907
908/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700909 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700910 * @mtd: mtd info
911 * @ofs: offset to start unlock from
912 * @len: length to unlock
Brian Norris8b6e50c2011-05-25 14:59:01 -0700913 * @invert: when = 0, unlock the range of blocks within the lower and
914 * upper boundary address
915 * when = 1, unlock the range of blocks outside the boundaries
916 * of the lower and upper boundary address
Vimal Singh7d70f332010-02-08 15:50:49 +0530917 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700918 * Returs unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530919 */
920static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
921 uint64_t len, int invert)
922{
923 int ret = 0;
924 int status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100925 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +0530926
927 /* Submit address of first page to unlock */
928 page = ofs >> chip->page_shift;
929 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
930
931 /* Submit address of last page to unlock */
932 page = (ofs + len) >> chip->page_shift;
933 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
934 (page | invert) & chip->pagemask);
935
936 /* Call wait ready function */
937 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +0530938 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -0400939 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -0700940 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530941 __func__, status);
942 ret = -EIO;
943 }
944
945 return ret;
946}
947
948/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700949 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
Randy Dunlapb6d676d2010-08-10 18:02:50 -0700950 * @mtd: mtd info
951 * @ofs: offset to start unlock from
952 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +0530953 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700954 * Returns unlock status.
Vimal Singh7d70f332010-02-08 15:50:49 +0530955 */
956int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
957{
958 int ret = 0;
959 int chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +0100960 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +0530961
Brian Norris289c0522011-07-19 10:06:09 -0700962 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530963 __func__, (unsigned long long)ofs, len);
964
965 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -0800966 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +0530967
968 /* Align to last block address if size addresses end of the device */
969 if (ofs + len == mtd->size)
970 len -= mtd->erasesize;
971
Huang Shijie6a8214a2012-11-19 14:43:30 +0800972 nand_get_device(mtd, FL_UNLOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +0530973
974 /* Shift to get chip number */
975 chipnr = ofs >> chip->chip_shift;
976
977 chip->select_chip(mtd, chipnr);
978
White Ding57d3a9a2014-07-24 00:10:45 +0800979 /*
980 * Reset the chip.
981 * If we want to check the WP through READ STATUS and check the bit 7
982 * we must reset the chip
983 * some operation can also clear the bit 7 of status register
984 * eg. erase/program a locked block
985 */
986 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
987
Vimal Singh7d70f332010-02-08 15:50:49 +0530988 /* Check, if it is write protected */
989 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -0700990 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +0530991 __func__);
992 ret = -EIO;
993 goto out;
994 }
995
996 ret = __nand_unlock(mtd, ofs, len, 0);
997
998out:
Huang Shijieb0bb6902012-11-19 14:43:29 +0800999 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301000 nand_release_device(mtd);
1001
1002 return ret;
1003}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001004EXPORT_SYMBOL(nand_unlock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301005
1006/**
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001007 * nand_lock - [REPLACEABLE] locks all blocks present in the device
Randy Dunlapb6d676d2010-08-10 18:02:50 -07001008 * @mtd: mtd info
1009 * @ofs: offset to start unlock from
1010 * @len: length to unlock
Vimal Singh7d70f332010-02-08 15:50:49 +05301011 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001012 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1013 * have this feature, but it allows only to lock all blocks, not for specified
1014 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1015 * now.
Vimal Singh7d70f332010-02-08 15:50:49 +05301016 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001017 * Returns lock status.
Vimal Singh7d70f332010-02-08 15:50:49 +05301018 */
1019int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1020{
1021 int ret = 0;
1022 int chipnr, status, page;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001023 struct nand_chip *chip = mtd_to_nand(mtd);
Vimal Singh7d70f332010-02-08 15:50:49 +05301024
Brian Norris289c0522011-07-19 10:06:09 -07001025 pr_debug("%s: start = 0x%012llx, len = %llu\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301026 __func__, (unsigned long long)ofs, len);
1027
1028 if (check_offs_len(mtd, ofs, len))
Brian Norrisb1a23482015-02-28 02:02:27 -08001029 return -EINVAL;
Vimal Singh7d70f332010-02-08 15:50:49 +05301030
Huang Shijie6a8214a2012-11-19 14:43:30 +08001031 nand_get_device(mtd, FL_LOCKING);
Vimal Singh7d70f332010-02-08 15:50:49 +05301032
1033 /* Shift to get chip number */
1034 chipnr = ofs >> chip->chip_shift;
1035
1036 chip->select_chip(mtd, chipnr);
1037
White Ding57d3a9a2014-07-24 00:10:45 +08001038 /*
1039 * Reset the chip.
1040 * If we want to check the WP through READ STATUS and check the bit 7
1041 * we must reset the chip
1042 * some operation can also clear the bit 7 of status register
1043 * eg. erase/program a locked block
1044 */
1045 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1046
Vimal Singh7d70f332010-02-08 15:50:49 +05301047 /* Check, if it is write protected */
1048 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07001049 pr_debug("%s: device is write protected!\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301050 __func__);
1051 status = MTD_ERASE_FAILED;
1052 ret = -EIO;
1053 goto out;
1054 }
1055
1056 /* Submit address of first page to lock */
1057 page = ofs >> chip->page_shift;
1058 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1059
1060 /* Call wait ready function */
1061 status = chip->waitfunc(mtd, chip);
Vimal Singh7d70f332010-02-08 15:50:49 +05301062 /* See if device thinks it succeeded */
Huang Shijie74830962012-10-14 23:47:24 -04001063 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07001064 pr_debug("%s: error status = 0x%08x\n",
Vimal Singh7d70f332010-02-08 15:50:49 +05301065 __func__, status);
1066 ret = -EIO;
1067 goto out;
1068 }
1069
1070 ret = __nand_unlock(mtd, ofs, len, 0x1);
1071
1072out:
Huang Shijieb0bb6902012-11-19 14:43:29 +08001073 chip->select_chip(mtd, -1);
Vimal Singh7d70f332010-02-08 15:50:49 +05301074 nand_release_device(mtd);
1075
1076 return ret;
1077}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001078EXPORT_SYMBOL(nand_lock);
Vimal Singh7d70f332010-02-08 15:50:49 +05301079
1080/**
Boris BREZILLON730a43f2015-09-03 18:03:38 +02001081 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1082 * @buf: buffer to test
1083 * @len: buffer length
1084 * @bitflips_threshold: maximum number of bitflips
1085 *
1086 * Check if a buffer contains only 0xff, which means the underlying region
1087 * has been erased and is ready to be programmed.
1088 * The bitflips_threshold specify the maximum number of bitflips before
1089 * considering the region is not erased.
1090 * Note: The logic of this function has been extracted from the memweight
1091 * implementation, except that nand_check_erased_buf function exit before
1092 * testing the whole buffer if the number of bitflips exceed the
1093 * bitflips_threshold value.
1094 *
1095 * Returns a positive number of bitflips less than or equal to
1096 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1097 * threshold.
1098 */
1099static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1100{
1101 const unsigned char *bitmap = buf;
1102 int bitflips = 0;
1103 int weight;
1104
1105 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1106 len--, bitmap++) {
1107 weight = hweight8(*bitmap);
1108 bitflips += BITS_PER_BYTE - weight;
1109 if (unlikely(bitflips > bitflips_threshold))
1110 return -EBADMSG;
1111 }
1112
1113 for (; len >= sizeof(long);
1114 len -= sizeof(long), bitmap += sizeof(long)) {
1115 weight = hweight_long(*((unsigned long *)bitmap));
1116 bitflips += BITS_PER_LONG - weight;
1117 if (unlikely(bitflips > bitflips_threshold))
1118 return -EBADMSG;
1119 }
1120
1121 for (; len > 0; len--, bitmap++) {
1122 weight = hweight8(*bitmap);
1123 bitflips += BITS_PER_BYTE - weight;
1124 if (unlikely(bitflips > bitflips_threshold))
1125 return -EBADMSG;
1126 }
1127
1128 return bitflips;
1129}
1130
1131/**
1132 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1133 * 0xff data
1134 * @data: data buffer to test
1135 * @datalen: data length
1136 * @ecc: ECC buffer
1137 * @ecclen: ECC length
1138 * @extraoob: extra OOB buffer
1139 * @extraooblen: extra OOB length
1140 * @bitflips_threshold: maximum number of bitflips
1141 *
1142 * Check if a data buffer and its associated ECC and OOB data contains only
1143 * 0xff pattern, which means the underlying region has been erased and is
1144 * ready to be programmed.
1145 * The bitflips_threshold specify the maximum number of bitflips before
1146 * considering the region as not erased.
1147 *
1148 * Note:
1149 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1150 * different from the NAND page size. When fixing bitflips, ECC engines will
1151 * report the number of errors per chunk, and the NAND core infrastructure
1152 * expect you to return the maximum number of bitflips for the whole page.
1153 * This is why you should always use this function on a single chunk and
1154 * not on the whole page. After checking each chunk you should update your
1155 * max_bitflips value accordingly.
1156 * 2/ When checking for bitflips in erased pages you should not only check
1157 * the payload data but also their associated ECC data, because a user might
1158 * have programmed almost all bits to 1 but a few. In this case, we
1159 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1160 * this case.
1161 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1162 * data are protected by the ECC engine.
1163 * It could also be used if you support subpages and want to attach some
1164 * extra OOB data to an ECC chunk.
1165 *
1166 * Returns a positive number of bitflips less than or equal to
1167 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1168 * threshold. In case of success, the passed buffers are filled with 0xff.
1169 */
1170int nand_check_erased_ecc_chunk(void *data, int datalen,
1171 void *ecc, int ecclen,
1172 void *extraoob, int extraooblen,
1173 int bitflips_threshold)
1174{
1175 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1176
1177 data_bitflips = nand_check_erased_buf(data, datalen,
1178 bitflips_threshold);
1179 if (data_bitflips < 0)
1180 return data_bitflips;
1181
1182 bitflips_threshold -= data_bitflips;
1183
1184 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1185 if (ecc_bitflips < 0)
1186 return ecc_bitflips;
1187
1188 bitflips_threshold -= ecc_bitflips;
1189
1190 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1191 bitflips_threshold);
1192 if (extraoob_bitflips < 0)
1193 return extraoob_bitflips;
1194
1195 if (data_bitflips)
1196 memset(data, 0xff, datalen);
1197
1198 if (ecc_bitflips)
1199 memset(ecc, 0xff, ecclen);
1200
1201 if (extraoob_bitflips)
1202 memset(extraoob, 0xff, extraooblen);
1203
1204 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1205}
1206EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1207
1208/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001209 * nand_read_page_raw - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001210 * @mtd: mtd info structure
1211 * @chip: nand chip info structure
1212 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001213 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001214 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001215 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001216 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001217 */
1218static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001219 uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001220{
1221 chip->read_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07001222 if (oob_required)
1223 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001224 return 0;
1225}
1226
1227/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001228 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001229 * @mtd: mtd info structure
1230 * @chip: nand chip info structure
1231 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001232 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001233 * @page: page number to read
David Brownell52ff49d2009-03-04 12:01:36 -08001234 *
1235 * We need a special oob layout and handling even when OOB isn't used.
1236 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001237static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001238 struct nand_chip *chip, uint8_t *buf,
1239 int oob_required, int page)
David Brownell52ff49d2009-03-04 12:01:36 -08001240{
1241 int eccsize = chip->ecc.size;
1242 int eccbytes = chip->ecc.bytes;
1243 uint8_t *oob = chip->oob_poi;
1244 int steps, size;
1245
1246 for (steps = chip->ecc.steps; steps > 0; steps--) {
1247 chip->read_buf(mtd, buf, eccsize);
1248 buf += eccsize;
1249
1250 if (chip->ecc.prepad) {
1251 chip->read_buf(mtd, oob, chip->ecc.prepad);
1252 oob += chip->ecc.prepad;
1253 }
1254
1255 chip->read_buf(mtd, oob, eccbytes);
1256 oob += eccbytes;
1257
1258 if (chip->ecc.postpad) {
1259 chip->read_buf(mtd, oob, chip->ecc.postpad);
1260 oob += chip->ecc.postpad;
1261 }
1262 }
1263
1264 size = mtd->oobsize - (oob - chip->oob_poi);
1265 if (size)
1266 chip->read_buf(mtd, oob, size);
1267
1268 return 0;
1269}
1270
1271/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001272 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001273 * @mtd: mtd info structure
1274 * @chip: nand chip info structure
1275 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001276 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001277 * @page: page number to read
David A. Marlin068e3c02005-01-24 03:07:46 +00001278 */
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001279static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001280 uint8_t *buf, int oob_required, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Boris Brezillon846031d2016-02-03 20:11:00 +01001282 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001283 int eccbytes = chip->ecc.bytes;
1284 int eccsteps = chip->ecc.steps;
1285 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001286 uint8_t *ecc_calc = chip->buffers->ecccalc;
1287 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001288 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001289
Brian Norris1fbb9382012-05-02 10:14:55 -07001290 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001291
1292 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1293 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1294
Boris Brezillon846031d2016-02-03 20:11:00 +01001295 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1296 chip->ecc.total);
1297 if (ret)
1298 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001299
1300 eccsteps = chip->ecc.steps;
1301 p = buf;
1302
1303 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1304 int stat;
1305
1306 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Mike Dunn3f91e942012-04-25 12:06:09 -07001307 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001308 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001309 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001310 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001311 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1312 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001313 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001314 return max_bitflips;
Thomas Gleixner22c60f52005-04-04 19:56:32 +01001315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317/**
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05301318 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001319 * @mtd: mtd info structure
1320 * @chip: nand chip info structure
1321 * @data_offs: offset of requested data within the page
1322 * @readlen: data length
1323 * @bufpoi: buffer to store read data
Huang Shijiee004deb2014-01-03 11:01:40 +08001324 * @page: page number to read
Alexey Korolev3d459552008-05-15 17:23:18 +01001325 */
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001326static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001327 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1328 int page)
Alexey Korolev3d459552008-05-15 17:23:18 +01001329{
Boris Brezillon846031d2016-02-03 20:11:00 +01001330 int start_step, end_step, num_steps, ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001331 uint8_t *p;
1332 int data_col_addr, i, gaps = 0;
1333 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1334 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Boris Brezillon846031d2016-02-03 20:11:00 +01001335 int index, section = 0;
Mike Dunn3f91e942012-04-25 12:06:09 -07001336 unsigned int max_bitflips = 0;
Boris Brezillon846031d2016-02-03 20:11:00 +01001337 struct mtd_oob_region oobregion = { };
Alexey Korolev3d459552008-05-15 17:23:18 +01001338
Brian Norris7854d3f2011-06-23 14:12:08 -07001339 /* Column address within the page aligned to ECC size (256bytes) */
Alexey Korolev3d459552008-05-15 17:23:18 +01001340 start_step = data_offs / chip->ecc.size;
1341 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1342 num_steps = end_step - start_step + 1;
Ron4a4163c2014-03-16 04:01:07 +10301343 index = start_step * chip->ecc.bytes;
Alexey Korolev3d459552008-05-15 17:23:18 +01001344
Brian Norris8b6e50c2011-05-25 14:59:01 -07001345 /* Data size aligned to ECC ecc.size */
Alexey Korolev3d459552008-05-15 17:23:18 +01001346 datafrag_len = num_steps * chip->ecc.size;
1347 eccfrag_len = num_steps * chip->ecc.bytes;
1348
1349 data_col_addr = start_step * chip->ecc.size;
1350 /* If we read not a page aligned data */
1351 if (data_col_addr != 0)
1352 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1353
1354 p = bufpoi + data_col_addr;
1355 chip->read_buf(mtd, p, datafrag_len);
1356
Brian Norris8b6e50c2011-05-25 14:59:01 -07001357 /* Calculate ECC */
Alexey Korolev3d459552008-05-15 17:23:18 +01001358 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1359 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1360
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 /*
1362 * The performance is faster if we position offsets according to
Brian Norris7854d3f2011-06-23 14:12:08 -07001363 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
Brian Norris8b6e50c2011-05-25 14:59:01 -07001364 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001365 ret = mtd_ooblayout_find_eccregion(mtd, index, &section, &oobregion);
1366 if (ret)
1367 return ret;
1368
1369 if (oobregion.length < eccfrag_len)
1370 gaps = 1;
1371
Alexey Korolev3d459552008-05-15 17:23:18 +01001372 if (gaps) {
1373 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1374 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1375 } else {
Brian Norris8b6e50c2011-05-25 14:59:01 -07001376 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07001377 * Send the command to read the particular ECC bytes take care
Brian Norris8b6e50c2011-05-25 14:59:01 -07001378 * about buswidth alignment in read_buf.
1379 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001380 aligned_pos = oobregion.offset & ~(busw - 1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001381 aligned_len = eccfrag_len;
Boris Brezillon846031d2016-02-03 20:11:00 +01001382 if (oobregion.offset & (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001383 aligned_len++;
Boris Brezillon846031d2016-02-03 20:11:00 +01001384 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1385 (busw - 1))
Alexey Korolev3d459552008-05-15 17:23:18 +01001386 aligned_len++;
1387
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001388 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
Boris Brezillon846031d2016-02-03 20:11:00 +01001389 mtd->writesize + aligned_pos, -1);
Alexey Korolev3d459552008-05-15 17:23:18 +01001390 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1391 }
1392
Boris Brezillon846031d2016-02-03 20:11:00 +01001393 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1394 chip->oob_poi, index, eccfrag_len);
1395 if (ret)
1396 return ret;
Alexey Korolev3d459552008-05-15 17:23:18 +01001397
1398 p = bufpoi + data_col_addr;
1399 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1400 int stat;
1401
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001402 stat = chip->ecc.correct(mtd, p,
1403 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001404 if (stat == -EBADMSG &&
1405 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1406 /* check for empty pages with bitflips */
1407 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1408 &chip->buffers->ecccode[i],
1409 chip->ecc.bytes,
1410 NULL, 0,
1411 chip->ecc.strength);
1412 }
1413
Mike Dunn3f91e942012-04-25 12:06:09 -07001414 if (stat < 0) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001415 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001416 } else {
Alexey Korolev3d459552008-05-15 17:23:18 +01001417 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001418 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1419 }
Alexey Korolev3d459552008-05-15 17:23:18 +01001420 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001421 return max_bitflips;
Alexey Korolev3d459552008-05-15 17:23:18 +01001422}
1423
1424/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001425 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001426 * @mtd: mtd info structure
1427 * @chip: nand chip info structure
1428 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001429 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001430 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001431 *
Brian Norris7854d3f2011-06-23 14:12:08 -07001432 * Not for syndrome calculating ECC controllers which need a special oob layout.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001433 */
1434static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001435 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001436{
Boris Brezillon846031d2016-02-03 20:11:00 +01001437 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001438 int eccbytes = chip->ecc.bytes;
1439 int eccsteps = chip->ecc.steps;
1440 uint8_t *p = buf;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001441 uint8_t *ecc_calc = chip->buffers->ecccalc;
1442 uint8_t *ecc_code = chip->buffers->ecccode;
Mike Dunn3f91e942012-04-25 12:06:09 -07001443 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001444
1445 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1446 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1447 chip->read_buf(mtd, p, eccsize);
1448 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1449 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001450 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001451
Boris Brezillon846031d2016-02-03 20:11:00 +01001452 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1453 chip->ecc.total);
1454 if (ret)
1455 return ret;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001456
1457 eccsteps = chip->ecc.steps;
1458 p = buf;
1459
1460 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1461 int stat;
1462
1463 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001464 if (stat == -EBADMSG &&
1465 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1466 /* check for empty pages with bitflips */
1467 stat = nand_check_erased_ecc_chunk(p, eccsize,
1468 &ecc_code[i], eccbytes,
1469 NULL, 0,
1470 chip->ecc.strength);
1471 }
1472
Mike Dunn3f91e942012-04-25 12:06:09 -07001473 if (stat < 0) {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001474 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001475 } else {
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001476 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001477 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1478 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001479 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001480 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001481}
1482
1483/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001484 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
Brian Norris8b6e50c2011-05-25 14:59:01 -07001485 * @mtd: mtd info structure
1486 * @chip: nand chip info structure
1487 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001488 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001489 * @page: page number to read
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001490 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001491 * Hardware ECC for large page chips, require OOB to be read first. For this
1492 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1493 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1494 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1495 * the data area, by overwriting the NAND manufacturer bad block markings.
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001496 */
1497static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001498 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001499{
Boris Brezillon846031d2016-02-03 20:11:00 +01001500 int i, eccsize = chip->ecc.size, ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001501 int eccbytes = chip->ecc.bytes;
1502 int eccsteps = chip->ecc.steps;
1503 uint8_t *p = buf;
1504 uint8_t *ecc_code = chip->buffers->ecccode;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001505 uint8_t *ecc_calc = chip->buffers->ecccalc;
Mike Dunn3f91e942012-04-25 12:06:09 -07001506 unsigned int max_bitflips = 0;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001507
1508 /* Read the OOB area first */
1509 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1510 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1511 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1512
Boris Brezillon846031d2016-02-03 20:11:00 +01001513 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1514 chip->ecc.total);
1515 if (ret)
1516 return ret;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001517
1518 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1519 int stat;
1520
1521 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1522 chip->read_buf(mtd, p, eccsize);
1523 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1524
1525 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001526 if (stat == -EBADMSG &&
1527 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1528 /* check for empty pages with bitflips */
1529 stat = nand_check_erased_ecc_chunk(p, eccsize,
1530 &ecc_code[i], eccbytes,
1531 NULL, 0,
1532 chip->ecc.strength);
1533 }
1534
Mike Dunn3f91e942012-04-25 12:06:09 -07001535 if (stat < 0) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001536 mtd->ecc_stats.failed++;
Mike Dunn3f91e942012-04-25 12:06:09 -07001537 } else {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001538 mtd->ecc_stats.corrected += stat;
Mike Dunn3f91e942012-04-25 12:06:09 -07001539 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1540 }
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001541 }
Mike Dunn3f91e942012-04-25 12:06:09 -07001542 return max_bitflips;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07001543}
1544
1545/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001546 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
Brian Norris8b6e50c2011-05-25 14:59:01 -07001547 * @mtd: mtd info structure
1548 * @chip: nand chip info structure
1549 * @buf: buffer to store read data
Brian Norris1fbb9382012-05-02 10:14:55 -07001550 * @oob_required: caller requires OOB data read to chip->oob_poi
Brian Norris8b6e50c2011-05-25 14:59:01 -07001551 * @page: page number to read
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001552 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001553 * The hw generator calculates the error syndrome automatically. Therefore we
1554 * need a special oob layout and handling.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001555 */
1556static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -07001557 uint8_t *buf, int oob_required, int page)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001558{
1559 int i, eccsize = chip->ecc.size;
1560 int eccbytes = chip->ecc.bytes;
1561 int eccsteps = chip->ecc.steps;
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001562 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001563 uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02001564 uint8_t *oob = chip->oob_poi;
Mike Dunn3f91e942012-04-25 12:06:09 -07001565 unsigned int max_bitflips = 0;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001566
1567 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1568 int stat;
1569
1570 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1571 chip->read_buf(mtd, p, eccsize);
1572
1573 if (chip->ecc.prepad) {
1574 chip->read_buf(mtd, oob, chip->ecc.prepad);
1575 oob += chip->ecc.prepad;
1576 }
1577
1578 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1579 chip->read_buf(mtd, oob, eccbytes);
1580 stat = chip->ecc.correct(mtd, p, oob, NULL);
1581
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001582 oob += eccbytes;
1583
1584 if (chip->ecc.postpad) {
1585 chip->read_buf(mtd, oob, chip->ecc.postpad);
1586 oob += chip->ecc.postpad;
1587 }
Boris BREZILLON40cbe6e2015-12-30 20:32:04 +01001588
1589 if (stat == -EBADMSG &&
1590 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1591 /* check for empty pages with bitflips */
1592 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1593 oob - eccpadbytes,
1594 eccpadbytes,
1595 NULL, 0,
1596 chip->ecc.strength);
1597 }
1598
1599 if (stat < 0) {
1600 mtd->ecc_stats.failed++;
1601 } else {
1602 mtd->ecc_stats.corrected += stat;
1603 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1604 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001605 }
1606
1607 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04001608 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001609 if (i)
1610 chip->read_buf(mtd, oob, i);
1611
Mike Dunn3f91e942012-04-25 12:06:09 -07001612 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001613}
1614
1615/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001616 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
Boris Brezillon846031d2016-02-03 20:11:00 +01001617 * @mtd: mtd info structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001618 * @oob: oob destination address
1619 * @ops: oob ops structure
1620 * @len: size of oob to transfer
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001621 */
Boris Brezillon846031d2016-02-03 20:11:00 +01001622static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
Vitaly Wool70145682006-11-03 18:20:38 +03001623 struct mtd_oob_ops *ops, size_t len)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001624{
Boris Brezillon846031d2016-02-03 20:11:00 +01001625 struct nand_chip *chip = mtd_to_nand(mtd);
1626 int ret;
1627
Florian Fainellif8ac0412010-09-07 13:23:43 +02001628 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001629
Brian Norris0612b9d2011-08-30 18:45:40 -07001630 case MTD_OPS_PLACE_OOB:
1631 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001632 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1633 return oob + len;
1634
Boris Brezillon846031d2016-02-03 20:11:00 +01001635 case MTD_OPS_AUTO_OOB:
1636 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1637 ops->ooboffs, len);
1638 BUG_ON(ret);
1639 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001640
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001641 default:
1642 BUG();
1643 }
1644 return NULL;
1645}
1646
1647/**
Brian Norrisba84fb52014-01-03 15:13:33 -08001648 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1649 * @mtd: MTD device structure
1650 * @retry_mode: the retry mode to use
1651 *
1652 * Some vendors supply a special command to shift the Vt threshold, to be used
1653 * when there are too many bitflips in a page (i.e., ECC error). After setting
1654 * a new threshold, the host should retry reading the page.
1655 */
1656static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1657{
Boris BREZILLON862eba52015-12-01 12:03:03 +01001658 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norrisba84fb52014-01-03 15:13:33 -08001659
1660 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1661
1662 if (retry_mode >= chip->read_retries)
1663 return -EINVAL;
1664
1665 if (!chip->setup_read_retry)
1666 return -EOPNOTSUPP;
1667
1668 return chip->setup_read_retry(mtd, retry_mode);
1669}
1670
1671/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001672 * nand_do_read_ops - [INTERN] Read data with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001673 * @mtd: MTD device structure
1674 * @from: offset to read from
1675 * @ops: oob ops structure
David A. Marlin068e3c02005-01-24 03:07:46 +00001676 *
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001677 * Internal function. Called with chip held.
David A. Marlin068e3c02005-01-24 03:07:46 +00001678 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001679static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1680 struct mtd_oob_ops *ops)
David A. Marlin068e3c02005-01-24 03:07:46 +00001681{
Brian Norrise47f3db2012-05-02 10:14:56 -07001682 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Boris BREZILLON862eba52015-12-01 12:03:03 +01001683 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001684 int ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001685 uint32_t readlen = ops->len;
Vitaly Wool70145682006-11-03 18:20:38 +03001686 uint32_t oobreadlen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01001687 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Maxim Levitsky9aca3342010-02-22 20:39:35 +02001688
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001689 uint8_t *bufpoi, *oob, *buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04001690 int use_bufpoi;
Mike Dunnedbc45402012-04-25 12:06:11 -07001691 unsigned int max_bitflips = 0;
Brian Norrisba84fb52014-01-03 15:13:33 -08001692 int retry_mode = 0;
Brian Norrisb72f3df2013-12-03 11:04:14 -08001693 bool ecc_fail = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001695 chipnr = (int)(from >> chip->chip_shift);
1696 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001698 realpage = (int)(from >> chip->page_shift);
1699 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001701 col = (int)(from & (mtd->writesize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001703 buf = ops->datbuf;
1704 oob = ops->oobbuf;
Brian Norrise47f3db2012-05-02 10:14:56 -07001705 oob_required = oob ? 1 : 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001706
Florian Fainellif8ac0412010-09-07 13:23:43 +02001707 while (1) {
Brian Norrisb72f3df2013-12-03 11:04:14 -08001708 unsigned int ecc_failures = mtd->ecc_stats.failed;
1709
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001710 bytes = min(mtd->writesize - col, readlen);
1711 aligned = (bytes == mtd->writesize);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001712
Kamal Dasu66507c72014-05-01 20:51:19 -04001713 if (!aligned)
1714 use_bufpoi = 1;
1715 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1716 use_bufpoi = !virt_addr_valid(buf);
1717 else
1718 use_bufpoi = 0;
1719
Brian Norris8b6e50c2011-05-25 14:59:01 -07001720 /* Is the current page in the buffer? */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001721 if (realpage != chip->pagebuf || oob) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001722 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1723
1724 if (use_bufpoi && aligned)
1725 pr_debug("%s: using read bounce buffer for buf@%p\n",
1726 __func__, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Brian Norrisba84fb52014-01-03 15:13:33 -08001728read_retry:
Brian Norrisc00a0992012-05-01 17:12:54 -07001729 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Mike Dunnedbc45402012-04-25 12:06:11 -07001731 /*
1732 * Now read the page into the buffer. Absent an error,
1733 * the read methods return max bitflips per ecc step.
1734 */
Brian Norris0612b9d2011-08-30 18:45:40 -07001735 if (unlikely(ops->mode == MTD_OPS_RAW))
Brian Norris1fbb9382012-05-02 10:14:55 -07001736 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001737 oob_required,
1738 page);
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001739 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1740 !oob)
Florian Fainelli7351d3a2010-09-07 13:23:45 +02001741 ret = chip->ecc.read_subpage(mtd, chip,
Huang Shijiee004deb2014-01-03 11:01:40 +08001742 col, bytes, bufpoi,
1743 page);
David Woodhouse956e9442006-09-25 17:12:39 +01001744 else
Sneha Narnakaje46a8cf22009-09-18 12:51:46 -07001745 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Brian Norrise47f3db2012-05-02 10:14:56 -07001746 oob_required, page);
Brian Norris6d77b9d2011-09-07 13:13:40 -07001747 if (ret < 0) {
Kamal Dasu66507c72014-05-01 20:51:19 -04001748 if (use_bufpoi)
Brian Norris6d77b9d2011-09-07 13:13:40 -07001749 /* Invalidate page cache */
1750 chip->pagebuf = -1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001751 break;
Brian Norris6d77b9d2011-09-07 13:13:40 -07001752 }
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001753
Mike Dunnedbc45402012-04-25 12:06:11 -07001754 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1755
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001756 /* Transfer not aligned data */
Kamal Dasu66507c72014-05-01 20:51:19 -04001757 if (use_bufpoi) {
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05001758 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Brian Norrisb72f3df2013-12-03 11:04:14 -08001759 !(mtd->ecc_stats.failed - ecc_failures) &&
Mike Dunnedbc45402012-04-25 12:06:11 -07001760 (ops->mode != MTD_OPS_RAW)) {
Alexey Korolev3d459552008-05-15 17:23:18 +01001761 chip->pagebuf = realpage;
Mike Dunnedbc45402012-04-25 12:06:11 -07001762 chip->pagebuf_bitflips = ret;
1763 } else {
Brian Norris6d77b9d2011-09-07 13:13:40 -07001764 /* Invalidate page cache */
1765 chip->pagebuf = -1;
Mike Dunnedbc45402012-04-25 12:06:11 -07001766 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001767 memcpy(buf, chip->buffers->databuf + col, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001769
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001770 if (unlikely(oob)) {
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001771 int toread = min(oobreadlen, max_oobsize);
1772
1773 if (toread) {
Boris Brezillon846031d2016-02-03 20:11:00 +01001774 oob = nand_transfer_oob(mtd,
Maxim Levitskyb64d39d2010-02-22 20:39:37 +02001775 oob, ops, toread);
1776 oobreadlen -= toread;
1777 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001778 }
Brian Norris5bc7c332013-03-13 09:51:31 -07001779
1780 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 }
Brian Norrisb72f3df2013-12-03 11:04:14 -08001787
Brian Norrisba84fb52014-01-03 15:13:33 -08001788 if (mtd->ecc_stats.failed - ecc_failures) {
Brian Norris28fa65e2014-02-12 16:08:28 -08001789 if (retry_mode + 1 < chip->read_retries) {
Brian Norrisba84fb52014-01-03 15:13:33 -08001790 retry_mode++;
1791 ret = nand_setup_read_retry(mtd,
1792 retry_mode);
1793 if (ret < 0)
1794 break;
1795
1796 /* Reset failures; retry */
1797 mtd->ecc_stats.failed = ecc_failures;
1798 goto read_retry;
1799 } else {
1800 /* No more retry modes; real failure */
1801 ecc_fail = true;
1802 }
1803 }
1804
1805 buf += bytes;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001806 } else {
David Woodhouse4bf63fc2006-09-25 17:08:04 +01001807 memcpy(buf, chip->buffers->databuf + col, bytes);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001808 buf += bytes;
Mike Dunnedbc45402012-04-25 12:06:11 -07001809 max_bitflips = max_t(unsigned int, max_bitflips,
1810 chip->pagebuf_bitflips);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001813 readlen -= bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001814
Brian Norrisba84fb52014-01-03 15:13:33 -08001815 /* Reset to retry mode 0 */
1816 if (retry_mode) {
1817 ret = nand_setup_read_retry(mtd, 0);
1818 if (ret < 0)
1819 break;
1820 retry_mode = 0;
1821 }
1822
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001823 if (!readlen)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Brian Norris8b6e50c2011-05-25 14:59:01 -07001826 /* For subsequent reads align to page boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 col = 0;
1828 /* Increment page address */
1829 realpage++;
1830
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001831 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* Check, if we cross a chip boundary */
1833 if (!page) {
1834 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02001835 chip->select_chip(mtd, -1);
1836 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08001839 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001841 ops->retlen = ops->len - (size_t) readlen;
Vitaly Wool70145682006-11-03 18:20:38 +03001842 if (oob)
1843 ops->oobretlen = ops->ooblen - oobreadlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Mike Dunn3f91e942012-04-25 12:06:09 -07001845 if (ret < 0)
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001846 return ret;
1847
Brian Norrisb72f3df2013-12-03 11:04:14 -08001848 if (ecc_fail)
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +02001849 return -EBADMSG;
1850
Mike Dunnedbc45402012-04-25 12:06:11 -07001851 return max_bitflips;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001852}
1853
1854/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001855 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Brian Norris8b6e50c2011-05-25 14:59:01 -07001856 * @mtd: MTD device structure
1857 * @from: offset to read from
1858 * @len: number of bytes to read
1859 * @retlen: pointer to variable to store the number of read bytes
1860 * @buf: the databuffer to put data
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001861 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001862 * Get hold of the chip and call nand_do_read.
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001863 */
1864static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1865 size_t *retlen, uint8_t *buf)
1866{
Brian Norris4a89ff82011-08-30 18:45:45 -07001867 struct mtd_oob_ops ops;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001868 int ret;
1869
Huang Shijie6a8214a2012-11-19 14:43:30 +08001870 nand_get_device(mtd, FL_READING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08001871 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07001872 ops.len = len;
1873 ops.datbuf = buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08001874 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07001875 ret = nand_do_read_ops(mtd, from, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07001876 *retlen = ops.retlen;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001877 nand_release_device(mtd);
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02001878 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001882 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001883 * @mtd: mtd info structure
1884 * @chip: nand chip info structure
1885 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001886 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001887int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001888{
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001889 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001890 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001891 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001892}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001893EXPORT_SYMBOL(nand_read_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001894
1895/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001896 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001897 * with syndromes
Brian Norris8b6e50c2011-05-25 14:59:01 -07001898 * @mtd: mtd info structure
1899 * @chip: nand chip info structure
1900 * @page: page number to read
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001901 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001902int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1903 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001904{
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001905 int length = mtd->oobsize;
1906 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1907 int eccsize = chip->ecc.size;
Baruch Siach2ea69d22015-01-22 15:23:05 +02001908 uint8_t *bufpoi = chip->oob_poi;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001909 int i, toread, sndrnd = 0, pos;
1910
1911 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1912 for (i = 0; i < chip->ecc.steps; i++) {
1913 if (sndrnd) {
1914 pos = eccsize + i * (eccsize + chunk);
1915 if (mtd->writesize > 512)
1916 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1917 else
1918 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1919 } else
1920 sndrnd = 1;
1921 toread = min_t(int, length, chunk);
1922 chip->read_buf(mtd, bufpoi, toread);
1923 bufpoi += toread;
1924 length -= toread;
1925 }
1926 if (length > 0)
1927 chip->read_buf(mtd, bufpoi, length);
1928
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +03001929 return 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001930}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001931EXPORT_SYMBOL(nand_read_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001932
1933/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001934 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07001935 * @mtd: mtd info structure
1936 * @chip: nand chip info structure
1937 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001938 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001939int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001940{
1941 int status = 0;
1942 const uint8_t *buf = chip->oob_poi;
1943 int length = mtd->oobsize;
1944
1945 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1946 chip->write_buf(mtd, buf, length);
1947 /* Send command to program the OOB data */
1948 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1949
1950 status = chip->waitfunc(mtd, chip);
1951
Savin Zlobec0d420f92006-06-21 11:51:20 +02001952 return status & NAND_STATUS_FAIL ? -EIO : 0;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001953}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001954EXPORT_SYMBOL(nand_write_oob_std);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001955
1956/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001957 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07001958 * with syndrome - only for large page flash
1959 * @mtd: mtd info structure
1960 * @chip: nand chip info structure
1961 * @page: page number to write
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001962 */
Boris Brezillon9d02fc22015-08-26 16:08:12 +02001963int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1964 int page)
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001965{
1966 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1967 int eccsize = chip->ecc.size, length = mtd->oobsize;
1968 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1969 const uint8_t *bufpoi = chip->oob_poi;
1970
1971 /*
1972 * data-ecc-data-ecc ... ecc-oob
1973 * or
1974 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1975 */
1976 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1977 pos = steps * (eccsize + chunk);
1978 steps = 0;
1979 } else
Vitaly Wool8b0036e2006-07-11 09:11:25 +02001980 pos = eccsize;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02001981
1982 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1983 for (i = 0; i < steps; i++) {
1984 if (sndcmd) {
1985 if (mtd->writesize <= 512) {
1986 uint32_t fill = 0xFFFFFFFF;
1987
1988 len = eccsize;
1989 while (len > 0) {
1990 int num = min_t(int, len, 4);
1991 chip->write_buf(mtd, (uint8_t *)&fill,
1992 num);
1993 len -= num;
1994 }
1995 } else {
1996 pos = eccsize + i * (eccsize + chunk);
1997 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1998 }
1999 } else
2000 sndcmd = 1;
2001 len = min_t(int, length, chunk);
2002 chip->write_buf(mtd, bufpoi, len);
2003 bufpoi += len;
2004 length -= len;
2005 }
2006 if (length > 0)
2007 chip->write_buf(mtd, bufpoi, length);
2008
2009 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2010 status = chip->waitfunc(mtd, chip);
2011
2012 return status & NAND_STATUS_FAIL ? -EIO : 0;
2013}
Boris Brezillon9d02fc22015-08-26 16:08:12 +02002014EXPORT_SYMBOL(nand_write_oob_syndrome);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002015
2016/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002017 * nand_do_read_oob - [INTERN] NAND read out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002018 * @mtd: MTD device structure
2019 * @from: offset to read from
2020 * @ops: oob operations description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002022 * NAND read out-of-band data from the spare area.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002024static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2025 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Brian Norrisc00a0992012-05-01 17:12:54 -07002027 int page, realpage, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002028 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris041e4572011-06-23 16:45:24 -07002029 struct mtd_ecc_stats stats;
Vitaly Wool70145682006-11-03 18:20:38 +03002030 int readlen = ops->ooblen;
2031 int len;
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002032 uint8_t *buf = ops->oobbuf;
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002033 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Brian Norris289c0522011-07-19 10:06:09 -07002035 pr_debug("%s: from = 0x%08Lx, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302036 __func__, (unsigned long long)from, readlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Brian Norris041e4572011-06-23 16:45:24 -07002038 stats = mtd->ecc_stats;
2039
Boris BREZILLON29f10582016-03-07 10:46:52 +01002040 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002041
2042 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002043 pr_debug("%s: attempt to start read outside oob\n",
2044 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002045 return -EINVAL;
2046 }
2047
2048 /* Do not allow reads past end of device */
2049 if (unlikely(from >= mtd->size ||
2050 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2051 (from >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002052 pr_debug("%s: attempt to read beyond end of device\n",
2053 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002054 return -EINVAL;
2055 }
Vitaly Wool70145682006-11-03 18:20:38 +03002056
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002057 chipnr = (int)(from >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002058 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002060 /* Shift to get page */
2061 realpage = (int)(from >> chip->page_shift);
2062 page = realpage & chip->pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Florian Fainellif8ac0412010-09-07 13:23:43 +02002064 while (1) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002065 if (ops->mode == MTD_OPS_RAW)
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002066 ret = chip->ecc.read_oob_raw(mtd, chip, page);
Brian Norrisc46f6482011-08-30 18:45:38 -07002067 else
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002068 ret = chip->ecc.read_oob(mtd, chip, page);
2069
2070 if (ret < 0)
2071 break;
Vitaly Wool70145682006-11-03 18:20:38 +03002072
2073 len = min(len, readlen);
Boris Brezillon846031d2016-02-03 20:11:00 +01002074 buf = nand_transfer_oob(mtd, buf, ops, len);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002075
Brian Norris5bc7c332013-03-13 09:51:31 -07002076 if (chip->options & NAND_NEED_READRDY) {
2077 /* Apply delay or wait for ready/busy pin */
2078 if (!chip->dev_ready)
2079 udelay(chip->chip_delay);
2080 else
2081 nand_wait_ready(mtd);
2082 }
2083
Vitaly Wool70145682006-11-03 18:20:38 +03002084 readlen -= len;
Savin Zlobec0d420f92006-06-21 11:51:20 +02002085 if (!readlen)
2086 break;
2087
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002088 /* Increment page address */
2089 realpage++;
2090
2091 page = realpage & chip->pagemask;
2092 /* Check, if we cross a chip boundary */
2093 if (!page) {
2094 chipnr++;
2095 chip->select_chip(mtd, -1);
2096 chip->select_chip(mtd, chipnr);
2097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Huang Shijieb0bb6902012-11-19 14:43:29 +08002099 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Shmulik Ladkani1951f2f2012-05-09 13:13:34 +03002101 ops->oobretlen = ops->ooblen - readlen;
2102
2103 if (ret < 0)
2104 return ret;
Brian Norris041e4572011-06-23 16:45:24 -07002105
2106 if (mtd->ecc_stats.failed - stats.failed)
2107 return -EBADMSG;
2108
2109 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002113 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002114 * @mtd: MTD device structure
2115 * @from: offset to read from
2116 * @ops: oob operation description structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002118 * NAND read data and/or out-of-band data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002120static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2121 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002123 int ret = -ENOTSUPP;
2124
2125 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* Do not allow reads past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002128 if (ops->datbuf && (from + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002129 pr_debug("%s: attempt to read beyond end of device\n",
2130 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return -EINVAL;
2132 }
2133
Huang Shijie6a8214a2012-11-19 14:43:30 +08002134 nand_get_device(mtd, FL_READING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Florian Fainellif8ac0412010-09-07 13:23:43 +02002136 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002137 case MTD_OPS_PLACE_OOB:
2138 case MTD_OPS_AUTO_OOB:
2139 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002140 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002141
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002142 default:
2143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002146 if (!ops->datbuf)
2147 ret = nand_do_read_oob(mtd, from, ops);
2148 else
2149 ret = nand_do_read_ops(mtd, from, ops);
2150
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002151out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002153 return ret;
2154}
2155
2156
2157/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002158 * nand_write_page_raw - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002159 * @mtd: mtd info structure
2160 * @chip: nand chip info structure
2161 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002162 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002163 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002164 *
Brian Norris7854d3f2011-06-23 14:12:08 -07002165 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002166 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002167static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002168 const uint8_t *buf, int oob_required, int page)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002169{
2170 chip->write_buf(mtd, buf, mtd->writesize);
Brian Norris279f08d2012-05-02 10:15:03 -07002171 if (oob_required)
2172 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002173
2174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002177/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002178 * nand_write_page_raw_syndrome - [INTERN] raw page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002179 * @mtd: mtd info structure
2180 * @chip: nand chip info structure
2181 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002182 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002183 * @page: page number to write
David Brownell52ff49d2009-03-04 12:01:36 -08002184 *
2185 * We need a special oob layout and handling even when ECC isn't checked.
2186 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002187static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002188 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002189 const uint8_t *buf, int oob_required,
2190 int page)
David Brownell52ff49d2009-03-04 12:01:36 -08002191{
2192 int eccsize = chip->ecc.size;
2193 int eccbytes = chip->ecc.bytes;
2194 uint8_t *oob = chip->oob_poi;
2195 int steps, size;
2196
2197 for (steps = chip->ecc.steps; steps > 0; steps--) {
2198 chip->write_buf(mtd, buf, eccsize);
2199 buf += eccsize;
2200
2201 if (chip->ecc.prepad) {
2202 chip->write_buf(mtd, oob, chip->ecc.prepad);
2203 oob += chip->ecc.prepad;
2204 }
2205
Boris BREZILLON60c3bc12014-02-01 19:10:28 +01002206 chip->write_buf(mtd, oob, eccbytes);
David Brownell52ff49d2009-03-04 12:01:36 -08002207 oob += eccbytes;
2208
2209 if (chip->ecc.postpad) {
2210 chip->write_buf(mtd, oob, chip->ecc.postpad);
2211 oob += chip->ecc.postpad;
2212 }
2213 }
2214
2215 size = mtd->oobsize - (oob - chip->oob_poi);
2216 if (size)
2217 chip->write_buf(mtd, oob, size);
Josh Wufdbad98d2012-06-25 18:07:45 +08002218
2219 return 0;
David Brownell52ff49d2009-03-04 12:01:36 -08002220}
2221/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002222 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002223 * @mtd: mtd info structure
2224 * @chip: nand chip info structure
2225 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002226 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002227 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002228 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002229static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002230 const uint8_t *buf, int oob_required,
2231 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002232{
Boris Brezillon846031d2016-02-03 20:11:00 +01002233 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002234 int eccbytes = chip->ecc.bytes;
2235 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002236 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002237 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002238
Brian Norris7854d3f2011-06-23 14:12:08 -07002239 /* Software ECC calculation */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002240 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2241 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002242
Boris Brezillon846031d2016-02-03 20:11:00 +01002243 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2244 chip->ecc.total);
2245 if (ret)
2246 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002247
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002248 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002249}
2250
2251/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002252 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002253 * @mtd: mtd info structure
2254 * @chip: nand chip info structure
2255 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002256 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002257 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002258 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002259static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002260 const uint8_t *buf, int oob_required,
2261 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002262{
Boris Brezillon846031d2016-02-03 20:11:00 +01002263 int i, eccsize = chip->ecc.size, ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002264 int eccbytes = chip->ecc.bytes;
2265 int eccsteps = chip->ecc.steps;
David Woodhouse4bf63fc2006-09-25 17:08:04 +01002266 uint8_t *ecc_calc = chip->buffers->ecccalc;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002267 const uint8_t *p = buf;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002268
2269 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2270 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
David Woodhouse29da9ce2006-05-26 23:05:44 +01002271 chip->write_buf(mtd, p, eccsize);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002272 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2273 }
2274
Boris Brezillon846031d2016-02-03 20:11:00 +01002275 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2276 chip->ecc.total);
2277 if (ret)
2278 return ret;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002279
2280 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08002281
2282 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002283}
2284
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302285
2286/**
Brian Norris73c8aaf2015-02-28 02:04:18 -08002287 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302288 * @mtd: mtd info structure
2289 * @chip: nand chip info structure
Brian Norrisd6a950802013-08-08 17:16:36 -07002290 * @offset: column address of subpage within the page
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302291 * @data_len: data length
Brian Norrisd6a950802013-08-08 17:16:36 -07002292 * @buf: data buffer
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302293 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002294 * @page: page number to write
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302295 */
2296static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2297 struct nand_chip *chip, uint32_t offset,
Brian Norrisd6a950802013-08-08 17:16:36 -07002298 uint32_t data_len, const uint8_t *buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002299 int oob_required, int page)
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302300{
2301 uint8_t *oob_buf = chip->oob_poi;
2302 uint8_t *ecc_calc = chip->buffers->ecccalc;
2303 int ecc_size = chip->ecc.size;
2304 int ecc_bytes = chip->ecc.bytes;
2305 int ecc_steps = chip->ecc.steps;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302306 uint32_t start_step = offset / ecc_size;
2307 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2308 int oob_bytes = mtd->oobsize / ecc_steps;
Boris Brezillon846031d2016-02-03 20:11:00 +01002309 int step, ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302310
2311 for (step = 0; step < ecc_steps; step++) {
2312 /* configure controller for WRITE access */
2313 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2314
2315 /* write data (untouched subpages already masked by 0xFF) */
Brian Norrisd6a950802013-08-08 17:16:36 -07002316 chip->write_buf(mtd, buf, ecc_size);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302317
2318 /* mask ECC of un-touched subpages by padding 0xFF */
2319 if ((step < start_step) || (step > end_step))
2320 memset(ecc_calc, 0xff, ecc_bytes);
2321 else
Brian Norrisd6a950802013-08-08 17:16:36 -07002322 chip->ecc.calculate(mtd, buf, ecc_calc);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302323
2324 /* mask OOB of un-touched subpages by padding 0xFF */
2325 /* if oob_required, preserve OOB metadata of written subpage */
2326 if (!oob_required || (step < start_step) || (step > end_step))
2327 memset(oob_buf, 0xff, oob_bytes);
2328
Brian Norrisd6a950802013-08-08 17:16:36 -07002329 buf += ecc_size;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302330 ecc_calc += ecc_bytes;
2331 oob_buf += oob_bytes;
2332 }
2333
2334 /* copy calculated ECC for whole page to chip->buffer->oob */
2335 /* this include masked-value(0xFF) for unwritten subpages */
2336 ecc_calc = chip->buffers->ecccalc;
Boris Brezillon846031d2016-02-03 20:11:00 +01002337 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2338 chip->ecc.total);
2339 if (ret)
2340 return ret;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302341
2342 /* write OOB buffer to NAND device */
2343 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2344
2345 return 0;
2346}
2347
2348
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002349/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002350 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
Brian Norris8b6e50c2011-05-25 14:59:01 -07002351 * @mtd: mtd info structure
2352 * @chip: nand chip info structure
2353 * @buf: data buffer
Brian Norris1fbb9382012-05-02 10:14:55 -07002354 * @oob_required: must write chip->oob_poi to OOB
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002355 * @page: page number to write
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002356 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002357 * The hw generator calculates the error syndrome automatically. Therefore we
2358 * need a special oob layout and handling.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002359 */
Josh Wufdbad98d2012-06-25 18:07:45 +08002360static int nand_write_page_syndrome(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07002361 struct nand_chip *chip,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002362 const uint8_t *buf, int oob_required,
2363 int page)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002364{
2365 int i, eccsize = chip->ecc.size;
2366 int eccbytes = chip->ecc.bytes;
2367 int eccsteps = chip->ecc.steps;
2368 const uint8_t *p = buf;
2369 uint8_t *oob = chip->oob_poi;
2370
2371 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2372
2373 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2374 chip->write_buf(mtd, p, eccsize);
2375
2376 if (chip->ecc.prepad) {
2377 chip->write_buf(mtd, oob, chip->ecc.prepad);
2378 oob += chip->ecc.prepad;
2379 }
2380
2381 chip->ecc.calculate(mtd, p, oob);
2382 chip->write_buf(mtd, oob, eccbytes);
2383 oob += eccbytes;
2384
2385 if (chip->ecc.postpad) {
2386 chip->write_buf(mtd, oob, chip->ecc.postpad);
2387 oob += chip->ecc.postpad;
2388 }
2389 }
2390
2391 /* Calculate remaining oob bytes */
Vitaly Wool7e4178f2006-06-07 09:34:37 +04002392 i = mtd->oobsize - (oob - chip->oob_poi);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002393 if (i)
2394 chip->write_buf(mtd, oob, i);
Josh Wufdbad98d2012-06-25 18:07:45 +08002395
2396 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002397}
2398
2399/**
David Woodhouse956e9442006-09-25 17:12:39 +01002400 * nand_write_page - [REPLACEABLE] write one page
Brian Norris8b6e50c2011-05-25 14:59:01 -07002401 * @mtd: MTD device structure
2402 * @chip: NAND chip descriptor
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302403 * @offset: address offset within the page
2404 * @data_len: length of actual data to be written
Brian Norris8b6e50c2011-05-25 14:59:01 -07002405 * @buf: the data to write
Brian Norris1fbb9382012-05-02 10:14:55 -07002406 * @oob_required: must write chip->oob_poi to OOB
Brian Norris8b6e50c2011-05-25 14:59:01 -07002407 * @page: page number to write
2408 * @cached: cached programming
2409 * @raw: use _raw version of write_page
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002410 */
2411static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302412 uint32_t offset, int data_len, const uint8_t *buf,
2413 int oob_required, int page, int cached, int raw)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002414{
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302415 int status, subpage;
2416
2417 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2418 chip->ecc.write_subpage)
2419 subpage = offset || (data_len < mtd->writesize);
2420 else
2421 subpage = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002422
2423 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2424
David Woodhouse956e9442006-09-25 17:12:39 +01002425 if (unlikely(raw))
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302426 status = chip->ecc.write_page_raw(mtd, chip, buf,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002427 oob_required, page);
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302428 else if (subpage)
2429 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002430 buf, oob_required, page);
David Woodhouse956e9442006-09-25 17:12:39 +01002431 else
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02002432 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2433 page);
Josh Wufdbad98d2012-06-25 18:07:45 +08002434
2435 if (status < 0)
2436 return status;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002437
2438 /*
Brian Norris7854d3f2011-06-23 14:12:08 -07002439 * Cached progamming disabled for now. Not sure if it's worth the
Brian Norris8b6e50c2011-05-25 14:59:01 -07002440 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002441 */
2442 cached = 0;
2443
Artem Bityutskiy3239a6c2013-03-04 14:56:18 +02002444 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002445
2446 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002447 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002448 /*
2449 * See if operation failed and additional status checks are
Brian Norris8b6e50c2011-05-25 14:59:01 -07002450 * available.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002451 */
2452 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2453 status = chip->errstat(mtd, chip, FL_WRITING, status,
2454 page);
2455
2456 if (status & NAND_STATUS_FAIL)
2457 return -EIO;
2458 } else {
2459 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002460 status = chip->waitfunc(mtd, chip);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002461 }
2462
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002463 return 0;
2464}
2465
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002466/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002467 * nand_fill_oob - [INTERN] Transfer client buffer to oob
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002468 * @mtd: MTD device structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07002469 * @oob: oob data buffer
2470 * @len: oob data write length
2471 * @ops: oob ops structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002472 */
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002473static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2474 struct mtd_oob_ops *ops)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002475{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002476 struct nand_chip *chip = mtd_to_nand(mtd);
Boris Brezillon846031d2016-02-03 20:11:00 +01002477 int ret;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002478
2479 /*
2480 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2481 * data from a previous OOB read.
2482 */
2483 memset(chip->oob_poi, 0xff, mtd->oobsize);
2484
Florian Fainellif8ac0412010-09-07 13:23:43 +02002485 switch (ops->mode) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002486
Brian Norris0612b9d2011-08-30 18:45:40 -07002487 case MTD_OPS_PLACE_OOB:
2488 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002489 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2490 return oob + len;
2491
Boris Brezillon846031d2016-02-03 20:11:00 +01002492 case MTD_OPS_AUTO_OOB:
2493 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2494 ops->ooboffs, len);
2495 BUG_ON(ret);
2496 return oob + len;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002497
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002498 default:
2499 BUG();
2500 }
2501 return NULL;
2502}
2503
Florian Fainellif8ac0412010-09-07 13:23:43 +02002504#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002505
2506/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002507 * nand_do_write_ops - [INTERN] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002508 * @mtd: MTD device structure
2509 * @to: offset to write to
2510 * @ops: oob operations description structure
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002511 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002512 * NAND write with ECC.
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002513 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002514static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2515 struct mtd_oob_ops *ops)
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002516{
Thomas Gleixner29072b92006-09-28 15:38:36 +02002517 int chipnr, realpage, page, blockmask, column;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002518 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002519 uint32_t writelen = ops->len;
Maxim Levitsky782ce792010-02-22 20:39:36 +02002520
2521 uint32_t oobwritelen = ops->ooblen;
Boris BREZILLON29f10582016-03-07 10:46:52 +01002522 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002523
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002524 uint8_t *oob = ops->oobbuf;
2525 uint8_t *buf = ops->datbuf;
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302526 int ret;
Brian Norrise47f3db2012-05-02 10:14:56 -07002527 int oob_required = oob ? 1 : 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002528
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002529 ops->retlen = 0;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002530 if (!writelen)
2531 return 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002532
Brian Norris8b6e50c2011-05-25 14:59:01 -07002533 /* Reject writes, which are not page aligned */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002534 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002535 pr_notice("%s: attempt to write non page aligned data\n",
2536 __func__);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002537 return -EINVAL;
2538 }
2539
Thomas Gleixner29072b92006-09-28 15:38:36 +02002540 column = to & (mtd->writesize - 1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002541
Thomas Gleixner6a930962006-06-28 00:11:45 +02002542 chipnr = (int)(to >> chip->chip_shift);
2543 chip->select_chip(mtd, chipnr);
2544
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002545 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002546 if (nand_check_wp(mtd)) {
2547 ret = -EIO;
2548 goto err_out;
2549 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002550
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002551 realpage = (int)(to >> chip->page_shift);
2552 page = realpage & chip->pagemask;
2553 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2554
2555 /* Invalidate the page cache, when we write to the cached page */
Brian Norris537ab1b2014-07-21 19:08:03 -07002556 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2557 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002558 chip->pagebuf = -1;
2559
Maxim Levitsky782ce792010-02-22 20:39:36 +02002560 /* Don't allow multipage oob writes with offset */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002561 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2562 ret = -EINVAL;
2563 goto err_out;
2564 }
Maxim Levitsky782ce792010-02-22 20:39:36 +02002565
Florian Fainellif8ac0412010-09-07 13:23:43 +02002566 while (1) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02002567 int bytes = mtd->writesize;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002568 int cached = writelen > bytes && page != blockmask;
Thomas Gleixner29072b92006-09-28 15:38:36 +02002569 uint8_t *wbuf = buf;
Kamal Dasu66507c72014-05-01 20:51:19 -04002570 int use_bufpoi;
2571 int part_pagewr = (column || writelen < (mtd->writesize - 1));
Thomas Gleixner29072b92006-09-28 15:38:36 +02002572
Kamal Dasu66507c72014-05-01 20:51:19 -04002573 if (part_pagewr)
2574 use_bufpoi = 1;
2575 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2576 use_bufpoi = !virt_addr_valid(buf);
2577 else
2578 use_bufpoi = 0;
2579
2580 /* Partial page write?, or need to use bounce buffer */
2581 if (use_bufpoi) {
2582 pr_debug("%s: using write bounce buffer for buf@%p\n",
2583 __func__, buf);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002584 cached = 0;
Kamal Dasu66507c72014-05-01 20:51:19 -04002585 if (part_pagewr)
2586 bytes = min_t(int, bytes - column, writelen);
Thomas Gleixner29072b92006-09-28 15:38:36 +02002587 chip->pagebuf = -1;
2588 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2589 memcpy(&chip->buffers->databuf[column], buf, bytes);
2590 wbuf = chip->buffers->databuf;
2591 }
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002592
Maxim Levitsky782ce792010-02-22 20:39:36 +02002593 if (unlikely(oob)) {
2594 size_t len = min(oobwritelen, oobmaxlen);
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002595 oob = nand_fill_oob(mtd, oob, len, ops);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002596 oobwritelen -= len;
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002597 } else {
2598 /* We still need to erase leftover OOB data */
2599 memset(chip->oob_poi, 0xff, mtd->oobsize);
Maxim Levitsky782ce792010-02-22 20:39:36 +02002600 }
Gupta, Pekon837a6ba2013-03-15 17:55:53 +05302601 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2602 oob_required, page, cached,
2603 (ops->mode == MTD_OPS_RAW));
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002604 if (ret)
2605 break;
2606
2607 writelen -= bytes;
2608 if (!writelen)
2609 break;
2610
Thomas Gleixner29072b92006-09-28 15:38:36 +02002611 column = 0;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002612 buf += bytes;
2613 realpage++;
2614
2615 page = realpage & chip->pagemask;
2616 /* Check, if we cross a chip boundary */
2617 if (!page) {
2618 chipnr++;
2619 chip->select_chip(mtd, -1);
2620 chip->select_chip(mtd, chipnr);
2621 }
2622 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002623
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002624 ops->retlen = ops->len - writelen;
Vitaly Wool70145682006-11-03 18:20:38 +03002625 if (unlikely(oob))
2626 ops->oobretlen = ops->ooblen;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002627
2628err_out:
2629 chip->select_chip(mtd, -1);
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02002630 return ret;
2631}
2632
2633/**
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002634 * panic_nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002635 * @mtd: MTD device structure
2636 * @to: offset to write to
2637 * @len: number of bytes to write
2638 * @retlen: pointer to variable to store the number of written bytes
2639 * @buf: the data to write
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002640 *
2641 * NAND write with ECC. Used when performing writes in interrupt context, this
2642 * may for example be called by mtdoops when writing an oops while in panic.
2643 */
2644static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2645 size_t *retlen, const uint8_t *buf)
2646{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002647 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris4a89ff82011-08-30 18:45:45 -07002648 struct mtd_oob_ops ops;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002649 int ret;
2650
Brian Norris8b6e50c2011-05-25 14:59:01 -07002651 /* Wait for the device to get ready */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002652 panic_nand_wait(mtd, chip, 400);
2653
Brian Norris8b6e50c2011-05-25 14:59:01 -07002654 /* Grab the device */
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002655 panic_nand_get_device(chip, mtd, FL_WRITING);
2656
Brian Norris0ec56dc2015-02-28 02:02:30 -08002657 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002658 ops.len = len;
2659 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002660 ops.mode = MTD_OPS_PLACE_OOB;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002661
Brian Norris4a89ff82011-08-30 18:45:45 -07002662 ret = nand_do_write_ops(mtd, to, &ops);
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002663
Brian Norris4a89ff82011-08-30 18:45:45 -07002664 *retlen = ops.retlen;
Simon Kagstrom2af7c652009-10-05 15:55:52 +02002665 return ret;
2666}
2667
2668/**
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002669 * nand_write - [MTD Interface] NAND write with ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07002670 * @mtd: MTD device structure
2671 * @to: offset to write to
2672 * @len: number of bytes to write
2673 * @retlen: pointer to variable to store the number of written bytes
2674 * @buf: the data to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002676 * NAND write with ECC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002678static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002679 size_t *retlen, const uint8_t *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Brian Norris4a89ff82011-08-30 18:45:45 -07002681 struct mtd_oob_ops ops;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002682 int ret;
2683
Huang Shijie6a8214a2012-11-19 14:43:30 +08002684 nand_get_device(mtd, FL_WRITING);
Brian Norris0ec56dc2015-02-28 02:02:30 -08002685 memset(&ops, 0, sizeof(ops));
Brian Norris4a89ff82011-08-30 18:45:45 -07002686 ops.len = len;
2687 ops.datbuf = (uint8_t *)buf;
Huang Shijie11041ae2012-07-03 16:44:14 +08002688 ops.mode = MTD_OPS_PLACE_OOB;
Brian Norris4a89ff82011-08-30 18:45:45 -07002689 ret = nand_do_write_ops(mtd, to, &ops);
Brian Norris4a89ff82011-08-30 18:45:45 -07002690 *retlen = ops.retlen;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002691 nand_release_device(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002692 return ret;
2693}
2694
2695/**
2696 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002697 * @mtd: MTD device structure
2698 * @to: offset to write to
2699 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002700 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002701 * NAND write out-of-band.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002702 */
2703static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2704 struct mtd_oob_ops *ops)
2705{
Adrian Hunter03736152007-01-31 17:58:29 +02002706 int chipnr, page, status, len;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002707 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Brian Norris289c0522011-07-19 10:06:09 -07002709 pr_debug("%s: to = 0x%08x, len = %i\n",
vimal singh20d8e242009-07-07 15:49:49 +05302710 __func__, (unsigned int)to, (int)ops->ooblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Boris BREZILLON29f10582016-03-07 10:46:52 +01002712 len = mtd_oobavail(mtd, ops);
Adrian Hunter03736152007-01-31 17:58:29 +02002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /* Do not allow write past end of page */
Adrian Hunter03736152007-01-31 17:58:29 +02002715 if ((ops->ooboffs + ops->ooblen) > len) {
Brian Norris289c0522011-07-19 10:06:09 -07002716 pr_debug("%s: attempt to write past end of page\n",
2717 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return -EINVAL;
2719 }
2720
Adrian Hunter03736152007-01-31 17:58:29 +02002721 if (unlikely(ops->ooboffs >= len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002722 pr_debug("%s: attempt to start write outside oob\n",
2723 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002724 return -EINVAL;
2725 }
2726
Jason Liu775adc32011-02-25 13:06:18 +08002727 /* Do not allow write past end of device */
Adrian Hunter03736152007-01-31 17:58:29 +02002728 if (unlikely(to >= mtd->size ||
2729 ops->ooboffs + ops->ooblen >
2730 ((mtd->size >> chip->page_shift) -
2731 (to >> chip->page_shift)) * len)) {
Brian Norris289c0522011-07-19 10:06:09 -07002732 pr_debug("%s: attempt to write beyond end of device\n",
2733 __func__);
Adrian Hunter03736152007-01-31 17:58:29 +02002734 return -EINVAL;
2735 }
2736
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002737 chipnr = (int)(to >> chip->chip_shift);
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002738 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Thomas Gleixner7314e9e2006-05-25 09:51:54 +02002740 /* Shift to get page */
2741 page = (int)(to >> chip->page_shift);
2742
2743 /*
2744 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2745 * of my DiskOnChip 2000 test units) will clear the whole data page too
2746 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2747 * it in the doc2000 driver in August 1999. dwmw2.
2748 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002749 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 /* Check, if it is write protected */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002752 if (nand_check_wp(mtd)) {
2753 chip->select_chip(mtd, -1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002754 return -EROFS;
Huang Shijieb0bb6902012-11-19 14:43:29 +08002755 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /* Invalidate the page cache, if we write to the cached page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002758 if (page == chip->pagebuf)
2759 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
THOMSON, Adam (Adam)f7220132011-06-14 16:52:38 +02002761 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
Brian Norris9ce244b2011-08-30 18:45:37 -07002762
Brian Norris0612b9d2011-08-30 18:45:40 -07002763 if (ops->mode == MTD_OPS_RAW)
Brian Norris9ce244b2011-08-30 18:45:37 -07002764 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2765 else
2766 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002767
Huang Shijieb0bb6902012-11-19 14:43:29 +08002768 chip->select_chip(mtd, -1);
2769
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002770 if (status)
2771 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Vitaly Wool70145682006-11-03 18:20:38 +03002773 ops->oobretlen = ops->ooblen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Thomas Gleixner7bc33122006-06-20 20:05:05 +02002775 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002776}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002778/**
2779 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Brian Norris8b6e50c2011-05-25 14:59:01 -07002780 * @mtd: MTD device structure
2781 * @to: offset to write to
2782 * @ops: oob operation description structure
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002783 */
2784static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2785 struct mtd_oob_ops *ops)
2786{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002787 int ret = -ENOTSUPP;
2788
2789 ops->retlen = 0;
2790
2791 /* Do not allow writes past end of device */
Vitaly Wool70145682006-11-03 18:20:38 +03002792 if (ops->datbuf && (to + ops->len) > mtd->size) {
Brian Norris289c0522011-07-19 10:06:09 -07002793 pr_debug("%s: attempt to write beyond end of device\n",
2794 __func__);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002795 return -EINVAL;
2796 }
2797
Huang Shijie6a8214a2012-11-19 14:43:30 +08002798 nand_get_device(mtd, FL_WRITING);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002799
Florian Fainellif8ac0412010-09-07 13:23:43 +02002800 switch (ops->mode) {
Brian Norris0612b9d2011-08-30 18:45:40 -07002801 case MTD_OPS_PLACE_OOB:
2802 case MTD_OPS_AUTO_OOB:
2803 case MTD_OPS_RAW:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002804 break;
2805
2806 default:
2807 goto out;
2808 }
2809
2810 if (!ops->datbuf)
2811 ret = nand_do_write_oob(mtd, to, ops);
2812 else
2813 ret = nand_do_write_ops(mtd, to, ops);
2814
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002815out:
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02002816 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return ret;
2818}
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820/**
Brian Norris49c50b92014-05-06 16:02:19 -07002821 * single_erase - [GENERIC] NAND standard block erase command function
Brian Norris8b6e50c2011-05-25 14:59:01 -07002822 * @mtd: MTD device structure
2823 * @page: the page address of the block which will be erased
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 *
Brian Norris49c50b92014-05-06 16:02:19 -07002825 * Standard erase command for NAND chips. Returns NAND status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
Brian Norris49c50b92014-05-06 16:02:19 -07002827static int single_erase(struct mtd_info *mtd, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Boris BREZILLON862eba52015-12-01 12:03:03 +01002829 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /* Send commands to erase a block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002831 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2832 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Brian Norris49c50b92014-05-06 16:02:19 -07002833
2834 return chip->waitfunc(mtd, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 * nand_erase - [MTD Interface] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002839 * @mtd: MTD device structure
2840 * @instr: erase instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002842 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002844static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
David Woodhousee0c7d762006-05-13 18:07:53 +01002846 return nand_erase_nand(mtd, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/**
Brian Norris7854d3f2011-06-23 14:12:08 -07002850 * nand_erase_nand - [INTERN] erase block(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07002851 * @mtd: MTD device structure
2852 * @instr: erase instruction
2853 * @allowbbt: allow erasing the bbt area
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002855 * Erase one ore more blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002857int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2858 int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Adrian Hunter69423d92008-12-10 13:37:21 +00002860 int page, status, pages_per_block, ret, chipnr;
Boris BREZILLON862eba52015-12-01 12:03:03 +01002861 struct nand_chip *chip = mtd_to_nand(mtd);
Adrian Hunter69423d92008-12-10 13:37:21 +00002862 loff_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Brian Norris289c0522011-07-19 10:06:09 -07002864 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2865 __func__, (unsigned long long)instr->addr,
2866 (unsigned long long)instr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Vimal Singh6fe5a6a2010-02-03 14:12:24 +05302868 if (check_offs_len(mtd, instr->addr, instr->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002872 nand_get_device(mtd, FL_ERASING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 /* Shift to get first page */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002875 page = (int)(instr->addr >> chip->page_shift);
2876 chipnr = (int)(instr->addr >> chip->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 /* Calculate pages in each block */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002879 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 /* Select the NAND device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002882 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /* Check, if it is write protected */
2885 if (nand_check_wp(mtd)) {
Brian Norris289c0522011-07-19 10:06:09 -07002886 pr_debug("%s: device is write protected!\n",
2887 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 instr->state = MTD_ERASE_FAILED;
2889 goto erase_exit;
2890 }
2891
2892 /* Loop through the pages */
2893 len = instr->len;
2894
2895 instr->state = MTD_ERASING;
2896
2897 while (len) {
Wolfram Sang12183a22011-12-21 23:01:20 +01002898 /* Check if we have a bad block, we do not erase bad blocks! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002899 if (nand_block_checkbad(mtd, ((loff_t) page) <<
Archit Taneja9f3e0422016-02-03 14:29:49 +05302900 chip->page_shift, allowbbt)) {
Brian Norrisd0370212011-07-19 10:06:08 -07002901 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2902 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 instr->state = MTD_ERASE_FAILED;
2904 goto erase_exit;
2905 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002906
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002907 /*
2908 * Invalidate the page cache, if we erase the block which
Brian Norris8b6e50c2011-05-25 14:59:01 -07002909 * contains the current cached page.
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002910 */
2911 if (page <= chip->pagebuf && chip->pagebuf <
2912 (page + pages_per_block))
2913 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Brian Norris49c50b92014-05-06 16:02:19 -07002915 status = chip->erase(mtd, page & chip->pagemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002917 /*
2918 * See if operation failed and additional status checks are
2919 * available
2920 */
2921 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2922 status = chip->errstat(mtd, chip, FL_ERASING,
2923 status, page);
David A. Marlin068e3c02005-01-24 03:07:46 +00002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 /* See if block erase succeeded */
David A. Marlina4ab4c52005-01-23 18:30:53 +00002926 if (status & NAND_STATUS_FAIL) {
Brian Norris289c0522011-07-19 10:06:09 -07002927 pr_debug("%s: failed erase, page 0x%08x\n",
2928 __func__, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +00002930 instr->fail_addr =
2931 ((loff_t)page << chip->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 goto erase_exit;
2933 }
David A. Marlin30f464b2005-01-17 18:35:25 +00002934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 /* Increment page address and decrement length */
Dan Carpenterdaae74c2013-08-09 12:49:05 +03002936 len -= (1ULL << chip->phys_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 page += pages_per_block;
2938
2939 /* Check, if we cross a chip boundary */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002940 if (len && !(page & chip->pagemask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 chipnr++;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002942 chip->select_chip(mtd, -1);
2943 chip->select_chip(mtd, chipnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
2945 }
2946 instr->state = MTD_ERASE_DONE;
2947
Florian Fainelli7351d3a2010-09-07 13:23:45 +02002948erase_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 /* Deselect and wake up anyone waiting on the device */
Huang Shijieb0bb6902012-11-19 14:43:29 +08002953 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 nand_release_device(mtd);
2955
David Woodhouse49defc02007-10-06 15:01:59 -04002956 /* Do call back function */
2957 if (!ret)
2958 mtd_erase_callback(instr);
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* Return more or less happy */
2961 return ret;
2962}
2963
2964/**
2965 * nand_sync - [MTD Interface] sync
Brian Norris8b6e50c2011-05-25 14:59:01 -07002966 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07002968 * Sync is actually a wait for chip ready function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 */
David Woodhousee0c7d762006-05-13 18:07:53 +01002970static void nand_sync(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Brian Norris289c0522011-07-19 10:06:09 -07002972 pr_debug("%s: called\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 /* Grab the lock and see if the device is available */
Huang Shijie6a8214a2012-11-19 14:43:30 +08002975 nand_get_device(mtd, FL_SYNCING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /* Release it and go back */
David Woodhousee0c7d762006-05-13 18:07:53 +01002977 nand_release_device(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002981 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07002982 * @mtd: MTD device structure
2983 * @offs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02002985static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Archit Taneja9f3e0422016-02-03 14:29:49 +05302987 struct nand_chip *chip = mtd_to_nand(mtd);
2988 int chipnr = (int)(offs >> chip->chip_shift);
2989 int ret;
2990
2991 /* Select the NAND device */
2992 nand_get_device(mtd, FL_READING);
2993 chip->select_chip(mtd, chipnr);
2994
2995 ret = nand_block_checkbad(mtd, offs, 0);
2996
2997 chip->select_chip(mtd, -1);
2998 nand_release_device(mtd);
2999
3000 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003/**
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003004 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07003005 * @mtd: MTD device structure
3006 * @ofs: offset relative to mtd start
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 */
David Woodhousee0c7d762006-05-13 18:07:53 +01003008static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 int ret;
3011
Florian Fainellif8ac0412010-09-07 13:23:43 +02003012 ret = nand_block_isbad(mtd, ofs);
3013 if (ret) {
Brian Norris8b6e50c2011-05-25 14:59:01 -07003014 /* If it was bad already, return success and do nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (ret > 0)
3016 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01003017 return ret;
3018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Brian Norris5a0edb22013-07-30 17:52:58 -07003020 return nand_block_markbad_lowlevel(mtd, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023/**
Huang Shijie7db03ec2012-09-13 14:57:52 +08003024 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3025 * @mtd: MTD device structure
3026 * @chip: nand chip info structure
3027 * @addr: feature address.
3028 * @subfeature_param: the subfeature parameters, a four bytes array.
3029 */
3030static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3031 int addr, uint8_t *subfeature_param)
3032{
3033 int status;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003034 int i;
Huang Shijie7db03ec2012-09-13 14:57:52 +08003035
David Mosbergerd914c932013-05-29 15:30:13 +03003036 if (!chip->onfi_version ||
3037 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3038 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003039 return -EINVAL;
3040
3041 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003042 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3043 chip->write_byte(mtd, subfeature_param[i]);
3044
Huang Shijie7db03ec2012-09-13 14:57:52 +08003045 status = chip->waitfunc(mtd, chip);
3046 if (status & NAND_STATUS_FAIL)
3047 return -EIO;
3048 return 0;
3049}
3050
3051/**
3052 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3053 * @mtd: MTD device structure
3054 * @chip: nand chip info structure
3055 * @addr: feature address.
3056 * @subfeature_param: the subfeature parameters, a four bytes array.
3057 */
3058static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3059 int addr, uint8_t *subfeature_param)
3060{
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003061 int i;
3062
David Mosbergerd914c932013-05-29 15:30:13 +03003063 if (!chip->onfi_version ||
3064 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3065 & ONFI_OPT_CMD_SET_GET_FEATURES))
Huang Shijie7db03ec2012-09-13 14:57:52 +08003066 return -EINVAL;
3067
Huang Shijie7db03ec2012-09-13 14:57:52 +08003068 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003069 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3070 *subfeature_param++ = chip->read_byte(mtd);
Huang Shijie7db03ec2012-09-13 14:57:52 +08003071 return 0;
3072}
3073
3074/**
Vitaly Wool962034f2005-09-15 14:58:53 +01003075 * nand_suspend - [MTD Interface] Suspend the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003076 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003077 */
3078static int nand_suspend(struct mtd_info *mtd)
3079{
Huang Shijie6a8214a2012-11-19 14:43:30 +08003080 return nand_get_device(mtd, FL_PM_SUSPENDED);
Vitaly Wool962034f2005-09-15 14:58:53 +01003081}
3082
3083/**
3084 * nand_resume - [MTD Interface] Resume the NAND flash
Brian Norris8b6e50c2011-05-25 14:59:01 -07003085 * @mtd: MTD device structure
Vitaly Wool962034f2005-09-15 14:58:53 +01003086 */
3087static void nand_resume(struct mtd_info *mtd)
3088{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003089 struct nand_chip *chip = mtd_to_nand(mtd);
Vitaly Wool962034f2005-09-15 14:58:53 +01003090
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003091 if (chip->state == FL_PM_SUSPENDED)
Vitaly Wool962034f2005-09-15 14:58:53 +01003092 nand_release_device(mtd);
3093 else
Brian Norrisd0370212011-07-19 10:06:08 -07003094 pr_err("%s called for a chip which is not in suspended state\n",
3095 __func__);
Vitaly Wool962034f2005-09-15 14:58:53 +01003096}
3097
Scott Branden72ea4032014-11-20 11:18:05 -08003098/**
3099 * nand_shutdown - [MTD Interface] Finish the current NAND operation and
3100 * prevent further operations
3101 * @mtd: MTD device structure
3102 */
3103static void nand_shutdown(struct mtd_info *mtd)
3104{
Brian Norris9ca641b2015-11-09 16:37:28 -08003105 nand_get_device(mtd, FL_PM_SUSPENDED);
Scott Branden72ea4032014-11-20 11:18:05 -08003106}
3107
Brian Norris8b6e50c2011-05-25 14:59:01 -07003108/* Set default functions */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003109static void nand_set_defaults(struct nand_chip *chip, int busw)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /* check for proper chip_delay setup, set 20us if not */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003112 if (!chip->chip_delay)
3113 chip->chip_delay = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* check, if a user supplied command function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003116 if (chip->cmdfunc == NULL)
3117 chip->cmdfunc = nand_command;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 /* check, if a user supplied wait function given */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003120 if (chip->waitfunc == NULL)
3121 chip->waitfunc = nand_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003123 if (!chip->select_chip)
3124 chip->select_chip = nand_select_chip;
Brian Norris68e80782013-07-18 01:17:02 -07003125
Huang Shijie4204ccc2013-08-16 10:10:07 +08003126 /* set for ONFI nand */
3127 if (!chip->onfi_set_features)
3128 chip->onfi_set_features = nand_onfi_set_features;
3129 if (!chip->onfi_get_features)
3130 chip->onfi_get_features = nand_onfi_get_features;
3131
Brian Norris68e80782013-07-18 01:17:02 -07003132 /* If called twice, pointers that depend on busw may need to be reset */
3133 if (!chip->read_byte || chip->read_byte == nand_read_byte)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003134 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3135 if (!chip->read_word)
3136 chip->read_word = nand_read_word;
3137 if (!chip->block_bad)
3138 chip->block_bad = nand_block_bad;
3139 if (!chip->block_markbad)
3140 chip->block_markbad = nand_default_block_markbad;
Brian Norris68e80782013-07-18 01:17:02 -07003141 if (!chip->write_buf || chip->write_buf == nand_write_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003142 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Uwe Kleine-König05f78352013-12-05 22:22:04 +01003143 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3144 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
Brian Norris68e80782013-07-18 01:17:02 -07003145 if (!chip->read_buf || chip->read_buf == nand_read_buf)
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003146 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003147 if (!chip->scan_bbt)
3148 chip->scan_bbt = nand_default_bbt;
Thomas Gleixnerf75e5092006-05-26 18:52:08 +02003149
3150 if (!chip->controller) {
3151 chip->controller = &chip->hwcontrol;
3152 spin_lock_init(&chip->controller->lock);
3153 init_waitqueue_head(&chip->controller->wq);
3154 }
3155
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003156}
3157
Brian Norris8b6e50c2011-05-25 14:59:01 -07003158/* Sanitize ONFI strings so we can safely print them */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003159static void sanitize_string(uint8_t *s, size_t len)
3160{
3161 ssize_t i;
3162
Brian Norris8b6e50c2011-05-25 14:59:01 -07003163 /* Null terminate */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003164 s[len - 1] = 0;
3165
Brian Norris8b6e50c2011-05-25 14:59:01 -07003166 /* Remove non printable chars */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003167 for (i = 0; i < len - 1; i++) {
3168 if (s[i] < ' ' || s[i] > 127)
3169 s[i] = '?';
3170 }
3171
Brian Norris8b6e50c2011-05-25 14:59:01 -07003172 /* Remove trailing spaces */
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003173 strim(s);
3174}
3175
3176static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3177{
3178 int i;
3179 while (len--) {
3180 crc ^= *p++ << 8;
3181 for (i = 0; i < 8; i++)
3182 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3183 }
3184
3185 return crc;
3186}
3187
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003188/* Parse the Extended Parameter Page. */
3189static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3190 struct nand_chip *chip, struct nand_onfi_params *p)
3191{
3192 struct onfi_ext_param_page *ep;
3193 struct onfi_ext_section *s;
3194 struct onfi_ext_ecc_info *ecc;
3195 uint8_t *cursor;
3196 int ret = -EINVAL;
3197 int len;
3198 int i;
3199
3200 len = le16_to_cpu(p->ext_param_page_length) * 16;
3201 ep = kmalloc(len, GFP_KERNEL);
Brian Norris5cb13272013-09-16 17:59:20 -07003202 if (!ep)
3203 return -ENOMEM;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003204
3205 /* Send our own NAND_CMD_PARAM. */
3206 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3207
3208 /* Use the Change Read Column command to skip the ONFI param pages. */
3209 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3210 sizeof(*p) * p->num_of_param_pages , -1);
3211
3212 /* Read out the Extended Parameter Page. */
3213 chip->read_buf(mtd, (uint8_t *)ep, len);
3214 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3215 != le16_to_cpu(ep->crc))) {
3216 pr_debug("fail in the CRC.\n");
3217 goto ext_out;
3218 }
3219
3220 /*
3221 * Check the signature.
3222 * Do not strictly follow the ONFI spec, maybe changed in future.
3223 */
3224 if (strncmp(ep->sig, "EPPS", 4)) {
3225 pr_debug("The signature is invalid.\n");
3226 goto ext_out;
3227 }
3228
3229 /* find the ECC section. */
3230 cursor = (uint8_t *)(ep + 1);
3231 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3232 s = ep->sections + i;
3233 if (s->type == ONFI_SECTION_TYPE_2)
3234 break;
3235 cursor += s->length * 16;
3236 }
3237 if (i == ONFI_EXT_SECTION_MAX) {
3238 pr_debug("We can not find the ECC section.\n");
3239 goto ext_out;
3240 }
3241
3242 /* get the info we want. */
3243 ecc = (struct onfi_ext_ecc_info *)cursor;
3244
Brian Norris4ae7d222013-09-16 18:20:21 -07003245 if (!ecc->codeword_size) {
3246 pr_debug("Invalid codeword size\n");
3247 goto ext_out;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003248 }
3249
Brian Norris4ae7d222013-09-16 18:20:21 -07003250 chip->ecc_strength_ds = ecc->ecc_bits;
3251 chip->ecc_step_ds = 1 << ecc->codeword_size;
Brian Norris5cb13272013-09-16 17:59:20 -07003252 ret = 0;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003253
3254ext_out:
3255 kfree(ep);
3256 return ret;
3257}
3258
Brian Norris8429bb32013-12-03 15:51:09 -08003259static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3260{
Boris BREZILLON862eba52015-12-01 12:03:03 +01003261 struct nand_chip *chip = mtd_to_nand(mtd);
Brian Norris8429bb32013-12-03 15:51:09 -08003262 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3263
3264 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3265 feature);
3266}
3267
3268/*
3269 * Configure chip properties from Micron vendor-specific ONFI table
3270 */
3271static void nand_onfi_detect_micron(struct nand_chip *chip,
3272 struct nand_onfi_params *p)
3273{
3274 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3275
3276 if (le16_to_cpu(p->vendor_revision) < 1)
3277 return;
3278
3279 chip->read_retries = micron->read_retry_options;
3280 chip->setup_read_retry = nand_setup_read_retry_micron;
3281}
3282
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003283/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003284 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003285 */
3286static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003287 int *busw)
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003288{
3289 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003290 int i, j;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003291 int val;
3292
Brian Norris7854d3f2011-06-23 14:12:08 -07003293 /* Try ONFI for unknown chip or LP */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003294 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3295 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3296 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3297 return 0;
3298
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003299 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3300 for (i = 0; i < 3; i++) {
Brian Norrisbd9c6e92013-11-29 22:04:28 -08003301 for (j = 0; j < sizeof(*p); j++)
3302 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003303 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3304 le16_to_cpu(p->crc)) {
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003305 break;
3306 }
3307 }
3308
Brian Norrisc7f23a72013-08-13 10:51:55 -07003309 if (i == 3) {
3310 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003311 return 0;
Brian Norrisc7f23a72013-08-13 10:51:55 -07003312 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003313
Brian Norris8b6e50c2011-05-25 14:59:01 -07003314 /* Check version */
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003315 val = le16_to_cpu(p->revision);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003316 if (val & (1 << 5))
3317 chip->onfi_version = 23;
3318 else if (val & (1 << 4))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003319 chip->onfi_version = 22;
3320 else if (val & (1 << 3))
3321 chip->onfi_version = 21;
3322 else if (val & (1 << 2))
3323 chip->onfi_version = 20;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003324 else if (val & (1 << 1))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003325 chip->onfi_version = 10;
Brian Norrisb7b1a292010-12-12 00:23:33 -08003326
3327 if (!chip->onfi_version) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003328 pr_info("unsupported ONFI version: %d\n", val);
Brian Norrisb7b1a292010-12-12 00:23:33 -08003329 return 0;
3330 }
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003331
3332 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3333 sanitize_string(p->model, sizeof(p->model));
3334 if (!mtd->name)
3335 mtd->name = p->model;
Brian Norris4355b702013-08-27 18:45:10 -07003336
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003337 mtd->writesize = le32_to_cpu(p->byte_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003338
3339 /*
3340 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3341 * (don't ask me who thought of this...). MTD assumes that these
3342 * dimensions will be power-of-2, so just truncate the remaining area.
3343 */
3344 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3345 mtd->erasesize *= mtd->writesize;
3346
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003347 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Brian Norris4355b702013-08-27 18:45:10 -07003348
3349 /* See erasesize comment */
3350 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTET63795752012-03-19 15:35:25 +01003351 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Huang Shijie13fbd172013-09-25 14:58:13 +08003352 chip->bits_per_cell = p->bits_per_cell;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003353
3354 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Matthieu CASTET08c248f2011-06-26 18:26:55 +02003355 *busw = NAND_BUSWIDTH_16;
Huang Shijiee2985fc2013-05-17 11:17:30 +08003356 else
3357 *busw = 0;
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003358
Huang Shijie10c86ba2013-05-17 11:17:26 +08003359 if (p->ecc_bits != 0xff) {
3360 chip->ecc_strength_ds = p->ecc_bits;
3361 chip->ecc_step_ds = 512;
Huang Shijie6dcbe0c2013-05-22 10:28:27 +08003362 } else if (chip->onfi_version >= 21 &&
3363 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3364
3365 /*
3366 * The nand_flash_detect_ext_param_page() uses the
3367 * Change Read Column command which maybe not supported
3368 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3369 * now. We do not replace user supplied command function.
3370 */
3371 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3372 chip->cmdfunc = nand_command_lp;
3373
3374 /* The Extended Parameter Page is supported since ONFI 2.1. */
3375 if (nand_flash_detect_ext_param_page(mtd, chip, p))
Brian Norrisc7f23a72013-08-13 10:51:55 -07003376 pr_warn("Failed to detect ONFI extended param page\n");
3377 } else {
3378 pr_warn("Could not retrieve ONFI ECC requirements\n");
Huang Shijie10c86ba2013-05-17 11:17:26 +08003379 }
3380
Brian Norris8429bb32013-12-03 15:51:09 -08003381 if (p->jedec_id == NAND_MFR_MICRON)
3382 nand_onfi_detect_micron(chip, p);
3383
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003384 return 1;
3385}
3386
3387/*
Huang Shijie91361812014-02-21 13:39:40 +08003388 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3389 */
3390static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3391 int *busw)
3392{
3393 struct nand_jedec_params *p = &chip->jedec_params;
3394 struct jedec_ecc_info *ecc;
3395 int val;
3396 int i, j;
3397
3398 /* Try JEDEC for unknown chip or LP */
3399 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3400 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3401 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3402 chip->read_byte(mtd) != 'C')
3403 return 0;
3404
3405 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3406 for (i = 0; i < 3; i++) {
3407 for (j = 0; j < sizeof(*p); j++)
3408 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3409
3410 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3411 le16_to_cpu(p->crc))
3412 break;
3413 }
3414
3415 if (i == 3) {
3416 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3417 return 0;
3418 }
3419
3420 /* Check version */
3421 val = le16_to_cpu(p->revision);
3422 if (val & (1 << 2))
3423 chip->jedec_version = 10;
3424 else if (val & (1 << 1))
3425 chip->jedec_version = 1; /* vendor specific version */
3426
3427 if (!chip->jedec_version) {
3428 pr_info("unsupported JEDEC version: %d\n", val);
3429 return 0;
3430 }
3431
3432 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3433 sanitize_string(p->model, sizeof(p->model));
3434 if (!mtd->name)
3435 mtd->name = p->model;
3436
3437 mtd->writesize = le32_to_cpu(p->byte_per_page);
3438
3439 /* Please reference to the comment for nand_flash_detect_onfi. */
3440 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3441 mtd->erasesize *= mtd->writesize;
3442
3443 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3444
3445 /* Please reference to the comment for nand_flash_detect_onfi. */
3446 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3447 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3448 chip->bits_per_cell = p->bits_per_cell;
3449
3450 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3451 *busw = NAND_BUSWIDTH_16;
3452 else
3453 *busw = 0;
3454
3455 /* ECC info */
3456 ecc = &p->ecc_info[0];
3457
3458 if (ecc->codeword_size >= 9) {
3459 chip->ecc_strength_ds = ecc->ecc_bits;
3460 chip->ecc_step_ds = 1 << ecc->codeword_size;
3461 } else {
3462 pr_warn("Invalid codeword size\n");
3463 }
3464
3465 return 1;
3466}
3467
3468/*
Brian Norrise3b88bd2012-09-24 20:40:52 -07003469 * nand_id_has_period - Check if an ID string has a given wraparound period
3470 * @id_data: the ID string
3471 * @arrlen: the length of the @id_data array
3472 * @period: the period of repitition
3473 *
3474 * Check if an ID string is repeated within a given sequence of bytes at
3475 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Brian Norrisd4d4f1b2012-11-14 21:54:20 -08003476 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Brian Norrise3b88bd2012-09-24 20:40:52 -07003477 * if the repetition has a period of @period; otherwise, returns zero.
3478 */
3479static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3480{
3481 int i, j;
3482 for (i = 0; i < period; i++)
3483 for (j = i + period; j < arrlen; j += period)
3484 if (id_data[i] != id_data[j])
3485 return 0;
3486 return 1;
3487}
3488
3489/*
3490 * nand_id_len - Get the length of an ID string returned by CMD_READID
3491 * @id_data: the ID string
3492 * @arrlen: the length of the @id_data array
3493
3494 * Returns the length of the ID string, according to known wraparound/trailing
3495 * zero patterns. If no pattern exists, returns the length of the array.
3496 */
3497static int nand_id_len(u8 *id_data, int arrlen)
3498{
3499 int last_nonzero, period;
3500
3501 /* Find last non-zero byte */
3502 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3503 if (id_data[last_nonzero])
3504 break;
3505
3506 /* All zeros */
3507 if (last_nonzero < 0)
3508 return 0;
3509
3510 /* Calculate wraparound period */
3511 for (period = 1; period < arrlen; period++)
3512 if (nand_id_has_period(id_data, arrlen, period))
3513 break;
3514
3515 /* There's a repeated pattern */
3516 if (period < arrlen)
3517 return period;
3518
3519 /* There are trailing zeros */
3520 if (last_nonzero < arrlen - 1)
3521 return last_nonzero + 1;
3522
3523 /* No pattern detected */
3524 return arrlen;
3525}
3526
Huang Shijie7db906b2013-09-25 14:58:11 +08003527/* Extract the bits of per cell from the 3rd byte of the extended ID */
3528static int nand_get_bits_per_cell(u8 cellinfo)
3529{
3530 int bits;
3531
3532 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3533 bits >>= NAND_CI_CELLTYPE_SHIFT;
3534 return bits + 1;
3535}
3536
Brian Norrise3b88bd2012-09-24 20:40:52 -07003537/*
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003538 * Many new NAND share similar device ID codes, which represent the size of the
3539 * chip. The rest of the parameters must be decoded according to generic or
3540 * manufacturer-specific "extended ID" decoding patterns.
3541 */
3542static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3543 u8 id_data[8], int *busw)
3544{
Brian Norrise3b88bd2012-09-24 20:40:52 -07003545 int extid, id_len;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003546 /* The 3rd id byte holds MLC / multichip data */
Huang Shijie7db906b2013-09-25 14:58:11 +08003547 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003548 /* The 4th id byte is the important one */
3549 extid = id_data[3];
3550
Brian Norrise3b88bd2012-09-24 20:40:52 -07003551 id_len = nand_id_len(id_data, 8);
3552
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003553 /*
3554 * Field definitions are in the following datasheets:
3555 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
Brian Norrisaf451af2012-10-09 23:26:06 -07003556 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
Brian Norris73ca3922012-09-24 20:40:54 -07003557 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003558 *
Brian Norrisaf451af2012-10-09 23:26:06 -07003559 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3560 * ID to decide what to do.
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003561 */
Brian Norrisaf451af2012-10-09 23:26:06 -07003562 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003563 !nand_is_slc(chip) && id_data[5] != 0x00) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003564 /* Calc pagesize */
3565 mtd->writesize = 2048 << (extid & 0x03);
3566 extid >>= 2;
3567 /* Calc oobsize */
Brian Norrise2d3a352012-09-24 20:40:55 -07003568 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003569 case 1:
3570 mtd->oobsize = 128;
3571 break;
3572 case 2:
3573 mtd->oobsize = 218;
3574 break;
3575 case 3:
3576 mtd->oobsize = 400;
3577 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003578 case 4:
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003579 mtd->oobsize = 436;
3580 break;
Brian Norrise2d3a352012-09-24 20:40:55 -07003581 case 5:
3582 mtd->oobsize = 512;
3583 break;
3584 case 6:
Brian Norrise2d3a352012-09-24 20:40:55 -07003585 mtd->oobsize = 640;
3586 break;
Huang Shijie94d04e82013-12-25 17:18:55 +08003587 case 7:
3588 default: /* Other cases are "reserved" (unknown) */
3589 mtd->oobsize = 1024;
3590 break;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003591 }
3592 extid >>= 2;
3593 /* Calc blocksize */
3594 mtd->erasesize = (128 * 1024) <<
3595 (((extid >> 1) & 0x04) | (extid & 0x03));
3596 *busw = 0;
Brian Norris73ca3922012-09-24 20:40:54 -07003597 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003598 !nand_is_slc(chip)) {
Brian Norris73ca3922012-09-24 20:40:54 -07003599 unsigned int tmp;
3600
3601 /* Calc pagesize */
3602 mtd->writesize = 2048 << (extid & 0x03);
3603 extid >>= 2;
3604 /* Calc oobsize */
3605 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3606 case 0:
3607 mtd->oobsize = 128;
3608 break;
3609 case 1:
3610 mtd->oobsize = 224;
3611 break;
3612 case 2:
3613 mtd->oobsize = 448;
3614 break;
3615 case 3:
3616 mtd->oobsize = 64;
3617 break;
3618 case 4:
3619 mtd->oobsize = 32;
3620 break;
3621 case 5:
3622 mtd->oobsize = 16;
3623 break;
3624 default:
3625 mtd->oobsize = 640;
3626 break;
3627 }
3628 extid >>= 2;
3629 /* Calc blocksize */
3630 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3631 if (tmp < 0x03)
3632 mtd->erasesize = (128 * 1024) << tmp;
3633 else if (tmp == 0x03)
3634 mtd->erasesize = 768 * 1024;
3635 else
3636 mtd->erasesize = (64 * 1024) << tmp;
3637 *busw = 0;
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003638 } else {
3639 /* Calc pagesize */
3640 mtd->writesize = 1024 << (extid & 0x03);
3641 extid >>= 2;
3642 /* Calc oobsize */
3643 mtd->oobsize = (8 << (extid & 0x01)) *
3644 (mtd->writesize >> 9);
3645 extid >>= 2;
3646 /* Calc blocksize. Blocksize is multiples of 64KiB */
3647 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3648 extid >>= 2;
3649 /* Get buswidth information */
3650 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Brian Norris60c67382013-06-25 13:17:59 -07003651
3652 /*
3653 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3654 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3655 * follows:
3656 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3657 * 110b -> 24nm
3658 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3659 */
3660 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
Huang Shijie1d0ed692013-09-25 14:58:10 +08003661 nand_is_slc(chip) &&
Brian Norris60c67382013-06-25 13:17:59 -07003662 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3663 !(id_data[4] & 0x80) /* !BENAND */) {
3664 mtd->oobsize = 32 * mtd->writesize >> 9;
3665 }
3666
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003667 }
3668}
3669
3670/*
Brian Norrisf23a4812012-09-24 20:40:51 -07003671 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3672 * decodes a matching ID table entry and assigns the MTD size parameters for
3673 * the chip.
3674 */
3675static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3676 struct nand_flash_dev *type, u8 id_data[8],
3677 int *busw)
3678{
3679 int maf_id = id_data[0];
3680
3681 mtd->erasesize = type->erasesize;
3682 mtd->writesize = type->pagesize;
3683 mtd->oobsize = mtd->writesize / 32;
3684 *busw = type->options & NAND_BUSWIDTH_16;
3685
Huang Shijie1c195e92013-09-25 14:58:12 +08003686 /* All legacy ID NAND are small-page, SLC */
3687 chip->bits_per_cell = 1;
3688
Brian Norrisf23a4812012-09-24 20:40:51 -07003689 /*
3690 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3691 * some Spansion chips have erasesize that conflicts with size
3692 * listed in nand_ids table.
3693 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3694 */
3695 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3696 && id_data[6] == 0x00 && id_data[7] == 0x00
3697 && mtd->writesize == 512) {
3698 mtd->erasesize = 128 * 1024;
3699 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3700 }
3701}
3702
3703/*
Brian Norris7e74c2d2012-09-24 20:40:49 -07003704 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3705 * heuristic patterns using various detected parameters (e.g., manufacturer,
3706 * page size, cell-type information).
3707 */
3708static void nand_decode_bbm_options(struct mtd_info *mtd,
3709 struct nand_chip *chip, u8 id_data[8])
3710{
3711 int maf_id = id_data[0];
3712
3713 /* Set the bad block position */
3714 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3715 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3716 else
3717 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3718
3719 /*
3720 * Bad block marker is stored in the last page of each block on Samsung
3721 * and Hynix MLC devices; stored in first two pages of each block on
3722 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3723 * AMD/Spansion, and Macronix. All others scan only the first page.
3724 */
Huang Shijie1d0ed692013-09-25 14:58:10 +08003725 if (!nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003726 (maf_id == NAND_MFR_SAMSUNG ||
3727 maf_id == NAND_MFR_HYNIX))
3728 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Huang Shijie1d0ed692013-09-25 14:58:10 +08003729 else if ((nand_is_slc(chip) &&
Brian Norris7e74c2d2012-09-24 20:40:49 -07003730 (maf_id == NAND_MFR_SAMSUNG ||
3731 maf_id == NAND_MFR_HYNIX ||
3732 maf_id == NAND_MFR_TOSHIBA ||
3733 maf_id == NAND_MFR_AMD ||
3734 maf_id == NAND_MFR_MACRONIX)) ||
3735 (mtd->writesize == 2048 &&
3736 maf_id == NAND_MFR_MICRON))
3737 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3738}
3739
Huang Shijieec6e87e2013-03-15 11:01:00 +08003740static inline bool is_full_id_nand(struct nand_flash_dev *type)
3741{
3742 return type->id_len;
3743}
3744
3745static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3746 struct nand_flash_dev *type, u8 *id_data, int *busw)
3747{
3748 if (!strncmp(type->id, id_data, type->id_len)) {
3749 mtd->writesize = type->pagesize;
3750 mtd->erasesize = type->erasesize;
3751 mtd->oobsize = type->oobsize;
3752
Huang Shijie7db906b2013-09-25 14:58:11 +08003753 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Huang Shijieec6e87e2013-03-15 11:01:00 +08003754 chip->chipsize = (uint64_t)type->chipsize << 20;
3755 chip->options |= type->options;
Huang Shijie57219342013-05-17 11:17:32 +08003756 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3757 chip->ecc_step_ds = NAND_ECC_STEP(type);
Boris BREZILLON57a94e22014-09-22 20:11:50 +02003758 chip->onfi_timing_mode_default =
3759 type->onfi_timing_mode_default;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003760
3761 *busw = type->options & NAND_BUSWIDTH_16;
3762
Cai Zhiyong092b6a12013-12-25 21:19:21 +08003763 if (!mtd->name)
3764 mtd->name = type->name;
3765
Huang Shijieec6e87e2013-03-15 11:01:00 +08003766 return true;
3767 }
3768 return false;
3769}
3770
Brian Norris7e74c2d2012-09-24 20:40:49 -07003771/*
Brian Norris8b6e50c2011-05-25 14:59:01 -07003772 * Get the flash and manufacturer id and lookup if the type is supported.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003773 */
3774static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003775 struct nand_chip *chip,
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003776 int *maf_id, int *dev_id,
David Woodhouse5e81e882010-02-26 18:32:56 +00003777 struct nand_flash_dev *type)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003778{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003779 int busw;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003780 int i, maf_idx;
Kevin Cernekee426c4572010-05-04 20:58:03 -07003781 u8 id_data[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
3783 /* Select the device */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003784 chip->select_chip(mtd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Karl Beldanef89a882008-09-15 14:37:29 +02003786 /*
3787 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Brian Norris8b6e50c2011-05-25 14:59:01 -07003788 * after power-up.
Karl Beldanef89a882008-09-15 14:37:29 +02003789 */
3790 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003793 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
3795 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003796 *maf_id = chip->read_byte(mtd);
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003797 *dev_id = chip->read_byte(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Brian Norris8b6e50c2011-05-25 14:59:01 -07003799 /*
3800 * Try again to make sure, as some systems the bus-hold or other
Ben Dooksed8165c2008-04-14 14:58:58 +01003801 * interface concerns can cause random data which looks like a
3802 * possibly credible NAND flash to appear. If the two results do
3803 * not match, ignore the device completely.
3804 */
3805
3806 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3807
Brian Norris4aef9b72012-09-24 20:40:48 -07003808 /* Read entire ID string */
3809 for (i = 0; i < 8; i++)
Kevin Cernekee426c4572010-05-04 20:58:03 -07003810 id_data[i] = chip->read_byte(mtd);
Ben Dooksed8165c2008-04-14 14:58:58 +01003811
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003812 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Ezequiel Garcia20171642013-11-25 08:30:31 -03003813 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003814 *maf_id, *dev_id, id_data[0], id_data[1]);
Ben Dooksed8165c2008-04-14 14:58:58 +01003815 return ERR_PTR(-ENODEV);
3816 }
3817
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003818 if (!type)
David Woodhouse5e81e882010-02-26 18:32:56 +00003819 type = nand_flash_ids;
3820
Huang Shijieec6e87e2013-03-15 11:01:00 +08003821 for (; type->name != NULL; type++) {
3822 if (is_full_id_nand(type)) {
3823 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3824 goto ident_done;
3825 } else if (*dev_id == type->dev_id) {
Brian Norrisdb5b09f2015-05-22 10:43:12 -07003826 break;
Huang Shijieec6e87e2013-03-15 11:01:00 +08003827 }
3828 }
David Woodhouse5e81e882010-02-26 18:32:56 +00003829
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003830 chip->onfi_version = 0;
3831 if (!type->name || !type->pagesize) {
Masahiro Yamada35fc5192014-04-09 16:26:26 +09003832 /* Check if the chip is ONFI compliant */
Brian Norris47450b32012-09-24 20:40:47 -07003833 if (nand_flash_detect_onfi(mtd, chip, &busw))
Florian Fainelli6fb277b2010-09-01 22:28:59 +02003834 goto ident_done;
Huang Shijie91361812014-02-21 13:39:40 +08003835
3836 /* Check if the chip is JEDEC compliant */
3837 if (nand_flash_detect_jedec(mtd, chip, &busw))
3838 goto ident_done;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003839 }
3840
David Woodhouse5e81e882010-02-26 18:32:56 +00003841 if (!type->name)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003842 return ERR_PTR(-ENODEV);
3843
Thomas Gleixnerba0251f2006-05-27 01:02:13 +02003844 if (!mtd->name)
3845 mtd->name = type->name;
3846
Adrian Hunter69423d92008-12-10 13:37:21 +00003847 chip->chipsize = (uint64_t)type->chipsize << 20;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003848
Boris BREZILLONa7f5ba42015-10-01 16:58:27 +02003849 if (!type->pagesize) {
Brian Norrisfc09bbc2012-09-24 20:40:50 -07003850 /* Decode parameters from extended ID */
3851 nand_decode_ext_id(mtd, chip, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003852 } else {
Brian Norrisf23a4812012-09-24 20:40:51 -07003853 nand_decode_id(mtd, chip, type, id_data, &busw);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003854 }
Brian Norrisbf7a01b2012-07-13 09:28:24 -07003855 /* Get chip options */
3856 chip->options |= type->options;
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003857
Brian Norris8b6e50c2011-05-25 14:59:01 -07003858 /*
3859 * Check if chip is not a Samsung device. Do not clear the
3860 * options for chips which do not have an extended id.
Florian Fainellid1e1f4e2010-08-30 18:32:24 +02003861 */
3862 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3863 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3864ident_done:
3865
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003866 /* Try to identify manufacturer */
David Woodhouse9a909862006-07-15 13:26:18 +01003867 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003868 if (nand_manuf_ids[maf_idx].id == *maf_id)
3869 break;
3870 }
3871
Matthieu CASTET64b37b22012-11-06 11:51:44 +01003872 if (chip->options & NAND_BUSWIDTH_AUTO) {
3873 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3874 chip->options |= busw;
3875 nand_set_defaults(chip, busw);
3876 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3877 /*
3878 * Check, if buswidth is correct. Hardware drivers should set
3879 * chip correct!
3880 */
Ezequiel Garcia20171642013-11-25 08:30:31 -03003881 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3882 *maf_id, *dev_id);
3883 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3884 pr_warn("bus width %d instead %d bit\n",
Brian Norrisd0370212011-07-19 10:06:08 -07003885 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3886 busw ? 16 : 8);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003887 return ERR_PTR(-EINVAL);
3888 }
3889
Brian Norris7e74c2d2012-09-24 20:40:49 -07003890 nand_decode_bbm_options(mtd, chip, id_data);
3891
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003892 /* Calculate the address shift from the page size */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003893 chip->page_shift = ffs(mtd->writesize) - 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07003894 /* Convert chipsize to number of pages per chip -1 */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003895 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003896
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003897 chip->bbt_erase_shift = chip->phys_erase_shift =
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003898 ffs(mtd->erasesize) - 1;
Adrian Hunter69423d92008-12-10 13:37:21 +00003899 if (chip->chipsize & 0xffffffff)
3900 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Florian Fainelli7351d3a2010-09-07 13:23:45 +02003901 else {
3902 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3903 chip->chip_shift += 32 - 1;
3904 }
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003905
Artem Bityutskiy26d9be12011-04-28 20:26:59 +03003906 chip->badblockbits = 8;
Brian Norris49c50b92014-05-06 16:02:19 -07003907 chip->erase = single_erase;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003908
Brian Norris8b6e50c2011-05-25 14:59:01 -07003909 /* Do not replace user supplied command function! */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02003910 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3911 chip->cmdfunc = nand_command_lp;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003912
Ezequiel Garcia20171642013-11-25 08:30:31 -03003913 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3914 *maf_id, *dev_id);
Huang Shijieffdac6cd2014-02-21 13:39:41 +08003915
3916 if (chip->onfi_version)
3917 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3918 chip->onfi_params.model);
3919 else if (chip->jedec_version)
3920 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3921 chip->jedec_params.model);
3922 else
3923 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3924 type->name);
3925
Rafał Miłecki3755a992014-10-21 00:01:04 +02003926 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Huang Shijie3723e932013-09-25 14:58:14 +08003927 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Rafał Miłecki3755a992014-10-21 00:01:04 +02003928 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003929 return type;
3930}
3931
Boris BREZILLON7194a292015-12-10 09:00:37 +01003932static int nand_dt_init(struct nand_chip *chip)
Brian Norris5844fee2015-01-23 00:22:27 -08003933{
Boris BREZILLON7194a292015-12-10 09:00:37 +01003934 struct device_node *dn = nand_get_flash_node(chip);
Rafał Miłecki79082452016-03-23 11:19:02 +01003935 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
Brian Norris5844fee2015-01-23 00:22:27 -08003936
Boris BREZILLON7194a292015-12-10 09:00:37 +01003937 if (!dn)
3938 return 0;
3939
Brian Norris5844fee2015-01-23 00:22:27 -08003940 if (of_get_nand_bus_width(dn) == 16)
3941 chip->options |= NAND_BUSWIDTH_16;
3942
3943 if (of_get_nand_on_flash_bbt(dn))
3944 chip->bbt_options |= NAND_BBT_USE_FLASH;
3945
3946 ecc_mode = of_get_nand_ecc_mode(dn);
Rafał Miłecki79082452016-03-23 11:19:02 +01003947 ecc_algo = of_get_nand_ecc_algo(dn);
Brian Norris5844fee2015-01-23 00:22:27 -08003948 ecc_strength = of_get_nand_ecc_strength(dn);
3949 ecc_step = of_get_nand_ecc_step_size(dn);
3950
3951 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3952 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3953 pr_err("must set both strength and step size in DT\n");
3954 return -EINVAL;
3955 }
3956
3957 if (ecc_mode >= 0)
3958 chip->ecc.mode = ecc_mode;
3959
Rafał Miłecki79082452016-03-23 11:19:02 +01003960 if (ecc_algo >= 0)
3961 chip->ecc.algo = ecc_algo;
3962
Brian Norris5844fee2015-01-23 00:22:27 -08003963 if (ecc_strength >= 0)
3964 chip->ecc.strength = ecc_strength;
3965
3966 if (ecc_step > 0)
3967 chip->ecc.size = ecc_step;
3968
3969 return 0;
3970}
3971
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003972/**
David Woodhouse3b85c322006-09-25 17:06:53 +01003973 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07003974 * @mtd: MTD device structure
3975 * @maxchips: number of chips to scan for
3976 * @table: alternative NAND ID table
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003977 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07003978 * This is the first phase of the normal nand_scan() function. It reads the
3979 * flash ID and sets up MTD fields accordingly.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003980 *
David Woodhouse3b85c322006-09-25 17:06:53 +01003981 * The mtd->owner field must be set to the module of the caller.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003982 */
David Woodhouse5e81e882010-02-26 18:32:56 +00003983int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3984 struct nand_flash_dev *table)
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003985{
Cai Zhiyongbb770822013-12-25 20:11:15 +08003986 int i, nand_maf_id, nand_dev_id;
Boris BREZILLON862eba52015-12-01 12:03:03 +01003987 struct nand_chip *chip = mtd_to_nand(mtd);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003988 struct nand_flash_dev *type;
Brian Norris5844fee2015-01-23 00:22:27 -08003989 int ret;
3990
Boris BREZILLON7194a292015-12-10 09:00:37 +01003991 ret = nand_dt_init(chip);
3992 if (ret)
3993 return ret;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003994
Brian Norrisf7a8e382016-01-05 10:39:45 -08003995 if (!mtd->name && mtd->dev.parent)
3996 mtd->name = dev_name(mtd->dev.parent);
3997
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02003998 /* Set the default functions */
Cai Zhiyongbb770822013-12-25 20:11:15 +08003999 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004000
4001 /* Read the flash type */
Cai Zhiyongbb770822013-12-25 20:11:15 +08004002 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4003 &nand_dev_id, table);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004004
4005 if (IS_ERR(type)) {
Ben Dooksb1c6e6d2009-11-02 18:12:33 +00004006 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
Brian Norrisd0370212011-07-19 10:06:08 -07004007 pr_warn("No NAND device found\n");
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004008 chip->select_chip(mtd, -1);
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004009 return PTR_ERR(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
4011
Huang Shijie07300162012-11-09 16:23:45 +08004012 chip->select_chip(mtd, -1);
4013
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004014 /* Check for a chip array */
David Woodhousee0c7d762006-05-13 18:07:53 +01004015 for (i = 1; i < maxchips; i++) {
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004016 chip->select_chip(mtd, i);
Karl Beldanef89a882008-09-15 14:37:29 +02004017 /* See comment in nand_get_flash_type for reset */
4018 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 /* Send the command for reading device ID */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004020 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 /* Read manufacturer and device IDs */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004022 if (nand_maf_id != chip->read_byte(mtd) ||
Huang Shijie07300162012-11-09 16:23:45 +08004023 nand_dev_id != chip->read_byte(mtd)) {
4024 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 break;
Huang Shijie07300162012-11-09 16:23:45 +08004026 }
4027 chip->select_chip(mtd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
4029 if (i > 1)
Ezequiel Garcia20171642013-11-25 08:30:31 -03004030 pr_info("%d chips detected\n", i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 /* Store the number of chips and calc total size for mtd */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004033 chip->numchips = i;
4034 mtd->size = i * chip->chipsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
David Woodhouse3b85c322006-09-25 17:06:53 +01004036 return 0;
4037}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004038EXPORT_SYMBOL(nand_scan_ident);
David Woodhouse3b85c322006-09-25 17:06:53 +01004039
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004040/*
4041 * Check if the chip configuration meet the datasheet requirements.
4042
4043 * If our configuration corrects A bits per B bytes and the minimum
4044 * required correction level is X bits per Y bytes, then we must ensure
4045 * both of the following are true:
4046 *
4047 * (1) A / B >= X / Y
4048 * (2) A >= X
4049 *
4050 * Requirement (1) ensures we can correct for the required bitflip density.
4051 * Requirement (2) ensures we can correct even when all bitflips are clumped
4052 * in the same sector.
4053 */
4054static bool nand_ecc_strength_good(struct mtd_info *mtd)
4055{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004056 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004057 struct nand_ecc_ctrl *ecc = &chip->ecc;
4058 int corr, ds_corr;
4059
4060 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4061 /* Not enough information */
4062 return true;
4063
4064 /*
4065 * We get the number of corrected bits per page to compare
4066 * the correction density.
4067 */
4068 corr = (mtd->writesize * ecc->strength) / ecc->size;
4069 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4070
4071 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4072}
David Woodhouse3b85c322006-09-25 17:06:53 +01004073
4074/**
4075 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004076 * @mtd: MTD device structure
David Woodhouse3b85c322006-09-25 17:06:53 +01004077 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004078 * This is the second phase of the normal nand_scan() function. It fills out
4079 * all the uninitialized function pointers with the defaults and scans for a
4080 * bad block table if appropriate.
David Woodhouse3b85c322006-09-25 17:06:53 +01004081 */
4082int nand_scan_tail(struct mtd_info *mtd)
4083{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004084 struct nand_chip *chip = mtd_to_nand(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004085 struct nand_ecc_ctrl *ecc = &chip->ecc;
Huang Shijief02ea4e2014-01-13 14:27:12 +08004086 struct nand_buffers *nbuf;
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004087 int ret;
David Woodhouse3b85c322006-09-25 17:06:53 +01004088
Brian Norrise2414f42012-02-06 13:44:00 -08004089 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004090 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4091 !(chip->bbt_options & NAND_BBT_USE_FLASH)))
4092 return -EINVAL;
Brian Norrise2414f42012-02-06 13:44:00 -08004093
Huang Shijief02ea4e2014-01-13 14:27:12 +08004094 if (!(chip->options & NAND_OWN_BUFFERS)) {
4095 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
4096 + mtd->oobsize * 3, GFP_KERNEL);
4097 if (!nbuf)
4098 return -ENOMEM;
4099 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
4100 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
4101 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
4102
4103 chip->buffers = nbuf;
4104 } else {
4105 if (!chip->buffers)
4106 return -ENOMEM;
4107 }
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004108
David Woodhouse7dcdcbef2006-10-21 17:09:53 +01004109 /* Set the internal oob buffer location, just after the page data */
David Woodhouse784f4d52006-10-22 01:47:45 +01004110 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004111
4112 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004113 * If no default placement scheme is given, select an appropriate one.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004114 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004115 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004116 switch (mtd->oobsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 case 8:
Huang Shijie97de79e02013-10-18 14:20:53 +08004118 ecc->layout = &nand_oob_8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 break;
4120 case 16:
Huang Shijie97de79e02013-10-18 14:20:53 +08004121 ecc->layout = &nand_oob_16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 break;
4123 case 64:
Huang Shijie97de79e02013-10-18 14:20:53 +08004124 ecc->layout = &nand_oob_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 break;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004126 case 128:
Huang Shijie97de79e02013-10-18 14:20:53 +08004127 ecc->layout = &nand_oob_128;
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 default:
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004130 WARN(1, "No oob scheme defined for oobsize %d\n",
4131 mtd->oobsize);
4132 ret = -EINVAL;
4133 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004136
David Woodhouse956e9442006-09-25 17:12:39 +01004137 if (!chip->write_page)
4138 chip->write_page = nand_write_page;
4139
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004140 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -07004141 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004142 * selected and we have 256 byte pagesize fallback to software ECC
David Woodhousee0c7d762006-05-13 18:07:53 +01004143 */
David Woodhouse956e9442006-09-25 17:12:39 +01004144
Huang Shijie97de79e02013-10-18 14:20:53 +08004145 switch (ecc->mode) {
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004146 case NAND_ECC_HW_OOB_FIRST:
4147 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Huang Shijie97de79e02013-10-18 14:20:53 +08004148 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004149 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4150 ret = -EINVAL;
4151 goto err_free;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004152 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004153 if (!ecc->read_page)
4154 ecc->read_page = nand_read_page_hwecc_oob_first;
Sneha Narnakaje6e0cb132009-09-18 12:51:47 -07004155
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004156 case NAND_ECC_HW:
Brian Norris8b6e50c2011-05-25 14:59:01 -07004157 /* Use standard hwecc read page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004158 if (!ecc->read_page)
4159 ecc->read_page = nand_read_page_hwecc;
4160 if (!ecc->write_page)
4161 ecc->write_page = nand_write_page_hwecc;
4162 if (!ecc->read_page_raw)
4163 ecc->read_page_raw = nand_read_page_raw;
4164 if (!ecc->write_page_raw)
4165 ecc->write_page_raw = nand_write_page_raw;
4166 if (!ecc->read_oob)
4167 ecc->read_oob = nand_read_oob_std;
4168 if (!ecc->write_oob)
4169 ecc->write_oob = nand_write_oob_std;
4170 if (!ecc->read_subpage)
4171 ecc->read_subpage = nand_read_subpage;
Helmut Schaa44991b32014-04-09 11:13:24 +02004172 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Huang Shijie97de79e02013-10-18 14:20:53 +08004173 ecc->write_subpage = nand_write_subpage_hwecc;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004174
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004175 case NAND_ECC_HW_SYNDROME:
Huang Shijie97de79e02013-10-18 14:20:53 +08004176 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4177 (!ecc->read_page ||
4178 ecc->read_page == nand_read_page_hwecc ||
4179 !ecc->write_page ||
4180 ecc->write_page == nand_write_page_hwecc)) {
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004181 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4182 ret = -EINVAL;
4183 goto err_free;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004184 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07004185 /* Use standard syndrome read/write page function? */
Huang Shijie97de79e02013-10-18 14:20:53 +08004186 if (!ecc->read_page)
4187 ecc->read_page = nand_read_page_syndrome;
4188 if (!ecc->write_page)
4189 ecc->write_page = nand_write_page_syndrome;
4190 if (!ecc->read_page_raw)
4191 ecc->read_page_raw = nand_read_page_raw_syndrome;
4192 if (!ecc->write_page_raw)
4193 ecc->write_page_raw = nand_write_page_raw_syndrome;
4194 if (!ecc->read_oob)
4195 ecc->read_oob = nand_read_oob_syndrome;
4196 if (!ecc->write_oob)
4197 ecc->write_oob = nand_write_oob_syndrome;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +02004198
Huang Shijie97de79e02013-10-18 14:20:53 +08004199 if (mtd->writesize >= ecc->size) {
4200 if (!ecc->strength) {
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004201 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4202 ret = -EINVAL;
4203 goto err_free;
Mike Dunne2788c92012-04-25 12:06:10 -07004204 }
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004205 break;
Mike Dunne2788c92012-04-25 12:06:10 -07004206 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004207 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4208 ecc->size, mtd->writesize);
Huang Shijie97de79e02013-10-18 14:20:53 +08004209 ecc->mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02004211 case NAND_ECC_SOFT:
Huang Shijie97de79e02013-10-18 14:20:53 +08004212 ecc->calculate = nand_calculate_ecc;
4213 ecc->correct = nand_correct_data;
4214 ecc->read_page = nand_read_page_swecc;
4215 ecc->read_subpage = nand_read_subpage;
4216 ecc->write_page = nand_write_page_swecc;
4217 ecc->read_page_raw = nand_read_page_raw;
4218 ecc->write_page_raw = nand_write_page_raw;
4219 ecc->read_oob = nand_read_oob_std;
4220 ecc->write_oob = nand_write_oob_std;
4221 if (!ecc->size)
4222 ecc->size = 256;
4223 ecc->bytes = 3;
4224 ecc->strength = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004226
Ivan Djelic193bd402011-03-11 11:05:33 +01004227 case NAND_ECC_SOFT_BCH:
4228 if (!mtd_nand_has_bch()) {
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004229 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4230 ret = -EINVAL;
4231 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004232 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004233 ecc->calculate = nand_bch_calculate_ecc;
4234 ecc->correct = nand_bch_correct_data;
4235 ecc->read_page = nand_read_page_swecc;
4236 ecc->read_subpage = nand_read_subpage;
4237 ecc->write_page = nand_write_page_swecc;
4238 ecc->read_page_raw = nand_read_page_raw;
4239 ecc->write_page_raw = nand_write_page_raw;
4240 ecc->read_oob = nand_read_oob_std;
4241 ecc->write_oob = nand_write_oob_std;
Ivan Djelic193bd402011-03-11 11:05:33 +01004242 /*
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004243 * Board driver should supply ecc.size and ecc.strength values
4244 * to select how many bits are correctable. Otherwise, default
4245 * to 4 bits for large page devices.
Ivan Djelic193bd402011-03-11 11:05:33 +01004246 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004247 if (!ecc->size && (mtd->oobsize >= 64)) {
4248 ecc->size = 512;
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004249 ecc->strength = 4;
Ivan Djelic193bd402011-03-11 11:05:33 +01004250 }
Aaron Sierrae0377cd2015-01-14 17:41:31 -06004251
4252 /* See nand_bch_init() for details. */
Boris BREZILLONa8c65d52016-03-07 10:46:54 +01004253 ecc->bytes = 0;
4254 ecc->priv = nand_bch_init(mtd);
Huang Shijie97de79e02013-10-18 14:20:53 +08004255 if (!ecc->priv) {
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004256 WARN(1, "BCH ECC initialization failed!\n");
4257 ret = -EINVAL;
4258 goto err_free;
Ivan Djelic193bd402011-03-11 11:05:33 +01004259 }
4260 break;
4261
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004262 case NAND_ECC_NONE:
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02004263 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Huang Shijie97de79e02013-10-18 14:20:53 +08004264 ecc->read_page = nand_read_page_raw;
4265 ecc->write_page = nand_write_page_raw;
4266 ecc->read_oob = nand_read_oob_std;
4267 ecc->read_page_raw = nand_read_page_raw;
4268 ecc->write_page_raw = nand_write_page_raw;
4269 ecc->write_oob = nand_write_oob_std;
4270 ecc->size = mtd->writesize;
4271 ecc->bytes = 0;
4272 ecc->strength = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 break;
David Woodhouse956e9442006-09-25 17:12:39 +01004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 default:
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004276 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
4277 ret = -EINVAL;
4278 goto err_free;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Brian Norris9ce244b2011-08-30 18:45:37 -07004281 /* For many systems, the standard OOB write also works for raw */
Huang Shijie97de79e02013-10-18 14:20:53 +08004282 if (!ecc->read_oob_raw)
4283 ecc->read_oob_raw = ecc->read_oob;
4284 if (!ecc->write_oob_raw)
4285 ecc->write_oob_raw = ecc->write_oob;
Brian Norris9ce244b2011-08-30 18:45:37 -07004286
Boris Brezillon846031d2016-02-03 20:11:00 +01004287 /* propagate ecc info to mtd_info */
4288 mtd->ecclayout = ecc->layout;
4289 mtd->ecc_strength = ecc->strength;
4290 mtd->ecc_step_size = ecc->size;
Ezequiel Garcia67a9ad92014-05-14 14:58:06 -03004291
Thomas Gleixner5bd34c02006-05-27 22:16:10 +02004292 /*
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004293 * Set the number of read / write steps for one page depending on ECC
Brian Norris8b6e50c2011-05-25 14:59:01 -07004294 * mode.
Thomas Gleixner7aa65bf2006-05-23 11:54:38 +02004295 */
Huang Shijie97de79e02013-10-18 14:20:53 +08004296 ecc->steps = mtd->writesize / ecc->size;
4297 if (ecc->steps * ecc->size != mtd->writesize) {
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004298 WARN(1, "Invalid ECC parameters\n");
4299 ret = -EINVAL;
4300 goto err_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 }
Huang Shijie97de79e02013-10-18 14:20:53 +08004302 ecc->total = ecc->steps * ecc->bytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004303
Boris Brezillon846031d2016-02-03 20:11:00 +01004304 /*
4305 * The number of bytes available for a client to place data into
4306 * the out of band area.
4307 */
4308 ret = mtd_ooblayout_count_freebytes(mtd);
4309 if (ret < 0)
4310 ret = 0;
4311
4312 mtd->oobavail = ret;
4313
4314 /* ECC sanity check: warn if it's too weak */
4315 if (!nand_ecc_strength_good(mtd))
4316 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4317 mtd->name);
4318
Brian Norris8b6e50c2011-05-25 14:59:01 -07004319 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Huang Shijie1d0ed692013-09-25 14:58:10 +08004320 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
Huang Shijie97de79e02013-10-18 14:20:53 +08004321 switch (ecc->steps) {
Thomas Gleixner29072b92006-09-28 15:38:36 +02004322 case 2:
4323 mtd->subpage_sft = 1;
4324 break;
4325 case 4:
4326 case 8:
Thomas Gleixner81ec5362007-12-12 17:27:03 +01004327 case 16:
Thomas Gleixner29072b92006-09-28 15:38:36 +02004328 mtd->subpage_sft = 2;
4329 break;
4330 }
4331 }
4332 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4333
Thomas Gleixner04bbd0e2006-05-25 09:45:29 +02004334 /* Initialize state */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004335 chip->state = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 /* Invalidate the pagebuffer reference */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004338 chip->pagebuf = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004340 /* Large page NAND with SOFT_ECC should support subpage reads */
Ron Lee4007e2d2014-04-25 15:01:35 +09304341 switch (ecc->mode) {
4342 case NAND_ECC_SOFT:
4343 case NAND_ECC_SOFT_BCH:
4344 if (chip->page_shift > 9)
4345 chip->options |= NAND_SUBPAGE_READ;
4346 break;
4347
4348 default:
4349 break;
4350 }
Jeff Westfahla5ff4f12012-08-13 16:35:30 -05004351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 /* Fill in remaining MTD driver data */
Huang Shijie963d1c22013-09-25 14:58:21 +08004353 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Maxim Levitsky93edbad2010-02-22 20:39:40 +02004354 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4355 MTD_CAP_NANDFLASH;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004356 mtd->_erase = nand_erase;
4357 mtd->_point = NULL;
4358 mtd->_unpoint = NULL;
4359 mtd->_read = nand_read;
4360 mtd->_write = nand_write;
4361 mtd->_panic_write = panic_nand_write;
4362 mtd->_read_oob = nand_read_oob;
4363 mtd->_write_oob = nand_write_oob;
4364 mtd->_sync = nand_sync;
4365 mtd->_lock = NULL;
4366 mtd->_unlock = NULL;
4367 mtd->_suspend = nand_suspend;
4368 mtd->_resume = nand_resume;
Scott Branden72ea4032014-11-20 11:18:05 -08004369 mtd->_reboot = nand_shutdown;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03004370 mtd->_block_isreserved = nand_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +02004371 mtd->_block_isbad = nand_block_isbad;
4372 mtd->_block_markbad = nand_block_markbad;
Anatolij Gustschincbcab652010-12-16 23:42:16 +01004373 mtd->writebufsize = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Shmulik Ladkaniea3b2ea2012-06-08 18:29:06 +03004375 /*
4376 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4377 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4378 * properly set.
4379 */
4380 if (!mtd->bitflip_threshold)
Brian Norris240181f2015-01-12 12:51:29 -08004381 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004383 /* Check, if we should skip the bad block table scan */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004384 if (chip->options & NAND_SKIP_BBTSCAN)
Thomas Gleixner0040bf32005-02-09 12:20:00 +00004385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
4387 /* Build bad block table */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004388 return chip->scan_bbt(mtd);
Ezequiel GarcĂ­a11eaf6d2016-04-01 18:29:24 -03004389err_free:
4390 if (!(chip->options & NAND_OWN_BUFFERS))
4391 kfree(chip->buffers);
4392 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004394EXPORT_SYMBOL(nand_scan_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Brian Norris8b6e50c2011-05-25 14:59:01 -07004396/*
4397 * is_module_text_address() isn't exported, and it's mostly a pointless
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004398 * test if this is a module _anyway_ -- they'd have to try _really_ hard
Brian Norris8b6e50c2011-05-25 14:59:01 -07004399 * to call us from in-kernel code if the core NAND support is modular.
4400 */
David Woodhouse3b85c322006-09-25 17:06:53 +01004401#ifdef MODULE
4402#define caller_is_module() (1)
4403#else
4404#define caller_is_module() \
Rusty Russella6e6abd2009-03-31 13:05:31 -06004405 is_module_text_address((unsigned long)__builtin_return_address(0))
David Woodhouse3b85c322006-09-25 17:06:53 +01004406#endif
4407
4408/**
4409 * nand_scan - [NAND Interface] Scan for the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004410 * @mtd: MTD device structure
4411 * @maxchips: number of chips to scan for
David Woodhouse3b85c322006-09-25 17:06:53 +01004412 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07004413 * This fills out all the uninitialized function pointers with the defaults.
4414 * The flash ID is read and the mtd/chip structures are filled with the
4415 * appropriate values. The mtd->owner field must be set to the module of the
4416 * caller.
David Woodhouse3b85c322006-09-25 17:06:53 +01004417 */
4418int nand_scan(struct mtd_info *mtd, int maxchips)
4419{
4420 int ret;
4421
4422 /* Many callers got this wrong, so check for it for a while... */
4423 if (!mtd->owner && caller_is_module()) {
Brian Norrisd0370212011-07-19 10:06:08 -07004424 pr_crit("%s called with NULL mtd->owner!\n", __func__);
David Woodhouse3b85c322006-09-25 17:06:53 +01004425 BUG();
4426 }
4427
David Woodhouse5e81e882010-02-26 18:32:56 +00004428 ret = nand_scan_ident(mtd, maxchips, NULL);
David Woodhouse3b85c322006-09-25 17:06:53 +01004429 if (!ret)
4430 ret = nand_scan_tail(mtd);
4431 return ret;
4432}
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004433EXPORT_SYMBOL(nand_scan);
David Woodhouse3b85c322006-09-25 17:06:53 +01004434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004436 * nand_release - [NAND Interface] Free resources held by the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07004437 * @mtd: MTD device structure
4438 */
David Woodhousee0c7d762006-05-13 18:07:53 +01004439void nand_release(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
Boris BREZILLON862eba52015-12-01 12:03:03 +01004441 struct nand_chip *chip = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Ivan Djelic193bd402011-03-11 11:05:33 +01004443 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4444 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4445
Jamie Iles5ffcaf32011-05-23 10:22:46 +01004446 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Jesper Juhlfa671642005-11-07 01:01:27 -08004448 /* Free bad block table memory */
Thomas Gleixnerace4dfe2006-05-24 12:07:37 +02004449 kfree(chip->bbt);
David Woodhouse4bf63fc2006-09-25 17:08:04 +01004450 if (!(chip->options & NAND_OWN_BUFFERS))
4451 kfree(chip->buffers);
Brian Norris58373ff2010-07-15 12:15:44 -07004452
4453 /* Free bad block descriptor memory */
4454 if (chip->badblock_pattern && chip->badblock_pattern->options
4455 & NAND_BBT_DYNAMICSTRUCT)
4456 kfree(chip->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
David Woodhousee0c7d762006-05-13 18:07:53 +01004458EXPORT_SYMBOL_GPL(nand_release);
Richard Purdie8fe833c2006-03-31 02:31:14 -08004459
David Woodhousee0c7d762006-05-13 18:07:53 +01004460MODULE_LICENSE("GPL");
Florian Fainelli7351d3a2010-09-07 13:23:45 +02004461MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4462MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
David Woodhousee0c7d762006-05-13 18:07:53 +01004463MODULE_DESCRIPTION("Generic NAND flash driver code");