blob: b025001337e1fd0c8806461753e699a9d06a4aad [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002 * 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.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -05007 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020011 *
William Juul52c07962007-10-31 13:53:06 +010012 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020013 * David Woodhouse for adding multichip support
14 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
William Juul52c07962007-10-31 13:53:06 +010018 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapin3a38a552013-01-14 03:46:50 +000021 * if we have HW ECC support.
Scott Wood3628f002008-10-24 16:20:43 -050022 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020023 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020024 * 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
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31#include <common.h>
Brian Norrisba6463d2016-06-15 21:09:22 +020032#if CONFIG_IS_ENABLED(OF_CONTROL)
33#include <fdtdec.h>
34#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020035#include <malloc.h>
36#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010037#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000038#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020039#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020042#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020043#ifdef CONFIG_MTD_PARTITIONS
44#include <linux/mtd/partitions.h>
45#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020046#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090047#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020048
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020049/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010050static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051 .eccbytes = 3,
52 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010053 .oobfree = {
54 {.offset = 3,
55 .length = 2},
56 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020057 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020058};
59
William Juul52c07962007-10-31 13:53:06 +010060static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020061 .eccbytes = 6,
62 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010063 .oobfree = {
64 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020065 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020066};
67
William Juul52c07962007-10-31 13:53:06 +010068static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020069 .eccbytes = 24,
70 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010071 40, 41, 42, 43, 44, 45, 46, 47,
72 48, 49, 50, 51, 52, 53, 54, 55,
73 56, 57, 58, 59, 60, 61, 62, 63},
74 .oobfree = {
75 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020076 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020077};
78
William Juul52c07962007-10-31 13:53:06 +010079static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020080 .eccbytes = 48,
81 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020082 80, 81, 82, 83, 84, 85, 86, 87,
83 88, 89, 90, 91, 92, 93, 94, 95,
84 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010085 104, 105, 106, 107, 108, 109, 110, 111,
86 112, 113, 114, 115, 116, 117, 118, 119,
87 120, 121, 122, 123, 124, 125, 126, 127},
88 .oobfree = {
89 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020090 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020091};
92
Heiko Schocherf5895d12014-06-24 10:10:04 +020093static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010094
95static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
96 struct mtd_oob_ops *ops);
97
Heiko Schocherf5895d12014-06-24 10:10:04 +020098/*
99 * For devices which display every fart in the system on a separate LED. Is
100 * compiled away when LED support is disabled.
101 */
102DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200103
Christian Hitzb8a6b372011-10-12 09:32:02 +0200104static int check_offs_len(struct mtd_info *mtd,
105 loff_t ofs, uint64_t len)
106{
Scott Wood17fed142016-05-30 13:57:56 -0500107 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200108 int ret = 0;
109
110 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200111 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
112 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200113 ret = -EINVAL;
114 }
115
116 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200117 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
118 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200119 ret = -EINVAL;
120 }
121
Christian Hitzb8a6b372011-10-12 09:32:02 +0200122 return ret;
123}
124
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200125/**
126 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000127 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200128 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200129 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200130 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200131static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100132{
Scott Wood17fed142016-05-30 13:57:56 -0500133 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200134
135 /* De-select the NAND device */
136 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100137}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200138
139/**
140 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000141 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200143 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200144 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000145uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146{
Scott Wood17fed142016-05-30 13:57:56 -0500147 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100148 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149}
150
151/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200152 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000153 * @mtd: MTD device structure
154 *
155 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200156 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200157 */
William Juul52c07962007-10-31 13:53:06 +0100158static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200159{
Scott Wood17fed142016-05-30 13:57:56 -0500160 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100161 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200162}
163
164/**
165 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000166 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200167 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000168 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200169 */
170static u16 nand_read_word(struct mtd_info *mtd)
171{
Scott Wood17fed142016-05-30 13:57:56 -0500172 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100173 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200174}
175
176/**
177 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000178 * @mtd: MTD device structure
179 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200180 *
181 * Default select function for 1 chip devices.
182 */
William Juul52c07962007-10-31 13:53:06 +0100183static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184{
Scott Wood17fed142016-05-30 13:57:56 -0500185 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100186
187 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200188 case -1:
William Juul52c07962007-10-31 13:53:06 +0100189 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200190 break;
191 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192 break;
193
194 default:
195 BUG();
196 }
197}
198
199/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200200 * nand_write_byte - [DEFAULT] write single byte to chip
201 * @mtd: MTD device structure
202 * @byte: value to write
203 *
204 * Default function to write a byte to I/O[7:0]
205 */
206static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
207{
Scott Wood17fed142016-05-30 13:57:56 -0500208 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200209
210 chip->write_buf(mtd, &byte, 1);
211}
212
213/**
214 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
215 * @mtd: MTD device structure
216 * @byte: value to write
217 *
218 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
219 */
220static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
221{
Scott Wood17fed142016-05-30 13:57:56 -0500222 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200223 uint16_t word = byte;
224
225 /*
226 * It's not entirely clear what should happen to I/O[15:8] when writing
227 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
228 *
229 * When the host supports a 16-bit bus width, only data is
230 * transferred at the 16-bit width. All address and command line
231 * transfers shall use only the lower 8-bits of the data bus. During
232 * command transfers, the host may place any value on the upper
233 * 8-bits of the data bus. During address transfers, the host shall
234 * set the upper 8-bits of the data bus to 00h.
235 *
236 * One user of the write_byte callback is nand_onfi_set_features. The
237 * four parameters are specified to be written to I/O[7:0], but this is
238 * neither an address nor a command transfer. Let's assume a 0 on the
239 * upper I/O lines is OK.
240 */
241 chip->write_buf(mtd, (uint8_t *)&word, 2);
242}
243
Heiko Schocherf5895d12014-06-24 10:10:04 +0200244static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
245{
246 int i;
247
248 for (i = 0; i < len; i++)
249 writeb(buf[i], addr);
250}
251static void ioread8_rep(void *addr, uint8_t *buf, int len)
252{
253 int i;
254
255 for (i = 0; i < len; i++)
256 buf[i] = readb(addr);
257}
258
259static void ioread16_rep(void *addr, void *buf, int len)
260{
261 int i;
262 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200263
Heiko Schocherf5895d12014-06-24 10:10:04 +0200264 for (i = 0; i < len; i++)
265 p[i] = readw(addr);
266}
267
268static void iowrite16_rep(void *addr, void *buf, int len)
269{
270 int i;
271 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200272
273 for (i = 0; i < len; i++)
274 writew(p[i], addr);
275}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276
277/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200278 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000279 * @mtd: MTD device structure
280 * @buf: data buffer
281 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000283 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200284 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000285void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286{
Scott Wood17fed142016-05-30 13:57:56 -0500287 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288
Heiko Schocherf5895d12014-06-24 10:10:04 +0200289 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200290}
291
292/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200293 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000294 * @mtd: MTD device structure
295 * @buf: buffer to store date
296 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200297 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000298 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200299 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400300void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301{
Scott Wood17fed142016-05-30 13:57:56 -0500302 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303
Heiko Schocherf5895d12014-06-24 10:10:04 +0200304 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305}
306
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200308 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000309 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200310 * @buf: data buffer
311 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200313 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200314 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200315void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316{
Scott Wood17fed142016-05-30 13:57:56 -0500317 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200319
Heiko Schocherf5895d12014-06-24 10:10:04 +0200320 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200321}
322
323/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000325 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200326 * @buf: buffer to store date
327 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200328 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200329 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200330 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200331void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332{
Scott Wood17fed142016-05-30 13:57:56 -0500333 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200335
Heiko Schocherf5895d12014-06-24 10:10:04 +0200336 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200337}
338
339/**
340 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000341 * @mtd: MTD device structure
342 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200343 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200344 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200345 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500346static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500348 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500349 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350 u16 bad;
351
Sergey Lapin3a38a552013-01-14 03:46:50 +0000352 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200353 ofs += mtd->erasesize - mtd->writesize;
354
William Juul52c07962007-10-31 13:53:06 +0100355 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200356
Sergey Lapin3a38a552013-01-14 03:46:50 +0000357 do {
358 if (chip->options & NAND_BUSWIDTH_16) {
359 chip->cmdfunc(mtd, NAND_CMD_READOOB,
360 chip->badblockpos & 0xFE, page);
361 bad = cpu_to_le16(chip->read_word(mtd));
362 if (chip->badblockpos & 0x1)
363 bad >>= 8;
364 else
365 bad &= 0xFF;
366 } else {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
368 page);
369 bad = chip->read_byte(mtd);
370 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200371
Sergey Lapin3a38a552013-01-14 03:46:50 +0000372 if (likely(chip->badblockbits == 8))
373 res = bad != 0xFF;
374 else
375 res = hweight8(bad) < chip->badblockbits;
376 ofs += mtd->writesize;
377 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
378 i++;
379 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200380
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200381 return res;
382}
383
384/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200385 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000386 * @mtd: MTD device structure
387 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200388 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000389 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200390 * specific driver. It provides the details for writing a bad block marker to a
391 * block.
392 */
393static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
394{
Scott Wood17fed142016-05-30 13:57:56 -0500395 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200396 struct mtd_oob_ops ops;
397 uint8_t buf[2] = { 0, 0 };
398 int ret = 0, res, i = 0;
399
Scott Wood3ea94ed2015-06-26 19:03:26 -0500400 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200401 ops.oobbuf = buf;
402 ops.ooboffs = chip->badblockpos;
403 if (chip->options & NAND_BUSWIDTH_16) {
404 ops.ooboffs &= ~0x01;
405 ops.len = ops.ooblen = 2;
406 } else {
407 ops.len = ops.ooblen = 1;
408 }
409 ops.mode = MTD_OPS_PLACE_OOB;
410
411 /* Write to first/last page(s) if necessary */
412 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
413 ofs += mtd->erasesize - mtd->writesize;
414 do {
415 res = nand_do_write_oob(mtd, ofs, &ops);
416 if (!ret)
417 ret = res;
418
419 i++;
420 ofs += mtd->writesize;
421 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
422
423 return ret;
424}
425
426/**
427 * nand_block_markbad_lowlevel - mark a block bad
428 * @mtd: MTD device structure
429 * @ofs: offset from device start
430 *
431 * This function performs the generic NAND bad block marking steps (i.e., bad
432 * block table(s) and/or marker(s)). We only allow the hardware driver to
433 * specify how to write bad block markers to OOB (chip->block_markbad).
434 *
435 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000436 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200437 * (2) write bad block marker to OOB area of affected block (unless flag
438 * NAND_BBT_NO_OOB_BBM is present)
439 * (3) update the BBT
440 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000441 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200442*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200443static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200444{
Scott Wood17fed142016-05-30 13:57:56 -0500445 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200446 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200447
Heiko Schocherf5895d12014-06-24 10:10:04 +0200448 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000449 struct erase_info einfo;
450
451 /* Attempt erase before marking OOB */
452 memset(&einfo, 0, sizeof(einfo));
453 einfo.mtd = mtd;
454 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200455 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000456 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200457
Heiko Schocherf5895d12014-06-24 10:10:04 +0200458 /* Write bad block marker to OOB */
459 nand_get_device(mtd, FL_WRITING);
460 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500461 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100462 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000463
Heiko Schocherf5895d12014-06-24 10:10:04 +0200464 /* Mark block bad in BBT */
465 if (chip->bbt) {
466 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000467 if (!ret)
468 ret = res;
469 }
470
William Juul52c07962007-10-31 13:53:06 +0100471 if (!ret)
472 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500473
William Juul52c07962007-10-31 13:53:06 +0100474 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200475}
476
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200477/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200478 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000479 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200480 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000481 * Check, if the device is write protected. The function expects, that the
482 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200483 */
William Juul52c07962007-10-31 13:53:06 +0100484static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200485{
Scott Wood17fed142016-05-30 13:57:56 -0500486 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200487
Sergey Lapin3a38a552013-01-14 03:46:50 +0000488 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200489 if (chip->options & NAND_BROKEN_XD)
490 return 0;
491
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200492 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100493 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
494 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200495}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100496
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200497/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500498 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000499 * @mtd: MTD device structure
500 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300501 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500502 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300503 */
504static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
505{
Scott Wood17fed142016-05-30 13:57:56 -0500506 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300507
508 if (!chip->bbt)
509 return 0;
510 /* Return info from the table */
511 return nand_isreserved_bbt(mtd, ofs);
512}
513
514/**
515 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
516 * @mtd: MTD device structure
517 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000518 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200519 *
520 * Check, if the block is bad. Either by reading the bad block table or
521 * calling of the scan function.
522 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500523static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524{
Scott Wood17fed142016-05-30 13:57:56 -0500525 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200526
Masahiro Yamada8d100542014-12-26 22:20:58 +0900527 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
528 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200529 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900530 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200531 }
532
William Juul52c07962007-10-31 13:53:06 +0100533 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500534 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200535
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200536 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100537 return nand_isbad_bbt(mtd, ofs, allowbbt);
538}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200539
Scott Wood52ab7ce2016-05-30 13:57:58 -0500540/**
541 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
542 * @mtd: MTD device structure
543 *
544 * Wait for the ready pin after a command, and warn if a timeout occurs.
545 */
William Juul52c07962007-10-31 13:53:06 +0100546void nand_wait_ready(struct mtd_info *mtd)
547{
Scott Wood17fed142016-05-30 13:57:56 -0500548 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500549 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000550 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100551
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000552 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000553 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000554 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100555 if (chip->dev_ready)
556 if (chip->dev_ready(mtd))
557 break;
558 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500559
560 if (!chip->dev_ready(mtd))
561 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200562}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200563EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200564
565/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500566 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
567 * @mtd: MTD device structure
568 * @timeo: Timeout in ms
569 *
570 * Wait for status ready (i.e. command done) or timeout.
571 */
572static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
573{
Scott Wood17fed142016-05-30 13:57:56 -0500574 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500575 u32 time_start;
576
577 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
578 time_start = get_timer(0);
579 while (get_timer(time_start) < timeo) {
580 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
581 break;
582 WATCHDOG_RESET();
583 }
584};
585
586/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200587 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000588 * @mtd: MTD device structure
589 * @command: the command to be sent
590 * @column: the column address for this command, -1 if none
591 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200592 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000593 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200594 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200595 */
William Juul52c07962007-10-31 13:53:06 +0100596static void nand_command(struct mtd_info *mtd, unsigned int command,
597 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200598{
Scott Wood17fed142016-05-30 13:57:56 -0500599 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100600 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200601
Sergey Lapin3a38a552013-01-14 03:46:50 +0000602 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 if (command == NAND_CMD_SEQIN) {
604 int readcmd;
605
William Juul52c07962007-10-31 13:53:06 +0100606 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200607 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100608 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200609 readcmd = NAND_CMD_READOOB;
610 } else if (column < 256) {
611 /* First 256 bytes --> READ0 */
612 readcmd = NAND_CMD_READ0;
613 } else {
614 column -= 256;
615 readcmd = NAND_CMD_READ1;
616 }
William Juul52c07962007-10-31 13:53:06 +0100617 chip->cmd_ctrl(mtd, readcmd, ctrl);
618 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619 }
William Juul52c07962007-10-31 13:53:06 +0100620 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621
Sergey Lapin3a38a552013-01-14 03:46:50 +0000622 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100623 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
624 /* Serially input address */
625 if (column != -1) {
626 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200627 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530628 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100629 column >>= 1;
630 chip->cmd_ctrl(mtd, column, ctrl);
631 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200632 }
William Juul52c07962007-10-31 13:53:06 +0100633 if (page_addr != -1) {
634 chip->cmd_ctrl(mtd, page_addr, ctrl);
635 ctrl &= ~NAND_CTRL_CHANGE;
636 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
637 /* One more address cycle for devices > 32MiB */
638 if (chip->chipsize > (32 << 20))
639 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
640 }
641 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200642
643 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000644 * Program and erase have their own busy handlers status and sequential
645 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100646 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200648
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200649 case NAND_CMD_PAGEPROG:
650 case NAND_CMD_ERASE1:
651 case NAND_CMD_ERASE2:
652 case NAND_CMD_SEQIN:
653 case NAND_CMD_STATUS:
654 return;
655
656 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100657 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200658 break;
William Juul52c07962007-10-31 13:53:06 +0100659 udelay(chip->chip_delay);
660 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
661 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
662 chip->cmd_ctrl(mtd,
663 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500664 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
665 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200666 return;
667
William Juul52c07962007-10-31 13:53:06 +0100668 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200669 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200670 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200671 * If we don't have access to the busy pin, we apply the given
672 * command delay
William Juul52c07962007-10-31 13:53:06 +0100673 */
674 if (!chip->dev_ready) {
675 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200676 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200677 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200678 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000679 /*
680 * Apply this short delay always to ensure that we do wait tWB in
681 * any case on any machine.
682 */
William Juul52c07962007-10-31 13:53:06 +0100683 ndelay(100);
684
685 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686}
687
688/**
689 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000690 * @mtd: MTD device structure
691 * @command: the command to be sent
692 * @column: the column address for this command, -1 if none
693 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200694 *
William Juul52c07962007-10-31 13:53:06 +0100695 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000696 * devices. We don't have the separate regions as we have in the small page
697 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200698 */
William Juul52c07962007-10-31 13:53:06 +0100699static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
700 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200701{
Scott Wood17fed142016-05-30 13:57:56 -0500702 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703
704 /* Emulate NAND_CMD_READOOB */
705 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100706 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200707 command = NAND_CMD_READ0;
708 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200709
William Juul52c07962007-10-31 13:53:06 +0100710 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200711 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200712
713 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100714 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715
716 /* Serially input address */
717 if (column != -1) {
718 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200719 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530720 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200721 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100722 chip->cmd_ctrl(mtd, column, ctrl);
723 ctrl &= ~NAND_CTRL_CHANGE;
724 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200725 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200726 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100727 chip->cmd_ctrl(mtd, page_addr, ctrl);
728 chip->cmd_ctrl(mtd, page_addr >> 8,
729 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200730 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100731 if (chip->chipsize > (128 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16,
733 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200734 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200735 }
William Juul52c07962007-10-31 13:53:06 +0100736 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200737
738 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000739 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500740 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100741 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200742 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200743
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200744 case NAND_CMD_CACHEDPROG:
745 case NAND_CMD_PAGEPROG:
746 case NAND_CMD_ERASE1:
747 case NAND_CMD_ERASE2:
748 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100749 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 case NAND_CMD_STATUS:
751 return;
752
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200753 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100754 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200755 break;
William Juul52c07962007-10-31 13:53:06 +0100756 udelay(chip->chip_delay);
757 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
758 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
759 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
760 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500761 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
762 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100763 return;
764
765 case NAND_CMD_RNDOUT:
766 /* No ready / busy check necessary */
767 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
768 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
769 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
770 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200771 return;
772
773 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100774 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
775 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
776 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
777 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200778
William Juul52c07962007-10-31 13:53:06 +0100779 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200780 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200781 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200782 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000783 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100784 */
785 if (!chip->dev_ready) {
786 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200787 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200788 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200789 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200790
Sergey Lapin3a38a552013-01-14 03:46:50 +0000791 /*
792 * Apply this short delay always to ensure that we do wait tWB in
793 * any case on any machine.
794 */
William Juul52c07962007-10-31 13:53:06 +0100795 ndelay(100);
796
797 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200798}
799
800/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200801 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000802 * @chip: the nand chip descriptor
803 * @mtd: MTD device structure
804 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200805 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200806 * Used when in panic, no locks are taken.
807 */
808static void panic_nand_get_device(struct nand_chip *chip,
809 struct mtd_info *mtd, int new_state)
810{
811 /* Hardware controller shared among independent devices */
812 chip->controller->active = chip;
813 chip->state = new_state;
814}
815
816/**
817 * nand_get_device - [GENERIC] Get chip for selected access
818 * @mtd: MTD device structure
819 * @new_state: the state which is requested
820 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200821 * Get the device and lock it for exclusive access
822 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200823static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200824nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100825{
Scott Wood17fed142016-05-30 13:57:56 -0500826 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200827 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100828 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200829}
830
831/**
832 * panic_nand_wait - [GENERIC] wait until the command is done
833 * @mtd: MTD device structure
834 * @chip: NAND chip structure
835 * @timeo: timeout
836 *
837 * Wait for command done. This is a helper function for nand_wait used when
838 * we are in interrupt context. May happen when in panic and trying to write
839 * an oops through mtdoops.
840 */
841static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
842 unsigned long timeo)
843{
844 int i;
845 for (i = 0; i < timeo; i++) {
846 if (chip->dev_ready) {
847 if (chip->dev_ready(mtd))
848 break;
849 } else {
850 if (chip->read_byte(mtd) & NAND_STATUS_READY)
851 break;
852 }
853 mdelay(1);
854 }
William Juul52c07962007-10-31 13:53:06 +0100855}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200856
857/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000858 * nand_wait - [DEFAULT] wait until the command is done
859 * @mtd: MTD device structure
860 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200861 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500862 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100863 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200864static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200865{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500866 int status;
867 unsigned long timeo = 400;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100868
Heiko Schocherf5895d12014-06-24 10:10:04 +0200869 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100870
Heiko Schocherf5895d12014-06-24 10:10:04 +0200871 /*
872 * Apply this short delay always to ensure that we do wait tWB in any
873 * case on any machine.
874 */
875 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100876
Heiko Schocherf5895d12014-06-24 10:10:04 +0200877 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100878
Heiko Schocherf5895d12014-06-24 10:10:04 +0200879 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
880 u32 time_start;
881
882 time_start = get_timer(0);
883 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200884 if (chip->dev_ready) {
885 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100886 break;
887 } else {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200888 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100889 break;
890 }
891 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200892 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100893
Heiko Schocherf5895d12014-06-24 10:10:04 +0200894 status = (int)chip->read_byte(mtd);
895 /* This can happen if in case of timeout or buggy dev_ready */
896 WARN_ON(!(status & NAND_STATUS_READY));
897 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200898}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500899
900#define BITS_PER_BYTE 8
901
902/**
903 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
904 * @buf: buffer to test
905 * @len: buffer length
906 * @bitflips_threshold: maximum number of bitflips
907 *
908 * Check if a buffer contains only 0xff, which means the underlying region
909 * has been erased and is ready to be programmed.
910 * The bitflips_threshold specify the maximum number of bitflips before
911 * considering the region is not erased.
912 * Note: The logic of this function has been extracted from the memweight
913 * implementation, except that nand_check_erased_buf function exit before
914 * testing the whole buffer if the number of bitflips exceed the
915 * bitflips_threshold value.
916 *
917 * Returns a positive number of bitflips less than or equal to
918 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
919 * threshold.
920 */
921static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
922{
923 const unsigned char *bitmap = buf;
924 int bitflips = 0;
925 int weight;
926
927 for (; len && ((uintptr_t)bitmap) % sizeof(long);
928 len--, bitmap++) {
929 weight = hweight8(*bitmap);
930 bitflips += BITS_PER_BYTE - weight;
931 if (unlikely(bitflips > bitflips_threshold))
932 return -EBADMSG;
933 }
934
935 for (; len >= 4; len -= 4, bitmap += 4) {
936 weight = hweight32(*((u32 *)bitmap));
937 bitflips += 32 - weight;
938 if (unlikely(bitflips > bitflips_threshold))
939 return -EBADMSG;
940 }
941
942 for (; len > 0; len--, bitmap++) {
943 weight = hweight8(*bitmap);
944 bitflips += BITS_PER_BYTE - weight;
945 if (unlikely(bitflips > bitflips_threshold))
946 return -EBADMSG;
947 }
948
949 return bitflips;
950}
951
952/**
953 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
954 * 0xff data
955 * @data: data buffer to test
956 * @datalen: data length
957 * @ecc: ECC buffer
958 * @ecclen: ECC length
959 * @extraoob: extra OOB buffer
960 * @extraooblen: extra OOB length
961 * @bitflips_threshold: maximum number of bitflips
962 *
963 * Check if a data buffer and its associated ECC and OOB data contains only
964 * 0xff pattern, which means the underlying region has been erased and is
965 * ready to be programmed.
966 * The bitflips_threshold specify the maximum number of bitflips before
967 * considering the region as not erased.
968 *
969 * Note:
970 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
971 * different from the NAND page size. When fixing bitflips, ECC engines will
972 * report the number of errors per chunk, and the NAND core infrastructure
973 * expect you to return the maximum number of bitflips for the whole page.
974 * This is why you should always use this function on a single chunk and
975 * not on the whole page. After checking each chunk you should update your
976 * max_bitflips value accordingly.
977 * 2/ When checking for bitflips in erased pages you should not only check
978 * the payload data but also their associated ECC data, because a user might
979 * have programmed almost all bits to 1 but a few. In this case, we
980 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
981 * this case.
982 * 3/ The extraoob argument is optional, and should be used if some of your OOB
983 * data are protected by the ECC engine.
984 * It could also be used if you support subpages and want to attach some
985 * extra OOB data to an ECC chunk.
986 *
987 * Returns a positive number of bitflips less than or equal to
988 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
989 * threshold. In case of success, the passed buffers are filled with 0xff.
990 */
991int nand_check_erased_ecc_chunk(void *data, int datalen,
992 void *ecc, int ecclen,
993 void *extraoob, int extraooblen,
994 int bitflips_threshold)
995{
996 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
997
998 data_bitflips = nand_check_erased_buf(data, datalen,
999 bitflips_threshold);
1000 if (data_bitflips < 0)
1001 return data_bitflips;
1002
1003 bitflips_threshold -= data_bitflips;
1004
1005 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1006 if (ecc_bitflips < 0)
1007 return ecc_bitflips;
1008
1009 bitflips_threshold -= ecc_bitflips;
1010
1011 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1012 bitflips_threshold);
1013 if (extraoob_bitflips < 0)
1014 return extraoob_bitflips;
1015
1016 if (data_bitflips)
1017 memset(data, 0xff, datalen);
1018
1019 if (ecc_bitflips)
1020 memset(ecc, 0xff, ecclen);
1021
1022 if (extraoob_bitflips)
1023 memset(extraoob, 0xff, extraooblen);
1024
1025 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1026}
1027EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001028
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001029/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001030 * nand_read_page_raw - [INTERN] read raw page data without ecc
1031 * @mtd: mtd info structure
1032 * @chip: nand chip info structure
1033 * @buf: buffer to store read data
1034 * @oob_required: caller requires OOB data read to chip->oob_poi
1035 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001036 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001037 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001038 */
William Juul52c07962007-10-31 13:53:06 +01001039static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001040 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001041{
William Juul52c07962007-10-31 13:53:06 +01001042 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001043 if (oob_required)
1044 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01001045 return 0;
1046}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001047
William Juul52c07962007-10-31 13:53:06 +01001048/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001049 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1050 * @mtd: mtd info structure
1051 * @chip: nand chip info structure
1052 * @buf: buffer to store read data
1053 * @oob_required: caller requires OOB data read to chip->oob_poi
1054 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001055 *
1056 * We need a special oob layout and handling even when OOB isn't used.
1057 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001058static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001059 struct nand_chip *chip, uint8_t *buf,
1060 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001061{
1062 int eccsize = chip->ecc.size;
1063 int eccbytes = chip->ecc.bytes;
1064 uint8_t *oob = chip->oob_poi;
1065 int steps, size;
1066
1067 for (steps = chip->ecc.steps; steps > 0; steps--) {
1068 chip->read_buf(mtd, buf, eccsize);
1069 buf += eccsize;
1070
1071 if (chip->ecc.prepad) {
1072 chip->read_buf(mtd, oob, chip->ecc.prepad);
1073 oob += chip->ecc.prepad;
1074 }
1075
1076 chip->read_buf(mtd, oob, eccbytes);
1077 oob += eccbytes;
1078
1079 if (chip->ecc.postpad) {
1080 chip->read_buf(mtd, oob, chip->ecc.postpad);
1081 oob += chip->ecc.postpad;
1082 }
1083 }
1084
1085 size = mtd->oobsize - (oob - chip->oob_poi);
1086 if (size)
1087 chip->read_buf(mtd, oob, size);
1088
1089 return 0;
1090}
1091
1092/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001093 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1094 * @mtd: mtd info structure
1095 * @chip: nand chip info structure
1096 * @buf: buffer to store read data
1097 * @oob_required: caller requires OOB data read to chip->oob_poi
1098 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001099 */
1100static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001101 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001102{
1103 int i, eccsize = chip->ecc.size;
1104 int eccbytes = chip->ecc.bytes;
1105 int eccsteps = chip->ecc.steps;
1106 uint8_t *p = buf;
1107 uint8_t *ecc_calc = chip->buffers->ecccalc;
1108 uint8_t *ecc_code = chip->buffers->ecccode;
1109 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001110 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001111
Sergey Lapin3a38a552013-01-14 03:46:50 +00001112 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001113
William Juul52c07962007-10-31 13:53:06 +01001114 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1115 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001116
William Juul52c07962007-10-31 13:53:06 +01001117 for (i = 0; i < chip->ecc.total; i++)
1118 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001119
William Juul52c07962007-10-31 13:53:06 +01001120 eccsteps = chip->ecc.steps;
1121 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001122
William Juul52c07962007-10-31 13:53:06 +01001123 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1124 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001125
William Juul52c07962007-10-31 13:53:06 +01001126 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001127 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001128 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001129 } else {
William Juul52c07962007-10-31 13:53:06 +01001130 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001131 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1132 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001133 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001134 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001135}
1136
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001137/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001138 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001139 * @mtd: mtd info structure
1140 * @chip: nand chip info structure
1141 * @data_offs: offset of requested data within the page
1142 * @readlen: data length
1143 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001144 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001145 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001146static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001147 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1148 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001149{
1150 int start_step, end_step, num_steps;
1151 uint32_t *eccpos = chip->ecc.layout->eccpos;
1152 uint8_t *p;
1153 int data_col_addr, i, gaps = 0;
1154 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1155 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001156 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001157 unsigned int max_bitflips = 0;
Scott Wood3628f002008-10-24 16:20:43 -05001158
Sergey Lapin3a38a552013-01-14 03:46:50 +00001159 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001160 start_step = data_offs / chip->ecc.size;
1161 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1162 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001163 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001164
Sergey Lapin3a38a552013-01-14 03:46:50 +00001165 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001166 datafrag_len = num_steps * chip->ecc.size;
1167 eccfrag_len = num_steps * chip->ecc.bytes;
1168
1169 data_col_addr = start_step * chip->ecc.size;
1170 /* If we read not a page aligned data */
1171 if (data_col_addr != 0)
1172 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1173
1174 p = bufpoi + data_col_addr;
1175 chip->read_buf(mtd, p, datafrag_len);
1176
Sergey Lapin3a38a552013-01-14 03:46:50 +00001177 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001178 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1179 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1180
Sergey Lapin3a38a552013-01-14 03:46:50 +00001181 /*
1182 * The performance is faster if we position offsets according to
1183 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1184 */
Scott Wood3628f002008-10-24 16:20:43 -05001185 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001186 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001187 gaps = 1;
1188 break;
1189 }
1190 }
1191 if (gaps) {
1192 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1193 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1194 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001195 /*
1196 * Send the command to read the particular ECC bytes take care
1197 * about buswidth alignment in read_buf.
1198 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001199 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001200 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001201 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001202 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001203 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001204 aligned_len++;
1205
Christian Hitzb8a6b372011-10-12 09:32:02 +02001206 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1207 mtd->writesize + aligned_pos, -1);
Scott Wood3628f002008-10-24 16:20:43 -05001208 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1209 }
1210
1211 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001212 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001213
1214 p = bufpoi + data_col_addr;
1215 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1216 int stat;
1217
Christian Hitzb8a6b372011-10-12 09:32:02 +02001218 stat = chip->ecc.correct(mtd, p,
1219 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001220 if (stat == -EBADMSG &&
1221 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1222 /* check for empty pages with bitflips */
1223 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1224 &chip->buffers->ecccode[i],
1225 chip->ecc.bytes,
1226 NULL, 0,
1227 chip->ecc.strength);
1228 }
1229
Heiko Schocherf5895d12014-06-24 10:10:04 +02001230 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001231 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001232 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001233 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001234 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1235 }
Scott Wood3628f002008-10-24 16:20:43 -05001236 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001237 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001238}
1239
1240/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001241 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1242 * @mtd: mtd info structure
1243 * @chip: nand chip info structure
1244 * @buf: buffer to store read data
1245 * @oob_required: caller requires OOB data read to chip->oob_poi
1246 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001247 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001248 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001249 */
William Juul52c07962007-10-31 13:53:06 +01001250static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001251 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001252{
William Juul52c07962007-10-31 13:53:06 +01001253 int i, eccsize = chip->ecc.size;
1254 int eccbytes = chip->ecc.bytes;
1255 int eccsteps = chip->ecc.steps;
1256 uint8_t *p = buf;
1257 uint8_t *ecc_calc = chip->buffers->ecccalc;
1258 uint8_t *ecc_code = chip->buffers->ecccode;
1259 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001260 unsigned int max_bitflips = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001261
William Juul52c07962007-10-31 13:53:06 +01001262 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1263 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1264 chip->read_buf(mtd, p, eccsize);
1265 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1266 }
1267 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001268
William Juul52c07962007-10-31 13:53:06 +01001269 for (i = 0; i < chip->ecc.total; i++)
1270 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001271
William Juul52c07962007-10-31 13:53:06 +01001272 eccsteps = chip->ecc.steps;
1273 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001274
William Juul52c07962007-10-31 13:53:06 +01001275 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1276 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001277
William Juul52c07962007-10-31 13:53:06 +01001278 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001279 if (stat == -EBADMSG &&
1280 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1281 /* check for empty pages with bitflips */
1282 stat = nand_check_erased_ecc_chunk(p, eccsize,
1283 &ecc_code[i], eccbytes,
1284 NULL, 0,
1285 chip->ecc.strength);
1286 }
1287
Heiko Schocherf5895d12014-06-24 10:10:04 +02001288 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001289 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001290 } else {
William Juul52c07962007-10-31 13:53:06 +01001291 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001292 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1293 }
William Juul52c07962007-10-31 13:53:06 +01001294 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001295 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001296}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001297
William Juul52c07962007-10-31 13:53:06 +01001298/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001299 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1300 * @mtd: mtd info structure
1301 * @chip: nand chip info structure
1302 * @buf: buffer to store read data
1303 * @oob_required: caller requires OOB data read to chip->oob_poi
1304 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001305 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001306 * Hardware ECC for large page chips, require OOB to be read first. For this
1307 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1308 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1309 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1310 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001311 */
1312static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001313 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001314{
1315 int i, eccsize = chip->ecc.size;
1316 int eccbytes = chip->ecc.bytes;
1317 int eccsteps = chip->ecc.steps;
1318 uint8_t *p = buf;
1319 uint8_t *ecc_code = chip->buffers->ecccode;
1320 uint32_t *eccpos = chip->ecc.layout->eccpos;
1321 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001322 unsigned int max_bitflips = 0;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001323
1324 /* Read the OOB area first */
1325 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1326 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1327 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1328
1329 for (i = 0; i < chip->ecc.total; i++)
1330 ecc_code[i] = chip->oob_poi[eccpos[i]];
1331
1332 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1333 int stat;
1334
1335 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1336 chip->read_buf(mtd, p, eccsize);
1337 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1338
1339 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001340 if (stat == -EBADMSG &&
1341 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1342 /* check for empty pages with bitflips */
1343 stat = nand_check_erased_ecc_chunk(p, eccsize,
1344 &ecc_code[i], eccbytes,
1345 NULL, 0,
1346 chip->ecc.strength);
1347 }
1348
Heiko Schocherf5895d12014-06-24 10:10:04 +02001349 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001350 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001351 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001352 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001353 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1354 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001355 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001356 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001357}
1358
1359/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001360 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1361 * @mtd: mtd info structure
1362 * @chip: nand chip info structure
1363 * @buf: buffer to store read data
1364 * @oob_required: caller requires OOB data read to chip->oob_poi
1365 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001366 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001367 * The hw generator calculates the error syndrome automatically. Therefore we
1368 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01001369 */
1370static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001371 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001372{
1373 int i, eccsize = chip->ecc.size;
1374 int eccbytes = chip->ecc.bytes;
1375 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05001376 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01001377 uint8_t *p = buf;
1378 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001379 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01001380
1381 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1382 int stat;
1383
1384 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1385 chip->read_buf(mtd, p, eccsize);
1386
1387 if (chip->ecc.prepad) {
1388 chip->read_buf(mtd, oob, chip->ecc.prepad);
1389 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001390 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001391
William Juul52c07962007-10-31 13:53:06 +01001392 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1393 chip->read_buf(mtd, oob, eccbytes);
1394 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001395
William Juul52c07962007-10-31 13:53:06 +01001396 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001397
William Juul52c07962007-10-31 13:53:06 +01001398 if (chip->ecc.postpad) {
1399 chip->read_buf(mtd, oob, chip->ecc.postpad);
1400 oob += chip->ecc.postpad;
1401 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05001402
1403 if (stat == -EBADMSG &&
1404 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1405 /* check for empty pages with bitflips */
1406 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1407 oob - eccpadbytes,
1408 eccpadbytes,
1409 NULL, 0,
1410 chip->ecc.strength);
1411 }
1412
1413 if (stat < 0) {
1414 mtd->ecc_stats.failed++;
1415 } else {
1416 mtd->ecc_stats.corrected += stat;
1417 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1418 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001419 }
William Juul52c07962007-10-31 13:53:06 +01001420
1421 /* Calculate remaining oob bytes */
1422 i = mtd->oobsize - (oob - chip->oob_poi);
1423 if (i)
1424 chip->read_buf(mtd, oob, i);
1425
Heiko Schocherf5895d12014-06-24 10:10:04 +02001426 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001427}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001428
1429/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001430 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1431 * @chip: nand chip structure
1432 * @oob: oob destination address
1433 * @ops: oob ops structure
1434 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01001435 */
1436static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1437 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001438{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001439 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001440
Sergey Lapin3a38a552013-01-14 03:46:50 +00001441 case MTD_OPS_PLACE_OOB:
1442 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001443 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1444 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001445
Sergey Lapin3a38a552013-01-14 03:46:50 +00001446 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01001447 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1448 uint32_t boffs = 0, roffs = ops->ooboffs;
1449 size_t bytes = 0;
1450
Christian Hitz13fc0e22011-10-12 09:32:01 +02001451 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001452 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01001453 if (unlikely(roffs)) {
1454 if (roffs >= free->length) {
1455 roffs -= free->length;
1456 continue;
1457 }
1458 boffs = free->offset + roffs;
1459 bytes = min_t(size_t, len,
1460 (free->length - roffs));
1461 roffs = 0;
1462 } else {
1463 bytes = min_t(size_t, len, free->length);
1464 boffs = free->offset;
1465 }
1466 memcpy(oob, chip->oob_poi + boffs, bytes);
1467 oob += bytes;
1468 }
1469 return oob;
1470 }
1471 default:
1472 BUG();
1473 }
1474 return NULL;
1475}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001476
1477/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001478 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1479 * @mtd: MTD device structure
1480 * @retry_mode: the retry mode to use
1481 *
1482 * Some vendors supply a special command to shift the Vt threshold, to be used
1483 * when there are too many bitflips in a page (i.e., ECC error). After setting
1484 * a new threshold, the host should retry reading the page.
1485 */
1486static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1487{
Scott Wood17fed142016-05-30 13:57:56 -05001488 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001489
1490 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1491
1492 if (retry_mode >= chip->read_retries)
1493 return -EINVAL;
1494
1495 if (!chip->setup_read_retry)
1496 return -EOPNOTSUPP;
1497
1498 return chip->setup_read_retry(mtd, retry_mode);
1499}
1500
1501/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001502 * nand_do_read_ops - [INTERN] Read data with ECC
1503 * @mtd: MTD device structure
1504 * @from: offset to read from
1505 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001506 *
William Juul52c07962007-10-31 13:53:06 +01001507 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001508 */
William Juul52c07962007-10-31 13:53:06 +01001509static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1510 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001511{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001512 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05001513 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01001514 int ret = 0;
1515 uint32_t readlen = ops->len;
1516 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05001517 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02001518
William Juul52c07962007-10-31 13:53:06 +01001519 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001520 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01001521 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001522 int retry_mode = 0;
1523 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001524
William Juul52c07962007-10-31 13:53:06 +01001525 chipnr = (int)(from >> chip->chip_shift);
1526 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001527
William Juul52c07962007-10-31 13:53:06 +01001528 realpage = (int)(from >> chip->page_shift);
1529 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001530
William Juul52c07962007-10-31 13:53:06 +01001531 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001532
William Juul52c07962007-10-31 13:53:06 +01001533 buf = ops->datbuf;
1534 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001535 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001536
Christian Hitz13fc0e22011-10-12 09:32:01 +02001537 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001538 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06001539
Heiko Schocherf5895d12014-06-24 10:10:04 +02001540 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01001541 bytes = min(mtd->writesize - col, readlen);
1542 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001543
Scott Wood3ea94ed2015-06-26 19:03:26 -05001544 if (!aligned)
1545 use_bufpoi = 1;
1546 else
1547 use_bufpoi = 0;
1548
Sergey Lapin3a38a552013-01-14 03:46:50 +00001549 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01001550 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001551 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1552
1553 if (use_bufpoi && aligned)
1554 pr_debug("%s: using read bounce buffer for buf@%p\n",
1555 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001556
Heiko Schocherf5895d12014-06-24 10:10:04 +02001557read_retry:
Sergey Lapin3a38a552013-01-14 03:46:50 +00001558 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001559
Paul Burton700a76c2013-09-04 15:16:56 +01001560 /*
1561 * Now read the page into the buffer. Absent an error,
1562 * the read methods return max bitflips per ecc step.
1563 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001564 if (unlikely(ops->mode == MTD_OPS_RAW))
1565 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1566 oob_required,
1567 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001568 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001569 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02001570 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001571 col, bytes, bufpoi,
1572 page);
William Juul52c07962007-10-31 13:53:06 +01001573 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001574 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001575 oob_required, page);
1576 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001577 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001578 /* Invalidate page cache */
1579 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01001580 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001581 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001582
Paul Burton700a76c2013-09-04 15:16:56 +01001583 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1584
William Juul52c07962007-10-31 13:53:06 +01001585 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05001586 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001587 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001588 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01001589 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05001590 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01001591 chip->pagebuf_bitflips = ret;
1592 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001593 /* Invalidate page cache */
1594 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01001595 }
William Juul52c07962007-10-31 13:53:06 +01001596 memcpy(buf, chip->buffers->databuf + col, bytes);
1597 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001598
William Juul52c07962007-10-31 13:53:06 +01001599 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001600 int toread = min(oobreadlen, max_oobsize);
1601
1602 if (toread) {
1603 oob = nand_transfer_oob(chip,
1604 oob, ops, toread);
1605 oobreadlen -= toread;
1606 }
William Juul52c07962007-10-31 13:53:06 +01001607 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001608
1609 if (chip->options & NAND_NEED_READRDY) {
1610 /* Apply delay or wait for ready/busy pin */
1611 if (!chip->dev_ready)
1612 udelay(chip->chip_delay);
1613 else
1614 nand_wait_ready(mtd);
1615 }
1616
1617 if (mtd->ecc_stats.failed - ecc_failures) {
1618 if (retry_mode + 1 < chip->read_retries) {
1619 retry_mode++;
1620 ret = nand_setup_read_retry(mtd,
1621 retry_mode);
1622 if (ret < 0)
1623 break;
1624
1625 /* Reset failures; retry */
1626 mtd->ecc_stats.failed = ecc_failures;
1627 goto read_retry;
1628 } else {
1629 /* No more retry modes; real failure */
1630 ecc_fail = true;
1631 }
1632 }
1633
1634 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01001635 } else {
1636 memcpy(buf, chip->buffers->databuf + col, bytes);
1637 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01001638 max_bitflips = max_t(unsigned int, max_bitflips,
1639 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01001640 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001641
William Juul52c07962007-10-31 13:53:06 +01001642 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001643
Heiko Schocherf5895d12014-06-24 10:10:04 +02001644 /* Reset to retry mode 0 */
1645 if (retry_mode) {
1646 ret = nand_setup_read_retry(mtd, 0);
1647 if (ret < 0)
1648 break;
1649 retry_mode = 0;
1650 }
1651
William Juul52c07962007-10-31 13:53:06 +01001652 if (!readlen)
1653 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001654
Sergey Lapin3a38a552013-01-14 03:46:50 +00001655 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01001656 col = 0;
1657 /* Increment page address */
1658 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001659
William Juul52c07962007-10-31 13:53:06 +01001660 page = realpage & chip->pagemask;
1661 /* Check, if we cross a chip boundary */
1662 if (!page) {
1663 chipnr++;
1664 chip->select_chip(mtd, -1);
1665 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001666 }
William Juul52c07962007-10-31 13:53:06 +01001667 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001668 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001669
William Juul52c07962007-10-31 13:53:06 +01001670 ops->retlen = ops->len - (size_t) readlen;
1671 if (oob)
1672 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001673
Heiko Schocherf5895d12014-06-24 10:10:04 +02001674 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01001675 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001676
Heiko Schocherf5895d12014-06-24 10:10:04 +02001677 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01001678 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001679
Paul Burton700a76c2013-09-04 15:16:56 +01001680 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001681}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001682
William Juul52c07962007-10-31 13:53:06 +01001683/**
Christian Hitz13fc0e22011-10-12 09:32:01 +02001684 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapin3a38a552013-01-14 03:46:50 +00001685 * @mtd: MTD device structure
1686 * @from: offset to read from
1687 * @len: number of bytes to read
1688 * @retlen: pointer to variable to store the number of read bytes
1689 * @buf: the databuffer to put data
William Juul52c07962007-10-31 13:53:06 +01001690 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001691 * Get hold of the chip and call nand_do_read.
William Juul52c07962007-10-31 13:53:06 +01001692 */
1693static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1694 size_t *retlen, uint8_t *buf)
1695{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001696 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01001697 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001698
Heiko Schocherf5895d12014-06-24 10:10:04 +02001699 nand_get_device(mtd, FL_READING);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001700 memset(&ops, 0, sizeof(ops));
Sergey Lapin3a38a552013-01-14 03:46:50 +00001701 ops.len = len;
1702 ops.datbuf = buf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001703 ops.mode = MTD_OPS_PLACE_OOB;
1704 ret = nand_do_read_ops(mtd, from, &ops);
1705 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01001706 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001707 return ret;
1708}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001709
William Juul52c07962007-10-31 13:53:06 +01001710/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001711 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1712 * @mtd: mtd info structure
1713 * @chip: nand chip info structure
1714 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001715 */
1716static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001717 int page)
William Juul52c07962007-10-31 13:53:06 +01001718{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001719 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juul52c07962007-10-31 13:53:06 +01001720 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001721 return 0;
William Juul52c07962007-10-31 13:53:06 +01001722}
1723
1724/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001725 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01001726 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00001727 * @mtd: mtd info structure
1728 * @chip: nand chip info structure
1729 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001730 */
1731static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001732 int page)
William Juul52c07962007-10-31 13:53:06 +01001733{
William Juul52c07962007-10-31 13:53:06 +01001734 int length = mtd->oobsize;
1735 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1736 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001737 uint8_t *bufpoi = chip->oob_poi;
William Juul52c07962007-10-31 13:53:06 +01001738 int i, toread, sndrnd = 0, pos;
1739
1740 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1741 for (i = 0; i < chip->ecc.steps; i++) {
1742 if (sndrnd) {
1743 pos = eccsize + i * (eccsize + chunk);
1744 if (mtd->writesize > 512)
1745 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1746 else
1747 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001748 } else
William Juul52c07962007-10-31 13:53:06 +01001749 sndrnd = 1;
1750 toread = min_t(int, length, chunk);
1751 chip->read_buf(mtd, bufpoi, toread);
1752 bufpoi += toread;
1753 length -= toread;
1754 }
1755 if (length > 0)
1756 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001757
Sergey Lapin3a38a552013-01-14 03:46:50 +00001758 return 0;
William Juul52c07962007-10-31 13:53:06 +01001759}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001760
William Juul52c07962007-10-31 13:53:06 +01001761/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001762 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1763 * @mtd: mtd info structure
1764 * @chip: nand chip info structure
1765 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001766 */
1767static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1768 int page)
1769{
1770 int status = 0;
1771 const uint8_t *buf = chip->oob_poi;
1772 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001773
William Juul52c07962007-10-31 13:53:06 +01001774 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1775 chip->write_buf(mtd, buf, length);
1776 /* Send command to program the OOB data */
1777 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001778
William Juul52c07962007-10-31 13:53:06 +01001779 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001780
William Juul52c07962007-10-31 13:53:06 +01001781 return status & NAND_STATUS_FAIL ? -EIO : 0;
1782}
1783
1784/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001785 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1786 * with syndrome - only for large page flash
1787 * @mtd: mtd info structure
1788 * @chip: nand chip info structure
1789 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001790 */
1791static int nand_write_oob_syndrome(struct mtd_info *mtd,
1792 struct nand_chip *chip, int page)
1793{
1794 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1795 int eccsize = chip->ecc.size, length = mtd->oobsize;
1796 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1797 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001798
1799 /*
William Juul52c07962007-10-31 13:53:06 +01001800 * data-ecc-data-ecc ... ecc-oob
1801 * or
1802 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001803 */
William Juul52c07962007-10-31 13:53:06 +01001804 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1805 pos = steps * (eccsize + chunk);
1806 steps = 0;
1807 } else
1808 pos = eccsize;
1809
1810 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1811 for (i = 0; i < steps; i++) {
1812 if (sndcmd) {
1813 if (mtd->writesize <= 512) {
1814 uint32_t fill = 0xFFFFFFFF;
1815
1816 len = eccsize;
1817 while (len > 0) {
1818 int num = min_t(int, len, 4);
1819 chip->write_buf(mtd, (uint8_t *)&fill,
1820 num);
1821 len -= num;
1822 }
1823 } else {
1824 pos = eccsize + i * (eccsize + chunk);
1825 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1826 }
1827 } else
1828 sndcmd = 1;
1829 len = min_t(int, length, chunk);
1830 chip->write_buf(mtd, bufpoi, len);
1831 bufpoi += len;
1832 length -= len;
1833 }
1834 if (length > 0)
1835 chip->write_buf(mtd, bufpoi, length);
1836
1837 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1838 status = chip->waitfunc(mtd, chip);
1839
1840 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001841}
1842
1843/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001844 * nand_do_read_oob - [INTERN] NAND read out-of-band
1845 * @mtd: MTD device structure
1846 * @from: offset to read from
1847 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001848 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001849 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001850 */
William Juul52c07962007-10-31 13:53:06 +01001851static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1852 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001853{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001854 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05001855 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001856 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01001857 int readlen = ops->ooblen;
1858 int len;
1859 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001860 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001861
Heiko Schocherf5895d12014-06-24 10:10:04 +02001862 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02001863 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001864
Sergey Lapin3a38a552013-01-14 03:46:50 +00001865 stats = mtd->ecc_stats;
1866
Scott Wood52ab7ce2016-05-30 13:57:58 -05001867 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001868
William Juul52c07962007-10-31 13:53:06 +01001869 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001870 pr_debug("%s: attempt to start read outside oob\n",
1871 __func__);
William Juul52c07962007-10-31 13:53:06 +01001872 return -EINVAL;
1873 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001874
1875 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001876 if (unlikely(from >= mtd->size ||
1877 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1878 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001879 pr_debug("%s: attempt to read beyond end of device\n",
1880 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001881 return -EINVAL;
1882 }
1883
William Juul52c07962007-10-31 13:53:06 +01001884 chipnr = (int)(from >> chip->chip_shift);
1885 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001886
William Juul52c07962007-10-31 13:53:06 +01001887 /* Shift to get page */
1888 realpage = (int)(from >> chip->page_shift);
1889 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001890
Christian Hitz13fc0e22011-10-12 09:32:01 +02001891 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06001892 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02001893
Sergey Lapin3a38a552013-01-14 03:46:50 +00001894 if (ops->mode == MTD_OPS_RAW)
1895 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1896 else
1897 ret = chip->ecc.read_oob(mtd, chip, page);
1898
1899 if (ret < 0)
1900 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001901
William Juul52c07962007-10-31 13:53:06 +01001902 len = min(len, readlen);
1903 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001904
Heiko Schocherf5895d12014-06-24 10:10:04 +02001905 if (chip->options & NAND_NEED_READRDY) {
1906 /* Apply delay or wait for ready/busy pin */
1907 if (!chip->dev_ready)
1908 udelay(chip->chip_delay);
1909 else
1910 nand_wait_ready(mtd);
1911 }
1912
William Juul52c07962007-10-31 13:53:06 +01001913 readlen -= len;
1914 if (!readlen)
1915 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001916
William Juul52c07962007-10-31 13:53:06 +01001917 /* Increment page address */
1918 realpage++;
1919
1920 page = realpage & chip->pagemask;
1921 /* Check, if we cross a chip boundary */
1922 if (!page) {
1923 chipnr++;
1924 chip->select_chip(mtd, -1);
1925 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001926 }
William Juul52c07962007-10-31 13:53:06 +01001927 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001928 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001929
Sergey Lapin3a38a552013-01-14 03:46:50 +00001930 ops->oobretlen = ops->ooblen - readlen;
1931
1932 if (ret < 0)
1933 return ret;
1934
1935 if (mtd->ecc_stats.failed - stats.failed)
1936 return -EBADMSG;
1937
1938 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001939}
1940
1941/**
William Juul52c07962007-10-31 13:53:06 +01001942 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00001943 * @mtd: MTD device structure
1944 * @from: offset to read from
1945 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001946 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001947 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001948 */
William Juul52c07962007-10-31 13:53:06 +01001949static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1950 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001951{
William Juul52c07962007-10-31 13:53:06 +01001952 int ret = -ENOTSUPP;
1953
1954 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001955
1956 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01001957 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001958 pr_debug("%s: attempt to read beyond end of device\n",
1959 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001960 return -EINVAL;
1961 }
1962
Heiko Schocherf5895d12014-06-24 10:10:04 +02001963 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001964
Christian Hitz13fc0e22011-10-12 09:32:01 +02001965 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001966 case MTD_OPS_PLACE_OOB:
1967 case MTD_OPS_AUTO_OOB:
1968 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001969 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001970
William Juul52c07962007-10-31 13:53:06 +01001971 default:
1972 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001973 }
1974
William Juul52c07962007-10-31 13:53:06 +01001975 if (!ops->datbuf)
1976 ret = nand_do_read_oob(mtd, from, ops);
1977 else
1978 ret = nand_do_read_ops(mtd, from, ops);
1979
Christian Hitz13fc0e22011-10-12 09:32:01 +02001980out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001981 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001982 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001983}
1984
1985
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001986/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001987 * nand_write_page_raw - [INTERN] raw page write function
1988 * @mtd: mtd info structure
1989 * @chip: nand chip info structure
1990 * @buf: data buffer
1991 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05001992 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05001993 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001994 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01001995 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001996static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05001997 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001998{
William Juul52c07962007-10-31 13:53:06 +01001999 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002000 if (oob_required)
2001 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2002
2003 return 0;
William Juul52c07962007-10-31 13:53:06 +01002004}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002005
William Juul52c07962007-10-31 13:53:06 +01002006/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002007 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2008 * @mtd: mtd info structure
2009 * @chip: nand chip info structure
2010 * @buf: data buffer
2011 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002012 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002013 *
2014 * We need a special oob layout and handling even when ECC isn't checked.
2015 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002016static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002017 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002018 const uint8_t *buf, int oob_required,
2019 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002020{
2021 int eccsize = chip->ecc.size;
2022 int eccbytes = chip->ecc.bytes;
2023 uint8_t *oob = chip->oob_poi;
2024 int steps, size;
2025
2026 for (steps = chip->ecc.steps; steps > 0; steps--) {
2027 chip->write_buf(mtd, buf, eccsize);
2028 buf += eccsize;
2029
2030 if (chip->ecc.prepad) {
2031 chip->write_buf(mtd, oob, chip->ecc.prepad);
2032 oob += chip->ecc.prepad;
2033 }
2034
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002035 chip->write_buf(mtd, oob, eccbytes);
David Brownellee86b8d2009-11-07 16:27:01 -05002036 oob += eccbytes;
2037
2038 if (chip->ecc.postpad) {
2039 chip->write_buf(mtd, oob, chip->ecc.postpad);
2040 oob += chip->ecc.postpad;
2041 }
2042 }
2043
2044 size = mtd->oobsize - (oob - chip->oob_poi);
2045 if (size)
2046 chip->write_buf(mtd, oob, size);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002047
2048 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002049}
2050/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002051 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2052 * @mtd: mtd info structure
2053 * @chip: nand chip info structure
2054 * @buf: data buffer
2055 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002056 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002057 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002058static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002059 const uint8_t *buf, int oob_required,
2060 int page)
William Juul52c07962007-10-31 13:53:06 +01002061{
2062 int i, eccsize = chip->ecc.size;
2063 int eccbytes = chip->ecc.bytes;
2064 int eccsteps = chip->ecc.steps;
2065 uint8_t *ecc_calc = chip->buffers->ecccalc;
2066 const uint8_t *p = buf;
2067 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002068
Sergey Lapin3a38a552013-01-14 03:46:50 +00002069 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002070 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2071 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002072
William Juul52c07962007-10-31 13:53:06 +01002073 for (i = 0; i < chip->ecc.total; i++)
2074 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002075
Scott Wood46e13102016-05-30 13:57:57 -05002076 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002077}
2078
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002079/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002080 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2081 * @mtd: mtd info structure
2082 * @chip: nand chip info structure
2083 * @buf: data buffer
2084 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002085 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002086 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002087static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002088 const uint8_t *buf, int oob_required,
2089 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002090{
William Juul52c07962007-10-31 13:53:06 +01002091 int i, eccsize = chip->ecc.size;
2092 int eccbytes = chip->ecc.bytes;
2093 int eccsteps = chip->ecc.steps;
2094 uint8_t *ecc_calc = chip->buffers->ecccalc;
2095 const uint8_t *p = buf;
2096 uint32_t *eccpos = chip->ecc.layout->eccpos;
2097
2098 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2099 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2100 chip->write_buf(mtd, p, eccsize);
2101 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2102 }
2103
2104 for (i = 0; i < chip->ecc.total; i++)
2105 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2106
2107 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002108
2109 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002110}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002111
Heiko Schocherf5895d12014-06-24 10:10:04 +02002112
2113/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002114 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002115 * @mtd: mtd info structure
2116 * @chip: nand chip info structure
2117 * @offset: column address of subpage within the page
2118 * @data_len: data length
2119 * @buf: data buffer
2120 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002121 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002122 */
2123static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2124 struct nand_chip *chip, uint32_t offset,
2125 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002126 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002127{
2128 uint8_t *oob_buf = chip->oob_poi;
2129 uint8_t *ecc_calc = chip->buffers->ecccalc;
2130 int ecc_size = chip->ecc.size;
2131 int ecc_bytes = chip->ecc.bytes;
2132 int ecc_steps = chip->ecc.steps;
2133 uint32_t *eccpos = chip->ecc.layout->eccpos;
2134 uint32_t start_step = offset / ecc_size;
2135 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2136 int oob_bytes = mtd->oobsize / ecc_steps;
2137 int step, i;
2138
2139 for (step = 0; step < ecc_steps; step++) {
2140 /* configure controller for WRITE access */
2141 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2142
2143 /* write data (untouched subpages already masked by 0xFF) */
2144 chip->write_buf(mtd, buf, ecc_size);
2145
2146 /* mask ECC of un-touched subpages by padding 0xFF */
2147 if ((step < start_step) || (step > end_step))
2148 memset(ecc_calc, 0xff, ecc_bytes);
2149 else
2150 chip->ecc.calculate(mtd, buf, ecc_calc);
2151
2152 /* mask OOB of un-touched subpages by padding 0xFF */
2153 /* if oob_required, preserve OOB metadata of written subpage */
2154 if (!oob_required || (step < start_step) || (step > end_step))
2155 memset(oob_buf, 0xff, oob_bytes);
2156
2157 buf += ecc_size;
2158 ecc_calc += ecc_bytes;
2159 oob_buf += oob_bytes;
2160 }
2161
2162 /* copy calculated ECC for whole page to chip->buffer->oob */
2163 /* this include masked-value(0xFF) for unwritten subpages */
2164 ecc_calc = chip->buffers->ecccalc;
2165 for (i = 0; i < chip->ecc.total; i++)
2166 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2167
2168 /* write OOB buffer to NAND device */
2169 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2170
2171 return 0;
2172}
2173
2174
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002175/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002176 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2177 * @mtd: mtd info structure
2178 * @chip: nand chip info structure
2179 * @buf: data buffer
2180 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002181 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002182 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002183 * The hw generator calculates the error syndrome automatically. Therefore we
2184 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002185 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002186static int nand_write_page_syndrome(struct mtd_info *mtd,
2187 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002188 const uint8_t *buf, int oob_required,
2189 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002190{
William Juul52c07962007-10-31 13:53:06 +01002191 int i, eccsize = chip->ecc.size;
2192 int eccbytes = chip->ecc.bytes;
2193 int eccsteps = chip->ecc.steps;
2194 const uint8_t *p = buf;
2195 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002196
William Juul52c07962007-10-31 13:53:06 +01002197 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002198
William Juul52c07962007-10-31 13:53:06 +01002199 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2200 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002201
William Juul52c07962007-10-31 13:53:06 +01002202 if (chip->ecc.prepad) {
2203 chip->write_buf(mtd, oob, chip->ecc.prepad);
2204 oob += chip->ecc.prepad;
2205 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002206
William Juul52c07962007-10-31 13:53:06 +01002207 chip->ecc.calculate(mtd, p, oob);
2208 chip->write_buf(mtd, oob, eccbytes);
2209 oob += eccbytes;
2210
2211 if (chip->ecc.postpad) {
2212 chip->write_buf(mtd, oob, chip->ecc.postpad);
2213 oob += chip->ecc.postpad;
2214 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002215 }
2216
William Juul52c07962007-10-31 13:53:06 +01002217 /* Calculate remaining oob bytes */
2218 i = mtd->oobsize - (oob - chip->oob_poi);
2219 if (i)
2220 chip->write_buf(mtd, oob, i);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002221
2222 return 0;
William Juul52c07962007-10-31 13:53:06 +01002223}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002224
William Juul52c07962007-10-31 13:53:06 +01002225/**
2226 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00002227 * @mtd: MTD device structure
2228 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02002229 * @offset: address offset within the page
2230 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00002231 * @buf: the data to write
2232 * @oob_required: must write chip->oob_poi to OOB
2233 * @page: page number to write
2234 * @cached: cached programming
2235 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01002236 */
2237static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002238 uint32_t offset, int data_len, const uint8_t *buf,
2239 int oob_required, int page, int cached, int raw)
William Juul52c07962007-10-31 13:53:06 +01002240{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002241 int status, subpage;
2242
2243 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2244 chip->ecc.write_subpage)
2245 subpage = offset || (data_len < mtd->writesize);
2246 else
2247 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002248
William Juul52c07962007-10-31 13:53:06 +01002249 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2250
2251 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02002252 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05002253 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002254 else if (subpage)
2255 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002256 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01002257 else
Scott Wood46e13102016-05-30 13:57:57 -05002258 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2259 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002260
2261 if (status < 0)
2262 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002263
William Juul52c07962007-10-31 13:53:06 +01002264 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00002265 * Cached progamming disabled for now. Not sure if it's worth the
2266 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juul52c07962007-10-31 13:53:06 +01002267 */
2268 cached = 0;
2269
Heiko Schocherf5895d12014-06-24 10:10:04 +02002270 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juul52c07962007-10-31 13:53:06 +01002271
2272 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2273 status = chip->waitfunc(mtd, chip);
2274 /*
2275 * See if operation failed and additional status checks are
Sergey Lapin3a38a552013-01-14 03:46:50 +00002276 * available.
William Juul52c07962007-10-31 13:53:06 +01002277 */
2278 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2279 status = chip->errstat(mtd, chip, FL_WRITING, status,
2280 page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002281
William Juul52c07962007-10-31 13:53:06 +01002282 if (status & NAND_STATUS_FAIL)
2283 return -EIO;
2284 } else {
2285 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2286 status = chip->waitfunc(mtd, chip);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002287 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002288
William Juul52c07962007-10-31 13:53:06 +01002289 return 0;
2290}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002291
William Juul52c07962007-10-31 13:53:06 +01002292/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002293 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2294 * @mtd: MTD device structure
2295 * @oob: oob data buffer
2296 * @len: oob data write length
2297 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01002298 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002299static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2300 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01002301{
Scott Wood17fed142016-05-30 13:57:56 -05002302 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002303
2304 /*
2305 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2306 * data from a previous OOB read.
2307 */
2308 memset(chip->oob_poi, 0xff, mtd->oobsize);
2309
Christian Hitz13fc0e22011-10-12 09:32:01 +02002310 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002311
Sergey Lapin3a38a552013-01-14 03:46:50 +00002312 case MTD_OPS_PLACE_OOB:
2313 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002314 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2315 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002316
Sergey Lapin3a38a552013-01-14 03:46:50 +00002317 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002318 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2319 uint32_t boffs = 0, woffs = ops->ooboffs;
2320 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002321
Christian Hitz13fc0e22011-10-12 09:32:01 +02002322 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002323 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002324 if (unlikely(woffs)) {
2325 if (woffs >= free->length) {
2326 woffs -= free->length;
2327 continue;
2328 }
2329 boffs = free->offset + woffs;
2330 bytes = min_t(size_t, len,
2331 (free->length - woffs));
2332 woffs = 0;
2333 } else {
2334 bytes = min_t(size_t, len, free->length);
2335 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002336 }
William Juul52c07962007-10-31 13:53:06 +01002337 memcpy(chip->oob_poi + boffs, oob, bytes);
2338 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002339 }
William Juul52c07962007-10-31 13:53:06 +01002340 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002341 }
William Juul52c07962007-10-31 13:53:06 +01002342 default:
2343 BUG();
2344 }
2345 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002346}
2347
Christian Hitzb8a6b372011-10-12 09:32:02 +02002348#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002349
2350/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002351 * nand_do_write_ops - [INTERN] NAND write with ECC
2352 * @mtd: MTD device structure
2353 * @to: offset to write to
2354 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002355 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002356 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002357 */
William Juul52c07962007-10-31 13:53:06 +01002358static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2359 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002360{
William Juul52c07962007-10-31 13:53:06 +01002361 int chipnr, realpage, page, blockmask, column;
Scott Wood17fed142016-05-30 13:57:56 -05002362 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002363 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002364
2365 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002366 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002367
William Juul52c07962007-10-31 13:53:06 +01002368 uint8_t *oob = ops->oobbuf;
2369 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002370 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002371 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002372
William Juul52c07962007-10-31 13:53:06 +01002373 ops->retlen = 0;
2374 if (!writelen)
2375 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002376
Heiko Schocherf5895d12014-06-24 10:10:04 +02002377 /* Reject writes, which are not page aligned */
2378 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002379 pr_notice("%s: attempt to write non page aligned data\n",
2380 __func__);
William Juul52c07962007-10-31 13:53:06 +01002381 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002382 }
2383
2384 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002385
William Juul52c07962007-10-31 13:53:06 +01002386 chipnr = (int)(to >> chip->chip_shift);
2387 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002388
2389 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01002390 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002391 ret = -EIO;
2392 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01002393 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002394
William Juul52c07962007-10-31 13:53:06 +01002395 realpage = (int)(to >> chip->page_shift);
2396 page = realpage & chip->pagemask;
2397 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002398
William Juul52c07962007-10-31 13:53:06 +01002399 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002400 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2401 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01002402 chip->pagebuf = -1;
2403
Christian Hitzb8a6b372011-10-12 09:32:02 +02002404 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002405 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2406 ret = -EINVAL;
2407 goto err_out;
2408 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02002409
Christian Hitz13fc0e22011-10-12 09:32:01 +02002410 while (1) {
William Juul52c07962007-10-31 13:53:06 +01002411 int bytes = mtd->writesize;
2412 int cached = writelen > bytes && page != blockmask;
2413 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002414 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02002415 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002416
2417 if (part_pagewr)
2418 use_bufpoi = 1;
2419 else
2420 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01002421
Heiko Schocherf5895d12014-06-24 10:10:04 +02002422 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05002423 /* Partial page write?, or need to use bounce buffer */
2424 if (use_bufpoi) {
2425 pr_debug("%s: using write bounce buffer for buf@%p\n",
2426 __func__, buf);
William Juul52c07962007-10-31 13:53:06 +01002427 cached = 0;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002428 if (part_pagewr)
2429 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01002430 chip->pagebuf = -1;
2431 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2432 memcpy(&chip->buffers->databuf[column], buf, bytes);
2433 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002434 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002435
Christian Hitzb8a6b372011-10-12 09:32:02 +02002436 if (unlikely(oob)) {
2437 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002438 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002439 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002440 } else {
2441 /* We still need to erase leftover OOB data */
2442 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002443 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002444 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2445 oob_required, page, cached,
2446 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01002447 if (ret)
2448 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002449
William Juul52c07962007-10-31 13:53:06 +01002450 writelen -= bytes;
2451 if (!writelen)
2452 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002453
Heiko Schocherf5895d12014-06-24 10:10:04 +02002454 column = 0;
2455 buf += bytes;
2456 realpage++;
2457
2458 page = realpage & chip->pagemask;
2459 /* Check, if we cross a chip boundary */
2460 if (!page) {
2461 chipnr++;
2462 chip->select_chip(mtd, -1);
2463 chip->select_chip(mtd, chipnr);
2464 }
2465 }
2466
2467 ops->retlen = ops->len - writelen;
2468 if (unlikely(oob))
2469 ops->oobretlen = ops->ooblen;
2470
2471err_out:
2472 chip->select_chip(mtd, -1);
2473 return ret;
2474}
2475
2476/**
2477 * panic_nand_write - [MTD Interface] NAND write with ECC
2478 * @mtd: MTD device structure
2479 * @to: offset to write to
2480 * @len: number of bytes to write
2481 * @retlen: pointer to variable to store the number of written bytes
2482 * @buf: the data to write
2483 *
2484 * NAND write with ECC. Used when performing writes in interrupt context, this
2485 * may for example be called by mtdoops when writing an oops while in panic.
2486 */
2487static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2488 size_t *retlen, const uint8_t *buf)
2489{
Scott Wood17fed142016-05-30 13:57:56 -05002490 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002491 struct mtd_oob_ops ops;
2492 int ret;
2493
2494 /* Wait for the device to get ready */
2495 panic_nand_wait(mtd, chip, 400);
2496
2497 /* Grab the device */
2498 panic_nand_get_device(chip, mtd, FL_WRITING);
2499
Scott Wood3ea94ed2015-06-26 19:03:26 -05002500 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02002501 ops.len = len;
2502 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002503 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01002504
Heiko Schocherf5895d12014-06-24 10:10:04 +02002505 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002506
Heiko Schocherf5895d12014-06-24 10:10:04 +02002507 *retlen = ops.retlen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002508 return ret;
2509}
2510
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002511/**
William Juul52c07962007-10-31 13:53:06 +01002512 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00002513 * @mtd: MTD device structure
2514 * @to: offset to write to
2515 * @len: number of bytes to write
2516 * @retlen: pointer to variable to store the number of written bytes
2517 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002518 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002519 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002520 */
William Juul52c07962007-10-31 13:53:06 +01002521static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2522 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002523{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002524 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01002525 int ret;
2526
Heiko Schocherf5895d12014-06-24 10:10:04 +02002527 nand_get_device(mtd, FL_WRITING);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002528 memset(&ops, 0, sizeof(ops));
Sergey Lapin3a38a552013-01-14 03:46:50 +00002529 ops.len = len;
2530 ops.datbuf = (uint8_t *)buf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002531 ops.mode = MTD_OPS_PLACE_OOB;
2532 ret = nand_do_write_ops(mtd, to, &ops);
2533 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01002534 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002535 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002536}
2537
2538/**
William Juul52c07962007-10-31 13:53:06 +01002539 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002540 * @mtd: MTD device structure
2541 * @to: offset to write to
2542 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002543 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002544 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002545 */
William Juul52c07962007-10-31 13:53:06 +01002546static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2547 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002548{
William Juul52c07962007-10-31 13:53:06 +01002549 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05002550 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002551
Heiko Schocherf5895d12014-06-24 10:10:04 +02002552 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002553 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002554
Scott Wood52ab7ce2016-05-30 13:57:58 -05002555 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002556
2557 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002558 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002559 pr_debug("%s: attempt to write past end of page\n",
2560 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002561 return -EINVAL;
2562 }
2563
William Juul52c07962007-10-31 13:53:06 +01002564 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002565 pr_debug("%s: attempt to start write outside oob\n",
2566 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002567 return -EINVAL;
2568 }
2569
Christian Hitz13fc0e22011-10-12 09:32:01 +02002570 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002571 if (unlikely(to >= mtd->size ||
2572 ops->ooboffs + ops->ooblen >
2573 ((mtd->size >> chip->page_shift) -
2574 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002575 pr_debug("%s: attempt to write beyond end of device\n",
2576 __func__);
William Juul52c07962007-10-31 13:53:06 +01002577 return -EINVAL;
2578 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002579
William Juul52c07962007-10-31 13:53:06 +01002580 chipnr = (int)(to >> chip->chip_shift);
2581 chip->select_chip(mtd, chipnr);
2582
2583 /* Shift to get page */
2584 page = (int)(to >> chip->page_shift);
2585
2586 /*
2587 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2588 * of my DiskOnChip 2000 test units) will clear the whole data page too
2589 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2590 * it in the doc2000 driver in August 1999. dwmw2.
2591 */
2592 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002593
2594 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002595 if (nand_check_wp(mtd)) {
2596 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01002597 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002598 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002599
William Juul52c07962007-10-31 13:53:06 +01002600 /* Invalidate the page cache, if we write to the cached page */
2601 if (page == chip->pagebuf)
2602 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002603
Sergey Lapin3a38a552013-01-14 03:46:50 +00002604 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2605
2606 if (ops->mode == MTD_OPS_RAW)
2607 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2608 else
2609 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002610
Heiko Schocherf5895d12014-06-24 10:10:04 +02002611 chip->select_chip(mtd, -1);
2612
William Juul52c07962007-10-31 13:53:06 +01002613 if (status)
2614 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002615
William Juul52c07962007-10-31 13:53:06 +01002616 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002617
William Juul52c07962007-10-31 13:53:06 +01002618 return 0;
2619}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002620
William Juul52c07962007-10-31 13:53:06 +01002621/**
2622 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002623 * @mtd: MTD device structure
2624 * @to: offset to write to
2625 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01002626 */
2627static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2628 struct mtd_oob_ops *ops)
2629{
William Juul52c07962007-10-31 13:53:06 +01002630 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002631
William Juul52c07962007-10-31 13:53:06 +01002632 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002633
William Juul52c07962007-10-31 13:53:06 +01002634 /* Do not allow writes past end of device */
2635 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002636 pr_debug("%s: attempt to write beyond end of device\n",
2637 __func__);
William Juul52c07962007-10-31 13:53:06 +01002638 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002639 }
William Juul52c07962007-10-31 13:53:06 +01002640
Heiko Schocherf5895d12014-06-24 10:10:04 +02002641 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01002642
Christian Hitz13fc0e22011-10-12 09:32:01 +02002643 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002644 case MTD_OPS_PLACE_OOB:
2645 case MTD_OPS_AUTO_OOB:
2646 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002647 break;
2648
2649 default:
2650 goto out;
2651 }
2652
2653 if (!ops->datbuf)
2654 ret = nand_do_write_oob(mtd, to, ops);
2655 else
2656 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002657
Christian Hitz13fc0e22011-10-12 09:32:01 +02002658out:
William Juul52c07962007-10-31 13:53:06 +01002659 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002660 return ret;
2661}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002662
2663/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002664 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00002665 * @mtd: MTD device structure
2666 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002667 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05002668 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002669 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002670static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002671{
Scott Wood17fed142016-05-30 13:57:56 -05002672 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002673 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002674 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2675 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002676
2677 return chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002678}
2679
2680/**
2681 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002682 * @mtd: MTD device structure
2683 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002684 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002685 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002686 */
William Juul52c07962007-10-31 13:53:06 +01002687static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002688{
William Juul52c07962007-10-31 13:53:06 +01002689 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002690}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002691
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002692/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002693 * nand_erase_nand - [INTERN] erase block(s)
2694 * @mtd: MTD device structure
2695 * @instr: erase instruction
2696 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002697 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002698 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002699 */
William Juul52c07962007-10-31 13:53:06 +01002700int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2701 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002702{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002703 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002704 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002705 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002706
Heiko Schocherf5895d12014-06-24 10:10:04 +02002707 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2708 __func__, (unsigned long long)instr->addr,
2709 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002710
Christian Hitzb8a6b372011-10-12 09:32:02 +02002711 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002712 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002713
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002714 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002715 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002716
2717 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002718 page = (int)(instr->addr >> chip->page_shift);
2719 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002720
2721 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002722 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002723
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002724 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002725 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002726
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002727 /* Check, if it is write protected */
2728 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002729 pr_debug("%s: device is write protected!\n",
2730 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002731 instr->state = MTD_ERASE_FAILED;
2732 goto erase_exit;
2733 }
2734
2735 /* Loop through the pages */
2736 len = instr->len;
2737
2738 instr->state = MTD_ERASING;
2739
2740 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002741 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002742
Sergey Lapin3a38a552013-01-14 03:46:50 +00002743 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09002744 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05002745 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002746 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002747 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002748 instr->state = MTD_ERASE_FAILED;
2749 goto erase_exit;
2750 }
William Juul52c07962007-10-31 13:53:06 +01002751
2752 /*
2753 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00002754 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01002755 */
2756 if (page <= chip->pagebuf && chip->pagebuf <
2757 (page + pages_per_block))
2758 chip->pagebuf = -1;
2759
Scott Wood3ea94ed2015-06-26 19:03:26 -05002760 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002761
William Juul52c07962007-10-31 13:53:06 +01002762 /*
2763 * See if operation failed and additional status checks are
2764 * available
2765 */
2766 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2767 status = chip->errstat(mtd, chip, FL_ERASING,
2768 status, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002769
2770 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002771 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002772 pr_debug("%s: failed erase, page 0x%08x\n",
2773 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002774 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002775 instr->fail_addr =
2776 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002777 goto erase_exit;
2778 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002779
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002780 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002781 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782 page += pages_per_block;
2783
2784 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002785 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002786 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002787 chip->select_chip(mtd, -1);
2788 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002789 }
2790 }
2791 instr->state = MTD_ERASE_DONE;
2792
Christian Hitz13fc0e22011-10-12 09:32:01 +02002793erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002794
2795 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002796
2797 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002798 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002799 nand_release_device(mtd);
2800
Scott Wood3628f002008-10-24 16:20:43 -05002801 /* Do call back function */
2802 if (!ret)
2803 mtd_erase_callback(instr);
2804
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002805 /* Return more or less happy */
2806 return ret;
2807}
2808
2809/**
2810 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00002811 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002812 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002813 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002814 */
William Juul52c07962007-10-31 13:53:06 +01002815static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002816{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002817 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002818
2819 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002820 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002821 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002822 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002823}
2824
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002825/**
William Juul52c07962007-10-31 13:53:06 +01002826 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002827 * @mtd: MTD device structure
2828 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002829 */
William Juul52c07962007-10-31 13:53:06 +01002830static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002831{
Scott Wood52ab7ce2016-05-30 13:57:58 -05002832 struct nand_chip *chip = mtd_to_nand(mtd);
2833 int chipnr = (int)(offs >> chip->chip_shift);
2834 int ret;
2835
2836 /* Select the NAND device */
2837 nand_get_device(mtd, FL_READING);
2838 chip->select_chip(mtd, chipnr);
2839
2840 ret = nand_block_checkbad(mtd, offs, 0);
2841
2842 chip->select_chip(mtd, -1);
2843 nand_release_device(mtd);
2844
2845 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002846}
2847
2848/**
William Juul52c07962007-10-31 13:53:06 +01002849 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002850 * @mtd: MTD device structure
2851 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002852 */
William Juul52c07962007-10-31 13:53:06 +01002853static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002854{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002855 int ret;
2856
Christian Hitzb8a6b372011-10-12 09:32:02 +02002857 ret = nand_block_isbad(mtd, ofs);
2858 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002859 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002860 if (ret > 0)
2861 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002862 return ret;
2863 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002864
Heiko Schocherf5895d12014-06-24 10:10:04 +02002865 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002866}
2867
Heiko Schocherf5895d12014-06-24 10:10:04 +02002868/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002869 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2870 * @mtd: MTD device structure
2871 * @chip: nand chip info structure
2872 * @addr: feature address.
2873 * @subfeature_param: the subfeature parameters, a four bytes array.
2874 */
2875static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2876 int addr, uint8_t *subfeature_param)
2877{
2878 int status;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002879 int i;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002880
Heiko Schocherf5895d12014-06-24 10:10:04 +02002881#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2882 if (!chip->onfi_version ||
2883 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2884 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapin3a38a552013-01-14 03:46:50 +00002885 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002886#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00002887
2888 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002889 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2890 chip->write_byte(mtd, subfeature_param[i]);
2891
Sergey Lapin3a38a552013-01-14 03:46:50 +00002892 status = chip->waitfunc(mtd, chip);
2893 if (status & NAND_STATUS_FAIL)
2894 return -EIO;
2895 return 0;
2896}
2897
2898/**
2899 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2900 * @mtd: MTD device structure
2901 * @chip: nand chip info structure
2902 * @addr: feature address.
2903 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01002904 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002905static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2906 int addr, uint8_t *subfeature_param)
2907{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002908 int i;
2909
2910#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2911 if (!chip->onfi_version ||
2912 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2913 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapin3a38a552013-01-14 03:46:50 +00002914 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002915#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00002916
Sergey Lapin3a38a552013-01-14 03:46:50 +00002917 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002918 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2919 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002920 return 0;
2921}
Heiko Schocherf5895d12014-06-24 10:10:04 +02002922
Sergey Lapin3a38a552013-01-14 03:46:50 +00002923/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01002924static void nand_set_defaults(struct nand_chip *chip, int busw)
2925{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002926 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01002927 if (!chip->chip_delay)
2928 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002929
2930 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01002931 if (chip->cmdfunc == NULL)
2932 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002933
2934 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01002935 if (chip->waitfunc == NULL)
2936 chip->waitfunc = nand_wait;
2937
2938 if (!chip->select_chip)
2939 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002940
2941 /* set for ONFI nand */
2942 if (!chip->onfi_set_features)
2943 chip->onfi_set_features = nand_onfi_set_features;
2944 if (!chip->onfi_get_features)
2945 chip->onfi_get_features = nand_onfi_get_features;
2946
2947 /* If called twice, pointers that depend on busw may need to be reset */
2948 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01002949 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2950 if (!chip->read_word)
2951 chip->read_word = nand_read_word;
2952 if (!chip->block_bad)
2953 chip->block_bad = nand_block_bad;
2954 if (!chip->block_markbad)
2955 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002956 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01002957 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002958 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2959 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2960 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01002961 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01002962 if (!chip->scan_bbt)
2963 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002964
2965 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01002966 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002967 spin_lock_init(&chip->controller->lock);
2968 init_waitqueue_head(&chip->controller->wq);
2969 }
2970
William Juul52c07962007-10-31 13:53:06 +01002971}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002972
Sergey Lapin3a38a552013-01-14 03:46:50 +00002973/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002974static void sanitize_string(char *s, size_t len)
2975{
2976 ssize_t i;
2977
Sergey Lapin3a38a552013-01-14 03:46:50 +00002978 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002979 s[len - 1] = 0;
2980
Sergey Lapin3a38a552013-01-14 03:46:50 +00002981 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002982 for (i = 0; i < len - 1; i++) {
2983 if (s[i] < ' ' || s[i] > 127)
2984 s[i] = '?';
2985 }
2986
Sergey Lapin3a38a552013-01-14 03:46:50 +00002987 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02002988 strim(s);
2989}
2990
Florian Fainellic98a9352011-02-25 00:01:34 +00002991static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2992{
2993 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00002994 while (len--) {
2995 crc ^= *p++ << 8;
2996 for (i = 0; i < 8; i++)
2997 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2998 }
2999
3000 return crc;
3001}
3002
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003003#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003004/* Parse the Extended Parameter Page. */
3005static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3006 struct nand_chip *chip, struct nand_onfi_params *p)
3007{
3008 struct onfi_ext_param_page *ep;
3009 struct onfi_ext_section *s;
3010 struct onfi_ext_ecc_info *ecc;
3011 uint8_t *cursor;
3012 int ret = -EINVAL;
3013 int len;
3014 int i;
3015
3016 len = le16_to_cpu(p->ext_param_page_length) * 16;
3017 ep = kmalloc(len, GFP_KERNEL);
3018 if (!ep)
3019 return -ENOMEM;
3020
3021 /* Send our own NAND_CMD_PARAM. */
3022 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3023
3024 /* Use the Change Read Column command to skip the ONFI param pages. */
3025 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3026 sizeof(*p) * p->num_of_param_pages , -1);
3027
3028 /* Read out the Extended Parameter Page. */
3029 chip->read_buf(mtd, (uint8_t *)ep, len);
3030 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3031 != le16_to_cpu(ep->crc))) {
3032 pr_debug("fail in the CRC.\n");
3033 goto ext_out;
3034 }
3035
3036 /*
3037 * Check the signature.
3038 * Do not strictly follow the ONFI spec, maybe changed in future.
3039 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003040 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003041 pr_debug("The signature is invalid.\n");
3042 goto ext_out;
3043 }
3044
3045 /* find the ECC section. */
3046 cursor = (uint8_t *)(ep + 1);
3047 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3048 s = ep->sections + i;
3049 if (s->type == ONFI_SECTION_TYPE_2)
3050 break;
3051 cursor += s->length * 16;
3052 }
3053 if (i == ONFI_EXT_SECTION_MAX) {
3054 pr_debug("We can not find the ECC section.\n");
3055 goto ext_out;
3056 }
3057
3058 /* get the info we want. */
3059 ecc = (struct onfi_ext_ecc_info *)cursor;
3060
3061 if (!ecc->codeword_size) {
3062 pr_debug("Invalid codeword size\n");
3063 goto ext_out;
3064 }
3065
3066 chip->ecc_strength_ds = ecc->ecc_bits;
3067 chip->ecc_step_ds = 1 << ecc->codeword_size;
3068 ret = 0;
3069
3070ext_out:
3071 kfree(ep);
3072 return ret;
3073}
3074
3075static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3076{
Scott Wood17fed142016-05-30 13:57:56 -05003077 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003078 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3079
3080 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3081 feature);
3082}
3083
3084/*
3085 * Configure chip properties from Micron vendor-specific ONFI table
3086 */
3087static void nand_onfi_detect_micron(struct nand_chip *chip,
3088 struct nand_onfi_params *p)
3089{
3090 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3091
3092 if (le16_to_cpu(p->vendor_revision) < 1)
3093 return;
3094
3095 chip->read_retries = micron->read_retry_options;
3096 chip->setup_read_retry = nand_setup_read_retry_micron;
3097}
3098
Florian Fainellic98a9352011-02-25 00:01:34 +00003099/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003100 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003101 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003102static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003103 int *busw)
3104{
3105 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisf3832302014-05-06 00:46:16 +05303106 int i, j;
Florian Fainellic98a9352011-02-25 00:01:34 +00003107 int val;
3108
Sergey Lapin3a38a552013-01-14 03:46:50 +00003109 /* Try ONFI for unknown chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00003110 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3111 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3112 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3113 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003114
Florian Fainellic98a9352011-02-25 00:01:34 +00003115 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3116 for (i = 0; i < 3; i++) {
Brian Norrisf3832302014-05-06 00:46:16 +05303117 for (j = 0; j < sizeof(*p); j++)
3118 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003119 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003120 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003121 break;
3122 }
3123 }
3124
Heiko Schocherf5895d12014-06-24 10:10:04 +02003125 if (i == 3) {
3126 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003127 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003128 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003129
Sergey Lapin3a38a552013-01-14 03:46:50 +00003130 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003131 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003132 if (val & (1 << 5))
3133 chip->onfi_version = 23;
3134 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003135 chip->onfi_version = 22;
3136 else if (val & (1 << 3))
3137 chip->onfi_version = 21;
3138 else if (val & (1 << 2))
3139 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003140 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003141 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003142
3143 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003144 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003145 return 0;
3146 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003147
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003148 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3149 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003150 if (!mtd->name)
3151 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003152
Florian Fainellic98a9352011-02-25 00:01:34 +00003153 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003154
3155 /*
3156 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3157 * (don't ask me who thought of this...). MTD assumes that these
3158 * dimensions will be power-of-2, so just truncate the remaining area.
3159 */
3160 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3161 mtd->erasesize *= mtd->writesize;
3162
Florian Fainellic98a9352011-02-25 00:01:34 +00003163 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003164
3165 /* See erasesize comment */
3166 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003167 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003168 chip->bits_per_cell = p->bits_per_cell;
3169
3170 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003171 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003172 else
3173 *busw = 0;
3174
3175 if (p->ecc_bits != 0xff) {
3176 chip->ecc_strength_ds = p->ecc_bits;
3177 chip->ecc_step_ds = 512;
3178 } else if (chip->onfi_version >= 21 &&
3179 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3180
3181 /*
3182 * The nand_flash_detect_ext_param_page() uses the
3183 * Change Read Column command which maybe not supported
3184 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3185 * now. We do not replace user supplied command function.
3186 */
3187 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3188 chip->cmdfunc = nand_command_lp;
3189
3190 /* The Extended Parameter Page is supported since ONFI 2.1. */
3191 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3192 pr_warn("Failed to detect ONFI extended param page\n");
3193 } else {
3194 pr_warn("Could not retrieve ONFI ECC requirements\n");
3195 }
3196
3197 if (p->jedec_id == NAND_MFR_MICRON)
3198 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003199
3200 return 1;
3201}
3202#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003203static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003204 int *busw)
3205{
3206 return 0;
3207}
3208#endif
3209
William Juul52c07962007-10-31 13:53:06 +01003210/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003211 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3212 */
3213static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3214 int *busw)
3215{
3216 struct nand_jedec_params *p = &chip->jedec_params;
3217 struct jedec_ecc_info *ecc;
3218 int val;
3219 int i, j;
3220
3221 /* Try JEDEC for unknown chip or LP */
3222 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3223 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3224 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3225 chip->read_byte(mtd) != 'C')
3226 return 0;
3227
3228 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3229 for (i = 0; i < 3; i++) {
3230 for (j = 0; j < sizeof(*p); j++)
3231 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3232
3233 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3234 le16_to_cpu(p->crc))
3235 break;
3236 }
3237
3238 if (i == 3) {
3239 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3240 return 0;
3241 }
3242
3243 /* Check version */
3244 val = le16_to_cpu(p->revision);
3245 if (val & (1 << 2))
3246 chip->jedec_version = 10;
3247 else if (val & (1 << 1))
3248 chip->jedec_version = 1; /* vendor specific version */
3249
3250 if (!chip->jedec_version) {
3251 pr_info("unsupported JEDEC version: %d\n", val);
3252 return 0;
3253 }
3254
3255 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3256 sanitize_string(p->model, sizeof(p->model));
3257 if (!mtd->name)
3258 mtd->name = p->model;
3259
3260 mtd->writesize = le32_to_cpu(p->byte_per_page);
3261
3262 /* Please reference to the comment for nand_flash_detect_onfi. */
3263 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3264 mtd->erasesize *= mtd->writesize;
3265
3266 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3267
3268 /* Please reference to the comment for nand_flash_detect_onfi. */
3269 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3270 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3271 chip->bits_per_cell = p->bits_per_cell;
3272
3273 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3274 *busw = NAND_BUSWIDTH_16;
3275 else
3276 *busw = 0;
3277
3278 /* ECC info */
3279 ecc = &p->ecc_info[0];
3280
3281 if (ecc->codeword_size >= 9) {
3282 chip->ecc_strength_ds = ecc->ecc_bits;
3283 chip->ecc_step_ds = 1 << ecc->codeword_size;
3284 } else {
3285 pr_warn("Invalid codeword size\n");
3286 }
3287
3288 return 1;
3289}
3290
3291/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003292 * nand_id_has_period - Check if an ID string has a given wraparound period
3293 * @id_data: the ID string
3294 * @arrlen: the length of the @id_data array
3295 * @period: the period of repitition
3296 *
3297 * Check if an ID string is repeated within a given sequence of bytes at
3298 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02003299 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00003300 * if the repetition has a period of @period; otherwise, returns zero.
3301 */
3302static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3303{
3304 int i, j;
3305 for (i = 0; i < period; i++)
3306 for (j = i + period; j < arrlen; j += period)
3307 if (id_data[i] != id_data[j])
3308 return 0;
3309 return 1;
3310}
3311
3312/*
3313 * nand_id_len - Get the length of an ID string returned by CMD_READID
3314 * @id_data: the ID string
3315 * @arrlen: the length of the @id_data array
3316
3317 * Returns the length of the ID string, according to known wraparound/trailing
3318 * zero patterns. If no pattern exists, returns the length of the array.
3319 */
3320static int nand_id_len(u8 *id_data, int arrlen)
3321{
3322 int last_nonzero, period;
3323
3324 /* Find last non-zero byte */
3325 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3326 if (id_data[last_nonzero])
3327 break;
3328
3329 /* All zeros */
3330 if (last_nonzero < 0)
3331 return 0;
3332
3333 /* Calculate wraparound period */
3334 for (period = 1; period < arrlen; period++)
3335 if (nand_id_has_period(id_data, arrlen, period))
3336 break;
3337
3338 /* There's a repeated pattern */
3339 if (period < arrlen)
3340 return period;
3341
3342 /* There are trailing zeros */
3343 if (last_nonzero < arrlen - 1)
3344 return last_nonzero + 1;
3345
3346 /* No pattern detected */
3347 return arrlen;
3348}
3349
Heiko Schocherf5895d12014-06-24 10:10:04 +02003350/* Extract the bits of per cell from the 3rd byte of the extended ID */
3351static int nand_get_bits_per_cell(u8 cellinfo)
3352{
3353 int bits;
3354
3355 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3356 bits >>= NAND_CI_CELLTYPE_SHIFT;
3357 return bits + 1;
3358}
3359
Sergey Lapin3a38a552013-01-14 03:46:50 +00003360/*
3361 * Many new NAND share similar device ID codes, which represent the size of the
3362 * chip. The rest of the parameters must be decoded according to generic or
3363 * manufacturer-specific "extended ID" decoding patterns.
3364 */
3365static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3366 u8 id_data[8], int *busw)
3367{
3368 int extid, id_len;
3369 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003370 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003371 /* The 4th id byte is the important one */
3372 extid = id_data[3];
3373
3374 id_len = nand_id_len(id_data, 8);
3375
3376 /*
3377 * Field definitions are in the following datasheets:
3378 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3379 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3380 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3381 *
3382 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3383 * ID to decide what to do.
3384 */
3385 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003386 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003387 /* Calc pagesize */
3388 mtd->writesize = 2048 << (extid & 0x03);
3389 extid >>= 2;
3390 /* Calc oobsize */
3391 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3392 case 1:
3393 mtd->oobsize = 128;
3394 break;
3395 case 2:
3396 mtd->oobsize = 218;
3397 break;
3398 case 3:
3399 mtd->oobsize = 400;
3400 break;
3401 case 4:
3402 mtd->oobsize = 436;
3403 break;
3404 case 5:
3405 mtd->oobsize = 512;
3406 break;
3407 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003408 mtd->oobsize = 640;
3409 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003410 case 7:
3411 default: /* Other cases are "reserved" (unknown) */
3412 mtd->oobsize = 1024;
3413 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003414 }
3415 extid >>= 2;
3416 /* Calc blocksize */
3417 mtd->erasesize = (128 * 1024) <<
3418 (((extid >> 1) & 0x04) | (extid & 0x03));
3419 *busw = 0;
3420 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003421 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003422 unsigned int tmp;
3423
3424 /* Calc pagesize */
3425 mtd->writesize = 2048 << (extid & 0x03);
3426 extid >>= 2;
3427 /* Calc oobsize */
3428 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3429 case 0:
3430 mtd->oobsize = 128;
3431 break;
3432 case 1:
3433 mtd->oobsize = 224;
3434 break;
3435 case 2:
3436 mtd->oobsize = 448;
3437 break;
3438 case 3:
3439 mtd->oobsize = 64;
3440 break;
3441 case 4:
3442 mtd->oobsize = 32;
3443 break;
3444 case 5:
3445 mtd->oobsize = 16;
3446 break;
3447 default:
3448 mtd->oobsize = 640;
3449 break;
3450 }
3451 extid >>= 2;
3452 /* Calc blocksize */
3453 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3454 if (tmp < 0x03)
3455 mtd->erasesize = (128 * 1024) << tmp;
3456 else if (tmp == 0x03)
3457 mtd->erasesize = 768 * 1024;
3458 else
3459 mtd->erasesize = (64 * 1024) << tmp;
3460 *busw = 0;
3461 } else {
3462 /* Calc pagesize */
3463 mtd->writesize = 1024 << (extid & 0x03);
3464 extid >>= 2;
3465 /* Calc oobsize */
3466 mtd->oobsize = (8 << (extid & 0x01)) *
3467 (mtd->writesize >> 9);
3468 extid >>= 2;
3469 /* Calc blocksize. Blocksize is multiples of 64KiB */
3470 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3471 extid >>= 2;
3472 /* Get buswidth information */
3473 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003474
3475 /*
3476 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3477 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3478 * follows:
3479 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3480 * 110b -> 24nm
3481 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3482 */
3483 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3484 nand_is_slc(chip) &&
3485 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3486 !(id_data[4] & 0x80) /* !BENAND */) {
3487 mtd->oobsize = 32 * mtd->writesize >> 9;
3488 }
3489
Sergey Lapin3a38a552013-01-14 03:46:50 +00003490 }
3491}
3492
Heiko Schocherf5895d12014-06-24 10:10:04 +02003493/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003494 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3495 * decodes a matching ID table entry and assigns the MTD size parameters for
3496 * the chip.
3497 */
3498static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003499 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00003500 int *busw)
3501{
3502 int maf_id = id_data[0];
3503
3504 mtd->erasesize = type->erasesize;
3505 mtd->writesize = type->pagesize;
3506 mtd->oobsize = mtd->writesize / 32;
3507 *busw = type->options & NAND_BUSWIDTH_16;
3508
Heiko Schocherf5895d12014-06-24 10:10:04 +02003509 /* All legacy ID NAND are small-page, SLC */
3510 chip->bits_per_cell = 1;
3511
Sergey Lapin3a38a552013-01-14 03:46:50 +00003512 /*
3513 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3514 * some Spansion chips have erasesize that conflicts with size
3515 * listed in nand_ids table.
3516 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3517 */
3518 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3519 && id_data[6] == 0x00 && id_data[7] == 0x00
3520 && mtd->writesize == 512) {
3521 mtd->erasesize = 128 * 1024;
3522 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3523 }
3524}
3525
Heiko Schocherf5895d12014-06-24 10:10:04 +02003526/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003527 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3528 * heuristic patterns using various detected parameters (e.g., manufacturer,
3529 * page size, cell-type information).
3530 */
3531static void nand_decode_bbm_options(struct mtd_info *mtd,
3532 struct nand_chip *chip, u8 id_data[8])
3533{
3534 int maf_id = id_data[0];
3535
3536 /* Set the bad block position */
3537 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3538 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3539 else
3540 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3541
3542 /*
3543 * Bad block marker is stored in the last page of each block on Samsung
3544 * and Hynix MLC devices; stored in first two pages of each block on
3545 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3546 * AMD/Spansion, and Macronix. All others scan only the first page.
3547 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003548 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003549 (maf_id == NAND_MFR_SAMSUNG ||
3550 maf_id == NAND_MFR_HYNIX))
3551 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003552 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003553 (maf_id == NAND_MFR_SAMSUNG ||
3554 maf_id == NAND_MFR_HYNIX ||
3555 maf_id == NAND_MFR_TOSHIBA ||
3556 maf_id == NAND_MFR_AMD ||
3557 maf_id == NAND_MFR_MACRONIX)) ||
3558 (mtd->writesize == 2048 &&
3559 maf_id == NAND_MFR_MICRON))
3560 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3561}
3562
Heiko Schocherf5895d12014-06-24 10:10:04 +02003563static inline bool is_full_id_nand(struct nand_flash_dev *type)
3564{
3565 return type->id_len;
3566}
3567
3568static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3569 struct nand_flash_dev *type, u8 *id_data, int *busw)
3570{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003571 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003572 mtd->writesize = type->pagesize;
3573 mtd->erasesize = type->erasesize;
3574 mtd->oobsize = type->oobsize;
3575
3576 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3577 chip->chipsize = (uint64_t)type->chipsize << 20;
3578 chip->options |= type->options;
3579 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3580 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003581 chip->onfi_timing_mode_default =
3582 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003583
3584 *busw = type->options & NAND_BUSWIDTH_16;
3585
3586 if (!mtd->name)
3587 mtd->name = type->name;
3588
3589 return true;
3590 }
3591 return false;
3592}
3593
Sergey Lapin3a38a552013-01-14 03:46:50 +00003594/*
3595 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01003596 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003597static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01003598 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003599 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003600 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01003601{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003602 int busw;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003603 int i, maf_idx;
3604 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003605
3606 /* Select the device */
William Juul52c07962007-10-31 13:53:06 +01003607 chip->select_chip(mtd, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003608
Karl Beldanb6322fc2008-09-15 16:08:03 +02003609 /*
3610 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003611 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02003612 */
3613 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3614
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003615 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01003616 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003617
3618 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003619 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003620 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003621
Sergey Lapin3a38a552013-01-14 03:46:50 +00003622 /*
3623 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05003624 * interface concerns can cause random data which looks like a
3625 * possibly credible NAND flash to appear. If the two results do
3626 * not match, ignore the device completely.
3627 */
3628
3629 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3630
Sergey Lapin3a38a552013-01-14 03:46:50 +00003631 /* Read entire ID string */
3632 for (i = 0; i < 8; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02003633 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05003634
Christian Hitzb8a6b372011-10-12 09:32:02 +02003635 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003636 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003637 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05003638 return ERR_PTR(-ENODEV);
3639 }
3640
Lei Wen75bde942011-01-06 09:48:18 +08003641 if (!type)
3642 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003643
Heiko Schocherf5895d12014-06-24 10:10:04 +02003644 for (; type->name != NULL; type++) {
3645 if (is_full_id_nand(type)) {
3646 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3647 goto ident_done;
3648 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05003649 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003650 }
3651 }
Lei Wen75bde942011-01-06 09:48:18 +08003652
Christian Hitzb8a6b372011-10-12 09:32:02 +02003653 chip->onfi_version = 0;
3654 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05003655 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003656 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02003657 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003658
3659 /* Check if the chip is JEDEC compliant */
3660 if (nand_flash_detect_jedec(mtd, chip, &busw))
3661 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02003662 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003663
Christian Hitzb8a6b372011-10-12 09:32:02 +02003664 if (!type->name)
3665 return ERR_PTR(-ENODEV);
3666
William Juul52c07962007-10-31 13:53:06 +01003667 if (!mtd->name)
3668 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003669
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003670 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003671
Scott Wood52ab7ce2016-05-30 13:57:58 -05003672 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003673 /* Decode parameters from extended ID */
3674 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003675 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003676 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003677 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003678 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00003679 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00003680
Sergey Lapin3a38a552013-01-14 03:46:50 +00003681 /*
3682 * Check if chip is not a Samsung device. Do not clear the
3683 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02003684 */
3685 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3686 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3687ident_done:
3688
William Juul52c07962007-10-31 13:53:06 +01003689 /* Try to identify manufacturer */
3690 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3691 if (nand_manuf_ids[maf_idx].id == *maf_id)
3692 break;
3693 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003694
Heiko Schocherf5895d12014-06-24 10:10:04 +02003695 if (chip->options & NAND_BUSWIDTH_AUTO) {
3696 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3697 chip->options |= busw;
3698 nand_set_defaults(chip, busw);
3699 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3700 /*
3701 * Check, if buswidth is correct. Hardware drivers should set
3702 * chip correct!
3703 */
3704 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3705 *maf_id, *dev_id);
3706 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3707 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003708 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3709 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01003710 return ERR_PTR(-EINVAL);
3711 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003712
Sergey Lapin3a38a552013-01-14 03:46:50 +00003713 nand_decode_bbm_options(mtd, chip, id_data);
3714
William Juul52c07962007-10-31 13:53:06 +01003715 /* Calculate the address shift from the page size */
3716 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003717 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01003718 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003719
William Juul52c07962007-10-31 13:53:06 +01003720 chip->bbt_erase_shift = chip->phys_erase_shift =
3721 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003722 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05003723 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003724 else {
3725 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3726 chip->chip_shift += 32 - 1;
3727 }
3728
3729 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003730 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003731
Sergey Lapin3a38a552013-01-14 03:46:50 +00003732 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01003733 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3734 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003735
Heiko Schocherf5895d12014-06-24 10:10:04 +02003736 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3737 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003738
Christian Hitzb8a6b372011-10-12 09:32:02 +02003739#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003740 if (chip->onfi_version)
3741 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3742 chip->onfi_params.model);
3743 else if (chip->jedec_version)
3744 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3745 chip->jedec_params.model);
3746 else
3747 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3748 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003749#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003750 if (chip->jedec_version)
3751 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3752 chip->jedec_params.model);
3753 else
3754 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3755 type->name);
3756
3757 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3758 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003759#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003760
Scott Wood3ea94ed2015-06-26 19:03:26 -05003761 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003762 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05003763 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01003764 return type;
3765}
3766
Brian Norrisba6463d2016-06-15 21:09:22 +02003767#if CONFIG_IS_ENABLED(OF_CONTROL)
3768DECLARE_GLOBAL_DATA_PTR;
3769
3770static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3771{
3772 int ret, ecc_mode = -1, ecc_strength, ecc_step;
3773 const void *blob = gd->fdt_blob;
3774 const char *str;
3775
3776 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3777 if (ret == 16)
3778 chip->options |= NAND_BUSWIDTH_16;
3779
3780 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3781 chip->bbt_options |= NAND_BBT_USE_FLASH;
3782
3783 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3784 if (str) {
3785 if (!strcmp(str, "none"))
3786 ecc_mode = NAND_ECC_NONE;
3787 else if (!strcmp(str, "soft"))
3788 ecc_mode = NAND_ECC_SOFT;
3789 else if (!strcmp(str, "hw"))
3790 ecc_mode = NAND_ECC_HW;
3791 else if (!strcmp(str, "hw_syndrome"))
3792 ecc_mode = NAND_ECC_HW_SYNDROME;
3793 else if (!strcmp(str, "hw_oob_first"))
3794 ecc_mode = NAND_ECC_HW_OOB_FIRST;
3795 else if (!strcmp(str, "soft_bch"))
3796 ecc_mode = NAND_ECC_SOFT_BCH;
3797 }
3798
3799
3800 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3801 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3802
3803 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3804 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3805 pr_err("must set both strength and step size in DT\n");
3806 return -EINVAL;
3807 }
3808
3809 if (ecc_mode >= 0)
3810 chip->ecc.mode = ecc_mode;
3811
3812 if (ecc_strength >= 0)
3813 chip->ecc.strength = ecc_strength;
3814
3815 if (ecc_step > 0)
3816 chip->ecc.size = ecc_step;
3817
3818 return 0;
3819}
3820#else
3821static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3822{
3823 return 0;
3824}
3825#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3826
William Juul52c07962007-10-31 13:53:06 +01003827/**
3828 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003829 * @mtd: MTD device structure
3830 * @maxchips: number of chips to scan for
3831 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01003832 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003833 * This is the first phase of the normal nand_scan() function. It reads the
3834 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01003835 *
William Juul52c07962007-10-31 13:53:06 +01003836 */
Lei Wen75bde942011-01-06 09:48:18 +08003837int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003838 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01003839{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003840 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05003841 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003842 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02003843 int ret;
3844
3845 if (chip->flash_node) {
3846 ret = nand_dt_init(mtd, chip, chip->flash_node);
3847 if (ret)
3848 return ret;
3849 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003850
William Juul52c07962007-10-31 13:53:06 +01003851 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003852 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01003853
3854 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003855 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3856 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01003857
3858 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003859 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3860 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01003861 chip->select_chip(mtd, -1);
3862 return PTR_ERR(type);
3863 }
3864
Heiko Schocherf5895d12014-06-24 10:10:04 +02003865 chip->select_chip(mtd, -1);
3866
William Juul52c07962007-10-31 13:53:06 +01003867 /* Check for a chip array */
3868 for (i = 1; i < maxchips; i++) {
3869 chip->select_chip(mtd, i);
Karl Beldanb6322fc2008-09-15 16:08:03 +02003870 /* See comment in nand_get_flash_type for reset */
3871 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01003872 /* Send the command for reading device ID */
3873 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003874 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003875 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherf5895d12014-06-24 10:10:04 +02003876 nand_dev_id != chip->read_byte(mtd)) {
3877 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003878 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003879 }
3880 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003881 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003882
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01003883#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003884 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02003885 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01003886#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003887
William Juul52c07962007-10-31 13:53:06 +01003888 /* Store the number of chips and calc total size for mtd */
3889 chip->numchips = i;
3890 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003891
William Juul52c07962007-10-31 13:53:06 +01003892 return 0;
3893}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003894EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003895
Scott Wood3ea94ed2015-06-26 19:03:26 -05003896/*
3897 * Check if the chip configuration meet the datasheet requirements.
3898
3899 * If our configuration corrects A bits per B bytes and the minimum
3900 * required correction level is X bits per Y bytes, then we must ensure
3901 * both of the following are true:
3902 *
3903 * (1) A / B >= X / Y
3904 * (2) A >= X
3905 *
3906 * Requirement (1) ensures we can correct for the required bitflip density.
3907 * Requirement (2) ensures we can correct even when all bitflips are clumped
3908 * in the same sector.
3909 */
3910static bool nand_ecc_strength_good(struct mtd_info *mtd)
3911{
Scott Wood17fed142016-05-30 13:57:56 -05003912 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003913 struct nand_ecc_ctrl *ecc = &chip->ecc;
3914 int corr, ds_corr;
3915
3916 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3917 /* Not enough information */
3918 return true;
3919
3920 /*
3921 * We get the number of corrected bits per page to compare
3922 * the correction density.
3923 */
3924 corr = (mtd->writesize * ecc->strength) / ecc->size;
3925 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3926
3927 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3928}
William Juul52c07962007-10-31 13:53:06 +01003929
3930/**
3931 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003932 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01003933 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003934 * This is the second phase of the normal nand_scan() function. It fills out
3935 * all the uninitialized function pointers with the defaults and scans for a
3936 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01003937 */
3938int nand_scan_tail(struct mtd_info *mtd)
3939{
3940 int i;
Scott Wood17fed142016-05-30 13:57:56 -05003941 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003942 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003943 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003944
Sergey Lapin3a38a552013-01-14 03:46:50 +00003945 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3946 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3947 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3948
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003949 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003950 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003951 chip->buffers = nbuf;
3952 } else {
3953 if (!chip->buffers)
3954 return -ENOMEM;
3955 }
William Juul52c07962007-10-31 13:53:06 +01003956
3957 /* Set the internal oob buffer location, just after the page data */
3958 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3959
3960 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003961 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01003962 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003963 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003964 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003965 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02003966 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003967 break;
3968 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02003969 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003970 break;
3971 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02003972 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003973 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003974 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02003975 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02003976 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003977 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003978 pr_warn("No oob scheme defined for oobsize %d\n",
3979 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003980 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003981 }
3982 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003983
William Juul52c07962007-10-31 13:53:06 +01003984 if (!chip->write_page)
3985 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003986
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003987 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003988 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01003989 * selected and we have 256 byte pagesize fallback to software ECC
3990 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003991
Heiko Schocherf5895d12014-06-24 10:10:04 +02003992 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04003993 case NAND_ECC_HW_OOB_FIRST:
3994 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003995 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05003996 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04003997 BUG();
3998 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003999 if (!ecc->read_page)
4000 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004001
William Juul52c07962007-10-31 13:53:06 +01004002 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004003 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004004 if (!ecc->read_page)
4005 ecc->read_page = nand_read_page_hwecc;
4006 if (!ecc->write_page)
4007 ecc->write_page = nand_write_page_hwecc;
4008 if (!ecc->read_page_raw)
4009 ecc->read_page_raw = nand_read_page_raw;
4010 if (!ecc->write_page_raw)
4011 ecc->write_page_raw = nand_write_page_raw;
4012 if (!ecc->read_oob)
4013 ecc->read_oob = nand_read_oob_std;
4014 if (!ecc->write_oob)
4015 ecc->write_oob = nand_write_oob_std;
4016 if (!ecc->read_subpage)
4017 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05004018 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004019 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004020
William Juul52c07962007-10-31 13:53:06 +01004021 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004022 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4023 (!ecc->read_page ||
4024 ecc->read_page == nand_read_page_hwecc ||
4025 !ecc->write_page ||
4026 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004027 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01004028 BUG();
4029 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00004030 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004031 if (!ecc->read_page)
4032 ecc->read_page = nand_read_page_syndrome;
4033 if (!ecc->write_page)
4034 ecc->write_page = nand_write_page_syndrome;
4035 if (!ecc->read_page_raw)
4036 ecc->read_page_raw = nand_read_page_raw_syndrome;
4037 if (!ecc->write_page_raw)
4038 ecc->write_page_raw = nand_write_page_raw_syndrome;
4039 if (!ecc->read_oob)
4040 ecc->read_oob = nand_read_oob_syndrome;
4041 if (!ecc->write_oob)
4042 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004043
Heiko Schocherf5895d12014-06-24 10:10:04 +02004044 if (mtd->writesize >= ecc->size) {
4045 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004046 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4047 BUG();
4048 }
William Juul52c07962007-10-31 13:53:06 +01004049 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004050 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004051 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4052 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004053 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004054
William Juul52c07962007-10-31 13:53:06 +01004055 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004056 ecc->calculate = nand_calculate_ecc;
4057 ecc->correct = nand_correct_data;
4058 ecc->read_page = nand_read_page_swecc;
4059 ecc->read_subpage = nand_read_subpage;
4060 ecc->write_page = nand_write_page_swecc;
4061 ecc->read_page_raw = nand_read_page_raw;
4062 ecc->write_page_raw = nand_write_page_raw;
4063 ecc->read_oob = nand_read_oob_std;
4064 ecc->write_oob = nand_write_oob_std;
4065 if (!ecc->size)
4066 ecc->size = 256;
4067 ecc->bytes = 3;
4068 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004069 break;
4070
Christian Hitz55f7bca2011-10-12 09:31:59 +02004071 case NAND_ECC_SOFT_BCH:
4072 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004073 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004074 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02004075 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004076 ecc->calculate = nand_bch_calculate_ecc;
4077 ecc->correct = nand_bch_correct_data;
4078 ecc->read_page = nand_read_page_swecc;
4079 ecc->read_subpage = nand_read_subpage;
4080 ecc->write_page = nand_write_page_swecc;
4081 ecc->read_page_raw = nand_read_page_raw;
4082 ecc->write_page_raw = nand_write_page_raw;
4083 ecc->read_oob = nand_read_oob_std;
4084 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004085 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05004086 * Board driver should supply ecc.size and ecc.strength values
4087 * to select how many bits are correctable. Otherwise, default
4088 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02004089 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004090 if (!ecc->size && (mtd->oobsize >= 64)) {
4091 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004092 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004093 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004094
4095 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004096 ecc->bytes = 0;
4097 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004098 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004099 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004100 BUG();
4101 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02004102 break;
4103
William Juul52c07962007-10-31 13:53:06 +01004104 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05004105 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004106 ecc->read_page = nand_read_page_raw;
4107 ecc->write_page = nand_write_page_raw;
4108 ecc->read_oob = nand_read_oob_std;
4109 ecc->read_page_raw = nand_read_page_raw;
4110 ecc->write_page_raw = nand_write_page_raw;
4111 ecc->write_oob = nand_write_oob_std;
4112 ecc->size = mtd->writesize;
4113 ecc->bytes = 0;
4114 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004115 break;
4116
4117 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004118 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01004119 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004120 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004121
Sergey Lapin3a38a552013-01-14 03:46:50 +00004122 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004123 if (!ecc->read_oob_raw)
4124 ecc->read_oob_raw = ecc->read_oob;
4125 if (!ecc->write_oob_raw)
4126 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004127
William Juul52c07962007-10-31 13:53:06 +01004128 /*
4129 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00004130 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01004131 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004132 mtd->oobavail = 0;
4133 if (ecc->layout) {
4134 for (i = 0; ecc->layout->oobfree[i].length; i++)
4135 mtd->oobavail += ecc->layout->oobfree[i].length;
4136 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004137
Scott Wood3ea94ed2015-06-26 19:03:26 -05004138 /* ECC sanity check: warn if it's too weak */
4139 if (!nand_ecc_strength_good(mtd))
4140 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4141 mtd->name);
4142
William Juul52c07962007-10-31 13:53:06 +01004143 /*
4144 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00004145 * mode.
William Juul52c07962007-10-31 13:53:06 +01004146 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004147 ecc->steps = mtd->writesize / ecc->size;
4148 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004149 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01004150 BUG();
4151 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004152 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004153
Sergey Lapin3a38a552013-01-14 03:46:50 +00004154 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004155 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4156 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01004157 case 2:
4158 mtd->subpage_sft = 1;
4159 break;
4160 case 4:
4161 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05004162 case 16:
William Juul52c07962007-10-31 13:53:06 +01004163 mtd->subpage_sft = 2;
4164 break;
4165 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004166 }
William Juul52c07962007-10-31 13:53:06 +01004167 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004168
William Juul52c07962007-10-31 13:53:06 +01004169 /* Initialize state */
4170 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004171
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004172 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01004173 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004174
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004175 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05004176 switch (ecc->mode) {
4177 case NAND_ECC_SOFT:
4178 case NAND_ECC_SOFT_BCH:
4179 if (chip->page_shift > 9)
4180 chip->options |= NAND_SUBPAGE_READ;
4181 break;
4182
4183 default:
4184 break;
4185 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004186
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004187 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004188 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004189 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4190 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004191 mtd->_erase = nand_erase;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004192 mtd->_read = nand_read;
4193 mtd->_write = nand_write;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004194 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004195 mtd->_read_oob = nand_read_oob;
4196 mtd->_write_oob = nand_write_oob;
4197 mtd->_sync = nand_sync;
4198 mtd->_lock = NULL;
4199 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03004200 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004201 mtd->_block_isbad = nand_block_isbad;
4202 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004203 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004204
Sergey Lapin3a38a552013-01-14 03:46:50 +00004205 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004206 mtd->ecclayout = ecc->layout;
4207 mtd->ecc_strength = ecc->strength;
4208 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004209 /*
4210 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4211 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4212 * properly set.
4213 */
4214 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05004215 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01004216
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02004217 return 0;
William Juul52c07962007-10-31 13:53:06 +01004218}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004219EXPORT_SYMBOL(nand_scan_tail);
4220
William Juul52c07962007-10-31 13:53:06 +01004221/**
4222 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004223 * @mtd: MTD device structure
4224 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01004225 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004226 * This fills out all the uninitialized function pointers with the defaults.
4227 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05004228 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01004229 */
4230int nand_scan(struct mtd_info *mtd, int maxchips)
4231{
4232 int ret;
4233
Lei Wen75bde942011-01-06 09:48:18 +08004234 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01004235 if (!ret)
4236 ret = nand_scan_tail(mtd);
4237 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004238}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004239EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004240
Heiko Schocherf5895d12014-06-24 10:10:04 +02004241MODULE_LICENSE("GPL");
4242MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4243MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4244MODULE_DESCRIPTION("Generic NAND flash driver code");