blob: 6d2ff58d86a557a1f64ad01f3385f6c033d5c5d0 [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 */
Stefan Agnerbd186142018-12-06 14:57:09 +010050#ifdef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
William Juul52c07962007-10-31 13:53:06 +010051static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020052 .eccbytes = 3,
53 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010054 .oobfree = {
55 {.offset = 3,
56 .length = 2},
57 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020058 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020059};
60
William Juul52c07962007-10-31 13:53:06 +010061static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020062 .eccbytes = 6,
63 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010064 .oobfree = {
65 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020066 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020067};
68
William Juul52c07962007-10-31 13:53:06 +010069static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020070 .eccbytes = 24,
71 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010072 40, 41, 42, 43, 44, 45, 46, 47,
73 48, 49, 50, 51, 52, 53, 54, 55,
74 56, 57, 58, 59, 60, 61, 62, 63},
75 .oobfree = {
76 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020077 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020078};
79
William Juul52c07962007-10-31 13:53:06 +010080static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020081 .eccbytes = 48,
82 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020083 80, 81, 82, 83, 84, 85, 86, 87,
84 88, 89, 90, 91, 92, 93, 94, 95,
85 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010086 104, 105, 106, 107, 108, 109, 110, 111,
87 112, 113, 114, 115, 116, 117, 118, 119,
88 120, 121, 122, 123, 124, 125, 126, 127},
89 .oobfree = {
90 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020091 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020092};
Stefan Agnerbd186142018-12-06 14:57:09 +010093#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020094
Heiko Schocherf5895d12014-06-24 10:10:04 +020095static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010096
97static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
98 struct mtd_oob_ops *ops);
99
Heiko Schocherf5895d12014-06-24 10:10:04 +0200100/*
101 * For devices which display every fart in the system on a separate LED. Is
102 * compiled away when LED support is disabled.
103 */
104DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200105
Christian Hitzb8a6b372011-10-12 09:32:02 +0200106static int check_offs_len(struct mtd_info *mtd,
107 loff_t ofs, uint64_t len)
108{
Scott Wood17fed142016-05-30 13:57:56 -0500109 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200110 int ret = 0;
111
112 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200113 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
114 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200115 ret = -EINVAL;
116 }
117
118 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200119 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
120 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200121 ret = -EINVAL;
122 }
123
Christian Hitzb8a6b372011-10-12 09:32:02 +0200124 return ret;
125}
126
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200127/**
128 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000129 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200130 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200131 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200132 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200133static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100134{
Scott Wood17fed142016-05-30 13:57:56 -0500135 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200136
137 /* De-select the NAND device */
138 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100139}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200140
141/**
142 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000143 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200144 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200145 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000147uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200148{
Scott Wood17fed142016-05-30 13:57:56 -0500149 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100150 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200151}
152
153/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200154 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000155 * @mtd: MTD device structure
156 *
157 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200158 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200159 */
William Juul52c07962007-10-31 13:53:06 +0100160static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200161{
Scott Wood17fed142016-05-30 13:57:56 -0500162 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100163 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200164}
165
166/**
167 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000168 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200169 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000170 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200171 */
172static u16 nand_read_word(struct mtd_info *mtd)
173{
Scott Wood17fed142016-05-30 13:57:56 -0500174 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100175 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200176}
177
178/**
179 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000180 * @mtd: MTD device structure
181 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200182 *
183 * Default select function for 1 chip devices.
184 */
William Juul52c07962007-10-31 13:53:06 +0100185static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200186{
Scott Wood17fed142016-05-30 13:57:56 -0500187 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100188
189 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200190 case -1:
William Juul52c07962007-10-31 13:53:06 +0100191 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192 break;
193 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200194 break;
195
196 default:
197 BUG();
198 }
199}
200
201/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200202 * nand_write_byte - [DEFAULT] write single byte to chip
203 * @mtd: MTD device structure
204 * @byte: value to write
205 *
206 * Default function to write a byte to I/O[7:0]
207 */
208static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
209{
Scott Wood17fed142016-05-30 13:57:56 -0500210 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200211
212 chip->write_buf(mtd, &byte, 1);
213}
214
215/**
216 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
217 * @mtd: MTD device structure
218 * @byte: value to write
219 *
220 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
221 */
222static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
223{
Scott Wood17fed142016-05-30 13:57:56 -0500224 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200225 uint16_t word = byte;
226
227 /*
228 * It's not entirely clear what should happen to I/O[15:8] when writing
229 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
230 *
231 * When the host supports a 16-bit bus width, only data is
232 * transferred at the 16-bit width. All address and command line
233 * transfers shall use only the lower 8-bits of the data bus. During
234 * command transfers, the host may place any value on the upper
235 * 8-bits of the data bus. During address transfers, the host shall
236 * set the upper 8-bits of the data bus to 00h.
237 *
238 * One user of the write_byte callback is nand_onfi_set_features. The
239 * four parameters are specified to be written to I/O[7:0], but this is
240 * neither an address nor a command transfer. Let's assume a 0 on the
241 * upper I/O lines is OK.
242 */
243 chip->write_buf(mtd, (uint8_t *)&word, 2);
244}
245
Heiko Schocherf5895d12014-06-24 10:10:04 +0200246static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
247{
248 int i;
249
250 for (i = 0; i < len; i++)
251 writeb(buf[i], addr);
252}
253static void ioread8_rep(void *addr, uint8_t *buf, int len)
254{
255 int i;
256
257 for (i = 0; i < len; i++)
258 buf[i] = readb(addr);
259}
260
261static void ioread16_rep(void *addr, void *buf, int len)
262{
263 int i;
264 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200265
Heiko Schocherf5895d12014-06-24 10:10:04 +0200266 for (i = 0; i < len; i++)
267 p[i] = readw(addr);
268}
269
270static void iowrite16_rep(void *addr, void *buf, int len)
271{
272 int i;
273 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200274
275 for (i = 0; i < len; i++)
276 writew(p[i], addr);
277}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200278
279/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200280 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000281 * @mtd: MTD device structure
282 * @buf: data buffer
283 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200284 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000285 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000287void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288{
Scott Wood17fed142016-05-30 13:57:56 -0500289 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200290
Heiko Schocherf5895d12014-06-24 10:10:04 +0200291 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200292}
293
294/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200295 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000296 * @mtd: MTD device structure
297 * @buf: buffer to store date
298 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200299 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000300 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400302void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303{
Scott Wood17fed142016-05-30 13:57:56 -0500304 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305
Heiko Schocherf5895d12014-06-24 10:10:04 +0200306 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307}
308
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200309/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200310 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000311 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312 * @buf: data buffer
313 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200314 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200315 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318{
Scott Wood17fed142016-05-30 13:57:56 -0500319 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200320 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200321
Heiko Schocherf5895d12014-06-24 10:10:04 +0200322 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323}
324
325/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200326 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000327 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200328 * @buf: buffer to store date
329 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200330 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200331 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200333void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334{
Scott Wood17fed142016-05-30 13:57:56 -0500335 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200336 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200337
Heiko Schocherf5895d12014-06-24 10:10:04 +0200338 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200339}
340
341/**
342 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000343 * @mtd: MTD device structure
344 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200345 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200346 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500348static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200349{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500350 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500351 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200352 u16 bad;
353
Sergey Lapin3a38a552013-01-14 03:46:50 +0000354 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200355 ofs += mtd->erasesize - mtd->writesize;
356
William Juul52c07962007-10-31 13:53:06 +0100357 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200358
Sergey Lapin3a38a552013-01-14 03:46:50 +0000359 do {
360 if (chip->options & NAND_BUSWIDTH_16) {
361 chip->cmdfunc(mtd, NAND_CMD_READOOB,
362 chip->badblockpos & 0xFE, page);
363 bad = cpu_to_le16(chip->read_word(mtd));
364 if (chip->badblockpos & 0x1)
365 bad >>= 8;
366 else
367 bad &= 0xFF;
368 } else {
369 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
370 page);
371 bad = chip->read_byte(mtd);
372 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200373
Sergey Lapin3a38a552013-01-14 03:46:50 +0000374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
378 ofs += mtd->writesize;
379 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
380 i++;
381 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200382
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200383 return res;
384}
385
386/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200387 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000388 * @mtd: MTD device structure
389 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200390 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000391 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200392 * specific driver. It provides the details for writing a bad block marker to a
393 * block.
394 */
395static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
396{
Scott Wood17fed142016-05-30 13:57:56 -0500397 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200398 struct mtd_oob_ops ops;
399 uint8_t buf[2] = { 0, 0 };
400 int ret = 0, res, i = 0;
401
Scott Wood3ea94ed2015-06-26 19:03:26 -0500402 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200403 ops.oobbuf = buf;
404 ops.ooboffs = chip->badblockpos;
405 if (chip->options & NAND_BUSWIDTH_16) {
406 ops.ooboffs &= ~0x01;
407 ops.len = ops.ooblen = 2;
408 } else {
409 ops.len = ops.ooblen = 1;
410 }
411 ops.mode = MTD_OPS_PLACE_OOB;
412
413 /* Write to first/last page(s) if necessary */
414 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
415 ofs += mtd->erasesize - mtd->writesize;
416 do {
417 res = nand_do_write_oob(mtd, ofs, &ops);
418 if (!ret)
419 ret = res;
420
421 i++;
422 ofs += mtd->writesize;
423 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
424
425 return ret;
426}
427
428/**
429 * nand_block_markbad_lowlevel - mark a block bad
430 * @mtd: MTD device structure
431 * @ofs: offset from device start
432 *
433 * This function performs the generic NAND bad block marking steps (i.e., bad
434 * block table(s) and/or marker(s)). We only allow the hardware driver to
435 * specify how to write bad block markers to OOB (chip->block_markbad).
436 *
437 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000438 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200439 * (2) write bad block marker to OOB area of affected block (unless flag
440 * NAND_BBT_NO_OOB_BBM is present)
441 * (3) update the BBT
442 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000443 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200444*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200445static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200446{
Scott Wood17fed142016-05-30 13:57:56 -0500447 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200448 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200449
Heiko Schocherf5895d12014-06-24 10:10:04 +0200450 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000451 struct erase_info einfo;
452
453 /* Attempt erase before marking OOB */
454 memset(&einfo, 0, sizeof(einfo));
455 einfo.mtd = mtd;
456 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200457 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000458 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200459
Heiko Schocherf5895d12014-06-24 10:10:04 +0200460 /* Write bad block marker to OOB */
461 nand_get_device(mtd, FL_WRITING);
462 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500463 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100464 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000465
Heiko Schocherf5895d12014-06-24 10:10:04 +0200466 /* Mark block bad in BBT */
467 if (chip->bbt) {
468 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000469 if (!ret)
470 ret = res;
471 }
472
William Juul52c07962007-10-31 13:53:06 +0100473 if (!ret)
474 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500475
William Juul52c07962007-10-31 13:53:06 +0100476 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200477}
478
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200479/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200480 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000481 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200482 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000483 * Check, if the device is write protected. The function expects, that the
484 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200485 */
William Juul52c07962007-10-31 13:53:06 +0100486static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200487{
Scott Wood17fed142016-05-30 13:57:56 -0500488 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200489
Sergey Lapin3a38a552013-01-14 03:46:50 +0000490 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200491 if (chip->options & NAND_BROKEN_XD)
492 return 0;
493
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200494 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100495 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
496 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200497}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100498
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200499/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500500 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000501 * @mtd: MTD device structure
502 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300503 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500504 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300505 */
506static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
507{
Scott Wood17fed142016-05-30 13:57:56 -0500508 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300509
510 if (!chip->bbt)
511 return 0;
512 /* Return info from the table */
513 return nand_isreserved_bbt(mtd, ofs);
514}
515
516/**
517 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
518 * @mtd: MTD device structure
519 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000520 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200521 *
522 * Check, if the block is bad. Either by reading the bad block table or
523 * calling of the scan function.
524 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500525static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200526{
Scott Wood17fed142016-05-30 13:57:56 -0500527 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200528
Masahiro Yamada8d100542014-12-26 22:20:58 +0900529 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
530 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200531 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900532 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200533 }
534
William Juul52c07962007-10-31 13:53:06 +0100535 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500536 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200537
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200538 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100539 return nand_isbad_bbt(mtd, ofs, allowbbt);
540}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200541
Scott Wood52ab7ce2016-05-30 13:57:58 -0500542/**
543 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
544 * @mtd: MTD device structure
545 *
546 * Wait for the ready pin after a command, and warn if a timeout occurs.
547 */
William Juul52c07962007-10-31 13:53:06 +0100548void nand_wait_ready(struct mtd_info *mtd)
549{
Scott Wood17fed142016-05-30 13:57:56 -0500550 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500551 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000552 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100553
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000554 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000555 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000556 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100557 if (chip->dev_ready)
558 if (chip->dev_ready(mtd))
559 break;
560 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500561
562 if (!chip->dev_ready(mtd))
563 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200564}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200565EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200566
567/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500568 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
569 * @mtd: MTD device structure
570 * @timeo: Timeout in ms
571 *
572 * Wait for status ready (i.e. command done) or timeout.
573 */
574static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
575{
Scott Wood17fed142016-05-30 13:57:56 -0500576 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500577 u32 time_start;
578
579 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
580 time_start = get_timer(0);
581 while (get_timer(time_start) < timeo) {
582 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
583 break;
584 WATCHDOG_RESET();
585 }
586};
587
588/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200589 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000590 * @mtd: MTD device structure
591 * @command: the command to be sent
592 * @column: the column address for this command, -1 if none
593 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200594 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000595 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200596 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200597 */
William Juul52c07962007-10-31 13:53:06 +0100598static void nand_command(struct mtd_info *mtd, unsigned int command,
599 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200600{
Scott Wood17fed142016-05-30 13:57:56 -0500601 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100602 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603
Sergey Lapin3a38a552013-01-14 03:46:50 +0000604 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200605 if (command == NAND_CMD_SEQIN) {
606 int readcmd;
607
William Juul52c07962007-10-31 13:53:06 +0100608 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200609 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100610 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 readcmd = NAND_CMD_READOOB;
612 } else if (column < 256) {
613 /* First 256 bytes --> READ0 */
614 readcmd = NAND_CMD_READ0;
615 } else {
616 column -= 256;
617 readcmd = NAND_CMD_READ1;
618 }
William Juul52c07962007-10-31 13:53:06 +0100619 chip->cmd_ctrl(mtd, readcmd, ctrl);
620 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621 }
William Juul52c07962007-10-31 13:53:06 +0100622 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623
Sergey Lapin3a38a552013-01-14 03:46:50 +0000624 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100625 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
626 /* Serially input address */
627 if (column != -1) {
628 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200629 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530630 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100631 column >>= 1;
632 chip->cmd_ctrl(mtd, column, ctrl);
633 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200634 }
William Juul52c07962007-10-31 13:53:06 +0100635 if (page_addr != -1) {
636 chip->cmd_ctrl(mtd, page_addr, ctrl);
637 ctrl &= ~NAND_CTRL_CHANGE;
638 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900639 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100640 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
641 }
642 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200643
644 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000645 * Program and erase have their own busy handlers status and sequential
646 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100647 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200648 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200649
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200650 case NAND_CMD_PAGEPROG:
651 case NAND_CMD_ERASE1:
652 case NAND_CMD_ERASE2:
653 case NAND_CMD_SEQIN:
654 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900655 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900656 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200657 return;
658
659 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100660 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200661 break;
William Juul52c07962007-10-31 13:53:06 +0100662 udelay(chip->chip_delay);
663 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
664 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
665 chip->cmd_ctrl(mtd,
666 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500667 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
668 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200669 return;
670
William Juul52c07962007-10-31 13:53:06 +0100671 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200672 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200673 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200674 * If we don't have access to the busy pin, we apply the given
675 * command delay
William Juul52c07962007-10-31 13:53:06 +0100676 */
677 if (!chip->dev_ready) {
678 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200679 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200680 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200681 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000682 /*
683 * Apply this short delay always to ensure that we do wait tWB in
684 * any case on any machine.
685 */
William Juul52c07962007-10-31 13:53:06 +0100686 ndelay(100);
687
688 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200689}
690
691/**
692 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000693 * @mtd: MTD device structure
694 * @command: the command to be sent
695 * @column: the column address for this command, -1 if none
696 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200697 *
William Juul52c07962007-10-31 13:53:06 +0100698 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000699 * devices. We don't have the separate regions as we have in the small page
700 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200701 */
William Juul52c07962007-10-31 13:53:06 +0100702static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
703 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200704{
Scott Wood17fed142016-05-30 13:57:56 -0500705 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200706
707 /* Emulate NAND_CMD_READOOB */
708 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100709 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200710 command = NAND_CMD_READ0;
711 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200712
William Juul52c07962007-10-31 13:53:06 +0100713 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200714 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715
716 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100717 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200718
719 /* Serially input address */
720 if (column != -1) {
721 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200722 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530723 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100725 chip->cmd_ctrl(mtd, column, ctrl);
726 ctrl &= ~NAND_CTRL_CHANGE;
727 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200728 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200729 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100730 chip->cmd_ctrl(mtd, page_addr, ctrl);
731 chip->cmd_ctrl(mtd, page_addr >> 8,
732 NAND_NCE | NAND_ALE);
Masahiro Yamada984926b2017-11-22 02:38:31 +0900733 if (chip->options & NAND_ROW_ADDR_3)
William Juul52c07962007-10-31 13:53:06 +0100734 chip->cmd_ctrl(mtd, page_addr >> 16,
735 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200736 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200737 }
William Juul52c07962007-10-31 13:53:06 +0100738 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200739
740 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000741 * Program and erase have their own busy handlers status, sequential
Scott Wood3ea94ed2015-06-26 19:03:26 -0500742 * in and status need no delay.
William Juul52c07962007-10-31 13:53:06 +0100743 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200744 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200745
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200746 case NAND_CMD_CACHEDPROG:
747 case NAND_CMD_PAGEPROG:
748 case NAND_CMD_ERASE1:
749 case NAND_CMD_ERASE2:
750 case NAND_CMD_SEQIN:
William Juul52c07962007-10-31 13:53:06 +0100751 case NAND_CMD_RNDIN:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200752 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900753 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900754 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200755 return;
756
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200757 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100758 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200759 break;
William Juul52c07962007-10-31 13:53:06 +0100760 udelay(chip->chip_delay);
761 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
762 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
763 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
764 NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500765 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
766 nand_wait_status_ready(mtd, 250);
William Juul52c07962007-10-31 13:53:06 +0100767 return;
768
769 case NAND_CMD_RNDOUT:
770 /* No ready / busy check necessary */
771 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
772 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
773 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
774 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200775 return;
776
777 case NAND_CMD_READ0:
William Juul52c07962007-10-31 13:53:06 +0100778 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
779 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
780 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
781 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200782
William Juul52c07962007-10-31 13:53:06 +0100783 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200784 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200785 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200786 * If we don't have access to the busy pin, we apply the given
Sergey Lapin3a38a552013-01-14 03:46:50 +0000787 * command delay.
William Juul52c07962007-10-31 13:53:06 +0100788 */
789 if (!chip->dev_ready) {
790 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200791 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200792 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200793 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200794
Sergey Lapin3a38a552013-01-14 03:46:50 +0000795 /*
796 * Apply this short delay always to ensure that we do wait tWB in
797 * any case on any machine.
798 */
William Juul52c07962007-10-31 13:53:06 +0100799 ndelay(100);
800
801 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200802}
803
804/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200805 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapin3a38a552013-01-14 03:46:50 +0000806 * @chip: the nand chip descriptor
807 * @mtd: MTD device structure
808 * @new_state: the state which is requested
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200809 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200810 * Used when in panic, no locks are taken.
811 */
812static void panic_nand_get_device(struct nand_chip *chip,
813 struct mtd_info *mtd, int new_state)
814{
815 /* Hardware controller shared among independent devices */
816 chip->controller->active = chip;
817 chip->state = new_state;
818}
819
820/**
821 * nand_get_device - [GENERIC] Get chip for selected access
822 * @mtd: MTD device structure
823 * @new_state: the state which is requested
824 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200825 * Get the device and lock it for exclusive access
826 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200827static int
Heiko Schocherf5895d12014-06-24 10:10:04 +0200828nand_get_device(struct mtd_info *mtd, int new_state)
William Juul52c07962007-10-31 13:53:06 +0100829{
Scott Wood17fed142016-05-30 13:57:56 -0500830 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200831 chip->state = new_state;
William Juul52c07962007-10-31 13:53:06 +0100832 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200833}
834
835/**
836 * panic_nand_wait - [GENERIC] wait until the command is done
837 * @mtd: MTD device structure
838 * @chip: NAND chip structure
839 * @timeo: timeout
840 *
841 * Wait for command done. This is a helper function for nand_wait used when
842 * we are in interrupt context. May happen when in panic and trying to write
843 * an oops through mtdoops.
844 */
845static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
846 unsigned long timeo)
847{
848 int i;
849 for (i = 0; i < timeo; i++) {
850 if (chip->dev_ready) {
851 if (chip->dev_ready(mtd))
852 break;
853 } else {
854 if (chip->read_byte(mtd) & NAND_STATUS_READY)
855 break;
856 }
857 mdelay(1);
858 }
William Juul52c07962007-10-31 13:53:06 +0100859}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200860
861/**
Sergey Lapin3a38a552013-01-14 03:46:50 +0000862 * nand_wait - [DEFAULT] wait until the command is done
863 * @mtd: MTD device structure
864 * @chip: NAND chip structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200865 *
Scott Wood52ab7ce2016-05-30 13:57:58 -0500866 * Wait for command done. This applies to erase and program only.
William Juul52c07962007-10-31 13:53:06 +0100867 */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200868static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200869{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500870 int status;
871 unsigned long timeo = 400;
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100872
Heiko Schocherf5895d12014-06-24 10:10:04 +0200873 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100874
Heiko Schocherf5895d12014-06-24 10:10:04 +0200875 /*
876 * Apply this short delay always to ensure that we do wait tWB in any
877 * case on any machine.
878 */
879 ndelay(100);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100880
Heiko Schocherf5895d12014-06-24 10:10:04 +0200881 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100882
Heiko Schocherf5895d12014-06-24 10:10:04 +0200883 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
884 u32 time_start;
885
886 time_start = get_timer(0);
887 while (get_timer(time_start) < timer) {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200888 if (chip->dev_ready) {
889 if (chip->dev_ready(mtd))
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100890 break;
891 } else {
Christian Hitzb8a6b372011-10-12 09:32:02 +0200892 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100893 break;
894 }
895 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200896 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka6eed2ab2006-02-24 09:37:22 +0100897
Heiko Schocherf5895d12014-06-24 10:10:04 +0200898 status = (int)chip->read_byte(mtd);
899 /* This can happen if in case of timeout or buggy dev_ready */
900 WARN_ON(!(status & NAND_STATUS_READY));
901 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200902}
Scott Wood52ab7ce2016-05-30 13:57:58 -0500903
Scott Wood52ab7ce2016-05-30 13:57:58 -0500904/**
Boris Brezillone509cba2017-11-22 02:38:19 +0900905 * nand_reset_data_interface - Reset data interface and timings
906 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900907 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900908 *
909 * Reset the Data interface and timings to ONFI mode 0.
910 *
911 * Returns 0 for success or negative error code otherwise.
912 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900913static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900914{
915 struct mtd_info *mtd = nand_to_mtd(chip);
916 const struct nand_data_interface *conf;
917 int ret;
918
919 if (!chip->setup_data_interface)
920 return 0;
921
922 /*
923 * The ONFI specification says:
924 * "
925 * To transition from NV-DDR or NV-DDR2 to the SDR data
926 * interface, the host shall use the Reset (FFh) command
927 * using SDR timing mode 0. A device in any timing mode is
928 * required to recognize Reset (FFh) command issued in SDR
929 * timing mode 0.
930 * "
931 *
932 * Configure the data interface in SDR mode and set the
933 * timings to timing mode 0.
934 */
935
936 conf = nand_get_default_data_interface();
Boris Brezillon32935f42017-11-22 02:38:28 +0900937 ret = chip->setup_data_interface(mtd, chipnr, conf);
Boris Brezillone509cba2017-11-22 02:38:19 +0900938 if (ret)
939 pr_err("Failed to configure data interface to SDR timing mode 0\n");
940
941 return ret;
942}
943
944/**
945 * nand_setup_data_interface - Setup the best data interface and timings
946 * @chip: The NAND chip
Boris Brezillon32935f42017-11-22 02:38:28 +0900947 * @chipnr: Internal die id
Boris Brezillone509cba2017-11-22 02:38:19 +0900948 *
949 * Find and configure the best data interface and NAND timings supported by
950 * the chip and the driver.
951 * First tries to retrieve supported timing modes from ONFI information,
952 * and if the NAND chip does not support ONFI, relies on the
953 * ->onfi_timing_mode_default specified in the nand_ids table.
954 *
955 * Returns 0 for success or negative error code otherwise.
956 */
Boris Brezillon32935f42017-11-22 02:38:28 +0900957static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
Boris Brezillone509cba2017-11-22 02:38:19 +0900958{
959 struct mtd_info *mtd = nand_to_mtd(chip);
960 int ret;
961
962 if (!chip->setup_data_interface || !chip->data_interface)
963 return 0;
964
965 /*
966 * Ensure the timing mode has been changed on the chip side
967 * before changing timings on the controller side.
968 */
969 if (chip->onfi_version) {
970 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
971 chip->onfi_timing_mode_default,
972 };
973
974 ret = chip->onfi_set_features(mtd, chip,
975 ONFI_FEATURE_ADDR_TIMING_MODE,
976 tmode_param);
977 if (ret)
978 goto err;
979 }
980
Boris Brezillon32935f42017-11-22 02:38:28 +0900981 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +0900982err:
983 return ret;
984}
985
986/**
987 * nand_init_data_interface - find the best data interface and timings
988 * @chip: The NAND chip
989 *
990 * Find the best data interface and NAND timings supported by the chip
991 * and the driver.
992 * First tries to retrieve supported timing modes from ONFI information,
993 * and if the NAND chip does not support ONFI, relies on the
994 * ->onfi_timing_mode_default specified in the nand_ids table. After this
995 * function nand_chip->data_interface is initialized with the best timing mode
996 * available.
997 *
998 * Returns 0 for success or negative error code otherwise.
999 */
1000static int nand_init_data_interface(struct nand_chip *chip)
1001{
1002 struct mtd_info *mtd = nand_to_mtd(chip);
1003 int modes, mode, ret;
1004
1005 if (!chip->setup_data_interface)
1006 return 0;
1007
1008 /*
1009 * First try to identify the best timings from ONFI parameters and
1010 * if the NAND does not support ONFI, fallback to the default ONFI
1011 * timing mode.
1012 */
1013 modes = onfi_get_async_timing_mode(chip);
1014 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1015 if (!chip->onfi_timing_mode_default)
1016 return 0;
1017
1018 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1019 }
1020
1021 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1022 GFP_KERNEL);
1023 if (!chip->data_interface)
1024 return -ENOMEM;
1025
1026 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1027 ret = onfi_init_data_interface(chip, chip->data_interface,
1028 NAND_SDR_IFACE, mode);
1029 if (ret)
1030 continue;
1031
Boris Brezillon32935f42017-11-22 02:38:28 +09001032 /* Pass -1 to only */
1033 ret = chip->setup_data_interface(mtd,
1034 NAND_DATA_IFACE_CHECK_ONLY,
1035 chip->data_interface);
Boris Brezillone509cba2017-11-22 02:38:19 +09001036 if (!ret) {
1037 chip->onfi_timing_mode_default = mode;
1038 break;
1039 }
1040 }
1041
1042 return 0;
1043}
1044
1045static void __maybe_unused nand_release_data_interface(struct nand_chip *chip)
1046{
1047 kfree(chip->data_interface);
1048}
1049
1050/**
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001051 * nand_reset - Reset and initialize a NAND device
1052 * @chip: The NAND chip
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001053 * @chipnr: Internal die id
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001054 *
1055 * Returns 0 for success or negative error code otherwise
1056 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001057int nand_reset(struct nand_chip *chip, int chipnr)
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001058{
1059 struct mtd_info *mtd = nand_to_mtd(chip);
Boris Brezillone509cba2017-11-22 02:38:19 +09001060 int ret;
1061
Boris Brezillon32935f42017-11-22 02:38:28 +09001062 ret = nand_reset_data_interface(chip, chipnr);
Boris Brezillone509cba2017-11-22 02:38:19 +09001063 if (ret)
1064 return ret;
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001065
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001066 /*
1067 * The CS line has to be released before we can apply the new NAND
1068 * interface settings, hence this weird ->select_chip() dance.
1069 */
1070 chip->select_chip(mtd, chipnr);
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001071 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001072 chip->select_chip(mtd, -1);
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001073
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001074 chip->select_chip(mtd, chipnr);
Boris Brezillon32935f42017-11-22 02:38:28 +09001075 ret = nand_setup_data_interface(chip, chipnr);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09001076 chip->select_chip(mtd, -1);
Boris Brezillone509cba2017-11-22 02:38:19 +09001077 if (ret)
1078 return ret;
1079
Sascha Hauer44ad3b92017-11-22 02:38:15 +09001080 return 0;
1081}
1082
1083/**
Scott Wood52ab7ce2016-05-30 13:57:58 -05001084 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
1085 * @buf: buffer to test
1086 * @len: buffer length
1087 * @bitflips_threshold: maximum number of bitflips
1088 *
1089 * Check if a buffer contains only 0xff, which means the underlying region
1090 * has been erased and is ready to be programmed.
1091 * The bitflips_threshold specify the maximum number of bitflips before
1092 * considering the region is not erased.
1093 * Note: The logic of this function has been extracted from the memweight
1094 * implementation, except that nand_check_erased_buf function exit before
1095 * testing the whole buffer if the number of bitflips exceed the
1096 * bitflips_threshold value.
1097 *
1098 * Returns a positive number of bitflips less than or equal to
1099 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1100 * threshold.
1101 */
1102static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1103{
1104 const unsigned char *bitmap = buf;
1105 int bitflips = 0;
1106 int weight;
1107
1108 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1109 len--, bitmap++) {
1110 weight = hweight8(*bitmap);
1111 bitflips += BITS_PER_BYTE - weight;
1112 if (unlikely(bitflips > bitflips_threshold))
1113 return -EBADMSG;
1114 }
1115
1116 for (; len >= 4; len -= 4, bitmap += 4) {
1117 weight = hweight32(*((u32 *)bitmap));
1118 bitflips += 32 - weight;
1119 if (unlikely(bitflips > bitflips_threshold))
1120 return -EBADMSG;
1121 }
1122
1123 for (; len > 0; len--, bitmap++) {
1124 weight = hweight8(*bitmap);
1125 bitflips += BITS_PER_BYTE - weight;
1126 if (unlikely(bitflips > bitflips_threshold))
1127 return -EBADMSG;
1128 }
1129
1130 return bitflips;
1131}
1132
1133/**
1134 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
1135 * 0xff data
1136 * @data: data buffer to test
1137 * @datalen: data length
1138 * @ecc: ECC buffer
1139 * @ecclen: ECC length
1140 * @extraoob: extra OOB buffer
1141 * @extraooblen: extra OOB length
1142 * @bitflips_threshold: maximum number of bitflips
1143 *
1144 * Check if a data buffer and its associated ECC and OOB data contains only
1145 * 0xff pattern, which means the underlying region has been erased and is
1146 * ready to be programmed.
1147 * The bitflips_threshold specify the maximum number of bitflips before
1148 * considering the region as not erased.
1149 *
1150 * Note:
1151 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
1152 * different from the NAND page size. When fixing bitflips, ECC engines will
1153 * report the number of errors per chunk, and the NAND core infrastructure
1154 * expect you to return the maximum number of bitflips for the whole page.
1155 * This is why you should always use this function on a single chunk and
1156 * not on the whole page. After checking each chunk you should update your
1157 * max_bitflips value accordingly.
1158 * 2/ When checking for bitflips in erased pages you should not only check
1159 * the payload data but also their associated ECC data, because a user might
1160 * have programmed almost all bits to 1 but a few. In this case, we
1161 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
1162 * this case.
1163 * 3/ The extraoob argument is optional, and should be used if some of your OOB
1164 * data are protected by the ECC engine.
1165 * It could also be used if you support subpages and want to attach some
1166 * extra OOB data to an ECC chunk.
1167 *
1168 * Returns a positive number of bitflips less than or equal to
1169 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
1170 * threshold. In case of success, the passed buffers are filled with 0xff.
1171 */
1172int nand_check_erased_ecc_chunk(void *data, int datalen,
1173 void *ecc, int ecclen,
1174 void *extraoob, int extraooblen,
1175 int bitflips_threshold)
1176{
1177 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1178
1179 data_bitflips = nand_check_erased_buf(data, datalen,
1180 bitflips_threshold);
1181 if (data_bitflips < 0)
1182 return data_bitflips;
1183
1184 bitflips_threshold -= data_bitflips;
1185
1186 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1187 if (ecc_bitflips < 0)
1188 return ecc_bitflips;
1189
1190 bitflips_threshold -= ecc_bitflips;
1191
1192 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1193 bitflips_threshold);
1194 if (extraoob_bitflips < 0)
1195 return extraoob_bitflips;
1196
1197 if (data_bitflips)
1198 memset(data, 0xff, datalen);
1199
1200 if (ecc_bitflips)
1201 memset(ecc, 0xff, ecclen);
1202
1203 if (extraoob_bitflips)
1204 memset(extraoob, 0xff, extraooblen);
1205
1206 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1207}
1208EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001209
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001210/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001211 * nand_read_page_raw - [INTERN] read raw page data without ecc
1212 * @mtd: mtd info structure
1213 * @chip: nand chip info structure
1214 * @buf: buffer to store read data
1215 * @oob_required: caller requires OOB data read to chip->oob_poi
1216 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001217 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001218 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001219 */
William Juul52c07962007-10-31 13:53:06 +01001220static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001221 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001222{
William Juul52c07962007-10-31 13:53:06 +01001223 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001224 if (oob_required)
1225 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01001226 return 0;
1227}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001228
William Juul52c07962007-10-31 13:53:06 +01001229/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001230 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1231 * @mtd: mtd info structure
1232 * @chip: nand chip info structure
1233 * @buf: buffer to store read data
1234 * @oob_required: caller requires OOB data read to chip->oob_poi
1235 * @page: page number to read
David Brownellee86b8d2009-11-07 16:27:01 -05001236 *
1237 * We need a special oob layout and handling even when OOB isn't used.
1238 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001239static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001240 struct nand_chip *chip, uint8_t *buf,
1241 int oob_required, int page)
David Brownellee86b8d2009-11-07 16:27:01 -05001242{
1243 int eccsize = chip->ecc.size;
1244 int eccbytes = chip->ecc.bytes;
1245 uint8_t *oob = chip->oob_poi;
1246 int steps, size;
1247
1248 for (steps = chip->ecc.steps; steps > 0; steps--) {
1249 chip->read_buf(mtd, buf, eccsize);
1250 buf += eccsize;
1251
1252 if (chip->ecc.prepad) {
1253 chip->read_buf(mtd, oob, chip->ecc.prepad);
1254 oob += chip->ecc.prepad;
1255 }
1256
1257 chip->read_buf(mtd, oob, eccbytes);
1258 oob += eccbytes;
1259
1260 if (chip->ecc.postpad) {
1261 chip->read_buf(mtd, oob, chip->ecc.postpad);
1262 oob += chip->ecc.postpad;
1263 }
1264 }
1265
1266 size = mtd->oobsize - (oob - chip->oob_poi);
1267 if (size)
1268 chip->read_buf(mtd, oob, size);
1269
1270 return 0;
1271}
1272
1273/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001274 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1275 * @mtd: mtd info structure
1276 * @chip: nand chip info structure
1277 * @buf: buffer to store read data
1278 * @oob_required: caller requires OOB data read to chip->oob_poi
1279 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001280 */
1281static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001282 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001283{
1284 int i, eccsize = chip->ecc.size;
1285 int eccbytes = chip->ecc.bytes;
1286 int eccsteps = chip->ecc.steps;
1287 uint8_t *p = buf;
1288 uint8_t *ecc_calc = chip->buffers->ecccalc;
1289 uint8_t *ecc_code = chip->buffers->ecccode;
1290 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001291 unsigned int max_bitflips = 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001292
Sergey Lapin3a38a552013-01-14 03:46:50 +00001293 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001294
William Juul52c07962007-10-31 13:53:06 +01001295 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1296 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001297
William Juul52c07962007-10-31 13:53:06 +01001298 for (i = 0; i < chip->ecc.total; i++)
1299 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001300
William Juul52c07962007-10-31 13:53:06 +01001301 eccsteps = chip->ecc.steps;
1302 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001303
William Juul52c07962007-10-31 13:53:06 +01001304 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1305 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001306
William Juul52c07962007-10-31 13:53:06 +01001307 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001308 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001309 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001310 } else {
William Juul52c07962007-10-31 13:53:06 +01001311 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001312 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1313 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001314 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001315 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001316}
1317
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001318/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001319 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapin3a38a552013-01-14 03:46:50 +00001320 * @mtd: mtd info structure
1321 * @chip: nand chip info structure
1322 * @data_offs: offset of requested data within the page
1323 * @readlen: data length
1324 * @bufpoi: buffer to store read data
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001325 * @page: page number to read
Scott Wood3628f002008-10-24 16:20:43 -05001326 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02001327static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001328 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1329 int page)
Scott Wood3628f002008-10-24 16:20:43 -05001330{
1331 int start_step, end_step, num_steps;
1332 uint32_t *eccpos = chip->ecc.layout->eccpos;
1333 uint8_t *p;
1334 int data_col_addr, i, gaps = 0;
1335 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1336 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001337 int index;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001338 unsigned int max_bitflips = 0;
Scott Wood3628f002008-10-24 16:20:43 -05001339
Sergey Lapin3a38a552013-01-14 03:46:50 +00001340 /* Column address within the page aligned to ECC size (256bytes) */
Scott Wood3628f002008-10-24 16:20:43 -05001341 start_step = data_offs / chip->ecc.size;
1342 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1343 num_steps = end_step - start_step + 1;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001344 index = start_step * chip->ecc.bytes;
Scott Wood3628f002008-10-24 16:20:43 -05001345
Sergey Lapin3a38a552013-01-14 03:46:50 +00001346 /* Data size aligned to ECC ecc.size */
Scott Wood3628f002008-10-24 16:20:43 -05001347 datafrag_len = num_steps * chip->ecc.size;
1348 eccfrag_len = num_steps * chip->ecc.bytes;
1349
1350 data_col_addr = start_step * chip->ecc.size;
1351 /* If we read not a page aligned data */
1352 if (data_col_addr != 0)
1353 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1354
1355 p = bufpoi + data_col_addr;
1356 chip->read_buf(mtd, p, datafrag_len);
1357
Sergey Lapin3a38a552013-01-14 03:46:50 +00001358 /* Calculate ECC */
Scott Wood3628f002008-10-24 16:20:43 -05001359 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1360 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1361
Sergey Lapin3a38a552013-01-14 03:46:50 +00001362 /*
1363 * The performance is faster if we position offsets according to
1364 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1365 */
Scott Wood3628f002008-10-24 16:20:43 -05001366 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001367 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Wood3628f002008-10-24 16:20:43 -05001368 gaps = 1;
1369 break;
1370 }
1371 }
1372 if (gaps) {
1373 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1374 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1375 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001376 /*
1377 * Send the command to read the particular ECC bytes take care
1378 * about buswidth alignment in read_buf.
1379 */
Christian Hitzb8a6b372011-10-12 09:32:02 +02001380 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Wood3628f002008-10-24 16:20:43 -05001381 aligned_len = eccfrag_len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001382 if (eccpos[index] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001383 aligned_len++;
Christian Hitzb8a6b372011-10-12 09:32:02 +02001384 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Wood3628f002008-10-24 16:20:43 -05001385 aligned_len++;
1386
Christian Hitzb8a6b372011-10-12 09:32:02 +02001387 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1388 mtd->writesize + aligned_pos, -1);
Scott Wood3628f002008-10-24 16:20:43 -05001389 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1390 }
1391
1392 for (i = 0; i < eccfrag_len; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02001393 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Wood3628f002008-10-24 16:20:43 -05001394
1395 p = bufpoi + data_col_addr;
1396 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1397 int stat;
1398
Christian Hitzb8a6b372011-10-12 09:32:02 +02001399 stat = chip->ecc.correct(mtd, p,
1400 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001401 if (stat == -EBADMSG &&
1402 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1403 /* check for empty pages with bitflips */
1404 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1405 &chip->buffers->ecccode[i],
1406 chip->ecc.bytes,
1407 NULL, 0,
1408 chip->ecc.strength);
1409 }
1410
Heiko Schocherf5895d12014-06-24 10:10:04 +02001411 if (stat < 0) {
Scott Wood3628f002008-10-24 16:20:43 -05001412 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001413 } else {
Scott Wood3628f002008-10-24 16:20:43 -05001414 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001415 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1416 }
Scott Wood3628f002008-10-24 16:20:43 -05001417 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001418 return max_bitflips;
Scott Wood3628f002008-10-24 16:20:43 -05001419}
1420
1421/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001422 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1423 * @mtd: mtd info structure
1424 * @chip: nand chip info structure
1425 * @buf: buffer to store read data
1426 * @oob_required: caller requires OOB data read to chip->oob_poi
1427 * @page: page number to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001428 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001429 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001430 */
William Juul52c07962007-10-31 13:53:06 +01001431static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001432 uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001433{
William Juul52c07962007-10-31 13:53:06 +01001434 int i, eccsize = chip->ecc.size;
1435 int eccbytes = chip->ecc.bytes;
1436 int eccsteps = chip->ecc.steps;
1437 uint8_t *p = buf;
1438 uint8_t *ecc_calc = chip->buffers->ecccalc;
1439 uint8_t *ecc_code = chip->buffers->ecccode;
1440 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001441 unsigned int max_bitflips = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001442
William Juul52c07962007-10-31 13:53:06 +01001443 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1444 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1445 chip->read_buf(mtd, p, eccsize);
1446 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1447 }
1448 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001449
William Juul52c07962007-10-31 13:53:06 +01001450 for (i = 0; i < chip->ecc.total; i++)
1451 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001452
William Juul52c07962007-10-31 13:53:06 +01001453 eccsteps = chip->ecc.steps;
1454 p = buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001455
William Juul52c07962007-10-31 13:53:06 +01001456 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1457 int stat;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001458
William Juul52c07962007-10-31 13:53:06 +01001459 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001460 if (stat == -EBADMSG &&
1461 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1462 /* check for empty pages with bitflips */
1463 stat = nand_check_erased_ecc_chunk(p, eccsize,
1464 &ecc_code[i], eccbytes,
1465 NULL, 0,
1466 chip->ecc.strength);
1467 }
1468
Heiko Schocherf5895d12014-06-24 10:10:04 +02001469 if (stat < 0) {
William Juul52c07962007-10-31 13:53:06 +01001470 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001471 } else {
William Juul52c07962007-10-31 13:53:06 +01001472 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001473 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1474 }
William Juul52c07962007-10-31 13:53:06 +01001475 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001476 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001477}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001478
William Juul52c07962007-10-31 13:53:06 +01001479/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001480 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1481 * @mtd: mtd info structure
1482 * @chip: nand chip info structure
1483 * @buf: buffer to store read data
1484 * @oob_required: caller requires OOB data read to chip->oob_poi
1485 * @page: page number to read
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001486 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001487 * Hardware ECC for large page chips, require OOB to be read first. For this
1488 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1489 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1490 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1491 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001492 */
1493static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001494 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001495{
1496 int i, eccsize = chip->ecc.size;
1497 int eccbytes = chip->ecc.bytes;
1498 int eccsteps = chip->ecc.steps;
1499 uint8_t *p = buf;
1500 uint8_t *ecc_code = chip->buffers->ecccode;
1501 uint32_t *eccpos = chip->ecc.layout->eccpos;
1502 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001503 unsigned int max_bitflips = 0;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001504
1505 /* Read the OOB area first */
1506 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1507 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1508 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1509
1510 for (i = 0; i < chip->ecc.total; i++)
1511 ecc_code[i] = chip->oob_poi[eccpos[i]];
1512
1513 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1514 int stat;
1515
1516 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1517 chip->read_buf(mtd, p, eccsize);
1518 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1519
1520 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001521 if (stat == -EBADMSG &&
1522 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1523 /* check for empty pages with bitflips */
1524 stat = nand_check_erased_ecc_chunk(p, eccsize,
1525 &ecc_code[i], eccbytes,
1526 NULL, 0,
1527 chip->ecc.strength);
1528 }
1529
Heiko Schocherf5895d12014-06-24 10:10:04 +02001530 if (stat < 0) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001531 mtd->ecc_stats.failed++;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001532 } else {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001533 mtd->ecc_stats.corrected += stat;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001534 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1535 }
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001536 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001537 return max_bitflips;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04001538}
1539
1540/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001541 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1542 * @mtd: mtd info structure
1543 * @chip: nand chip info structure
1544 * @buf: buffer to store read data
1545 * @oob_required: caller requires OOB data read to chip->oob_poi
1546 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001547 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001548 * The hw generator calculates the error syndrome automatically. Therefore we
1549 * need a special oob layout and handling.
William Juul52c07962007-10-31 13:53:06 +01001550 */
1551static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001552 uint8_t *buf, int oob_required, int page)
William Juul52c07962007-10-31 13:53:06 +01001553{
1554 int i, eccsize = chip->ecc.size;
1555 int eccbytes = chip->ecc.bytes;
1556 int eccsteps = chip->ecc.steps;
Scott Wood52ab7ce2016-05-30 13:57:58 -05001557 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juul52c07962007-10-31 13:53:06 +01001558 uint8_t *p = buf;
1559 uint8_t *oob = chip->oob_poi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001560 unsigned int max_bitflips = 0;
William Juul52c07962007-10-31 13:53:06 +01001561
1562 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1563 int stat;
1564
1565 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1566 chip->read_buf(mtd, p, eccsize);
1567
1568 if (chip->ecc.prepad) {
1569 chip->read_buf(mtd, oob, chip->ecc.prepad);
1570 oob += chip->ecc.prepad;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001571 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001572
William Juul52c07962007-10-31 13:53:06 +01001573 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1574 chip->read_buf(mtd, oob, eccbytes);
1575 stat = chip->ecc.correct(mtd, p, oob, NULL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001576
William Juul52c07962007-10-31 13:53:06 +01001577 oob += eccbytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001578
William Juul52c07962007-10-31 13:53:06 +01001579 if (chip->ecc.postpad) {
1580 chip->read_buf(mtd, oob, chip->ecc.postpad);
1581 oob += chip->ecc.postpad;
1582 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05001583
1584 if (stat == -EBADMSG &&
1585 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1586 /* check for empty pages with bitflips */
1587 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1588 oob - eccpadbytes,
1589 eccpadbytes,
1590 NULL, 0,
1591 chip->ecc.strength);
1592 }
1593
1594 if (stat < 0) {
1595 mtd->ecc_stats.failed++;
1596 } else {
1597 mtd->ecc_stats.corrected += stat;
1598 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1599 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001600 }
William Juul52c07962007-10-31 13:53:06 +01001601
1602 /* Calculate remaining oob bytes */
1603 i = mtd->oobsize - (oob - chip->oob_poi);
1604 if (i)
1605 chip->read_buf(mtd, oob, i);
1606
Heiko Schocherf5895d12014-06-24 10:10:04 +02001607 return max_bitflips;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001608}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001609
1610/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001611 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1612 * @chip: nand chip structure
1613 * @oob: oob destination address
1614 * @ops: oob ops structure
1615 * @len: size of oob to transfer
William Juul52c07962007-10-31 13:53:06 +01001616 */
1617static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1618 struct mtd_oob_ops *ops, size_t len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001619{
Christian Hitz13fc0e22011-10-12 09:32:01 +02001620 switch (ops->mode) {
William Juul52c07962007-10-31 13:53:06 +01001621
Sergey Lapin3a38a552013-01-14 03:46:50 +00001622 case MTD_OPS_PLACE_OOB:
1623 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01001624 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1625 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001626
Sergey Lapin3a38a552013-01-14 03:46:50 +00001627 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01001628 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1629 uint32_t boffs = 0, roffs = ops->ooboffs;
1630 size_t bytes = 0;
1631
Christian Hitz13fc0e22011-10-12 09:32:01 +02001632 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001633 /* Read request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01001634 if (unlikely(roffs)) {
1635 if (roffs >= free->length) {
1636 roffs -= free->length;
1637 continue;
1638 }
1639 boffs = free->offset + roffs;
1640 bytes = min_t(size_t, len,
1641 (free->length - roffs));
1642 roffs = 0;
1643 } else {
1644 bytes = min_t(size_t, len, free->length);
1645 boffs = free->offset;
1646 }
1647 memcpy(oob, chip->oob_poi + boffs, bytes);
1648 oob += bytes;
1649 }
1650 return oob;
1651 }
1652 default:
1653 BUG();
1654 }
1655 return NULL;
1656}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001657
1658/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001659 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1660 * @mtd: MTD device structure
1661 * @retry_mode: the retry mode to use
1662 *
1663 * Some vendors supply a special command to shift the Vt threshold, to be used
1664 * when there are too many bitflips in a page (i.e., ECC error). After setting
1665 * a new threshold, the host should retry reading the page.
1666 */
1667static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1668{
Scott Wood17fed142016-05-30 13:57:56 -05001669 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001670
1671 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1672
1673 if (retry_mode >= chip->read_retries)
1674 return -EINVAL;
1675
1676 if (!chip->setup_read_retry)
1677 return -EOPNOTSUPP;
1678
1679 return chip->setup_read_retry(mtd, retry_mode);
1680}
1681
1682/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001683 * nand_do_read_ops - [INTERN] Read data with ECC
1684 * @mtd: MTD device structure
1685 * @from: offset to read from
1686 * @ops: oob ops structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001687 *
William Juul52c07962007-10-31 13:53:06 +01001688 * Internal function. Called with chip held.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001689 */
William Juul52c07962007-10-31 13:53:06 +01001690static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1691 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001692{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001693 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17fed142016-05-30 13:57:56 -05001694 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01001695 int ret = 0;
1696 uint32_t readlen = ops->len;
1697 uint32_t oobreadlen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05001698 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02001699
William Juul52c07962007-10-31 13:53:06 +01001700 uint8_t *bufpoi, *oob, *buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001701 int use_bufpoi;
Paul Burton700a76c2013-09-04 15:16:56 +01001702 unsigned int max_bitflips = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001703 int retry_mode = 0;
1704 bool ecc_fail = false;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001705
William Juul52c07962007-10-31 13:53:06 +01001706 chipnr = (int)(from >> chip->chip_shift);
1707 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001708
William Juul52c07962007-10-31 13:53:06 +01001709 realpage = (int)(from >> chip->page_shift);
1710 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001711
William Juul52c07962007-10-31 13:53:06 +01001712 col = (int)(from & (mtd->writesize - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001713
William Juul52c07962007-10-31 13:53:06 +01001714 buf = ops->datbuf;
1715 oob = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001716 oob_required = oob ? 1 : 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001717
Christian Hitz13fc0e22011-10-12 09:32:01 +02001718 while (1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001719 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Woodea95b642011-02-02 18:15:57 -06001720
Heiko Schocherf5895d12014-06-24 10:10:04 +02001721 WATCHDOG_RESET();
William Juul52c07962007-10-31 13:53:06 +01001722 bytes = min(mtd->writesize - col, readlen);
1723 aligned = (bytes == mtd->writesize);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001724
Scott Wood3ea94ed2015-06-26 19:03:26 -05001725 if (!aligned)
1726 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09001727 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
1728 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
1729 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001730 else
1731 use_bufpoi = 0;
1732
Sergey Lapin3a38a552013-01-14 03:46:50 +00001733 /* Is the current page in the buffer? */
William Juul52c07962007-10-31 13:53:06 +01001734 if (realpage != chip->pagebuf || oob) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001735 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1736
1737 if (use_bufpoi && aligned)
1738 pr_debug("%s: using read bounce buffer for buf@%p\n",
1739 __func__, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001740
Heiko Schocherf5895d12014-06-24 10:10:04 +02001741read_retry:
Marc Gonzalezc3a29852017-11-22 02:38:22 +09001742 if (nand_standard_page_accessors(&chip->ecc))
1743 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001744
Paul Burton700a76c2013-09-04 15:16:56 +01001745 /*
1746 * Now read the page into the buffer. Absent an error,
1747 * the read methods return max bitflips per ecc step.
1748 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001749 if (unlikely(ops->mode == MTD_OPS_RAW))
1750 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1751 oob_required,
1752 page);
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001753 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001754 !oob)
Christian Hitz13fc0e22011-10-12 09:32:01 +02001755 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001756 col, bytes, bufpoi,
1757 page);
William Juul52c07962007-10-31 13:53:06 +01001758 else
Sandeep Paulraj883189e2009-08-10 13:27:46 -04001759 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001760 oob_required, page);
1761 if (ret < 0) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05001762 if (use_bufpoi)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001763 /* Invalidate page cache */
1764 chip->pagebuf = -1;
William Juul52c07962007-10-31 13:53:06 +01001765 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001766 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001767
Paul Burton700a76c2013-09-04 15:16:56 +01001768 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1769
William Juul52c07962007-10-31 13:53:06 +01001770 /* Transfer not aligned data */
Scott Wood3ea94ed2015-06-26 19:03:26 -05001771 if (use_bufpoi) {
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00001772 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02001773 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton700a76c2013-09-04 15:16:56 +01001774 (ops->mode != MTD_OPS_RAW)) {
Scott Wood3628f002008-10-24 16:20:43 -05001775 chip->pagebuf = realpage;
Paul Burton700a76c2013-09-04 15:16:56 +01001776 chip->pagebuf_bitflips = ret;
1777 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001778 /* Invalidate page cache */
1779 chip->pagebuf = -1;
Paul Burton700a76c2013-09-04 15:16:56 +01001780 }
William Juul52c07962007-10-31 13:53:06 +01001781 memcpy(buf, chip->buffers->databuf + col, bytes);
1782 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001783
William Juul52c07962007-10-31 13:53:06 +01001784 if (unlikely(oob)) {
Christian Hitzb8a6b372011-10-12 09:32:02 +02001785 int toread = min(oobreadlen, max_oobsize);
1786
1787 if (toread) {
1788 oob = nand_transfer_oob(chip,
1789 oob, ops, toread);
1790 oobreadlen -= toread;
1791 }
William Juul52c07962007-10-31 13:53:06 +01001792 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001793
1794 if (chip->options & NAND_NEED_READRDY) {
1795 /* Apply delay or wait for ready/busy pin */
1796 if (!chip->dev_ready)
1797 udelay(chip->chip_delay);
1798 else
1799 nand_wait_ready(mtd);
1800 }
1801
1802 if (mtd->ecc_stats.failed - ecc_failures) {
1803 if (retry_mode + 1 < chip->read_retries) {
1804 retry_mode++;
1805 ret = nand_setup_read_retry(mtd,
1806 retry_mode);
1807 if (ret < 0)
1808 break;
1809
1810 /* Reset failures; retry */
1811 mtd->ecc_stats.failed = ecc_failures;
1812 goto read_retry;
1813 } else {
1814 /* No more retry modes; real failure */
1815 ecc_fail = true;
1816 }
1817 }
1818
1819 buf += bytes;
William Juul52c07962007-10-31 13:53:06 +01001820 } else {
1821 memcpy(buf, chip->buffers->databuf + col, bytes);
1822 buf += bytes;
Paul Burton700a76c2013-09-04 15:16:56 +01001823 max_bitflips = max_t(unsigned int, max_bitflips,
1824 chip->pagebuf_bitflips);
William Juul52c07962007-10-31 13:53:06 +01001825 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001826
William Juul52c07962007-10-31 13:53:06 +01001827 readlen -= bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001828
Heiko Schocherf5895d12014-06-24 10:10:04 +02001829 /* Reset to retry mode 0 */
1830 if (retry_mode) {
1831 ret = nand_setup_read_retry(mtd, 0);
1832 if (ret < 0)
1833 break;
1834 retry_mode = 0;
1835 }
1836
William Juul52c07962007-10-31 13:53:06 +01001837 if (!readlen)
1838 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001839
Sergey Lapin3a38a552013-01-14 03:46:50 +00001840 /* For subsequent reads align to page boundary */
William Juul52c07962007-10-31 13:53:06 +01001841 col = 0;
1842 /* Increment page address */
1843 realpage++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001844
William Juul52c07962007-10-31 13:53:06 +01001845 page = realpage & chip->pagemask;
1846 /* Check, if we cross a chip boundary */
1847 if (!page) {
1848 chipnr++;
1849 chip->select_chip(mtd, -1);
1850 chip->select_chip(mtd, chipnr);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001851 }
William Juul52c07962007-10-31 13:53:06 +01001852 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001853 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001854
William Juul52c07962007-10-31 13:53:06 +01001855 ops->retlen = ops->len - (size_t) readlen;
1856 if (oob)
1857 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001858
Heiko Schocherf5895d12014-06-24 10:10:04 +02001859 if (ret < 0)
William Juul52c07962007-10-31 13:53:06 +01001860 return ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001861
Heiko Schocherf5895d12014-06-24 10:10:04 +02001862 if (ecc_fail)
William Juul52c07962007-10-31 13:53:06 +01001863 return -EBADMSG;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001864
Paul Burton700a76c2013-09-04 15:16:56 +01001865 return max_bitflips;
William Juul52c07962007-10-31 13:53:06 +01001866}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001867
William Juul52c07962007-10-31 13:53:06 +01001868/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001869 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1870 * @mtd: mtd info structure
1871 * @chip: nand chip info structure
1872 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001873 */
1874static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001875 int page)
William Juul52c07962007-10-31 13:53:06 +01001876{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001877 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juul52c07962007-10-31 13:53:06 +01001878 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001879 return 0;
William Juul52c07962007-10-31 13:53:06 +01001880}
1881
1882/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001883 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01001884 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00001885 * @mtd: mtd info structure
1886 * @chip: nand chip info structure
1887 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001888 */
1889static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001890 int page)
William Juul52c07962007-10-31 13:53:06 +01001891{
William Juul52c07962007-10-31 13:53:06 +01001892 int length = mtd->oobsize;
1893 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1894 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001895 uint8_t *bufpoi = chip->oob_poi;
William Juul52c07962007-10-31 13:53:06 +01001896 int i, toread, sndrnd = 0, pos;
1897
1898 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1899 for (i = 0; i < chip->ecc.steps; i++) {
1900 if (sndrnd) {
1901 pos = eccsize + i * (eccsize + chunk);
1902 if (mtd->writesize > 512)
1903 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1904 else
1905 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001906 } else
William Juul52c07962007-10-31 13:53:06 +01001907 sndrnd = 1;
1908 toread = min_t(int, length, chunk);
1909 chip->read_buf(mtd, bufpoi, toread);
1910 bufpoi += toread;
1911 length -= toread;
1912 }
1913 if (length > 0)
1914 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001915
Sergey Lapin3a38a552013-01-14 03:46:50 +00001916 return 0;
William Juul52c07962007-10-31 13:53:06 +01001917}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001918
William Juul52c07962007-10-31 13:53:06 +01001919/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001920 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1921 * @mtd: mtd info structure
1922 * @chip: nand chip info structure
1923 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001924 */
1925static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1926 int page)
1927{
1928 int status = 0;
1929 const uint8_t *buf = chip->oob_poi;
1930 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001931
William Juul52c07962007-10-31 13:53:06 +01001932 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1933 chip->write_buf(mtd, buf, length);
1934 /* Send command to program the OOB data */
1935 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001936
William Juul52c07962007-10-31 13:53:06 +01001937 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001938
William Juul52c07962007-10-31 13:53:06 +01001939 return status & NAND_STATUS_FAIL ? -EIO : 0;
1940}
1941
1942/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001943 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1944 * with syndrome - only for large page flash
1945 * @mtd: mtd info structure
1946 * @chip: nand chip info structure
1947 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001948 */
1949static int nand_write_oob_syndrome(struct mtd_info *mtd,
1950 struct nand_chip *chip, int page)
1951{
1952 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1953 int eccsize = chip->ecc.size, length = mtd->oobsize;
1954 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1955 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001956
1957 /*
William Juul52c07962007-10-31 13:53:06 +01001958 * data-ecc-data-ecc ... ecc-oob
1959 * or
1960 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001961 */
William Juul52c07962007-10-31 13:53:06 +01001962 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1963 pos = steps * (eccsize + chunk);
1964 steps = 0;
1965 } else
1966 pos = eccsize;
1967
1968 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1969 for (i = 0; i < steps; i++) {
1970 if (sndcmd) {
1971 if (mtd->writesize <= 512) {
1972 uint32_t fill = 0xFFFFFFFF;
1973
1974 len = eccsize;
1975 while (len > 0) {
1976 int num = min_t(int, len, 4);
1977 chip->write_buf(mtd, (uint8_t *)&fill,
1978 num);
1979 len -= num;
1980 }
1981 } else {
1982 pos = eccsize + i * (eccsize + chunk);
1983 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1984 }
1985 } else
1986 sndcmd = 1;
1987 len = min_t(int, length, chunk);
1988 chip->write_buf(mtd, bufpoi, len);
1989 bufpoi += len;
1990 length -= len;
1991 }
1992 if (length > 0)
1993 chip->write_buf(mtd, bufpoi, length);
1994
1995 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1996 status = chip->waitfunc(mtd, chip);
1997
1998 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001999}
2000
2001/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002002 * nand_do_read_oob - [INTERN] NAND read out-of-band
2003 * @mtd: MTD device structure
2004 * @from: offset to read from
2005 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002006 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002007 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002008 */
William Juul52c07962007-10-31 13:53:06 +01002009static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2010 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002011{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002012 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002013 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002014 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002015 int readlen = ops->ooblen;
2016 int len;
2017 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002018 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002019
Heiko Schocherf5895d12014-06-24 10:10:04 +02002020 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002021 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002022
Sergey Lapin3a38a552013-01-14 03:46:50 +00002023 stats = mtd->ecc_stats;
2024
Scott Wood52ab7ce2016-05-30 13:57:58 -05002025 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002026
William Juul52c07962007-10-31 13:53:06 +01002027 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002028 pr_debug("%s: attempt to start read outside oob\n",
2029 __func__);
William Juul52c07962007-10-31 13:53:06 +01002030 return -EINVAL;
2031 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002032
2033 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002034 if (unlikely(from >= mtd->size ||
2035 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2036 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002037 pr_debug("%s: attempt to read beyond end of device\n",
2038 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002039 return -EINVAL;
2040 }
2041
William Juul52c07962007-10-31 13:53:06 +01002042 chipnr = (int)(from >> chip->chip_shift);
2043 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002044
William Juul52c07962007-10-31 13:53:06 +01002045 /* Shift to get page */
2046 realpage = (int)(from >> chip->page_shift);
2047 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002048
Christian Hitz13fc0e22011-10-12 09:32:01 +02002049 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002050 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002051
Sergey Lapin3a38a552013-01-14 03:46:50 +00002052 if (ops->mode == MTD_OPS_RAW)
2053 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2054 else
2055 ret = chip->ecc.read_oob(mtd, chip, page);
2056
2057 if (ret < 0)
2058 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002059
William Juul52c07962007-10-31 13:53:06 +01002060 len = min(len, readlen);
2061 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002062
Heiko Schocherf5895d12014-06-24 10:10:04 +02002063 if (chip->options & NAND_NEED_READRDY) {
2064 /* Apply delay or wait for ready/busy pin */
2065 if (!chip->dev_ready)
2066 udelay(chip->chip_delay);
2067 else
2068 nand_wait_ready(mtd);
2069 }
2070
William Juul52c07962007-10-31 13:53:06 +01002071 readlen -= len;
2072 if (!readlen)
2073 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002074
William Juul52c07962007-10-31 13:53:06 +01002075 /* Increment page address */
2076 realpage++;
2077
2078 page = realpage & chip->pagemask;
2079 /* Check, if we cross a chip boundary */
2080 if (!page) {
2081 chipnr++;
2082 chip->select_chip(mtd, -1);
2083 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002084 }
William Juul52c07962007-10-31 13:53:06 +01002085 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002086 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002087
Sergey Lapin3a38a552013-01-14 03:46:50 +00002088 ops->oobretlen = ops->ooblen - readlen;
2089
2090 if (ret < 0)
2091 return ret;
2092
2093 if (mtd->ecc_stats.failed - stats.failed)
2094 return -EBADMSG;
2095
2096 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002097}
2098
2099/**
William Juul52c07962007-10-31 13:53:06 +01002100 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002101 * @mtd: MTD device structure
2102 * @from: offset to read from
2103 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002104 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002105 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002106 */
William Juul52c07962007-10-31 13:53:06 +01002107static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2108 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002109{
William Juul52c07962007-10-31 13:53:06 +01002110 int ret = -ENOTSUPP;
2111
2112 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002113
2114 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002115 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002116 pr_debug("%s: attempt to read beyond end of device\n",
2117 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002118 return -EINVAL;
2119 }
2120
Heiko Schocherf5895d12014-06-24 10:10:04 +02002121 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002122
Christian Hitz13fc0e22011-10-12 09:32:01 +02002123 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002124 case MTD_OPS_PLACE_OOB:
2125 case MTD_OPS_AUTO_OOB:
2126 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002127 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002128
William Juul52c07962007-10-31 13:53:06 +01002129 default:
2130 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002131 }
2132
William Juul52c07962007-10-31 13:53:06 +01002133 if (!ops->datbuf)
2134 ret = nand_do_read_oob(mtd, from, ops);
2135 else
2136 ret = nand_do_read_ops(mtd, from, ops);
2137
Christian Hitz13fc0e22011-10-12 09:32:01 +02002138out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002139 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002140 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002141}
2142
2143
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002144/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002145 * nand_write_page_raw - [INTERN] raw page write function
2146 * @mtd: mtd info structure
2147 * @chip: nand chip info structure
2148 * @buf: data buffer
2149 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002150 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002151 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002152 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002153 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002154static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002155 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002156{
William Juul52c07962007-10-31 13:53:06 +01002157 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002158 if (oob_required)
2159 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2160
2161 return 0;
William Juul52c07962007-10-31 13:53:06 +01002162}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002163
William Juul52c07962007-10-31 13:53:06 +01002164/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002165 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2166 * @mtd: mtd info structure
2167 * @chip: nand chip info structure
2168 * @buf: data buffer
2169 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002170 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002171 *
2172 * We need a special oob layout and handling even when ECC isn't checked.
2173 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002174static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002175 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002176 const uint8_t *buf, int oob_required,
2177 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002178{
2179 int eccsize = chip->ecc.size;
2180 int eccbytes = chip->ecc.bytes;
2181 uint8_t *oob = chip->oob_poi;
2182 int steps, size;
2183
2184 for (steps = chip->ecc.steps; steps > 0; steps--) {
2185 chip->write_buf(mtd, buf, eccsize);
2186 buf += eccsize;
2187
2188 if (chip->ecc.prepad) {
2189 chip->write_buf(mtd, oob, chip->ecc.prepad);
2190 oob += chip->ecc.prepad;
2191 }
2192
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002193 chip->write_buf(mtd, oob, eccbytes);
David Brownellee86b8d2009-11-07 16:27:01 -05002194 oob += eccbytes;
2195
2196 if (chip->ecc.postpad) {
2197 chip->write_buf(mtd, oob, chip->ecc.postpad);
2198 oob += chip->ecc.postpad;
2199 }
2200 }
2201
2202 size = mtd->oobsize - (oob - chip->oob_poi);
2203 if (size)
2204 chip->write_buf(mtd, oob, size);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002205
2206 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002207}
2208/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002209 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2210 * @mtd: mtd info structure
2211 * @chip: nand chip info structure
2212 * @buf: data buffer
2213 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002214 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002215 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002216static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002217 const uint8_t *buf, int oob_required,
2218 int page)
William Juul52c07962007-10-31 13:53:06 +01002219{
2220 int i, eccsize = chip->ecc.size;
2221 int eccbytes = chip->ecc.bytes;
2222 int eccsteps = chip->ecc.steps;
2223 uint8_t *ecc_calc = chip->buffers->ecccalc;
2224 const uint8_t *p = buf;
2225 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002226
Sergey Lapin3a38a552013-01-14 03:46:50 +00002227 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002228 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2229 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002230
William Juul52c07962007-10-31 13:53:06 +01002231 for (i = 0; i < chip->ecc.total; i++)
2232 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002233
Scott Wood46e13102016-05-30 13:57:57 -05002234 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002235}
2236
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002237/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002238 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2239 * @mtd: mtd info structure
2240 * @chip: nand chip info structure
2241 * @buf: data buffer
2242 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002243 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002244 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002245static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002246 const uint8_t *buf, int oob_required,
2247 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002248{
William Juul52c07962007-10-31 13:53:06 +01002249 int i, eccsize = chip->ecc.size;
2250 int eccbytes = chip->ecc.bytes;
2251 int eccsteps = chip->ecc.steps;
2252 uint8_t *ecc_calc = chip->buffers->ecccalc;
2253 const uint8_t *p = buf;
2254 uint32_t *eccpos = chip->ecc.layout->eccpos;
2255
2256 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2257 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2258 chip->write_buf(mtd, p, eccsize);
2259 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2260 }
2261
2262 for (i = 0; i < chip->ecc.total; i++)
2263 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2264
2265 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002266
2267 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002268}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002269
Heiko Schocherf5895d12014-06-24 10:10:04 +02002270
2271/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002272 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002273 * @mtd: mtd info structure
2274 * @chip: nand chip info structure
2275 * @offset: column address of subpage within the page
2276 * @data_len: data length
2277 * @buf: data buffer
2278 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002279 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002280 */
2281static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2282 struct nand_chip *chip, uint32_t offset,
2283 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002284 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002285{
2286 uint8_t *oob_buf = chip->oob_poi;
2287 uint8_t *ecc_calc = chip->buffers->ecccalc;
2288 int ecc_size = chip->ecc.size;
2289 int ecc_bytes = chip->ecc.bytes;
2290 int ecc_steps = chip->ecc.steps;
2291 uint32_t *eccpos = chip->ecc.layout->eccpos;
2292 uint32_t start_step = offset / ecc_size;
2293 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2294 int oob_bytes = mtd->oobsize / ecc_steps;
2295 int step, i;
2296
2297 for (step = 0; step < ecc_steps; step++) {
2298 /* configure controller for WRITE access */
2299 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2300
2301 /* write data (untouched subpages already masked by 0xFF) */
2302 chip->write_buf(mtd, buf, ecc_size);
2303
2304 /* mask ECC of un-touched subpages by padding 0xFF */
2305 if ((step < start_step) || (step > end_step))
2306 memset(ecc_calc, 0xff, ecc_bytes);
2307 else
2308 chip->ecc.calculate(mtd, buf, ecc_calc);
2309
2310 /* mask OOB of un-touched subpages by padding 0xFF */
2311 /* if oob_required, preserve OOB metadata of written subpage */
2312 if (!oob_required || (step < start_step) || (step > end_step))
2313 memset(oob_buf, 0xff, oob_bytes);
2314
2315 buf += ecc_size;
2316 ecc_calc += ecc_bytes;
2317 oob_buf += oob_bytes;
2318 }
2319
2320 /* copy calculated ECC for whole page to chip->buffer->oob */
2321 /* this include masked-value(0xFF) for unwritten subpages */
2322 ecc_calc = chip->buffers->ecccalc;
2323 for (i = 0; i < chip->ecc.total; i++)
2324 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2325
2326 /* write OOB buffer to NAND device */
2327 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2328
2329 return 0;
2330}
2331
2332
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002333/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002334 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2335 * @mtd: mtd info structure
2336 * @chip: nand chip info structure
2337 * @buf: data buffer
2338 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002339 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002340 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002341 * The hw generator calculates the error syndrome automatically. Therefore we
2342 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002343 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002344static int nand_write_page_syndrome(struct mtd_info *mtd,
2345 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002346 const uint8_t *buf, int oob_required,
2347 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002348{
William Juul52c07962007-10-31 13:53:06 +01002349 int i, eccsize = chip->ecc.size;
2350 int eccbytes = chip->ecc.bytes;
2351 int eccsteps = chip->ecc.steps;
2352 const uint8_t *p = buf;
2353 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002354
William Juul52c07962007-10-31 13:53:06 +01002355 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002356
William Juul52c07962007-10-31 13:53:06 +01002357 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2358 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002359
William Juul52c07962007-10-31 13:53:06 +01002360 if (chip->ecc.prepad) {
2361 chip->write_buf(mtd, oob, chip->ecc.prepad);
2362 oob += chip->ecc.prepad;
2363 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002364
William Juul52c07962007-10-31 13:53:06 +01002365 chip->ecc.calculate(mtd, p, oob);
2366 chip->write_buf(mtd, oob, eccbytes);
2367 oob += eccbytes;
2368
2369 if (chip->ecc.postpad) {
2370 chip->write_buf(mtd, oob, chip->ecc.postpad);
2371 oob += chip->ecc.postpad;
2372 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002373 }
2374
William Juul52c07962007-10-31 13:53:06 +01002375 /* Calculate remaining oob bytes */
2376 i = mtd->oobsize - (oob - chip->oob_poi);
2377 if (i)
2378 chip->write_buf(mtd, oob, i);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002379
2380 return 0;
William Juul52c07962007-10-31 13:53:06 +01002381}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002382
William Juul52c07962007-10-31 13:53:06 +01002383/**
2384 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00002385 * @mtd: MTD device structure
2386 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02002387 * @offset: address offset within the page
2388 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00002389 * @buf: the data to write
2390 * @oob_required: must write chip->oob_poi to OOB
2391 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00002392 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01002393 */
2394static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002395 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002396 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01002397{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002398 int status, subpage;
2399
2400 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2401 chip->ecc.write_subpage)
2402 subpage = offset || (data_len < mtd->writesize);
2403 else
2404 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002405
Marc Gonzalezc3a29852017-11-22 02:38:22 +09002406 if (nand_standard_page_accessors(&chip->ecc))
2407 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
William Juul52c07962007-10-31 13:53:06 +01002408
2409 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02002410 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05002411 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002412 else if (subpage)
2413 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002414 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01002415 else
Scott Wood46e13102016-05-30 13:57:57 -05002416 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2417 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002418
2419 if (status < 0)
2420 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002421
Boris Brezillond4949322017-11-22 02:38:26 +09002422 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002423 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002424
Boris Brezillond4949322017-11-22 02:38:26 +09002425 status = chip->waitfunc(mtd, chip);
2426 if (status & NAND_STATUS_FAIL)
2427 return -EIO;
2428 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002429
William Juul52c07962007-10-31 13:53:06 +01002430 return 0;
2431}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002432
William Juul52c07962007-10-31 13:53:06 +01002433/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002434 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2435 * @mtd: MTD device structure
2436 * @oob: oob data buffer
2437 * @len: oob data write length
2438 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01002439 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002440static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2441 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01002442{
Scott Wood17fed142016-05-30 13:57:56 -05002443 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002444
2445 /*
2446 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2447 * data from a previous OOB read.
2448 */
2449 memset(chip->oob_poi, 0xff, mtd->oobsize);
2450
Christian Hitz13fc0e22011-10-12 09:32:01 +02002451 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002452
Sergey Lapin3a38a552013-01-14 03:46:50 +00002453 case MTD_OPS_PLACE_OOB:
2454 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002455 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2456 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002457
Sergey Lapin3a38a552013-01-14 03:46:50 +00002458 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002459 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2460 uint32_t boffs = 0, woffs = ops->ooboffs;
2461 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002462
Christian Hitz13fc0e22011-10-12 09:32:01 +02002463 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002464 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002465 if (unlikely(woffs)) {
2466 if (woffs >= free->length) {
2467 woffs -= free->length;
2468 continue;
2469 }
2470 boffs = free->offset + woffs;
2471 bytes = min_t(size_t, len,
2472 (free->length - woffs));
2473 woffs = 0;
2474 } else {
2475 bytes = min_t(size_t, len, free->length);
2476 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002477 }
William Juul52c07962007-10-31 13:53:06 +01002478 memcpy(chip->oob_poi + boffs, oob, bytes);
2479 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002480 }
William Juul52c07962007-10-31 13:53:06 +01002481 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002482 }
William Juul52c07962007-10-31 13:53:06 +01002483 default:
2484 BUG();
2485 }
2486 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002487}
2488
Christian Hitzb8a6b372011-10-12 09:32:02 +02002489#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002490
2491/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002492 * nand_do_write_ops - [INTERN] NAND write with ECC
2493 * @mtd: MTD device structure
2494 * @to: offset to write to
2495 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002496 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002497 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002498 */
William Juul52c07962007-10-31 13:53:06 +01002499static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2500 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002501{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002502 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05002503 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002504 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002505
2506 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002507 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002508
William Juul52c07962007-10-31 13:53:06 +01002509 uint8_t *oob = ops->oobbuf;
2510 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002511 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002512 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002513
William Juul52c07962007-10-31 13:53:06 +01002514 ops->retlen = 0;
2515 if (!writelen)
2516 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002517
Heiko Schocherf5895d12014-06-24 10:10:04 +02002518 /* Reject writes, which are not page aligned */
2519 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002520 pr_notice("%s: attempt to write non page aligned data\n",
2521 __func__);
William Juul52c07962007-10-31 13:53:06 +01002522 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002523 }
2524
2525 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002526
William Juul52c07962007-10-31 13:53:06 +01002527 chipnr = (int)(to >> chip->chip_shift);
2528 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002529
2530 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01002531 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002532 ret = -EIO;
2533 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01002534 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002535
William Juul52c07962007-10-31 13:53:06 +01002536 realpage = (int)(to >> chip->page_shift);
2537 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002538
William Juul52c07962007-10-31 13:53:06 +01002539 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002540 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2541 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01002542 chip->pagebuf = -1;
2543
Christian Hitzb8a6b372011-10-12 09:32:02 +02002544 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002545 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2546 ret = -EINVAL;
2547 goto err_out;
2548 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02002549
Christian Hitz13fc0e22011-10-12 09:32:01 +02002550 while (1) {
William Juul52c07962007-10-31 13:53:06 +01002551 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01002552 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002553 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02002554 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002555
2556 if (part_pagewr)
2557 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002558 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2559 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2560 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002561 else
2562 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01002563
Heiko Schocherf5895d12014-06-24 10:10:04 +02002564 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05002565 /* Partial page write?, or need to use bounce buffer */
2566 if (use_bufpoi) {
2567 pr_debug("%s: using write bounce buffer for buf@%p\n",
2568 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002569 if (part_pagewr)
2570 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01002571 chip->pagebuf = -1;
2572 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2573 memcpy(&chip->buffers->databuf[column], buf, bytes);
2574 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002575 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002576
Christian Hitzb8a6b372011-10-12 09:32:02 +02002577 if (unlikely(oob)) {
2578 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002579 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002580 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002581 } else {
2582 /* We still need to erase leftover OOB data */
2583 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002584 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002585 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002586 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002587 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01002588 if (ret)
2589 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002590
William Juul52c07962007-10-31 13:53:06 +01002591 writelen -= bytes;
2592 if (!writelen)
2593 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002594
Heiko Schocherf5895d12014-06-24 10:10:04 +02002595 column = 0;
2596 buf += bytes;
2597 realpage++;
2598
2599 page = realpage & chip->pagemask;
2600 /* Check, if we cross a chip boundary */
2601 if (!page) {
2602 chipnr++;
2603 chip->select_chip(mtd, -1);
2604 chip->select_chip(mtd, chipnr);
2605 }
2606 }
2607
2608 ops->retlen = ops->len - writelen;
2609 if (unlikely(oob))
2610 ops->oobretlen = ops->ooblen;
2611
2612err_out:
2613 chip->select_chip(mtd, -1);
2614 return ret;
2615}
2616
2617/**
2618 * panic_nand_write - [MTD Interface] NAND write with ECC
2619 * @mtd: MTD device structure
2620 * @to: offset to write to
2621 * @len: number of bytes to write
2622 * @retlen: pointer to variable to store the number of written bytes
2623 * @buf: the data to write
2624 *
2625 * NAND write with ECC. Used when performing writes in interrupt context, this
2626 * may for example be called by mtdoops when writing an oops while in panic.
2627 */
2628static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2629 size_t *retlen, const uint8_t *buf)
2630{
Scott Wood17fed142016-05-30 13:57:56 -05002631 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002632 struct mtd_oob_ops ops;
2633 int ret;
2634
2635 /* Wait for the device to get ready */
2636 panic_nand_wait(mtd, chip, 400);
2637
2638 /* Grab the device */
2639 panic_nand_get_device(chip, mtd, FL_WRITING);
2640
Scott Wood3ea94ed2015-06-26 19:03:26 -05002641 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02002642 ops.len = len;
2643 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002644 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01002645
Heiko Schocherf5895d12014-06-24 10:10:04 +02002646 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002647
Sergey Lapin3a38a552013-01-14 03:46:50 +00002648 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01002649 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002650}
2651
2652/**
William Juul52c07962007-10-31 13:53:06 +01002653 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002654 * @mtd: MTD device structure
2655 * @to: offset to write to
2656 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002657 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002658 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002659 */
William Juul52c07962007-10-31 13:53:06 +01002660static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2661 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002662{
William Juul52c07962007-10-31 13:53:06 +01002663 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05002664 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002665
Heiko Schocherf5895d12014-06-24 10:10:04 +02002666 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002667 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002668
Scott Wood52ab7ce2016-05-30 13:57:58 -05002669 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002670
2671 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002672 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002673 pr_debug("%s: attempt to write past end of page\n",
2674 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002675 return -EINVAL;
2676 }
2677
William Juul52c07962007-10-31 13:53:06 +01002678 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002679 pr_debug("%s: attempt to start write outside oob\n",
2680 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002681 return -EINVAL;
2682 }
2683
Christian Hitz13fc0e22011-10-12 09:32:01 +02002684 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002685 if (unlikely(to >= mtd->size ||
2686 ops->ooboffs + ops->ooblen >
2687 ((mtd->size >> chip->page_shift) -
2688 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002689 pr_debug("%s: attempt to write beyond end of device\n",
2690 __func__);
William Juul52c07962007-10-31 13:53:06 +01002691 return -EINVAL;
2692 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002693
William Juul52c07962007-10-31 13:53:06 +01002694 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01002695
2696 /*
2697 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2698 * of my DiskOnChip 2000 test units) will clear the whole data page too
2699 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2700 * it in the doc2000 driver in August 1999. dwmw2.
2701 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09002702 nand_reset(chip, chipnr);
2703
2704 chip->select_chip(mtd, chipnr);
2705
2706 /* Shift to get page */
2707 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002708
2709 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002710 if (nand_check_wp(mtd)) {
2711 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01002712 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002713 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002714
William Juul52c07962007-10-31 13:53:06 +01002715 /* Invalidate the page cache, if we write to the cached page */
2716 if (page == chip->pagebuf)
2717 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002718
Sergey Lapin3a38a552013-01-14 03:46:50 +00002719 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2720
2721 if (ops->mode == MTD_OPS_RAW)
2722 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2723 else
2724 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002725
Heiko Schocherf5895d12014-06-24 10:10:04 +02002726 chip->select_chip(mtd, -1);
2727
William Juul52c07962007-10-31 13:53:06 +01002728 if (status)
2729 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002730
William Juul52c07962007-10-31 13:53:06 +01002731 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002732
William Juul52c07962007-10-31 13:53:06 +01002733 return 0;
2734}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002735
William Juul52c07962007-10-31 13:53:06 +01002736/**
2737 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002738 * @mtd: MTD device structure
2739 * @to: offset to write to
2740 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01002741 */
2742static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2743 struct mtd_oob_ops *ops)
2744{
William Juul52c07962007-10-31 13:53:06 +01002745 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002746
William Juul52c07962007-10-31 13:53:06 +01002747 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002748
William Juul52c07962007-10-31 13:53:06 +01002749 /* Do not allow writes past end of device */
2750 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002751 pr_debug("%s: attempt to write beyond end of device\n",
2752 __func__);
William Juul52c07962007-10-31 13:53:06 +01002753 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002754 }
William Juul52c07962007-10-31 13:53:06 +01002755
Heiko Schocherf5895d12014-06-24 10:10:04 +02002756 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01002757
Christian Hitz13fc0e22011-10-12 09:32:01 +02002758 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002759 case MTD_OPS_PLACE_OOB:
2760 case MTD_OPS_AUTO_OOB:
2761 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002762 break;
2763
2764 default:
2765 goto out;
2766 }
2767
2768 if (!ops->datbuf)
2769 ret = nand_do_write_oob(mtd, to, ops);
2770 else
2771 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002772
Christian Hitz13fc0e22011-10-12 09:32:01 +02002773out:
William Juul52c07962007-10-31 13:53:06 +01002774 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002775 return ret;
2776}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002777
2778/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002779 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00002780 * @mtd: MTD device structure
2781 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002782 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05002783 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002784 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002785static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002786{
Scott Wood17fed142016-05-30 13:57:56 -05002787 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002788 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002789 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2790 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002791
2792 return chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002793}
2794
2795/**
2796 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002797 * @mtd: MTD device structure
2798 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002799 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002800 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002801 */
William Juul52c07962007-10-31 13:53:06 +01002802static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002803{
William Juul52c07962007-10-31 13:53:06 +01002804 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002805}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002806
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002807/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002808 * nand_erase_nand - [INTERN] erase block(s)
2809 * @mtd: MTD device structure
2810 * @instr: erase instruction
2811 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002812 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002813 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002814 */
William Juul52c07962007-10-31 13:53:06 +01002815int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2816 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002817{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002818 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002819 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002820 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002821
Heiko Schocherf5895d12014-06-24 10:10:04 +02002822 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2823 __func__, (unsigned long long)instr->addr,
2824 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002825
Christian Hitzb8a6b372011-10-12 09:32:02 +02002826 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002827 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002828
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002829 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002830 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002831
2832 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002833 page = (int)(instr->addr >> chip->page_shift);
2834 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002835
2836 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002837 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002838
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002839 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002840 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002841
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002842 /* Check, if it is write protected */
2843 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002844 pr_debug("%s: device is write protected!\n",
2845 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002846 instr->state = MTD_ERASE_FAILED;
2847 goto erase_exit;
2848 }
2849
2850 /* Loop through the pages */
2851 len = instr->len;
2852
2853 instr->state = MTD_ERASING;
2854
2855 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002856 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002857
Sergey Lapin3a38a552013-01-14 03:46:50 +00002858 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09002859 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05002860 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002861 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002862 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002863 instr->state = MTD_ERASE_FAILED;
2864 goto erase_exit;
2865 }
William Juul52c07962007-10-31 13:53:06 +01002866
2867 /*
2868 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00002869 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01002870 */
2871 if (page <= chip->pagebuf && chip->pagebuf <
2872 (page + pages_per_block))
2873 chip->pagebuf = -1;
2874
Scott Wood3ea94ed2015-06-26 19:03:26 -05002875 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002876
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002877 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002878 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002879 pr_debug("%s: failed erase, page 0x%08x\n",
2880 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002881 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002882 instr->fail_addr =
2883 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002884 goto erase_exit;
2885 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002886
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002887 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002888 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002889 page += pages_per_block;
2890
2891 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002892 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002893 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002894 chip->select_chip(mtd, -1);
2895 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002896 }
2897 }
2898 instr->state = MTD_ERASE_DONE;
2899
Christian Hitz13fc0e22011-10-12 09:32:01 +02002900erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002901
2902 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002903
2904 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002905 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002906 nand_release_device(mtd);
2907
Scott Wood3628f002008-10-24 16:20:43 -05002908 /* Do call back function */
2909 if (!ret)
2910 mtd_erase_callback(instr);
2911
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002912 /* Return more or less happy */
2913 return ret;
2914}
2915
2916/**
2917 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00002918 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002919 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002920 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002921 */
William Juul52c07962007-10-31 13:53:06 +01002922static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002923{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002924 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002925
2926 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002927 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002928 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002929 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002930}
2931
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002932/**
William Juul52c07962007-10-31 13:53:06 +01002933 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002934 * @mtd: MTD device structure
2935 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002936 */
William Juul52c07962007-10-31 13:53:06 +01002937static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002938{
Scott Wood52ab7ce2016-05-30 13:57:58 -05002939 struct nand_chip *chip = mtd_to_nand(mtd);
2940 int chipnr = (int)(offs >> chip->chip_shift);
2941 int ret;
2942
2943 /* Select the NAND device */
2944 nand_get_device(mtd, FL_READING);
2945 chip->select_chip(mtd, chipnr);
2946
2947 ret = nand_block_checkbad(mtd, offs, 0);
2948
2949 chip->select_chip(mtd, -1);
2950 nand_release_device(mtd);
2951
2952 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002953}
2954
2955/**
William Juul52c07962007-10-31 13:53:06 +01002956 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002957 * @mtd: MTD device structure
2958 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002959 */
William Juul52c07962007-10-31 13:53:06 +01002960static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002961{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002962 int ret;
2963
Christian Hitzb8a6b372011-10-12 09:32:02 +02002964 ret = nand_block_isbad(mtd, ofs);
2965 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002966 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002967 if (ret > 0)
2968 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002969 return ret;
2970 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002971
Heiko Schocherf5895d12014-06-24 10:10:04 +02002972 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002973}
2974
Heiko Schocherf5895d12014-06-24 10:10:04 +02002975/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002976 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2977 * @mtd: MTD device structure
2978 * @chip: nand chip info structure
2979 * @addr: feature address.
2980 * @subfeature_param: the subfeature parameters, a four bytes array.
2981 */
2982static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2983 int addr, uint8_t *subfeature_param)
2984{
2985 int status;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002986 int i;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002987
Heiko Schocherf5895d12014-06-24 10:10:04 +02002988#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2989 if (!chip->onfi_version ||
2990 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2991 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02002992 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002993#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00002994
2995 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002996 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2997 chip->write_byte(mtd, subfeature_param[i]);
2998
Sergey Lapin3a38a552013-01-14 03:46:50 +00002999 status = chip->waitfunc(mtd, chip);
3000 if (status & NAND_STATUS_FAIL)
3001 return -EIO;
3002 return 0;
3003}
3004
3005/**
3006 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3007 * @mtd: MTD device structure
3008 * @chip: nand chip info structure
3009 * @addr: feature address.
3010 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003011 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003012static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3013 int addr, uint8_t *subfeature_param)
3014{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003015 int i;
3016
3017#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3018 if (!chip->onfi_version ||
3019 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3020 & ONFI_OPT_CMD_SET_GET_FEATURES))
Mylène Josserandc21946b2018-07-13 18:10:23 +02003021 return -ENOTSUPP;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003022#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003023
Sergey Lapin3a38a552013-01-14 03:46:50 +00003024 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003025 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3026 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003027 return 0;
3028}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003029
Sergey Lapin3a38a552013-01-14 03:46:50 +00003030/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003031static void nand_set_defaults(struct nand_chip *chip, int busw)
3032{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003033 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003034 if (!chip->chip_delay)
3035 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003036
3037 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003038 if (chip->cmdfunc == NULL)
3039 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003040
3041 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003042 if (chip->waitfunc == NULL)
3043 chip->waitfunc = nand_wait;
3044
3045 if (!chip->select_chip)
3046 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003047
3048 /* set for ONFI nand */
3049 if (!chip->onfi_set_features)
3050 chip->onfi_set_features = nand_onfi_set_features;
3051 if (!chip->onfi_get_features)
3052 chip->onfi_get_features = nand_onfi_get_features;
3053
3054 /* If called twice, pointers that depend on busw may need to be reset */
3055 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003056 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3057 if (!chip->read_word)
3058 chip->read_word = nand_read_word;
3059 if (!chip->block_bad)
3060 chip->block_bad = nand_block_bad;
3061 if (!chip->block_markbad)
3062 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003063 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003064 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003065 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3066 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3067 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003068 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003069 if (!chip->scan_bbt)
3070 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003071
3072 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003073 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003074 spin_lock_init(&chip->controller->lock);
3075 init_waitqueue_head(&chip->controller->wq);
3076 }
3077
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003078 if (!chip->buf_align)
3079 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003080}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003081
Sergey Lapin3a38a552013-01-14 03:46:50 +00003082/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003083static void sanitize_string(char *s, size_t len)
3084{
3085 ssize_t i;
3086
Sergey Lapin3a38a552013-01-14 03:46:50 +00003087 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003088 s[len - 1] = 0;
3089
Sergey Lapin3a38a552013-01-14 03:46:50 +00003090 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003091 for (i = 0; i < len - 1; i++) {
3092 if (s[i] < ' ' || s[i] > 127)
3093 s[i] = '?';
3094 }
3095
Sergey Lapin3a38a552013-01-14 03:46:50 +00003096 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003097 strim(s);
3098}
3099
Florian Fainellic98a9352011-02-25 00:01:34 +00003100static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3101{
3102 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003103 while (len--) {
3104 crc ^= *p++ << 8;
3105 for (i = 0; i < 8; i++)
3106 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3107 }
3108
3109 return crc;
3110}
3111
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003112#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003113/* Parse the Extended Parameter Page. */
3114static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3115 struct nand_chip *chip, struct nand_onfi_params *p)
3116{
3117 struct onfi_ext_param_page *ep;
3118 struct onfi_ext_section *s;
3119 struct onfi_ext_ecc_info *ecc;
3120 uint8_t *cursor;
3121 int ret = -EINVAL;
3122 int len;
3123 int i;
3124
3125 len = le16_to_cpu(p->ext_param_page_length) * 16;
3126 ep = kmalloc(len, GFP_KERNEL);
3127 if (!ep)
3128 return -ENOMEM;
3129
3130 /* Send our own NAND_CMD_PARAM. */
3131 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3132
3133 /* Use the Change Read Column command to skip the ONFI param pages. */
3134 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3135 sizeof(*p) * p->num_of_param_pages , -1);
3136
3137 /* Read out the Extended Parameter Page. */
3138 chip->read_buf(mtd, (uint8_t *)ep, len);
3139 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3140 != le16_to_cpu(ep->crc))) {
3141 pr_debug("fail in the CRC.\n");
3142 goto ext_out;
3143 }
3144
3145 /*
3146 * Check the signature.
3147 * Do not strictly follow the ONFI spec, maybe changed in future.
3148 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003149 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003150 pr_debug("The signature is invalid.\n");
3151 goto ext_out;
3152 }
3153
3154 /* find the ECC section. */
3155 cursor = (uint8_t *)(ep + 1);
3156 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3157 s = ep->sections + i;
3158 if (s->type == ONFI_SECTION_TYPE_2)
3159 break;
3160 cursor += s->length * 16;
3161 }
3162 if (i == ONFI_EXT_SECTION_MAX) {
3163 pr_debug("We can not find the ECC section.\n");
3164 goto ext_out;
3165 }
3166
3167 /* get the info we want. */
3168 ecc = (struct onfi_ext_ecc_info *)cursor;
3169
3170 if (!ecc->codeword_size) {
3171 pr_debug("Invalid codeword size\n");
3172 goto ext_out;
3173 }
3174
3175 chip->ecc_strength_ds = ecc->ecc_bits;
3176 chip->ecc_step_ds = 1 << ecc->codeword_size;
3177 ret = 0;
3178
3179ext_out:
3180 kfree(ep);
3181 return ret;
3182}
3183
3184static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3185{
Scott Wood17fed142016-05-30 13:57:56 -05003186 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003187 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3188
3189 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3190 feature);
3191}
3192
3193/*
3194 * Configure chip properties from Micron vendor-specific ONFI table
3195 */
3196static void nand_onfi_detect_micron(struct nand_chip *chip,
3197 struct nand_onfi_params *p)
3198{
3199 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3200
3201 if (le16_to_cpu(p->vendor_revision) < 1)
3202 return;
3203
3204 chip->read_retries = micron->read_retry_options;
3205 chip->setup_read_retry = nand_setup_read_retry_micron;
3206}
3207
Florian Fainellic98a9352011-02-25 00:01:34 +00003208/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003209 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003210 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003211static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003212 int *busw)
3213{
3214 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisf3832302014-05-06 00:46:16 +05303215 int i, j;
Florian Fainellic98a9352011-02-25 00:01:34 +00003216 int val;
3217
Sergey Lapin3a38a552013-01-14 03:46:50 +00003218 /* Try ONFI for unknown chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00003219 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3220 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3221 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3222 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003223
Florian Fainellic98a9352011-02-25 00:01:34 +00003224 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3225 for (i = 0; i < 3; i++) {
Brian Norrisf3832302014-05-06 00:46:16 +05303226 for (j = 0; j < sizeof(*p); j++)
3227 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003228 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003229 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003230 break;
3231 }
3232 }
3233
Heiko Schocherf5895d12014-06-24 10:10:04 +02003234 if (i == 3) {
3235 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003236 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003237 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003238
Sergey Lapin3a38a552013-01-14 03:46:50 +00003239 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003240 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003241 if (val & (1 << 5))
3242 chip->onfi_version = 23;
3243 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003244 chip->onfi_version = 22;
3245 else if (val & (1 << 3))
3246 chip->onfi_version = 21;
3247 else if (val & (1 << 2))
3248 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003249 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003250 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003251
3252 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003253 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003254 return 0;
3255 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003256
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003257 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3258 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003259 if (!mtd->name)
3260 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003261
Florian Fainellic98a9352011-02-25 00:01:34 +00003262 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003263
3264 /*
3265 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3266 * (don't ask me who thought of this...). MTD assumes that these
3267 * dimensions will be power-of-2, so just truncate the remaining area.
3268 */
3269 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3270 mtd->erasesize *= mtd->writesize;
3271
Florian Fainellic98a9352011-02-25 00:01:34 +00003272 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003273
3274 /* See erasesize comment */
3275 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003276 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003277 chip->bits_per_cell = p->bits_per_cell;
3278
3279 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003280 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003281 else
3282 *busw = 0;
3283
3284 if (p->ecc_bits != 0xff) {
3285 chip->ecc_strength_ds = p->ecc_bits;
3286 chip->ecc_step_ds = 512;
3287 } else if (chip->onfi_version >= 21 &&
3288 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3289
3290 /*
3291 * The nand_flash_detect_ext_param_page() uses the
3292 * Change Read Column command which maybe not supported
3293 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3294 * now. We do not replace user supplied command function.
3295 */
3296 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3297 chip->cmdfunc = nand_command_lp;
3298
3299 /* The Extended Parameter Page is supported since ONFI 2.1. */
3300 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3301 pr_warn("Failed to detect ONFI extended param page\n");
3302 } else {
3303 pr_warn("Could not retrieve ONFI ECC requirements\n");
3304 }
3305
3306 if (p->jedec_id == NAND_MFR_MICRON)
3307 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003308
3309 return 1;
3310}
3311#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003312static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003313 int *busw)
3314{
3315 return 0;
3316}
3317#endif
3318
William Juul52c07962007-10-31 13:53:06 +01003319/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003320 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3321 */
3322static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3323 int *busw)
3324{
3325 struct nand_jedec_params *p = &chip->jedec_params;
3326 struct jedec_ecc_info *ecc;
3327 int val;
3328 int i, j;
3329
3330 /* Try JEDEC for unknown chip or LP */
3331 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3332 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3333 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3334 chip->read_byte(mtd) != 'C')
3335 return 0;
3336
3337 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3338 for (i = 0; i < 3; i++) {
3339 for (j = 0; j < sizeof(*p); j++)
3340 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3341
3342 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3343 le16_to_cpu(p->crc))
3344 break;
3345 }
3346
3347 if (i == 3) {
3348 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3349 return 0;
3350 }
3351
3352 /* Check version */
3353 val = le16_to_cpu(p->revision);
3354 if (val & (1 << 2))
3355 chip->jedec_version = 10;
3356 else if (val & (1 << 1))
3357 chip->jedec_version = 1; /* vendor specific version */
3358
3359 if (!chip->jedec_version) {
3360 pr_info("unsupported JEDEC version: %d\n", val);
3361 return 0;
3362 }
3363
3364 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3365 sanitize_string(p->model, sizeof(p->model));
3366 if (!mtd->name)
3367 mtd->name = p->model;
3368
3369 mtd->writesize = le32_to_cpu(p->byte_per_page);
3370
3371 /* Please reference to the comment for nand_flash_detect_onfi. */
3372 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3373 mtd->erasesize *= mtd->writesize;
3374
3375 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3376
3377 /* Please reference to the comment for nand_flash_detect_onfi. */
3378 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3379 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3380 chip->bits_per_cell = p->bits_per_cell;
3381
3382 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3383 *busw = NAND_BUSWIDTH_16;
3384 else
3385 *busw = 0;
3386
3387 /* ECC info */
3388 ecc = &p->ecc_info[0];
3389
3390 if (ecc->codeword_size >= 9) {
3391 chip->ecc_strength_ds = ecc->ecc_bits;
3392 chip->ecc_step_ds = 1 << ecc->codeword_size;
3393 } else {
3394 pr_warn("Invalid codeword size\n");
3395 }
3396
3397 return 1;
3398}
3399
3400/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003401 * nand_id_has_period - Check if an ID string has a given wraparound period
3402 * @id_data: the ID string
3403 * @arrlen: the length of the @id_data array
3404 * @period: the period of repitition
3405 *
3406 * Check if an ID string is repeated within a given sequence of bytes at
3407 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02003408 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00003409 * if the repetition has a period of @period; otherwise, returns zero.
3410 */
3411static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3412{
3413 int i, j;
3414 for (i = 0; i < period; i++)
3415 for (j = i + period; j < arrlen; j += period)
3416 if (id_data[i] != id_data[j])
3417 return 0;
3418 return 1;
3419}
3420
3421/*
3422 * nand_id_len - Get the length of an ID string returned by CMD_READID
3423 * @id_data: the ID string
3424 * @arrlen: the length of the @id_data array
3425
3426 * Returns the length of the ID string, according to known wraparound/trailing
3427 * zero patterns. If no pattern exists, returns the length of the array.
3428 */
3429static int nand_id_len(u8 *id_data, int arrlen)
3430{
3431 int last_nonzero, period;
3432
3433 /* Find last non-zero byte */
3434 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3435 if (id_data[last_nonzero])
3436 break;
3437
3438 /* All zeros */
3439 if (last_nonzero < 0)
3440 return 0;
3441
3442 /* Calculate wraparound period */
3443 for (period = 1; period < arrlen; period++)
3444 if (nand_id_has_period(id_data, arrlen, period))
3445 break;
3446
3447 /* There's a repeated pattern */
3448 if (period < arrlen)
3449 return period;
3450
3451 /* There are trailing zeros */
3452 if (last_nonzero < arrlen - 1)
3453 return last_nonzero + 1;
3454
3455 /* No pattern detected */
3456 return arrlen;
3457}
3458
Heiko Schocherf5895d12014-06-24 10:10:04 +02003459/* Extract the bits of per cell from the 3rd byte of the extended ID */
3460static int nand_get_bits_per_cell(u8 cellinfo)
3461{
3462 int bits;
3463
3464 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3465 bits >>= NAND_CI_CELLTYPE_SHIFT;
3466 return bits + 1;
3467}
3468
Sergey Lapin3a38a552013-01-14 03:46:50 +00003469/*
3470 * Many new NAND share similar device ID codes, which represent the size of the
3471 * chip. The rest of the parameters must be decoded according to generic or
3472 * manufacturer-specific "extended ID" decoding patterns.
3473 */
3474static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3475 u8 id_data[8], int *busw)
3476{
3477 int extid, id_len;
3478 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003479 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003480 /* The 4th id byte is the important one */
3481 extid = id_data[3];
3482
3483 id_len = nand_id_len(id_data, 8);
3484
3485 /*
3486 * Field definitions are in the following datasheets:
3487 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3488 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3489 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3490 *
3491 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3492 * ID to decide what to do.
3493 */
3494 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003495 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003496 /* Calc pagesize */
3497 mtd->writesize = 2048 << (extid & 0x03);
3498 extid >>= 2;
3499 /* Calc oobsize */
3500 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3501 case 1:
3502 mtd->oobsize = 128;
3503 break;
3504 case 2:
3505 mtd->oobsize = 218;
3506 break;
3507 case 3:
3508 mtd->oobsize = 400;
3509 break;
3510 case 4:
3511 mtd->oobsize = 436;
3512 break;
3513 case 5:
3514 mtd->oobsize = 512;
3515 break;
3516 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003517 mtd->oobsize = 640;
3518 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003519 case 7:
3520 default: /* Other cases are "reserved" (unknown) */
3521 mtd->oobsize = 1024;
3522 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003523 }
3524 extid >>= 2;
3525 /* Calc blocksize */
3526 mtd->erasesize = (128 * 1024) <<
3527 (((extid >> 1) & 0x04) | (extid & 0x03));
3528 *busw = 0;
3529 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003530 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003531 unsigned int tmp;
3532
3533 /* Calc pagesize */
3534 mtd->writesize = 2048 << (extid & 0x03);
3535 extid >>= 2;
3536 /* Calc oobsize */
3537 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3538 case 0:
3539 mtd->oobsize = 128;
3540 break;
3541 case 1:
3542 mtd->oobsize = 224;
3543 break;
3544 case 2:
3545 mtd->oobsize = 448;
3546 break;
3547 case 3:
3548 mtd->oobsize = 64;
3549 break;
3550 case 4:
3551 mtd->oobsize = 32;
3552 break;
3553 case 5:
3554 mtd->oobsize = 16;
3555 break;
3556 default:
3557 mtd->oobsize = 640;
3558 break;
3559 }
3560 extid >>= 2;
3561 /* Calc blocksize */
3562 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3563 if (tmp < 0x03)
3564 mtd->erasesize = (128 * 1024) << tmp;
3565 else if (tmp == 0x03)
3566 mtd->erasesize = 768 * 1024;
3567 else
3568 mtd->erasesize = (64 * 1024) << tmp;
3569 *busw = 0;
3570 } else {
3571 /* Calc pagesize */
3572 mtd->writesize = 1024 << (extid & 0x03);
3573 extid >>= 2;
3574 /* Calc oobsize */
3575 mtd->oobsize = (8 << (extid & 0x01)) *
3576 (mtd->writesize >> 9);
3577 extid >>= 2;
3578 /* Calc blocksize. Blocksize is multiples of 64KiB */
3579 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3580 extid >>= 2;
3581 /* Get buswidth information */
3582 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003583
3584 /*
3585 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3586 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3587 * follows:
3588 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3589 * 110b -> 24nm
3590 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3591 */
3592 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3593 nand_is_slc(chip) &&
3594 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3595 !(id_data[4] & 0x80) /* !BENAND */) {
3596 mtd->oobsize = 32 * mtd->writesize >> 9;
3597 }
3598
Sergey Lapin3a38a552013-01-14 03:46:50 +00003599 }
3600}
3601
Heiko Schocherf5895d12014-06-24 10:10:04 +02003602/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003603 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3604 * decodes a matching ID table entry and assigns the MTD size parameters for
3605 * the chip.
3606 */
3607static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003608 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00003609 int *busw)
3610{
3611 int maf_id = id_data[0];
3612
3613 mtd->erasesize = type->erasesize;
3614 mtd->writesize = type->pagesize;
3615 mtd->oobsize = mtd->writesize / 32;
3616 *busw = type->options & NAND_BUSWIDTH_16;
3617
Heiko Schocherf5895d12014-06-24 10:10:04 +02003618 /* All legacy ID NAND are small-page, SLC */
3619 chip->bits_per_cell = 1;
3620
Sergey Lapin3a38a552013-01-14 03:46:50 +00003621 /*
3622 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3623 * some Spansion chips have erasesize that conflicts with size
3624 * listed in nand_ids table.
3625 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3626 */
3627 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3628 && id_data[6] == 0x00 && id_data[7] == 0x00
3629 && mtd->writesize == 512) {
3630 mtd->erasesize = 128 * 1024;
3631 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3632 }
3633}
3634
Heiko Schocherf5895d12014-06-24 10:10:04 +02003635/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003636 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3637 * heuristic patterns using various detected parameters (e.g., manufacturer,
3638 * page size, cell-type information).
3639 */
3640static void nand_decode_bbm_options(struct mtd_info *mtd,
3641 struct nand_chip *chip, u8 id_data[8])
3642{
3643 int maf_id = id_data[0];
3644
3645 /* Set the bad block position */
3646 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3647 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3648 else
3649 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3650
3651 /*
3652 * Bad block marker is stored in the last page of each block on Samsung
3653 * and Hynix MLC devices; stored in first two pages of each block on
3654 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3655 * AMD/Spansion, and Macronix. All others scan only the first page.
3656 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003657 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003658 (maf_id == NAND_MFR_SAMSUNG ||
3659 maf_id == NAND_MFR_HYNIX))
3660 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003661 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003662 (maf_id == NAND_MFR_SAMSUNG ||
3663 maf_id == NAND_MFR_HYNIX ||
3664 maf_id == NAND_MFR_TOSHIBA ||
3665 maf_id == NAND_MFR_AMD ||
3666 maf_id == NAND_MFR_MACRONIX)) ||
3667 (mtd->writesize == 2048 &&
3668 maf_id == NAND_MFR_MICRON))
3669 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3670}
3671
Heiko Schocherf5895d12014-06-24 10:10:04 +02003672static inline bool is_full_id_nand(struct nand_flash_dev *type)
3673{
3674 return type->id_len;
3675}
3676
3677static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3678 struct nand_flash_dev *type, u8 *id_data, int *busw)
3679{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003680 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003681 mtd->writesize = type->pagesize;
3682 mtd->erasesize = type->erasesize;
3683 mtd->oobsize = type->oobsize;
3684
3685 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3686 chip->chipsize = (uint64_t)type->chipsize << 20;
3687 chip->options |= type->options;
3688 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3689 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003690 chip->onfi_timing_mode_default =
3691 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003692
3693 *busw = type->options & NAND_BUSWIDTH_16;
3694
3695 if (!mtd->name)
3696 mtd->name = type->name;
3697
3698 return true;
3699 }
3700 return false;
3701}
3702
Sergey Lapin3a38a552013-01-14 03:46:50 +00003703/*
3704 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01003705 */
Jörg Krause929fb442018-01-14 19:26:37 +01003706struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01003707 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003708 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003709 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01003710{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003711 int busw;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003712 int i, maf_idx;
3713 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003714
Karl Beldanb6322fc2008-09-15 16:08:03 +02003715 /*
3716 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003717 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02003718 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003719 nand_reset(chip, 0);
3720
3721 /* Select the device */
3722 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02003723
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003724 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01003725 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003726
3727 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003728 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003729 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003730
Sergey Lapin3a38a552013-01-14 03:46:50 +00003731 /*
3732 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05003733 * interface concerns can cause random data which looks like a
3734 * possibly credible NAND flash to appear. If the two results do
3735 * not match, ignore the device completely.
3736 */
3737
3738 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3739
Sergey Lapin3a38a552013-01-14 03:46:50 +00003740 /* Read entire ID string */
3741 for (i = 0; i < 8; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02003742 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05003743
Christian Hitzb8a6b372011-10-12 09:32:02 +02003744 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003745 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003746 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05003747 return ERR_PTR(-ENODEV);
3748 }
3749
Lei Wen75bde942011-01-06 09:48:18 +08003750 if (!type)
3751 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003752
Heiko Schocherf5895d12014-06-24 10:10:04 +02003753 for (; type->name != NULL; type++) {
3754 if (is_full_id_nand(type)) {
3755 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3756 goto ident_done;
3757 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05003758 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003759 }
3760 }
Lei Wen75bde942011-01-06 09:48:18 +08003761
Christian Hitzb8a6b372011-10-12 09:32:02 +02003762 chip->onfi_version = 0;
3763 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05003764 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003765 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02003766 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003767
3768 /* Check if the chip is JEDEC compliant */
3769 if (nand_flash_detect_jedec(mtd, chip, &busw))
3770 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02003771 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003772
Christian Hitzb8a6b372011-10-12 09:32:02 +02003773 if (!type->name)
3774 return ERR_PTR(-ENODEV);
3775
William Juul52c07962007-10-31 13:53:06 +01003776 if (!mtd->name)
3777 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003778
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003779 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003780
Scott Wood52ab7ce2016-05-30 13:57:58 -05003781 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003782 /* Decode parameters from extended ID */
3783 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003784 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003785 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003786 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003787 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00003788 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00003789
Sergey Lapin3a38a552013-01-14 03:46:50 +00003790 /*
3791 * Check if chip is not a Samsung device. Do not clear the
3792 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02003793 */
3794 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3795 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3796ident_done:
3797
William Juul52c07962007-10-31 13:53:06 +01003798 /* Try to identify manufacturer */
3799 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3800 if (nand_manuf_ids[maf_idx].id == *maf_id)
3801 break;
3802 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003803
Heiko Schocherf5895d12014-06-24 10:10:04 +02003804 if (chip->options & NAND_BUSWIDTH_AUTO) {
3805 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3806 chip->options |= busw;
3807 nand_set_defaults(chip, busw);
3808 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3809 /*
3810 * Check, if buswidth is correct. Hardware drivers should set
3811 * chip correct!
3812 */
3813 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3814 *maf_id, *dev_id);
3815 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3816 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003817 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3818 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01003819 return ERR_PTR(-EINVAL);
3820 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003821
Sergey Lapin3a38a552013-01-14 03:46:50 +00003822 nand_decode_bbm_options(mtd, chip, id_data);
3823
William Juul52c07962007-10-31 13:53:06 +01003824 /* Calculate the address shift from the page size */
3825 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003826 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01003827 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003828
William Juul52c07962007-10-31 13:53:06 +01003829 chip->bbt_erase_shift = chip->phys_erase_shift =
3830 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003831 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05003832 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003833 else {
3834 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3835 chip->chip_shift += 32 - 1;
3836 }
3837
Masahiro Yamada984926b2017-11-22 02:38:31 +09003838 if (chip->chip_shift - chip->page_shift > 16)
3839 chip->options |= NAND_ROW_ADDR_3;
3840
Christian Hitzb8a6b372011-10-12 09:32:02 +02003841 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003842 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003843
Sergey Lapin3a38a552013-01-14 03:46:50 +00003844 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01003845 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3846 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003847
Heiko Schocherf5895d12014-06-24 10:10:04 +02003848 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3849 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003850
Christian Hitzb8a6b372011-10-12 09:32:02 +02003851#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003852 if (chip->onfi_version)
3853 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3854 chip->onfi_params.model);
3855 else if (chip->jedec_version)
3856 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3857 chip->jedec_params.model);
3858 else
3859 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3860 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003861#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003862 if (chip->jedec_version)
3863 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3864 chip->jedec_params.model);
3865 else
3866 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3867 type->name);
3868
3869 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3870 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003871#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003872
Scott Wood3ea94ed2015-06-26 19:03:26 -05003873 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003874 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05003875 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01003876 return type;
3877}
Jörg Krause929fb442018-01-14 19:26:37 +01003878EXPORT_SYMBOL(nand_get_flash_type);
William Juul52c07962007-10-31 13:53:06 +01003879
Brian Norrisba6463d2016-06-15 21:09:22 +02003880#if CONFIG_IS_ENABLED(OF_CONTROL)
3881DECLARE_GLOBAL_DATA_PTR;
3882
3883static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3884{
3885 int ret, ecc_mode = -1, ecc_strength, ecc_step;
3886 const void *blob = gd->fdt_blob;
3887 const char *str;
3888
3889 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3890 if (ret == 16)
3891 chip->options |= NAND_BUSWIDTH_16;
3892
3893 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3894 chip->bbt_options |= NAND_BBT_USE_FLASH;
3895
3896 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3897 if (str) {
3898 if (!strcmp(str, "none"))
3899 ecc_mode = NAND_ECC_NONE;
3900 else if (!strcmp(str, "soft"))
3901 ecc_mode = NAND_ECC_SOFT;
3902 else if (!strcmp(str, "hw"))
3903 ecc_mode = NAND_ECC_HW;
3904 else if (!strcmp(str, "hw_syndrome"))
3905 ecc_mode = NAND_ECC_HW_SYNDROME;
3906 else if (!strcmp(str, "hw_oob_first"))
3907 ecc_mode = NAND_ECC_HW_OOB_FIRST;
3908 else if (!strcmp(str, "soft_bch"))
3909 ecc_mode = NAND_ECC_SOFT_BCH;
3910 }
3911
3912
3913 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3914 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3915
3916 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3917 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3918 pr_err("must set both strength and step size in DT\n");
3919 return -EINVAL;
3920 }
3921
3922 if (ecc_mode >= 0)
3923 chip->ecc.mode = ecc_mode;
3924
3925 if (ecc_strength >= 0)
3926 chip->ecc.strength = ecc_strength;
3927
3928 if (ecc_step > 0)
3929 chip->ecc.size = ecc_step;
3930
Boris Brezillonf1a54b02017-11-22 02:38:13 +09003931 if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
3932 chip->ecc.options |= NAND_ECC_MAXIMIZE;
3933
Brian Norrisba6463d2016-06-15 21:09:22 +02003934 return 0;
3935}
3936#else
3937static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3938{
3939 return 0;
3940}
3941#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3942
William Juul52c07962007-10-31 13:53:06 +01003943/**
3944 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003945 * @mtd: MTD device structure
3946 * @maxchips: number of chips to scan for
3947 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01003948 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003949 * This is the first phase of the normal nand_scan() function. It reads the
3950 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01003951 *
William Juul52c07962007-10-31 13:53:06 +01003952 */
Lei Wen75bde942011-01-06 09:48:18 +08003953int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003954 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01003955{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003956 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05003957 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003958 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02003959 int ret;
3960
3961 if (chip->flash_node) {
3962 ret = nand_dt_init(mtd, chip, chip->flash_node);
3963 if (ret)
3964 return ret;
3965 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003966
William Juul52c07962007-10-31 13:53:06 +01003967 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003968 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01003969
3970 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003971 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3972 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01003973
3974 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003975 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3976 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01003977 chip->select_chip(mtd, -1);
3978 return PTR_ERR(type);
3979 }
3980
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003981 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09003982 ret = nand_init_data_interface(chip);
3983 if (ret)
3984 return ret;
3985
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003986 /*
3987 * Setup the data interface correctly on the chip and controller side.
3988 * This explicit call to nand_setup_data_interface() is only required
3989 * for the first die, because nand_reset() has been called before
3990 * ->data_interface and ->default_onfi_timing_mode were set.
3991 * For the other dies, nand_reset() will automatically switch to the
3992 * best mode for us.
3993 */
Boris Brezillon32935f42017-11-22 02:38:28 +09003994 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003995 if (ret)
3996 return ret;
3997
Heiko Schocherf5895d12014-06-24 10:10:04 +02003998 chip->select_chip(mtd, -1);
3999
William Juul52c07962007-10-31 13:53:06 +01004000 /* Check for a chip array */
4001 for (i = 1; i < maxchips; i++) {
Karl Beldanb6322fc2008-09-15 16:08:03 +02004002 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004003 nand_reset(chip, i);
4004
4005 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004006 /* Send the command for reading device ID */
4007 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004008 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01004009 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherf5895d12014-06-24 10:10:04 +02004010 nand_dev_id != chip->read_byte(mtd)) {
4011 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004012 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004013 }
4014 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004015 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004016
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004017#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004018 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004019 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004020#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004021
William Juul52c07962007-10-31 13:53:06 +01004022 /* Store the number of chips and calc total size for mtd */
4023 chip->numchips = i;
4024 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004025
William Juul52c07962007-10-31 13:53:06 +01004026 return 0;
4027}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004028EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004029
Masahiro Yamada820eb482017-11-22 02:38:29 +09004030/**
4031 * nand_check_ecc_caps - check the sanity of preset ECC settings
4032 * @chip: nand chip info structure
4033 * @caps: ECC caps info structure
4034 * @oobavail: OOB size that the ECC engine can use
4035 *
4036 * When ECC step size and strength are already set, check if they are supported
4037 * by the controller and the calculated ECC bytes fit within the chip's OOB.
4038 * On success, the calculated ECC bytes is set.
4039 */
4040int nand_check_ecc_caps(struct nand_chip *chip,
4041 const struct nand_ecc_caps *caps, int oobavail)
4042{
4043 struct mtd_info *mtd = nand_to_mtd(chip);
4044 const struct nand_ecc_step_info *stepinfo;
4045 int preset_step = chip->ecc.size;
4046 int preset_strength = chip->ecc.strength;
4047 int nsteps, ecc_bytes;
4048 int i, j;
4049
4050 if (WARN_ON(oobavail < 0))
4051 return -EINVAL;
4052
4053 if (!preset_step || !preset_strength)
4054 return -ENODATA;
4055
4056 nsteps = mtd->writesize / preset_step;
4057
4058 for (i = 0; i < caps->nstepinfos; i++) {
4059 stepinfo = &caps->stepinfos[i];
4060
4061 if (stepinfo->stepsize != preset_step)
4062 continue;
4063
4064 for (j = 0; j < stepinfo->nstrengths; j++) {
4065 if (stepinfo->strengths[j] != preset_strength)
4066 continue;
4067
4068 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4069 preset_strength);
4070 if (WARN_ON_ONCE(ecc_bytes < 0))
4071 return ecc_bytes;
4072
4073 if (ecc_bytes * nsteps > oobavail) {
4074 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4075 preset_step, preset_strength);
4076 return -ENOSPC;
4077 }
4078
4079 chip->ecc.bytes = ecc_bytes;
4080
4081 return 0;
4082 }
4083 }
4084
4085 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4086 preset_step, preset_strength);
4087
4088 return -ENOTSUPP;
4089}
4090EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4091
4092/**
4093 * nand_match_ecc_req - meet the chip's requirement with least ECC bytes
4094 * @chip: nand chip info structure
4095 * @caps: ECC engine caps info structure
4096 * @oobavail: OOB size that the ECC engine can use
4097 *
4098 * If a chip's ECC requirement is provided, try to meet it with the least
4099 * number of ECC bytes (i.e. with the largest number of OOB-free bytes).
4100 * On success, the chosen ECC settings are set.
4101 */
4102int nand_match_ecc_req(struct nand_chip *chip,
4103 const struct nand_ecc_caps *caps, int oobavail)
4104{
4105 struct mtd_info *mtd = nand_to_mtd(chip);
4106 const struct nand_ecc_step_info *stepinfo;
4107 int req_step = chip->ecc_step_ds;
4108 int req_strength = chip->ecc_strength_ds;
4109 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4110 int best_step, best_strength, best_ecc_bytes;
4111 int best_ecc_bytes_total = INT_MAX;
4112 int i, j;
4113
4114 if (WARN_ON(oobavail < 0))
4115 return -EINVAL;
4116
4117 /* No information provided by the NAND chip */
4118 if (!req_step || !req_strength)
4119 return -ENOTSUPP;
4120
4121 /* number of correctable bits the chip requires in a page */
4122 req_corr = mtd->writesize / req_step * req_strength;
4123
4124 for (i = 0; i < caps->nstepinfos; i++) {
4125 stepinfo = &caps->stepinfos[i];
4126 step_size = stepinfo->stepsize;
4127
4128 for (j = 0; j < stepinfo->nstrengths; j++) {
4129 strength = stepinfo->strengths[j];
4130
4131 /*
4132 * If both step size and strength are smaller than the
4133 * chip's requirement, it is not easy to compare the
4134 * resulted reliability.
4135 */
4136 if (step_size < req_step && strength < req_strength)
4137 continue;
4138
4139 if (mtd->writesize % step_size)
4140 continue;
4141
4142 nsteps = mtd->writesize / step_size;
4143
4144 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4145 if (WARN_ON_ONCE(ecc_bytes < 0))
4146 continue;
4147 ecc_bytes_total = ecc_bytes * nsteps;
4148
4149 if (ecc_bytes_total > oobavail ||
4150 strength * nsteps < req_corr)
4151 continue;
4152
4153 /*
4154 * We assume the best is to meet the chip's requrement
4155 * with the least number of ECC bytes.
4156 */
4157 if (ecc_bytes_total < best_ecc_bytes_total) {
4158 best_ecc_bytes_total = ecc_bytes_total;
4159 best_step = step_size;
4160 best_strength = strength;
4161 best_ecc_bytes = ecc_bytes;
4162 }
4163 }
4164 }
4165
4166 if (best_ecc_bytes_total == INT_MAX)
4167 return -ENOTSUPP;
4168
4169 chip->ecc.size = best_step;
4170 chip->ecc.strength = best_strength;
4171 chip->ecc.bytes = best_ecc_bytes;
4172
4173 return 0;
4174}
4175EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4176
4177/**
4178 * nand_maximize_ecc - choose the max ECC strength available
4179 * @chip: nand chip info structure
4180 * @caps: ECC engine caps info structure
4181 * @oobavail: OOB size that the ECC engine can use
4182 *
4183 * Choose the max ECC strength that is supported on the controller, and can fit
4184 * within the chip's OOB. On success, the chosen ECC settings are set.
4185 */
4186int nand_maximize_ecc(struct nand_chip *chip,
4187 const struct nand_ecc_caps *caps, int oobavail)
4188{
4189 struct mtd_info *mtd = nand_to_mtd(chip);
4190 const struct nand_ecc_step_info *stepinfo;
4191 int step_size, strength, nsteps, ecc_bytes, corr;
4192 int best_corr = 0;
4193 int best_step = 0;
4194 int best_strength, best_ecc_bytes;
4195 int i, j;
4196
4197 if (WARN_ON(oobavail < 0))
4198 return -EINVAL;
4199
4200 for (i = 0; i < caps->nstepinfos; i++) {
4201 stepinfo = &caps->stepinfos[i];
4202 step_size = stepinfo->stepsize;
4203
4204 /* If chip->ecc.size is already set, respect it */
4205 if (chip->ecc.size && step_size != chip->ecc.size)
4206 continue;
4207
4208 for (j = 0; j < stepinfo->nstrengths; j++) {
4209 strength = stepinfo->strengths[j];
4210
4211 if (mtd->writesize % step_size)
4212 continue;
4213
4214 nsteps = mtd->writesize / step_size;
4215
4216 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4217 if (WARN_ON_ONCE(ecc_bytes < 0))
4218 continue;
4219
4220 if (ecc_bytes * nsteps > oobavail)
4221 continue;
4222
4223 corr = strength * nsteps;
4224
4225 /*
4226 * If the number of correctable bits is the same,
4227 * bigger step_size has more reliability.
4228 */
4229 if (corr > best_corr ||
4230 (corr == best_corr && step_size > best_step)) {
4231 best_corr = corr;
4232 best_step = step_size;
4233 best_strength = strength;
4234 best_ecc_bytes = ecc_bytes;
4235 }
4236 }
4237 }
4238
4239 if (!best_corr)
4240 return -ENOTSUPP;
4241
4242 chip->ecc.size = best_step;
4243 chip->ecc.strength = best_strength;
4244 chip->ecc.bytes = best_ecc_bytes;
4245
4246 return 0;
4247}
4248EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4249
Scott Wood3ea94ed2015-06-26 19:03:26 -05004250/*
4251 * Check if the chip configuration meet the datasheet requirements.
4252
4253 * If our configuration corrects A bits per B bytes and the minimum
4254 * required correction level is X bits per Y bytes, then we must ensure
4255 * both of the following are true:
4256 *
4257 * (1) A / B >= X / Y
4258 * (2) A >= X
4259 *
4260 * Requirement (1) ensures we can correct for the required bitflip density.
4261 * Requirement (2) ensures we can correct even when all bitflips are clumped
4262 * in the same sector.
4263 */
4264static bool nand_ecc_strength_good(struct mtd_info *mtd)
4265{
Scott Wood17fed142016-05-30 13:57:56 -05004266 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004267 struct nand_ecc_ctrl *ecc = &chip->ecc;
4268 int corr, ds_corr;
4269
4270 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4271 /* Not enough information */
4272 return true;
4273
4274 /*
4275 * We get the number of corrected bits per page to compare
4276 * the correction density.
4277 */
4278 corr = (mtd->writesize * ecc->strength) / ecc->size;
4279 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4280
4281 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4282}
William Juul52c07962007-10-31 13:53:06 +01004283
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004284static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4285{
4286 struct nand_ecc_ctrl *ecc = &chip->ecc;
4287
4288 if (nand_standard_page_accessors(ecc))
4289 return false;
4290
4291 /*
4292 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4293 * controller driver implements all the page accessors because
4294 * default helpers are not suitable when the core does not
4295 * send the READ0/PAGEPROG commands.
4296 */
4297 return (!ecc->read_page || !ecc->write_page ||
4298 !ecc->read_page_raw || !ecc->write_page_raw ||
4299 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4300 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4301 ecc->hwctl && ecc->calculate));
4302}
4303
William Juul52c07962007-10-31 13:53:06 +01004304/**
4305 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004306 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004307 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004308 * This is the second phase of the normal nand_scan() function. It fills out
4309 * all the uninitialized function pointers with the defaults and scans for a
4310 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004311 */
4312int nand_scan_tail(struct mtd_info *mtd)
4313{
4314 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004315 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004316 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004317 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004318
Sergey Lapin3a38a552013-01-14 03:46:50 +00004319 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4320 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4321 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4322
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004323 if (invalid_ecc_page_accessors(chip)) {
4324 pr_err("Invalid ECC page accessors setup\n");
4325 return -EINVAL;
4326 }
4327
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004328 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004329 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004330 chip->buffers = nbuf;
4331 } else {
4332 if (!chip->buffers)
4333 return -ENOMEM;
4334 }
William Juul52c07962007-10-31 13:53:06 +01004335
4336 /* Set the internal oob buffer location, just after the page data */
4337 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4338
4339 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004340 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004341 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004342 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004343 switch (mtd->oobsize) {
Stefan Agnerbd186142018-12-06 14:57:09 +01004344#ifdef CONFIG_SYS_NAND_DRIVER_ECC_LAYOUT
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004345 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004346 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004347 break;
4348 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004349 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004350 break;
4351 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004352 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004353 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004354 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004355 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004356 break;
Stefan Agnerbd186142018-12-06 14:57:09 +01004357#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004358 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004359 pr_warn("No oob scheme defined for oobsize %d\n",
4360 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004361 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004362 }
4363 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004364
William Juul52c07962007-10-31 13:53:06 +01004365 if (!chip->write_page)
4366 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004367
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004368 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004369 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004370 * selected and we have 256 byte pagesize fallback to software ECC
4371 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004372
Heiko Schocherf5895d12014-06-24 10:10:04 +02004373 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004374 case NAND_ECC_HW_OOB_FIRST:
4375 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004376 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004377 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004378 BUG();
4379 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004380 if (!ecc->read_page)
4381 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004382
William Juul52c07962007-10-31 13:53:06 +01004383 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004384 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004385 if (!ecc->read_page)
4386 ecc->read_page = nand_read_page_hwecc;
4387 if (!ecc->write_page)
4388 ecc->write_page = nand_write_page_hwecc;
4389 if (!ecc->read_page_raw)
4390 ecc->read_page_raw = nand_read_page_raw;
4391 if (!ecc->write_page_raw)
4392 ecc->write_page_raw = nand_write_page_raw;
4393 if (!ecc->read_oob)
4394 ecc->read_oob = nand_read_oob_std;
4395 if (!ecc->write_oob)
4396 ecc->write_oob = nand_write_oob_std;
4397 if (!ecc->read_subpage)
4398 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05004399 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004400 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004401
William Juul52c07962007-10-31 13:53:06 +01004402 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004403 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4404 (!ecc->read_page ||
4405 ecc->read_page == nand_read_page_hwecc ||
4406 !ecc->write_page ||
4407 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004408 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01004409 BUG();
4410 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00004411 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004412 if (!ecc->read_page)
4413 ecc->read_page = nand_read_page_syndrome;
4414 if (!ecc->write_page)
4415 ecc->write_page = nand_write_page_syndrome;
4416 if (!ecc->read_page_raw)
4417 ecc->read_page_raw = nand_read_page_raw_syndrome;
4418 if (!ecc->write_page_raw)
4419 ecc->write_page_raw = nand_write_page_raw_syndrome;
4420 if (!ecc->read_oob)
4421 ecc->read_oob = nand_read_oob_syndrome;
4422 if (!ecc->write_oob)
4423 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004424
Heiko Schocherf5895d12014-06-24 10:10:04 +02004425 if (mtd->writesize >= ecc->size) {
4426 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004427 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4428 BUG();
4429 }
William Juul52c07962007-10-31 13:53:06 +01004430 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004431 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004432 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4433 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004434 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004435
William Juul52c07962007-10-31 13:53:06 +01004436 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004437 ecc->calculate = nand_calculate_ecc;
4438 ecc->correct = nand_correct_data;
4439 ecc->read_page = nand_read_page_swecc;
4440 ecc->read_subpage = nand_read_subpage;
4441 ecc->write_page = nand_write_page_swecc;
4442 ecc->read_page_raw = nand_read_page_raw;
4443 ecc->write_page_raw = nand_write_page_raw;
4444 ecc->read_oob = nand_read_oob_std;
4445 ecc->write_oob = nand_write_oob_std;
4446 if (!ecc->size)
4447 ecc->size = 256;
4448 ecc->bytes = 3;
4449 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004450 break;
4451
Christian Hitz55f7bca2011-10-12 09:31:59 +02004452 case NAND_ECC_SOFT_BCH:
4453 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004454 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004455 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02004456 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004457 ecc->calculate = nand_bch_calculate_ecc;
4458 ecc->correct = nand_bch_correct_data;
4459 ecc->read_page = nand_read_page_swecc;
4460 ecc->read_subpage = nand_read_subpage;
4461 ecc->write_page = nand_write_page_swecc;
4462 ecc->read_page_raw = nand_read_page_raw;
4463 ecc->write_page_raw = nand_write_page_raw;
4464 ecc->read_oob = nand_read_oob_std;
4465 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004466 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05004467 * Board driver should supply ecc.size and ecc.strength values
4468 * to select how many bits are correctable. Otherwise, default
4469 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02004470 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004471 if (!ecc->size && (mtd->oobsize >= 64)) {
4472 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004473 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004474 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004475
4476 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004477 ecc->bytes = 0;
4478 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004479 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004480 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004481 BUG();
4482 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02004483 break;
4484
William Juul52c07962007-10-31 13:53:06 +01004485 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05004486 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004487 ecc->read_page = nand_read_page_raw;
4488 ecc->write_page = nand_write_page_raw;
4489 ecc->read_oob = nand_read_oob_std;
4490 ecc->read_page_raw = nand_read_page_raw;
4491 ecc->write_page_raw = nand_write_page_raw;
4492 ecc->write_oob = nand_write_oob_std;
4493 ecc->size = mtd->writesize;
4494 ecc->bytes = 0;
4495 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004496 break;
4497
4498 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004499 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01004500 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004501 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004502
Sergey Lapin3a38a552013-01-14 03:46:50 +00004503 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004504 if (!ecc->read_oob_raw)
4505 ecc->read_oob_raw = ecc->read_oob;
4506 if (!ecc->write_oob_raw)
4507 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004508
William Juul52c07962007-10-31 13:53:06 +01004509 /*
4510 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00004511 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01004512 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004513 mtd->oobavail = 0;
4514 if (ecc->layout) {
4515 for (i = 0; ecc->layout->oobfree[i].length; i++)
4516 mtd->oobavail += ecc->layout->oobfree[i].length;
4517 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004518
Scott Wood3ea94ed2015-06-26 19:03:26 -05004519 /* ECC sanity check: warn if it's too weak */
4520 if (!nand_ecc_strength_good(mtd))
4521 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4522 mtd->name);
4523
William Juul52c07962007-10-31 13:53:06 +01004524 /*
4525 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00004526 * mode.
William Juul52c07962007-10-31 13:53:06 +01004527 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004528 ecc->steps = mtd->writesize / ecc->size;
4529 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004530 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01004531 BUG();
4532 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004533 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004534
Sergey Lapin3a38a552013-01-14 03:46:50 +00004535 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004536 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4537 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01004538 case 2:
4539 mtd->subpage_sft = 1;
4540 break;
4541 case 4:
4542 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05004543 case 16:
William Juul52c07962007-10-31 13:53:06 +01004544 mtd->subpage_sft = 2;
4545 break;
4546 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004547 }
William Juul52c07962007-10-31 13:53:06 +01004548 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004549
William Juul52c07962007-10-31 13:53:06 +01004550 /* Initialize state */
4551 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004552
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004553 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01004554 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004555
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004556 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05004557 switch (ecc->mode) {
4558 case NAND_ECC_SOFT:
4559 case NAND_ECC_SOFT_BCH:
4560 if (chip->page_shift > 9)
4561 chip->options |= NAND_SUBPAGE_READ;
4562 break;
4563
4564 default:
4565 break;
4566 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004567
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004568 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004569 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004570 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4571 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004572 mtd->_erase = nand_erase;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004573 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004574 mtd->_read_oob = nand_read_oob;
4575 mtd->_write_oob = nand_write_oob;
4576 mtd->_sync = nand_sync;
4577 mtd->_lock = NULL;
4578 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03004579 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004580 mtd->_block_isbad = nand_block_isbad;
4581 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004582 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004583
Sergey Lapin3a38a552013-01-14 03:46:50 +00004584 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004585 mtd->ecclayout = ecc->layout;
4586 mtd->ecc_strength = ecc->strength;
4587 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004588 /*
4589 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4590 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4591 * properly set.
4592 */
4593 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05004594 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01004595
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02004596 return 0;
William Juul52c07962007-10-31 13:53:06 +01004597}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004598EXPORT_SYMBOL(nand_scan_tail);
4599
William Juul52c07962007-10-31 13:53:06 +01004600/**
4601 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004602 * @mtd: MTD device structure
4603 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01004604 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004605 * This fills out all the uninitialized function pointers with the defaults.
4606 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05004607 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01004608 */
4609int nand_scan(struct mtd_info *mtd, int maxchips)
4610{
4611 int ret;
4612
Lei Wen75bde942011-01-06 09:48:18 +08004613 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01004614 if (!ret)
4615 ret = nand_scan_tail(mtd);
4616 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004617}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004618EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004619
Heiko Schocherf5895d12014-06-24 10:10:04 +02004620MODULE_LICENSE("GPL");
4621MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4622MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4623MODULE_DESCRIPTION("Generic NAND flash driver code");