blob: 92daebe120bffb496e0d6614a5921c8d3a75f358 [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>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090040#include <linux/mtd/rawnand.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020041#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);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900637 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100638 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
639 }
640 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200641
642 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000643 * Program and erase have their own busy handlers status and sequential
644 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100645 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200646 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200647
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 case NAND_CMD_PAGEPROG:
649 case NAND_CMD_ERASE1:
650 case NAND_CMD_ERASE2:
651 case NAND_CMD_SEQIN:
652 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900653 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900654 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200655 return;
656
657 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100658 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200659 break;
William Juul52c07962007-10-31 13:53:06 +0100660 udelay(chip->chip_delay);
661 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
662 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
663 chip->cmd_ctrl(mtd,
664 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500665 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
666 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200667 return;
668
William Juul52c07962007-10-31 13:53:06 +0100669 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200670 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200671 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200672 * If we don't have access to the busy pin, we apply the given
673 * command delay
William Juul52c07962007-10-31 13:53:06 +0100674 */
675 if (!chip->dev_ready) {
676 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200677 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200678 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200679 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000680 /*
681 * Apply this short delay always to ensure that we do wait tWB in
682 * any case on any machine.
683 */
William Juul52c07962007-10-31 13:53:06 +0100684 ndelay(100);
685
686 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200687}
688
689/**
690 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000691 * @mtd: MTD device structure
692 * @command: the command to be sent
693 * @column: the column address for this command, -1 if none
694 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200695 *
William Juul52c07962007-10-31 13:53:06 +0100696 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000697 * devices. We don't have the separate regions as we have in the small page
698 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200699 */
William Juul52c07962007-10-31 13:53:06 +0100700static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
701 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200702{
Scott Wood17fed142016-05-30 13:57:56 -0500703 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200704
705 /* Emulate NAND_CMD_READOOB */
706 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100707 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200708 command = NAND_CMD_READ0;
709 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200710
William Juul52c07962007-10-31 13:53:06 +0100711 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200712 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200713
714 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100715 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200716
717 /* Serially input address */
718 if (column != -1) {
719 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200720 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530721 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200722 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100723 chip->cmd_ctrl(mtd, column, ctrl);
724 ctrl &= ~NAND_CTRL_CHANGE;
725 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200726 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200727 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100728 chip->cmd_ctrl(mtd, page_addr, ctrl);
729 chip->cmd_ctrl(mtd, page_addr >> 8,
730 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900731 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100732 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:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900751 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900752 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200753 return;
754
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200755 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100756 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200757 break;
William Juul52c07962007-10-31 13:53:06 +0100758 udelay(chip->chip_delay);
759 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
760 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
761 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
762 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500763 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
764 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100765 return;
766
767 case NAND_CMD_RNDOUT:
768 /* No ready / busy check necessary */
769 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
770 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
771 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
772 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200773 return;
774
775 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100776 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
777 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
778 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
779 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200780
William Juul52c07962007-10-31 13:53:06 +0100781 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200782 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200783 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200784 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000785 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100786 */
787 if (!chip->dev_ready) {
788 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200789 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200790 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200791 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200792
Sergey Lapin3a38a552013-01-14 03:46:50 +0000793 /*
794 * Apply this short delay always to ensure that we do wait tWB in
795 * any case on any machine.
796 */
William Juul52c07962007-10-31 13:53:06 +0100797 ndelay(100);
798
799 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200800}
801
802/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200803 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000804 * @chip: the nand chip descriptor
805 * @mtd: MTD device structure
806 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200807 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200808 * Used when in panic, no locks are taken.
809 */
810static void panic_nand_get_device(struct nand_chip *chip,
811 struct mtd_info *mtd, int new_state)
812{
813 /* Hardware controller shared among independent devices */
814 chip->controller->active = chip;
815 chip->state = new_state;
816}
817
818/**
819 * nand_get_device - [GENERIC] Get chip for selected access
820 * @mtd: MTD device structure
821 * @new_state: the state which is requested
822 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200823 * Get the device and lock it for exclusive access
824 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200825static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200826nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100827{
Scott Wood17fed142016-05-30 13:57:56 -0500828 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200829 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100830 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200831}
832
833/**
834 * panic_nand_wait - [GENERIC] wait until the command is done
835 * @mtd: MTD device structure
836 * @chip: NAND chip structure
837 * @timeo: timeout
838 *
839 * Wait for command done. This is a helper function for nand_wait used when
840 * we are in interrupt context. May happen when in panic and trying to write
841 * an oops through mtdoops.
842 */
843static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
844 unsigned long timeo)
845{
846 int i;
847 for (i = 0; i < timeo; i++) {
848 if (chip->dev_ready) {
849 if (chip->dev_ready(mtd))
850 break;
851 } else {
852 if (chip->read_byte(mtd) & NAND_STATUS_READY)
853 break;
854 }
855 mdelay(1);
856 }
William Juul52c07962007-10-31 13:53:06 +0100857}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200858
859/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000860 * nand_wait - [DEFAULT] wait until the command is done
861 * @mtd: MTD device structure
862 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200863 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500864 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100865 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200866static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200867{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500868 int status;
869 unsigned long timeo = 400;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100870
Heiko Schocherf5895d12014-06-24 10:10:04 +0200871 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100872
Heiko Schocherf5895d12014-06-24 10:10:04 +0200873 /*
874 * Apply this short delay always to ensure that we do wait tWB in any
875 * case on any machine.
876 */
877 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100878
Heiko Schocherf5895d12014-06-24 10:10:04 +0200879 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100880
Heiko Schocherf5895d12014-06-24 10:10:04 +0200881 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
882 u32 time_start;
883
884 time_start = get_timer(0);
885 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200886 if (chip->dev_ready) {
887 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100888 break;
889 } else {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200890 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100891 break;
892 }
893 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200894 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100895
Heiko Schocherf5895d12014-06-24 10:10:04 +0200896 status = (int)chip->read_byte(mtd);
897 /* This can happen if in case of timeout or buggy dev_ready */
898 WARN_ON(!(status & NAND_STATUS_READY));
899 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200900}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500901
Scott Wood52ab7ce2016-05-30 13:57:58 -0500902/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900903 * nand_reset_data_interface - Reset data interface and timings
904 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900905 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900906 *
907 * Reset the Data interface and timings to ONFI mode 0.
908 *
909 * Returns 0 for success or negative error code otherwise.
910 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900911static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900912{
913 struct mtd_info *mtd = nand_to_mtd(chip);
914 const struct nand_data_interface *conf;
915 int ret;
916
917 if (!chip->setup_data_interface)
918 return 0;
919
920 /*
921 * The ONFI specification says:
922 * "
923 * To transition from NV-DDR or NV-DDR2 to the SDR data
924 * interface, the host shall use the Reset (FFh) command
925 * using SDR timing mode 0. A device in any timing mode is
926 * required to recognize Reset (FFh) command issued in SDR
927 * timing mode 0.
928 * "
929 *
930 * Configure the data interface in SDR mode and set the
931 * timings to timing mode 0.
932 */
933
934 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900935 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900936 if (ret)
937 pr_err("Failed to configure data interface to SDR timing mode 0\n");
938
939 return ret;
940}
941
942/**
943 * nand_setup_data_interface - Setup the best data interface and timings
944 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900945 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900946 *
947 * Find and configure the best data interface and NAND timings supported by
948 * the chip and the driver.
949 * First tries to retrieve supported timing modes from ONFI information,
950 * and if the NAND chip does not support ONFI, relies on the
951 * ->onfi_timing_mode_default specified in the nand_ids table.
952 *
953 * Returns 0 for success or negative error code otherwise.
954 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900955static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900956{
957 struct mtd_info *mtd = nand_to_mtd(chip);
958 int ret;
959
960 if (!chip->setup_data_interface || !chip->data_interface)
961 return 0;
962
963 /*
964 * Ensure the timing mode has been changed on the chip side
965 * before changing timings on the controller side.
966 */
967 if (chip->onfi_version) {
968 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
969 chip->onfi_timing_mode_default,
970 };
971
972 ret = chip->onfi_set_features(mtd, chip,
973 ONFI_FEATURE_ADDR_TIMING_MODE,
974 tmode_param);
975 if (ret)
976 goto err;
977 }
978
Boris Brezillon32935f42017-11-22 02:38:28 +0900979 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +0900980err:
981 return ret;
982}
983
984/**
985 * nand_init_data_interface - find the best data interface and timings
986 * @chip: The NAND chip
987 *
988 * Find the best data interface and NAND timings supported by the chip
989 * and the driver.
990 * First tries to retrieve supported timing modes from ONFI information,
991 * and if the NAND chip does not support ONFI, relies on the
992 * ->onfi_timing_mode_default specified in the nand_ids table. After this
993 * function nand_chip->data_interface is initialized with the best timing mode
994 * available.
995 *
996 * Returns 0 for success or negative error code otherwise.
997 */
998static int nand_init_data_interface(struct nand_chip *chip)
999{
1000 struct mtd_info *mtd = nand_to_mtd(chip);
1001 int modes, mode, ret;
1002
1003 if (!chip->setup_data_interface)
1004 return 0;
1005
1006 /*
1007 * First try to identify the best timings from ONFI parameters and
1008 * if the NAND does not support ONFI, fallback to the default ONFI
1009 * timing mode.
1010 */
1011 modes = onfi_get_async_timing_mode(chip);
1012 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1013 if (!chip->onfi_timing_mode_default)
1014 return 0;
1015
1016 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1017 }
1018
1019 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1020 GFP_KERNEL);
1021 if (!chip->data_interface)
1022 return -ENOMEM;
1023
1024 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1025 ret = onfi_init_data_interface(chip, chip->data_interface,
1026 NAND_SDR_IFACE, mode);
1027 if (ret)
1028 continue;
1029
Boris Brezillon32935f42017-11-22 02:38:28 +09001030 /* Pass -1 to only */
1031 ret = chip->setup_data_interface(mtd,
1032 NAND_DATA_IFACE_CHECK_ONLY,
1033 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001034 if (!ret) {
1035 chip->onfi_timing_mode_default = mode;
1036 break;
1037 }
1038 }
1039
1040 return 0;
1041}
1042
1043static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1044{
1045 kfree(chip->data_interface);
1046}
1047
1048/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001049 * nand_reset - Reset and initialize a NAND device
1050 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001051 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001052 *
1053 * Returns 0 for success or negative error code otherwise
1054 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001055int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001056{
1057 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001058 int ret;
1059
Boris Brezillon32935f42017-11-22 02:38:28 +09001060 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001061 if (ret)
1062 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001063
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001064 /*
1065 * The CS line has to be released before we can apply the new NAND
1066 * interface settings, hence this weird ->select_chip() dance.
1067 */
1068 chip->select_chip(mtd, chipnr);
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001069 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001070 chip->select_chip(mtd, -1);
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001071
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001072 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001073 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001074 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001075 if (ret)
1076 return ret;
1077
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001078 return 0;
1079}
1080
1081/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001082 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1083 * @buf: buffer to test
1084 * @len: buffer length
1085 * @bitflips_threshold: maximum number of bitflips
1086 *
1087 * Check if a buffer contains only 0xff, which means the underlying region
1088 * has been erased and is ready to be programmed.
1089 * The bitflips_threshold specify the maximum number of bitflips before
1090 * considering the region is not erased.
1091 * Note: The logic of this function has been extracted from the memweight
1092 * implementation, except that nand_check_erased_buf function exit before
1093 * testing the whole buffer if the number of bitflips exceed the
1094 * bitflips_threshold value.
1095 *
1096 * Returns a positive number of bitflips less than or equal to
1097 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1098 * threshold.
1099 */
1100static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1101{
1102 const unsigned char *bitmap = buf;
1103 int bitflips = 0;
1104 int weight;
1105
1106 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1107 len--, bitmap++) {
1108 weight = hweight8(*bitmap);
1109 bitflips += BITS_PER_BYTE - weight;
1110 if (unlikely(bitflips > bitflips_threshold))
1111 return -EBADMSG;
1112 }
1113
1114 for (; len >= 4; len -= 4, bitmap += 4) {
1115 weight = hweight32(*((u32 *)bitmap));
1116 bitflips += 32 - weight;
1117 if (unlikely(bitflips > bitflips_threshold))
1118 return -EBADMSG;
1119 }
1120
1121 for (; len > 0; len--, bitmap++) {
1122 weight = hweight8(*bitmap);
1123 bitflips += BITS_PER_BYTE - weight;
1124 if (unlikely(bitflips > bitflips_threshold))
1125 return -EBADMSG;
1126 }
1127
1128 return bitflips;
1129}
1130
1131/**
1132 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1133 * 0xff data
1134 * @data: data buffer to test
1135 * @datalen: data length
1136 * @ecc: ECC buffer
1137 * @ecclen: ECC length
1138 * @extraoob: extra OOB buffer
1139 * @extraooblen: extra OOB length
1140 * @bitflips_threshold: maximum number of bitflips
1141 *
1142 * Check if a data buffer and its associated ECC and OOB data contains only
1143 * 0xff pattern, which means the underlying region has been erased and is
1144 * ready to be programmed.
1145 * The bitflips_threshold specify the maximum number of bitflips before
1146 * considering the region as not erased.
1147 *
1148 * Note:
1149 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1150 * different from the NAND page size. When fixing bitflips, ECC engines will
1151 * report the number of errors per chunk, and the NAND core infrastructure
1152 * expect you to return the maximum number of bitflips for the whole page.
1153 * This is why you should always use this function on a single chunk and
1154 * not on the whole page. After checking each chunk you should update your
1155 * max_bitflips value accordingly.
1156 * 2/ When checking for bitflips in erased pages you should not only check
1157 * the payload data but also their associated ECC data, because a user might
1158 * have programmed almost all bits to 1 but a few. In this case, we
1159 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1160 * this case.
1161 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1162 * data are protected by the ECC engine.
1163 * It could also be used if you support subpages and want to attach some
1164 * extra OOB data to an ECC chunk.
1165 *
1166 * Returns a positive number of bitflips less than or equal to
1167 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1168 * threshold. In case of success, the passed buffers are filled with 0xff.
1169 */
1170int nand_check_erased_ecc_chunk(void *data, int datalen,
1171 void *ecc, int ecclen,
1172 void *extraoob, int extraooblen,
1173 int bitflips_threshold)
1174{
1175 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1176
1177 data_bitflips = nand_check_erased_buf(data, datalen,
1178 bitflips_threshold);
1179 if (data_bitflips < 0)
1180 return data_bitflips;
1181
1182 bitflips_threshold -= data_bitflips;
1183
1184 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1185 if (ecc_bitflips < 0)
1186 return ecc_bitflips;
1187
1188 bitflips_threshold -= ecc_bitflips;
1189
1190 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1191 bitflips_threshold);
1192 if (extraoob_bitflips < 0)
1193 return extraoob_bitflips;
1194
1195 if (data_bitflips)
1196 memset(data, 0xff, datalen);
1197
1198 if (ecc_bitflips)
1199 memset(ecc, 0xff, ecclen);
1200
1201 if (extraoob_bitflips)
1202 memset(extraoob, 0xff, extraooblen);
1203
1204 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1205}
1206EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001207
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001208/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001209 * nand_read_page_raw - [INTERN] read raw page data without ecc
1210 * @mtd: mtd info structure
1211 * @chip: nand chip info structure
1212 * @buf: buffer to store read data
1213 * @oob_required: caller requires OOB data read to chip->oob_poi
1214 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001215 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001216 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001217 */
William Juul52c07962007-10-31 13:53:06 +01001218static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001219 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001220{
William Juul52c07962007-10-31 13:53:06 +01001221 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001222 if (oob_required)
1223 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01001224 return 0;
1225}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001226
William Juul52c07962007-10-31 13:53:06 +01001227/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001228 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1229 * @mtd: mtd info structure
1230 * @chip: nand chip info structure
1231 * @buf: buffer to store read data
1232 * @oob_required: caller requires OOB data read to chip->oob_poi
1233 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001234 *
1235 * We need a special oob layout and handling even when OOB isn't used.
1236 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001237static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001238 struct nand_chip *chip, uint8_t *buf,
1239 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001240{
1241 int eccsize = chip->ecc.size;
1242 int eccbytes = chip->ecc.bytes;
1243 uint8_t *oob = chip->oob_poi;
1244 int steps, size;
1245
1246 for (steps = chip->ecc.steps; steps > 0; steps--) {
1247 chip->read_buf(mtd, buf, eccsize);
1248 buf += eccsize;
1249
1250 if (chip->ecc.prepad) {
1251 chip->read_buf(mtd, oob, chip->ecc.prepad);
1252 oob += chip->ecc.prepad;
1253 }
1254
1255 chip->read_buf(mtd, oob, eccbytes);
1256 oob += eccbytes;
1257
1258 if (chip->ecc.postpad) {
1259 chip->read_buf(mtd, oob, chip->ecc.postpad);
1260 oob += chip->ecc.postpad;
1261 }
1262 }
1263
1264 size = mtd->oobsize - (oob - chip->oob_poi);
1265 if (size)
1266 chip->read_buf(mtd, oob, size);
1267
1268 return 0;
1269}
1270
1271/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001272 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1273 * @mtd: mtd info structure
1274 * @chip: nand chip info structure
1275 * @buf: buffer to store read data
1276 * @oob_required: caller requires OOB data read to chip->oob_poi
1277 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001278 */
1279static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001280 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001281{
1282 int i, eccsize = chip->ecc.size;
1283 int eccbytes = chip->ecc.bytes;
1284 int eccsteps = chip->ecc.steps;
1285 uint8_t *p = buf;
1286 uint8_t *ecc_calc = chip->buffers->ecccalc;
1287 uint8_t *ecc_code = chip->buffers->ecccode;
1288 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001289 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001290
Sergey Lapin3a38a552013-01-14 03:46:50 +00001291 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001292
William Juul52c07962007-10-31 13:53:06 +01001293 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1294 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001295
William Juul52c07962007-10-31 13:53:06 +01001296 for (i = 0; i < chip->ecc.total; i++)
1297 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001298
William Juul52c07962007-10-31 13:53:06 +01001299 eccsteps = chip->ecc.steps;
1300 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001301
William Juul52c07962007-10-31 13:53:06 +01001302 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1303 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001304
William Juul52c07962007-10-31 13:53:06 +01001305 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001306 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001307 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001308 } else {
William Juul52c07962007-10-31 13:53:06 +01001309 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001310 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1311 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001312 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001313 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001314}
1315
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001316/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001317 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001318 * @mtd: mtd info structure
1319 * @chip: nand chip info structure
1320 * @data_offs: offset of requested data within the page
1321 * @readlen: data length
1322 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001323 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001324 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001325static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001326 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1327 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001328{
1329 int start_step, end_step, num_steps;
1330 uint32_t *eccpos = chip->ecc.layout->eccpos;
1331 uint8_t *p;
1332 int data_col_addr, i, gaps = 0;
1333 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1334 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001335 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001336 unsigned int max_bitflips = 0;
Scott Wood3628f002008-10-24 16:20:43 -05001337
Sergey Lapin3a38a552013-01-14 03:46:50 +00001338 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001339 start_step = data_offs / chip->ecc.size;
1340 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1341 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001342 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001343
Sergey Lapin3a38a552013-01-14 03:46:50 +00001344 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001345 datafrag_len = num_steps * chip->ecc.size;
1346 eccfrag_len = num_steps * chip->ecc.bytes;
1347
1348 data_col_addr = start_step * chip->ecc.size;
1349 /* If we read not a page aligned data */
1350 if (data_col_addr != 0)
1351 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1352
1353 p = bufpoi + data_col_addr;
1354 chip->read_buf(mtd, p, datafrag_len);
1355
Sergey Lapin3a38a552013-01-14 03:46:50 +00001356 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001357 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1358 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1359
Sergey Lapin3a38a552013-01-14 03:46:50 +00001360 /*
1361 * The performance is faster if we position offsets according to
1362 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1363 */
Scott Wood3628f002008-10-24 16:20:43 -05001364 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001365 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001366 gaps = 1;
1367 break;
1368 }
1369 }
1370 if (gaps) {
1371 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1372 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1373 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001374 /*
1375 * Send the command to read the particular ECC bytes take care
1376 * about buswidth alignment in read_buf.
1377 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001378 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001379 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001380 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001381 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001382 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001383 aligned_len++;
1384
Christian Hitzb8a6b372011-10-12 09:32:02 +02001385 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1386 mtd->writesize + aligned_pos, -1);
Scott Wood3628f002008-10-24 16:20:43 -05001387 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1388 }
1389
1390 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001391 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001392
1393 p = bufpoi + data_col_addr;
1394 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1395 int stat;
1396
Christian Hitzb8a6b372011-10-12 09:32:02 +02001397 stat = chip->ecc.correct(mtd, p,
1398 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001399 if (stat == -EBADMSG &&
1400 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1401 /* check for empty pages with bitflips */
1402 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1403 &chip->buffers->ecccode[i],
1404 chip->ecc.bytes,
1405 NULL, 0,
1406 chip->ecc.strength);
1407 }
1408
Heiko Schocherf5895d12014-06-24 10:10:04 +02001409 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001410 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001411 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001412 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001413 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1414 }
Scott Wood3628f002008-10-24 16:20:43 -05001415 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001416 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001417}
1418
1419/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001420 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1421 * @mtd: mtd info structure
1422 * @chip: nand chip info structure
1423 * @buf: buffer to store read data
1424 * @oob_required: caller requires OOB data read to chip->oob_poi
1425 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001426 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001427 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001428 */
William Juul52c07962007-10-31 13:53:06 +01001429static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001430 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001431{
William Juul52c07962007-10-31 13:53:06 +01001432 int i, eccsize = chip->ecc.size;
1433 int eccbytes = chip->ecc.bytes;
1434 int eccsteps = chip->ecc.steps;
1435 uint8_t *p = buf;
1436 uint8_t *ecc_calc = chip->buffers->ecccalc;
1437 uint8_t *ecc_code = chip->buffers->ecccode;
1438 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001439 unsigned int max_bitflips = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001440
William Juul52c07962007-10-31 13:53:06 +01001441 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1442 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1443 chip->read_buf(mtd, p, eccsize);
1444 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1445 }
1446 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001447
William Juul52c07962007-10-31 13:53:06 +01001448 for (i = 0; i < chip->ecc.total; i++)
1449 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001450
William Juul52c07962007-10-31 13:53:06 +01001451 eccsteps = chip->ecc.steps;
1452 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001453
William Juul52c07962007-10-31 13:53:06 +01001454 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1455 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001456
William Juul52c07962007-10-31 13:53:06 +01001457 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001458 if (stat == -EBADMSG &&
1459 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1460 /* check for empty pages with bitflips */
1461 stat = nand_check_erased_ecc_chunk(p, eccsize,
1462 &ecc_code[i], eccbytes,
1463 NULL, 0,
1464 chip->ecc.strength);
1465 }
1466
Heiko Schocherf5895d12014-06-24 10:10:04 +02001467 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001468 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001469 } else {
William Juul52c07962007-10-31 13:53:06 +01001470 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001471 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1472 }
William Juul52c07962007-10-31 13:53:06 +01001473 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001474 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001475}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001476
William Juul52c07962007-10-31 13:53:06 +01001477/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001478 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1479 * @mtd: mtd info structure
1480 * @chip: nand chip info structure
1481 * @buf: buffer to store read data
1482 * @oob_required: caller requires OOB data read to chip->oob_poi
1483 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001484 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001485 * Hardware ECC for large page chips, require OOB to be read first. For this
1486 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1487 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1488 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1489 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001490 */
1491static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001492 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001493{
1494 int i, eccsize = chip->ecc.size;
1495 int eccbytes = chip->ecc.bytes;
1496 int eccsteps = chip->ecc.steps;
1497 uint8_t *p = buf;
1498 uint8_t *ecc_code = chip->buffers->ecccode;
1499 uint32_t *eccpos = chip->ecc.layout->eccpos;
1500 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001501 unsigned int max_bitflips = 0;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001502
1503 /* Read the OOB area first */
1504 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1505 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1506 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1507
1508 for (i = 0; i < chip->ecc.total; i++)
1509 ecc_code[i] = chip->oob_poi[eccpos[i]];
1510
1511 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1512 int stat;
1513
1514 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1515 chip->read_buf(mtd, p, eccsize);
1516 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1517
1518 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001519 if (stat == -EBADMSG &&
1520 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1521 /* check for empty pages with bitflips */
1522 stat = nand_check_erased_ecc_chunk(p, eccsize,
1523 &ecc_code[i], eccbytes,
1524 NULL, 0,
1525 chip->ecc.strength);
1526 }
1527
Heiko Schocherf5895d12014-06-24 10:10:04 +02001528 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001529 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001530 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001531 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001532 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1533 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001534 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001535 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001536}
1537
1538/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001539 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1540 * @mtd: mtd info structure
1541 * @chip: nand chip info structure
1542 * @buf: buffer to store read data
1543 * @oob_required: caller requires OOB data read to chip->oob_poi
1544 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001545 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001546 * The hw generator calculates the error syndrome automatically. Therefore we
1547 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01001548 */
1549static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001550 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001551{
1552 int i, eccsize = chip->ecc.size;
1553 int eccbytes = chip->ecc.bytes;
1554 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05001555 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01001556 uint8_t *p = buf;
1557 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001558 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01001559
1560 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1561 int stat;
1562
1563 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1564 chip->read_buf(mtd, p, eccsize);
1565
1566 if (chip->ecc.prepad) {
1567 chip->read_buf(mtd, oob, chip->ecc.prepad);
1568 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001569 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001570
William Juul52c07962007-10-31 13:53:06 +01001571 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1572 chip->read_buf(mtd, oob, eccbytes);
1573 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001574
William Juul52c07962007-10-31 13:53:06 +01001575 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001576
William Juul52c07962007-10-31 13:53:06 +01001577 if (chip->ecc.postpad) {
1578 chip->read_buf(mtd, oob, chip->ecc.postpad);
1579 oob += chip->ecc.postpad;
1580 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05001581
1582 if (stat == -EBADMSG &&
1583 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1584 /* check for empty pages with bitflips */
1585 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1586 oob - eccpadbytes,
1587 eccpadbytes,
1588 NULL, 0,
1589 chip->ecc.strength);
1590 }
1591
1592 if (stat < 0) {
1593 mtd->ecc_stats.failed++;
1594 } else {
1595 mtd->ecc_stats.corrected += stat;
1596 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1597 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001598 }
William Juul52c07962007-10-31 13:53:06 +01001599
1600 /* Calculate remaining oob bytes */
1601 i = mtd->oobsize - (oob - chip->oob_poi);
1602 if (i)
1603 chip->read_buf(mtd, oob, i);
1604
Heiko Schocherf5895d12014-06-24 10:10:04 +02001605 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001606}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001607
1608/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001609 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1610 * @chip: nand chip structure
1611 * @oob: oob destination address
1612 * @ops: oob ops structure
1613 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01001614 */
1615static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1616 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001617{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001618 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001619
Sergey Lapin3a38a552013-01-14 03:46:50 +00001620 case MTD_OPS_PLACE_OOB:
1621 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001622 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1623 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001624
Sergey Lapin3a38a552013-01-14 03:46:50 +00001625 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01001626 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1627 uint32_t boffs = 0, roffs = ops->ooboffs;
1628 size_t bytes = 0;
1629
Christian Hitz13fc0e22011-10-12 09:32:01 +02001630 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001631 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01001632 if (unlikely(roffs)) {
1633 if (roffs >= free->length) {
1634 roffs -= free->length;
1635 continue;
1636 }
1637 boffs = free->offset + roffs;
1638 bytes = min_t(size_t, len,
1639 (free->length - roffs));
1640 roffs = 0;
1641 } else {
1642 bytes = min_t(size_t, len, free->length);
1643 boffs = free->offset;
1644 }
1645 memcpy(oob, chip->oob_poi + boffs, bytes);
1646 oob += bytes;
1647 }
1648 return oob;
1649 }
1650 default:
1651 BUG();
1652 }
1653 return NULL;
1654}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001655
1656/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001657 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1658 * @mtd: MTD device structure
1659 * @retry_mode: the retry mode to use
1660 *
1661 * Some vendors supply a special command to shift the Vt threshold, to be used
1662 * when there are too many bitflips in a page (i.e., ECC error). After setting
1663 * a new threshold, the host should retry reading the page.
1664 */
1665static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1666{
Scott Wood17fed142016-05-30 13:57:56 -05001667 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001668
1669 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1670
1671 if (retry_mode >= chip->read_retries)
1672 return -EINVAL;
1673
1674 if (!chip->setup_read_retry)
1675 return -EOPNOTSUPP;
1676
1677 return chip->setup_read_retry(mtd, retry_mode);
1678}
1679
1680/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001681 * nand_do_read_ops - [INTERN] Read data with ECC
1682 * @mtd: MTD device structure
1683 * @from: offset to read from
1684 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001685 *
William Juul52c07962007-10-31 13:53:06 +01001686 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001687 */
William Juul52c07962007-10-31 13:53:06 +01001688static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1689 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001690{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001691 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05001692 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01001693 int ret = 0;
1694 uint32_t readlen = ops->len;
1695 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05001696 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02001697
William Juul52c07962007-10-31 13:53:06 +01001698 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001699 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01001700 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001701 int retry_mode = 0;
1702 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001703
William Juul52c07962007-10-31 13:53:06 +01001704 chipnr = (int)(from >> chip->chip_shift);
1705 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001706
William Juul52c07962007-10-31 13:53:06 +01001707 realpage = (int)(from >> chip->page_shift);
1708 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001709
William Juul52c07962007-10-31 13:53:06 +01001710 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001711
William Juul52c07962007-10-31 13:53:06 +01001712 buf = ops->datbuf;
1713 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001714 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001715
Christian Hitz13fc0e22011-10-12 09:32:01 +02001716 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001717 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06001718
Heiko Schocherf5895d12014-06-24 10:10:04 +02001719 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01001720 bytes = min(mtd->writesize - col, readlen);
1721 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001722
Scott Wood3ea94ed2015-06-26 19:03:26 -05001723 if (!aligned)
1724 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09001725 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1726 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
1727 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001728 else
1729 use_bufpoi = 0;
1730
Sergey Lapin3a38a552013-01-14 03:46:50 +00001731 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01001732 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001733 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1734
1735 if (use_bufpoi && aligned)
1736 pr_debug("%s: using read bounce buffer for buf@%p\n",
1737 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001738
Heiko Schocherf5895d12014-06-24 10:10:04 +02001739read_retry:
Marc Gonzalezc3a29852017-11-22 02:38:22 +09001740 if (nand_standard_page_accessors(&chip->ecc))
1741 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001742
Paul Burton700a76c2013-09-04 15:16:56 +01001743 /*
1744 * Now read the page into the buffer. Absent an error,
1745 * the read methods return max bitflips per ecc step.
1746 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001747 if (unlikely(ops->mode == MTD_OPS_RAW))
1748 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1749 oob_required,
1750 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001751 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001752 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02001753 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001754 col, bytes, bufpoi,
1755 page);
William Juul52c07962007-10-31 13:53:06 +01001756 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001757 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001758 oob_required, page);
1759 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001760 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001761 /* Invalidate page cache */
1762 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01001763 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001764 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001765
Paul Burton700a76c2013-09-04 15:16:56 +01001766 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1767
William Juul52c07962007-10-31 13:53:06 +01001768 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05001769 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001770 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001771 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01001772 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05001773 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01001774 chip->pagebuf_bitflips = ret;
1775 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001776 /* Invalidate page cache */
1777 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01001778 }
William Juul52c07962007-10-31 13:53:06 +01001779 memcpy(buf, chip->buffers->databuf + col, bytes);
1780 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001781
William Juul52c07962007-10-31 13:53:06 +01001782 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001783 int toread = min(oobreadlen, max_oobsize);
1784
1785 if (toread) {
1786 oob = nand_transfer_oob(chip,
1787 oob, ops, toread);
1788 oobreadlen -= toread;
1789 }
William Juul52c07962007-10-31 13:53:06 +01001790 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001791
1792 if (chip->options & NAND_NEED_READRDY) {
1793 /* Apply delay or wait for ready/busy pin */
1794 if (!chip->dev_ready)
1795 udelay(chip->chip_delay);
1796 else
1797 nand_wait_ready(mtd);
1798 }
1799
1800 if (mtd->ecc_stats.failed - ecc_failures) {
1801 if (retry_mode + 1 < chip->read_retries) {
1802 retry_mode++;
1803 ret = nand_setup_read_retry(mtd,
1804 retry_mode);
1805 if (ret < 0)
1806 break;
1807
1808 /* Reset failures; retry */
1809 mtd->ecc_stats.failed = ecc_failures;
1810 goto read_retry;
1811 } else {
1812 /* No more retry modes; real failure */
1813 ecc_fail = true;
1814 }
1815 }
1816
1817 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01001818 } else {
1819 memcpy(buf, chip->buffers->databuf + col, bytes);
1820 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01001821 max_bitflips = max_t(unsigned int, max_bitflips,
1822 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01001823 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001824
William Juul52c07962007-10-31 13:53:06 +01001825 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001826
Heiko Schocherf5895d12014-06-24 10:10:04 +02001827 /* Reset to retry mode 0 */
1828 if (retry_mode) {
1829 ret = nand_setup_read_retry(mtd, 0);
1830 if (ret < 0)
1831 break;
1832 retry_mode = 0;
1833 }
1834
William Juul52c07962007-10-31 13:53:06 +01001835 if (!readlen)
1836 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001837
Sergey Lapin3a38a552013-01-14 03:46:50 +00001838 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01001839 col = 0;
1840 /* Increment page address */
1841 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001842
William Juul52c07962007-10-31 13:53:06 +01001843 page = realpage & chip->pagemask;
1844 /* Check, if we cross a chip boundary */
1845 if (!page) {
1846 chipnr++;
1847 chip->select_chip(mtd, -1);
1848 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001849 }
William Juul52c07962007-10-31 13:53:06 +01001850 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001851 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001852
William Juul52c07962007-10-31 13:53:06 +01001853 ops->retlen = ops->len - (size_t) readlen;
1854 if (oob)
1855 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001856
Heiko Schocherf5895d12014-06-24 10:10:04 +02001857 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01001858 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001859
Heiko Schocherf5895d12014-06-24 10:10:04 +02001860 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01001861 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001862
Paul Burton700a76c2013-09-04 15:16:56 +01001863 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001864}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001865
William Juul52c07962007-10-31 13:53:06 +01001866/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001867 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1868 * @mtd: mtd info structure
1869 * @chip: nand chip info structure
1870 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001871 */
1872static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001873 int page)
William Juul52c07962007-10-31 13:53:06 +01001874{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001875 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juul52c07962007-10-31 13:53:06 +01001876 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001877 return 0;
William Juul52c07962007-10-31 13:53:06 +01001878}
1879
1880/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001881 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01001882 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00001883 * @mtd: mtd info structure
1884 * @chip: nand chip info structure
1885 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001886 */
1887static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001888 int page)
William Juul52c07962007-10-31 13:53:06 +01001889{
William Juul52c07962007-10-31 13:53:06 +01001890 int length = mtd->oobsize;
1891 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1892 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001893 uint8_t *bufpoi = chip->oob_poi;
William Juul52c07962007-10-31 13:53:06 +01001894 int i, toread, sndrnd = 0, pos;
1895
1896 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1897 for (i = 0; i < chip->ecc.steps; i++) {
1898 if (sndrnd) {
1899 pos = eccsize + i * (eccsize + chunk);
1900 if (mtd->writesize > 512)
1901 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1902 else
1903 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001904 } else
William Juul52c07962007-10-31 13:53:06 +01001905 sndrnd = 1;
1906 toread = min_t(int, length, chunk);
1907 chip->read_buf(mtd, bufpoi, toread);
1908 bufpoi += toread;
1909 length -= toread;
1910 }
1911 if (length > 0)
1912 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001913
Sergey Lapin3a38a552013-01-14 03:46:50 +00001914 return 0;
William Juul52c07962007-10-31 13:53:06 +01001915}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001916
William Juul52c07962007-10-31 13:53:06 +01001917/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001918 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1919 * @mtd: mtd info structure
1920 * @chip: nand chip info structure
1921 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001922 */
1923static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1924 int page)
1925{
1926 int status = 0;
1927 const uint8_t *buf = chip->oob_poi;
1928 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001929
William Juul52c07962007-10-31 13:53:06 +01001930 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1931 chip->write_buf(mtd, buf, length);
1932 /* Send command to program the OOB data */
1933 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001934
William Juul52c07962007-10-31 13:53:06 +01001935 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001936
William Juul52c07962007-10-31 13:53:06 +01001937 return status & NAND_STATUS_FAIL ? -EIO : 0;
1938}
1939
1940/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001941 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1942 * with syndrome - only for large page flash
1943 * @mtd: mtd info structure
1944 * @chip: nand chip info structure
1945 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001946 */
1947static int nand_write_oob_syndrome(struct mtd_info *mtd,
1948 struct nand_chip *chip, int page)
1949{
1950 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1951 int eccsize = chip->ecc.size, length = mtd->oobsize;
1952 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1953 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001954
1955 /*
William Juul52c07962007-10-31 13:53:06 +01001956 * data-ecc-data-ecc ... ecc-oob
1957 * or
1958 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001959 */
William Juul52c07962007-10-31 13:53:06 +01001960 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1961 pos = steps * (eccsize + chunk);
1962 steps = 0;
1963 } else
1964 pos = eccsize;
1965
1966 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1967 for (i = 0; i < steps; i++) {
1968 if (sndcmd) {
1969 if (mtd->writesize <= 512) {
1970 uint32_t fill = 0xFFFFFFFF;
1971
1972 len = eccsize;
1973 while (len > 0) {
1974 int num = min_t(int, len, 4);
1975 chip->write_buf(mtd, (uint8_t *)&fill,
1976 num);
1977 len -= num;
1978 }
1979 } else {
1980 pos = eccsize + i * (eccsize + chunk);
1981 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1982 }
1983 } else
1984 sndcmd = 1;
1985 len = min_t(int, length, chunk);
1986 chip->write_buf(mtd, bufpoi, len);
1987 bufpoi += len;
1988 length -= len;
1989 }
1990 if (length > 0)
1991 chip->write_buf(mtd, bufpoi, length);
1992
1993 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1994 status = chip->waitfunc(mtd, chip);
1995
1996 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001997}
1998
1999/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002000 * nand_do_read_oob - [INTERN] NAND read out-of-band
2001 * @mtd: MTD device structure
2002 * @from: offset to read from
2003 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002004 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002005 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002006 */
William Juul52c07962007-10-31 13:53:06 +01002007static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2008 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002009{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002010 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002011 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002012 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002013 int readlen = ops->ooblen;
2014 int len;
2015 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002016 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002017
Heiko Schocherf5895d12014-06-24 10:10:04 +02002018 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002019 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002020
Sergey Lapin3a38a552013-01-14 03:46:50 +00002021 stats = mtd->ecc_stats;
2022
Scott Wood52ab7ce2016-05-30 13:57:58 -05002023 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002024
William Juul52c07962007-10-31 13:53:06 +01002025 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002026 pr_debug("%s: attempt to start read outside oob\n",
2027 __func__);
William Juul52c07962007-10-31 13:53:06 +01002028 return -EINVAL;
2029 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002030
2031 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002032 if (unlikely(from >= mtd->size ||
2033 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2034 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002035 pr_debug("%s: attempt to read beyond end of device\n",
2036 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002037 return -EINVAL;
2038 }
2039
William Juul52c07962007-10-31 13:53:06 +01002040 chipnr = (int)(from >> chip->chip_shift);
2041 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002042
William Juul52c07962007-10-31 13:53:06 +01002043 /* Shift to get page */
2044 realpage = (int)(from >> chip->page_shift);
2045 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046
Christian Hitz13fc0e22011-10-12 09:32:01 +02002047 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002048 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002049
Sergey Lapin3a38a552013-01-14 03:46:50 +00002050 if (ops->mode == MTD_OPS_RAW)
2051 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2052 else
2053 ret = chip->ecc.read_oob(mtd, chip, page);
2054
2055 if (ret < 0)
2056 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002057
William Juul52c07962007-10-31 13:53:06 +01002058 len = min(len, readlen);
2059 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002060
Heiko Schocherf5895d12014-06-24 10:10:04 +02002061 if (chip->options & NAND_NEED_READRDY) {
2062 /* Apply delay or wait for ready/busy pin */
2063 if (!chip->dev_ready)
2064 udelay(chip->chip_delay);
2065 else
2066 nand_wait_ready(mtd);
2067 }
2068
William Juul52c07962007-10-31 13:53:06 +01002069 readlen -= len;
2070 if (!readlen)
2071 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002072
William Juul52c07962007-10-31 13:53:06 +01002073 /* Increment page address */
2074 realpage++;
2075
2076 page = realpage & chip->pagemask;
2077 /* Check, if we cross a chip boundary */
2078 if (!page) {
2079 chipnr++;
2080 chip->select_chip(mtd, -1);
2081 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002082 }
William Juul52c07962007-10-31 13:53:06 +01002083 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002084 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002085
Sergey Lapin3a38a552013-01-14 03:46:50 +00002086 ops->oobretlen = ops->ooblen - readlen;
2087
2088 if (ret < 0)
2089 return ret;
2090
2091 if (mtd->ecc_stats.failed - stats.failed)
2092 return -EBADMSG;
2093
2094 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002095}
2096
2097/**
William Juul52c07962007-10-31 13:53:06 +01002098 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002099 * @mtd: MTD device structure
2100 * @from: offset to read from
2101 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002102 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002103 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002104 */
William Juul52c07962007-10-31 13:53:06 +01002105static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2106 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002107{
William Juul52c07962007-10-31 13:53:06 +01002108 int ret = -ENOTSUPP;
2109
2110 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002111
2112 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002113 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002114 pr_debug("%s: attempt to read beyond end of device\n",
2115 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002116 return -EINVAL;
2117 }
2118
Heiko Schocherf5895d12014-06-24 10:10:04 +02002119 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002120
Christian Hitz13fc0e22011-10-12 09:32:01 +02002121 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002122 case MTD_OPS_PLACE_OOB:
2123 case MTD_OPS_AUTO_OOB:
2124 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002125 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002126
William Juul52c07962007-10-31 13:53:06 +01002127 default:
2128 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002129 }
2130
William Juul52c07962007-10-31 13:53:06 +01002131 if (!ops->datbuf)
2132 ret = nand_do_read_oob(mtd, from, ops);
2133 else
2134 ret = nand_do_read_ops(mtd, from, ops);
2135
Christian Hitz13fc0e22011-10-12 09:32:01 +02002136out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002137 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002138 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002139}
2140
2141
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002142/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002143 * nand_write_page_raw - [INTERN] raw page write function
2144 * @mtd: mtd info structure
2145 * @chip: nand chip info structure
2146 * @buf: data buffer
2147 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002148 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002149 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002150 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002151 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002152static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002153 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002154{
William Juul52c07962007-10-31 13:53:06 +01002155 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002156 if (oob_required)
2157 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2158
2159 return 0;
William Juul52c07962007-10-31 13:53:06 +01002160}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002161
William Juul52c07962007-10-31 13:53:06 +01002162/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002163 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2164 * @mtd: mtd info structure
2165 * @chip: nand chip info structure
2166 * @buf: data buffer
2167 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002168 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002169 *
2170 * We need a special oob layout and handling even when ECC isn't checked.
2171 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002172static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002173 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002174 const uint8_t *buf, int oob_required,
2175 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002176{
2177 int eccsize = chip->ecc.size;
2178 int eccbytes = chip->ecc.bytes;
2179 uint8_t *oob = chip->oob_poi;
2180 int steps, size;
2181
2182 for (steps = chip->ecc.steps; steps > 0; steps--) {
2183 chip->write_buf(mtd, buf, eccsize);
2184 buf += eccsize;
2185
2186 if (chip->ecc.prepad) {
2187 chip->write_buf(mtd, oob, chip->ecc.prepad);
2188 oob += chip->ecc.prepad;
2189 }
2190
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002191 chip->write_buf(mtd, oob, eccbytes);
David Brownellee86b8d2009-11-07 16:27:01 -05002192 oob += eccbytes;
2193
2194 if (chip->ecc.postpad) {
2195 chip->write_buf(mtd, oob, chip->ecc.postpad);
2196 oob += chip->ecc.postpad;
2197 }
2198 }
2199
2200 size = mtd->oobsize - (oob - chip->oob_poi);
2201 if (size)
2202 chip->write_buf(mtd, oob, size);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002203
2204 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002205}
2206/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002207 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2208 * @mtd: mtd info structure
2209 * @chip: nand chip info structure
2210 * @buf: data buffer
2211 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002212 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002213 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002214static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002215 const uint8_t *buf, int oob_required,
2216 int page)
William Juul52c07962007-10-31 13:53:06 +01002217{
2218 int i, eccsize = chip->ecc.size;
2219 int eccbytes = chip->ecc.bytes;
2220 int eccsteps = chip->ecc.steps;
2221 uint8_t *ecc_calc = chip->buffers->ecccalc;
2222 const uint8_t *p = buf;
2223 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002224
Sergey Lapin3a38a552013-01-14 03:46:50 +00002225 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002226 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2227 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002228
William Juul52c07962007-10-31 13:53:06 +01002229 for (i = 0; i < chip->ecc.total; i++)
2230 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002231
Scott Wood46e13102016-05-30 13:57:57 -05002232 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002233}
2234
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002235/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002236 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2237 * @mtd: mtd info structure
2238 * @chip: nand chip info structure
2239 * @buf: data buffer
2240 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002241 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002242 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002243static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002244 const uint8_t *buf, int oob_required,
2245 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002246{
William Juul52c07962007-10-31 13:53:06 +01002247 int i, eccsize = chip->ecc.size;
2248 int eccbytes = chip->ecc.bytes;
2249 int eccsteps = chip->ecc.steps;
2250 uint8_t *ecc_calc = chip->buffers->ecccalc;
2251 const uint8_t *p = buf;
2252 uint32_t *eccpos = chip->ecc.layout->eccpos;
2253
2254 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2255 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2256 chip->write_buf(mtd, p, eccsize);
2257 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2258 }
2259
2260 for (i = 0; i < chip->ecc.total; i++)
2261 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2262
2263 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002264
2265 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002266}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002267
Heiko Schocherf5895d12014-06-24 10:10:04 +02002268
2269/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002270 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002271 * @mtd: mtd info structure
2272 * @chip: nand chip info structure
2273 * @offset: column address of subpage within the page
2274 * @data_len: data length
2275 * @buf: data buffer
2276 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002277 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002278 */
2279static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2280 struct nand_chip *chip, uint32_t offset,
2281 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002282 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002283{
2284 uint8_t *oob_buf = chip->oob_poi;
2285 uint8_t *ecc_calc = chip->buffers->ecccalc;
2286 int ecc_size = chip->ecc.size;
2287 int ecc_bytes = chip->ecc.bytes;
2288 int ecc_steps = chip->ecc.steps;
2289 uint32_t *eccpos = chip->ecc.layout->eccpos;
2290 uint32_t start_step = offset / ecc_size;
2291 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2292 int oob_bytes = mtd->oobsize / ecc_steps;
2293 int step, i;
2294
2295 for (step = 0; step < ecc_steps; step++) {
2296 /* configure controller for WRITE access */
2297 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2298
2299 /* write data (untouched subpages already masked by 0xFF) */
2300 chip->write_buf(mtd, buf, ecc_size);
2301
2302 /* mask ECC of un-touched subpages by padding 0xFF */
2303 if ((step < start_step) || (step > end_step))
2304 memset(ecc_calc, 0xff, ecc_bytes);
2305 else
2306 chip->ecc.calculate(mtd, buf, ecc_calc);
2307
2308 /* mask OOB of un-touched subpages by padding 0xFF */
2309 /* if oob_required, preserve OOB metadata of written subpage */
2310 if (!oob_required || (step < start_step) || (step > end_step))
2311 memset(oob_buf, 0xff, oob_bytes);
2312
2313 buf += ecc_size;
2314 ecc_calc += ecc_bytes;
2315 oob_buf += oob_bytes;
2316 }
2317
2318 /* copy calculated ECC for whole page to chip->buffer->oob */
2319 /* this include masked-value(0xFF) for unwritten subpages */
2320 ecc_calc = chip->buffers->ecccalc;
2321 for (i = 0; i < chip->ecc.total; i++)
2322 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2323
2324 /* write OOB buffer to NAND device */
2325 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2326
2327 return 0;
2328}
2329
2330
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002331/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002332 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2333 * @mtd: mtd info structure
2334 * @chip: nand chip info structure
2335 * @buf: data buffer
2336 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002337 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002338 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002339 * The hw generator calculates the error syndrome automatically. Therefore we
2340 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002341 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002342static int nand_write_page_syndrome(struct mtd_info *mtd,
2343 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002344 const uint8_t *buf, int oob_required,
2345 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002346{
William Juul52c07962007-10-31 13:53:06 +01002347 int i, eccsize = chip->ecc.size;
2348 int eccbytes = chip->ecc.bytes;
2349 int eccsteps = chip->ecc.steps;
2350 const uint8_t *p = buf;
2351 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002352
William Juul52c07962007-10-31 13:53:06 +01002353 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002354
William Juul52c07962007-10-31 13:53:06 +01002355 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2356 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002357
William Juul52c07962007-10-31 13:53:06 +01002358 if (chip->ecc.prepad) {
2359 chip->write_buf(mtd, oob, chip->ecc.prepad);
2360 oob += chip->ecc.prepad;
2361 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002362
William Juul52c07962007-10-31 13:53:06 +01002363 chip->ecc.calculate(mtd, p, oob);
2364 chip->write_buf(mtd, oob, eccbytes);
2365 oob += eccbytes;
2366
2367 if (chip->ecc.postpad) {
2368 chip->write_buf(mtd, oob, chip->ecc.postpad);
2369 oob += chip->ecc.postpad;
2370 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002371 }
2372
William Juul52c07962007-10-31 13:53:06 +01002373 /* Calculate remaining oob bytes */
2374 i = mtd->oobsize - (oob - chip->oob_poi);
2375 if (i)
2376 chip->write_buf(mtd, oob, i);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002377
2378 return 0;
William Juul52c07962007-10-31 13:53:06 +01002379}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002380
William Juul52c07962007-10-31 13:53:06 +01002381/**
2382 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00002383 * @mtd: MTD device structure
2384 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02002385 * @offset: address offset within the page
2386 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00002387 * @buf: the data to write
2388 * @oob_required: must write chip->oob_poi to OOB
2389 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00002390 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01002391 */
2392static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002393 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002394 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01002395{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002396 int status, subpage;
2397
2398 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2399 chip->ecc.write_subpage)
2400 subpage = offset || (data_len < mtd->writesize);
2401 else
2402 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002403
Marc Gonzalezc3a29852017-11-22 02:38:22 +09002404 if (nand_standard_page_accessors(&chip->ecc))
2405 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
William Juul52c07962007-10-31 13:53:06 +01002406
2407 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02002408 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05002409 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002410 else if (subpage)
2411 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002412 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01002413 else
Scott Wood46e13102016-05-30 13:57:57 -05002414 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2415 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002416
2417 if (status < 0)
2418 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002419
Boris Brezillond4949322017-11-22 02:38:26 +09002420 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002421 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002422
Boris Brezillond4949322017-11-22 02:38:26 +09002423 status = chip->waitfunc(mtd, chip);
2424 if (status & NAND_STATUS_FAIL)
2425 return -EIO;
2426 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002427
William Juul52c07962007-10-31 13:53:06 +01002428 return 0;
2429}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002430
William Juul52c07962007-10-31 13:53:06 +01002431/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002432 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2433 * @mtd: MTD device structure
2434 * @oob: oob data buffer
2435 * @len: oob data write length
2436 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01002437 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002438static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2439 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01002440{
Scott Wood17fed142016-05-30 13:57:56 -05002441 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002442
2443 /*
2444 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2445 * data from a previous OOB read.
2446 */
2447 memset(chip->oob_poi, 0xff, mtd->oobsize);
2448
Christian Hitz13fc0e22011-10-12 09:32:01 +02002449 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002450
Sergey Lapin3a38a552013-01-14 03:46:50 +00002451 case MTD_OPS_PLACE_OOB:
2452 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002453 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2454 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002455
Sergey Lapin3a38a552013-01-14 03:46:50 +00002456 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002457 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2458 uint32_t boffs = 0, woffs = ops->ooboffs;
2459 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002460
Christian Hitz13fc0e22011-10-12 09:32:01 +02002461 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002462 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002463 if (unlikely(woffs)) {
2464 if (woffs >= free->length) {
2465 woffs -= free->length;
2466 continue;
2467 }
2468 boffs = free->offset + woffs;
2469 bytes = min_t(size_t, len,
2470 (free->length - woffs));
2471 woffs = 0;
2472 } else {
2473 bytes = min_t(size_t, len, free->length);
2474 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002475 }
William Juul52c07962007-10-31 13:53:06 +01002476 memcpy(chip->oob_poi + boffs, oob, bytes);
2477 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002478 }
William Juul52c07962007-10-31 13:53:06 +01002479 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002480 }
William Juul52c07962007-10-31 13:53:06 +01002481 default:
2482 BUG();
2483 }
2484 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002485}
2486
Christian Hitzb8a6b372011-10-12 09:32:02 +02002487#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002488
2489/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002490 * nand_do_write_ops - [INTERN] NAND write with ECC
2491 * @mtd: MTD device structure
2492 * @to: offset to write to
2493 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002494 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002495 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002496 */
William Juul52c07962007-10-31 13:53:06 +01002497static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2498 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002499{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002500 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05002501 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002502 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002503
2504 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002505 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002506
William Juul52c07962007-10-31 13:53:06 +01002507 uint8_t *oob = ops->oobbuf;
2508 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002509 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002510 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002511
William Juul52c07962007-10-31 13:53:06 +01002512 ops->retlen = 0;
2513 if (!writelen)
2514 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002515
Heiko Schocherf5895d12014-06-24 10:10:04 +02002516 /* Reject writes, which are not page aligned */
2517 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002518 pr_notice("%s: attempt to write non page aligned data\n",
2519 __func__);
William Juul52c07962007-10-31 13:53:06 +01002520 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002521 }
2522
2523 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002524
William Juul52c07962007-10-31 13:53:06 +01002525 chipnr = (int)(to >> chip->chip_shift);
2526 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002527
2528 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01002529 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002530 ret = -EIO;
2531 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01002532 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002533
William Juul52c07962007-10-31 13:53:06 +01002534 realpage = (int)(to >> chip->page_shift);
2535 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002536
William Juul52c07962007-10-31 13:53:06 +01002537 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002538 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2539 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01002540 chip->pagebuf = -1;
2541
Christian Hitzb8a6b372011-10-12 09:32:02 +02002542 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002543 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2544 ret = -EINVAL;
2545 goto err_out;
2546 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02002547
Christian Hitz13fc0e22011-10-12 09:32:01 +02002548 while (1) {
William Juul52c07962007-10-31 13:53:06 +01002549 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01002550 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002551 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02002552 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002553
2554 if (part_pagewr)
2555 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002556 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2557 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2558 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002559 else
2560 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01002561
Heiko Schocherf5895d12014-06-24 10:10:04 +02002562 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05002563 /* Partial page write?, or need to use bounce buffer */
2564 if (use_bufpoi) {
2565 pr_debug("%s: using write bounce buffer for buf@%p\n",
2566 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002567 if (part_pagewr)
2568 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01002569 chip->pagebuf = -1;
2570 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2571 memcpy(&chip->buffers->databuf[column], buf, bytes);
2572 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002573 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002574
Christian Hitzb8a6b372011-10-12 09:32:02 +02002575 if (unlikely(oob)) {
2576 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002577 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002578 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002579 } else {
2580 /* We still need to erase leftover OOB data */
2581 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002582 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002583 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002584 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002585 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01002586 if (ret)
2587 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002588
William Juul52c07962007-10-31 13:53:06 +01002589 writelen -= bytes;
2590 if (!writelen)
2591 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002592
Heiko Schocherf5895d12014-06-24 10:10:04 +02002593 column = 0;
2594 buf += bytes;
2595 realpage++;
2596
2597 page = realpage & chip->pagemask;
2598 /* Check, if we cross a chip boundary */
2599 if (!page) {
2600 chipnr++;
2601 chip->select_chip(mtd, -1);
2602 chip->select_chip(mtd, chipnr);
2603 }
2604 }
2605
2606 ops->retlen = ops->len - writelen;
2607 if (unlikely(oob))
2608 ops->oobretlen = ops->ooblen;
2609
2610err_out:
2611 chip->select_chip(mtd, -1);
2612 return ret;
2613}
2614
2615/**
2616 * panic_nand_write - [MTD Interface] NAND write with ECC
2617 * @mtd: MTD device structure
2618 * @to: offset to write to
2619 * @len: number of bytes to write
2620 * @retlen: pointer to variable to store the number of written bytes
2621 * @buf: the data to write
2622 *
2623 * NAND write with ECC. Used when performing writes in interrupt context, this
2624 * may for example be called by mtdoops when writing an oops while in panic.
2625 */
2626static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2627 size_t *retlen, const uint8_t *buf)
2628{
Scott Wood17fed142016-05-30 13:57:56 -05002629 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002630 struct mtd_oob_ops ops;
2631 int ret;
2632
2633 /* Wait for the device to get ready */
2634 panic_nand_wait(mtd, chip, 400);
2635
2636 /* Grab the device */
2637 panic_nand_get_device(chip, mtd, FL_WRITING);
2638
Scott Wood3ea94ed2015-06-26 19:03:26 -05002639 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02002640 ops.len = len;
2641 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002642 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01002643
Heiko Schocherf5895d12014-06-24 10:10:04 +02002644 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002645
Sergey Lapin3a38a552013-01-14 03:46:50 +00002646 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01002647 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002648}
2649
2650/**
William Juul52c07962007-10-31 13:53:06 +01002651 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002652 * @mtd: MTD device structure
2653 * @to: offset to write to
2654 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002655 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002656 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002657 */
William Juul52c07962007-10-31 13:53:06 +01002658static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2659 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002660{
William Juul52c07962007-10-31 13:53:06 +01002661 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05002662 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002663
Heiko Schocherf5895d12014-06-24 10:10:04 +02002664 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002665 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002666
Scott Wood52ab7ce2016-05-30 13:57:58 -05002667 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002668
2669 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002670 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002671 pr_debug("%s: attempt to write past end of page\n",
2672 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002673 return -EINVAL;
2674 }
2675
William Juul52c07962007-10-31 13:53:06 +01002676 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002677 pr_debug("%s: attempt to start write outside oob\n",
2678 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002679 return -EINVAL;
2680 }
2681
Christian Hitz13fc0e22011-10-12 09:32:01 +02002682 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002683 if (unlikely(to >= mtd->size ||
2684 ops->ooboffs + ops->ooblen >
2685 ((mtd->size >> chip->page_shift) -
2686 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002687 pr_debug("%s: attempt to write beyond end of device\n",
2688 __func__);
William Juul52c07962007-10-31 13:53:06 +01002689 return -EINVAL;
2690 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002691
William Juul52c07962007-10-31 13:53:06 +01002692 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01002693
2694 /*
2695 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2696 * of my DiskOnChip 2000 test units) will clear the whole data page too
2697 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2698 * it in the doc2000 driver in August 1999. dwmw2.
2699 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09002700 nand_reset(chip, chipnr);
2701
2702 chip->select_chip(mtd, chipnr);
2703
2704 /* Shift to get page */
2705 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002706
2707 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002708 if (nand_check_wp(mtd)) {
2709 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01002710 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002711 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002712
William Juul52c07962007-10-31 13:53:06 +01002713 /* Invalidate the page cache, if we write to the cached page */
2714 if (page == chip->pagebuf)
2715 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002716
Sergey Lapin3a38a552013-01-14 03:46:50 +00002717 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2718
2719 if (ops->mode == MTD_OPS_RAW)
2720 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2721 else
2722 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002723
Heiko Schocherf5895d12014-06-24 10:10:04 +02002724 chip->select_chip(mtd, -1);
2725
William Juul52c07962007-10-31 13:53:06 +01002726 if (status)
2727 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002728
William Juul52c07962007-10-31 13:53:06 +01002729 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002730
William Juul52c07962007-10-31 13:53:06 +01002731 return 0;
2732}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002733
William Juul52c07962007-10-31 13:53:06 +01002734/**
2735 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002736 * @mtd: MTD device structure
2737 * @to: offset to write to
2738 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01002739 */
2740static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2741 struct mtd_oob_ops *ops)
2742{
William Juul52c07962007-10-31 13:53:06 +01002743 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002744
William Juul52c07962007-10-31 13:53:06 +01002745 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002746
William Juul52c07962007-10-31 13:53:06 +01002747 /* Do not allow writes past end of device */
2748 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002749 pr_debug("%s: attempt to write beyond end of device\n",
2750 __func__);
William Juul52c07962007-10-31 13:53:06 +01002751 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002752 }
William Juul52c07962007-10-31 13:53:06 +01002753
Heiko Schocherf5895d12014-06-24 10:10:04 +02002754 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01002755
Christian Hitz13fc0e22011-10-12 09:32:01 +02002756 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002757 case MTD_OPS_PLACE_OOB:
2758 case MTD_OPS_AUTO_OOB:
2759 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002760 break;
2761
2762 default:
2763 goto out;
2764 }
2765
2766 if (!ops->datbuf)
2767 ret = nand_do_write_oob(mtd, to, ops);
2768 else
2769 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002770
Christian Hitz13fc0e22011-10-12 09:32:01 +02002771out:
William Juul52c07962007-10-31 13:53:06 +01002772 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002773 return ret;
2774}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002775
2776/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002777 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00002778 * @mtd: MTD device structure
2779 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002780 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05002781 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002783static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002784{
Scott Wood17fed142016-05-30 13:57:56 -05002785 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002786 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002787 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2788 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002789
2790 return chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002791}
2792
2793/**
2794 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002795 * @mtd: MTD device structure
2796 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002797 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002798 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002799 */
William Juul52c07962007-10-31 13:53:06 +01002800static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002801{
William Juul52c07962007-10-31 13:53:06 +01002802 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002803}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002804
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002805/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002806 * nand_erase_nand - [INTERN] erase block(s)
2807 * @mtd: MTD device structure
2808 * @instr: erase instruction
2809 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002810 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002811 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002812 */
William Juul52c07962007-10-31 13:53:06 +01002813int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2814 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002815{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002816 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002817 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002818 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002819
Heiko Schocherf5895d12014-06-24 10:10:04 +02002820 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2821 __func__, (unsigned long long)instr->addr,
2822 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002823
Christian Hitzb8a6b372011-10-12 09:32:02 +02002824 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002825 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002826
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002827 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002828 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002829
2830 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002831 page = (int)(instr->addr >> chip->page_shift);
2832 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002833
2834 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002835 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002836
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002837 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002838 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002839
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002840 /* Check, if it is write protected */
2841 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002842 pr_debug("%s: device is write protected!\n",
2843 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002844 instr->state = MTD_ERASE_FAILED;
2845 goto erase_exit;
2846 }
2847
2848 /* Loop through the pages */
2849 len = instr->len;
2850
2851 instr->state = MTD_ERASING;
2852
2853 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002854 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002855
Sergey Lapin3a38a552013-01-14 03:46:50 +00002856 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09002857 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05002858 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002859 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002860 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002861 instr->state = MTD_ERASE_FAILED;
2862 goto erase_exit;
2863 }
William Juul52c07962007-10-31 13:53:06 +01002864
2865 /*
2866 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00002867 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01002868 */
2869 if (page <= chip->pagebuf && chip->pagebuf <
2870 (page + pages_per_block))
2871 chip->pagebuf = -1;
2872
Scott Wood3ea94ed2015-06-26 19:03:26 -05002873 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002874
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002875 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002876 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002877 pr_debug("%s: failed erase, page 0x%08x\n",
2878 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002879 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002880 instr->fail_addr =
2881 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002882 goto erase_exit;
2883 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002884
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002885 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002886 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002887 page += pages_per_block;
2888
2889 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002890 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002891 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002892 chip->select_chip(mtd, -1);
2893 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002894 }
2895 }
2896 instr->state = MTD_ERASE_DONE;
2897
Christian Hitz13fc0e22011-10-12 09:32:01 +02002898erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002899
2900 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002901
2902 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002903 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002904 nand_release_device(mtd);
2905
Scott Wood3628f002008-10-24 16:20:43 -05002906 /* Do call back function */
2907 if (!ret)
2908 mtd_erase_callback(instr);
2909
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002910 /* Return more or less happy */
2911 return ret;
2912}
2913
2914/**
2915 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00002916 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002917 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002918 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002919 */
William Juul52c07962007-10-31 13:53:06 +01002920static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002921{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002922 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002923
2924 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002925 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002926 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002927 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002928}
2929
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002930/**
William Juul52c07962007-10-31 13:53:06 +01002931 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002932 * @mtd: MTD device structure
2933 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002934 */
William Juul52c07962007-10-31 13:53:06 +01002935static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002936{
Scott Wood52ab7ce2016-05-30 13:57:58 -05002937 struct nand_chip *chip = mtd_to_nand(mtd);
2938 int chipnr = (int)(offs >> chip->chip_shift);
2939 int ret;
2940
2941 /* Select the NAND device */
2942 nand_get_device(mtd, FL_READING);
2943 chip->select_chip(mtd, chipnr);
2944
2945 ret = nand_block_checkbad(mtd, offs, 0);
2946
2947 chip->select_chip(mtd, -1);
2948 nand_release_device(mtd);
2949
2950 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002951}
2952
2953/**
William Juul52c07962007-10-31 13:53:06 +01002954 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002955 * @mtd: MTD device structure
2956 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002957 */
William Juul52c07962007-10-31 13:53:06 +01002958static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002959{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002960 int ret;
2961
Christian Hitzb8a6b372011-10-12 09:32:02 +02002962 ret = nand_block_isbad(mtd, ofs);
2963 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002964 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002965 if (ret > 0)
2966 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002967 return ret;
2968 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002969
Heiko Schocherf5895d12014-06-24 10:10:04 +02002970 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002971}
2972
Heiko Schocherf5895d12014-06-24 10:10:04 +02002973/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002974 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2975 * @mtd: MTD device structure
2976 * @chip: nand chip info structure
2977 * @addr: feature address.
2978 * @subfeature_param: the subfeature parameters, a four bytes array.
2979 */
2980static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2981 int addr, uint8_t *subfeature_param)
2982{
2983 int status;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002984 int i;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002985
Heiko Schocherf5895d12014-06-24 10:10:04 +02002986#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2987 if (!chip->onfi_version ||
2988 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2989 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02002990 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002991#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00002992
2993 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002994 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2995 chip->write_byte(mtd, subfeature_param[i]);
2996
Sergey Lapin3a38a552013-01-14 03:46:50 +00002997 status = chip->waitfunc(mtd, chip);
2998 if (status & NAND_STATUS_FAIL)
2999 return -EIO;
3000 return 0;
3001}
3002
3003/**
3004 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3005 * @mtd: MTD device structure
3006 * @chip: nand chip info structure
3007 * @addr: feature address.
3008 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003009 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003010static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3011 int addr, uint8_t *subfeature_param)
3012{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003013 int i;
3014
3015#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3016 if (!chip->onfi_version ||
3017 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3018 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003019 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003020#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003021
Sergey Lapin3a38a552013-01-14 03:46:50 +00003022 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003023 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3024 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003025 return 0;
3026}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003027
Sergey Lapin3a38a552013-01-14 03:46:50 +00003028/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003029static void nand_set_defaults(struct nand_chip *chip, int busw)
3030{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003031 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003032 if (!chip->chip_delay)
3033 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003034
3035 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003036 if (chip->cmdfunc == NULL)
3037 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003038
3039 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003040 if (chip->waitfunc == NULL)
3041 chip->waitfunc = nand_wait;
3042
3043 if (!chip->select_chip)
3044 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003045
3046 /* set for ONFI nand */
3047 if (!chip->onfi_set_features)
3048 chip->onfi_set_features = nand_onfi_set_features;
3049 if (!chip->onfi_get_features)
3050 chip->onfi_get_features = nand_onfi_get_features;
3051
3052 /* If called twice, pointers that depend on busw may need to be reset */
3053 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003054 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3055 if (!chip->read_word)
3056 chip->read_word = nand_read_word;
3057 if (!chip->block_bad)
3058 chip->block_bad = nand_block_bad;
3059 if (!chip->block_markbad)
3060 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003061 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003062 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003063 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3064 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3065 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003066 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003067 if (!chip->scan_bbt)
3068 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003069
3070 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003071 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003072 spin_lock_init(&chip->controller->lock);
3073 init_waitqueue_head(&chip->controller->wq);
3074 }
3075
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003076 if (!chip->buf_align)
3077 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003078}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003079
Sergey Lapin3a38a552013-01-14 03:46:50 +00003080/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003081static void sanitize_string(char *s, size_t len)
3082{
3083 ssize_t i;
3084
Sergey Lapin3a38a552013-01-14 03:46:50 +00003085 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003086 s[len - 1] = 0;
3087
Sergey Lapin3a38a552013-01-14 03:46:50 +00003088 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003089 for (i = 0; i < len - 1; i++) {
3090 if (s[i] < ' ' || s[i] > 127)
3091 s[i] = '?';
3092 }
3093
Sergey Lapin3a38a552013-01-14 03:46:50 +00003094 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003095 strim(s);
3096}
3097
Florian Fainellic98a9352011-02-25 00:01:34 +00003098static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3099{
3100 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003101 while (len--) {
3102 crc ^= *p++ << 8;
3103 for (i = 0; i < 8; i++)
3104 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3105 }
3106
3107 return crc;
3108}
3109
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003110#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003111/* Parse the Extended Parameter Page. */
3112static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3113 struct nand_chip *chip, struct nand_onfi_params *p)
3114{
3115 struct onfi_ext_param_page *ep;
3116 struct onfi_ext_section *s;
3117 struct onfi_ext_ecc_info *ecc;
3118 uint8_t *cursor;
3119 int ret = -EINVAL;
3120 int len;
3121 int i;
3122
3123 len = le16_to_cpu(p->ext_param_page_length) * 16;
3124 ep = kmalloc(len, GFP_KERNEL);
3125 if (!ep)
3126 return -ENOMEM;
3127
3128 /* Send our own NAND_CMD_PARAM. */
3129 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3130
3131 /* Use the Change Read Column command to skip the ONFI param pages. */
3132 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3133 sizeof(*p) * p->num_of_param_pages , -1);
3134
3135 /* Read out the Extended Parameter Page. */
3136 chip->read_buf(mtd, (uint8_t *)ep, len);
3137 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3138 != le16_to_cpu(ep->crc))) {
3139 pr_debug("fail in the CRC.\n");
3140 goto ext_out;
3141 }
3142
3143 /*
3144 * Check the signature.
3145 * Do not strictly follow the ONFI spec, maybe changed in future.
3146 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003147 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003148 pr_debug("The signature is invalid.\n");
3149 goto ext_out;
3150 }
3151
3152 /* find the ECC section. */
3153 cursor = (uint8_t *)(ep + 1);
3154 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3155 s = ep->sections + i;
3156 if (s->type == ONFI_SECTION_TYPE_2)
3157 break;
3158 cursor += s->length * 16;
3159 }
3160 if (i == ONFI_EXT_SECTION_MAX) {
3161 pr_debug("We can not find the ECC section.\n");
3162 goto ext_out;
3163 }
3164
3165 /* get the info we want. */
3166 ecc = (struct onfi_ext_ecc_info *)cursor;
3167
3168 if (!ecc->codeword_size) {
3169 pr_debug("Invalid codeword size\n");
3170 goto ext_out;
3171 }
3172
3173 chip->ecc_strength_ds = ecc->ecc_bits;
3174 chip->ecc_step_ds = 1 << ecc->codeword_size;
3175 ret = 0;
3176
3177ext_out:
3178 kfree(ep);
3179 return ret;
3180}
3181
3182static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3183{
Scott Wood17fed142016-05-30 13:57:56 -05003184 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003185 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3186
3187 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3188 feature);
3189}
3190
3191/*
3192 * Configure chip properties from Micron vendor-specific ONFI table
3193 */
3194static void nand_onfi_detect_micron(struct nand_chip *chip,
3195 struct nand_onfi_params *p)
3196{
3197 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3198
3199 if (le16_to_cpu(p->vendor_revision) < 1)
3200 return;
3201
3202 chip->read_retries = micron->read_retry_options;
3203 chip->setup_read_retry = nand_setup_read_retry_micron;
3204}
3205
Florian Fainellic98a9352011-02-25 00:01:34 +00003206/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003207 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003208 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003209static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003210 int *busw)
3211{
3212 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisf3832302014-05-06 00:46:16 +05303213 int i, j;
Florian Fainellic98a9352011-02-25 00:01:34 +00003214 int val;
3215
Sergey Lapin3a38a552013-01-14 03:46:50 +00003216 /* Try ONFI for unknown chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00003217 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3218 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3219 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3220 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003221
Florian Fainellic98a9352011-02-25 00:01:34 +00003222 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3223 for (i = 0; i < 3; i++) {
Brian Norrisf3832302014-05-06 00:46:16 +05303224 for (j = 0; j < sizeof(*p); j++)
3225 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003226 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003227 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003228 break;
3229 }
3230 }
3231
Heiko Schocherf5895d12014-06-24 10:10:04 +02003232 if (i == 3) {
3233 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003234 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003235 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003236
Sergey Lapin3a38a552013-01-14 03:46:50 +00003237 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003238 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003239 if (val & (1 << 5))
3240 chip->onfi_version = 23;
3241 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003242 chip->onfi_version = 22;
3243 else if (val & (1 << 3))
3244 chip->onfi_version = 21;
3245 else if (val & (1 << 2))
3246 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003247 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003248 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003249
3250 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003251 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003252 return 0;
3253 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003254
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003255 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3256 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003257 if (!mtd->name)
3258 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003259
Florian Fainellic98a9352011-02-25 00:01:34 +00003260 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003261
3262 /*
3263 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3264 * (don't ask me who thought of this...). MTD assumes that these
3265 * dimensions will be power-of-2, so just truncate the remaining area.
3266 */
3267 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3268 mtd->erasesize *= mtd->writesize;
3269
Florian Fainellic98a9352011-02-25 00:01:34 +00003270 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003271
3272 /* See erasesize comment */
3273 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003274 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003275 chip->bits_per_cell = p->bits_per_cell;
3276
3277 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003278 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003279 else
3280 *busw = 0;
3281
3282 if (p->ecc_bits != 0xff) {
3283 chip->ecc_strength_ds = p->ecc_bits;
3284 chip->ecc_step_ds = 512;
3285 } else if (chip->onfi_version >= 21 &&
3286 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3287
3288 /*
3289 * The nand_flash_detect_ext_param_page() uses the
3290 * Change Read Column command which maybe not supported
3291 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3292 * now. We do not replace user supplied command function.
3293 */
3294 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3295 chip->cmdfunc = nand_command_lp;
3296
3297 /* The Extended Parameter Page is supported since ONFI 2.1. */
3298 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3299 pr_warn("Failed to detect ONFI extended param page\n");
3300 } else {
3301 pr_warn("Could not retrieve ONFI ECC requirements\n");
3302 }
3303
3304 if (p->jedec_id == NAND_MFR_MICRON)
3305 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003306
3307 return 1;
3308}
3309#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003310static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003311 int *busw)
3312{
3313 return 0;
3314}
3315#endif
3316
William Juul52c07962007-10-31 13:53:06 +01003317/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003318 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3319 */
3320static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3321 int *busw)
3322{
3323 struct nand_jedec_params *p = &chip->jedec_params;
3324 struct jedec_ecc_info *ecc;
3325 int val;
3326 int i, j;
3327
3328 /* Try JEDEC for unknown chip or LP */
3329 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3330 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3331 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3332 chip->read_byte(mtd) != 'C')
3333 return 0;
3334
3335 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3336 for (i = 0; i < 3; i++) {
3337 for (j = 0; j < sizeof(*p); j++)
3338 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3339
3340 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3341 le16_to_cpu(p->crc))
3342 break;
3343 }
3344
3345 if (i == 3) {
3346 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3347 return 0;
3348 }
3349
3350 /* Check version */
3351 val = le16_to_cpu(p->revision);
3352 if (val & (1 << 2))
3353 chip->jedec_version = 10;
3354 else if (val & (1 << 1))
3355 chip->jedec_version = 1; /* vendor specific version */
3356
3357 if (!chip->jedec_version) {
3358 pr_info("unsupported JEDEC version: %d\n", val);
3359 return 0;
3360 }
3361
3362 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3363 sanitize_string(p->model, sizeof(p->model));
3364 if (!mtd->name)
3365 mtd->name = p->model;
3366
3367 mtd->writesize = le32_to_cpu(p->byte_per_page);
3368
3369 /* Please reference to the comment for nand_flash_detect_onfi. */
3370 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3371 mtd->erasesize *= mtd->writesize;
3372
3373 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3374
3375 /* Please reference to the comment for nand_flash_detect_onfi. */
3376 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3377 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3378 chip->bits_per_cell = p->bits_per_cell;
3379
3380 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3381 *busw = NAND_BUSWIDTH_16;
3382 else
3383 *busw = 0;
3384
3385 /* ECC info */
3386 ecc = &p->ecc_info[0];
3387
3388 if (ecc->codeword_size >= 9) {
3389 chip->ecc_strength_ds = ecc->ecc_bits;
3390 chip->ecc_step_ds = 1 << ecc->codeword_size;
3391 } else {
3392 pr_warn("Invalid codeword size\n");
3393 }
3394
3395 return 1;
3396}
3397
3398/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003399 * nand_id_has_period - Check if an ID string has a given wraparound period
3400 * @id_data: the ID string
3401 * @arrlen: the length of the @id_data array
3402 * @period: the period of repitition
3403 *
3404 * Check if an ID string is repeated within a given sequence of bytes at
3405 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02003406 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00003407 * if the repetition has a period of @period; otherwise, returns zero.
3408 */
3409static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3410{
3411 int i, j;
3412 for (i = 0; i < period; i++)
3413 for (j = i + period; j < arrlen; j += period)
3414 if (id_data[i] != id_data[j])
3415 return 0;
3416 return 1;
3417}
3418
3419/*
3420 * nand_id_len - Get the length of an ID string returned by CMD_READID
3421 * @id_data: the ID string
3422 * @arrlen: the length of the @id_data array
3423
3424 * Returns the length of the ID string, according to known wraparound/trailing
3425 * zero patterns. If no pattern exists, returns the length of the array.
3426 */
3427static int nand_id_len(u8 *id_data, int arrlen)
3428{
3429 int last_nonzero, period;
3430
3431 /* Find last non-zero byte */
3432 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3433 if (id_data[last_nonzero])
3434 break;
3435
3436 /* All zeros */
3437 if (last_nonzero < 0)
3438 return 0;
3439
3440 /* Calculate wraparound period */
3441 for (period = 1; period < arrlen; period++)
3442 if (nand_id_has_period(id_data, arrlen, period))
3443 break;
3444
3445 /* There's a repeated pattern */
3446 if (period < arrlen)
3447 return period;
3448
3449 /* There are trailing zeros */
3450 if (last_nonzero < arrlen - 1)
3451 return last_nonzero + 1;
3452
3453 /* No pattern detected */
3454 return arrlen;
3455}
3456
Heiko Schocherf5895d12014-06-24 10:10:04 +02003457/* Extract the bits of per cell from the 3rd byte of the extended ID */
3458static int nand_get_bits_per_cell(u8 cellinfo)
3459{
3460 int bits;
3461
3462 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3463 bits >>= NAND_CI_CELLTYPE_SHIFT;
3464 return bits + 1;
3465}
3466
Sergey Lapin3a38a552013-01-14 03:46:50 +00003467/*
3468 * Many new NAND share similar device ID codes, which represent the size of the
3469 * chip. The rest of the parameters must be decoded according to generic or
3470 * manufacturer-specific "extended ID" decoding patterns.
3471 */
3472static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3473 u8 id_data[8], int *busw)
3474{
3475 int extid, id_len;
3476 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003477 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003478 /* The 4th id byte is the important one */
3479 extid = id_data[3];
3480
3481 id_len = nand_id_len(id_data, 8);
3482
3483 /*
3484 * Field definitions are in the following datasheets:
3485 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3486 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3487 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3488 *
3489 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3490 * ID to decide what to do.
3491 */
3492 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003493 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003494 /* Calc pagesize */
3495 mtd->writesize = 2048 << (extid & 0x03);
3496 extid >>= 2;
3497 /* Calc oobsize */
3498 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3499 case 1:
3500 mtd->oobsize = 128;
3501 break;
3502 case 2:
3503 mtd->oobsize = 218;
3504 break;
3505 case 3:
3506 mtd->oobsize = 400;
3507 break;
3508 case 4:
3509 mtd->oobsize = 436;
3510 break;
3511 case 5:
3512 mtd->oobsize = 512;
3513 break;
3514 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003515 mtd->oobsize = 640;
3516 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003517 case 7:
3518 default: /* Other cases are "reserved" (unknown) */
3519 mtd->oobsize = 1024;
3520 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003521 }
3522 extid >>= 2;
3523 /* Calc blocksize */
3524 mtd->erasesize = (128 * 1024) <<
3525 (((extid >> 1) & 0x04) | (extid & 0x03));
3526 *busw = 0;
3527 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003528 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003529 unsigned int tmp;
3530
3531 /* Calc pagesize */
3532 mtd->writesize = 2048 << (extid & 0x03);
3533 extid >>= 2;
3534 /* Calc oobsize */
3535 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3536 case 0:
3537 mtd->oobsize = 128;
3538 break;
3539 case 1:
3540 mtd->oobsize = 224;
3541 break;
3542 case 2:
3543 mtd->oobsize = 448;
3544 break;
3545 case 3:
3546 mtd->oobsize = 64;
3547 break;
3548 case 4:
3549 mtd->oobsize = 32;
3550 break;
3551 case 5:
3552 mtd->oobsize = 16;
3553 break;
3554 default:
3555 mtd->oobsize = 640;
3556 break;
3557 }
3558 extid >>= 2;
3559 /* Calc blocksize */
3560 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3561 if (tmp < 0x03)
3562 mtd->erasesize = (128 * 1024) << tmp;
3563 else if (tmp == 0x03)
3564 mtd->erasesize = 768 * 1024;
3565 else
3566 mtd->erasesize = (64 * 1024) << tmp;
3567 *busw = 0;
3568 } else {
3569 /* Calc pagesize */
3570 mtd->writesize = 1024 << (extid & 0x03);
3571 extid >>= 2;
3572 /* Calc oobsize */
3573 mtd->oobsize = (8 << (extid & 0x01)) *
3574 (mtd->writesize >> 9);
3575 extid >>= 2;
3576 /* Calc blocksize. Blocksize is multiples of 64KiB */
3577 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3578 extid >>= 2;
3579 /* Get buswidth information */
3580 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003581
3582 /*
3583 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3584 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3585 * follows:
3586 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3587 * 110b -> 24nm
3588 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3589 */
3590 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3591 nand_is_slc(chip) &&
3592 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3593 !(id_data[4] & 0x80) /* !BENAND */) {
3594 mtd->oobsize = 32 * mtd->writesize >> 9;
3595 }
3596
Sergey Lapin3a38a552013-01-14 03:46:50 +00003597 }
3598}
3599
Heiko Schocherf5895d12014-06-24 10:10:04 +02003600/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003601 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3602 * decodes a matching ID table entry and assigns the MTD size parameters for
3603 * the chip.
3604 */
3605static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003606 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00003607 int *busw)
3608{
3609 int maf_id = id_data[0];
3610
3611 mtd->erasesize = type->erasesize;
3612 mtd->writesize = type->pagesize;
3613 mtd->oobsize = mtd->writesize / 32;
3614 *busw = type->options & NAND_BUSWIDTH_16;
3615
Heiko Schocherf5895d12014-06-24 10:10:04 +02003616 /* All legacy ID NAND are small-page, SLC */
3617 chip->bits_per_cell = 1;
3618
Sergey Lapin3a38a552013-01-14 03:46:50 +00003619 /*
3620 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3621 * some Spansion chips have erasesize that conflicts with size
3622 * listed in nand_ids table.
3623 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3624 */
3625 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3626 && id_data[6] == 0x00 && id_data[7] == 0x00
3627 && mtd->writesize == 512) {
3628 mtd->erasesize = 128 * 1024;
3629 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3630 }
3631}
3632
Heiko Schocherf5895d12014-06-24 10:10:04 +02003633/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003634 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3635 * heuristic patterns using various detected parameters (e.g., manufacturer,
3636 * page size, cell-type information).
3637 */
3638static void nand_decode_bbm_options(struct mtd_info *mtd,
3639 struct nand_chip *chip, u8 id_data[8])
3640{
3641 int maf_id = id_data[0];
3642
3643 /* Set the bad block position */
3644 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3645 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3646 else
3647 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3648
3649 /*
3650 * Bad block marker is stored in the last page of each block on Samsung
3651 * and Hynix MLC devices; stored in first two pages of each block on
3652 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3653 * AMD/Spansion, and Macronix. All others scan only the first page.
3654 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003655 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003656 (maf_id == NAND_MFR_SAMSUNG ||
3657 maf_id == NAND_MFR_HYNIX))
3658 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003659 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003660 (maf_id == NAND_MFR_SAMSUNG ||
3661 maf_id == NAND_MFR_HYNIX ||
3662 maf_id == NAND_MFR_TOSHIBA ||
3663 maf_id == NAND_MFR_AMD ||
3664 maf_id == NAND_MFR_MACRONIX)) ||
3665 (mtd->writesize == 2048 &&
3666 maf_id == NAND_MFR_MICRON))
3667 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3668}
3669
Heiko Schocherf5895d12014-06-24 10:10:04 +02003670static inline bool is_full_id_nand(struct nand_flash_dev *type)
3671{
3672 return type->id_len;
3673}
3674
3675static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3676 struct nand_flash_dev *type, u8 *id_data, int *busw)
3677{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003678 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003679 mtd->writesize = type->pagesize;
3680 mtd->erasesize = type->erasesize;
3681 mtd->oobsize = type->oobsize;
3682
3683 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3684 chip->chipsize = (uint64_t)type->chipsize << 20;
3685 chip->options |= type->options;
3686 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3687 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003688 chip->onfi_timing_mode_default =
3689 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003690
3691 *busw = type->options & NAND_BUSWIDTH_16;
3692
3693 if (!mtd->name)
3694 mtd->name = type->name;
3695
3696 return true;
3697 }
3698 return false;
3699}
3700
Sergey Lapin3a38a552013-01-14 03:46:50 +00003701/*
3702 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01003703 */
Jörg Krause929fb442018-01-14 19:26:37 +01003704struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01003705 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003706 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003707 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01003708{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003709 int busw;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003710 int i, maf_idx;
3711 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003712
Karl Beldanb6322fc2008-09-15 16:08:03 +02003713 /*
3714 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003715 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02003716 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003717 nand_reset(chip, 0);
3718
3719 /* Select the device */
3720 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02003721
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003722 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01003723 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003724
3725 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003726 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003727 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003728
Sergey Lapin3a38a552013-01-14 03:46:50 +00003729 /*
3730 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05003731 * interface concerns can cause random data which looks like a
3732 * possibly credible NAND flash to appear. If the two results do
3733 * not match, ignore the device completely.
3734 */
3735
3736 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3737
Sergey Lapin3a38a552013-01-14 03:46:50 +00003738 /* Read entire ID string */
3739 for (i = 0; i < 8; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02003740 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05003741
Christian Hitzb8a6b372011-10-12 09:32:02 +02003742 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003743 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003744 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05003745 return ERR_PTR(-ENODEV);
3746 }
3747
Lei Wen75bde942011-01-06 09:48:18 +08003748 if (!type)
3749 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003750
Heiko Schocherf5895d12014-06-24 10:10:04 +02003751 for (; type->name != NULL; type++) {
3752 if (is_full_id_nand(type)) {
3753 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3754 goto ident_done;
3755 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05003756 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003757 }
3758 }
Lei Wen75bde942011-01-06 09:48:18 +08003759
Christian Hitzb8a6b372011-10-12 09:32:02 +02003760 chip->onfi_version = 0;
3761 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05003762 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003763 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02003764 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003765
3766 /* Check if the chip is JEDEC compliant */
3767 if (nand_flash_detect_jedec(mtd, chip, &busw))
3768 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02003769 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003770
Christian Hitzb8a6b372011-10-12 09:32:02 +02003771 if (!type->name)
3772 return ERR_PTR(-ENODEV);
3773
William Juul52c07962007-10-31 13:53:06 +01003774 if (!mtd->name)
3775 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003776
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003777 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003778
Scott Wood52ab7ce2016-05-30 13:57:58 -05003779 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003780 /* Decode parameters from extended ID */
3781 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003782 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003783 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003784 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003785 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00003786 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00003787
Sergey Lapin3a38a552013-01-14 03:46:50 +00003788 /*
3789 * Check if chip is not a Samsung device. Do not clear the
3790 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02003791 */
3792 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3793 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3794ident_done:
3795
William Juul52c07962007-10-31 13:53:06 +01003796 /* Try to identify manufacturer */
3797 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3798 if (nand_manuf_ids[maf_idx].id == *maf_id)
3799 break;
3800 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003801
Heiko Schocherf5895d12014-06-24 10:10:04 +02003802 if (chip->options & NAND_BUSWIDTH_AUTO) {
3803 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3804 chip->options |= busw;
3805 nand_set_defaults(chip, busw);
3806 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3807 /*
3808 * Check, if buswidth is correct. Hardware drivers should set
3809 * chip correct!
3810 */
3811 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3812 *maf_id, *dev_id);
3813 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3814 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003815 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3816 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01003817 return ERR_PTR(-EINVAL);
3818 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003819
Sergey Lapin3a38a552013-01-14 03:46:50 +00003820 nand_decode_bbm_options(mtd, chip, id_data);
3821
William Juul52c07962007-10-31 13:53:06 +01003822 /* Calculate the address shift from the page size */
3823 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003824 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01003825 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003826
William Juul52c07962007-10-31 13:53:06 +01003827 chip->bbt_erase_shift = chip->phys_erase_shift =
3828 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003829 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05003830 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003831 else {
3832 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3833 chip->chip_shift += 32 - 1;
3834 }
3835
Masahiro Yamada984926b2017-11-22 02:38:31 +09003836 if (chip->chip_shift - chip->page_shift > 16)
3837 chip->options |= NAND_ROW_ADDR_3;
3838
Christian Hitzb8a6b372011-10-12 09:32:02 +02003839 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003840 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003841
Sergey Lapin3a38a552013-01-14 03:46:50 +00003842 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01003843 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3844 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003845
Heiko Schocherf5895d12014-06-24 10:10:04 +02003846 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3847 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003848
Christian Hitzb8a6b372011-10-12 09:32:02 +02003849#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003850 if (chip->onfi_version)
3851 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3852 chip->onfi_params.model);
3853 else if (chip->jedec_version)
3854 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3855 chip->jedec_params.model);
3856 else
3857 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3858 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003859#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003860 if (chip->jedec_version)
3861 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3862 chip->jedec_params.model);
3863 else
3864 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3865 type->name);
3866
3867 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3868 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003869#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003870
Scott Wood3ea94ed2015-06-26 19:03:26 -05003871 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003872 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05003873 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01003874 return type;
3875}
Jörg Krause929fb442018-01-14 19:26:37 +01003876EXPORT_SYMBOL(nand_get_flash_type);
William Juul52c07962007-10-31 13:53:06 +01003877
Brian Norrisba6463d2016-06-15 21:09:22 +02003878#if CONFIG_IS_ENABLED(OF_CONTROL)
3879DECLARE_GLOBAL_DATA_PTR;
3880
3881static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3882{
3883 int ret, ecc_mode = -1, ecc_strength, ecc_step;
3884 const void *blob = gd->fdt_blob;
3885 const char *str;
3886
3887 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3888 if (ret == 16)
3889 chip->options |= NAND_BUSWIDTH_16;
3890
3891 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3892 chip->bbt_options |= NAND_BBT_USE_FLASH;
3893
3894 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3895 if (str) {
3896 if (!strcmp(str, "none"))
3897 ecc_mode = NAND_ECC_NONE;
3898 else if (!strcmp(str, "soft"))
3899 ecc_mode = NAND_ECC_SOFT;
3900 else if (!strcmp(str, "hw"))
3901 ecc_mode = NAND_ECC_HW;
3902 else if (!strcmp(str, "hw_syndrome"))
3903 ecc_mode = NAND_ECC_HW_SYNDROME;
3904 else if (!strcmp(str, "hw_oob_first"))
3905 ecc_mode = NAND_ECC_HW_OOB_FIRST;
3906 else if (!strcmp(str, "soft_bch"))
3907 ecc_mode = NAND_ECC_SOFT_BCH;
3908 }
3909
3910
3911 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3912 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3913
3914 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3915 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3916 pr_err("must set both strength and step size in DT\n");
3917 return -EINVAL;
3918 }
3919
3920 if (ecc_mode >= 0)
3921 chip->ecc.mode = ecc_mode;
3922
3923 if (ecc_strength >= 0)
3924 chip->ecc.strength = ecc_strength;
3925
3926 if (ecc_step > 0)
3927 chip->ecc.size = ecc_step;
3928
Boris Brezillonf1a54b02017-11-22 02:38:13 +09003929 if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
3930 chip->ecc.options |= NAND_ECC_MAXIMIZE;
3931
Brian Norrisba6463d2016-06-15 21:09:22 +02003932 return 0;
3933}
3934#else
3935static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3936{
3937 return 0;
3938}
3939#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3940
William Juul52c07962007-10-31 13:53:06 +01003941/**
3942 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003943 * @mtd: MTD device structure
3944 * @maxchips: number of chips to scan for
3945 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01003946 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003947 * This is the first phase of the normal nand_scan() function. It reads the
3948 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01003949 *
William Juul52c07962007-10-31 13:53:06 +01003950 */
Lei Wen75bde942011-01-06 09:48:18 +08003951int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003952 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01003953{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003954 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05003955 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003956 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02003957 int ret;
3958
3959 if (chip->flash_node) {
3960 ret = nand_dt_init(mtd, chip, chip->flash_node);
3961 if (ret)
3962 return ret;
3963 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003964
William Juul52c07962007-10-31 13:53:06 +01003965 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003966 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01003967
3968 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003969 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3970 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01003971
3972 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003973 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3974 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01003975 chip->select_chip(mtd, -1);
3976 return PTR_ERR(type);
3977 }
3978
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003979 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09003980 ret = nand_init_data_interface(chip);
3981 if (ret)
3982 return ret;
3983
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003984 /*
3985 * Setup the data interface correctly on the chip and controller side.
3986 * This explicit call to nand_setup_data_interface() is only required
3987 * for the first die, because nand_reset() has been called before
3988 * ->data_interface and ->default_onfi_timing_mode were set.
3989 * For the other dies, nand_reset() will automatically switch to the
3990 * best mode for us.
3991 */
Boris Brezillon32935f42017-11-22 02:38:28 +09003992 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003993 if (ret)
3994 return ret;
3995
Heiko Schocherf5895d12014-06-24 10:10:04 +02003996 chip->select_chip(mtd, -1);
3997
William Juul52c07962007-10-31 13:53:06 +01003998 /* Check for a chip array */
3999 for (i = 1; i < maxchips; i++) {
Karl Beldanb6322fc2008-09-15 16:08:03 +02004000 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004001 nand_reset(chip, i);
4002
4003 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004004 /* Send the command for reading device ID */
4005 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004006 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01004007 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherf5895d12014-06-24 10:10:04 +02004008 nand_dev_id != chip->read_byte(mtd)) {
4009 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004010 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004011 }
4012 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004013 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004014
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004015#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004016 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004017 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004018#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004019
William Juul52c07962007-10-31 13:53:06 +01004020 /* Store the number of chips and calc total size for mtd */
4021 chip->numchips = i;
4022 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004023
William Juul52c07962007-10-31 13:53:06 +01004024 return 0;
4025}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004026EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004027
Masahiro Yamada820eb482017-11-22 02:38:29 +09004028/**
4029 * nand_check_ecc_caps - check the sanity of preset ECC settings
4030 * @chip: nand chip info structure
4031 * @caps: ECC caps info structure
4032 * @oobavail: OOB size that the ECC engine can use
4033 *
4034 * When ECC step size and strength are already set, check if they are supported
4035 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4036 * On success, the calculated ECC bytes is set.
4037 */
4038int nand_check_ecc_caps(struct nand_chip *chip,
4039 const struct nand_ecc_caps *caps, int oobavail)
4040{
4041 struct mtd_info *mtd = nand_to_mtd(chip);
4042 const struct nand_ecc_step_info *stepinfo;
4043 int preset_step = chip->ecc.size;
4044 int preset_strength = chip->ecc.strength;
4045 int nsteps, ecc_bytes;
4046 int i, j;
4047
4048 if (WARN_ON(oobavail < 0))
4049 return -EINVAL;
4050
4051 if (!preset_step || !preset_strength)
4052 return -ENODATA;
4053
4054 nsteps = mtd->writesize / preset_step;
4055
4056 for (i = 0; i < caps->nstepinfos; i++) {
4057 stepinfo = &caps->stepinfos[i];
4058
4059 if (stepinfo->stepsize != preset_step)
4060 continue;
4061
4062 for (j = 0; j < stepinfo->nstrengths; j++) {
4063 if (stepinfo->strengths[j] != preset_strength)
4064 continue;
4065
4066 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4067 preset_strength);
4068 if (WARN_ON_ONCE(ecc_bytes < 0))
4069 return ecc_bytes;
4070
4071 if (ecc_bytes * nsteps > oobavail) {
4072 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4073 preset_step, preset_strength);
4074 return -ENOSPC;
4075 }
4076
4077 chip->ecc.bytes = ecc_bytes;
4078
4079 return 0;
4080 }
4081 }
4082
4083 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4084 preset_step, preset_strength);
4085
4086 return -ENOTSUPP;
4087}
4088EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4089
4090/**
4091 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4092 * @chip: nand chip info structure
4093 * @caps: ECC engine caps info structure
4094 * @oobavail: OOB size that the ECC engine can use
4095 *
4096 * If a chip's ECC requirement is provided, try to meet it with the least
4097 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4098 * On success, the chosen ECC settings are set.
4099 */
4100int nand_match_ecc_req(struct nand_chip *chip,
4101 const struct nand_ecc_caps *caps, int oobavail)
4102{
4103 struct mtd_info *mtd = nand_to_mtd(chip);
4104 const struct nand_ecc_step_info *stepinfo;
4105 int req_step = chip->ecc_step_ds;
4106 int req_strength = chip->ecc_strength_ds;
4107 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4108 int best_step, best_strength, best_ecc_bytes;
4109 int best_ecc_bytes_total = INT_MAX;
4110 int i, j;
4111
4112 if (WARN_ON(oobavail < 0))
4113 return -EINVAL;
4114
4115 /* No information provided by the NAND chip */
4116 if (!req_step || !req_strength)
4117 return -ENOTSUPP;
4118
4119 /* number of correctable bits the chip requires in a page */
4120 req_corr = mtd->writesize / req_step * req_strength;
4121
4122 for (i = 0; i < caps->nstepinfos; i++) {
4123 stepinfo = &caps->stepinfos[i];
4124 step_size = stepinfo->stepsize;
4125
4126 for (j = 0; j < stepinfo->nstrengths; j++) {
4127 strength = stepinfo->strengths[j];
4128
4129 /*
4130 * If both step size and strength are smaller than the
4131 * chip's requirement, it is not easy to compare the
4132 * resulted reliability.
4133 */
4134 if (step_size < req_step && strength < req_strength)
4135 continue;
4136
4137 if (mtd->writesize % step_size)
4138 continue;
4139
4140 nsteps = mtd->writesize / step_size;
4141
4142 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4143 if (WARN_ON_ONCE(ecc_bytes < 0))
4144 continue;
4145 ecc_bytes_total = ecc_bytes * nsteps;
4146
4147 if (ecc_bytes_total > oobavail ||
4148 strength * nsteps < req_corr)
4149 continue;
4150
4151 /*
4152 * We assume the best is to meet the chip's requrement
4153 * with the least number of ECC bytes.
4154 */
4155 if (ecc_bytes_total < best_ecc_bytes_total) {
4156 best_ecc_bytes_total = ecc_bytes_total;
4157 best_step = step_size;
4158 best_strength = strength;
4159 best_ecc_bytes = ecc_bytes;
4160 }
4161 }
4162 }
4163
4164 if (best_ecc_bytes_total == INT_MAX)
4165 return -ENOTSUPP;
4166
4167 chip->ecc.size = best_step;
4168 chip->ecc.strength = best_strength;
4169 chip->ecc.bytes = best_ecc_bytes;
4170
4171 return 0;
4172}
4173EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4174
4175/**
4176 * nand_maximize_ecc - choose the max ECC strength available
4177 * @chip: nand chip info structure
4178 * @caps: ECC engine caps info structure
4179 * @oobavail: OOB size that the ECC engine can use
4180 *
4181 * Choose the max ECC strength that is supported on the controller, and can fit
4182 * within the chip's OOB. On success, the chosen ECC settings are set.
4183 */
4184int nand_maximize_ecc(struct nand_chip *chip,
4185 const struct nand_ecc_caps *caps, int oobavail)
4186{
4187 struct mtd_info *mtd = nand_to_mtd(chip);
4188 const struct nand_ecc_step_info *stepinfo;
4189 int step_size, strength, nsteps, ecc_bytes, corr;
4190 int best_corr = 0;
4191 int best_step = 0;
4192 int best_strength, best_ecc_bytes;
4193 int i, j;
4194
4195 if (WARN_ON(oobavail < 0))
4196 return -EINVAL;
4197
4198 for (i = 0; i < caps->nstepinfos; i++) {
4199 stepinfo = &caps->stepinfos[i];
4200 step_size = stepinfo->stepsize;
4201
4202 /* If chip->ecc.size is already set, respect it */
4203 if (chip->ecc.size && step_size != chip->ecc.size)
4204 continue;
4205
4206 for (j = 0; j < stepinfo->nstrengths; j++) {
4207 strength = stepinfo->strengths[j];
4208
4209 if (mtd->writesize % step_size)
4210 continue;
4211
4212 nsteps = mtd->writesize / step_size;
4213
4214 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4215 if (WARN_ON_ONCE(ecc_bytes < 0))
4216 continue;
4217
4218 if (ecc_bytes * nsteps > oobavail)
4219 continue;
4220
4221 corr = strength * nsteps;
4222
4223 /*
4224 * If the number of correctable bits is the same,
4225 * bigger step_size has more reliability.
4226 */
4227 if (corr > best_corr ||
4228 (corr == best_corr && step_size > best_step)) {
4229 best_corr = corr;
4230 best_step = step_size;
4231 best_strength = strength;
4232 best_ecc_bytes = ecc_bytes;
4233 }
4234 }
4235 }
4236
4237 if (!best_corr)
4238 return -ENOTSUPP;
4239
4240 chip->ecc.size = best_step;
4241 chip->ecc.strength = best_strength;
4242 chip->ecc.bytes = best_ecc_bytes;
4243
4244 return 0;
4245}
4246EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4247
Scott Wood3ea94ed2015-06-26 19:03:26 -05004248/*
4249 * Check if the chip configuration meet the datasheet requirements.
4250
4251 * If our configuration corrects A bits per B bytes and the minimum
4252 * required correction level is X bits per Y bytes, then we must ensure
4253 * both of the following are true:
4254 *
4255 * (1) A / B >= X / Y
4256 * (2) A >= X
4257 *
4258 * Requirement (1) ensures we can correct for the required bitflip density.
4259 * Requirement (2) ensures we can correct even when all bitflips are clumped
4260 * in the same sector.
4261 */
4262static bool nand_ecc_strength_good(struct mtd_info *mtd)
4263{
Scott Wood17fed142016-05-30 13:57:56 -05004264 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004265 struct nand_ecc_ctrl *ecc = &chip->ecc;
4266 int corr, ds_corr;
4267
4268 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4269 /* Not enough information */
4270 return true;
4271
4272 /*
4273 * We get the number of corrected bits per page to compare
4274 * the correction density.
4275 */
4276 corr = (mtd->writesize * ecc->strength) / ecc->size;
4277 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4278
4279 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4280}
William Juul52c07962007-10-31 13:53:06 +01004281
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004282static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4283{
4284 struct nand_ecc_ctrl *ecc = &chip->ecc;
4285
4286 if (nand_standard_page_accessors(ecc))
4287 return false;
4288
4289 /*
4290 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4291 * controller driver implements all the page accessors because
4292 * default helpers are not suitable when the core does not
4293 * send the READ0/PAGEPROG commands.
4294 */
4295 return (!ecc->read_page || !ecc->write_page ||
4296 !ecc->read_page_raw || !ecc->write_page_raw ||
4297 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4298 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4299 ecc->hwctl && ecc->calculate));
4300}
4301
William Juul52c07962007-10-31 13:53:06 +01004302/**
4303 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004304 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004305 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004306 * This is the second phase of the normal nand_scan() function. It fills out
4307 * all the uninitialized function pointers with the defaults and scans for a
4308 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004309 */
4310int nand_scan_tail(struct mtd_info *mtd)
4311{
4312 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004313 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004314 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004315 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004316
Sergey Lapin3a38a552013-01-14 03:46:50 +00004317 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4318 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4319 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4320
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004321 if (invalid_ecc_page_accessors(chip)) {
4322 pr_err("Invalid ECC page accessors setup\n");
4323 return -EINVAL;
4324 }
4325
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004326 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004327 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004328 chip->buffers = nbuf;
4329 } else {
4330 if (!chip->buffers)
4331 return -ENOMEM;
4332 }
William Juul52c07962007-10-31 13:53:06 +01004333
4334 /* Set the internal oob buffer location, just after the page data */
4335 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4336
4337 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004338 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004339 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004340 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004341 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004342 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004343 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004344 break;
4345 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004346 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004347 break;
4348 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004349 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004350 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004351 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004352 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004353 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004354 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004355 pr_warn("No oob scheme defined for oobsize %d\n",
4356 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004357 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004358 }
4359 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004360
William Juul52c07962007-10-31 13:53:06 +01004361 if (!chip->write_page)
4362 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004363
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004364 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004365 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004366 * selected and we have 256 byte pagesize fallback to software ECC
4367 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004368
Heiko Schocherf5895d12014-06-24 10:10:04 +02004369 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004370 case NAND_ECC_HW_OOB_FIRST:
4371 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004372 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004373 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004374 BUG();
4375 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004376 if (!ecc->read_page)
4377 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004378
William Juul52c07962007-10-31 13:53:06 +01004379 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004380 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004381 if (!ecc->read_page)
4382 ecc->read_page = nand_read_page_hwecc;
4383 if (!ecc->write_page)
4384 ecc->write_page = nand_write_page_hwecc;
4385 if (!ecc->read_page_raw)
4386 ecc->read_page_raw = nand_read_page_raw;
4387 if (!ecc->write_page_raw)
4388 ecc->write_page_raw = nand_write_page_raw;
4389 if (!ecc->read_oob)
4390 ecc->read_oob = nand_read_oob_std;
4391 if (!ecc->write_oob)
4392 ecc->write_oob = nand_write_oob_std;
4393 if (!ecc->read_subpage)
4394 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05004395 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004396 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004397
William Juul52c07962007-10-31 13:53:06 +01004398 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004399 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4400 (!ecc->read_page ||
4401 ecc->read_page == nand_read_page_hwecc ||
4402 !ecc->write_page ||
4403 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004404 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01004405 BUG();
4406 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00004407 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004408 if (!ecc->read_page)
4409 ecc->read_page = nand_read_page_syndrome;
4410 if (!ecc->write_page)
4411 ecc->write_page = nand_write_page_syndrome;
4412 if (!ecc->read_page_raw)
4413 ecc->read_page_raw = nand_read_page_raw_syndrome;
4414 if (!ecc->write_page_raw)
4415 ecc->write_page_raw = nand_write_page_raw_syndrome;
4416 if (!ecc->read_oob)
4417 ecc->read_oob = nand_read_oob_syndrome;
4418 if (!ecc->write_oob)
4419 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004420
Heiko Schocherf5895d12014-06-24 10:10:04 +02004421 if (mtd->writesize >= ecc->size) {
4422 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004423 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4424 BUG();
4425 }
William Juul52c07962007-10-31 13:53:06 +01004426 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004427 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004428 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4429 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004430 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004431
William Juul52c07962007-10-31 13:53:06 +01004432 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004433 ecc->calculate = nand_calculate_ecc;
4434 ecc->correct = nand_correct_data;
4435 ecc->read_page = nand_read_page_swecc;
4436 ecc->read_subpage = nand_read_subpage;
4437 ecc->write_page = nand_write_page_swecc;
4438 ecc->read_page_raw = nand_read_page_raw;
4439 ecc->write_page_raw = nand_write_page_raw;
4440 ecc->read_oob = nand_read_oob_std;
4441 ecc->write_oob = nand_write_oob_std;
4442 if (!ecc->size)
4443 ecc->size = 256;
4444 ecc->bytes = 3;
4445 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004446 break;
4447
Christian Hitz55f7bca2011-10-12 09:31:59 +02004448 case NAND_ECC_SOFT_BCH:
4449 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004450 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004451 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02004452 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004453 ecc->calculate = nand_bch_calculate_ecc;
4454 ecc->correct = nand_bch_correct_data;
4455 ecc->read_page = nand_read_page_swecc;
4456 ecc->read_subpage = nand_read_subpage;
4457 ecc->write_page = nand_write_page_swecc;
4458 ecc->read_page_raw = nand_read_page_raw;
4459 ecc->write_page_raw = nand_write_page_raw;
4460 ecc->read_oob = nand_read_oob_std;
4461 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004462 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05004463 * Board driver should supply ecc.size and ecc.strength values
4464 * to select how many bits are correctable. Otherwise, default
4465 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02004466 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004467 if (!ecc->size && (mtd->oobsize >= 64)) {
4468 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004469 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004470 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004471
4472 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004473 ecc->bytes = 0;
4474 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004475 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004476 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004477 BUG();
4478 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02004479 break;
4480
William Juul52c07962007-10-31 13:53:06 +01004481 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05004482 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004483 ecc->read_page = nand_read_page_raw;
4484 ecc->write_page = nand_write_page_raw;
4485 ecc->read_oob = nand_read_oob_std;
4486 ecc->read_page_raw = nand_read_page_raw;
4487 ecc->write_page_raw = nand_write_page_raw;
4488 ecc->write_oob = nand_write_oob_std;
4489 ecc->size = mtd->writesize;
4490 ecc->bytes = 0;
4491 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004492 break;
4493
4494 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004495 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01004496 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004497 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004498
Sergey Lapin3a38a552013-01-14 03:46:50 +00004499 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004500 if (!ecc->read_oob_raw)
4501 ecc->read_oob_raw = ecc->read_oob;
4502 if (!ecc->write_oob_raw)
4503 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004504
William Juul52c07962007-10-31 13:53:06 +01004505 /*
4506 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00004507 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01004508 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004509 mtd->oobavail = 0;
4510 if (ecc->layout) {
4511 for (i = 0; ecc->layout->oobfree[i].length; i++)
4512 mtd->oobavail += ecc->layout->oobfree[i].length;
4513 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004514
Scott Wood3ea94ed2015-06-26 19:03:26 -05004515 /* ECC sanity check: warn if it's too weak */
4516 if (!nand_ecc_strength_good(mtd))
4517 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4518 mtd->name);
4519
William Juul52c07962007-10-31 13:53:06 +01004520 /*
4521 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00004522 * mode.
William Juul52c07962007-10-31 13:53:06 +01004523 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004524 ecc->steps = mtd->writesize / ecc->size;
4525 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004526 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01004527 BUG();
4528 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004529 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004530
Sergey Lapin3a38a552013-01-14 03:46:50 +00004531 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004532 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4533 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01004534 case 2:
4535 mtd->subpage_sft = 1;
4536 break;
4537 case 4:
4538 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05004539 case 16:
William Juul52c07962007-10-31 13:53:06 +01004540 mtd->subpage_sft = 2;
4541 break;
4542 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004543 }
William Juul52c07962007-10-31 13:53:06 +01004544 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004545
William Juul52c07962007-10-31 13:53:06 +01004546 /* Initialize state */
4547 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004548
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004549 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01004550 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004551
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004552 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05004553 switch (ecc->mode) {
4554 case NAND_ECC_SOFT:
4555 case NAND_ECC_SOFT_BCH:
4556 if (chip->page_shift > 9)
4557 chip->options |= NAND_SUBPAGE_READ;
4558 break;
4559
4560 default:
4561 break;
4562 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004563
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004564 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004565 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004566 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4567 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004568 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004569 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004570 mtd->_read_oob = nand_read_oob;
4571 mtd->_write_oob = nand_write_oob;
4572 mtd->_sync = nand_sync;
4573 mtd->_lock = NULL;
4574 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03004575 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004576 mtd->_block_isbad = nand_block_isbad;
4577 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004578 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004579
Sergey Lapin3a38a552013-01-14 03:46:50 +00004580 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004581 mtd->ecclayout = ecc->layout;
4582 mtd->ecc_strength = ecc->strength;
4583 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004584 /*
4585 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4586 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4587 * properly set.
4588 */
4589 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05004590 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01004591
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02004592 return 0;
William Juul52c07962007-10-31 13:53:06 +01004593}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004594EXPORT_SYMBOL(nand_scan_tail);
4595
William Juul52c07962007-10-31 13:53:06 +01004596/**
4597 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004598 * @mtd: MTD device structure
4599 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01004600 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004601 * This fills out all the uninitialized function pointers with the defaults.
4602 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05004603 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01004604 */
4605int nand_scan(struct mtd_info *mtd, int maxchips)
4606{
4607 int ret;
4608
Lei Wen75bde942011-01-06 09:48:18 +08004609 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01004610 if (!ret)
4611 ret = nand_scan_tail(mtd);
4612 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004613}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004614EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004615
Heiko Schocherf5895d12014-06-24 10:10:04 +02004616MODULE_LICENSE("GPL");
4617MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4618MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4619MODULE_DESCRIPTION("Generic NAND flash driver code");