blob: c0e381ad2d155cf74b09df1ff4464012764bf129 [file] [log] [blame]
Wolfgang Denk932394a2005-08-17 12:55:25 +02001/*
2 * drivers/mtd/nand.c
3 *
4 * Overview:
5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02007 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02008 * Additional technical information is available on
Scott Woodc45912d2008-10-24 16:20:43 -05009 * http://www.linux-mtd.infradead.org/doc/nand.html
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020010 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020011 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
William Juulcfa460a2007-10-31 13:53:06 +010012 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk932394a2005-08-17 12:55:25 +020013 *
William Juulcfa460a2007-10-31 13:53:06 +010014 * Credits:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020015 * David Woodhouse for adding multichip support
16 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020017 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
18 * rework for 2K page size chips
19 *
William Juulcfa460a2007-10-31 13:53:06 +010020 * TODO:
Wolfgang Denk932394a2005-08-17 12:55:25 +020021 * Enable cached programming for 2k page size chips
22 * Check, if mtd->ecctype should be set to MTD_ECC_HW
Sergey Lapindfe64e22013-01-14 03:46:50 +000023 * if we have HW ECC support.
Scott Woodc45912d2008-10-24 16:20:43 -050024 * BBT table is not serialized, has to be fixed
Wolfgang Denk932394a2005-08-17 12:55:25 +020025 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020026 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License version 2 as
28 * published by the Free Software Foundation.
29 *
30 */
31
Heiko Schocherff94bc42014-06-24 10:10:04 +020032#ifndef __UBOOT__
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35#include <linux/module.h>
36#include <linux/delay.h>
37#include <linux/errno.h>
38#include <linux/err.h>
39#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/types.h>
42#include <linux/mtd/mtd.h>
43#include <linux/mtd/nand.h>
44#include <linux/mtd/nand_ecc.h>
45#include <linux/mtd/nand_bch.h>
46#include <linux/interrupt.h>
47#include <linux/bitops.h>
48#include <linux/leds.h>
49#include <linux/io.h>
50#include <linux/mtd/partitions.h>
51#else
52#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Wolfgang Denk932394a2005-08-17 12:55:25 +020053#include <common.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020054#include <malloc.h>
55#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010056#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000057#include <linux/compat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020058#include <linux/mtd/mtd.h>
59#include <linux/mtd/nand.h>
60#include <linux/mtd/nand_ecc.h>
Christian Hitz4c6de852011-10-12 09:31:59 +020061#include <linux/mtd/nand_bch.h>
Stefan Roese10bb62d2009-04-24 15:58:33 +020062#ifdef CONFIG_MTD_PARTITIONS
63#include <linux/mtd/partitions.h>
64#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020065#include <asm/io.h>
66#include <asm/errno.h>
67
Peter Tyser8da60122009-02-04 13:47:22 -060068/*
69 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
70 * a flash. NAND flash is initialized prior to interrupts so standard timers
71 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
72 * which is greater than (max NAND reset time / NAND status read time).
73 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
74 */
75#ifndef CONFIG_SYS_NAND_RESET_CNT
76#define CONFIG_SYS_NAND_RESET_CNT 200000
77#endif
78
Heiko Schocherff94bc42014-06-24 10:10:04 +020079static bool is_module_text_address(unsigned long addr) {return 0;}
80#endif
81
Wolfgang Denk932394a2005-08-17 12:55:25 +020082/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010083static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020084 .eccbytes = 3,
85 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010086 .oobfree = {
87 {.offset = 3,
88 .length = 2},
89 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020090 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020091};
92
William Juulcfa460a2007-10-31 13:53:06 +010093static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020094 .eccbytes = 6,
95 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010096 .oobfree = {
97 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020098 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020099};
100
William Juulcfa460a2007-10-31 13:53:06 +0100101static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200102 .eccbytes = 24,
103 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +0100104 40, 41, 42, 43, 44, 45, 46, 47,
105 48, 49, 50, 51, 52, 53, 54, 55,
106 56, 57, 58, 59, 60, 61, 62, 63},
107 .oobfree = {
108 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200109 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200110};
111
William Juulcfa460a2007-10-31 13:53:06 +0100112static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200113 .eccbytes = 48,
114 .eccpos = {
Christian Hitz90e3f392011-10-12 09:32:01 +0200115 80, 81, 82, 83, 84, 85, 86, 87,
116 88, 89, 90, 91, 92, 93, 94, 95,
117 96, 97, 98, 99, 100, 101, 102, 103,
William Juulcfa460a2007-10-31 13:53:06 +0100118 104, 105, 106, 107, 108, 109, 110, 111,
119 112, 113, 114, 115, 116, 117, 118, 119,
120 120, 121, 122, 123, 124, 125, 126, 127},
121 .oobfree = {
122 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200123 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200124};
125
Heiko Schocherff94bc42014-06-24 10:10:04 +0200126static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juulcfa460a2007-10-31 13:53:06 +0100127
128static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
129 struct mtd_oob_ops *ops);
130
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131/*
132 * For devices which display every fart in the system on a separate LED. Is
133 * compiled away when LED support is disabled.
134 */
135DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200136
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200137static int check_offs_len(struct mtd_info *mtd,
138 loff_t ofs, uint64_t len)
139{
140 struct nand_chip *chip = mtd->priv;
141 int ret = 0;
142
143 /* Start address must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200144 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
145 pr_debug("%s: unaligned address\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200146 ret = -EINVAL;
147 }
148
149 /* Length must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200150 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
151 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200152 ret = -EINVAL;
153 }
154
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200155 return ret;
156}
157
Wolfgang Denk932394a2005-08-17 12:55:25 +0200158/**
159 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000160 * @mtd: MTD device structure
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200161 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200162 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200163 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200164static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100165{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200166 struct nand_chip *chip = mtd->priv;
167
Heiko Schocherff94bc42014-06-24 10:10:04 +0200168#ifndef __UBOOT__
169 /* Release the controller and the chip */
170 spin_lock(&chip->controller->lock);
171 chip->controller->active = NULL;
172 chip->state = FL_READY;
173 wake_up(&chip->controller->wq);
174 spin_unlock(&chip->controller->lock);
175#else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200176 /* De-select the NAND device */
177 chip->select_chip(mtd, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200178#endif
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100179}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200180
181/**
182 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000183 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200184 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200185 * Default read function for 8bit buswidth
Wolfgang Denk932394a2005-08-17 12:55:25 +0200186 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200187#ifndef __UBOOT__
188static uint8_t nand_read_byte(struct mtd_info *mtd)
189#else
Simon Schwarz82645f82011-10-31 06:34:44 +0000190uint8_t nand_read_byte(struct mtd_info *mtd)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200191#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200192{
William Juulcfa460a2007-10-31 13:53:06 +0100193 struct nand_chip *chip = mtd->priv;
194 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200195}
196
197/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200198 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000199 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
200 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200201 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000202 * Default read function for 16bit buswidth with endianness conversion.
203 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200204 */
William Juulcfa460a2007-10-31 13:53:06 +0100205static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200206{
William Juulcfa460a2007-10-31 13:53:06 +0100207 struct nand_chip *chip = mtd->priv;
208 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200209}
210
211/**
212 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000213 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200214 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000215 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200216 */
217static u16 nand_read_word(struct mtd_info *mtd)
218{
William Juulcfa460a2007-10-31 13:53:06 +0100219 struct nand_chip *chip = mtd->priv;
220 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200221}
222
223/**
224 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000225 * @mtd: MTD device structure
226 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200227 *
228 * Default select function for 1 chip devices.
229 */
William Juulcfa460a2007-10-31 13:53:06 +0100230static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200231{
William Juulcfa460a2007-10-31 13:53:06 +0100232 struct nand_chip *chip = mtd->priv;
233
234 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200235 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100236 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200237 break;
238 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200239 break;
240
241 default:
242 BUG();
243 }
244}
245
246/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200247 * nand_write_byte - [DEFAULT] write single byte to chip
248 * @mtd: MTD device structure
249 * @byte: value to write
250 *
251 * Default function to write a byte to I/O[7:0]
252 */
253static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
254{
255 struct nand_chip *chip = mtd->priv;
256
257 chip->write_buf(mtd, &byte, 1);
258}
259
260/**
261 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
262 * @mtd: MTD device structure
263 * @byte: value to write
264 *
265 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
266 */
267static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
268{
269 struct nand_chip *chip = mtd->priv;
270 uint16_t word = byte;
271
272 /*
273 * It's not entirely clear what should happen to I/O[15:8] when writing
274 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
275 *
276 * When the host supports a 16-bit bus width, only data is
277 * transferred at the 16-bit width. All address and command line
278 * transfers shall use only the lower 8-bits of the data bus. During
279 * command transfers, the host may place any value on the upper
280 * 8-bits of the data bus. During address transfers, the host shall
281 * set the upper 8-bits of the data bus to 00h.
282 *
283 * One user of the write_byte callback is nand_onfi_set_features. The
284 * four parameters are specified to be written to I/O[7:0], but this is
285 * neither an address nor a command transfer. Let's assume a 0 on the
286 * upper I/O lines is OK.
287 */
288 chip->write_buf(mtd, (uint8_t *)&word, 2);
289}
290
291#if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
292static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
293{
294 int i;
295
296 for (i = 0; i < len; i++)
297 writeb(buf[i], addr);
298}
299static void ioread8_rep(void *addr, uint8_t *buf, int len)
300{
301 int i;
302
303 for (i = 0; i < len; i++)
304 buf[i] = readb(addr);
305}
306
307static void ioread16_rep(void *addr, void *buf, int len)
308{
309 int i;
310 u16 *p = (u16 *) buf;
Stefan Roesebe16aba2014-09-05 09:57:01 +0200311
Heiko Schocherff94bc42014-06-24 10:10:04 +0200312 for (i = 0; i < len; i++)
313 p[i] = readw(addr);
314}
315
316static void iowrite16_rep(void *addr, void *buf, int len)
317{
318 int i;
319 u16 *p = (u16 *) buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200320
321 for (i = 0; i < len; i++)
322 writew(p[i], addr);
323}
324#endif
325
326/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200327 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000328 * @mtd: MTD device structure
329 * @buf: data buffer
330 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200331 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000332 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200333 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200334#ifndef __UBOOT__
335static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
336#else
Simon Schwarz82645f82011-10-31 06:34:44 +0000337void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200338#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200339{
William Juulcfa460a2007-10-31 13:53:06 +0100340 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200341
Heiko Schocherff94bc42014-06-24 10:10:04 +0200342 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200343}
344
345/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200346 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000347 * @mtd: MTD device structure
348 * @buf: buffer to store date
349 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200350 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000351 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200352 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200353#ifndef __UBOOT__
354static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
355#else
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400356void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200357#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200358{
William Juulcfa460a2007-10-31 13:53:06 +0100359 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200360
Heiko Schocherff94bc42014-06-24 10:10:04 +0200361 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200362}
363
Heiko Schocherff94bc42014-06-24 10:10:04 +0200364/**
365 * nand_write_buf16 - [DEFAULT] write buffer to chip
366 * @mtd: MTD device structure
367 * @buf: data buffer
368 * @len: number of bytes to write
369 *
370 * Default write function for 16bit buswidth.
371 */
372#ifndef __UBOOT__
373static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
374#else
375void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
376#endif
377{
378 struct nand_chip *chip = mtd->priv;
379 u16 *p = (u16 *) buf;
380
381 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
382}
383
384/**
385 * nand_read_buf16 - [DEFAULT] read chip data into buffer
386 * @mtd: MTD device structure
387 * @buf: buffer to store date
388 * @len: number of bytes to read
389 *
390 * Default read function for 16bit buswidth.
391 */
392#ifndef __UBOOT__
393static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
394#else
395void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
396#endif
397{
398 struct nand_chip *chip = mtd->priv;
399 u16 *p = (u16 *) buf;
400
401 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
402}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200403
404/**
405 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000406 * @mtd: MTD device structure
407 * @ofs: offset from device start
408 * @getchip: 0, if the chip is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200409 *
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200410 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200411 */
412static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
413{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000414 int page, chipnr, res = 0, i = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100415 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200416 u16 bad;
417
Sergey Lapindfe64e22013-01-14 03:46:50 +0000418 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200419 ofs += mtd->erasesize - mtd->writesize;
420
William Juulcfa460a2007-10-31 13:53:06 +0100421 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200422
Wolfgang Denk932394a2005-08-17 12:55:25 +0200423 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100424 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200425
Heiko Schocherff94bc42014-06-24 10:10:04 +0200426 nand_get_device(mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200427
428 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100429 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200430 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200431
Sergey Lapindfe64e22013-01-14 03:46:50 +0000432 do {
433 if (chip->options & NAND_BUSWIDTH_16) {
434 chip->cmdfunc(mtd, NAND_CMD_READOOB,
435 chip->badblockpos & 0xFE, page);
436 bad = cpu_to_le16(chip->read_word(mtd));
437 if (chip->badblockpos & 0x1)
438 bad >>= 8;
439 else
440 bad &= 0xFF;
441 } else {
442 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
443 page);
444 bad = chip->read_byte(mtd);
445 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200446
Sergey Lapindfe64e22013-01-14 03:46:50 +0000447 if (likely(chip->badblockbits == 8))
448 res = bad != 0xFF;
449 else
450 res = hweight8(bad) < chip->badblockbits;
451 ofs += mtd->writesize;
452 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
453 i++;
454 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200455
Heiko Schocherff94bc42014-06-24 10:10:04 +0200456 if (getchip) {
457 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200458 nand_release_device(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200459 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200460
Wolfgang Denk932394a2005-08-17 12:55:25 +0200461 return res;
462}
463
464/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200465 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapindfe64e22013-01-14 03:46:50 +0000466 * @mtd: MTD device structure
467 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200468 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000469 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherff94bc42014-06-24 10:10:04 +0200470 * specific driver. It provides the details for writing a bad block marker to a
471 * block.
472 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200473static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
474{
William Juulcfa460a2007-10-31 13:53:06 +0100475 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200476 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +0100477 uint8_t buf[2] = { 0, 0 };
Heiko Schocherff94bc42014-06-24 10:10:04 +0200478 int ret = 0, res, i = 0;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200479
Heiko Schocherff94bc42014-06-24 10:10:04 +0200480 ops.datbuf = NULL;
481 ops.oobbuf = buf;
482 ops.ooboffs = chip->badblockpos;
483 if (chip->options & NAND_BUSWIDTH_16) {
484 ops.ooboffs &= ~0x01;
485 ops.len = ops.ooblen = 2;
486 } else {
487 ops.len = ops.ooblen = 1;
488 }
489 ops.mode = MTD_OPS_PLACE_OOB;
490
491 /* Write to first/last page(s) if necessary */
492 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
493 ofs += mtd->erasesize - mtd->writesize;
494 do {
495 res = nand_do_write_oob(mtd, ofs, &ops);
496 if (!ret)
497 ret = res;
498
499 i++;
500 ofs += mtd->writesize;
501 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
502
503 return ret;
504}
505
506/**
507 * nand_block_markbad_lowlevel - mark a block bad
508 * @mtd: MTD device structure
509 * @ofs: offset from device start
510 *
511 * This function performs the generic NAND bad block marking steps (i.e., bad
512 * block table(s) and/or marker(s)). We only allow the hardware driver to
513 * specify how to write bad block markers to OOB (chip->block_markbad).
514 *
515 * We try operations in the following order:
516 * (1) erase the affected block, to allow OOB marker to be written cleanly
517 * (2) write bad block marker to OOB area of affected block (unless flag
518 * NAND_BBT_NO_OOB_BBM is present)
519 * (3) update the BBT
520 * Note that we retain the first error encountered in (2) or (3), finish the
521 * procedures, and dump the error in the end.
522*/
523static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
524{
525 struct nand_chip *chip = mtd->priv;
526 int res, ret = 0;
527
528 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000529 struct erase_info einfo;
530
531 /* Attempt erase before marking OOB */
532 memset(&einfo, 0, sizeof(einfo));
533 einfo.mtd = mtd;
534 einfo.addr = ofs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200535 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000536 nand_erase_nand(mtd, &einfo, 0);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200537
Heiko Schocherff94bc42014-06-24 10:10:04 +0200538 /* Write bad block marker to OOB */
539 nand_get_device(mtd, FL_WRITING);
540 ret = chip->block_markbad(mtd, ofs);
Scott Woodc45912d2008-10-24 16:20:43 -0500541 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100542 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000543
Heiko Schocherff94bc42014-06-24 10:10:04 +0200544 /* Mark block bad in BBT */
545 if (chip->bbt) {
546 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000547 if (!ret)
548 ret = res;
549 }
550
William Juulcfa460a2007-10-31 13:53:06 +0100551 if (!ret)
552 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500553
William Juulcfa460a2007-10-31 13:53:06 +0100554 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200555}
556
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200557/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200558 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000559 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200560 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000561 * Check, if the device is write protected. The function expects, that the
562 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200563 */
William Juulcfa460a2007-10-31 13:53:06 +0100564static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200565{
William Juulcfa460a2007-10-31 13:53:06 +0100566 struct nand_chip *chip = mtd->priv;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200567
Sergey Lapindfe64e22013-01-14 03:46:50 +0000568 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200569 if (chip->options & NAND_BROKEN_XD)
570 return 0;
571
Wolfgang Denk932394a2005-08-17 12:55:25 +0200572 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100573 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
574 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200575}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100576
Wolfgang Denk932394a2005-08-17 12:55:25 +0200577/**
578 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000579 * @mtd: MTD device structure
580 * @ofs: offset from device start
581 * @getchip: 0, if the chip is already selected
582 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200583 *
584 * Check, if the block is bad. Either by reading the bad block table or
585 * calling of the scan function.
586 */
William Juulcfa460a2007-10-31 13:53:06 +0100587static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
588 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200589{
William Juulcfa460a2007-10-31 13:53:06 +0100590 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200591
Masahiro Yamadaab37b762014-12-26 22:20:58 +0900592 if (!(chip->options & NAND_SKIP_BBTSCAN) &&
593 !(chip->options & NAND_BBT_SCANNED)) {
Rostislav Lisovy35c204d2014-10-22 13:40:44 +0200594 chip->options |= NAND_BBT_SCANNED;
Masahiro Yamadabf80ee62014-12-26 22:20:57 +0900595 chip->scan_bbt(mtd);
Rostislav Lisovy35c204d2014-10-22 13:40:44 +0200596 }
597
William Juulcfa460a2007-10-31 13:53:06 +0100598 if (!chip->bbt)
599 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200600
Wolfgang Denk932394a2005-08-17 12:55:25 +0200601 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100602 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200603}
604
Heiko Schocherff94bc42014-06-24 10:10:04 +0200605#ifndef __UBOOT__
606/**
607 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
608 * @mtd: MTD device structure
609 * @timeo: Timeout
610 *
611 * Helper function for nand_wait_ready used when needing to wait in interrupt
612 * context.
613 */
614static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
615{
616 struct nand_chip *chip = mtd->priv;
617 int i;
618
619 /* Wait for the device to get ready */
620 for (i = 0; i < timeo; i++) {
621 if (chip->dev_ready(mtd))
622 break;
623 touch_softlockup_watchdog();
624 mdelay(1);
625 }
626}
627#endif
628
Sergey Lapindfe64e22013-01-14 03:46:50 +0000629/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juulcfa460a2007-10-31 13:53:06 +0100630void nand_wait_ready(struct mtd_info *mtd)
631{
632 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200633#ifndef __UBOOT__
634 unsigned long timeo = jiffies + msecs_to_jiffies(20);
635
636 /* 400ms timeout */
637 if (in_interrupt() || oops_in_progress)
638 return panic_nand_wait_ready(mtd, 400);
639
640 led_trigger_event(nand_led_trigger, LED_FULL);
641 /* Wait until command is processed or timeout occurs */
642 do {
643 if (chip->dev_ready(mtd))
644 break;
645 touch_softlockup_watchdog();
646 } while (time_before(jiffies, timeo));
647 led_trigger_event(nand_led_trigger, LED_OFF);
648#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200649 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000650 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100651
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000652 time_start = get_timer(0);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000653 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000654 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100655 if (chip->dev_ready)
656 if (chip->dev_ready(mtd))
657 break;
658 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200659#endif
William Juulcfa460a2007-10-31 13:53:06 +0100660}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200661EXPORT_SYMBOL_GPL(nand_wait_ready);
William Juulcfa460a2007-10-31 13:53:06 +0100662
Wolfgang Denk932394a2005-08-17 12:55:25 +0200663/**
664 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000665 * @mtd: MTD device structure
666 * @command: the command to be sent
667 * @column: the column address for this command, -1 if none
668 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200669 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000670 * Send command to NAND device. This function is used for small page devices
Heiko Schocherff94bc42014-06-24 10:10:04 +0200671 * (512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200672 */
William Juulcfa460a2007-10-31 13:53:06 +0100673static void nand_command(struct mtd_info *mtd, unsigned int command,
674 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200675{
William Juulcfa460a2007-10-31 13:53:06 +0100676 register struct nand_chip *chip = mtd->priv;
677 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600678 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200679
Sergey Lapindfe64e22013-01-14 03:46:50 +0000680 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200681 if (command == NAND_CMD_SEQIN) {
682 int readcmd;
683
William Juulcfa460a2007-10-31 13:53:06 +0100684 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200685 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100686 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200687 readcmd = NAND_CMD_READOOB;
688 } else if (column < 256) {
689 /* First 256 bytes --> READ0 */
690 readcmd = NAND_CMD_READ0;
691 } else {
692 column -= 256;
693 readcmd = NAND_CMD_READ1;
694 }
William Juulcfa460a2007-10-31 13:53:06 +0100695 chip->cmd_ctrl(mtd, readcmd, ctrl);
696 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200697 }
William Juulcfa460a2007-10-31 13:53:06 +0100698 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200699
Sergey Lapindfe64e22013-01-14 03:46:50 +0000700 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100701 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
702 /* Serially input address */
703 if (column != -1) {
704 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200705 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530706 !nand_opcode_8bits(command))
William Juulcfa460a2007-10-31 13:53:06 +0100707 column >>= 1;
708 chip->cmd_ctrl(mtd, column, ctrl);
709 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200710 }
William Juulcfa460a2007-10-31 13:53:06 +0100711 if (page_addr != -1) {
712 chip->cmd_ctrl(mtd, page_addr, ctrl);
713 ctrl &= ~NAND_CTRL_CHANGE;
714 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
715 /* One more address cycle for devices > 32MiB */
716 if (chip->chipsize > (32 << 20))
717 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
718 }
719 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200720
721 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000722 * Program and erase have their own busy handlers status and sequential
723 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100724 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200725 switch (command) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200726
Wolfgang Denk932394a2005-08-17 12:55:25 +0200727 case NAND_CMD_PAGEPROG:
728 case NAND_CMD_ERASE1:
729 case NAND_CMD_ERASE2:
730 case NAND_CMD_SEQIN:
731 case NAND_CMD_STATUS:
732 return;
733
734 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100735 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200736 break;
William Juulcfa460a2007-10-31 13:53:06 +0100737 udelay(chip->chip_delay);
738 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
739 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
740 chip->cmd_ctrl(mtd,
741 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600742 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
743 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200744 return;
745
William Juulcfa460a2007-10-31 13:53:06 +0100746 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200747 default:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200748 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200749 * If we don't have access to the busy pin, we apply the given
750 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100751 */
752 if (!chip->dev_ready) {
753 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200754 return;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200755 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200756 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000757 /*
758 * Apply this short delay always to ensure that we do wait tWB in
759 * any case on any machine.
760 */
William Juulcfa460a2007-10-31 13:53:06 +0100761 ndelay(100);
762
763 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200764}
765
766/**
767 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000768 * @mtd: MTD device structure
769 * @command: the command to be sent
770 * @column: the column address for this command, -1 if none
771 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200772 *
William Juulcfa460a2007-10-31 13:53:06 +0100773 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000774 * devices. We don't have the separate regions as we have in the small page
775 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200776 */
William Juulcfa460a2007-10-31 13:53:06 +0100777static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
778 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200779{
William Juulcfa460a2007-10-31 13:53:06 +0100780 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600781 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200782
783 /* Emulate NAND_CMD_READOOB */
784 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100785 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200786 command = NAND_CMD_READ0;
787 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200788
William Juulcfa460a2007-10-31 13:53:06 +0100789 /* Command latch cycle */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200790 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200791
792 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100793 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200794
795 /* Serially input address */
796 if (column != -1) {
797 /* Adjust columns for 16 bit buswidth */
Heiko Schocher4e67c572014-07-15 16:08:43 +0200798 if (chip->options & NAND_BUSWIDTH_16 &&
Brian Norris27ce9e42014-05-06 00:46:17 +0530799 !nand_opcode_8bits(command))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200800 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100801 chip->cmd_ctrl(mtd, column, ctrl);
802 ctrl &= ~NAND_CTRL_CHANGE;
803 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200804 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200805 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100806 chip->cmd_ctrl(mtd, page_addr, ctrl);
807 chip->cmd_ctrl(mtd, page_addr >> 8,
808 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200809 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100810 if (chip->chipsize > (128 << 20))
811 chip->cmd_ctrl(mtd, page_addr >> 16,
812 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200813 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200814 }
William Juulcfa460a2007-10-31 13:53:06 +0100815 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200816
817 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000818 * Program and erase have their own busy handlers status, sequential
819 * in, and deplete1 need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100820 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200821 switch (command) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200822
Wolfgang Denk932394a2005-08-17 12:55:25 +0200823 case NAND_CMD_CACHEDPROG:
824 case NAND_CMD_PAGEPROG:
825 case NAND_CMD_ERASE1:
826 case NAND_CMD_ERASE2:
827 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100828 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200829 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100830 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200831
832 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100833 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200834 break;
William Juulcfa460a2007-10-31 13:53:06 +0100835 udelay(chip->chip_delay);
836 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
837 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
838 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
839 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600840 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
841 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100842 return;
843
844 case NAND_CMD_RNDOUT:
845 /* No ready / busy check necessary */
846 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
847 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
848 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
849 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200850 return;
851
852 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100853 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
854 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
855 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
856 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200857
William Juulcfa460a2007-10-31 13:53:06 +0100858 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200859 default:
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200860 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200861 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000862 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100863 */
864 if (!chip->dev_ready) {
865 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200866 return;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200867 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200868 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200869
Sergey Lapindfe64e22013-01-14 03:46:50 +0000870 /*
871 * Apply this short delay always to ensure that we do wait tWB in
872 * any case on any machine.
873 */
William Juulcfa460a2007-10-31 13:53:06 +0100874 ndelay(100);
875
876 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200877}
878
879/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200880 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000881 * @chip: the nand chip descriptor
882 * @mtd: MTD device structure
883 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200884 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200885 * Used when in panic, no locks are taken.
886 */
887static void panic_nand_get_device(struct nand_chip *chip,
888 struct mtd_info *mtd, int new_state)
889{
890 /* Hardware controller shared among independent devices */
891 chip->controller->active = chip;
892 chip->state = new_state;
893}
894
895/**
896 * nand_get_device - [GENERIC] Get chip for selected access
897 * @mtd: MTD device structure
898 * @new_state: the state which is requested
899 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200900 * Get the device and lock it for exclusive access
901 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200902static int
Heiko Schocherff94bc42014-06-24 10:10:04 +0200903nand_get_device(struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100904{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200905 struct nand_chip *chip = mtd->priv;
906#ifndef __UBOOT__
907 spinlock_t *lock = &chip->controller->lock;
908 wait_queue_head_t *wq = &chip->controller->wq;
909 DECLARE_WAITQUEUE(wait, current);
910retry:
911 spin_lock(lock);
912
913 /* Hardware controller shared among independent devices */
914 if (!chip->controller->active)
915 chip->controller->active = chip;
916
917 if (chip->controller->active == chip && chip->state == FL_READY) {
918 chip->state = new_state;
919 spin_unlock(lock);
920 return 0;
921 }
922 if (new_state == FL_PM_SUSPENDED) {
923 if (chip->controller->active->state == FL_PM_SUSPENDED) {
924 chip->state = FL_PM_SUSPENDED;
925 spin_unlock(lock);
926 return 0;
927 }
928 }
929 set_current_state(TASK_UNINTERRUPTIBLE);
930 add_wait_queue(wq, &wait);
931 spin_unlock(lock);
932 schedule();
933 remove_wait_queue(wq, &wait);
934 goto retry;
935#else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200936 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100937 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200938#endif
939}
940
941/**
942 * panic_nand_wait - [GENERIC] wait until the command is done
943 * @mtd: MTD device structure
944 * @chip: NAND chip structure
945 * @timeo: timeout
946 *
947 * Wait for command done. This is a helper function for nand_wait used when
948 * we are in interrupt context. May happen when in panic and trying to write
949 * an oops through mtdoops.
950 */
951static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
952 unsigned long timeo)
953{
954 int i;
955 for (i = 0; i < timeo; i++) {
956 if (chip->dev_ready) {
957 if (chip->dev_ready(mtd))
958 break;
959 } else {
960 if (chip->read_byte(mtd) & NAND_STATUS_READY)
961 break;
962 }
963 mdelay(1);
964 }
William Juulcfa460a2007-10-31 13:53:06 +0100965}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200966
967/**
Sergey Lapindfe64e22013-01-14 03:46:50 +0000968 * nand_wait - [DEFAULT] wait until the command is done
969 * @mtd: MTD device structure
970 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200971 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000972 * Wait for command done. This applies to erase and program only. Erase can
973 * take up to 400ms and program up to 20ms according to general NAND and
974 * SmartMedia specs.
William Juulcfa460a2007-10-31 13:53:06 +0100975 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200976static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200977{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100978
Heiko Schocherff94bc42014-06-24 10:10:04 +0200979 int status, state = chip->state;
980 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100981
Heiko Schocherff94bc42014-06-24 10:10:04 +0200982 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100983
Heiko Schocherff94bc42014-06-24 10:10:04 +0200984 /*
985 * Apply this short delay always to ensure that we do wait tWB in any
986 * case on any machine.
987 */
988 ndelay(100);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100989
Heiko Schocherff94bc42014-06-24 10:10:04 +0200990 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
991
992#ifndef __UBOOT__
993 if (in_interrupt() || oops_in_progress)
994 panic_nand_wait(mtd, chip, timeo);
995 else {
996 timeo = jiffies + msecs_to_jiffies(timeo);
997 while (time_before(jiffies, timeo)) {
998 if (chip->dev_ready) {
999 if (chip->dev_ready(mtd))
1000 break;
1001 } else {
1002 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1003 break;
1004 }
1005 cond_resched();
Stefan Roese15784862006-11-27 17:22:19 +01001006 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001007 }
1008#else
1009 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
1010 u32 time_start;
1011
1012 time_start = get_timer(0);
1013 while (get_timer(time_start) < timer) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001014 if (chip->dev_ready) {
1015 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001016 break;
1017 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001018 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001019 break;
1020 }
1021 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001022#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +02001023 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +01001024
Heiko Schocherff94bc42014-06-24 10:10:04 +02001025 status = (int)chip->read_byte(mtd);
1026 /* This can happen if in case of timeout or buggy dev_ready */
1027 WARN_ON(!(status & NAND_STATUS_READY));
1028 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001029}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001030
Heiko Schocherff94bc42014-06-24 10:10:04 +02001031#ifndef __UBOOT__
1032/**
1033 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1034 * @mtd: mtd info
1035 * @ofs: offset to start unlock from
1036 * @len: length to unlock
1037 * @invert: when = 0, unlock the range of blocks within the lower and
1038 * upper boundary address
1039 * when = 1, unlock the range of blocks outside the boundaries
1040 * of the lower and upper boundary address
1041 *
1042 * Returs unlock status.
1043 */
1044static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1045 uint64_t len, int invert)
1046{
1047 int ret = 0;
1048 int status, page;
1049 struct nand_chip *chip = mtd->priv;
1050
1051 /* Submit address of first page to unlock */
1052 page = ofs >> chip->page_shift;
1053 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1054
1055 /* Submit address of last page to unlock */
1056 page = (ofs + len) >> chip->page_shift;
1057 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1058 (page | invert) & chip->pagemask);
1059
1060 /* Call wait ready function */
1061 status = chip->waitfunc(mtd, chip);
1062 /* See if device thinks it succeeded */
1063 if (status & NAND_STATUS_FAIL) {
1064 pr_debug("%s: error status = 0x%08x\n",
1065 __func__, status);
1066 ret = -EIO;
1067 }
1068
1069 return ret;
1070}
1071
1072/**
1073 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1074 * @mtd: mtd info
1075 * @ofs: offset to start unlock from
1076 * @len: length to unlock
1077 *
1078 * Returns unlock status.
1079 */
1080int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1081{
1082 int ret = 0;
1083 int chipnr;
1084 struct nand_chip *chip = mtd->priv;
1085
1086 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1087 __func__, (unsigned long long)ofs, len);
1088
1089 if (check_offs_len(mtd, ofs, len))
1090 ret = -EINVAL;
1091
1092 /* Align to last block address if size addresses end of the device */
1093 if (ofs + len == mtd->size)
1094 len -= mtd->erasesize;
1095
1096 nand_get_device(mtd, FL_UNLOCKING);
1097
1098 /* Shift to get chip number */
1099 chipnr = ofs >> chip->chip_shift;
1100
1101 chip->select_chip(mtd, chipnr);
1102
1103 /* Check, if it is write protected */
1104 if (nand_check_wp(mtd)) {
1105 pr_debug("%s: device is write protected!\n",
1106 __func__);
1107 ret = -EIO;
1108 goto out;
1109 }
1110
1111 ret = __nand_unlock(mtd, ofs, len, 0);
1112
1113out:
1114 chip->select_chip(mtd, -1);
1115 nand_release_device(mtd);
1116
1117 return ret;
1118}
1119EXPORT_SYMBOL(nand_unlock);
1120
1121/**
1122 * nand_lock - [REPLACEABLE] locks all blocks present in the device
1123 * @mtd: mtd info
1124 * @ofs: offset to start unlock from
1125 * @len: length to unlock
1126 *
1127 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1128 * have this feature, but it allows only to lock all blocks, not for specified
1129 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1130 * now.
1131 *
1132 * Returns lock status.
1133 */
1134int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1135{
1136 int ret = 0;
1137 int chipnr, status, page;
1138 struct nand_chip *chip = mtd->priv;
1139
1140 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1141 __func__, (unsigned long long)ofs, len);
1142
1143 if (check_offs_len(mtd, ofs, len))
1144 ret = -EINVAL;
1145
1146 nand_get_device(mtd, FL_LOCKING);
1147
1148 /* Shift to get chip number */
1149 chipnr = ofs >> chip->chip_shift;
1150
1151 chip->select_chip(mtd, chipnr);
1152
1153 /* Check, if it is write protected */
1154 if (nand_check_wp(mtd)) {
1155 pr_debug("%s: device is write protected!\n",
1156 __func__);
1157 status = MTD_ERASE_FAILED;
1158 ret = -EIO;
1159 goto out;
1160 }
1161
1162 /* Submit address of first page to lock */
1163 page = ofs >> chip->page_shift;
1164 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1165
1166 /* Call wait ready function */
1167 status = chip->waitfunc(mtd, chip);
1168 /* See if device thinks it succeeded */
1169 if (status & NAND_STATUS_FAIL) {
1170 pr_debug("%s: error status = 0x%08x\n",
1171 __func__, status);
1172 ret = -EIO;
1173 goto out;
1174 }
1175
1176 ret = __nand_unlock(mtd, ofs, len, 0x1);
1177
1178out:
1179 chip->select_chip(mtd, -1);
1180 nand_release_device(mtd);
1181
1182 return ret;
1183}
1184EXPORT_SYMBOL(nand_lock);
1185#endif
1186
Wolfgang Denk932394a2005-08-17 12:55:25 +02001187/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001188 * nand_read_page_raw - [INTERN] read raw page data without ecc
1189 * @mtd: mtd info structure
1190 * @chip: nand chip info structure
1191 * @buf: buffer to store read data
1192 * @oob_required: caller requires OOB data read to chip->oob_poi
1193 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001194 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001195 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001196 */
William Juulcfa460a2007-10-31 13:53:06 +01001197static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001198 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001199{
William Juulcfa460a2007-10-31 13:53:06 +01001200 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001201 if (oob_required)
1202 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01001203 return 0;
1204}
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001205
William Juulcfa460a2007-10-31 13:53:06 +01001206/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001207 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1208 * @mtd: mtd info structure
1209 * @chip: nand chip info structure
1210 * @buf: buffer to store read data
1211 * @oob_required: caller requires OOB data read to chip->oob_poi
1212 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001213 *
1214 * We need a special oob layout and handling even when OOB isn't used.
1215 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001216static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001217 struct nand_chip *chip, uint8_t *buf,
1218 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -05001219{
1220 int eccsize = chip->ecc.size;
1221 int eccbytes = chip->ecc.bytes;
1222 uint8_t *oob = chip->oob_poi;
1223 int steps, size;
1224
1225 for (steps = chip->ecc.steps; steps > 0; steps--) {
1226 chip->read_buf(mtd, buf, eccsize);
1227 buf += eccsize;
1228
1229 if (chip->ecc.prepad) {
1230 chip->read_buf(mtd, oob, chip->ecc.prepad);
1231 oob += chip->ecc.prepad;
1232 }
1233
1234 chip->read_buf(mtd, oob, eccbytes);
1235 oob += eccbytes;
1236
1237 if (chip->ecc.postpad) {
1238 chip->read_buf(mtd, oob, chip->ecc.postpad);
1239 oob += chip->ecc.postpad;
1240 }
1241 }
1242
1243 size = mtd->oobsize - (oob - chip->oob_poi);
1244 if (size)
1245 chip->read_buf(mtd, oob, size);
1246
1247 return 0;
1248}
1249
1250/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001251 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1252 * @mtd: mtd info structure
1253 * @chip: nand chip info structure
1254 * @buf: buffer to store read data
1255 * @oob_required: caller requires OOB data read to chip->oob_poi
1256 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001257 */
1258static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001259 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001260{
1261 int i, eccsize = chip->ecc.size;
1262 int eccbytes = chip->ecc.bytes;
1263 int eccsteps = chip->ecc.steps;
1264 uint8_t *p = buf;
1265 uint8_t *ecc_calc = chip->buffers->ecccalc;
1266 uint8_t *ecc_code = chip->buffers->ecccode;
1267 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001268 unsigned int max_bitflips = 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001269
Sergey Lapindfe64e22013-01-14 03:46:50 +00001270 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001271
William Juulcfa460a2007-10-31 13:53:06 +01001272 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1273 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001274
William Juulcfa460a2007-10-31 13:53:06 +01001275 for (i = 0; i < chip->ecc.total; i++)
1276 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001277
William Juulcfa460a2007-10-31 13:53:06 +01001278 eccsteps = chip->ecc.steps;
1279 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001280
William Juulcfa460a2007-10-31 13:53:06 +01001281 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1282 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001283
William Juulcfa460a2007-10-31 13:53:06 +01001284 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001285 if (stat < 0) {
Scott Woodc45912d2008-10-24 16:20:43 -05001286 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001287 } else {
Scott Woodc45912d2008-10-24 16:20:43 -05001288 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001289 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1290 }
Scott Woodc45912d2008-10-24 16:20:43 -05001291 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001292 return max_bitflips;
Scott Woodc45912d2008-10-24 16:20:43 -05001293}
1294
1295/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001296 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapindfe64e22013-01-14 03:46:50 +00001297 * @mtd: mtd info structure
1298 * @chip: nand chip info structure
1299 * @data_offs: offset of requested data within the page
1300 * @readlen: data length
1301 * @bufpoi: buffer to store read data
Heiko Schocher4e67c572014-07-15 16:08:43 +02001302 * @page: page number to read
Scott Woodc45912d2008-10-24 16:20:43 -05001303 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001304static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001305 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1306 int page)
Scott Woodc45912d2008-10-24 16:20:43 -05001307{
1308 int start_step, end_step, num_steps;
1309 uint32_t *eccpos = chip->ecc.layout->eccpos;
1310 uint8_t *p;
1311 int data_col_addr, i, gaps = 0;
1312 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1313 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001314 int index;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001315 unsigned int max_bitflips = 0;
Scott Woodc45912d2008-10-24 16:20:43 -05001316
Sergey Lapindfe64e22013-01-14 03:46:50 +00001317 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -05001318 start_step = data_offs / chip->ecc.size;
1319 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1320 num_steps = end_step - start_step + 1;
Heiko Schocher4e67c572014-07-15 16:08:43 +02001321 index = start_step * chip->ecc.bytes;
Scott Woodc45912d2008-10-24 16:20:43 -05001322
Sergey Lapindfe64e22013-01-14 03:46:50 +00001323 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -05001324 datafrag_len = num_steps * chip->ecc.size;
1325 eccfrag_len = num_steps * chip->ecc.bytes;
1326
1327 data_col_addr = start_step * chip->ecc.size;
1328 /* If we read not a page aligned data */
1329 if (data_col_addr != 0)
1330 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1331
1332 p = bufpoi + data_col_addr;
1333 chip->read_buf(mtd, p, datafrag_len);
1334
Sergey Lapindfe64e22013-01-14 03:46:50 +00001335 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -05001336 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1337 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1338
Sergey Lapindfe64e22013-01-14 03:46:50 +00001339 /*
1340 * The performance is faster if we position offsets according to
1341 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1342 */
Scott Woodc45912d2008-10-24 16:20:43 -05001343 for (i = 0; i < eccfrag_len - 1; i++) {
1344 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1345 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1346 gaps = 1;
1347 break;
1348 }
1349 }
1350 if (gaps) {
1351 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1352 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1353 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001354 /*
1355 * Send the command to read the particular ECC bytes take care
1356 * about buswidth alignment in read_buf.
1357 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001358 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -05001359 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001360 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001361 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001362 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001363 aligned_len++;
1364
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001365 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1366 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001367 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1368 }
1369
1370 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001371 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001372
1373 p = bufpoi + data_col_addr;
1374 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1375 int stat;
1376
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001377 stat = chip->ecc.correct(mtd, p,
1378 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001379 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001380 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001381 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001382 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001383 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1384 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001385 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001386 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001387}
1388
Wolfgang Denk932394a2005-08-17 12:55:25 +02001389/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001390 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1391 * @mtd: mtd info structure
1392 * @chip: nand chip info structure
1393 * @buf: buffer to store read data
1394 * @oob_required: caller requires OOB data read to chip->oob_poi
1395 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001396 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001397 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001398 */
William Juulcfa460a2007-10-31 13:53:06 +01001399static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001400 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001401{
William Juulcfa460a2007-10-31 13:53:06 +01001402 int i, eccsize = chip->ecc.size;
1403 int eccbytes = chip->ecc.bytes;
1404 int eccsteps = chip->ecc.steps;
1405 uint8_t *p = buf;
1406 uint8_t *ecc_calc = chip->buffers->ecccalc;
1407 uint8_t *ecc_code = chip->buffers->ecccode;
1408 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001409 unsigned int max_bitflips = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001410
William Juulcfa460a2007-10-31 13:53:06 +01001411 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1412 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1413 chip->read_buf(mtd, p, eccsize);
1414 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1415 }
1416 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001417
William Juulcfa460a2007-10-31 13:53:06 +01001418 for (i = 0; i < chip->ecc.total; i++)
1419 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001420
William Juulcfa460a2007-10-31 13:53:06 +01001421 eccsteps = chip->ecc.steps;
1422 p = buf;
1423
1424 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1425 int stat;
1426
1427 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001428 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001429 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001430 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001431 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001432 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1433 }
William Juulcfa460a2007-10-31 13:53:06 +01001434 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001435 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001436}
1437
1438/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001439 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1440 * @mtd: mtd info structure
1441 * @chip: nand chip info structure
1442 * @buf: buffer to store read data
1443 * @oob_required: caller requires OOB data read to chip->oob_poi
1444 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001445 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001446 * Hardware ECC for large page chips, require OOB to be read first. For this
1447 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1448 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1449 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1450 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001451 */
1452static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001453 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001454{
1455 int i, eccsize = chip->ecc.size;
1456 int eccbytes = chip->ecc.bytes;
1457 int eccsteps = chip->ecc.steps;
1458 uint8_t *p = buf;
1459 uint8_t *ecc_code = chip->buffers->ecccode;
1460 uint32_t *eccpos = chip->ecc.layout->eccpos;
1461 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001462 unsigned int max_bitflips = 0;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001463
1464 /* Read the OOB area first */
1465 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1466 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1467 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1468
1469 for (i = 0; i < chip->ecc.total; i++)
1470 ecc_code[i] = chip->oob_poi[eccpos[i]];
1471
1472 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1473 int stat;
1474
1475 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1476 chip->read_buf(mtd, p, eccsize);
1477 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1478
1479 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001480 if (stat < 0) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001481 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001482 } else {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001483 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001484 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1485 }
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001486 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001487 return max_bitflips;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001488}
1489
1490/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001491 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1492 * @mtd: mtd info structure
1493 * @chip: nand chip info structure
1494 * @buf: buffer to store read data
1495 * @oob_required: caller requires OOB data read to chip->oob_poi
1496 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001497 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001498 * The hw generator calculates the error syndrome automatically. Therefore we
1499 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001500 */
1501static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001502 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001503{
1504 int i, eccsize = chip->ecc.size;
1505 int eccbytes = chip->ecc.bytes;
1506 int eccsteps = chip->ecc.steps;
1507 uint8_t *p = buf;
1508 uint8_t *oob = chip->oob_poi;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001509 unsigned int max_bitflips = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001510
1511 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1512 int stat;
1513
1514 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1515 chip->read_buf(mtd, p, eccsize);
1516
1517 if (chip->ecc.prepad) {
1518 chip->read_buf(mtd, oob, chip->ecc.prepad);
1519 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001520 }
1521
William Juulcfa460a2007-10-31 13:53:06 +01001522 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1523 chip->read_buf(mtd, oob, eccbytes);
1524 stat = chip->ecc.correct(mtd, p, oob, NULL);
1525
Heiko Schocherff94bc42014-06-24 10:10:04 +02001526 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001527 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001528 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001529 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001530 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1531 }
William Juulcfa460a2007-10-31 13:53:06 +01001532
1533 oob += eccbytes;
1534
1535 if (chip->ecc.postpad) {
1536 chip->read_buf(mtd, oob, chip->ecc.postpad);
1537 oob += chip->ecc.postpad;
1538 }
1539 }
1540
1541 /* Calculate remaining oob bytes */
1542 i = mtd->oobsize - (oob - chip->oob_poi);
1543 if (i)
1544 chip->read_buf(mtd, oob, i);
1545
Heiko Schocherff94bc42014-06-24 10:10:04 +02001546 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001547}
1548
1549/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001550 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1551 * @chip: nand chip structure
1552 * @oob: oob destination address
1553 * @ops: oob ops structure
1554 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001555 */
1556static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1557 struct mtd_oob_ops *ops, size_t len)
1558{
Christian Hitz90e3f392011-10-12 09:32:01 +02001559 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001560
Sergey Lapindfe64e22013-01-14 03:46:50 +00001561 case MTD_OPS_PLACE_OOB:
1562 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001563 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1564 return oob + len;
1565
Sergey Lapindfe64e22013-01-14 03:46:50 +00001566 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001567 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1568 uint32_t boffs = 0, roffs = ops->ooboffs;
1569 size_t bytes = 0;
1570
Christian Hitz90e3f392011-10-12 09:32:01 +02001571 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001572 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001573 if (unlikely(roffs)) {
1574 if (roffs >= free->length) {
1575 roffs -= free->length;
1576 continue;
1577 }
1578 boffs = free->offset + roffs;
1579 bytes = min_t(size_t, len,
1580 (free->length - roffs));
1581 roffs = 0;
1582 } else {
1583 bytes = min_t(size_t, len, free->length);
1584 boffs = free->offset;
1585 }
1586 memcpy(oob, chip->oob_poi + boffs, bytes);
1587 oob += bytes;
1588 }
1589 return oob;
1590 }
1591 default:
1592 BUG();
1593 }
1594 return NULL;
1595}
1596
1597/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001598 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1599 * @mtd: MTD device structure
1600 * @retry_mode: the retry mode to use
1601 *
1602 * Some vendors supply a special command to shift the Vt threshold, to be used
1603 * when there are too many bitflips in a page (i.e., ECC error). After setting
1604 * a new threshold, the host should retry reading the page.
1605 */
1606static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1607{
1608 struct nand_chip *chip = mtd->priv;
1609
1610 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1611
1612 if (retry_mode >= chip->read_retries)
1613 return -EINVAL;
1614
1615 if (!chip->setup_read_retry)
1616 return -EOPNOTSUPP;
1617
1618 return chip->setup_read_retry(mtd, retry_mode);
1619}
1620
1621/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001622 * nand_do_read_ops - [INTERN] Read data with ECC
1623 * @mtd: MTD device structure
1624 * @from: offset to read from
1625 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001626 *
1627 * Internal function. Called with chip held.
1628 */
1629static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1630 struct mtd_oob_ops *ops)
1631{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001632 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juulcfa460a2007-10-31 13:53:06 +01001633 struct nand_chip *chip = mtd->priv;
William Juulcfa460a2007-10-31 13:53:06 +01001634 int ret = 0;
1635 uint32_t readlen = ops->len;
1636 uint32_t oobreadlen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001637 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001638 mtd->oobavail : mtd->oobsize;
1639
William Juulcfa460a2007-10-31 13:53:06 +01001640 uint8_t *bufpoi, *oob, *buf;
Paul Burton40462e52013-09-04 15:16:56 +01001641 unsigned int max_bitflips = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001642 int retry_mode = 0;
1643 bool ecc_fail = false;
William Juulcfa460a2007-10-31 13:53:06 +01001644
1645 chipnr = (int)(from >> chip->chip_shift);
1646 chip->select_chip(mtd, chipnr);
1647
1648 realpage = (int)(from >> chip->page_shift);
1649 page = realpage & chip->pagemask;
1650
1651 col = (int)(from & (mtd->writesize - 1));
1652
1653 buf = ops->datbuf;
1654 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001655 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001656
Christian Hitz90e3f392011-10-12 09:32:01 +02001657 while (1) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001658 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Wood6f2ffc32011-02-02 18:15:57 -06001659
Heiko Schocherff94bc42014-06-24 10:10:04 +02001660 WATCHDOG_RESET();
William Juulcfa460a2007-10-31 13:53:06 +01001661 bytes = min(mtd->writesize - col, readlen);
1662 aligned = (bytes == mtd->writesize);
1663
Sergey Lapindfe64e22013-01-14 03:46:50 +00001664 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001665 if (realpage != chip->pagebuf || oob) {
1666 bufpoi = aligned ? buf : chip->buffers->databuf;
1667
Heiko Schocherff94bc42014-06-24 10:10:04 +02001668read_retry:
Sergey Lapindfe64e22013-01-14 03:46:50 +00001669 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001670
Paul Burton40462e52013-09-04 15:16:56 +01001671 /*
1672 * Now read the page into the buffer. Absent an error,
1673 * the read methods return max bitflips per ecc step.
1674 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001675 if (unlikely(ops->mode == MTD_OPS_RAW))
1676 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1677 oob_required,
1678 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001679 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001680 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001681 ret = chip->ecc.read_subpage(mtd, chip,
Heiko Schocher4e67c572014-07-15 16:08:43 +02001682 col, bytes, bufpoi,
1683 page);
William Juulcfa460a2007-10-31 13:53:06 +01001684 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001685 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001686 oob_required, page);
1687 if (ret < 0) {
1688 if (!aligned)
1689 /* Invalidate page cache */
1690 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001691 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001692 }
William Juulcfa460a2007-10-31 13:53:06 +01001693
Paul Burton40462e52013-09-04 15:16:56 +01001694 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1695
William Juulcfa460a2007-10-31 13:53:06 +01001696 /* Transfer not aligned data */
1697 if (!aligned) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001698 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001699 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton40462e52013-09-04 15:16:56 +01001700 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001701 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001702 chip->pagebuf_bitflips = ret;
1703 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001704 /* Invalidate page cache */
1705 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001706 }
William Juulcfa460a2007-10-31 13:53:06 +01001707 memcpy(buf, chip->buffers->databuf + col, bytes);
1708 }
1709
William Juulcfa460a2007-10-31 13:53:06 +01001710 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001711 int toread = min(oobreadlen, max_oobsize);
1712
1713 if (toread) {
1714 oob = nand_transfer_oob(chip,
1715 oob, ops, toread);
1716 oobreadlen -= toread;
1717 }
William Juulcfa460a2007-10-31 13:53:06 +01001718 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001719
1720 if (chip->options & NAND_NEED_READRDY) {
1721 /* Apply delay or wait for ready/busy pin */
1722 if (!chip->dev_ready)
1723 udelay(chip->chip_delay);
1724 else
1725 nand_wait_ready(mtd);
1726 }
1727
1728 if (mtd->ecc_stats.failed - ecc_failures) {
1729 if (retry_mode + 1 < chip->read_retries) {
1730 retry_mode++;
1731 ret = nand_setup_read_retry(mtd,
1732 retry_mode);
1733 if (ret < 0)
1734 break;
1735
1736 /* Reset failures; retry */
1737 mtd->ecc_stats.failed = ecc_failures;
1738 goto read_retry;
1739 } else {
1740 /* No more retry modes; real failure */
1741 ecc_fail = true;
1742 }
1743 }
1744
1745 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001746 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001747 memcpy(buf, chip->buffers->databuf + col, bytes);
1748 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001749 max_bitflips = max_t(unsigned int, max_bitflips,
1750 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001751 }
1752
William Juulcfa460a2007-10-31 13:53:06 +01001753 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001754
Heiko Schocherff94bc42014-06-24 10:10:04 +02001755 /* Reset to retry mode 0 */
1756 if (retry_mode) {
1757 ret = nand_setup_read_retry(mtd, 0);
1758 if (ret < 0)
1759 break;
1760 retry_mode = 0;
1761 }
1762
William Juulcfa460a2007-10-31 13:53:06 +01001763 if (!readlen)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001764 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001765
Sergey Lapindfe64e22013-01-14 03:46:50 +00001766 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001767 col = 0;
1768 /* Increment page address */
1769 realpage++;
1770
William Juulcfa460a2007-10-31 13:53:06 +01001771 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001772 /* Check, if we cross a chip boundary */
1773 if (!page) {
1774 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001775 chip->select_chip(mtd, -1);
1776 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001777 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001778 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001779 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001780
William Juulcfa460a2007-10-31 13:53:06 +01001781 ops->retlen = ops->len - (size_t) readlen;
1782 if (oob)
1783 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001784
Heiko Schocherff94bc42014-06-24 10:10:04 +02001785 if (ret < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001786 return ret;
1787
Heiko Schocherff94bc42014-06-24 10:10:04 +02001788 if (ecc_fail)
William Juulcfa460a2007-10-31 13:53:06 +01001789 return -EBADMSG;
1790
Paul Burton40462e52013-09-04 15:16:56 +01001791 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001792}
1793
1794/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001795 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001796 * @mtd: MTD device structure
1797 * @from: offset to read from
1798 * @len: number of bytes to read
1799 * @retlen: pointer to variable to store the number of read bytes
1800 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001801 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001802 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001803 */
William Juulcfa460a2007-10-31 13:53:06 +01001804static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1805 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001806{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001807 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001808 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001809
Heiko Schocherff94bc42014-06-24 10:10:04 +02001810 nand_get_device(mtd, FL_READING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001811 ops.len = len;
1812 ops.datbuf = buf;
1813 ops.oobbuf = NULL;
1814 ops.mode = MTD_OPS_PLACE_OOB;
1815 ret = nand_do_read_ops(mtd, from, &ops);
1816 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001817 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001818 return ret;
1819}
1820
William Juulcfa460a2007-10-31 13:53:06 +01001821/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001822 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1823 * @mtd: mtd info structure
1824 * @chip: nand chip info structure
1825 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001826 */
1827static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001828 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001829{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001830 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001831 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001832 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001833}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001834
1835/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001836 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001837 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001838 * @mtd: mtd info structure
1839 * @chip: nand chip info structure
1840 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001841 */
1842static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001843 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001844{
1845 uint8_t *buf = chip->oob_poi;
1846 int length = mtd->oobsize;
1847 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1848 int eccsize = chip->ecc.size;
1849 uint8_t *bufpoi = buf;
1850 int i, toread, sndrnd = 0, pos;
1851
1852 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1853 for (i = 0; i < chip->ecc.steps; i++) {
1854 if (sndrnd) {
1855 pos = eccsize + i * (eccsize + chunk);
1856 if (mtd->writesize > 512)
1857 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1858 else
1859 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1860 } else
1861 sndrnd = 1;
1862 toread = min_t(int, length, chunk);
1863 chip->read_buf(mtd, bufpoi, toread);
1864 bufpoi += toread;
1865 length -= toread;
1866 }
1867 if (length > 0)
1868 chip->read_buf(mtd, bufpoi, length);
1869
Sergey Lapindfe64e22013-01-14 03:46:50 +00001870 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001871}
1872
1873/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001874 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1875 * @mtd: mtd info structure
1876 * @chip: nand chip info structure
1877 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001878 */
1879static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1880 int page)
1881{
1882 int status = 0;
1883 const uint8_t *buf = chip->oob_poi;
1884 int length = mtd->oobsize;
1885
1886 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1887 chip->write_buf(mtd, buf, length);
1888 /* Send command to program the OOB data */
1889 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1890
1891 status = chip->waitfunc(mtd, chip);
1892
1893 return status & NAND_STATUS_FAIL ? -EIO : 0;
1894}
1895
1896/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001897 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1898 * with syndrome - only for large page flash
1899 * @mtd: mtd info structure
1900 * @chip: nand chip info structure
1901 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001902 */
1903static int nand_write_oob_syndrome(struct mtd_info *mtd,
1904 struct nand_chip *chip, int page)
1905{
1906 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1907 int eccsize = chip->ecc.size, length = mtd->oobsize;
1908 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1909 const uint8_t *bufpoi = chip->oob_poi;
1910
1911 /*
1912 * data-ecc-data-ecc ... ecc-oob
1913 * or
1914 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1915 */
1916 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1917 pos = steps * (eccsize + chunk);
1918 steps = 0;
1919 } else
1920 pos = eccsize;
1921
1922 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1923 for (i = 0; i < steps; i++) {
1924 if (sndcmd) {
1925 if (mtd->writesize <= 512) {
1926 uint32_t fill = 0xFFFFFFFF;
1927
1928 len = eccsize;
1929 while (len > 0) {
1930 int num = min_t(int, len, 4);
1931 chip->write_buf(mtd, (uint8_t *)&fill,
1932 num);
1933 len -= num;
1934 }
1935 } else {
1936 pos = eccsize + i * (eccsize + chunk);
1937 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1938 }
1939 } else
1940 sndcmd = 1;
1941 len = min_t(int, length, chunk);
1942 chip->write_buf(mtd, bufpoi, len);
1943 bufpoi += len;
1944 length -= len;
1945 }
1946 if (length > 0)
1947 chip->write_buf(mtd, bufpoi, length);
1948
1949 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1950 status = chip->waitfunc(mtd, chip);
1951
1952 return status & NAND_STATUS_FAIL ? -EIO : 0;
1953}
1954
1955/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001956 * nand_do_read_oob - [INTERN] NAND read out-of-band
1957 * @mtd: MTD device structure
1958 * @from: offset to read from
1959 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01001960 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001961 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01001962 */
1963static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1964 struct mtd_oob_ops *ops)
1965{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001966 int page, realpage, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01001967 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001968 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01001969 int readlen = ops->ooblen;
1970 int len;
1971 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001972 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001973
Heiko Schocherff94bc42014-06-24 10:10:04 +02001974 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02001975 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01001976
Sergey Lapindfe64e22013-01-14 03:46:50 +00001977 stats = mtd->ecc_stats;
1978
1979 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01001980 len = chip->ecc.layout->oobavail;
1981 else
1982 len = mtd->oobsize;
1983
1984 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001985 pr_debug("%s: attempt to start read outside oob\n",
1986 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001987 return -EINVAL;
1988 }
1989
1990 /* Do not allow reads past end of device */
1991 if (unlikely(from >= mtd->size ||
1992 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1993 (from >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001994 pr_debug("%s: attempt to read beyond end of device\n",
1995 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01001996 return -EINVAL;
1997 }
1998
1999 chipnr = (int)(from >> chip->chip_shift);
2000 chip->select_chip(mtd, chipnr);
2001
2002 /* Shift to get page */
2003 realpage = (int)(from >> chip->page_shift);
2004 page = realpage & chip->pagemask;
2005
Christian Hitz90e3f392011-10-12 09:32:01 +02002006 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002007 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002008
Sergey Lapindfe64e22013-01-14 03:46:50 +00002009 if (ops->mode == MTD_OPS_RAW)
2010 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2011 else
2012 ret = chip->ecc.read_oob(mtd, chip, page);
2013
2014 if (ret < 0)
2015 break;
William Juulcfa460a2007-10-31 13:53:06 +01002016
2017 len = min(len, readlen);
2018 buf = nand_transfer_oob(chip, buf, ops, len);
2019
Heiko Schocherff94bc42014-06-24 10:10:04 +02002020 if (chip->options & NAND_NEED_READRDY) {
2021 /* Apply delay or wait for ready/busy pin */
2022 if (!chip->dev_ready)
2023 udelay(chip->chip_delay);
2024 else
2025 nand_wait_ready(mtd);
2026 }
2027
William Juulcfa460a2007-10-31 13:53:06 +01002028 readlen -= len;
2029 if (!readlen)
2030 break;
2031
2032 /* Increment page address */
2033 realpage++;
2034
2035 page = realpage & chip->pagemask;
2036 /* Check, if we cross a chip boundary */
2037 if (!page) {
2038 chipnr++;
2039 chip->select_chip(mtd, -1);
2040 chip->select_chip(mtd, chipnr);
2041 }
William Juulcfa460a2007-10-31 13:53:06 +01002042 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002043 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002044
Sergey Lapindfe64e22013-01-14 03:46:50 +00002045 ops->oobretlen = ops->ooblen - readlen;
2046
2047 if (ret < 0)
2048 return ret;
2049
2050 if (mtd->ecc_stats.failed - stats.failed)
2051 return -EBADMSG;
2052
2053 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002054}
2055
2056/**
2057 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002058 * @mtd: MTD device structure
2059 * @from: offset to read from
2060 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002061 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002062 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01002063 */
2064static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2065 struct mtd_oob_ops *ops)
2066{
William Juulcfa460a2007-10-31 13:53:06 +01002067 int ret = -ENOTSUPP;
2068
2069 ops->retlen = 0;
2070
2071 /* Do not allow reads past end of device */
2072 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002073 pr_debug("%s: attempt to read beyond end of device\n",
2074 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002075 return -EINVAL;
2076 }
2077
Heiko Schocherff94bc42014-06-24 10:10:04 +02002078 nand_get_device(mtd, FL_READING);
William Juulcfa460a2007-10-31 13:53:06 +01002079
Christian Hitz90e3f392011-10-12 09:32:01 +02002080 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002081 case MTD_OPS_PLACE_OOB:
2082 case MTD_OPS_AUTO_OOB:
2083 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002084 break;
2085
2086 default:
2087 goto out;
2088 }
2089
2090 if (!ops->datbuf)
2091 ret = nand_do_read_oob(mtd, from, ops);
2092 else
2093 ret = nand_do_read_ops(mtd, from, ops);
2094
Christian Hitz90e3f392011-10-12 09:32:01 +02002095out:
William Juulcfa460a2007-10-31 13:53:06 +01002096 nand_release_device(mtd);
2097 return ret;
2098}
2099
2100
2101/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002102 * nand_write_page_raw - [INTERN] raw page write function
2103 * @mtd: mtd info structure
2104 * @chip: nand chip info structure
2105 * @buf: data buffer
2106 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05002107 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002108 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01002109 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002110static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2111 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002112{
2113 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002114 if (oob_required)
2115 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2116
2117 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002118}
2119
2120/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002121 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2122 * @mtd: mtd info structure
2123 * @chip: nand chip info structure
2124 * @buf: data buffer
2125 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05002126 *
2127 * We need a special oob layout and handling even when ECC isn't checked.
2128 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002129static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02002130 struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00002131 const uint8_t *buf, int oob_required)
David Brownell7e866612009-11-07 16:27:01 -05002132{
2133 int eccsize = chip->ecc.size;
2134 int eccbytes = chip->ecc.bytes;
2135 uint8_t *oob = chip->oob_poi;
2136 int steps, size;
2137
2138 for (steps = chip->ecc.steps; steps > 0; steps--) {
2139 chip->write_buf(mtd, buf, eccsize);
2140 buf += eccsize;
2141
2142 if (chip->ecc.prepad) {
2143 chip->write_buf(mtd, oob, chip->ecc.prepad);
2144 oob += chip->ecc.prepad;
2145 }
2146
Heiko Schocher4e67c572014-07-15 16:08:43 +02002147 chip->write_buf(mtd, oob, eccbytes);
David Brownell7e866612009-11-07 16:27:01 -05002148 oob += eccbytes;
2149
2150 if (chip->ecc.postpad) {
2151 chip->write_buf(mtd, oob, chip->ecc.postpad);
2152 oob += chip->ecc.postpad;
2153 }
2154 }
2155
2156 size = mtd->oobsize - (oob - chip->oob_poi);
2157 if (size)
2158 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002159
2160 return 0;
David Brownell7e866612009-11-07 16:27:01 -05002161}
2162/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002163 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2164 * @mtd: mtd info structure
2165 * @chip: nand chip info structure
2166 * @buf: data buffer
2167 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002168 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002169static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2170 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002171{
2172 int i, eccsize = chip->ecc.size;
2173 int eccbytes = chip->ecc.bytes;
2174 int eccsteps = chip->ecc.steps;
2175 uint8_t *ecc_calc = chip->buffers->ecccalc;
2176 const uint8_t *p = buf;
2177 uint32_t *eccpos = chip->ecc.layout->eccpos;
2178
Sergey Lapindfe64e22013-01-14 03:46:50 +00002179 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01002180 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2181 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2182
2183 for (i = 0; i < chip->ecc.total; i++)
2184 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2185
Sergey Lapindfe64e22013-01-14 03:46:50 +00002186 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
William Juulcfa460a2007-10-31 13:53:06 +01002187}
2188
2189/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002190 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2191 * @mtd: mtd info structure
2192 * @chip: nand chip info structure
2193 * @buf: data buffer
2194 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002195 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002196static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2197 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002198{
2199 int i, eccsize = chip->ecc.size;
2200 int eccbytes = chip->ecc.bytes;
2201 int eccsteps = chip->ecc.steps;
2202 uint8_t *ecc_calc = chip->buffers->ecccalc;
2203 const uint8_t *p = buf;
2204 uint32_t *eccpos = chip->ecc.layout->eccpos;
2205
2206 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2207 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2208 chip->write_buf(mtd, p, eccsize);
2209 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2210 }
2211
2212 for (i = 0; i < chip->ecc.total; i++)
2213 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2214
2215 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002216
2217 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002218}
2219
Heiko Schocherff94bc42014-06-24 10:10:04 +02002220
2221/**
2222 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2223 * @mtd: mtd info structure
2224 * @chip: nand chip info structure
2225 * @offset: column address of subpage within the page
2226 * @data_len: data length
2227 * @buf: data buffer
2228 * @oob_required: must write chip->oob_poi to OOB
2229 */
2230static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2231 struct nand_chip *chip, uint32_t offset,
2232 uint32_t data_len, const uint8_t *buf,
2233 int oob_required)
2234{
2235 uint8_t *oob_buf = chip->oob_poi;
2236 uint8_t *ecc_calc = chip->buffers->ecccalc;
2237 int ecc_size = chip->ecc.size;
2238 int ecc_bytes = chip->ecc.bytes;
2239 int ecc_steps = chip->ecc.steps;
2240 uint32_t *eccpos = chip->ecc.layout->eccpos;
2241 uint32_t start_step = offset / ecc_size;
2242 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2243 int oob_bytes = mtd->oobsize / ecc_steps;
2244 int step, i;
2245
2246 for (step = 0; step < ecc_steps; step++) {
2247 /* configure controller for WRITE access */
2248 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2249
2250 /* write data (untouched subpages already masked by 0xFF) */
2251 chip->write_buf(mtd, buf, ecc_size);
2252
2253 /* mask ECC of un-touched subpages by padding 0xFF */
2254 if ((step < start_step) || (step > end_step))
2255 memset(ecc_calc, 0xff, ecc_bytes);
2256 else
2257 chip->ecc.calculate(mtd, buf, ecc_calc);
2258
2259 /* mask OOB of un-touched subpages by padding 0xFF */
2260 /* if oob_required, preserve OOB metadata of written subpage */
2261 if (!oob_required || (step < start_step) || (step > end_step))
2262 memset(oob_buf, 0xff, oob_bytes);
2263
2264 buf += ecc_size;
2265 ecc_calc += ecc_bytes;
2266 oob_buf += oob_bytes;
2267 }
2268
2269 /* copy calculated ECC for whole page to chip->buffer->oob */
2270 /* this include masked-value(0xFF) for unwritten subpages */
2271 ecc_calc = chip->buffers->ecccalc;
2272 for (i = 0; i < chip->ecc.total; i++)
2273 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2274
2275 /* write OOB buffer to NAND device */
2276 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2277
2278 return 0;
2279}
2280
2281
William Juulcfa460a2007-10-31 13:53:06 +01002282/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002283 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2284 * @mtd: mtd info structure
2285 * @chip: nand chip info structure
2286 * @buf: data buffer
2287 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002288 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002289 * The hw generator calculates the error syndrome automatically. Therefore we
2290 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01002291 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002292static int nand_write_page_syndrome(struct mtd_info *mtd,
2293 struct nand_chip *chip,
2294 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002295{
2296 int i, eccsize = chip->ecc.size;
2297 int eccbytes = chip->ecc.bytes;
2298 int eccsteps = chip->ecc.steps;
2299 const uint8_t *p = buf;
2300 uint8_t *oob = chip->oob_poi;
2301
2302 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2303
2304 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2305 chip->write_buf(mtd, p, eccsize);
2306
2307 if (chip->ecc.prepad) {
2308 chip->write_buf(mtd, oob, chip->ecc.prepad);
2309 oob += chip->ecc.prepad;
2310 }
2311
2312 chip->ecc.calculate(mtd, p, oob);
2313 chip->write_buf(mtd, oob, eccbytes);
2314 oob += eccbytes;
2315
2316 if (chip->ecc.postpad) {
2317 chip->write_buf(mtd, oob, chip->ecc.postpad);
2318 oob += chip->ecc.postpad;
2319 }
2320 }
2321
2322 /* Calculate remaining oob bytes */
2323 i = mtd->oobsize - (oob - chip->oob_poi);
2324 if (i)
2325 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002326
2327 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002328}
2329
2330/**
2331 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00002332 * @mtd: MTD device structure
2333 * @chip: NAND chip descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +02002334 * @offset: address offset within the page
2335 * @data_len: length of actual data to be written
Sergey Lapindfe64e22013-01-14 03:46:50 +00002336 * @buf: the data to write
2337 * @oob_required: must write chip->oob_poi to OOB
2338 * @page: page number to write
2339 * @cached: cached programming
2340 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01002341 */
2342static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02002343 uint32_t offset, int data_len, const uint8_t *buf,
2344 int oob_required, int page, int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01002345{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002346 int status, subpage;
2347
2348 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2349 chip->ecc.write_subpage)
2350 subpage = offset || (data_len < mtd->writesize);
2351 else
2352 subpage = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002353
2354 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2355
2356 if (unlikely(raw))
Heiko Schocherff94bc42014-06-24 10:10:04 +02002357 status = chip->ecc.write_page_raw(mtd, chip, buf,
2358 oob_required);
2359 else if (subpage)
2360 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2361 buf, oob_required);
William Juulcfa460a2007-10-31 13:53:06 +01002362 else
Sergey Lapindfe64e22013-01-14 03:46:50 +00002363 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2364
2365 if (status < 0)
2366 return status;
William Juulcfa460a2007-10-31 13:53:06 +01002367
2368 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002369 * Cached progamming disabled for now. Not sure if it's worth the
2370 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01002371 */
2372 cached = 0;
2373
Heiko Schocherff94bc42014-06-24 10:10:04 +02002374 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juulcfa460a2007-10-31 13:53:06 +01002375
2376 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2377 status = chip->waitfunc(mtd, chip);
2378 /*
2379 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00002380 * available.
William Juulcfa460a2007-10-31 13:53:06 +01002381 */
2382 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2383 status = chip->errstat(mtd, chip, FL_WRITING, status,
2384 page);
2385
2386 if (status & NAND_STATUS_FAIL)
2387 return -EIO;
2388 } else {
2389 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2390 status = chip->waitfunc(mtd, chip);
2391 }
2392
William Juulcfa460a2007-10-31 13:53:06 +01002393 return 0;
2394}
2395
2396/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002397 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2398 * @mtd: MTD device structure
2399 * @oob: oob data buffer
2400 * @len: oob data write length
2401 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01002402 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002403static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2404 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01002405{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002406 struct nand_chip *chip = mtd->priv;
2407
2408 /*
2409 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2410 * data from a previous OOB read.
2411 */
2412 memset(chip->oob_poi, 0xff, mtd->oobsize);
2413
Christian Hitz90e3f392011-10-12 09:32:01 +02002414 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01002415
Sergey Lapindfe64e22013-01-14 03:46:50 +00002416 case MTD_OPS_PLACE_OOB:
2417 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002418 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2419 return oob + len;
2420
Sergey Lapindfe64e22013-01-14 03:46:50 +00002421 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01002422 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2423 uint32_t boffs = 0, woffs = ops->ooboffs;
2424 size_t bytes = 0;
2425
Christian Hitz90e3f392011-10-12 09:32:01 +02002426 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002427 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01002428 if (unlikely(woffs)) {
2429 if (woffs >= free->length) {
2430 woffs -= free->length;
2431 continue;
2432 }
2433 boffs = free->offset + woffs;
2434 bytes = min_t(size_t, len,
2435 (free->length - woffs));
2436 woffs = 0;
2437 } else {
2438 bytes = min_t(size_t, len, free->length);
2439 boffs = free->offset;
2440 }
2441 memcpy(chip->oob_poi + boffs, oob, bytes);
2442 oob += bytes;
2443 }
2444 return oob;
2445 }
2446 default:
2447 BUG();
2448 }
2449 return NULL;
2450}
2451
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002452#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01002453
2454/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002455 * nand_do_write_ops - [INTERN] NAND write with ECC
2456 * @mtd: MTD device structure
2457 * @to: offset to write to
2458 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002459 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002460 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002461 */
2462static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2463 struct mtd_oob_ops *ops)
2464{
2465 int chipnr, realpage, page, blockmask, column;
2466 struct nand_chip *chip = mtd->priv;
2467 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002468
2469 uint32_t oobwritelen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002470 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002471 mtd->oobavail : mtd->oobsize;
2472
William Juulcfa460a2007-10-31 13:53:06 +01002473 uint8_t *oob = ops->oobbuf;
2474 uint8_t *buf = ops->datbuf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002475 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002476 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002477
2478 ops->retlen = 0;
2479 if (!writelen)
2480 return 0;
2481
Heiko Schocherff94bc42014-06-24 10:10:04 +02002482#ifndef __UBOOT__
2483 /* Reject writes, which are not page aligned */
2484 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2485#else
2486 /* Reject writes, which are not page aligned */
2487 if (NOTALIGNED(to)) {
2488#endif
2489 pr_notice("%s: attempt to write non page aligned data\n",
2490 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002491 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002492 }
2493
2494 column = to & (mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +01002495
2496 chipnr = (int)(to >> chip->chip_shift);
2497 chip->select_chip(mtd, chipnr);
2498
2499 /* Check, if it is write protected */
2500 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002501 ret = -EIO;
2502 goto err_out;
William Juulcfa460a2007-10-31 13:53:06 +01002503 }
2504
2505 realpage = (int)(to >> chip->page_shift);
2506 page = realpage & chip->pagemask;
2507 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2508
2509 /* Invalidate the page cache, when we write to the cached page */
2510 if (to <= (chip->pagebuf << chip->page_shift) &&
2511 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2512 chip->pagebuf = -1;
2513
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002514 /* Don't allow multipage oob writes with offset */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002515 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2516 ret = -EINVAL;
2517 goto err_out;
2518 }
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002519
Christian Hitz90e3f392011-10-12 09:32:01 +02002520 while (1) {
William Juulcfa460a2007-10-31 13:53:06 +01002521 int bytes = mtd->writesize;
2522 int cached = writelen > bytes && page != blockmask;
2523 uint8_t *wbuf = buf;
2524
Heiko Schocherff94bc42014-06-24 10:10:04 +02002525 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00002526 /* Partial page write? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002527 if (unlikely(column || writelen < (mtd->writesize - 1))) {
William Juulcfa460a2007-10-31 13:53:06 +01002528 cached = 0;
2529 bytes = min_t(int, bytes - column, (int) writelen);
2530 chip->pagebuf = -1;
2531 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2532 memcpy(&chip->buffers->databuf[column], buf, bytes);
2533 wbuf = chip->buffers->databuf;
2534 }
2535
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002536 if (unlikely(oob)) {
2537 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002538 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002539 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002540 } else {
2541 /* We still need to erase leftover OOB data */
2542 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002543 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002544 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2545 oob_required, page, cached,
2546 (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002547 if (ret)
2548 break;
2549
2550 writelen -= bytes;
2551 if (!writelen)
2552 break;
2553
2554 column = 0;
2555 buf += bytes;
2556 realpage++;
2557
2558 page = realpage & chip->pagemask;
2559 /* Check, if we cross a chip boundary */
2560 if (!page) {
2561 chipnr++;
2562 chip->select_chip(mtd, -1);
2563 chip->select_chip(mtd, chipnr);
2564 }
2565 }
2566
2567 ops->retlen = ops->len - writelen;
2568 if (unlikely(oob))
2569 ops->oobretlen = ops->ooblen;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002570
2571err_out:
2572 chip->select_chip(mtd, -1);
2573 return ret;
2574}
2575
2576/**
2577 * panic_nand_write - [MTD Interface] NAND write with ECC
2578 * @mtd: MTD device structure
2579 * @to: offset to write to
2580 * @len: number of bytes to write
2581 * @retlen: pointer to variable to store the number of written bytes
2582 * @buf: the data to write
2583 *
2584 * NAND write with ECC. Used when performing writes in interrupt context, this
2585 * may for example be called by mtdoops when writing an oops while in panic.
2586 */
2587static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2588 size_t *retlen, const uint8_t *buf)
2589{
2590 struct nand_chip *chip = mtd->priv;
2591 struct mtd_oob_ops ops;
2592 int ret;
2593
2594 /* Wait for the device to get ready */
2595 panic_nand_wait(mtd, chip, 400);
2596
2597 /* Grab the device */
2598 panic_nand_get_device(chip, mtd, FL_WRITING);
2599
2600 ops.len = len;
2601 ops.datbuf = (uint8_t *)buf;
2602 ops.oobbuf = NULL;
2603 ops.mode = MTD_OPS_PLACE_OOB;
2604
2605 ret = nand_do_write_ops(mtd, to, &ops);
2606
2607 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002608 return ret;
2609}
2610
2611/**
2612 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002613 * @mtd: MTD device structure
2614 * @to: offset to write to
2615 * @len: number of bytes to write
2616 * @retlen: pointer to variable to store the number of written bytes
2617 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002618 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002619 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002620 */
2621static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2622 size_t *retlen, const uint8_t *buf)
2623{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002624 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002625 int ret;
2626
Heiko Schocherff94bc42014-06-24 10:10:04 +02002627 nand_get_device(mtd, FL_WRITING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002628 ops.len = len;
2629 ops.datbuf = (uint8_t *)buf;
2630 ops.oobbuf = NULL;
2631 ops.mode = MTD_OPS_PLACE_OOB;
2632 ret = nand_do_write_ops(mtd, to, &ops);
2633 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002634 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002635 return ret;
2636}
2637
2638/**
2639 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002640 * @mtd: MTD device structure
2641 * @to: offset to write to
2642 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002643 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002644 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002645 */
William Juulcfa460a2007-10-31 13:53:06 +01002646static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2647 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002648{
William Juulcfa460a2007-10-31 13:53:06 +01002649 int chipnr, page, status, len;
2650 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002651
Heiko Schocherff94bc42014-06-24 10:10:04 +02002652 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002653 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002654
Sergey Lapindfe64e22013-01-14 03:46:50 +00002655 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002656 len = chip->ecc.layout->oobavail;
2657 else
2658 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002659
2660 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002661 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002662 pr_debug("%s: attempt to write past end of page\n",
2663 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002664 return -EINVAL;
2665 }
2666
William Juulcfa460a2007-10-31 13:53:06 +01002667 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002668 pr_debug("%s: attempt to start write outside oob\n",
2669 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002670 return -EINVAL;
2671 }
2672
Christian Hitz90e3f392011-10-12 09:32:01 +02002673 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002674 if (unlikely(to >= mtd->size ||
2675 ops->ooboffs + ops->ooblen >
2676 ((mtd->size >> chip->page_shift) -
2677 (to >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002678 pr_debug("%s: attempt to write beyond end of device\n",
2679 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002680 return -EINVAL;
2681 }
2682
William Juulcfa460a2007-10-31 13:53:06 +01002683 chipnr = (int)(to >> chip->chip_shift);
2684 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002685
William Juulcfa460a2007-10-31 13:53:06 +01002686 /* Shift to get page */
2687 page = (int)(to >> chip->page_shift);
2688
2689 /*
2690 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2691 * of my DiskOnChip 2000 test units) will clear the whole data page too
2692 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2693 * it in the doc2000 driver in August 1999. dwmw2.
2694 */
2695 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002696
2697 /* Check, if it is write protected */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002698 if (nand_check_wp(mtd)) {
2699 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002700 return -EROFS;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002701 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002702
Wolfgang Denk932394a2005-08-17 12:55:25 +02002703 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002704 if (page == chip->pagebuf)
2705 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002706
Sergey Lapindfe64e22013-01-14 03:46:50 +00002707 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2708
2709 if (ops->mode == MTD_OPS_RAW)
2710 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2711 else
2712 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002713
Heiko Schocherff94bc42014-06-24 10:10:04 +02002714 chip->select_chip(mtd, -1);
2715
William Juulcfa460a2007-10-31 13:53:06 +01002716 if (status)
2717 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002718
William Juulcfa460a2007-10-31 13:53:06 +01002719 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002720
William Juulcfa460a2007-10-31 13:53:06 +01002721 return 0;
2722}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002723
William Juulcfa460a2007-10-31 13:53:06 +01002724/**
2725 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002726 * @mtd: MTD device structure
2727 * @to: offset to write to
2728 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002729 */
2730static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2731 struct mtd_oob_ops *ops)
2732{
William Juulcfa460a2007-10-31 13:53:06 +01002733 int ret = -ENOTSUPP;
2734
2735 ops->retlen = 0;
2736
2737 /* Do not allow writes past end of device */
2738 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002739 pr_debug("%s: attempt to write beyond end of device\n",
2740 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002741 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002742 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002743
Heiko Schocherff94bc42014-06-24 10:10:04 +02002744 nand_get_device(mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +01002745
Christian Hitz90e3f392011-10-12 09:32:01 +02002746 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002747 case MTD_OPS_PLACE_OOB:
2748 case MTD_OPS_AUTO_OOB:
2749 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002750 break;
2751
2752 default:
2753 goto out;
2754 }
2755
2756 if (!ops->datbuf)
2757 ret = nand_do_write_oob(mtd, to, ops);
2758 else
2759 ret = nand_do_write_ops(mtd, to, ops);
2760
Christian Hitz90e3f392011-10-12 09:32:01 +02002761out:
William Juulcfa460a2007-10-31 13:53:06 +01002762 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002763 return ret;
2764}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002765
2766/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002767 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2768 * @mtd: MTD device structure
2769 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002770 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002771 * Standard erase command for NAND chips.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002772 */
William Juulcfa460a2007-10-31 13:53:06 +01002773static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002774{
William Juulcfa460a2007-10-31 13:53:06 +01002775 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002776 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002777 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2778 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002779}
2780
2781/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002782 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002783 * @mtd: MTD device structure
2784 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002785 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002786 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002787 */
William Juulcfa460a2007-10-31 13:53:06 +01002788static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002789{
William Juulcfa460a2007-10-31 13:53:06 +01002790 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002791}
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002792
Wolfgang Denk932394a2005-08-17 12:55:25 +02002793/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002794 * nand_erase_nand - [INTERN] erase block(s)
2795 * @mtd: MTD device structure
2796 * @instr: erase instruction
2797 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002798 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002799 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002800 */
William Juulcfa460a2007-10-31 13:53:06 +01002801int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2802 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002803{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002804 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002805 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002806 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002807
Heiko Schocherff94bc42014-06-24 10:10:04 +02002808 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2809 __func__, (unsigned long long)instr->addr,
2810 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002811
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002812 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002813 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002814
Wolfgang Denk932394a2005-08-17 12:55:25 +02002815 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002816 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002817
2818 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002819 page = (int)(instr->addr >> chip->page_shift);
2820 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002821
2822 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002823 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002824
Wolfgang Denk932394a2005-08-17 12:55:25 +02002825 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002826 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002827
Wolfgang Denk932394a2005-08-17 12:55:25 +02002828 /* Check, if it is write protected */
2829 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002830 pr_debug("%s: device is write protected!\n",
2831 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002832 instr->state = MTD_ERASE_FAILED;
2833 goto erase_exit;
2834 }
2835
2836 /* Loop through the pages */
2837 len = instr->len;
2838
2839 instr->state = MTD_ERASING;
2840
2841 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002842 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002843
Sergey Lapindfe64e22013-01-14 03:46:50 +00002844 /* Check if we have a bad block, we do not erase bad blocks! */
Masahiro Yamada756963d2014-12-16 15:36:33 +09002845 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002846 chip->page_shift, 0, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002847 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02002848 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002849 instr->state = MTD_ERASE_FAILED;
2850 goto erase_exit;
2851 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002852
William Juulcfa460a2007-10-31 13:53:06 +01002853 /*
2854 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002855 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002856 */
2857 if (page <= chip->pagebuf && chip->pagebuf <
2858 (page + pages_per_block))
2859 chip->pagebuf = -1;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002860
William Juulcfa460a2007-10-31 13:53:06 +01002861 chip->erase_cmd(mtd, page & chip->pagemask);
2862
2863 status = chip->waitfunc(mtd, chip);
2864
2865 /*
2866 * See if operation failed and additional status checks are
2867 * available
2868 */
2869 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2870 status = chip->errstat(mtd, chip, FL_ERASING,
2871 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002872
2873 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002874 if (status & NAND_STATUS_FAIL) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002875 pr_debug("%s: failed erase, page 0x%08x\n",
2876 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002877 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002878 instr->fail_addr =
2879 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002880 goto erase_exit;
2881 }
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002882
Wolfgang Denk932394a2005-08-17 12:55:25 +02002883 /* Increment page address and decrement length */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002884 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002885 page += pages_per_block;
2886
2887 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002888 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002889 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002890 chip->select_chip(mtd, -1);
2891 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002892 }
2893 }
2894 instr->state = MTD_ERASE_DONE;
2895
Christian Hitz90e3f392011-10-12 09:32:01 +02002896erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002897
2898 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002899
2900 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002901 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002902 nand_release_device(mtd);
2903
Scott Woodc45912d2008-10-24 16:20:43 -05002904 /* Do call back function */
2905 if (!ret)
2906 mtd_erase_callback(instr);
2907
Wolfgang Denk932394a2005-08-17 12:55:25 +02002908 /* Return more or less happy */
2909 return ret;
2910}
2911
2912/**
2913 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002914 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002915 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002916 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002917 */
William Juulcfa460a2007-10-31 13:53:06 +01002918static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002919{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002920 pr_debug("%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002921
2922 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002923 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002924 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002925 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002926}
2927
Wolfgang Denk932394a2005-08-17 12:55:25 +02002928/**
William Juulcfa460a2007-10-31 13:53:06 +01002929 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002930 * @mtd: MTD device structure
2931 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002932 */
William Juulcfa460a2007-10-31 13:53:06 +01002933static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002934{
William Juulcfa460a2007-10-31 13:53:06 +01002935 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002936}
2937
2938/**
William Juulcfa460a2007-10-31 13:53:06 +01002939 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002940 * @mtd: MTD device structure
2941 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002942 */
William Juulcfa460a2007-10-31 13:53:06 +01002943static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002944{
Wolfgang Denk932394a2005-08-17 12:55:25 +02002945 int ret;
2946
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002947 ret = nand_block_isbad(mtd, ofs);
2948 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002949 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02002950 if (ret > 0)
2951 return 0;
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02002952 return ret;
2953 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002954
Heiko Schocherff94bc42014-06-24 10:10:04 +02002955 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002956}
2957
Heiko Schocherff94bc42014-06-24 10:10:04 +02002958/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002959 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2960 * @mtd: MTD device structure
2961 * @chip: nand chip info structure
2962 * @addr: feature address.
2963 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01002964 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002965static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2966 int addr, uint8_t *subfeature_param)
2967{
2968 int status;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002969 int i;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002970
Heiko Schocherff94bc42014-06-24 10:10:04 +02002971#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2972 if (!chip->onfi_version ||
2973 !(le16_to_cpu(chip->onfi_params.opt_cmd)
2974 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00002975 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002976#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00002977
2978 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002979 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2980 chip->write_byte(mtd, subfeature_param[i]);
2981
Sergey Lapindfe64e22013-01-14 03:46:50 +00002982 status = chip->waitfunc(mtd, chip);
2983 if (status & NAND_STATUS_FAIL)
2984 return -EIO;
2985 return 0;
2986}
2987
2988/**
2989 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2990 * @mtd: MTD device structure
2991 * @chip: nand chip info structure
2992 * @addr: feature address.
2993 * @subfeature_param: the subfeature parameters, a four bytes array.
2994 */
2995static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2996 int addr, uint8_t *subfeature_param)
2997{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002998 int i;
2999
3000#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3001 if (!chip->onfi_version ||
3002 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3003 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00003004 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003005#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003006
3007 /* clear the sub feature parameters */
3008 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
3009
3010 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003011 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3012 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003013 return 0;
3014}
3015
Heiko Schocherff94bc42014-06-24 10:10:04 +02003016#ifndef __UBOOT__
3017/**
3018 * nand_suspend - [MTD Interface] Suspend the NAND flash
3019 * @mtd: MTD device structure
3020 */
3021static int nand_suspend(struct mtd_info *mtd)
3022{
3023 return nand_get_device(mtd, FL_PM_SUSPENDED);
3024}
3025
3026/**
3027 * nand_resume - [MTD Interface] Resume the NAND flash
3028 * @mtd: MTD device structure
3029 */
3030static void nand_resume(struct mtd_info *mtd)
3031{
3032 struct nand_chip *chip = mtd->priv;
3033
3034 if (chip->state == FL_PM_SUSPENDED)
3035 nand_release_device(mtd);
3036 else
3037 pr_err("%s called for a chip which is not in suspended state\n",
3038 __func__);
3039}
3040#endif
3041
Sergey Lapindfe64e22013-01-14 03:46:50 +00003042/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01003043static void nand_set_defaults(struct nand_chip *chip, int busw)
3044{
3045 /* check for proper chip_delay setup, set 20us if not */
3046 if (!chip->chip_delay)
3047 chip->chip_delay = 20;
3048
3049 /* check, if a user supplied command function given */
3050 if (chip->cmdfunc == NULL)
3051 chip->cmdfunc = nand_command;
3052
3053 /* check, if a user supplied wait function given */
3054 if (chip->waitfunc == NULL)
3055 chip->waitfunc = nand_wait;
3056
3057 if (!chip->select_chip)
3058 chip->select_chip = nand_select_chip;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003059
3060 /* set for ONFI nand */
3061 if (!chip->onfi_set_features)
3062 chip->onfi_set_features = nand_onfi_set_features;
3063 if (!chip->onfi_get_features)
3064 chip->onfi_get_features = nand_onfi_get_features;
3065
3066 /* If called twice, pointers that depend on busw may need to be reset */
3067 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juulcfa460a2007-10-31 13:53:06 +01003068 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3069 if (!chip->read_word)
3070 chip->read_word = nand_read_word;
3071 if (!chip->block_bad)
3072 chip->block_bad = nand_block_bad;
3073 if (!chip->block_markbad)
3074 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003075 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juulcfa460a2007-10-31 13:53:06 +01003076 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003077 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3078 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3079 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juulcfa460a2007-10-31 13:53:06 +01003080 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juulcfa460a2007-10-31 13:53:06 +01003081 if (!chip->scan_bbt)
3082 chip->scan_bbt = nand_default_bbt;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003083
3084 if (!chip->controller) {
William Juulcfa460a2007-10-31 13:53:06 +01003085 chip->controller = &chip->hwcontrol;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003086 spin_lock_init(&chip->controller->lock);
3087 init_waitqueue_head(&chip->controller->wq);
3088 }
3089
William Juulcfa460a2007-10-31 13:53:06 +01003090}
3091
Sergey Lapindfe64e22013-01-14 03:46:50 +00003092/* Sanitize ONFI strings so we can safely print them */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003093#ifndef __UBOOT__
3094static void sanitize_string(uint8_t *s, size_t len)
3095#else
Christian Hitz5454ddb2011-10-12 09:32:05 +02003096static void sanitize_string(char *s, size_t len)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003097#endif
Christian Hitz5454ddb2011-10-12 09:32:05 +02003098{
3099 ssize_t i;
3100
Sergey Lapindfe64e22013-01-14 03:46:50 +00003101 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02003102 s[len - 1] = 0;
3103
Sergey Lapindfe64e22013-01-14 03:46:50 +00003104 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02003105 for (i = 0; i < len - 1; i++) {
3106 if (s[i] < ' ' || s[i] > 127)
3107 s[i] = '?';
3108 }
3109
Sergey Lapindfe64e22013-01-14 03:46:50 +00003110 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02003111 strim(s);
3112}
3113
Florian Fainelli0272c712011-02-25 00:01:34 +00003114static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01003115{
Florian Fainelli0272c712011-02-25 00:01:34 +00003116 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00003117 while (len--) {
3118 crc ^= *p++ << 8;
3119 for (i = 0; i < 8; i++)
3120 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05003121 }
3122
Florian Fainelli0272c712011-02-25 00:01:34 +00003123 return crc;
3124}
William Juulcfa460a2007-10-31 13:53:06 +01003125
Heiko Schocher4e67c572014-07-15 16:08:43 +02003126#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherff94bc42014-06-24 10:10:04 +02003127/* Parse the Extended Parameter Page. */
3128static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3129 struct nand_chip *chip, struct nand_onfi_params *p)
3130{
3131 struct onfi_ext_param_page *ep;
3132 struct onfi_ext_section *s;
3133 struct onfi_ext_ecc_info *ecc;
3134 uint8_t *cursor;
3135 int ret = -EINVAL;
3136 int len;
3137 int i;
3138
3139 len = le16_to_cpu(p->ext_param_page_length) * 16;
3140 ep = kmalloc(len, GFP_KERNEL);
3141 if (!ep)
3142 return -ENOMEM;
3143
3144 /* Send our own NAND_CMD_PARAM. */
3145 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3146
3147 /* Use the Change Read Column command to skip the ONFI param pages. */
3148 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3149 sizeof(*p) * p->num_of_param_pages , -1);
3150
3151 /* Read out the Extended Parameter Page. */
3152 chip->read_buf(mtd, (uint8_t *)ep, len);
3153 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3154 != le16_to_cpu(ep->crc))) {
3155 pr_debug("fail in the CRC.\n");
3156 goto ext_out;
3157 }
3158
3159 /*
3160 * Check the signature.
3161 * Do not strictly follow the ONFI spec, maybe changed in future.
3162 */
3163#ifndef __UBOOT__
3164 if (strncmp(ep->sig, "EPPS", 4)) {
3165#else
3166 if (strncmp((char *)ep->sig, "EPPS", 4)) {
3167#endif
3168 pr_debug("The signature is invalid.\n");
3169 goto ext_out;
3170 }
3171
3172 /* find the ECC section. */
3173 cursor = (uint8_t *)(ep + 1);
3174 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3175 s = ep->sections + i;
3176 if (s->type == ONFI_SECTION_TYPE_2)
3177 break;
3178 cursor += s->length * 16;
3179 }
3180 if (i == ONFI_EXT_SECTION_MAX) {
3181 pr_debug("We can not find the ECC section.\n");
3182 goto ext_out;
3183 }
3184
3185 /* get the info we want. */
3186 ecc = (struct onfi_ext_ecc_info *)cursor;
3187
3188 if (!ecc->codeword_size) {
3189 pr_debug("Invalid codeword size\n");
3190 goto ext_out;
3191 }
3192
3193 chip->ecc_strength_ds = ecc->ecc_bits;
3194 chip->ecc_step_ds = 1 << ecc->codeword_size;
3195 ret = 0;
3196
3197ext_out:
3198 kfree(ep);
3199 return ret;
3200}
3201
3202static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3203{
3204 struct nand_chip *chip = mtd->priv;
3205 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3206
3207 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3208 feature);
3209}
3210
3211/*
3212 * Configure chip properties from Micron vendor-specific ONFI table
3213 */
3214static void nand_onfi_detect_micron(struct nand_chip *chip,
3215 struct nand_onfi_params *p)
3216{
3217 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3218
3219 if (le16_to_cpu(p->vendor_revision) < 1)
3220 return;
3221
3222 chip->read_retries = micron->read_retry_options;
3223 chip->setup_read_retry = nand_setup_read_retry_micron;
3224}
3225
Florian Fainelli0272c712011-02-25 00:01:34 +00003226/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003227 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00003228 */
Christian Hitz90e3f392011-10-12 09:32:01 +02003229static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003230 int *busw)
3231{
3232 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05303233 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00003234 int val;
3235
Sergey Lapindfe64e22013-01-14 03:46:50 +00003236 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00003237 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3238 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3239 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3240 return 0;
3241
Florian Fainelli0272c712011-02-25 00:01:34 +00003242 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3243 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05303244 for (j = 0; j < sizeof(*p); j++)
3245 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003246 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02003247 le16_to_cpu(p->crc)) {
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01003248 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00003249 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02003250 }
William Juulcfa460a2007-10-31 13:53:06 +01003251
Heiko Schocherff94bc42014-06-24 10:10:04 +02003252 if (i == 3) {
3253 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00003254 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003255 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003256
Sergey Lapindfe64e22013-01-14 03:46:50 +00003257 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00003258 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003259 if (val & (1 << 5))
3260 chip->onfi_version = 23;
3261 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00003262 chip->onfi_version = 22;
3263 else if (val & (1 << 3))
3264 chip->onfi_version = 21;
3265 else if (val & (1 << 2))
3266 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003267 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00003268 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003269
3270 if (!chip->onfi_version) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003271 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003272 return 0;
3273 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003274
Christian Hitz5454ddb2011-10-12 09:32:05 +02003275 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3276 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01003277 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00003278 mtd->name = p->model;
William Juulcfa460a2007-10-31 13:53:06 +01003279
Heiko Schocherff94bc42014-06-24 10:10:04 +02003280 mtd->writesize = le32_to_cpu(p->byte_per_page);
3281
3282 /*
3283 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3284 * (don't ask me who thought of this...). MTD assumes that these
3285 * dimensions will be power-of-2, so just truncate the remaining area.
3286 */
3287 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3288 mtd->erasesize *= mtd->writesize;
3289
3290 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3291
3292 /* See erasesize comment */
3293 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3294 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3295 chip->bits_per_cell = p->bits_per_cell;
3296
3297 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3298 *busw = NAND_BUSWIDTH_16;
3299 else
3300 *busw = 0;
3301
3302 if (p->ecc_bits != 0xff) {
3303 chip->ecc_strength_ds = p->ecc_bits;
3304 chip->ecc_step_ds = 512;
3305 } else if (chip->onfi_version >= 21 &&
3306 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3307
3308 /*
3309 * The nand_flash_detect_ext_param_page() uses the
3310 * Change Read Column command which maybe not supported
3311 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3312 * now. We do not replace user supplied command function.
3313 */
3314 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3315 chip->cmdfunc = nand_command_lp;
3316
3317 /* The Extended Parameter Page is supported since ONFI 2.1. */
3318 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3319 pr_warn("Failed to detect ONFI extended param page\n");
3320 } else {
3321 pr_warn("Could not retrieve ONFI ECC requirements\n");
3322 }
3323
3324 if (p->jedec_id == NAND_MFR_MICRON)
3325 nand_onfi_detect_micron(chip, p);
3326
Florian Fainelli0272c712011-02-25 00:01:34 +00003327 return 1;
3328}
3329#else
Heiko Schocherff94bc42014-06-24 10:10:04 +02003330static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003331 int *busw)
3332{
3333 return 0;
3334}
3335#endif
3336
Florian Fainelli0272c712011-02-25 00:01:34 +00003337/*
Heiko Schocher4e67c572014-07-15 16:08:43 +02003338 * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3339 */
3340static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3341 int *busw)
3342{
3343 struct nand_jedec_params *p = &chip->jedec_params;
3344 struct jedec_ecc_info *ecc;
3345 int val;
3346 int i, j;
3347
3348 /* Try JEDEC for unknown chip or LP */
3349 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3350 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3351 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3352 chip->read_byte(mtd) != 'C')
3353 return 0;
3354
3355 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3356 for (i = 0; i < 3; i++) {
3357 for (j = 0; j < sizeof(*p); j++)
3358 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3359
3360 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3361 le16_to_cpu(p->crc))
3362 break;
3363 }
3364
3365 if (i == 3) {
3366 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3367 return 0;
3368 }
3369
3370 /* Check version */
3371 val = le16_to_cpu(p->revision);
3372 if (val & (1 << 2))
3373 chip->jedec_version = 10;
3374 else if (val & (1 << 1))
3375 chip->jedec_version = 1; /* vendor specific version */
3376
3377 if (!chip->jedec_version) {
3378 pr_info("unsupported JEDEC version: %d\n", val);
3379 return 0;
3380 }
3381
3382 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3383 sanitize_string(p->model, sizeof(p->model));
3384 if (!mtd->name)
3385 mtd->name = p->model;
3386
3387 mtd->writesize = le32_to_cpu(p->byte_per_page);
3388
3389 /* Please reference to the comment for nand_flash_detect_onfi. */
3390 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3391 mtd->erasesize *= mtd->writesize;
3392
3393 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3394
3395 /* Please reference to the comment for nand_flash_detect_onfi. */
3396 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3397 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3398 chip->bits_per_cell = p->bits_per_cell;
3399
3400 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3401 *busw = NAND_BUSWIDTH_16;
3402 else
3403 *busw = 0;
3404
3405 /* ECC info */
3406 ecc = &p->ecc_info[0];
3407
3408 if (ecc->codeword_size >= 9) {
3409 chip->ecc_strength_ds = ecc->ecc_bits;
3410 chip->ecc_step_ds = 1 << ecc->codeword_size;
3411 } else {
3412 pr_warn("Invalid codeword size\n");
3413 }
3414
3415 return 1;
3416}
3417
3418/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003419 * nand_id_has_period - Check if an ID string has a given wraparound period
3420 * @id_data: the ID string
3421 * @arrlen: the length of the @id_data array
3422 * @period: the period of repitition
3423 *
3424 * Check if an ID string is repeated within a given sequence of bytes at
3425 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherff94bc42014-06-24 10:10:04 +02003426 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapindfe64e22013-01-14 03:46:50 +00003427 * if the repetition has a period of @period; otherwise, returns zero.
3428 */
3429static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3430{
3431 int i, j;
3432 for (i = 0; i < period; i++)
3433 for (j = i + period; j < arrlen; j += period)
3434 if (id_data[i] != id_data[j])
3435 return 0;
3436 return 1;
3437}
3438
3439/*
3440 * nand_id_len - Get the length of an ID string returned by CMD_READID
3441 * @id_data: the ID string
3442 * @arrlen: the length of the @id_data array
3443
3444 * Returns the length of the ID string, according to known wraparound/trailing
3445 * zero patterns. If no pattern exists, returns the length of the array.
3446 */
3447static int nand_id_len(u8 *id_data, int arrlen)
3448{
3449 int last_nonzero, period;
3450
3451 /* Find last non-zero byte */
3452 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3453 if (id_data[last_nonzero])
3454 break;
3455
3456 /* All zeros */
3457 if (last_nonzero < 0)
3458 return 0;
3459
3460 /* Calculate wraparound period */
3461 for (period = 1; period < arrlen; period++)
3462 if (nand_id_has_period(id_data, arrlen, period))
3463 break;
3464
3465 /* There's a repeated pattern */
3466 if (period < arrlen)
3467 return period;
3468
3469 /* There are trailing zeros */
3470 if (last_nonzero < arrlen - 1)
3471 return last_nonzero + 1;
3472
3473 /* No pattern detected */
3474 return arrlen;
3475}
3476
Heiko Schocherff94bc42014-06-24 10:10:04 +02003477/* Extract the bits of per cell from the 3rd byte of the extended ID */
3478static int nand_get_bits_per_cell(u8 cellinfo)
3479{
3480 int bits;
3481
3482 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3483 bits >>= NAND_CI_CELLTYPE_SHIFT;
3484 return bits + 1;
3485}
3486
Sergey Lapindfe64e22013-01-14 03:46:50 +00003487/*
3488 * Many new NAND share similar device ID codes, which represent the size of the
3489 * chip. The rest of the parameters must be decoded according to generic or
3490 * manufacturer-specific "extended ID" decoding patterns.
3491 */
3492static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3493 u8 id_data[8], int *busw)
3494{
3495 int extid, id_len;
3496 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003497 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003498 /* The 4th id byte is the important one */
3499 extid = id_data[3];
3500
3501 id_len = nand_id_len(id_data, 8);
3502
3503 /*
3504 * Field definitions are in the following datasheets:
3505 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3506 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3507 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3508 *
3509 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3510 * ID to decide what to do.
3511 */
3512 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003513 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003514 /* Calc pagesize */
3515 mtd->writesize = 2048 << (extid & 0x03);
3516 extid >>= 2;
3517 /* Calc oobsize */
3518 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3519 case 1:
3520 mtd->oobsize = 128;
3521 break;
3522 case 2:
3523 mtd->oobsize = 218;
3524 break;
3525 case 3:
3526 mtd->oobsize = 400;
3527 break;
3528 case 4:
3529 mtd->oobsize = 436;
3530 break;
3531 case 5:
3532 mtd->oobsize = 512;
3533 break;
3534 case 6:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003535 mtd->oobsize = 640;
3536 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003537 case 7:
3538 default: /* Other cases are "reserved" (unknown) */
3539 mtd->oobsize = 1024;
3540 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003541 }
3542 extid >>= 2;
3543 /* Calc blocksize */
3544 mtd->erasesize = (128 * 1024) <<
3545 (((extid >> 1) & 0x04) | (extid & 0x03));
3546 *busw = 0;
3547 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003548 !nand_is_slc(chip)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003549 unsigned int tmp;
3550
3551 /* Calc pagesize */
3552 mtd->writesize = 2048 << (extid & 0x03);
3553 extid >>= 2;
3554 /* Calc oobsize */
3555 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3556 case 0:
3557 mtd->oobsize = 128;
3558 break;
3559 case 1:
3560 mtd->oobsize = 224;
3561 break;
3562 case 2:
3563 mtd->oobsize = 448;
3564 break;
3565 case 3:
3566 mtd->oobsize = 64;
3567 break;
3568 case 4:
3569 mtd->oobsize = 32;
3570 break;
3571 case 5:
3572 mtd->oobsize = 16;
3573 break;
3574 default:
3575 mtd->oobsize = 640;
3576 break;
3577 }
3578 extid >>= 2;
3579 /* Calc blocksize */
3580 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3581 if (tmp < 0x03)
3582 mtd->erasesize = (128 * 1024) << tmp;
3583 else if (tmp == 0x03)
3584 mtd->erasesize = 768 * 1024;
3585 else
3586 mtd->erasesize = (64 * 1024) << tmp;
3587 *busw = 0;
3588 } else {
3589 /* Calc pagesize */
3590 mtd->writesize = 1024 << (extid & 0x03);
3591 extid >>= 2;
3592 /* Calc oobsize */
3593 mtd->oobsize = (8 << (extid & 0x01)) *
3594 (mtd->writesize >> 9);
3595 extid >>= 2;
3596 /* Calc blocksize. Blocksize is multiples of 64KiB */
3597 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3598 extid >>= 2;
3599 /* Get buswidth information */
3600 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003601
3602 /*
3603 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3604 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3605 * follows:
3606 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3607 * 110b -> 24nm
3608 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3609 */
3610 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3611 nand_is_slc(chip) &&
3612 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3613 !(id_data[4] & 0x80) /* !BENAND */) {
3614 mtd->oobsize = 32 * mtd->writesize >> 9;
3615 }
3616
Sergey Lapindfe64e22013-01-14 03:46:50 +00003617 }
3618}
3619
Heiko Schocherff94bc42014-06-24 10:10:04 +02003620/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003621 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3622 * decodes a matching ID table entry and assigns the MTD size parameters for
3623 * the chip.
3624 */
3625static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003626 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapindfe64e22013-01-14 03:46:50 +00003627 int *busw)
3628{
3629 int maf_id = id_data[0];
3630
3631 mtd->erasesize = type->erasesize;
3632 mtd->writesize = type->pagesize;
3633 mtd->oobsize = mtd->writesize / 32;
3634 *busw = type->options & NAND_BUSWIDTH_16;
3635
Heiko Schocherff94bc42014-06-24 10:10:04 +02003636 /* All legacy ID NAND are small-page, SLC */
3637 chip->bits_per_cell = 1;
3638
Sergey Lapindfe64e22013-01-14 03:46:50 +00003639 /*
3640 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3641 * some Spansion chips have erasesize that conflicts with size
3642 * listed in nand_ids table.
3643 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3644 */
3645 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3646 && id_data[6] == 0x00 && id_data[7] == 0x00
3647 && mtd->writesize == 512) {
3648 mtd->erasesize = 128 * 1024;
3649 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3650 }
3651}
3652
Heiko Schocherff94bc42014-06-24 10:10:04 +02003653/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003654 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3655 * heuristic patterns using various detected parameters (e.g., manufacturer,
3656 * page size, cell-type information).
3657 */
3658static void nand_decode_bbm_options(struct mtd_info *mtd,
3659 struct nand_chip *chip, u8 id_data[8])
3660{
3661 int maf_id = id_data[0];
3662
3663 /* Set the bad block position */
3664 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3665 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3666 else
3667 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3668
3669 /*
3670 * Bad block marker is stored in the last page of each block on Samsung
3671 * and Hynix MLC devices; stored in first two pages of each block on
3672 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3673 * AMD/Spansion, and Macronix. All others scan only the first page.
3674 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003675 if (!nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003676 (maf_id == NAND_MFR_SAMSUNG ||
3677 maf_id == NAND_MFR_HYNIX))
3678 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003679 else if ((nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003680 (maf_id == NAND_MFR_SAMSUNG ||
3681 maf_id == NAND_MFR_HYNIX ||
3682 maf_id == NAND_MFR_TOSHIBA ||
3683 maf_id == NAND_MFR_AMD ||
3684 maf_id == NAND_MFR_MACRONIX)) ||
3685 (mtd->writesize == 2048 &&
3686 maf_id == NAND_MFR_MICRON))
3687 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3688}
3689
Heiko Schocherff94bc42014-06-24 10:10:04 +02003690static inline bool is_full_id_nand(struct nand_flash_dev *type)
3691{
3692 return type->id_len;
3693}
3694
3695static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3696 struct nand_flash_dev *type, u8 *id_data, int *busw)
3697{
3698#ifndef __UBOOT__
3699 if (!strncmp(type->id, id_data, type->id_len)) {
3700#else
3701 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3702#endif
3703 mtd->writesize = type->pagesize;
3704 mtd->erasesize = type->erasesize;
3705 mtd->oobsize = type->oobsize;
3706
3707 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3708 chip->chipsize = (uint64_t)type->chipsize << 20;
3709 chip->options |= type->options;
3710 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3711 chip->ecc_step_ds = NAND_ECC_STEP(type);
3712
3713 *busw = type->options & NAND_BUSWIDTH_16;
3714
3715 if (!mtd->name)
3716 mtd->name = type->name;
3717
3718 return true;
3719 }
3720 return false;
3721}
3722
Sergey Lapindfe64e22013-01-14 03:46:50 +00003723/*
3724 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00003725 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003726static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Florian Fainelli0272c712011-02-25 00:01:34 +00003727 struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003728 int *maf_id, int *dev_id,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003729 struct nand_flash_dev *type)
Florian Fainelli0272c712011-02-25 00:01:34 +00003730{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003731 int busw;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003732 int i, maf_idx;
3733 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00003734
3735 /* Select the device */
3736 chip->select_chip(mtd, 0);
3737
3738 /*
3739 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003740 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00003741 */
3742 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3743
3744 /* Send the command for reading device ID */
3745 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3746
3747 /* Read manufacturer and device IDs */
3748 *maf_id = chip->read_byte(mtd);
3749 *dev_id = chip->read_byte(mtd);
3750
Sergey Lapindfe64e22013-01-14 03:46:50 +00003751 /*
3752 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00003753 * interface concerns can cause random data which looks like a
3754 * possibly credible NAND flash to appear. If the two results do
3755 * not match, ignore the device completely.
3756 */
3757
3758 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3759
Sergey Lapindfe64e22013-01-14 03:46:50 +00003760 /* Read entire ID string */
3761 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003762 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003763
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003764 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003765 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003766 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00003767 return ERR_PTR(-ENODEV);
3768 }
3769
3770 if (!type)
3771 type = nand_flash_ids;
3772
Heiko Schocherff94bc42014-06-24 10:10:04 +02003773 for (; type->name != NULL; type++) {
3774 if (is_full_id_nand(type)) {
3775 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3776 goto ident_done;
3777 } else if (*dev_id == type->dev_id) {
3778 break;
3779 }
3780 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003781
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003782 chip->onfi_version = 0;
3783 if (!type->name || !type->pagesize) {
3784 /* Check is chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003785 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003786 goto ident_done;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003787
3788 /* Check if the chip is JEDEC compliant */
3789 if (nand_flash_detect_jedec(mtd, chip, &busw))
3790 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00003791 }
3792
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003793 if (!type->name)
3794 return ERR_PTR(-ENODEV);
3795
Florian Fainelli0272c712011-02-25 00:01:34 +00003796 if (!mtd->name)
3797 mtd->name = type->name;
3798
3799 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00003800
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003801 if (!type->pagesize && chip->init_size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003802 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003803 busw = chip->init_size(mtd, chip, id_data);
3804 } else if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003805 /* Decode parameters from extended ID */
3806 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003807 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003808 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003809 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003810 /* Get chip options */
Marek Vasut9c790a72012-08-30 13:39:38 +00003811 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00003812
Sergey Lapindfe64e22013-01-14 03:46:50 +00003813 /*
3814 * Check if chip is not a Samsung device. Do not clear the
3815 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003816 */
3817 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3818 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3819ident_done:
3820
William Juulcfa460a2007-10-31 13:53:06 +01003821 /* Try to identify manufacturer */
3822 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3823 if (nand_manuf_ids[maf_idx].id == *maf_id)
3824 break;
3825 }
3826
Heiko Schocherff94bc42014-06-24 10:10:04 +02003827 if (chip->options & NAND_BUSWIDTH_AUTO) {
3828 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3829 chip->options |= busw;
3830 nand_set_defaults(chip, busw);
3831 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3832 /*
3833 * Check, if buswidth is correct. Hardware drivers should set
3834 * chip correct!
3835 */
3836 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3837 *maf_id, *dev_id);
3838 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3839 pr_warn("bus width %d instead %d bit\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003840 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3841 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003842 return ERR_PTR(-EINVAL);
3843 }
3844
Sergey Lapindfe64e22013-01-14 03:46:50 +00003845 nand_decode_bbm_options(mtd, chip, id_data);
3846
William Juulcfa460a2007-10-31 13:53:06 +01003847 /* Calculate the address shift from the page size */
3848 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003849 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003850 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3851
3852 chip->bbt_erase_shift = chip->phys_erase_shift =
3853 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003854 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003855 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003856 else {
3857 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3858 chip->chip_shift += 32 - 1;
3859 }
3860
3861 chip->badblockbits = 8;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003862 chip->erase_cmd = single_erase_cmd;
William Juulcfa460a2007-10-31 13:53:06 +01003863
Sergey Lapindfe64e22013-01-14 03:46:50 +00003864 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003865 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3866 chip->cmdfunc = nand_command_lp;
3867
Heiko Schocherff94bc42014-06-24 10:10:04 +02003868 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3869 *maf_id, *dev_id);
Heiko Schocher4e67c572014-07-15 16:08:43 +02003870
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003871#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocher4e67c572014-07-15 16:08:43 +02003872 if (chip->onfi_version)
3873 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3874 chip->onfi_params.model);
3875 else if (chip->jedec_version)
3876 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3877 chip->jedec_params.model);
3878 else
3879 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3880 type->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003881#else
Heiko Schocher4e67c572014-07-15 16:08:43 +02003882 if (chip->jedec_version)
3883 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3884 chip->jedec_params.model);
3885 else
3886 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3887 type->name);
3888
3889 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3890 type->name);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003891#endif
Heiko Schocher4e67c572014-07-15 16:08:43 +02003892
Heiko Schocherff94bc42014-06-24 10:10:04 +02003893 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3894 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003895 mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003896 return type;
3897}
3898
3899/**
3900 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003901 * @mtd: MTD device structure
3902 * @maxchips: number of chips to scan for
3903 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003904 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003905 * This is the first phase of the normal nand_scan() function. It reads the
3906 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003907 *
3908 * The mtd->owner field must be set to the module of the caller.
3909 */
Lei Wen245eb902011-01-06 09:48:18 +08003910int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003911 struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003912{
Heiko Schocher4e67c572014-07-15 16:08:43 +02003913 int i, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01003914 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003915 struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003916
William Juulcfa460a2007-10-31 13:53:06 +01003917 /* Set the default functions */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003918 nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
William Juulcfa460a2007-10-31 13:53:06 +01003919
3920 /* Read the flash type */
Heiko Schocher4e67c572014-07-15 16:08:43 +02003921 type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3922 &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003923
3924 if (IS_ERR(type)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003925 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3926 pr_warn("No NAND device found\n");
William Juulcfa460a2007-10-31 13:53:06 +01003927 chip->select_chip(mtd, -1);
3928 return PTR_ERR(type);
3929 }
3930
Heiko Schocherff94bc42014-06-24 10:10:04 +02003931 chip->select_chip(mtd, -1);
3932
William Juulcfa460a2007-10-31 13:53:06 +01003933 /* Check for a chip array */
3934 for (i = 1; i < maxchips; i++) {
3935 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003936 /* See comment in nand_get_flash_type for reset */
3937 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003938 /* Send the command for reading device ID */
3939 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3940 /* Read manufacturer and device IDs */
3941 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02003942 nand_dev_id != chip->read_byte(mtd)) {
3943 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003944 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003945 }
3946 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003947 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003948
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003949#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003950 if (i > 1)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003951 pr_info("%d chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003952#endif
William Juulcfa460a2007-10-31 13:53:06 +01003953
3954 /* Store the number of chips and calc total size for mtd */
3955 chip->numchips = i;
3956 mtd->size = i * chip->chipsize;
3957
3958 return 0;
3959}
Heiko Schocherff94bc42014-06-24 10:10:04 +02003960EXPORT_SYMBOL(nand_scan_ident);
William Juulcfa460a2007-10-31 13:53:06 +01003961
3962
3963/**
3964 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003965 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003966 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003967 * This is the second phase of the normal nand_scan() function. It fills out
3968 * all the uninitialized function pointers with the defaults and scans for a
3969 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003970 */
3971int nand_scan_tail(struct mtd_info *mtd)
3972{
3973 int i;
3974 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003975 struct nand_ecc_ctrl *ecc = &chip->ecc;
Heiko Schocher4e67c572014-07-15 16:08:43 +02003976 struct nand_buffers *nbuf;
William Juulcfa460a2007-10-31 13:53:06 +01003977
Sergey Lapindfe64e22013-01-14 03:46:50 +00003978 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3979 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3980 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3981
Heiko Schocher4e67c572014-07-15 16:08:43 +02003982 if (!(chip->options & NAND_OWN_BUFFERS)) {
3983#ifndef __UBOOT__
3984 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3985 + mtd->oobsize * 3, GFP_KERNEL);
3986 if (!nbuf)
3987 return -ENOMEM;
3988 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3989 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3990 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3991#else
3992 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
3993#endif
3994
3995 chip->buffers = nbuf;
3996 } else {
3997 if (!chip->buffers)
3998 return -ENOMEM;
3999 }
William Juulcfa460a2007-10-31 13:53:06 +01004000
4001 /* Set the internal oob buffer location, just after the page data */
4002 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4003
4004 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00004005 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01004006 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004007 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01004008 switch (mtd->oobsize) {
4009 case 8:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004010 ecc->layout = &nand_oob_8;
William Juulcfa460a2007-10-31 13:53:06 +01004011 break;
4012 case 16:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004013 ecc->layout = &nand_oob_16;
William Juulcfa460a2007-10-31 13:53:06 +01004014 break;
4015 case 64:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004016 ecc->layout = &nand_oob_64;
William Juulcfa460a2007-10-31 13:53:06 +01004017 break;
4018 case 128:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004019 ecc->layout = &nand_oob_128;
William Juulcfa460a2007-10-31 13:53:06 +01004020 break;
4021 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004022 pr_warn("No oob scheme defined for oobsize %d\n",
4023 mtd->oobsize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02004024 BUG();
William Juulcfa460a2007-10-31 13:53:06 +01004025 }
4026 }
4027
4028 if (!chip->write_page)
4029 chip->write_page = nand_write_page;
4030
4031 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00004032 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01004033 * selected and we have 256 byte pagesize fallback to software ECC
4034 */
William Juulcfa460a2007-10-31 13:53:06 +01004035
Heiko Schocherff94bc42014-06-24 10:10:04 +02004036 switch (ecc->mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004037 case NAND_ECC_HW_OOB_FIRST:
4038 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004039 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004040 pr_warn("No ECC functions supplied; "
4041 "hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004042 BUG();
4043 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004044 if (!ecc->read_page)
4045 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004046
William Juulcfa460a2007-10-31 13:53:06 +01004047 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004048 /* Use standard hwecc read page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004049 if (!ecc->read_page)
4050 ecc->read_page = nand_read_page_hwecc;
4051 if (!ecc->write_page)
4052 ecc->write_page = nand_write_page_hwecc;
4053 if (!ecc->read_page_raw)
4054 ecc->read_page_raw = nand_read_page_raw;
4055 if (!ecc->write_page_raw)
4056 ecc->write_page_raw = nand_write_page_raw;
4057 if (!ecc->read_oob)
4058 ecc->read_oob = nand_read_oob_std;
4059 if (!ecc->write_oob)
4060 ecc->write_oob = nand_write_oob_std;
4061 if (!ecc->read_subpage)
4062 ecc->read_subpage = nand_read_subpage;
4063 if (!ecc->write_subpage)
4064 ecc->write_subpage = nand_write_subpage_hwecc;
William Juulcfa460a2007-10-31 13:53:06 +01004065
4066 case NAND_ECC_HW_SYNDROME:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004067 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4068 (!ecc->read_page ||
4069 ecc->read_page == nand_read_page_hwecc ||
4070 !ecc->write_page ||
4071 ecc->write_page == nand_write_page_hwecc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004072 pr_warn("No ECC functions supplied; "
4073 "hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01004074 BUG();
4075 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00004076 /* Use standard syndrome read/write page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004077 if (!ecc->read_page)
4078 ecc->read_page = nand_read_page_syndrome;
4079 if (!ecc->write_page)
4080 ecc->write_page = nand_write_page_syndrome;
4081 if (!ecc->read_page_raw)
4082 ecc->read_page_raw = nand_read_page_raw_syndrome;
4083 if (!ecc->write_page_raw)
4084 ecc->write_page_raw = nand_write_page_raw_syndrome;
4085 if (!ecc->read_oob)
4086 ecc->read_oob = nand_read_oob_syndrome;
4087 if (!ecc->write_oob)
4088 ecc->write_oob = nand_write_oob_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01004089
Heiko Schocherff94bc42014-06-24 10:10:04 +02004090 if (mtd->writesize >= ecc->size) {
4091 if (!ecc->strength) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004092 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4093 BUG();
4094 }
William Juulcfa460a2007-10-31 13:53:06 +01004095 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004096 }
4097 pr_warn("%d byte HW ECC not possible on "
4098 "%d byte page size, fallback to SW ECC\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02004099 ecc->size, mtd->writesize);
4100 ecc->mode = NAND_ECC_SOFT;
William Juulcfa460a2007-10-31 13:53:06 +01004101
4102 case NAND_ECC_SOFT:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004103 ecc->calculate = nand_calculate_ecc;
4104 ecc->correct = nand_correct_data;
4105 ecc->read_page = nand_read_page_swecc;
4106 ecc->read_subpage = nand_read_subpage;
4107 ecc->write_page = nand_write_page_swecc;
4108 ecc->read_page_raw = nand_read_page_raw;
4109 ecc->write_page_raw = nand_write_page_raw;
4110 ecc->read_oob = nand_read_oob_std;
4111 ecc->write_oob = nand_write_oob_std;
4112 if (!ecc->size)
4113 ecc->size = 256;
4114 ecc->bytes = 3;
4115 ecc->strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01004116 break;
4117
Christian Hitz4c6de852011-10-12 09:31:59 +02004118 case NAND_ECC_SOFT_BCH:
4119 if (!mtd_nand_has_bch()) {
Heiko Schocher4e67c572014-07-15 16:08:43 +02004120 pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004121 BUG();
Christian Hitz4c6de852011-10-12 09:31:59 +02004122 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004123 ecc->calculate = nand_bch_calculate_ecc;
4124 ecc->correct = nand_bch_correct_data;
4125 ecc->read_page = nand_read_page_swecc;
4126 ecc->read_subpage = nand_read_subpage;
4127 ecc->write_page = nand_write_page_swecc;
4128 ecc->read_page_raw = nand_read_page_raw;
4129 ecc->write_page_raw = nand_write_page_raw;
4130 ecc->read_oob = nand_read_oob_std;
4131 ecc->write_oob = nand_write_oob_std;
Christian Hitz4c6de852011-10-12 09:31:59 +02004132 /*
4133 * Board driver should supply ecc.size and ecc.bytes values to
4134 * select how many bits are correctable; see nand_bch_init()
Sergey Lapindfe64e22013-01-14 03:46:50 +00004135 * for details. Otherwise, default to 4 bits for large page
4136 * devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02004137 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004138 if (!ecc->size && (mtd->oobsize >= 64)) {
4139 ecc->size = 512;
4140 ecc->bytes = 7;
Christian Hitz4c6de852011-10-12 09:31:59 +02004141 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004142 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4143 &ecc->layout);
4144 if (!ecc->priv) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004145 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004146 BUG();
4147 }
4148 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Christian Hitz4c6de852011-10-12 09:31:59 +02004149 break;
4150
William Juulcfa460a2007-10-31 13:53:06 +01004151 case NAND_ECC_NONE:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004152 pr_warn("NAND_ECC_NONE selected by board driver. "
Heiko Schocherff94bc42014-06-24 10:10:04 +02004153 "This is not recommended!\n");
4154 ecc->read_page = nand_read_page_raw;
4155 ecc->write_page = nand_write_page_raw;
4156 ecc->read_oob = nand_read_oob_std;
4157 ecc->read_page_raw = nand_read_page_raw;
4158 ecc->write_page_raw = nand_write_page_raw;
4159 ecc->write_oob = nand_write_oob_std;
4160 ecc->size = mtd->writesize;
4161 ecc->bytes = 0;
4162 ecc->strength = 0;
William Juulcfa460a2007-10-31 13:53:06 +01004163 break;
4164
4165 default:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004166 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juulcfa460a2007-10-31 13:53:06 +01004167 BUG();
4168 }
4169
Sergey Lapindfe64e22013-01-14 03:46:50 +00004170 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004171 if (!ecc->read_oob_raw)
4172 ecc->read_oob_raw = ecc->read_oob;
4173 if (!ecc->write_oob_raw)
4174 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004175
William Juulcfa460a2007-10-31 13:53:06 +01004176 /*
4177 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00004178 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01004179 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004180 ecc->layout->oobavail = 0;
4181 for (i = 0; ecc->layout->oobfree[i].length
4182 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4183 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4184 mtd->oobavail = ecc->layout->oobavail;
William Juulcfa460a2007-10-31 13:53:06 +01004185
4186 /*
4187 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00004188 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01004189 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004190 ecc->steps = mtd->writesize / ecc->size;
4191 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004192 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01004193 BUG();
4194 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004195 ecc->total = ecc->steps * ecc->bytes;
William Juulcfa460a2007-10-31 13:53:06 +01004196
Sergey Lapindfe64e22013-01-14 03:46:50 +00004197 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004198 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4199 switch (ecc->steps) {
William Juulcfa460a2007-10-31 13:53:06 +01004200 case 2:
4201 mtd->subpage_sft = 1;
4202 break;
4203 case 4:
4204 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05004205 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01004206 mtd->subpage_sft = 2;
4207 break;
4208 }
4209 }
4210 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4211
4212 /* Initialize state */
4213 chip->state = FL_READY;
4214
William Juulcfa460a2007-10-31 13:53:06 +01004215 /* Invalidate the pagebuffer reference */
4216 chip->pagebuf = -1;
4217
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004218 /* Large page NAND with SOFT_ECC should support subpage reads */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004219 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004220 chip->options |= NAND_SUBPAGE_READ;
4221
William Juulcfa460a2007-10-31 13:53:06 +01004222 /* Fill in remaining MTD driver data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004223 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004224 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4225 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004226 mtd->_erase = nand_erase;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004227#ifndef __UBOOT__
Sergey Lapindfe64e22013-01-14 03:46:50 +00004228 mtd->_point = NULL;
4229 mtd->_unpoint = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004230#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00004231 mtd->_read = nand_read;
4232 mtd->_write = nand_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004233 mtd->_panic_write = panic_nand_write;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004234 mtd->_read_oob = nand_read_oob;
4235 mtd->_write_oob = nand_write_oob;
4236 mtd->_sync = nand_sync;
4237 mtd->_lock = NULL;
4238 mtd->_unlock = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004239#ifndef __UBOOT__
4240 mtd->_suspend = nand_suspend;
4241 mtd->_resume = nand_resume;
4242#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00004243 mtd->_block_isbad = nand_block_isbad;
4244 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004245 mtd->writebufsize = mtd->writesize;
William Juulcfa460a2007-10-31 13:53:06 +01004246
Sergey Lapindfe64e22013-01-14 03:46:50 +00004247 /* propagate ecc info to mtd_info */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004248 mtd->ecclayout = ecc->layout;
4249 mtd->ecc_strength = ecc->strength;
4250 mtd->ecc_step_size = ecc->size;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004251 /*
4252 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4253 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4254 * properly set.
4255 */
4256 if (!mtd->bitflip_threshold)
4257 mtd->bitflip_threshold = mtd->ecc_strength;
William Juulcfa460a2007-10-31 13:53:06 +01004258
Rostislav Lisovy35c204d2014-10-22 13:40:44 +02004259 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01004260}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004261EXPORT_SYMBOL(nand_scan_tail);
4262
4263/*
4264 * is_module_text_address() isn't exported, and it's mostly a pointless
4265 * test if this is a module _anyway_ -- they'd have to try _really_ hard
4266 * to call us from in-kernel code if the core NAND support is modular.
4267 */
4268#ifdef MODULE
4269#define caller_is_module() (1)
4270#else
4271#define caller_is_module() \
4272 is_module_text_address((unsigned long)__builtin_return_address(0))
4273#endif
William Juulcfa460a2007-10-31 13:53:06 +01004274
William Juulcfa460a2007-10-31 13:53:06 +01004275/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02004276 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004277 * @mtd: MTD device structure
4278 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02004279 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00004280 * This fills out all the uninitialized function pointers with the defaults.
4281 * The flash ID is read and the mtd/chip structures are filled with the
4282 * appropriate values. The mtd->owner field must be set to the module of the
4283 * caller.
Wolfgang Denk932394a2005-08-17 12:55:25 +02004284 */
William Juulcfa460a2007-10-31 13:53:06 +01004285int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004286{
William Juulcfa460a2007-10-31 13:53:06 +01004287 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004288
Heiko Schocherff94bc42014-06-24 10:10:04 +02004289 /* Many callers got this wrong, so check for it for a while... */
4290 if (!mtd->owner && caller_is_module()) {
4291 pr_crit("%s called with NULL mtd->owner!\n", __func__);
4292 BUG();
4293 }
4294
Lei Wen245eb902011-01-06 09:48:18 +08004295 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01004296 if (!ret)
4297 ret = nand_scan_tail(mtd);
4298 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004299}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004300EXPORT_SYMBOL(nand_scan);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004301
Heiko Schocherff94bc42014-06-24 10:10:04 +02004302#ifndef __UBOOT__
Wolfgang Denk932394a2005-08-17 12:55:25 +02004303/**
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02004304 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004305 * @mtd: MTD device structure
4306 */
William Juulcfa460a2007-10-31 13:53:06 +01004307void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004308{
William Juulcfa460a2007-10-31 13:53:06 +01004309 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004310
Christian Hitz4c6de852011-10-12 09:31:59 +02004311 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4312 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4313
Heiko Schocherff94bc42014-06-24 10:10:04 +02004314 mtd_device_unregister(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01004315
4316 /* Free bad block table memory */
4317 kfree(chip->bbt);
4318 if (!(chip->options & NAND_OWN_BUFFERS))
4319 kfree(chip->buffers);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004320
4321 /* Free bad block descriptor memory */
4322 if (chip->badblock_pattern && chip->badblock_pattern->options
4323 & NAND_BBT_DYNAMICSTRUCT)
4324 kfree(chip->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004325}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004326EXPORT_SYMBOL_GPL(nand_release);
4327
4328static int __init nand_base_init(void)
4329{
4330 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4331 return 0;
4332}
4333
4334static void __exit nand_base_exit(void)
4335{
4336 led_trigger_unregister_simple(nand_led_trigger);
4337}
4338#endif
4339
4340module_init(nand_base_init);
4341module_exit(nand_base_exit);
4342
4343MODULE_LICENSE("GPL");
4344MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4345MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4346MODULE_DESCRIPTION("Generic NAND flash driver code");