blob: d1287bc3be9f880d4776683d8f4d267e3f34d643 [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +02005 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02006 * Additional technical information is available on
Scott Woodc45912d2008-10-24 16:20:43 -05007 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02008 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02009 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juulcfa460a2007-10-31 13:53:06 +010010 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020011 *
William Juulcfa460a2007-10-31 13:53:06 +010012 * Credits:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020013 * David Woodhouse for adding multichip support
14 *
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +010018 * TODO:
Wolfgang Denk932394a2005-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 Lapindfe64e22013-01-14 03:46:50 +000021 * if we have HW ECC support.
Scott Woodc45912d2008-10-24 16:20:43 -050022 * BBT table is not serialized, has to be fixed
Wolfgang Denk932394a2005-08-17 12:55:25 +020023 *
Wolfgang Denk932394a2005-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 Schocherff94bc42014-06-24 10:10:04 +020030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Wolfgang Denk932394a2005-08-17 12:55:25 +020031#include <common.h>
Brian Norris42bd19c2016-06-15 21:09:22 +020032#if CONFIG_IS_ENABLED(OF_CONTROL)
33#include <fdtdec.h>
34#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020035#include <malloc.h>
36#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010037#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000038#include <linux/compat.h>
Wolfgang Denk932394a2005-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 Hitz4c6de852011-10-12 09:31:59 +020042#include <linux/mtd/nand_bch.h>
Stefan Roese10bb62d2009-04-24 15:58:33 +020043#ifdef CONFIG_MTD_PARTITIONS
44#include <linux/mtd/partitions.h>
45#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020046#include <asm/io.h>
47#include <asm/errno.h>
48
Wolfgang Denk932394a2005-08-17 12:55:25 +020049/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010050static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020051 .eccbytes = 3,
52 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010053 .oobfree = {
54 {.offset = 3,
55 .length = 2},
56 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020057 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020058};
59
William Juulcfa460a2007-10-31 13:53:06 +010060static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020061 .eccbytes = 6,
62 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010063 .oobfree = {
64 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020065 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020066};
67
William Juulcfa460a2007-10-31 13:53:06 +010068static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020069 .eccbytes = 24,
70 .eccpos = {
William Juulcfa460a2007-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 Hitz90e3f392011-10-12 09:32:01 +020076 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020077};
78
William Juulcfa460a2007-10-31 13:53:06 +010079static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +020080 .eccbytes = 48,
81 .eccpos = {
Christian Hitz90e3f392011-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 Juulcfa460a2007-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 Hitz90e3f392011-10-12 09:32:01 +020090 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020091};
92
Heiko Schocherff94bc42014-06-24 10:10:04 +020093static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juulcfa460a2007-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 Schocherff94bc42014-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 Poselenov248ae5c2008-06-06 15:42:43 +0200103
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200104static int check_offs_len(struct mtd_info *mtd,
105 loff_t ofs, uint64_t len)
106{
Scott Wood17cb4b82016-05-30 13:57:56 -0500107 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200108 int ret = 0;
109
110 /* Start address must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200111 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
112 pr_debug("%s: unaligned address\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200113 ret = -EINVAL;
114 }
115
116 /* Length must align on block boundary */
Heiko Schocherff94bc42014-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 Hitz2a8e0fc2011-10-12 09:32:02 +0200119 ret = -EINVAL;
120 }
121
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200122 return ret;
123}
124
Wolfgang Denk932394a2005-08-17 12:55:25 +0200125/**
126 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000127 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200128 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200129 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200130 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200131static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100132{
Scott Wood17cb4b82016-05-30 13:57:56 -0500133 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200134
135 /* De-select the NAND device */
136 chip->select_chip(mtd, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100137}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200138
139/**
140 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000141 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200142 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200143 * Default read function for 8bit buswidth
Wolfgang Denk932394a2005-08-17 12:55:25 +0200144 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000145uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200146{
Scott Wood17cb4b82016-05-30 13:57:56 -0500147 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100148 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200149}
150
151/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200152 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000153 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200154 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000155 * Default read function for 16bit buswidth with endianness conversion.
156 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200157 */
William Juulcfa460a2007-10-31 13:53:06 +0100158static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200159{
Scott Wood17cb4b82016-05-30 13:57:56 -0500160 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100161 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200162}
163
164/**
165 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000166 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200167 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000168 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200169 */
170static u16 nand_read_word(struct mtd_info *mtd)
171{
Scott Wood17cb4b82016-05-30 13:57:56 -0500172 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100173 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200174}
175
176/**
177 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000178 * @mtd: MTD device structure
179 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200180 *
181 * Default select function for 1 chip devices.
182 */
William Juulcfa460a2007-10-31 13:53:06 +0100183static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200184{
Scott Wood17cb4b82016-05-30 13:57:56 -0500185 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100186
187 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200188 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100189 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200190 break;
191 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200192 break;
193
194 default:
195 BUG();
196 }
197}
198
199/**
Heiko Schocherff94bc42014-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 Wood17cb4b82016-05-30 13:57:56 -0500208 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-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 Wood17cb4b82016-05-30 13:57:56 -0500222 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-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
Scott Wood27331062015-06-22 22:38:32 -0500244#if !defined(CONFIG_BLACKFIN)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200245static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
246{
247 int i;
248
249 for (i = 0; i < len; i++)
250 writeb(buf[i], addr);
251}
252static void ioread8_rep(void *addr, uint8_t *buf, int len)
253{
254 int i;
255
256 for (i = 0; i < len; i++)
257 buf[i] = readb(addr);
258}
259
260static void ioread16_rep(void *addr, void *buf, int len)
261{
262 int i;
263 u16 *p = (u16 *) buf;
Stefan Roesebe16aba2014-09-05 09:57:01 +0200264
Heiko Schocherff94bc42014-06-24 10:10:04 +0200265 for (i = 0; i < len; i++)
266 p[i] = readw(addr);
267}
268
269static void iowrite16_rep(void *addr, void *buf, int len)
270{
271 int i;
272 u16 *p = (u16 *) buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200273
274 for (i = 0; i < len; i++)
275 writew(p[i], addr);
276}
277#endif
278
279/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200280 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000281 * @mtd: MTD device structure
282 * @buf: data buffer
283 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200284 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000285 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200286 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000287void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200288{
Scott Wood17cb4b82016-05-30 13:57:56 -0500289 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200290
Heiko Schocherff94bc42014-06-24 10:10:04 +0200291 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200292}
293
294/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200295 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000296 * @mtd: MTD device structure
297 * @buf: buffer to store date
298 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200299 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000300 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200301 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400302void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200303{
Scott Wood17cb4b82016-05-30 13:57:56 -0500304 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200305
Heiko Schocherff94bc42014-06-24 10:10:04 +0200306 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200307}
308
Heiko Schocherff94bc42014-06-24 10:10:04 +0200309/**
310 * nand_write_buf16 - [DEFAULT] write buffer to chip
311 * @mtd: MTD device structure
312 * @buf: data buffer
313 * @len: number of bytes to write
314 *
315 * Default write function for 16bit buswidth.
316 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200317void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200318{
Scott Wood17cb4b82016-05-30 13:57:56 -0500319 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200320 u16 *p = (u16 *) buf;
321
322 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
323}
324
325/**
326 * nand_read_buf16 - [DEFAULT] read chip data into buffer
327 * @mtd: MTD device structure
328 * @buf: buffer to store date
329 * @len: number of bytes to read
330 *
331 * Default read function for 16bit buswidth.
332 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200333void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200334{
Scott Wood17cb4b82016-05-30 13:57:56 -0500335 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200336 u16 *p = (u16 *) buf;
337
338 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
339}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200340
341/**
342 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000343 * @mtd: MTD device structure
344 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200345 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200346 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200347 */
Scott Woodceee07b2016-05-30 13:57:58 -0500348static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200349{
Scott Woodceee07b2016-05-30 13:57:58 -0500350 int page, res = 0, i = 0;
Scott Wood17cb4b82016-05-30 13:57:56 -0500351 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200352 u16 bad;
353
Sergey Lapindfe64e22013-01-14 03:46:50 +0000354 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200355 ofs += mtd->erasesize - mtd->writesize;
356
William Juulcfa460a2007-10-31 13:53:06 +0100357 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200358
Sergey Lapindfe64e22013-01-14 03:46:50 +0000359 do {
360 if (chip->options & NAND_BUSWIDTH_16) {
361 chip->cmdfunc(mtd, NAND_CMD_READOOB,
362 chip->badblockpos & 0xFE, page);
363 bad = cpu_to_le16(chip->read_word(mtd));
364 if (chip->badblockpos & 0x1)
365 bad >>= 8;
366 else
367 bad &= 0xFF;
368 } else {
369 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
370 page);
371 bad = chip->read_byte(mtd);
372 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200373
Sergey Lapindfe64e22013-01-14 03:46:50 +0000374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
378 ofs += mtd->writesize;
379 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
380 i++;
381 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200382
Wolfgang Denk932394a2005-08-17 12:55:25 +0200383 return res;
384}
385
386/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200387 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapindfe64e22013-01-14 03:46:50 +0000388 * @mtd: MTD device structure
389 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200390 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000391 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherff94bc42014-06-24 10:10:04 +0200392 * specific driver. It provides the details for writing a bad block marker to a
393 * block.
394 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200395static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
396{
Scott Wood17cb4b82016-05-30 13:57:56 -0500397 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200398 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +0100399 uint8_t buf[2] = { 0, 0 };
Heiko Schocherff94bc42014-06-24 10:10:04 +0200400 int ret = 0, res, i = 0;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200401
Scott Woodd3963722015-06-26 19:03:26 -0500402 memset(&ops, 0, sizeof(ops));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200403 ops.oobbuf = buf;
404 ops.ooboffs = chip->badblockpos;
405 if (chip->options & NAND_BUSWIDTH_16) {
406 ops.ooboffs &= ~0x01;
407 ops.len = ops.ooblen = 2;
408 } else {
409 ops.len = ops.ooblen = 1;
410 }
411 ops.mode = MTD_OPS_PLACE_OOB;
412
413 /* Write to first/last page(s) if necessary */
414 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
415 ofs += mtd->erasesize - mtd->writesize;
416 do {
417 res = nand_do_write_oob(mtd, ofs, &ops);
418 if (!ret)
419 ret = res;
420
421 i++;
422 ofs += mtd->writesize;
423 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
424
425 return ret;
426}
427
428/**
429 * nand_block_markbad_lowlevel - mark a block bad
430 * @mtd: MTD device structure
431 * @ofs: offset from device start
432 *
433 * This function performs the generic NAND bad block marking steps (i.e., bad
434 * block table(s) and/or marker(s)). We only allow the hardware driver to
435 * specify how to write bad block markers to OOB (chip->block_markbad).
436 *
437 * We try operations in the following order:
438 * (1) erase the affected block, to allow OOB marker to be written cleanly
439 * (2) write bad block marker to OOB area of affected block (unless flag
440 * NAND_BBT_NO_OOB_BBM is present)
441 * (3) update the BBT
442 * Note that we retain the first error encountered in (2) or (3), finish the
443 * procedures, and dump the error in the end.
444*/
445static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
446{
Scott Wood17cb4b82016-05-30 13:57:56 -0500447 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200448 int res, ret = 0;
449
450 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000451 struct erase_info einfo;
452
453 /* Attempt erase before marking OOB */
454 memset(&einfo, 0, sizeof(einfo));
455 einfo.mtd = mtd;
456 einfo.addr = ofs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200457 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000458 nand_erase_nand(mtd, &einfo, 0);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200459
Heiko Schocherff94bc42014-06-24 10:10:04 +0200460 /* Write bad block marker to OOB */
461 nand_get_device(mtd, FL_WRITING);
462 ret = chip->block_markbad(mtd, ofs);
Scott Woodc45912d2008-10-24 16:20:43 -0500463 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100464 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000465
Heiko Schocherff94bc42014-06-24 10:10:04 +0200466 /* Mark block bad in BBT */
467 if (chip->bbt) {
468 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000469 if (!ret)
470 ret = res;
471 }
472
William Juulcfa460a2007-10-31 13:53:06 +0100473 if (!ret)
474 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500475
William Juulcfa460a2007-10-31 13:53:06 +0100476 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200477}
478
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200479/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200480 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000481 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200482 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000483 * Check, if the device is write protected. The function expects, that the
484 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200485 */
William Juulcfa460a2007-10-31 13:53:06 +0100486static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200487{
Scott Wood17cb4b82016-05-30 13:57:56 -0500488 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200489
Sergey Lapindfe64e22013-01-14 03:46:50 +0000490 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200491 if (chip->options & NAND_BROKEN_XD)
492 return 0;
493
Wolfgang Denk932394a2005-08-17 12:55:25 +0200494 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100495 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
496 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200497}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100498
Wolfgang Denk932394a2005-08-17 12:55:25 +0200499/**
Scott Woodd3963722015-06-26 19:03:26 -0500500 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000501 * @mtd: MTD device structure
502 * @ofs: offset from device start
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300503 *
Scott Woodd3963722015-06-26 19:03:26 -0500504 * Check if the block is marked as reserved.
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300505 */
506static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
507{
Scott Wood17cb4b82016-05-30 13:57:56 -0500508 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300509
510 if (!chip->bbt)
511 return 0;
512 /* Return info from the table */
513 return nand_isreserved_bbt(mtd, ofs);
514}
515
516/**
517 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
518 * @mtd: MTD device structure
519 * @ofs: offset from device start
Sergey Lapindfe64e22013-01-14 03:46:50 +0000520 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200521 *
522 * Check, if the block is bad. Either by reading the bad block table or
523 * calling of the scan function.
524 */
Scott Woodceee07b2016-05-30 13:57:58 -0500525static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200526{
Scott Wood17cb4b82016-05-30 13:57:56 -0500527 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200528
Masahiro Yamadaab37b762014-12-26 22:20:58 +0900529 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
530 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovy35c204d2014-10-22 13:40:44 +0200531 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamadabf80ee62014-12-26 22:20:57 +0900532 chip->scan_bbt(mtd);
Rostislav Lisovy35c204d2014-10-22 13:40:44 +0200533 }
534
William Juulcfa460a2007-10-31 13:53:06 +0100535 if (!chip->bbt)
Scott Woodceee07b2016-05-30 13:57:58 -0500536 return chip->block_bad(mtd, ofs);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200537
Wolfgang Denk932394a2005-08-17 12:55:25 +0200538 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100539 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200540}
541
Scott Woodceee07b2016-05-30 13:57:58 -0500542/**
543 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
544 * @mtd: MTD device structure
545 *
546 * Wait for the ready pin after a command, and warn if a timeout occurs.
547 */
William Juulcfa460a2007-10-31 13:53:06 +0100548void nand_wait_ready(struct mtd_info *mtd)
549{
Scott Wood17cb4b82016-05-30 13:57:56 -0500550 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Woodceee07b2016-05-30 13:57:58 -0500551 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000552 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100553
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000554 time_start = get_timer(0);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000555 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000556 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100557 if (chip->dev_ready)
558 if (chip->dev_ready(mtd))
559 break;
560 }
Scott Woodceee07b2016-05-30 13:57:58 -0500561
562 if (!chip->dev_ready(mtd))
563 pr_warn("timeout while waiting for chip to become ready\n");
William Juulcfa460a2007-10-31 13:53:06 +0100564}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200565EXPORT_SYMBOL_GPL(nand_wait_ready);
William Juulcfa460a2007-10-31 13:53:06 +0100566
Wolfgang Denk932394a2005-08-17 12:55:25 +0200567/**
Scott Woodd3963722015-06-26 19:03:26 -0500568 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
569 * @mtd: MTD device structure
570 * @timeo: Timeout in ms
571 *
572 * Wait for status ready (i.e. command done) or timeout.
573 */
574static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
575{
Scott Wood17cb4b82016-05-30 13:57:56 -0500576 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Woodd3963722015-06-26 19:03:26 -0500577 u32 time_start;
578
579 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
580 time_start = get_timer(0);
581 while (get_timer(time_start) < timeo) {
582 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
583 break;
584 WATCHDOG_RESET();
585 }
586};
587
588/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200589 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000590 * @mtd: MTD device structure
591 * @command: the command to be sent
592 * @column: the column address for this command, -1 if none
593 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200594 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000595 * Send command to NAND device. This function is used for small page devices
Heiko Schocherff94bc42014-06-24 10:10:04 +0200596 * (512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200597 */
William Juulcfa460a2007-10-31 13:53:06 +0100598static void nand_command(struct mtd_info *mtd, unsigned int command,
599 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200600{
Scott Wood17cb4b82016-05-30 13:57:56 -0500601 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100602 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200603
Sergey Lapindfe64e22013-01-14 03:46:50 +0000604 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200605 if (command == NAND_CMD_SEQIN) {
606 int readcmd;
607
William Juulcfa460a2007-10-31 13:53:06 +0100608 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200609 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100610 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200611 readcmd = NAND_CMD_READOOB;
612 } else if (column < 256) {
613 /* First 256 bytes --> READ0 */
614 readcmd = NAND_CMD_READ0;
615 } else {
616 column -= 256;
617 readcmd = NAND_CMD_READ1;
618 }
William Juulcfa460a2007-10-31 13:53:06 +0100619 chip->cmd_ctrl(mtd, readcmd, ctrl);
620 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200621 }
William Juulcfa460a2007-10-31 13:53:06 +0100622 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200623
Sergey Lapindfe64e22013-01-14 03:46:50 +0000624 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100625 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
626 /* Serially input address */
627 if (column != -1) {
628 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200629 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530630 !nand_opcode_8bits(command))
William Juulcfa460a2007-10-31 13:53:06 +0100631 column >>= 1;
632 chip->cmd_ctrl(mtd, column, ctrl);
633 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200634 }
William Juulcfa460a2007-10-31 13:53:06 +0100635 if (page_addr != -1) {
636 chip->cmd_ctrl(mtd, page_addr, ctrl);
637 ctrl &= ~NAND_CTRL_CHANGE;
638 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
639 /* One more address cycle for devices > 32MiB */
640 if (chip->chipsize > (32 << 20))
641 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
642 }
643 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200644
645 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000646 * Program and erase have their own busy handlers status and sequential
647 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100648 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200649 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200650
Wolfgang Denk932394a2005-08-17 12:55:25 +0200651 case NAND_CMD_PAGEPROG:
652 case NAND_CMD_ERASE1:
653 case NAND_CMD_ERASE2:
654 case NAND_CMD_SEQIN:
655 case NAND_CMD_STATUS:
656 return;
657
658 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100659 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200660 break;
William Juulcfa460a2007-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 Woodd3963722015-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 Denk932394a2005-08-17 12:55:25 +0200668 return;
669
William Juulcfa460a2007-10-31 13:53:06 +0100670 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200671 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200672 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200673 * If we don't have access to the busy pin, we apply the given
674 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100675 */
676 if (!chip->dev_ready) {
677 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200678 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200679 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200680 }
Sergey Lapindfe64e22013-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 Juulcfa460a2007-10-31 13:53:06 +0100685 ndelay(100);
686
687 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200688}
689
690/**
691 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-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 Denk932394a2005-08-17 12:55:25 +0200696 *
William Juulcfa460a2007-10-31 13:53:06 +0100697 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-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 Denk932394a2005-08-17 12:55:25 +0200700 */
William Juulcfa460a2007-10-31 13:53:06 +0100701static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
702 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200703{
Scott Wood17cb4b82016-05-30 13:57:56 -0500704 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200705
706 /* Emulate NAND_CMD_READOOB */
707 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100708 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200709 command = NAND_CMD_READ0;
710 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200711
William Juulcfa460a2007-10-31 13:53:06 +0100712 /* Command latch cycle */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200713 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200714
715 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100716 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200717
718 /* Serially input address */
719 if (column != -1) {
720 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200721 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530722 !nand_opcode_8bits(command))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200723 column >>= 1;
William Juulcfa460a2007-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 Denkac7eb8a2005-09-14 23:53:32 +0200727 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200728 if (page_addr != -1) {
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200732 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-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 Denk932394a2005-08-17 12:55:25 +0200736 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200737 }
William Juulcfa460a2007-10-31 13:53:06 +0100738 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200739
740 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000741 * Program and erase have their own busy handlers status, sequential
Scott Woodd3963722015-06-26 19:03:26 -0500742 * in and status need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100743 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200744 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200745
Wolfgang Denk932394a2005-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 Juulcfa460a2007-10-31 13:53:06 +0100751 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200752 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100753 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200754
755 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100756 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200757 break;
William Juulcfa460a2007-10-31 13:53:06 +0100758 udelay(chip->chip_delay);
759 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
760 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
761 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
762 NAND_NCE | NAND_CTRL_CHANGE);
Scott Woodd3963722015-06-26 19:03:26 -0500763 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
764 nand_wait_status_ready(mtd, 250);
William Juulcfa460a2007-10-31 13:53:06 +0100765 return;
766
767 case NAND_CMD_RNDOUT:
768 /* No ready / busy check necessary */
769 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
770 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
771 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
772 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200773 return;
774
775 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100776 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
777 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
778 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
779 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200780
William Juulcfa460a2007-10-31 13:53:06 +0100781 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200782 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200783 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200784 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000785 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100786 */
787 if (!chip->dev_ready) {
788 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200789 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200790 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200791 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200792
Sergey Lapindfe64e22013-01-14 03:46:50 +0000793 /*
794 * Apply this short delay always to ensure that we do wait tWB in
795 * any case on any machine.
796 */
William Juulcfa460a2007-10-31 13:53:06 +0100797 ndelay(100);
798
799 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200800}
801
802/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200803 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000804 * @chip: the nand chip descriptor
805 * @mtd: MTD device structure
806 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200807 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200808 * Used when in panic, no locks are taken.
809 */
810static void panic_nand_get_device(struct nand_chip *chip,
811 struct mtd_info *mtd, int new_state)
812{
813 /* Hardware controller shared among independent devices */
814 chip->controller->active = chip;
815 chip->state = new_state;
816}
817
818/**
819 * nand_get_device - [GENERIC] Get chip for selected access
820 * @mtd: MTD device structure
821 * @new_state: the state which is requested
822 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200823 * Get the device and lock it for exclusive access
824 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200825static int
Heiko Schocherff94bc42014-06-24 10:10:04 +0200826nand_get_device(struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100827{
Scott Wood17cb4b82016-05-30 13:57:56 -0500828 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200829 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100830 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200831}
832
833/**
834 * panic_nand_wait - [GENERIC] wait until the command is done
835 * @mtd: MTD device structure
836 * @chip: NAND chip structure
837 * @timeo: timeout
838 *
839 * Wait for command done. This is a helper function for nand_wait used when
840 * we are in interrupt context. May happen when in panic and trying to write
841 * an oops through mtdoops.
842 */
843static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
844 unsigned long timeo)
845{
846 int i;
847 for (i = 0; i < timeo; i++) {
848 if (chip->dev_ready) {
849 if (chip->dev_ready(mtd))
850 break;
851 } else {
852 if (chip->read_byte(mtd) & NAND_STATUS_READY)
853 break;
854 }
855 mdelay(1);
856 }
William Juulcfa460a2007-10-31 13:53:06 +0100857}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200858
859/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000860 * nand_wait - [DEFAULT] wait until the command is done
861 * @mtd: MTD device structure
862 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200863 *
Scott Woodceee07b2016-05-30 13:57:58 -0500864 * Wait for command done. This applies to erase and program only.
William Juulcfa460a2007-10-31 13:53:06 +0100865 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200866static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200867{
Scott Woodceee07b2016-05-30 13:57:58 -0500868 int status;
869 unsigned long timeo = 400;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100870
Heiko Schocherff94bc42014-06-24 10:10:04 +0200871 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100872
Heiko Schocherff94bc42014-06-24 10:10:04 +0200873 /*
874 * Apply this short delay always to ensure that we do wait tWB in any
875 * case on any machine.
876 */
877 ndelay(100);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100878
Heiko Schocherff94bc42014-06-24 10:10:04 +0200879 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
880
Heiko Schocherff94bc42014-06-24 10:10:04 +0200881 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
882 u32 time_start;
883
884 time_start = get_timer(0);
885 while (get_timer(time_start) < timer) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200886 if (chip->dev_ready) {
887 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100888 break;
889 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200890 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100891 break;
892 }
893 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200894 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100895
Heiko Schocherff94bc42014-06-24 10:10:04 +0200896 status = (int)chip->read_byte(mtd);
897 /* This can happen if in case of timeout or buggy dev_ready */
898 WARN_ON(!(status & NAND_STATUS_READY));
899 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200900}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200901
Scott Woodceee07b2016-05-30 13:57:58 -0500902#define BITS_PER_BYTE 8
903
904/**
905 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
906 * @buf: buffer to test
907 * @len: buffer length
908 * @bitflips_threshold: maximum number of bitflips
909 *
910 * Check if a buffer contains only 0xff, which means the underlying region
911 * has been erased and is ready to be programmed.
912 * The bitflips_threshold specify the maximum number of bitflips before
913 * considering the region is not erased.
914 * Note: The logic of this function has been extracted from the memweight
915 * implementation, except that nand_check_erased_buf function exit before
916 * testing the whole buffer if the number of bitflips exceed the
917 * bitflips_threshold value.
918 *
919 * Returns a positive number of bitflips less than or equal to
920 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
921 * threshold.
922 */
923static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
924{
925 const unsigned char *bitmap = buf;
926 int bitflips = 0;
927 int weight;
928
929 for (; len && ((uintptr_t)bitmap) % sizeof(long);
930 len--, bitmap++) {
931 weight = hweight8(*bitmap);
932 bitflips += BITS_PER_BYTE - weight;
933 if (unlikely(bitflips > bitflips_threshold))
934 return -EBADMSG;
935 }
936
937 for (; len >= 4; len -= 4, bitmap += 4) {
938 weight = hweight32(*((u32 *)bitmap));
939 bitflips += 32 - weight;
940 if (unlikely(bitflips > bitflips_threshold))
941 return -EBADMSG;
942 }
943
944 for (; len > 0; len--, bitmap++) {
945 weight = hweight8(*bitmap);
946 bitflips += BITS_PER_BYTE - weight;
947 if (unlikely(bitflips > bitflips_threshold))
948 return -EBADMSG;
949 }
950
951 return bitflips;
952}
953
954/**
955 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
956 * 0xff data
957 * @data: data buffer to test
958 * @datalen: data length
959 * @ecc: ECC buffer
960 * @ecclen: ECC length
961 * @extraoob: extra OOB buffer
962 * @extraooblen: extra OOB length
963 * @bitflips_threshold: maximum number of bitflips
964 *
965 * Check if a data buffer and its associated ECC and OOB data contains only
966 * 0xff pattern, which means the underlying region has been erased and is
967 * ready to be programmed.
968 * The bitflips_threshold specify the maximum number of bitflips before
969 * considering the region as not erased.
970 *
971 * Note:
972 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
973 * different from the NAND page size. When fixing bitflips, ECC engines will
974 * report the number of errors per chunk, and the NAND core infrastructure
975 * expect you to return the maximum number of bitflips for the whole page.
976 * This is why you should always use this function on a single chunk and
977 * not on the whole page. After checking each chunk you should update your
978 * max_bitflips value accordingly.
979 * 2/ When checking for bitflips in erased pages you should not only check
980 * the payload data but also their associated ECC data, because a user might
981 * have programmed almost all bits to 1 but a few. In this case, we
982 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
983 * this case.
984 * 3/ The extraoob argument is optional, and should be used if some of your OOB
985 * data are protected by the ECC engine.
986 * It could also be used if you support subpages and want to attach some
987 * extra OOB data to an ECC chunk.
988 *
989 * Returns a positive number of bitflips less than or equal to
990 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
991 * threshold. In case of success, the passed buffers are filled with 0xff.
992 */
993int nand_check_erased_ecc_chunk(void *data, int datalen,
994 void *ecc, int ecclen,
995 void *extraoob, int extraooblen,
996 int bitflips_threshold)
997{
998 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
999
1000 data_bitflips = nand_check_erased_buf(data, datalen,
1001 bitflips_threshold);
1002 if (data_bitflips < 0)
1003 return data_bitflips;
1004
1005 bitflips_threshold -= data_bitflips;
1006
1007 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1008 if (ecc_bitflips < 0)
1009 return ecc_bitflips;
1010
1011 bitflips_threshold -= ecc_bitflips;
1012
1013 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1014 bitflips_threshold);
1015 if (extraoob_bitflips < 0)
1016 return extraoob_bitflips;
1017
1018 if (data_bitflips)
1019 memset(data, 0xff, datalen);
1020
1021 if (ecc_bitflips)
1022 memset(ecc, 0xff, ecclen);
1023
1024 if (extraoob_bitflips)
1025 memset(extraoob, 0xff, extraooblen);
1026
1027 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1028}
1029EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1030
Wolfgang Denk932394a2005-08-17 12:55:25 +02001031/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001032 * nand_read_page_raw - [INTERN] read raw page data without ecc
1033 * @mtd: mtd info structure
1034 * @chip: nand chip info structure
1035 * @buf: buffer to store read data
1036 * @oob_required: caller requires OOB data read to chip->oob_poi
1037 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001038 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001039 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001040 */
William Juulcfa460a2007-10-31 13:53:06 +01001041static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001042 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001043{
William Juulcfa460a2007-10-31 13:53:06 +01001044 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001045 if (oob_required)
1046 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01001047 return 0;
1048}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001049
William Juulcfa460a2007-10-31 13:53:06 +01001050/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001051 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1052 * @mtd: mtd info structure
1053 * @chip: nand chip info structure
1054 * @buf: buffer to store read data
1055 * @oob_required: caller requires OOB data read to chip->oob_poi
1056 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001057 *
1058 * We need a special oob layout and handling even when OOB isn't used.
1059 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001060static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001061 struct nand_chip *chip, uint8_t *buf,
1062 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -05001063{
1064 int eccsize = chip->ecc.size;
1065 int eccbytes = chip->ecc.bytes;
1066 uint8_t *oob = chip->oob_poi;
1067 int steps, size;
1068
1069 for (steps = chip->ecc.steps; steps > 0; steps--) {
1070 chip->read_buf(mtd, buf, eccsize);
1071 buf += eccsize;
1072
1073 if (chip->ecc.prepad) {
1074 chip->read_buf(mtd, oob, chip->ecc.prepad);
1075 oob += chip->ecc.prepad;
1076 }
1077
1078 chip->read_buf(mtd, oob, eccbytes);
1079 oob += eccbytes;
1080
1081 if (chip->ecc.postpad) {
1082 chip->read_buf(mtd, oob, chip->ecc.postpad);
1083 oob += chip->ecc.postpad;
1084 }
1085 }
1086
1087 size = mtd->oobsize - (oob - chip->oob_poi);
1088 if (size)
1089 chip->read_buf(mtd, oob, size);
1090
1091 return 0;
1092}
1093
1094/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001095 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1096 * @mtd: mtd info structure
1097 * @chip: nand chip info structure
1098 * @buf: buffer to store read data
1099 * @oob_required: caller requires OOB data read to chip->oob_poi
1100 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001101 */
1102static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001103 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001104{
1105 int i, eccsize = chip->ecc.size;
1106 int eccbytes = chip->ecc.bytes;
1107 int eccsteps = chip->ecc.steps;
1108 uint8_t *p = buf;
1109 uint8_t *ecc_calc = chip->buffers->ecccalc;
1110 uint8_t *ecc_code = chip->buffers->ecccode;
1111 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001112 unsigned int max_bitflips = 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001113
Sergey Lapindfe64e22013-01-14 03:46:50 +00001114 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001115
William Juulcfa460a2007-10-31 13:53:06 +01001116 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1117 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001118
William Juulcfa460a2007-10-31 13:53:06 +01001119 for (i = 0; i < chip->ecc.total; i++)
1120 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001121
William Juulcfa460a2007-10-31 13:53:06 +01001122 eccsteps = chip->ecc.steps;
1123 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001124
William Juulcfa460a2007-10-31 13:53:06 +01001125 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1126 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001127
William Juulcfa460a2007-10-31 13:53:06 +01001128 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001129 if (stat < 0) {
Scott Woodc45912d2008-10-24 16:20:43 -05001130 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001131 } else {
Scott Woodc45912d2008-10-24 16:20:43 -05001132 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001133 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1134 }
Scott Woodc45912d2008-10-24 16:20:43 -05001135 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001136 return max_bitflips;
Scott Woodc45912d2008-10-24 16:20:43 -05001137}
1138
1139/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001140 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapindfe64e22013-01-14 03:46:50 +00001141 * @mtd: mtd info structure
1142 * @chip: nand chip info structure
1143 * @data_offs: offset of requested data within the page
1144 * @readlen: data length
1145 * @bufpoi: buffer to store read data
Heiko Schocher4e67c572014-07-15 16:08:43 +02001146 * @page: page number to read
Scott Woodc45912d2008-10-24 16:20:43 -05001147 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001148static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001149 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1150 int page)
Scott Woodc45912d2008-10-24 16:20:43 -05001151{
1152 int start_step, end_step, num_steps;
1153 uint32_t *eccpos = chip->ecc.layout->eccpos;
1154 uint8_t *p;
1155 int data_col_addr, i, gaps = 0;
1156 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1157 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001158 int index;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001159 unsigned int max_bitflips = 0;
Scott Woodc45912d2008-10-24 16:20:43 -05001160
Sergey Lapindfe64e22013-01-14 03:46:50 +00001161 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -05001162 start_step = data_offs / chip->ecc.size;
1163 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1164 num_steps = end_step - start_step + 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001165 index = start_step * chip->ecc.bytes;
Scott Woodc45912d2008-10-24 16:20:43 -05001166
Sergey Lapindfe64e22013-01-14 03:46:50 +00001167 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -05001168 datafrag_len = num_steps * chip->ecc.size;
1169 eccfrag_len = num_steps * chip->ecc.bytes;
1170
1171 data_col_addr = start_step * chip->ecc.size;
1172 /* If we read not a page aligned data */
1173 if (data_col_addr != 0)
1174 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1175
1176 p = bufpoi + data_col_addr;
1177 chip->read_buf(mtd, p, datafrag_len);
1178
Sergey Lapindfe64e22013-01-14 03:46:50 +00001179 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -05001180 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1181 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1182
Sergey Lapindfe64e22013-01-14 03:46:50 +00001183 /*
1184 * The performance is faster if we position offsets according to
1185 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1186 */
Scott Woodc45912d2008-10-24 16:20:43 -05001187 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Woodd3963722015-06-26 19:03:26 -05001188 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Woodc45912d2008-10-24 16:20:43 -05001189 gaps = 1;
1190 break;
1191 }
1192 }
1193 if (gaps) {
1194 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1195 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1196 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001197 /*
1198 * Send the command to read the particular ECC bytes take care
1199 * about buswidth alignment in read_buf.
1200 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001201 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -05001202 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001203 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001204 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001205 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001206 aligned_len++;
1207
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001208 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1209 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001210 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1211 }
1212
1213 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001214 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001215
1216 p = bufpoi + data_col_addr;
1217 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1218 int stat;
1219
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001220 stat = chip->ecc.correct(mtd, p,
1221 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Woodceee07b2016-05-30 13:57:58 -05001222 if (stat == -EBADMSG &&
1223 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1224 /* check for empty pages with bitflips */
1225 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1226 &chip->buffers->ecccode[i],
1227 chip->ecc.bytes,
1228 NULL, 0,
1229 chip->ecc.strength);
1230 }
1231
Heiko Schocherff94bc42014-06-24 10:10:04 +02001232 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001233 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001234 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001235 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001236 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1237 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001238 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001239 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001240}
1241
Wolfgang Denk932394a2005-08-17 12:55:25 +02001242/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001243 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1244 * @mtd: mtd info structure
1245 * @chip: nand chip info structure
1246 * @buf: buffer to store read data
1247 * @oob_required: caller requires OOB data read to chip->oob_poi
1248 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001249 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001250 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001251 */
William Juulcfa460a2007-10-31 13:53:06 +01001252static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001253 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001254{
William Juulcfa460a2007-10-31 13:53:06 +01001255 int i, eccsize = chip->ecc.size;
1256 int eccbytes = chip->ecc.bytes;
1257 int eccsteps = chip->ecc.steps;
1258 uint8_t *p = buf;
1259 uint8_t *ecc_calc = chip->buffers->ecccalc;
1260 uint8_t *ecc_code = chip->buffers->ecccode;
1261 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001262 unsigned int max_bitflips = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001263
William Juulcfa460a2007-10-31 13:53:06 +01001264 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1265 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1266 chip->read_buf(mtd, p, eccsize);
1267 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1268 }
1269 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001270
William Juulcfa460a2007-10-31 13:53:06 +01001271 for (i = 0; i < chip->ecc.total; i++)
1272 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001273
William Juulcfa460a2007-10-31 13:53:06 +01001274 eccsteps = chip->ecc.steps;
1275 p = buf;
1276
1277 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1278 int stat;
1279
1280 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodceee07b2016-05-30 13:57:58 -05001281 if (stat == -EBADMSG &&
1282 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1283 /* check for empty pages with bitflips */
1284 stat = nand_check_erased_ecc_chunk(p, eccsize,
1285 &ecc_code[i], eccbytes,
1286 NULL, 0,
1287 chip->ecc.strength);
1288 }
1289
Heiko Schocherff94bc42014-06-24 10:10:04 +02001290 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001291 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001292 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001293 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001294 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1295 }
William Juulcfa460a2007-10-31 13:53:06 +01001296 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001297 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001298}
1299
1300/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001301 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1302 * @mtd: mtd info structure
1303 * @chip: nand chip info structure
1304 * @buf: buffer to store read data
1305 * @oob_required: caller requires OOB data read to chip->oob_poi
1306 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001307 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001308 * Hardware ECC for large page chips, require OOB to be read first. For this
1309 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1310 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1311 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1312 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001313 */
1314static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001315 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001316{
1317 int i, eccsize = chip->ecc.size;
1318 int eccbytes = chip->ecc.bytes;
1319 int eccsteps = chip->ecc.steps;
1320 uint8_t *p = buf;
1321 uint8_t *ecc_code = chip->buffers->ecccode;
1322 uint32_t *eccpos = chip->ecc.layout->eccpos;
1323 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001324 unsigned int max_bitflips = 0;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001325
1326 /* Read the OOB area first */
1327 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1328 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1329 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1330
1331 for (i = 0; i < chip->ecc.total; i++)
1332 ecc_code[i] = chip->oob_poi[eccpos[i]];
1333
1334 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1335 int stat;
1336
1337 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1338 chip->read_buf(mtd, p, eccsize);
1339 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1340
1341 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Woodceee07b2016-05-30 13:57:58 -05001342 if (stat == -EBADMSG &&
1343 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1344 /* check for empty pages with bitflips */
1345 stat = nand_check_erased_ecc_chunk(p, eccsize,
1346 &ecc_code[i], eccbytes,
1347 NULL, 0,
1348 chip->ecc.strength);
1349 }
1350
Heiko Schocherff94bc42014-06-24 10:10:04 +02001351 if (stat < 0) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001352 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001353 } else {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001354 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001355 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1356 }
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001357 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001358 return max_bitflips;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001359}
1360
1361/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001362 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1363 * @mtd: mtd info structure
1364 * @chip: nand chip info structure
1365 * @buf: buffer to store read data
1366 * @oob_required: caller requires OOB data read to chip->oob_poi
1367 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001368 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001369 * The hw generator calculates the error syndrome automatically. Therefore we
1370 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001371 */
1372static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001373 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001374{
1375 int i, eccsize = chip->ecc.size;
1376 int eccbytes = chip->ecc.bytes;
1377 int eccsteps = chip->ecc.steps;
Scott Woodceee07b2016-05-30 13:57:58 -05001378 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juulcfa460a2007-10-31 13:53:06 +01001379 uint8_t *p = buf;
1380 uint8_t *oob = chip->oob_poi;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001381 unsigned int max_bitflips = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001382
1383 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1384 int stat;
1385
1386 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1387 chip->read_buf(mtd, p, eccsize);
1388
1389 if (chip->ecc.prepad) {
1390 chip->read_buf(mtd, oob, chip->ecc.prepad);
1391 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001392 }
1393
William Juulcfa460a2007-10-31 13:53:06 +01001394 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1395 chip->read_buf(mtd, oob, eccbytes);
1396 stat = chip->ecc.correct(mtd, p, oob, NULL);
1397
William Juulcfa460a2007-10-31 13:53:06 +01001398 oob += eccbytes;
1399
1400 if (chip->ecc.postpad) {
1401 chip->read_buf(mtd, oob, chip->ecc.postpad);
1402 oob += chip->ecc.postpad;
1403 }
Scott Woodceee07b2016-05-30 13:57:58 -05001404
1405 if (stat == -EBADMSG &&
1406 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1407 /* check for empty pages with bitflips */
1408 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1409 oob - eccpadbytes,
1410 eccpadbytes,
1411 NULL, 0,
1412 chip->ecc.strength);
1413 }
1414
1415 if (stat < 0) {
1416 mtd->ecc_stats.failed++;
1417 } else {
1418 mtd->ecc_stats.corrected += stat;
1419 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1420 }
William Juulcfa460a2007-10-31 13:53:06 +01001421 }
1422
1423 /* Calculate remaining oob bytes */
1424 i = mtd->oobsize - (oob - chip->oob_poi);
1425 if (i)
1426 chip->read_buf(mtd, oob, i);
1427
Heiko Schocherff94bc42014-06-24 10:10:04 +02001428 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001429}
1430
1431/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001432 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1433 * @chip: nand chip structure
1434 * @oob: oob destination address
1435 * @ops: oob ops structure
1436 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001437 */
1438static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1439 struct mtd_oob_ops *ops, size_t len)
1440{
Christian Hitz90e3f392011-10-12 09:32:01 +02001441 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001442
Sergey Lapindfe64e22013-01-14 03:46:50 +00001443 case MTD_OPS_PLACE_OOB:
1444 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001445 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1446 return oob + len;
1447
Sergey Lapindfe64e22013-01-14 03:46:50 +00001448 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001449 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1450 uint32_t boffs = 0, roffs = ops->ooboffs;
1451 size_t bytes = 0;
1452
Christian Hitz90e3f392011-10-12 09:32:01 +02001453 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001454 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001455 if (unlikely(roffs)) {
1456 if (roffs >= free->length) {
1457 roffs -= free->length;
1458 continue;
1459 }
1460 boffs = free->offset + roffs;
1461 bytes = min_t(size_t, len,
1462 (free->length - roffs));
1463 roffs = 0;
1464 } else {
1465 bytes = min_t(size_t, len, free->length);
1466 boffs = free->offset;
1467 }
1468 memcpy(oob, chip->oob_poi + boffs, bytes);
1469 oob += bytes;
1470 }
1471 return oob;
1472 }
1473 default:
1474 BUG();
1475 }
1476 return NULL;
1477}
1478
1479/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001480 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1481 * @mtd: MTD device structure
1482 * @retry_mode: the retry mode to use
1483 *
1484 * Some vendors supply a special command to shift the Vt threshold, to be used
1485 * when there are too many bitflips in a page (i.e., ECC error). After setting
1486 * a new threshold, the host should retry reading the page.
1487 */
1488static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1489{
Scott Wood17cb4b82016-05-30 13:57:56 -05001490 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001491
1492 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1493
1494 if (retry_mode >= chip->read_retries)
1495 return -EINVAL;
1496
1497 if (!chip->setup_read_retry)
1498 return -EOPNOTSUPP;
1499
1500 return chip->setup_read_retry(mtd, retry_mode);
1501}
1502
1503/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001504 * nand_do_read_ops - [INTERN] Read data with ECC
1505 * @mtd: MTD device structure
1506 * @from: offset to read from
1507 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001508 *
1509 * Internal function. Called with chip held.
1510 */
1511static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1512 struct mtd_oob_ops *ops)
1513{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001514 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17cb4b82016-05-30 13:57:56 -05001515 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01001516 int ret = 0;
1517 uint32_t readlen = ops->len;
1518 uint32_t oobreadlen = ops->ooblen;
Scott Woodceee07b2016-05-30 13:57:58 -05001519 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001520
William Juulcfa460a2007-10-31 13:53:06 +01001521 uint8_t *bufpoi, *oob, *buf;
Scott Woodd3963722015-06-26 19:03:26 -05001522 int use_bufpoi;
Paul Burton40462e52013-09-04 15:16:56 +01001523 unsigned int max_bitflips = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001524 int retry_mode = 0;
1525 bool ecc_fail = false;
William Juulcfa460a2007-10-31 13:53:06 +01001526
1527 chipnr = (int)(from >> chip->chip_shift);
1528 chip->select_chip(mtd, chipnr);
1529
1530 realpage = (int)(from >> chip->page_shift);
1531 page = realpage & chip->pagemask;
1532
1533 col = (int)(from & (mtd->writesize - 1));
1534
1535 buf = ops->datbuf;
1536 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001537 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001538
Christian Hitz90e3f392011-10-12 09:32:01 +02001539 while (1) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001540 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Wood6f2ffc32011-02-02 18:15:57 -06001541
Heiko Schocherff94bc42014-06-24 10:10:04 +02001542 WATCHDOG_RESET();
William Juulcfa460a2007-10-31 13:53:06 +01001543 bytes = min(mtd->writesize - col, readlen);
1544 aligned = (bytes == mtd->writesize);
1545
Scott Woodd3963722015-06-26 19:03:26 -05001546 if (!aligned)
1547 use_bufpoi = 1;
1548 else
1549 use_bufpoi = 0;
1550
Sergey Lapindfe64e22013-01-14 03:46:50 +00001551 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001552 if (realpage != chip->pagebuf || oob) {
Scott Woodd3963722015-06-26 19:03:26 -05001553 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1554
1555 if (use_bufpoi && aligned)
1556 pr_debug("%s: using read bounce buffer for buf@%p\n",
1557 __func__, buf);
William Juulcfa460a2007-10-31 13:53:06 +01001558
Heiko Schocherff94bc42014-06-24 10:10:04 +02001559read_retry:
Sergey Lapindfe64e22013-01-14 03:46:50 +00001560 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001561
Paul Burton40462e52013-09-04 15:16:56 +01001562 /*
1563 * Now read the page into the buffer. Absent an error,
1564 * the read methods return max bitflips per ecc step.
1565 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001566 if (unlikely(ops->mode == MTD_OPS_RAW))
1567 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1568 oob_required,
1569 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001570 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001571 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001572 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001573 col, bytes, bufpoi,
1574 page);
William Juulcfa460a2007-10-31 13:53:06 +01001575 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001576 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001577 oob_required, page);
1578 if (ret < 0) {
Scott Woodd3963722015-06-26 19:03:26 -05001579 if (use_bufpoi)
Sergey Lapindfe64e22013-01-14 03:46:50 +00001580 /* Invalidate page cache */
1581 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001582 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001583 }
William Juulcfa460a2007-10-31 13:53:06 +01001584
Paul Burton40462e52013-09-04 15:16:56 +01001585 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1586
William Juulcfa460a2007-10-31 13:53:06 +01001587 /* Transfer not aligned data */
Scott Woodd3963722015-06-26 19:03:26 -05001588 if (use_bufpoi) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001589 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001590 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton40462e52013-09-04 15:16:56 +01001591 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001592 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001593 chip->pagebuf_bitflips = ret;
1594 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001595 /* Invalidate page cache */
1596 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001597 }
William Juulcfa460a2007-10-31 13:53:06 +01001598 memcpy(buf, chip->buffers->databuf + col, bytes);
1599 }
1600
William Juulcfa460a2007-10-31 13:53:06 +01001601 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001602 int toread = min(oobreadlen, max_oobsize);
1603
1604 if (toread) {
1605 oob = nand_transfer_oob(chip,
1606 oob, ops, toread);
1607 oobreadlen -= toread;
1608 }
William Juulcfa460a2007-10-31 13:53:06 +01001609 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001610
1611 if (chip->options & NAND_NEED_READRDY) {
1612 /* Apply delay or wait for ready/busy pin */
1613 if (!chip->dev_ready)
1614 udelay(chip->chip_delay);
1615 else
1616 nand_wait_ready(mtd);
1617 }
1618
1619 if (mtd->ecc_stats.failed - ecc_failures) {
1620 if (retry_mode + 1 < chip->read_retries) {
1621 retry_mode++;
1622 ret = nand_setup_read_retry(mtd,
1623 retry_mode);
1624 if (ret < 0)
1625 break;
1626
1627 /* Reset failures; retry */
1628 mtd->ecc_stats.failed = ecc_failures;
1629 goto read_retry;
1630 } else {
1631 /* No more retry modes; real failure */
1632 ecc_fail = true;
1633 }
1634 }
1635
1636 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001637 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001638 memcpy(buf, chip->buffers->databuf + col, bytes);
1639 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001640 max_bitflips = max_t(unsigned int, max_bitflips,
1641 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001642 }
1643
William Juulcfa460a2007-10-31 13:53:06 +01001644 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001645
Heiko Schocherff94bc42014-06-24 10:10:04 +02001646 /* Reset to retry mode 0 */
1647 if (retry_mode) {
1648 ret = nand_setup_read_retry(mtd, 0);
1649 if (ret < 0)
1650 break;
1651 retry_mode = 0;
1652 }
1653
William Juulcfa460a2007-10-31 13:53:06 +01001654 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001655 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001656
Sergey Lapindfe64e22013-01-14 03:46:50 +00001657 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001658 col = 0;
1659 /* Increment page address */
1660 realpage++;
1661
William Juulcfa460a2007-10-31 13:53:06 +01001662 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001663 /* Check, if we cross a chip boundary */
1664 if (!page) {
1665 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001666 chip->select_chip(mtd, -1);
1667 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001668 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001669 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001670 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001671
William Juulcfa460a2007-10-31 13:53:06 +01001672 ops->retlen = ops->len - (size_t) readlen;
1673 if (oob)
1674 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001675
Heiko Schocherff94bc42014-06-24 10:10:04 +02001676 if (ret < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001677 return ret;
1678
Heiko Schocherff94bc42014-06-24 10:10:04 +02001679 if (ecc_fail)
William Juulcfa460a2007-10-31 13:53:06 +01001680 return -EBADMSG;
1681
Paul Burton40462e52013-09-04 15:16:56 +01001682 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001683}
1684
1685/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001686 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001687 * @mtd: MTD device structure
1688 * @from: offset to read from
1689 * @len: number of bytes to read
1690 * @retlen: pointer to variable to store the number of read bytes
1691 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001692 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001693 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001694 */
William Juulcfa460a2007-10-31 13:53:06 +01001695static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1696 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001697{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001698 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001699 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001700
Heiko Schocherff94bc42014-06-24 10:10:04 +02001701 nand_get_device(mtd, FL_READING);
Scott Woodd3963722015-06-26 19:03:26 -05001702 memset(&ops, 0, sizeof(ops));
Sergey Lapindfe64e22013-01-14 03:46:50 +00001703 ops.len = len;
1704 ops.datbuf = buf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001705 ops.mode = MTD_OPS_PLACE_OOB;
1706 ret = nand_do_read_ops(mtd, from, &ops);
1707 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001708 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001709 return ret;
1710}
1711
William Juulcfa460a2007-10-31 13:53:06 +01001712/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001713 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1714 * @mtd: mtd info structure
1715 * @chip: nand chip info structure
1716 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001717 */
1718static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001719 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001720{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001721 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001722 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001723 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001724}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001725
1726/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001727 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001728 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001729 * @mtd: mtd info structure
1730 * @chip: nand chip info structure
1731 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001732 */
1733static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001734 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001735{
William Juulcfa460a2007-10-31 13:53:06 +01001736 int length = mtd->oobsize;
1737 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1738 int eccsize = chip->ecc.size;
Scott Woodd3963722015-06-26 19:03:26 -05001739 uint8_t *bufpoi = chip->oob_poi;
William Juulcfa460a2007-10-31 13:53:06 +01001740 int i, toread, sndrnd = 0, pos;
1741
1742 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1743 for (i = 0; i < chip->ecc.steps; i++) {
1744 if (sndrnd) {
1745 pos = eccsize + i * (eccsize + chunk);
1746 if (mtd->writesize > 512)
1747 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1748 else
1749 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1750 } else
1751 sndrnd = 1;
1752 toread = min_t(int, length, chunk);
1753 chip->read_buf(mtd, bufpoi, toread);
1754 bufpoi += toread;
1755 length -= toread;
1756 }
1757 if (length > 0)
1758 chip->read_buf(mtd, bufpoi, length);
1759
Sergey Lapindfe64e22013-01-14 03:46:50 +00001760 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001761}
1762
1763/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001764 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1765 * @mtd: mtd info structure
1766 * @chip: nand chip info structure
1767 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001768 */
1769static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1770 int page)
1771{
1772 int status = 0;
1773 const uint8_t *buf = chip->oob_poi;
1774 int length = mtd->oobsize;
1775
1776 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1777 chip->write_buf(mtd, buf, length);
1778 /* Send command to program the OOB data */
1779 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1780
1781 status = chip->waitfunc(mtd, chip);
1782
1783 return status & NAND_STATUS_FAIL ? -EIO : 0;
1784}
1785
1786/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001787 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1788 * with syndrome - only for large page flash
1789 * @mtd: mtd info structure
1790 * @chip: nand chip info structure
1791 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001792 */
1793static int nand_write_oob_syndrome(struct mtd_info *mtd,
1794 struct nand_chip *chip, int page)
1795{
1796 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1797 int eccsize = chip->ecc.size, length = mtd->oobsize;
1798 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1799 const uint8_t *bufpoi = chip->oob_poi;
1800
1801 /*
1802 * data-ecc-data-ecc ... ecc-oob
1803 * or
1804 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1805 */
1806 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1807 pos = steps * (eccsize + chunk);
1808 steps = 0;
1809 } else
1810 pos = eccsize;
1811
1812 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1813 for (i = 0; i < steps; i++) {
1814 if (sndcmd) {
1815 if (mtd->writesize <= 512) {
1816 uint32_t fill = 0xFFFFFFFF;
1817
1818 len = eccsize;
1819 while (len > 0) {
1820 int num = min_t(int, len, 4);
1821 chip->write_buf(mtd, (uint8_t *)&fill,
1822 num);
1823 len -= num;
1824 }
1825 } else {
1826 pos = eccsize + i * (eccsize + chunk);
1827 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1828 }
1829 } else
1830 sndcmd = 1;
1831 len = min_t(int, length, chunk);
1832 chip->write_buf(mtd, bufpoi, len);
1833 bufpoi += len;
1834 length -= len;
1835 }
1836 if (length > 0)
1837 chip->write_buf(mtd, bufpoi, length);
1838
1839 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1840 status = chip->waitfunc(mtd, chip);
1841
1842 return status & NAND_STATUS_FAIL ? -EIO : 0;
1843}
1844
1845/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001846 * nand_do_read_oob - [INTERN] NAND read out-of-band
1847 * @mtd: MTD device structure
1848 * @from: offset to read from
1849 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001850 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001851 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01001852 */
1853static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1854 struct mtd_oob_ops *ops)
1855{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001856 int page, realpage, chipnr;
Scott Wood17cb4b82016-05-30 13:57:56 -05001857 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001858 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001859 int readlen = ops->ooblen;
1860 int len;
1861 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001862 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001863
Heiko Schocherff94bc42014-06-24 10:10:04 +02001864 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02001865 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001866
Sergey Lapindfe64e22013-01-14 03:46:50 +00001867 stats = mtd->ecc_stats;
1868
Scott Woodceee07b2016-05-30 13:57:58 -05001869 len = mtd_oobavail(mtd, ops);
William Juulcfa460a2007-10-31 13:53:06 +01001870
1871 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001872 pr_debug("%s: attempt to start read outside oob\n",
1873 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001874 return -EINVAL;
1875 }
1876
1877 /* Do not allow reads past end of device */
1878 if (unlikely(from >= mtd->size ||
1879 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1880 (from >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001881 pr_debug("%s: attempt to read beyond end of device\n",
1882 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001883 return -EINVAL;
1884 }
1885
1886 chipnr = (int)(from >> chip->chip_shift);
1887 chip->select_chip(mtd, chipnr);
1888
1889 /* Shift to get page */
1890 realpage = (int)(from >> chip->page_shift);
1891 page = realpage & chip->pagemask;
1892
Christian Hitz90e3f392011-10-12 09:32:01 +02001893 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001894 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02001895
Sergey Lapindfe64e22013-01-14 03:46:50 +00001896 if (ops->mode == MTD_OPS_RAW)
1897 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1898 else
1899 ret = chip->ecc.read_oob(mtd, chip, page);
1900
1901 if (ret < 0)
1902 break;
William Juulcfa460a2007-10-31 13:53:06 +01001903
1904 len = min(len, readlen);
1905 buf = nand_transfer_oob(chip, buf, ops, len);
1906
Heiko Schocherff94bc42014-06-24 10:10:04 +02001907 if (chip->options & NAND_NEED_READRDY) {
1908 /* Apply delay or wait for ready/busy pin */
1909 if (!chip->dev_ready)
1910 udelay(chip->chip_delay);
1911 else
1912 nand_wait_ready(mtd);
1913 }
1914
William Juulcfa460a2007-10-31 13:53:06 +01001915 readlen -= len;
1916 if (!readlen)
1917 break;
1918
1919 /* Increment page address */
1920 realpage++;
1921
1922 page = realpage & chip->pagemask;
1923 /* Check, if we cross a chip boundary */
1924 if (!page) {
1925 chipnr++;
1926 chip->select_chip(mtd, -1);
1927 chip->select_chip(mtd, chipnr);
1928 }
William Juulcfa460a2007-10-31 13:53:06 +01001929 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001930 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01001931
Sergey Lapindfe64e22013-01-14 03:46:50 +00001932 ops->oobretlen = ops->ooblen - readlen;
1933
1934 if (ret < 0)
1935 return ret;
1936
1937 if (mtd->ecc_stats.failed - stats.failed)
1938 return -EBADMSG;
1939
1940 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001941}
1942
1943/**
1944 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00001945 * @mtd: MTD device structure
1946 * @from: offset to read from
1947 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01001948 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001949 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01001950 */
1951static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1952 struct mtd_oob_ops *ops)
1953{
William Juulcfa460a2007-10-31 13:53:06 +01001954 int ret = -ENOTSUPP;
1955
1956 ops->retlen = 0;
1957
1958 /* Do not allow reads past end of device */
1959 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001960 pr_debug("%s: attempt to read beyond end of device\n",
1961 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001962 return -EINVAL;
1963 }
1964
Heiko Schocherff94bc42014-06-24 10:10:04 +02001965 nand_get_device(mtd, FL_READING);
William Juulcfa460a2007-10-31 13:53:06 +01001966
Christian Hitz90e3f392011-10-12 09:32:01 +02001967 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001968 case MTD_OPS_PLACE_OOB:
1969 case MTD_OPS_AUTO_OOB:
1970 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001971 break;
1972
1973 default:
1974 goto out;
1975 }
1976
1977 if (!ops->datbuf)
1978 ret = nand_do_read_oob(mtd, from, ops);
1979 else
1980 ret = nand_do_read_ops(mtd, from, ops);
1981
Christian Hitz90e3f392011-10-12 09:32:01 +02001982out:
William Juulcfa460a2007-10-31 13:53:06 +01001983 nand_release_device(mtd);
1984 return ret;
1985}
1986
1987
1988/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001989 * nand_write_page_raw - [INTERN] raw page write function
1990 * @mtd: mtd info structure
1991 * @chip: nand chip info structure
1992 * @buf: data buffer
1993 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05001994 * @page: page number to write
David Brownell7e866612009-11-07 16:27:01 -05001995 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001996 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01001997 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001998static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Woodceee07b2016-05-30 13:57:58 -05001999 const uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01002000{
2001 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002002 if (oob_required)
2003 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2004
2005 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002006}
2007
2008/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002009 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2010 * @mtd: mtd info structure
2011 * @chip: nand chip info structure
2012 * @buf: data buffer
2013 * @oob_required: must write chip->oob_poi to OOB
Scott Woodceee07b2016-05-30 13:57:58 -05002014 * @page: page number to write
David Brownell7e866612009-11-07 16:27:01 -05002015 *
2016 * We need a special oob layout and handling even when ECC isn't checked.
2017 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002018static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02002019 struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -05002020 const uint8_t *buf, int oob_required,
2021 int page)
David Brownell7e866612009-11-07 16:27:01 -05002022{
2023 int eccsize = chip->ecc.size;
2024 int eccbytes = chip->ecc.bytes;
2025 uint8_t *oob = chip->oob_poi;
2026 int steps, size;
2027
2028 for (steps = chip->ecc.steps; steps > 0; steps--) {
2029 chip->write_buf(mtd, buf, eccsize);
2030 buf += eccsize;
2031
2032 if (chip->ecc.prepad) {
2033 chip->write_buf(mtd, oob, chip->ecc.prepad);
2034 oob += chip->ecc.prepad;
2035 }
2036
Heiko Schocher4e67c572014-07-15 16:08:43 +02002037 chip->write_buf(mtd, oob, eccbytes);
David Brownell7e866612009-11-07 16:27:01 -05002038 oob += eccbytes;
2039
2040 if (chip->ecc.postpad) {
2041 chip->write_buf(mtd, oob, chip->ecc.postpad);
2042 oob += chip->ecc.postpad;
2043 }
2044 }
2045
2046 size = mtd->oobsize - (oob - chip->oob_poi);
2047 if (size)
2048 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002049
2050 return 0;
David Brownell7e866612009-11-07 16:27:01 -05002051}
2052/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002053 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2054 * @mtd: mtd info structure
2055 * @chip: nand chip info structure
2056 * @buf: data buffer
2057 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05002058 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01002059 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002060static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Woodceee07b2016-05-30 13:57:58 -05002061 const uint8_t *buf, int oob_required,
2062 int page)
William Juulcfa460a2007-10-31 13:53:06 +01002063{
2064 int i, eccsize = chip->ecc.size;
2065 int eccbytes = chip->ecc.bytes;
2066 int eccsteps = chip->ecc.steps;
2067 uint8_t *ecc_calc = chip->buffers->ecccalc;
2068 const uint8_t *p = buf;
2069 uint32_t *eccpos = chip->ecc.layout->eccpos;
2070
Sergey Lapindfe64e22013-01-14 03:46:50 +00002071 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01002072 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2073 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2074
2075 for (i = 0; i < chip->ecc.total; i++)
2076 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2077
Scott Wood81c77252016-05-30 13:57:57 -05002078 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
William Juulcfa460a2007-10-31 13:53:06 +01002079}
2080
2081/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002082 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2083 * @mtd: mtd info structure
2084 * @chip: nand chip info structure
2085 * @buf: data buffer
2086 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05002087 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01002088 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002089static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -05002090 const uint8_t *buf, int oob_required,
2091 int page)
William Juulcfa460a2007-10-31 13:53:06 +01002092{
2093 int i, eccsize = chip->ecc.size;
2094 int eccbytes = chip->ecc.bytes;
2095 int eccsteps = chip->ecc.steps;
2096 uint8_t *ecc_calc = chip->buffers->ecccalc;
2097 const uint8_t *p = buf;
2098 uint32_t *eccpos = chip->ecc.layout->eccpos;
2099
2100 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2101 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2102 chip->write_buf(mtd, p, eccsize);
2103 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2104 }
2105
2106 for (i = 0; i < chip->ecc.total; i++)
2107 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2108
2109 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002110
2111 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002112}
2113
Heiko Schocherff94bc42014-06-24 10:10:04 +02002114
2115/**
Scott Woodd3963722015-06-26 19:03:26 -05002116 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherff94bc42014-06-24 10:10:04 +02002117 * @mtd: mtd info structure
2118 * @chip: nand chip info structure
2119 * @offset: column address of subpage within the page
2120 * @data_len: data length
2121 * @buf: data buffer
2122 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05002123 * @page: page number to write
Heiko Schocherff94bc42014-06-24 10:10:04 +02002124 */
2125static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2126 struct nand_chip *chip, uint32_t offset,
2127 uint32_t data_len, const uint8_t *buf,
Scott Wood81c77252016-05-30 13:57:57 -05002128 int oob_required, int page)
Heiko Schocherff94bc42014-06-24 10:10:04 +02002129{
2130 uint8_t *oob_buf = chip->oob_poi;
2131 uint8_t *ecc_calc = chip->buffers->ecccalc;
2132 int ecc_size = chip->ecc.size;
2133 int ecc_bytes = chip->ecc.bytes;
2134 int ecc_steps = chip->ecc.steps;
2135 uint32_t *eccpos = chip->ecc.layout->eccpos;
2136 uint32_t start_step = offset / ecc_size;
2137 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2138 int oob_bytes = mtd->oobsize / ecc_steps;
2139 int step, i;
2140
2141 for (step = 0; step < ecc_steps; step++) {
2142 /* configure controller for WRITE access */
2143 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2144
2145 /* write data (untouched subpages already masked by 0xFF) */
2146 chip->write_buf(mtd, buf, ecc_size);
2147
2148 /* mask ECC of un-touched subpages by padding 0xFF */
2149 if ((step < start_step) || (step > end_step))
2150 memset(ecc_calc, 0xff, ecc_bytes);
2151 else
2152 chip->ecc.calculate(mtd, buf, ecc_calc);
2153
2154 /* mask OOB of un-touched subpages by padding 0xFF */
2155 /* if oob_required, preserve OOB metadata of written subpage */
2156 if (!oob_required || (step < start_step) || (step > end_step))
2157 memset(oob_buf, 0xff, oob_bytes);
2158
2159 buf += ecc_size;
2160 ecc_calc += ecc_bytes;
2161 oob_buf += oob_bytes;
2162 }
2163
2164 /* copy calculated ECC for whole page to chip->buffer->oob */
2165 /* this include masked-value(0xFF) for unwritten subpages */
2166 ecc_calc = chip->buffers->ecccalc;
2167 for (i = 0; i < chip->ecc.total; i++)
2168 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2169
2170 /* write OOB buffer to NAND device */
2171 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2172
2173 return 0;
2174}
2175
2176
William Juulcfa460a2007-10-31 13:53:06 +01002177/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002178 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2179 * @mtd: mtd info structure
2180 * @chip: nand chip info structure
2181 * @buf: data buffer
2182 * @oob_required: must write chip->oob_poi to OOB
Scott Woodceee07b2016-05-30 13:57:58 -05002183 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01002184 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002185 * The hw generator calculates the error syndrome automatically. Therefore we
2186 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01002187 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002188static int nand_write_page_syndrome(struct mtd_info *mtd,
2189 struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -05002190 const uint8_t *buf, int oob_required,
2191 int page)
William Juulcfa460a2007-10-31 13:53:06 +01002192{
2193 int i, eccsize = chip->ecc.size;
2194 int eccbytes = chip->ecc.bytes;
2195 int eccsteps = chip->ecc.steps;
2196 const uint8_t *p = buf;
2197 uint8_t *oob = chip->oob_poi;
2198
2199 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2200
2201 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2202 chip->write_buf(mtd, p, eccsize);
2203
2204 if (chip->ecc.prepad) {
2205 chip->write_buf(mtd, oob, chip->ecc.prepad);
2206 oob += chip->ecc.prepad;
2207 }
2208
2209 chip->ecc.calculate(mtd, p, oob);
2210 chip->write_buf(mtd, oob, eccbytes);
2211 oob += eccbytes;
2212
2213 if (chip->ecc.postpad) {
2214 chip->write_buf(mtd, oob, chip->ecc.postpad);
2215 oob += chip->ecc.postpad;
2216 }
2217 }
2218
2219 /* Calculate remaining oob bytes */
2220 i = mtd->oobsize - (oob - chip->oob_poi);
2221 if (i)
2222 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002223
2224 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002225}
2226
2227/**
2228 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00002229 * @mtd: MTD device structure
2230 * @chip: NAND chip descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +02002231 * @offset: address offset within the page
2232 * @data_len: length of actual data to be written
Sergey Lapindfe64e22013-01-14 03:46:50 +00002233 * @buf: the data to write
2234 * @oob_required: must write chip->oob_poi to OOB
2235 * @page: page number to write
2236 * @cached: cached programming
2237 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01002238 */
2239static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02002240 uint32_t offset, int data_len, const uint8_t *buf,
2241 int oob_required, int page, int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01002242{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002243 int status, subpage;
2244
2245 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2246 chip->ecc.write_subpage)
2247 subpage = offset || (data_len < mtd->writesize);
2248 else
2249 subpage = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002250
2251 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2252
2253 if (unlikely(raw))
Heiko Schocherff94bc42014-06-24 10:10:04 +02002254 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood81c77252016-05-30 13:57:57 -05002255 oob_required, page);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002256 else if (subpage)
2257 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Woodceee07b2016-05-30 13:57:58 -05002258 buf, oob_required, page);
William Juulcfa460a2007-10-31 13:53:06 +01002259 else
Scott Wood81c77252016-05-30 13:57:57 -05002260 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2261 page);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002262
2263 if (status < 0)
2264 return status;
William Juulcfa460a2007-10-31 13:53:06 +01002265
2266 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002267 * Cached progamming disabled for now. Not sure if it's worth the
2268 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01002269 */
2270 cached = 0;
2271
Heiko Schocherff94bc42014-06-24 10:10:04 +02002272 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juulcfa460a2007-10-31 13:53:06 +01002273
2274 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2275 status = chip->waitfunc(mtd, chip);
2276 /*
2277 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00002278 * available.
William Juulcfa460a2007-10-31 13:53:06 +01002279 */
2280 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2281 status = chip->errstat(mtd, chip, FL_WRITING, status,
2282 page);
2283
2284 if (status & NAND_STATUS_FAIL)
2285 return -EIO;
2286 } else {
2287 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2288 status = chip->waitfunc(mtd, chip);
2289 }
2290
William Juulcfa460a2007-10-31 13:53:06 +01002291 return 0;
2292}
2293
2294/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002295 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2296 * @mtd: MTD device structure
2297 * @oob: oob data buffer
2298 * @len: oob data write length
2299 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01002300 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002301static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2302 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01002303{
Scott Wood17cb4b82016-05-30 13:57:56 -05002304 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002305
2306 /*
2307 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2308 * data from a previous OOB read.
2309 */
2310 memset(chip->oob_poi, 0xff, mtd->oobsize);
2311
Christian Hitz90e3f392011-10-12 09:32:01 +02002312 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01002313
Sergey Lapindfe64e22013-01-14 03:46:50 +00002314 case MTD_OPS_PLACE_OOB:
2315 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002316 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2317 return oob + len;
2318
Sergey Lapindfe64e22013-01-14 03:46:50 +00002319 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01002320 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2321 uint32_t boffs = 0, woffs = ops->ooboffs;
2322 size_t bytes = 0;
2323
Christian Hitz90e3f392011-10-12 09:32:01 +02002324 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002325 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01002326 if (unlikely(woffs)) {
2327 if (woffs >= free->length) {
2328 woffs -= free->length;
2329 continue;
2330 }
2331 boffs = free->offset + woffs;
2332 bytes = min_t(size_t, len,
2333 (free->length - woffs));
2334 woffs = 0;
2335 } else {
2336 bytes = min_t(size_t, len, free->length);
2337 boffs = free->offset;
2338 }
2339 memcpy(chip->oob_poi + boffs, oob, bytes);
2340 oob += bytes;
2341 }
2342 return oob;
2343 }
2344 default:
2345 BUG();
2346 }
2347 return NULL;
2348}
2349
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002350#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01002351
2352/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002353 * nand_do_write_ops - [INTERN] NAND write with ECC
2354 * @mtd: MTD device structure
2355 * @to: offset to write to
2356 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002357 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002358 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002359 */
2360static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2361 struct mtd_oob_ops *ops)
2362{
2363 int chipnr, realpage, page, blockmask, column;
Scott Wood17cb4b82016-05-30 13:57:56 -05002364 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002365 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002366
2367 uint32_t oobwritelen = ops->ooblen;
Scott Woodceee07b2016-05-30 13:57:58 -05002368 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002369
William Juulcfa460a2007-10-31 13:53:06 +01002370 uint8_t *oob = ops->oobbuf;
2371 uint8_t *buf = ops->datbuf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002372 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002373 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002374
2375 ops->retlen = 0;
2376 if (!writelen)
2377 return 0;
2378
Heiko Schocherff94bc42014-06-24 10:10:04 +02002379 /* Reject writes, which are not page aligned */
2380 if (NOTALIGNED(to)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002381 pr_notice("%s: attempt to write non page aligned data\n",
2382 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002383 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002384 }
2385
2386 column = to & (mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +01002387
2388 chipnr = (int)(to >> chip->chip_shift);
2389 chip->select_chip(mtd, chipnr);
2390
2391 /* Check, if it is write protected */
2392 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002393 ret = -EIO;
2394 goto err_out;
William Juulcfa460a2007-10-31 13:53:06 +01002395 }
2396
2397 realpage = (int)(to >> chip->page_shift);
2398 page = realpage & chip->pagemask;
2399 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2400
2401 /* Invalidate the page cache, when we write to the cached page */
Scott Woodd3963722015-06-26 19:03:26 -05002402 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2403 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juulcfa460a2007-10-31 13:53:06 +01002404 chip->pagebuf = -1;
2405
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002406 /* Don't allow multipage oob writes with offset */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002407 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2408 ret = -EINVAL;
2409 goto err_out;
2410 }
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002411
Christian Hitz90e3f392011-10-12 09:32:01 +02002412 while (1) {
William Juulcfa460a2007-10-31 13:53:06 +01002413 int bytes = mtd->writesize;
2414 int cached = writelen > bytes && page != blockmask;
2415 uint8_t *wbuf = buf;
Scott Woodd3963722015-06-26 19:03:26 -05002416 int use_bufpoi;
Hector Palaciosebb7feb2016-07-18 09:37:41 +02002417 int part_pagewr = (column || writelen < mtd->writesize);
Scott Woodd3963722015-06-26 19:03:26 -05002418
2419 if (part_pagewr)
2420 use_bufpoi = 1;
2421 else
2422 use_bufpoi = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002423
Heiko Schocherff94bc42014-06-24 10:10:04 +02002424 WATCHDOG_RESET();
Scott Woodd3963722015-06-26 19:03:26 -05002425 /* Partial page write?, or need to use bounce buffer */
2426 if (use_bufpoi) {
2427 pr_debug("%s: using write bounce buffer for buf@%p\n",
2428 __func__, buf);
William Juulcfa460a2007-10-31 13:53:06 +01002429 cached = 0;
Scott Woodd3963722015-06-26 19:03:26 -05002430 if (part_pagewr)
2431 bytes = min_t(int, bytes - column, writelen);
William Juulcfa460a2007-10-31 13:53:06 +01002432 chip->pagebuf = -1;
2433 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2434 memcpy(&chip->buffers->databuf[column], buf, bytes);
2435 wbuf = chip->buffers->databuf;
2436 }
2437
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002438 if (unlikely(oob)) {
2439 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002440 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002441 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002442 } else {
2443 /* We still need to erase leftover OOB data */
2444 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002445 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002446 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2447 oob_required, page, cached,
2448 (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002449 if (ret)
2450 break;
2451
2452 writelen -= bytes;
2453 if (!writelen)
2454 break;
2455
2456 column = 0;
2457 buf += bytes;
2458 realpage++;
2459
2460 page = realpage & chip->pagemask;
2461 /* Check, if we cross a chip boundary */
2462 if (!page) {
2463 chipnr++;
2464 chip->select_chip(mtd, -1);
2465 chip->select_chip(mtd, chipnr);
2466 }
2467 }
2468
2469 ops->retlen = ops->len - writelen;
2470 if (unlikely(oob))
2471 ops->oobretlen = ops->ooblen;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002472
2473err_out:
2474 chip->select_chip(mtd, -1);
2475 return ret;
2476}
2477
2478/**
2479 * panic_nand_write - [MTD Interface] NAND write with ECC
2480 * @mtd: MTD device structure
2481 * @to: offset to write to
2482 * @len: number of bytes to write
2483 * @retlen: pointer to variable to store the number of written bytes
2484 * @buf: the data to write
2485 *
2486 * NAND write with ECC. Used when performing writes in interrupt context, this
2487 * may for example be called by mtdoops when writing an oops while in panic.
2488 */
2489static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2490 size_t *retlen, const uint8_t *buf)
2491{
Scott Wood17cb4b82016-05-30 13:57:56 -05002492 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002493 struct mtd_oob_ops ops;
2494 int ret;
2495
2496 /* Wait for the device to get ready */
2497 panic_nand_wait(mtd, chip, 400);
2498
2499 /* Grab the device */
2500 panic_nand_get_device(chip, mtd, FL_WRITING);
2501
Scott Woodd3963722015-06-26 19:03:26 -05002502 memset(&ops, 0, sizeof(ops));
Heiko Schocherff94bc42014-06-24 10:10:04 +02002503 ops.len = len;
2504 ops.datbuf = (uint8_t *)buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002505 ops.mode = MTD_OPS_PLACE_OOB;
2506
2507 ret = nand_do_write_ops(mtd, to, &ops);
2508
2509 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002510 return ret;
2511}
2512
2513/**
2514 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002515 * @mtd: MTD device structure
2516 * @to: offset to write to
2517 * @len: number of bytes to write
2518 * @retlen: pointer to variable to store the number of written bytes
2519 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002520 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002521 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002522 */
2523static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2524 size_t *retlen, const uint8_t *buf)
2525{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002526 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002527 int ret;
2528
Heiko Schocherff94bc42014-06-24 10:10:04 +02002529 nand_get_device(mtd, FL_WRITING);
Scott Woodd3963722015-06-26 19:03:26 -05002530 memset(&ops, 0, sizeof(ops));
Sergey Lapindfe64e22013-01-14 03:46:50 +00002531 ops.len = len;
2532 ops.datbuf = (uint8_t *)buf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002533 ops.mode = MTD_OPS_PLACE_OOB;
2534 ret = nand_do_write_ops(mtd, to, &ops);
2535 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002536 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002537 return ret;
2538}
2539
2540/**
2541 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002542 * @mtd: MTD device structure
2543 * @to: offset to write to
2544 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002545 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002546 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002547 */
William Juulcfa460a2007-10-31 13:53:06 +01002548static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2549 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002550{
William Juulcfa460a2007-10-31 13:53:06 +01002551 int chipnr, page, status, len;
Scott Wood17cb4b82016-05-30 13:57:56 -05002552 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002553
Heiko Schocherff94bc42014-06-24 10:10:04 +02002554 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002555 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002556
Scott Woodceee07b2016-05-30 13:57:58 -05002557 len = mtd_oobavail(mtd, ops);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002558
2559 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002560 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002561 pr_debug("%s: attempt to write past end of page\n",
2562 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002563 return -EINVAL;
2564 }
2565
William Juulcfa460a2007-10-31 13:53:06 +01002566 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002567 pr_debug("%s: attempt to start write outside oob\n",
2568 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002569 return -EINVAL;
2570 }
2571
Christian Hitz90e3f392011-10-12 09:32:01 +02002572 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002573 if (unlikely(to >= mtd->size ||
2574 ops->ooboffs + ops->ooblen >
2575 ((mtd->size >> chip->page_shift) -
2576 (to >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002577 pr_debug("%s: attempt to write beyond end of device\n",
2578 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002579 return -EINVAL;
2580 }
2581
William Juulcfa460a2007-10-31 13:53:06 +01002582 chipnr = (int)(to >> chip->chip_shift);
2583 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002584
William Juulcfa460a2007-10-31 13:53:06 +01002585 /* Shift to get page */
2586 page = (int)(to >> chip->page_shift);
2587
2588 /*
2589 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2590 * of my DiskOnChip 2000 test units) will clear the whole data page too
2591 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2592 * it in the doc2000 driver in August 1999. dwmw2.
2593 */
2594 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002595
2596 /* Check, if it is write protected */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002597 if (nand_check_wp(mtd)) {
2598 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002599 return -EROFS;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002600 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002601
Wolfgang Denk932394a2005-08-17 12:55:25 +02002602 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002603 if (page == chip->pagebuf)
2604 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002605
Sergey Lapindfe64e22013-01-14 03:46:50 +00002606 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2607
2608 if (ops->mode == MTD_OPS_RAW)
2609 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2610 else
2611 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002612
Heiko Schocherff94bc42014-06-24 10:10:04 +02002613 chip->select_chip(mtd, -1);
2614
William Juulcfa460a2007-10-31 13:53:06 +01002615 if (status)
2616 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002617
William Juulcfa460a2007-10-31 13:53:06 +01002618 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002619
William Juulcfa460a2007-10-31 13:53:06 +01002620 return 0;
2621}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002622
William Juulcfa460a2007-10-31 13:53:06 +01002623/**
2624 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002625 * @mtd: MTD device structure
2626 * @to: offset to write to
2627 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002628 */
2629static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2630 struct mtd_oob_ops *ops)
2631{
William Juulcfa460a2007-10-31 13:53:06 +01002632 int ret = -ENOTSUPP;
2633
2634 ops->retlen = 0;
2635
2636 /* Do not allow writes past end of device */
2637 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002638 pr_debug("%s: attempt to write beyond end of device\n",
2639 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002640 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002641 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002642
Heiko Schocherff94bc42014-06-24 10:10:04 +02002643 nand_get_device(mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +01002644
Christian Hitz90e3f392011-10-12 09:32:01 +02002645 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002646 case MTD_OPS_PLACE_OOB:
2647 case MTD_OPS_AUTO_OOB:
2648 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002649 break;
2650
2651 default:
2652 goto out;
2653 }
2654
2655 if (!ops->datbuf)
2656 ret = nand_do_write_oob(mtd, to, ops);
2657 else
2658 ret = nand_do_write_ops(mtd, to, ops);
2659
Christian Hitz90e3f392011-10-12 09:32:01 +02002660out:
William Juulcfa460a2007-10-31 13:53:06 +01002661 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002662 return ret;
2663}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002664
2665/**
Scott Woodd3963722015-06-26 19:03:26 -05002666 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapindfe64e22013-01-14 03:46:50 +00002667 * @mtd: MTD device structure
2668 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002669 *
Scott Woodd3963722015-06-26 19:03:26 -05002670 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002671 */
Scott Woodd3963722015-06-26 19:03:26 -05002672static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002673{
Scott Wood17cb4b82016-05-30 13:57:56 -05002674 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002675 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002676 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2677 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Scott Woodd3963722015-06-26 19:03:26 -05002678
2679 return chip->waitfunc(mtd, chip);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002680}
2681
2682/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002683 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002684 * @mtd: MTD device structure
2685 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002686 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002687 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002688 */
William Juulcfa460a2007-10-31 13:53:06 +01002689static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002690{
William Juulcfa460a2007-10-31 13:53:06 +01002691 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002692}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002693
Wolfgang Denk932394a2005-08-17 12:55:25 +02002694/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002695 * nand_erase_nand - [INTERN] erase block(s)
2696 * @mtd: MTD device structure
2697 * @instr: erase instruction
2698 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002699 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002700 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002701 */
William Juulcfa460a2007-10-31 13:53:06 +01002702int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2703 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002704{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002705 int page, status, pages_per_block, ret, chipnr;
Scott Wood17cb4b82016-05-30 13:57:56 -05002706 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002707 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002708
Heiko Schocherff94bc42014-06-24 10:10:04 +02002709 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2710 __func__, (unsigned long long)instr->addr,
2711 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002712
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002713 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002714 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002715
Wolfgang Denk932394a2005-08-17 12:55:25 +02002716 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002717 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002718
2719 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002720 page = (int)(instr->addr >> chip->page_shift);
2721 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002722
2723 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002724 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002725
Wolfgang Denk932394a2005-08-17 12:55:25 +02002726 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002727 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002728
Wolfgang Denk932394a2005-08-17 12:55:25 +02002729 /* Check, if it is write protected */
2730 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002731 pr_debug("%s: device is write protected!\n",
2732 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002733 instr->state = MTD_ERASE_FAILED;
2734 goto erase_exit;
2735 }
2736
2737 /* Loop through the pages */
2738 len = instr->len;
2739
2740 instr->state = MTD_ERASING;
2741
2742 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002743 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002744
Sergey Lapindfe64e22013-01-14 03:46:50 +00002745 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamada756963d2014-12-16 15:36:33 +09002746 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Woodceee07b2016-05-30 13:57:58 -05002747 chip->page_shift, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002748 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02002749 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002750 instr->state = MTD_ERASE_FAILED;
2751 goto erase_exit;
2752 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002753
William Juulcfa460a2007-10-31 13:53:06 +01002754 /*
2755 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002756 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002757 */
2758 if (page <= chip->pagebuf && chip->pagebuf <
2759 (page + pages_per_block))
2760 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002761
Scott Woodd3963722015-06-26 19:03:26 -05002762 status = chip->erase(mtd, page & chip->pagemask);
William Juulcfa460a2007-10-31 13:53:06 +01002763
2764 /*
2765 * See if operation failed and additional status checks are
2766 * available
2767 */
2768 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2769 status = chip->errstat(mtd, chip, FL_ERASING,
2770 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002771
2772 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002773 if (status & NAND_STATUS_FAIL) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002774 pr_debug("%s: failed erase, page 0x%08x\n",
2775 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002776 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002777 instr->fail_addr =
2778 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002779 goto erase_exit;
2780 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002781
Wolfgang Denk932394a2005-08-17 12:55:25 +02002782 /* Increment page address and decrement length */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002783 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002784 page += pages_per_block;
2785
2786 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002787 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002788 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002789 chip->select_chip(mtd, -1);
2790 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002791 }
2792 }
2793 instr->state = MTD_ERASE_DONE;
2794
Christian Hitz90e3f392011-10-12 09:32:01 +02002795erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002796
2797 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002798
2799 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002800 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002801 nand_release_device(mtd);
2802
Scott Woodc45912d2008-10-24 16:20:43 -05002803 /* Do call back function */
2804 if (!ret)
2805 mtd_erase_callback(instr);
2806
Wolfgang Denk932394a2005-08-17 12:55:25 +02002807 /* Return more or less happy */
2808 return ret;
2809}
2810
2811/**
2812 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002813 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002814 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002815 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002816 */
William Juulcfa460a2007-10-31 13:53:06 +01002817static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002818{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002819 pr_debug("%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002820
2821 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002822 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002823 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002824 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002825}
2826
Wolfgang Denk932394a2005-08-17 12:55:25 +02002827/**
William Juulcfa460a2007-10-31 13:53:06 +01002828 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002829 * @mtd: MTD device structure
2830 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002831 */
William Juulcfa460a2007-10-31 13:53:06 +01002832static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002833{
Scott Woodceee07b2016-05-30 13:57:58 -05002834 struct nand_chip *chip = mtd_to_nand(mtd);
2835 int chipnr = (int)(offs >> chip->chip_shift);
2836 int ret;
2837
2838 /* Select the NAND device */
2839 nand_get_device(mtd, FL_READING);
2840 chip->select_chip(mtd, chipnr);
2841
2842 ret = nand_block_checkbad(mtd, offs, 0);
2843
2844 chip->select_chip(mtd, -1);
2845 nand_release_device(mtd);
2846
2847 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002848}
2849
2850/**
William Juulcfa460a2007-10-31 13:53:06 +01002851 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002852 * @mtd: MTD device structure
2853 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002854 */
William Juulcfa460a2007-10-31 13:53:06 +01002855static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002856{
Wolfgang Denk932394a2005-08-17 12:55:25 +02002857 int ret;
2858
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002859 ret = nand_block_isbad(mtd, ofs);
2860 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002861 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002862 if (ret > 0)
2863 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002864 return ret;
2865 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002866
Heiko Schocherff94bc42014-06-24 10:10:04 +02002867 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002868}
2869
Heiko Schocherff94bc42014-06-24 10:10:04 +02002870/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002871 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2872 * @mtd: MTD device structure
2873 * @chip: nand chip info structure
2874 * @addr: feature address.
2875 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01002876 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002877static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2878 int addr, uint8_t *subfeature_param)
2879{
2880 int status;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002881 int i;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002882
Heiko Schocherff94bc42014-06-24 10:10:04 +02002883#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2884 if (!chip->onfi_version ||
2885 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2886 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00002887 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002888#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00002889
2890 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002891 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2892 chip->write_byte(mtd, subfeature_param[i]);
2893
Sergey Lapindfe64e22013-01-14 03:46:50 +00002894 status = chip->waitfunc(mtd, chip);
2895 if (status & NAND_STATUS_FAIL)
2896 return -EIO;
2897 return 0;
2898}
2899
2900/**
2901 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2902 * @mtd: MTD device structure
2903 * @chip: nand chip info structure
2904 * @addr: feature address.
2905 * @subfeature_param: the subfeature parameters, a four bytes array.
2906 */
2907static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2908 int addr, uint8_t *subfeature_param)
2909{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002910 int i;
2911
2912#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2913 if (!chip->onfi_version ||
2914 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2915 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00002916 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002917#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00002918
Sergey Lapindfe64e22013-01-14 03:46:50 +00002919 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002920 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2921 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002922 return 0;
2923}
2924
2925/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01002926static void nand_set_defaults(struct nand_chip *chip, int busw)
2927{
2928 /* check for proper chip_delay setup, set 20us if not */
2929 if (!chip->chip_delay)
2930 chip->chip_delay = 20;
2931
2932 /* check, if a user supplied command function given */
2933 if (chip->cmdfunc == NULL)
2934 chip->cmdfunc = nand_command;
2935
2936 /* check, if a user supplied wait function given */
2937 if (chip->waitfunc == NULL)
2938 chip->waitfunc = nand_wait;
2939
2940 if (!chip->select_chip)
2941 chip->select_chip = nand_select_chip;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002942
2943 /* set for ONFI nand */
2944 if (!chip->onfi_set_features)
2945 chip->onfi_set_features = nand_onfi_set_features;
2946 if (!chip->onfi_get_features)
2947 chip->onfi_get_features = nand_onfi_get_features;
2948
2949 /* If called twice, pointers that depend on busw may need to be reset */
2950 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juulcfa460a2007-10-31 13:53:06 +01002951 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2952 if (!chip->read_word)
2953 chip->read_word = nand_read_word;
2954 if (!chip->block_bad)
2955 chip->block_bad = nand_block_bad;
2956 if (!chip->block_markbad)
2957 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002958 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juulcfa460a2007-10-31 13:53:06 +01002959 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002960 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2961 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2962 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juulcfa460a2007-10-31 13:53:06 +01002963 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juulcfa460a2007-10-31 13:53:06 +01002964 if (!chip->scan_bbt)
2965 chip->scan_bbt = nand_default_bbt;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002966
2967 if (!chip->controller) {
William Juulcfa460a2007-10-31 13:53:06 +01002968 chip->controller = &chip->hwcontrol;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002969 spin_lock_init(&chip->controller->lock);
2970 init_waitqueue_head(&chip->controller->wq);
2971 }
2972
William Juulcfa460a2007-10-31 13:53:06 +01002973}
2974
Sergey Lapindfe64e22013-01-14 03:46:50 +00002975/* Sanitize ONFI strings so we can safely print them */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002976static void sanitize_string(char *s, size_t len)
2977{
2978 ssize_t i;
2979
Sergey Lapindfe64e22013-01-14 03:46:50 +00002980 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002981 s[len - 1] = 0;
2982
Sergey Lapindfe64e22013-01-14 03:46:50 +00002983 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002984 for (i = 0; i < len - 1; i++) {
2985 if (s[i] < ' ' || s[i] > 127)
2986 s[i] = '?';
2987 }
2988
Sergey Lapindfe64e22013-01-14 03:46:50 +00002989 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002990 strim(s);
2991}
2992
Florian Fainelli0272c712011-02-25 00:01:34 +00002993static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01002994{
Florian Fainelli0272c712011-02-25 00:01:34 +00002995 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00002996 while (len--) {
2997 crc ^= *p++ << 8;
2998 for (i = 0; i < 8; i++)
2999 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05003000 }
3001
Florian Fainelli0272c712011-02-25 00:01:34 +00003002 return crc;
3003}
William Juulcfa460a2007-10-31 13:53:06 +01003004
Heiko Schocher4e67c572014-07-15 16:08:43 +02003005#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherff94bc42014-06-24 10:10:04 +02003006/* Parse the Extended Parameter Page. */
3007static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3008 struct nand_chip *chip, struct nand_onfi_params *p)
3009{
3010 struct onfi_ext_param_page *ep;
3011 struct onfi_ext_section *s;
3012 struct onfi_ext_ecc_info *ecc;
3013 uint8_t *cursor;
3014 int ret = -EINVAL;
3015 int len;
3016 int i;
3017
3018 len = le16_to_cpu(p->ext_param_page_length) * 16;
3019 ep = kmalloc(len, GFP_KERNEL);
3020 if (!ep)
3021 return -ENOMEM;
3022
3023 /* Send our own NAND_CMD_PARAM. */
3024 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3025
3026 /* Use the Change Read Column command to skip the ONFI param pages. */
3027 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3028 sizeof(*p) * p->num_of_param_pages , -1);
3029
3030 /* Read out the Extended Parameter Page. */
3031 chip->read_buf(mtd, (uint8_t *)ep, len);
3032 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3033 != le16_to_cpu(ep->crc))) {
3034 pr_debug("fail in the CRC.\n");
3035 goto ext_out;
3036 }
3037
3038 /*
3039 * Check the signature.
3040 * Do not strictly follow the ONFI spec, maybe changed in future.
3041 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003042 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003043 pr_debug("The signature is invalid.\n");
3044 goto ext_out;
3045 }
3046
3047 /* find the ECC section. */
3048 cursor = (uint8_t *)(ep + 1);
3049 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3050 s = ep->sections + i;
3051 if (s->type == ONFI_SECTION_TYPE_2)
3052 break;
3053 cursor += s->length * 16;
3054 }
3055 if (i == ONFI_EXT_SECTION_MAX) {
3056 pr_debug("We can not find the ECC section.\n");
3057 goto ext_out;
3058 }
3059
3060 /* get the info we want. */
3061 ecc = (struct onfi_ext_ecc_info *)cursor;
3062
3063 if (!ecc->codeword_size) {
3064 pr_debug("Invalid codeword size\n");
3065 goto ext_out;
3066 }
3067
3068 chip->ecc_strength_ds = ecc->ecc_bits;
3069 chip->ecc_step_ds = 1 << ecc->codeword_size;
3070 ret = 0;
3071
3072ext_out:
3073 kfree(ep);
3074 return ret;
3075}
3076
3077static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3078{
Scott Wood17cb4b82016-05-30 13:57:56 -05003079 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003080 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3081
3082 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3083 feature);
3084}
3085
3086/*
3087 * Configure chip properties from Micron vendor-specific ONFI table
3088 */
3089static void nand_onfi_detect_micron(struct nand_chip *chip,
3090 struct nand_onfi_params *p)
3091{
3092 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3093
3094 if (le16_to_cpu(p->vendor_revision) < 1)
3095 return;
3096
3097 chip->read_retries = micron->read_retry_options;
3098 chip->setup_read_retry = nand_setup_read_retry_micron;
3099}
3100
Florian Fainelli0272c712011-02-25 00:01:34 +00003101/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003102 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00003103 */
Christian Hitz90e3f392011-10-12 09:32:01 +02003104static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003105 int *busw)
3106{
3107 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05303108 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00003109 int val;
3110
Sergey Lapindfe64e22013-01-14 03:46:50 +00003111 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00003112 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3113 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3114 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3115 return 0;
3116
Florian Fainelli0272c712011-02-25 00:01:34 +00003117 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3118 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05303119 for (j = 0; j < sizeof(*p); j++)
3120 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003121 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02003122 le16_to_cpu(p->crc)) {
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01003123 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00003124 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02003125 }
William Juulcfa460a2007-10-31 13:53:06 +01003126
Heiko Schocherff94bc42014-06-24 10:10:04 +02003127 if (i == 3) {
3128 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00003129 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003130 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003131
Sergey Lapindfe64e22013-01-14 03:46:50 +00003132 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00003133 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003134 if (val & (1 << 5))
3135 chip->onfi_version = 23;
3136 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00003137 chip->onfi_version = 22;
3138 else if (val & (1 << 3))
3139 chip->onfi_version = 21;
3140 else if (val & (1 << 2))
3141 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003142 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00003143 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003144
3145 if (!chip->onfi_version) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003146 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003147 return 0;
3148 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003149
Christian Hitz5454ddb2011-10-12 09:32:05 +02003150 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3151 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01003152 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00003153 mtd->name = p->model;
William Juulcfa460a2007-10-31 13:53:06 +01003154
Heiko Schocherff94bc42014-06-24 10:10:04 +02003155 mtd->writesize = le32_to_cpu(p->byte_per_page);
3156
3157 /*
3158 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3159 * (don't ask me who thought of this...). MTD assumes that these
3160 * dimensions will be power-of-2, so just truncate the remaining area.
3161 */
3162 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3163 mtd->erasesize *= mtd->writesize;
3164
3165 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3166
3167 /* See erasesize comment */
3168 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3169 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3170 chip->bits_per_cell = p->bits_per_cell;
3171
3172 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3173 *busw = NAND_BUSWIDTH_16;
3174 else
3175 *busw = 0;
3176
3177 if (p->ecc_bits != 0xff) {
3178 chip->ecc_strength_ds = p->ecc_bits;
3179 chip->ecc_step_ds = 512;
3180 } else if (chip->onfi_version >= 21 &&
3181 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3182
3183 /*
3184 * The nand_flash_detect_ext_param_page() uses the
3185 * Change Read Column command which maybe not supported
3186 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3187 * now. We do not replace user supplied command function.
3188 */
3189 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3190 chip->cmdfunc = nand_command_lp;
3191
3192 /* The Extended Parameter Page is supported since ONFI 2.1. */
3193 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3194 pr_warn("Failed to detect ONFI extended param page\n");
3195 } else {
3196 pr_warn("Could not retrieve ONFI ECC requirements\n");
3197 }
3198
3199 if (p->jedec_id == NAND_MFR_MICRON)
3200 nand_onfi_detect_micron(chip, p);
3201
Florian Fainelli0272c712011-02-25 00:01:34 +00003202 return 1;
3203}
3204#else
Heiko Schocherff94bc42014-06-24 10:10:04 +02003205static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003206 int *busw)
3207{
3208 return 0;
3209}
3210#endif
3211
Florian Fainelli0272c712011-02-25 00:01:34 +00003212/*
Heiko Schocher4e67c572014-07-15 16:08:43 +02003213 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3214 */
3215static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3216 int *busw)
3217{
3218 struct nand_jedec_params *p = &chip->jedec_params;
3219 struct jedec_ecc_info *ecc;
3220 int val;
3221 int i, j;
3222
3223 /* Try JEDEC for unknown chip or LP */
3224 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3225 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3226 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3227 chip->read_byte(mtd) != 'C')
3228 return 0;
3229
3230 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3231 for (i = 0; i < 3; i++) {
3232 for (j = 0; j < sizeof(*p); j++)
3233 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3234
3235 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3236 le16_to_cpu(p->crc))
3237 break;
3238 }
3239
3240 if (i == 3) {
3241 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3242 return 0;
3243 }
3244
3245 /* Check version */
3246 val = le16_to_cpu(p->revision);
3247 if (val & (1 << 2))
3248 chip->jedec_version = 10;
3249 else if (val & (1 << 1))
3250 chip->jedec_version = 1; /* vendor specific version */
3251
3252 if (!chip->jedec_version) {
3253 pr_info("unsupported JEDEC version: %d\n", val);
3254 return 0;
3255 }
3256
3257 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3258 sanitize_string(p->model, sizeof(p->model));
3259 if (!mtd->name)
3260 mtd->name = p->model;
3261
3262 mtd->writesize = le32_to_cpu(p->byte_per_page);
3263
3264 /* Please reference to the comment for nand_flash_detect_onfi. */
3265 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3266 mtd->erasesize *= mtd->writesize;
3267
3268 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3269
3270 /* Please reference to the comment for nand_flash_detect_onfi. */
3271 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3272 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3273 chip->bits_per_cell = p->bits_per_cell;
3274
3275 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3276 *busw = NAND_BUSWIDTH_16;
3277 else
3278 *busw = 0;
3279
3280 /* ECC info */
3281 ecc = &p->ecc_info[0];
3282
3283 if (ecc->codeword_size >= 9) {
3284 chip->ecc_strength_ds = ecc->ecc_bits;
3285 chip->ecc_step_ds = 1 << ecc->codeword_size;
3286 } else {
3287 pr_warn("Invalid codeword size\n");
3288 }
3289
3290 return 1;
3291}
3292
3293/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003294 * nand_id_has_period - Check if an ID string has a given wraparound period
3295 * @id_data: the ID string
3296 * @arrlen: the length of the @id_data array
3297 * @period: the period of repitition
3298 *
3299 * Check if an ID string is repeated within a given sequence of bytes at
3300 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherff94bc42014-06-24 10:10:04 +02003301 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapindfe64e22013-01-14 03:46:50 +00003302 * if the repetition has a period of @period; otherwise, returns zero.
3303 */
3304static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3305{
3306 int i, j;
3307 for (i = 0; i < period; i++)
3308 for (j = i + period; j < arrlen; j += period)
3309 if (id_data[i] != id_data[j])
3310 return 0;
3311 return 1;
3312}
3313
3314/*
3315 * nand_id_len - Get the length of an ID string returned by CMD_READID
3316 * @id_data: the ID string
3317 * @arrlen: the length of the @id_data array
3318
3319 * Returns the length of the ID string, according to known wraparound/trailing
3320 * zero patterns. If no pattern exists, returns the length of the array.
3321 */
3322static int nand_id_len(u8 *id_data, int arrlen)
3323{
3324 int last_nonzero, period;
3325
3326 /* Find last non-zero byte */
3327 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3328 if (id_data[last_nonzero])
3329 break;
3330
3331 /* All zeros */
3332 if (last_nonzero < 0)
3333 return 0;
3334
3335 /* Calculate wraparound period */
3336 for (period = 1; period < arrlen; period++)
3337 if (nand_id_has_period(id_data, arrlen, period))
3338 break;
3339
3340 /* There's a repeated pattern */
3341 if (period < arrlen)
3342 return period;
3343
3344 /* There are trailing zeros */
3345 if (last_nonzero < arrlen - 1)
3346 return last_nonzero + 1;
3347
3348 /* No pattern detected */
3349 return arrlen;
3350}
3351
Heiko Schocherff94bc42014-06-24 10:10:04 +02003352/* Extract the bits of per cell from the 3rd byte of the extended ID */
3353static int nand_get_bits_per_cell(u8 cellinfo)
3354{
3355 int bits;
3356
3357 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3358 bits >>= NAND_CI_CELLTYPE_SHIFT;
3359 return bits + 1;
3360}
3361
Sergey Lapindfe64e22013-01-14 03:46:50 +00003362/*
3363 * Many new NAND share similar device ID codes, which represent the size of the
3364 * chip. The rest of the parameters must be decoded according to generic or
3365 * manufacturer-specific "extended ID" decoding patterns.
3366 */
3367static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3368 u8 id_data[8], int *busw)
3369{
3370 int extid, id_len;
3371 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003372 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003373 /* The 4th id byte is the important one */
3374 extid = id_data[3];
3375
3376 id_len = nand_id_len(id_data, 8);
3377
3378 /*
3379 * Field definitions are in the following datasheets:
3380 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3381 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3382 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3383 *
3384 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3385 * ID to decide what to do.
3386 */
3387 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003388 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003389 /* Calc pagesize */
3390 mtd->writesize = 2048 << (extid & 0x03);
3391 extid >>= 2;
3392 /* Calc oobsize */
3393 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3394 case 1:
3395 mtd->oobsize = 128;
3396 break;
3397 case 2:
3398 mtd->oobsize = 218;
3399 break;
3400 case 3:
3401 mtd->oobsize = 400;
3402 break;
3403 case 4:
3404 mtd->oobsize = 436;
3405 break;
3406 case 5:
3407 mtd->oobsize = 512;
3408 break;
3409 case 6:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003410 mtd->oobsize = 640;
3411 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003412 case 7:
3413 default: /* Other cases are "reserved" (unknown) */
3414 mtd->oobsize = 1024;
3415 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003416 }
3417 extid >>= 2;
3418 /* Calc blocksize */
3419 mtd->erasesize = (128 * 1024) <<
3420 (((extid >> 1) & 0x04) | (extid & 0x03));
3421 *busw = 0;
3422 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003423 !nand_is_slc(chip)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003424 unsigned int tmp;
3425
3426 /* Calc pagesize */
3427 mtd->writesize = 2048 << (extid & 0x03);
3428 extid >>= 2;
3429 /* Calc oobsize */
3430 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3431 case 0:
3432 mtd->oobsize = 128;
3433 break;
3434 case 1:
3435 mtd->oobsize = 224;
3436 break;
3437 case 2:
3438 mtd->oobsize = 448;
3439 break;
3440 case 3:
3441 mtd->oobsize = 64;
3442 break;
3443 case 4:
3444 mtd->oobsize = 32;
3445 break;
3446 case 5:
3447 mtd->oobsize = 16;
3448 break;
3449 default:
3450 mtd->oobsize = 640;
3451 break;
3452 }
3453 extid >>= 2;
3454 /* Calc blocksize */
3455 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3456 if (tmp < 0x03)
3457 mtd->erasesize = (128 * 1024) << tmp;
3458 else if (tmp == 0x03)
3459 mtd->erasesize = 768 * 1024;
3460 else
3461 mtd->erasesize = (64 * 1024) << tmp;
3462 *busw = 0;
3463 } else {
3464 /* Calc pagesize */
3465 mtd->writesize = 1024 << (extid & 0x03);
3466 extid >>= 2;
3467 /* Calc oobsize */
3468 mtd->oobsize = (8 << (extid & 0x01)) *
3469 (mtd->writesize >> 9);
3470 extid >>= 2;
3471 /* Calc blocksize. Blocksize is multiples of 64KiB */
3472 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3473 extid >>= 2;
3474 /* Get buswidth information */
3475 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003476
3477 /*
3478 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3479 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3480 * follows:
3481 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3482 * 110b -> 24nm
3483 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3484 */
3485 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3486 nand_is_slc(chip) &&
3487 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3488 !(id_data[4] & 0x80) /* !BENAND */) {
3489 mtd->oobsize = 32 * mtd->writesize >> 9;
3490 }
3491
Sergey Lapindfe64e22013-01-14 03:46:50 +00003492 }
3493}
3494
Heiko Schocherff94bc42014-06-24 10:10:04 +02003495/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003496 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3497 * decodes a matching ID table entry and assigns the MTD size parameters for
3498 * the chip.
3499 */
3500static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003501 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapindfe64e22013-01-14 03:46:50 +00003502 int *busw)
3503{
3504 int maf_id = id_data[0];
3505
3506 mtd->erasesize = type->erasesize;
3507 mtd->writesize = type->pagesize;
3508 mtd->oobsize = mtd->writesize / 32;
3509 *busw = type->options & NAND_BUSWIDTH_16;
3510
Heiko Schocherff94bc42014-06-24 10:10:04 +02003511 /* All legacy ID NAND are small-page, SLC */
3512 chip->bits_per_cell = 1;
3513
Sergey Lapindfe64e22013-01-14 03:46:50 +00003514 /*
3515 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3516 * some Spansion chips have erasesize that conflicts with size
3517 * listed in nand_ids table.
3518 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3519 */
3520 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3521 && id_data[6] == 0x00 && id_data[7] == 0x00
3522 && mtd->writesize == 512) {
3523 mtd->erasesize = 128 * 1024;
3524 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3525 }
3526}
3527
Heiko Schocherff94bc42014-06-24 10:10:04 +02003528/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003529 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3530 * heuristic patterns using various detected parameters (e.g., manufacturer,
3531 * page size, cell-type information).
3532 */
3533static void nand_decode_bbm_options(struct mtd_info *mtd,
3534 struct nand_chip *chip, u8 id_data[8])
3535{
3536 int maf_id = id_data[0];
3537
3538 /* Set the bad block position */
3539 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3540 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3541 else
3542 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3543
3544 /*
3545 * Bad block marker is stored in the last page of each block on Samsung
3546 * and Hynix MLC devices; stored in first two pages of each block on
3547 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3548 * AMD/Spansion, and Macronix. All others scan only the first page.
3549 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003550 if (!nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003551 (maf_id == NAND_MFR_SAMSUNG ||
3552 maf_id == NAND_MFR_HYNIX))
3553 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003554 else if ((nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003555 (maf_id == NAND_MFR_SAMSUNG ||
3556 maf_id == NAND_MFR_HYNIX ||
3557 maf_id == NAND_MFR_TOSHIBA ||
3558 maf_id == NAND_MFR_AMD ||
3559 maf_id == NAND_MFR_MACRONIX)) ||
3560 (mtd->writesize == 2048 &&
3561 maf_id == NAND_MFR_MICRON))
3562 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3563}
3564
Heiko Schocherff94bc42014-06-24 10:10:04 +02003565static inline bool is_full_id_nand(struct nand_flash_dev *type)
3566{
3567 return type->id_len;
3568}
3569
3570static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3571 struct nand_flash_dev *type, u8 *id_data, int *busw)
3572{
Heiko Schocherff94bc42014-06-24 10:10:04 +02003573 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003574 mtd->writesize = type->pagesize;
3575 mtd->erasesize = type->erasesize;
3576 mtd->oobsize = type->oobsize;
3577
3578 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3579 chip->chipsize = (uint64_t)type->chipsize << 20;
3580 chip->options |= type->options;
3581 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3582 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Woodd3963722015-06-26 19:03:26 -05003583 chip->onfi_timing_mode_default =
3584 type->onfi_timing_mode_default;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003585
3586 *busw = type->options & NAND_BUSWIDTH_16;
3587
3588 if (!mtd->name)
3589 mtd->name = type->name;
3590
3591 return true;
3592 }
3593 return false;
3594}
3595
Sergey Lapindfe64e22013-01-14 03:46:50 +00003596/*
3597 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00003598 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003599static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Florian Fainelli0272c712011-02-25 00:01:34 +00003600 struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003601 int *maf_id, int *dev_id,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003602 struct nand_flash_dev *type)
Florian Fainelli0272c712011-02-25 00:01:34 +00003603{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003604 int busw;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003605 int i, maf_idx;
3606 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00003607
3608 /* Select the device */
3609 chip->select_chip(mtd, 0);
3610
3611 /*
3612 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003613 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00003614 */
3615 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3616
3617 /* Send the command for reading device ID */
3618 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3619
3620 /* Read manufacturer and device IDs */
3621 *maf_id = chip->read_byte(mtd);
3622 *dev_id = chip->read_byte(mtd);
3623
Sergey Lapindfe64e22013-01-14 03:46:50 +00003624 /*
3625 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00003626 * interface concerns can cause random data which looks like a
3627 * possibly credible NAND flash to appear. If the two results do
3628 * not match, ignore the device completely.
3629 */
3630
3631 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3632
Sergey Lapindfe64e22013-01-14 03:46:50 +00003633 /* Read entire ID string */
3634 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003635 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003636
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003637 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003638 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003639 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00003640 return ERR_PTR(-ENODEV);
3641 }
3642
3643 if (!type)
3644 type = nand_flash_ids;
3645
Heiko Schocherff94bc42014-06-24 10:10:04 +02003646 for (; type->name != NULL; type++) {
3647 if (is_full_id_nand(type)) {
3648 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3649 goto ident_done;
3650 } else if (*dev_id == type->dev_id) {
Scott Woodceee07b2016-05-30 13:57:58 -05003651 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003652 }
3653 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003654
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003655 chip->onfi_version = 0;
3656 if (!type->name || !type->pagesize) {
Scott Woodd3963722015-06-26 19:03:26 -05003657 /* Check if the chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003658 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003659 goto ident_done;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003660
3661 /* Check if the chip is JEDEC compliant */
3662 if (nand_flash_detect_jedec(mtd, chip, &busw))
3663 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00003664 }
3665
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003666 if (!type->name)
3667 return ERR_PTR(-ENODEV);
3668
Florian Fainelli0272c712011-02-25 00:01:34 +00003669 if (!mtd->name)
3670 mtd->name = type->name;
3671
3672 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00003673
Scott Woodceee07b2016-05-30 13:57:58 -05003674 if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003675 /* Decode parameters from extended ID */
3676 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003677 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003678 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003679 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003680 /* Get chip options */
Marek Vasut9c790a72012-08-30 13:39:38 +00003681 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00003682
Sergey Lapindfe64e22013-01-14 03:46:50 +00003683 /*
3684 * Check if chip is not a Samsung device. Do not clear the
3685 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003686 */
3687 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3688 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3689ident_done:
3690
William Juulcfa460a2007-10-31 13:53:06 +01003691 /* Try to identify manufacturer */
3692 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3693 if (nand_manuf_ids[maf_idx].id == *maf_id)
3694 break;
3695 }
3696
Heiko Schocherff94bc42014-06-24 10:10:04 +02003697 if (chip->options & NAND_BUSWIDTH_AUTO) {
3698 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3699 chip->options |= busw;
3700 nand_set_defaults(chip, busw);
3701 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3702 /*
3703 * Check, if buswidth is correct. Hardware drivers should set
3704 * chip correct!
3705 */
3706 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3707 *maf_id, *dev_id);
3708 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3709 pr_warn("bus width %d instead %d bit\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003710 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3711 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003712 return ERR_PTR(-EINVAL);
3713 }
3714
Sergey Lapindfe64e22013-01-14 03:46:50 +00003715 nand_decode_bbm_options(mtd, chip, id_data);
3716
William Juulcfa460a2007-10-31 13:53:06 +01003717 /* Calculate the address shift from the page size */
3718 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003719 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003720 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3721
3722 chip->bbt_erase_shift = chip->phys_erase_shift =
3723 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003724 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003725 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003726 else {
3727 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3728 chip->chip_shift += 32 - 1;
3729 }
3730
3731 chip->badblockbits = 8;
Scott Woodd3963722015-06-26 19:03:26 -05003732 chip->erase = single_erase;
William Juulcfa460a2007-10-31 13:53:06 +01003733
Sergey Lapindfe64e22013-01-14 03:46:50 +00003734 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003735 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3736 chip->cmdfunc = nand_command_lp;
3737
Heiko Schocherff94bc42014-06-24 10:10:04 +02003738 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3739 *maf_id, *dev_id);
Heiko Schocher4e67c572014-07-15 16:08:43 +02003740
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003741#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher4e67c572014-07-15 16:08:43 +02003742 if (chip->onfi_version)
3743 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3744 chip->onfi_params.model);
3745 else if (chip->jedec_version)
3746 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3747 chip->jedec_params.model);
3748 else
3749 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3750 type->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003751#else
Heiko Schocher4e67c572014-07-15 16:08:43 +02003752 if (chip->jedec_version)
3753 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3754 chip->jedec_params.model);
3755 else
3756 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3757 type->name);
3758
3759 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3760 type->name);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003761#endif
Heiko Schocher4e67c572014-07-15 16:08:43 +02003762
Scott Woodd3963722015-06-26 19:03:26 -05003763 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02003764 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Woodd3963722015-06-26 19:03:26 -05003765 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003766 return type;
3767}
3768
Brian Norris42bd19c2016-06-15 21:09:22 +02003769#if CONFIG_IS_ENABLED(OF_CONTROL)
3770DECLARE_GLOBAL_DATA_PTR;
3771
3772static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3773{
3774 int ret, ecc_mode = -1, ecc_strength, ecc_step;
3775 const void *blob = gd->fdt_blob;
3776 const char *str;
3777
3778 ret = fdtdec_get_int(blob, node, "nand-bus-width", -1);
3779 if (ret == 16)
3780 chip->options |= NAND_BUSWIDTH_16;
3781
3782 if (fdtdec_get_bool(blob, node, "nand-on-flash-bbt"))
3783 chip->bbt_options |= NAND_BBT_USE_FLASH;
3784
3785 str = fdt_getprop(blob, node, "nand-ecc-mode", NULL);
3786 if (str) {
3787 if (!strcmp(str, "none"))
3788 ecc_mode = NAND_ECC_NONE;
3789 else if (!strcmp(str, "soft"))
3790 ecc_mode = NAND_ECC_SOFT;
3791 else if (!strcmp(str, "hw"))
3792 ecc_mode = NAND_ECC_HW;
3793 else if (!strcmp(str, "hw_syndrome"))
3794 ecc_mode = NAND_ECC_HW_SYNDROME;
3795 else if (!strcmp(str, "hw_oob_first"))
3796 ecc_mode = NAND_ECC_HW_OOB_FIRST;
3797 else if (!strcmp(str, "soft_bch"))
3798 ecc_mode = NAND_ECC_SOFT_BCH;
3799 }
3800
3801
3802 ecc_strength = fdtdec_get_int(blob, node, "nand-ecc-strength", -1);
3803 ecc_step = fdtdec_get_int(blob, node, "nand-ecc-step-size", -1);
3804
3805 if ((ecc_step >= 0 && !(ecc_strength >= 0)) ||
3806 (!(ecc_step >= 0) && ecc_strength >= 0)) {
3807 pr_err("must set both strength and step size in DT\n");
3808 return -EINVAL;
3809 }
3810
3811 if (ecc_mode >= 0)
3812 chip->ecc.mode = ecc_mode;
3813
3814 if (ecc_strength >= 0)
3815 chip->ecc.strength = ecc_strength;
3816
3817 if (ecc_step > 0)
3818 chip->ecc.size = ecc_step;
3819
3820 return 0;
3821}
3822#else
3823static int nand_dt_init(struct mtd_info *mtd, struct nand_chip *chip, int node)
3824{
3825 return 0;
3826}
3827#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
3828
William Juulcfa460a2007-10-31 13:53:06 +01003829/**
3830 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003831 * @mtd: MTD device structure
3832 * @maxchips: number of chips to scan for
3833 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003834 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003835 * This is the first phase of the normal nand_scan() function. It reads the
3836 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003837 *
William Juulcfa460a2007-10-31 13:53:06 +01003838 */
Lei Wen245eb902011-01-06 09:48:18 +08003839int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003840 struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003841{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003842 int i, nand_maf_id, nand_dev_id;
Scott Wood17cb4b82016-05-30 13:57:56 -05003843 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003844 struct nand_flash_dev *type;
Brian Norris42bd19c2016-06-15 21:09:22 +02003845 int ret;
3846
3847 if (chip->flash_node) {
3848 ret = nand_dt_init(mtd, chip, chip->flash_node);
3849 if (ret)
3850 return ret;
3851 }
William Juulcfa460a2007-10-31 13:53:06 +01003852
William Juulcfa460a2007-10-31 13:53:06 +01003853 /* Set the default functions */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003854 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juulcfa460a2007-10-31 13:53:06 +01003855
3856 /* Read the flash type */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003857 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3858 &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003859
3860 if (IS_ERR(type)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003861 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3862 pr_warn("No NAND device found\n");
William Juulcfa460a2007-10-31 13:53:06 +01003863 chip->select_chip(mtd, -1);
3864 return PTR_ERR(type);
3865 }
3866
Heiko Schocherff94bc42014-06-24 10:10:04 +02003867 chip->select_chip(mtd, -1);
3868
William Juulcfa460a2007-10-31 13:53:06 +01003869 /* Check for a chip array */
3870 for (i = 1; i < maxchips; i++) {
3871 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003872 /* See comment in nand_get_flash_type for reset */
3873 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003874 /* Send the command for reading device ID */
3875 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3876 /* Read manufacturer and device IDs */
3877 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02003878 nand_dev_id != chip->read_byte(mtd)) {
3879 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003880 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003881 }
3882 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003883 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003884
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003885#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003886 if (i > 1)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003887 pr_info("%d chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003888#endif
William Juulcfa460a2007-10-31 13:53:06 +01003889
3890 /* Store the number of chips and calc total size for mtd */
3891 chip->numchips = i;
3892 mtd->size = i * chip->chipsize;
3893
3894 return 0;
3895}
Heiko Schocherff94bc42014-06-24 10:10:04 +02003896EXPORT_SYMBOL(nand_scan_ident);
William Juulcfa460a2007-10-31 13:53:06 +01003897
Scott Woodd3963722015-06-26 19:03:26 -05003898/*
3899 * Check if the chip configuration meet the datasheet requirements.
3900
3901 * If our configuration corrects A bits per B bytes and the minimum
3902 * required correction level is X bits per Y bytes, then we must ensure
3903 * both of the following are true:
3904 *
3905 * (1) A / B >= X / Y
3906 * (2) A >= X
3907 *
3908 * Requirement (1) ensures we can correct for the required bitflip density.
3909 * Requirement (2) ensures we can correct even when all bitflips are clumped
3910 * in the same sector.
3911 */
3912static bool nand_ecc_strength_good(struct mtd_info *mtd)
3913{
Scott Wood17cb4b82016-05-30 13:57:56 -05003914 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Woodd3963722015-06-26 19:03:26 -05003915 struct nand_ecc_ctrl *ecc = &chip->ecc;
3916 int corr, ds_corr;
3917
3918 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3919 /* Not enough information */
3920 return true;
3921
3922 /*
3923 * We get the number of corrected bits per page to compare
3924 * the correction density.
3925 */
3926 corr = (mtd->writesize * ecc->strength) / ecc->size;
3927 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3928
3929 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3930}
William Juulcfa460a2007-10-31 13:53:06 +01003931
3932/**
3933 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003934 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003935 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003936 * This is the second phase of the normal nand_scan() function. It fills out
3937 * all the uninitialized function pointers with the defaults and scans for a
3938 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003939 */
3940int nand_scan_tail(struct mtd_info *mtd)
3941{
3942 int i;
Scott Wood17cb4b82016-05-30 13:57:56 -05003943 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003944 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003945 struct nand_buffers *nbuf;
William Juulcfa460a2007-10-31 13:53:06 +01003946
Sergey Lapindfe64e22013-01-14 03:46:50 +00003947 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3948 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3949 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3950
Heiko Schocher4e67c572014-07-15 16:08:43 +02003951 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher4e67c572014-07-15 16:08:43 +02003952 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher4e67c572014-07-15 16:08:43 +02003953 chip->buffers = nbuf;
3954 } else {
3955 if (!chip->buffers)
3956 return -ENOMEM;
3957 }
William Juulcfa460a2007-10-31 13:53:06 +01003958
3959 /* Set the internal oob buffer location, just after the page data */
3960 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3961
3962 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003963 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01003964 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003965 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01003966 switch (mtd->oobsize) {
3967 case 8:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003968 ecc->layout = &nand_oob_8;
William Juulcfa460a2007-10-31 13:53:06 +01003969 break;
3970 case 16:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003971 ecc->layout = &nand_oob_16;
William Juulcfa460a2007-10-31 13:53:06 +01003972 break;
3973 case 64:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003974 ecc->layout = &nand_oob_64;
William Juulcfa460a2007-10-31 13:53:06 +01003975 break;
3976 case 128:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003977 ecc->layout = &nand_oob_128;
William Juulcfa460a2007-10-31 13:53:06 +01003978 break;
3979 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003980 pr_warn("No oob scheme defined for oobsize %d\n",
3981 mtd->oobsize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003982 BUG();
William Juulcfa460a2007-10-31 13:53:06 +01003983 }
3984 }
3985
3986 if (!chip->write_page)
3987 chip->write_page = nand_write_page;
3988
3989 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003990 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01003991 * selected and we have 256 byte pagesize fallback to software ECC
3992 */
William Juulcfa460a2007-10-31 13:53:06 +01003993
Heiko Schocherff94bc42014-06-24 10:10:04 +02003994 switch (ecc->mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003995 case NAND_ECC_HW_OOB_FIRST:
3996 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003997 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Woodd3963722015-06-26 19:03:26 -05003998 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003999 BUG();
4000 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004001 if (!ecc->read_page)
4002 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004003
William Juulcfa460a2007-10-31 13:53:06 +01004004 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004005 /* Use standard hwecc read page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004006 if (!ecc->read_page)
4007 ecc->read_page = nand_read_page_hwecc;
4008 if (!ecc->write_page)
4009 ecc->write_page = nand_write_page_hwecc;
4010 if (!ecc->read_page_raw)
4011 ecc->read_page_raw = nand_read_page_raw;
4012 if (!ecc->write_page_raw)
4013 ecc->write_page_raw = nand_write_page_raw;
4014 if (!ecc->read_oob)
4015 ecc->read_oob = nand_read_oob_std;
4016 if (!ecc->write_oob)
4017 ecc->write_oob = nand_write_oob_std;
4018 if (!ecc->read_subpage)
4019 ecc->read_subpage = nand_read_subpage;
Scott Woodceee07b2016-05-30 13:57:58 -05004020 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherff94bc42014-06-24 10:10:04 +02004021 ecc->write_subpage = nand_write_subpage_hwecc;
William Juulcfa460a2007-10-31 13:53:06 +01004022
4023 case NAND_ECC_HW_SYNDROME:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004024 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4025 (!ecc->read_page ||
4026 ecc->read_page == nand_read_page_hwecc ||
4027 !ecc->write_page ||
4028 ecc->write_page == nand_write_page_hwecc)) {
Scott Woodd3963722015-06-26 19:03:26 -05004029 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01004030 BUG();
4031 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00004032 /* Use standard syndrome read/write page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004033 if (!ecc->read_page)
4034 ecc->read_page = nand_read_page_syndrome;
4035 if (!ecc->write_page)
4036 ecc->write_page = nand_write_page_syndrome;
4037 if (!ecc->read_page_raw)
4038 ecc->read_page_raw = nand_read_page_raw_syndrome;
4039 if (!ecc->write_page_raw)
4040 ecc->write_page_raw = nand_write_page_raw_syndrome;
4041 if (!ecc->read_oob)
4042 ecc->read_oob = nand_read_oob_syndrome;
4043 if (!ecc->write_oob)
4044 ecc->write_oob = nand_write_oob_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01004045
Heiko Schocherff94bc42014-06-24 10:10:04 +02004046 if (mtd->writesize >= ecc->size) {
4047 if (!ecc->strength) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004048 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4049 BUG();
4050 }
William Juulcfa460a2007-10-31 13:53:06 +01004051 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004052 }
Scott Woodd3963722015-06-26 19:03:26 -05004053 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4054 ecc->size, mtd->writesize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02004055 ecc->mode = NAND_ECC_SOFT;
William Juulcfa460a2007-10-31 13:53:06 +01004056
4057 case NAND_ECC_SOFT:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004058 ecc->calculate = nand_calculate_ecc;
4059 ecc->correct = nand_correct_data;
4060 ecc->read_page = nand_read_page_swecc;
4061 ecc->read_subpage = nand_read_subpage;
4062 ecc->write_page = nand_write_page_swecc;
4063 ecc->read_page_raw = nand_read_page_raw;
4064 ecc->write_page_raw = nand_write_page_raw;
4065 ecc->read_oob = nand_read_oob_std;
4066 ecc->write_oob = nand_write_oob_std;
4067 if (!ecc->size)
4068 ecc->size = 256;
4069 ecc->bytes = 3;
4070 ecc->strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01004071 break;
4072
Christian Hitz4c6de852011-10-12 09:31:59 +02004073 case NAND_ECC_SOFT_BCH:
4074 if (!mtd_nand_has_bch()) {
Heiko Schocher4e67c572014-07-15 16:08:43 +02004075 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004076 BUG();
Christian Hitz4c6de852011-10-12 09:31:59 +02004077 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004078 ecc->calculate = nand_bch_calculate_ecc;
4079 ecc->correct = nand_bch_correct_data;
4080 ecc->read_page = nand_read_page_swecc;
4081 ecc->read_subpage = nand_read_subpage;
4082 ecc->write_page = nand_write_page_swecc;
4083 ecc->read_page_raw = nand_read_page_raw;
4084 ecc->write_page_raw = nand_write_page_raw;
4085 ecc->read_oob = nand_read_oob_std;
4086 ecc->write_oob = nand_write_oob_std;
Christian Hitz4c6de852011-10-12 09:31:59 +02004087 /*
Scott Woodd3963722015-06-26 19:03:26 -05004088 * Board driver should supply ecc.size and ecc.strength values
4089 * to select how many bits are correctable. Otherwise, default
4090 * to 4 bits for large page devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02004091 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004092 if (!ecc->size && (mtd->oobsize >= 64)) {
4093 ecc->size = 512;
Scott Woodd3963722015-06-26 19:03:26 -05004094 ecc->strength = 4;
Christian Hitz4c6de852011-10-12 09:31:59 +02004095 }
Scott Woodd3963722015-06-26 19:03:26 -05004096
4097 /* See nand_bch_init() for details. */
Scott Woodceee07b2016-05-30 13:57:58 -05004098 ecc->bytes = 0;
4099 ecc->priv = nand_bch_init(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02004100 if (!ecc->priv) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004101 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004102 BUG();
4103 }
Christian Hitz4c6de852011-10-12 09:31:59 +02004104 break;
4105
William Juulcfa460a2007-10-31 13:53:06 +01004106 case NAND_ECC_NONE:
Scott Woodd3963722015-06-26 19:03:26 -05004107 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004108 ecc->read_page = nand_read_page_raw;
4109 ecc->write_page = nand_write_page_raw;
4110 ecc->read_oob = nand_read_oob_std;
4111 ecc->read_page_raw = nand_read_page_raw;
4112 ecc->write_page_raw = nand_write_page_raw;
4113 ecc->write_oob = nand_write_oob_std;
4114 ecc->size = mtd->writesize;
4115 ecc->bytes = 0;
4116 ecc->strength = 0;
William Juulcfa460a2007-10-31 13:53:06 +01004117 break;
4118
4119 default:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004120 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juulcfa460a2007-10-31 13:53:06 +01004121 BUG();
4122 }
4123
Sergey Lapindfe64e22013-01-14 03:46:50 +00004124 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004125 if (!ecc->read_oob_raw)
4126 ecc->read_oob_raw = ecc->read_oob;
4127 if (!ecc->write_oob_raw)
4128 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004129
William Juulcfa460a2007-10-31 13:53:06 +01004130 /*
4131 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00004132 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01004133 */
Scott Woodceee07b2016-05-30 13:57:58 -05004134 mtd->oobavail = 0;
4135 if (ecc->layout) {
4136 for (i = 0; ecc->layout->oobfree[i].length; i++)
4137 mtd->oobavail += ecc->layout->oobfree[i].length;
4138 }
William Juulcfa460a2007-10-31 13:53:06 +01004139
Scott Woodd3963722015-06-26 19:03:26 -05004140 /* ECC sanity check: warn if it's too weak */
4141 if (!nand_ecc_strength_good(mtd))
4142 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4143 mtd->name);
4144
William Juulcfa460a2007-10-31 13:53:06 +01004145 /*
4146 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00004147 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01004148 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004149 ecc->steps = mtd->writesize / ecc->size;
4150 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004151 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01004152 BUG();
4153 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004154 ecc->total = ecc->steps * ecc->bytes;
William Juulcfa460a2007-10-31 13:53:06 +01004155
Sergey Lapindfe64e22013-01-14 03:46:50 +00004156 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004157 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4158 switch (ecc->steps) {
William Juulcfa460a2007-10-31 13:53:06 +01004159 case 2:
4160 mtd->subpage_sft = 1;
4161 break;
4162 case 4:
4163 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05004164 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01004165 mtd->subpage_sft = 2;
4166 break;
4167 }
4168 }
4169 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4170
4171 /* Initialize state */
4172 chip->state = FL_READY;
4173
William Juulcfa460a2007-10-31 13:53:06 +01004174 /* Invalidate the pagebuffer reference */
4175 chip->pagebuf = -1;
4176
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004177 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Woodd3963722015-06-26 19:03:26 -05004178 switch (ecc->mode) {
4179 case NAND_ECC_SOFT:
4180 case NAND_ECC_SOFT_BCH:
4181 if (chip->page_shift > 9)
4182 chip->options |= NAND_SUBPAGE_READ;
4183 break;
4184
4185 default:
4186 break;
4187 }
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004188
William Juulcfa460a2007-10-31 13:53:06 +01004189 /* Fill in remaining MTD driver data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004190 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004191 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4192 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004193 mtd->_erase = nand_erase;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004194 mtd->_read = nand_read;
4195 mtd->_write = nand_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004196 mtd->_panic_write = panic_nand_write;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004197 mtd->_read_oob = nand_read_oob;
4198 mtd->_write_oob = nand_write_oob;
4199 mtd->_sync = nand_sync;
4200 mtd->_lock = NULL;
4201 mtd->_unlock = NULL;
Ezequiel Garcia86a720a2014-05-21 19:06:12 -03004202 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004203 mtd->_block_isbad = nand_block_isbad;
4204 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004205 mtd->writebufsize = mtd->writesize;
William Juulcfa460a2007-10-31 13:53:06 +01004206
Sergey Lapindfe64e22013-01-14 03:46:50 +00004207 /* propagate ecc info to mtd_info */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004208 mtd->ecclayout = ecc->layout;
4209 mtd->ecc_strength = ecc->strength;
4210 mtd->ecc_step_size = ecc->size;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004211 /*
4212 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4213 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4214 * properly set.
4215 */
4216 if (!mtd->bitflip_threshold)
Scott Woodd3963722015-06-26 19:03:26 -05004217 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juulcfa460a2007-10-31 13:53:06 +01004218
Rostislav Lisovy35c204d2014-10-22 13:40:44 +02004219 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01004220}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004221EXPORT_SYMBOL(nand_scan_tail);
4222
William Juulcfa460a2007-10-31 13:53:06 +01004223/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02004224 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004225 * @mtd: MTD device structure
4226 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02004227 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00004228 * This fills out all the uninitialized function pointers with the defaults.
4229 * The flash ID is read and the mtd/chip structures are filled with the
Scott Woodceee07b2016-05-30 13:57:58 -05004230 * appropriate values.
Wolfgang Denk932394a2005-08-17 12:55:25 +02004231 */
William Juulcfa460a2007-10-31 13:53:06 +01004232int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004233{
William Juulcfa460a2007-10-31 13:53:06 +01004234 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004235
Lei Wen245eb902011-01-06 09:48:18 +08004236 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01004237 if (!ret)
4238 ret = nand_scan_tail(mtd);
4239 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004240}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004241EXPORT_SYMBOL(nand_scan);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004242
Heiko Schocherff94bc42014-06-24 10:10:04 +02004243MODULE_LICENSE("GPL");
4244MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4245MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4246MODULE_DESCRIPTION("Generic NAND flash driver code");