blob: ae61cca440ef090021eeda0baa4d5f039ea385c1 [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 Denkac7eb8a2005-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 Denkac7eb8a2005-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 Denkac7eb8a2005-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#define __UBOOT__
33#ifndef __UBOOT__
34#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35
36#include <linux/module.h>
37#include <linux/delay.h>
38#include <linux/errno.h>
39#include <linux/err.h>
40#include <linux/sched.h>
41#include <linux/slab.h>
42#include <linux/types.h>
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/mtd/nand_ecc.h>
46#include <linux/mtd/nand_bch.h>
47#include <linux/interrupt.h>
48#include <linux/bitops.h>
49#include <linux/leds.h>
50#include <linux/io.h>
51#include <linux/mtd/partitions.h>
52#else
53#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Wolfgang Denk932394a2005-08-17 12:55:25 +020054#include <common.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020055#include <malloc.h>
56#include <watchdog.h>
William Juulcfa460a2007-10-31 13:53:06 +010057#include <linux/err.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000058#include <linux/compat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020059#include <linux/mtd/mtd.h>
60#include <linux/mtd/nand.h>
61#include <linux/mtd/nand_ecc.h>
Christian Hitz4c6de852011-10-12 09:31:59 +020062#include <linux/mtd/nand_bch.h>
Stefan Roese10bb62d2009-04-24 15:58:33 +020063#ifdef CONFIG_MTD_PARTITIONS
64#include <linux/mtd/partitions.h>
65#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +020066#include <asm/io.h>
67#include <asm/errno.h>
68
Peter Tyser8da60122009-02-04 13:47:22 -060069/*
70 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
71 * a flash. NAND flash is initialized prior to interrupts so standard timers
72 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
73 * which is greater than (max NAND reset time / NAND status read time).
74 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
75 */
76#ifndef CONFIG_SYS_NAND_RESET_CNT
77#define CONFIG_SYS_NAND_RESET_CNT 200000
78#endif
79
Heiko Schocherff94bc42014-06-24 10:10:04 +020080static bool is_module_text_address(unsigned long addr) {return 0;}
81#endif
82
Wolfgang Denk932394a2005-08-17 12:55:25 +020083/* Define default oob placement schemes for large and small page devices */
William Juulcfa460a2007-10-31 13:53:06 +010084static struct nand_ecclayout nand_oob_8 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020085 .eccbytes = 3,
86 .eccpos = {0, 1, 2},
William Juulcfa460a2007-10-31 13:53:06 +010087 .oobfree = {
88 {.offset = 3,
89 .length = 2},
90 {.offset = 6,
Christian Hitz90e3f392011-10-12 09:32:01 +020091 .length = 2} }
Wolfgang Denk932394a2005-08-17 12:55:25 +020092};
93
William Juulcfa460a2007-10-31 13:53:06 +010094static struct nand_ecclayout nand_oob_16 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +020095 .eccbytes = 6,
96 .eccpos = {0, 1, 2, 3, 6, 7},
William Juulcfa460a2007-10-31 13:53:06 +010097 .oobfree = {
98 {.offset = 8,
Christian Hitz90e3f392011-10-12 09:32:01 +020099 . length = 8} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200100};
101
William Juulcfa460a2007-10-31 13:53:06 +0100102static struct nand_ecclayout nand_oob_64 = {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200103 .eccbytes = 24,
104 .eccpos = {
William Juulcfa460a2007-10-31 13:53:06 +0100105 40, 41, 42, 43, 44, 45, 46, 47,
106 48, 49, 50, 51, 52, 53, 54, 55,
107 56, 57, 58, 59, 60, 61, 62, 63},
108 .oobfree = {
109 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200110 .length = 38} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200111};
112
William Juulcfa460a2007-10-31 13:53:06 +0100113static struct nand_ecclayout nand_oob_128 = {
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200114 .eccbytes = 48,
115 .eccpos = {
Christian Hitz90e3f392011-10-12 09:32:01 +0200116 80, 81, 82, 83, 84, 85, 86, 87,
117 88, 89, 90, 91, 92, 93, 94, 95,
118 96, 97, 98, 99, 100, 101, 102, 103,
William Juulcfa460a2007-10-31 13:53:06 +0100119 104, 105, 106, 107, 108, 109, 110, 111,
120 112, 113, 114, 115, 116, 117, 118, 119,
121 120, 121, 122, 123, 124, 125, 126, 127},
122 .oobfree = {
123 {.offset = 2,
Christian Hitz90e3f392011-10-12 09:32:01 +0200124 .length = 78} }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200125};
126
Heiko Schocherff94bc42014-06-24 10:10:04 +0200127static int nand_get_device(struct mtd_info *mtd, int new_state);
William Juulcfa460a2007-10-31 13:53:06 +0100128
129static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
130 struct mtd_oob_ops *ops);
131
Heiko Schocherff94bc42014-06-24 10:10:04 +0200132/*
133 * For devices which display every fart in the system on a separate LED. Is
134 * compiled away when LED support is disabled.
135 */
136DEFINE_LED_TRIGGER(nand_led_trigger);
Sergei Poselenov248ae5c2008-06-06 15:42:43 +0200137
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200138static int check_offs_len(struct mtd_info *mtd,
139 loff_t ofs, uint64_t len)
140{
141 struct nand_chip *chip = mtd->priv;
142 int ret = 0;
143
144 /* Start address must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200145 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
146 pr_debug("%s: unaligned address\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200147 ret = -EINVAL;
148 }
149
150 /* Length must align on block boundary */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200151 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
152 pr_debug("%s: length not block aligned\n", __func__);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200153 ret = -EINVAL;
154 }
155
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200156 return ret;
157}
158
Wolfgang Denk932394a2005-08-17 12:55:25 +0200159/**
160 * nand_release_device - [GENERIC] release chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000161 * @mtd: MTD device structure
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200162 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200163 * Release chip lock and wake up anyone waiting on the device.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200164 */
Christian Hitz90e3f392011-10-12 09:32:01 +0200165static void nand_release_device(struct mtd_info *mtd)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100166{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200167 struct nand_chip *chip = mtd->priv;
168
Heiko Schocherff94bc42014-06-24 10:10:04 +0200169#ifndef __UBOOT__
170 /* Release the controller and the chip */
171 spin_lock(&chip->controller->lock);
172 chip->controller->active = NULL;
173 chip->state = FL_READY;
174 wake_up(&chip->controller->wq);
175 spin_unlock(&chip->controller->lock);
176#else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200177 /* De-select the NAND device */
178 chip->select_chip(mtd, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200179#endif
Wolfgang Denk8e9655f2005-11-02 14:29:12 +0100180}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200181
182/**
183 * nand_read_byte - [DEFAULT] read one byte from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000184 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200185 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200186 * Default read function for 8bit buswidth
Wolfgang Denk932394a2005-08-17 12:55:25 +0200187 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200188#ifndef __UBOOT__
189static uint8_t nand_read_byte(struct mtd_info *mtd)
190#else
Simon Schwarz82645f82011-10-31 06:34:44 +0000191uint8_t nand_read_byte(struct mtd_info *mtd)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200192#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200193{
William Juulcfa460a2007-10-31 13:53:06 +0100194 struct nand_chip *chip = mtd->priv;
195 return readb(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200196}
197
198/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200199 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000200 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
201 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200202 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000203 * Default read function for 16bit buswidth with endianness conversion.
204 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200205 */
William Juulcfa460a2007-10-31 13:53:06 +0100206static uint8_t nand_read_byte16(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200207{
William Juulcfa460a2007-10-31 13:53:06 +0100208 struct nand_chip *chip = mtd->priv;
209 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200210}
211
212/**
213 * nand_read_word - [DEFAULT] read one word from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000214 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200215 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000216 * Default read function for 16bit buswidth without endianness conversion.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200217 */
218static u16 nand_read_word(struct mtd_info *mtd)
219{
William Juulcfa460a2007-10-31 13:53:06 +0100220 struct nand_chip *chip = mtd->priv;
221 return readw(chip->IO_ADDR_R);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200222}
223
224/**
225 * nand_select_chip - [DEFAULT] control CE line
Sergey Lapindfe64e22013-01-14 03:46:50 +0000226 * @mtd: MTD device structure
227 * @chipnr: chipnumber to select, -1 for deselect
Wolfgang Denk932394a2005-08-17 12:55:25 +0200228 *
229 * Default select function for 1 chip devices.
230 */
William Juulcfa460a2007-10-31 13:53:06 +0100231static void nand_select_chip(struct mtd_info *mtd, int chipnr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200232{
William Juulcfa460a2007-10-31 13:53:06 +0100233 struct nand_chip *chip = mtd->priv;
234
235 switch (chipnr) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200236 case -1:
William Juulcfa460a2007-10-31 13:53:06 +0100237 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200238 break;
239 case 0:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200240 break;
241
242 default:
243 BUG();
244 }
245}
246
247/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200248 * nand_write_byte - [DEFAULT] write single byte to chip
249 * @mtd: MTD device structure
250 * @byte: value to write
251 *
252 * Default function to write a byte to I/O[7:0]
253 */
254static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
255{
256 struct nand_chip *chip = mtd->priv;
257
258 chip->write_buf(mtd, &byte, 1);
259}
260
261/**
262 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
263 * @mtd: MTD device structure
264 * @byte: value to write
265 *
266 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
267 */
268static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
269{
270 struct nand_chip *chip = mtd->priv;
271 uint16_t word = byte;
272
273 /*
274 * It's not entirely clear what should happen to I/O[15:8] when writing
275 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
276 *
277 * When the host supports a 16-bit bus width, only data is
278 * transferred at the 16-bit width. All address and command line
279 * transfers shall use only the lower 8-bits of the data bus. During
280 * command transfers, the host may place any value on the upper
281 * 8-bits of the data bus. During address transfers, the host shall
282 * set the upper 8-bits of the data bus to 00h.
283 *
284 * One user of the write_byte callback is nand_onfi_set_features. The
285 * four parameters are specified to be written to I/O[7:0], but this is
286 * neither an address nor a command transfer. Let's assume a 0 on the
287 * upper I/O lines is OK.
288 */
289 chip->write_buf(mtd, (uint8_t *)&word, 2);
290}
291
292#if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
293static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
294{
295 int i;
296
297 for (i = 0; i < len; i++)
298 writeb(buf[i], addr);
299}
300static void ioread8_rep(void *addr, uint8_t *buf, int len)
301{
302 int i;
303
304 for (i = 0; i < len; i++)
305 buf[i] = readb(addr);
306}
307
308static void ioread16_rep(void *addr, void *buf, int len)
309{
310 int i;
311 u16 *p = (u16 *) buf;
312 len >>= 1;
313
314 for (i = 0; i < len; i++)
315 p[i] = readw(addr);
316}
317
318static void iowrite16_rep(void *addr, void *buf, int len)
319{
320 int i;
321 u16 *p = (u16 *) buf;
322 len >>= 1;
323
324 for (i = 0; i < len; i++)
325 writew(p[i], addr);
326}
327#endif
328
329/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200330 * nand_write_buf - [DEFAULT] write buffer to chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000331 * @mtd: MTD device structure
332 * @buf: data buffer
333 * @len: number of bytes to write
Wolfgang Denk932394a2005-08-17 12:55:25 +0200334 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000335 * Default write function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200336 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200337#ifndef __UBOOT__
338static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
339#else
Simon Schwarz82645f82011-10-31 06:34:44 +0000340void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200341#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200342{
William Juulcfa460a2007-10-31 13:53:06 +0100343 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200344
Heiko Schocherff94bc42014-06-24 10:10:04 +0200345 iowrite8_rep(chip->IO_ADDR_W, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200346}
347
348/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200349 * nand_read_buf - [DEFAULT] read chip data into buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000350 * @mtd: MTD device structure
351 * @buf: buffer to store date
352 * @len: number of bytes to read
Wolfgang Denk932394a2005-08-17 12:55:25 +0200353 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000354 * Default read function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200355 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200356#ifndef __UBOOT__
357static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
358#else
Simon Schwarz12c2f1e2011-09-14 15:30:16 -0400359void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200360#endif
Wolfgang Denk932394a2005-08-17 12:55:25 +0200361{
William Juulcfa460a2007-10-31 13:53:06 +0100362 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200363
Heiko Schocherff94bc42014-06-24 10:10:04 +0200364 ioread8_rep(chip->IO_ADDR_R, buf, len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200365}
366
Heiko Schocherff94bc42014-06-24 10:10:04 +0200367#ifdef __UBOOT__
368#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200369/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200370 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000371 * @mtd: MTD device structure
372 * @buf: buffer containing the data to compare
373 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200374 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000375 * Default verify function for 8bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200376 */
William Juulcfa460a2007-10-31 13:53:06 +0100377static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200378{
379 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100380 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200381
William Juulcfa460a2007-10-31 13:53:06 +0100382 for (i = 0; i < len; i++)
383 if (buf[i] != readb(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200384 return -EFAULT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200385 return 0;
386}
387
388/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200389 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
Sergey Lapindfe64e22013-01-14 03:46:50 +0000390 * @mtd: MTD device structure
391 * @buf: buffer containing the data to compare
392 * @len: number of bytes to compare
Wolfgang Denk932394a2005-08-17 12:55:25 +0200393 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000394 * Default verify function for 16bit buswidth.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200395 */
William Juulcfa460a2007-10-31 13:53:06 +0100396static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200397{
398 int i;
William Juulcfa460a2007-10-31 13:53:06 +0100399 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200400 u16 *p = (u16 *) buf;
401 len >>= 1;
402
William Juulcfa460a2007-10-31 13:53:06 +0100403 for (i = 0; i < len; i++)
404 if (p[i] != readw(chip->IO_ADDR_R))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200405 return -EFAULT;
406
407 return 0;
408}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200409#endif
410#endif
411
412/**
413 * nand_write_buf16 - [DEFAULT] write buffer to chip
414 * @mtd: MTD device structure
415 * @buf: data buffer
416 * @len: number of bytes to write
417 *
418 * Default write function for 16bit buswidth.
419 */
420#ifndef __UBOOT__
421static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
422#else
423void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
424#endif
425{
426 struct nand_chip *chip = mtd->priv;
427 u16 *p = (u16 *) buf;
428
429 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
430}
431
432/**
433 * nand_read_buf16 - [DEFAULT] read chip data into buffer
434 * @mtd: MTD device structure
435 * @buf: buffer to store date
436 * @len: number of bytes to read
437 *
438 * Default read function for 16bit buswidth.
439 */
440#ifndef __UBOOT__
441static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
442#else
443void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
444#endif
445{
446 struct nand_chip *chip = mtd->priv;
447 u16 *p = (u16 *) buf;
448
449 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
450}
Wolfgang Denk932394a2005-08-17 12:55:25 +0200451
452/**
453 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
Sergey Lapindfe64e22013-01-14 03:46:50 +0000454 * @mtd: MTD device structure
455 * @ofs: offset from device start
456 * @getchip: 0, if the chip is already selected
Wolfgang Denk932394a2005-08-17 12:55:25 +0200457 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200458 * Check, if the block is bad.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200459 */
460static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
461{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000462 int page, chipnr, res = 0, i = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100463 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200464 u16 bad;
465
Sergey Lapindfe64e22013-01-14 03:46:50 +0000466 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200467 ofs += mtd->erasesize - mtd->writesize;
468
William Juulcfa460a2007-10-31 13:53:06 +0100469 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
Thomas Knoblocha7988652007-05-05 07:04:42 +0200470
Wolfgang Denk932394a2005-08-17 12:55:25 +0200471 if (getchip) {
William Juulcfa460a2007-10-31 13:53:06 +0100472 chipnr = (int)(ofs >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200473
Heiko Schocherff94bc42014-06-24 10:10:04 +0200474 nand_get_device(mtd, FL_READING);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200475
476 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +0100477 chip->select_chip(mtd, chipnr);
Thomas Knoblocha7988652007-05-05 07:04:42 +0200478 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200479
Sergey Lapindfe64e22013-01-14 03:46:50 +0000480 do {
481 if (chip->options & NAND_BUSWIDTH_16) {
482 chip->cmdfunc(mtd, NAND_CMD_READOOB,
483 chip->badblockpos & 0xFE, page);
484 bad = cpu_to_le16(chip->read_word(mtd));
485 if (chip->badblockpos & 0x1)
486 bad >>= 8;
487 else
488 bad &= 0xFF;
489 } else {
490 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
491 page);
492 bad = chip->read_byte(mtd);
493 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200494
Sergey Lapindfe64e22013-01-14 03:46:50 +0000495 if (likely(chip->badblockbits == 8))
496 res = bad != 0xFF;
497 else
498 res = hweight8(bad) < chip->badblockbits;
499 ofs += mtd->writesize;
500 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
501 i++;
502 } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200503
Heiko Schocherff94bc42014-06-24 10:10:04 +0200504 if (getchip) {
505 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200506 nand_release_device(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200507 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200508
Wolfgang Denk932394a2005-08-17 12:55:25 +0200509 return res;
510}
511
512/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200513 * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
Sergey Lapindfe64e22013-01-14 03:46:50 +0000514 * @mtd: MTD device structure
515 * @ofs: offset from device start
Wolfgang Denk932394a2005-08-17 12:55:25 +0200516 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000517 * This is the default implementation, which can be overridden by a hardware
Heiko Schocherff94bc42014-06-24 10:10:04 +0200518 * specific driver. It provides the details for writing a bad block marker to a
519 * block.
520 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200521static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
522{
William Juulcfa460a2007-10-31 13:53:06 +0100523 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200524 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +0100525 uint8_t buf[2] = { 0, 0 };
Heiko Schocherff94bc42014-06-24 10:10:04 +0200526 int ret = 0, res, i = 0;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200527
Heiko Schocherff94bc42014-06-24 10:10:04 +0200528 ops.datbuf = NULL;
529 ops.oobbuf = buf;
530 ops.ooboffs = chip->badblockpos;
531 if (chip->options & NAND_BUSWIDTH_16) {
532 ops.ooboffs &= ~0x01;
533 ops.len = ops.ooblen = 2;
534 } else {
535 ops.len = ops.ooblen = 1;
536 }
537 ops.mode = MTD_OPS_PLACE_OOB;
538
539 /* Write to first/last page(s) if necessary */
540 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
541 ofs += mtd->erasesize - mtd->writesize;
542 do {
543 res = nand_do_write_oob(mtd, ofs, &ops);
544 if (!ret)
545 ret = res;
546
547 i++;
548 ofs += mtd->writesize;
549 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
550
551 return ret;
552}
553
554/**
555 * nand_block_markbad_lowlevel - mark a block bad
556 * @mtd: MTD device structure
557 * @ofs: offset from device start
558 *
559 * This function performs the generic NAND bad block marking steps (i.e., bad
560 * block table(s) and/or marker(s)). We only allow the hardware driver to
561 * specify how to write bad block markers to OOB (chip->block_markbad).
562 *
563 * We try operations in the following order:
564 * (1) erase the affected block, to allow OOB marker to be written cleanly
565 * (2) write bad block marker to OOB area of affected block (unless flag
566 * NAND_BBT_NO_OOB_BBM is present)
567 * (3) update the BBT
568 * Note that we retain the first error encountered in (2) or (3), finish the
569 * procedures, and dump the error in the end.
570*/
571static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
572{
573 struct nand_chip *chip = mtd->priv;
574 int res, ret = 0;
575
576 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000577 struct erase_info einfo;
578
579 /* Attempt erase before marking OOB */
580 memset(&einfo, 0, sizeof(einfo));
581 einfo.mtd = mtd;
582 einfo.addr = ofs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200583 einfo.len = 1ULL << chip->phys_erase_shift;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000584 nand_erase_nand(mtd, &einfo, 0);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200585
Heiko Schocherff94bc42014-06-24 10:10:04 +0200586 /* Write bad block marker to OOB */
587 nand_get_device(mtd, FL_WRITING);
588 ret = chip->block_markbad(mtd, ofs);
Scott Woodc45912d2008-10-24 16:20:43 -0500589 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +0100590 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000591
Heiko Schocherff94bc42014-06-24 10:10:04 +0200592 /* Mark block bad in BBT */
593 if (chip->bbt) {
594 res = nand_markbad_bbt(mtd, ofs);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000595 if (!ret)
596 ret = res;
597 }
598
William Juulcfa460a2007-10-31 13:53:06 +0100599 if (!ret)
600 mtd->ecc_stats.badblocks++;
Scott Woodc45912d2008-10-24 16:20:43 -0500601
William Juulcfa460a2007-10-31 13:53:06 +0100602 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200603}
604
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200605/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200606 * nand_check_wp - [GENERIC] check if the chip is write protected
Sergey Lapindfe64e22013-01-14 03:46:50 +0000607 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +0200608 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000609 * Check, if the device is write protected. The function expects, that the
610 * device is already selected.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200611 */
William Juulcfa460a2007-10-31 13:53:06 +0100612static int nand_check_wp(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200613{
William Juulcfa460a2007-10-31 13:53:06 +0100614 struct nand_chip *chip = mtd->priv;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200615
Sergey Lapindfe64e22013-01-14 03:46:50 +0000616 /* Broken xD cards report WP despite being writable */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200617 if (chip->options & NAND_BROKEN_XD)
618 return 0;
619
Wolfgang Denk932394a2005-08-17 12:55:25 +0200620 /* Check the WP bit */
William Juulcfa460a2007-10-31 13:53:06 +0100621 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
622 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200623}
Markus Klotzbücher43638c62006-03-06 15:04:25 +0100624
Wolfgang Denk932394a2005-08-17 12:55:25 +0200625/**
626 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
Sergey Lapindfe64e22013-01-14 03:46:50 +0000627 * @mtd: MTD device structure
628 * @ofs: offset from device start
629 * @getchip: 0, if the chip is already selected
630 * @allowbbt: 1, if its allowed to access the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +0200631 *
632 * Check, if the block is bad. Either by reading the bad block table or
633 * calling of the scan function.
634 */
William Juulcfa460a2007-10-31 13:53:06 +0100635static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
636 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200637{
William Juulcfa460a2007-10-31 13:53:06 +0100638 struct nand_chip *chip = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200639
William Juulcfa460a2007-10-31 13:53:06 +0100640 if (!chip->bbt)
641 return chip->block_bad(mtd, ofs, getchip);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200642
Wolfgang Denk932394a2005-08-17 12:55:25 +0200643 /* Return info from the table */
William Juulcfa460a2007-10-31 13:53:06 +0100644 return nand_isbad_bbt(mtd, ofs, allowbbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200645}
646
Heiko Schocherff94bc42014-06-24 10:10:04 +0200647#ifndef __UBOOT__
648/**
649 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
650 * @mtd: MTD device structure
651 * @timeo: Timeout
652 *
653 * Helper function for nand_wait_ready used when needing to wait in interrupt
654 * context.
655 */
656static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
657{
658 struct nand_chip *chip = mtd->priv;
659 int i;
660
661 /* Wait for the device to get ready */
662 for (i = 0; i < timeo; i++) {
663 if (chip->dev_ready(mtd))
664 break;
665 touch_softlockup_watchdog();
666 mdelay(1);
667 }
668}
669#endif
670
Sergey Lapindfe64e22013-01-14 03:46:50 +0000671/* Wait for the ready pin, after a command. The timeout is caught later. */
William Juulcfa460a2007-10-31 13:53:06 +0100672void nand_wait_ready(struct mtd_info *mtd)
673{
674 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200675#ifndef __UBOOT__
676 unsigned long timeo = jiffies + msecs_to_jiffies(20);
677
678 /* 400ms timeout */
679 if (in_interrupt() || oops_in_progress)
680 return panic_nand_wait_ready(mtd, 400);
681
682 led_trigger_event(nand_led_trigger, LED_FULL);
683 /* Wait until command is processed or timeout occurs */
684 do {
685 if (chip->dev_ready(mtd))
686 break;
687 touch_softlockup_watchdog();
688 } while (time_before(jiffies, timeo));
689 led_trigger_event(nand_led_trigger, LED_OFF);
690#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200691 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000692 u32 time_start;
Stefan Roese12072262008-01-05 16:43:25 +0100693
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000694 time_start = get_timer(0);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000695 /* Wait until command is processed or timeout occurs */
Reinhard Meyer7a8fc362010-11-18 03:14:26 +0000696 while (get_timer(time_start) < timeo) {
Stefan Roese12072262008-01-05 16:43:25 +0100697 if (chip->dev_ready)
698 if (chip->dev_ready(mtd))
699 break;
700 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200701#endif
William Juulcfa460a2007-10-31 13:53:06 +0100702}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200703EXPORT_SYMBOL_GPL(nand_wait_ready);
William Juulcfa460a2007-10-31 13:53:06 +0100704
Wolfgang Denk932394a2005-08-17 12:55:25 +0200705/**
706 * nand_command - [DEFAULT] Send command to NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000707 * @mtd: MTD device structure
708 * @command: the command to be sent
709 * @column: the column address for this command, -1 if none
710 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200711 *
Sergey Lapindfe64e22013-01-14 03:46:50 +0000712 * Send command to NAND device. This function is used for small page devices
Heiko Schocherff94bc42014-06-24 10:10:04 +0200713 * (512 Bytes per page).
Wolfgang Denk932394a2005-08-17 12:55:25 +0200714 */
William Juulcfa460a2007-10-31 13:53:06 +0100715static void nand_command(struct mtd_info *mtd, unsigned int command,
716 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200717{
William Juulcfa460a2007-10-31 13:53:06 +0100718 register struct nand_chip *chip = mtd->priv;
719 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
Peter Tyser8da60122009-02-04 13:47:22 -0600720 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200721
Sergey Lapindfe64e22013-01-14 03:46:50 +0000722 /* Write out the command to the device */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200723 if (command == NAND_CMD_SEQIN) {
724 int readcmd;
725
William Juulcfa460a2007-10-31 13:53:06 +0100726 if (column >= mtd->writesize) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200727 /* OOB area */
William Juulcfa460a2007-10-31 13:53:06 +0100728 column -= mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200729 readcmd = NAND_CMD_READOOB;
730 } else if (column < 256) {
731 /* First 256 bytes --> READ0 */
732 readcmd = NAND_CMD_READ0;
733 } else {
734 column -= 256;
735 readcmd = NAND_CMD_READ1;
736 }
William Juulcfa460a2007-10-31 13:53:06 +0100737 chip->cmd_ctrl(mtd, readcmd, ctrl);
738 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200739 }
William Juulcfa460a2007-10-31 13:53:06 +0100740 chip->cmd_ctrl(mtd, command, ctrl);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200741
Sergey Lapindfe64e22013-01-14 03:46:50 +0000742 /* Address cycle, when necessary */
William Juulcfa460a2007-10-31 13:53:06 +0100743 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
744 /* Serially input address */
745 if (column != -1) {
746 /* Adjust columns for 16 bit buswidth */
Brian Norris27ce9e42014-05-06 00:46:17 +0530747 if ((chip->options & NAND_BUSWIDTH_16) &&
748 !nand_opcode_8bits(command))
William Juulcfa460a2007-10-31 13:53:06 +0100749 column >>= 1;
750 chip->cmd_ctrl(mtd, column, ctrl);
751 ctrl &= ~NAND_CTRL_CHANGE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200752 }
William Juulcfa460a2007-10-31 13:53:06 +0100753 if (page_addr != -1) {
754 chip->cmd_ctrl(mtd, page_addr, ctrl);
755 ctrl &= ~NAND_CTRL_CHANGE;
756 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
757 /* One more address cycle for devices > 32MiB */
758 if (chip->chipsize > (32 << 20))
759 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
760 }
761 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200762
763 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000764 * Program and erase have their own busy handlers status and sequential
765 * in needs no delay
William Juulcfa460a2007-10-31 13:53:06 +0100766 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200767 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200768
Wolfgang Denk932394a2005-08-17 12:55:25 +0200769 case NAND_CMD_PAGEPROG:
770 case NAND_CMD_ERASE1:
771 case NAND_CMD_ERASE2:
772 case NAND_CMD_SEQIN:
773 case NAND_CMD_STATUS:
774 return;
775
776 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100777 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200778 break;
William Juulcfa460a2007-10-31 13:53:06 +0100779 udelay(chip->chip_delay);
780 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
781 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
782 chip->cmd_ctrl(mtd,
783 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600784 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
785 (rst_sts_cnt--));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200786 return;
787
William Juulcfa460a2007-10-31 13:53:06 +0100788 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200789 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200790 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200791 * If we don't have access to the busy pin, we apply the given
792 * command delay
William Juulcfa460a2007-10-31 13:53:06 +0100793 */
794 if (!chip->dev_ready) {
795 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200796 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200797 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200798 }
Sergey Lapindfe64e22013-01-14 03:46:50 +0000799 /*
800 * Apply this short delay always to ensure that we do wait tWB in
801 * any case on any machine.
802 */
William Juulcfa460a2007-10-31 13:53:06 +0100803 ndelay(100);
804
805 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200806}
807
808/**
809 * nand_command_lp - [DEFAULT] Send command to NAND large page device
Sergey Lapindfe64e22013-01-14 03:46:50 +0000810 * @mtd: MTD device structure
811 * @command: the command to be sent
812 * @column: the column address for this command, -1 if none
813 * @page_addr: the page address for this command, -1 if none
Wolfgang Denk932394a2005-08-17 12:55:25 +0200814 *
William Juulcfa460a2007-10-31 13:53:06 +0100815 * Send command to NAND device. This is the version for the new large page
Sergey Lapindfe64e22013-01-14 03:46:50 +0000816 * devices. We don't have the separate regions as we have in the small page
817 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200818 */
William Juulcfa460a2007-10-31 13:53:06 +0100819static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
820 int column, int page_addr)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200821{
William Juulcfa460a2007-10-31 13:53:06 +0100822 register struct nand_chip *chip = mtd->priv;
Peter Tyser8da60122009-02-04 13:47:22 -0600823 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200824
825 /* Emulate NAND_CMD_READOOB */
826 if (command == NAND_CMD_READOOB) {
William Juulcfa460a2007-10-31 13:53:06 +0100827 column += mtd->writesize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200828 command = NAND_CMD_READ0;
829 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200830
William Juulcfa460a2007-10-31 13:53:06 +0100831 /* Command latch cycle */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200832 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200833
834 if (column != -1 || page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100835 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200836
837 /* Serially input address */
838 if (column != -1) {
839 /* Adjust columns for 16 bit buswidth */
Brian Norris27ce9e42014-05-06 00:46:17 +0530840 if ((chip->options & NAND_BUSWIDTH_16) &&
841 !nand_opcode_8bits(command))
Wolfgang Denk932394a2005-08-17 12:55:25 +0200842 column >>= 1;
William Juulcfa460a2007-10-31 13:53:06 +0100843 chip->cmd_ctrl(mtd, column, ctrl);
844 ctrl &= ~NAND_CTRL_CHANGE;
845 chip->cmd_ctrl(mtd, column >> 8, ctrl);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200846 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200847 if (page_addr != -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100848 chip->cmd_ctrl(mtd, page_addr, ctrl);
849 chip->cmd_ctrl(mtd, page_addr >> 8,
850 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200851 /* One more address cycle for devices > 128MiB */
William Juulcfa460a2007-10-31 13:53:06 +0100852 if (chip->chipsize > (128 << 20))
853 chip->cmd_ctrl(mtd, page_addr >> 16,
854 NAND_NCE | NAND_ALE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200855 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200856 }
William Juulcfa460a2007-10-31 13:53:06 +0100857 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200858
859 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +0000860 * Program and erase have their own busy handlers status, sequential
861 * in, and deplete1 need no delay.
William Juulcfa460a2007-10-31 13:53:06 +0100862 */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200863 switch (command) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200864
Wolfgang Denk932394a2005-08-17 12:55:25 +0200865 case NAND_CMD_CACHEDPROG:
866 case NAND_CMD_PAGEPROG:
867 case NAND_CMD_ERASE1:
868 case NAND_CMD_ERASE2:
869 case NAND_CMD_SEQIN:
William Juulcfa460a2007-10-31 13:53:06 +0100870 case NAND_CMD_RNDIN:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200871 case NAND_CMD_STATUS:
William Juulcfa460a2007-10-31 13:53:06 +0100872 return;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200873
874 case NAND_CMD_RESET:
William Juulcfa460a2007-10-31 13:53:06 +0100875 if (chip->dev_ready)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200876 break;
William Juulcfa460a2007-10-31 13:53:06 +0100877 udelay(chip->chip_delay);
878 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
879 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
880 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
881 NAND_NCE | NAND_CTRL_CHANGE);
Peter Tyser8da60122009-02-04 13:47:22 -0600882 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
883 (rst_sts_cnt--));
William Juulcfa460a2007-10-31 13:53:06 +0100884 return;
885
886 case NAND_CMD_RNDOUT:
887 /* No ready / busy check necessary */
888 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
889 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
890 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
891 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200892 return;
893
894 case NAND_CMD_READ0:
William Juulcfa460a2007-10-31 13:53:06 +0100895 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
896 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
897 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
898 NAND_NCE | NAND_CTRL_CHANGE);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200899
William Juulcfa460a2007-10-31 13:53:06 +0100900 /* This applies to read commands */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200901 default:
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200902 /*
Wolfgang Denk932394a2005-08-17 12:55:25 +0200903 * If we don't have access to the busy pin, we apply the given
Sergey Lapindfe64e22013-01-14 03:46:50 +0000904 * command delay.
William Juulcfa460a2007-10-31 13:53:06 +0100905 */
906 if (!chip->dev_ready) {
907 udelay(chip->chip_delay);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200908 return;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200909 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200910 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200911
Sergey Lapindfe64e22013-01-14 03:46:50 +0000912 /*
913 * Apply this short delay always to ensure that we do wait tWB in
914 * any case on any machine.
915 */
William Juulcfa460a2007-10-31 13:53:06 +0100916 ndelay(100);
917
918 nand_wait_ready(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200919}
920
921/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200922 * panic_nand_get_device - [GENERIC] Get chip for selected access
Sergey Lapindfe64e22013-01-14 03:46:50 +0000923 * @chip: the nand chip descriptor
924 * @mtd: MTD device structure
925 * @new_state: the state which is requested
Wolfgang Denk932394a2005-08-17 12:55:25 +0200926 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200927 * Used when in panic, no locks are taken.
928 */
929static void panic_nand_get_device(struct nand_chip *chip,
930 struct mtd_info *mtd, int new_state)
931{
932 /* Hardware controller shared among independent devices */
933 chip->controller->active = chip;
934 chip->state = new_state;
935}
936
937/**
938 * nand_get_device - [GENERIC] Get chip for selected access
939 * @mtd: MTD device structure
940 * @new_state: the state which is requested
941 *
Wolfgang Denk932394a2005-08-17 12:55:25 +0200942 * Get the device and lock it for exclusive access
943 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200944static int
Heiko Schocherff94bc42014-06-24 10:10:04 +0200945nand_get_device(struct mtd_info *mtd, int new_state)
William Juulcfa460a2007-10-31 13:53:06 +0100946{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200947 struct nand_chip *chip = mtd->priv;
948#ifndef __UBOOT__
949 spinlock_t *lock = &chip->controller->lock;
950 wait_queue_head_t *wq = &chip->controller->wq;
951 DECLARE_WAITQUEUE(wait, current);
952retry:
953 spin_lock(lock);
954
955 /* Hardware controller shared among independent devices */
956 if (!chip->controller->active)
957 chip->controller->active = chip;
958
959 if (chip->controller->active == chip && chip->state == FL_READY) {
960 chip->state = new_state;
961 spin_unlock(lock);
962 return 0;
963 }
964 if (new_state == FL_PM_SUSPENDED) {
965 if (chip->controller->active->state == FL_PM_SUSPENDED) {
966 chip->state = FL_PM_SUSPENDED;
967 spin_unlock(lock);
968 return 0;
969 }
970 }
971 set_current_state(TASK_UNINTERRUPTIBLE);
972 add_wait_queue(wq, &wait);
973 spin_unlock(lock);
974 schedule();
975 remove_wait_queue(wq, &wait);
976 goto retry;
977#else
Christian Hitz2a8e0fc2011-10-12 09:32:02 +0200978 chip->state = new_state;
William Juulcfa460a2007-10-31 13:53:06 +0100979 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200980#endif
981}
982
983/**
984 * panic_nand_wait - [GENERIC] wait until the command is done
985 * @mtd: MTD device structure
986 * @chip: NAND chip structure
987 * @timeo: timeout
988 *
989 * Wait for command done. This is a helper function for nand_wait used when
990 * we are in interrupt context. May happen when in panic and trying to write
991 * an oops through mtdoops.
992 */
993static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
994 unsigned long timeo)
995{
996 int i;
997 for (i = 0; i < timeo; i++) {
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 mdelay(1);
1006 }
William Juulcfa460a2007-10-31 13:53:06 +01001007}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001008
1009/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001010 * nand_wait - [DEFAULT] wait until the command is done
1011 * @mtd: MTD device structure
1012 * @chip: NAND chip structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02001013 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001014 * Wait for command done. This applies to erase and program only. Erase can
1015 * take up to 400ms and program up to 20ms according to general NAND and
1016 * SmartMedia specs.
William Juulcfa460a2007-10-31 13:53:06 +01001017 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001018static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001019{
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001020
Heiko Schocherff94bc42014-06-24 10:10:04 +02001021 int status, state = chip->state;
1022 unsigned long timeo = (state == FL_ERASING ? 400 : 20);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001023
Heiko Schocherff94bc42014-06-24 10:10:04 +02001024 led_trigger_event(nand_led_trigger, LED_FULL);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001025
Heiko Schocherff94bc42014-06-24 10:10:04 +02001026 /*
1027 * Apply this short delay always to ensure that we do wait tWB in any
1028 * case on any machine.
1029 */
1030 ndelay(100);
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001031
Heiko Schocherff94bc42014-06-24 10:10:04 +02001032 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1033
1034#ifndef __UBOOT__
1035 if (in_interrupt() || oops_in_progress)
1036 panic_nand_wait(mtd, chip, timeo);
1037 else {
1038 timeo = jiffies + msecs_to_jiffies(timeo);
1039 while (time_before(jiffies, timeo)) {
1040 if (chip->dev_ready) {
1041 if (chip->dev_ready(mtd))
1042 break;
1043 } else {
1044 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1045 break;
1046 }
1047 cond_resched();
Stefan Roese15784862006-11-27 17:22:19 +01001048 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001049 }
1050#else
1051 u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
1052 u32 time_start;
1053
1054 time_start = get_timer(0);
1055 while (get_timer(time_start) < timer) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001056 if (chip->dev_ready) {
1057 if (chip->dev_ready(mtd))
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001058 break;
1059 } else {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001060 if (chip->read_byte(mtd) & NAND_STATUS_READY)
Wolfgang Denk8e9655f2005-11-02 14:29:12 +01001061 break;
1062 }
1063 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001064#endif
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001065#ifdef PPCHAMELON_NAND_TIMER_HACK
Reinhard Meyer7a8fc362010-11-18 03:14:26 +00001066 time_start = get_timer(0);
1067 while (get_timer(time_start) < 10)
1068 ;
Bartlomiej Siekaaddb2e12006-03-05 18:57:33 +01001069#endif /* PPCHAMELON_NAND_TIMER_HACK */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001070 led_trigger_event(nand_led_trigger, LED_OFF);
Bartlomiej Sieka038ccac2006-02-24 09:37:22 +01001071
Heiko Schocherff94bc42014-06-24 10:10:04 +02001072 status = (int)chip->read_byte(mtd);
1073 /* This can happen if in case of timeout or buggy dev_ready */
1074 WARN_ON(!(status & NAND_STATUS_READY));
1075 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001076}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001077
Heiko Schocherff94bc42014-06-24 10:10:04 +02001078#ifndef __UBOOT__
1079/**
1080 * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1081 * @mtd: mtd info
1082 * @ofs: offset to start unlock from
1083 * @len: length to unlock
1084 * @invert: when = 0, unlock the range of blocks within the lower and
1085 * upper boundary address
1086 * when = 1, unlock the range of blocks outside the boundaries
1087 * of the lower and upper boundary address
1088 *
1089 * Returs unlock status.
1090 */
1091static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1092 uint64_t len, int invert)
1093{
1094 int ret = 0;
1095 int status, page;
1096 struct nand_chip *chip = mtd->priv;
1097
1098 /* Submit address of first page to unlock */
1099 page = ofs >> chip->page_shift;
1100 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1101
1102 /* Submit address of last page to unlock */
1103 page = (ofs + len) >> chip->page_shift;
1104 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1105 (page | invert) & chip->pagemask);
1106
1107 /* Call wait ready function */
1108 status = chip->waitfunc(mtd, chip);
1109 /* See if device thinks it succeeded */
1110 if (status & NAND_STATUS_FAIL) {
1111 pr_debug("%s: error status = 0x%08x\n",
1112 __func__, status);
1113 ret = -EIO;
1114 }
1115
1116 return ret;
1117}
1118
1119/**
1120 * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1121 * @mtd: mtd info
1122 * @ofs: offset to start unlock from
1123 * @len: length to unlock
1124 *
1125 * Returns unlock status.
1126 */
1127int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1128{
1129 int ret = 0;
1130 int chipnr;
1131 struct nand_chip *chip = mtd->priv;
1132
1133 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1134 __func__, (unsigned long long)ofs, len);
1135
1136 if (check_offs_len(mtd, ofs, len))
1137 ret = -EINVAL;
1138
1139 /* Align to last block address if size addresses end of the device */
1140 if (ofs + len == mtd->size)
1141 len -= mtd->erasesize;
1142
1143 nand_get_device(mtd, FL_UNLOCKING);
1144
1145 /* Shift to get chip number */
1146 chipnr = ofs >> chip->chip_shift;
1147
1148 chip->select_chip(mtd, chipnr);
1149
1150 /* Check, if it is write protected */
1151 if (nand_check_wp(mtd)) {
1152 pr_debug("%s: device is write protected!\n",
1153 __func__);
1154 ret = -EIO;
1155 goto out;
1156 }
1157
1158 ret = __nand_unlock(mtd, ofs, len, 0);
1159
1160out:
1161 chip->select_chip(mtd, -1);
1162 nand_release_device(mtd);
1163
1164 return ret;
1165}
1166EXPORT_SYMBOL(nand_unlock);
1167
1168/**
1169 * nand_lock - [REPLACEABLE] locks all blocks present in the device
1170 * @mtd: mtd info
1171 * @ofs: offset to start unlock from
1172 * @len: length to unlock
1173 *
1174 * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1175 * have this feature, but it allows only to lock all blocks, not for specified
1176 * range for block. Implementing 'lock' feature by making use of 'unlock', for
1177 * now.
1178 *
1179 * Returns lock status.
1180 */
1181int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1182{
1183 int ret = 0;
1184 int chipnr, status, page;
1185 struct nand_chip *chip = mtd->priv;
1186
1187 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1188 __func__, (unsigned long long)ofs, len);
1189
1190 if (check_offs_len(mtd, ofs, len))
1191 ret = -EINVAL;
1192
1193 nand_get_device(mtd, FL_LOCKING);
1194
1195 /* Shift to get chip number */
1196 chipnr = ofs >> chip->chip_shift;
1197
1198 chip->select_chip(mtd, chipnr);
1199
1200 /* Check, if it is write protected */
1201 if (nand_check_wp(mtd)) {
1202 pr_debug("%s: device is write protected!\n",
1203 __func__);
1204 status = MTD_ERASE_FAILED;
1205 ret = -EIO;
1206 goto out;
1207 }
1208
1209 /* Submit address of first page to lock */
1210 page = ofs >> chip->page_shift;
1211 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1212
1213 /* Call wait ready function */
1214 status = chip->waitfunc(mtd, chip);
1215 /* See if device thinks it succeeded */
1216 if (status & NAND_STATUS_FAIL) {
1217 pr_debug("%s: error status = 0x%08x\n",
1218 __func__, status);
1219 ret = -EIO;
1220 goto out;
1221 }
1222
1223 ret = __nand_unlock(mtd, ofs, len, 0x1);
1224
1225out:
1226 chip->select_chip(mtd, -1);
1227 nand_release_device(mtd);
1228
1229 return ret;
1230}
1231EXPORT_SYMBOL(nand_lock);
1232#endif
1233
Wolfgang Denk932394a2005-08-17 12:55:25 +02001234/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001235 * nand_read_page_raw - [INTERN] read raw page data without ecc
1236 * @mtd: mtd info structure
1237 * @chip: nand chip info structure
1238 * @buf: buffer to store read data
1239 * @oob_required: caller requires OOB data read to chip->oob_poi
1240 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001241 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001242 * Not for syndrome calculating ECC controllers, which use a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001243 */
William Juulcfa460a2007-10-31 13:53:06 +01001244static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001245 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001246{
William Juulcfa460a2007-10-31 13:53:06 +01001247 chip->read_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001248 if (oob_required)
1249 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01001250 return 0;
1251}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001252
William Juulcfa460a2007-10-31 13:53:06 +01001253/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001254 * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1255 * @mtd: mtd info structure
1256 * @chip: nand chip info structure
1257 * @buf: buffer to store read data
1258 * @oob_required: caller requires OOB data read to chip->oob_poi
1259 * @page: page number to read
David Brownell7e866612009-11-07 16:27:01 -05001260 *
1261 * We need a special oob layout and handling even when OOB isn't used.
1262 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001263static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001264 struct nand_chip *chip, uint8_t *buf,
1265 int oob_required, int page)
David Brownell7e866612009-11-07 16:27:01 -05001266{
1267 int eccsize = chip->ecc.size;
1268 int eccbytes = chip->ecc.bytes;
1269 uint8_t *oob = chip->oob_poi;
1270 int steps, size;
1271
1272 for (steps = chip->ecc.steps; steps > 0; steps--) {
1273 chip->read_buf(mtd, buf, eccsize);
1274 buf += eccsize;
1275
1276 if (chip->ecc.prepad) {
1277 chip->read_buf(mtd, oob, chip->ecc.prepad);
1278 oob += chip->ecc.prepad;
1279 }
1280
1281 chip->read_buf(mtd, oob, eccbytes);
1282 oob += eccbytes;
1283
1284 if (chip->ecc.postpad) {
1285 chip->read_buf(mtd, oob, chip->ecc.postpad);
1286 oob += chip->ecc.postpad;
1287 }
1288 }
1289
1290 size = mtd->oobsize - (oob - chip->oob_poi);
1291 if (size)
1292 chip->read_buf(mtd, oob, size);
1293
1294 return 0;
1295}
1296
1297/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001298 * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1299 * @mtd: mtd info structure
1300 * @chip: nand chip info structure
1301 * @buf: buffer to store read data
1302 * @oob_required: caller requires OOB data read to chip->oob_poi
1303 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001304 */
1305static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001306 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001307{
1308 int i, eccsize = chip->ecc.size;
1309 int eccbytes = chip->ecc.bytes;
1310 int eccsteps = chip->ecc.steps;
1311 uint8_t *p = buf;
1312 uint8_t *ecc_calc = chip->buffers->ecccalc;
1313 uint8_t *ecc_code = chip->buffers->ecccode;
1314 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001315 unsigned int max_bitflips = 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001316
Sergey Lapindfe64e22013-01-14 03:46:50 +00001317 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001318
William Juulcfa460a2007-10-31 13:53:06 +01001319 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1320 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001321
William Juulcfa460a2007-10-31 13:53:06 +01001322 for (i = 0; i < chip->ecc.total; i++)
1323 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001324
William Juulcfa460a2007-10-31 13:53:06 +01001325 eccsteps = chip->ecc.steps;
1326 p = buf;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001327
William Juulcfa460a2007-10-31 13:53:06 +01001328 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1329 int stat;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001330
William Juulcfa460a2007-10-31 13:53:06 +01001331 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001332 if (stat < 0) {
Scott Woodc45912d2008-10-24 16:20:43 -05001333 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001334 } else {
Scott Woodc45912d2008-10-24 16:20:43 -05001335 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001336 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1337 }
Scott Woodc45912d2008-10-24 16:20:43 -05001338 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001339 return max_bitflips;
Scott Woodc45912d2008-10-24 16:20:43 -05001340}
1341
1342/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001343 * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
Sergey Lapindfe64e22013-01-14 03:46:50 +00001344 * @mtd: mtd info structure
1345 * @chip: nand chip info structure
1346 * @data_offs: offset of requested data within the page
1347 * @readlen: data length
1348 * @bufpoi: buffer to store read data
Scott Woodc45912d2008-10-24 16:20:43 -05001349 */
Christian Hitz90e3f392011-10-12 09:32:01 +02001350static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1351 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
Scott Woodc45912d2008-10-24 16:20:43 -05001352{
1353 int start_step, end_step, num_steps;
1354 uint32_t *eccpos = chip->ecc.layout->eccpos;
1355 uint8_t *p;
1356 int data_col_addr, i, gaps = 0;
1357 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1358 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001359 int index = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001360 unsigned int max_bitflips = 0;
Scott Woodc45912d2008-10-24 16:20:43 -05001361
Sergey Lapindfe64e22013-01-14 03:46:50 +00001362 /* Column address within the page aligned to ECC size (256bytes) */
Scott Woodc45912d2008-10-24 16:20:43 -05001363 start_step = data_offs / chip->ecc.size;
1364 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1365 num_steps = end_step - start_step + 1;
1366
Sergey Lapindfe64e22013-01-14 03:46:50 +00001367 /* Data size aligned to ECC ecc.size */
Scott Woodc45912d2008-10-24 16:20:43 -05001368 datafrag_len = num_steps * chip->ecc.size;
1369 eccfrag_len = num_steps * chip->ecc.bytes;
1370
1371 data_col_addr = start_step * chip->ecc.size;
1372 /* If we read not a page aligned data */
1373 if (data_col_addr != 0)
1374 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1375
1376 p = bufpoi + data_col_addr;
1377 chip->read_buf(mtd, p, datafrag_len);
1378
Sergey Lapindfe64e22013-01-14 03:46:50 +00001379 /* Calculate ECC */
Scott Woodc45912d2008-10-24 16:20:43 -05001380 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1381 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1382
Sergey Lapindfe64e22013-01-14 03:46:50 +00001383 /*
1384 * The performance is faster if we position offsets according to
1385 * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1386 */
Scott Woodc45912d2008-10-24 16:20:43 -05001387 for (i = 0; i < eccfrag_len - 1; i++) {
1388 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1389 eccpos[i + start_step * chip->ecc.bytes + 1]) {
1390 gaps = 1;
1391 break;
1392 }
1393 }
1394 if (gaps) {
1395 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1396 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1397 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001398 /*
1399 * Send the command to read the particular ECC bytes take care
1400 * about buswidth alignment in read_buf.
1401 */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001402 index = start_step * chip->ecc.bytes;
1403
1404 aligned_pos = eccpos[index] & ~(busw - 1);
Scott Woodc45912d2008-10-24 16:20:43 -05001405 aligned_len = eccfrag_len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001406 if (eccpos[index] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001407 aligned_len++;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001408 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
Scott Woodc45912d2008-10-24 16:20:43 -05001409 aligned_len++;
1410
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001411 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1412 mtd->writesize + aligned_pos, -1);
Scott Woodc45912d2008-10-24 16:20:43 -05001413 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1414 }
1415
1416 for (i = 0; i < eccfrag_len; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001417 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
Scott Woodc45912d2008-10-24 16:20:43 -05001418
1419 p = bufpoi + data_col_addr;
1420 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1421 int stat;
1422
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001423 stat = chip->ecc.correct(mtd, p,
1424 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001425 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001426 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001427 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001428 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001429 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1430 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001431 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001432 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001433}
1434
Wolfgang Denk932394a2005-08-17 12:55:25 +02001435/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001436 * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1437 * @mtd: mtd info structure
1438 * @chip: nand chip info structure
1439 * @buf: buffer to store read data
1440 * @oob_required: caller requires OOB data read to chip->oob_poi
1441 * @page: page number to read
Wolfgang Denk932394a2005-08-17 12:55:25 +02001442 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001443 * Not for syndrome calculating ECC controllers which need a special oob layout.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001444 */
William Juulcfa460a2007-10-31 13:53:06 +01001445static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001446 uint8_t *buf, int oob_required, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001447{
William Juulcfa460a2007-10-31 13:53:06 +01001448 int i, eccsize = chip->ecc.size;
1449 int eccbytes = chip->ecc.bytes;
1450 int eccsteps = chip->ecc.steps;
1451 uint8_t *p = buf;
1452 uint8_t *ecc_calc = chip->buffers->ecccalc;
1453 uint8_t *ecc_code = chip->buffers->ecccode;
1454 uint32_t *eccpos = chip->ecc.layout->eccpos;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001455 unsigned int max_bitflips = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001456
William Juulcfa460a2007-10-31 13:53:06 +01001457 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1458 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1459 chip->read_buf(mtd, p, eccsize);
1460 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1461 }
1462 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001463
William Juulcfa460a2007-10-31 13:53:06 +01001464 for (i = 0; i < chip->ecc.total; i++)
1465 ecc_code[i] = chip->oob_poi[eccpos[i]];
Wolfgang Denk932394a2005-08-17 12:55:25 +02001466
William Juulcfa460a2007-10-31 13:53:06 +01001467 eccsteps = chip->ecc.steps;
1468 p = buf;
1469
1470 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1471 int stat;
1472
1473 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001474 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001475 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001476 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001477 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001478 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1479 }
William Juulcfa460a2007-10-31 13:53:06 +01001480 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001481 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001482}
1483
1484/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001485 * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1486 * @mtd: mtd info structure
1487 * @chip: nand chip info structure
1488 * @buf: buffer to store read data
1489 * @oob_required: caller requires OOB data read to chip->oob_poi
1490 * @page: page number to read
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001491 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001492 * Hardware ECC for large page chips, require OOB to be read first. For this
1493 * ECC mode, the write_page method is re-used from ECC_HW. These methods
1494 * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1495 * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1496 * the data area, by overwriting the NAND manufacturer bad block markings.
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001497 */
1498static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001499 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001500{
1501 int i, eccsize = chip->ecc.size;
1502 int eccbytes = chip->ecc.bytes;
1503 int eccsteps = chip->ecc.steps;
1504 uint8_t *p = buf;
1505 uint8_t *ecc_code = chip->buffers->ecccode;
1506 uint32_t *eccpos = chip->ecc.layout->eccpos;
1507 uint8_t *ecc_calc = chip->buffers->ecccalc;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001508 unsigned int max_bitflips = 0;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001509
1510 /* Read the OOB area first */
1511 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1512 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1513 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1514
1515 for (i = 0; i < chip->ecc.total; i++)
1516 ecc_code[i] = chip->oob_poi[eccpos[i]];
1517
1518 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1519 int stat;
1520
1521 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1522 chip->read_buf(mtd, p, eccsize);
1523 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1524
1525 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001526 if (stat < 0) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001527 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001528 } else {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001529 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001530 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1531 }
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001532 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001533 return max_bitflips;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04001534}
1535
1536/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001537 * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1538 * @mtd: mtd info structure
1539 * @chip: nand chip info structure
1540 * @buf: buffer to store read data
1541 * @oob_required: caller requires OOB data read to chip->oob_poi
1542 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001543 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001544 * The hw generator calculates the error syndrome automatically. Therefore we
1545 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01001546 */
1547static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001548 uint8_t *buf, int oob_required, int page)
William Juulcfa460a2007-10-31 13:53:06 +01001549{
1550 int i, eccsize = chip->ecc.size;
1551 int eccbytes = chip->ecc.bytes;
1552 int eccsteps = chip->ecc.steps;
1553 uint8_t *p = buf;
1554 uint8_t *oob = chip->oob_poi;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001555 unsigned int max_bitflips = 0;
William Juulcfa460a2007-10-31 13:53:06 +01001556
1557 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1558 int stat;
1559
1560 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1561 chip->read_buf(mtd, p, eccsize);
1562
1563 if (chip->ecc.prepad) {
1564 chip->read_buf(mtd, oob, chip->ecc.prepad);
1565 oob += chip->ecc.prepad;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001566 }
1567
William Juulcfa460a2007-10-31 13:53:06 +01001568 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1569 chip->read_buf(mtd, oob, eccbytes);
1570 stat = chip->ecc.correct(mtd, p, oob, NULL);
1571
Heiko Schocherff94bc42014-06-24 10:10:04 +02001572 if (stat < 0) {
William Juulcfa460a2007-10-31 13:53:06 +01001573 mtd->ecc_stats.failed++;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001574 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001575 mtd->ecc_stats.corrected += stat;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001576 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1577 }
William Juulcfa460a2007-10-31 13:53:06 +01001578
1579 oob += eccbytes;
1580
1581 if (chip->ecc.postpad) {
1582 chip->read_buf(mtd, oob, chip->ecc.postpad);
1583 oob += chip->ecc.postpad;
1584 }
1585 }
1586
1587 /* Calculate remaining oob bytes */
1588 i = mtd->oobsize - (oob - chip->oob_poi);
1589 if (i)
1590 chip->read_buf(mtd, oob, i);
1591
Heiko Schocherff94bc42014-06-24 10:10:04 +02001592 return max_bitflips;
William Juulcfa460a2007-10-31 13:53:06 +01001593}
1594
1595/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001596 * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1597 * @chip: nand chip structure
1598 * @oob: oob destination address
1599 * @ops: oob ops structure
1600 * @len: size of oob to transfer
William Juulcfa460a2007-10-31 13:53:06 +01001601 */
1602static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1603 struct mtd_oob_ops *ops, size_t len)
1604{
Christian Hitz90e3f392011-10-12 09:32:01 +02001605 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01001606
Sergey Lapindfe64e22013-01-14 03:46:50 +00001607 case MTD_OPS_PLACE_OOB:
1608 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01001609 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1610 return oob + len;
1611
Sergey Lapindfe64e22013-01-14 03:46:50 +00001612 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01001613 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1614 uint32_t boffs = 0, roffs = ops->ooboffs;
1615 size_t bytes = 0;
1616
Christian Hitz90e3f392011-10-12 09:32:01 +02001617 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001618 /* Read request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01001619 if (unlikely(roffs)) {
1620 if (roffs >= free->length) {
1621 roffs -= free->length;
1622 continue;
1623 }
1624 boffs = free->offset + roffs;
1625 bytes = min_t(size_t, len,
1626 (free->length - roffs));
1627 roffs = 0;
1628 } else {
1629 bytes = min_t(size_t, len, free->length);
1630 boffs = free->offset;
1631 }
1632 memcpy(oob, chip->oob_poi + boffs, bytes);
1633 oob += bytes;
1634 }
1635 return oob;
1636 }
1637 default:
1638 BUG();
1639 }
1640 return NULL;
1641}
1642
1643/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001644 * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1645 * @mtd: MTD device structure
1646 * @retry_mode: the retry mode to use
1647 *
1648 * Some vendors supply a special command to shift the Vt threshold, to be used
1649 * when there are too many bitflips in a page (i.e., ECC error). After setting
1650 * a new threshold, the host should retry reading the page.
1651 */
1652static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1653{
1654 struct nand_chip *chip = mtd->priv;
1655
1656 pr_debug("setting READ RETRY mode %d\n", retry_mode);
1657
1658 if (retry_mode >= chip->read_retries)
1659 return -EINVAL;
1660
1661 if (!chip->setup_read_retry)
1662 return -EOPNOTSUPP;
1663
1664 return chip->setup_read_retry(mtd, retry_mode);
1665}
1666
1667/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001668 * nand_do_read_ops - [INTERN] Read data with ECC
1669 * @mtd: MTD device structure
1670 * @from: offset to read from
1671 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01001672 *
1673 * Internal function. Called with chip held.
1674 */
1675static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1676 struct mtd_oob_ops *ops)
1677{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001678 int chipnr, page, realpage, col, bytes, aligned, oob_required;
William Juulcfa460a2007-10-31 13:53:06 +01001679 struct nand_chip *chip = mtd->priv;
William Juulcfa460a2007-10-31 13:53:06 +01001680 int ret = 0;
1681 uint32_t readlen = ops->len;
1682 uint32_t oobreadlen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001683 uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001684 mtd->oobavail : mtd->oobsize;
1685
William Juulcfa460a2007-10-31 13:53:06 +01001686 uint8_t *bufpoi, *oob, *buf;
Paul Burton40462e52013-09-04 15:16:56 +01001687 unsigned int max_bitflips = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001688 int retry_mode = 0;
1689 bool ecc_fail = false;
William Juulcfa460a2007-10-31 13:53:06 +01001690
1691 chipnr = (int)(from >> chip->chip_shift);
1692 chip->select_chip(mtd, chipnr);
1693
1694 realpage = (int)(from >> chip->page_shift);
1695 page = realpage & chip->pagemask;
1696
1697 col = (int)(from & (mtd->writesize - 1));
1698
1699 buf = ops->datbuf;
1700 oob = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001701 oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01001702
Christian Hitz90e3f392011-10-12 09:32:01 +02001703 while (1) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001704 unsigned int ecc_failures = mtd->ecc_stats.failed;
Scott Wood6f2ffc32011-02-02 18:15:57 -06001705
Heiko Schocherff94bc42014-06-24 10:10:04 +02001706 WATCHDOG_RESET();
William Juulcfa460a2007-10-31 13:53:06 +01001707 bytes = min(mtd->writesize - col, readlen);
1708 aligned = (bytes == mtd->writesize);
1709
Sergey Lapindfe64e22013-01-14 03:46:50 +00001710 /* Is the current page in the buffer? */
William Juulcfa460a2007-10-31 13:53:06 +01001711 if (realpage != chip->pagebuf || oob) {
1712 bufpoi = aligned ? buf : chip->buffers->databuf;
1713
Heiko Schocherff94bc42014-06-24 10:10:04 +02001714read_retry:
Sergey Lapindfe64e22013-01-14 03:46:50 +00001715 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
William Juulcfa460a2007-10-31 13:53:06 +01001716
Paul Burton40462e52013-09-04 15:16:56 +01001717 /*
1718 * Now read the page into the buffer. Absent an error,
1719 * the read methods return max bitflips per ecc step.
1720 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001721 if (unlikely(ops->mode == MTD_OPS_RAW))
1722 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1723 oob_required,
1724 page);
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001725 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001726 !oob)
Christian Hitz90e3f392011-10-12 09:32:01 +02001727 ret = chip->ecc.read_subpage(mtd, chip,
1728 col, bytes, bufpoi);
William Juulcfa460a2007-10-31 13:53:06 +01001729 else
Sandeep Paulraja2c65b42009-08-10 13:27:46 -04001730 ret = chip->ecc.read_page(mtd, chip, bufpoi,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001731 oob_required, page);
1732 if (ret < 0) {
1733 if (!aligned)
1734 /* Invalidate page cache */
1735 chip->pagebuf = -1;
William Juulcfa460a2007-10-31 13:53:06 +01001736 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00001737 }
William Juulcfa460a2007-10-31 13:53:06 +01001738
Paul Burton40462e52013-09-04 15:16:56 +01001739 max_bitflips = max_t(unsigned int, max_bitflips, ret);
1740
William Juulcfa460a2007-10-31 13:53:06 +01001741 /* Transfer not aligned data */
1742 if (!aligned) {
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00001743 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02001744 !(mtd->ecc_stats.failed - ecc_failures) &&
Paul Burton40462e52013-09-04 15:16:56 +01001745 (ops->mode != MTD_OPS_RAW)) {
Scott Woodc45912d2008-10-24 16:20:43 -05001746 chip->pagebuf = realpage;
Paul Burton40462e52013-09-04 15:16:56 +01001747 chip->pagebuf_bitflips = ret;
1748 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00001749 /* Invalidate page cache */
1750 chip->pagebuf = -1;
Paul Burton40462e52013-09-04 15:16:56 +01001751 }
William Juulcfa460a2007-10-31 13:53:06 +01001752 memcpy(buf, chip->buffers->databuf + col, bytes);
1753 }
1754
William Juulcfa460a2007-10-31 13:53:06 +01001755 if (unlikely(oob)) {
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02001756 int toread = min(oobreadlen, max_oobsize);
1757
1758 if (toread) {
1759 oob = nand_transfer_oob(chip,
1760 oob, ops, toread);
1761 oobreadlen -= toread;
1762 }
William Juulcfa460a2007-10-31 13:53:06 +01001763 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001764
1765 if (chip->options & NAND_NEED_READRDY) {
1766 /* Apply delay or wait for ready/busy pin */
1767 if (!chip->dev_ready)
1768 udelay(chip->chip_delay);
1769 else
1770 nand_wait_ready(mtd);
1771 }
1772
1773 if (mtd->ecc_stats.failed - ecc_failures) {
1774 if (retry_mode + 1 < chip->read_retries) {
1775 retry_mode++;
1776 ret = nand_setup_read_retry(mtd,
1777 retry_mode);
1778 if (ret < 0)
1779 break;
1780
1781 /* Reset failures; retry */
1782 mtd->ecc_stats.failed = ecc_failures;
1783 goto read_retry;
1784 } else {
1785 /* No more retry modes; real failure */
1786 ecc_fail = true;
1787 }
1788 }
1789
1790 buf += bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001791 } else {
William Juulcfa460a2007-10-31 13:53:06 +01001792 memcpy(buf, chip->buffers->databuf + col, bytes);
1793 buf += bytes;
Paul Burton40462e52013-09-04 15:16:56 +01001794 max_bitflips = max_t(unsigned int, max_bitflips,
1795 chip->pagebuf_bitflips);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001796 }
1797
William Juulcfa460a2007-10-31 13:53:06 +01001798 readlen -= bytes;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001799
Heiko Schocherff94bc42014-06-24 10:10:04 +02001800 /* Reset to retry mode 0 */
1801 if (retry_mode) {
1802 ret = nand_setup_read_retry(mtd, 0);
1803 if (ret < 0)
1804 break;
1805 retry_mode = 0;
1806 }
1807
William Juulcfa460a2007-10-31 13:53:06 +01001808 if (!readlen)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001809 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001810
Sergey Lapindfe64e22013-01-14 03:46:50 +00001811 /* For subsequent reads align to page boundary */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001812 col = 0;
1813 /* Increment page address */
1814 realpage++;
1815
William Juulcfa460a2007-10-31 13:53:06 +01001816 page = realpage & chip->pagemask;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001817 /* Check, if we cross a chip boundary */
1818 if (!page) {
1819 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01001820 chip->select_chip(mtd, -1);
1821 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001822 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001823 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001824 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001825
William Juulcfa460a2007-10-31 13:53:06 +01001826 ops->retlen = ops->len - (size_t) readlen;
1827 if (oob)
1828 ops->oobretlen = ops->ooblen - oobreadlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001829
Heiko Schocherff94bc42014-06-24 10:10:04 +02001830 if (ret < 0)
William Juulcfa460a2007-10-31 13:53:06 +01001831 return ret;
1832
Heiko Schocherff94bc42014-06-24 10:10:04 +02001833 if (ecc_fail)
William Juulcfa460a2007-10-31 13:53:06 +01001834 return -EBADMSG;
1835
Paul Burton40462e52013-09-04 15:16:56 +01001836 return max_bitflips;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001837}
1838
1839/**
Christian Hitz90e3f392011-10-12 09:32:01 +02001840 * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
Sergey Lapindfe64e22013-01-14 03:46:50 +00001841 * @mtd: MTD device structure
1842 * @from: offset to read from
1843 * @len: number of bytes to read
1844 * @retlen: pointer to variable to store the number of read bytes
1845 * @buf: the databuffer to put data
Wolfgang Denk932394a2005-08-17 12:55:25 +02001846 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00001847 * Get hold of the chip and call nand_do_read.
Wolfgang Denk932394a2005-08-17 12:55:25 +02001848 */
William Juulcfa460a2007-10-31 13:53:06 +01001849static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1850 size_t *retlen, uint8_t *buf)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001851{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001852 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01001853 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001854
Heiko Schocherff94bc42014-06-24 10:10:04 +02001855 nand_get_device(mtd, FL_READING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001856 ops.len = len;
1857 ops.datbuf = buf;
1858 ops.oobbuf = NULL;
1859 ops.mode = MTD_OPS_PLACE_OOB;
1860 ret = nand_do_read_ops(mtd, from, &ops);
1861 *retlen = ops.retlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001862 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001863 return ret;
1864}
1865
William Juulcfa460a2007-10-31 13:53:06 +01001866/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001867 * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1868 * @mtd: mtd info structure
1869 * @chip: nand chip info structure
1870 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001871 */
1872static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001873 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001874{
Sergey Lapindfe64e22013-01-14 03:46:50 +00001875 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
William Juulcfa460a2007-10-31 13:53:06 +01001876 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00001877 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001878}
Wolfgang Denk932394a2005-08-17 12:55:25 +02001879
1880/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001881 * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
William Juulcfa460a2007-10-31 13:53:06 +01001882 * with syndromes
Sergey Lapindfe64e22013-01-14 03:46:50 +00001883 * @mtd: mtd info structure
1884 * @chip: nand chip info structure
1885 * @page: page number to read
William Juulcfa460a2007-10-31 13:53:06 +01001886 */
1887static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00001888 int page)
William Juulcfa460a2007-10-31 13:53:06 +01001889{
1890 uint8_t *buf = chip->oob_poi;
1891 int length = mtd->oobsize;
1892 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1893 int eccsize = chip->ecc.size;
1894 uint8_t *bufpoi = buf;
1895 int i, toread, sndrnd = 0, pos;
1896
1897 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1898 for (i = 0; i < chip->ecc.steps; i++) {
1899 if (sndrnd) {
1900 pos = eccsize + i * (eccsize + chunk);
1901 if (mtd->writesize > 512)
1902 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1903 else
1904 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1905 } else
1906 sndrnd = 1;
1907 toread = min_t(int, length, chunk);
1908 chip->read_buf(mtd, bufpoi, toread);
1909 bufpoi += toread;
1910 length -= toread;
1911 }
1912 if (length > 0)
1913 chip->read_buf(mtd, bufpoi, length);
1914
Sergey Lapindfe64e22013-01-14 03:46:50 +00001915 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01001916}
1917
1918/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001919 * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1920 * @mtd: mtd info structure
1921 * @chip: nand chip info structure
1922 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001923 */
1924static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1925 int page)
1926{
1927 int status = 0;
1928 const uint8_t *buf = chip->oob_poi;
1929 int length = mtd->oobsize;
1930
1931 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1932 chip->write_buf(mtd, buf, length);
1933 /* Send command to program the OOB data */
1934 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1935
1936 status = chip->waitfunc(mtd, chip);
1937
1938 return status & NAND_STATUS_FAIL ? -EIO : 0;
1939}
1940
1941/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00001942 * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1943 * with syndrome - only for large page flash
1944 * @mtd: mtd info structure
1945 * @chip: nand chip info structure
1946 * @page: page number to write
William Juulcfa460a2007-10-31 13:53:06 +01001947 */
1948static int nand_write_oob_syndrome(struct mtd_info *mtd,
1949 struct nand_chip *chip, int page)
1950{
1951 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1952 int eccsize = chip->ecc.size, length = mtd->oobsize;
1953 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1954 const uint8_t *bufpoi = chip->oob_poi;
1955
1956 /*
1957 * data-ecc-data-ecc ... ecc-oob
1958 * or
1959 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1960 */
1961 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1962 pos = steps * (eccsize + chunk);
1963 steps = 0;
1964 } else
1965 pos = eccsize;
1966
1967 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1968 for (i = 0; i < steps; i++) {
1969 if (sndcmd) {
1970 if (mtd->writesize <= 512) {
1971 uint32_t fill = 0xFFFFFFFF;
1972
1973 len = eccsize;
1974 while (len > 0) {
1975 int num = min_t(int, len, 4);
1976 chip->write_buf(mtd, (uint8_t *)&fill,
1977 num);
1978 len -= num;
1979 }
1980 } else {
1981 pos = eccsize + i * (eccsize + chunk);
1982 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1983 }
1984 } else
1985 sndcmd = 1;
1986 len = min_t(int, length, chunk);
1987 chip->write_buf(mtd, bufpoi, len);
1988 bufpoi += len;
1989 length -= len;
1990 }
1991 if (length > 0)
1992 chip->write_buf(mtd, bufpoi, length);
1993
1994 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1995 status = chip->waitfunc(mtd, chip);
1996
1997 return status & NAND_STATUS_FAIL ? -EIO : 0;
1998}
1999
2000/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002001 * nand_do_read_oob - [INTERN] NAND read out-of-band
2002 * @mtd: MTD device structure
2003 * @from: offset to read from
2004 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002005 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002006 * NAND read out-of-band data from the spare area.
William Juulcfa460a2007-10-31 13:53:06 +01002007 */
2008static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2009 struct mtd_oob_ops *ops)
2010{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002011 int page, realpage, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002012 struct nand_chip *chip = mtd->priv;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002013 struct mtd_ecc_stats stats;
William Juulcfa460a2007-10-31 13:53:06 +01002014 int readlen = ops->ooblen;
2015 int len;
2016 uint8_t *buf = ops->oobbuf;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002017 int ret = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002018
Heiko Schocherff94bc42014-06-24 10:10:04 +02002019 pr_debug("%s: from = 0x%08Lx, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002020 __func__, (unsigned long long)from, readlen);
William Juulcfa460a2007-10-31 13:53:06 +01002021
Sergey Lapindfe64e22013-01-14 03:46:50 +00002022 stats = mtd->ecc_stats;
2023
2024 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002025 len = chip->ecc.layout->oobavail;
2026 else
2027 len = mtd->oobsize;
2028
2029 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002030 pr_debug("%s: attempt to start read outside oob\n",
2031 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002032 return -EINVAL;
2033 }
2034
2035 /* Do not allow reads past end of device */
2036 if (unlikely(from >= mtd->size ||
2037 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2038 (from >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002039 pr_debug("%s: attempt to read beyond end of device\n",
2040 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002041 return -EINVAL;
2042 }
2043
2044 chipnr = (int)(from >> chip->chip_shift);
2045 chip->select_chip(mtd, chipnr);
2046
2047 /* Shift to get page */
2048 realpage = (int)(from >> chip->page_shift);
2049 page = realpage & chip->pagemask;
2050
Christian Hitz90e3f392011-10-12 09:32:01 +02002051 while (1) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002052 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002053
Sergey Lapindfe64e22013-01-14 03:46:50 +00002054 if (ops->mode == MTD_OPS_RAW)
2055 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2056 else
2057 ret = chip->ecc.read_oob(mtd, chip, page);
2058
2059 if (ret < 0)
2060 break;
William Juulcfa460a2007-10-31 13:53:06 +01002061
2062 len = min(len, readlen);
2063 buf = nand_transfer_oob(chip, buf, ops, len);
2064
Heiko Schocherff94bc42014-06-24 10:10:04 +02002065 if (chip->options & NAND_NEED_READRDY) {
2066 /* Apply delay or wait for ready/busy pin */
2067 if (!chip->dev_ready)
2068 udelay(chip->chip_delay);
2069 else
2070 nand_wait_ready(mtd);
2071 }
2072
William Juulcfa460a2007-10-31 13:53:06 +01002073 readlen -= len;
2074 if (!readlen)
2075 break;
2076
2077 /* Increment page address */
2078 realpage++;
2079
2080 page = realpage & chip->pagemask;
2081 /* Check, if we cross a chip boundary */
2082 if (!page) {
2083 chipnr++;
2084 chip->select_chip(mtd, -1);
2085 chip->select_chip(mtd, chipnr);
2086 }
William Juulcfa460a2007-10-31 13:53:06 +01002087 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002088 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002089
Sergey Lapindfe64e22013-01-14 03:46:50 +00002090 ops->oobretlen = ops->ooblen - readlen;
2091
2092 if (ret < 0)
2093 return ret;
2094
2095 if (mtd->ecc_stats.failed - stats.failed)
2096 return -EBADMSG;
2097
2098 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002099}
2100
2101/**
2102 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002103 * @mtd: MTD device structure
2104 * @from: offset to read from
2105 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002106 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002107 * NAND read data and/or out-of-band data.
William Juulcfa460a2007-10-31 13:53:06 +01002108 */
2109static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2110 struct mtd_oob_ops *ops)
2111{
William Juulcfa460a2007-10-31 13:53:06 +01002112 int ret = -ENOTSUPP;
2113
2114 ops->retlen = 0;
2115
2116 /* Do not allow reads past end of device */
2117 if (ops->datbuf && (from + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002118 pr_debug("%s: attempt to read beyond end of device\n",
2119 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002120 return -EINVAL;
2121 }
2122
Heiko Schocherff94bc42014-06-24 10:10:04 +02002123 nand_get_device(mtd, FL_READING);
William Juulcfa460a2007-10-31 13:53:06 +01002124
Christian Hitz90e3f392011-10-12 09:32:01 +02002125 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002126 case MTD_OPS_PLACE_OOB:
2127 case MTD_OPS_AUTO_OOB:
2128 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002129 break;
2130
2131 default:
2132 goto out;
2133 }
2134
2135 if (!ops->datbuf)
2136 ret = nand_do_read_oob(mtd, from, ops);
2137 else
2138 ret = nand_do_read_ops(mtd, from, ops);
2139
Christian Hitz90e3f392011-10-12 09:32:01 +02002140out:
William Juulcfa460a2007-10-31 13:53:06 +01002141 nand_release_device(mtd);
2142 return ret;
2143}
2144
2145
2146/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002147 * nand_write_page_raw - [INTERN] raw page write function
2148 * @mtd: mtd info structure
2149 * @chip: nand chip info structure
2150 * @buf: data buffer
2151 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05002152 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002153 * Not for syndrome calculating ECC controllers, which use a special oob layout.
William Juulcfa460a2007-10-31 13:53:06 +01002154 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002155static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2156 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002157{
2158 chip->write_buf(mtd, buf, mtd->writesize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002159 if (oob_required)
2160 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2161
2162 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002163}
2164
2165/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002166 * nand_write_page_raw_syndrome - [INTERN] raw page write function
2167 * @mtd: mtd info structure
2168 * @chip: nand chip info structure
2169 * @buf: data buffer
2170 * @oob_required: must write chip->oob_poi to OOB
David Brownell7e866612009-11-07 16:27:01 -05002171 *
2172 * We need a special oob layout and handling even when ECC isn't checked.
2173 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002174static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
Christian Hitz90e3f392011-10-12 09:32:01 +02002175 struct nand_chip *chip,
Sergey Lapindfe64e22013-01-14 03:46:50 +00002176 const uint8_t *buf, int oob_required)
David Brownell7e866612009-11-07 16:27:01 -05002177{
2178 int eccsize = chip->ecc.size;
2179 int eccbytes = chip->ecc.bytes;
2180 uint8_t *oob = chip->oob_poi;
2181 int steps, size;
2182
2183 for (steps = chip->ecc.steps; steps > 0; steps--) {
2184 chip->write_buf(mtd, buf, eccsize);
2185 buf += eccsize;
2186
2187 if (chip->ecc.prepad) {
2188 chip->write_buf(mtd, oob, chip->ecc.prepad);
2189 oob += chip->ecc.prepad;
2190 }
2191
2192 chip->read_buf(mtd, oob, eccbytes);
2193 oob += eccbytes;
2194
2195 if (chip->ecc.postpad) {
2196 chip->write_buf(mtd, oob, chip->ecc.postpad);
2197 oob += chip->ecc.postpad;
2198 }
2199 }
2200
2201 size = mtd->oobsize - (oob - chip->oob_poi);
2202 if (size)
2203 chip->write_buf(mtd, oob, size);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002204
2205 return 0;
David Brownell7e866612009-11-07 16:27:01 -05002206}
2207/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002208 * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2209 * @mtd: mtd info structure
2210 * @chip: nand chip info structure
2211 * @buf: data buffer
2212 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002213 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002214static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2215 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002216{
2217 int i, eccsize = chip->ecc.size;
2218 int eccbytes = chip->ecc.bytes;
2219 int eccsteps = chip->ecc.steps;
2220 uint8_t *ecc_calc = chip->buffers->ecccalc;
2221 const uint8_t *p = buf;
2222 uint32_t *eccpos = chip->ecc.layout->eccpos;
2223
Sergey Lapindfe64e22013-01-14 03:46:50 +00002224 /* Software ECC calculation */
William Juulcfa460a2007-10-31 13:53:06 +01002225 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2226 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2227
2228 for (i = 0; i < chip->ecc.total; i++)
2229 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2230
Sergey Lapindfe64e22013-01-14 03:46:50 +00002231 return chip->ecc.write_page_raw(mtd, chip, buf, 1);
William Juulcfa460a2007-10-31 13:53:06 +01002232}
2233
2234/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002235 * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2236 * @mtd: mtd info structure
2237 * @chip: nand chip info structure
2238 * @buf: data buffer
2239 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002240 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002241static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2242 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002243{
2244 int i, eccsize = chip->ecc.size;
2245 int eccbytes = chip->ecc.bytes;
2246 int eccsteps = chip->ecc.steps;
2247 uint8_t *ecc_calc = chip->buffers->ecccalc;
2248 const uint8_t *p = buf;
2249 uint32_t *eccpos = chip->ecc.layout->eccpos;
2250
2251 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2252 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2253 chip->write_buf(mtd, p, eccsize);
2254 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2255 }
2256
2257 for (i = 0; i < chip->ecc.total; i++)
2258 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2259
2260 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002261
2262 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002263}
2264
Heiko Schocherff94bc42014-06-24 10:10:04 +02002265
2266/**
2267 * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2268 * @mtd: mtd info structure
2269 * @chip: nand chip info structure
2270 * @offset: column address of subpage within the page
2271 * @data_len: data length
2272 * @buf: data buffer
2273 * @oob_required: must write chip->oob_poi to OOB
2274 */
2275static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2276 struct nand_chip *chip, uint32_t offset,
2277 uint32_t data_len, const uint8_t *buf,
2278 int oob_required)
2279{
2280 uint8_t *oob_buf = chip->oob_poi;
2281 uint8_t *ecc_calc = chip->buffers->ecccalc;
2282 int ecc_size = chip->ecc.size;
2283 int ecc_bytes = chip->ecc.bytes;
2284 int ecc_steps = chip->ecc.steps;
2285 uint32_t *eccpos = chip->ecc.layout->eccpos;
2286 uint32_t start_step = offset / ecc_size;
2287 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2288 int oob_bytes = mtd->oobsize / ecc_steps;
2289 int step, i;
2290
2291 for (step = 0; step < ecc_steps; step++) {
2292 /* configure controller for WRITE access */
2293 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2294
2295 /* write data (untouched subpages already masked by 0xFF) */
2296 chip->write_buf(mtd, buf, ecc_size);
2297
2298 /* mask ECC of un-touched subpages by padding 0xFF */
2299 if ((step < start_step) || (step > end_step))
2300 memset(ecc_calc, 0xff, ecc_bytes);
2301 else
2302 chip->ecc.calculate(mtd, buf, ecc_calc);
2303
2304 /* mask OOB of un-touched subpages by padding 0xFF */
2305 /* if oob_required, preserve OOB metadata of written subpage */
2306 if (!oob_required || (step < start_step) || (step > end_step))
2307 memset(oob_buf, 0xff, oob_bytes);
2308
2309 buf += ecc_size;
2310 ecc_calc += ecc_bytes;
2311 oob_buf += oob_bytes;
2312 }
2313
2314 /* copy calculated ECC for whole page to chip->buffer->oob */
2315 /* this include masked-value(0xFF) for unwritten subpages */
2316 ecc_calc = chip->buffers->ecccalc;
2317 for (i = 0; i < chip->ecc.total; i++)
2318 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2319
2320 /* write OOB buffer to NAND device */
2321 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2322
2323 return 0;
2324}
2325
2326
William Juulcfa460a2007-10-31 13:53:06 +01002327/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002328 * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2329 * @mtd: mtd info structure
2330 * @chip: nand chip info structure
2331 * @buf: data buffer
2332 * @oob_required: must write chip->oob_poi to OOB
William Juulcfa460a2007-10-31 13:53:06 +01002333 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002334 * The hw generator calculates the error syndrome automatically. Therefore we
2335 * need a special oob layout and handling.
William Juulcfa460a2007-10-31 13:53:06 +01002336 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002337static int nand_write_page_syndrome(struct mtd_info *mtd,
2338 struct nand_chip *chip,
2339 const uint8_t *buf, int oob_required)
William Juulcfa460a2007-10-31 13:53:06 +01002340{
2341 int i, eccsize = chip->ecc.size;
2342 int eccbytes = chip->ecc.bytes;
2343 int eccsteps = chip->ecc.steps;
2344 const uint8_t *p = buf;
2345 uint8_t *oob = chip->oob_poi;
2346
2347 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2348
2349 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2350 chip->write_buf(mtd, p, eccsize);
2351
2352 if (chip->ecc.prepad) {
2353 chip->write_buf(mtd, oob, chip->ecc.prepad);
2354 oob += chip->ecc.prepad;
2355 }
2356
2357 chip->ecc.calculate(mtd, p, oob);
2358 chip->write_buf(mtd, oob, eccbytes);
2359 oob += eccbytes;
2360
2361 if (chip->ecc.postpad) {
2362 chip->write_buf(mtd, oob, chip->ecc.postpad);
2363 oob += chip->ecc.postpad;
2364 }
2365 }
2366
2367 /* Calculate remaining oob bytes */
2368 i = mtd->oobsize - (oob - chip->oob_poi);
2369 if (i)
2370 chip->write_buf(mtd, oob, i);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002371
2372 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01002373}
2374
2375/**
2376 * nand_write_page - [REPLACEABLE] write one page
Sergey Lapindfe64e22013-01-14 03:46:50 +00002377 * @mtd: MTD device structure
2378 * @chip: NAND chip descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +02002379 * @offset: address offset within the page
2380 * @data_len: length of actual data to be written
Sergey Lapindfe64e22013-01-14 03:46:50 +00002381 * @buf: the data to write
2382 * @oob_required: must write chip->oob_poi to OOB
2383 * @page: page number to write
2384 * @cached: cached programming
2385 * @raw: use _raw version of write_page
William Juulcfa460a2007-10-31 13:53:06 +01002386 */
2387static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02002388 uint32_t offset, int data_len, const uint8_t *buf,
2389 int oob_required, int page, int cached, int raw)
William Juulcfa460a2007-10-31 13:53:06 +01002390{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002391 int status, subpage;
2392
2393 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2394 chip->ecc.write_subpage)
2395 subpage = offset || (data_len < mtd->writesize);
2396 else
2397 subpage = 0;
William Juulcfa460a2007-10-31 13:53:06 +01002398
2399 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2400
2401 if (unlikely(raw))
Heiko Schocherff94bc42014-06-24 10:10:04 +02002402 status = chip->ecc.write_page_raw(mtd, chip, buf,
2403 oob_required);
2404 else if (subpage)
2405 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2406 buf, oob_required);
William Juulcfa460a2007-10-31 13:53:06 +01002407 else
Sergey Lapindfe64e22013-01-14 03:46:50 +00002408 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2409
2410 if (status < 0)
2411 return status;
William Juulcfa460a2007-10-31 13:53:06 +01002412
2413 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00002414 * Cached progamming disabled for now. Not sure if it's worth the
2415 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
William Juulcfa460a2007-10-31 13:53:06 +01002416 */
2417 cached = 0;
2418
Heiko Schocherff94bc42014-06-24 10:10:04 +02002419 if (!cached || !NAND_HAS_CACHEPROG(chip)) {
William Juulcfa460a2007-10-31 13:53:06 +01002420
2421 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2422 status = chip->waitfunc(mtd, chip);
2423 /*
2424 * See if operation failed and additional status checks are
Sergey Lapindfe64e22013-01-14 03:46:50 +00002425 * available.
William Juulcfa460a2007-10-31 13:53:06 +01002426 */
2427 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2428 status = chip->errstat(mtd, chip, FL_WRITING, status,
2429 page);
2430
2431 if (status & NAND_STATUS_FAIL)
2432 return -EIO;
2433 } else {
2434 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2435 status = chip->waitfunc(mtd, chip);
2436 }
2437
Heiko Schocherff94bc42014-06-24 10:10:04 +02002438
2439#ifdef __UBOOT__
2440#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
William Juulcfa460a2007-10-31 13:53:06 +01002441 /* Send command to read back the data */
2442 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
2443
2444 if (chip->verify_buf(mtd, buf, mtd->writesize))
2445 return -EIO;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002446
2447 /* Make sure the next page prog is preceded by a status read */
2448 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002449#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +02002450#endif
2451
William Juulcfa460a2007-10-31 13:53:06 +01002452 return 0;
2453}
2454
2455/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002456 * nand_fill_oob - [INTERN] Transfer client buffer to oob
2457 * @mtd: MTD device structure
2458 * @oob: oob data buffer
2459 * @len: oob data write length
2460 * @ops: oob ops structure
William Juulcfa460a2007-10-31 13:53:06 +01002461 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00002462static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2463 struct mtd_oob_ops *ops)
William Juulcfa460a2007-10-31 13:53:06 +01002464{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002465 struct nand_chip *chip = mtd->priv;
2466
2467 /*
2468 * Initialise to all 0xFF, to avoid the possibility of left over OOB
2469 * data from a previous OOB read.
2470 */
2471 memset(chip->oob_poi, 0xff, mtd->oobsize);
2472
Christian Hitz90e3f392011-10-12 09:32:01 +02002473 switch (ops->mode) {
William Juulcfa460a2007-10-31 13:53:06 +01002474
Sergey Lapindfe64e22013-01-14 03:46:50 +00002475 case MTD_OPS_PLACE_OOB:
2476 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002477 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2478 return oob + len;
2479
Sergey Lapindfe64e22013-01-14 03:46:50 +00002480 case MTD_OPS_AUTO_OOB: {
William Juulcfa460a2007-10-31 13:53:06 +01002481 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2482 uint32_t boffs = 0, woffs = ops->ooboffs;
2483 size_t bytes = 0;
2484
Christian Hitz90e3f392011-10-12 09:32:01 +02002485 for (; free->length && len; free++, len -= bytes) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002486 /* Write request not from offset 0? */
William Juulcfa460a2007-10-31 13:53:06 +01002487 if (unlikely(woffs)) {
2488 if (woffs >= free->length) {
2489 woffs -= free->length;
2490 continue;
2491 }
2492 boffs = free->offset + woffs;
2493 bytes = min_t(size_t, len,
2494 (free->length - woffs));
2495 woffs = 0;
2496 } else {
2497 bytes = min_t(size_t, len, free->length);
2498 boffs = free->offset;
2499 }
2500 memcpy(chip->oob_poi + boffs, oob, bytes);
2501 oob += bytes;
2502 }
2503 return oob;
2504 }
2505 default:
2506 BUG();
2507 }
2508 return NULL;
2509}
2510
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002511#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
William Juulcfa460a2007-10-31 13:53:06 +01002512
2513/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002514 * nand_do_write_ops - [INTERN] NAND write with ECC
2515 * @mtd: MTD device structure
2516 * @to: offset to write to
2517 * @ops: oob operations description structure
William Juulcfa460a2007-10-31 13:53:06 +01002518 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002519 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002520 */
2521static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2522 struct mtd_oob_ops *ops)
2523{
2524 int chipnr, realpage, page, blockmask, column;
2525 struct nand_chip *chip = mtd->priv;
2526 uint32_t writelen = ops->len;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002527
2528 uint32_t oobwritelen = ops->ooblen;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002529 uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002530 mtd->oobavail : mtd->oobsize;
2531
William Juulcfa460a2007-10-31 13:53:06 +01002532 uint8_t *oob = ops->oobbuf;
2533 uint8_t *buf = ops->datbuf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002534 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002535 int oob_required = oob ? 1 : 0;
William Juulcfa460a2007-10-31 13:53:06 +01002536
2537 ops->retlen = 0;
2538 if (!writelen)
2539 return 0;
2540
Heiko Schocherff94bc42014-06-24 10:10:04 +02002541#ifndef __UBOOT__
2542 /* Reject writes, which are not page aligned */
2543 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2544#else
2545 /* Reject writes, which are not page aligned */
2546 if (NOTALIGNED(to)) {
2547#endif
2548 pr_notice("%s: attempt to write non page aligned data\n",
2549 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002550 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002551 }
2552
2553 column = to & (mtd->writesize - 1);
William Juulcfa460a2007-10-31 13:53:06 +01002554
2555 chipnr = (int)(to >> chip->chip_shift);
2556 chip->select_chip(mtd, chipnr);
2557
2558 /* Check, if it is write protected */
2559 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002560 ret = -EIO;
2561 goto err_out;
William Juulcfa460a2007-10-31 13:53:06 +01002562 }
2563
2564 realpage = (int)(to >> chip->page_shift);
2565 page = realpage & chip->pagemask;
2566 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2567
2568 /* Invalidate the page cache, when we write to the cached page */
2569 if (to <= (chip->pagebuf << chip->page_shift) &&
2570 (chip->pagebuf << chip->page_shift) < (to + ops->len))
2571 chip->pagebuf = -1;
2572
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002573 /* Don't allow multipage oob writes with offset */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002574 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2575 ret = -EINVAL;
2576 goto err_out;
2577 }
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002578
Christian Hitz90e3f392011-10-12 09:32:01 +02002579 while (1) {
William Juulcfa460a2007-10-31 13:53:06 +01002580 int bytes = mtd->writesize;
2581 int cached = writelen > bytes && page != blockmask;
2582 uint8_t *wbuf = buf;
2583
Heiko Schocherff94bc42014-06-24 10:10:04 +02002584 WATCHDOG_RESET();
Sergey Lapindfe64e22013-01-14 03:46:50 +00002585 /* Partial page write? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002586 if (unlikely(column || writelen < (mtd->writesize - 1))) {
William Juulcfa460a2007-10-31 13:53:06 +01002587 cached = 0;
2588 bytes = min_t(int, bytes - column, (int) writelen);
2589 chip->pagebuf = -1;
2590 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2591 memcpy(&chip->buffers->databuf[column], buf, bytes);
2592 wbuf = chip->buffers->databuf;
2593 }
2594
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002595 if (unlikely(oob)) {
2596 size_t len = min(oobwritelen, oobmaxlen);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002597 oob = nand_fill_oob(mtd, oob, len, ops);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002598 oobwritelen -= len;
Sergey Lapindfe64e22013-01-14 03:46:50 +00002599 } else {
2600 /* We still need to erase leftover OOB data */
2601 memset(chip->oob_poi, 0xff, mtd->oobsize);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002602 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002603 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2604 oob_required, page, cached,
2605 (ops->mode == MTD_OPS_RAW));
William Juulcfa460a2007-10-31 13:53:06 +01002606 if (ret)
2607 break;
2608
2609 writelen -= bytes;
2610 if (!writelen)
2611 break;
2612
2613 column = 0;
2614 buf += bytes;
2615 realpage++;
2616
2617 page = realpage & chip->pagemask;
2618 /* Check, if we cross a chip boundary */
2619 if (!page) {
2620 chipnr++;
2621 chip->select_chip(mtd, -1);
2622 chip->select_chip(mtd, chipnr);
2623 }
2624 }
2625
2626 ops->retlen = ops->len - writelen;
2627 if (unlikely(oob))
2628 ops->oobretlen = ops->ooblen;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002629
2630err_out:
2631 chip->select_chip(mtd, -1);
2632 return ret;
2633}
2634
2635/**
2636 * panic_nand_write - [MTD Interface] NAND write with ECC
2637 * @mtd: MTD device structure
2638 * @to: offset to write to
2639 * @len: number of bytes to write
2640 * @retlen: pointer to variable to store the number of written bytes
2641 * @buf: the data to write
2642 *
2643 * NAND write with ECC. Used when performing writes in interrupt context, this
2644 * may for example be called by mtdoops when writing an oops while in panic.
2645 */
2646static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2647 size_t *retlen, const uint8_t *buf)
2648{
2649 struct nand_chip *chip = mtd->priv;
2650 struct mtd_oob_ops ops;
2651 int ret;
2652
2653 /* Wait for the device to get ready */
2654 panic_nand_wait(mtd, chip, 400);
2655
2656 /* Grab the device */
2657 panic_nand_get_device(chip, mtd, FL_WRITING);
2658
2659 ops.len = len;
2660 ops.datbuf = (uint8_t *)buf;
2661 ops.oobbuf = NULL;
2662 ops.mode = MTD_OPS_PLACE_OOB;
2663
2664 ret = nand_do_write_ops(mtd, to, &ops);
2665
2666 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002667 return ret;
2668}
2669
2670/**
2671 * nand_write - [MTD Interface] NAND write with ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00002672 * @mtd: MTD device structure
2673 * @to: offset to write to
2674 * @len: number of bytes to write
2675 * @retlen: pointer to variable to store the number of written bytes
2676 * @buf: the data to write
Wolfgang Denk932394a2005-08-17 12:55:25 +02002677 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002678 * NAND write with ECC.
William Juulcfa460a2007-10-31 13:53:06 +01002679 */
2680static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2681 size_t *retlen, const uint8_t *buf)
2682{
Sergey Lapindfe64e22013-01-14 03:46:50 +00002683 struct mtd_oob_ops ops;
William Juulcfa460a2007-10-31 13:53:06 +01002684 int ret;
2685
Heiko Schocherff94bc42014-06-24 10:10:04 +02002686 nand_get_device(mtd, FL_WRITING);
Sergey Lapindfe64e22013-01-14 03:46:50 +00002687 ops.len = len;
2688 ops.datbuf = (uint8_t *)buf;
2689 ops.oobbuf = NULL;
2690 ops.mode = MTD_OPS_PLACE_OOB;
2691 ret = nand_do_write_ops(mtd, to, &ops);
2692 *retlen = ops.retlen;
William Juulcfa460a2007-10-31 13:53:06 +01002693 nand_release_device(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01002694 return ret;
2695}
2696
2697/**
2698 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002699 * @mtd: MTD device structure
2700 * @to: offset to write to
2701 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002702 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002703 * NAND write out-of-band.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002704 */
William Juulcfa460a2007-10-31 13:53:06 +01002705static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2706 struct mtd_oob_ops *ops)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002707{
William Juulcfa460a2007-10-31 13:53:06 +01002708 int chipnr, page, status, len;
2709 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002710
Heiko Schocherff94bc42014-06-24 10:10:04 +02002711 pr_debug("%s: to = 0x%08x, len = %i\n",
Christian Hitz90e3f392011-10-12 09:32:01 +02002712 __func__, (unsigned int)to, (int)ops->ooblen);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002713
Sergey Lapindfe64e22013-01-14 03:46:50 +00002714 if (ops->mode == MTD_OPS_AUTO_OOB)
William Juulcfa460a2007-10-31 13:53:06 +01002715 len = chip->ecc.layout->oobavail;
2716 else
2717 len = mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002718
2719 /* Do not allow write past end of page */
William Juulcfa460a2007-10-31 13:53:06 +01002720 if ((ops->ooboffs + ops->ooblen) > len) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002721 pr_debug("%s: attempt to write past end of page\n",
2722 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002723 return -EINVAL;
2724 }
2725
William Juulcfa460a2007-10-31 13:53:06 +01002726 if (unlikely(ops->ooboffs >= len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002727 pr_debug("%s: attempt to start write outside oob\n",
2728 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002729 return -EINVAL;
2730 }
2731
Christian Hitz90e3f392011-10-12 09:32:01 +02002732 /* Do not allow write past end of device */
William Juulcfa460a2007-10-31 13:53:06 +01002733 if (unlikely(to >= mtd->size ||
2734 ops->ooboffs + ops->ooblen >
2735 ((mtd->size >> chip->page_shift) -
2736 (to >> chip->page_shift)) * len)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002737 pr_debug("%s: attempt to write beyond end of device\n",
2738 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002739 return -EINVAL;
2740 }
2741
William Juulcfa460a2007-10-31 13:53:06 +01002742 chipnr = (int)(to >> chip->chip_shift);
2743 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002744
William Juulcfa460a2007-10-31 13:53:06 +01002745 /* Shift to get page */
2746 page = (int)(to >> chip->page_shift);
2747
2748 /*
2749 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2750 * of my DiskOnChip 2000 test units) will clear the whole data page too
2751 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2752 * it in the doc2000 driver in August 1999. dwmw2.
2753 */
2754 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002755
2756 /* Check, if it is write protected */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002757 if (nand_check_wp(mtd)) {
2758 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01002759 return -EROFS;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002760 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002761
Wolfgang Denk932394a2005-08-17 12:55:25 +02002762 /* Invalidate the page cache, if we write to the cached page */
William Juulcfa460a2007-10-31 13:53:06 +01002763 if (page == chip->pagebuf)
2764 chip->pagebuf = -1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002765
Sergey Lapindfe64e22013-01-14 03:46:50 +00002766 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2767
2768 if (ops->mode == MTD_OPS_RAW)
2769 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2770 else
2771 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002772
Heiko Schocherff94bc42014-06-24 10:10:04 +02002773 chip->select_chip(mtd, -1);
2774
William Juulcfa460a2007-10-31 13:53:06 +01002775 if (status)
2776 return status;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002777
William Juulcfa460a2007-10-31 13:53:06 +01002778 ops->oobretlen = ops->ooblen;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002779
William Juulcfa460a2007-10-31 13:53:06 +01002780 return 0;
2781}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002782
William Juulcfa460a2007-10-31 13:53:06 +01002783/**
2784 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
Sergey Lapindfe64e22013-01-14 03:46:50 +00002785 * @mtd: MTD device structure
2786 * @to: offset to write to
2787 * @ops: oob operation description structure
William Juulcfa460a2007-10-31 13:53:06 +01002788 */
2789static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2790 struct mtd_oob_ops *ops)
2791{
William Juulcfa460a2007-10-31 13:53:06 +01002792 int ret = -ENOTSUPP;
2793
2794 ops->retlen = 0;
2795
2796 /* Do not allow writes past end of device */
2797 if (ops->datbuf && (to + ops->len) > mtd->size) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002798 pr_debug("%s: attempt to write beyond end of device\n",
2799 __func__);
William Juulcfa460a2007-10-31 13:53:06 +01002800 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002801 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002802
Heiko Schocherff94bc42014-06-24 10:10:04 +02002803 nand_get_device(mtd, FL_WRITING);
William Juulcfa460a2007-10-31 13:53:06 +01002804
Christian Hitz90e3f392011-10-12 09:32:01 +02002805 switch (ops->mode) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002806 case MTD_OPS_PLACE_OOB:
2807 case MTD_OPS_AUTO_OOB:
2808 case MTD_OPS_RAW:
William Juulcfa460a2007-10-31 13:53:06 +01002809 break;
2810
2811 default:
2812 goto out;
2813 }
2814
2815 if (!ops->datbuf)
2816 ret = nand_do_write_oob(mtd, to, ops);
2817 else
2818 ret = nand_do_write_ops(mtd, to, ops);
2819
Christian Hitz90e3f392011-10-12 09:32:01 +02002820out:
William Juulcfa460a2007-10-31 13:53:06 +01002821 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002822 return ret;
2823}
Wolfgang Denk932394a2005-08-17 12:55:25 +02002824
2825/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002826 * single_erase_cmd - [GENERIC] NAND standard block erase command function
2827 * @mtd: MTD device structure
2828 * @page: the page address of the block which will be erased
Wolfgang Denk932394a2005-08-17 12:55:25 +02002829 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002830 * Standard erase command for NAND chips.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002831 */
William Juulcfa460a2007-10-31 13:53:06 +01002832static void single_erase_cmd(struct mtd_info *mtd, int page)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002833{
William Juulcfa460a2007-10-31 13:53:06 +01002834 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002835 /* Send commands to erase a block */
William Juulcfa460a2007-10-31 13:53:06 +01002836 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2837 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002838}
2839
2840/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02002841 * nand_erase - [MTD Interface] erase block(s)
Sergey Lapindfe64e22013-01-14 03:46:50 +00002842 * @mtd: MTD device structure
2843 * @instr: erase instruction
Wolfgang Denk932394a2005-08-17 12:55:25 +02002844 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002845 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002846 */
William Juulcfa460a2007-10-31 13:53:06 +01002847static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002848{
William Juulcfa460a2007-10-31 13:53:06 +01002849 return nand_erase_nand(mtd, instr, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002850}
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002851
Wolfgang Denk932394a2005-08-17 12:55:25 +02002852/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00002853 * nand_erase_nand - [INTERN] erase block(s)
2854 * @mtd: MTD device structure
2855 * @instr: erase instruction
2856 * @allowbbt: allow erasing the bbt area
Wolfgang Denk932394a2005-08-17 12:55:25 +02002857 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002858 * Erase one ore more blocks.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002859 */
William Juulcfa460a2007-10-31 13:53:06 +01002860int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2861 int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002862{
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002863 int page, status, pages_per_block, ret, chipnr;
William Juulcfa460a2007-10-31 13:53:06 +01002864 struct nand_chip *chip = mtd->priv;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04002865 loff_t len;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002866
Heiko Schocherff94bc42014-06-24 10:10:04 +02002867 pr_debug("%s: start = 0x%012llx, len = %llu\n",
2868 __func__, (unsigned long long)instr->addr,
2869 (unsigned long long)instr->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002870
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02002871 if (check_offs_len(mtd, instr->addr, instr->len))
Wolfgang Denk932394a2005-08-17 12:55:25 +02002872 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002873
Wolfgang Denk932394a2005-08-17 12:55:25 +02002874 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002875 nand_get_device(mtd, FL_ERASING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002876
2877 /* Shift to get first page */
William Juulcfa460a2007-10-31 13:53:06 +01002878 page = (int)(instr->addr >> chip->page_shift);
2879 chipnr = (int)(instr->addr >> chip->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002880
2881 /* Calculate pages in each block */
William Juulcfa460a2007-10-31 13:53:06 +01002882 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
William Juul4cbb6512007-11-08 10:39:53 +01002883
Wolfgang Denk932394a2005-08-17 12:55:25 +02002884 /* Select the NAND device */
William Juulcfa460a2007-10-31 13:53:06 +01002885 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002886
Wolfgang Denk932394a2005-08-17 12:55:25 +02002887 /* Check, if it is write protected */
2888 if (nand_check_wp(mtd)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002889 pr_debug("%s: device is write protected!\n",
2890 __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002891 instr->state = MTD_ERASE_FAILED;
2892 goto erase_exit;
2893 }
2894
2895 /* Loop through the pages */
2896 len = instr->len;
2897
2898 instr->state = MTD_ERASING;
2899
2900 while (len) {
Scott Wood6f2ffc32011-02-02 18:15:57 -06002901 WATCHDOG_RESET();
Heiko Schocherff94bc42014-06-24 10:10:04 +02002902
Sergey Lapindfe64e22013-01-14 03:46:50 +00002903 /* Check if we have a bad block, we do not erase bad blocks! */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002904 if (nand_block_checkbad(mtd, ((loff_t) page) <<
William Juulcfa460a2007-10-31 13:53:06 +01002905 chip->page_shift, 0, allowbbt)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00002906 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02002907 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002908 instr->state = MTD_ERASE_FAILED;
2909 goto erase_exit;
2910 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02002911
William Juulcfa460a2007-10-31 13:53:06 +01002912 /*
2913 * Invalidate the page cache, if we erase the block which
Sergey Lapindfe64e22013-01-14 03:46:50 +00002914 * contains the current cached page.
William Juulcfa460a2007-10-31 13:53:06 +01002915 */
2916 if (page <= chip->pagebuf && chip->pagebuf <
2917 (page + pages_per_block))
2918 chip->pagebuf = -1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002919
William Juulcfa460a2007-10-31 13:53:06 +01002920 chip->erase_cmd(mtd, page & chip->pagemask);
2921
2922 status = chip->waitfunc(mtd, chip);
2923
2924 /*
2925 * See if operation failed and additional status checks are
2926 * available
2927 */
2928 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2929 status = chip->errstat(mtd, chip, FL_ERASING,
2930 status, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002931
2932 /* See if block erase succeeded */
William Juulcfa460a2007-10-31 13:53:06 +01002933 if (status & NAND_STATUS_FAIL) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002934 pr_debug("%s: failed erase, page 0x%08x\n",
2935 __func__, page);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002936 instr->state = MTD_ERASE_FAILED;
Christian Hitz90e3f392011-10-12 09:32:01 +02002937 instr->fail_addr =
2938 ((loff_t)page << chip->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002939 goto erase_exit;
2940 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02002941
Wolfgang Denk932394a2005-08-17 12:55:25 +02002942 /* Increment page address and decrement length */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002943 len -= (1ULL << chip->phys_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002944 page += pages_per_block;
2945
2946 /* Check, if we cross a chip boundary */
William Juulcfa460a2007-10-31 13:53:06 +01002947 if (len && !(page & chip->pagemask)) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02002948 chipnr++;
William Juulcfa460a2007-10-31 13:53:06 +01002949 chip->select_chip(mtd, -1);
2950 chip->select_chip(mtd, chipnr);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002951 }
2952 }
2953 instr->state = MTD_ERASE_DONE;
2954
Christian Hitz90e3f392011-10-12 09:32:01 +02002955erase_exit:
Wolfgang Denk932394a2005-08-17 12:55:25 +02002956
2957 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
Wolfgang Denk932394a2005-08-17 12:55:25 +02002958
2959 /* Deselect and wake up anyone waiting on the device */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002960 chip->select_chip(mtd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002961 nand_release_device(mtd);
2962
Scott Woodc45912d2008-10-24 16:20:43 -05002963 /* Do call back function */
2964 if (!ret)
2965 mtd_erase_callback(instr);
2966
Wolfgang Denk932394a2005-08-17 12:55:25 +02002967 /* Return more or less happy */
2968 return ret;
2969}
2970
2971/**
2972 * nand_sync - [MTD Interface] sync
Sergey Lapindfe64e22013-01-14 03:46:50 +00002973 * @mtd: MTD device structure
Wolfgang Denk932394a2005-08-17 12:55:25 +02002974 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00002975 * Sync is actually a wait for chip ready function.
Wolfgang Denk932394a2005-08-17 12:55:25 +02002976 */
William Juulcfa460a2007-10-31 13:53:06 +01002977static void nand_sync(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002978{
Heiko Schocherff94bc42014-06-24 10:10:04 +02002979 pr_debug("%s: called\n", __func__);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002980
2981 /* Grab the lock and see if the device is available */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002982 nand_get_device(mtd, FL_SYNCING);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002983 /* Release it and go back */
William Juulcfa460a2007-10-31 13:53:06 +01002984 nand_release_device(mtd);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002985}
2986
Wolfgang Denk932394a2005-08-17 12:55:25 +02002987/**
William Juulcfa460a2007-10-31 13:53:06 +01002988 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002989 * @mtd: MTD device structure
2990 * @offs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02002991 */
William Juulcfa460a2007-10-31 13:53:06 +01002992static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02002993{
William Juulcfa460a2007-10-31 13:53:06 +01002994 return nand_block_checkbad(mtd, offs, 1, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +02002995}
2996
2997/**
William Juulcfa460a2007-10-31 13:53:06 +01002998 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
Sergey Lapindfe64e22013-01-14 03:46:50 +00002999 * @mtd: MTD device structure
3000 * @ofs: offset relative to mtd start
Wolfgang Denk932394a2005-08-17 12:55:25 +02003001 */
William Juulcfa460a2007-10-31 13:53:06 +01003002static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02003003{
Wolfgang Denk932394a2005-08-17 12:55:25 +02003004 int ret;
3005
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003006 ret = nand_block_isbad(mtd, ofs);
3007 if (ret) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003008 /* If it was bad already, return success and do nothing */
Wolfgang Denk932394a2005-08-17 12:55:25 +02003009 if (ret > 0)
3010 return 0;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02003011 return ret;
3012 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02003013
Heiko Schocherff94bc42014-06-24 10:10:04 +02003014 return nand_block_markbad_lowlevel(mtd, ofs);
Wolfgang Denk932394a2005-08-17 12:55:25 +02003015}
3016
Heiko Schocherff94bc42014-06-24 10:10:04 +02003017/**
Sergey Lapindfe64e22013-01-14 03:46:50 +00003018 * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
3019 * @mtd: MTD device structure
3020 * @chip: nand chip info structure
3021 * @addr: feature address.
3022 * @subfeature_param: the subfeature parameters, a four bytes array.
William Juulcfa460a2007-10-31 13:53:06 +01003023 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003024static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3025 int addr, uint8_t *subfeature_param)
3026{
3027 int status;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003028 int i;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003029
Heiko Schocherff94bc42014-06-24 10:10:04 +02003030#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3031 if (!chip->onfi_version ||
3032 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3033 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00003034 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003035#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003036
3037 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003038 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3039 chip->write_byte(mtd, subfeature_param[i]);
3040
Sergey Lapindfe64e22013-01-14 03:46:50 +00003041 status = chip->waitfunc(mtd, chip);
3042 if (status & NAND_STATUS_FAIL)
3043 return -EIO;
3044 return 0;
3045}
3046
3047/**
3048 * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
3049 * @mtd: MTD device structure
3050 * @chip: nand chip info structure
3051 * @addr: feature address.
3052 * @subfeature_param: the subfeature parameters, a four bytes array.
3053 */
3054static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3055 int addr, uint8_t *subfeature_param)
3056{
Heiko Schocherff94bc42014-06-24 10:10:04 +02003057 int i;
3058
3059#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3060 if (!chip->onfi_version ||
3061 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3062 & ONFI_OPT_CMD_SET_GET_FEATURES))
Sergey Lapindfe64e22013-01-14 03:46:50 +00003063 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003064#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00003065
3066 /* clear the sub feature parameters */
3067 memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
3068
3069 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003070 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3071 *subfeature_param++ = chip->read_byte(mtd);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003072 return 0;
3073}
3074
Heiko Schocherff94bc42014-06-24 10:10:04 +02003075#ifndef __UBOOT__
3076/**
3077 * nand_suspend - [MTD Interface] Suspend the NAND flash
3078 * @mtd: MTD device structure
3079 */
3080static int nand_suspend(struct mtd_info *mtd)
3081{
3082 return nand_get_device(mtd, FL_PM_SUSPENDED);
3083}
3084
3085/**
3086 * nand_resume - [MTD Interface] Resume the NAND flash
3087 * @mtd: MTD device structure
3088 */
3089static void nand_resume(struct mtd_info *mtd)
3090{
3091 struct nand_chip *chip = mtd->priv;
3092
3093 if (chip->state == FL_PM_SUSPENDED)
3094 nand_release_device(mtd);
3095 else
3096 pr_err("%s called for a chip which is not in suspended state\n",
3097 __func__);
3098}
3099#endif
3100
Sergey Lapindfe64e22013-01-14 03:46:50 +00003101/* Set default functions */
William Juulcfa460a2007-10-31 13:53:06 +01003102static void nand_set_defaults(struct nand_chip *chip, int busw)
3103{
3104 /* check for proper chip_delay setup, set 20us if not */
3105 if (!chip->chip_delay)
3106 chip->chip_delay = 20;
3107
3108 /* check, if a user supplied command function given */
3109 if (chip->cmdfunc == NULL)
3110 chip->cmdfunc = nand_command;
3111
3112 /* check, if a user supplied wait function given */
3113 if (chip->waitfunc == NULL)
3114 chip->waitfunc = nand_wait;
3115
3116 if (!chip->select_chip)
3117 chip->select_chip = nand_select_chip;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003118
3119 /* set for ONFI nand */
3120 if (!chip->onfi_set_features)
3121 chip->onfi_set_features = nand_onfi_set_features;
3122 if (!chip->onfi_get_features)
3123 chip->onfi_get_features = nand_onfi_get_features;
3124
3125 /* If called twice, pointers that depend on busw may need to be reset */
3126 if (!chip->read_byte || chip->read_byte == nand_read_byte)
William Juulcfa460a2007-10-31 13:53:06 +01003127 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3128 if (!chip->read_word)
3129 chip->read_word = nand_read_word;
3130 if (!chip->block_bad)
3131 chip->block_bad = nand_block_bad;
3132 if (!chip->block_markbad)
3133 chip->block_markbad = nand_default_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003134 if (!chip->write_buf || chip->write_buf == nand_write_buf)
William Juulcfa460a2007-10-31 13:53:06 +01003135 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003136 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3137 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3138 if (!chip->read_buf || chip->read_buf == nand_read_buf)
William Juulcfa460a2007-10-31 13:53:06 +01003139 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
William Juulcfa460a2007-10-31 13:53:06 +01003140 if (!chip->scan_bbt)
3141 chip->scan_bbt = nand_default_bbt;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003142#ifdef __UBOOT__
3143#if defined(CONFIG_MTD_NAND_VERIFY_WRITE)
3144 if (!chip->verify_buf)
3145 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
3146#endif
3147#endif
3148
3149 if (!chip->controller) {
William Juulcfa460a2007-10-31 13:53:06 +01003150 chip->controller = &chip->hwcontrol;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003151 spin_lock_init(&chip->controller->lock);
3152 init_waitqueue_head(&chip->controller->wq);
3153 }
3154
William Juulcfa460a2007-10-31 13:53:06 +01003155}
3156
Florian Fainelli0272c712011-02-25 00:01:34 +00003157#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Sergey Lapindfe64e22013-01-14 03:46:50 +00003158/* Sanitize ONFI strings so we can safely print them */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003159#ifndef __UBOOT__
3160static void sanitize_string(uint8_t *s, size_t len)
3161#else
Christian Hitz5454ddb2011-10-12 09:32:05 +02003162static void sanitize_string(char *s, size_t len)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003163#endif
Christian Hitz5454ddb2011-10-12 09:32:05 +02003164{
3165 ssize_t i;
3166
Sergey Lapindfe64e22013-01-14 03:46:50 +00003167 /* Null terminate */
Christian Hitz5454ddb2011-10-12 09:32:05 +02003168 s[len - 1] = 0;
3169
Sergey Lapindfe64e22013-01-14 03:46:50 +00003170 /* Remove non printable chars */
Christian Hitz5454ddb2011-10-12 09:32:05 +02003171 for (i = 0; i < len - 1; i++) {
3172 if (s[i] < ' ' || s[i] > 127)
3173 s[i] = '?';
3174 }
3175
Sergey Lapindfe64e22013-01-14 03:46:50 +00003176 /* Remove trailing spaces */
Christian Hitz5454ddb2011-10-12 09:32:05 +02003177 strim(s);
3178}
3179
Florian Fainelli0272c712011-02-25 00:01:34 +00003180static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
William Juulcfa460a2007-10-31 13:53:06 +01003181{
Florian Fainelli0272c712011-02-25 00:01:34 +00003182 int i;
Florian Fainelli0272c712011-02-25 00:01:34 +00003183 while (len--) {
3184 crc ^= *p++ << 8;
3185 for (i = 0; i < 8; i++)
3186 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
Scott Woodc45912d2008-10-24 16:20:43 -05003187 }
3188
Florian Fainelli0272c712011-02-25 00:01:34 +00003189 return crc;
3190}
William Juulcfa460a2007-10-31 13:53:06 +01003191
Heiko Schocherff94bc42014-06-24 10:10:04 +02003192/* Parse the Extended Parameter Page. */
3193static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3194 struct nand_chip *chip, struct nand_onfi_params *p)
3195{
3196 struct onfi_ext_param_page *ep;
3197 struct onfi_ext_section *s;
3198 struct onfi_ext_ecc_info *ecc;
3199 uint8_t *cursor;
3200 int ret = -EINVAL;
3201 int len;
3202 int i;
3203
3204 len = le16_to_cpu(p->ext_param_page_length) * 16;
3205 ep = kmalloc(len, GFP_KERNEL);
3206 if (!ep)
3207 return -ENOMEM;
3208
3209 /* Send our own NAND_CMD_PARAM. */
3210 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3211
3212 /* Use the Change Read Column command to skip the ONFI param pages. */
3213 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3214 sizeof(*p) * p->num_of_param_pages , -1);
3215
3216 /* Read out the Extended Parameter Page. */
3217 chip->read_buf(mtd, (uint8_t *)ep, len);
3218 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3219 != le16_to_cpu(ep->crc))) {
3220 pr_debug("fail in the CRC.\n");
3221 goto ext_out;
3222 }
3223
3224 /*
3225 * Check the signature.
3226 * Do not strictly follow the ONFI spec, maybe changed in future.
3227 */
3228#ifndef __UBOOT__
3229 if (strncmp(ep->sig, "EPPS", 4)) {
3230#else
3231 if (strncmp((char *)ep->sig, "EPPS", 4)) {
3232#endif
3233 pr_debug("The signature is invalid.\n");
3234 goto ext_out;
3235 }
3236
3237 /* find the ECC section. */
3238 cursor = (uint8_t *)(ep + 1);
3239 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3240 s = ep->sections + i;
3241 if (s->type == ONFI_SECTION_TYPE_2)
3242 break;
3243 cursor += s->length * 16;
3244 }
3245 if (i == ONFI_EXT_SECTION_MAX) {
3246 pr_debug("We can not find the ECC section.\n");
3247 goto ext_out;
3248 }
3249
3250 /* get the info we want. */
3251 ecc = (struct onfi_ext_ecc_info *)cursor;
3252
3253 if (!ecc->codeword_size) {
3254 pr_debug("Invalid codeword size\n");
3255 goto ext_out;
3256 }
3257
3258 chip->ecc_strength_ds = ecc->ecc_bits;
3259 chip->ecc_step_ds = 1 << ecc->codeword_size;
3260 ret = 0;
3261
3262ext_out:
3263 kfree(ep);
3264 return ret;
3265}
3266
3267static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3268{
3269 struct nand_chip *chip = mtd->priv;
3270 uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3271
3272 return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3273 feature);
3274}
3275
3276/*
3277 * Configure chip properties from Micron vendor-specific ONFI table
3278 */
3279static void nand_onfi_detect_micron(struct nand_chip *chip,
3280 struct nand_onfi_params *p)
3281{
3282 struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3283
3284 if (le16_to_cpu(p->vendor_revision) < 1)
3285 return;
3286
3287 chip->read_retries = micron->read_retry_options;
3288 chip->setup_read_retry = nand_setup_read_retry_micron;
3289}
3290
Florian Fainelli0272c712011-02-25 00:01:34 +00003291/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003292 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
Florian Fainelli0272c712011-02-25 00:01:34 +00003293 */
Christian Hitz90e3f392011-10-12 09:32:01 +02003294static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003295 int *busw)
3296{
3297 struct nand_onfi_params *p = &chip->onfi_params;
Brian Norrisb9ae6092014-05-06 00:46:16 +05303298 int i, j;
Florian Fainelli0272c712011-02-25 00:01:34 +00003299 int val;
3300
Sergey Lapindfe64e22013-01-14 03:46:50 +00003301 /* Try ONFI for unknown chip or LP */
Florian Fainelli0272c712011-02-25 00:01:34 +00003302 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3303 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3304 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3305 return 0;
3306
Heiko Schocherff94bc42014-06-24 10:10:04 +02003307 /*
3308 * ONFI must be probed in 8-bit mode or with NAND_BUSWIDTH_AUTO, not
3309 * with NAND_BUSWIDTH_16
3310 */
3311 if (chip->options & NAND_BUSWIDTH_16) {
3312 pr_err("ONFI cannot be probed in 16-bit mode; aborting\n");
3313 return 0;
3314 }
3315
Florian Fainelli0272c712011-02-25 00:01:34 +00003316 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3317 for (i = 0; i < 3; i++) {
Brian Norrisb9ae6092014-05-06 00:46:16 +05303318 for (j = 0; j < sizeof(*p); j++)
3319 ((uint8_t *)p)[j] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003320 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Christian Hitz90e3f392011-10-12 09:32:01 +02003321 le16_to_cpu(p->crc)) {
Wolfgang Denkd1a24f02011-02-02 22:36:10 +01003322 break;
Florian Fainelli0272c712011-02-25 00:01:34 +00003323 }
Florian Fainelli3e9b3492010-06-12 20:59:25 +02003324 }
William Juulcfa460a2007-10-31 13:53:06 +01003325
Heiko Schocherff94bc42014-06-24 10:10:04 +02003326 if (i == 3) {
3327 pr_err("Could not find valid ONFI parameter page; aborting\n");
Florian Fainelli0272c712011-02-25 00:01:34 +00003328 return 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003329 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003330
Sergey Lapindfe64e22013-01-14 03:46:50 +00003331 /* Check version */
Florian Fainelli0272c712011-02-25 00:01:34 +00003332 val = le16_to_cpu(p->revision);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003333 if (val & (1 << 5))
3334 chip->onfi_version = 23;
3335 else if (val & (1 << 4))
Florian Fainelli0272c712011-02-25 00:01:34 +00003336 chip->onfi_version = 22;
3337 else if (val & (1 << 3))
3338 chip->onfi_version = 21;
3339 else if (val & (1 << 2))
3340 chip->onfi_version = 20;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003341 else if (val & (1 << 1))
Florian Fainelli0272c712011-02-25 00:01:34 +00003342 chip->onfi_version = 10;
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003343
3344 if (!chip->onfi_version) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003345 pr_info("unsupported ONFI version: %d\n", val);
Florian Fainelliaad99bb2011-04-03 18:23:56 +02003346 return 0;
3347 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003348
Christian Hitz5454ddb2011-10-12 09:32:05 +02003349 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3350 sanitize_string(p->model, sizeof(p->model));
William Juulcfa460a2007-10-31 13:53:06 +01003351 if (!mtd->name)
Florian Fainelli0272c712011-02-25 00:01:34 +00003352 mtd->name = p->model;
William Juulcfa460a2007-10-31 13:53:06 +01003353
Heiko Schocherff94bc42014-06-24 10:10:04 +02003354 mtd->writesize = le32_to_cpu(p->byte_per_page);
3355
3356 /*
3357 * pages_per_block and blocks_per_lun may not be a power-of-2 size
3358 * (don't ask me who thought of this...). MTD assumes that these
3359 * dimensions will be power-of-2, so just truncate the remaining area.
3360 */
3361 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3362 mtd->erasesize *= mtd->writesize;
3363
3364 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3365
3366 /* See erasesize comment */
3367 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3368 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3369 chip->bits_per_cell = p->bits_per_cell;
3370
3371 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3372 *busw = NAND_BUSWIDTH_16;
3373 else
3374 *busw = 0;
3375
3376 if (p->ecc_bits != 0xff) {
3377 chip->ecc_strength_ds = p->ecc_bits;
3378 chip->ecc_step_ds = 512;
3379 } else if (chip->onfi_version >= 21 &&
3380 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3381
3382 /*
3383 * The nand_flash_detect_ext_param_page() uses the
3384 * Change Read Column command which maybe not supported
3385 * by the chip->cmdfunc. So try to update the chip->cmdfunc
3386 * now. We do not replace user supplied command function.
3387 */
3388 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3389 chip->cmdfunc = nand_command_lp;
3390
3391 /* The Extended Parameter Page is supported since ONFI 2.1. */
3392 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3393 pr_warn("Failed to detect ONFI extended param page\n");
3394 } else {
3395 pr_warn("Could not retrieve ONFI ECC requirements\n");
3396 }
3397
3398 if (p->jedec_id == NAND_MFR_MICRON)
3399 nand_onfi_detect_micron(chip, p);
3400
Florian Fainelli0272c712011-02-25 00:01:34 +00003401 return 1;
3402}
3403#else
Heiko Schocherff94bc42014-06-24 10:10:04 +02003404static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
Florian Fainelli0272c712011-02-25 00:01:34 +00003405 int *busw)
3406{
3407 return 0;
3408}
3409#endif
3410
Florian Fainelli0272c712011-02-25 00:01:34 +00003411/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003412 * nand_id_has_period - Check if an ID string has a given wraparound period
3413 * @id_data: the ID string
3414 * @arrlen: the length of the @id_data array
3415 * @period: the period of repitition
3416 *
3417 * Check if an ID string is repeated within a given sequence of bytes at
3418 * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
Heiko Schocherff94bc42014-06-24 10:10:04 +02003419 * period of 3). This is a helper function for nand_id_len(). Returns non-zero
Sergey Lapindfe64e22013-01-14 03:46:50 +00003420 * if the repetition has a period of @period; otherwise, returns zero.
3421 */
3422static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3423{
3424 int i, j;
3425 for (i = 0; i < period; i++)
3426 for (j = i + period; j < arrlen; j += period)
3427 if (id_data[i] != id_data[j])
3428 return 0;
3429 return 1;
3430}
3431
3432/*
3433 * nand_id_len - Get the length of an ID string returned by CMD_READID
3434 * @id_data: the ID string
3435 * @arrlen: the length of the @id_data array
3436
3437 * Returns the length of the ID string, according to known wraparound/trailing
3438 * zero patterns. If no pattern exists, returns the length of the array.
3439 */
3440static int nand_id_len(u8 *id_data, int arrlen)
3441{
3442 int last_nonzero, period;
3443
3444 /* Find last non-zero byte */
3445 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3446 if (id_data[last_nonzero])
3447 break;
3448
3449 /* All zeros */
3450 if (last_nonzero < 0)
3451 return 0;
3452
3453 /* Calculate wraparound period */
3454 for (period = 1; period < arrlen; period++)
3455 if (nand_id_has_period(id_data, arrlen, period))
3456 break;
3457
3458 /* There's a repeated pattern */
3459 if (period < arrlen)
3460 return period;
3461
3462 /* There are trailing zeros */
3463 if (last_nonzero < arrlen - 1)
3464 return last_nonzero + 1;
3465
3466 /* No pattern detected */
3467 return arrlen;
3468}
3469
Heiko Schocherff94bc42014-06-24 10:10:04 +02003470/* Extract the bits of per cell from the 3rd byte of the extended ID */
3471static int nand_get_bits_per_cell(u8 cellinfo)
3472{
3473 int bits;
3474
3475 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3476 bits >>= NAND_CI_CELLTYPE_SHIFT;
3477 return bits + 1;
3478}
3479
Sergey Lapindfe64e22013-01-14 03:46:50 +00003480/*
3481 * Many new NAND share similar device ID codes, which represent the size of the
3482 * chip. The rest of the parameters must be decoded according to generic or
3483 * manufacturer-specific "extended ID" decoding patterns.
3484 */
3485static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3486 u8 id_data[8], int *busw)
3487{
3488 int extid, id_len;
3489 /* The 3rd id byte holds MLC / multichip data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003490 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
Sergey Lapindfe64e22013-01-14 03:46:50 +00003491 /* The 4th id byte is the important one */
3492 extid = id_data[3];
3493
3494 id_len = nand_id_len(id_data, 8);
3495
3496 /*
3497 * Field definitions are in the following datasheets:
3498 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3499 * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3500 * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22)
3501 *
3502 * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3503 * ID to decide what to do.
3504 */
3505 if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003506 !nand_is_slc(chip) && id_data[5] != 0x00) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003507 /* Calc pagesize */
3508 mtd->writesize = 2048 << (extid & 0x03);
3509 extid >>= 2;
3510 /* Calc oobsize */
3511 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3512 case 1:
3513 mtd->oobsize = 128;
3514 break;
3515 case 2:
3516 mtd->oobsize = 218;
3517 break;
3518 case 3:
3519 mtd->oobsize = 400;
3520 break;
3521 case 4:
3522 mtd->oobsize = 436;
3523 break;
3524 case 5:
3525 mtd->oobsize = 512;
3526 break;
3527 case 6:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003528 mtd->oobsize = 640;
3529 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003530 case 7:
3531 default: /* Other cases are "reserved" (unknown) */
3532 mtd->oobsize = 1024;
3533 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003534 }
3535 extid >>= 2;
3536 /* Calc blocksize */
3537 mtd->erasesize = (128 * 1024) <<
3538 (((extid >> 1) & 0x04) | (extid & 0x03));
3539 *busw = 0;
3540 } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
Heiko Schocherff94bc42014-06-24 10:10:04 +02003541 !nand_is_slc(chip)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003542 unsigned int tmp;
3543
3544 /* Calc pagesize */
3545 mtd->writesize = 2048 << (extid & 0x03);
3546 extid >>= 2;
3547 /* Calc oobsize */
3548 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3549 case 0:
3550 mtd->oobsize = 128;
3551 break;
3552 case 1:
3553 mtd->oobsize = 224;
3554 break;
3555 case 2:
3556 mtd->oobsize = 448;
3557 break;
3558 case 3:
3559 mtd->oobsize = 64;
3560 break;
3561 case 4:
3562 mtd->oobsize = 32;
3563 break;
3564 case 5:
3565 mtd->oobsize = 16;
3566 break;
3567 default:
3568 mtd->oobsize = 640;
3569 break;
3570 }
3571 extid >>= 2;
3572 /* Calc blocksize */
3573 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3574 if (tmp < 0x03)
3575 mtd->erasesize = (128 * 1024) << tmp;
3576 else if (tmp == 0x03)
3577 mtd->erasesize = 768 * 1024;
3578 else
3579 mtd->erasesize = (64 * 1024) << tmp;
3580 *busw = 0;
3581 } else {
3582 /* Calc pagesize */
3583 mtd->writesize = 1024 << (extid & 0x03);
3584 extid >>= 2;
3585 /* Calc oobsize */
3586 mtd->oobsize = (8 << (extid & 0x01)) *
3587 (mtd->writesize >> 9);
3588 extid >>= 2;
3589 /* Calc blocksize. Blocksize is multiples of 64KiB */
3590 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3591 extid >>= 2;
3592 /* Get buswidth information */
3593 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003594
3595 /*
3596 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3597 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3598 * follows:
3599 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3600 * 110b -> 24nm
3601 * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC
3602 */
3603 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3604 nand_is_slc(chip) &&
3605 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3606 !(id_data[4] & 0x80) /* !BENAND */) {
3607 mtd->oobsize = 32 * mtd->writesize >> 9;
3608 }
3609
Sergey Lapindfe64e22013-01-14 03:46:50 +00003610 }
3611}
3612
Heiko Schocherff94bc42014-06-24 10:10:04 +02003613/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003614 * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3615 * decodes a matching ID table entry and assigns the MTD size parameters for
3616 * the chip.
3617 */
3618static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003619 struct nand_flash_dev *type, u8 id_data[8],
Sergey Lapindfe64e22013-01-14 03:46:50 +00003620 int *busw)
3621{
3622 int maf_id = id_data[0];
3623
3624 mtd->erasesize = type->erasesize;
3625 mtd->writesize = type->pagesize;
3626 mtd->oobsize = mtd->writesize / 32;
3627 *busw = type->options & NAND_BUSWIDTH_16;
3628
Heiko Schocherff94bc42014-06-24 10:10:04 +02003629 /* All legacy ID NAND are small-page, SLC */
3630 chip->bits_per_cell = 1;
3631
Sergey Lapindfe64e22013-01-14 03:46:50 +00003632 /*
3633 * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3634 * some Spansion chips have erasesize that conflicts with size
3635 * listed in nand_ids table.
3636 * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3637 */
3638 if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3639 && id_data[6] == 0x00 && id_data[7] == 0x00
3640 && mtd->writesize == 512) {
3641 mtd->erasesize = 128 * 1024;
3642 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3643 }
3644}
3645
Heiko Schocherff94bc42014-06-24 10:10:04 +02003646/*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003647 * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3648 * heuristic patterns using various detected parameters (e.g., manufacturer,
3649 * page size, cell-type information).
3650 */
3651static void nand_decode_bbm_options(struct mtd_info *mtd,
3652 struct nand_chip *chip, u8 id_data[8])
3653{
3654 int maf_id = id_data[0];
3655
3656 /* Set the bad block position */
3657 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3658 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3659 else
3660 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3661
3662 /*
3663 * Bad block marker is stored in the last page of each block on Samsung
3664 * and Hynix MLC devices; stored in first two pages of each block on
3665 * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3666 * AMD/Spansion, and Macronix. All others scan only the first page.
3667 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003668 if (!nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003669 (maf_id == NAND_MFR_SAMSUNG ||
3670 maf_id == NAND_MFR_HYNIX))
3671 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003672 else if ((nand_is_slc(chip) &&
Sergey Lapindfe64e22013-01-14 03:46:50 +00003673 (maf_id == NAND_MFR_SAMSUNG ||
3674 maf_id == NAND_MFR_HYNIX ||
3675 maf_id == NAND_MFR_TOSHIBA ||
3676 maf_id == NAND_MFR_AMD ||
3677 maf_id == NAND_MFR_MACRONIX)) ||
3678 (mtd->writesize == 2048 &&
3679 maf_id == NAND_MFR_MICRON))
3680 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3681}
3682
Heiko Schocherff94bc42014-06-24 10:10:04 +02003683static inline bool is_full_id_nand(struct nand_flash_dev *type)
3684{
3685 return type->id_len;
3686}
3687
3688static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3689 struct nand_flash_dev *type, u8 *id_data, int *busw)
3690{
3691#ifndef __UBOOT__
3692 if (!strncmp(type->id, id_data, type->id_len)) {
3693#else
3694 if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3695#endif
3696 mtd->writesize = type->pagesize;
3697 mtd->erasesize = type->erasesize;
3698 mtd->oobsize = type->oobsize;
3699
3700 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3701 chip->chipsize = (uint64_t)type->chipsize << 20;
3702 chip->options |= type->options;
3703 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3704 chip->ecc_step_ds = NAND_ECC_STEP(type);
3705
3706 *busw = type->options & NAND_BUSWIDTH_16;
3707
3708 if (!mtd->name)
3709 mtd->name = type->name;
3710
3711 return true;
3712 }
3713 return false;
3714}
3715
Sergey Lapindfe64e22013-01-14 03:46:50 +00003716/*
3717 * Get the flash and manufacturer id and lookup if the type is supported.
Florian Fainelli0272c712011-02-25 00:01:34 +00003718 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003719static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
Florian Fainelli0272c712011-02-25 00:01:34 +00003720 struct nand_chip *chip,
3721 int busw,
3722 int *maf_id, int *dev_id,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003723 struct nand_flash_dev *type)
Florian Fainelli0272c712011-02-25 00:01:34 +00003724{
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003725 int i, maf_idx;
3726 u8 id_data[8];
Florian Fainelli0272c712011-02-25 00:01:34 +00003727
3728 /* Select the device */
3729 chip->select_chip(mtd, 0);
3730
3731 /*
3732 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
Sergey Lapindfe64e22013-01-14 03:46:50 +00003733 * after power-up.
Florian Fainelli0272c712011-02-25 00:01:34 +00003734 */
3735 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3736
3737 /* Send the command for reading device ID */
3738 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3739
3740 /* Read manufacturer and device IDs */
3741 *maf_id = chip->read_byte(mtd);
3742 *dev_id = chip->read_byte(mtd);
3743
Sergey Lapindfe64e22013-01-14 03:46:50 +00003744 /*
3745 * Try again to make sure, as some systems the bus-hold or other
Florian Fainelli0272c712011-02-25 00:01:34 +00003746 * interface concerns can cause random data which looks like a
3747 * possibly credible NAND flash to appear. If the two results do
3748 * not match, ignore the device completely.
3749 */
3750
3751 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3752
Sergey Lapindfe64e22013-01-14 03:46:50 +00003753 /* Read entire ID string */
3754 for (i = 0; i < 8; i++)
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003755 id_data[i] = chip->read_byte(mtd);
Florian Fainelli0272c712011-02-25 00:01:34 +00003756
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003757 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003758 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003759 *maf_id, *dev_id, id_data[0], id_data[1]);
Florian Fainelli0272c712011-02-25 00:01:34 +00003760 return ERR_PTR(-ENODEV);
3761 }
3762
3763 if (!type)
3764 type = nand_flash_ids;
3765
Heiko Schocherff94bc42014-06-24 10:10:04 +02003766 for (; type->name != NULL; type++) {
3767 if (is_full_id_nand(type)) {
3768 if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3769 goto ident_done;
3770 } else if (*dev_id == type->dev_id) {
3771 break;
3772 }
3773 }
Florian Fainelli0272c712011-02-25 00:01:34 +00003774
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003775 chip->onfi_version = 0;
3776 if (!type->name || !type->pagesize) {
3777 /* Check is chip is ONFI compliant */
Sergey Lapindfe64e22013-01-14 03:46:50 +00003778 if (nand_flash_detect_onfi(mtd, chip, &busw))
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003779 goto ident_done;
Florian Fainelli0272c712011-02-25 00:01:34 +00003780 }
3781
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003782 if (!type->name)
3783 return ERR_PTR(-ENODEV);
3784
Florian Fainelli0272c712011-02-25 00:01:34 +00003785 if (!mtd->name)
3786 mtd->name = type->name;
3787
3788 chip->chipsize = (uint64_t)type->chipsize << 20;
Florian Fainelli0272c712011-02-25 00:01:34 +00003789
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003790 if (!type->pagesize && chip->init_size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003791 /* Set the pagesize, oobsize, erasesize by the driver */
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003792 busw = chip->init_size(mtd, chip, id_data);
3793 } else if (!type->pagesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003794 /* Decode parameters from extended ID */
3795 nand_decode_ext_id(mtd, chip, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003796 } else {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003797 nand_decode_id(mtd, chip, type, id_data, &busw);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003798 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003799 /* Get chip options */
Marek Vasut9c790a72012-08-30 13:39:38 +00003800 chip->options |= type->options;
Florian Fainelli0272c712011-02-25 00:01:34 +00003801
Sergey Lapindfe64e22013-01-14 03:46:50 +00003802 /*
3803 * Check if chip is not a Samsung device. Do not clear the
3804 * options for chips which do not have an extended id.
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003805 */
3806 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3807 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3808ident_done:
3809
William Juulcfa460a2007-10-31 13:53:06 +01003810 /* Try to identify manufacturer */
3811 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3812 if (nand_manuf_ids[maf_idx].id == *maf_id)
3813 break;
3814 }
3815
Heiko Schocherff94bc42014-06-24 10:10:04 +02003816 if (chip->options & NAND_BUSWIDTH_AUTO) {
3817 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3818 chip->options |= busw;
3819 nand_set_defaults(chip, busw);
3820 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3821 /*
3822 * Check, if buswidth is correct. Hardware drivers should set
3823 * chip correct!
3824 */
3825 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3826 *maf_id, *dev_id);
3827 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3828 pr_warn("bus width %d instead %d bit\n",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003829 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3830 busw ? 16 : 8);
William Juulcfa460a2007-10-31 13:53:06 +01003831 return ERR_PTR(-EINVAL);
3832 }
3833
Sergey Lapindfe64e22013-01-14 03:46:50 +00003834 nand_decode_bbm_options(mtd, chip, id_data);
3835
William Juulcfa460a2007-10-31 13:53:06 +01003836 /* Calculate the address shift from the page size */
3837 chip->page_shift = ffs(mtd->writesize) - 1;
Sergey Lapindfe64e22013-01-14 03:46:50 +00003838 /* Convert chipsize to number of pages per chip -1 */
William Juulcfa460a2007-10-31 13:53:06 +01003839 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3840
3841 chip->bbt_erase_shift = chip->phys_erase_shift =
3842 ffs(mtd->erasesize) - 1;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -04003843 if (chip->chipsize & 0xffffffff)
Sandeep Paulraj4f41e7e2009-11-07 14:24:06 -05003844 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003845 else {
3846 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3847 chip->chip_shift += 32 - 1;
3848 }
3849
3850 chip->badblockbits = 8;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003851 chip->erase_cmd = single_erase_cmd;
William Juulcfa460a2007-10-31 13:53:06 +01003852
Sergey Lapindfe64e22013-01-14 03:46:50 +00003853 /* Do not replace user supplied command function! */
William Juulcfa460a2007-10-31 13:53:06 +01003854 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3855 chip->cmdfunc = nand_command_lp;
3856
Heiko Schocherff94bc42014-06-24 10:10:04 +02003857 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3858 *maf_id, *dev_id);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003859#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
Heiko Schocherff94bc42014-06-24 10:10:04 +02003860 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3861 chip->onfi_version ? chip->onfi_params.model : type->name);
3862#else
3863 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, type->name);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02003864#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +02003865 pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3866 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
Sergey Lapindfe64e22013-01-14 03:46:50 +00003867 mtd->writesize, mtd->oobsize);
William Juulcfa460a2007-10-31 13:53:06 +01003868 return type;
3869}
3870
3871/**
3872 * nand_scan_ident - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003873 * @mtd: MTD device structure
3874 * @maxchips: number of chips to scan for
3875 * @table: alternative NAND ID table
William Juulcfa460a2007-10-31 13:53:06 +01003876 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003877 * This is the first phase of the normal nand_scan() function. It reads the
3878 * flash ID and sets up MTD fields accordingly.
William Juulcfa460a2007-10-31 13:53:06 +01003879 *
3880 * The mtd->owner field must be set to the module of the caller.
3881 */
Lei Wen245eb902011-01-06 09:48:18 +08003882int nand_scan_ident(struct mtd_info *mtd, int maxchips,
Heiko Schocherff94bc42014-06-24 10:10:04 +02003883 struct nand_flash_dev *table)
William Juulcfa460a2007-10-31 13:53:06 +01003884{
Florian Fainelli0272c712011-02-25 00:01:34 +00003885 int i, busw, nand_maf_id, nand_dev_id;
William Juulcfa460a2007-10-31 13:53:06 +01003886 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003887 struct nand_flash_dev *type;
William Juulcfa460a2007-10-31 13:53:06 +01003888
3889 /* Get buswidth to select the correct functions */
3890 busw = chip->options & NAND_BUSWIDTH_16;
3891 /* Set the default functions */
3892 nand_set_defaults(chip, busw);
3893
3894 /* Read the flash type */
Christian Hitz90e3f392011-10-12 09:32:01 +02003895 type = nand_get_flash_type(mtd, chip, busw,
3896 &nand_maf_id, &nand_dev_id, table);
William Juulcfa460a2007-10-31 13:53:06 +01003897
3898 if (IS_ERR(type)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003899 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3900 pr_warn("No NAND device found\n");
William Juulcfa460a2007-10-31 13:53:06 +01003901 chip->select_chip(mtd, -1);
3902 return PTR_ERR(type);
3903 }
3904
Heiko Schocherff94bc42014-06-24 10:10:04 +02003905 chip->select_chip(mtd, -1);
3906
William Juulcfa460a2007-10-31 13:53:06 +01003907 /* Check for a chip array */
3908 for (i = 1; i < maxchips; i++) {
3909 chip->select_chip(mtd, i);
Karl Beldan33efde52008-09-15 16:08:03 +02003910 /* See comment in nand_get_flash_type for reset */
3911 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003912 /* Send the command for reading device ID */
3913 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3914 /* Read manufacturer and device IDs */
3915 if (nand_maf_id != chip->read_byte(mtd) ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02003916 nand_dev_id != chip->read_byte(mtd)) {
3917 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003918 break;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003919 }
3920 chip->select_chip(mtd, -1);
William Juulcfa460a2007-10-31 13:53:06 +01003921 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02003922
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003923#ifdef DEBUG
William Juulcfa460a2007-10-31 13:53:06 +01003924 if (i > 1)
Heiko Schocherff94bc42014-06-24 10:10:04 +02003925 pr_info("%d chips detected\n", i);
Wolfgang Grandegger672ed2a2009-02-11 18:38:20 +01003926#endif
William Juulcfa460a2007-10-31 13:53:06 +01003927
3928 /* Store the number of chips and calc total size for mtd */
3929 chip->numchips = i;
3930 mtd->size = i * chip->chipsize;
3931
3932 return 0;
3933}
Heiko Schocherff94bc42014-06-24 10:10:04 +02003934EXPORT_SYMBOL(nand_scan_ident);
William Juulcfa460a2007-10-31 13:53:06 +01003935
3936
3937/**
3938 * nand_scan_tail - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00003939 * @mtd: MTD device structure
William Juulcfa460a2007-10-31 13:53:06 +01003940 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00003941 * This is the second phase of the normal nand_scan() function. It fills out
3942 * all the uninitialized function pointers with the defaults and scans for a
3943 * bad block table if appropriate.
William Juulcfa460a2007-10-31 13:53:06 +01003944 */
3945int nand_scan_tail(struct mtd_info *mtd)
3946{
3947 int i;
3948 struct nand_chip *chip = mtd->priv;
Heiko Schocherff94bc42014-06-24 10:10:04 +02003949 struct nand_ecc_ctrl *ecc = &chip->ecc;
William Juulcfa460a2007-10-31 13:53:06 +01003950
Sergey Lapindfe64e22013-01-14 03:46:50 +00003951 /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3952 BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3953 !(chip->bbt_options & NAND_BBT_USE_FLASH));
3954
William Juulcfa460a2007-10-31 13:53:06 +01003955 if (!(chip->options & NAND_OWN_BUFFERS))
Heiko Schocherff94bc42014-06-24 10:10:04 +02003956 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
William Juulcfa460a2007-10-31 13:53:06 +01003957 if (!chip->buffers)
3958 return -ENOMEM;
3959
3960 /* Set the internal oob buffer location, just after the page data */
3961 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
3962
3963 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003964 * If no default placement scheme is given, select an appropriate one.
William Juulcfa460a2007-10-31 13:53:06 +01003965 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003966 if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
William Juulcfa460a2007-10-31 13:53:06 +01003967 switch (mtd->oobsize) {
3968 case 8:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003969 ecc->layout = &nand_oob_8;
William Juulcfa460a2007-10-31 13:53:06 +01003970 break;
3971 case 16:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003972 ecc->layout = &nand_oob_16;
William Juulcfa460a2007-10-31 13:53:06 +01003973 break;
3974 case 64:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003975 ecc->layout = &nand_oob_64;
William Juulcfa460a2007-10-31 13:53:06 +01003976 break;
3977 case 128:
Heiko Schocherff94bc42014-06-24 10:10:04 +02003978 ecc->layout = &nand_oob_128;
William Juulcfa460a2007-10-31 13:53:06 +01003979 break;
3980 default:
Sergey Lapindfe64e22013-01-14 03:46:50 +00003981 pr_warn("No oob scheme defined for oobsize %d\n",
3982 mtd->oobsize);
Heiko Schocherff94bc42014-06-24 10:10:04 +02003983 BUG();
William Juulcfa460a2007-10-31 13:53:06 +01003984 }
3985 }
3986
3987 if (!chip->write_page)
3988 chip->write_page = nand_write_page;
3989
3990 /*
Sergey Lapindfe64e22013-01-14 03:46:50 +00003991 * Check ECC mode, default to software if 3byte/512byte hardware ECC is
William Juulcfa460a2007-10-31 13:53:06 +01003992 * selected and we have 256 byte pagesize fallback to software ECC
3993 */
William Juulcfa460a2007-10-31 13:53:06 +01003994
Heiko Schocherff94bc42014-06-24 10:10:04 +02003995 switch (ecc->mode) {
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04003996 case NAND_ECC_HW_OOB_FIRST:
3997 /* Similar to NAND_ECC_HW, but a separate read_page handle */
Heiko Schocherff94bc42014-06-24 10:10:04 +02003998 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00003999 pr_warn("No ECC functions supplied; "
4000 "hardware ECC not possible\n");
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004001 BUG();
4002 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004003 if (!ecc->read_page)
4004 ecc->read_page = nand_read_page_hwecc_oob_first;
Sandeep Paulrajf83b7f92009-08-10 13:27:56 -04004005
William Juulcfa460a2007-10-31 13:53:06 +01004006 case NAND_ECC_HW:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004007 /* Use standard hwecc read page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004008 if (!ecc->read_page)
4009 ecc->read_page = nand_read_page_hwecc;
4010 if (!ecc->write_page)
4011 ecc->write_page = nand_write_page_hwecc;
4012 if (!ecc->read_page_raw)
4013 ecc->read_page_raw = nand_read_page_raw;
4014 if (!ecc->write_page_raw)
4015 ecc->write_page_raw = nand_write_page_raw;
4016 if (!ecc->read_oob)
4017 ecc->read_oob = nand_read_oob_std;
4018 if (!ecc->write_oob)
4019 ecc->write_oob = nand_write_oob_std;
4020 if (!ecc->read_subpage)
4021 ecc->read_subpage = nand_read_subpage;
4022 if (!ecc->write_subpage)
4023 ecc->write_subpage = nand_write_subpage_hwecc;
William Juulcfa460a2007-10-31 13:53:06 +01004024
4025 case NAND_ECC_HW_SYNDROME:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004026 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4027 (!ecc->read_page ||
4028 ecc->read_page == nand_read_page_hwecc ||
4029 !ecc->write_page ||
4030 ecc->write_page == nand_write_page_hwecc)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004031 pr_warn("No ECC functions supplied; "
4032 "hardware ECC not possible\n");
William Juulcfa460a2007-10-31 13:53:06 +01004033 BUG();
4034 }
Sergey Lapindfe64e22013-01-14 03:46:50 +00004035 /* Use standard syndrome read/write page function? */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004036 if (!ecc->read_page)
4037 ecc->read_page = nand_read_page_syndrome;
4038 if (!ecc->write_page)
4039 ecc->write_page = nand_write_page_syndrome;
4040 if (!ecc->read_page_raw)
4041 ecc->read_page_raw = nand_read_page_raw_syndrome;
4042 if (!ecc->write_page_raw)
4043 ecc->write_page_raw = nand_write_page_raw_syndrome;
4044 if (!ecc->read_oob)
4045 ecc->read_oob = nand_read_oob_syndrome;
4046 if (!ecc->write_oob)
4047 ecc->write_oob = nand_write_oob_syndrome;
William Juulcfa460a2007-10-31 13:53:06 +01004048
Heiko Schocherff94bc42014-06-24 10:10:04 +02004049 if (mtd->writesize >= ecc->size) {
4050 if (!ecc->strength) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004051 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4052 BUG();
4053 }
William Juulcfa460a2007-10-31 13:53:06 +01004054 break;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004055 }
4056 pr_warn("%d byte HW ECC not possible on "
4057 "%d byte page size, fallback to SW ECC\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +02004058 ecc->size, mtd->writesize);
4059 ecc->mode = NAND_ECC_SOFT;
William Juulcfa460a2007-10-31 13:53:06 +01004060
4061 case NAND_ECC_SOFT:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004062 ecc->calculate = nand_calculate_ecc;
4063 ecc->correct = nand_correct_data;
4064 ecc->read_page = nand_read_page_swecc;
4065 ecc->read_subpage = nand_read_subpage;
4066 ecc->write_page = nand_write_page_swecc;
4067 ecc->read_page_raw = nand_read_page_raw;
4068 ecc->write_page_raw = nand_write_page_raw;
4069 ecc->read_oob = nand_read_oob_std;
4070 ecc->write_oob = nand_write_oob_std;
4071 if (!ecc->size)
4072 ecc->size = 256;
4073 ecc->bytes = 3;
4074 ecc->strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +01004075 break;
4076
Christian Hitz4c6de852011-10-12 09:31:59 +02004077 case NAND_ECC_SOFT_BCH:
4078 if (!mtd_nand_has_bch()) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004079 pr_warn("CONFIG_MTD_ECC_BCH not enabled\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004080 BUG();
Christian Hitz4c6de852011-10-12 09:31:59 +02004081 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004082 ecc->calculate = nand_bch_calculate_ecc;
4083 ecc->correct = nand_bch_correct_data;
4084 ecc->read_page = nand_read_page_swecc;
4085 ecc->read_subpage = nand_read_subpage;
4086 ecc->write_page = nand_write_page_swecc;
4087 ecc->read_page_raw = nand_read_page_raw;
4088 ecc->write_page_raw = nand_write_page_raw;
4089 ecc->read_oob = nand_read_oob_std;
4090 ecc->write_oob = nand_write_oob_std;
Christian Hitz4c6de852011-10-12 09:31:59 +02004091 /*
4092 * Board driver should supply ecc.size and ecc.bytes values to
4093 * select how many bits are correctable; see nand_bch_init()
Sergey Lapindfe64e22013-01-14 03:46:50 +00004094 * for details. Otherwise, default to 4 bits for large page
4095 * devices.
Christian Hitz4c6de852011-10-12 09:31:59 +02004096 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004097 if (!ecc->size && (mtd->oobsize >= 64)) {
4098 ecc->size = 512;
4099 ecc->bytes = 7;
Christian Hitz4c6de852011-10-12 09:31:59 +02004100 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004101 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4102 &ecc->layout);
4103 if (!ecc->priv) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004104 pr_warn("BCH ECC initialization failed!\n");
Heiko Schocherff94bc42014-06-24 10:10:04 +02004105 BUG();
4106 }
4107 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
Christian Hitz4c6de852011-10-12 09:31:59 +02004108 break;
4109
William Juulcfa460a2007-10-31 13:53:06 +01004110 case NAND_ECC_NONE:
Sergey Lapindfe64e22013-01-14 03:46:50 +00004111 pr_warn("NAND_ECC_NONE selected by board driver. "
Heiko Schocherff94bc42014-06-24 10:10:04 +02004112 "This is not recommended!\n");
4113 ecc->read_page = nand_read_page_raw;
4114 ecc->write_page = nand_write_page_raw;
4115 ecc->read_oob = nand_read_oob_std;
4116 ecc->read_page_raw = nand_read_page_raw;
4117 ecc->write_page_raw = nand_write_page_raw;
4118 ecc->write_oob = nand_write_oob_std;
4119 ecc->size = mtd->writesize;
4120 ecc->bytes = 0;
4121 ecc->strength = 0;
William Juulcfa460a2007-10-31 13:53:06 +01004122 break;
4123
4124 default:
Heiko Schocherff94bc42014-06-24 10:10:04 +02004125 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
William Juulcfa460a2007-10-31 13:53:06 +01004126 BUG();
4127 }
4128
Sergey Lapindfe64e22013-01-14 03:46:50 +00004129 /* For many systems, the standard OOB write also works for raw */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004130 if (!ecc->read_oob_raw)
4131 ecc->read_oob_raw = ecc->read_oob;
4132 if (!ecc->write_oob_raw)
4133 ecc->write_oob_raw = ecc->write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004134
William Juulcfa460a2007-10-31 13:53:06 +01004135 /*
4136 * The number of bytes available for a client to place data into
Sergey Lapindfe64e22013-01-14 03:46:50 +00004137 * the out of band area.
William Juulcfa460a2007-10-31 13:53:06 +01004138 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004139 ecc->layout->oobavail = 0;
4140 for (i = 0; ecc->layout->oobfree[i].length
4141 && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4142 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4143 mtd->oobavail = ecc->layout->oobavail;
William Juulcfa460a2007-10-31 13:53:06 +01004144
4145 /*
4146 * Set the number of read / write steps for one page depending on ECC
Sergey Lapindfe64e22013-01-14 03:46:50 +00004147 * mode.
William Juulcfa460a2007-10-31 13:53:06 +01004148 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004149 ecc->steps = mtd->writesize / ecc->size;
4150 if (ecc->steps * ecc->size != mtd->writesize) {
Sergey Lapindfe64e22013-01-14 03:46:50 +00004151 pr_warn("Invalid ECC parameters\n");
William Juulcfa460a2007-10-31 13:53:06 +01004152 BUG();
4153 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02004154 ecc->total = ecc->steps * ecc->bytes;
William Juulcfa460a2007-10-31 13:53:06 +01004155
Sergey Lapindfe64e22013-01-14 03:46:50 +00004156 /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004157 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4158 switch (ecc->steps) {
William Juulcfa460a2007-10-31 13:53:06 +01004159 case 2:
4160 mtd->subpage_sft = 1;
4161 break;
4162 case 4:
4163 case 8:
Sandeep Paulrajaad4a282009-11-07 14:24:34 -05004164 case 16:
William Juulcfa460a2007-10-31 13:53:06 +01004165 mtd->subpage_sft = 2;
4166 break;
4167 }
4168 }
4169 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4170
4171 /* Initialize state */
4172 chip->state = FL_READY;
4173
William Juulcfa460a2007-10-31 13:53:06 +01004174 /* Invalidate the pagebuffer reference */
4175 chip->pagebuf = -1;
4176
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004177 /* Large page NAND with SOFT_ECC should support subpage reads */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004178 if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
Joe Hershbergerc788ecf2012-11-05 06:46:31 +00004179 chip->options |= NAND_SUBPAGE_READ;
4180
William Juulcfa460a2007-10-31 13:53:06 +01004181 /* Fill in remaining MTD driver data */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004182 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004183 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4184 MTD_CAP_NANDFLASH;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004185 mtd->_erase = nand_erase;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004186#ifndef __UBOOT__
Sergey Lapindfe64e22013-01-14 03:46:50 +00004187 mtd->_point = NULL;
4188 mtd->_unpoint = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004189#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00004190 mtd->_read = nand_read;
4191 mtd->_write = nand_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004192 mtd->_panic_write = panic_nand_write;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004193 mtd->_read_oob = nand_read_oob;
4194 mtd->_write_oob = nand_write_oob;
4195 mtd->_sync = nand_sync;
4196 mtd->_lock = NULL;
4197 mtd->_unlock = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004198#ifndef __UBOOT__
4199 mtd->_suspend = nand_suspend;
4200 mtd->_resume = nand_resume;
4201#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +00004202 mtd->_block_isbad = nand_block_isbad;
4203 mtd->_block_markbad = nand_block_markbad;
Heiko Schocherff94bc42014-06-24 10:10:04 +02004204 mtd->writebufsize = mtd->writesize;
William Juulcfa460a2007-10-31 13:53:06 +01004205
Sergey Lapindfe64e22013-01-14 03:46:50 +00004206 /* propagate ecc info to mtd_info */
Heiko Schocherff94bc42014-06-24 10:10:04 +02004207 mtd->ecclayout = ecc->layout;
4208 mtd->ecc_strength = ecc->strength;
4209 mtd->ecc_step_size = ecc->size;
Sergey Lapindfe64e22013-01-14 03:46:50 +00004210 /*
4211 * Initialize bitflip_threshold to its default prior scan_bbt() call.
4212 * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4213 * properly set.
4214 */
4215 if (!mtd->bitflip_threshold)
4216 mtd->bitflip_threshold = mtd->ecc_strength;
William Juulcfa460a2007-10-31 13:53:06 +01004217
4218 /* Check, if we should skip the bad block table scan */
4219 if (chip->options & NAND_SKIP_BBTSCAN)
Heiko Schocherff94bc42014-06-24 10:10:04 +02004220 return 0;
William Juulcfa460a2007-10-31 13:53:06 +01004221
Heiko Schocherff94bc42014-06-24 10:10:04 +02004222 /* Build bad block table */
4223 return chip->scan_bbt(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01004224}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004225EXPORT_SYMBOL(nand_scan_tail);
4226
4227/*
4228 * is_module_text_address() isn't exported, and it's mostly a pointless
4229 * test if this is a module _anyway_ -- they'd have to try _really_ hard
4230 * to call us from in-kernel code if the core NAND support is modular.
4231 */
4232#ifdef MODULE
4233#define caller_is_module() (1)
4234#else
4235#define caller_is_module() \
4236 is_module_text_address((unsigned long)__builtin_return_address(0))
4237#endif
William Juulcfa460a2007-10-31 13:53:06 +01004238
William Juulcfa460a2007-10-31 13:53:06 +01004239/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02004240 * nand_scan - [NAND Interface] Scan for the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004241 * @mtd: MTD device structure
4242 * @maxchips: number of chips to scan for
Wolfgang Denk932394a2005-08-17 12:55:25 +02004243 *
Sergey Lapindfe64e22013-01-14 03:46:50 +00004244 * This fills out all the uninitialized function pointers with the defaults.
4245 * The flash ID is read and the mtd/chip structures are filled with the
4246 * appropriate values. The mtd->owner field must be set to the module of the
4247 * caller.
Wolfgang Denk932394a2005-08-17 12:55:25 +02004248 */
William Juulcfa460a2007-10-31 13:53:06 +01004249int nand_scan(struct mtd_info *mtd, int maxchips)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004250{
William Juulcfa460a2007-10-31 13:53:06 +01004251 int ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004252
Heiko Schocherff94bc42014-06-24 10:10:04 +02004253 /* Many callers got this wrong, so check for it for a while... */
4254 if (!mtd->owner && caller_is_module()) {
4255 pr_crit("%s called with NULL mtd->owner!\n", __func__);
4256 BUG();
4257 }
4258
Lei Wen245eb902011-01-06 09:48:18 +08004259 ret = nand_scan_ident(mtd, maxchips, NULL);
William Juulcfa460a2007-10-31 13:53:06 +01004260 if (!ret)
4261 ret = nand_scan_tail(mtd);
4262 return ret;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004263}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004264EXPORT_SYMBOL(nand_scan);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004265
Heiko Schocherff94bc42014-06-24 10:10:04 +02004266#ifndef __UBOOT__
Wolfgang Denk932394a2005-08-17 12:55:25 +02004267/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02004268 * nand_release - [NAND Interface] Free resources held by the NAND device
Sergey Lapindfe64e22013-01-14 03:46:50 +00004269 * @mtd: MTD device structure
4270 */
William Juulcfa460a2007-10-31 13:53:06 +01004271void nand_release(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02004272{
William Juulcfa460a2007-10-31 13:53:06 +01004273 struct nand_chip *chip = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +02004274
Christian Hitz4c6de852011-10-12 09:31:59 +02004275 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4276 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4277
Heiko Schocherff94bc42014-06-24 10:10:04 +02004278 mtd_device_unregister(mtd);
William Juulcfa460a2007-10-31 13:53:06 +01004279
4280 /* Free bad block table memory */
4281 kfree(chip->bbt);
4282 if (!(chip->options & NAND_OWN_BUFFERS))
4283 kfree(chip->buffers);
Christian Hitz2a8e0fc2011-10-12 09:32:02 +02004284
4285 /* Free bad block descriptor memory */
4286 if (chip->badblock_pattern && chip->badblock_pattern->options
4287 & NAND_BBT_DYNAMICSTRUCT)
4288 kfree(chip->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02004289}
Heiko Schocherff94bc42014-06-24 10:10:04 +02004290EXPORT_SYMBOL_GPL(nand_release);
4291
4292static int __init nand_base_init(void)
4293{
4294 led_trigger_register_simple("nand-disk", &nand_led_trigger);
4295 return 0;
4296}
4297
4298static void __exit nand_base_exit(void)
4299{
4300 led_trigger_unregister_simple(nand_led_trigger);
4301}
4302#endif
4303
4304module_init(nand_base_init);
4305module_exit(nand_base_exit);
4306
4307MODULE_LICENSE("GPL");
4308MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4309MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4310MODULE_DESCRIPTION("Generic NAND flash driver code");