blob: e490c84e84db8374e680c40b3c970ea7941140f6 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002 * Overview:
3 * This is the generic MTD driver for NAND flash devices. It should be
4 * capable of working with almost all NAND chips currently available.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02005 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 * Additional technical information is available on
Scott Wood3628f002008-10-24 16:20:43 -05007 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02008 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juul52c07962007-10-31 13:53:06 +010010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020011 *
William Juul52c07962007-10-31 13:53:06 +010012 * Credits:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020013 * David Woodhouse for adding multichip support
14 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020015 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
16 * rework for 2K page size chips
17 *
William Juul52c07962007-10-31 13:53:06 +010018 * TODO:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020019 * Enable cached programming for 2k page size chips
20 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapin3a38a552013-01-14 03:46:50 +000021 * if we have HW ECC support.
Scott Wood3628f002008-10-24 16:20:43 -050022 * BBT table is not serialized, has to be fixed
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020023 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020024 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License version 2 as
26 * published by the Free Software Foundation.
27 *
28 */
29
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31#include <common.h>
Brian Norrisba6463d2016-06-15 21:09:22 +020032#if CONFIG_IS_ENABLED(OF_CONTROL)
33#include <fdtdec.h>
34#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020035#include <malloc.h>
36#include <watchdog.h>
William Juul52c07962007-10-31 13:53:06 +010037#include <linux/err.h>
Mike Frysinger11d1a092012-04-09 13:39:55 +000038#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020039#include <linux/mtd/mtd.h>
40#include <linux/mtd/nand.h>
41#include <linux/mtd/nand_ecc.h>
Christian Hitz55f7bca2011-10-12 09:31:59 +020042#include <linux/mtd/nand_bch.h>
Stefan Roesefa252ea2009-04-24 15:58:33 +020043#ifdef CONFIG_MTD_PARTITIONS
44#include <linux/mtd/partitions.h>
45#endif
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020046#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090047#include <linux/errno.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020048
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020049/* Define default oob placement schemes for large and small page devices */
William Juul52c07962007-10-31 13:53:06 +010050static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051 .eccbytes = 3,
52 .eccpos = {0, 1, 2},
William Juul52c07962007-10-31 13:53:06 +010053 .oobfree = {
54 {.offset = 3,
55 .length = 2},
56 {.offset = 6,
Christian Hitz13fc0e22011-10-12 09:32:01 +020057 .length = 2} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020058};
59
William Juul52c07962007-10-31 13:53:06 +010060static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020061 .eccbytes = 6,
62 .eccpos = {0, 1, 2, 3, 6, 7},
William Juul52c07962007-10-31 13:53:06 +010063 .oobfree = {
64 {.offset = 8,
Christian Hitz13fc0e22011-10-12 09:32:01 +020065 . length = 8} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020066};
67
William Juul52c07962007-10-31 13:53:06 +010068static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020069 .eccbytes = 24,
70 .eccpos = {
William Juul52c07962007-10-31 13:53:06 +010071 40, 41, 42, 43, 44, 45, 46, 47,
72 48, 49, 50, 51, 52, 53, 54, 55,
73 56, 57, 58, 59, 60, 61, 62, 63},
74 .oobfree = {
75 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020076 .length = 38} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020077};
78
William Juul52c07962007-10-31 13:53:06 +010079static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov04fbaa02008-06-06 15:42:43 +020080 .eccbytes = 48,
81 .eccpos = {
Christian Hitz13fc0e22011-10-12 09:32:01 +020082 80, 81, 82, 83, 84, 85, 86, 87,
83 88, 89, 90, 91, 92, 93, 94, 95,
84 96, 97, 98, 99, 100, 101, 102, 103,
William Juul52c07962007-10-31 13:53:06 +010085 104, 105, 106, 107, 108, 109, 110, 111,
86 112, 113, 114, 115, 116, 117, 118, 119,
87 120, 121, 122, 123, 124, 125, 126, 127},
88 .oobfree = {
89 {.offset = 2,
Christian Hitz13fc0e22011-10-12 09:32:01 +020090 .length = 78} }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020091};
92
Heiko Schocherf5895d12014-06-24 10:10:04 +020093static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juul52c07962007-10-31 13:53:06 +010094
95static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
96 struct mtd_oob_ops *ops);
97
Heiko Schocherf5895d12014-06-24 10:10:04 +020098/*
99 * For devices which display every fart in the system on a separate LED. Is
100 * compiled away when LED support is disabled.
101 */
102DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov04fbaa02008-06-06 15:42:43 +0200103
Christian Hitzb8a6b372011-10-12 09:32:02 +0200104static int check_offs_len(struct mtd_info *mtd,
105 loff_t ofs, uint64_t len)
106{
Scott Wood17fed142016-05-30 13:57:56 -0500107 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200108 int ret = 0;
109
110 /* Start address must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200111 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
112 pr_debug("%s: unaligned address\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200113 ret = -EINVAL;
114 }
115
116 /* Length must align on block boundary */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200117 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
118 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200119 ret = -EINVAL;
120 }
121
Christian Hitzb8a6b372011-10-12 09:32:02 +0200122 return ret;
123}
124
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200125/**
126 * nand_release_device - [GENERIC] release chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000127 * @mtd: MTD device structure
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200128 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200129 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200130 */
Christian Hitz13fc0e22011-10-12 09:32:01 +0200131static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100132{
Scott Wood17fed142016-05-30 13:57:56 -0500133 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200134
135 /* De-select the NAND device */
136 chip->select_chip(mtd, -1);
Wolfgang Denk9a08dd12005-11-02 14:29:12 +0100137}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200138
139/**
140 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000141 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200143 * Default read function for 8bit buswidth
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200144 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000145uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200146{
Scott Wood17fed142016-05-30 13:57:56 -0500147 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100148 return readb(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200149}
150
151/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200152 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000153 * @mtd: MTD device structure
154 *
155 * Default read function for 16bit buswidth with endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200156 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200157 */
William Juul52c07962007-10-31 13:53:06 +0100158static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200159{
Scott Wood17fed142016-05-30 13:57:56 -0500160 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100161 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200162}
163
164/**
165 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000166 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200167 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000168 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200169 */
170static u16 nand_read_word(struct mtd_info *mtd)
171{
Scott Wood17fed142016-05-30 13:57:56 -0500172 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100173 return readw(chip->IO_ADDR_R);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200174}
175
176/**
177 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapin3a38a552013-01-14 03:46:50 +0000178 * @mtd: MTD device structure
179 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200180 *
181 * Default select function for 1 chip devices.
182 */
William Juul52c07962007-10-31 13:53:06 +0100183static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200184{
Scott Wood17fed142016-05-30 13:57:56 -0500185 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100186
187 switch (chipnr) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200188 case -1:
William Juul52c07962007-10-31 13:53:06 +0100189 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200190 break;
191 case 0:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200192 break;
193
194 default:
195 BUG();
196 }
197}
198
199/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200200 * nand_write_byte - [DEFAULT] write single byte to chip
201 * @mtd: MTD device structure
202 * @byte: value to write
203 *
204 * Default function to write a byte to I/O[7:0]
205 */
206static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
207{
Scott Wood17fed142016-05-30 13:57:56 -0500208 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200209
210 chip->write_buf(mtd, &byte, 1);
211}
212
213/**
214 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
215 * @mtd: MTD device structure
216 * @byte: value to write
217 *
218 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
219 */
220static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
221{
Scott Wood17fed142016-05-30 13:57:56 -0500222 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200223 uint16_t word = byte;
224
225 /*
226 * It's not entirely clear what should happen to I/O[15:8] when writing
227 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
228 *
229 * When the host supports a 16-bit bus width, only data is
230 * transferred at the 16-bit width. All address and command line
231 * transfers shall use only the lower 8-bits of the data bus. During
232 * command transfers, the host may place any value on the upper
233 * 8-bits of the data bus. During address transfers, the host shall
234 * set the upper 8-bits of the data bus to 00h.
235 *
236 * One user of the write_byte callback is nand_onfi_set_features. The
237 * four parameters are specified to be written to I/O[7:0], but this is
238 * neither an address nor a command transfer. Let's assume a 0 on the
239 * upper I/O lines is OK.
240 */
241 chip->write_buf(mtd, (uint8_t *)&word, 2);
242}
243
Heiko Schocherf5895d12014-06-24 10:10:04 +0200244static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
245{
246 int i;
247
248 for (i = 0; i < len; i++)
249 writeb(buf[i], addr);
250}
251static void ioread8_rep(void *addr, uint8_t *buf, int len)
252{
253 int i;
254
255 for (i = 0; i < len; i++)
256 buf[i] = readb(addr);
257}
258
259static void ioread16_rep(void *addr, void *buf, int len)
260{
261 int i;
262 u16 *p = (u16 *) buf;
Stefan Roesea9e99542014-09-05 09:57:01 +0200263
Heiko Schocherf5895d12014-06-24 10:10:04 +0200264 for (i = 0; i < len; i++)
265 p[i] = readw(addr);
266}
267
268static void iowrite16_rep(void *addr, void *buf, int len)
269{
270 int i;
271 u16 *p = (u16 *) buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200272
273 for (i = 0; i < len; i++)
274 writew(p[i], addr);
275}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276
277/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200278 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000279 * @mtd: MTD device structure
280 * @buf: data buffer
281 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000283 * Default write function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200284 */
Simon Schwarz5a9fc192011-10-31 06:34:44 +0000285void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200286{
Scott Wood17fed142016-05-30 13:57:56 -0500287 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200288
Heiko Schocherf5895d12014-06-24 10:10:04 +0200289 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200290}
291
292/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200293 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000294 * @mtd: MTD device structure
295 * @buf: buffer to store date
296 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200297 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000298 * Default read function for 8bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200299 */
Simon Schwarz4f62e982011-09-14 15:30:16 -0400300void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200301{
Scott Wood17fed142016-05-30 13:57:56 -0500302 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303
Heiko Schocherf5895d12014-06-24 10:10:04 +0200304 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200305}
306
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200307/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200308 * nand_write_buf16 - [DEFAULT] write buffer to chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000309 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200310 * @buf: data buffer
311 * @len: number of bytes to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200313 * Default write function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200314 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200315void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200316{
Scott Wood17fed142016-05-30 13:57:56 -0500317 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200319
Heiko Schocherf5895d12014-06-24 10:10:04 +0200320 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200321}
322
323/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324 * nand_read_buf16 - [DEFAULT] read chip data into buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000325 * @mtd: MTD device structure
Heiko Schocherf5895d12014-06-24 10:10:04 +0200326 * @buf: buffer to store date
327 * @len: number of bytes to read
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200328 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200329 * Default read function for 16bit buswidth.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200330 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200331void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200332{
Scott Wood17fed142016-05-30 13:57:56 -0500333 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200334 u16 *p = (u16 *) buf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200335
Heiko Schocherf5895d12014-06-24 10:10:04 +0200336 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200337}
338
339/**
340 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapin3a38a552013-01-14 03:46:50 +0000341 * @mtd: MTD device structure
342 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200343 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200344 * Check, if the block is bad.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200345 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500346static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200347{
Scott Wood52ab7ce2016-05-30 13:57:58 -0500348 int page, res = 0, i = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500349 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200350 u16 bad;
351
Sergey Lapin3a38a552013-01-14 03:46:50 +0000352 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitzb8a6b372011-10-12 09:32:02 +0200353 ofs += mtd->erasesize - mtd->writesize;
354
William Juul52c07962007-10-31 13:53:06 +0100355 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knobloch9e2aeaf2007-05-05 07:04:42 +0200356
Sergey Lapin3a38a552013-01-14 03:46:50 +0000357 do {
358 if (chip->options & NAND_BUSWIDTH_16) {
359 chip->cmdfunc(mtd, NAND_CMD_READOOB,
360 chip->badblockpos & 0xFE, page);
361 bad = cpu_to_le16(chip->read_word(mtd));
362 if (chip->badblockpos & 0x1)
363 bad >>= 8;
364 else
365 bad &= 0xFF;
366 } else {
367 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
368 page);
369 bad = chip->read_byte(mtd);
370 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200371
Sergey Lapin3a38a552013-01-14 03:46:50 +0000372 if (likely(chip->badblockbits == 8))
373 res = bad != 0xFF;
374 else
375 res = hweight8(bad) < chip->badblockbits;
376 ofs += mtd->writesize;
377 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
378 i++;
379 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitzb8a6b372011-10-12 09:32:02 +0200380
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200381 return res;
382}
383
384/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200385 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapin3a38a552013-01-14 03:46:50 +0000386 * @mtd: MTD device structure
387 * @ofs: offset from device start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200388 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000389 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherf5895d12014-06-24 10:10:04 +0200390 * specific driver. It provides the details for writing a bad block marker to a
391 * block.
392 */
393static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
394{
Scott Wood17fed142016-05-30 13:57:56 -0500395 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200396 struct mtd_oob_ops ops;
397 uint8_t buf[2] = { 0, 0 };
398 int ret = 0, res, i = 0;
399
Scott Wood3ea94ed2015-06-26 19:03:26 -0500400 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200401 ops.oobbuf = buf;
402 ops.ooboffs = chip->badblockpos;
403 if (chip->options & NAND_BUSWIDTH_16) {
404 ops.ooboffs &= ~0x01;
405 ops.len = ops.ooblen = 2;
406 } else {
407 ops.len = ops.ooblen = 1;
408 }
409 ops.mode = MTD_OPS_PLACE_OOB;
410
411 /* Write to first/last page(s) if necessary */
412 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
413 ofs += mtd->erasesize - mtd->writesize;
414 do {
415 res = nand_do_write_oob(mtd, ofs, &ops);
416 if (!ret)
417 ret = res;
418
419 i++;
420 ofs += mtd->writesize;
421 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
422
423 return ret;
424}
425
426/**
427 * nand_block_markbad_lowlevel - mark a block bad
428 * @mtd: MTD device structure
429 * @ofs: offset from device start
430 *
431 * This function performs the generic NAND bad block marking steps (i.e., bad
432 * block table(s) and/or marker(s)). We only allow the hardware driver to
433 * specify how to write bad block markers to OOB (chip->block_markbad).
434 *
435 * We try operations in the following order:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000436 * (1) erase the affected block, to allow OOB marker to be written cleanly
Heiko Schocherf5895d12014-06-24 10:10:04 +0200437 * (2) write bad block marker to OOB area of affected block (unless flag
438 * NAND_BBT_NO_OOB_BBM is present)
439 * (3) update the BBT
440 * Note that we retain the first error encountered in (2) or (3), finish the
Sergey Lapin3a38a552013-01-14 03:46:50 +0000441 * procedures, and dump the error in the end.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200442*/
Heiko Schocherf5895d12014-06-24 10:10:04 +0200443static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200444{
Scott Wood17fed142016-05-30 13:57:56 -0500445 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200446 int res, ret = 0;
Christian Hitzb8a6b372011-10-12 09:32:02 +0200447
Heiko Schocherf5895d12014-06-24 10:10:04 +0200448 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000449 struct erase_info einfo;
450
451 /* Attempt erase before marking OOB */
452 memset(&einfo, 0, sizeof(einfo));
453 einfo.mtd = mtd;
454 einfo.addr = ofs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200455 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000456 nand_erase_nand(mtd, &einfo, 0);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200457
Heiko Schocherf5895d12014-06-24 10:10:04 +0200458 /* Write bad block marker to OOB */
459 nand_get_device(mtd, FL_WRITING);
460 ret = chip->block_markbad(mtd, ofs);
Scott Wood3628f002008-10-24 16:20:43 -0500461 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +0100462 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000463
Heiko Schocherf5895d12014-06-24 10:10:04 +0200464 /* Mark block bad in BBT */
465 if (chip->bbt) {
466 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000467 if (!ret)
468 ret = res;
469 }
470
William Juul52c07962007-10-31 13:53:06 +0100471 if (!ret)
472 mtd->ecc_stats.badblocks++;
Scott Wood3628f002008-10-24 16:20:43 -0500473
William Juul52c07962007-10-31 13:53:06 +0100474 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200475}
476
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200477/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200478 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapin3a38a552013-01-14 03:46:50 +0000479 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200480 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000481 * Check, if the device is write protected. The function expects, that the
482 * device is already selected.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200483 */
William Juul52c07962007-10-31 13:53:06 +0100484static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200485{
Scott Wood17fed142016-05-30 13:57:56 -0500486 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitzb8a6b372011-10-12 09:32:02 +0200487
Sergey Lapin3a38a552013-01-14 03:46:50 +0000488 /* Broken xD cards report WP despite being writable */
Christian Hitzb8a6b372011-10-12 09:32:02 +0200489 if (chip->options & NAND_BROKEN_XD)
490 return 0;
491
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200492 /* Check the WP bit */
William Juul52c07962007-10-31 13:53:06 +0100493 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
494 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200495}
Markus Klotzbücher27eba142006-03-06 15:04:25 +0100496
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200497/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500498 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000499 * @mtd: MTD device structure
500 * @ofs: offset from device start
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300501 *
Scott Wood3ea94ed2015-06-26 19:03:26 -0500502 * Check if the block is marked as reserved.
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300503 */
504static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
505{
Scott Wood17fed142016-05-30 13:57:56 -0500506 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300507
508 if (!chip->bbt)
509 return 0;
510 /* Return info from the table */
511 return nand_isreserved_bbt(mtd, ofs);
512}
513
514/**
515 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
516 * @mtd: MTD device structure
517 * @ofs: offset from device start
Sergey Lapin3a38a552013-01-14 03:46:50 +0000518 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200519 *
520 * Check, if the block is bad. Either by reading the bad block table or
521 * calling of the scan function.
522 */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500523static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524{
Scott Wood17fed142016-05-30 13:57:56 -0500525 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200526
Masahiro Yamada8d100542014-12-26 22:20:58 +0900527 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
528 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200529 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamada8c6c14a2014-12-26 22:20:57 +0900530 chip->scan_bbt(mtd);
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +0200531 }
532
William Juul52c07962007-10-31 13:53:06 +0100533 if (!chip->bbt)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500534 return chip->block_bad(mtd, ofs);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200535
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200536 /* Return info from the table */
William Juul52c07962007-10-31 13:53:06 +0100537 return nand_isbad_bbt(mtd, ofs, allowbbt);
538}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200539
Scott Wood52ab7ce2016-05-30 13:57:58 -0500540/**
541 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
542 * @mtd: MTD device structure
543 *
544 * Wait for the ready pin after a command, and warn if a timeout occurs.
545 */
William Juul52c07962007-10-31 13:53:06 +0100546void nand_wait_ready(struct mtd_info *mtd)
547{
Scott Wood17fed142016-05-30 13:57:56 -0500548 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500549 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000550 u32 time_start;
Stefan Roesea5c312c2008-01-05 16:43:25 +0100551
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000552 time_start = get_timer(0);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000553 /* Wait until command is processed or timeout occurs */
Reinhard Meyer4d1fb182010-11-18 03:14:26 +0000554 while (get_timer(time_start) < timeo) {
Stefan Roesea5c312c2008-01-05 16:43:25 +0100555 if (chip->dev_ready)
556 if (chip->dev_ready(mtd))
557 break;
558 }
Scott Wood52ab7ce2016-05-30 13:57:58 -0500559
560 if (!chip->dev_ready(mtd))
561 pr_warn("timeout while waiting for chip to become ready\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200562}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200563EXPORT_SYMBOL_GPL(nand_wait_ready);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200564
565/**
Scott Wood3ea94ed2015-06-26 19:03:26 -0500566 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
567 * @mtd: MTD device structure
568 * @timeo: Timeout in ms
569 *
570 * Wait for status ready (i.e. command done) or timeout.
571 */
572static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
573{
Scott Wood17fed142016-05-30 13:57:56 -0500574 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500575 u32 time_start;
576
577 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
578 time_start = get_timer(0);
579 while (get_timer(time_start) < timeo) {
580 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
581 break;
582 WATCHDOG_RESET();
583 }
584};
585
586/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200587 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000588 * @mtd: MTD device structure
589 * @command: the command to be sent
590 * @column: the column address for this command, -1 if none
591 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200592 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000593 * Send command to NAND device. This function is used for small page devices
Heiko Schocherf5895d12014-06-24 10:10:04 +0200594 * (512 Bytes per page).
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200595 */
William Juul52c07962007-10-31 13:53:06 +0100596static void nand_command(struct mtd_info *mtd, unsigned int command,
597 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200598{
Scott Wood17fed142016-05-30 13:57:56 -0500599 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +0100600 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200601
Sergey Lapin3a38a552013-01-14 03:46:50 +0000602 /* Write out the command to the device */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 if (command == NAND_CMD_SEQIN) {
604 int readcmd;
605
William Juul52c07962007-10-31 13:53:06 +0100606 if (column >= mtd->writesize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200607 /* OOB area */
William Juul52c07962007-10-31 13:53:06 +0100608 column -= mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200609 readcmd = NAND_CMD_READOOB;
610 } else if (column < 256) {
611 /* First 256 bytes --> READ0 */
612 readcmd = NAND_CMD_READ0;
613 } else {
614 column -= 256;
615 readcmd = NAND_CMD_READ1;
616 }
William Juul52c07962007-10-31 13:53:06 +0100617 chip->cmd_ctrl(mtd, readcmd, ctrl);
618 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619 }
William Juul52c07962007-10-31 13:53:06 +0100620 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621
Sergey Lapin3a38a552013-01-14 03:46:50 +0000622 /* Address cycle, when necessary */
William Juul52c07962007-10-31 13:53:06 +0100623 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
624 /* Serially input address */
625 if (column != -1) {
626 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200627 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530628 !nand_opcode_8bits(command))
William Juul52c07962007-10-31 13:53:06 +0100629 column >>= 1;
630 chip->cmd_ctrl(mtd, column, ctrl);
631 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200632 }
William Juul52c07962007-10-31 13:53:06 +0100633 if (page_addr != -1) {
634 chip->cmd_ctrl(mtd, page_addr, ctrl);
635 ctrl &= ~NAND_CTRL_CHANGE;
636 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
637 /* One more address cycle for devices > 32MiB */
638 if (chip->chipsize > (32 << 20))
639 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
640 }
641 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200642
643 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000644 * Program and erase have their own busy handlers status and sequential
645 * in needs no delay
William Juul52c07962007-10-31 13:53:06 +0100646 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200647 switch (command) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200648
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200649 case NAND_CMD_PAGEPROG:
650 case NAND_CMD_ERASE1:
651 case NAND_CMD_ERASE2:
652 case NAND_CMD_SEQIN:
653 case NAND_CMD_STATUS:
Masahiro Yamada7f9baa12017-09-15 21:44:58 +0900654 case NAND_CMD_READID:
Masahiro Yamada0cd10182017-09-15 21:44:59 +0900655 case NAND_CMD_SET_FEATURES:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200656 return;
657
658 case NAND_CMD_RESET:
William Juul52c07962007-10-31 13:53:06 +0100659 if (chip->dev_ready)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200660 break;
William Juul52c07962007-10-31 13:53:06 +0100661 udelay(chip->chip_delay);
662 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
663 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
664 chip->cmd_ctrl(mtd,
665 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Wood3ea94ed2015-06-26 19:03:26 -0500666 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
667 nand_wait_status_ready(mtd, 250);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200668 return;
669
William Juul52c07962007-10-31 13:53:06 +0100670 /* This applies to read commands */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200671 default:
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200672 /*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200673 * If we don't have access to the busy pin, we apply the given
674 * command delay
William Juul52c07962007-10-31 13:53:06 +0100675 */
676 if (!chip->dev_ready) {
677 udelay(chip->chip_delay);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200678 return;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200679 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200680 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000681 /*
682 * Apply this short delay always to ensure that we do wait tWB in
683 * any case on any machine.
684 */
William Juul52c07962007-10-31 13:53:06 +0100685 ndelay(100);
686
687 nand_wait_ready(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200688}
689
690/**
691 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000692 * @mtd: MTD device structure
693 * @command: the command to be sent
694 * @column: the column address for this command, -1 if none
695 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200696 *
William Juul52c07962007-10-31 13:53:06 +0100697 * Send command to NAND device. This is the version for the new large page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000698 * devices. We don't have the separate regions as we have in the small page
699 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200700 */
William Juul52c07962007-10-31 13:53:06 +0100701static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
702 int column, int page_addr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200703{
Scott Wood17fed142016-05-30 13:57:56 -0500704 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200705
706 /* Emulate NAND_CMD_READOOB */
707 if (command == NAND_CMD_READOOB) {
William Juul52c07962007-10-31 13:53:06 +0100708 column += mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200709 command = NAND_CMD_READ0;
710 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200711
William Juul52c07962007-10-31 13:53:06 +0100712 /* Command latch cycle */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200713 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200714
715 if (column != -1 || page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100716 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717
718 /* Serially input address */
719 if (column != -1) {
720 /* Adjust columns for 16 bit buswidth */
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200721 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris67675222014-05-06 00:46:17 +0530722 !nand_opcode_8bits(command))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200723 column >>= 1;
William Juul52c07962007-10-31 13:53:06 +0100724 chip->cmd_ctrl(mtd, column, ctrl);
725 ctrl &= ~NAND_CTRL_CHANGE;
726 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200727 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200728 if (page_addr != -1) {
William Juul52c07962007-10-31 13:53:06 +0100729 chip->cmd_ctrl(mtd, page_addr, ctrl);
730 chip->cmd_ctrl(mtd, page_addr >> 8,
731 NAND_NCE | NAND_ALE);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200732 /* One more address cycle for devices > 128MiB */
William Juul52c07962007-10-31 13:53:06 +0100733 if (chip->chipsize > (128 << 20))
734 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/**
Christian Hitz13fc0e22011-10-12 09:32:01 +02001869 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapin3a38a552013-01-14 03:46:50 +00001870 * @mtd: MTD device structure
1871 * @from: offset to read from
1872 * @len: number of bytes to read
1873 * @retlen: pointer to variable to store the number of read bytes
1874 * @buf: the databuffer to put data
William Juul52c07962007-10-31 13:53:06 +01001875 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001876 * Get hold of the chip and call nand_do_read.
William Juul52c07962007-10-31 13:53:06 +01001877 */
1878static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1879 size_t *retlen, uint8_t *buf)
1880{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001881 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01001882 int ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001883
Heiko Schocherf5895d12014-06-24 10:10:04 +02001884 nand_get_device(mtd, FL_READING);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001885 memset(&ops, 0, sizeof(ops));
Sergey Lapin3a38a552013-01-14 03:46:50 +00001886 ops.len = len;
1887 ops.datbuf = buf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001888 ops.mode = MTD_OPS_PLACE_OOB;
1889 ret = nand_do_read_ops(mtd, from, &ops);
1890 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01001891 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01001892 return ret;
1893}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001894
William Juul52c07962007-10-31 13:53:06 +01001895/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001896 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1897 * @mtd: mtd info structure
1898 * @chip: nand chip info structure
1899 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001900 */
1901static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001902 int page)
William Juul52c07962007-10-31 13:53:06 +01001903{
Sergey Lapin3a38a552013-01-14 03:46:50 +00001904 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juul52c07962007-10-31 13:53:06 +01001905 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001906 return 0;
William Juul52c07962007-10-31 13:53:06 +01001907}
1908
1909/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001910 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juul52c07962007-10-31 13:53:06 +01001911 * with syndromes
Sergey Lapin3a38a552013-01-14 03:46:50 +00001912 * @mtd: mtd info structure
1913 * @chip: nand chip info structure
1914 * @page: page number to read
William Juul52c07962007-10-31 13:53:06 +01001915 */
1916static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001917 int page)
William Juul52c07962007-10-31 13:53:06 +01001918{
William Juul52c07962007-10-31 13:53:06 +01001919 int length = mtd->oobsize;
1920 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1921 int eccsize = chip->ecc.size;
Scott Wood3ea94ed2015-06-26 19:03:26 -05001922 uint8_t *bufpoi = chip->oob_poi;
William Juul52c07962007-10-31 13:53:06 +01001923 int i, toread, sndrnd = 0, pos;
1924
1925 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1926 for (i = 0; i < chip->ecc.steps; i++) {
1927 if (sndrnd) {
1928 pos = eccsize + i * (eccsize + chunk);
1929 if (mtd->writesize > 512)
1930 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1931 else
1932 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001933 } else
William Juul52c07962007-10-31 13:53:06 +01001934 sndrnd = 1;
1935 toread = min_t(int, length, chunk);
1936 chip->read_buf(mtd, bufpoi, toread);
1937 bufpoi += toread;
1938 length -= toread;
1939 }
1940 if (length > 0)
1941 chip->read_buf(mtd, bufpoi, length);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001942
Sergey Lapin3a38a552013-01-14 03:46:50 +00001943 return 0;
William Juul52c07962007-10-31 13:53:06 +01001944}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001945
William Juul52c07962007-10-31 13:53:06 +01001946/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001947 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1948 * @mtd: mtd info structure
1949 * @chip: nand chip info structure
1950 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001951 */
1952static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1953 int page)
1954{
1955 int status = 0;
1956 const uint8_t *buf = chip->oob_poi;
1957 int length = mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001958
William Juul52c07962007-10-31 13:53:06 +01001959 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1960 chip->write_buf(mtd, buf, length);
1961 /* Send command to program the OOB data */
1962 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001963
William Juul52c07962007-10-31 13:53:06 +01001964 status = chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001965
William Juul52c07962007-10-31 13:53:06 +01001966 return status & NAND_STATUS_FAIL ? -EIO : 0;
1967}
1968
1969/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001970 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1971 * with syndrome - only for large page flash
1972 * @mtd: mtd info structure
1973 * @chip: nand chip info structure
1974 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01001975 */
1976static int nand_write_oob_syndrome(struct mtd_info *mtd,
1977 struct nand_chip *chip, int page)
1978{
1979 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1980 int eccsize = chip->ecc.size, length = mtd->oobsize;
1981 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1982 const uint8_t *bufpoi = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001983
1984 /*
William Juul52c07962007-10-31 13:53:06 +01001985 * data-ecc-data-ecc ... ecc-oob
1986 * or
1987 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001988 */
William Juul52c07962007-10-31 13:53:06 +01001989 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1990 pos = steps * (eccsize + chunk);
1991 steps = 0;
1992 } else
1993 pos = eccsize;
1994
1995 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1996 for (i = 0; i < steps; i++) {
1997 if (sndcmd) {
1998 if (mtd->writesize <= 512) {
1999 uint32_t fill = 0xFFFFFFFF;
2000
2001 len = eccsize;
2002 while (len > 0) {
2003 int num = min_t(int, len, 4);
2004 chip->write_buf(mtd, (uint8_t *)&fill,
2005 num);
2006 len -= num;
2007 }
2008 } else {
2009 pos = eccsize + i * (eccsize + chunk);
2010 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2011 }
2012 } else
2013 sndcmd = 1;
2014 len = min_t(int, length, chunk);
2015 chip->write_buf(mtd, bufpoi, len);
2016 bufpoi += len;
2017 length -= len;
2018 }
2019 if (length > 0)
2020 chip->write_buf(mtd, bufpoi, length);
2021
2022 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2023 status = chip->waitfunc(mtd, chip);
2024
2025 return status & NAND_STATUS_FAIL ? -EIO : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002026}
2027
2028/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002029 * nand_do_read_oob - [INTERN] NAND read out-of-band
2030 * @mtd: MTD device structure
2031 * @from: offset to read from
2032 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002033 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002034 * NAND read out-of-band data from the spare area.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002035 */
William Juul52c07962007-10-31 13:53:06 +01002036static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2037 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002038{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002039 int page, realpage, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002040 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002041 struct mtd_ecc_stats stats;
William Juul52c07962007-10-31 13:53:06 +01002042 int readlen = ops->ooblen;
2043 int len;
2044 uint8_t *buf = ops->oobbuf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002045 int ret = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002046
Heiko Schocherf5895d12014-06-24 10:10:04 +02002047 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002048 __func__, (unsigned long long)from, readlen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002049
Sergey Lapin3a38a552013-01-14 03:46:50 +00002050 stats = mtd->ecc_stats;
2051
Scott Wood52ab7ce2016-05-30 13:57:58 -05002052 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002053
William Juul52c07962007-10-31 13:53:06 +01002054 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002055 pr_debug("%s: attempt to start read outside oob\n",
2056 __func__);
William Juul52c07962007-10-31 13:53:06 +01002057 return -EINVAL;
2058 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002059
2060 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002061 if (unlikely(from >= mtd->size ||
2062 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2063 (from >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002064 pr_debug("%s: attempt to read beyond end of device\n",
2065 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002066 return -EINVAL;
2067 }
2068
William Juul52c07962007-10-31 13:53:06 +01002069 chipnr = (int)(from >> chip->chip_shift);
2070 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002071
William Juul52c07962007-10-31 13:53:06 +01002072 /* Shift to get page */
2073 realpage = (int)(from >> chip->page_shift);
2074 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002075
Christian Hitz13fc0e22011-10-12 09:32:01 +02002076 while (1) {
Scott Woodea95b642011-02-02 18:15:57 -06002077 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002078
Sergey Lapin3a38a552013-01-14 03:46:50 +00002079 if (ops->mode == MTD_OPS_RAW)
2080 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2081 else
2082 ret = chip->ecc.read_oob(mtd, chip, page);
2083
2084 if (ret < 0)
2085 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002086
William Juul52c07962007-10-31 13:53:06 +01002087 len = min(len, readlen);
2088 buf = nand_transfer_oob(chip, buf, ops, len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002089
Heiko Schocherf5895d12014-06-24 10:10:04 +02002090 if (chip->options & NAND_NEED_READRDY) {
2091 /* Apply delay or wait for ready/busy pin */
2092 if (!chip->dev_ready)
2093 udelay(chip->chip_delay);
2094 else
2095 nand_wait_ready(mtd);
2096 }
2097
William Juul52c07962007-10-31 13:53:06 +01002098 readlen -= len;
2099 if (!readlen)
2100 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002101
William Juul52c07962007-10-31 13:53:06 +01002102 /* Increment page address */
2103 realpage++;
2104
2105 page = realpage & chip->pagemask;
2106 /* Check, if we cross a chip boundary */
2107 if (!page) {
2108 chipnr++;
2109 chip->select_chip(mtd, -1);
2110 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002111 }
William Juul52c07962007-10-31 13:53:06 +01002112 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002113 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002114
Sergey Lapin3a38a552013-01-14 03:46:50 +00002115 ops->oobretlen = ops->ooblen - readlen;
2116
2117 if (ret < 0)
2118 return ret;
2119
2120 if (mtd->ecc_stats.failed - stats.failed)
2121 return -EBADMSG;
2122
2123 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002124}
2125
2126/**
William Juul52c07962007-10-31 13:53:06 +01002127 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002128 * @mtd: MTD device structure
2129 * @from: offset to read from
2130 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002131 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002132 * NAND read data and/or out-of-band data.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002133 */
William Juul52c07962007-10-31 13:53:06 +01002134static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2135 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002136{
William Juul52c07962007-10-31 13:53:06 +01002137 int ret = -ENOTSUPP;
2138
2139 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002140
2141 /* Do not allow reads past end of device */
William Juul52c07962007-10-31 13:53:06 +01002142 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002143 pr_debug("%s: attempt to read beyond end of device\n",
2144 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002145 return -EINVAL;
2146 }
2147
Heiko Schocherf5895d12014-06-24 10:10:04 +02002148 nand_get_device(mtd, FL_READING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002149
Christian Hitz13fc0e22011-10-12 09:32:01 +02002150 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002151 case MTD_OPS_PLACE_OOB:
2152 case MTD_OPS_AUTO_OOB:
2153 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002154 break;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002155
William Juul52c07962007-10-31 13:53:06 +01002156 default:
2157 goto out;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002158 }
2159
William Juul52c07962007-10-31 13:53:06 +01002160 if (!ops->datbuf)
2161 ret = nand_do_read_oob(mtd, from, ops);
2162 else
2163 ret = nand_do_read_ops(mtd, from, ops);
2164
Christian Hitz13fc0e22011-10-12 09:32:01 +02002165out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002166 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002167 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002168}
2169
2170
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002171/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002172 * nand_write_page_raw - [INTERN] raw page write function
2173 * @mtd: mtd info structure
2174 * @chip: nand chip info structure
2175 * @buf: data buffer
2176 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002177 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002178 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002179 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juul52c07962007-10-31 13:53:06 +01002180 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002181static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002182 const uint8_t *buf, int oob_required, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002183{
William Juul52c07962007-10-31 13:53:06 +01002184 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002185 if (oob_required)
2186 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2187
2188 return 0;
William Juul52c07962007-10-31 13:53:06 +01002189}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002190
William Juul52c07962007-10-31 13:53:06 +01002191/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002192 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2193 * @mtd: mtd info structure
2194 * @chip: nand chip info structure
2195 * @buf: data buffer
2196 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002197 * @page: page number to write
David Brownellee86b8d2009-11-07 16:27:01 -05002198 *
2199 * We need a special oob layout and handling even when ECC isn't checked.
2200 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002201static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz13fc0e22011-10-12 09:32:01 +02002202 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002203 const uint8_t *buf, int oob_required,
2204 int page)
David Brownellee86b8d2009-11-07 16:27:01 -05002205{
2206 int eccsize = chip->ecc.size;
2207 int eccbytes = chip->ecc.bytes;
2208 uint8_t *oob = chip->oob_poi;
2209 int steps, size;
2210
2211 for (steps = chip->ecc.steps; steps > 0; steps--) {
2212 chip->write_buf(mtd, buf, eccsize);
2213 buf += eccsize;
2214
2215 if (chip->ecc.prepad) {
2216 chip->write_buf(mtd, oob, chip->ecc.prepad);
2217 oob += chip->ecc.prepad;
2218 }
2219
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002220 chip->write_buf(mtd, oob, eccbytes);
David Brownellee86b8d2009-11-07 16:27:01 -05002221 oob += eccbytes;
2222
2223 if (chip->ecc.postpad) {
2224 chip->write_buf(mtd, oob, chip->ecc.postpad);
2225 oob += chip->ecc.postpad;
2226 }
2227 }
2228
2229 size = mtd->oobsize - (oob - chip->oob_poi);
2230 if (size)
2231 chip->write_buf(mtd, oob, size);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002232
2233 return 0;
David Brownellee86b8d2009-11-07 16:27:01 -05002234}
2235/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002236 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2237 * @mtd: mtd info structure
2238 * @chip: nand chip info structure
2239 * @buf: data buffer
2240 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002241 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002242 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002243static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002244 const uint8_t *buf, int oob_required,
2245 int page)
William Juul52c07962007-10-31 13:53:06 +01002246{
2247 int i, eccsize = chip->ecc.size;
2248 int eccbytes = chip->ecc.bytes;
2249 int eccsteps = chip->ecc.steps;
2250 uint8_t *ecc_calc = chip->buffers->ecccalc;
2251 const uint8_t *p = buf;
2252 uint32_t *eccpos = chip->ecc.layout->eccpos;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002253
Sergey Lapin3a38a552013-01-14 03:46:50 +00002254 /* Software ECC calculation */
William Juul52c07962007-10-31 13:53:06 +01002255 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2256 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002257
William Juul52c07962007-10-31 13:53:06 +01002258 for (i = 0; i < chip->ecc.total; i++)
2259 chip->oob_poi[eccpos[i]] = ecc_calc[i];
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002260
Scott Wood46e13102016-05-30 13:57:57 -05002261 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002262}
2263
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002264/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002265 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2266 * @mtd: mtd info structure
2267 * @chip: nand chip info structure
2268 * @buf: data buffer
2269 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002270 * @page: page number to write
William Juul52c07962007-10-31 13:53:06 +01002271 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002272static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002273 const uint8_t *buf, int oob_required,
2274 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002275{
William Juul52c07962007-10-31 13:53:06 +01002276 int i, eccsize = chip->ecc.size;
2277 int eccbytes = chip->ecc.bytes;
2278 int eccsteps = chip->ecc.steps;
2279 uint8_t *ecc_calc = chip->buffers->ecccalc;
2280 const uint8_t *p = buf;
2281 uint32_t *eccpos = chip->ecc.layout->eccpos;
2282
2283 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2284 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2285 chip->write_buf(mtd, p, eccsize);
2286 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2287 }
2288
2289 for (i = 0; i < chip->ecc.total; i++)
2290 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2291
2292 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002293
2294 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002295}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002296
Heiko Schocherf5895d12014-06-24 10:10:04 +02002297
2298/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002299 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002300 * @mtd: mtd info structure
2301 * @chip: nand chip info structure
2302 * @offset: column address of subpage within the page
2303 * @data_len: data length
2304 * @buf: data buffer
2305 * @oob_required: must write chip->oob_poi to OOB
Scott Wood46e13102016-05-30 13:57:57 -05002306 * @page: page number to write
Heiko Schocherf5895d12014-06-24 10:10:04 +02002307 */
2308static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2309 struct nand_chip *chip, uint32_t offset,
2310 uint32_t data_len, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -05002311 int oob_required, int page)
Heiko Schocherf5895d12014-06-24 10:10:04 +02002312{
2313 uint8_t *oob_buf = chip->oob_poi;
2314 uint8_t *ecc_calc = chip->buffers->ecccalc;
2315 int ecc_size = chip->ecc.size;
2316 int ecc_bytes = chip->ecc.bytes;
2317 int ecc_steps = chip->ecc.steps;
2318 uint32_t *eccpos = chip->ecc.layout->eccpos;
2319 uint32_t start_step = offset / ecc_size;
2320 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2321 int oob_bytes = mtd->oobsize / ecc_steps;
2322 int step, i;
2323
2324 for (step = 0; step < ecc_steps; step++) {
2325 /* configure controller for WRITE access */
2326 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2327
2328 /* write data (untouched subpages already masked by 0xFF) */
2329 chip->write_buf(mtd, buf, ecc_size);
2330
2331 /* mask ECC of un-touched subpages by padding 0xFF */
2332 if ((step < start_step) || (step > end_step))
2333 memset(ecc_calc, 0xff, ecc_bytes);
2334 else
2335 chip->ecc.calculate(mtd, buf, ecc_calc);
2336
2337 /* mask OOB of un-touched subpages by padding 0xFF */
2338 /* if oob_required, preserve OOB metadata of written subpage */
2339 if (!oob_required || (step < start_step) || (step > end_step))
2340 memset(oob_buf, 0xff, oob_bytes);
2341
2342 buf += ecc_size;
2343 ecc_calc += ecc_bytes;
2344 oob_buf += oob_bytes;
2345 }
2346
2347 /* copy calculated ECC for whole page to chip->buffer->oob */
2348 /* this include masked-value(0xFF) for unwritten subpages */
2349 ecc_calc = chip->buffers->ecccalc;
2350 for (i = 0; i < chip->ecc.total; i++)
2351 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2352
2353 /* write OOB buffer to NAND device */
2354 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2355
2356 return 0;
2357}
2358
2359
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002360/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002361 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2362 * @mtd: mtd info structure
2363 * @chip: nand chip info structure
2364 * @buf: data buffer
2365 * @oob_required: must write chip->oob_poi to OOB
Scott Wood52ab7ce2016-05-30 13:57:58 -05002366 * @page: page number to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002367 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002368 * The hw generator calculates the error syndrome automatically. Therefore we
2369 * need a special oob layout and handling.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002370 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002371static int nand_write_page_syndrome(struct mtd_info *mtd,
2372 struct nand_chip *chip,
Scott Wood46e13102016-05-30 13:57:57 -05002373 const uint8_t *buf, int oob_required,
2374 int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002375{
William Juul52c07962007-10-31 13:53:06 +01002376 int i, eccsize = chip->ecc.size;
2377 int eccbytes = chip->ecc.bytes;
2378 int eccsteps = chip->ecc.steps;
2379 const uint8_t *p = buf;
2380 uint8_t *oob = chip->oob_poi;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002381
William Juul52c07962007-10-31 13:53:06 +01002382 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002383
William Juul52c07962007-10-31 13:53:06 +01002384 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2385 chip->write_buf(mtd, p, eccsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002386
William Juul52c07962007-10-31 13:53:06 +01002387 if (chip->ecc.prepad) {
2388 chip->write_buf(mtd, oob, chip->ecc.prepad);
2389 oob += chip->ecc.prepad;
2390 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002391
William Juul52c07962007-10-31 13:53:06 +01002392 chip->ecc.calculate(mtd, p, oob);
2393 chip->write_buf(mtd, oob, eccbytes);
2394 oob += eccbytes;
2395
2396 if (chip->ecc.postpad) {
2397 chip->write_buf(mtd, oob, chip->ecc.postpad);
2398 oob += chip->ecc.postpad;
2399 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002400 }
2401
William Juul52c07962007-10-31 13:53:06 +01002402 /* Calculate remaining oob bytes */
2403 i = mtd->oobsize - (oob - chip->oob_poi);
2404 if (i)
2405 chip->write_buf(mtd, oob, i);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002406
2407 return 0;
William Juul52c07962007-10-31 13:53:06 +01002408}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002409
William Juul52c07962007-10-31 13:53:06 +01002410/**
2411 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapin3a38a552013-01-14 03:46:50 +00002412 * @mtd: MTD device structure
2413 * @chip: NAND chip descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +02002414 * @offset: address offset within the page
2415 * @data_len: length of actual data to be written
Sergey Lapin3a38a552013-01-14 03:46:50 +00002416 * @buf: the data to write
2417 * @oob_required: must write chip->oob_poi to OOB
2418 * @page: page number to write
Sergey Lapin3a38a552013-01-14 03:46:50 +00002419 * @raw: use _raw version of write_page
William Juul52c07962007-10-31 13:53:06 +01002420 */
2421static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002422 uint32_t offset, int data_len, const uint8_t *buf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002423 int oob_required, int page, int raw)
William Juul52c07962007-10-31 13:53:06 +01002424{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002425 int status, subpage;
2426
2427 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2428 chip->ecc.write_subpage)
2429 subpage = offset || (data_len < mtd->writesize);
2430 else
2431 subpage = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002432
Marc Gonzalezc3a29852017-11-22 02:38:22 +09002433 if (nand_standard_page_accessors(&chip->ecc))
2434 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
William Juul52c07962007-10-31 13:53:06 +01002435
2436 if (unlikely(raw))
Heiko Schocherf5895d12014-06-24 10:10:04 +02002437 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood46e13102016-05-30 13:57:57 -05002438 oob_required, page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002439 else if (subpage)
2440 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Wood52ab7ce2016-05-30 13:57:58 -05002441 buf, oob_required, page);
William Juul52c07962007-10-31 13:53:06 +01002442 else
Scott Wood46e13102016-05-30 13:57:57 -05002443 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2444 page);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002445
2446 if (status < 0)
2447 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002448
Boris Brezillond4949322017-11-22 02:38:26 +09002449 if (nand_standard_page_accessors(&chip->ecc)) {
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002450 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
William Juul52c07962007-10-31 13:53:06 +01002451
Boris Brezillond4949322017-11-22 02:38:26 +09002452 status = chip->waitfunc(mtd, chip);
2453 if (status & NAND_STATUS_FAIL)
2454 return -EIO;
2455 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002456
William Juul52c07962007-10-31 13:53:06 +01002457 return 0;
2458}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002459
William Juul52c07962007-10-31 13:53:06 +01002460/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002461 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2462 * @mtd: MTD device structure
2463 * @oob: oob data buffer
2464 * @len: oob data write length
2465 * @ops: oob ops structure
William Juul52c07962007-10-31 13:53:06 +01002466 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00002467static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2468 struct mtd_oob_ops *ops)
William Juul52c07962007-10-31 13:53:06 +01002469{
Scott Wood17fed142016-05-30 13:57:56 -05002470 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002471
2472 /*
2473 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2474 * data from a previous OOB read.
2475 */
2476 memset(chip->oob_poi, 0xff, mtd->oobsize);
2477
Christian Hitz13fc0e22011-10-12 09:32:01 +02002478 switch (ops->mode) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002479
Sergey Lapin3a38a552013-01-14 03:46:50 +00002480 case MTD_OPS_PLACE_OOB:
2481 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002482 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2483 return oob + len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002484
Sergey Lapin3a38a552013-01-14 03:46:50 +00002485 case MTD_OPS_AUTO_OOB: {
William Juul52c07962007-10-31 13:53:06 +01002486 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2487 uint32_t boffs = 0, woffs = ops->ooboffs;
2488 size_t bytes = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002489
Christian Hitz13fc0e22011-10-12 09:32:01 +02002490 for (; free->length && len; free++, len -= bytes) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002491 /* Write request not from offset 0? */
William Juul52c07962007-10-31 13:53:06 +01002492 if (unlikely(woffs)) {
2493 if (woffs >= free->length) {
2494 woffs -= free->length;
2495 continue;
2496 }
2497 boffs = free->offset + woffs;
2498 bytes = min_t(size_t, len,
2499 (free->length - woffs));
2500 woffs = 0;
2501 } else {
2502 bytes = min_t(size_t, len, free->length);
2503 boffs = free->offset;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002504 }
William Juul52c07962007-10-31 13:53:06 +01002505 memcpy(chip->oob_poi + boffs, oob, bytes);
2506 oob += bytes;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002507 }
William Juul52c07962007-10-31 13:53:06 +01002508 return oob;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002509 }
William Juul52c07962007-10-31 13:53:06 +01002510 default:
2511 BUG();
2512 }
2513 return NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002514}
2515
Christian Hitzb8a6b372011-10-12 09:32:02 +02002516#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002517
2518/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002519 * nand_do_write_ops - [INTERN] NAND write with ECC
2520 * @mtd: MTD device structure
2521 * @to: offset to write to
2522 * @ops: oob operations description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002523 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002524 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002525 */
William Juul52c07962007-10-31 13:53:06 +01002526static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2527 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002528{
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002529 int chipnr, realpage, page, column;
Scott Wood17fed142016-05-30 13:57:56 -05002530 struct nand_chip *chip = mtd_to_nand(mtd);
William Juul52c07962007-10-31 13:53:06 +01002531 uint32_t writelen = ops->len;
Christian Hitzb8a6b372011-10-12 09:32:02 +02002532
2533 uint32_t oobwritelen = ops->ooblen;
Scott Wood52ab7ce2016-05-30 13:57:58 -05002534 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002535
William Juul52c07962007-10-31 13:53:06 +01002536 uint8_t *oob = ops->oobbuf;
2537 uint8_t *buf = ops->datbuf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002538 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002539 int oob_required = oob ? 1 : 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002540
William Juul52c07962007-10-31 13:53:06 +01002541 ops->retlen = 0;
2542 if (!writelen)
2543 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002544
Heiko Schocherf5895d12014-06-24 10:10:04 +02002545 /* Reject writes, which are not page aligned */
2546 if (NOTALIGNED(to)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002547 pr_notice("%s: attempt to write non page aligned data\n",
2548 __func__);
William Juul52c07962007-10-31 13:53:06 +01002549 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002550 }
2551
2552 column = to & (mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002553
William Juul52c07962007-10-31 13:53:06 +01002554 chipnr = (int)(to >> chip->chip_shift);
2555 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002556
2557 /* Check, if it is write protected */
William Juul52c07962007-10-31 13:53:06 +01002558 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002559 ret = -EIO;
2560 goto err_out;
William Juul52c07962007-10-31 13:53:06 +01002561 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002562
William Juul52c07962007-10-31 13:53:06 +01002563 realpage = (int)(to >> chip->page_shift);
2564 page = realpage & chip->pagemask;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002565
William Juul52c07962007-10-31 13:53:06 +01002566 /* Invalidate the page cache, when we write to the cached page */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002567 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2568 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juul52c07962007-10-31 13:53:06 +01002569 chip->pagebuf = -1;
2570
Christian Hitzb8a6b372011-10-12 09:32:02 +02002571 /* Don't allow multipage oob writes with offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002572 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2573 ret = -EINVAL;
2574 goto err_out;
2575 }
Christian Hitzb8a6b372011-10-12 09:32:02 +02002576
Christian Hitz13fc0e22011-10-12 09:32:01 +02002577 while (1) {
William Juul52c07962007-10-31 13:53:06 +01002578 int bytes = mtd->writesize;
William Juul52c07962007-10-31 13:53:06 +01002579 uint8_t *wbuf = buf;
Scott Wood3ea94ed2015-06-26 19:03:26 -05002580 int use_bufpoi;
Hector Palaciose4fcdbb2016-07-18 09:37:41 +02002581 int part_pagewr = (column || writelen < mtd->writesize);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002582
2583 if (part_pagewr)
2584 use_bufpoi = 1;
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09002585 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2586 use_bufpoi = !IS_ALIGNED((unsigned long)buf,
2587 chip->buf_align);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002588 else
2589 use_bufpoi = 0;
William Juul52c07962007-10-31 13:53:06 +01002590
Heiko Schocherf5895d12014-06-24 10:10:04 +02002591 WATCHDOG_RESET();
Scott Wood3ea94ed2015-06-26 19:03:26 -05002592 /* Partial page write?, or need to use bounce buffer */
2593 if (use_bufpoi) {
2594 pr_debug("%s: using write bounce buffer for buf@%p\n",
2595 __func__, buf);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002596 if (part_pagewr)
2597 bytes = min_t(int, bytes - column, writelen);
William Juul52c07962007-10-31 13:53:06 +01002598 chip->pagebuf = -1;
2599 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2600 memcpy(&chip->buffers->databuf[column], buf, bytes);
2601 wbuf = chip->buffers->databuf;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02002602 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002603
Christian Hitzb8a6b372011-10-12 09:32:02 +02002604 if (unlikely(oob)) {
2605 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapin3a38a552013-01-14 03:46:50 +00002606 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002607 oobwritelen -= len;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002608 } else {
2609 /* We still need to erase leftover OOB data */
2610 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitzb8a6b372011-10-12 09:32:02 +02002611 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002612 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +09002613 oob_required, page,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002614 (ops->mode == MTD_OPS_RAW));
William Juul52c07962007-10-31 13:53:06 +01002615 if (ret)
2616 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002617
William Juul52c07962007-10-31 13:53:06 +01002618 writelen -= bytes;
2619 if (!writelen)
2620 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002621
Heiko Schocherf5895d12014-06-24 10:10:04 +02002622 column = 0;
2623 buf += bytes;
2624 realpage++;
2625
2626 page = realpage & chip->pagemask;
2627 /* Check, if we cross a chip boundary */
2628 if (!page) {
2629 chipnr++;
2630 chip->select_chip(mtd, -1);
2631 chip->select_chip(mtd, chipnr);
2632 }
2633 }
2634
2635 ops->retlen = ops->len - writelen;
2636 if (unlikely(oob))
2637 ops->oobretlen = ops->ooblen;
2638
2639err_out:
2640 chip->select_chip(mtd, -1);
2641 return ret;
2642}
2643
2644/**
2645 * panic_nand_write - [MTD Interface] NAND write with ECC
2646 * @mtd: MTD device structure
2647 * @to: offset to write to
2648 * @len: number of bytes to write
2649 * @retlen: pointer to variable to store the number of written bytes
2650 * @buf: the data to write
2651 *
2652 * NAND write with ECC. Used when performing writes in interrupt context, this
2653 * may for example be called by mtdoops when writing an oops while in panic.
2654 */
2655static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2656 size_t *retlen, const uint8_t *buf)
2657{
Scott Wood17fed142016-05-30 13:57:56 -05002658 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002659 struct mtd_oob_ops ops;
2660 int ret;
2661
2662 /* Wait for the device to get ready */
2663 panic_nand_wait(mtd, chip, 400);
2664
2665 /* Grab the device */
2666 panic_nand_get_device(chip, mtd, FL_WRITING);
2667
Scott Wood3ea94ed2015-06-26 19:03:26 -05002668 memset(&ops, 0, sizeof(ops));
Heiko Schocherf5895d12014-06-24 10:10:04 +02002669 ops.len = len;
2670 ops.datbuf = (uint8_t *)buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002671 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +01002672
Heiko Schocherf5895d12014-06-24 10:10:04 +02002673 ret = nand_do_write_ops(mtd, to, &ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002674
Heiko Schocherf5895d12014-06-24 10:10:04 +02002675 *retlen = ops.retlen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002676 return ret;
2677}
2678
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002679/**
William Juul52c07962007-10-31 13:53:06 +01002680 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00002681 * @mtd: MTD device structure
2682 * @to: offset to write to
2683 * @len: number of bytes to write
2684 * @retlen: pointer to variable to store the number of written bytes
2685 * @buf: the data to write
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002686 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002687 * NAND write with ECC.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002688 */
William Juul52c07962007-10-31 13:53:06 +01002689static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2690 size_t *retlen, const uint8_t *buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002691{
Sergey Lapin3a38a552013-01-14 03:46:50 +00002692 struct mtd_oob_ops ops;
William Juul52c07962007-10-31 13:53:06 +01002693 int ret;
2694
Heiko Schocherf5895d12014-06-24 10:10:04 +02002695 nand_get_device(mtd, FL_WRITING);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002696 memset(&ops, 0, sizeof(ops));
Sergey Lapin3a38a552013-01-14 03:46:50 +00002697 ops.len = len;
2698 ops.datbuf = (uint8_t *)buf;
Sergey Lapin3a38a552013-01-14 03:46:50 +00002699 ops.mode = MTD_OPS_PLACE_OOB;
2700 ret = nand_do_write_ops(mtd, to, &ops);
2701 *retlen = ops.retlen;
William Juul52c07962007-10-31 13:53:06 +01002702 nand_release_device(mtd);
William Juul52c07962007-10-31 13:53:06 +01002703 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002704}
2705
2706/**
William Juul52c07962007-10-31 13:53:06 +01002707 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002708 * @mtd: MTD device structure
2709 * @to: offset to write to
2710 * @ops: oob operation description structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002711 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002712 * NAND write out-of-band.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002713 */
William Juul52c07962007-10-31 13:53:06 +01002714static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2715 struct mtd_oob_ops *ops)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002716{
William Juul52c07962007-10-31 13:53:06 +01002717 int chipnr, page, status, len;
Scott Wood17fed142016-05-30 13:57:56 -05002718 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002719
Heiko Schocherf5895d12014-06-24 10:10:04 +02002720 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz13fc0e22011-10-12 09:32:01 +02002721 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002722
Scott Wood52ab7ce2016-05-30 13:57:58 -05002723 len = mtd_oobavail(mtd, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002724
2725 /* Do not allow write past end of page */
William Juul52c07962007-10-31 13:53:06 +01002726 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002727 pr_debug("%s: attempt to write past end of page\n",
2728 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002729 return -EINVAL;
2730 }
2731
William Juul52c07962007-10-31 13:53:06 +01002732 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002733 pr_debug("%s: attempt to start write outside oob\n",
2734 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002735 return -EINVAL;
2736 }
2737
Christian Hitz13fc0e22011-10-12 09:32:01 +02002738 /* Do not allow write past end of device */
William Juul52c07962007-10-31 13:53:06 +01002739 if (unlikely(to >= mtd->size ||
2740 ops->ooboffs + ops->ooblen >
2741 ((mtd->size >> chip->page_shift) -
2742 (to >> chip->page_shift)) * len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002743 pr_debug("%s: attempt to write beyond end of device\n",
2744 __func__);
William Juul52c07962007-10-31 13:53:06 +01002745 return -EINVAL;
2746 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002747
William Juul52c07962007-10-31 13:53:06 +01002748 chipnr = (int)(to >> chip->chip_shift);
William Juul52c07962007-10-31 13:53:06 +01002749
2750 /*
2751 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2752 * of my DiskOnChip 2000 test units) will clear the whole data page too
2753 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2754 * it in the doc2000 driver in August 1999. dwmw2.
2755 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09002756 nand_reset(chip, chipnr);
2757
2758 chip->select_chip(mtd, chipnr);
2759
2760 /* Shift to get page */
2761 page = (int)(to >> chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002762
2763 /* Check, if it is write protected */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002764 if (nand_check_wp(mtd)) {
2765 chip->select_chip(mtd, -1);
William Juul52c07962007-10-31 13:53:06 +01002766 return -EROFS;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002767 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002768
William Juul52c07962007-10-31 13:53:06 +01002769 /* Invalidate the page cache, if we write to the cached page */
2770 if (page == chip->pagebuf)
2771 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002772
Sergey Lapin3a38a552013-01-14 03:46:50 +00002773 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2774
2775 if (ops->mode == MTD_OPS_RAW)
2776 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2777 else
2778 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002779
Heiko Schocherf5895d12014-06-24 10:10:04 +02002780 chip->select_chip(mtd, -1);
2781
William Juul52c07962007-10-31 13:53:06 +01002782 if (status)
2783 return status;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002784
William Juul52c07962007-10-31 13:53:06 +01002785 ops->oobretlen = ops->ooblen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002786
William Juul52c07962007-10-31 13:53:06 +01002787 return 0;
2788}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002789
William Juul52c07962007-10-31 13:53:06 +01002790/**
2791 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapin3a38a552013-01-14 03:46:50 +00002792 * @mtd: MTD device structure
2793 * @to: offset to write to
2794 * @ops: oob operation description structure
William Juul52c07962007-10-31 13:53:06 +01002795 */
2796static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2797 struct mtd_oob_ops *ops)
2798{
William Juul52c07962007-10-31 13:53:06 +01002799 int ret = -ENOTSUPP;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002800
William Juul52c07962007-10-31 13:53:06 +01002801 ops->retlen = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002802
William Juul52c07962007-10-31 13:53:06 +01002803 /* Do not allow writes past end of device */
2804 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002805 pr_debug("%s: attempt to write beyond end of device\n",
2806 __func__);
William Juul52c07962007-10-31 13:53:06 +01002807 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002808 }
William Juul52c07962007-10-31 13:53:06 +01002809
Heiko Schocherf5895d12014-06-24 10:10:04 +02002810 nand_get_device(mtd, FL_WRITING);
William Juul52c07962007-10-31 13:53:06 +01002811
Christian Hitz13fc0e22011-10-12 09:32:01 +02002812 switch (ops->mode) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002813 case MTD_OPS_PLACE_OOB:
2814 case MTD_OPS_AUTO_OOB:
2815 case MTD_OPS_RAW:
William Juul52c07962007-10-31 13:53:06 +01002816 break;
2817
2818 default:
2819 goto out;
2820 }
2821
2822 if (!ops->datbuf)
2823 ret = nand_do_write_oob(mtd, to, ops);
2824 else
2825 ret = nand_do_write_ops(mtd, to, ops);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002826
Christian Hitz13fc0e22011-10-12 09:32:01 +02002827out:
William Juul52c07962007-10-31 13:53:06 +01002828 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002829 return ret;
2830}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002831
2832/**
Scott Wood3ea94ed2015-06-26 19:03:26 -05002833 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapin3a38a552013-01-14 03:46:50 +00002834 * @mtd: MTD device structure
2835 * @page: the page address of the block which will be erased
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002836 *
Scott Wood3ea94ed2015-06-26 19:03:26 -05002837 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002838 */
Scott Wood3ea94ed2015-06-26 19:03:26 -05002839static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002840{
Scott Wood17fed142016-05-30 13:57:56 -05002841 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002842 /* Send commands to erase a block */
William Juul52c07962007-10-31 13:53:06 +01002843 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2844 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Scott Wood3ea94ed2015-06-26 19:03:26 -05002845
2846 return chip->waitfunc(mtd, chip);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002847}
2848
2849/**
2850 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00002851 * @mtd: MTD device structure
2852 * @instr: erase instruction
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002853 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002854 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002855 */
William Juul52c07962007-10-31 13:53:06 +01002856static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002857{
William Juul52c07962007-10-31 13:53:06 +01002858 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002859}
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002860
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002861/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00002862 * nand_erase_nand - [INTERN] erase block(s)
2863 * @mtd: MTD device structure
2864 * @instr: erase instruction
2865 * @allowbbt: allow erasing the bbt area
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002866 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002867 * Erase one ore more blocks.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002868 */
William Juul52c07962007-10-31 13:53:06 +01002869int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2870 int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002871{
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002872 int page, status, pages_per_block, ret, chipnr;
Scott Wood17fed142016-05-30 13:57:56 -05002873 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04002874 loff_t len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002875
Heiko Schocherf5895d12014-06-24 10:10:04 +02002876 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2877 __func__, (unsigned long long)instr->addr,
2878 (unsigned long long)instr->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002879
Christian Hitzb8a6b372011-10-12 09:32:02 +02002880 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002881 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002882
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002883 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002884 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002885
2886 /* Shift to get first page */
William Juul52c07962007-10-31 13:53:06 +01002887 page = (int)(instr->addr >> chip->page_shift);
2888 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002889
2890 /* Calculate pages in each block */
William Juul52c07962007-10-31 13:53:06 +01002891 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juulb76ec382007-11-08 10:39:53 +01002892
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002893 /* Select the NAND device */
William Juul52c07962007-10-31 13:53:06 +01002894 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002895
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002896 /* Check, if it is write protected */
2897 if (nand_check_wp(mtd)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002898 pr_debug("%s: device is write protected!\n",
2899 __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002900 instr->state = MTD_ERASE_FAILED;
2901 goto erase_exit;
2902 }
2903
2904 /* Loop through the pages */
2905 len = instr->len;
2906
2907 instr->state = MTD_ERASING;
2908
2909 while (len) {
Scott Woodea95b642011-02-02 18:15:57 -06002910 WATCHDOG_RESET();
Heiko Schocherf5895d12014-06-24 10:10:04 +02002911
Sergey Lapin3a38a552013-01-14 03:46:50 +00002912 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamadaf5a19022014-12-16 15:36:33 +09002913 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Wood52ab7ce2016-05-30 13:57:58 -05002914 chip->page_shift, allowbbt)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00002915 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002916 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002917 instr->state = MTD_ERASE_FAILED;
2918 goto erase_exit;
2919 }
William Juul52c07962007-10-31 13:53:06 +01002920
2921 /*
2922 * Invalidate the page cache, if we erase the block which
Sergey Lapin3a38a552013-01-14 03:46:50 +00002923 * contains the current cached page.
William Juul52c07962007-10-31 13:53:06 +01002924 */
2925 if (page <= chip->pagebuf && chip->pagebuf <
2926 (page + pages_per_block))
2927 chip->pagebuf = -1;
2928
Scott Wood3ea94ed2015-06-26 19:03:26 -05002929 status = chip->erase(mtd, page & chip->pagemask);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002930
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002931 /* See if block erase succeeded */
William Juul52c07962007-10-31 13:53:06 +01002932 if (status & NAND_STATUS_FAIL) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002933 pr_debug("%s: failed erase, page 0x%08x\n",
2934 __func__, page);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002935 instr->state = MTD_ERASE_FAILED;
Christian Hitz13fc0e22011-10-12 09:32:01 +02002936 instr->fail_addr =
2937 ((loff_t)page << chip->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002938 goto erase_exit;
2939 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02002940
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002941 /* Increment page address and decrement length */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002942 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002943 page += pages_per_block;
2944
2945 /* Check, if we cross a chip boundary */
William Juul52c07962007-10-31 13:53:06 +01002946 if (len && !(page & chip->pagemask)) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002947 chipnr++;
William Juul52c07962007-10-31 13:53:06 +01002948 chip->select_chip(mtd, -1);
2949 chip->select_chip(mtd, chipnr);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002950 }
2951 }
2952 instr->state = MTD_ERASE_DONE;
2953
Christian Hitz13fc0e22011-10-12 09:32:01 +02002954erase_exit:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002955
2956 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002957
2958 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002959 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002960 nand_release_device(mtd);
2961
Scott Wood3628f002008-10-24 16:20:43 -05002962 /* Do call back function */
2963 if (!ret)
2964 mtd_erase_callback(instr);
2965
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002966 /* Return more or less happy */
2967 return ret;
2968}
2969
2970/**
2971 * nand_sync - [MTD Interface] sync
Sergey Lapin3a38a552013-01-14 03:46:50 +00002972 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002973 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00002974 * Sync is actually a wait for chip ready function.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002975 */
William Juul52c07962007-10-31 13:53:06 +01002976static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002977{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002978 pr_debug("%s: called\n", __func__);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002979
2980 /* Grab the lock and see if the device is available */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002981 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002982 /* Release it and go back */
William Juul52c07962007-10-31 13:53:06 +01002983 nand_release_device(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002984}
2985
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002986/**
William Juul52c07962007-10-31 13:53:06 +01002987 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00002988 * @mtd: MTD device structure
2989 * @offs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002990 */
William Juul52c07962007-10-31 13:53:06 +01002991static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002992{
Scott Wood52ab7ce2016-05-30 13:57:58 -05002993 struct nand_chip *chip = mtd_to_nand(mtd);
2994 int chipnr = (int)(offs >> chip->chip_shift);
2995 int ret;
2996
2997 /* Select the NAND device */
2998 nand_get_device(mtd, FL_READING);
2999 chip->select_chip(mtd, chipnr);
3000
3001 ret = nand_block_checkbad(mtd, offs, 0);
3002
3003 chip->select_chip(mtd, -1);
3004 nand_release_device(mtd);
3005
3006 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003007}
3008
3009/**
William Juul52c07962007-10-31 13:53:06 +01003010 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00003011 * @mtd: MTD device structure
3012 * @ofs: offset relative to mtd start
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003013 */
William Juul52c07962007-10-31 13:53:06 +01003014static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003015{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003016 int ret;
3017
Christian Hitzb8a6b372011-10-12 09:32:02 +02003018 ret = nand_block_isbad(mtd, ofs);
3019 if (ret) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003020 /* If it was bad already, return success and do nothing */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003021 if (ret > 0)
3022 return 0;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003023 return ret;
3024 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003025
Heiko Schocherf5895d12014-06-24 10:10:04 +02003026 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003027}
3028
Heiko Schocherf5895d12014-06-24 10:10:04 +02003029/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00003030 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3031 * @mtd: MTD device structure
3032 * @chip: nand chip info structure
3033 * @addr: feature address.
3034 * @subfeature_param: the subfeature parameters, a four bytes array.
3035 */
3036static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3037 int addr, uint8_t *subfeature_param)
3038{
3039 int status;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003040 int i;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003041
Heiko Schocherf5895d12014-06-24 10:10:04 +02003042#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3043 if (!chip->onfi_version ||
3044 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3045 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapin3a38a552013-01-14 03:46:50 +00003046 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003047#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003048
3049 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003050 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3051 chip->write_byte(mtd, subfeature_param[i]);
3052
Sergey Lapin3a38a552013-01-14 03:46:50 +00003053 status = chip->waitfunc(mtd, chip);
3054 if (status & NAND_STATUS_FAIL)
3055 return -EIO;
3056 return 0;
3057}
3058
3059/**
3060 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3061 * @mtd: MTD device structure
3062 * @chip: nand chip info structure
3063 * @addr: feature address.
3064 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juul52c07962007-10-31 13:53:06 +01003065 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003066static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3067 int addr, uint8_t *subfeature_param)
3068{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003069 int i;
3070
3071#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3072 if (!chip->onfi_version ||
3073 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3074 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapin3a38a552013-01-14 03:46:50 +00003075 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003076#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +00003077
Sergey Lapin3a38a552013-01-14 03:46:50 +00003078 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003079 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3080 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003081 return 0;
3082}
Heiko Schocherf5895d12014-06-24 10:10:04 +02003083
Sergey Lapin3a38a552013-01-14 03:46:50 +00003084/* Set default functions */
William Juul52c07962007-10-31 13:53:06 +01003085static void nand_set_defaults(struct nand_chip *chip, int busw)
3086{
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003087 /* check for proper chip_delay setup, set 20us if not */
William Juul52c07962007-10-31 13:53:06 +01003088 if (!chip->chip_delay)
3089 chip->chip_delay = 20;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003090
3091 /* check, if a user supplied command function given */
William Juul52c07962007-10-31 13:53:06 +01003092 if (chip->cmdfunc == NULL)
3093 chip->cmdfunc = nand_command;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003094
3095 /* check, if a user supplied wait function given */
William Juul52c07962007-10-31 13:53:06 +01003096 if (chip->waitfunc == NULL)
3097 chip->waitfunc = nand_wait;
3098
3099 if (!chip->select_chip)
3100 chip->select_chip = nand_select_chip;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003101
3102 /* set for ONFI nand */
3103 if (!chip->onfi_set_features)
3104 chip->onfi_set_features = nand_onfi_set_features;
3105 if (!chip->onfi_get_features)
3106 chip->onfi_get_features = nand_onfi_get_features;
3107
3108 /* If called twice, pointers that depend on busw may need to be reset */
3109 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juul52c07962007-10-31 13:53:06 +01003110 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3111 if (!chip->read_word)
3112 chip->read_word = nand_read_word;
3113 if (!chip->block_bad)
3114 chip->block_bad = nand_block_bad;
3115 if (!chip->block_markbad)
3116 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003117 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juul52c07962007-10-31 13:53:06 +01003118 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003119 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3120 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3121 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juul52c07962007-10-31 13:53:06 +01003122 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juul52c07962007-10-31 13:53:06 +01003123 if (!chip->scan_bbt)
3124 chip->scan_bbt = nand_default_bbt;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003125
3126 if (!chip->controller) {
William Juul52c07962007-10-31 13:53:06 +01003127 chip->controller = &chip->hwcontrol;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003128 spin_lock_init(&chip->controller->lock);
3129 init_waitqueue_head(&chip->controller->wq);
3130 }
3131
Masahiro Yamadab9c07b62017-11-22 02:38:27 +09003132 if (!chip->buf_align)
3133 chip->buf_align = 1;
William Juul52c07962007-10-31 13:53:06 +01003134}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003135
Sergey Lapin3a38a552013-01-14 03:46:50 +00003136/* Sanitize ONFI strings so we can safely print them */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003137static void sanitize_string(char *s, size_t len)
3138{
3139 ssize_t i;
3140
Sergey Lapin3a38a552013-01-14 03:46:50 +00003141 /* Null terminate */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003142 s[len - 1] = 0;
3143
Sergey Lapin3a38a552013-01-14 03:46:50 +00003144 /* Remove non printable chars */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003145 for (i = 0; i < len - 1; i++) {
3146 if (s[i] < ' ' || s[i] > 127)
3147 s[i] = '?';
3148 }
3149
Sergey Lapin3a38a552013-01-14 03:46:50 +00003150 /* Remove trailing spaces */
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003151 strim(s);
3152}
3153
Florian Fainellic98a9352011-02-25 00:01:34 +00003154static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3155{
3156 int i;
Florian Fainellic98a9352011-02-25 00:01:34 +00003157 while (len--) {
3158 crc ^= *p++ << 8;
3159 for (i = 0; i < 8; i++)
3160 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3161 }
3162
3163 return crc;
3164}
3165
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003166#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherf5895d12014-06-24 10:10:04 +02003167/* Parse the Extended Parameter Page. */
3168static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3169 struct nand_chip *chip, struct nand_onfi_params *p)
3170{
3171 struct onfi_ext_param_page *ep;
3172 struct onfi_ext_section *s;
3173 struct onfi_ext_ecc_info *ecc;
3174 uint8_t *cursor;
3175 int ret = -EINVAL;
3176 int len;
3177 int i;
3178
3179 len = le16_to_cpu(p->ext_param_page_length) * 16;
3180 ep = kmalloc(len, GFP_KERNEL);
3181 if (!ep)
3182 return -ENOMEM;
3183
3184 /* Send our own NAND_CMD_PARAM. */
3185 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3186
3187 /* Use the Change Read Column command to skip the ONFI param pages. */
3188 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3189 sizeof(*p) * p->num_of_param_pages , -1);
3190
3191 /* Read out the Extended Parameter Page. */
3192 chip->read_buf(mtd, (uint8_t *)ep, len);
3193 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3194 != le16_to_cpu(ep->crc))) {
3195 pr_debug("fail in the CRC.\n");
3196 goto ext_out;
3197 }
3198
3199 /*
3200 * Check the signature.
3201 * Do not strictly follow the ONFI spec, maybe changed in future.
3202 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003203 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003204 pr_debug("The signature is invalid.\n");
3205 goto ext_out;
3206 }
3207
3208 /* find the ECC section. */
3209 cursor = (uint8_t *)(ep + 1);
3210 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3211 s = ep->sections + i;
3212 if (s->type == ONFI_SECTION_TYPE_2)
3213 break;
3214 cursor += s->length * 16;
3215 }
3216 if (i == ONFI_EXT_SECTION_MAX) {
3217 pr_debug("We can not find the ECC section.\n");
3218 goto ext_out;
3219 }
3220
3221 /* get the info we want. */
3222 ecc = (struct onfi_ext_ecc_info *)cursor;
3223
3224 if (!ecc->codeword_size) {
3225 pr_debug("Invalid codeword size\n");
3226 goto ext_out;
3227 }
3228
3229 chip->ecc_strength_ds = ecc->ecc_bits;
3230 chip->ecc_step_ds = 1 << ecc->codeword_size;
3231 ret = 0;
3232
3233ext_out:
3234 kfree(ep);
3235 return ret;
3236}
3237
3238static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3239{
Scott Wood17fed142016-05-30 13:57:56 -05003240 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003241 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3242
3243 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3244 feature);
3245}
3246
3247/*
3248 * Configure chip properties from Micron vendor-specific ONFI table
3249 */
3250static void nand_onfi_detect_micron(struct nand_chip *chip,
3251 struct nand_onfi_params *p)
3252{
3253 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3254
3255 if (le16_to_cpu(p->vendor_revision) < 1)
3256 return;
3257
3258 chip->read_retries = micron->read_retry_options;
3259 chip->setup_read_retry = nand_setup_read_retry_micron;
3260}
3261
Florian Fainellic98a9352011-02-25 00:01:34 +00003262/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003263 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainellic98a9352011-02-25 00:01:34 +00003264 */
Christian Hitz13fc0e22011-10-12 09:32:01 +02003265static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003266 int *busw)
3267{
3268 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisf3832302014-05-06 00:46:16 +05303269 int i, j;
Florian Fainellic98a9352011-02-25 00:01:34 +00003270 int val;
3271
Sergey Lapin3a38a552013-01-14 03:46:50 +00003272 /* Try ONFI for unknown chip or LP */
Florian Fainellic98a9352011-02-25 00:01:34 +00003273 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3274 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3275 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3276 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003277
Florian Fainellic98a9352011-02-25 00:01:34 +00003278 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3279 for (i = 0; i < 3; i++) {
Brian Norrisf3832302014-05-06 00:46:16 +05303280 for (j = 0; j < sizeof(*p); j++)
3281 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003282 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz13fc0e22011-10-12 09:32:01 +02003283 le16_to_cpu(p->crc)) {
Florian Fainellic98a9352011-02-25 00:01:34 +00003284 break;
3285 }
3286 }
3287
Heiko Schocherf5895d12014-06-24 10:10:04 +02003288 if (i == 3) {
3289 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainellic98a9352011-02-25 00:01:34 +00003290 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003291 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003292
Sergey Lapin3a38a552013-01-14 03:46:50 +00003293 /* Check version */
Florian Fainellic98a9352011-02-25 00:01:34 +00003294 val = le16_to_cpu(p->revision);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003295 if (val & (1 << 5))
3296 chip->onfi_version = 23;
3297 else if (val & (1 << 4))
Florian Fainellic98a9352011-02-25 00:01:34 +00003298 chip->onfi_version = 22;
3299 else if (val & (1 << 3))
3300 chip->onfi_version = 21;
3301 else if (val & (1 << 2))
3302 chip->onfi_version = 20;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003303 else if (val & (1 << 1))
Florian Fainellic98a9352011-02-25 00:01:34 +00003304 chip->onfi_version = 10;
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003305
3306 if (!chip->onfi_version) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003307 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelli3eb4fc62011-04-03 18:23:56 +02003308 return 0;
3309 }
Florian Fainellic98a9352011-02-25 00:01:34 +00003310
Christian Hitz6f1c9e02011-10-12 09:32:05 +02003311 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3312 sanitize_string(p->model, sizeof(p->model));
Florian Fainellic98a9352011-02-25 00:01:34 +00003313 if (!mtd->name)
3314 mtd->name = p->model;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003315
Florian Fainellic98a9352011-02-25 00:01:34 +00003316 mtd->writesize = le32_to_cpu(p->byte_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003317
3318 /*
3319 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3320 * (don't ask me who thought of this...). MTD assumes that these
3321 * dimensions will be power-of-2, so just truncate the remaining area.
3322 */
3323 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3324 mtd->erasesize *= mtd->writesize;
3325
Florian Fainellic98a9352011-02-25 00:01:34 +00003326 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003327
3328 /* See erasesize comment */
3329 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
Matthieu CASTETb20b6f22012-03-19 15:35:25 +01003330 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003331 chip->bits_per_cell = p->bits_per_cell;
3332
3333 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
Florian Fainellic98a9352011-02-25 00:01:34 +00003334 *busw = NAND_BUSWIDTH_16;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003335 else
3336 *busw = 0;
3337
3338 if (p->ecc_bits != 0xff) {
3339 chip->ecc_strength_ds = p->ecc_bits;
3340 chip->ecc_step_ds = 512;
3341 } else if (chip->onfi_version >= 21 &&
3342 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3343
3344 /*
3345 * The nand_flash_detect_ext_param_page() uses the
3346 * Change Read Column command which maybe not supported
3347 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3348 * now. We do not replace user supplied command function.
3349 */
3350 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3351 chip->cmdfunc = nand_command_lp;
3352
3353 /* The Extended Parameter Page is supported since ONFI 2.1. */
3354 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3355 pr_warn("Failed to detect ONFI extended param page\n");
3356 } else {
3357 pr_warn("Could not retrieve ONFI ECC requirements\n");
3358 }
3359
3360 if (p->jedec_id == NAND_MFR_MICRON)
3361 nand_onfi_detect_micron(chip, p);
Florian Fainellic98a9352011-02-25 00:01:34 +00003362
3363 return 1;
3364}
3365#else
Heiko Schocherf5895d12014-06-24 10:10:04 +02003366static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003367 int *busw)
3368{
3369 return 0;
3370}
3371#endif
3372
William Juul52c07962007-10-31 13:53:06 +01003373/*
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003374 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3375 */
3376static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3377 int *busw)
3378{
3379 struct nand_jedec_params *p = &chip->jedec_params;
3380 struct jedec_ecc_info *ecc;
3381 int val;
3382 int i, j;
3383
3384 /* Try JEDEC for unknown chip or LP */
3385 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3386 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3387 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3388 chip->read_byte(mtd) != 'C')
3389 return 0;
3390
3391 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3392 for (i = 0; i < 3; i++) {
3393 for (j = 0; j < sizeof(*p); j++)
3394 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3395
3396 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3397 le16_to_cpu(p->crc))
3398 break;
3399 }
3400
3401 if (i == 3) {
3402 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3403 return 0;
3404 }
3405
3406 /* Check version */
3407 val = le16_to_cpu(p->revision);
3408 if (val & (1 << 2))
3409 chip->jedec_version = 10;
3410 else if (val & (1 << 1))
3411 chip->jedec_version = 1; /* vendor specific version */
3412
3413 if (!chip->jedec_version) {
3414 pr_info("unsupported JEDEC version: %d\n", val);
3415 return 0;
3416 }
3417
3418 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3419 sanitize_string(p->model, sizeof(p->model));
3420 if (!mtd->name)
3421 mtd->name = p->model;
3422
3423 mtd->writesize = le32_to_cpu(p->byte_per_page);
3424
3425 /* Please reference to the comment for nand_flash_detect_onfi. */
3426 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3427 mtd->erasesize *= mtd->writesize;
3428
3429 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3430
3431 /* Please reference to the comment for nand_flash_detect_onfi. */
3432 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3433 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3434 chip->bits_per_cell = p->bits_per_cell;
3435
3436 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3437 *busw = NAND_BUSWIDTH_16;
3438 else
3439 *busw = 0;
3440
3441 /* ECC info */
3442 ecc = &p->ecc_info[0];
3443
3444 if (ecc->codeword_size >= 9) {
3445 chip->ecc_strength_ds = ecc->ecc_bits;
3446 chip->ecc_step_ds = 1 << ecc->codeword_size;
3447 } else {
3448 pr_warn("Invalid codeword size\n");
3449 }
3450
3451 return 1;
3452}
3453
3454/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003455 * nand_id_has_period - Check if an ID string has a given wraparound period
3456 * @id_data: the ID string
3457 * @arrlen: the length of the @id_data array
3458 * @period: the period of repitition
3459 *
3460 * Check if an ID string is repeated within a given sequence of bytes at
3461 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherf5895d12014-06-24 10:10:04 +02003462 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapin3a38a552013-01-14 03:46:50 +00003463 * if the repetition has a period of @period; otherwise, returns zero.
3464 */
3465static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3466{
3467 int i, j;
3468 for (i = 0; i < period; i++)
3469 for (j = i + period; j < arrlen; j += period)
3470 if (id_data[i] != id_data[j])
3471 return 0;
3472 return 1;
3473}
3474
3475/*
3476 * nand_id_len - Get the length of an ID string returned by CMD_READID
3477 * @id_data: the ID string
3478 * @arrlen: the length of the @id_data array
3479
3480 * Returns the length of the ID string, according to known wraparound/trailing
3481 * zero patterns. If no pattern exists, returns the length of the array.
3482 */
3483static int nand_id_len(u8 *id_data, int arrlen)
3484{
3485 int last_nonzero, period;
3486
3487 /* Find last non-zero byte */
3488 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3489 if (id_data[last_nonzero])
3490 break;
3491
3492 /* All zeros */
3493 if (last_nonzero < 0)
3494 return 0;
3495
3496 /* Calculate wraparound period */
3497 for (period = 1; period < arrlen; period++)
3498 if (nand_id_has_period(id_data, arrlen, period))
3499 break;
3500
3501 /* There's a repeated pattern */
3502 if (period < arrlen)
3503 return period;
3504
3505 /* There are trailing zeros */
3506 if (last_nonzero < arrlen - 1)
3507 return last_nonzero + 1;
3508
3509 /* No pattern detected */
3510 return arrlen;
3511}
3512
Heiko Schocherf5895d12014-06-24 10:10:04 +02003513/* Extract the bits of per cell from the 3rd byte of the extended ID */
3514static int nand_get_bits_per_cell(u8 cellinfo)
3515{
3516 int bits;
3517
3518 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3519 bits >>= NAND_CI_CELLTYPE_SHIFT;
3520 return bits + 1;
3521}
3522
Sergey Lapin3a38a552013-01-14 03:46:50 +00003523/*
3524 * Many new NAND share similar device ID codes, which represent the size of the
3525 * chip. The rest of the parameters must be decoded according to generic or
3526 * manufacturer-specific "extended ID" decoding patterns.
3527 */
3528static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3529 u8 id_data[8], int *busw)
3530{
3531 int extid, id_len;
3532 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003533 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapin3a38a552013-01-14 03:46:50 +00003534 /* The 4th id byte is the important one */
3535 extid = id_data[3];
3536
3537 id_len = nand_id_len(id_data, 8);
3538
3539 /*
3540 * Field definitions are in the following datasheets:
3541 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3542 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3543 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3544 *
3545 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3546 * ID to decide what to do.
3547 */
3548 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003549 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003550 /* Calc pagesize */
3551 mtd->writesize = 2048 << (extid & 0x03);
3552 extid >>= 2;
3553 /* Calc oobsize */
3554 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3555 case 1:
3556 mtd->oobsize = 128;
3557 break;
3558 case 2:
3559 mtd->oobsize = 218;
3560 break;
3561 case 3:
3562 mtd->oobsize = 400;
3563 break;
3564 case 4:
3565 mtd->oobsize = 436;
3566 break;
3567 case 5:
3568 mtd->oobsize = 512;
3569 break;
3570 case 6:
Sergey Lapin3a38a552013-01-14 03:46:50 +00003571 mtd->oobsize = 640;
3572 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003573 case 7:
3574 default: /* Other cases are "reserved" (unknown) */
3575 mtd->oobsize = 1024;
3576 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003577 }
3578 extid >>= 2;
3579 /* Calc blocksize */
3580 mtd->erasesize = (128 * 1024) <<
3581 (((extid >> 1) & 0x04) | (extid & 0x03));
3582 *busw = 0;
3583 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherf5895d12014-06-24 10:10:04 +02003584 !nand_is_slc(chip)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003585 unsigned int tmp;
3586
3587 /* Calc pagesize */
3588 mtd->writesize = 2048 << (extid & 0x03);
3589 extid >>= 2;
3590 /* Calc oobsize */
3591 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3592 case 0:
3593 mtd->oobsize = 128;
3594 break;
3595 case 1:
3596 mtd->oobsize = 224;
3597 break;
3598 case 2:
3599 mtd->oobsize = 448;
3600 break;
3601 case 3:
3602 mtd->oobsize = 64;
3603 break;
3604 case 4:
3605 mtd->oobsize = 32;
3606 break;
3607 case 5:
3608 mtd->oobsize = 16;
3609 break;
3610 default:
3611 mtd->oobsize = 640;
3612 break;
3613 }
3614 extid >>= 2;
3615 /* Calc blocksize */
3616 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3617 if (tmp < 0x03)
3618 mtd->erasesize = (128 * 1024) << tmp;
3619 else if (tmp == 0x03)
3620 mtd->erasesize = 768 * 1024;
3621 else
3622 mtd->erasesize = (64 * 1024) << tmp;
3623 *busw = 0;
3624 } else {
3625 /* Calc pagesize */
3626 mtd->writesize = 1024 << (extid & 0x03);
3627 extid >>= 2;
3628 /* Calc oobsize */
3629 mtd->oobsize = (8 << (extid & 0x01)) *
3630 (mtd->writesize >> 9);
3631 extid >>= 2;
3632 /* Calc blocksize. Blocksize is multiples of 64KiB */
3633 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3634 extid >>= 2;
3635 /* Get buswidth information */
3636 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003637
3638 /*
3639 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3640 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3641 * follows:
3642 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3643 * 110b -> 24nm
3644 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3645 */
3646 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3647 nand_is_slc(chip) &&
3648 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3649 !(id_data[4] & 0x80) /* !BENAND */) {
3650 mtd->oobsize = 32 * mtd->writesize >> 9;
3651 }
3652
Sergey Lapin3a38a552013-01-14 03:46:50 +00003653 }
3654}
3655
Heiko Schocherf5895d12014-06-24 10:10:04 +02003656/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003657 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3658 * decodes a matching ID table entry and assigns the MTD size parameters for
3659 * the chip.
3660 */
3661static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003662 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapin3a38a552013-01-14 03:46:50 +00003663 int *busw)
3664{
3665 int maf_id = id_data[0];
3666
3667 mtd->erasesize = type->erasesize;
3668 mtd->writesize = type->pagesize;
3669 mtd->oobsize = mtd->writesize / 32;
3670 *busw = type->options & NAND_BUSWIDTH_16;
3671
Heiko Schocherf5895d12014-06-24 10:10:04 +02003672 /* All legacy ID NAND are small-page, SLC */
3673 chip->bits_per_cell = 1;
3674
Sergey Lapin3a38a552013-01-14 03:46:50 +00003675 /*
3676 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3677 * some Spansion chips have erasesize that conflicts with size
3678 * listed in nand_ids table.
3679 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3680 */
3681 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3682 && id_data[6] == 0x00 && id_data[7] == 0x00
3683 && mtd->writesize == 512) {
3684 mtd->erasesize = 128 * 1024;
3685 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3686 }
3687}
3688
Heiko Schocherf5895d12014-06-24 10:10:04 +02003689/*
Sergey Lapin3a38a552013-01-14 03:46:50 +00003690 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3691 * heuristic patterns using various detected parameters (e.g., manufacturer,
3692 * page size, cell-type information).
3693 */
3694static void nand_decode_bbm_options(struct mtd_info *mtd,
3695 struct nand_chip *chip, u8 id_data[8])
3696{
3697 int maf_id = id_data[0];
3698
3699 /* Set the bad block position */
3700 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3701 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3702 else
3703 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3704
3705 /*
3706 * Bad block marker is stored in the last page of each block on Samsung
3707 * and Hynix MLC devices; stored in first two pages of each block on
3708 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3709 * AMD/Spansion, and Macronix. All others scan only the first page.
3710 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003711 if (!nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003712 (maf_id == NAND_MFR_SAMSUNG ||
3713 maf_id == NAND_MFR_HYNIX))
3714 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003715 else if ((nand_is_slc(chip) &&
Sergey Lapin3a38a552013-01-14 03:46:50 +00003716 (maf_id == NAND_MFR_SAMSUNG ||
3717 maf_id == NAND_MFR_HYNIX ||
3718 maf_id == NAND_MFR_TOSHIBA ||
3719 maf_id == NAND_MFR_AMD ||
3720 maf_id == NAND_MFR_MACRONIX)) ||
3721 (mtd->writesize == 2048 &&
3722 maf_id == NAND_MFR_MICRON))
3723 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3724}
3725
Heiko Schocherf5895d12014-06-24 10:10:04 +02003726static inline bool is_full_id_nand(struct nand_flash_dev *type)
3727{
3728 return type->id_len;
3729}
3730
3731static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3732 struct nand_flash_dev *type, u8 *id_data, int *busw)
3733{
Heiko Schocherf5895d12014-06-24 10:10:04 +02003734 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003735 mtd->writesize = type->pagesize;
3736 mtd->erasesize = type->erasesize;
3737 mtd->oobsize = type->oobsize;
3738
3739 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3740 chip->chipsize = (uint64_t)type->chipsize << 20;
3741 chip->options |= type->options;
3742 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3743 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Wood3ea94ed2015-06-26 19:03:26 -05003744 chip->onfi_timing_mode_default =
3745 type->onfi_timing_mode_default;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003746
3747 *busw = type->options & NAND_BUSWIDTH_16;
3748
3749 if (!mtd->name)
3750 mtd->name = type->name;
3751
3752 return true;
3753 }
3754 return false;
3755}
3756
Sergey Lapin3a38a552013-01-14 03:46:50 +00003757/*
3758 * Get the flash and manufacturer id and lookup if the type is supported.
William Juul52c07962007-10-31 13:53:06 +01003759 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02003760static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
William Juul52c07962007-10-31 13:53:06 +01003761 struct nand_chip *chip,
Florian Fainellic98a9352011-02-25 00:01:34 +00003762 int *maf_id, int *dev_id,
Heiko Schocherf5895d12014-06-24 10:10:04 +02003763 struct nand_flash_dev *type)
William Juul52c07962007-10-31 13:53:06 +01003764{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003765 int busw;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003766 int i, maf_idx;
3767 u8 id_data[8];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003768
Karl Beldanb6322fc2008-09-15 16:08:03 +02003769 /*
3770 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapin3a38a552013-01-14 03:46:50 +00003771 * after power-up.
Karl Beldanb6322fc2008-09-15 16:08:03 +02003772 */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09003773 nand_reset(chip, 0);
3774
3775 /* Select the device */
3776 chip->select_chip(mtd, 0);
Karl Beldanb6322fc2008-09-15 16:08:03 +02003777
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003778 /* Send the command for reading device ID */
William Juul52c07962007-10-31 13:53:06 +01003779 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003780
3781 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01003782 *maf_id = chip->read_byte(mtd);
Florian Fainellic98a9352011-02-25 00:01:34 +00003783 *dev_id = chip->read_byte(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003784
Sergey Lapin3a38a552013-01-14 03:46:50 +00003785 /*
3786 * Try again to make sure, as some systems the bus-hold or other
Scott Wood3628f002008-10-24 16:20:43 -05003787 * interface concerns can cause random data which looks like a
3788 * possibly credible NAND flash to appear. If the two results do
3789 * not match, ignore the device completely.
3790 */
3791
3792 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3793
Sergey Lapin3a38a552013-01-14 03:46:50 +00003794 /* Read entire ID string */
3795 for (i = 0; i < 8; i++)
Christian Hitzb8a6b372011-10-12 09:32:02 +02003796 id_data[i] = chip->read_byte(mtd);
Scott Wood3628f002008-10-24 16:20:43 -05003797
Christian Hitzb8a6b372011-10-12 09:32:02 +02003798 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02003799 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003800 *maf_id, *dev_id, id_data[0], id_data[1]);
Scott Wood3628f002008-10-24 16:20:43 -05003801 return ERR_PTR(-ENODEV);
3802 }
3803
Lei Wen75bde942011-01-06 09:48:18 +08003804 if (!type)
3805 type = nand_flash_ids;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003806
Heiko Schocherf5895d12014-06-24 10:10:04 +02003807 for (; type->name != NULL; type++) {
3808 if (is_full_id_nand(type)) {
3809 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3810 goto ident_done;
3811 } else if (*dev_id == type->dev_id) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05003812 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02003813 }
3814 }
Lei Wen75bde942011-01-06 09:48:18 +08003815
Christian Hitzb8a6b372011-10-12 09:32:02 +02003816 chip->onfi_version = 0;
3817 if (!type->name || !type->pagesize) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05003818 /* Check if the chip is ONFI compliant */
Sergey Lapin3a38a552013-01-14 03:46:50 +00003819 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitzb8a6b372011-10-12 09:32:02 +02003820 goto ident_done;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003821
3822 /* Check if the chip is JEDEC compliant */
3823 if (nand_flash_detect_jedec(mtd, chip, &busw))
3824 goto ident_done;
Florian Fainellid6191892010-06-12 20:59:25 +02003825 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003826
Christian Hitzb8a6b372011-10-12 09:32:02 +02003827 if (!type->name)
3828 return ERR_PTR(-ENODEV);
3829
William Juul52c07962007-10-31 13:53:06 +01003830 if (!mtd->name)
3831 mtd->name = type->name;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003832
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003833 chip->chipsize = (uint64_t)type->chipsize << 20;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003834
Scott Wood52ab7ce2016-05-30 13:57:58 -05003835 if (!type->pagesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003836 /* Decode parameters from extended ID */
3837 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003838 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +00003839 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003840 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02003841 /* Get chip options */
Marek Vasutfc417192012-08-30 13:39:38 +00003842 chip->options |= type->options;
Florian Fainellic98a9352011-02-25 00:01:34 +00003843
Sergey Lapin3a38a552013-01-14 03:46:50 +00003844 /*
3845 * Check if chip is not a Samsung device. Do not clear the
3846 * options for chips which do not have an extended id.
Christian Hitzb8a6b372011-10-12 09:32:02 +02003847 */
3848 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3849 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3850ident_done:
3851
William Juul52c07962007-10-31 13:53:06 +01003852 /* Try to identify manufacturer */
3853 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3854 if (nand_manuf_ids[maf_idx].id == *maf_id)
3855 break;
3856 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003857
Heiko Schocherf5895d12014-06-24 10:10:04 +02003858 if (chip->options & NAND_BUSWIDTH_AUTO) {
3859 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3860 chip->options |= busw;
3861 nand_set_defaults(chip, busw);
3862 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3863 /*
3864 * Check, if buswidth is correct. Hardware drivers should set
3865 * chip correct!
3866 */
3867 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3868 *maf_id, *dev_id);
3869 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3870 pr_warn("bus width %d instead %d bit\n",
Sergey Lapin3a38a552013-01-14 03:46:50 +00003871 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3872 busw ? 16 : 8);
William Juul52c07962007-10-31 13:53:06 +01003873 return ERR_PTR(-EINVAL);
3874 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003875
Sergey Lapin3a38a552013-01-14 03:46:50 +00003876 nand_decode_bbm_options(mtd, chip, id_data);
3877
William Juul52c07962007-10-31 13:53:06 +01003878 /* Calculate the address shift from the page size */
3879 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00003880 /* Convert chipsize to number of pages per chip -1 */
William Juul52c07962007-10-31 13:53:06 +01003881 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02003882
William Juul52c07962007-10-31 13:53:06 +01003883 chip->bbt_erase_shift = chip->phys_erase_shift =
3884 ffs(mtd->erasesize) - 1;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -04003885 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj1bc877c2009-11-07 14:24:06 -05003886 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitzb8a6b372011-10-12 09:32:02 +02003887 else {
3888 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3889 chip->chip_shift += 32 - 1;
3890 }
3891
3892 chip->badblockbits = 8;
Scott Wood3ea94ed2015-06-26 19:03:26 -05003893 chip->erase = single_erase;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003894
Sergey Lapin3a38a552013-01-14 03:46:50 +00003895 /* Do not replace user supplied command function! */
William Juul52c07962007-10-31 13:53:06 +01003896 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3897 chip->cmdfunc = nand_command_lp;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02003898
Heiko Schocherf5895d12014-06-24 10:10:04 +02003899 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3900 *maf_id, *dev_id);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003901
Christian Hitzb8a6b372011-10-12 09:32:02 +02003902#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003903 if (chip->onfi_version)
3904 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3905 chip->onfi_params.model);
3906 else if (chip->jedec_version)
3907 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3908 chip->jedec_params.model);
3909 else
3910 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3911 type->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003912#else
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003913 if (chip->jedec_version)
3914 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3915 chip->jedec_params.model);
3916 else
3917 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3918 type->name);
3919
3920 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3921 type->name);
Christian Hitzb8a6b372011-10-12 09:32:02 +02003922#endif
Heiko Schocher081fe9e2014-07-15 16:08:43 +02003923
Scott Wood3ea94ed2015-06-26 19:03:26 -05003924 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02003925 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Wood3ea94ed2015-06-26 19:03:26 -05003926 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juul52c07962007-10-31 13:53:06 +01003927 return type;
3928}
3929
Brian Norrisba6463d2016-06-15 21:09:22 +02003930#if CONFIG_IS_ENABLED(OF_CONTROL)
3931DECLARE_GLOBAL_DATA_PTR;
3932
3933static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3934{
3935 int ret, ecc_mode = -1, ecc_strength, ecc_step;
3936 const void *blob = gd->fdt_blob;
3937 const char *str;
3938
3939 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3940 if (ret == 16)
3941 chip->options |= NAND_BUSWIDTH_16;
3942
3943 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3944 chip->bbt_options |= NAND_BBT_USE_FLASH;
3945
3946 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3947 if (str) {
3948 if (!strcmp(str, "none"))
3949 ecc_mode = NAND_ECC_NONE;
3950 else if (!strcmp(str, "soft"))
3951 ecc_mode = NAND_ECC_SOFT;
3952 else if (!strcmp(str, "hw"))
3953 ecc_mode = NAND_ECC_HW;
3954 else if (!strcmp(str, "hw_syndrome"))
3955 ecc_mode = NAND_ECC_HW_SYNDROME;
3956 else if (!strcmp(str, "hw_oob_first"))
3957 ecc_mode = NAND_ECC_HW_OOB_FIRST;
3958 else if (!strcmp(str, "soft_bch"))
3959 ecc_mode = NAND_ECC_SOFT_BCH;
3960 }
3961
3962
3963 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3964 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3965
3966 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3967 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3968 pr_err("must set both strength and step size in DT\n");
3969 return -EINVAL;
3970 }
3971
3972 if (ecc_mode >= 0)
3973 chip->ecc.mode = ecc_mode;
3974
3975 if (ecc_strength >= 0)
3976 chip->ecc.strength = ecc_strength;
3977
3978 if (ecc_step > 0)
3979 chip->ecc.size = ecc_step;
3980
Boris Brezillonf1a54b02017-11-22 02:38:13 +09003981 if (fdt_getprop(blob, node, "nand-ecc-maximize", NULL))
3982 chip->ecc.options |= NAND_ECC_MAXIMIZE;
3983
Brian Norrisba6463d2016-06-15 21:09:22 +02003984 return 0;
3985}
3986#else
3987static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3988{
3989 return 0;
3990}
3991#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3992
William Juul52c07962007-10-31 13:53:06 +01003993/**
3994 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00003995 * @mtd: MTD device structure
3996 * @maxchips: number of chips to scan for
3997 * @table: alternative NAND ID table
William Juul52c07962007-10-31 13:53:06 +01003998 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00003999 * This is the first phase of the normal nand_scan() function. It reads the
4000 * flash ID and sets up MTD fields accordingly.
William Juul52c07962007-10-31 13:53:06 +01004001 *
William Juul52c07962007-10-31 13:53:06 +01004002 */
Lei Wen75bde942011-01-06 09:48:18 +08004003int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherf5895d12014-06-24 10:10:04 +02004004 struct nand_flash_dev *table)
William Juul52c07962007-10-31 13:53:06 +01004005{
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004006 int i, nand_maf_id, nand_dev_id;
Scott Wood17fed142016-05-30 13:57:56 -05004007 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004008 struct nand_flash_dev *type;
Brian Norrisba6463d2016-06-15 21:09:22 +02004009 int ret;
4010
4011 if (chip->flash_node) {
4012 ret = nand_dt_init(mtd, chip, chip->flash_node);
4013 if (ret)
4014 return ret;
4015 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004016
William Juul52c07962007-10-31 13:53:06 +01004017 /* Set the default functions */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004018 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juul52c07962007-10-31 13:53:06 +01004019
4020 /* Read the flash type */
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004021 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
4022 &nand_dev_id, table);
William Juul52c07962007-10-31 13:53:06 +01004023
4024 if (IS_ERR(type)) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02004025 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4026 pr_warn("No NAND device found\n");
William Juul52c07962007-10-31 13:53:06 +01004027 chip->select_chip(mtd, -1);
4028 return PTR_ERR(type);
4029 }
4030
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004031 /* Initialize the ->data_interface field. */
Boris Brezillone509cba2017-11-22 02:38:19 +09004032 ret = nand_init_data_interface(chip);
4033 if (ret)
4034 return ret;
4035
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004036 /*
4037 * Setup the data interface correctly on the chip and controller side.
4038 * This explicit call to nand_setup_data_interface() is only required
4039 * for the first die, because nand_reset() has been called before
4040 * ->data_interface and ->default_onfi_timing_mode were set.
4041 * For the other dies, nand_reset() will automatically switch to the
4042 * best mode for us.
4043 */
Boris Brezillon32935f42017-11-22 02:38:28 +09004044 ret = nand_setup_data_interface(chip, 0);
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004045 if (ret)
4046 return ret;
4047
Heiko Schocherf5895d12014-06-24 10:10:04 +02004048 chip->select_chip(mtd, -1);
4049
William Juul52c07962007-10-31 13:53:06 +01004050 /* Check for a chip array */
4051 for (i = 1; i < maxchips; i++) {
Karl Beldanb6322fc2008-09-15 16:08:03 +02004052 /* See comment in nand_get_flash_type for reset */
Boris Brezillon7ec6dc52017-11-22 02:38:20 +09004053 nand_reset(chip, i);
4054
4055 chip->select_chip(mtd, i);
William Juul52c07962007-10-31 13:53:06 +01004056 /* Send the command for reading device ID */
4057 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004058 /* Read manufacturer and device IDs */
William Juul52c07962007-10-31 13:53:06 +01004059 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherf5895d12014-06-24 10:10:04 +02004060 nand_dev_id != chip->read_byte(mtd)) {
4061 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004062 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004063 }
4064 chip->select_chip(mtd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004065 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004066
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004067#ifdef DEBUG
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004068 if (i > 1)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004069 pr_info("%d chips detected\n", i);
Wolfgang Grandeggerb325d7e2009-02-11 18:38:20 +01004070#endif
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004071
William Juul52c07962007-10-31 13:53:06 +01004072 /* Store the number of chips and calc total size for mtd */
4073 chip->numchips = i;
4074 mtd->size = i * chip->chipsize;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004075
William Juul52c07962007-10-31 13:53:06 +01004076 return 0;
4077}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004078EXPORT_SYMBOL(nand_scan_ident);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004079
Scott Wood3ea94ed2015-06-26 19:03:26 -05004080/*
4081 * Check if the chip configuration meet the datasheet requirements.
4082
4083 * If our configuration corrects A bits per B bytes and the minimum
4084 * required correction level is X bits per Y bytes, then we must ensure
4085 * both of the following are true:
4086 *
4087 * (1) A / B >= X / Y
4088 * (2) A >= X
4089 *
4090 * Requirement (1) ensures we can correct for the required bitflip density.
4091 * Requirement (2) ensures we can correct even when all bitflips are clumped
4092 * in the same sector.
4093 */
4094static bool nand_ecc_strength_good(struct mtd_info *mtd)
4095{
Scott Wood17fed142016-05-30 13:57:56 -05004096 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05004097 struct nand_ecc_ctrl *ecc = &chip->ecc;
4098 int corr, ds_corr;
4099
4100 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4101 /* Not enough information */
4102 return true;
4103
4104 /*
4105 * We get the number of corrected bits per page to compare
4106 * the correction density.
4107 */
4108 corr = (mtd->writesize * ecc->strength) / ecc->size;
4109 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4110
4111 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4112}
William Juul52c07962007-10-31 13:53:06 +01004113
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004114static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4115{
4116 struct nand_ecc_ctrl *ecc = &chip->ecc;
4117
4118 if (nand_standard_page_accessors(ecc))
4119 return false;
4120
4121 /*
4122 * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND
4123 * controller driver implements all the page accessors because
4124 * default helpers are not suitable when the core does not
4125 * send the READ0/PAGEPROG commands.
4126 */
4127 return (!ecc->read_page || !ecc->write_page ||
4128 !ecc->read_page_raw || !ecc->write_page_raw ||
4129 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4130 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4131 ecc->hwctl && ecc->calculate));
4132}
4133
William Juul52c07962007-10-31 13:53:06 +01004134/**
4135 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004136 * @mtd: MTD device structure
William Juul52c07962007-10-31 13:53:06 +01004137 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004138 * This is the second phase of the normal nand_scan() function. It fills out
4139 * all the uninitialized function pointers with the defaults and scans for a
4140 * bad block table if appropriate.
William Juul52c07962007-10-31 13:53:06 +01004141 */
4142int nand_scan_tail(struct mtd_info *mtd)
4143{
4144 int i;
Scott Wood17fed142016-05-30 13:57:56 -05004145 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004146 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004147 struct nand_buffers *nbuf;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004148
Sergey Lapin3a38a552013-01-14 03:46:50 +00004149 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
4150 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4151 !(chip->bbt_options & NAND_BBT_USE_FLASH));
4152
Marc Gonzalezc3a29852017-11-22 02:38:22 +09004153 if (invalid_ecc_page_accessors(chip)) {
4154 pr_err("Invalid ECC page accessors setup\n");
4155 return -EINVAL;
4156 }
4157
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004158 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004159 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004160 chip->buffers = nbuf;
4161 } else {
4162 if (!chip->buffers)
4163 return -ENOMEM;
4164 }
William Juul52c07962007-10-31 13:53:06 +01004165
4166 /* Set the internal oob buffer location, just after the page data */
4167 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4168
4169 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004170 * If no default placement scheme is given, select an appropriate one.
William Juul52c07962007-10-31 13:53:06 +01004171 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004172 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004173 switch (mtd->oobsize) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004174 case 8:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004175 ecc->layout = &nand_oob_8;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004176 break;
4177 case 16:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004178 ecc->layout = &nand_oob_16;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004179 break;
4180 case 64:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004181 ecc->layout = &nand_oob_64;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004182 break;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004183 case 128:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004184 ecc->layout = &nand_oob_128;
Sergei Poselenov04fbaa02008-06-06 15:42:43 +02004185 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004186 default:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004187 pr_warn("No oob scheme defined for oobsize %d\n",
4188 mtd->oobsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004189 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004190 }
4191 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004192
William Juul52c07962007-10-31 13:53:06 +01004193 if (!chip->write_page)
4194 chip->write_page = nand_write_page;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004195
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004196 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +00004197 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juul52c07962007-10-31 13:53:06 +01004198 * selected and we have 256 byte pagesize fallback to software ECC
4199 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004200
Heiko Schocherf5895d12014-06-24 10:10:04 +02004201 switch (ecc->mode) {
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004202 case NAND_ECC_HW_OOB_FIRST:
4203 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004204 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004205 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004206 BUG();
4207 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004208 if (!ecc->read_page)
4209 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajdea40702009-08-10 13:27:56 -04004210
William Juul52c07962007-10-31 13:53:06 +01004211 case NAND_ECC_HW:
Sergey Lapin3a38a552013-01-14 03:46:50 +00004212 /* Use standard hwecc read page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004213 if (!ecc->read_page)
4214 ecc->read_page = nand_read_page_hwecc;
4215 if (!ecc->write_page)
4216 ecc->write_page = nand_write_page_hwecc;
4217 if (!ecc->read_page_raw)
4218 ecc->read_page_raw = nand_read_page_raw;
4219 if (!ecc->write_page_raw)
4220 ecc->write_page_raw = nand_write_page_raw;
4221 if (!ecc->read_oob)
4222 ecc->read_oob = nand_read_oob_std;
4223 if (!ecc->write_oob)
4224 ecc->write_oob = nand_write_oob_std;
4225 if (!ecc->read_subpage)
4226 ecc->read_subpage = nand_read_subpage;
Scott Wood52ab7ce2016-05-30 13:57:58 -05004227 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherf5895d12014-06-24 10:10:04 +02004228 ecc->write_subpage = nand_write_subpage_hwecc;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004229
William Juul52c07962007-10-31 13:53:06 +01004230 case NAND_ECC_HW_SYNDROME:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004231 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4232 (!ecc->read_page ||
4233 ecc->read_page == nand_read_page_hwecc ||
4234 !ecc->write_page ||
4235 ecc->write_page == nand_write_page_hwecc)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -05004236 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juul52c07962007-10-31 13:53:06 +01004237 BUG();
4238 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00004239 /* Use standard syndrome read/write page function? */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004240 if (!ecc->read_page)
4241 ecc->read_page = nand_read_page_syndrome;
4242 if (!ecc->write_page)
4243 ecc->write_page = nand_write_page_syndrome;
4244 if (!ecc->read_page_raw)
4245 ecc->read_page_raw = nand_read_page_raw_syndrome;
4246 if (!ecc->write_page_raw)
4247 ecc->write_page_raw = nand_write_page_raw_syndrome;
4248 if (!ecc->read_oob)
4249 ecc->read_oob = nand_read_oob_syndrome;
4250 if (!ecc->write_oob)
4251 ecc->write_oob = nand_write_oob_syndrome;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004252
Heiko Schocherf5895d12014-06-24 10:10:04 +02004253 if (mtd->writesize >= ecc->size) {
4254 if (!ecc->strength) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004255 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4256 BUG();
4257 }
William Juul52c07962007-10-31 13:53:06 +01004258 break;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004259 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004260 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4261 ecc->size, mtd->writesize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004262 ecc->mode = NAND_ECC_SOFT;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004263
William Juul52c07962007-10-31 13:53:06 +01004264 case NAND_ECC_SOFT:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004265 ecc->calculate = nand_calculate_ecc;
4266 ecc->correct = nand_correct_data;
4267 ecc->read_page = nand_read_page_swecc;
4268 ecc->read_subpage = nand_read_subpage;
4269 ecc->write_page = nand_write_page_swecc;
4270 ecc->read_page_raw = nand_read_page_raw;
4271 ecc->write_page_raw = nand_write_page_raw;
4272 ecc->read_oob = nand_read_oob_std;
4273 ecc->write_oob = nand_write_oob_std;
4274 if (!ecc->size)
4275 ecc->size = 256;
4276 ecc->bytes = 3;
4277 ecc->strength = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004278 break;
4279
Christian Hitz55f7bca2011-10-12 09:31:59 +02004280 case NAND_ECC_SOFT_BCH:
4281 if (!mtd_nand_has_bch()) {
Heiko Schocher081fe9e2014-07-15 16:08:43 +02004282 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004283 BUG();
Christian Hitz55f7bca2011-10-12 09:31:59 +02004284 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004285 ecc->calculate = nand_bch_calculate_ecc;
4286 ecc->correct = nand_bch_correct_data;
4287 ecc->read_page = nand_read_page_swecc;
4288 ecc->read_subpage = nand_read_subpage;
4289 ecc->write_page = nand_write_page_swecc;
4290 ecc->read_page_raw = nand_read_page_raw;
4291 ecc->write_page_raw = nand_write_page_raw;
4292 ecc->read_oob = nand_read_oob_std;
4293 ecc->write_oob = nand_write_oob_std;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004294 /*
Scott Wood3ea94ed2015-06-26 19:03:26 -05004295 * Board driver should supply ecc.size and ecc.strength values
4296 * to select how many bits are correctable. Otherwise, default
4297 * to 4 bits for large page devices.
Christian Hitz55f7bca2011-10-12 09:31:59 +02004298 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004299 if (!ecc->size && (mtd->oobsize >= 64)) {
4300 ecc->size = 512;
Scott Wood3ea94ed2015-06-26 19:03:26 -05004301 ecc->strength = 4;
Christian Hitz55f7bca2011-10-12 09:31:59 +02004302 }
Scott Wood3ea94ed2015-06-26 19:03:26 -05004303
4304 /* See nand_bch_init() for details. */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004305 ecc->bytes = 0;
4306 ecc->priv = nand_bch_init(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02004307 if (!ecc->priv) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004308 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004309 BUG();
4310 }
Christian Hitz55f7bca2011-10-12 09:31:59 +02004311 break;
4312
William Juul52c07962007-10-31 13:53:06 +01004313 case NAND_ECC_NONE:
Scott Wood3ea94ed2015-06-26 19:03:26 -05004314 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherf5895d12014-06-24 10:10:04 +02004315 ecc->read_page = nand_read_page_raw;
4316 ecc->write_page = nand_write_page_raw;
4317 ecc->read_oob = nand_read_oob_std;
4318 ecc->read_page_raw = nand_read_page_raw;
4319 ecc->write_page_raw = nand_write_page_raw;
4320 ecc->write_oob = nand_write_oob_std;
4321 ecc->size = mtd->writesize;
4322 ecc->bytes = 0;
4323 ecc->strength = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004324 break;
4325
4326 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +02004327 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juul52c07962007-10-31 13:53:06 +01004328 BUG();
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004329 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004330
Sergey Lapin3a38a552013-01-14 03:46:50 +00004331 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004332 if (!ecc->read_oob_raw)
4333 ecc->read_oob_raw = ecc->read_oob;
4334 if (!ecc->write_oob_raw)
4335 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004336
William Juul52c07962007-10-31 13:53:06 +01004337 /*
4338 * The number of bytes available for a client to place data into
Sergey Lapin3a38a552013-01-14 03:46:50 +00004339 * the out of band area.
William Juul52c07962007-10-31 13:53:06 +01004340 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05004341 mtd->oobavail = 0;
4342 if (ecc->layout) {
4343 for (i = 0; ecc->layout->oobfree[i].length; i++)
4344 mtd->oobavail += ecc->layout->oobfree[i].length;
4345 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004346
Scott Wood3ea94ed2015-06-26 19:03:26 -05004347 /* ECC sanity check: warn if it's too weak */
4348 if (!nand_ecc_strength_good(mtd))
4349 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4350 mtd->name);
4351
William Juul52c07962007-10-31 13:53:06 +01004352 /*
4353 * Set the number of read / write steps for one page depending on ECC
Sergey Lapin3a38a552013-01-14 03:46:50 +00004354 * mode.
William Juul52c07962007-10-31 13:53:06 +01004355 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004356 ecc->steps = mtd->writesize / ecc->size;
4357 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00004358 pr_warn("Invalid ECC parameters\n");
William Juul52c07962007-10-31 13:53:06 +01004359 BUG();
4360 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02004361 ecc->total = ecc->steps * ecc->bytes;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004362
Sergey Lapin3a38a552013-01-14 03:46:50 +00004363 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004364 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4365 switch (ecc->steps) {
William Juul52c07962007-10-31 13:53:06 +01004366 case 2:
4367 mtd->subpage_sft = 1;
4368 break;
4369 case 4:
4370 case 8:
Sandeep Paulrajfd9874d2009-11-07 14:24:34 -05004371 case 16:
William Juul52c07962007-10-31 13:53:06 +01004372 mtd->subpage_sft = 2;
4373 break;
4374 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004375 }
William Juul52c07962007-10-31 13:53:06 +01004376 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004377
William Juul52c07962007-10-31 13:53:06 +01004378 /* Initialize state */
4379 chip->state = FL_READY;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004380
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004381 /* Invalidate the pagebuffer reference */
William Juul52c07962007-10-31 13:53:06 +01004382 chip->pagebuf = -1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004383
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004384 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Wood3ea94ed2015-06-26 19:03:26 -05004385 switch (ecc->mode) {
4386 case NAND_ECC_SOFT:
4387 case NAND_ECC_SOFT_BCH:
4388 if (chip->page_shift > 9)
4389 chip->options |= NAND_SUBPAGE_READ;
4390 break;
4391
4392 default:
4393 break;
4394 }
Joe Hershberger7a38ffa2012-11-05 06:46:31 +00004395
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004396 /* Fill in remaining MTD driver data */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004397 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitzb8a6b372011-10-12 09:32:02 +02004398 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4399 MTD_CAP_NANDFLASH;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004400 mtd->_erase = nand_erase;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004401 mtd->_read = nand_read;
4402 mtd->_write = nand_write;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004403 mtd->_panic_write = panic_nand_write;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004404 mtd->_read_oob = nand_read_oob;
4405 mtd->_write_oob = nand_write_oob;
4406 mtd->_sync = nand_sync;
4407 mtd->_lock = NULL;
4408 mtd->_unlock = NULL;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03004409 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004410 mtd->_block_isbad = nand_block_isbad;
4411 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherf5895d12014-06-24 10:10:04 +02004412 mtd->writebufsize = mtd->writesize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004413
Sergey Lapin3a38a552013-01-14 03:46:50 +00004414 /* propagate ecc info to mtd_info */
Heiko Schocherf5895d12014-06-24 10:10:04 +02004415 mtd->ecclayout = ecc->layout;
4416 mtd->ecc_strength = ecc->strength;
4417 mtd->ecc_step_size = ecc->size;
Sergey Lapin3a38a552013-01-14 03:46:50 +00004418 /*
4419 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4420 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4421 * properly set.
4422 */
4423 if (!mtd->bitflip_threshold)
Scott Wood3ea94ed2015-06-26 19:03:26 -05004424 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juul52c07962007-10-31 13:53:06 +01004425
Rostislav Lisovydc17bdc2014-10-22 13:40:44 +02004426 return 0;
William Juul52c07962007-10-31 13:53:06 +01004427}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004428EXPORT_SYMBOL(nand_scan_tail);
4429
William Juul52c07962007-10-31 13:53:06 +01004430/**
4431 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapin3a38a552013-01-14 03:46:50 +00004432 * @mtd: MTD device structure
4433 * @maxchips: number of chips to scan for
William Juul52c07962007-10-31 13:53:06 +01004434 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00004435 * This fills out all the uninitialized function pointers with the defaults.
4436 * The flash ID is read and the mtd/chip structures are filled with the
Scott Wood52ab7ce2016-05-30 13:57:58 -05004437 * appropriate values.
William Juul52c07962007-10-31 13:53:06 +01004438 */
4439int nand_scan(struct mtd_info *mtd, int maxchips)
4440{
4441 int ret;
4442
Lei Wen75bde942011-01-06 09:48:18 +08004443 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juul52c07962007-10-31 13:53:06 +01004444 if (!ret)
4445 ret = nand_scan_tail(mtd);
4446 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004447}
Heiko Schocherf5895d12014-06-24 10:10:04 +02004448EXPORT_SYMBOL(nand_scan);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02004449
Heiko Schocherf5895d12014-06-24 10:10:04 +02004450MODULE_LICENSE("GPL");
4451MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4452MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4453MODULE_DESCRIPTION("Generic NAND flash driver code");