blob: 689716753ae646f35ba697367314a7f6b0bf4001 [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>
Wolfgang Denk932394a2005-08-17 12:55:25 +020032#include <malloc.h>
33#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010034#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000035#include <linux/compat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020036#include <linux/mtd/mtd.h>
37#include <linux/mtd/nand.h>
38#include <linux/mtd/nand_ecc.h>
Christian Hitz4c6de852011-10-12 09:31:59 +020039#include <linux/mtd/nand_bch.h>
Stefan Roese10bb62d2009-04-24 15:58:33 +020040#ifdef CONFIG_MTD_PARTITIONS
41#include <linux/mtd/partitions.h>
42#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020043#include <asm/io.h>
44#include <asm/errno.h>
45
Wolfgang Denk932394a2005-08-17 12:55:25 +020046/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010047static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020048 .eccbytes = 3,
49 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010050 .oobfree = {
51 {.offset = 3,
52 .length = 2},
53 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020054 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020055};
56
William Juulcfa460a2007-10-31 13:53:06 +010057static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020058 .eccbytes = 6,
59 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010060 .oobfree = {
61 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020062 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020063};
64
William Juulcfa460a2007-10-31 13:53:06 +010065static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020066 .eccbytes = 24,
67 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +010068 40, 41, 42, 43, 44, 45, 46, 47,
69 48, 49, 50, 51, 52, 53, 54, 55,
70 56, 57, 58, 59, 60, 61, 62, 63},
71 .oobfree = {
72 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +020073 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020074};
75
William Juulcfa460a2007-10-31 13:53:06 +010076static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +020077 .eccbytes = 48,
78 .eccpos = {
Christian Hitz90e3f392011-10-12 09:32:01 +020079 80, 81, 82, 83, 84, 85, 86, 87,
80 88, 89, 90, 91, 92, 93, 94, 95,
81 96, 97, 98, 99, 100, 101, 102, 103,
William Juulcfa460a2007-10-31 13:53:06 +010082 104, 105, 106, 107, 108, 109, 110, 111,
83 112, 113, 114, 115, 116, 117, 118, 119,
84 120, 121, 122, 123, 124, 125, 126, 127},
85 .oobfree = {
86 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +020087 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020088};
89
Heiko Schocherff94bc42014-06-24 10:10:04 +020090static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juulcfa460a2007-10-31 13:53:06 +010091
92static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
93 struct mtd_oob_ops *ops);
94
Heiko Schocherff94bc42014-06-24 10:10:04 +020095/*
96 * For devices which display every fart in the system on a separate LED. Is
97 * compiled away when LED support is disabled.
98 */
99DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200100
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200101static int check_offs_len(struct mtd_info *mtd,
102 loff_t ofs, uint64_t len)
103{
Scott Wood17cb4b82016-05-30 13:57:56 -0500104 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200105 int ret = 0;
106
107 /* Start address must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200108 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
109 pr_debug("%s: unaligned address\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200110 ret = -EINVAL;
111 }
112
113 /* Length must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200114 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
115 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200116 ret = -EINVAL;
117 }
118
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200119 return ret;
120}
121
Wolfgang Denk932394a2005-08-17 12:55:25 +0200122/**
123 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000124 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200125 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200126 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200127 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200128static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100129{
Scott Wood17cb4b82016-05-30 13:57:56 -0500130 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200131
132 /* De-select the NAND device */
133 chip->select_chip(mtd, -1);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100134}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200135
136/**
137 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000138 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200139 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200140 * Default read function for 8bit buswidth
Wolfgang Denk932394a2005-08-17 12:55:25 +0200141 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000142uint8_t nand_read_byte(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200143{
Scott Wood17cb4b82016-05-30 13:57:56 -0500144 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100145 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200146}
147
148/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200149 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000150 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200151 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000152 * Default read function for 16bit buswidth with endianness conversion.
153 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200154 */
William Juulcfa460a2007-10-31 13:53:06 +0100155static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200156{
Scott Wood17cb4b82016-05-30 13:57:56 -0500157 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100158 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200159}
160
161/**
162 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000163 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200164 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000165 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200166 */
167static u16 nand_read_word(struct mtd_info *mtd)
168{
Scott Wood17cb4b82016-05-30 13:57:56 -0500169 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100170 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200171}
172
173/**
174 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000175 * @mtd: MTD device structure
176 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200177 *
178 * Default select function for 1 chip devices.
179 */
William Juulcfa460a2007-10-31 13:53:06 +0100180static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200181{
Scott Wood17cb4b82016-05-30 13:57:56 -0500182 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100183
184 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200185 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100186 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200187 break;
188 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200189 break;
190
191 default:
192 BUG();
193 }
194}
195
196/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200197 * nand_write_byte - [DEFAULT] write single byte to chip
198 * @mtd: MTD device structure
199 * @byte: value to write
200 *
201 * Default function to write a byte to I/O[7:0]
202 */
203static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
204{
Scott Wood17cb4b82016-05-30 13:57:56 -0500205 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200206
207 chip->write_buf(mtd, &byte, 1);
208}
209
210/**
211 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
212 * @mtd: MTD device structure
213 * @byte: value to write
214 *
215 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
216 */
217static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
218{
Scott Wood17cb4b82016-05-30 13:57:56 -0500219 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200220 uint16_t word = byte;
221
222 /*
223 * It's not entirely clear what should happen to I/O[15:8] when writing
224 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
225 *
226 * When the host supports a 16-bit bus width, only data is
227 * transferred at the 16-bit width. All address and command line
228 * transfers shall use only the lower 8-bits of the data bus. During
229 * command transfers, the host may place any value on the upper
230 * 8-bits of the data bus. During address transfers, the host shall
231 * set the upper 8-bits of the data bus to 00h.
232 *
233 * One user of the write_byte callback is nand_onfi_set_features. The
234 * four parameters are specified to be written to I/O[7:0], but this is
235 * neither an address nor a command transfer. Let's assume a 0 on the
236 * upper I/O lines is OK.
237 */
238 chip->write_buf(mtd, (uint8_t *)&word, 2);
239}
240
Scott Wood27331062015-06-22 22:38:32 -0500241#if !defined(CONFIG_BLACKFIN)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200242static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
243{
244 int i;
245
246 for (i = 0; i < len; i++)
247 writeb(buf[i], addr);
248}
249static void ioread8_rep(void *addr, uint8_t *buf, int len)
250{
251 int i;
252
253 for (i = 0; i < len; i++)
254 buf[i] = readb(addr);
255}
256
257static void ioread16_rep(void *addr, void *buf, int len)
258{
259 int i;
260 u16 *p = (u16 *) buf;
Stefan Roesebe16aba2014-09-05 09:57:01 +0200261
Heiko Schocherff94bc42014-06-24 10:10:04 +0200262 for (i = 0; i < len; i++)
263 p[i] = readw(addr);
264}
265
266static void iowrite16_rep(void *addr, void *buf, int len)
267{
268 int i;
269 u16 *p = (u16 *) buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200270
271 for (i = 0; i < len; i++)
272 writew(p[i], addr);
273}
274#endif
275
276/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200277 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000278 * @mtd: MTD device structure
279 * @buf: data buffer
280 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200281 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000282 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200283 */
Simon Schwarz82645f82011-10-31 06:34:44 +0000284void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200285{
Scott Wood17cb4b82016-05-30 13:57:56 -0500286 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200287
Heiko Schocherff94bc42014-06-24 10:10:04 +0200288 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200289}
290
291/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200292 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000293 * @mtd: MTD device structure
294 * @buf: buffer to store date
295 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200296 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000297 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200298 */
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400299void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200300{
Scott Wood17cb4b82016-05-30 13:57:56 -0500301 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200302
Heiko Schocherff94bc42014-06-24 10:10:04 +0200303 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200304}
305
Heiko Schocherff94bc42014-06-24 10:10:04 +0200306/**
307 * nand_write_buf16 - [DEFAULT] write buffer to chip
308 * @mtd: MTD device structure
309 * @buf: data buffer
310 * @len: number of bytes to write
311 *
312 * Default write function for 16bit buswidth.
313 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200314void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200315{
Scott Wood17cb4b82016-05-30 13:57:56 -0500316 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200317 u16 *p = (u16 *) buf;
318
319 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
320}
321
322/**
323 * nand_read_buf16 - [DEFAULT] read chip data into buffer
324 * @mtd: MTD device structure
325 * @buf: buffer to store date
326 * @len: number of bytes to read
327 *
328 * Default read function for 16bit buswidth.
329 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200330void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200331{
Scott Wood17cb4b82016-05-30 13:57:56 -0500332 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200333 u16 *p = (u16 *) buf;
334
335 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
336}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200337
338/**
339 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000340 * @mtd: MTD device structure
341 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200342 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200343 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200344 */
Scott Woodceee07b2016-05-30 13:57:58 -0500345static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200346{
Scott Woodceee07b2016-05-30 13:57:58 -0500347 int page, res = 0, i = 0;
Scott Wood17cb4b82016-05-30 13:57:56 -0500348 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200349 u16 bad;
350
Sergey Lapindfe64e22013-01-14 03:46:50 +0000351 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200352 ofs += mtd->erasesize - mtd->writesize;
353
William Juulcfa460a2007-10-31 13:53:06 +0100354 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200355
Sergey Lapindfe64e22013-01-14 03:46:50 +0000356 do {
357 if (chip->options & NAND_BUSWIDTH_16) {
358 chip->cmdfunc(mtd, NAND_CMD_READOOB,
359 chip->badblockpos & 0xFE, page);
360 bad = cpu_to_le16(chip->read_word(mtd));
361 if (chip->badblockpos & 0x1)
362 bad >>= 8;
363 else
364 bad &= 0xFF;
365 } else {
366 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
367 page);
368 bad = chip->read_byte(mtd);
369 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200370
Sergey Lapindfe64e22013-01-14 03:46:50 +0000371 if (likely(chip->badblockbits == 8))
372 res = bad != 0xFF;
373 else
374 res = hweight8(bad) < chip->badblockbits;
375 ofs += mtd->writesize;
376 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
377 i++;
378 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200379
Wolfgang Denk932394a2005-08-17 12:55:25 +0200380 return res;
381}
382
383/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200384 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapindfe64e22013-01-14 03:46:50 +0000385 * @mtd: MTD device structure
386 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200387 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000388 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherff94bc42014-06-24 10:10:04 +0200389 * specific driver. It provides the details for writing a bad block marker to a
390 * block.
391 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200392static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
393{
Scott Wood17cb4b82016-05-30 13:57:56 -0500394 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200395 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +0100396 uint8_t buf[2] = { 0, 0 };
Heiko Schocherff94bc42014-06-24 10:10:04 +0200397 int ret = 0, res, i = 0;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200398
Scott Woodd3963722015-06-26 19:03:26 -0500399 memset(&ops, 0, sizeof(ops));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200400 ops.oobbuf = buf;
401 ops.ooboffs = chip->badblockpos;
402 if (chip->options & NAND_BUSWIDTH_16) {
403 ops.ooboffs &= ~0x01;
404 ops.len = ops.ooblen = 2;
405 } else {
406 ops.len = ops.ooblen = 1;
407 }
408 ops.mode = MTD_OPS_PLACE_OOB;
409
410 /* Write to first/last page(s) if necessary */
411 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
412 ofs += mtd->erasesize - mtd->writesize;
413 do {
414 res = nand_do_write_oob(mtd, ofs, &ops);
415 if (!ret)
416 ret = res;
417
418 i++;
419 ofs += mtd->writesize;
420 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
421
422 return ret;
423}
424
425/**
426 * nand_block_markbad_lowlevel - mark a block bad
427 * @mtd: MTD device structure
428 * @ofs: offset from device start
429 *
430 * This function performs the generic NAND bad block marking steps (i.e., bad
431 * block table(s) and/or marker(s)). We only allow the hardware driver to
432 * specify how to write bad block markers to OOB (chip->block_markbad).
433 *
434 * We try operations in the following order:
435 * (1) erase the affected block, to allow OOB marker to be written cleanly
436 * (2) write bad block marker to OOB area of affected block (unless flag
437 * NAND_BBT_NO_OOB_BBM is present)
438 * (3) update the BBT
439 * Note that we retain the first error encountered in (2) or (3), finish the
440 * procedures, and dump the error in the end.
441*/
442static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
443{
Scott Wood17cb4b82016-05-30 13:57:56 -0500444 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200445 int res, ret = 0;
446
447 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000448 struct erase_info einfo;
449
450 /* Attempt erase before marking OOB */
451 memset(&einfo, 0, sizeof(einfo));
452 einfo.mtd = mtd;
453 einfo.addr = ofs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200454 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000455 nand_erase_nand(mtd, &einfo, 0);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200456
Heiko Schocherff94bc42014-06-24 10:10:04 +0200457 /* Write bad block marker to OOB */
458 nand_get_device(mtd, FL_WRITING);
459 ret = chip->block_markbad(mtd, ofs);
Scott Woodc45912d2008-10-24 16:20:43 -0500460 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100461 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000462
Heiko Schocherff94bc42014-06-24 10:10:04 +0200463 /* Mark block bad in BBT */
464 if (chip->bbt) {
465 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000466 if (!ret)
467 ret = res;
468 }
469
William Juulcfa460a2007-10-31 13:53:06 +0100470 if (!ret)
471 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500472
William Juulcfa460a2007-10-31 13:53:06 +0100473 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200474}
475
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200476/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200477 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000478 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200479 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000480 * Check, if the device is write protected. The function expects, that the
481 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200482 */
William Juulcfa460a2007-10-31 13:53:06 +0100483static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200484{
Scott Wood17cb4b82016-05-30 13:57:56 -0500485 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200486
Sergey Lapindfe64e22013-01-14 03:46:50 +0000487 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200488 if (chip->options & NAND_BROKEN_XD)
489 return 0;
490
Wolfgang Denk932394a2005-08-17 12:55:25 +0200491 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100492 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
493 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200494}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100495
Wolfgang Denk932394a2005-08-17 12:55:25 +0200496/**
Scott Woodd3963722015-06-26 19:03:26 -0500497 * nand_block_isreserved - [GENERIC] Check if a block is marked reserved.
Sergey Lapindfe64e22013-01-14 03:46:50 +0000498 * @mtd: MTD device structure
499 * @ofs: offset from device start
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300500 *
Scott Woodd3963722015-06-26 19:03:26 -0500501 * Check if the block is marked as reserved.
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300502 */
503static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
504{
Scott Wood17cb4b82016-05-30 13:57:56 -0500505 struct nand_chip *chip = mtd_to_nand(mtd);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300506
507 if (!chip->bbt)
508 return 0;
509 /* Return info from the table */
510 return nand_isreserved_bbt(mtd, ofs);
511}
512
513/**
514 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
515 * @mtd: MTD device structure
516 * @ofs: offset from device start
Sergey Lapindfe64e22013-01-14 03:46:50 +0000517 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200518 *
519 * Check, if the block is bad. Either by reading the bad block table or
520 * calling of the scan function.
521 */
Scott Woodceee07b2016-05-30 13:57:58 -0500522static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200523{
Scott Wood17cb4b82016-05-30 13:57:56 -0500524 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200525
Masahiro Yamadaab37b762014-12-26 22:20:58 +0900526 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
527 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovy35c204d2014-10-22 13:40:44 +0200528 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamadabf80ee62014-12-26 22:20:57 +0900529 chip->scan_bbt(mtd);
Rostislav Lisovy35c204d2014-10-22 13:40:44 +0200530 }
531
William Juulcfa460a2007-10-31 13:53:06 +0100532 if (!chip->bbt)
Scott Woodceee07b2016-05-30 13:57:58 -0500533 return chip->block_bad(mtd, ofs);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200534
Wolfgang Denk932394a2005-08-17 12:55:25 +0200535 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100536 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200537}
538
Scott Woodceee07b2016-05-30 13:57:58 -0500539/**
540 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
541 * @mtd: MTD device structure
542 *
543 * Wait for the ready pin after a command, and warn if a timeout occurs.
544 */
William Juulcfa460a2007-10-31 13:53:06 +0100545void nand_wait_ready(struct mtd_info *mtd)
546{
Scott Wood17cb4b82016-05-30 13:57:56 -0500547 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Woodceee07b2016-05-30 13:57:58 -0500548 u32 timeo = (CONFIG_SYS_HZ * 400) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000549 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100550
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000551 time_start = get_timer(0);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000552 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000553 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100554 if (chip->dev_ready)
555 if (chip->dev_ready(mtd))
556 break;
557 }
Scott Woodceee07b2016-05-30 13:57:58 -0500558
559 if (!chip->dev_ready(mtd))
560 pr_warn("timeout while waiting for chip to become ready\n");
William Juulcfa460a2007-10-31 13:53:06 +0100561}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200562EXPORT_SYMBOL_GPL(nand_wait_ready);
William Juulcfa460a2007-10-31 13:53:06 +0100563
Wolfgang Denk932394a2005-08-17 12:55:25 +0200564/**
Scott Woodd3963722015-06-26 19:03:26 -0500565 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
566 * @mtd: MTD device structure
567 * @timeo: Timeout in ms
568 *
569 * Wait for status ready (i.e. command done) or timeout.
570 */
571static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
572{
Scott Wood17cb4b82016-05-30 13:57:56 -0500573 register struct nand_chip *chip = mtd_to_nand(mtd);
Scott Woodd3963722015-06-26 19:03:26 -0500574 u32 time_start;
575
576 timeo = (CONFIG_SYS_HZ * timeo) / 1000;
577 time_start = get_timer(0);
578 while (get_timer(time_start) < timeo) {
579 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
580 break;
581 WATCHDOG_RESET();
582 }
583};
584
585/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200586 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000587 * @mtd: MTD device structure
588 * @command: the command to be sent
589 * @column: the column address for this command, -1 if none
590 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200591 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000592 * Send command to NAND device. This function is used for small page devices
Heiko Schocherff94bc42014-06-24 10:10:04 +0200593 * (512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200594 */
William Juulcfa460a2007-10-31 13:53:06 +0100595static void nand_command(struct mtd_info *mtd, unsigned int command,
596 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200597{
Scott Wood17cb4b82016-05-30 13:57:56 -0500598 register struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100599 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200600
Sergey Lapindfe64e22013-01-14 03:46:50 +0000601 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200602 if (command == NAND_CMD_SEQIN) {
603 int readcmd;
604
William Juulcfa460a2007-10-31 13:53:06 +0100605 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200606 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100607 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200608 readcmd = NAND_CMD_READOOB;
609 } else if (column < 256) {
610 /* First 256 bytes --> READ0 */
611 readcmd = NAND_CMD_READ0;
612 } else {
613 column -= 256;
614 readcmd = NAND_CMD_READ1;
615 }
William Juulcfa460a2007-10-31 13:53:06 +0100616 chip->cmd_ctrl(mtd, readcmd, ctrl);
617 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200618 }
William Juulcfa460a2007-10-31 13:53:06 +0100619 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200620
Sergey Lapindfe64e22013-01-14 03:46:50 +0000621 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100622 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
623 /* Serially input address */
624 if (column != -1) {
625 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200626 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530627 !nand_opcode_8bits(command))
William Juulcfa460a2007-10-31 13:53:06 +0100628 column >>= 1;
629 chip->cmd_ctrl(mtd, column, ctrl);
630 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200631 }
William Juulcfa460a2007-10-31 13:53:06 +0100632 if (page_addr != -1) {
633 chip->cmd_ctrl(mtd, page_addr, ctrl);
634 ctrl &= ~NAND_CTRL_CHANGE;
635 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
636 /* One more address cycle for devices > 32MiB */
637 if (chip->chipsize > (32 << 20))
638 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
639 }
640 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200641
642 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000643 * Program and erase have their own busy handlers status and sequential
644 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100645 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200646 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200647
Wolfgang Denk932394a2005-08-17 12:55:25 +0200648 case NAND_CMD_PAGEPROG:
649 case NAND_CMD_ERASE1:
650 case NAND_CMD_ERASE2:
651 case NAND_CMD_SEQIN:
652 case NAND_CMD_STATUS:
653 return;
654
655 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100656 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200657 break;
William Juulcfa460a2007-10-31 13:53:06 +0100658 udelay(chip->chip_delay);
659 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
660 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
661 chip->cmd_ctrl(mtd,
662 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Scott Woodd3963722015-06-26 19:03:26 -0500663 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
664 nand_wait_status_ready(mtd, 250);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200665 return;
666
William Juulcfa460a2007-10-31 13:53:06 +0100667 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200668 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200669 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200670 * If we don't have access to the busy pin, we apply the given
671 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100672 */
673 if (!chip->dev_ready) {
674 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200675 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200676 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200677 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000678 /*
679 * Apply this short delay always to ensure that we do wait tWB in
680 * any case on any machine.
681 */
William Juulcfa460a2007-10-31 13:53:06 +0100682 ndelay(100);
683
684 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200685}
686
687/**
688 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000689 * @mtd: MTD device structure
690 * @command: the command to be sent
691 * @column: the column address for this command, -1 if none
692 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200693 *
William Juulcfa460a2007-10-31 13:53:06 +0100694 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000695 * devices. We don't have the separate regions as we have in the small page
696 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200697 */
William Juulcfa460a2007-10-31 13:53:06 +0100698static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
699 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200700{
Scott Wood17cb4b82016-05-30 13:57:56 -0500701 register struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200702
703 /* Emulate NAND_CMD_READOOB */
704 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100705 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200706 command = NAND_CMD_READ0;
707 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200708
William Juulcfa460a2007-10-31 13:53:06 +0100709 /* Command latch cycle */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200710 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200711
712 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100713 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200714
715 /* Serially input address */
716 if (column != -1) {
717 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200718 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530719 !nand_opcode_8bits(command))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200720 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100721 chip->cmd_ctrl(mtd, column, ctrl);
722 ctrl &= ~NAND_CTRL_CHANGE;
723 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200724 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200725 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100726 chip->cmd_ctrl(mtd, page_addr, ctrl);
727 chip->cmd_ctrl(mtd, page_addr >> 8,
728 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200729 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100730 if (chip->chipsize > (128 << 20))
731 chip->cmd_ctrl(mtd, page_addr >> 16,
732 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200733 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200734 }
William Juulcfa460a2007-10-31 13:53:06 +0100735 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200736
737 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000738 * Program and erase have their own busy handlers status, sequential
Scott Woodd3963722015-06-26 19:03:26 -0500739 * in and status need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100740 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200741 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200742
Wolfgang Denk932394a2005-08-17 12:55:25 +0200743 case NAND_CMD_CACHEDPROG:
744 case NAND_CMD_PAGEPROG:
745 case NAND_CMD_ERASE1:
746 case NAND_CMD_ERASE2:
747 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100748 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200749 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100750 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200751
752 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100753 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200754 break;
William Juulcfa460a2007-10-31 13:53:06 +0100755 udelay(chip->chip_delay);
756 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
757 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
758 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
759 NAND_NCE | NAND_CTRL_CHANGE);
Scott Woodd3963722015-06-26 19:03:26 -0500760 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
761 nand_wait_status_ready(mtd, 250);
William Juulcfa460a2007-10-31 13:53:06 +0100762 return;
763
764 case NAND_CMD_RNDOUT:
765 /* No ready / busy check necessary */
766 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
767 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
768 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
769 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200770 return;
771
772 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100773 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
774 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
775 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
776 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200777
William Juulcfa460a2007-10-31 13:53:06 +0100778 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200779 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200780 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200781 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000782 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100783 */
784 if (!chip->dev_ready) {
785 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200786 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200787 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200788 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200789
Sergey Lapindfe64e22013-01-14 03:46:50 +0000790 /*
791 * Apply this short delay always to ensure that we do wait tWB in
792 * any case on any machine.
793 */
William Juulcfa460a2007-10-31 13:53:06 +0100794 ndelay(100);
795
796 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200797}
798
799/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200800 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000801 * @chip: the nand chip descriptor
802 * @mtd: MTD device structure
803 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200804 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200805 * Used when in panic, no locks are taken.
806 */
807static void panic_nand_get_device(struct nand_chip *chip,
808 struct mtd_info *mtd, int new_state)
809{
810 /* Hardware controller shared among independent devices */
811 chip->controller->active = chip;
812 chip->state = new_state;
813}
814
815/**
816 * nand_get_device - [GENERIC] Get chip for selected access
817 * @mtd: MTD device structure
818 * @new_state: the state which is requested
819 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200820 * Get the device and lock it for exclusive access
821 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200822static int
Heiko Schocherff94bc42014-06-24 10:10:04 +0200823nand_get_device(struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100824{
Scott Wood17cb4b82016-05-30 13:57:56 -0500825 struct nand_chip *chip = mtd_to_nand(mtd);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200826 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100827 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200828}
829
830/**
831 * panic_nand_wait - [GENERIC] wait until the command is done
832 * @mtd: MTD device structure
833 * @chip: NAND chip structure
834 * @timeo: timeout
835 *
836 * Wait for command done. This is a helper function for nand_wait used when
837 * we are in interrupt context. May happen when in panic and trying to write
838 * an oops through mtdoops.
839 */
840static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
841 unsigned long timeo)
842{
843 int i;
844 for (i = 0; i < timeo; i++) {
845 if (chip->dev_ready) {
846 if (chip->dev_ready(mtd))
847 break;
848 } else {
849 if (chip->read_byte(mtd) & NAND_STATUS_READY)
850 break;
851 }
852 mdelay(1);
853 }
William Juulcfa460a2007-10-31 13:53:06 +0100854}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200855
856/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000857 * nand_wait - [DEFAULT] wait until the command is done
858 * @mtd: MTD device structure
859 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200860 *
Scott Woodceee07b2016-05-30 13:57:58 -0500861 * Wait for command done. This applies to erase and program only.
William Juulcfa460a2007-10-31 13:53:06 +0100862 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200863static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200864{
Scott Woodceee07b2016-05-30 13:57:58 -0500865 int status;
866 unsigned long timeo = 400;
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100867
Heiko Schocherff94bc42014-06-24 10:10:04 +0200868 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100869
Heiko Schocherff94bc42014-06-24 10:10:04 +0200870 /*
871 * Apply this short delay always to ensure that we do wait tWB in any
872 * case on any machine.
873 */
874 ndelay(100);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100875
Heiko Schocherff94bc42014-06-24 10:10:04 +0200876 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
877
Heiko Schocherff94bc42014-06-24 10:10:04 +0200878 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
879 u32 time_start;
880
881 time_start = get_timer(0);
882 while (get_timer(time_start) < timer) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200883 if (chip->dev_ready) {
884 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100885 break;
886 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200887 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100888 break;
889 }
890 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200891 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +0100892
Heiko Schocherff94bc42014-06-24 10:10:04 +0200893 status = (int)chip->read_byte(mtd);
894 /* This can happen if in case of timeout or buggy dev_ready */
895 WARN_ON(!(status & NAND_STATUS_READY));
896 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200897}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200898
Scott Woodceee07b2016-05-30 13:57:58 -0500899#define BITS_PER_BYTE 8
900
901/**
902 * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data
903 * @buf: buffer to test
904 * @len: buffer length
905 * @bitflips_threshold: maximum number of bitflips
906 *
907 * Check if a buffer contains only 0xff, which means the underlying region
908 * has been erased and is ready to be programmed.
909 * The bitflips_threshold specify the maximum number of bitflips before
910 * considering the region is not erased.
911 * Note: The logic of this function has been extracted from the memweight
912 * implementation, except that nand_check_erased_buf function exit before
913 * testing the whole buffer if the number of bitflips exceed the
914 * bitflips_threshold value.
915 *
916 * Returns a positive number of bitflips less than or equal to
917 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
918 * threshold.
919 */
920static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
921{
922 const unsigned char *bitmap = buf;
923 int bitflips = 0;
924 int weight;
925
926 for (; len && ((uintptr_t)bitmap) % sizeof(long);
927 len--, bitmap++) {
928 weight = hweight8(*bitmap);
929 bitflips += BITS_PER_BYTE - weight;
930 if (unlikely(bitflips > bitflips_threshold))
931 return -EBADMSG;
932 }
933
934 for (; len >= 4; len -= 4, bitmap += 4) {
935 weight = hweight32(*((u32 *)bitmap));
936 bitflips += 32 - weight;
937 if (unlikely(bitflips > bitflips_threshold))
938 return -EBADMSG;
939 }
940
941 for (; len > 0; len--, bitmap++) {
942 weight = hweight8(*bitmap);
943 bitflips += BITS_PER_BYTE - weight;
944 if (unlikely(bitflips > bitflips_threshold))
945 return -EBADMSG;
946 }
947
948 return bitflips;
949}
950
951/**
952 * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only
953 * 0xff data
954 * @data: data buffer to test
955 * @datalen: data length
956 * @ecc: ECC buffer
957 * @ecclen: ECC length
958 * @extraoob: extra OOB buffer
959 * @extraooblen: extra OOB length
960 * @bitflips_threshold: maximum number of bitflips
961 *
962 * Check if a data buffer and its associated ECC and OOB data contains only
963 * 0xff pattern, which means the underlying region has been erased and is
964 * ready to be programmed.
965 * The bitflips_threshold specify the maximum number of bitflips before
966 * considering the region as not erased.
967 *
968 * Note:
969 * 1/ ECC algorithms are working on pre-defined block sizes which are usually
970 * different from the NAND page size. When fixing bitflips, ECC engines will
971 * report the number of errors per chunk, and the NAND core infrastructure
972 * expect you to return the maximum number of bitflips for the whole page.
973 * This is why you should always use this function on a single chunk and
974 * not on the whole page. After checking each chunk you should update your
975 * max_bitflips value accordingly.
976 * 2/ When checking for bitflips in erased pages you should not only check
977 * the payload data but also their associated ECC data, because a user might
978 * have programmed almost all bits to 1 but a few. In this case, we
979 * shouldn't consider the chunk as erased, and checking ECC bytes prevent
980 * this case.
981 * 3/ The extraoob argument is optional, and should be used if some of your OOB
982 * data are protected by the ECC engine.
983 * It could also be used if you support subpages and want to attach some
984 * extra OOB data to an ECC chunk.
985 *
986 * Returns a positive number of bitflips less than or equal to
987 * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the
988 * threshold. In case of success, the passed buffers are filled with 0xff.
989 */
990int nand_check_erased_ecc_chunk(void *data, int datalen,
991 void *ecc, int ecclen,
992 void *extraoob, int extraooblen,
993 int bitflips_threshold)
994{
995 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
996
997 data_bitflips = nand_check_erased_buf(data, datalen,
998 bitflips_threshold);
999 if (data_bitflips < 0)
1000 return data_bitflips;
1001
1002 bitflips_threshold -= data_bitflips;
1003
1004 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1005 if (ecc_bitflips < 0)
1006 return ecc_bitflips;
1007
1008 bitflips_threshold -= ecc_bitflips;
1009
1010 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1011 bitflips_threshold);
1012 if (extraoob_bitflips < 0)
1013 return extraoob_bitflips;
1014
1015 if (data_bitflips)
1016 memset(data, 0xff, datalen);
1017
1018 if (ecc_bitflips)
1019 memset(ecc, 0xff, ecclen);
1020
1021 if (extraoob_bitflips)
1022 memset(extraoob, 0xff, extraooblen);
1023
1024 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1025}
1026EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1027
Wolfgang Denk932394a2005-08-17 12:55:25 +02001028/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001029 * nand_read_page_raw - [INTERN] read raw page data without ecc
1030 * @mtd: mtd info structure
1031 * @chip: nand chip info structure
1032 * @buf: buffer to store read data
1033 * @oob_required: caller requires OOB data read to chip->oob_poi
1034 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001035 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001036 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001037 */
William Juulcfa460a2007-10-31 13:53:06 +01001038static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001039 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001040{
William Juulcfa460a2007-10-31 13:53:06 +01001041 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001042 if (oob_required)
1043 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01001044 return 0;
1045}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001046
William Juulcfa460a2007-10-31 13:53:06 +01001047/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001048 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1049 * @mtd: mtd info structure
1050 * @chip: nand chip info structure
1051 * @buf: buffer to store read data
1052 * @oob_required: caller requires OOB data read to chip->oob_poi
1053 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001054 *
1055 * We need a special oob layout and handling even when OOB isn't used.
1056 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001057static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001058 struct nand_chip *chip, uint8_t *buf,
1059 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -05001060{
1061 int eccsize = chip->ecc.size;
1062 int eccbytes = chip->ecc.bytes;
1063 uint8_t *oob = chip->oob_poi;
1064 int steps, size;
1065
1066 for (steps = chip->ecc.steps; steps > 0; steps--) {
1067 chip->read_buf(mtd, buf, eccsize);
1068 buf += eccsize;
1069
1070 if (chip->ecc.prepad) {
1071 chip->read_buf(mtd, oob, chip->ecc.prepad);
1072 oob += chip->ecc.prepad;
1073 }
1074
1075 chip->read_buf(mtd, oob, eccbytes);
1076 oob += eccbytes;
1077
1078 if (chip->ecc.postpad) {
1079 chip->read_buf(mtd, oob, chip->ecc.postpad);
1080 oob += chip->ecc.postpad;
1081 }
1082 }
1083
1084 size = mtd->oobsize - (oob - chip->oob_poi);
1085 if (size)
1086 chip->read_buf(mtd, oob, size);
1087
1088 return 0;
1089}
1090
1091/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001092 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1093 * @mtd: mtd info structure
1094 * @chip: nand chip info structure
1095 * @buf: buffer to store read data
1096 * @oob_required: caller requires OOB data read to chip->oob_poi
1097 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001098 */
1099static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001100 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001101{
1102 int i, eccsize = chip->ecc.size;
1103 int eccbytes = chip->ecc.bytes;
1104 int eccsteps = chip->ecc.steps;
1105 uint8_t *p = buf;
1106 uint8_t *ecc_calc = chip->buffers->ecccalc;
1107 uint8_t *ecc_code = chip->buffers->ecccode;
1108 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001109 unsigned int max_bitflips = 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001110
Sergey Lapindfe64e22013-01-14 03:46:50 +00001111 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001112
William Juulcfa460a2007-10-31 13:53:06 +01001113 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1114 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001115
William Juulcfa460a2007-10-31 13:53:06 +01001116 for (i = 0; i < chip->ecc.total; i++)
1117 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001118
William Juulcfa460a2007-10-31 13:53:06 +01001119 eccsteps = chip->ecc.steps;
1120 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001121
William Juulcfa460a2007-10-31 13:53:06 +01001122 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1123 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001124
William Juulcfa460a2007-10-31 13:53:06 +01001125 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001126 if (stat < 0) {
Scott Woodc45912d2008-10-24 16:20:43 -05001127 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001128 } else {
Scott Woodc45912d2008-10-24 16:20:43 -05001129 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001130 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1131 }
Scott Woodc45912d2008-10-24 16:20:43 -05001132 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001133 return max_bitflips;
Scott Woodc45912d2008-10-24 16:20:43 -05001134}
1135
1136/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001137 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapindfe64e22013-01-14 03:46:50 +00001138 * @mtd: mtd info structure
1139 * @chip: nand chip info structure
1140 * @data_offs: offset of requested data within the page
1141 * @readlen: data length
1142 * @bufpoi: buffer to store read data
Heiko Schocher4e67c572014-07-15 16:08:43 +02001143 * @page: page number to read
Scott Woodc45912d2008-10-24 16:20:43 -05001144 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001145static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001146 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1147 int page)
Scott Woodc45912d2008-10-24 16:20:43 -05001148{
1149 int start_step, end_step, num_steps;
1150 uint32_t *eccpos = chip->ecc.layout->eccpos;
1151 uint8_t *p;
1152 int data_col_addr, i, gaps = 0;
1153 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1154 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001155 int index;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001156 unsigned int max_bitflips = 0;
Scott Woodc45912d2008-10-24 16:20:43 -05001157
Sergey Lapindfe64e22013-01-14 03:46:50 +00001158 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -05001159 start_step = data_offs / chip->ecc.size;
1160 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1161 num_steps = end_step - start_step + 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001162 index = start_step * chip->ecc.bytes;
Scott Woodc45912d2008-10-24 16:20:43 -05001163
Sergey Lapindfe64e22013-01-14 03:46:50 +00001164 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -05001165 datafrag_len = num_steps * chip->ecc.size;
1166 eccfrag_len = num_steps * chip->ecc.bytes;
1167
1168 data_col_addr = start_step * chip->ecc.size;
1169 /* If we read not a page aligned data */
1170 if (data_col_addr != 0)
1171 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1172
1173 p = bufpoi + data_col_addr;
1174 chip->read_buf(mtd, p, datafrag_len);
1175
Sergey Lapindfe64e22013-01-14 03:46:50 +00001176 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -05001177 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1178 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1179
Sergey Lapindfe64e22013-01-14 03:46:50 +00001180 /*
1181 * The performance is faster if we position offsets according to
1182 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1183 */
Scott Woodc45912d2008-10-24 16:20:43 -05001184 for (i = 0; i < eccfrag_len - 1; i++) {
Scott Woodd3963722015-06-26 19:03:26 -05001185 if (eccpos[i + index] + 1 != eccpos[i + index + 1]) {
Scott Woodc45912d2008-10-24 16:20:43 -05001186 gaps = 1;
1187 break;
1188 }
1189 }
1190 if (gaps) {
1191 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1192 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1193 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001194 /*
1195 * Send the command to read the particular ECC bytes take care
1196 * about buswidth alignment in read_buf.
1197 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001198 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -05001199 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001200 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001201 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001202 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001203 aligned_len++;
1204
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001205 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1206 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001207 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1208 }
1209
1210 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001211 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001212
1213 p = bufpoi + data_col_addr;
1214 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1215 int stat;
1216
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001217 stat = chip->ecc.correct(mtd, p,
1218 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Scott Woodceee07b2016-05-30 13:57:58 -05001219 if (stat == -EBADMSG &&
1220 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1221 /* check for empty pages with bitflips */
1222 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1223 &chip->buffers->ecccode[i],
1224 chip->ecc.bytes,
1225 NULL, 0,
1226 chip->ecc.strength);
1227 }
1228
Heiko Schocherff94bc42014-06-24 10:10:04 +02001229 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001230 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001231 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001232 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001233 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1234 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001235 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001236 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001237}
1238
Wolfgang Denk932394a2005-08-17 12:55:25 +02001239/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001240 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1241 * @mtd: mtd info structure
1242 * @chip: nand chip info structure
1243 * @buf: buffer to store read data
1244 * @oob_required: caller requires OOB data read to chip->oob_poi
1245 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001246 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001247 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001248 */
William Juulcfa460a2007-10-31 13:53:06 +01001249static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001250 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001251{
William Juulcfa460a2007-10-31 13:53:06 +01001252 int i, eccsize = chip->ecc.size;
1253 int eccbytes = chip->ecc.bytes;
1254 int eccsteps = chip->ecc.steps;
1255 uint8_t *p = buf;
1256 uint8_t *ecc_calc = chip->buffers->ecccalc;
1257 uint8_t *ecc_code = chip->buffers->ecccode;
1258 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001259 unsigned int max_bitflips = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001260
William Juulcfa460a2007-10-31 13:53:06 +01001261 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1262 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1263 chip->read_buf(mtd, p, eccsize);
1264 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1265 }
1266 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001267
William Juulcfa460a2007-10-31 13:53:06 +01001268 for (i = 0; i < chip->ecc.total; i++)
1269 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001270
William Juulcfa460a2007-10-31 13:53:06 +01001271 eccsteps = chip->ecc.steps;
1272 p = buf;
1273
1274 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1275 int stat;
1276
1277 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Scott Woodceee07b2016-05-30 13:57:58 -05001278 if (stat == -EBADMSG &&
1279 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1280 /* check for empty pages with bitflips */
1281 stat = nand_check_erased_ecc_chunk(p, eccsize,
1282 &ecc_code[i], eccbytes,
1283 NULL, 0,
1284 chip->ecc.strength);
1285 }
1286
Heiko Schocherff94bc42014-06-24 10:10:04 +02001287 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001288 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001289 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001290 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001291 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1292 }
William Juulcfa460a2007-10-31 13:53:06 +01001293 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001294 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001295}
1296
1297/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001298 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1299 * @mtd: mtd info structure
1300 * @chip: nand chip info structure
1301 * @buf: buffer to store read data
1302 * @oob_required: caller requires OOB data read to chip->oob_poi
1303 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001304 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001305 * Hardware ECC for large page chips, require OOB to be read first. For this
1306 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1307 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1308 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1309 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001310 */
1311static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001312 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001313{
1314 int i, eccsize = chip->ecc.size;
1315 int eccbytes = chip->ecc.bytes;
1316 int eccsteps = chip->ecc.steps;
1317 uint8_t *p = buf;
1318 uint8_t *ecc_code = chip->buffers->ecccode;
1319 uint32_t *eccpos = chip->ecc.layout->eccpos;
1320 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001321 unsigned int max_bitflips = 0;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001322
1323 /* Read the OOB area first */
1324 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1325 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1326 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1327
1328 for (i = 0; i < chip->ecc.total; i++)
1329 ecc_code[i] = chip->oob_poi[eccpos[i]];
1330
1331 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1332 int stat;
1333
1334 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1335 chip->read_buf(mtd, p, eccsize);
1336 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1337
1338 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Scott Woodceee07b2016-05-30 13:57:58 -05001339 if (stat == -EBADMSG &&
1340 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1341 /* check for empty pages with bitflips */
1342 stat = nand_check_erased_ecc_chunk(p, eccsize,
1343 &ecc_code[i], eccbytes,
1344 NULL, 0,
1345 chip->ecc.strength);
1346 }
1347
Heiko Schocherff94bc42014-06-24 10:10:04 +02001348 if (stat < 0) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001349 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001350 } else {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001351 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001352 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1353 }
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001354 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001355 return max_bitflips;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001356}
1357
1358/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001359 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1360 * @mtd: mtd info structure
1361 * @chip: nand chip info structure
1362 * @buf: buffer to store read data
1363 * @oob_required: caller requires OOB data read to chip->oob_poi
1364 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001365 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001366 * The hw generator calculates the error syndrome automatically. Therefore we
1367 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001368 */
1369static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001370 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001371{
1372 int i, eccsize = chip->ecc.size;
1373 int eccbytes = chip->ecc.bytes;
1374 int eccsteps = chip->ecc.steps;
Scott Woodceee07b2016-05-30 13:57:58 -05001375 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
William Juulcfa460a2007-10-31 13:53:06 +01001376 uint8_t *p = buf;
1377 uint8_t *oob = chip->oob_poi;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001378 unsigned int max_bitflips = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001379
1380 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1381 int stat;
1382
1383 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1384 chip->read_buf(mtd, p, eccsize);
1385
1386 if (chip->ecc.prepad) {
1387 chip->read_buf(mtd, oob, chip->ecc.prepad);
1388 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001389 }
1390
William Juulcfa460a2007-10-31 13:53:06 +01001391 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1392 chip->read_buf(mtd, oob, eccbytes);
1393 stat = chip->ecc.correct(mtd, p, oob, NULL);
1394
William Juulcfa460a2007-10-31 13:53:06 +01001395 oob += eccbytes;
1396
1397 if (chip->ecc.postpad) {
1398 chip->read_buf(mtd, oob, chip->ecc.postpad);
1399 oob += chip->ecc.postpad;
1400 }
Scott Woodceee07b2016-05-30 13:57:58 -05001401
1402 if (stat == -EBADMSG &&
1403 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1404 /* check for empty pages with bitflips */
1405 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1406 oob - eccpadbytes,
1407 eccpadbytes,
1408 NULL, 0,
1409 chip->ecc.strength);
1410 }
1411
1412 if (stat < 0) {
1413 mtd->ecc_stats.failed++;
1414 } else {
1415 mtd->ecc_stats.corrected += stat;
1416 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1417 }
William Juulcfa460a2007-10-31 13:53:06 +01001418 }
1419
1420 /* Calculate remaining oob bytes */
1421 i = mtd->oobsize - (oob - chip->oob_poi);
1422 if (i)
1423 chip->read_buf(mtd, oob, i);
1424
Heiko Schocherff94bc42014-06-24 10:10:04 +02001425 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001426}
1427
1428/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001429 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1430 * @chip: nand chip structure
1431 * @oob: oob destination address
1432 * @ops: oob ops structure
1433 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001434 */
1435static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1436 struct mtd_oob_ops *ops, size_t len)
1437{
Christian Hitz90e3f392011-10-12 09:32:01 +02001438 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001439
Sergey Lapindfe64e22013-01-14 03:46:50 +00001440 case MTD_OPS_PLACE_OOB:
1441 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001442 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1443 return oob + len;
1444
Sergey Lapindfe64e22013-01-14 03:46:50 +00001445 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001446 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1447 uint32_t boffs = 0, roffs = ops->ooboffs;
1448 size_t bytes = 0;
1449
Christian Hitz90e3f392011-10-12 09:32:01 +02001450 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001451 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001452 if (unlikely(roffs)) {
1453 if (roffs >= free->length) {
1454 roffs -= free->length;
1455 continue;
1456 }
1457 boffs = free->offset + roffs;
1458 bytes = min_t(size_t, len,
1459 (free->length - roffs));
1460 roffs = 0;
1461 } else {
1462 bytes = min_t(size_t, len, free->length);
1463 boffs = free->offset;
1464 }
1465 memcpy(oob, chip->oob_poi + boffs, bytes);
1466 oob += bytes;
1467 }
1468 return oob;
1469 }
1470 default:
1471 BUG();
1472 }
1473 return NULL;
1474}
1475
1476/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001477 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1478 * @mtd: MTD device structure
1479 * @retry_mode: the retry mode to use
1480 *
1481 * Some vendors supply a special command to shift the Vt threshold, to be used
1482 * when there are too many bitflips in a page (i.e., ECC error). After setting
1483 * a new threshold, the host should retry reading the page.
1484 */
1485static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1486{
Scott Wood17cb4b82016-05-30 13:57:56 -05001487 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001488
1489 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1490
1491 if (retry_mode >= chip->read_retries)
1492 return -EINVAL;
1493
1494 if (!chip->setup_read_retry)
1495 return -EOPNOTSUPP;
1496
1497 return chip->setup_read_retry(mtd, retry_mode);
1498}
1499
1500/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001501 * nand_do_read_ops - [INTERN] Read data with ECC
1502 * @mtd: MTD device structure
1503 * @from: offset to read from
1504 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001505 *
1506 * Internal function. Called with chip held.
1507 */
1508static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1509 struct mtd_oob_ops *ops)
1510{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001511 int chipnr, page, realpage, col, bytes, aligned, oob_required;
Scott Wood17cb4b82016-05-30 13:57:56 -05001512 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01001513 int ret = 0;
1514 uint32_t readlen = ops->len;
1515 uint32_t oobreadlen = ops->ooblen;
Scott Woodceee07b2016-05-30 13:57:58 -05001516 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001517
William Juulcfa460a2007-10-31 13:53:06 +01001518 uint8_t *bufpoi, *oob, *buf;
Scott Woodd3963722015-06-26 19:03:26 -05001519 int use_bufpoi;
Paul Burton40462e52013-09-04 15:16:56 +01001520 unsigned int max_bitflips = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001521 int retry_mode = 0;
1522 bool ecc_fail = false;
William Juulcfa460a2007-10-31 13:53:06 +01001523
1524 chipnr = (int)(from >> chip->chip_shift);
1525 chip->select_chip(mtd, chipnr);
1526
1527 realpage = (int)(from >> chip->page_shift);
1528 page = realpage & chip->pagemask;
1529
1530 col = (int)(from & (mtd->writesize - 1));
1531
1532 buf = ops->datbuf;
1533 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001534 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001535
Christian Hitz90e3f392011-10-12 09:32:01 +02001536 while (1) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001537 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Wood6f2ffc32011-02-02 18:15:57 -06001538
Heiko Schocherff94bc42014-06-24 10:10:04 +02001539 WATCHDOG_RESET();
William Juulcfa460a2007-10-31 13:53:06 +01001540 bytes = min(mtd->writesize - col, readlen);
1541 aligned = (bytes == mtd->writesize);
1542
Scott Woodd3963722015-06-26 19:03:26 -05001543 if (!aligned)
1544 use_bufpoi = 1;
1545 else
1546 use_bufpoi = 0;
1547
Sergey Lapindfe64e22013-01-14 03:46:50 +00001548 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001549 if (realpage != chip->pagebuf || oob) {
Scott Woodd3963722015-06-26 19:03:26 -05001550 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
1551
1552 if (use_bufpoi && aligned)
1553 pr_debug("%s: using read bounce buffer for buf@%p\n",
1554 __func__, buf);
William Juulcfa460a2007-10-31 13:53:06 +01001555
Heiko Schocherff94bc42014-06-24 10:10:04 +02001556read_retry:
Sergey Lapindfe64e22013-01-14 03:46:50 +00001557 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001558
Paul Burton40462e52013-09-04 15:16:56 +01001559 /*
1560 * Now read the page into the buffer. Absent an error,
1561 * the read methods return max bitflips per ecc step.
1562 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001563 if (unlikely(ops->mode == MTD_OPS_RAW))
1564 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1565 oob_required,
1566 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001567 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001568 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001569 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001570 col, bytes, bufpoi,
1571 page);
William Juulcfa460a2007-10-31 13:53:06 +01001572 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001573 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001574 oob_required, page);
1575 if (ret < 0) {
Scott Woodd3963722015-06-26 19:03:26 -05001576 if (use_bufpoi)
Sergey Lapindfe64e22013-01-14 03:46:50 +00001577 /* Invalidate page cache */
1578 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001579 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001580 }
William Juulcfa460a2007-10-31 13:53:06 +01001581
Paul Burton40462e52013-09-04 15:16:56 +01001582 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1583
William Juulcfa460a2007-10-31 13:53:06 +01001584 /* Transfer not aligned data */
Scott Woodd3963722015-06-26 19:03:26 -05001585 if (use_bufpoi) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001586 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001587 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton40462e52013-09-04 15:16:56 +01001588 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001589 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001590 chip->pagebuf_bitflips = ret;
1591 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001592 /* Invalidate page cache */
1593 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001594 }
William Juulcfa460a2007-10-31 13:53:06 +01001595 memcpy(buf, chip->buffers->databuf + col, bytes);
1596 }
1597
William Juulcfa460a2007-10-31 13:53:06 +01001598 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001599 int toread = min(oobreadlen, max_oobsize);
1600
1601 if (toread) {
1602 oob = nand_transfer_oob(chip,
1603 oob, ops, toread);
1604 oobreadlen -= toread;
1605 }
William Juulcfa460a2007-10-31 13:53:06 +01001606 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001607
1608 if (chip->options & NAND_NEED_READRDY) {
1609 /* Apply delay or wait for ready/busy pin */
1610 if (!chip->dev_ready)
1611 udelay(chip->chip_delay);
1612 else
1613 nand_wait_ready(mtd);
1614 }
1615
1616 if (mtd->ecc_stats.failed - ecc_failures) {
1617 if (retry_mode + 1 < chip->read_retries) {
1618 retry_mode++;
1619 ret = nand_setup_read_retry(mtd,
1620 retry_mode);
1621 if (ret < 0)
1622 break;
1623
1624 /* Reset failures; retry */
1625 mtd->ecc_stats.failed = ecc_failures;
1626 goto read_retry;
1627 } else {
1628 /* No more retry modes; real failure */
1629 ecc_fail = true;
1630 }
1631 }
1632
1633 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001634 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001635 memcpy(buf, chip->buffers->databuf + col, bytes);
1636 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001637 max_bitflips = max_t(unsigned int, max_bitflips,
1638 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001639 }
1640
William Juulcfa460a2007-10-31 13:53:06 +01001641 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001642
Heiko Schocherff94bc42014-06-24 10:10:04 +02001643 /* Reset to retry mode 0 */
1644 if (retry_mode) {
1645 ret = nand_setup_read_retry(mtd, 0);
1646 if (ret < 0)
1647 break;
1648 retry_mode = 0;
1649 }
1650
William Juulcfa460a2007-10-31 13:53:06 +01001651 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001652 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001653
Sergey Lapindfe64e22013-01-14 03:46:50 +00001654 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001655 col = 0;
1656 /* Increment page address */
1657 realpage++;
1658
William Juulcfa460a2007-10-31 13:53:06 +01001659 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001660 /* Check, if we cross a chip boundary */
1661 if (!page) {
1662 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001663 chip->select_chip(mtd, -1);
1664 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001665 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001666 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001667 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001668
William Juulcfa460a2007-10-31 13:53:06 +01001669 ops->retlen = ops->len - (size_t) readlen;
1670 if (oob)
1671 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001672
Heiko Schocherff94bc42014-06-24 10:10:04 +02001673 if (ret < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001674 return ret;
1675
Heiko Schocherff94bc42014-06-24 10:10:04 +02001676 if (ecc_fail)
William Juulcfa460a2007-10-31 13:53:06 +01001677 return -EBADMSG;
1678
Paul Burton40462e52013-09-04 15:16:56 +01001679 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001680}
1681
1682/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001683 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001684 * @mtd: MTD device structure
1685 * @from: offset to read from
1686 * @len: number of bytes to read
1687 * @retlen: pointer to variable to store the number of read bytes
1688 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001689 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001690 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001691 */
William Juulcfa460a2007-10-31 13:53:06 +01001692static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1693 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001694{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001695 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001696 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001697
Heiko Schocherff94bc42014-06-24 10:10:04 +02001698 nand_get_device(mtd, FL_READING);
Scott Woodd3963722015-06-26 19:03:26 -05001699 memset(&ops, 0, sizeof(ops));
Sergey Lapindfe64e22013-01-14 03:46:50 +00001700 ops.len = len;
1701 ops.datbuf = buf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001702 ops.mode = MTD_OPS_PLACE_OOB;
1703 ret = nand_do_read_ops(mtd, from, &ops);
1704 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001705 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001706 return ret;
1707}
1708
William Juulcfa460a2007-10-31 13:53:06 +01001709/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001710 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1711 * @mtd: mtd info structure
1712 * @chip: nand chip info structure
1713 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001714 */
1715static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001716 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001717{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001718 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001719 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001720 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001721}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001722
1723/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001724 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001725 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001726 * @mtd: mtd info structure
1727 * @chip: nand chip info structure
1728 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001729 */
1730static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001731 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001732{
William Juulcfa460a2007-10-31 13:53:06 +01001733 int length = mtd->oobsize;
1734 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1735 int eccsize = chip->ecc.size;
Scott Woodd3963722015-06-26 19:03:26 -05001736 uint8_t *bufpoi = chip->oob_poi;
William Juulcfa460a2007-10-31 13:53:06 +01001737 int i, toread, sndrnd = 0, pos;
1738
1739 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1740 for (i = 0; i < chip->ecc.steps; i++) {
1741 if (sndrnd) {
1742 pos = eccsize + i * (eccsize + chunk);
1743 if (mtd->writesize > 512)
1744 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1745 else
1746 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1747 } else
1748 sndrnd = 1;
1749 toread = min_t(int, length, chunk);
1750 chip->read_buf(mtd, bufpoi, toread);
1751 bufpoi += toread;
1752 length -= toread;
1753 }
1754 if (length > 0)
1755 chip->read_buf(mtd, bufpoi, length);
1756
Sergey Lapindfe64e22013-01-14 03:46:50 +00001757 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001758}
1759
1760/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001761 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1762 * @mtd: mtd info structure
1763 * @chip: nand chip info structure
1764 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001765 */
1766static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1767 int page)
1768{
1769 int status = 0;
1770 const uint8_t *buf = chip->oob_poi;
1771 int length = mtd->oobsize;
1772
1773 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1774 chip->write_buf(mtd, buf, length);
1775 /* Send command to program the OOB data */
1776 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1777
1778 status = chip->waitfunc(mtd, chip);
1779
1780 return status & NAND_STATUS_FAIL ? -EIO : 0;
1781}
1782
1783/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001784 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1785 * with syndrome - only for large page flash
1786 * @mtd: mtd info structure
1787 * @chip: nand chip info structure
1788 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001789 */
1790static int nand_write_oob_syndrome(struct mtd_info *mtd,
1791 struct nand_chip *chip, int page)
1792{
1793 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1794 int eccsize = chip->ecc.size, length = mtd->oobsize;
1795 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1796 const uint8_t *bufpoi = chip->oob_poi;
1797
1798 /*
1799 * data-ecc-data-ecc ... ecc-oob
1800 * or
1801 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1802 */
1803 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1804 pos = steps * (eccsize + chunk);
1805 steps = 0;
1806 } else
1807 pos = eccsize;
1808
1809 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1810 for (i = 0; i < steps; i++) {
1811 if (sndcmd) {
1812 if (mtd->writesize <= 512) {
1813 uint32_t fill = 0xFFFFFFFF;
1814
1815 len = eccsize;
1816 while (len > 0) {
1817 int num = min_t(int, len, 4);
1818 chip->write_buf(mtd, (uint8_t *)&fill,
1819 num);
1820 len -= num;
1821 }
1822 } else {
1823 pos = eccsize + i * (eccsize + chunk);
1824 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1825 }
1826 } else
1827 sndcmd = 1;
1828 len = min_t(int, length, chunk);
1829 chip->write_buf(mtd, bufpoi, len);
1830 bufpoi += len;
1831 length -= len;
1832 }
1833 if (length > 0)
1834 chip->write_buf(mtd, bufpoi, length);
1835
1836 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1837 status = chip->waitfunc(mtd, chip);
1838
1839 return status & NAND_STATUS_FAIL ? -EIO : 0;
1840}
1841
1842/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001843 * nand_do_read_oob - [INTERN] NAND read out-of-band
1844 * @mtd: MTD device structure
1845 * @from: offset to read from
1846 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001847 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001848 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01001849 */
1850static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1851 struct mtd_oob_ops *ops)
1852{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001853 int page, realpage, chipnr;
Scott Wood17cb4b82016-05-30 13:57:56 -05001854 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001855 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001856 int readlen = ops->ooblen;
1857 int len;
1858 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001859 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001860
Heiko Schocherff94bc42014-06-24 10:10:04 +02001861 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02001862 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001863
Sergey Lapindfe64e22013-01-14 03:46:50 +00001864 stats = mtd->ecc_stats;
1865
Scott Woodceee07b2016-05-30 13:57:58 -05001866 len = mtd_oobavail(mtd, ops);
William Juulcfa460a2007-10-31 13:53:06 +01001867
1868 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001869 pr_debug("%s: attempt to start read outside oob\n",
1870 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001871 return -EINVAL;
1872 }
1873
1874 /* Do not allow reads past end of device */
1875 if (unlikely(from >= mtd->size ||
1876 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1877 (from >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001878 pr_debug("%s: attempt to read beyond end of device\n",
1879 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001880 return -EINVAL;
1881 }
1882
1883 chipnr = (int)(from >> chip->chip_shift);
1884 chip->select_chip(mtd, chipnr);
1885
1886 /* Shift to get page */
1887 realpage = (int)(from >> chip->page_shift);
1888 page = realpage & chip->pagemask;
1889
Christian Hitz90e3f392011-10-12 09:32:01 +02001890 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06001891 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02001892
Sergey Lapindfe64e22013-01-14 03:46:50 +00001893 if (ops->mode == MTD_OPS_RAW)
1894 ret = chip->ecc.read_oob_raw(mtd, chip, page);
1895 else
1896 ret = chip->ecc.read_oob(mtd, chip, page);
1897
1898 if (ret < 0)
1899 break;
William Juulcfa460a2007-10-31 13:53:06 +01001900
1901 len = min(len, readlen);
1902 buf = nand_transfer_oob(chip, buf, ops, len);
1903
Heiko Schocherff94bc42014-06-24 10:10:04 +02001904 if (chip->options & NAND_NEED_READRDY) {
1905 /* Apply delay or wait for ready/busy pin */
1906 if (!chip->dev_ready)
1907 udelay(chip->chip_delay);
1908 else
1909 nand_wait_ready(mtd);
1910 }
1911
William Juulcfa460a2007-10-31 13:53:06 +01001912 readlen -= len;
1913 if (!readlen)
1914 break;
1915
1916 /* Increment page address */
1917 realpage++;
1918
1919 page = realpage & chip->pagemask;
1920 /* Check, if we cross a chip boundary */
1921 if (!page) {
1922 chipnr++;
1923 chip->select_chip(mtd, -1);
1924 chip->select_chip(mtd, chipnr);
1925 }
William Juulcfa460a2007-10-31 13:53:06 +01001926 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001927 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01001928
Sergey Lapindfe64e22013-01-14 03:46:50 +00001929 ops->oobretlen = ops->ooblen - readlen;
1930
1931 if (ret < 0)
1932 return ret;
1933
1934 if (mtd->ecc_stats.failed - stats.failed)
1935 return -EBADMSG;
1936
1937 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001938}
1939
1940/**
1941 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00001942 * @mtd: MTD device structure
1943 * @from: offset to read from
1944 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01001945 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001946 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01001947 */
1948static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1949 struct mtd_oob_ops *ops)
1950{
William Juulcfa460a2007-10-31 13:53:06 +01001951 int ret = -ENOTSUPP;
1952
1953 ops->retlen = 0;
1954
1955 /* Do not allow reads past end of device */
1956 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001957 pr_debug("%s: attempt to read beyond end of device\n",
1958 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001959 return -EINVAL;
1960 }
1961
Heiko Schocherff94bc42014-06-24 10:10:04 +02001962 nand_get_device(mtd, FL_READING);
William Juulcfa460a2007-10-31 13:53:06 +01001963
Christian Hitz90e3f392011-10-12 09:32:01 +02001964 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001965 case MTD_OPS_PLACE_OOB:
1966 case MTD_OPS_AUTO_OOB:
1967 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001968 break;
1969
1970 default:
1971 goto out;
1972 }
1973
1974 if (!ops->datbuf)
1975 ret = nand_do_read_oob(mtd, from, ops);
1976 else
1977 ret = nand_do_read_ops(mtd, from, ops);
1978
Christian Hitz90e3f392011-10-12 09:32:01 +02001979out:
William Juulcfa460a2007-10-31 13:53:06 +01001980 nand_release_device(mtd);
1981 return ret;
1982}
1983
1984
1985/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001986 * nand_write_page_raw - [INTERN] raw page write function
1987 * @mtd: mtd info structure
1988 * @chip: nand chip info structure
1989 * @buf: data buffer
1990 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05001991 * @page: page number to write
David Brownell7e866612009-11-07 16:27:01 -05001992 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001993 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01001994 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001995static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Scott Woodceee07b2016-05-30 13:57:58 -05001996 const uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001997{
1998 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001999 if (oob_required)
2000 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2001
2002 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002003}
2004
2005/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002006 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2007 * @mtd: mtd info structure
2008 * @chip: nand chip info structure
2009 * @buf: data buffer
2010 * @oob_required: must write chip->oob_poi to OOB
Scott Woodceee07b2016-05-30 13:57:58 -05002011 * @page: page number to write
David Brownell7e866612009-11-07 16:27:01 -05002012 *
2013 * We need a special oob layout and handling even when ECC isn't checked.
2014 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002015static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02002016 struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -05002017 const uint8_t *buf, int oob_required,
2018 int page)
David Brownell7e866612009-11-07 16:27:01 -05002019{
2020 int eccsize = chip->ecc.size;
2021 int eccbytes = chip->ecc.bytes;
2022 uint8_t *oob = chip->oob_poi;
2023 int steps, size;
2024
2025 for (steps = chip->ecc.steps; steps > 0; steps--) {
2026 chip->write_buf(mtd, buf, eccsize);
2027 buf += eccsize;
2028
2029 if (chip->ecc.prepad) {
2030 chip->write_buf(mtd, oob, chip->ecc.prepad);
2031 oob += chip->ecc.prepad;
2032 }
2033
Heiko Schocher4e67c572014-07-15 16:08:43 +02002034 chip->write_buf(mtd, oob, eccbytes);
David Brownell7e866612009-11-07 16:27:01 -05002035 oob += eccbytes;
2036
2037 if (chip->ecc.postpad) {
2038 chip->write_buf(mtd, oob, chip->ecc.postpad);
2039 oob += chip->ecc.postpad;
2040 }
2041 }
2042
2043 size = mtd->oobsize - (oob - chip->oob_poi);
2044 if (size)
2045 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002046
2047 return 0;
David Brownell7e866612009-11-07 16:27:01 -05002048}
2049/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002050 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2051 * @mtd: mtd info structure
2052 * @chip: nand chip info structure
2053 * @buf: data buffer
2054 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05002055 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01002056 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002057static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Woodceee07b2016-05-30 13:57:58 -05002058 const uint8_t *buf, int oob_required,
2059 int page)
William Juulcfa460a2007-10-31 13:53:06 +01002060{
2061 int i, eccsize = chip->ecc.size;
2062 int eccbytes = chip->ecc.bytes;
2063 int eccsteps = chip->ecc.steps;
2064 uint8_t *ecc_calc = chip->buffers->ecccalc;
2065 const uint8_t *p = buf;
2066 uint32_t *eccpos = chip->ecc.layout->eccpos;
2067
Sergey Lapindfe64e22013-01-14 03:46:50 +00002068 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01002069 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2070 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2071
2072 for (i = 0; i < chip->ecc.total; i++)
2073 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2074
Scott Wood81c77252016-05-30 13:57:57 -05002075 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
William Juulcfa460a2007-10-31 13:53:06 +01002076}
2077
2078/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002079 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2080 * @mtd: mtd info structure
2081 * @chip: nand chip info structure
2082 * @buf: data buffer
2083 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05002084 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01002085 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002086static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -05002087 const uint8_t *buf, int oob_required,
2088 int page)
William Juulcfa460a2007-10-31 13:53:06 +01002089{
2090 int i, eccsize = chip->ecc.size;
2091 int eccbytes = chip->ecc.bytes;
2092 int eccsteps = chip->ecc.steps;
2093 uint8_t *ecc_calc = chip->buffers->ecccalc;
2094 const uint8_t *p = buf;
2095 uint32_t *eccpos = chip->ecc.layout->eccpos;
2096
2097 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2098 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2099 chip->write_buf(mtd, p, eccsize);
2100 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2101 }
2102
2103 for (i = 0; i < chip->ecc.total; i++)
2104 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2105
2106 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002107
2108 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002109}
2110
Heiko Schocherff94bc42014-06-24 10:10:04 +02002111
2112/**
Scott Woodd3963722015-06-26 19:03:26 -05002113 * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write
Heiko Schocherff94bc42014-06-24 10:10:04 +02002114 * @mtd: mtd info structure
2115 * @chip: nand chip info structure
2116 * @offset: column address of subpage within the page
2117 * @data_len: data length
2118 * @buf: data buffer
2119 * @oob_required: must write chip->oob_poi to OOB
Scott Wood81c77252016-05-30 13:57:57 -05002120 * @page: page number to write
Heiko Schocherff94bc42014-06-24 10:10:04 +02002121 */
2122static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2123 struct nand_chip *chip, uint32_t offset,
2124 uint32_t data_len, const uint8_t *buf,
Scott Wood81c77252016-05-30 13:57:57 -05002125 int oob_required, int page)
Heiko Schocherff94bc42014-06-24 10:10:04 +02002126{
2127 uint8_t *oob_buf = chip->oob_poi;
2128 uint8_t *ecc_calc = chip->buffers->ecccalc;
2129 int ecc_size = chip->ecc.size;
2130 int ecc_bytes = chip->ecc.bytes;
2131 int ecc_steps = chip->ecc.steps;
2132 uint32_t *eccpos = chip->ecc.layout->eccpos;
2133 uint32_t start_step = offset / ecc_size;
2134 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2135 int oob_bytes = mtd->oobsize / ecc_steps;
2136 int step, i;
2137
2138 for (step = 0; step < ecc_steps; step++) {
2139 /* configure controller for WRITE access */
2140 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2141
2142 /* write data (untouched subpages already masked by 0xFF) */
2143 chip->write_buf(mtd, buf, ecc_size);
2144
2145 /* mask ECC of un-touched subpages by padding 0xFF */
2146 if ((step < start_step) || (step > end_step))
2147 memset(ecc_calc, 0xff, ecc_bytes);
2148 else
2149 chip->ecc.calculate(mtd, buf, ecc_calc);
2150
2151 /* mask OOB of un-touched subpages by padding 0xFF */
2152 /* if oob_required, preserve OOB metadata of written subpage */
2153 if (!oob_required || (step < start_step) || (step > end_step))
2154 memset(oob_buf, 0xff, oob_bytes);
2155
2156 buf += ecc_size;
2157 ecc_calc += ecc_bytes;
2158 oob_buf += oob_bytes;
2159 }
2160
2161 /* copy calculated ECC for whole page to chip->buffer->oob */
2162 /* this include masked-value(0xFF) for unwritten subpages */
2163 ecc_calc = chip->buffers->ecccalc;
2164 for (i = 0; i < chip->ecc.total; i++)
2165 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2166
2167 /* write OOB buffer to NAND device */
2168 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2169
2170 return 0;
2171}
2172
2173
William Juulcfa460a2007-10-31 13:53:06 +01002174/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002175 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2176 * @mtd: mtd info structure
2177 * @chip: nand chip info structure
2178 * @buf: data buffer
2179 * @oob_required: must write chip->oob_poi to OOB
Scott Woodceee07b2016-05-30 13:57:58 -05002180 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01002181 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002182 * The hw generator calculates the error syndrome automatically. Therefore we
2183 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01002184 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002185static int nand_write_page_syndrome(struct mtd_info *mtd,
2186 struct nand_chip *chip,
Scott Wood81c77252016-05-30 13:57:57 -05002187 const uint8_t *buf, int oob_required,
2188 int page)
William Juulcfa460a2007-10-31 13:53:06 +01002189{
2190 int i, eccsize = chip->ecc.size;
2191 int eccbytes = chip->ecc.bytes;
2192 int eccsteps = chip->ecc.steps;
2193 const uint8_t *p = buf;
2194 uint8_t *oob = chip->oob_poi;
2195
2196 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2197
2198 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2199 chip->write_buf(mtd, p, eccsize);
2200
2201 if (chip->ecc.prepad) {
2202 chip->write_buf(mtd, oob, chip->ecc.prepad);
2203 oob += chip->ecc.prepad;
2204 }
2205
2206 chip->ecc.calculate(mtd, p, oob);
2207 chip->write_buf(mtd, oob, eccbytes);
2208 oob += eccbytes;
2209
2210 if (chip->ecc.postpad) {
2211 chip->write_buf(mtd, oob, chip->ecc.postpad);
2212 oob += chip->ecc.postpad;
2213 }
2214 }
2215
2216 /* Calculate remaining oob bytes */
2217 i = mtd->oobsize - (oob - chip->oob_poi);
2218 if (i)
2219 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002220
2221 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002222}
2223
2224/**
2225 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00002226 * @mtd: MTD device structure
2227 * @chip: NAND chip descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +02002228 * @offset: address offset within the page
2229 * @data_len: length of actual data to be written
Sergey Lapindfe64e22013-01-14 03:46:50 +00002230 * @buf: the data to write
2231 * @oob_required: must write chip->oob_poi to OOB
2232 * @page: page number to write
2233 * @cached: cached programming
2234 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01002235 */
2236static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02002237 uint32_t offset, int data_len, const uint8_t *buf,
2238 int oob_required, int page, int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01002239{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002240 int status, subpage;
2241
2242 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2243 chip->ecc.write_subpage)
2244 subpage = offset || (data_len < mtd->writesize);
2245 else
2246 subpage = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002247
2248 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2249
2250 if (unlikely(raw))
Heiko Schocherff94bc42014-06-24 10:10:04 +02002251 status = chip->ecc.write_page_raw(mtd, chip, buf,
Scott Wood81c77252016-05-30 13:57:57 -05002252 oob_required, page);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002253 else if (subpage)
2254 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
Scott Woodceee07b2016-05-30 13:57:58 -05002255 buf, oob_required, page);
William Juulcfa460a2007-10-31 13:53:06 +01002256 else
Scott Wood81c77252016-05-30 13:57:57 -05002257 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2258 page);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002259
2260 if (status < 0)
2261 return status;
William Juulcfa460a2007-10-31 13:53:06 +01002262
2263 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002264 * Cached progamming disabled for now. Not sure if it's worth the
2265 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01002266 */
2267 cached = 0;
2268
Heiko Schocherff94bc42014-06-24 10:10:04 +02002269 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juulcfa460a2007-10-31 13:53:06 +01002270
2271 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2272 status = chip->waitfunc(mtd, chip);
2273 /*
2274 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00002275 * available.
William Juulcfa460a2007-10-31 13:53:06 +01002276 */
2277 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2278 status = chip->errstat(mtd, chip, FL_WRITING, status,
2279 page);
2280
2281 if (status & NAND_STATUS_FAIL)
2282 return -EIO;
2283 } else {
2284 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2285 status = chip->waitfunc(mtd, chip);
2286 }
2287
William Juulcfa460a2007-10-31 13:53:06 +01002288 return 0;
2289}
2290
2291/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002292 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2293 * @mtd: MTD device structure
2294 * @oob: oob data buffer
2295 * @len: oob data write length
2296 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01002297 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002298static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2299 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01002300{
Scott Wood17cb4b82016-05-30 13:57:56 -05002301 struct nand_chip *chip = mtd_to_nand(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002302
2303 /*
2304 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2305 * data from a previous OOB read.
2306 */
2307 memset(chip->oob_poi, 0xff, mtd->oobsize);
2308
Christian Hitz90e3f392011-10-12 09:32:01 +02002309 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01002310
Sergey Lapindfe64e22013-01-14 03:46:50 +00002311 case MTD_OPS_PLACE_OOB:
2312 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002313 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2314 return oob + len;
2315
Sergey Lapindfe64e22013-01-14 03:46:50 +00002316 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01002317 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2318 uint32_t boffs = 0, woffs = ops->ooboffs;
2319 size_t bytes = 0;
2320
Christian Hitz90e3f392011-10-12 09:32:01 +02002321 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002322 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01002323 if (unlikely(woffs)) {
2324 if (woffs >= free->length) {
2325 woffs -= free->length;
2326 continue;
2327 }
2328 boffs = free->offset + woffs;
2329 bytes = min_t(size_t, len,
2330 (free->length - woffs));
2331 woffs = 0;
2332 } else {
2333 bytes = min_t(size_t, len, free->length);
2334 boffs = free->offset;
2335 }
2336 memcpy(chip->oob_poi + boffs, oob, bytes);
2337 oob += bytes;
2338 }
2339 return oob;
2340 }
2341 default:
2342 BUG();
2343 }
2344 return NULL;
2345}
2346
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002347#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01002348
2349/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002350 * nand_do_write_ops - [INTERN] NAND write with ECC
2351 * @mtd: MTD device structure
2352 * @to: offset to write to
2353 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002354 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002355 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002356 */
2357static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2358 struct mtd_oob_ops *ops)
2359{
2360 int chipnr, realpage, page, blockmask, column;
Scott Wood17cb4b82016-05-30 13:57:56 -05002361 struct nand_chip *chip = mtd_to_nand(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002362 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002363
2364 uint32_t oobwritelen = ops->ooblen;
Scott Woodceee07b2016-05-30 13:57:58 -05002365 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002366
William Juulcfa460a2007-10-31 13:53:06 +01002367 uint8_t *oob = ops->oobbuf;
2368 uint8_t *buf = ops->datbuf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002369 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002370 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002371
2372 ops->retlen = 0;
2373 if (!writelen)
2374 return 0;
2375
Heiko Schocherff94bc42014-06-24 10:10:04 +02002376 /* Reject writes, which are not page aligned */
2377 if (NOTALIGNED(to)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002378 pr_notice("%s: attempt to write non page aligned data\n",
2379 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002380 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002381 }
2382
2383 column = to & (mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +01002384
2385 chipnr = (int)(to >> chip->chip_shift);
2386 chip->select_chip(mtd, chipnr);
2387
2388 /* Check, if it is write protected */
2389 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002390 ret = -EIO;
2391 goto err_out;
William Juulcfa460a2007-10-31 13:53:06 +01002392 }
2393
2394 realpage = (int)(to >> chip->page_shift);
2395 page = realpage & chip->pagemask;
2396 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2397
2398 /* Invalidate the page cache, when we write to the cached page */
Scott Woodd3963722015-06-26 19:03:26 -05002399 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2400 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
William Juulcfa460a2007-10-31 13:53:06 +01002401 chip->pagebuf = -1;
2402
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002403 /* Don't allow multipage oob writes with offset */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002404 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2405 ret = -EINVAL;
2406 goto err_out;
2407 }
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002408
Christian Hitz90e3f392011-10-12 09:32:01 +02002409 while (1) {
William Juulcfa460a2007-10-31 13:53:06 +01002410 int bytes = mtd->writesize;
2411 int cached = writelen > bytes && page != blockmask;
2412 uint8_t *wbuf = buf;
Scott Woodd3963722015-06-26 19:03:26 -05002413 int use_bufpoi;
2414 int part_pagewr = (column || writelen < (mtd->writesize - 1));
2415
2416 if (part_pagewr)
2417 use_bufpoi = 1;
2418 else
2419 use_bufpoi = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002420
Heiko Schocherff94bc42014-06-24 10:10:04 +02002421 WATCHDOG_RESET();
Scott Woodd3963722015-06-26 19:03:26 -05002422 /* Partial page write?, or need to use bounce buffer */
2423 if (use_bufpoi) {
2424 pr_debug("%s: using write bounce buffer for buf@%p\n",
2425 __func__, buf);
William Juulcfa460a2007-10-31 13:53:06 +01002426 cached = 0;
Scott Woodd3963722015-06-26 19:03:26 -05002427 if (part_pagewr)
2428 bytes = min_t(int, bytes - column, writelen);
William Juulcfa460a2007-10-31 13:53:06 +01002429 chip->pagebuf = -1;
2430 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2431 memcpy(&chip->buffers->databuf[column], buf, bytes);
2432 wbuf = chip->buffers->databuf;
2433 }
2434
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002435 if (unlikely(oob)) {
2436 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002437 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002438 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002439 } else {
2440 /* We still need to erase leftover OOB data */
2441 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002442 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002443 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2444 oob_required, page, cached,
2445 (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002446 if (ret)
2447 break;
2448
2449 writelen -= bytes;
2450 if (!writelen)
2451 break;
2452
2453 column = 0;
2454 buf += bytes;
2455 realpage++;
2456
2457 page = realpage & chip->pagemask;
2458 /* Check, if we cross a chip boundary */
2459 if (!page) {
2460 chipnr++;
2461 chip->select_chip(mtd, -1);
2462 chip->select_chip(mtd, chipnr);
2463 }
2464 }
2465
2466 ops->retlen = ops->len - writelen;
2467 if (unlikely(oob))
2468 ops->oobretlen = ops->ooblen;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002469
2470err_out:
2471 chip->select_chip(mtd, -1);
2472 return ret;
2473}
2474
2475/**
2476 * panic_nand_write - [MTD Interface] NAND write with ECC
2477 * @mtd: MTD device structure
2478 * @to: offset to write to
2479 * @len: number of bytes to write
2480 * @retlen: pointer to variable to store the number of written bytes
2481 * @buf: the data to write
2482 *
2483 * NAND write with ECC. Used when performing writes in interrupt context, this
2484 * may for example be called by mtdoops when writing an oops while in panic.
2485 */
2486static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2487 size_t *retlen, const uint8_t *buf)
2488{
Scott Wood17cb4b82016-05-30 13:57:56 -05002489 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002490 struct mtd_oob_ops ops;
2491 int ret;
2492
2493 /* Wait for the device to get ready */
2494 panic_nand_wait(mtd, chip, 400);
2495
2496 /* Grab the device */
2497 panic_nand_get_device(chip, mtd, FL_WRITING);
2498
Scott Woodd3963722015-06-26 19:03:26 -05002499 memset(&ops, 0, sizeof(ops));
Heiko Schocherff94bc42014-06-24 10:10:04 +02002500 ops.len = len;
2501 ops.datbuf = (uint8_t *)buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002502 ops.mode = MTD_OPS_PLACE_OOB;
2503
2504 ret = nand_do_write_ops(mtd, to, &ops);
2505
2506 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002507 return ret;
2508}
2509
2510/**
2511 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002512 * @mtd: MTD device structure
2513 * @to: offset to write to
2514 * @len: number of bytes to write
2515 * @retlen: pointer to variable to store the number of written bytes
2516 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002517 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002518 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002519 */
2520static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2521 size_t *retlen, const uint8_t *buf)
2522{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002523 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002524 int ret;
2525
Heiko Schocherff94bc42014-06-24 10:10:04 +02002526 nand_get_device(mtd, FL_WRITING);
Scott Woodd3963722015-06-26 19:03:26 -05002527 memset(&ops, 0, sizeof(ops));
Sergey Lapindfe64e22013-01-14 03:46:50 +00002528 ops.len = len;
2529 ops.datbuf = (uint8_t *)buf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002530 ops.mode = MTD_OPS_PLACE_OOB;
2531 ret = nand_do_write_ops(mtd, to, &ops);
2532 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002533 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002534 return ret;
2535}
2536
2537/**
2538 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002539 * @mtd: MTD device structure
2540 * @to: offset to write to
2541 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002542 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002543 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002544 */
William Juulcfa460a2007-10-31 13:53:06 +01002545static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2546 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002547{
William Juulcfa460a2007-10-31 13:53:06 +01002548 int chipnr, page, status, len;
Scott Wood17cb4b82016-05-30 13:57:56 -05002549 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002550
Heiko Schocherff94bc42014-06-24 10:10:04 +02002551 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002552 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002553
Scott Woodceee07b2016-05-30 13:57:58 -05002554 len = mtd_oobavail(mtd, ops);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002555
2556 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002557 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002558 pr_debug("%s: attempt to write past end of page\n",
2559 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002560 return -EINVAL;
2561 }
2562
William Juulcfa460a2007-10-31 13:53:06 +01002563 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002564 pr_debug("%s: attempt to start write outside oob\n",
2565 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002566 return -EINVAL;
2567 }
2568
Christian Hitz90e3f392011-10-12 09:32:01 +02002569 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002570 if (unlikely(to >= mtd->size ||
2571 ops->ooboffs + ops->ooblen >
2572 ((mtd->size >> chip->page_shift) -
2573 (to >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002574 pr_debug("%s: attempt to write beyond end of device\n",
2575 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002576 return -EINVAL;
2577 }
2578
William Juulcfa460a2007-10-31 13:53:06 +01002579 chipnr = (int)(to >> chip->chip_shift);
2580 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002581
William Juulcfa460a2007-10-31 13:53:06 +01002582 /* Shift to get page */
2583 page = (int)(to >> chip->page_shift);
2584
2585 /*
2586 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2587 * of my DiskOnChip 2000 test units) will clear the whole data page too
2588 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2589 * it in the doc2000 driver in August 1999. dwmw2.
2590 */
2591 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002592
2593 /* Check, if it is write protected */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002594 if (nand_check_wp(mtd)) {
2595 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002596 return -EROFS;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002597 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002598
Wolfgang Denk932394a2005-08-17 12:55:25 +02002599 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002600 if (page == chip->pagebuf)
2601 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002602
Sergey Lapindfe64e22013-01-14 03:46:50 +00002603 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2604
2605 if (ops->mode == MTD_OPS_RAW)
2606 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2607 else
2608 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002609
Heiko Schocherff94bc42014-06-24 10:10:04 +02002610 chip->select_chip(mtd, -1);
2611
William Juulcfa460a2007-10-31 13:53:06 +01002612 if (status)
2613 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002614
William Juulcfa460a2007-10-31 13:53:06 +01002615 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002616
William Juulcfa460a2007-10-31 13:53:06 +01002617 return 0;
2618}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002619
William Juulcfa460a2007-10-31 13:53:06 +01002620/**
2621 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002622 * @mtd: MTD device structure
2623 * @to: offset to write to
2624 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002625 */
2626static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2627 struct mtd_oob_ops *ops)
2628{
William Juulcfa460a2007-10-31 13:53:06 +01002629 int ret = -ENOTSUPP;
2630
2631 ops->retlen = 0;
2632
2633 /* Do not allow writes past end of device */
2634 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002635 pr_debug("%s: attempt to write beyond end of device\n",
2636 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002637 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002638 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002639
Heiko Schocherff94bc42014-06-24 10:10:04 +02002640 nand_get_device(mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +01002641
Christian Hitz90e3f392011-10-12 09:32:01 +02002642 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002643 case MTD_OPS_PLACE_OOB:
2644 case MTD_OPS_AUTO_OOB:
2645 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002646 break;
2647
2648 default:
2649 goto out;
2650 }
2651
2652 if (!ops->datbuf)
2653 ret = nand_do_write_oob(mtd, to, ops);
2654 else
2655 ret = nand_do_write_ops(mtd, to, ops);
2656
Christian Hitz90e3f392011-10-12 09:32:01 +02002657out:
William Juulcfa460a2007-10-31 13:53:06 +01002658 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002659 return ret;
2660}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002661
2662/**
Scott Woodd3963722015-06-26 19:03:26 -05002663 * single_erase - [GENERIC] NAND standard block erase command function
Sergey Lapindfe64e22013-01-14 03:46:50 +00002664 * @mtd: MTD device structure
2665 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002666 *
Scott Woodd3963722015-06-26 19:03:26 -05002667 * Standard erase command for NAND chips. Returns NAND status.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002668 */
Scott Woodd3963722015-06-26 19:03:26 -05002669static int single_erase(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002670{
Scott Wood17cb4b82016-05-30 13:57:56 -05002671 struct nand_chip *chip = mtd_to_nand(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002672 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002673 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2674 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Scott Woodd3963722015-06-26 19:03:26 -05002675
2676 return chip->waitfunc(mtd, chip);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002677}
2678
2679/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002680 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002681 * @mtd: MTD device structure
2682 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002683 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002684 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002685 */
William Juulcfa460a2007-10-31 13:53:06 +01002686static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002687{
William Juulcfa460a2007-10-31 13:53:06 +01002688 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002689}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002690
Wolfgang Denk932394a2005-08-17 12:55:25 +02002691/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002692 * nand_erase_nand - [INTERN] erase block(s)
2693 * @mtd: MTD device structure
2694 * @instr: erase instruction
2695 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002696 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002697 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002698 */
William Juulcfa460a2007-10-31 13:53:06 +01002699int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2700 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002701{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002702 int page, status, pages_per_block, ret, chipnr;
Scott Wood17cb4b82016-05-30 13:57:56 -05002703 struct nand_chip *chip = mtd_to_nand(mtd);
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002704 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002705
Heiko Schocherff94bc42014-06-24 10:10:04 +02002706 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2707 __func__, (unsigned long long)instr->addr,
2708 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002709
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002710 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002711 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002712
Wolfgang Denk932394a2005-08-17 12:55:25 +02002713 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002714 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002715
2716 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002717 page = (int)(instr->addr >> chip->page_shift);
2718 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002719
2720 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002721 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002722
Wolfgang Denk932394a2005-08-17 12:55:25 +02002723 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002724 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002725
Wolfgang Denk932394a2005-08-17 12:55:25 +02002726 /* Check, if it is write protected */
2727 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002728 pr_debug("%s: device is write protected!\n",
2729 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002730 instr->state = MTD_ERASE_FAILED;
2731 goto erase_exit;
2732 }
2733
2734 /* Loop through the pages */
2735 len = instr->len;
2736
2737 instr->state = MTD_ERASING;
2738
2739 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002740 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002741
Sergey Lapindfe64e22013-01-14 03:46:50 +00002742 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamada756963d2014-12-16 15:36:33 +09002743 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
Scott Woodceee07b2016-05-30 13:57:58 -05002744 chip->page_shift, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002745 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02002746 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002747 instr->state = MTD_ERASE_FAILED;
2748 goto erase_exit;
2749 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002750
William Juulcfa460a2007-10-31 13:53:06 +01002751 /*
2752 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002753 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002754 */
2755 if (page <= chip->pagebuf && chip->pagebuf <
2756 (page + pages_per_block))
2757 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002758
Scott Woodd3963722015-06-26 19:03:26 -05002759 status = chip->erase(mtd, page & chip->pagemask);
William Juulcfa460a2007-10-31 13:53:06 +01002760
2761 /*
2762 * See if operation failed and additional status checks are
2763 * available
2764 */
2765 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2766 status = chip->errstat(mtd, chip, FL_ERASING,
2767 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002768
2769 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002770 if (status & NAND_STATUS_FAIL) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002771 pr_debug("%s: failed erase, page 0x%08x\n",
2772 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002773 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002774 instr->fail_addr =
2775 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002776 goto erase_exit;
2777 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002778
Wolfgang Denk932394a2005-08-17 12:55:25 +02002779 /* Increment page address and decrement length */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002780 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002781 page += pages_per_block;
2782
2783 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002784 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002785 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002786 chip->select_chip(mtd, -1);
2787 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002788 }
2789 }
2790 instr->state = MTD_ERASE_DONE;
2791
Christian Hitz90e3f392011-10-12 09:32:01 +02002792erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002793
2794 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002795
2796 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002797 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002798 nand_release_device(mtd);
2799
Scott Woodc45912d2008-10-24 16:20:43 -05002800 /* Do call back function */
2801 if (!ret)
2802 mtd_erase_callback(instr);
2803
Wolfgang Denk932394a2005-08-17 12:55:25 +02002804 /* Return more or less happy */
2805 return ret;
2806}
2807
2808/**
2809 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002810 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002811 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002812 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002813 */
William Juulcfa460a2007-10-31 13:53:06 +01002814static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002815{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002816 pr_debug("%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002817
2818 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002819 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002820 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002821 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002822}
2823
Wolfgang Denk932394a2005-08-17 12:55:25 +02002824/**
William Juulcfa460a2007-10-31 13:53:06 +01002825 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002826 * @mtd: MTD device structure
2827 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002828 */
William Juulcfa460a2007-10-31 13:53:06 +01002829static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002830{
Scott Woodceee07b2016-05-30 13:57:58 -05002831 struct nand_chip *chip = mtd_to_nand(mtd);
2832 int chipnr = (int)(offs >> chip->chip_shift);
2833 int ret;
2834
2835 /* Select the NAND device */
2836 nand_get_device(mtd, FL_READING);
2837 chip->select_chip(mtd, chipnr);
2838
2839 ret = nand_block_checkbad(mtd, offs, 0);
2840
2841 chip->select_chip(mtd, -1);
2842 nand_release_device(mtd);
2843
2844 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002845}
2846
2847/**
William Juulcfa460a2007-10-31 13:53:06 +01002848 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002849 * @mtd: MTD device structure
2850 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002851 */
William Juulcfa460a2007-10-31 13:53:06 +01002852static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002853{
Wolfgang Denk932394a2005-08-17 12:55:25 +02002854 int ret;
2855
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002856 ret = nand_block_isbad(mtd, ofs);
2857 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002858 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002859 if (ret > 0)
2860 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002861 return ret;
2862 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002863
Heiko Schocherff94bc42014-06-24 10:10:04 +02002864 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002865}
2866
Heiko Schocherff94bc42014-06-24 10:10:04 +02002867/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002868 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2869 * @mtd: MTD device structure
2870 * @chip: nand chip info structure
2871 * @addr: feature address.
2872 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01002873 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002874static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2875 int addr, uint8_t *subfeature_param)
2876{
2877 int status;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002878 int i;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002879
Heiko Schocherff94bc42014-06-24 10:10:04 +02002880#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2881 if (!chip->onfi_version ||
2882 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2883 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00002884 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002885#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00002886
2887 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002888 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2889 chip->write_byte(mtd, subfeature_param[i]);
2890
Sergey Lapindfe64e22013-01-14 03:46:50 +00002891 status = chip->waitfunc(mtd, chip);
2892 if (status & NAND_STATUS_FAIL)
2893 return -EIO;
2894 return 0;
2895}
2896
2897/**
2898 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2899 * @mtd: MTD device structure
2900 * @chip: nand chip info structure
2901 * @addr: feature address.
2902 * @subfeature_param: the subfeature parameters, a four bytes array.
2903 */
2904static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2905 int addr, uint8_t *subfeature_param)
2906{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002907 int i;
2908
2909#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2910 if (!chip->onfi_version ||
2911 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2912 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00002913 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002914#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00002915
Sergey Lapindfe64e22013-01-14 03:46:50 +00002916 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002917 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2918 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002919 return 0;
2920}
2921
2922/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01002923static void nand_set_defaults(struct nand_chip *chip, int busw)
2924{
2925 /* check for proper chip_delay setup, set 20us if not */
2926 if (!chip->chip_delay)
2927 chip->chip_delay = 20;
2928
2929 /* check, if a user supplied command function given */
2930 if (chip->cmdfunc == NULL)
2931 chip->cmdfunc = nand_command;
2932
2933 /* check, if a user supplied wait function given */
2934 if (chip->waitfunc == NULL)
2935 chip->waitfunc = nand_wait;
2936
2937 if (!chip->select_chip)
2938 chip->select_chip = nand_select_chip;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002939
2940 /* set for ONFI nand */
2941 if (!chip->onfi_set_features)
2942 chip->onfi_set_features = nand_onfi_set_features;
2943 if (!chip->onfi_get_features)
2944 chip->onfi_get_features = nand_onfi_get_features;
2945
2946 /* If called twice, pointers that depend on busw may need to be reset */
2947 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juulcfa460a2007-10-31 13:53:06 +01002948 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2949 if (!chip->read_word)
2950 chip->read_word = nand_read_word;
2951 if (!chip->block_bad)
2952 chip->block_bad = nand_block_bad;
2953 if (!chip->block_markbad)
2954 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002955 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juulcfa460a2007-10-31 13:53:06 +01002956 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002957 if (!chip->write_byte || chip->write_byte == nand_write_byte)
2958 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
2959 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juulcfa460a2007-10-31 13:53:06 +01002960 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juulcfa460a2007-10-31 13:53:06 +01002961 if (!chip->scan_bbt)
2962 chip->scan_bbt = nand_default_bbt;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002963
2964 if (!chip->controller) {
William Juulcfa460a2007-10-31 13:53:06 +01002965 chip->controller = &chip->hwcontrol;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002966 spin_lock_init(&chip->controller->lock);
2967 init_waitqueue_head(&chip->controller->wq);
2968 }
2969
William Juulcfa460a2007-10-31 13:53:06 +01002970}
2971
Sergey Lapindfe64e22013-01-14 03:46:50 +00002972/* Sanitize ONFI strings so we can safely print them */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002973static void sanitize_string(char *s, size_t len)
2974{
2975 ssize_t i;
2976
Sergey Lapindfe64e22013-01-14 03:46:50 +00002977 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002978 s[len - 1] = 0;
2979
Sergey Lapindfe64e22013-01-14 03:46:50 +00002980 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002981 for (i = 0; i < len - 1; i++) {
2982 if (s[i] < ' ' || s[i] > 127)
2983 s[i] = '?';
2984 }
2985
Sergey Lapindfe64e22013-01-14 03:46:50 +00002986 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02002987 strim(s);
2988}
2989
Florian Fainelli0272c712011-02-25 00:01:34 +00002990static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01002991{
Florian Fainelli0272c712011-02-25 00:01:34 +00002992 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00002993 while (len--) {
2994 crc ^= *p++ << 8;
2995 for (i = 0; i < 8; i++)
2996 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05002997 }
2998
Florian Fainelli0272c712011-02-25 00:01:34 +00002999 return crc;
3000}
William Juulcfa460a2007-10-31 13:53:06 +01003001
Heiko Schocher4e67c572014-07-15 16:08:43 +02003002#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherff94bc42014-06-24 10:10:04 +02003003/* Parse the Extended Parameter Page. */
3004static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3005 struct nand_chip *chip, struct nand_onfi_params *p)
3006{
3007 struct onfi_ext_param_page *ep;
3008 struct onfi_ext_section *s;
3009 struct onfi_ext_ecc_info *ecc;
3010 uint8_t *cursor;
3011 int ret = -EINVAL;
3012 int len;
3013 int i;
3014
3015 len = le16_to_cpu(p->ext_param_page_length) * 16;
3016 ep = kmalloc(len, GFP_KERNEL);
3017 if (!ep)
3018 return -ENOMEM;
3019
3020 /* Send our own NAND_CMD_PARAM. */
3021 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3022
3023 /* Use the Change Read Column command to skip the ONFI param pages. */
3024 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3025 sizeof(*p) * p->num_of_param_pages , -1);
3026
3027 /* Read out the Extended Parameter Page. */
3028 chip->read_buf(mtd, (uint8_t *)ep, len);
3029 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3030 != le16_to_cpu(ep->crc))) {
3031 pr_debug("fail in the CRC.\n");
3032 goto ext_out;
3033 }
3034
3035 /*
3036 * Check the signature.
3037 * Do not strictly follow the ONFI spec, maybe changed in future.
3038 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003039 if (strncmp((char *)ep->sig, "EPPS", 4)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003040 pr_debug("The signature is invalid.\n");
3041 goto ext_out;
3042 }
3043
3044 /* find the ECC section. */
3045 cursor = (uint8_t *)(ep + 1);
3046 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3047 s = ep->sections + i;
3048 if (s->type == ONFI_SECTION_TYPE_2)
3049 break;
3050 cursor += s->length * 16;
3051 }
3052 if (i == ONFI_EXT_SECTION_MAX) {
3053 pr_debug("We can not find the ECC section.\n");
3054 goto ext_out;
3055 }
3056
3057 /* get the info we want. */
3058 ecc = (struct onfi_ext_ecc_info *)cursor;
3059
3060 if (!ecc->codeword_size) {
3061 pr_debug("Invalid codeword size\n");
3062 goto ext_out;
3063 }
3064
3065 chip->ecc_strength_ds = ecc->ecc_bits;
3066 chip->ecc_step_ds = 1 << ecc->codeword_size;
3067 ret = 0;
3068
3069ext_out:
3070 kfree(ep);
3071 return ret;
3072}
3073
3074static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3075{
Scott Wood17cb4b82016-05-30 13:57:56 -05003076 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003077 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3078
3079 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3080 feature);
3081}
3082
3083/*
3084 * Configure chip properties from Micron vendor-specific ONFI table
3085 */
3086static void nand_onfi_detect_micron(struct nand_chip *chip,
3087 struct nand_onfi_params *p)
3088{
3089 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3090
3091 if (le16_to_cpu(p->vendor_revision) < 1)
3092 return;
3093
3094 chip->read_retries = micron->read_retry_options;
3095 chip->setup_read_retry = nand_setup_read_retry_micron;
3096}
3097
Florian Fainelli0272c712011-02-25 00:01:34 +00003098/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003099 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00003100 */
Christian Hitz90e3f392011-10-12 09:32:01 +02003101static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003102 int *busw)
3103{
3104 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05303105 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00003106 int val;
3107
Sergey Lapindfe64e22013-01-14 03:46:50 +00003108 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00003109 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3110 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3111 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3112 return 0;
3113
Florian Fainelli0272c712011-02-25 00:01:34 +00003114 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3115 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05303116 for (j = 0; j < sizeof(*p); j++)
3117 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003118 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02003119 le16_to_cpu(p->crc)) {
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01003120 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00003121 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02003122 }
William Juulcfa460a2007-10-31 13:53:06 +01003123
Heiko Schocherff94bc42014-06-24 10:10:04 +02003124 if (i == 3) {
3125 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00003126 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003127 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003128
Sergey Lapindfe64e22013-01-14 03:46:50 +00003129 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00003130 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003131 if (val & (1 << 5))
3132 chip->onfi_version = 23;
3133 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00003134 chip->onfi_version = 22;
3135 else if (val & (1 << 3))
3136 chip->onfi_version = 21;
3137 else if (val & (1 << 2))
3138 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003139 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00003140 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003141
3142 if (!chip->onfi_version) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003143 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003144 return 0;
3145 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003146
Christian Hitz5454ddb2011-10-12 09:32:05 +02003147 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3148 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01003149 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00003150 mtd->name = p->model;
William Juulcfa460a2007-10-31 13:53:06 +01003151
Heiko Schocherff94bc42014-06-24 10:10:04 +02003152 mtd->writesize = le32_to_cpu(p->byte_per_page);
3153
3154 /*
3155 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3156 * (don't ask me who thought of this...). MTD assumes that these
3157 * dimensions will be power-of-2, so just truncate the remaining area.
3158 */
3159 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3160 mtd->erasesize *= mtd->writesize;
3161
3162 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3163
3164 /* See erasesize comment */
3165 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3166 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3167 chip->bits_per_cell = p->bits_per_cell;
3168
3169 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3170 *busw = NAND_BUSWIDTH_16;
3171 else
3172 *busw = 0;
3173
3174 if (p->ecc_bits != 0xff) {
3175 chip->ecc_strength_ds = p->ecc_bits;
3176 chip->ecc_step_ds = 512;
3177 } else if (chip->onfi_version >= 21 &&
3178 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3179
3180 /*
3181 * The nand_flash_detect_ext_param_page() uses the
3182 * Change Read Column command which maybe not supported
3183 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3184 * now. We do not replace user supplied command function.
3185 */
3186 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3187 chip->cmdfunc = nand_command_lp;
3188
3189 /* The Extended Parameter Page is supported since ONFI 2.1. */
3190 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3191 pr_warn("Failed to detect ONFI extended param page\n");
3192 } else {
3193 pr_warn("Could not retrieve ONFI ECC requirements\n");
3194 }
3195
3196 if (p->jedec_id == NAND_MFR_MICRON)
3197 nand_onfi_detect_micron(chip, p);
3198
Florian Fainelli0272c712011-02-25 00:01:34 +00003199 return 1;
3200}
3201#else
Heiko Schocherff94bc42014-06-24 10:10:04 +02003202static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003203 int *busw)
3204{
3205 return 0;
3206}
3207#endif
3208
Florian Fainelli0272c712011-02-25 00:01:34 +00003209/*
Heiko Schocher4e67c572014-07-15 16:08:43 +02003210 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3211 */
3212static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3213 int *busw)
3214{
3215 struct nand_jedec_params *p = &chip->jedec_params;
3216 struct jedec_ecc_info *ecc;
3217 int val;
3218 int i, j;
3219
3220 /* Try JEDEC for unknown chip or LP */
3221 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3222 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3223 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3224 chip->read_byte(mtd) != 'C')
3225 return 0;
3226
3227 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3228 for (i = 0; i < 3; i++) {
3229 for (j = 0; j < sizeof(*p); j++)
3230 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3231
3232 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3233 le16_to_cpu(p->crc))
3234 break;
3235 }
3236
3237 if (i == 3) {
3238 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3239 return 0;
3240 }
3241
3242 /* Check version */
3243 val = le16_to_cpu(p->revision);
3244 if (val & (1 << 2))
3245 chip->jedec_version = 10;
3246 else if (val & (1 << 1))
3247 chip->jedec_version = 1; /* vendor specific version */
3248
3249 if (!chip->jedec_version) {
3250 pr_info("unsupported JEDEC version: %d\n", val);
3251 return 0;
3252 }
3253
3254 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3255 sanitize_string(p->model, sizeof(p->model));
3256 if (!mtd->name)
3257 mtd->name = p->model;
3258
3259 mtd->writesize = le32_to_cpu(p->byte_per_page);
3260
3261 /* Please reference to the comment for nand_flash_detect_onfi. */
3262 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3263 mtd->erasesize *= mtd->writesize;
3264
3265 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3266
3267 /* Please reference to the comment for nand_flash_detect_onfi. */
3268 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3269 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3270 chip->bits_per_cell = p->bits_per_cell;
3271
3272 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3273 *busw = NAND_BUSWIDTH_16;
3274 else
3275 *busw = 0;
3276
3277 /* ECC info */
3278 ecc = &p->ecc_info[0];
3279
3280 if (ecc->codeword_size >= 9) {
3281 chip->ecc_strength_ds = ecc->ecc_bits;
3282 chip->ecc_step_ds = 1 << ecc->codeword_size;
3283 } else {
3284 pr_warn("Invalid codeword size\n");
3285 }
3286
3287 return 1;
3288}
3289
3290/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003291 * nand_id_has_period - Check if an ID string has a given wraparound period
3292 * @id_data: the ID string
3293 * @arrlen: the length of the @id_data array
3294 * @period: the period of repitition
3295 *
3296 * Check if an ID string is repeated within a given sequence of bytes at
3297 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherff94bc42014-06-24 10:10:04 +02003298 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapindfe64e22013-01-14 03:46:50 +00003299 * if the repetition has a period of @period; otherwise, returns zero.
3300 */
3301static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3302{
3303 int i, j;
3304 for (i = 0; i < period; i++)
3305 for (j = i + period; j < arrlen; j += period)
3306 if (id_data[i] != id_data[j])
3307 return 0;
3308 return 1;
3309}
3310
3311/*
3312 * nand_id_len - Get the length of an ID string returned by CMD_READID
3313 * @id_data: the ID string
3314 * @arrlen: the length of the @id_data array
3315
3316 * Returns the length of the ID string, according to known wraparound/trailing
3317 * zero patterns. If no pattern exists, returns the length of the array.
3318 */
3319static int nand_id_len(u8 *id_data, int arrlen)
3320{
3321 int last_nonzero, period;
3322
3323 /* Find last non-zero byte */
3324 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3325 if (id_data[last_nonzero])
3326 break;
3327
3328 /* All zeros */
3329 if (last_nonzero < 0)
3330 return 0;
3331
3332 /* Calculate wraparound period */
3333 for (period = 1; period < arrlen; period++)
3334 if (nand_id_has_period(id_data, arrlen, period))
3335 break;
3336
3337 /* There's a repeated pattern */
3338 if (period < arrlen)
3339 return period;
3340
3341 /* There are trailing zeros */
3342 if (last_nonzero < arrlen - 1)
3343 return last_nonzero + 1;
3344
3345 /* No pattern detected */
3346 return arrlen;
3347}
3348
Heiko Schocherff94bc42014-06-24 10:10:04 +02003349/* Extract the bits of per cell from the 3rd byte of the extended ID */
3350static int nand_get_bits_per_cell(u8 cellinfo)
3351{
3352 int bits;
3353
3354 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3355 bits >>= NAND_CI_CELLTYPE_SHIFT;
3356 return bits + 1;
3357}
3358
Sergey Lapindfe64e22013-01-14 03:46:50 +00003359/*
3360 * Many new NAND share similar device ID codes, which represent the size of the
3361 * chip. The rest of the parameters must be decoded according to generic or
3362 * manufacturer-specific "extended ID" decoding patterns.
3363 */
3364static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3365 u8 id_data[8], int *busw)
3366{
3367 int extid, id_len;
3368 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003369 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003370 /* The 4th id byte is the important one */
3371 extid = id_data[3];
3372
3373 id_len = nand_id_len(id_data, 8);
3374
3375 /*
3376 * Field definitions are in the following datasheets:
3377 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3378 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3379 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3380 *
3381 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3382 * ID to decide what to do.
3383 */
3384 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003385 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003386 /* Calc pagesize */
3387 mtd->writesize = 2048 << (extid & 0x03);
3388 extid >>= 2;
3389 /* Calc oobsize */
3390 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3391 case 1:
3392 mtd->oobsize = 128;
3393 break;
3394 case 2:
3395 mtd->oobsize = 218;
3396 break;
3397 case 3:
3398 mtd->oobsize = 400;
3399 break;
3400 case 4:
3401 mtd->oobsize = 436;
3402 break;
3403 case 5:
3404 mtd->oobsize = 512;
3405 break;
3406 case 6:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003407 mtd->oobsize = 640;
3408 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003409 case 7:
3410 default: /* Other cases are "reserved" (unknown) */
3411 mtd->oobsize = 1024;
3412 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003413 }
3414 extid >>= 2;
3415 /* Calc blocksize */
3416 mtd->erasesize = (128 * 1024) <<
3417 (((extid >> 1) & 0x04) | (extid & 0x03));
3418 *busw = 0;
3419 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003420 !nand_is_slc(chip)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003421 unsigned int tmp;
3422
3423 /* Calc pagesize */
3424 mtd->writesize = 2048 << (extid & 0x03);
3425 extid >>= 2;
3426 /* Calc oobsize */
3427 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3428 case 0:
3429 mtd->oobsize = 128;
3430 break;
3431 case 1:
3432 mtd->oobsize = 224;
3433 break;
3434 case 2:
3435 mtd->oobsize = 448;
3436 break;
3437 case 3:
3438 mtd->oobsize = 64;
3439 break;
3440 case 4:
3441 mtd->oobsize = 32;
3442 break;
3443 case 5:
3444 mtd->oobsize = 16;
3445 break;
3446 default:
3447 mtd->oobsize = 640;
3448 break;
3449 }
3450 extid >>= 2;
3451 /* Calc blocksize */
3452 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3453 if (tmp < 0x03)
3454 mtd->erasesize = (128 * 1024) << tmp;
3455 else if (tmp == 0x03)
3456 mtd->erasesize = 768 * 1024;
3457 else
3458 mtd->erasesize = (64 * 1024) << tmp;
3459 *busw = 0;
3460 } else {
3461 /* Calc pagesize */
3462 mtd->writesize = 1024 << (extid & 0x03);
3463 extid >>= 2;
3464 /* Calc oobsize */
3465 mtd->oobsize = (8 << (extid & 0x01)) *
3466 (mtd->writesize >> 9);
3467 extid >>= 2;
3468 /* Calc blocksize. Blocksize is multiples of 64KiB */
3469 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3470 extid >>= 2;
3471 /* Get buswidth information */
3472 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003473
3474 /*
3475 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3476 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3477 * follows:
3478 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3479 * 110b -> 24nm
3480 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3481 */
3482 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3483 nand_is_slc(chip) &&
3484 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3485 !(id_data[4] & 0x80) /* !BENAND */) {
3486 mtd->oobsize = 32 * mtd->writesize >> 9;
3487 }
3488
Sergey Lapindfe64e22013-01-14 03:46:50 +00003489 }
3490}
3491
Heiko Schocherff94bc42014-06-24 10:10:04 +02003492/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003493 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3494 * decodes a matching ID table entry and assigns the MTD size parameters for
3495 * the chip.
3496 */
3497static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003498 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapindfe64e22013-01-14 03:46:50 +00003499 int *busw)
3500{
3501 int maf_id = id_data[0];
3502
3503 mtd->erasesize = type->erasesize;
3504 mtd->writesize = type->pagesize;
3505 mtd->oobsize = mtd->writesize / 32;
3506 *busw = type->options & NAND_BUSWIDTH_16;
3507
Heiko Schocherff94bc42014-06-24 10:10:04 +02003508 /* All legacy ID NAND are small-page, SLC */
3509 chip->bits_per_cell = 1;
3510
Sergey Lapindfe64e22013-01-14 03:46:50 +00003511 /*
3512 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3513 * some Spansion chips have erasesize that conflicts with size
3514 * listed in nand_ids table.
3515 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3516 */
3517 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3518 && id_data[6] == 0x00 && id_data[7] == 0x00
3519 && mtd->writesize == 512) {
3520 mtd->erasesize = 128 * 1024;
3521 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3522 }
3523}
3524
Heiko Schocherff94bc42014-06-24 10:10:04 +02003525/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003526 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3527 * heuristic patterns using various detected parameters (e.g., manufacturer,
3528 * page size, cell-type information).
3529 */
3530static void nand_decode_bbm_options(struct mtd_info *mtd,
3531 struct nand_chip *chip, u8 id_data[8])
3532{
3533 int maf_id = id_data[0];
3534
3535 /* Set the bad block position */
3536 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3537 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3538 else
3539 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3540
3541 /*
3542 * Bad block marker is stored in the last page of each block on Samsung
3543 * and Hynix MLC devices; stored in first two pages of each block on
3544 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3545 * AMD/Spansion, and Macronix. All others scan only the first page.
3546 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003547 if (!nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003548 (maf_id == NAND_MFR_SAMSUNG ||
3549 maf_id == NAND_MFR_HYNIX))
3550 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003551 else if ((nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003552 (maf_id == NAND_MFR_SAMSUNG ||
3553 maf_id == NAND_MFR_HYNIX ||
3554 maf_id == NAND_MFR_TOSHIBA ||
3555 maf_id == NAND_MFR_AMD ||
3556 maf_id == NAND_MFR_MACRONIX)) ||
3557 (mtd->writesize == 2048 &&
3558 maf_id == NAND_MFR_MICRON))
3559 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3560}
3561
Heiko Schocherff94bc42014-06-24 10:10:04 +02003562static inline bool is_full_id_nand(struct nand_flash_dev *type)
3563{
3564 return type->id_len;
3565}
3566
3567static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3568 struct nand_flash_dev *type, u8 *id_data, int *busw)
3569{
Heiko Schocherff94bc42014-06-24 10:10:04 +02003570 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003571 mtd->writesize = type->pagesize;
3572 mtd->erasesize = type->erasesize;
3573 mtd->oobsize = type->oobsize;
3574
3575 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3576 chip->chipsize = (uint64_t)type->chipsize << 20;
3577 chip->options |= type->options;
3578 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3579 chip->ecc_step_ds = NAND_ECC_STEP(type);
Scott Woodd3963722015-06-26 19:03:26 -05003580 chip->onfi_timing_mode_default =
3581 type->onfi_timing_mode_default;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003582
3583 *busw = type->options & NAND_BUSWIDTH_16;
3584
3585 if (!mtd->name)
3586 mtd->name = type->name;
3587
3588 return true;
3589 }
3590 return false;
3591}
3592
Sergey Lapindfe64e22013-01-14 03:46:50 +00003593/*
3594 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00003595 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003596static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Florian Fainelli0272c712011-02-25 00:01:34 +00003597 struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003598 int *maf_id, int *dev_id,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003599 struct nand_flash_dev *type)
Florian Fainelli0272c712011-02-25 00:01:34 +00003600{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003601 int busw;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003602 int i, maf_idx;
3603 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00003604
3605 /* Select the device */
3606 chip->select_chip(mtd, 0);
3607
3608 /*
3609 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003610 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00003611 */
3612 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3613
3614 /* Send the command for reading device ID */
3615 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3616
3617 /* Read manufacturer and device IDs */
3618 *maf_id = chip->read_byte(mtd);
3619 *dev_id = chip->read_byte(mtd);
3620
Sergey Lapindfe64e22013-01-14 03:46:50 +00003621 /*
3622 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00003623 * interface concerns can cause random data which looks like a
3624 * possibly credible NAND flash to appear. If the two results do
3625 * not match, ignore the device completely.
3626 */
3627
3628 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3629
Sergey Lapindfe64e22013-01-14 03:46:50 +00003630 /* Read entire ID string */
3631 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003632 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003633
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003634 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003635 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003636 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00003637 return ERR_PTR(-ENODEV);
3638 }
3639
3640 if (!type)
3641 type = nand_flash_ids;
3642
Heiko Schocherff94bc42014-06-24 10:10:04 +02003643 for (; type->name != NULL; type++) {
3644 if (is_full_id_nand(type)) {
3645 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3646 goto ident_done;
3647 } else if (*dev_id == type->dev_id) {
Scott Woodceee07b2016-05-30 13:57:58 -05003648 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003649 }
3650 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003651
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003652 chip->onfi_version = 0;
3653 if (!type->name || !type->pagesize) {
Scott Woodd3963722015-06-26 19:03:26 -05003654 /* Check if the chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003655 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003656 goto ident_done;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003657
3658 /* Check if the chip is JEDEC compliant */
3659 if (nand_flash_detect_jedec(mtd, chip, &busw))
3660 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00003661 }
3662
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003663 if (!type->name)
3664 return ERR_PTR(-ENODEV);
3665
Florian Fainelli0272c712011-02-25 00:01:34 +00003666 if (!mtd->name)
3667 mtd->name = type->name;
3668
3669 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00003670
Scott Woodceee07b2016-05-30 13:57:58 -05003671 if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003672 /* Decode parameters from extended ID */
3673 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003674 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003675 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003676 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003677 /* Get chip options */
Marek Vasut9c790a72012-08-30 13:39:38 +00003678 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00003679
Sergey Lapindfe64e22013-01-14 03:46:50 +00003680 /*
3681 * Check if chip is not a Samsung device. Do not clear the
3682 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003683 */
3684 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3685 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3686ident_done:
3687
William Juulcfa460a2007-10-31 13:53:06 +01003688 /* Try to identify manufacturer */
3689 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3690 if (nand_manuf_ids[maf_idx].id == *maf_id)
3691 break;
3692 }
3693
Heiko Schocherff94bc42014-06-24 10:10:04 +02003694 if (chip->options & NAND_BUSWIDTH_AUTO) {
3695 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3696 chip->options |= busw;
3697 nand_set_defaults(chip, busw);
3698 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3699 /*
3700 * Check, if buswidth is correct. Hardware drivers should set
3701 * chip correct!
3702 */
3703 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3704 *maf_id, *dev_id);
3705 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3706 pr_warn("bus width %d instead %d bit\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003707 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3708 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003709 return ERR_PTR(-EINVAL);
3710 }
3711
Sergey Lapindfe64e22013-01-14 03:46:50 +00003712 nand_decode_bbm_options(mtd, chip, id_data);
3713
William Juulcfa460a2007-10-31 13:53:06 +01003714 /* Calculate the address shift from the page size */
3715 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003716 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003717 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3718
3719 chip->bbt_erase_shift = chip->phys_erase_shift =
3720 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003721 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003722 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003723 else {
3724 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3725 chip->chip_shift += 32 - 1;
3726 }
3727
3728 chip->badblockbits = 8;
Scott Woodd3963722015-06-26 19:03:26 -05003729 chip->erase = single_erase;
William Juulcfa460a2007-10-31 13:53:06 +01003730
Sergey Lapindfe64e22013-01-14 03:46:50 +00003731 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003732 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3733 chip->cmdfunc = nand_command_lp;
3734
Heiko Schocherff94bc42014-06-24 10:10:04 +02003735 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3736 *maf_id, *dev_id);
Heiko Schocher4e67c572014-07-15 16:08:43 +02003737
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003738#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher4e67c572014-07-15 16:08:43 +02003739 if (chip->onfi_version)
3740 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3741 chip->onfi_params.model);
3742 else if (chip->jedec_version)
3743 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3744 chip->jedec_params.model);
3745 else
3746 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3747 type->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003748#else
Heiko Schocher4e67c572014-07-15 16:08:43 +02003749 if (chip->jedec_version)
3750 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3751 chip->jedec_params.model);
3752 else
3753 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3754 type->name);
3755
3756 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3757 type->name);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003758#endif
Heiko Schocher4e67c572014-07-15 16:08:43 +02003759
Scott Woodd3963722015-06-26 19:03:26 -05003760 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02003761 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Scott Woodd3963722015-06-26 19:03:26 -05003762 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003763 return type;
3764}
3765
3766/**
3767 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003768 * @mtd: MTD device structure
3769 * @maxchips: number of chips to scan for
3770 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003771 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003772 * This is the first phase of the normal nand_scan() function. It reads the
3773 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003774 *
William Juulcfa460a2007-10-31 13:53:06 +01003775 */
Lei Wen245eb902011-01-06 09:48:18 +08003776int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003777 struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003778{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003779 int i, nand_maf_id, nand_dev_id;
Scott Wood17cb4b82016-05-30 13:57:56 -05003780 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003781 struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003782
William Juulcfa460a2007-10-31 13:53:06 +01003783 /* Set the default functions */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003784 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juulcfa460a2007-10-31 13:53:06 +01003785
3786 /* Read the flash type */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003787 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3788 &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003789
3790 if (IS_ERR(type)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003791 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3792 pr_warn("No NAND device found\n");
William Juulcfa460a2007-10-31 13:53:06 +01003793 chip->select_chip(mtd, -1);
3794 return PTR_ERR(type);
3795 }
3796
Heiko Schocherff94bc42014-06-24 10:10:04 +02003797 chip->select_chip(mtd, -1);
3798
William Juulcfa460a2007-10-31 13:53:06 +01003799 /* Check for a chip array */
3800 for (i = 1; i < maxchips; i++) {
3801 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003802 /* See comment in nand_get_flash_type for reset */
3803 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003804 /* Send the command for reading device ID */
3805 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3806 /* Read manufacturer and device IDs */
3807 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02003808 nand_dev_id != chip->read_byte(mtd)) {
3809 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003810 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003811 }
3812 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003813 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003814
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003815#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003816 if (i > 1)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003817 pr_info("%d chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003818#endif
William Juulcfa460a2007-10-31 13:53:06 +01003819
3820 /* Store the number of chips and calc total size for mtd */
3821 chip->numchips = i;
3822 mtd->size = i * chip->chipsize;
3823
3824 return 0;
3825}
Heiko Schocherff94bc42014-06-24 10:10:04 +02003826EXPORT_SYMBOL(nand_scan_ident);
William Juulcfa460a2007-10-31 13:53:06 +01003827
Scott Woodd3963722015-06-26 19:03:26 -05003828/*
3829 * Check if the chip configuration meet the datasheet requirements.
3830
3831 * If our configuration corrects A bits per B bytes and the minimum
3832 * required correction level is X bits per Y bytes, then we must ensure
3833 * both of the following are true:
3834 *
3835 * (1) A / B >= X / Y
3836 * (2) A >= X
3837 *
3838 * Requirement (1) ensures we can correct for the required bitflip density.
3839 * Requirement (2) ensures we can correct even when all bitflips are clumped
3840 * in the same sector.
3841 */
3842static bool nand_ecc_strength_good(struct mtd_info *mtd)
3843{
Scott Wood17cb4b82016-05-30 13:57:56 -05003844 struct nand_chip *chip = mtd_to_nand(mtd);
Scott Woodd3963722015-06-26 19:03:26 -05003845 struct nand_ecc_ctrl *ecc = &chip->ecc;
3846 int corr, ds_corr;
3847
3848 if (ecc->size == 0 || chip->ecc_step_ds == 0)
3849 /* Not enough information */
3850 return true;
3851
3852 /*
3853 * We get the number of corrected bits per page to compare
3854 * the correction density.
3855 */
3856 corr = (mtd->writesize * ecc->strength) / ecc->size;
3857 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
3858
3859 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
3860}
William Juulcfa460a2007-10-31 13:53:06 +01003861
3862/**
3863 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003864 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003865 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003866 * This is the second phase of the normal nand_scan() function. It fills out
3867 * all the uninitialized function pointers with the defaults and scans for a
3868 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003869 */
3870int nand_scan_tail(struct mtd_info *mtd)
3871{
3872 int i;
Scott Wood17cb4b82016-05-30 13:57:56 -05003873 struct nand_chip *chip = mtd_to_nand(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003874 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003875 struct nand_buffers *nbuf;
William Juulcfa460a2007-10-31 13:53:06 +01003876
Sergey Lapindfe64e22013-01-14 03:46:50 +00003877 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3878 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3879 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3880
Heiko Schocher4e67c572014-07-15 16:08:43 +02003881 if (!(chip->options & NAND_OWN_BUFFERS)) {
Heiko Schocher4e67c572014-07-15 16:08:43 +02003882 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
Heiko Schocher4e67c572014-07-15 16:08:43 +02003883 chip->buffers = nbuf;
3884 } else {
3885 if (!chip->buffers)
3886 return -ENOMEM;
3887 }
William Juulcfa460a2007-10-31 13:53:06 +01003888
3889 /* Set the internal oob buffer location, just after the page data */
3890 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3891
3892 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003893 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01003894 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003895 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01003896 switch (mtd->oobsize) {
3897 case 8:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003898 ecc->layout = &nand_oob_8;
William Juulcfa460a2007-10-31 13:53:06 +01003899 break;
3900 case 16:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003901 ecc->layout = &nand_oob_16;
William Juulcfa460a2007-10-31 13:53:06 +01003902 break;
3903 case 64:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003904 ecc->layout = &nand_oob_64;
William Juulcfa460a2007-10-31 13:53:06 +01003905 break;
3906 case 128:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003907 ecc->layout = &nand_oob_128;
William Juulcfa460a2007-10-31 13:53:06 +01003908 break;
3909 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003910 pr_warn("No oob scheme defined for oobsize %d\n",
3911 mtd->oobsize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003912 BUG();
William Juulcfa460a2007-10-31 13:53:06 +01003913 }
3914 }
3915
3916 if (!chip->write_page)
3917 chip->write_page = nand_write_page;
3918
3919 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003920 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01003921 * selected and we have 256 byte pagesize fallback to software ECC
3922 */
William Juulcfa460a2007-10-31 13:53:06 +01003923
Heiko Schocherff94bc42014-06-24 10:10:04 +02003924 switch (ecc->mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003925 case NAND_ECC_HW_OOB_FIRST:
3926 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003927 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Scott Woodd3963722015-06-26 19:03:26 -05003928 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003929 BUG();
3930 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003931 if (!ecc->read_page)
3932 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003933
William Juulcfa460a2007-10-31 13:53:06 +01003934 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003935 /* Use standard hwecc read page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003936 if (!ecc->read_page)
3937 ecc->read_page = nand_read_page_hwecc;
3938 if (!ecc->write_page)
3939 ecc->write_page = nand_write_page_hwecc;
3940 if (!ecc->read_page_raw)
3941 ecc->read_page_raw = nand_read_page_raw;
3942 if (!ecc->write_page_raw)
3943 ecc->write_page_raw = nand_write_page_raw;
3944 if (!ecc->read_oob)
3945 ecc->read_oob = nand_read_oob_std;
3946 if (!ecc->write_oob)
3947 ecc->write_oob = nand_write_oob_std;
3948 if (!ecc->read_subpage)
3949 ecc->read_subpage = nand_read_subpage;
Scott Woodceee07b2016-05-30 13:57:58 -05003950 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003951 ecc->write_subpage = nand_write_subpage_hwecc;
William Juulcfa460a2007-10-31 13:53:06 +01003952
3953 case NAND_ECC_HW_SYNDROME:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003954 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
3955 (!ecc->read_page ||
3956 ecc->read_page == nand_read_page_hwecc ||
3957 !ecc->write_page ||
3958 ecc->write_page == nand_write_page_hwecc)) {
Scott Woodd3963722015-06-26 19:03:26 -05003959 pr_warn("No ECC functions supplied; hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01003960 BUG();
3961 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00003962 /* Use standard syndrome read/write page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003963 if (!ecc->read_page)
3964 ecc->read_page = nand_read_page_syndrome;
3965 if (!ecc->write_page)
3966 ecc->write_page = nand_write_page_syndrome;
3967 if (!ecc->read_page_raw)
3968 ecc->read_page_raw = nand_read_page_raw_syndrome;
3969 if (!ecc->write_page_raw)
3970 ecc->write_page_raw = nand_write_page_raw_syndrome;
3971 if (!ecc->read_oob)
3972 ecc->read_oob = nand_read_oob_syndrome;
3973 if (!ecc->write_oob)
3974 ecc->write_oob = nand_write_oob_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01003975
Heiko Schocherff94bc42014-06-24 10:10:04 +02003976 if (mtd->writesize >= ecc->size) {
3977 if (!ecc->strength) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003978 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
3979 BUG();
3980 }
William Juulcfa460a2007-10-31 13:53:06 +01003981 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003982 }
Scott Woodd3963722015-06-26 19:03:26 -05003983 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
3984 ecc->size, mtd->writesize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003985 ecc->mode = NAND_ECC_SOFT;
William Juulcfa460a2007-10-31 13:53:06 +01003986
3987 case NAND_ECC_SOFT:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003988 ecc->calculate = nand_calculate_ecc;
3989 ecc->correct = nand_correct_data;
3990 ecc->read_page = nand_read_page_swecc;
3991 ecc->read_subpage = nand_read_subpage;
3992 ecc->write_page = nand_write_page_swecc;
3993 ecc->read_page_raw = nand_read_page_raw;
3994 ecc->write_page_raw = nand_write_page_raw;
3995 ecc->read_oob = nand_read_oob_std;
3996 ecc->write_oob = nand_write_oob_std;
3997 if (!ecc->size)
3998 ecc->size = 256;
3999 ecc->bytes = 3;
4000 ecc->strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01004001 break;
4002
Christian Hitz4c6de852011-10-12 09:31:59 +02004003 case NAND_ECC_SOFT_BCH:
4004 if (!mtd_nand_has_bch()) {
Heiko Schocher4e67c572014-07-15 16:08:43 +02004005 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004006 BUG();
Christian Hitz4c6de852011-10-12 09:31:59 +02004007 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004008 ecc->calculate = nand_bch_calculate_ecc;
4009 ecc->correct = nand_bch_correct_data;
4010 ecc->read_page = nand_read_page_swecc;
4011 ecc->read_subpage = nand_read_subpage;
4012 ecc->write_page = nand_write_page_swecc;
4013 ecc->read_page_raw = nand_read_page_raw;
4014 ecc->write_page_raw = nand_write_page_raw;
4015 ecc->read_oob = nand_read_oob_std;
4016 ecc->write_oob = nand_write_oob_std;
Christian Hitz4c6de852011-10-12 09:31:59 +02004017 /*
Scott Woodd3963722015-06-26 19:03:26 -05004018 * Board driver should supply ecc.size and ecc.strength values
4019 * to select how many bits are correctable. Otherwise, default
4020 * to 4 bits for large page devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02004021 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004022 if (!ecc->size && (mtd->oobsize >= 64)) {
4023 ecc->size = 512;
Scott Woodd3963722015-06-26 19:03:26 -05004024 ecc->strength = 4;
Christian Hitz4c6de852011-10-12 09:31:59 +02004025 }
Scott Woodd3963722015-06-26 19:03:26 -05004026
4027 /* See nand_bch_init() for details. */
Scott Woodceee07b2016-05-30 13:57:58 -05004028 ecc->bytes = 0;
4029 ecc->priv = nand_bch_init(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +02004030 if (!ecc->priv) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004031 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004032 BUG();
4033 }
Christian Hitz4c6de852011-10-12 09:31:59 +02004034 break;
4035
William Juulcfa460a2007-10-31 13:53:06 +01004036 case NAND_ECC_NONE:
Scott Woodd3963722015-06-26 19:03:26 -05004037 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004038 ecc->read_page = nand_read_page_raw;
4039 ecc->write_page = nand_write_page_raw;
4040 ecc->read_oob = nand_read_oob_std;
4041 ecc->read_page_raw = nand_read_page_raw;
4042 ecc->write_page_raw = nand_write_page_raw;
4043 ecc->write_oob = nand_write_oob_std;
4044 ecc->size = mtd->writesize;
4045 ecc->bytes = 0;
4046 ecc->strength = 0;
William Juulcfa460a2007-10-31 13:53:06 +01004047 break;
4048
4049 default:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004050 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juulcfa460a2007-10-31 13:53:06 +01004051 BUG();
4052 }
4053
Sergey Lapindfe64e22013-01-14 03:46:50 +00004054 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004055 if (!ecc->read_oob_raw)
4056 ecc->read_oob_raw = ecc->read_oob;
4057 if (!ecc->write_oob_raw)
4058 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004059
William Juulcfa460a2007-10-31 13:53:06 +01004060 /*
4061 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00004062 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01004063 */
Scott Woodceee07b2016-05-30 13:57:58 -05004064 mtd->oobavail = 0;
4065 if (ecc->layout) {
4066 for (i = 0; ecc->layout->oobfree[i].length; i++)
4067 mtd->oobavail += ecc->layout->oobfree[i].length;
4068 }
William Juulcfa460a2007-10-31 13:53:06 +01004069
Scott Woodd3963722015-06-26 19:03:26 -05004070 /* ECC sanity check: warn if it's too weak */
4071 if (!nand_ecc_strength_good(mtd))
4072 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
4073 mtd->name);
4074
William Juulcfa460a2007-10-31 13:53:06 +01004075 /*
4076 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00004077 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01004078 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004079 ecc->steps = mtd->writesize / ecc->size;
4080 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004081 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01004082 BUG();
4083 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004084 ecc->total = ecc->steps * ecc->bytes;
William Juulcfa460a2007-10-31 13:53:06 +01004085
Sergey Lapindfe64e22013-01-14 03:46:50 +00004086 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004087 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4088 switch (ecc->steps) {
William Juulcfa460a2007-10-31 13:53:06 +01004089 case 2:
4090 mtd->subpage_sft = 1;
4091 break;
4092 case 4:
4093 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05004094 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01004095 mtd->subpage_sft = 2;
4096 break;
4097 }
4098 }
4099 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4100
4101 /* Initialize state */
4102 chip->state = FL_READY;
4103
William Juulcfa460a2007-10-31 13:53:06 +01004104 /* Invalidate the pagebuffer reference */
4105 chip->pagebuf = -1;
4106
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004107 /* Large page NAND with SOFT_ECC should support subpage reads */
Scott Woodd3963722015-06-26 19:03:26 -05004108 switch (ecc->mode) {
4109 case NAND_ECC_SOFT:
4110 case NAND_ECC_SOFT_BCH:
4111 if (chip->page_shift > 9)
4112 chip->options |= NAND_SUBPAGE_READ;
4113 break;
4114
4115 default:
4116 break;
4117 }
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004118
William Juulcfa460a2007-10-31 13:53:06 +01004119 /* Fill in remaining MTD driver data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004120 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004121 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4122 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004123 mtd->_erase = nand_erase;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004124 mtd->_read = nand_read;
4125 mtd->_write = nand_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004126 mtd->_panic_write = panic_nand_write;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004127 mtd->_read_oob = nand_read_oob;
4128 mtd->_write_oob = nand_write_oob;
4129 mtd->_sync = nand_sync;
4130 mtd->_lock = NULL;
4131 mtd->_unlock = NULL;
Ezequiel Garcia86a720a2014-05-21 19:06:12 -03004132 mtd->_block_isreserved = nand_block_isreserved;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004133 mtd->_block_isbad = nand_block_isbad;
4134 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004135 mtd->writebufsize = mtd->writesize;
William Juulcfa460a2007-10-31 13:53:06 +01004136
Sergey Lapindfe64e22013-01-14 03:46:50 +00004137 /* propagate ecc info to mtd_info */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004138 mtd->ecclayout = ecc->layout;
4139 mtd->ecc_strength = ecc->strength;
4140 mtd->ecc_step_size = ecc->size;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004141 /*
4142 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4143 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4144 * properly set.
4145 */
4146 if (!mtd->bitflip_threshold)
Scott Woodd3963722015-06-26 19:03:26 -05004147 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
William Juulcfa460a2007-10-31 13:53:06 +01004148
Rostislav Lisovy35c204d2014-10-22 13:40:44 +02004149 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01004150}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004151EXPORT_SYMBOL(nand_scan_tail);
4152
William Juulcfa460a2007-10-31 13:53:06 +01004153/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02004154 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004155 * @mtd: MTD device structure
4156 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02004157 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00004158 * This fills out all the uninitialized function pointers with the defaults.
4159 * The flash ID is read and the mtd/chip structures are filled with the
Scott Woodceee07b2016-05-30 13:57:58 -05004160 * appropriate values.
Wolfgang Denk932394a2005-08-17 12:55:25 +02004161 */
William Juulcfa460a2007-10-31 13:53:06 +01004162int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004163{
William Juulcfa460a2007-10-31 13:53:06 +01004164 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004165
Lei Wen245eb902011-01-06 09:48:18 +08004166 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01004167 if (!ret)
4168 ret = nand_scan_tail(mtd);
4169 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004170}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004171EXPORT_SYMBOL(nand_scan);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004172
Heiko Schocherff94bc42014-06-24 10:10:04 +02004173MODULE_LICENSE("GPL");
4174MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4175MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4176MODULE_DESCRIPTION("Generic NAND flash driver code");